Skip to content

Commit

Permalink
fix comments to add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Feb 3, 2024
1 parent e96d867 commit c2a1ad8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 17 deletions.
4 changes: 2 additions & 2 deletions core/encoding/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type Encoder struct {

var _ core.Encoder = &Encoder{}

func NewEncoder(config EncoderConfig, isEncoder bool) (*Encoder, error) {
kzgEncoderGroup, err := kzgEncoder.NewKzgEncoderGroup(&config.KzgConfig, isEncoder)
func NewEncoder(config EncoderConfig, loadG2Points bool) (*Encoder, error) {
kzgEncoderGroup, err := kzgEncoder.NewKzgEncoderGroup(&config.KzgConfig, loadG2Points)
if err != nil {
return nil, err
}
Expand Down
67 changes: 64 additions & 3 deletions disperser/encoder/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ var (

var logger = &cmock.Logger{}

func makeTestEncoder() (*encoding.Encoder, ServerConfig) {
func makeTestEncoder(numPoint uint64) (*encoding.Encoder, ServerConfig) {
kzgConfig := kzgEncoder.KzgConfig{
G1Path: "../../inabox/resources/kzg/g1.point",
G2Path: "../../inabox/resources/kzg/g2.point",
CacheDir: "../../inabox/resources/kzg/SRSTables",
SRSOrder: 3000,
SRSNumberToLoad: 3000,
SRSNumberToLoad: numPoint,
NumWorker: uint64(runtime.GOMAXPROCS(0)),
}

Expand All @@ -51,7 +51,7 @@ func makeTestEncoder() (*encoding.Encoder, ServerConfig) {
return encoder, encoderServerConfig
}

var testEncoder, testServerConfig = makeTestEncoder()
var testEncoder, testServerConfig = makeTestEncoder(3000)

func getTestData() (core.Blob, core.EncodingParams) {
var quorumID core.QuorumID = 0
Expand Down Expand Up @@ -246,3 +246,64 @@ func TestThrottling(t *testing.T) {
}
}
}

func TestEncoderPointsLoading(t *testing.T) {
// encoder 1 only loads 1500 points
encoder1, config1 := makeTestEncoder(1500)
metrics := NewMetrics("9000", logger)
server1 := NewServer(config1, logger, encoder1, metrics)

testBlobData, testEncodingParams := getTestData()

testEncodingParamsProto := &pb.EncodingParams{
ChunkLength: uint32(testEncodingParams.ChunkLength),
NumChunks: uint32(testEncodingParams.NumChunks),
}

encodeBlobRequestProto := &pb.EncodeBlobRequest{
Data: []byte(testBlobData.Data),
EncodingParams: testEncodingParamsProto,
}

reply1, err := server1.EncodeBlob(context.Background(), encodeBlobRequestProto)
assert.NoError(t, err)
assert.NotNil(t, reply1.Chunks)

// Decode Server Data
var chunksData []*core.Chunk

for i := range reply1.Chunks {
chunkSerialized, _ := new(core.Chunk).Deserialize(reply1.GetChunks()[i])
// perform an operation
chunksData = append(chunksData, chunkSerialized)
}
assert.NotNil(t, chunksData)

// Indices obtained from Encoder_Test
indices := []core.ChunkNumber{
0, 1, 2, 3, 4, 5, 6, 7,
}

maxInputSize := uint64(len(gettysburgAddressBytes)) + 10
decoded, err := testEncoder.Decode(chunksData, indices, testEncodingParams, maxInputSize)
assert.Nil(t, err)
recovered := bytes.TrimRight(decoded, "\x00")
assert.Equal(t, recovered, gettysburgAddressBytes)

// encoder 2 only loads 2900 points
encoder2, config2 := makeTestEncoder(2900)
server2 := NewServer(config2, logger, encoder2, metrics)

reply2, err := server2.EncodeBlob(context.Background(), encodeBlobRequestProto)
assert.NoError(t, err)
assert.NotNil(t, reply2.Chunks)

for i := range reply2.Chunks {
chunkSerialized, _ := new(core.Chunk).Deserialize(reply2.GetChunks()[i])
// perform an operation
assert.Equal(t, len(chunkSerialized.Coeffs), len(chunksData[i].Coeffs))
assert.Equal(t, chunkSerialized.Coeffs, chunksData[i].Coeffs)
assert.Equal(t, chunkSerialized.Proof, chunksData[i].Proof)
}

}
16 changes: 16 additions & 0 deletions pkg/encoding/kzgEncoder/batchCommitEquivalence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,20 @@ func TestBatchEquivalence(t *testing.T) {
}

assert.False(t, group.BatchVerifyCommitEquivalence(commitPairs) == nil, "batch equivalence negative test failed\n")

for z := 0; z < numBlob; z++ {
inputFr := rs.ToFrArray(GETTYSBURG_ADDRESS_BYTES)

commit, g2commit, _, _, _, err := enc.Encode(inputFr)
require.Nil(t, err)

commitPairs[z] = kzgRs.CommitmentPair{
Commitment: *commit,
LengthCommitment: *g2commit,
}
}

bn254.AddG1(&commitPairs[numBlob/2].Commitment, &commitPairs[numBlob/2].Commitment, &commitPairs[numBlob/2].Commitment)

assert.False(t, group.BatchVerifyCommitEquivalence(commitPairs) == nil, "batch equivalence negative test failed in outer loop\n")
}
22 changes: 12 additions & 10 deletions pkg/encoding/kzgEncoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ type KzgConfig struct {

type KzgEncoderGroup struct {
*KzgConfig
Srs *kzg.SRS
G2Trailing []bls.G2Point
mu sync.Mutex
Srs *kzg.SRS
G2Trailing []bls.G2Point
mu sync.Mutex
LoadG2Points bool

Encoders map[rs.EncodingParams]*KzgEncoder
Verifiers map[rs.EncodingParams]*KzgVerifier
Expand All @@ -55,7 +56,7 @@ type KzgEncoder struct {
FFTPointsT [][]bls.G1Point // transpose of FFTPoints
}

func NewKzgEncoderGroup(config *KzgConfig, isEncoder bool) (*KzgEncoderGroup, error) {
func NewKzgEncoderGroup(config *KzgConfig, loadG2Points bool) (*KzgEncoderGroup, error) {

if config.SRSNumberToLoad > config.SRSOrder {
return nil, errors.New("SRSOrder is less than srsNumberToLoad")
Expand All @@ -72,7 +73,7 @@ func NewKzgEncoderGroup(config *KzgConfig, isEncoder bool) (*KzgEncoderGroup, er
g2Trailing := make([]bls.G2Point, 0)

// PreloadEncoder is by default not used by operator node, PreloadEncoder
if isEncoder {
if loadG2Points {
s2, err = utils.ReadG2Points(config.G2Path, config.SRSNumberToLoad, config.NumWorker)
if err != nil {
log.Println("failed to read G2 points", err)
Expand All @@ -99,11 +100,12 @@ func NewKzgEncoderGroup(config *KzgConfig, isEncoder bool) (*KzgEncoderGroup, er
fmt.Println("numthread", runtime.GOMAXPROCS(0))

encoderGroup := &KzgEncoderGroup{
KzgConfig: config,
Srs: srs,
G2Trailing: g2Trailing,
Encoders: make(map[rs.EncodingParams]*KzgEncoder),
Verifiers: make(map[rs.EncodingParams]*KzgVerifier),
KzgConfig: config,
Srs: srs,
G2Trailing: g2Trailing,
Encoders: make(map[rs.EncodingParams]*KzgEncoder),
Verifiers: make(map[rs.EncodingParams]*KzgVerifier),
LoadG2Points: loadG2Points,
}

if config.PreloadEncoder {
Expand Down
2 changes: 0 additions & 2 deletions pkg/kzg/srs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ package kzg

import bls "github.com/Layr-Labs/eigenda/pkg/kzg/bn254"

//bls "github.com/Layr-Labs/eigenda/pkg/kzg/bn254"

type SRS struct {

// [b.multiply(b.G1, pow(s, i, MODULUS)) for i in range(WIDTH+1)],
Expand Down

0 comments on commit c2a1ad8

Please sign in to comment.