Skip to content

Commit

Permalink
Merge branch 'main' into rp/eds-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed Jul 10, 2023
2 parents ce6d92c + 964ba61 commit f480fb4
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 120 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
)

func main() {
// Size of each share, in bytes
bufferSize := 64
// shareSize is the size of each share (in bytes).
shareSize := 512
// Init new codec
codec := rsmt2d.NewLeoRSCodec()

ones := bytes.Repeat([]byte{1}, bufferSize)
twos := bytes.Repeat([]byte{2}, bufferSize)
threes := bytes.Repeat([]byte{3}, bufferSize)
fours := bytes.Repeat([]byte{4}, bufferSize)
ones := bytes.Repeat([]byte{1}, shareSize)
twos := bytes.Repeat([]byte{2}, shareSize)
threes := bytes.Repeat([]byte{3}, shareSize)
fours := bytes.Repeat([]byte{4}, shareSize)

// Compute parity shares
eds, err := rsmt2d.ComputeExtendedDataSquare(
Expand Down
27 changes: 27 additions & 0 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,30 @@ func generateMissingData(count int, codec Codec) [][]byte {

return output
}

// testCodec is a codec that is used for testing purposes.
type testCodec struct{}

func newTestCodec() Codec {
return &testCodec{}
}

func (c *testCodec) Encode(chunk [][]byte) ([][]byte, error) {
return chunk, nil
}

func (c *testCodec) Decode(chunk [][]byte) ([][]byte, error) {
return chunk, nil
}

func (c *testCodec) MaxChunks() int {
return 0
}

func (c *testCodec) Name() string {
return "testCodec"
}

func (c *testCodec) ValidateChunkSize(chunkSize int) error {

Check warning on line 117 in codec_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unused-parameter: parameter 'chunkSize' seems to be unused, consider removing or renaming it as _ (revive)
return nil
}
3 changes: 3 additions & 0 deletions codecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type Codec interface {
MaxChunks() int
// Name returns the name of the codec.
Name() string
// ValidateChunkSize returns an error if this codec does not support
// chunkSize. Returns nil if chunkSize is supported.
ValidateChunkSize(chunkSize int) error
}

// codecs is a global map used for keeping track of registered codecs for testing and JSON unmarshalling
Expand Down
13 changes: 6 additions & 7 deletions extendeddatacrossword.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ func (e *ErrByzantineData) Error() string {
return fmt.Sprintf("byzantine %s: %d", e.Axis, e.Index)
}

// Repair attempts to repair an incomplete extended data
// square (EDS), comparing repaired rows and columns against expected Merkle
// roots.
//
// # Input
//
// Missing shares must be nil.
// Repair attempts to repair an incomplete extended data square (EDS). The
// parameters rowRoots and colRoots are the expected Merkle roots for each row
// and column. rowRoots and colRoots are used to verify that a repaired row or
// column is correct. Prior to the repair process, if a row or column is already
// complete but the Merkle root for the row or column doesn't match the expected
// root, an error is returned. Missing shares in the EDS must be nil.
//
// # Output
//
Expand Down
45 changes: 18 additions & 27 deletions extendeddatacrossword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/stretchr/testify/require"
)

// shareSize is the size of each share (in bytes) used for testing.
const shareSize = 512

// PseudoFraudProof is an example fraud proof.
// TODO a real fraud proof would have a Merkle proof for each share.
type PseudoFraudProof struct {
Expand All @@ -20,19 +23,16 @@ type PseudoFraudProof struct {
}

func TestRepairExtendedDataSquare(t *testing.T) {
bufferSize := 64
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec Codec
name string
codec Codec
}{
{"leopard", bufferSize, NewLeoRSCodec()},
{"leopard", NewLeoRSCodec()},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
name, codec, shareSize := test.name, test.codec, test.shareSize
name, codec := test.name, test.codec
original := createTestEds(codec, shareSize)

rowRoots, err := original.RowRoots()
Expand Down Expand Up @@ -90,20 +90,17 @@ func TestRepairExtendedDataSquare(t *testing.T) {
}

func TestValidFraudProof(t *testing.T) {
bufferSize := 64
corruptChunk := bytes.Repeat([]byte{66}, bufferSize)
corruptChunk := bytes.Repeat([]byte{66}, shareSize)
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec Codec
name string
codec Codec
}{
{"leopard", bufferSize, NewLeoRSCodec()},
{"leopard", NewLeoRSCodec()},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
name, codec, shareSize := test.name, test.codec, test.shareSize
name, codec := test.name, test.codec
original := createTestEds(codec, shareSize)

var byzData *ErrByzantineData
Expand Down Expand Up @@ -151,21 +148,17 @@ func TestValidFraudProof(t *testing.T) {
}

func TestCannotRepairSquareWithBadRoots(t *testing.T) {
bufferSize := 64
corruptChunk := bytes.Repeat([]byte{66}, bufferSize)
corruptChunk := bytes.Repeat([]byte{66}, shareSize)
tests := []struct {
name string
// Size of each share, in bytes
shareSize int
codec Codec
name string
codec Codec
}{
{"leopard", bufferSize, NewLeoRSCodec()},
{"leopard", NewLeoRSCodec()},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
codec, shareSize := test.codec, test.shareSize
original := createTestEds(codec, shareSize)
original := createTestEds(test.codec, shareSize)

rowRoots, err := original.RowRoots()
require.NoError(t, err)
Expand All @@ -184,7 +177,6 @@ func TestCannotRepairSquareWithBadRoots(t *testing.T) {
}

func TestCorruptedEdsReturnsErrByzantineData(t *testing.T) {
shareSize := 64
corruptChunk := bytes.Repeat([]byte{66}, shareSize)

tests := []struct {
Expand Down Expand Up @@ -261,7 +253,6 @@ func TestCorruptedEdsReturnsErrByzantineData(t *testing.T) {
}

func BenchmarkRepair(b *testing.B) {
chunkSize := uint(256)
// For different ODS sizes
for originalDataWidth := 4; originalDataWidth <= 512; originalDataWidth *= 2 {
for codecName, codec := range codecs {
Expand All @@ -271,7 +262,7 @@ func BenchmarkRepair(b *testing.B) {
}

// Generate a new range original data square then extend it
square := genRandDS(originalDataWidth, int(chunkSize))
square := genRandDS(originalDataWidth, shareSize)
eds, err := ComputeExtendedDataSquare(square, codec, NewDefaultTree)
if err != nil {
b.Error(err)
Expand Down
18 changes: 16 additions & 2 deletions extendeddatasquare.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"

"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -44,7 +45,8 @@ func (eds *ExtendedDataSquare) UnmarshalJSON(b []byte) error {
return nil
}

// ComputeExtendedDataSquare computes the extended data square for some chunks of data.
// ComputeExtendedDataSquare computes the extended data square for some chunks
// of original data.
func ComputeExtendedDataSquare(
data [][]byte,
codec Codec,
Expand All @@ -55,6 +57,10 @@ func ComputeExtendedDataSquare(
}

chunkSize := getChunkSize(data)
err := codec.ValidateChunkSize(int(chunkSize))
if err != nil {
return nil, err
}
ds, err := newDataSquare(data, treeCreatorFn, uint(chunkSize))
if err != nil {
return nil, err
Expand All @@ -80,6 +86,10 @@ func ImportExtendedDataSquare(
}

chunkSize := getChunkSize(data)
err := codec.ValidateChunkSize(int(chunkSize))
if err != nil {
return nil, err
}
ds, err := newDataSquare(data, treeCreatorFn, uint(chunkSize))
if err != nil {
return nil, err
Expand All @@ -104,6 +114,10 @@ func NewExtendedDataSquare(codec Codec, treeCreatorFn TreeConstructorFn, edsWidt
if err != nil {
return nil, err
}
err = codec.ValidateChunkSize(int(chunkSize))
if err != nil {
return nil, err

Check warning on line 119 in extendeddatasquare.go

View check run for this annotation

Codecov / codecov/patch

extendeddatasquare.go#L119

Added line #L119 was not covered by tests
}

data := make([][]byte, edsWidth*edsWidth)
dataSquare, err := newDataSquare(data, treeCreatorFn, chunkSize)
Expand Down Expand Up @@ -310,7 +324,7 @@ func (eds *ExtendedDataSquare) Equals(other *ExtendedDataSquare) bool {
// extended data square.
func validateEdsWidth(edsWidth uint) error {
if edsWidth%2 != 0 {
return errors.New("square width must be even")
return fmt.Errorf("extended data square width %v must be even", edsWidth)
}

return nil
Expand Down
Loading

0 comments on commit f480fb4

Please sign in to comment.