Skip to content

Commit

Permalink
chore!: Use RecoverCellsAndKZGProofs instead of RecoverAllCells
Browse files Browse the repository at this point in the history
…-> `CellsToBlob` -> `ComputeCellsAndKZGProofs` (#14183)

* use recoverCellsAndKZGProofs

* make recoverAllCells and CellsToBlob private

* chore: all methods now return CellsAndProof struct

* chore: update code
  • Loading branch information
kevaundray authored and nalepae committed Oct 23, 2024
1 parent d0a2295 commit f8473ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
25 changes: 13 additions & 12 deletions beacon-chain/blockchain/kzg/kzg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions beacon-chain/core/peerdas/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}

Expand Down
19 changes: 4 additions & 15 deletions beacon-chain/sync/data_columns_reconstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit f8473ba

Please sign in to comment.