From f665f7dbab88e0689719dc08400f8acf8af20ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Gamenik?= Date: Sun, 15 Mar 2026 07:35:34 +0100 Subject: [PATCH] crypto/kzg4844: List semantic for CalcBlobHAshV1 --- core/types/tx_blob.go | 14 +++++--------- crypto/kzg4844/kzg4844.go | 10 ++++++++++ eth/catalyst/simulated_beacon.go | 11 ++++++----- internal/ethapi/transaction_args.go | 6 +----- signer/core/apitypes/types.go | 6 +----- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/core/types/tx_blob.go b/core/types/tx_blob.go index 31aadb5419..430e7177f2 100644 --- a/core/types/tx_blob.go +++ b/core/types/tx_blob.go @@ -18,7 +18,6 @@ package types import ( "bytes" - "crypto/sha256" "errors" "fmt" "math/big" @@ -87,11 +86,8 @@ func NewBlobTxSidecar(version byte, blobs []kzg4844.Blob, commitments []kzg4844. // BlobHashes computes the blob hashes of the given blobs. func (sc *BlobTxSidecar) BlobHashes() []common.Hash { - hasher := sha256.New() h := make([]common.Hash, len(sc.Commitments)) - for i := range sc.Blobs { - h[i] = kzg4844.CalcBlobHashV1(hasher, &sc.Commitments[i]) - } + kzg4844.CalcBlobHashV1List(sc.Commitments, h) return h } @@ -153,11 +149,11 @@ func (sc *BlobTxSidecar) ValidateBlobCommitmentHashes(hashes []common.Hash) erro if len(sc.Commitments) != len(hashes) { return fmt.Errorf("invalid number of %d blob commitments compared to %d blob hashes", len(sc.Commitments), len(hashes)) } - hasher := sha256.New() + computed := make([]common.Hash, len(sc.Commitments)) + kzg4844.CalcBlobHashV1List(sc.Commitments, computed) for i, vhash := range hashes { - computed := kzg4844.CalcBlobHashV1(hasher, &sc.Commitments[i]) - if vhash != computed { - return fmt.Errorf("blob %d: computed hash %#x mismatches transaction one %#x", i, computed, vhash) + if vhash != computed[i] { + return fmt.Errorf("blob %d: computed hash %#x mismatches transaction one %#x", i, computed[i], vhash) } } return nil diff --git a/crypto/kzg4844/kzg4844.go b/crypto/kzg4844/kzg4844.go index 3ccc204838..49e969e382 100644 --- a/crypto/kzg4844/kzg4844.go +++ b/crypto/kzg4844/kzg4844.go @@ -18,12 +18,14 @@ package kzg4844 import ( + "crypto/sha256" "embed" "errors" "hash" "reflect" "sync/atomic" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ) @@ -185,6 +187,14 @@ func CalcBlobHashV1(hasher hash.Hash, commit *Commitment) (vh [32]byte) { return vh } +// Hashes List of commitments +func CalcBlobHashV1List(list []Commitment, hashes []common.Hash) { + hasher := sha256.New() + for nr, commit := range list { + hashes[nr] = CalcBlobHashV1(hasher, &commit) + } +} + // IsValidVersionedHash checks that h is a structurally-valid versioned blob hash. func IsValidVersionedHash(h []byte) bool { return len(h) == 32 && h[0] == 0x01 diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index 452902c78c..78048d81a3 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -19,7 +19,6 @@ package catalyst import ( "context" "crypto/rand" - "crypto/sha256" "errors" "fmt" "math" @@ -253,15 +252,17 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u // Independently calculate the blob hashes from sidecars. blobHashes = make([]common.Hash, 0) if envelope.BlobsBundle != nil { - hasher := sha256.New() - for _, commit := range envelope.BlobsBundle.Commitments { + blobHashes = make([]common.Hash, len(envelope.BlobsBundle.Commitments)) + commits := make([]kzg4844.Commitment, len(envelope.BlobsBundle.Commitments)) + for nr, commit := range envelope.BlobsBundle.Commitments { var c kzg4844.Commitment if len(commit) != len(c) { return errors.New("invalid commitment length") } - copy(c[:], commit) - blobHashes = append(blobHashes, kzg4844.CalcBlobHashV1(hasher, &c)) + copy(commits[nr][:], commit) } + + kzg4844.CalcBlobHashV1List(commits, blobHashes) } beaconRoot = &common.Hash{} requests = envelope.Requests diff --git a/internal/ethapi/transaction_args.go b/internal/ethapi/transaction_args.go index 4fb30e6289..3cb13a85d7 100644 --- a/internal/ethapi/transaction_args.go +++ b/internal/ethapi/transaction_args.go @@ -19,7 +19,6 @@ package ethapi import ( "bytes" "context" - "crypto/sha256" "errors" "fmt" "math" @@ -371,10 +370,7 @@ func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config sideca // Generate blob hashes if they are missing, or validate them if they are provided. hashes := make([]common.Hash, n) - hasher := sha256.New() - for i, c := range args.Commitments { - hashes[i] = kzg4844.CalcBlobHashV1(hasher, &c) - } + kzg4844.CalcBlobHashV1List(args.Commitments, hashes) if args.BlobHashes != nil { for i, h := range hashes { if h != args.BlobHashes[i] { diff --git a/signer/core/apitypes/types.go b/signer/core/apitypes/types.go index 401f4fba07..5a50cbf421 100644 --- a/signer/core/apitypes/types.go +++ b/signer/core/apitypes/types.go @@ -18,7 +18,6 @@ package apitypes import ( "bytes" - "crypto/sha256" "encoding/json" "errors" "fmt" @@ -291,10 +290,7 @@ func (args *SendTxArgs) validateTxSidecar() error { } hashes := make([]common.Hash, n) - hasher := sha256.New() - for i, c := range args.Commitments { - hashes[i] = kzg4844.CalcBlobHashV1(hasher, &c) - } + kzg4844.CalcBlobHashV1List(args.Commitments, hashes) if args.BlobHashes != nil { for i, h := range hashes { if h != args.BlobHashes[i] {