Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kbucket
Commits
346c167f
Commit
346c167f
authored
Oct 18, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved XOR keyspace -> util
parent
706732d0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
38 deletions
+6
-38
xor.go
xor.go
+3
-10
xor_test.go
xor_test.go
+3
-28
No files found.
xor.go
View file @
346c167f
...
...
@@ -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
++
{
...
...
xor_test.go
View file @
346c167f
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment