-
Notifications
You must be signed in to change notification settings - Fork 22
/
consensus_specs_test.go
637 lines (556 loc) · 16.9 KB
/
consensus_specs_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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
// This code was copied from @jtraglia here: https://github.com/ethereum/c-kzg-4844/blob/599ae2fe2138e3085453b5424254e0a7c22b2ca3/bindings/go/main_test.go#L1
package goethkzg_test
import (
"encoding/hex"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
goethkzg "github.com/crate-crypto/go-eth-kzg"
"github.com/crate-crypto/go-eth-kzg/internal/kzg"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
var (
testDir = "tests"
blobToKZGCommitmentTests = filepath.Join(testDir, "blob_to_kzg_commitment/*/*/*")
computeKZGProofTests = filepath.Join(testDir, "compute_kzg_proof/*/*/*")
computeBlobKZGProofTests = filepath.Join(testDir, "compute_blob_kzg_proof/*/*/*")
verifyKZGProofTests = filepath.Join(testDir, "verify_kzg_proof/*/*/*")
verifyBlobKZGProofTests = filepath.Join(testDir, "verify_blob_kzg_proof/*/*/*")
verifyBlobKZGProofBatchTests = filepath.Join(testDir, "verify_blob_kzg_proof_batch/*/*/*")
computeCellsAndKZGProofsTests = filepath.Join(testDir, "compute_cells_and_kzg_proofs/*/*/*")
verifyCellKZGProofBatchTests = filepath.Join(testDir, "verify_cell_kzg_proof_batch/*/*/*")
recoverCellsAndKZGProofsTests = filepath.Join(testDir, "recover_cells_and_kzg_proofs/*/*/*")
)
func TestBlobToKZGCommitment(t *testing.T) {
type Test struct {
Input struct {
Blob string `yaml:"blob"`
}
Commitment *string `yaml:"output"`
}
tests, err := filepath.Glob(blobToKZGCommitmentTests)
require.True(t, len(tests) > 0)
require.NoError(t, err)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, err)
testCaseValid := test.Commitment != nil
blob, err := hexStrToBlob(test.Input.Blob)
if err != nil {
require.False(t, testCaseValid)
return
}
gotCommitment, err := ctx.BlobToKZGCommitment(blob, NumGoRoutines)
if err != nil {
require.False(t, testCaseValid)
return
}
require.True(t, testCaseValid)
expectedCommitment, err := hexStrToCommitment(*test.Commitment)
require.NoError(t, err)
require.Equal(t, expectedCommitment, gotCommitment)
})
}
}
func TestComputeKZGProof(t *testing.T) {
type Test struct {
Input struct {
Blob string `yaml:"blob"`
InputPoint string `yaml:"z"`
}
ProofAndOutputPoint *[2]string `yaml:"output"`
}
tests, err := filepath.Glob(computeKZGProofTests)
require.True(t, len(tests) > 0)
require.NoError(t, err)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, testFile.Close())
require.NoError(t, err)
testCaseValid := test.ProofAndOutputPoint != nil
blob, err := hexStrToBlob(test.Input.Blob)
if err != nil {
require.False(t, testCaseValid)
return
}
inputPoint, err := hexStrToScalar(test.Input.InputPoint)
if err != nil {
require.False(t, testCaseValid)
return
}
proof, outputPoint, err := ctx.ComputeKZGProof(blob, inputPoint, NumGoRoutines)
if err != nil {
require.False(t, testCaseValid)
return
}
require.True(t, testCaseValid)
expectedProof, err := hexStrToProof(test.ProofAndOutputPoint[0])
require.NoError(t, err)
expectedOutputPoint, err := hexStrToScalar(test.ProofAndOutputPoint[1])
require.NoError(t, err)
require.Equal(t, expectedProof, proof)
require.Equal(t, expectedOutputPoint, outputPoint)
})
}
}
func TestComputeBlobKZGProof(t *testing.T) {
type Test struct {
Input struct {
Blob string `yaml:"blob"`
Commitment string `yaml:"commitment"`
}
Proof *string `yaml:"output"`
}
tests, err := filepath.Glob(computeBlobKZGProofTests)
require.True(t, len(tests) > 0)
require.NoError(t, err)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, testFile.Close())
require.NoError(t, err)
testCaseValid := test.Proof != nil
blob, err := hexStrToBlob(test.Input.Blob)
if err != nil {
require.False(t, testCaseValid)
return
}
commitment, err := hexStrToCommitment(test.Input.Commitment)
if err != nil {
require.False(t, testCaseValid)
return
}
proof, err := ctx.ComputeBlobKZGProof(blob, commitment, NumGoRoutines)
if err != nil {
require.False(t, testCaseValid)
return
}
require.True(t, testCaseValid)
expectedProof, err := hexStrToProof(*test.Proof)
require.NoError(t, err)
require.Equal(t, expectedProof, proof)
})
}
}
func TestVerifyKZGProof(t *testing.T) {
type Test struct {
Input struct {
Commitment string `yaml:"commitment"`
InputPoint string `yaml:"z"`
OutputPoint string `yaml:"y"`
Proof string `yaml:"proof"`
}
ProofIsValid *bool `yaml:"output"`
}
tests, err := filepath.Glob(verifyKZGProofTests)
require.True(t, len(tests) > 0)
require.NoError(t, err)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, testFile.Close())
require.NoError(t, err)
testCaseValid := test.ProofIsValid != nil
commitment, err := hexStrToCommitment(test.Input.Commitment)
if err != nil {
require.False(t, testCaseValid)
return
}
inputPoint, err := hexStrToScalar(test.Input.InputPoint)
if err != nil {
require.False(t, testCaseValid)
return
}
outputPoint, err := hexStrToScalar(test.Input.OutputPoint)
if err != nil {
require.False(t, testCaseValid)
return
}
proof, err := hexStrToProof(test.Input.Proof)
if err != nil {
require.False(t, testCaseValid)
return
}
err = ctx.VerifyKZGProof(commitment, inputPoint, outputPoint, proof)
// Test specifically distinguish between the test failing
// because of the pairing check and failing because of
// validation errors
if err != nil && !errors.Is(err, kzg.ErrVerifyOpeningProof) {
require.False(t, testCaseValid)
} else {
// Either the error is nil or it is a verification error
expectedOutput := *test.ProofIsValid
gotOutput := !errors.Is(err, kzg.ErrVerifyOpeningProof)
require.Equal(t, expectedOutput, gotOutput)
}
})
}
}
func TestVerifyBlobKZGProof(t *testing.T) {
type Test struct {
Input struct {
Blob string `yaml:"blob"`
Commitment string `yaml:"commitment"`
Proof string `yaml:"proof"`
}
ProofIsValid *bool `yaml:"output"`
}
tests, err := filepath.Glob(verifyBlobKZGProofTests)
require.True(t, len(tests) > 0)
require.NoError(t, err)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, testFile.Close())
require.NoError(t, err)
testCaseValid := test.ProofIsValid != nil
blob, err := hexStrToBlob(test.Input.Blob)
if err != nil {
require.False(t, testCaseValid)
return
}
commitment, err := hexStrToCommitment(test.Input.Commitment)
if err != nil {
require.False(t, testCaseValid)
return
}
proof, err := hexStrToProof(test.Input.Proof)
if err != nil {
require.False(t, testCaseValid)
return
}
err = ctx.VerifyBlobKZGProof(blob, commitment, proof)
// Test specifically distinguish between the test failing
// because of the pairing check and failing because of
// validation errors
if err != nil && !errors.Is(err, kzg.ErrVerifyOpeningProof) {
require.False(t, testCaseValid)
} else {
// Either the error is nil or it is a verification error
expectedOutput := *test.ProofIsValid
gotOutput := !errors.Is(err, kzg.ErrVerifyOpeningProof)
require.Equal(t, expectedOutput, gotOutput)
}
})
}
}
func TestVerifyBlobKZGProofBatch(t *testing.T) {
type Test struct {
Input struct {
Blobs []string `yaml:"blobs"`
Commitments []string `yaml:"commitments"`
Proofs []string `yaml:"proofs"`
}
ProofIsValid *bool `yaml:"output"`
}
tests, err := filepath.Glob(verifyBlobKZGProofBatchTests)
require.True(t, len(tests) > 0)
require.NoError(t, err)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, testFile.Close())
require.NoError(t, err)
testCaseValid := test.ProofIsValid != nil
var blobs []goethkzg.Blob
for _, b := range test.Input.Blobs {
blob, err := hexStrToBlob(b)
if err != nil {
require.False(t, testCaseValid)
return
}
blobs = append(blobs, *blob)
}
var commitments []goethkzg.KZGCommitment
for _, c := range test.Input.Commitments {
commitment, err := hexStrToCommitment(c)
if err != nil {
require.False(t, testCaseValid)
return
}
commitments = append(commitments, commitment)
}
var proofs []goethkzg.KZGProof
for _, p := range test.Input.Proofs {
proof, err := hexStrToProof(p)
if err != nil {
require.False(t, testCaseValid)
return
}
proofs = append(proofs, proof)
}
err = ctx.VerifyBlobKZGProofBatch(blobs, commitments, proofs)
errPar := ctx.VerifyBlobKZGProofBatchPar(blobs, commitments, proofs)
require.Equal(t, err, errPar)
// Test specifically distinguish between the test failing
// because of the pairing check and failing because of
// validation errors
if err != nil && err != kzg.ErrVerifyOpeningProof {
require.False(t, testCaseValid)
} else {
// Either the error is nil or it is a verification error
expectedOutput := *test.ProofIsValid
gotOutput := err != kzg.ErrVerifyOpeningProof
require.Equal(t, expectedOutput, gotOutput)
}
})
}
}
func TestComputeCellsAndKZGProofs(t *testing.T) {
type Test struct {
Input struct {
Blob string `yaml:"blob"`
}
Output *[][]string `yaml:"output"`
}
tests, err := filepath.Glob(computeCellsAndKZGProofsTests)
require.NoError(t, err)
require.True(t, len(tests) > 0)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, err)
testCaseValid := test.Output != nil
blob, err := hexStrToBlob(test.Input.Blob)
if err != nil {
require.False(t, testCaseValid)
return
}
cells, proofs, err := ctx.ComputeCellsAndKZGProofs(blob, 0)
if err == nil {
expectedCellStrs := (*test.Output)[0]
expectedCells, err := hexStrArrToCells(expectedCellStrs)
require.NoError(t, err)
require.Equal(t, expectedCells, cells[:])
expectedProofStrs := (*test.Output)[1]
expectedProofs, err := HexStrArrToProofs(expectedProofStrs)
require.NoError(t, err)
require.Equal(t, expectedProofs, proofs[:])
} else {
require.Nil(t, test.Output)
}
})
}
}
func TestVerifyCellKZGProofBatch(t *testing.T) {
type Test struct {
Input struct {
Commitments []string `yaml:"commitments"`
CellIndices []uint64 `yaml:"cell_indices"`
Cells []string `yaml:"cells"`
Proofs []string `yaml:"proofs"`
}
Output *bool `yaml:"output"`
}
tests, err := filepath.Glob(verifyCellKZGProofBatchTests)
require.NoError(t, err)
require.True(t, len(tests) > 0)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, testFile.Close())
require.NoError(t, err)
testCaseValid := test.Output != nil
commitments, err := HexStrArrToCommitments(test.Input.Commitments)
if err != nil {
require.False(t, testCaseValid)
return
}
cellIndices := test.Input.CellIndices
cells, err := hexStrArrToCells(test.Input.Cells)
if err != nil {
require.False(t, testCaseValid)
return
}
proofs, err := HexStrArrToProofs(test.Input.Proofs)
if err != nil {
require.False(t, testCaseValid)
return
}
err = ctx.VerifyCellKZGProofBatch(commitments, cellIndices, cells, proofs)
// Test specifically distinguish between the test failing
// because of the pairing check and failing because of
// validation errors on the input
if err != nil && !errors.Is(err, kzg.ErrVerifyOpeningProof) {
require.False(t, testCaseValid)
} else {
// Either the error is nil or it is a verification error
expectedOutput := *test.Output
gotOutput := !errors.Is(err, kzg.ErrVerifyOpeningProof)
require.Equal(t, expectedOutput, gotOutput)
}
})
}
}
func TestRecoverCellsAndKZGProofs(t *testing.T) {
type Test struct {
Input struct {
CellIds []uint64 `yaml:"cell_indices"`
Cells []string `yaml:"cells"`
}
Output *[][]string `yaml:"output"`
}
tests, err := filepath.Glob(recoverCellsAndKZGProofsTests)
require.NoError(t, err)
require.True(t, len(tests) > 0)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, testFile.Close())
require.NoError(t, err)
testCaseValid := test.Output != nil
cellIds := test.Input.CellIds
cells, err := hexStrArrToCells(test.Input.Cells)
if err != nil {
require.False(t, testCaseValid)
return
}
recoveredCells, recoveredProofs, err := ctx.RecoverCellsAndComputeKZGProofs(cellIds, cells, 0)
if err == nil {
require.NotNil(t, test.Output)
expectedCellStrs := (*test.Output)[0]
expectedCells, err := hexStrArrToCells(expectedCellStrs)
require.NoError(t, err)
require.Equal(t, expectedCells, recoveredCells[:])
expectedProofStrs := (*test.Output)[1]
expectedProofs, err := HexStrArrToProofs(expectedProofStrs)
require.NoError(t, err)
require.Equal(t, expectedProofs, recoveredProofs[:])
} else {
require.Nil(t, test.Output)
}
})
}
}
func hexStrToCell(hexStr string) (*goethkzg.Cell, error) {
var cell goethkzg.Cell
byts, err := hexStrToBytes(hexStr)
if err != nil {
return nil, err
}
if len(cell) != len(byts) {
return nil, fmt.Errorf("cell does not have the correct length, %d ", len(byts))
}
copy(cell[:], byts)
return &cell, nil
}
func hexStrArrToCells(hexStrs []string) ([]*goethkzg.Cell, error) {
cells := make([]*goethkzg.Cell, len(hexStrs))
for i, hexStr := range hexStrs {
cell, err := hexStrToCell(hexStr)
if err != nil {
return nil, err
}
cells[i] = cell
}
return cells, nil
}
func HexStrArrToProofs(hexStrs []string) ([]goethkzg.KZGProof, error) {
proofs := make([]goethkzg.KZGProof, len(hexStrs))
for i, hexStr := range hexStrs {
proof, err := hexStrToProof(hexStr)
if err != nil {
return nil, err
}
proofs[i] = proof
}
return proofs, nil
}
func HexStrArrToCommitments(hexStrs []string) ([]goethkzg.KZGCommitment, error) {
commitments := make([]goethkzg.KZGCommitment, len(hexStrs))
for i, hexStr := range hexStrs {
commitment, err := hexStrToCommitment(hexStr)
if err != nil {
return nil, err
}
commitments[i] = commitment
}
return commitments, nil
}
func hexStrToBlob(hexStr string) (*goethkzg.Blob, error) {
var blob goethkzg.Blob
byts, err := hexStrToBytes(hexStr)
if err != nil {
return nil, err
}
if len(blob) != len(byts) {
return nil, fmt.Errorf("blob does not have the correct length, %d ", len(byts))
}
copy(blob[:], byts)
return &blob, nil
}
func hexStrToScalar(hexStr string) (goethkzg.Scalar, error) {
var scalar goethkzg.Scalar
byts, err := hexStrToBytes(hexStr)
if err != nil {
return scalar, err
}
if len(scalar) != len(byts) {
return scalar, fmt.Errorf("scalar does not have the correct length, %d ", len(byts))
}
copy(scalar[:], byts)
return scalar, nil
}
func hexStrToCommitment(hexStr string) (goethkzg.KZGCommitment, error) {
point, err := hexStrToG1Point(hexStr)
return goethkzg.KZGCommitment(point), err
}
func hexStrToProof(hexStr string) (goethkzg.KZGProof, error) {
point, err := hexStrToG1Point(hexStr)
return goethkzg.KZGProof(point), err
}
func hexStrToG1Point(hexStr string) (goethkzg.G1Point, error) {
var point goethkzg.G1Point
byts, err := hexStrToBytes(hexStr)
if err != nil {
return point, err
}
if len(point) != len(byts) {
return point, fmt.Errorf("point does not have the correct length, %d ", len(byts))
}
copy(point[:], byts)
return point, nil
}
func hexStrToBytes(hexStr string) ([]byte, error) {
hexStr = trim0xPrefix(hexStr)
return hex.DecodeString(hexStr)
}
func trim0xPrefix(hexString string) string {
// Check that we are trimming off 0x
if hexString[0:2] != "0x" {
panic("hex string is not prefixed with 0x")
}
return hexString[2:]
}