-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuplehash_test.go
207 lines (166 loc) · 4.45 KB
/
tuplehash_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2023 Yawning Angel
//
// SPDX-License-Identifier: BSD-3-Clause
package tuplehash
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"hash"
"math/big"
"os"
"testing"
"github.com/stretchr/testify/require"
)
// The JSON format test vectors were converted manually from:
// https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/TupleHash_samples.pdf
// https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/TupleHashXOF_samples.pdf
type tupleHashTestVector struct {
Name string `json:"name"`
SecurityStrength int `json:"security_strength"`
OutputLength int `json:"output_length"`
S string `json:"s"`
Tuples []string `json:"tuples"`
Outval string `json:"outval"`
IsXOF bool `json:"is_xof"`
}
func (tv *tupleHashTestVector) Run(t *testing.T) {
var h *Hasher
switch tv.IsXOF {
case false:
switch tv.SecurityStrength {
case 128:
h = NewTupleHash128([]byte(tv.S), tv.OutputLength/8)
case 256:
h = NewTupleHash256([]byte(tv.S), tv.OutputLength/8)
}
case true:
switch tv.SecurityStrength {
case 128:
h = NewTupleHashXOF128([]byte(tv.S))
case 256:
h = NewTupleHashXOF256([]byte(tv.S))
}
}
require.NotNil(t, h, "NewTupleHash(XOF)")
switch tv.SecurityStrength {
case 128:
require.Equal(t, rate128, h.BlockSize())
case 256:
require.Equal(t, rate256, h.BlockSize())
}
switch tv.IsXOF {
case false:
require.Equal(t, tv.OutputLength/8, h.Size())
case true:
require.Panics(t, func() { h.Size() })
}
for _, tuple := range tv.Tuples {
b := mustUnhex(tuple)
n, err := h.Write(b)
require.Equal(t, len(b), n, "h.Write")
require.NoError(t, err, "h.Write")
}
var out []byte
expected := mustUnhex(tv.Outval)
switch tv.IsXOF {
case false:
out = h.Sum(nil)
case true:
out = make([]byte, len(expected))
n, err := h.Read(out)
require.Equal(t, len(out), n, "h.Read")
require.NoError(t, err, "h.Read")
}
require.Equal(t, expected, out, "h.Sum/h.Read")
}
func TestNISTVectors(t *testing.T) {
f, err := os.Open("testdata/tuplehash.json")
require.NoError(t, err, "os.Open")
defer f.Close()
var testVectors []tupleHashTestVector
dec := json.NewDecoder(f)
err = dec.Decode(&testVectors)
require.NoError(t, err, "dec.Decode")
for _, vec := range testVectors {
t.Run(vec.Name, vec.Run)
}
}
type encodeTestVector struct {
Value string `json:"value"`
Left string `json:"left"`
Right string `json:"right"`
}
func (tv *encodeTestVector) Run(t *testing.T) {
expectedLeft := mustUnhex(tv.Left)
expectedRight := mustUnhex(tv.Right)
v, _ := new(big.Int).SetString(tv.Value, 0)
var buf [2 * 8]byte
v.FillBytes(buf[:])
hi := binary.BigEndian.Uint64(buf[0:])
lo := binary.BigEndian.Uint64(buf[8:])
w := bytes.NewBuffer(nil)
leftEncode(w, hi, lo)
require.EqualValues(t, expectedLeft, w.Bytes())
w.Reset()
rightEncode(w, hi, lo)
require.EqualValues(t, expectedRight, w.Bytes())
}
func TestEncode(t *testing.T) {
f, err := os.Open("testdata/lr_encode.json")
require.NoError(t, err, "os.Open")
defer f.Close()
var testVectors []encodeTestVector
dec := json.NewDecoder(f)
err = dec.Decode(&testVectors)
require.NoError(t, err, "dec.Decode")
for _, vec := range testVectors {
t.Run(vec.Value, vec.Run)
}
}
func TestAPI(t *testing.T) {
var (
testSep = []byte("yawning/tuplehash/tests")
testTuple = []byte("this is my tuple")
testTuple2 = []byte("there are many others like it")
)
t.Run("Hash", func(t *testing.T) {
h := NewTupleHash128(testSep, 32)
_ = (hash.Hash)(h)
_, _ = h.Write(testTuple)
require.Panics(t, func() {
var tmp [10]byte
_, _ = h.Read(tmp[:])
})
out := h.Sum(nil)
dst := make([]byte, 0, 32)
out2 := h.Sum(dst)
require.Equal(t, out, out2)
h.Reset()
_, _ = h.Write(testTuple)
out3 := h.Sum(nil)
require.Equal(t, out, out3)
})
t.Run("ShakeHash", func(t *testing.T) {
xof := NewTupleHashXOF128(testSep)
_, _ = xof.Write(testTuple)
require.Panics(t, func() { xof.Sum(nil) })
xof2 := xof.Clone()
_, _ = xof.Write(testTuple2)
dst := make([]byte, 64)
_, _ = xof.Read(dst)
_, _ = xof2.Write(testTuple2)
dst2 := make([]byte, 64)
_, _ = xof2.Read(dst2)
require.EqualValues(t, dst, dst2)
})
require.Panics(t, func() { NewTupleHash128(nil, -1) })
}
func mustUnhex(x string) []byte {
b, err := hex.DecodeString(x)
if err != nil {
panic(err)
}
return b
}