Skip to content

Commit 14e2883

Browse files
MariusVanDerWijdenTristan-Wilson
authored andcommitted
signer/core/apitypes: add cell proofs
1 parent 87627fb commit 14e2883

File tree

2 files changed

+174
-3
lines changed

2 files changed

+174
-3
lines changed

signer/core/apitypes/types.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (args *SendTxArgs) validateTxSidecar() error {
259259
if args.Commitments == nil {
260260
// Generate commitment and proof.
261261
commitments := make([]kzg4844.Commitment, n)
262-
proofs := make([]kzg4844.Proof, n)
262+
proofs := make([]kzg4844.Proof, 0, n)
263263
for i, b := range args.Blobs {
264264
c, err := kzg4844.BlobToCommitment(&b)
265265
if err != nil {
@@ -275,14 +275,13 @@ func (args *SendTxArgs) validateTxSidecar() error {
275275
}
276276
proofs = append(proofs, p...)
277277
}
278-
279278
} else {
280279
for i, b := range args.Blobs {
281280
p, err := kzg4844.ComputeBlobProof(&b, commitments[i])
282281
if err != nil {
283282
return fmt.Errorf("blobs[%d]: error computing proof: %v", i, err)
284283
}
285-
proofs[i] = p
284+
proofs = append(proofs, p)
286285
}
287286
}
288287
args.Commitments = commitments

signer/core/apitypes/types_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package apitypes
1818

1919
import (
20+
"crypto/rand"
2021
"crypto/sha256"
2122
"encoding/json"
23+
"fmt"
2224
"testing"
2325

2426
"github.com/ethereum/go-ethereum/common"
@@ -229,3 +231,173 @@ func TestType_TypeName(t *testing.T) {
229231
}
230232
}
231233
}
234+
235+
func TestValidateTxSidecar(t *testing.T) {
236+
t.Parallel()
237+
238+
// Helper function to create a test blob and its commitment/proof
239+
createTestBlob := func() (kzg4844.Blob, kzg4844.Commitment, kzg4844.Proof, common.Hash) {
240+
b := make([]byte, 31)
241+
rand.Read(b)
242+
var blob kzg4844.Blob
243+
for i := range b {
244+
blob[i+1] = b[i]
245+
}
246+
commitment, err := kzg4844.BlobToCommitment(&blob)
247+
if err != nil {
248+
t.Fatal(err)
249+
}
250+
proof, err := kzg4844.ComputeBlobProof(&blob, commitment)
251+
if err != nil {
252+
t.Fatal(err)
253+
}
254+
hash := kzg4844.CalcBlobHashV1(sha256.New(), &commitment)
255+
return blob, commitment, proof, hash
256+
}
257+
258+
// Helper function to create test cell proofs for v1 transactions
259+
createTestCellProofs := func(blob kzg4844.Blob) []kzg4844.Proof {
260+
cellProofs, err := kzg4844.ComputeCellProofs(&blob)
261+
if err != nil {
262+
t.Fatal(err)
263+
}
264+
return cellProofs
265+
}
266+
267+
blob1, commitment1, proof1, hash1 := createTestBlob()
268+
blob2, commitment2, proof2, hash2 := createTestBlob()
269+
270+
tests := []struct {
271+
name string
272+
args SendTxArgs
273+
wantErr bool
274+
}{
275+
{
276+
name: "no blobs - should pass",
277+
args: SendTxArgs{},
278+
wantErr: false,
279+
},
280+
{
281+
name: "valid blobs with commitments and proofs",
282+
args: SendTxArgs{
283+
Blobs: []kzg4844.Blob{blob1, blob2},
284+
Commitments: []kzg4844.Commitment{commitment1, commitment2},
285+
Proofs: []kzg4844.Proof{proof1, proof2},
286+
BlobHashes: []common.Hash{hash1, hash2},
287+
},
288+
wantErr: false,
289+
},
290+
{
291+
name: "valid blobs without commitments/proofs - should generate them",
292+
args: SendTxArgs{
293+
Blobs: []kzg4844.Blob{blob1},
294+
},
295+
wantErr: false,
296+
},
297+
{
298+
name: "valid blobs with v1 cell proofs",
299+
args: SendTxArgs{
300+
Blobs: []kzg4844.Blob{blob1},
301+
Commitments: []kzg4844.Commitment{commitment1},
302+
Proofs: createTestCellProofs(blob1),
303+
BlobHashes: []common.Hash{hash1},
304+
},
305+
wantErr: false,
306+
},
307+
{
308+
name: "blobs with v1 version flag - should generate cell proofs",
309+
args: SendTxArgs{
310+
Blobs: []kzg4844.Blob{blob1},
311+
BlobVersion: 1,
312+
},
313+
wantErr: false,
314+
},
315+
{
316+
name: "proofs provided but commitments not",
317+
args: SendTxArgs{
318+
Blobs: []kzg4844.Blob{blob1},
319+
Proofs: []kzg4844.Proof{proof1},
320+
},
321+
wantErr: true,
322+
},
323+
{
324+
name: "commitments provided but proofs not",
325+
args: SendTxArgs{
326+
Blobs: []kzg4844.Blob{blob1},
327+
Commitments: []kzg4844.Commitment{commitment1},
328+
},
329+
wantErr: true,
330+
},
331+
{
332+
name: "mismatch between blobs and commitments",
333+
args: SendTxArgs{
334+
Blobs: []kzg4844.Blob{blob1, blob2},
335+
Commitments: []kzg4844.Commitment{commitment1}, // Only one commitment for two blobs
336+
Proofs: []kzg4844.Proof{proof1},
337+
},
338+
wantErr: true,
339+
},
340+
{
341+
name: "mismatch between blobs and hashes",
342+
args: SendTxArgs{
343+
Blobs: []kzg4844.Blob{blob1, blob2},
344+
Commitments: []kzg4844.Commitment{commitment1, commitment2},
345+
Proofs: []kzg4844.Proof{proof1, proof2},
346+
BlobHashes: []common.Hash{hash1}, // Only one hash for two blobs
347+
},
348+
wantErr: true,
349+
},
350+
{
351+
name: "wrong number of proofs",
352+
args: SendTxArgs{
353+
Blobs: []kzg4844.Blob{blob1, blob2},
354+
Commitments: []kzg4844.Commitment{commitment1, commitment2},
355+
Proofs: []kzg4844.Proof{proof1, proof2, proof1}, // 3 proofs for 2 blobs
356+
},
357+
wantErr: true,
358+
},
359+
{
360+
name: "invalid blob hash",
361+
args: SendTxArgs{
362+
Blobs: []kzg4844.Blob{blob1},
363+
Commitments: []kzg4844.Commitment{commitment1},
364+
Proofs: []kzg4844.Proof{proof1},
365+
BlobHashes: []common.Hash{hash2}, // Wrong hash
366+
},
367+
wantErr: true,
368+
},
369+
}
370+
371+
for _, tt := range tests {
372+
t.Run(tt.name, func(t *testing.T) {
373+
// Make a copy to avoid modifying the original test case
374+
args := tt.args
375+
err := args.validateTxSidecar()
376+
377+
if tt.wantErr {
378+
if err == nil {
379+
t.Errorf("validateTxSidecar() expected error but got none")
380+
return
381+
}
382+
} else {
383+
if err != nil {
384+
t.Errorf("validateTxSidecar() unexpected error = %v", err)
385+
}
386+
387+
// For successful cases, verify that commitments and proofs were generated if they weren't provided
388+
if len(args.Blobs) > 0 {
389+
if args.Commitments == nil || len(args.Commitments) != len(args.Blobs) {
390+
t.Errorf("validateTxSidecar() should have generated commitments")
391+
}
392+
if args.Proofs == nil || (len(args.Proofs) != len(args.Blobs) && len(args.Proofs) != len(args.Blobs)*kzg4844.CellProofsPerBlob) {
393+
fmt.Println("proofs", args.Proofs)
394+
t.Errorf("validateTxSidecar() should have generated proofs")
395+
}
396+
if args.BlobHashes == nil || len(args.BlobHashes) != len(args.Blobs) {
397+
t.Errorf("validateTxSidecar() should have generated blob hashes")
398+
}
399+
}
400+
}
401+
})
402+
}
403+
}

0 commit comments

Comments
 (0)