mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
crypto/kzg4844: add helper function CalcBlobHashV1
The code to compute a versioned hash was duplicated a couple times, and also had a small issue: if we ever change params.BlobTxHashVersion, it will most likely also cause changes to the actual hash computation. So it's a bit useless to have this constant in params.
This commit is contained in:
parent
2e2e89c2fb
commit
7cdd355939
4 changed files with 21 additions and 34 deletions
|
|
@ -51,21 +51,9 @@ var (
|
||||||
emptyBlob = kzg4844.Blob{}
|
emptyBlob = kzg4844.Blob{}
|
||||||
emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob)
|
emptyBlobCommit, _ = kzg4844.BlobToCommitment(emptyBlob)
|
||||||
emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit)
|
emptyBlobProof, _ = kzg4844.ComputeBlobProof(emptyBlob, emptyBlobCommit)
|
||||||
emptyBlobVHash = blobHash(emptyBlobCommit)
|
emptyBlobVHash = kzg4844.CalcBlobHashV1(sha256.New(), &emptyBlobCommit)
|
||||||
)
|
)
|
||||||
|
|
||||||
func blobHash(commit kzg4844.Commitment) common.Hash {
|
|
||||||
hasher := sha256.New()
|
|
||||||
hasher.Write(commit[:])
|
|
||||||
hash := hasher.Sum(nil)
|
|
||||||
|
|
||||||
var vhash common.Hash
|
|
||||||
vhash[0] = params.BlobTxHashVersion
|
|
||||||
copy(vhash[1:], hash[1:])
|
|
||||||
|
|
||||||
return vhash
|
|
||||||
}
|
|
||||||
|
|
||||||
// Chain configuration with Cancun enabled.
|
// Chain configuration with Cancun enabled.
|
||||||
//
|
//
|
||||||
// TODO(karalabe): replace with params.MainnetChainConfig after Cancun.
|
// TODO(karalabe): replace with params.MainnetChainConfig after Cancun.
|
||||||
|
|
|
||||||
|
|
@ -143,17 +143,10 @@ func validateBlobSidecar(hashes []common.Hash, sidecar *types.BlobTxSidecar) err
|
||||||
// Blob quantities match up, validate that the provers match with the
|
// Blob quantities match up, validate that the provers match with the
|
||||||
// transaction hash before getting to the cryptography
|
// transaction hash before getting to the cryptography
|
||||||
hasher := sha256.New()
|
hasher := sha256.New()
|
||||||
for i, want := range hashes {
|
for i, vhash := range hashes {
|
||||||
hasher.Write(sidecar.Commitments[i][:])
|
computed := kzg4844.CalcBlobHashV1(hasher, &sidecar.Commitments[i])
|
||||||
hash := hasher.Sum(nil)
|
if vhash != computed {
|
||||||
hasher.Reset()
|
return fmt.Errorf("blob %d: computed hash %#x mismatches transaction one %#x", i, computed, vhash)
|
||||||
|
|
||||||
var vhash common.Hash
|
|
||||||
vhash[0] = params.BlobTxHashVersion
|
|
||||||
copy(vhash[1:], hash[1:])
|
|
||||||
|
|
||||||
if vhash != want {
|
|
||||||
return fmt.Errorf("blob %d: computed hash %#x mismatches transaction one %#x", i, vhash, want)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Blob commitments match with the hashes in the transaction, verify the
|
// Blob commitments match with the hashes in the transaction, verify the
|
||||||
|
|
|
||||||
|
|
@ -61,9 +61,10 @@ type BlobTxSidecar struct {
|
||||||
|
|
||||||
// BlobHashes computes the blob hashes of the given blobs.
|
// BlobHashes computes the blob hashes of the given blobs.
|
||||||
func (sc *BlobTxSidecar) BlobHashes() []common.Hash {
|
func (sc *BlobTxSidecar) BlobHashes() []common.Hash {
|
||||||
|
hasher := sha256.New()
|
||||||
h := make([]common.Hash, len(sc.Commitments))
|
h := make([]common.Hash, len(sc.Commitments))
|
||||||
for i := range sc.Blobs {
|
for i := range sc.Blobs {
|
||||||
h[i] = blobHash(&sc.Commitments[i])
|
h[i] = kzg4844.CalcBlobHashV1(hasher, &sc.Commitments[i])
|
||||||
}
|
}
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
@ -235,12 +236,3 @@ func (tx *BlobTx) decode(input []byte) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func blobHash(commit *kzg4844.Commitment) common.Hash {
|
|
||||||
hasher := sha256.New()
|
|
||||||
hasher.Write(commit[:])
|
|
||||||
var vhash common.Hash
|
|
||||||
hasher.Sum(vhash[:0])
|
|
||||||
vhash[0] = params.BlobTxHashVersion
|
|
||||||
return vhash
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ package kzg4844
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"errors"
|
"errors"
|
||||||
|
"hash"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -108,3 +109,16 @@ func VerifyBlobProof(blob Blob, commitment Commitment, proof Proof) error {
|
||||||
}
|
}
|
||||||
return gokzgVerifyBlobProof(blob, commitment, proof)
|
return gokzgVerifyBlobProof(blob, commitment, proof)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CalcBlobHashV1 calculates the 'versioned blob hash' of a commitment.
|
||||||
|
// The given hasher must be a sha256 hash instance, otherwise the result will be invalid!
|
||||||
|
func CalcBlobHashV1(hasher hash.Hash, commit *Commitment) (vh [32]byte) {
|
||||||
|
if hasher.Size() != 32 {
|
||||||
|
panic("wrong hash size")
|
||||||
|
}
|
||||||
|
hasher.Reset()
|
||||||
|
hasher.Write(commit[:])
|
||||||
|
hasher.Sum(vh[:0])
|
||||||
|
vh[0] = 0x01 // version
|
||||||
|
return vh
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue