2020-01-09 20:10:01 +00:00
|
|
|
package blake3_test
|
|
|
|
|
|
|
|
import (
|
2020-01-09 21:25:24 +00:00
|
|
|
"bytes"
|
2020-01-09 20:10:01 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2020-01-09 21:25:24 +00:00
|
|
|
"io"
|
2020-01-09 20:10:01 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"lukechampine.com/blake3"
|
|
|
|
)
|
|
|
|
|
2020-01-09 22:58:48 +00:00
|
|
|
func toHex(data []byte) string { return hex.EncodeToString(data) }
|
2020-01-09 20:10:01 +00:00
|
|
|
|
2020-01-10 17:43:02 +00:00
|
|
|
var testVectors = func() (vecs struct {
|
|
|
|
Key string
|
|
|
|
Cases []struct {
|
|
|
|
InputLen int `json:"input_len"`
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
KeyedHash string `json:"keyed_hash"`
|
|
|
|
DeriveKey string `json:"derive_key"`
|
|
|
|
}
|
|
|
|
}) {
|
2020-01-09 20:10:01 +00:00
|
|
|
data, err := ioutil.ReadFile("testdata/vectors.json")
|
|
|
|
if err != nil {
|
2020-01-10 17:43:02 +00:00
|
|
|
panic(err)
|
2020-01-09 20:10:01 +00:00
|
|
|
}
|
2020-01-10 17:43:02 +00:00
|
|
|
if err := json.Unmarshal(data, &vecs); err != nil {
|
|
|
|
panic(err)
|
2020-01-09 20:10:01 +00:00
|
|
|
}
|
2020-01-10 17:43:02 +00:00
|
|
|
return
|
|
|
|
}()
|
2020-01-09 20:10:01 +00:00
|
|
|
|
2020-01-10 17:43:02 +00:00
|
|
|
var testInput = func() []byte {
|
2020-01-09 20:10:01 +00:00
|
|
|
input := make([]byte, 1<<15)
|
|
|
|
for i := range input {
|
|
|
|
input[i] = byte(i % 251)
|
|
|
|
}
|
2020-01-10 17:43:02 +00:00
|
|
|
return input
|
|
|
|
}()
|
|
|
|
|
|
|
|
func TestVectors(t *testing.T) {
|
|
|
|
for _, vec := range testVectors.Cases {
|
|
|
|
in := testInput[:vec.InputLen]
|
2020-01-09 20:10:01 +00:00
|
|
|
|
|
|
|
// regular
|
|
|
|
h := blake3.New(len(vec.Hash)/2, nil)
|
2020-01-09 20:38:44 +00:00
|
|
|
h.Write(in)
|
2020-01-09 20:10:01 +00:00
|
|
|
if out := toHex(h.Sum(nil)); out != vec.Hash {
|
|
|
|
t.Errorf("output did not match test vector:\n\texpected: %v...\n\t got: %v...", vec.Hash[:10], out[:10])
|
|
|
|
}
|
2020-01-10 17:43:02 +00:00
|
|
|
|
2020-01-09 20:10:01 +00:00
|
|
|
// keyed
|
2020-01-10 17:43:02 +00:00
|
|
|
h = blake3.New(len(vec.KeyedHash)/2, []byte(testVectors.Key))
|
2020-01-09 20:38:44 +00:00
|
|
|
h.Write(in)
|
2020-01-09 20:10:01 +00:00
|
|
|
if out := toHex(h.Sum(nil)); out != vec.KeyedHash {
|
|
|
|
t.Errorf("output did not match test vector:\n\texpected: %v...\n\t got: %v...", vec.KeyedHash[:10], out[:10])
|
|
|
|
}
|
2020-01-10 17:43:02 +00:00
|
|
|
|
2020-01-09 20:38:44 +00:00
|
|
|
// derive key
|
|
|
|
const ctx = "BLAKE3 2019-12-27 16:29:52 test vectors context"
|
2020-01-10 05:46:44 +00:00
|
|
|
subKey := make([]byte, len(vec.DeriveKey)/2)
|
|
|
|
blake3.DeriveKey(subKey, ctx, in)
|
|
|
|
if out := toHex(subKey); out != vec.DeriveKey {
|
|
|
|
t.Errorf("output did not match test vector:\n\texpected: %v...\n\t got: %v...", vec.DeriveKey[:10], subKey[:10])
|
2020-01-09 20:38:44 +00:00
|
|
|
}
|
2020-01-10 17:43:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestXOF(t *testing.T) {
|
|
|
|
for _, vec := range testVectors.Cases {
|
|
|
|
in := testInput[:vec.InputLen]
|
|
|
|
|
|
|
|
// XOF should produce same output as Sum, even when outputting 7 bytes at a time
|
|
|
|
h := blake3.New(len(vec.Hash)/2, nil)
|
2020-01-09 21:25:24 +00:00
|
|
|
h.Write(in)
|
|
|
|
var xofBuf bytes.Buffer
|
|
|
|
io.CopyBuffer(&xofBuf, io.LimitReader(h.XOF(), int64(len(vec.Hash)/2)), make([]byte, 7))
|
|
|
|
if out := toHex(xofBuf.Bytes()); out != vec.Hash {
|
|
|
|
t.Errorf("XOF output did not match test vector:\n\texpected: %v...\n\t got: %v...", vec.Hash[:10], out[:10])
|
|
|
|
}
|
2020-01-10 17:43:02 +00:00
|
|
|
|
|
|
|
// Should be able to Seek around in the output stream without affecting correctness
|
|
|
|
seeks := []struct {
|
|
|
|
offset int64
|
|
|
|
whence int
|
|
|
|
}{
|
|
|
|
{0, io.SeekStart},
|
|
|
|
{17, io.SeekCurrent},
|
|
|
|
{-5, io.SeekCurrent},
|
|
|
|
{int64(h.Size()), io.SeekStart},
|
|
|
|
{int64(h.Size()), io.SeekCurrent},
|
|
|
|
}
|
|
|
|
xof := h.XOF()
|
|
|
|
outR := bytes.NewReader(xofBuf.Bytes())
|
|
|
|
for _, s := range seeks {
|
|
|
|
outRead := make([]byte, 10)
|
|
|
|
xofRead := make([]byte, 10)
|
|
|
|
offset, _ := outR.Seek(s.offset, s.whence)
|
|
|
|
n, _ := outR.Read(outRead)
|
|
|
|
xof.Seek(s.offset, s.whence)
|
|
|
|
xof.Read(xofRead[:n])
|
|
|
|
if !bytes.Equal(outRead[:n], xofRead[:n]) {
|
|
|
|
t.Errorf("XOF output did not match test vector at offset %v:\n\texpected: %x...\n\t got: %x...", offset, outRead[:10], xofRead[:10])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-01-09 20:10:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 17:43:02 +00:00
|
|
|
type nopReader struct{}
|
|
|
|
|
|
|
|
func (nopReader) Read(p []byte) (int, error) { return len(p), nil }
|
|
|
|
|
2020-01-09 20:10:01 +00:00
|
|
|
func BenchmarkWrite(b *testing.B) {
|
2020-01-10 17:43:02 +00:00
|
|
|
b.SetBytes(1)
|
|
|
|
io.CopyN(blake3.New(0, nil), nopReader{}, int64(b.N))
|
2020-01-09 20:10:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 17:43:02 +00:00
|
|
|
func BenchmarkSum256(b *testing.B) {
|
2020-01-09 22:58:48 +00:00
|
|
|
buf := make([]byte, 1024)
|
2020-01-09 20:10:01 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-01-10 17:43:02 +00:00
|
|
|
blake3.Sum256(buf)
|
2020-01-09 20:10:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-09 21:25:24 +00:00
|
|
|
|
|
|
|
func BenchmarkXOF(b *testing.B) {
|
|
|
|
b.SetBytes(1)
|
2020-01-10 17:43:02 +00:00
|
|
|
io.CopyN(ioutil.Discard, blake3.New(0, nil).XOF(), int64(b.N))
|
2020-01-09 21:25:24 +00:00
|
|
|
}
|