-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconsensus_specs_test.go
421 lines (373 loc) · 11.3 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
// This code was copied from @jtraglia here: https://github.com/ethereum/c-kzg-4844/blob/599ae2fe2138e3085453b5424254e0a7c22b2ca3/bindings/go/main_test.go#L1
package gokzg4844_test
import (
"encoding/hex"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
gokzg4844 "github.com/crate-crypto/go-kzg-4844"
"github.com/crate-crypto/go-kzg-4844/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/*/*/*")
)
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 []gokzg4844.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 []gokzg4844.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 []gokzg4844.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 hexStrToBlob(hexStr string) (*gokzg4844.Blob, error) {
var blob gokzg4844.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) (gokzg4844.Scalar, error) {
var scalar gokzg4844.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) (gokzg4844.KZGCommitment, error) {
point, err := hexStrToG1Point(hexStr)
return gokzg4844.KZGCommitment(point), err
}
func hexStrToProof(hexStr string) (gokzg4844.KZGProof, error) {
point, err := hexStrToG1Point(hexStr)
return gokzg4844.KZGProof(point), err
}
func hexStrToG1Point(hexStr string) (gokzg4844.G1Point, error) {
var point gokzg4844.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:]
}