1 package lattice
2 3 import "testing"
4 5 // HashKey must reproduce moxie's SipHash-2-4-128, which is not standard
6 // SipHash-2-4: v1 is XORed with 0xee at initialization, and the first
7 // finalization XORs 0xee into v2 instead of 0xff. The six vectors below are the
8 // low 64 bits under moxie's default key (k0 0x736f6d6570736575, k1
9 // 0x646f72616e646f6d), computed from that source's control flow in an
10 // independent implementation (Python, arbitrary precision, separate tail
11 // handling) rather than from this code. If HashKey is ever replaced with a
12 // library's Sum64 or Sum128 these fail on the first case.
13 14 func TestHashKeyVectors(t *testing.T) {
15 cases := []struct {
16 msg string
17 want uint64
18 }{
19 {"", 0xf4dcc78ed07a0282},
20 {"a", 0x9a5a64d6976b8d51},
21 {"hello", 0x7c9b637d8554a1e4},
22 {"\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0x9cae4d8e6a407542},
23 }
24 for _, c := range cases {
25 if got := uint64(HashKey([]byte(c.msg))); got != c.want {
26 t.Fatalf("HashKey(%q) = %016x, want %016x", c.msg, got, c.want)
27 }
28 }
29 }
30 31 // A zero-key SipHash-2-4 port, written on first principles, is not what
32 // HashKey is. If this ever agrees with HashKey on "a", the 0xee init XOR has
33 // been lost.
34 func TestHashKeyDiffersFromZeroKeySipHash(t *testing.T) {
35 if uint64(HashKey([]byte("a"))) == 0x8b5a0baa49fbc58d {
36 t.Fatal("HashKey matches zero-key SipHash-2-4 of \"a\"; the moxie init/finalization XORs are missing")
37 }
38 }
39