Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Mar 7, 2025
1 parent 77e99ce commit 4093946
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
6 changes: 6 additions & 0 deletions nodebuilder/tests/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ func TestShareModule(t *testing.T) {
parsedBlob, err := blob.ToNodeBlobs(blbs...)
require.NoError(t, err)
require.Equal(t, nodeBlob[0].Commitment, parsedBlob[0].Commitment)

rngProofsOnly, err := client.Share.GetRange(ctx, nodeBlob[0].Namespace(), height, from, to, true)
require.NoError(t, err)
assert.Empty(t, rngProofsOnly.Shares)
err = rngProofsOnly.VerifyShares(rng.Shares, nodeBlob[0].Namespace(), from, to, dah.Hash())
require.NoError(t, err)
}
},
},
Expand Down
34 changes: 27 additions & 7 deletions share/shwap/range_namespace_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type RangeNamespaceData struct {
// namespace is the target namespace for the built range;
// from is the coordinates of the first share of the range within the EDS.
// to is the coordinates of the last inclusive share of the range within the EDS.
// TODO(@vgonkivs): proof collection can be simplified to store only
// incomplete rows(the first and the last), since we can recompute proofs
// for all complete rows
func RangedNamespaceDataFromShares(
shares [][]libshare.Share,
namespace libshare.Namespace,
Expand Down Expand Up @@ -95,18 +98,30 @@ func RangedNamespaceDataFromShares(
return rngData, nil
}

// Verify performs a basic validation of the incoming data. It ensures that the response data
// correspond to the request.
// Verify verifies the underlying shares are included in the data root.
func (rngdata *RangeNamespaceData) Verify(
namespace libshare.Namespace,
from SampleCoords,
to SampleCoords,
dataHash []byte,
) error {
if len(rngdata.Shares) != len(rngdata.Proof) {
return rngdata.VerifyShares(rngdata.Shares, namespace, from, to, dataHash)
}

// VerifyShares verifies the passed shares are included in the data root.
// `[][]libshare.Share` is a collection of the row shares from the range
// NOTE: the underlying shares will be ignored even if they are not empty.
func (rngdata *RangeNamespaceData) VerifyShares(
shares [][]libshare.Share,
namespace libshare.Namespace,
from SampleCoords,
to SampleCoords,
dataHash []byte,
) error {
if len(shares) != len(rngdata.Proof) {
return fmt.Errorf(
"mismatch amount of row shares and proofs, %d:%d",
len(rngdata.Shares), len(rngdata.Proof),
len(shares), len(rngdata.Proof),
)
}
if rngdata.IsEmpty() {
Expand All @@ -126,15 +141,15 @@ func (rngdata *RangeNamespaceData) Verify(
)
}

for i, nsData := range rngdata.Shares {
for i, rowShares := range shares {
if rngdata.Proof[i].IsEmptyProof() {
return fmt.Errorf("nil proof for row: %d", rngdata.Start+i)
}
if rngdata.Proof[i].shareProof.IsOfAbsence() {
return fmt.Errorf("absence proof for row: %d", rngdata.Start+i)
}

err := rngdata.Proof[i].VerifyInclusion(nsData, namespace, dataHash)
err := rngdata.Proof[i].VerifyInclusion(rowShares, namespace, dataHash)
if err != nil {
return fmt.Errorf("%w for row: %d, %w", ErrFailedVerification, rngdata.Start+i, err)
}
Expand All @@ -159,10 +174,15 @@ func (rngdata *RangeNamespaceData) ToProto() *pb.RangeNamespaceData {
pbShares := make([]*pb.RowShares, len(rngdata.Shares))
pbProofs := make([]*pb.Proof, len(rngdata.Proof))
for i, shr := range rngdata.Shares {
pbShares[i] = &pb.RowShares{}
rowShares := SharesToProto(shr)
pbShares[i].Shares = rowShares
pbProofs[i] = rngdata.Proof[i].ToProto()
}

for i, proof := range rngdata.Proof {
pbProofs[i] = proof.ToProto()
}

return &pb.RangeNamespaceData{
Start: int32(rngdata.Start),
Shares: pbShares,
Expand Down
40 changes: 40 additions & 0 deletions share/shwap/range_namespace_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package shwap_test

import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -71,6 +72,9 @@ func TestRangeNamespaceData(t *testing.T) {
require.NoError(t, err)
err = rngdata.Verify(ns, dataID.From, dataID.To, root.Hash())
require.NoError(t, err)

err = rngdata.VerifyShares(rngdata.Shares, ns, dataID.From, dataID.To, root.Hash())
require.NoError(t, err)
})
}
}
Expand All @@ -96,6 +100,42 @@ func TestRangeCoordsFromIdx(t *testing.T) {
}
}

func TestRangeNamespaceDataMarshalUnmarshal(t *testing.T) {
const (
odsSize = 4
sharesAmount = odsSize * odsSize
)

ns := libshare.RandomNamespace()
square, root := edstest.RandEDSWithNamespace(t, ns, sharesAmount, odsSize)
eds := eds.Rsmt2D{ExtendedDataSquare: square}

from := shwap.SampleCoords{Row: 0, Col: 0}
to := shwap.SampleCoords{Row: odsSize - 1, Col: odsSize - 1}
rngdata, err := eds.RangeNamespaceData(
context.Background(),
ns,
from,
to,
)
require.NoError(t, err)
err = rngdata.Verify(ns, from, to, root.Hash())
require.NoError(t, err)

data, err := json.Marshal(rngdata)
require.NoError(t, err)

newData := &shwap.RangeNamespaceData{}
err = json.Unmarshal(data, newData)
require.NoError(t, err)
assert.Equal(t, rngdata, *newData)

pbData := newData.ToProto()
rangeNsData, err := shwap.RangeNamespaceDataFromProto(pbData)
require.NoError(t, err)
assert.Equal(t, rngdata, *rangeNsData)
}

func FuzzRangeCoordsFromIdx(f *testing.F) {
if testing.Short() {
f.Skip()
Expand Down

0 comments on commit 4093946

Please sign in to comment.