github.com/cloudflare/circl@v1.5.0/xof/k12/k12_test.go (about) 1 package k12 2 3 import ( 4 "encoding/hex" 5 "testing" 6 ) 7 8 // See draft-irtf-cfrg-kangarootwelve-10 ยง4. 9 // https://datatracker.ietf.org/doc/draft-irtf-cfrg-kangarootwelve/10/ 10 func ptn(n int) []byte { 11 buf := make([]byte, n) 12 for i := 0; i < n; i++ { 13 buf[i] = byte(i % 0xfb) 14 } 15 return buf 16 } 17 18 func testK12(t *testing.T, msg []byte, c []byte, l int, want string) { 19 do := func(lanes byte, writeSize int) { 20 h := newDraft10(c, lanes) 21 msg2 := msg 22 for len(msg2) > 0 { 23 to := writeSize 24 if len(msg2) < to { 25 to = len(msg2) 26 } 27 _, _ = h.Write(msg2[:to]) 28 msg2 = msg2[to:] 29 } 30 buf := make([]byte, l) 31 _, _ = h.Read(buf) 32 got := hex.EncodeToString(buf) 33 if want != got { 34 t.Fatalf("%s != %s (lanes=%d, writeSize=%d )", want, got, lanes, writeSize) 35 } 36 } 37 38 for _, lanes := range []byte{1, 2, 4} { 39 for _, writeSize := range []int{7919, 1024, 8 * 1024} { 40 do(lanes, writeSize) 41 } 42 } 43 } 44 45 func TestK12(t *testing.T) { 46 // I-D test vectors 47 testK12(t, []byte{}, []byte{}, 32, "1ac2d450fc3b4205d19da7bfca1b37513c0803577ac7167f06fe2ce1f0ef39e5") 48 i := 17 49 testK12(t, ptn(i), []byte{}, 32, "6bf75fa2239198db4772e36478f8e19b0f371205f6a9a93a273f51df37122888") 50 i *= 17 51 testK12(t, ptn(i), []byte{}, 32, "0c315ebcdedbf61426de7dcf8fb725d1e74675d7f5327a5067f367b108ecb67c") 52 i *= 17 53 testK12(t, ptn(i), []byte{}, 32, "cb552e2ec77d9910701d578b457ddf772c12e322e4ee7fe417f92c758f0d59d0") 54 i *= 17 55 testK12(t, ptn(i), []byte{}, 32, "8701045e22205345ff4dda05555cbb5c3af1a771c2b89baef37db43d9998b9fe") 56 i *= 17 57 testK12(t, ptn(i), []byte{}, 32, "844d610933b1b9963cbdeb5ae3b6b05cc7cbd67ceedf883eb678a0a8e0371682") 58 i *= 17 59 testK12(t, ptn(i), []byte{}, 32, "3c390782a8a4e89fa6367f72feaaf13255c8d95878481d3cd8ce85f58e880af8") 60 testK12(t, []byte{}, ptn(1), 32, "fab658db63e94a246188bf7af69a133045f46ee984c56e3c3328caaf1aa1a583") 61 testK12(t, []byte{0xff}, ptn(41), 32, "d848c5068ced736f4462159b9867fd4c20b808acc3d5bc48e0b06ba0a3762ec4") 62 testK12(t, []byte{0xff, 0xff, 0xff}, ptn(41*41), 32, "c389e5009ae57120854c2e8c64670ac01358cf4c1baf89447a724234dc7ced74") 63 testK12(t, []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, ptn(41*41*41), 32, "75d2f86a2e644566726b4fbcfc5657b9dbcf070c7b0dca06450ab291d7443bcf") 64 65 // Cornercases 66 testK12(t, ptn(chunkSize), []byte{}, 16, "48f256f6772f9edfb6a8b661ec92dc93") 67 testK12(t, ptn(chunkSize+1), []byte{}, 16, "bb66fe72eaea5179418d5295ee134485") 68 testK12(t, ptn(2*chunkSize), []byte{}, 16, "82778f7f7234c83352e76837b721fbdb") 69 testK12(t, ptn(2*chunkSize+1), []byte{}, 16, "5f8d2b943922b451842b4e82740d0236") 70 testK12(t, ptn(3*chunkSize), []byte{}, 16, "f4082a8fe7d1635aa042cd1da63bf235") 71 testK12(t, ptn(3*chunkSize+1), []byte{}, 16, "38cb940999aca742d69dd79298c6051c") 72 } 73 74 func BenchmarkK12_100B(b *testing.B) { benchmarkK12(b, 100, 1) } 75 func BenchmarkK12_10K(b *testing.B) { benchmarkK12(b, 10000, 1) } 76 func BenchmarkK12_100K(b *testing.B) { benchmarkK12(b, 10000, 10) } 77 func BenchmarkK12_1M(b *testing.B) { benchmarkK12(b, 10000, 100) } 78 func BenchmarkK12_10M(b *testing.B) { benchmarkK12(b, 10000, 1000) } 79 80 func benchmarkK12(b *testing.B, size, num int) { 81 b.StopTimer() 82 h := NewDraft10([]byte{}) 83 data := make([]byte, size) 84 d := make([]byte, 32) 85 86 b.SetBytes(int64(size * num)) 87 b.StartTimer() 88 89 for i := 0; i < b.N; i++ { 90 h.Reset() 91 for j := 0; j < num; j++ { 92 _, _ = h.Write(data) 93 } 94 _, _ = h.Read(d) 95 } 96 }