From f8473ba6aa2b147139179b23ed31f11ac1da2cfe Mon Sep 17 00:00:00 2001 From: kevaundray Date: Tue, 9 Jul 2024 15:20:21 +0100 Subject: [PATCH] chore!: Use `RecoverCellsAndKZGProofs` instead of `RecoverAllCells ` -> `CellsToBlob` -> `ComputeCellsAndKZGProofs` (#14183) * use recoverCellsAndKZGProofs * make recoverAllCells and CellsToBlob private * chore: all methods now return CellsAndProof struct * chore: update code --- beacon-chain/blockchain/kzg/kzg.go | 25 ++++++++++--------- beacon-chain/core/peerdas/helpers.go | 15 +++++------ beacon-chain/sync/data_columns_reconstruct.go | 19 +++----------- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/beacon-chain/blockchain/kzg/kzg.go b/beacon-chain/blockchain/kzg/kzg.go index 8454a25b3787..9b9e01e50b3b 100644 --- a/beacon-chain/blockchain/kzg/kzg.go +++ b/beacon-chain/blockchain/kzg/kzg.go @@ -61,11 +61,11 @@ func ComputeBlobKZGProof(blob *Blob, commitment Commitment) (Proof, error) { return Proof(proof), nil } -func ComputeCellsAndKZGProofs(blob *Blob) ([ckzg4844.CellsPerExtBlob]Cell, [ckzg4844.CellsPerExtBlob]Proof, error) { +func ComputeCellsAndKZGProofs(blob *Blob) (CellsAndProofs, error) { ckzgBlob := ckzg4844.Blob(*blob) _cells, _proofs, err := ckzg4844.ComputeCellsAndKZGProofs(&ckzgBlob) if err != nil { - return [ckzg4844.CellsPerExtBlob]Cell{}, [ckzg4844.CellsPerExtBlob]Proof{}, err + return CellsAndProofs{}, err } // Convert Cells and Proofs to types defined in this package @@ -79,7 +79,10 @@ func ComputeCellsAndKZGProofs(blob *Blob) ([ckzg4844.CellsPerExtBlob]Cell, [ckzg proofs[i] = Proof(_proofs[i]) } - return cells, proofs, nil + return CellsAndProofs{ + Cells: cells, + Proofs: proofs, + }, nil } // VerifyCellKZGProof is unused. TODO: We can check when the batch size for `VerifyCellKZGProofBatch` is 1 @@ -98,7 +101,7 @@ func VerifyCellKZGProofBatch(commitmentsBytes []Bytes48, rowIndices, columnIndic return ckzg4844.VerifyCellKZGProofBatch(commitmentsBytes, rowIndices, columnIndices, ckzgCells, proofsBytes) } -func RecoverAllCells(cellIds []uint64, _cells []Cell) ([ckzg4844.CellsPerExtBlob]Cell, error) { +func recoverAllCells(cellIds []uint64, _cells []Cell) ([ckzg4844.CellsPerExtBlob]Cell, error) { // Convert `Cell` type to `ckzg4844.Cell` ckzgCells := make([]ckzg4844.Cell, len(_cells)) for i := range _cells { @@ -124,26 +127,24 @@ func RecoverAllCells(cellIds []uint64, _cells []Cell) ([ckzg4844.CellsPerExtBlob } // RecoverCellsAndKZGProofs recovers the cells and compute the KZG Proofs associated with the cells. -// -// This method will supersede the `RecoverAllCells` and `CellsToBlob` methods. -func RecoverCellsAndKZGProofs(cellIds []uint64, _cells []Cell) ([ckzg4844.CellsPerExtBlob]Cell, [ckzg4844.CellsPerExtBlob]Proof, error) { +func RecoverCellsAndKZGProofs(cellIds []uint64, _cells []Cell) (CellsAndProofs, error) { // First recover all of the cells - recoveredCells, err := RecoverAllCells(cellIds, _cells) + recoveredCells, err := recoverAllCells(cellIds, _cells) if err != nil { - return [ckzg4844.CellsPerExtBlob]Cell{}, [ckzg4844.CellsPerExtBlob]Proof{}, err + return CellsAndProofs{}, err } // Extract the Blob from all of the Cells - blob, err := CellsToBlob(&recoveredCells) + blob, err := cellsToBlob(&recoveredCells) if err != nil { - return [ckzg4844.CellsPerExtBlob]Cell{}, [ckzg4844.CellsPerExtBlob]Proof{}, err + return CellsAndProofs{}, err } // Compute all of the cells and KZG proofs return ComputeCellsAndKZGProofs(&blob) } -func CellsToBlob(_cells *[ckzg4844.CellsPerExtBlob]Cell) (Blob, error) { +func cellsToBlob(_cells *[ckzg4844.CellsPerExtBlob]Cell) (Blob, error) { // Convert `Cell` type to `ckzg4844.Cell` var ckzgCells [ckzg4844.CellsPerExtBlob]ckzg4844.Cell for i := range _cells { diff --git a/beacon-chain/core/peerdas/helpers.go b/beacon-chain/core/peerdas/helpers.go index dcaaee298825..cb433532266b 100644 --- a/beacon-chain/core/peerdas/helpers.go +++ b/beacon-chain/core/peerdas/helpers.go @@ -138,18 +138,16 @@ func DataColumnSidecars(signedBlock interfaces.ReadOnlySignedBeaconBlock, blobs } // Compute cells and proofs. - cells := make([][kzg.CellsPerExtBlob]kzg.Cell, 0, blobsCount) - proofs := make([][kzg.CellsPerExtBlob]kzg.Proof, 0, blobsCount) + cellsAndProofs := make([]kzg.CellsAndProofs, 0, blobsCount) for i := range blobs { blob := &blobs[i] - blobCells, blobProofs, err := kzg.ComputeCellsAndKZGProofs(blob) + blobCellsAndProofs, err := kzg.ComputeCellsAndKZGProofs(blob) if err != nil { return nil, errors.Wrap(err, "compute cells and KZG proofs") } - cells = append(cells, blobCells) - proofs = append(proofs, blobProofs) + cellsAndProofs = append(cellsAndProofs, blobCellsAndProofs) } // Get the column sidecars. @@ -159,10 +157,13 @@ func DataColumnSidecars(signedBlock interfaces.ReadOnlySignedBeaconBlock, blobs kzgProofOfColumn := make([]kzg.Proof, 0, blobsCount) for rowIndex := 0; rowIndex < blobsCount; rowIndex++ { - cell := cells[rowIndex][columnIndex] + cellsForRow := cellsAndProofs[rowIndex].Cells + proofsForRow := cellsAndProofs[rowIndex].Proofs + + cell := cellsForRow[columnIndex] column = append(column, cell) - kzgProof := proofs[rowIndex][columnIndex] + kzgProof := proofsForRow[columnIndex] kzgProofOfColumn = append(kzgProofOfColumn, kzgProof) } diff --git a/beacon-chain/sync/data_columns_reconstruct.go b/beacon-chain/sync/data_columns_reconstruct.go index b5a855fd2f1e..0f0e10153229 100644 --- a/beacon-chain/sync/data_columns_reconstruct.go +++ b/beacon-chain/sync/data_columns_reconstruct.go @@ -60,25 +60,14 @@ func recoverCellsAndProofs( cells = append(cells, kzg.Cell(cell)) } - // Recover the blob. - recoveredCells, err := kzg.RecoverAllCells(cellsId, cells) - if err != nil { - return nil, errors.Wrapf(err, "recover all cells for blob %d", blobIndex) - } + // Recover the cells and proofs for the corresponding blob + cellsAndProofs, err := kzg.RecoverCellsAndKZGProofs(cellsId, cells) - recoveredBlob, err := kzg.CellsToBlob(&recoveredCells) if err != nil { - return nil, errors.Wrapf(err, "cells to blob for blob %d", blobIndex) + return nil, errors.Wrapf(err, "recover cells and KZG proofs for blob %d", blobIndex) } - blobCells, blobProofs, err := kzg.ComputeCellsAndKZGProofs(&recoveredBlob) - if err != nil { - return nil, errors.Wrapf(err, "compute cells and KZG proofs for blob %d", blobIndex) - } - recoveredCellsAndProofs = append(recoveredCellsAndProofs, kzg.CellsAndProofs{ - Cells: blobCells, - Proofs: blobProofs, - }) + recoveredCellsAndProofs = append(recoveredCellsAndProofs, cellsAndProofs) log.WithFields(logrus.Fields{ "elapsed": time.Since(start),