Skip to content

Commit

Permalink
rm custom bn254 under kzg and cleanup (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxue-l2 and [email protected] authored Feb 26, 2024
1 parent 206721f commit 1dc1721
Show file tree
Hide file tree
Showing 104 changed files with 1,354 additions and 4,972 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ inabox/anvil.pid

test/testdata/*
inabox/resources/kzg/SRSTables/*
encoding/kzgrs/prover/data/SRSTable/*

**/bin/*
coverage.*

contracts/broadcast
contracts/broadcast


4 changes: 2 additions & 2 deletions clients/retrieval_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/Layr-Labs/eigenda/common"
"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/Layr-Labs/eigenda/pkg/kzg/bn254"

"github.com/gammazero/workerpool"
"github.com/wealdtech/go-merkletree"
"github.com/wealdtech/go-merkletree/keccak256"
Expand Down Expand Up @@ -172,5 +172,5 @@ func (r *retrievalClient) RetrieveBlob(
indices = append(indices, assignment.GetIndices()...)
}

return r.verifier.Decode(chunks, indices, encodingParams, uint64(blobHeader.Length)*bn254.BYTES_PER_COEFFICIENT)
return r.verifier.Decode(chunks, indices, encodingParams, uint64(blobHeader.Length)*encoding.BYTES_PER_COEFFICIENT)
}
8 changes: 4 additions & 4 deletions clients/tests/retrieval_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
coreindexer "github.com/Layr-Labs/eigenda/core/indexer"
coremock "github.com/Layr-Labs/eigenda/core/mock"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/Layr-Labs/eigenda/encoding/kzgrs"
"github.com/Layr-Labs/eigenda/encoding/kzgrs/prover"
"github.com/Layr-Labs/eigenda/encoding/kzgrs/verifier"
"github.com/Layr-Labs/eigenda/encoding/kzg"
"github.com/Layr-Labs/eigenda/encoding/kzg/prover"
"github.com/Layr-Labs/eigenda/encoding/kzg/verifier"
indexermock "github.com/Layr-Labs/eigenda/indexer/mock"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/stretchr/testify/assert"
Expand All @@ -27,7 +27,7 @@ import (
const numOperators = 10

func makeTestComponents() (encoding.Prover, encoding.Verifier, error) {
config := &kzgrs.KzgConfig{
config := &kzg.KzgConfig{
G1Path: "../../inabox/resources/kzg/g1.point",
G2Path: "../../inabox/resources/kzg/g2.point",
CacheDir: "../../inabox/resources/kzg/SRSTables",
Expand Down
3 changes: 1 addition & 2 deletions core/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/Layr-Labs/eigenda/common"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/Layr-Labs/eigenda/pkg/kzg/bn254"
)

type AccountID = string
Expand Down Expand Up @@ -122,7 +121,7 @@ func (b *BlobHeader) EncodedSizeAllQuorums() int64 {
size := int64(0)
for _, quorum := range b.QuorumInfos {

size += int64(roundUpDivide(b.Length*percentMultiplier*bn254.BYTES_PER_COEFFICIENT, uint(quorum.QuorumThreshold-quorum.AdversaryThreshold)))
size += int64(roundUpDivide(b.Length*percentMultiplier*encoding.BYTES_PER_COEFFICIENT, uint(quorum.QuorumThreshold-quorum.AdversaryThreshold)))
}
return size
}
Expand Down
3 changes: 1 addition & 2 deletions core/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/core/eth"
"github.com/Layr-Labs/eigenda/encoding"
kzgbn254 "github.com/Layr-Labs/eigenda/pkg/kzg/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fp"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -73,7 +72,7 @@ func TestBlobHeaderEncoding(t *testing.T) {
_, err = lengthYA1.SetString("4082367875863433681332203403145435568316851327593401208105741076214120093531")
assert.NoError(t, err)

var lengthProof, lengthCommitment kzgbn254.G2Point
var lengthProof, lengthCommitment bn254.G2Affine
lengthProof.X.A0 = lengthXA0
lengthProof.X.A1 = lengthXA1
lengthProof.Y.A0 = lengthYA0
Expand Down
8 changes: 4 additions & 4 deletions core/test/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/core/mock"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/Layr-Labs/eigenda/encoding/kzgrs"
"github.com/Layr-Labs/eigenda/encoding/kzgrs/prover"
"github.com/Layr-Labs/eigenda/encoding/kzgrs/verifier"
"github.com/Layr-Labs/eigenda/encoding/kzg"
"github.com/Layr-Labs/eigenda/encoding/kzg/prover"
"github.com/Layr-Labs/eigenda/encoding/kzg/verifier"
"github.com/gammazero/workerpool"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -42,7 +42,7 @@ func setup(m *testing.M) {

// makeTestComponents makes a prover and verifier currently using the only supported backend.
func makeTestComponents() (encoding.Prover, encoding.Verifier, error) {
config := &kzgrs.KzgConfig{
config := &kzg.KzgConfig{
G1Path: "../../inabox/resources/kzg/g1.point",
G2Path: "../../inabox/resources/kzg/g2.point",
CacheDir: "../../inabox/resources/kzg/SRSTables",
Expand Down
6 changes: 3 additions & 3 deletions disperser/batcher/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/Layr-Labs/eigenda/disperser/common/inmem"
dmock "github.com/Layr-Labs/eigenda/disperser/mock"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/Layr-Labs/eigenda/encoding/kzgrs"
"github.com/Layr-Labs/eigenda/encoding/kzgrs/prover"
"github.com/Layr-Labs/eigenda/encoding/kzg"
"github.com/Layr-Labs/eigenda/encoding/kzg/prover"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/assert"
Expand All @@ -45,7 +45,7 @@ type batcherComponents struct {
// makeTestEncoder makes an encoder currently using the only supported backend.
func makeTestProver() (encoding.Prover, error) {

config := &kzgrs.KzgConfig{
config := &kzg.KzgConfig{
G1Path: "../../inabox/resources/kzg/g1.point",
G2Path: "../../inabox/resources/kzg/g2.point",
CacheDir: "../../inabox/resources/kzg/SRSTables",
Expand Down
6 changes: 3 additions & 3 deletions disperser/cmd/batcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/Layr-Labs/eigenda/disperser/batcher"
"github.com/Layr-Labs/eigenda/disperser/cmd/batcher/flags"
"github.com/Layr-Labs/eigenda/disperser/common/blobstore"
"github.com/Layr-Labs/eigenda/encoding/kzgrs"
"github.com/Layr-Labs/eigenda/encoding/kzg"
"github.com/Layr-Labs/eigenda/indexer"
"github.com/urfave/cli"
)
Expand All @@ -18,7 +18,7 @@ type Config struct {
BlobstoreConfig blobstore.Config
EthClientConfig geth.EthClientConfig
AwsClientConfig aws.ClientConfig
EncoderConfig kzgrs.KzgConfig
EncoderConfig kzg.KzgConfig
LoggerConfig logging.Config
MetricsConfig batcher.MetricsConfig
IndexerConfig indexer.Config
Expand All @@ -40,7 +40,7 @@ func NewConfig(ctx *cli.Context) Config {
},
EthClientConfig: geth.ReadEthClientConfig(ctx),
AwsClientConfig: aws.ReadClientConfig(ctx, flags.FlagPrefix),
EncoderConfig: kzgrs.ReadCLIConfig(ctx),
EncoderConfig: kzg.ReadCLIConfig(ctx),
LoggerConfig: logging.ReadCLIConfig(ctx, flags.FlagPrefix),
BatcherConfig: batcher.Config{
PullInterval: ctx.GlobalDuration(flags.PullIntervalFlag.Name),
Expand Down
6 changes: 3 additions & 3 deletions disperser/cmd/encoder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"github.com/Layr-Labs/eigenda/common/logging"
"github.com/Layr-Labs/eigenda/disperser/cmd/encoder/flags"
"github.com/Layr-Labs/eigenda/disperser/encoder"
"github.com/Layr-Labs/eigenda/encoding/kzgrs"
"github.com/Layr-Labs/eigenda/encoding/kzg"
"github.com/urfave/cli"
)

type Config struct {
EncoderConfig kzgrs.KzgConfig
EncoderConfig kzg.KzgConfig
LoggerConfig logging.Config
ServerConfig *encoder.ServerConfig
MetricsConfig encoder.MetrisConfig
}

func NewConfig(ctx *cli.Context) Config {
config := Config{
EncoderConfig: kzgrs.ReadCLIConfig(ctx),
EncoderConfig: kzg.ReadCLIConfig(ctx),
LoggerConfig: logging.ReadCLIConfig(ctx, flags.FlagPrefix),
ServerConfig: &encoder.ServerConfig{
GrpcPort: ctx.GlobalString(flags.GrpcPortFlag.Name),
Expand Down
2 changes: 1 addition & 1 deletion disperser/cmd/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/Layr-Labs/eigenda/common"
"github.com/Layr-Labs/eigenda/disperser/encoder"
"github.com/Layr-Labs/eigenda/encoding/kzgrs/prover"
"github.com/Layr-Labs/eigenda/encoding/kzg/prover"
)

type EncoderGRPCServer struct {
Expand Down
4 changes: 2 additions & 2 deletions disperser/cmd/encoder/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package flags
import (
"github.com/Layr-Labs/eigenda/common"
"github.com/Layr-Labs/eigenda/common/logging"
"github.com/Layr-Labs/eigenda/encoding/kzgrs"
"github.com/Layr-Labs/eigenda/encoding/kzg"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -66,6 +66,6 @@ var Flags []cli.Flag

func init() {
Flags = append(requiredFlags, optionalFlags...)
Flags = append(Flags, kzgrs.CLIFlags(envVarPrefix)...)
Flags = append(Flags, kzg.CLIFlags(envVarPrefix)...)
Flags = append(Flags, logging.CLIFlags(envVarPrefix, FlagPrefix)...)
}
10 changes: 5 additions & 5 deletions disperser/encoder/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import (
"testing"
"time"

"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/Layr-Labs/eigenda/encoding/kzg"
encmock "github.com/Layr-Labs/eigenda/encoding/mock"

cmock "github.com/Layr-Labs/eigenda/common/mock"
"github.com/Layr-Labs/eigenda/core"
coremock "github.com/Layr-Labs/eigenda/core/mock"
pb "github.com/Layr-Labs/eigenda/disperser/api/grpc/encoder"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/Layr-Labs/eigenda/encoding/kzgrs"
"github.com/Layr-Labs/eigenda/encoding/kzgrs/prover"
"github.com/Layr-Labs/eigenda/pkg/kzg/bn254"
"github.com/Layr-Labs/eigenda/encoding/kzg/prover"
)

var (
Expand All @@ -33,7 +33,7 @@ var (
var logger = &cmock.Logger{}

func makeTestProver(numPoint uint64) (encoding.Prover, ServerConfig) {
kzgConfig := &kzgrs.KzgConfig{
kzgConfig := &kzg.KzgConfig{
G1Path: "../../inabox/resources/kzg/g1.point",
G2Path: "../../inabox/resources/kzg/g2.point",
CacheDir: "../../inabox/resources/kzg/SRSTables",
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestThrottling(t *testing.T) {
_, err = lengthYA1.SetString("4082367875863433681332203403145435568316851327593401208105741076214120093531")
assert.NoError(t, err)

var lengthProof, lengthCommitment bn254.G2Point
var lengthProof, lengthCommitment bn254.G2Affine
lengthProof.X.A0 = lengthXA0
lengthProof.X.A1 = lengthXA1
lengthProof.Y.A0 = lengthYA0
Expand Down
75 changes: 32 additions & 43 deletions pkg/kzg/bn254/globals.go → encoding/constants.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
package bn254
package encoding

func ToFr(v string) (out Fr) {
SetFr(&out, v)
return
}
import (
"fmt"

"github.com/consensys/gnark-crypto/ecc/bn254/fr"
)

const BYTES_PER_COEFFICIENT = 31

var Scale2RootOfUnity []Fr
var ZERO, ONE, TWO Fr
var MODULUS_MINUS1, MODULUS_MINUS1_DIV2, MODULUS_MINUS2 Fr
var INVERSE_TWO Fr
func init() {
initGlobals()
}

func ToFr(v string) fr.Element {
var out fr.Element
_, err := out.SetString(v)
if err != nil {
fmt.Println("Failed to initialize Root of Unity")
panic(err)
}
return out
}

var Scale2RootOfUnity []fr.Element
var ZERO, ONE, TWO fr.Element
var MODULUS_MINUS1, MODULUS_MINUS1_DIV2, MODULUS_MINUS2 fr.Element
var INVERSE_TWO fr.Element

// copied from https://github.com/adjoint-io/pairing/blob/master/src/Data/Pairing/BN254.hs
func initGlobals() {
Scale2RootOfUnity = []Fr{
Scale2RootOfUnity = []fr.Element{
ToFr("1"),
ToFr("21888242871839275222246405745257275088548364400416034343698204186575808495616"),
ToFr("21888242871839275217838484774961031246007050428528088939761107053157389710902"),
Expand Down Expand Up @@ -46,37 +61,11 @@ func initGlobals() {
ToFr("19103219067921713944291392827692070036145651957329286315305642004821462161904"),
}

AsFr(&ZERO, 0)
AsFr(&ONE, 1)
AsFr(&TWO, 2)

SubModFr(&MODULUS_MINUS1, &ZERO, &ONE)
DivModFr(&MODULUS_MINUS1_DIV2, &MODULUS_MINUS1, &TWO)
SubModFr(&MODULUS_MINUS2, &ZERO, &TWO)
InvModFr(&INVERSE_TWO, &TWO)
}

func IsPowerOfTwo(v uint64) bool {
return v&(v-1) == 0
}

func EvalPolyAtUnoptimized(dst *Fr, coeffs []Fr, x *Fr) {
if len(coeffs) == 0 {
CopyFr(dst, &ZERO)
return
}
if EqualZero(x) {
CopyFr(dst, &coeffs[0])
return
}
// Horner's method: work backwards, avoid doing more than N multiplications
// https://en.wikipedia.org/wiki/Horner%27s_method
var last Fr
CopyFr(&last, &coeffs[len(coeffs)-1])
var tmp Fr
for i := len(coeffs) - 2; i >= 0; i-- {
MulModFr(&tmp, &last, x)
AddModFr(&last, &tmp, &coeffs[i])
}
CopyFr(dst, &last)
ZERO.SetZero()
ONE.SetOne()
TWO.SetInt64(int64(2))
MODULUS_MINUS1.Sub(&ZERO, &ONE)
MODULUS_MINUS1_DIV2.Div(&MODULUS_MINUS1, &TWO)
MODULUS_MINUS2.Sub(&ZERO, &TWO)
INVERSE_TWO.Inverse(&TWO)
}
15 changes: 9 additions & 6 deletions encoding/data.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package encoding

import "github.com/Layr-Labs/eigenda/pkg/kzg/bn254"
import (
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
)

// Commitment is a polynomial commitment (e.g. a kzg commitment)
type G1Commitment bn254.G1Point
type G1Commitment bn254.G1Affine

// Commitment is a polynomial commitment (e.g. a kzg commitment)
type G2Commitment bn254.G2Point
type G2Commitment bn254.G2Affine

// LengthProof is a polynomial commitment on G2 (e.g. a kzg commitment) used for low degree proof
type LengthProof = G2Commitment

// The proof used to open a commitment. In the case of Kzg, this is also a kzg commitment, and is different from a Commitment only semantically.
type Proof = bn254.G1Point
type Proof = bn254.G1Affine

// Symbol is a symbol in the field used for polynomial commitments
type Symbol = bn254.Fr
type Symbol = fr.Element

// BlomCommitments contains the blob's commitment, degree proof, and the actual degree.
type BlobCommitments struct {
Expand All @@ -39,7 +42,7 @@ func (f *Frame) Length() int {

// Returns the size of chunk in bytes.
func (f *Frame) Size() int {
return f.Length() * bn254.BYTES_PER_COEFFICIENT
return f.Length() * BYTES_PER_COEFFICIENT
}

// Sample is a chunk with associated metadata used by the Universal Batch Verifier
Expand Down
4 changes: 1 addition & 3 deletions pkg/kzg/errors.go → encoding/fft/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package kzg
package fft

import (
"errors"
)

var ErrFrListTooLarge = errors.New("ErrFrListTooLarge")
var ErrG1ListTooLarge = errors.New("ErrG1ListTooLarge")
var ErrZeroPolyTooLarge = errors.New("ErrZeroPolyTooLarge")
var ErrDestNotPowerOfTwo = errors.New("ErrDestNotPowerOfTwo")
var ErrEmptyLeaves = errors.New("ErrEmptyLeaves")
Expand Down
Loading

0 comments on commit 1dc1721

Please sign in to comment.