Skip to content

Commit

Permalink
add erasure-code (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray authored Aug 20, 2024
1 parent d969d3a commit 8c3d851
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
6 changes: 3 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"

"github.com/crate-crypto/go-eth-kzg/internal/domain"
"github.com/crate-crypto/go-eth-kzg/internal/erasure_code"
"github.com/crate-crypto/go-eth-kzg/internal/kzg"
kzgmulti "github.com/crate-crypto/go-eth-kzg/internal/kzg_multi"
"github.com/crate-crypto/go-eth-kzg/internal/kzg_multi/fk20"
)

Expand All @@ -22,7 +22,7 @@ type Context struct {

fk20 *fk20.FK20

dataRecovery *kzgmulti.DataRecovery
dataRecovery *erasure_code.DataRecovery
}

// BlsModulus is the bytes representation of the bls12-381 scalar field modulus.
Expand Down Expand Up @@ -145,6 +145,6 @@ func NewContext4096(trustedSetup *JSONTrustedSetup) (*Context, error) {
// TODO: We could pass it in, but it breaks the API.
// TODO: And although its not an issue now because fft uses just the primitiveGenerator, the extended domain
// TODO: that recovery takes is not bit reversed.
dataRecovery: kzgmulti.NewDataRecovery(scalarsPerCell, ScalarsPerBlob, expansionFactor),
dataRecovery: erasure_code.NewDataRecovery(scalarsPerCell, ScalarsPerBlob, expansionFactor),
}, nil
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package kzgmulti
package erasure_code

import (
"errors"

"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
"github.com/crate-crypto/go-eth-kzg/internal/domain"
kzgmulti "github.com/crate-crypto/go-eth-kzg/internal/kzg_multi"
)

// BlockErasureIndex is used to indicate the index of the block erasure that is missing
Expand Down Expand Up @@ -88,6 +89,17 @@ func (dr *DataRecovery) constructVanishingPolyOnIndices(missingBlockErasureIndic
return zeroPolyCoeff
}

// Encode the polynomial by evaluating it on the extended domain.
//
// Note: `polyCoeff` is mutated in-place, ie it should be seen as mutable reference.
func (dr *DataRecovery) Encode(polyCoeff []fr.Element) []fr.Element {
// Pad to the correct length
for i := len(polyCoeff); i < len(dr.domainExtended.Roots); i++ {
polyCoeff = append(polyCoeff, fr.Element{})
}
return dr.domainExtended.FftFr(polyCoeff)
}

// NumBlocksNeededToReconstruct returns the number of blocks that are needed to reconstruct
// the original data word.
func (dr *DataRecovery) NumBlocksNeededToReconstruct() int {
Expand Down Expand Up @@ -126,3 +138,19 @@ func (dr *DataRecovery) RecoverPolynomialCoefficients(data []fr.Element, missing
polyCoeff = polyCoeff[:dr.numScalarsInDataWord]
return polyCoeff, nil
}

// vanishingPolyCoeff returns the polynomial that has roots at the given points
func vanishingPolyCoeff(xs []fr.Element) kzgmulti.PolynomialCoeff {
result := []fr.Element{fr.One()}

for _, x := range xs {
// This is to silence: G601: Implicit memory aliasing in for loop.
x := x

negX := fr.Element{}
negX.Neg(&x)
result = kzgmulti.PolyMul(result, []fr.Element{negX, fr.One()})
}

return result
}
19 changes: 19 additions & 0 deletions internal/erasure_code/erasure_code_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package erasure_code

import (
"testing"

"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
kzgmulti "github.com/crate-crypto/go-eth-kzg/internal/kzg_multi"
)

func TestVanishingPoly(t *testing.T) {
points := []fr.Element{fr.NewElement(1), fr.NewElement(2), fr.NewElement(3), fr.NewElement(4)}
vanishingPoly := vanishingPolyCoeff(points)
for _, point := range points {
eval := kzgmulti.PolyEval(vanishingPoly, point)
if !eval.IsZero() {
t.Fatalf("expected evaluation at the vanishing polynomial to be zero")
}
}
}

0 comments on commit 8c3d851

Please sign in to comment.