keccak/keccak_test.go

94 lines
2.4 KiB
Go
Raw Normal View History

2014-12-31 22:59:00 +00:00
package keccak
import (
2015-01-03 08:03:28 +00:00
"bytes"
"hash"
2014-12-31 22:59:00 +00:00
"testing"
)
2015-01-03 08:03:28 +00:00
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,
},
},
2014-12-31 22:59:00 +00:00
}
2015-01-03 08:03:28 +00:00
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)
}
2015-01-01 07:19:05 +00:00
}
}
2015-01-03 08:03:28 +00:00
func benchmark(b *testing.B, f func() hash.Hash, size int64) {
2014-12-31 22:59:00 +00:00
var tmp [Size]byte
2015-01-03 08:03:28 +00:00
var msg [8192]byte
b.SetBytes(size)
h := f()
2014-12-31 23:19:23 +00:00
for i := 0; i < b.N; i++ {
2015-01-03 08:03:28 +00:00
h.Reset()
h.Write(msg[:size])
h.Sum(tmp[:0])
2014-12-31 22:59:00 +00:00
}
}
2015-01-03 08:03:28 +00:00
// 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) }