-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto_test.go
387 lines (321 loc) · 11.6 KB
/
crypto_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"io"
"os"
"strings"
"testing"
multiproof "github.com/crate-crypto/go-ipa"
"github.com/crate-crypto/go-ipa/bandersnatch/fp"
"github.com/crate-crypto/go-ipa/bandersnatch/fr"
"github.com/crate-crypto/go-ipa/banderwagon"
"github.com/crate-crypto/go-ipa/common"
"github.com/crate-crypto/go-ipa/ipa"
"github.com/stretchr/testify/require"
)
func Test001(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
Scalars []string `json:"scalars"`
SerializedCommitment string `json:"serializedCommitment"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../001_vector_commitment.json", &data)
scalars := make([]fr.Element, 256)
for i := 0; i < 256; i++ {
scalars[i].SetString(data.TestData.Scalars[i])
}
config, err := ipa.NewIPASettings()
require.NoError(t, err)
comm := config.Commit(scalars).Bytes()
gotComm := hex.EncodeToString(comm[:])
require.Equal(t, data.TestData.SerializedCommitment[2:], gotComm)
}
func Test002(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
SerializedPoint string `json:"serializedPoint"`
FieldElement string `json:"fieldElement"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../002_map_to_field_element.json", &data)
var point banderwagon.Element
pointBytes, err := hex.DecodeString(data.TestData.SerializedPoint[2:])
require.NoError(t, err)
err = point.SetBytes(pointBytes)
require.NoError(t, err)
var got fr.Element
point.MapToScalarField(&got)
require.Equal(t, data.TestData.FieldElement, got.String())
}
func Test003(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
SerializeXCoordinate string `json:"serializedXCoordinate"`
SerializeYCoordinate string `json:"serializedYCoordinate"`
ExpectedSerializedPoint string `json:"expectedSerializedPoint"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../003_serialize_lexicographically_highest.json", &data)
xCoordBytes, err := hex.DecodeString(data.TestData.SerializeXCoordinate[2:])
require.NoError(t, err)
yCoordBytes, err := hex.DecodeString(data.TestData.SerializeYCoordinate[2:])
require.NoError(t, err)
var y fp.Element
y.SetBytes(yCoordBytes)
require.True(t, y.LexicographicallyLargest()) // Shouldn't be needed -- double-check test assumption.
var point banderwagon.Element
err = point.SetBytesUncompressed(append(xCoordBytes, yCoordBytes...), false) // Use false just to be sure.
require.NoError(t, err)
gotSerializedPoint := point.Bytes()
expSerializedPoint, err := hex.DecodeString(data.TestData.ExpectedSerializedPoint[2:])
require.NoError(t, err)
require.Equal(t, gotSerializedPoint[:], expSerializedPoint)
}
func Test004(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
SerializeXCoordinate string `json:"serializedXCoordinate"`
SerializeYCoordinate string `json:"serializedYCoordinate"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../004_deserialize_lexicographically_lowest.json", &data)
xCoordBytes, err := hex.DecodeString(data.TestData.SerializeXCoordinate[2:])
require.NoError(t, err)
yCoordBytes, err := hex.DecodeString(data.TestData.SerializeYCoordinate[2:])
require.NoError(t, err)
var y fp.Element
y.SetBytes(yCoordBytes)
require.False(t, y.LexicographicallyLargest()) // Double-check test assumption.
var point banderwagon.Element
err = point.SetBytesUncompressed(append(xCoordBytes, yCoordBytes...), false) // Use false just to be sure.
// Consider go-ipa returning wrapped-sentinel errors to be more specific.
if err == nil || !strings.Contains(err.Error(), "Y coordinate doesn't correspond to X") {
t.Fatalf("expected concrete error %s", err)
}
}
func Test005(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
SerializedPoint string `json:"serializedPoint"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../005_deserialize_point_not_in_curve.json", &data)
serializedBytes, err := hex.DecodeString(data.TestData.SerializedPoint[2:])
require.NoError(t, err)
var point banderwagon.Element
// TODO: consider go-ipa returning wrapped-sentinel errors to be more specific
// about the error type.
if err := point.SetBytesUncompressed(serializedBytes, false); err == nil {
t.Fatalf("expected error, got nil")
}
}
func Test006(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
SerializedPoint string `json:"serializedPoint"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../006_deserialize_point_not_in_subgroup.json", &data)
serializedBytes, err := hex.DecodeString(data.TestData.SerializedPoint[2:])
require.NoError(t, err)
var point banderwagon.Element
if err := point.SetBytesUncompressed(serializedBytes, false); err == nil {
t.Fatalf("expected error, got nil")
}
}
func Test007(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
SerializedPoint string `json:"serializedPoint"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../007_deserialize_point_x_bigger_than_field.json", &data)
serializedBytes, err := hex.DecodeString(data.TestData.SerializedPoint[2:])
require.NoError(t, err)
var point banderwagon.Element
err = point.SetBytes(serializedBytes)
// Consider go-ipa returning wrapped-sentinel errors to be more specific.
if err == nil || !strings.Contains(err.Error(), "invalid compressed point") {
t.Fatalf("expected concrete error")
}
}
func Test008(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
SerializedPoints []string `json:"serializedPoint"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../008_deserialize_point_x_wrong_length.json", &data)
for _, serializedPoint := range data.TestData.SerializedPoints {
serializedBytes, err := hex.DecodeString(serializedPoint[2:])
require.NoError(t, err)
var point banderwagon.Element
err = point.SetBytes(serializedBytes)
// Consider go-ipa returning wrapped-sentinel errors to be more specific.
if err == nil || !strings.Contains(err.Error(), "invalid compressed point") {
t.Fatalf("expected concrete error")
}
}
}
func Test009(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
SerializedProof string `json:"serializedProof"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../009_deserialize_proof_invalid_final_scalar.json", &data)
proofBytes, err := hex.DecodeString(data.TestData.SerializedProof[2:])
require.NoError(t, err)
var proof multiproof.MultiProof
// Consider go-ipa returning wrapped-sentinel errors to be more specific.
if err := proof.Read(bytes.NewReader(proofBytes)); err == nil || !strings.Contains(err.Error(), "failed to read A_scalar") {
t.Fatalf("expected concrete error")
}
}
func Test010(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
SerializedProofs []string `json:"serializedProofs"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../010_deserialize_proof_wrong_length.json", &data)
for _, serializedProof := range data.TestData.SerializedProofs {
proofBytes, err := hex.DecodeString(serializedProof[2:])
require.NoError(t, err)
var proof multiproof.MultiProof
// Consider go-ipa returning wrapped-sentinel errors to be more specific.
if err := proof.Read(bytes.NewReader(proofBytes)); err == nil || !strings.Contains(err.Error(), "EOF") {
t.Fatalf("expected concrete error %s", err)
}
}
}
func Test011(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
ForgedEvaluationResult struct {
PedersenCommitment string `json:"pedersenCommitment"`
EvaluationPoint int `json:"evaluationPoint"`
EvaluationResultFr string `json:"evaluationResultFr"`
SerializedProof string `json:"serializedProof"`
} `json:"forgedEvaluationResult"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../011_range_proof_verification.json", &data)
commitmentBytes, err := hex.DecodeString(data.TestData.ForgedEvaluationResult.PedersenCommitment[2:])
require.NoError(t, err)
var commitment banderwagon.Element
err = commitment.SetBytes(commitmentBytes)
require.NoError(t, err)
evaluationResultFr, err := hex.DecodeString(data.TestData.ForgedEvaluationResult.EvaluationResultFr[2:])
require.NoError(t, err)
var evalResult fr.Element
evalResult.SetBytes(evaluationResultFr)
serializedProof, err := hex.DecodeString(data.TestData.ForgedEvaluationResult.SerializedProof[2:])
require.NoError(t, err)
var proof multiproof.MultiProof
err = proof.Read(bytes.NewReader(serializedProof))
require.NoError(t, err)
config, err := ipa.NewIPASettings()
require.NoError(t, err)
// Walk the [0, 255] range and verify the proof fails for every evaluation point but the correct one.
for i := 0; i < 256; i++ {
transcript := common.NewTranscript("multiproof")
ok, err := multiproof.CheckMultiProof(transcript, config, &proof, []*banderwagon.Element{&commitment}, []*fr.Element{&evalResult}, []uint8{uint8(i)})
require.NoError(t, err)
require.True(t, ok == (i == data.TestData.ForgedEvaluationResult.EvaluationPoint))
}
}
func Test012(t *testing.T) {
t.Parallel()
type testType = struct {
Name string `json:"name"`
Description string `json:"description"`
TestData struct {
PedersenCommitment string `json:"pedersenCommitment"`
EvaluationPoint int `json:"evaluationPoint"`
EvaluationResultFr string `json:"evaluationResultFr"`
IPASerializedProof string `json:"ipaSerializedProof"`
} `json:"testData"`
}
var data testType
readTestFile(t, "../../012_in_domain_ipa_proof_verification.json", &data)
commitmentBytes, err := hex.DecodeString(data.TestData.PedersenCommitment[2:])
require.NoError(t, err)
var commitment banderwagon.Element
err = commitment.SetBytes(commitmentBytes)
require.NoError(t, err)
var evalPoint fr.Element
evalPoint.SetUint64(uint64(data.TestData.EvaluationPoint))
evaluationResultFr, err := hex.DecodeString(data.TestData.EvaluationResultFr[2:])
require.NoError(t, err)
var evalResult fr.Element
evalResult.SetBytes(evaluationResultFr)
serializedProof, err := hex.DecodeString(data.TestData.IPASerializedProof[2:])
require.NoError(t, err)
var proof ipa.IPAProof
err = proof.Read(bytes.NewReader(serializedProof))
require.NoError(t, err)
config, err := ipa.NewIPASettings()
require.NoError(t, err)
transcript := common.NewTranscript("ipa")
ok, err := ipa.CheckIPAProof(transcript, config, commitment, proof, evalPoint, evalResult)
require.NoError(t, err)
require.True(t, ok)
}
func readTestFile(t *testing.T, testFilePath string, out interface{}) {
t.Helper()
f, err := os.Open(testFilePath)
require.NoError(t, err)
defer f.Close()
content, err := io.ReadAll(f)
require.NoError(t, err)
err = json.Unmarshal(content, out)
require.NoError(t, err)
}