From 4ed2953fcff70f921661f2ffed8b85bbc2a80de9 Mon Sep 17 00:00:00 2001 From: Manu NALEPA Date: Thu, 2 Oct 2025 15:17:11 +0200 Subject: [PATCH] `inclusionProofKey`: Include the commitments in the key. (#15795) * `inclusionProofKey`: Include the commitments in the key. * Fix Potuz's comment. * Update beacon-chain/verification/data_column.go Co-authored-by: Potuz * Fix Potuz's comment. --------- Co-authored-by: Potuz --- beacon-chain/verification/data_column.go | 38 +++++++++++++++++------- changelog/manu-inclusion-cache-key.md | 3 ++ 2 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 changelog/manu-inclusion-cache-key.md diff --git a/beacon-chain/verification/data_column.go b/beacon-chain/verification/data_column.go index 6abd92812c..c6ae4891c3 100644 --- a/beacon-chain/verification/data_column.go +++ b/beacon-chain/verification/data_column.go @@ -2,6 +2,7 @@ package verification import ( "context" + "crypto/sha256" "fmt" "strings" "time" @@ -524,24 +525,39 @@ func columnErrBuilder(baseErr error) error { return errors.Wrap(baseErr, errColumnsInvalid.Error()) } -func inclusionProofKey(c blocks.RODataColumn) ([160]byte, error) { - var key [160]byte - if len(c.KzgCommitmentsInclusionProof) != 4 { +// incluseionProofKey computes a unique key based on the KZG commitments, +// the KZG commitments inclusion proof, and the signed block header root. +func inclusionProofKey(c blocks.RODataColumn) ([32]byte, error) { + const ( + commsIncProofLen = 4 + commsIncProofByteCount = commsIncProofLen * 32 + ) + + if len(c.KzgCommitmentsInclusionProof) != commsIncProofLen { // This should be already enforced by ssz unmarshaling; still check so we don't panic on array bounds. - return key, columnErrBuilder(ErrSidecarInclusionProofInvalid) + return [32]byte{}, columnErrBuilder(ErrSidecarInclusionProofInvalid) } + commsByteCount := len(c.KzgCommitments) * fieldparams.KzgCommitmentSize + unhashedKey := make([]byte, 0, commsIncProofByteCount+fieldparams.RootLength+commsByteCount) + + // Include the commitments inclusion proof in the key. + for _, proof := range c.KzgCommitmentsInclusionProof { + unhashedKey = append(unhashedKey, proof...) + } + + // Include the block root in the key. root, err := c.SignedBlockHeader.HashTreeRoot() if err != nil { - return [160]byte{}, columnErrBuilder(errors.Wrap(err, "hash tree root")) + return [32]byte{}, columnErrBuilder(errors.Wrap(err, "hash tree root")) } - for i := range c.KzgCommitmentsInclusionProof { - if copy(key[32*i:32*i+32], c.KzgCommitmentsInclusionProof[i]) != 32 { - return key, columnErrBuilder(ErrSidecarInclusionProofInvalid) - } + unhashedKey = append(unhashedKey, root[:]...) + + // Include the commitments in the key. + for _, commitment := range c.KzgCommitments { + unhashedKey = append(unhashedKey, commitment...) } - copy(key[128:], root[:]) - return key, nil + return sha256.Sum256(unhashedKey), nil } diff --git a/changelog/manu-inclusion-cache-key.md b/changelog/manu-inclusion-cache-key.md new file mode 100644 index 0000000000..e541de0cdb --- /dev/null +++ b/changelog/manu-inclusion-cache-key.md @@ -0,0 +1,3 @@ +### Fixed +- `inclusionProofKey`: Include the commitments in the key. +