package siphash import "testing" // Reference test vectors from the SipHash paper (Aumasson & Bernstein 2012). // Key: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f // Input: 00 01 02 ... (incremental bytes) var testKey = Key{0x0706050403020100, 0x0f0e0d0c0b0a0908} // 64-bit test vector: SipHash-2-4("", key) from the reference appendix. func TestSum64Empty(t *testing.T) { got := Sum64(testKey, []byte{}) // Reference vector for empty input. want := uint64(0x726fdb47dd0e0e31) if got != want { t.Fatalf("Sum64(\"\") = %016x, want %016x", got, want) } } // 64-bit test vector: input = [0x00] (single zero byte). func TestSum64OneByte(t *testing.T) { got := Sum64(testKey, []byte{0x00}) want := uint64(0x74f839c593dc67fd) if got != want { t.Fatalf("Sum64([0x00]) = %016x, want %016x", got, want) } } // 64-bit test vectors for n=8 and n=16. func TestSum64Eight(t *testing.T) { p := []byte{0, 1, 2, 3, 4, 5, 6, 7} got := Sum64(testKey, p) want := uint64(0x93f5f5799a932462) // reference vector n=8 if got != want { t.Fatalf("Sum64(0..7) = %016x, want %016x", got, want) } } func TestSum64Sixteen(t *testing.T) { p := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} got := Sum64(testKey, p) want := uint64(0x3f2acc7f57c29bdb) // reference vector n=16 if got != want { t.Fatalf("Sum64(0..15) = %016x, want %016x", got, want) } } // Verify Sum128 produces distinct lo/hi values. func TestSum128Distinct(t *testing.T) { p := []byte("hello world") r := Sum128(testKey, p) if r[0] == r[1] { t.Fatal("Sum128 lo == hi, suspicious") } if r[0] == 0 || r[1] == 0 { t.Fatal("Sum128 has zero half") } } // Verify domain separation: different prefixes produce different keys. func TestSum128DomainSep(t *testing.T) { word := []byte("cat") en := append([]byte{0x01}, word...) ja := append([]byte{0x02}, word...) rEN := Sum128(testKey, en) rJA := Sum128(testKey, ja) if rEN == rJA { t.Fatal("domain separation failed: EN and JA keys identical") } } // Verify determinism. func TestSum128Deterministic(t *testing.T) { p := []byte("東京") a := Sum128(testKey, p) b := Sum128(testKey, p) if a != b { t.Fatal("Sum128 not deterministic") } }