2014-12-31 22:59:00 +00:00
|
|
|
package keccak
|
|
|
|
|
|
|
|
import (
|
2015-01-03 08:03:28 +00:00
|
|
|
"bytes"
|
2015-01-03 09:36:56 +00:00
|
|
|
"encoding/hex"
|
2015-01-03 08:03:28 +00:00
|
|
|
"hash"
|
2015-01-03 09:36:56 +00:00
|
|
|
"io"
|
2014-12-31 22:59:00 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-01-03 08:03:28 +00:00
|
|
|
var tests = []struct {
|
2015-01-08 23:09:38 +00:00
|
|
|
f func() hash.Hash
|
|
|
|
name string
|
|
|
|
text string
|
|
|
|
hash string
|
2015-01-03 08:03:28 +00:00
|
|
|
}{
|
|
|
|
{
|
2015-01-08 23:09:38 +00:00
|
|
|
f: newKeccak256,
|
|
|
|
name: "Keccak-256",
|
|
|
|
hash: "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
|
2015-01-03 08:03:28 +00:00
|
|
|
},
|
|
|
|
{
|
2015-01-08 23:09:38 +00:00
|
|
|
f: newKeccak512,
|
|
|
|
name: "Keccak-512",
|
|
|
|
hash: "0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e",
|
2015-01-03 08:03:28 +00:00
|
|
|
},
|
|
|
|
{
|
2015-01-08 23:09:38 +00:00
|
|
|
f: New256,
|
|
|
|
name: "SHA3-256",
|
|
|
|
hash: "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
|
2015-01-03 08:03:28 +00:00
|
|
|
},
|
|
|
|
{
|
2015-01-08 23:09:38 +00:00
|
|
|
f: New512,
|
|
|
|
name: "SHA3-512",
|
|
|
|
hash: "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26",
|
2015-01-03 09:36:56 +00:00
|
|
|
},
|
|
|
|
{
|
2015-01-08 23:09:38 +00:00
|
|
|
f: New256,
|
|
|
|
name: "SHA3-256",
|
|
|
|
text: "The quick brown fox jumps over the lazy dog",
|
|
|
|
hash: "69070dda01975c8c120c3aada1b282394e7f032fa9cf32f4cb2259a0897dfc04",
|
2015-01-03 08:03:28 +00:00
|
|
|
},
|
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 {
|
2015-01-08 23:09:38 +00:00
|
|
|
want, err := hex.DecodeString(tt.hash)
|
2015-01-03 09:36:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s(%q): %s", tt.name, tt.text, err)
|
|
|
|
continue
|
|
|
|
}
|
2015-01-03 08:03:28 +00:00
|
|
|
h := tt.f()
|
2015-01-03 09:36:56 +00:00
|
|
|
io.WriteString(h, tt.text)
|
2015-01-08 23:09:38 +00:00
|
|
|
got := h.Sum(nil)
|
|
|
|
if !bytes.Equal(got, want) {
|
|
|
|
t.Errorf("%s(%q) = %x, want %x", tt.name, tt.text, got, want)
|
2015-01-03 08:03:28 +00:00
|
|
|
}
|
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) {
|
2024-10-06 02:50:43 +00:00
|
|
|
var tmp [Size * 2]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])
|
2015-01-02 04:46:20 +00:00
|
|
|
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) }
|