-
Notifications
You must be signed in to change notification settings - Fork 146
/
expander_test.go
115 lines (102 loc) · 2.43 KB
/
expander_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package expander_test
import (
"bytes"
"crypto"
_ "crypto/sha256"
_ "crypto/sha512"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/cloudflare/circl/expander"
"github.com/cloudflare/circl/internal/test"
"github.com/cloudflare/circl/xof"
)
func TestExpander(t *testing.T) {
fileNames, err := filepath.Glob("./testdata/*.json")
if err != nil {
t.Fatal(err)
}
for _, fileName := range fileNames {
f, err := os.Open(fileName)
if err != nil {
t.Fatal(err)
}
dec := json.NewDecoder(f)
var v vectorExpanderSuite
err = dec.Decode(&v)
if err != nil {
t.Fatal(err)
}
f.Close()
t.Run(v.Name+"/"+v.Hash, func(t *testing.T) { testExpander(t, &v) })
}
}
func testExpander(t *testing.T, vs *vectorExpanderSuite) {
var exp expander.Expander
switch vs.Hash {
case "SHA256":
exp = expander.NewExpanderMD(crypto.SHA256, []byte(vs.DST))
case "SHA512":
exp = expander.NewExpanderMD(crypto.SHA512, []byte(vs.DST))
case "SHAKE128":
exp = expander.NewExpanderXOF(xof.SHAKE128, vs.K, []byte(vs.DST))
case "SHAKE256":
exp = expander.NewExpanderXOF(xof.SHAKE256, vs.K, []byte(vs.DST))
default:
t.Skip("hash not supported: " + vs.Hash)
}
for i, v := range vs.Tests {
lenBytes, err := strconv.ParseUint(v.Len, 0, 64)
if err != nil {
t.Fatal(err)
}
got := exp.Expand([]byte(v.Msg), uint(lenBytes))
want, err := hex.DecodeString(v.UniformBytes)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
test.ReportError(t, got, want, i)
}
}
}
type vectorExpanderSuite struct {
DST string `json:"DST"`
Hash string `json:"hash"`
Name string `json:"name"`
K uint `json:"k"`
Tests []struct {
DstPrime string `json:"DST_prime"`
Len string `json:"len_in_bytes"`
Msg string `json:"msg"`
MsgPrime string `json:"msg_prime"`
UniformBytes string `json:"uniform_bytes"`
} `json:"tests"`
}
func BenchmarkExpander(b *testing.B) {
in := []byte("input")
dst := []byte("dst")
for _, v := range []struct {
Name string
Exp expander.Expander
}{
{"XMD", expander.NewExpanderMD(crypto.SHA256, dst)},
{"XOF", expander.NewExpanderXOF(xof.SHAKE128, 0, dst)},
} {
exp := v.Exp
for l := 8; l <= 10; l++ {
max := int64(1) << uint(l)
b.Run(fmt.Sprintf("%v/%v", v.Name, max), func(b *testing.B) {
b.SetBytes(max)
b.ResetTimer()
for i := 0; i < b.N; i++ {
exp.Expand(in, uint(max))
}
})
}
}
}