Skip to content

Commit

Permalink
move kzg files to kzgrs and new fft package
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Feb 24, 2024
1 parent 1f93618 commit c5cccd3
Show file tree
Hide file tree
Showing 47 changed files with 253 additions and 726 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


3 changes: 1 addition & 2 deletions clients/retrieval_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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"

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

return r.verifier.Decode(chunks, indices, encodingParams, uint64(blobHeader.Length)*kzg.BYTES_PER_COEFFICIENT)
return r.verifier.Decode(chunks, indices, encodingParams, uint64(blobHeader.Length)*encoding.BYTES_PER_COEFFICIENT)
}
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"
)

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*kzg.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
89 changes: 14 additions & 75 deletions pkg/kzg/constants.go → encoding/constants.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package kzg
package encoding

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

const BYTES_PER_COEFFICIENT = 31

func init() {
initGlobals()
}

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

const BYTES_PER_COEFFICIENT = 31

var Scale2RootOfUnity []fr.Element
var ZERO, ONE, TWO fr.Element
var MODULUS_MINUS1, MODULUS_MINUS1_DIV2, MODULUS_MINUS2 fr.Element
Expand Down Expand Up @@ -60,72 +68,3 @@ func initGlobals() {
MODULUS_MINUS2.Sub(&ZERO, &TWO)
INVERSE_TWO.Inverse(&TWO)
}

func init() {
initGlobals()
initG1G2()
}

var ZERO_G1 bn254.G1Affine
var ZERO_G2 bn254.G2Affine

var GenG1 bn254.G1Affine
var GenG2 bn254.G2Affine

var ZeroG1 bn254.G1Affine
var ZeroG2 bn254.G2Affine

//var InfG1 G1Point
//var InfG2 G2Point

func initG1G2() {

_, _, genG1, genG2 := bn254.Generators()

GenG1 = *(*bn254.G1Affine)(&genG1)
GenG2 = *(*bn254.G2Affine)(&genG2)

var g1Jac bn254.G1Jac
g1Jac.X.SetZero()
g1Jac.Y.SetOne()
g1Jac.Z.SetZero()

var g1Aff bn254.G1Affine
g1Aff.FromJacobian(&g1Jac)
ZeroG1 = *(*bn254.G1Affine)(&g1Aff)

var g2Jac bn254.G2Jac
g2Jac.X.SetZero()
g2Jac.Y.SetOne()
g2Jac.Z.SetZero()
var g2Aff bn254.G2Affine
g2Aff.FromJacobian(&g2Jac)
ZeroG2 = *(*bn254.G2Affine)(&g2Aff)
}

