Commit 346c167f authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

moved XOR keyspace -> util

parent 706732d0
......@@ -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++ {
......
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment