package lattice import "testing" // HashKey must reproduce moxie's SipHash-2-4-128, which is not standard // SipHash-2-4: v1 is XORed with 0xee at initialization, and the first // finalization XORs 0xee into v2 instead of 0xff. The six vectors below are the // low 64 bits under moxie's default key (k0 0x736f6d6570736575, k1 // 0x646f72616e646f6d), computed from that source's control flow in an // independent implementation (Python, arbitrary precision, separate tail // handling) rather than from this code. If HashKey is ever replaced with a // library's Sum64 or Sum128 these fail on the first case. func TestHashKeyVectors(t *testing.T) { cases := []struct { msg string want uint64 }{ {"", 0xf4dcc78ed07a0282}, {"a", 0x9a5a64d6976b8d51}, {"hello", 0x7c9b637d8554a1e4}, {"\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0x9cae4d8e6a407542}, } for _, c := range cases { if got := uint64(HashKey([]byte(c.msg))); got != c.want { t.Fatalf("HashKey(%q) = %016x, want %016x", c.msg, got, c.want) } } } // A zero-key SipHash-2-4 port, written on first principles, is not what // HashKey is. If this ever agrees with HashKey on "a", the 0xee init XOR has // been lost. func TestHashKeyDiffersFromZeroKeySipHash(t *testing.T) { if uint64(HashKey([]byte("a"))) == 0x8b5a0baa49fbc58d { t.Fatal("HashKey matches zero-key SipHash-2-4 of \"a\"; the moxie init/finalization XORs are missing") } }