From 16d859b6d8bbe07c927e26e72dcb6e0ddd3bf5c5 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sat, 3 Jan 2015 00:03:28 -0800 Subject: [PATCH] More tests and benchmarks. --- keccak_test.go | 110 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 33 deletions(-) diff --git a/keccak_test.go b/keccak_test.go index a8f61fe..7950dbf 100644 --- a/keccak_test.go +++ b/keccak_test.go @@ -1,49 +1,93 @@ package keccak import ( - "reflect" + "bytes" + "hash" "testing" ) -var vector256 = []byte{ - 0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, - 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, - 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, - 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70, +var tests = []struct { + name string + f func() hash.Hash + vector []byte +}{ + { + name: "Keccak-256", + f: newKeccak256, + vector: []byte{ + 0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, + 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, + 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, + 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70, + }, + }, + { + name: "Keccak-512", + f: newKeccak512, + vector: []byte{ + 0x0e, 0xab, 0x42, 0xde, 0x4c, 0x3c, 0xeb, 0x92, + 0x35, 0xfc, 0x91, 0xac, 0xff, 0xe7, 0x46, 0xb2, + 0x9c, 0x29, 0xa8, 0xc3, 0x66, 0xb7, 0xc6, 0x0e, + 0x4e, 0x67, 0xc4, 0x66, 0xf3, 0x6a, 0x43, 0x04, + 0xc0, 0x0f, 0xa9, 0xca, 0xf9, 0xd8, 0x79, 0x76, + 0xba, 0x46, 0x9b, 0xcb, 0xe0, 0x67, 0x13, 0xb4, + 0x35, 0xf0, 0x91, 0xef, 0x27, 0x69, 0xfb, 0x16, + 0x0c, 0xda, 0xb3, 0x3d, 0x36, 0x70, 0x68, 0x0e, + }, + }, + { + name: "SHA3-256", + f: New256, + vector: []byte{ + 0xa7, 0xff, 0xc6, 0xf8, 0xbf, 0x1e, 0xd7, 0x66, + 0x51, 0xc1, 0x47, 0x56, 0xa0, 0x61, 0xd6, 0x62, + 0xf5, 0x80, 0xff, 0x4d, 0xe4, 0x3b, 0x49, 0xfa, + 0x82, 0xd8, 0x0a, 0x4b, 0x80, 0xf8, 0x43, 0x4a, + }, + }, + { + name: "SHA3-512", + f: New512, + vector: []byte{ + 0xa6, 0x9f, 0x73, 0xcc, 0xa2, 0x3a, 0x9a, 0xc5, + 0xc8, 0xb5, 0x67, 0xdc, 0x18, 0x5a, 0x75, 0x6e, + 0x97, 0xc9, 0x82, 0x16, 0x4f, 0xe2, 0x58, 0x59, + 0xe0, 0xd1, 0xdc, 0xc1, 0x47, 0x5c, 0x80, 0xa6, + 0x15, 0xb2, 0x12, 0x3a, 0xf1, 0xf5, 0xf9, 0x4c, + 0x11, 0xe3, 0xe9, 0x40, 0x2c, 0x3a, 0xc5, 0x58, + 0xf5, 0x00, 0x19, 0x9d, 0x95, 0xb6, 0xd3, 0xe3, + 0x01, 0x75, 0x85, 0x86, 0x28, 0x1d, 0xcd, 0x26, + }, + }, } -var vector512 = []byte{ - 0x0e, 0xab, 0x42, 0xde, 0x4c, 0x3c, 0xeb, 0x92, - 0x35, 0xfc, 0x91, 0xac, 0xff, 0xe7, 0x46, 0xb2, - 0x9c, 0x29, 0xa8, 0xc3, 0x66, 0xb7, 0xc6, 0x0e, - 0x4e, 0x67, 0xc4, 0x66, 0xf3, 0x6a, 0x43, 0x04, - 0xc0, 0x0f, 0xa9, 0xca, 0xf9, 0xd8, 0x79, 0x76, - 0xba, 0x46, 0x9b, 0xcb, 0xe0, 0x67, 0x13, 0xb4, - 0x35, 0xf0, 0x91, 0xef, 0x27, 0x69, 0xfb, 0x16, - 0x0c, 0xda, 0xb3, 0x3d, 0x36, 0x70, 0x68, 0x0e, -} - -func TestKeccak256(t *testing.T) { - h := newKeccak256() - sum := h.Sum(nil) - if !reflect.DeepEqual(sum, vector256) { - t.Errorf("Keccak-256(\"\"): want % x, got % x", vector256, sum) +func TestHash(t *testing.T) { + for _, tt := range tests { + h := tt.f() + sum := h.Sum(nil) + if !bytes.Equal(sum, tt.vector) { + t.Errorf("%s(\"\"): want %x, got %x", tt.name, tt.vector, sum) + } } } -func TestKeccak512(t *testing.T) { - h := newKeccak512() - sum := h.Sum(nil) - if !reflect.DeepEqual(sum, vector512) { - t.Errorf("Keccak-512(\"\"): want % x, got % x", vector512, sum) - } -} - -func Benchmark256(b *testing.B) { +func benchmark(b *testing.B, f func() hash.Hash, size int64) { var tmp [Size]byte - h := New256() - b.SetBytes(BlockSize) + var msg [8192]byte + b.SetBytes(size) + h := f() for i := 0; i < b.N; i++ { + h.Reset() + h.Write(msg[:size]) h.Sum(tmp[:0]) } } + +// Benchmark the Keccak-f permutation function +func Benchmark256_8(b *testing.B) { benchmark(b, New256, 8) } +func Benchmark256_1k(b *testing.B) { benchmark(b, New256, 1024) } +func Benchmark256_8k(b *testing.B) { benchmark(b, New256, 8192) } + +func Benchmark512_8(b *testing.B) { benchmark(b, New512, 8) } +func Benchmark512_1k(b *testing.B) { benchmark(b, New512, 1024) } +func Benchmark512_8k(b *testing.B) { benchmark(b, New512, 8192) }