Skip to content

Commit

Permalink
fix: verify encoding of shares in case when axis is fully reconstruct…
Browse files Browse the repository at this point in the history
…ed from orthogonal (#313)

If axis is fully recomputed from orthogonal axises, we need to verify
correct encoding on top of verification of roots.
Test coverage should happen by
#312
  • Loading branch information
walldiss committed Apr 16, 2024
1 parent 015e3f2 commit d2da725
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions extendeddatacrossword.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ func (eds *ExtendedDataSquare) solveCrosswordRow(

// Prepare shares
shares := make([][]byte, eds.width)
vectorData := eds.row(uint(r))
for c := 0; c < int(eds.width); c++ {
shares[c] = vectorData[c]
}
copy(shares, eds.row(uint(r)))

// Attempt rebuild the row
rebuiltShares, isDecoded, err := eds.rebuildShares(shares)
Expand Down Expand Up @@ -177,6 +174,10 @@ func (eds *ExtendedDataSquare) solveCrosswordRow(
}
return false, false, err
}

if eds.verifyEncoding(col, r, rebuiltShares[c]) != nil {
return false, false, &ErrByzantineData{Col, uint(c), col}
}
}
}

Expand Down Expand Up @@ -211,10 +212,7 @@ func (eds *ExtendedDataSquare) solveCrosswordCol(

// Prepare shares
shares := make([][]byte, eds.width)
vectorData := eds.col(uint(c))
for r := 0; r < int(eds.width); r++ {
shares[r] = vectorData[r]
}
copy(shares, eds.col(uint(c)))

// Attempt rebuild
rebuiltShares, isDecoded, err := eds.rebuildShares(shares)
Expand Down Expand Up @@ -250,6 +248,10 @@ func (eds *ExtendedDataSquare) solveCrosswordCol(
}
return false, false, err
}

if eds.verifyEncoding(row, c, rebuiltShares[r]) != nil {
return false, false, &ErrByzantineData{Row, uint(r), row}
}
}
}

Expand Down Expand Up @@ -468,3 +470,23 @@ func (eds *ExtendedDataSquare) computeSharesRootWithRebuiltShare(shares [][]byte
}
return tree.Root()
}

// verifyEncoding checks the Reed-Solomon encoding of the provided data.
func (eds *ExtendedDataSquare) verifyEncoding(data [][]byte, rebuiltIndex int, rebuiltShare []byte) error {
data[rebuiltIndex] = rebuiltShare
half := len(data) / 2
original := data[:half]
parity, err := eds.codec.Encode(original)
if err != nil {
return err
}

for i := half; i < len(data); i++ {
if !bytes.Equal(data[i], parity[i-half]) {
data[rebuiltIndex] = nil
return errors.New("parity data does not match encoded data")
}
}

return nil
}

0 comments on commit d2da725

Please sign in to comment.