Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
20d1d354
提交
20d1d354
authored
10月 18, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
moved XOR keyspace -> util
上级
3ab3170a
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
42 行增加
和
38 行删除
+42
-38
xor.go
routing/keyspace/xor.go
+3
-10
xor_test.go
routing/keyspace/xor_test.go
+3
-28
key.go
util/key.go
+9
-0
util_test.go
util/util_test.go
+27
-0
没有找到文件。
routing/keyspace/xor.go
浏览文件 @
20d1d354
...
...
@@ -4,6 +4,8 @@ import (
"bytes"
"crypto/sha256"
"math/big"
u
"github.com/jbenet/go-ipfs/util"
)
// XORKeySpace is a KeySpace which:
...
...
@@ -33,7 +35,7 @@ func (s *xorKeySpace) Equal(k1, k2 Key) bool {
// Distance returns the distance metric in this key space
func
(
s
*
xorKeySpace
)
Distance
(
k1
,
k2
Key
)
*
big
.
Int
{
// XOR the keys
k3
:=
XOR
(
k1
.
Bytes
,
k2
.
Bytes
)
k3
:=
u
.
XOR
(
k1
.
Bytes
,
k2
.
Bytes
)
// interpret it as an integer
dist
:=
big
.
NewInt
(
0
)
.
SetBytes
(
k3
)
...
...
@@ -52,15 +54,6 @@ func (s *xorKeySpace) Less(k1, k2 Key) bool {
return
true
}
// XOR takes two byte slices, XORs them together, returns the resulting slice.
func
XOR
(
a
,
b
[]
byte
)
[]
byte
{
c
:=
make
([]
byte
,
len
(
a
))
for
i
:=
0
;
i
<
len
(
a
);
i
++
{
c
[
i
]
=
a
[
i
]
^
b
[
i
]
}
return
c
}
// ZeroPrefixLen returns the number of consecutive zeroes in a byte slice.
func
ZeroPrefixLen
(
id
[]
byte
)
int
{
for
i
:=
0
;
i
<
len
(
id
);
i
++
{
...
...
routing/keyspace/xor_test.go
浏览文件 @
20d1d354
...
...
@@ -4,34 +4,9 @@ import (
"bytes"
"math/big"
"testing"
)
func
TestXOR
(
t
*
testing
.
T
)
{
cases
:=
[][
3
][]
byte
{
[
3
][]
byte
{
[]
byte
{
0xFF
,
0xFF
,
0xFF
},
[]
byte
{
0xFF
,
0xFF
,
0xFF
},
[]
byte
{
0x00
,
0x00
,
0x00
},
},
[
3
][]
byte
{
[]
byte
{
0x00
,
0xFF
,
0x00
},
[]
byte
{
0xFF
,
0xFF
,
0xFF
},
[]
byte
{
0xFF
,
0x00
,
0xFF
},
},
[
3
][]
byte
{
[]
byte
{
0x55
,
0x55
,
0x55
},
[]
byte
{
0x55
,
0xFF
,
0xAA
},
[]
byte
{
0x00
,
0xAA
,
0xFF
},
},
}
for
_
,
c
:=
range
cases
{
r
:=
XOR
(
c
[
0
],
c
[
1
])
if
!
bytes
.
Equal
(
r
,
c
[
2
])
{
t
.
Error
(
"XOR failed"
)
}
}
}
u
"github.com/jbenet/go-ipfs/util"
)
func
TestPrefixLen
(
t
*
testing
.
T
)
{
cases
:=
[][]
byte
{
...
...
@@ -126,7 +101,7 @@ func TestDistancesAndCenterSorting(t *testing.T) {
}
d1
:=
keys
[
2
]
.
Distance
(
keys
[
5
])
d2
:=
XOR
(
keys
[
2
]
.
Bytes
,
keys
[
5
]
.
Bytes
)
d2
:=
u
.
XOR
(
keys
[
2
]
.
Bytes
,
keys
[
5
]
.
Bytes
)
d2
=
d2
[
len
(
keys
[
2
]
.
Bytes
)
-
len
(
d1
.
Bytes
())
:
]
// skip empty space for big
if
!
bytes
.
Equal
(
d1
.
Bytes
(),
d2
)
{
t
.
Errorf
(
"bytes should be the same. %v == %v"
,
d1
.
Bytes
(),
d2
)
...
...
util/key.go
浏览文件 @
20d1d354
...
...
@@ -71,3 +71,12 @@ func IsValidHash(s string) bool {
}
return
true
}
// XOR takes two byte slices, XORs them together, returns the resulting slice.
func
XOR
(
a
,
b
[]
byte
)
[]
byte
{
c
:=
make
([]
byte
,
len
(
a
))
for
i
:=
0
;
i
<
len
(
a
);
i
++
{
c
[
i
]
=
a
[
i
]
^
b
[
i
]
}
return
c
}
util/util_test.go
浏览文件 @
20d1d354
...
...
@@ -58,3 +58,30 @@ func TestByteChanReader(t *testing.T) {
t
.
Fatal
(
"Reader failed to stream correct bytes"
)
}
}
func
TestXOR
(
t
*
testing
.
T
)
{
cases
:=
[][
3
][]
byte
{
[
3
][]
byte
{
[]
byte
{
0xFF
,
0xFF
,
0xFF
},
[]
byte
{
0xFF
,
0xFF
,
0xFF
},
[]
byte
{
0x00
,
0x00
,
0x00
},
},
[
3
][]
byte
{
[]
byte
{
0x00
,
0xFF
,
0x00
},
[]
byte
{
0xFF
,
0xFF
,
0xFF
},
[]
byte
{
0xFF
,
0x00
,
0xFF
},
},
[
3
][]
byte
{
[]
byte
{
0x55
,
0x55
,
0x55
},
[]
byte
{
0x55
,
0xFF
,
0xAA
},
[]
byte
{
0x00
,
0xAA
,
0xFF
},
},
}
for
_
,
c
:=
range
cases
{
r
:=
XOR
(
c
[
0
],
c
[
1
])
if
!
bytes
.
Equal
(
r
,
c
[
2
])
{
t
.
Error
(
"XOR failed"
)
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论