From 79b27a1530135dcf63a0531f7bad69539983039b Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sun, 6 Oct 2024 00:40:46 -0700 Subject: [PATCH] add a few more test vectors for SHA-256 these test some boundary conditions for an optimization i'm about to do. computed using https://emn178.github.io/online-tools/sha3_256.html --- keccak_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/keccak_test.go b/keccak_test.go index 3e8d0ae..128a064 100644 --- a/keccak_test.go +++ b/keccak_test.go @@ -40,6 +40,31 @@ var tests = []struct { text: "The quick brown fox jumps over the lazy dog", hash: "69070dda01975c8c120c3aada1b282394e7f032fa9cf32f4cb2259a0897dfc04", }, + + { + f: New256, + name: "SHA3-256", + text: "a", + hash: "80084bf2fba02475726feb2cab2d8215eab14bc6bdd8bfb2c8151257032ecd8b", + }, + { + f: New256, + name: "SHA3-256", + text: "abcdefg", + hash: "7d55114476dfc6a2fbeaa10e221a8d0f32fc8f2efb69a6e878f4633366917a62", + }, + { + f: New256, + name: "SHA3-256", + text: "abcdefgh", + hash: "3e2020725a38a48eb3bbf75767f03a22c6b3f41f459c831309b06433ec649779", + }, + { + f: New256, + name: "SHA3-256", + text: "abcdefghi", + hash: "f74eb337992307c22bc59eb43e59583a683f3b93077e7f2472508e8c464d2657", + }, } func TestHash(t *testing.T) { @@ -58,6 +83,24 @@ func TestHash(t *testing.T) { } } +func TestHashSmallWrites(t *testing.T) { + for _, tt := range tests { + want, err := hex.DecodeString(tt.hash) + if err != nil { + t.Errorf("%s(%q): %s", tt.name, tt.text, err) + continue + } + h := tt.f() + for i := range []byte(tt.text) { + io.WriteString(h, tt.text[i:i+1]) + } + got := h.Sum(nil) + if !bytes.Equal(got, want) { + t.Errorf("%s(%q) = %x, want %x", tt.name, tt.text, got, want) + } + } +} + func benchmark(b *testing.B, f func() hash.Hash, size int64) { var tmp [Size * 2]byte var msg [8192]byte