-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Layr-Labs/encoding-lib
Encoding lib
- Loading branch information
Showing
31 changed files
with
366 additions
and
651 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
test/resources/kzg/* | ||
test/resources/kzg/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package encoding | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/Layr-Labs/eigenda/core" | ||
enc "github.com/Layr-Labs/eigenda/pkg/encoding/encoder" | ||
kzgEnc "github.com/Layr-Labs/eigenda/pkg/encoding/kzgEncoder" | ||
"github.com/Layr-Labs/eigenda/pkg/kzg/bn254" | ||
) | ||
|
||
var ( | ||
ErrConglomFeatureNotSupported = errors.New("conglomerate feature not supported") | ||
) | ||
|
||
type Conglomerate struct { | ||
data [][]byte | ||
header core.ConglomerateHeader | ||
} | ||
|
||
func (c *Conglomerate) Data() [][]byte { | ||
return c.data | ||
} | ||
|
||
func (c *Conglomerate) Header() core.ConglomerateHeader { | ||
return c.header | ||
} | ||
|
||
func toEncParams(params core.EncodingParams) enc.EncodingParams { | ||
return enc.EncodingParams{ | ||
NumChunks: uint64(params.NumChunks), | ||
ChunkLen: uint64(params.ChunkSize), | ||
} | ||
} | ||
|
||
type Encoder struct { | ||
EncoderGroup kzgEnc.KzgEncoderGroup | ||
} | ||
|
||
func (e *Encoder) Encode(data [][]byte, params core.EncodingParams) (core.ConglomerateCommitments, []core.Chunk, error) { | ||
|
||
encParams := toEncParams(params) | ||
|
||
encoder, err := e.EncoderGroup.GetKzgEncoder(encParams) | ||
if err != nil { | ||
return core.ConglomerateCommitments{}, nil, err | ||
} | ||
if len(data) != 1 { | ||
return core.ConglomerateCommitments{}, nil, ErrConglomFeatureNotSupported | ||
} | ||
|
||
blob := data[0] | ||
|
||
commit, lowDegreeProof, kzgFrames, _, err := encoder.EncodeBytes(blob) | ||
if err != nil { | ||
return core.ConglomerateCommitments{}, nil, ErrConglomFeatureNotSupported | ||
} | ||
|
||
chunks := make([]core.Chunk, len(kzgFrames)) | ||
for ind, frame := range kzgFrames { | ||
|
||
chunks[ind] = core.Chunk{ | ||
Data: frame.Coeffs, | ||
Proof: frame.Proof, | ||
} | ||
|
||
} | ||
|
||
degree := uint(len(enc.ToFrArray(blob))) | ||
commitments := core.ConglomerateCommitments{ | ||
Commitment: commit, | ||
DegreeProof: lowDegreeProof, | ||
Degree: degree, | ||
} | ||
|
||
return commitments, chunks, nil | ||
|
||
} | ||
|
||
func (e *Encoder) VerifyChunks(chunks []core.Chunk, indices []core.ChunkIndex, commitments core.ConglomerateCommitments, params core.EncodingParams) error { | ||
|
||
encParams := toEncParams(params) | ||
|
||
verifier, err := e.EncoderGroup.GetKzgVerifier(encParams) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = verifier.VerifyCommit(commitments.Commitment.(*bn254.G1Point), commitments.DegreeProof.(*bn254.G1Point), uint64(commitments.Degree)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for ind := range chunks { | ||
err = verifier.VerifyFrame( | ||
commitments.Commitment.(*bn254.G1Point), | ||
chunks[ind].Data.(*kzgEnc.Frame), | ||
uint64(indices[ind]), | ||
) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.