Skip to content

Commit b64dfe5

Browse files
authored
Merge pull request #105 from kevaundray/kw/crypto-free-the-blobs
Update code for free the blobs
2 parents 5ee2738 + 602ad1f commit b64dfe5

File tree

17 files changed

+117
-522
lines changed

17 files changed

+117
-522
lines changed

accounts/external/backend.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,14 @@ func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transactio
218218
args.MaxFeePerGas = (*hexutil.Big)(tx.GasFeeCap())
219219
args.MaxPriorityFeePerGas = (*hexutil.Big)(tx.GasTipCap())
220220
case types.BlobTxType:
221-
hashes, _, blobs, aggProof := tx.BlobWrapData()
221+
hashes, _, blobs, proofs := tx.BlobWrapData()
222222
if len(hashes) != len(blobs) {
223223
return nil, fmt.Errorf("missing blobs data, expected %d blobs", len(hashes))
224224
}
225-
var z types.KZGProof
226-
if aggProof == z {
227-
return nil, fmt.Errorf("missing aggregated proof in blobs")
225+
if len(hashes) != len(proofs) {
226+
return nil, fmt.Errorf("missing proofs data, expected %d proofs", len(proofs))
228227
}
228+
229229
args.MaxFeePerGas = (*hexutil.Big)(tx.GasFeeCap())
230230
args.MaxPriorityFeePerGas = (*hexutil.Big)(tx.GasTipCap())
231231
args.Blobs = blobs

beacon/engine/types.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package engine
1818

1919
import (
20-
"errors"
2120
"fmt"
2221
"math/big"
2322

@@ -257,15 +256,12 @@ func BlockToBlobData(block *types.Block) (*BlobsBundle, error) {
257256
}
258257
for i, tx := range block.Transactions() {
259258
if tx.Type() == types.BlobTxType {
260-
versionedHashes, kzgs, blobs, aggProof := tx.BlobWrapData()
261-
if len(versionedHashes) != len(kzgs) || len(versionedHashes) != len(blobs) {
259+
versionedHashes, kzgs, blobs, proofs := tx.BlobWrapData()
260+
if len(versionedHashes) != len(kzgs) || len(versionedHashes) != len(blobs) || len(blobs) != len(proofs) {
262261
return nil, fmt.Errorf("tx %d in block %s has inconsistent blobs (%d) / kzgs (%d)"+
263-
" / versioned hashes (%d)", i, blockHash, len(blobs), len(kzgs), len(versionedHashes))
264-
}
265-
var zProof types.KZGProof
266-
if zProof == aggProof {
267-
return nil, errors.New("aggregated proof is not available in blobs")
262+
" / versioned hashes (%d) / proofs (%d)", i, blockHash, len(blobs), len(kzgs), len(versionedHashes), len(proofs))
268263
}
264+
269265
blobsBundle.Blobs = append(blobsBundle.Blobs, blobs...)
270266
blobsBundle.KZGs = append(blobsBundle.KZGs, kzgs...)
271267
}

core/txpool/txpool_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ func blobTx(nonce uint64, gaslimit uint64, gasFee uint64, tip uint64, dataGasFee
128128
for i := 0; i < len(blobData.BlobKzgs); i++ {
129129
hashes = append(hashes, blobData.BlobKzgs[i].ComputeVersionedHash())
130130
}
131-
_, _, aggregatedProof, err := blobData.Blobs.ComputeCommitmentsAndAggregatedProof()
131+
_, _, proofs, err := blobData.Blobs.ComputeCommitmentsAndProofs()
132132
if err != nil {
133133
panic(err)
134134
}
135-
blobData.KzgAggregatedProof = aggregatedProof
135+
blobData.Proofs = proofs
136136

137137
address := types.AddressSSZ(common.Address{})
138138
sbtx := &types.SignedBlobTx{

core/types/data_blob.go

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"io"
88

99
"github.com/crate-crypto/go-proto-danksharding-crypto/eth"
10-
api "github.com/crate-crypto/go-proto-danksharding-crypto/serialisation"
10+
api "github.com/crate-crypto/go-proto-danksharding-crypto/serialization"
1111
"github.com/ethereum/go-ethereum/common"
1212
"github.com/ethereum/go-ethereum/common/hexutil"
1313
"github.com/ethereum/go-ethereum/params"
@@ -199,6 +199,36 @@ func (li BlobKzgs) copy() BlobKzgs {
199199
return cpy
200200
}
201201

202+
type KZGProofs []KZGProof
203+
204+
func (li *KZGProofs) Deserialize(dr *codec.DecodingReader) error {
205+
return dr.List(func() codec.Deserializable {
206+
i := len(*li)
207+
*li = append(*li, KZGProof{})
208+
return &(*li)[i]
209+
}, 48, params.MaxBlobsPerBlock)
210+
}
211+
212+
func (li KZGProofs) Serialize(w *codec.EncodingWriter) error {
213+
return w.List(func(i uint64) codec.Serializable {
214+
return &li[i]
215+
}, 48, uint64(len(li)))
216+
}
217+
218+
func (li KZGProofs) ByteLength() uint64 {
219+
return uint64(len(li)) * 48
220+
}
221+
222+
func (li KZGProofs) FixedLength() uint64 {
223+
return 0
224+
}
225+
226+
func (li KZGProofs) copy() KZGProofs {
227+
cpy := make(KZGProofs, len(li))
228+
copy(cpy, li)
229+
return cpy
230+
}
231+
202232
type Blobs []Blob
203233

204234
func (a *Blobs) Deserialize(dr *codec.DecodingReader) error {
@@ -229,28 +259,28 @@ func (blobs Blobs) copy() Blobs {
229259
return cpy
230260
}
231261

232-
// Return KZG commitments, versioned hashes and the aggregated KZG proof that correspond to these blobs
233-
func (blobs Blobs) ComputeCommitmentsAndAggregatedProof() (commitments []KZGCommitment, versionedHashes []common.Hash, aggregatedProof KZGProof, err error) {
262+
// Return KZG commitments, versioned hashes and the proofs that correspond to these blobs
263+
func (blobs Blobs) ComputeCommitmentsAndProofs() (commitments []KZGCommitment, versionedHashes []common.Hash, proofs []KZGProof, err error) {
234264
commitments = make([]KZGCommitment, len(blobs))
265+
proofs = make([]KZGProof, len(blobs))
235266
versionedHashes = make([]common.Hash, len(blobs))
236267

237268
for i, blob := range blobs {
238-
commitment, err := eth.CryptoCtx.BlobToCommitment(blob)
269+
commitment, err := eth.CryptoCtx.BlobToKZGCommitment(blob)
239270
if err != nil {
271+
return nil, nil, nil, fmt.Errorf("could not convert blob to commitment: %v", err)
272+
}
240273

241-
return nil, nil, KZGProof{}, fmt.Errorf("could not convert blob to commitment: %v", err)
274+
proof, err := eth.CryptoCtx.ComputeBlobKZGProof(blob, commitment)
275+
if err != nil {
276+
return nil, nil, nil, fmt.Errorf("could not compute proof for blob: %v", err)
242277
}
243278
commitments[i] = KZGCommitment(commitment)
279+
proofs[i] = KZGProof(proof)
244280
versionedHashes[i] = common.Hash(eth.KZGToVersionedHash(commitment))
245281
}
246282

247-
proof, _, err := eth.CryptoCtx.ComputeAggregateKZGProof(toBlobs(blobs))
248-
if err != nil {
249-
return nil, nil, KZGProof{}, err
250-
}
251-
var kzgProof = KZGProof(proof)
252-
253-
return commitments, versionedHashes, kzgProof, nil
283+
return commitments, versionedHashes, proofs, nil
254284
}
255285

256286
func toBlobs(_blobs Blobs) []api.Blob {
@@ -267,39 +297,46 @@ func toComms(_comms BlobKzgs) []api.KZGCommitment {
267297
}
268298
return comms
269299
}
300+
func toProofs(_proofs KZGProofs) []api.KZGProof {
301+
proofs := make([]api.KZGProof, len(_proofs))
302+
for i, _proof := range _proofs {
303+
proofs[i] = api.KZGProof(_proof)
304+
}
305+
return proofs
306+
}
270307

271308
type BlobTxWrapper struct {
272-
Tx SignedBlobTx
273-
BlobKzgs BlobKzgs
274-
Blobs Blobs
275-
KzgAggregatedProof KZGProof
309+
Tx SignedBlobTx
310+
BlobKzgs BlobKzgs
311+
Blobs Blobs
312+
Proofs KZGProofs
276313
}
277314

278315
func (txw *BlobTxWrapper) Deserialize(dr *codec.DecodingReader) error {
279-
return dr.Container(&txw.Tx, &txw.BlobKzgs, &txw.Blobs, &txw.KzgAggregatedProof)
316+
return dr.Container(&txw.Tx, &txw.BlobKzgs, &txw.Blobs, &txw.Proofs)
280317
}
281318

282319
func (txw *BlobTxWrapper) Serialize(w *codec.EncodingWriter) error {
283-
return w.Container(&txw.Tx, &txw.BlobKzgs, &txw.Blobs, &txw.KzgAggregatedProof)
320+
return w.Container(&txw.Tx, &txw.BlobKzgs, &txw.Blobs, &txw.Proofs)
284321
}
285322

286323
func (txw *BlobTxWrapper) ByteLength() uint64 {
287-
return codec.ContainerLength(&txw.Tx, &txw.BlobKzgs, &txw.Blobs, &txw.KzgAggregatedProof)
324+
return codec.ContainerLength(&txw.Tx, &txw.BlobKzgs, &txw.Blobs, &txw.Proofs)
288325
}
289326

290327
func (txw *BlobTxWrapper) FixedLength() uint64 {
291328
return 0
292329
}
293330

294331
type BlobTxWrapData struct {
295-
BlobKzgs BlobKzgs
296-
Blobs Blobs
297-
KzgAggregatedProof KZGProof
332+
BlobKzgs BlobKzgs
333+
Blobs Blobs
334+
Proofs KZGProofs
298335
}
299336

300337
// sizeWrapData returns the size in bytes of the ssz-encoded BlobTxWrapData
301338
func (b *BlobTxWrapData) sizeWrapData() common.StorageSize {
302-
return common.StorageSize(codec.ContainerLength(&b.BlobKzgs, &b.Blobs, &b.KzgAggregatedProof))
339+
return common.StorageSize(codec.ContainerLength(&b.BlobKzgs, &b.Blobs, &b.Proofs))
303340
}
304341

305342
// validateBlobTransactionWrapper implements validate_blob_transaction_wrapper from EIP-4844
@@ -320,7 +357,7 @@ func (b *BlobTxWrapData) validateBlobTransactionWrapper(inner TxData) error {
320357
if l1 > params.MaxBlobsPerBlock {
321358
return fmt.Errorf("number of blobs exceeds max: %v", l1)
322359
}
323-
err := eth.CryptoCtx.VerifyAggregateKZGProof(toBlobs(b.Blobs), api.KZGProof(b.KzgAggregatedProof), toComms(b.BlobKzgs))
360+
err := eth.CryptoCtx.VerifyBlobKZGProofBatch(toBlobs(b.Blobs), toProofs(b.Proofs), toComms(b.BlobKzgs))
324361
if err != nil {
325362
return fmt.Errorf("error during proof verification: %v", err)
326363
}
@@ -337,9 +374,9 @@ func (b *BlobTxWrapData) validateBlobTransactionWrapper(inner TxData) error {
337374

338375
func (b *BlobTxWrapData) copy() TxWrapData {
339376
return &BlobTxWrapData{
340-
BlobKzgs: b.BlobKzgs.copy(),
341-
Blobs: b.Blobs.copy(),
342-
KzgAggregatedProof: b.KzgAggregatedProof,
377+
BlobKzgs: b.BlobKzgs.copy(),
378+
Blobs: b.Blobs.copy(),
379+
Proofs: b.Proofs.copy(),
343380
}
344381
}
345382

@@ -351,8 +388,8 @@ func (b *BlobTxWrapData) blobs() Blobs {
351388
return b.Blobs
352389
}
353390

354-
func (b *BlobTxWrapData) aggregatedProof() KZGProof {
355-
return b.KzgAggregatedProof
391+
func (b *BlobTxWrapData) proofs() KZGProofs {
392+
return b.Proofs
356393
}
357394

358395
func (b *BlobTxWrapData) encodeTyped(w io.Writer, txdata TxData) error {
@@ -364,10 +401,10 @@ func (b *BlobTxWrapData) encodeTyped(w io.Writer, txdata TxData) error {
364401
return fmt.Errorf("expected signed blob tx, got %T", txdata)
365402
}
366403
wrapped := BlobTxWrapper{
367-
Tx: *blobTx,
368-
BlobKzgs: b.BlobKzgs,
369-
Blobs: b.Blobs,
370-
KzgAggregatedProof: b.KzgAggregatedProof,
404+
Tx: *blobTx,
405+
BlobKzgs: b.BlobKzgs,
406+
Blobs: b.Blobs,
407+
Proofs: b.Proofs,
371408
}
372409
return EncodeSSZ(w, &wrapped)
373410
}

core/types/transaction.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type TxWrapData interface {
9595
copy() TxWrapData
9696
kzgs() BlobKzgs
9797
blobs() Blobs
98-
aggregatedProof() KZGProof
98+
proofs() KZGProofs
9999
encodeTyped(w io.Writer, txdata TxData) error
100100
sizeWrapData() common.StorageSize
101101
validateBlobTransactionWrapper(inner TxData) error
@@ -270,7 +270,7 @@ func (tx *Transaction) decodeTyped(b []byte) (TxData, TxWrapData, error) {
270270
case BlobTxType:
271271
var wrapped BlobTxWrapper
272272
err := DecodeSSZ(b[1:], &wrapped)
273-
return &wrapped.Tx, &BlobTxWrapData{BlobKzgs: wrapped.BlobKzgs, Blobs: wrapped.Blobs, KzgAggregatedProof: wrapped.KzgAggregatedProof}, err
273+
return &wrapped.Tx, &BlobTxWrapData{BlobKzgs: wrapped.BlobKzgs, Blobs: wrapped.Blobs, Proofs: wrapped.Proofs}, err
274274
default:
275275
minimal, err := tx.decodeTypedMinimal(b)
276276
return minimal, nil, err
@@ -555,13 +555,13 @@ func (tx *Transaction) VerifyBlobs() error {
555555

556556
// BlobWrapData returns the blob and kzg data, if any.
557557
// kzgs and blobs may be empty if the transaction is not wrapped.
558-
func (tx *Transaction) BlobWrapData() (versionedHashes []common.Hash, kzgs BlobKzgs, blobs Blobs, aggProof KZGProof) {
558+
func (tx *Transaction) BlobWrapData() (versionedHashes []common.Hash, kzgs BlobKzgs, blobs Blobs, proofs KZGProofs) {
559559
if blobWrap, ok := tx.wrapData.(*BlobTxWrapData); ok {
560560
if signedBlobTx, ok := tx.inner.(*SignedBlobTx); ok {
561-
return signedBlobTx.Message.BlobVersionedHashes, blobWrap.BlobKzgs, blobWrap.Blobs, blobWrap.KzgAggregatedProof
561+
return signedBlobTx.Message.BlobVersionedHashes, blobWrap.BlobKzgs, blobWrap.Blobs, blobWrap.Proofs
562562
}
563563
}
564-
return nil, nil, nil, KZGProof{}
564+
return nil, nil, nil, nil
565565
}
566566

567567
// WithSignature returns a new transaction with the given signature.

core/types/transaction_marshalling.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type txJSON struct {
5454
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"`
5555
Blobs Blobs `json:"blobs,omitempty"`
5656
BlobKzgs BlobKzgs `json:"blobKzgs,omitempty"`
57-
KzgAggregatedProof KZGProof `json:"kzgAggregatedProof,omitempty"`
57+
Proofs KZGProofs `json:"proofs,omitempty"`
5858

5959
// Only used for encoding:
6060
Hash common.Hash `json:"hash"`
@@ -123,7 +123,7 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) {
123123
if tx.wrapData != nil {
124124
enc.Blobs = tx.wrapData.blobs()
125125
enc.BlobKzgs = tx.wrapData.kzgs()
126-
enc.KzgAggregatedProof = tx.wrapData.aggregatedProof()
126+
enc.Proofs = tx.wrapData.proofs()
127127
}
128128
}
129129
return json.Marshal(&enc)
@@ -362,9 +362,9 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
362362
// A BlobTx may not contain data
363363
if len(dec.Blobs) != 0 || len(dec.BlobKzgs) != 0 {
364364
tx.wrapData = &BlobTxWrapData{
365-
BlobKzgs: dec.BlobKzgs,
366-
Blobs: dec.Blobs,
367-
KzgAggregatedProof: dec.KzgAggregatedProof,
365+
BlobKzgs: dec.BlobKzgs,
366+
Blobs: dec.Blobs,
367+
Proofs: dec.Proofs,
368368
}
369369
// Verify that versioned hashes match kzgs, and kzgs match blobs.
370370
if err := tx.wrapData.validateBlobTransactionWrapper(&itx); err != nil {

core/types/transaction_signing_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ func TestEIP4844Signing(t *testing.T) {
9999
// This is the identity point serialised
100100
var kzgProof KZGProof = [48]byte{192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
101101
wrapData := &BlobTxWrapData{
102-
BlobKzgs: BlobKzgs{KZGCommitment{0: 0xc0}},
103-
Blobs: Blobs{Blob{}},
104-
KzgAggregatedProof: kzgProof,
102+
BlobKzgs: BlobKzgs{KZGCommitment{0: 0xc0}},
103+
Blobs: Blobs{Blob{}},
104+
Proofs: KZGProofs{kzgProof},
105105
}
106106
tx := NewTx(txdata, WithTxWrapData(wrapData))
107107
tx, err := SignTx(tx, signer, key)

core/types/transaction_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,9 @@ func TestTransactionCoding(t *testing.T) {
495495
// This is the identity point serialised
496496
var kzgProof KZGProof = [48]byte{192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
497497
wrapData = &BlobTxWrapData{
498-
BlobKzgs: BlobKzgs{KZGCommitment{0: 0xc0}},
499-
Blobs: Blobs{Blob{}},
500-
KzgAggregatedProof: kzgProof,
498+
BlobKzgs: BlobKzgs{KZGCommitment{0: 0xc0}},
499+
Blobs: Blobs{Blob{}},
500+
Proofs: KZGProofs{kzgProof},
501501
}
502502
}
503503
tx, err := SignNewTx(key, signer, txdata, WithTxWrapData(wrapData))

core/types/types_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ func benchRLP(b *testing.B, encode bool) {
149149
BlobVersionedHashes: VersionedHashesView{common.Hash{0xaa}},
150150
},
151151
}, WithTxWrapData(&BlobTxWrapData{
152-
BlobKzgs: BlobKzgs{KZGCommitment{0xbb}},
153-
Blobs: Blobs{Blob{}},
154-
KzgAggregatedProof: KZGProof{0xbc},
152+
BlobKzgs: BlobKzgs{KZGCommitment{0xbb}},
153+
Blobs: Blobs{Blob{}},
154+
Proofs: KZGProofs{KZGProof{0xbc}},
155155
})),
156156
},
157157
} {

eth/catalyst/api_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ func newRandomBlobTx(t *testing.T, chain *core.BlockChain, nonce uint64) *types.
16141614
var blobs types.Blobs
16151615
blobs = append(blobs, types.Blob{})
16161616

1617-
commitments, versionedHashes, aggregatedProof, err := blobs.ComputeCommitmentsAndAggregatedProof()
1617+
commitments, versionedHashes, proofs, err := blobs.ComputeCommitmentsAndProofs()
16181618
if err != nil {
16191619
t.Fatal(err)
16201620
}
@@ -1639,9 +1639,9 @@ func newRandomBlobTx(t *testing.T, chain *core.BlockChain, nonce uint64) *types.
16391639
},
16401640
}
16411641
wrapData := &types.BlobTxWrapData{
1642-
BlobKzgs: commitments,
1643-
Blobs: blobs,
1644-
KzgAggregatedProof: aggregatedProof,
1642+
BlobKzgs: commitments,
1643+
Blobs: blobs,
1644+
Proofs: proofs,
16451645
}
16461646
tx := types.NewTx(txData, types.WithTxWrapData(wrapData))
16471647
signer := types.NewDankSigner(chainID)

0 commit comments

Comments
 (0)