提交 20d1d354 作者: Juan Batiz-Benet

moved XOR keyspace -> util

上级 3ab3170a
...@@ -4,6 +4,8 @@ import ( ...@@ -4,6 +4,8 @@ import (
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
"math/big" "math/big"
u "github.com/jbenet/go-ipfs/util"
) )
// XORKeySpace is a KeySpace which: // XORKeySpace is a KeySpace which:
...@@ -33,7 +35,7 @@ func (s *xorKeySpace) Equal(k1, k2 Key) bool { ...@@ -33,7 +35,7 @@ func (s *xorKeySpace) Equal(k1, k2 Key) bool {
// Distance returns the distance metric in this key space // Distance returns the distance metric in this key space
func (s *xorKeySpace) Distance(k1, k2 Key) *big.Int { func (s *xorKeySpace) Distance(k1, k2 Key) *big.Int {
// XOR the keys // XOR the keys
k3 := XOR(k1.Bytes, k2.Bytes) k3 := u.XOR(k1.Bytes, k2.Bytes)
// interpret it as an integer // interpret it as an integer
dist := big.NewInt(0).SetBytes(k3) dist := big.NewInt(0).SetBytes(k3)
...@@ -52,15 +54,6 @@ func (s *xorKeySpace) Less(k1, k2 Key) bool { ...@@ -52,15 +54,6 @@ func (s *xorKeySpace) Less(k1, k2 Key) bool {
return true 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. // ZeroPrefixLen returns the number of consecutive zeroes in a byte slice.
func ZeroPrefixLen(id []byte) int { func ZeroPrefixLen(id []byte) int {
for i := 0; i < len(id); i++ { for i := 0; i < len(id); i++ {
......
...@@ -4,34 +4,9 @@ import ( ...@@ -4,34 +4,9 @@ import (
"bytes" "bytes"
"math/big" "math/big"
"testing" "testing"
)
func TestXOR(t *testing.T) { u "github.com/jbenet/go-ipfs/util"
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")
}
}
}
func TestPrefixLen(t *testing.T) { func TestPrefixLen(t *testing.T) {
cases := [][]byte{ cases := [][]byte{
...@@ -126,7 +101,7 @@ func TestDistancesAndCenterSorting(t *testing.T) { ...@@ -126,7 +101,7 @@ func TestDistancesAndCenterSorting(t *testing.T) {
} }
d1 := keys[2].Distance(keys[5]) 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 d2 = d2[len(keys[2].Bytes)-len(d1.Bytes()):] // skip empty space for big
if !bytes.Equal(d1.Bytes(), d2) { if !bytes.Equal(d1.Bytes(), d2) {
t.Errorf("bytes should be the same. %v == %v", d1.Bytes(), d2) t.Errorf("bytes should be the same. %v == %v", d1.Bytes(), d2)
......
...@@ -71,3 +71,12 @@ func IsValidHash(s string) bool { ...@@ -71,3 +71,12 @@ func IsValidHash(s string) bool {
} }
return true 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
}
...@@ -58,3 +58,30 @@ func TestByteChanReader(t *testing.T) { ...@@ -58,3 +58,30 @@ func TestByteChanReader(t *testing.T) {
t.Fatal("Reader failed to stream correct bytes") 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论