Skip to content

Commit

Permalink
feat: add method to compute cells
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed Feb 21, 2025
1 parent 1afb045 commit 37673f5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
30 changes: 28 additions & 2 deletions api_eip7594.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ import (
kzgmulti "github.com/crate-crypto/go-eth-kzg/internal/kzg_multi"
)

func (ctx *Context) ComputeCells(blob *Blob, numGoRoutines int) ([CellsPerExtBlob]*Cell, error) {
polynomial, err := DeserializeBlob(blob)
if err != nil {
return [CellsPerExtBlob]*Cell{}, err
}

// Bit reverse the polynomial representing the Blob so that it is in normal order
domain.BitReverse(polynomial)

// Convert the polynomial in lagrange form to a polynomial in monomial form
polyCoeff := ctx.domain.IfftFr(polynomial)

// Extend the polynomial
cosetEvaluations := ctx.fk20.ComputeExtendedPolynomial(polyCoeff)

return serializeCells(cosetEvaluations)
}

func (ctx *Context) ComputeCellsAndKZGProofs(blob *Blob, numGoRoutines int) ([CellsPerExtBlob]*Cell, [CellsPerExtBlob]KZGProof, error) {
polynomial, err := DeserializeBlob(blob)
if err != nil {
Expand Down Expand Up @@ -45,17 +63,25 @@ func (ctx *Context) computeCellsAndKZGProofsFromPolyCoeff(polyCoeff []fr.Element
}

// Serialize Cells
cells, err := serializeCells(cosetEvaluations)
if err != nil {
return [CellsPerExtBlob]*Cell{}, [CellsPerExtBlob]KZGProof{}, err
}
return cells, serializedProofs, nil
}

func serializeCells(cosetEvaluations [][]fr.Element) ([CellsPerExtBlob]*Cell, error) {
var Cells [CellsPerExtBlob]*Cell
for i, cosetEval := range cosetEvaluations {
if len(cosetEval) != scalarsPerCell {
return [CellsPerExtBlob]*Cell{}, [CellsPerExtBlob]KZGProof{}, ErrCosetEvaluationLengthCheck
return [CellsPerExtBlob]*Cell{}, ErrCosetEvaluationLengthCheck
}
cosetEvalArr := (*[scalarsPerCell]fr.Element)(cosetEval)

Cells[i] = serializeEvaluations(cosetEvalArr)
}

return Cells, serializedProofs, nil
return Cells, nil
}

func (ctx *Context) RecoverCellsAndComputeKZGProofs(cellIDs []uint64, cells []*Cell, numGoRoutines int) ([CellsPerExtBlob]*Cell, [CellsPerExtBlob]KZGProof, error) {
Expand Down
3 changes: 3 additions & 0 deletions consensus_specs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,15 @@ func TestComputeCellsAndKZGProofs(t *testing.T) {
}

cells, proofs, err := ctx.ComputeCellsAndKZGProofs(blob, 0)
cells_, err_ := ctx.ComputeCells(blob, 0)

if err == nil {
expectedCellStrs := (*test.Output)[0]
expectedCells, err := hexStrArrToCells(expectedCellStrs)
require.NoError(t, err)
require.NoError(t, err_)
require.Equal(t, expectedCells, cells[:])
require.Equal(t, cells_[:], cells[:])

expectedProofStrs := (*test.Output)[1]
expectedProofs, err := HexStrArrToProofs(expectedProofStrs)
Expand Down
6 changes: 5 additions & 1 deletion internal/kzg_multi/fk20/fk20.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ func (fk *FK20) computeEvaluationSet(polyCoeff []fr.Element) [][]fr.Element {
return partition(evaluations, fk.evalSetSize)
}

func (fk *FK20) ComputeExtendedPolynomial(poly []fr.Element) [][]fr.Element {
return fk.computeEvaluationSet(poly)
}

func (fk *FK20) ComputeMultiOpenProof(poly []fr.Element) ([]bls12381.G1Affine, [][]fr.Element, error) {
// Note: `computeEvaluationSet` will create a copy of `poly`
// and pad it. Hence, the rest of this method, does not use the padded
// version of `poly`.
outputSets := fk.computeEvaluationSet(poly)
outputSets := fk.ComputeExtendedPolynomial(poly)

hComms, err := fk.computeHPolysComm(poly)
if err != nil {
Expand Down

0 comments on commit 37673f5

Please sign in to comment.