Debian Codegraph @

h2c/vectors_test.go

raw symbols SCIP
1 // Copyright (c) 2021 Oasis Labs Inc. All rights reserved.
2 // Copyright (c) 2021 Yawning Angel. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 package h2c
32
33 import (
34 "bytes"
35 "compress/gzip"
36 "crypto"
37 _ "crypto/sha256"
38 _ "crypto/sha512"
39 "encoding/hex"
40 "encoding/json"
41 "fmt"
42 "os"
43 "strings"
44 "testing"
45
46 "golang.org/x/crypto/sha3"
47
48 "filippo.io/edwards25519"
49 "filippo.io/edwards25519/field"
50
51 "gitlab.com/yawning/edwards25519-extra/internal/montgomery"
52 )
53
54 type suiteTestDef struct {
55 n string
56 file string
57 fn func([]byte, []byte) (*edwards25519.Point, error)
58 fn2 func([]byte, []byte) (*field.Element, *field.Element, error)
59 }
60
61 type expandTestDef struct {
62 n string
63 file string
64 h crypto.Hash
65 xof sha3.ShakeHash
66 }
67
68 func TestVectors(t *testing.T) {
69 for _, expandTest := range []expandTestDef{
70 {
71 n: "SHA256",
72 file: "testdata/expand_message_xmd_SHA256_38.json.gz",
73 h: crypto.SHA256,
74 },
75 {
76 n: "SHA256-LongDST",
77 file: "testdata/expand_message_xmd_SHA256_256.json.gz",
78 h: crypto.SHA256,
79 },
80 {
81 n: "SHA512",
82 file: "testdata/expand_message_xmd_SHA512_38.json.gz",
83 h: crypto.SHA512,
84 },
85 {
86 n: "SHAKE128",
87 file: "testdata/expand_message_xof_SHAKE128_36.json.gz",
88 xof: sha3.NewShake128(),
89 },
90 {
91 n: "SHAKE128-LongDST",
92 file: "testdata/expand_message_xof_SHAKE128_256.json.gz",
93 xof: sha3.NewShake128(),
94 },
95 {
96 n: "SHAKE256",
97 file: "testdata/expand_message_xof_SHAKE256_36.json.gz",
98 xof: sha3.NewShake256(),
99 },
100 } {
101 t.Run(expandTest.n, func(t *testing.T) {
102 testExpand(t, &expandTest)
103 })
104 }
105
106 for _, suiteTest := range []suiteTestDef{
107 {
108 n: "edwards25519_XMD:SHA-512_ELL2_RO_",
109 file: "testdata/edwards25519_XMD_SHA-512_ELL2_RO_.json.gz",
110 fn: Edwards25519_XMD_SHA512_ELL2_RO,
111 },
112 {
113 n: "edwards25519_XMD:SHA-512_ELL2_NU_",
114 file: "testdata/edwards25519_XMD_SHA-512_ELL2_NU_.json.gz",
115 fn: Edwards25519_XMD_SHA512_ELL2_NU,
116 },
117 {
118 n: "curve25519_XMD:SHA-512_ELL2_RO_",
119 file: "testdata/curve25519_XMD_SHA-512_ELL2_RO_.json.gz",
120 fn2: Curve25519_XMD_SHA512_ELL2_RO,
121 },
122 {
123 n: "curve25519_XMD:SHA-512_ELL2_NU_",
124 file: "testdata/curve25519_XMD_SHA-512_ELL2_NU_.json.gz",
125 fn2: Curve25519_XMD_SHA512_ELL2_NU,
126 },
127 } {
128 t.Run(suiteTest.n, func(t *testing.T) {
129 testSuite(t, &suiteTest)
130 })
131 }
132 }
133
134 type suiteTestVectors struct {
135 DST string `json:"DST"`
136 Vectors []suiteTestVector `json:"vectors"`
137 }
138
139 type suiteTestVector struct {
140 P suiteTestPoint
141 Msg string `json:"msg"`
142 }
143
144 type suiteTestPoint struct {
145 X string `json:"x"`
146 Y string `json:"y"`
147 }
148
149 func (pt *suiteTestPoint) ToCoordinates(t *testing.T) (*field.Element, *field.Element, error) {
150 x := mustUnhex(t, trimOhEcks(pt.X))
151 y := mustUnhex(t, trimOhEcks(pt.Y))
152
153 // The IETF test vectors provide all coordinates in big-endian byte order.
154 x = reversedByteSlice(x)
155 y = reversedByteSlice(y)
156
157 // Generate a point from the test vector x and y-coordinates.
158 var feX, feY field.Element
159 if _, err := feX.SetBytes(x); err != nil {
160 return nil, nil, fmt.Errorf("h2c: failed to deserialize x: %w", err)
161 }
162 if _, err := feY.SetBytes(y); err != nil {
163 return nil, nil, fmt.Errorf("h2c: failed to deserialize y: %w", err)
164 }
165
166 return &feX, &feY, nil
167 }
168
169 func (pt *suiteTestPoint) ToEdwardsPoint(t *testing.T) (*edwards25519.Point, error) {
170 feX, feY, err := pt.ToCoordinates(t)
171 if err != nil {
172 return nil, err
173 }
174 return montgomery.NewEdwardsFromXY(feX, feY), nil
175 }
176
177 func (pt *suiteTestPoint) ToMontgomeryPoint(t *testing.T) (*field.Element, *field.Element, error) {
178 feU, feV, err := pt.ToCoordinates(t)
179 return feU, feV, err
180 }
181
182 func testSuite(t *testing.T, def *suiteTestDef) {
183 f, err := os.Open(def.file)
184 if err != nil {
185 t.Fatal(err)
186 }
187 defer f.Close()
188 rd, err := gzip.NewReader(f)
189 if err != nil {
190 t.Fatal(err)
191 }
192 defer rd.Close()
193
194 var testVectors suiteTestVectors
195
196 dec := json.NewDecoder(rd)
197 if err = dec.Decode(&testVectors); err != nil {
198 t.Fatal(err)
199 }
200
201 for i, vec := range testVectors.Vectors {
202 t.Run(fmt.Sprintf("TestCase/%d", i), func(t *testing.T) {
203 switch {
204 case def.fn != nil:
205 expectedP, err := vec.P.ToEdwardsPoint(t)
206 if err != nil {
207 t.Fatalf("failed to deserialized result: %v", err)
208 }
209
210 p, err := def.fn([]byte(testVectors.DST), []byte(vec.Msg))
211 if err != nil {
212 t.Fatalf("hash to curve failed: %v", err)
213 }
214
215 if expectedP.Equal(p) != 1 {
216 t.Fatalf("h2c: point mismatch (Got: '%x')", p.Bytes())
217 }
218 case def.fn2 != nil:
219 expectedU, expectedV, err := vec.P.ToMontgomeryPoint(t)
220 if err != nil {
221 t.Fatalf("failed to deserialized result: %v", err)
222 }
223
224 u, v, err := def.fn2([]byte(testVectors.DST), []byte(vec.Msg))
225 if err != nil {
226 t.Fatalf("hash to curve failed: %v", err)
227 }
228
229 if expectedU.Equal(u) != 1 {
230 t.Fatalf("h2c: point u-cooredinate mismatch (Got: '%x')", u.Bytes())
231 }
232 if expectedV.Equal(v) != 1 {
233 t.Fatalf("h2c: point v-cooredinate mismatch (Got: '%x')", v.Bytes())
234 }
235 default:
236 t.Fatalf("h2c: no suite function defined")
237 }
238 })
239 }
240 }
241
242 type expandTestVectors struct {
243 DST string `json:"DST"`
244 Tests []expandTestVector `json:"tests"`
245 }
246
247 type expandTestVector struct {
248 Msg string `json:"msg"`
249 UniformBytes string `json:"uniform_bytes"`
250 }
251
252 func testExpand(t *testing.T, def *expandTestDef) {
253 f, err := os.Open(def.file)
254 if err != nil {
255 t.Fatal(err)
256 }
257 defer f.Close()
258 rd, err := gzip.NewReader(f)
259 if err != nil {
260 t.Fatal(err)
261 }
262 defer rd.Close()
263
264 var testVectors expandTestVectors
265
266 dec := json.NewDecoder(rd)
267 if err = dec.Decode(&testVectors); err != nil {
268 t.Fatal(err)
269 }
270
271 for i, vec := range testVectors.Tests {
272 t.Run(fmt.Sprintf("TestCase/%d", i), func(t *testing.T) {
273 expectedU := mustUnhex(t, vec.UniformBytes)
274 out := make([]byte, len(expectedU))
275
276 var err error
277 switch {
278 case def.h != 0:
279 err = ExpandMessageXMD(out, def.h, []byte(testVectors.DST), []byte(vec.Msg))
280 case def.xof != nil:
281 err = ExpandMessageXOF(out, def.xof, []byte(testVectors.DST), []byte(vec.Msg))
282 default:
283 t.Fatalf("malformed test vector, unknown hash/XOF")
284 }
285 if err != nil {
286 t.Fatalf("failed ExpandMessage(out, h, dst, msg): %v", err)
287 }
288
289 if !bytes.Equal(expectedU, out) {
290 t.Fatalf("output mismatch: got '%x'", out)
291 }
292 })
293 }
294 }
295
296 func trimOhEcks(s string) string {
297 return strings.TrimPrefix(s, "0x")
298 }
299
300 func mustUnhex(t *testing.T, x string) []byte {
301 b, err := hex.DecodeString(strings.ReplaceAll(x, " ", ""))
302 if err != nil {
303 t.Fatalf("failed to parse hex: %v", err)
304 }
305
306 return b
307 }