moved XOR keyspace -> util
Juan Batiz-Benet committed
Oct 18, 2014 at 05:52 UTC
20d1d354f3489e1f1a2f2f8de656957e5c3b2c96
4 files changed
+42
-38
routing/keyspace/xor.go
+3
-10
@@ -4,6 +4,8 @@ import (
4
"bytes"
5
"crypto/sha256"
6
"math/big"
7
+
8
+ u "github.com/jbenet/go-ipfs/util"
9
)
10
11
// XORKeySpace is a KeySpace which:
@@ -33,7 +35,7 @@ func (s *xorKeySpace) Equal(k1, k2 Key) bool {
35
// Distance returns the distance metric in this key space
36
func (s *xorKeySpace) Distance(k1, k2 Key) *big.Int {
37
// XOR the keys
36
- k3 := XOR(k1.Bytes, k2.Bytes)
38
+ k3 := u.XOR(k1.Bytes, k2.Bytes)
39
40
// interpret it as an integer
41
dist := big.NewInt(0).SetBytes(k3)
@@ -52,15 +54,6 @@ func (s *xorKeySpace) Less(k1, k2 Key) bool {
54
return true
55
}
56
55
-// XOR takes two byte slices, XORs them together, returns the resulting slice.
56
-func XOR(a, b []byte) []byte {
57
- c := make([]byte, len(a))
58
- for i := 0; i < len(a); i++ {
59
- c[i] = a[i] ^ b[i]
60
- }
61
- return c
62
-}
63
-
57
// ZeroPrefixLen returns the number of consecutive zeroes in a byte slice.
58
func ZeroPrefixLen(id []byte) int {
59
for i := 0; i < len(id); i++ {
routing/keyspace/xor_test.go
+3
-28
@@ -4,34 +4,9 @@ import (
4
"bytes"
5
"math/big"
6
"testing"
7
-)
8
-
9
-func TestXOR(t *testing.T) {
10
- cases := [][3][]byte{
11
- [3][]byte{
12
- []byte{0xFF, 0xFF, 0xFF},
13
- []byte{0xFF, 0xFF, 0xFF},
14
- []byte{0x00, 0x00, 0x00},
15
- },
16
- [3][]byte{
17
- []byte{0x00, 0xFF, 0x00},
18
- []byte{0xFF, 0xFF, 0xFF},
19
- []byte{0xFF, 0x00, 0xFF},
20
- },
21
- [3][]byte{
22
- []byte{0x55, 0x55, 0x55},
23
- []byte{0x55, 0xFF, 0xAA},
24
- []byte{0x00, 0xAA, 0xFF},
25
- },
26
- }
7
28
- for _, c := range cases {
29
- r := XOR(c[0], c[1])
30
- if !bytes.Equal(r, c[2]) {
31
- t.Error("XOR failed")
32
- }
33
- }
34
-}
8
+ u "github.com/jbenet/go-ipfs/util"
9
+)
10
11
func TestPrefixLen(t *testing.T) {
12
cases := [][]byte{
@@ -126,7 +101,7 @@ func TestDistancesAndCenterSorting(t *testing.T) {
101
}
102
103
d1 := keys[2].Distance(keys[5])
129
- d2 := XOR(keys[2].Bytes, keys[5].Bytes)
104
+ d2 := u.XOR(keys[2].Bytes, keys[5].Bytes)
105
d2 = d2[len(keys[2].Bytes)-len(d1.Bytes()):] // skip empty space for big
106
if !bytes.Equal(d1.Bytes(), d2) {
107
t.Errorf("bytes should be the same. %v == %v", d1.Bytes(), d2)
util/key.go
+9
@@ -71,3 +71,12 @@ func IsValidHash(s string) bool {
71
}
72
return true
73
}
74
+
75
+// XOR takes two byte slices, XORs them together, returns the resulting slice.
76
+func XOR(a, b []byte) []byte {
77
+ c := make([]byte, len(a))
78
+ for i := 0; i < len(a); i++ {
79
+ c[i] = a[i] ^ b[i]
80
+ }
81
+ return c
82
+}
util/util_test.go
+27
@@ -58,3 +58,30 @@ func TestByteChanReader(t *testing.T) {
58
t.Fatal("Reader failed to stream correct bytes")
59
}
60
}
61
+
62
+func TestXOR(t *testing.T) {
63
+ cases := [][3][]byte{
64
+ [3][]byte{
65
+ []byte{0xFF, 0xFF, 0xFF},
66
+ []byte{0xFF, 0xFF, 0xFF},
67
+ []byte{0x00, 0x00, 0x00},
68
+ },
69
+ [3][]byte{
70
+ []byte{0x00, 0xFF, 0x00},
71
+ []byte{0xFF, 0xFF, 0xFF},
72
+ []byte{0xFF, 0x00, 0xFF},
73
+ },
74
+ [3][]byte{
75
+ []byte{0x55, 0x55, 0x55},
76
+ []byte{0x55, 0xFF, 0xAA},
77
+ []byte{0x00, 0xAA, 0xFF},
78
+ },
79
+ }
80
+
81
+ for _, c := range cases {
82
+ r := XOR(c[0], c[1])
83
+ if !bytes.Equal(r, c[2]) {
84
+ t.Error("XOR failed")
85
+ }
86
+ }
87
+}