func EvalPolyAt(dst *fr.Element, coeffs []fr.Element, x *fr.Element) {
if len(coeffs) == 0 {
//CopyFr(dst, &ZERO)
dst.SetZero()
return
}
if x.IsZero() {
//CopyFr(dst, &coeffs[0])
dst.Set(&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.Element
//CopyFr(&last, &coeffs[len(coeffs)-1])
last.Set(&coeffs[len(coeffs)-1])
var tmp fr.Element
for i := len(coeffs) - 2; i >= 0; i-- {
tmp.Mul(&last, x)
//MulModFr(&tmp, &last, x)
//AddModFr(&last, &tmp, &coeffs[i])
last.Add(&tmp, &coeffs[i])
}
//CopyFr(dst, &last)
dst.Set(&last)
}
3 changes: 1 addition & 2 deletions encoding/data.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package encoding

import (
"github.com/Layr-Labs/eigenda/pkg/kzg"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
)
Expand Down Expand Up @@ -43,7 +42,7 @@ func (f *Frame) Length() int {

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

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

package kzg
package encoding

import (
"errors"
Expand Down
9 changes: 5 additions & 4 deletions pkg/kzg/fft.go → encoding/fft/fft.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

// Original: https://github.com/ethereum/research/blob/master/mimc_stark/fft.py

package kzg
package fft

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

"math/bits"
Expand Down Expand Up @@ -70,8 +71,8 @@ type FFTSettings struct {

func NewFFTSettings(maxScale uint8) *FFTSettings {
width := uint64(1) << maxScale
root := &Scale2RootOfUnity[maxScale]
rootz := expandRootOfUnity(&Scale2RootOfUnity[maxScale])
root := &encoding.Scale2RootOfUnity[maxScale]
rootz := expandRootOfUnity(&encoding.Scale2RootOfUnity[maxScale])

// reverse roots of unity
rootzReverse := make([]fr.Element, len(rootz))
Expand All @@ -86,4 +87,4 @@ func NewFFTSettings(maxScale uint8) *FFTSettings {
ExpandedRootsOfUnity: rootz,
ReverseRootsOfUnity: rootzReverse,
}
}
}
14 changes: 10 additions & 4 deletions pkg/kzg/fft_fr.go → encoding/fft/fft_fr.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package kzg
package fft

import (
"fmt"

"github.com/Layr-Labs/eigenda/encoding"
rb "github.com/Layr-Labs/eigenda/encoding/utils/reverseBits"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
)

Expand Down Expand Up @@ -149,10 +151,10 @@ func (fs *FFTSettings) InplaceFFT(vals []fr.Element, out []fr.Element, inv bool)
// rearrange Fr elements in reverse bit order. Supports 2**31 max element count.
func reverseBitOrderFr(values []fr.Element) error {
if len(values) > (1 << 31) {
return ErrFrListTooLarge
return encoding.ErrFrListTooLarge
}
var tmp fr.Element
reverseBitOrder(uint32(len(values)), func(i, j uint32) {
rb.ReverseBitOrder(uint32(len(values)), func(i, j uint32) {

Check failure on line 157 in encoding/fft/fft_fr.go

View workflow job for this annotation

GitHub Actions / Linter

Error return value of `rb.ReverseBitOrder` is not checked (errcheck)
tmp.Set(&values[i])
//bls.CopyFr(&tmp, &values[i])
values[i].Set(&values[j])
Expand All @@ -172,4 +174,8 @@ func reverseBitOrderFr(values []fr.Element) error {
// values[i], values[j] = values[j], values[i]
// })
// return nil
// }
// }

func IsPowerOfTwo(v uint64) bool {
return v&(v-1) == 0
}
38 changes: 19 additions & 19 deletions pkg/kzg/fft_fr_test.go → encoding/fft/fft_fr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package kzg
package fft

import (
"testing"
Expand Down Expand Up @@ -62,24 +62,24 @@ func TestInvFFT(t *testing.T) {
require.Nil(t, err)
require.NotNil(t, res)

expected := []fr.Element{
ToFr("10944121435919637611123202872628637544274182200208017171849102093287904247816"),
ToFr("1936030771851033959223912058450265953781825736913396623629635806885115007405"),
ToFr("16407567355707715082381689537916387329395994555403796510305004205827931381005"),
ToFr("10191068092603585790326358584923261075982428954421092317052884890230353083980"),
ToFr("21888242871839275220042445260109153167277707414472061641729655619866599103259"),
ToFr("21152419124866706061239949059012548909204540700669677175965090584889269743773"),
ToFr("16407567355707715086789610508212631171937308527291741914242101339246350165720"),
ToFr("12897381804114154238953344473132041472086565426937872290416035768380869236628"),
ToFr("10944121435919637611123202872628637544274182200208017171849102093287904247808"),
ToFr("8990861067725120983293061272125233616461798973478162053282168418194939258988"),
ToFr("5480675516131560135456795237044643916611055873124292429456102847329458329896"),
ToFr("735823746972569161006456686244726179343823699746357167733113601686538751843"),
ToFr("2203960485148121921270656985943972701968548566709209392357"),
ToFr("11697174779235689431920047160334014012565935445994942026645319296345455411636"),
ToFr("5480675516131560139864716207340887759152369845012237833393199980747877114611"),
ToFr("19952212099988241263022493686807009134766538663502637720068568379690693488211"),
}

expected := make([]fr.Element, 16)
expected[0].SetString("10944121435919637611123202872628637544274182200208017171849102093287904247816")

Check failure on line 67 in encoding/fft/fft_fr_test.go

View workflow job for this annotation

GitHub Actions / Linter

Error return value of `(*github.com/consensys/gnark-crypto/ecc/bn254/fr.Element).SetString` is not checked (errcheck)
expected[1].SetString("1936030771851033959223912058450265953781825736913396623629635806885115007405")

Check failure on line 68 in encoding/fft/fft_fr_test.go

View workflow job for this annotation

GitHub Actions / Linter

Error return value of `(*github.com/consensys/gnark-crypto/ecc/bn254/fr.Element).SetString` is not checked (errcheck)
expected[2].SetString("16407567355707715082381689537916387329395994555403796510305004205827931381005")

Check failure on line 69 in encoding/fft/fft_fr_test.go

View workflow job for this annotation

GitHub Actions / Linter

Error return value of `(*github.com/consensys/gnark-crypto/ecc/bn254/fr.Element).SetString` is not checked (errcheck)
expected[3].SetString("10191068092603585790326358584923261075982428954421092317052884890230353083980")
expected[4].SetString("21888242871839275220042445260109153167277707414472061641729655619866599103259")
expected[5].SetString("21152419124866706061239949059012548909204540700669677175965090584889269743773")
expected[6].SetString("16407567355707715086789610508212631171937308527291741914242101339246350165720")
expected[7].SetString("12897381804114154238953344473132041472086565426937872290416035768380869236628")
expected[8].SetString("10944121435919637611123202872628637544274182200208017171849102093287904247808")
expected[9].SetString("8990861067725120983293061272125233616461798973478162053282168418194939258988")
expected[10].SetString("5480675516131560135456795237044643916611055873124292429456102847329458329896")
expected[11].SetString("735823746972569161006456686244726179343823699746357167733113601686538751843")
expected[12].SetString("2203960485148121921270656985943972701968548566709209392357")
expected[13].SetString("11697174779235689431920047160334014012565935445994942026645319296345455411636")
expected[14].SetString("5480675516131560139864716207340887759152369845012237833393199980747877114611")
expected[15].SetString("19952212099988241263022493686807009134766538663502637720068568379690693488211")

for i := range res {
assert.True(t, res[i].Equal(&expected[i]))
Expand Down
34 changes: 13 additions & 21 deletions pkg/kzg/fft_g1.go → encoding/fft/fft_g1.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//go:build !bignum_pure && !bignum_hol256
// +build !bignum_pure,!bignum_hol256

package kzg
package fft

import (
"fmt"
Expand Down Expand Up @@ -104,10 +104,6 @@ func (fs *FFTSettings) _fftG1(vals []bn254.G1Affine, valsOffset uint64, valsStri
}
}

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

func (fs *FFTSettings) FFTG1(vals []bn254.G1Affine, inv bool) ([]bn254.G1Affine, error) {
n := uint64(len(vals))
if n > fs.MaxWidth {
Expand Down Expand Up @@ -155,19 +151,15 @@ func (fs *FFTSettings) FFTG1(vals []bn254.G1Affine, inv bool) ([]bn254.G1Affine,
}

// rearrange G1 elements in reverse bit order. Supports 2**31 max element count.
func reverseBitOrderG1(values []bn254.G1Affine) error {
if len(values) > (1 << 31) {
return ErrG1ListTooLarge
}
var tmp bn254.G1Affine
reverseBitOrder(uint32(len(values)), func(i, j uint32) {
tmp.Set(&values[i])
values[i].Set(&values[j])
values[j].Set(&tmp)

//bls.CopyG1(&tmp, &values[i])
//bls.CopyG1(&values[i], &values[j])
//bls.CopyG1(&values[j], &tmp)
})
return nil
}
//func reverseBitOrderG1(values []bn254.G1Affine) error {
// if len(values) > (1 << 31) {
// return ErrG1ListTooLarge
// }
// var tmp bn254.G1Affine
// reverseBitOrder(uint32(len(values)), func(i, j uint32) {
// tmp.Set(&values[i])
// values[i].Set(&values[j])
// values[j].Set(&tmp)
// })
// return nil
//}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package kzg
package fft

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package kzg
package fft

import (
"fmt"
Expand Down
Loading

0 comments on commit c5cccd3

Please sign in to comment.