siphash_test.mx raw
1 package siphash
2
3 import "testing"
4
5 // Reference test vectors from the SipHash paper (Aumasson & Bernstein 2012).
6 // Key: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
7 // Input: 00 01 02 ... (incremental bytes)
8 var testKey = Key{0x0706050403020100, 0x0f0e0d0c0b0a0908}
9
10 // 64-bit test vector: SipHash-2-4("", key) from the reference appendix.
11 func TestSum64Empty(t *testing.T) {
12 got := Sum64(testKey, []byte{})
13 // Reference vector for empty input.
14 want := uint64(0x726fdb47dd0e0e31)
15 if got != want {
16 t.Fatalf("Sum64(\"\") = %016x, want %016x", got, want)
17 }
18 }
19
20 // 64-bit test vector: input = [0x00] (single zero byte).
21 func TestSum64OneByte(t *testing.T) {
22 got := Sum64(testKey, []byte{0x00})
23 want := uint64(0x74f839c593dc67fd)
24 if got != want {
25 t.Fatalf("Sum64([0x00]) = %016x, want %016x", got, want)
26 }
27 }
28
29 // 64-bit test vectors for n=8 and n=16.
30 func TestSum64Eight(t *testing.T) {
31 p := []byte{0, 1, 2, 3, 4, 5, 6, 7}
32 got := Sum64(testKey, p)
33 want := uint64(0x93f5f5799a932462) // reference vector n=8
34 if got != want {
35 t.Fatalf("Sum64(0..7) = %016x, want %016x", got, want)
36 }
37 }
38
39 func TestSum64Sixteen(t *testing.T) {
40 p := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
41 got := Sum64(testKey, p)
42 want := uint64(0x3f2acc7f57c29bdb) // reference vector n=16
43 if got != want {
44 t.Fatalf("Sum64(0..15) = %016x, want %016x", got, want)
45 }
46 }
47
48 // Verify Sum128 produces distinct lo/hi values.
49 func TestSum128Distinct(t *testing.T) {
50 p := []byte("hello world")
51 r := Sum128(testKey, p)
52 if r[0] == r[1] {
53 t.Fatal("Sum128 lo == hi, suspicious")
54 }
55 if r[0] == 0 || r[1] == 0 {
56 t.Fatal("Sum128 has zero half")
57 }
58 }
59
60 // Verify domain separation: different prefixes produce different keys.
61 func TestSum128DomainSep(t *testing.T) {
62 word := []byte("cat")
63 en := append([]byte{0x01}, word...)
64 ja := append([]byte{0x02}, word...)
65 rEN := Sum128(testKey, en)
66 rJA := Sum128(testKey, ja)
67 if rEN == rJA {
68 t.Fatal("domain separation failed: EN and JA keys identical")
69 }
70 }
71
72 // Verify determinism.
73 func TestSum128Deterministic(t *testing.T) {
74 p := []byte("東京")
75 a := Sum128(testKey, p)
76 b := Sum128(testKey, p)
77 if a != b {
78 t.Fatal("Sum128 not deterministic")
79 }
80 }
81