This repository has been archived on 2023-05-01. You can view files and clone it, but cannot push or open issues or pull requests.
blake3/blake3_test.go

94 lines
2.4 KiB
Go
Raw Normal View History

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"
)
func toHex(data []byte) string { return hex.EncodeToString(data) }
2020-01-09 20:10:01 +00:00
func TestVectors(t *testing.T) {
data, err := ioutil.ReadFile("testdata/vectors.json")
if err != nil {
t.Fatal(err)
}
var vectors struct {
Key string
Cases []struct {
InputLen int `json:"input_len"`
Hash string `json:"hash"`
KeyedHash string `json:"keyed_hash"`
DeriveKey string `json:"derive_key"`
}
}
if err := json.Unmarshal(data, &vectors); err != nil {
t.Fatal(err)
}
input := make([]byte, 1<<15)
for i := range input {
input[i] = byte(i % 251)
}
for _, vec := range vectors.Cases {
2020-01-09 20:38:44 +00:00
in := input[: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])
}
// keyed
h = blake3.New(len(vec.KeyedHash)/2, []byte(vectors.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-09 20:38:44 +00:00
// derive key
const ctx = "BLAKE3 2019-12-27 16:29:52 test vectors context"
h = blake3.NewFromDerivedKey(len(vec.DeriveKey)/2, ctx)
h.Write(in)
if out := toHex(h.Sum(nil)); out != vec.DeriveKey {
t.Errorf("output did not match test vector:\n\texpected: %v...\n\t got: %v...", vec.DeriveKey[:10], out[:10])
}
2020-01-09 21:25:24 +00:00
// XOF should produce identical results, even when outputting 7 bytes at a time
h = blake3.New(len(vec.Hash)/2, nil)
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-09 20:10:01 +00:00
}
}
func BenchmarkWrite(b *testing.B) {
h := blake3.New(32, nil)
buf := make([]byte, 1<<15)
b.SetBytes(int64(len(buf)))
for i := 0; i < b.N; i++ {
h.Write(buf)
}
}
2020-01-09 21:25:24 +00:00
func BenchmarkChunk(b *testing.B) {
2020-01-09 20:10:01 +00:00
h := blake3.New(32, nil)
buf := make([]byte, 1024)
out := make([]byte, 0, 32)
2020-01-09 20:10:01 +00:00
for i := 0; i < b.N; i++ {
h.Write(buf)
h.Sum(out)
}
}
2020-01-09 21:25:24 +00:00
func BenchmarkXOF(b *testing.B) {
b.SetBytes(1)
io.CopyN(ioutil.Discard, blake3.New(32, nil).XOF(), int64(b.N))
}