Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: verify encoding of shares in case when axis is fully reconstructed from orthogonal #313

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions extendeddatacrossword.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@

// 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)))
Copy link
Member Author

@walldiss walldiss Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated, just small simplification change to use builtin func instead of loop


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

if eds.verifyEncoding(col, r, rebuiltShares[c]) != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something worth consideration is the the precedence of these returned errors i.e., 1) root mismatch (due to the call to verifyAgainstColRoots) and 2) the encoding error (caused by verifyEncoding). I think if the encoding is already wrong, perhaps it does not matter whether the roots match or not, so returning the encoding verification error would be more accurate and informative. Depending on this decision, we may want to swap the order of the returned errors. This can be tackled in a separate issue though if further discussion is needed.

return false, false, &ErrByzantineData{Col, uint(c), col}

Check warning on line 179 in extendeddatacrossword.go

View check run for this annotation

Codecov / codecov/patch

extendeddatacrossword.go#L179

Added line #L179 was not covered by tests
}
}
}

Expand Down Expand Up @@ -211,10 +212,7 @@

// 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 @@
}
return false, false, err
}

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

Check warning on line 253 in extendeddatacrossword.go

View check run for this annotation

Codecov / codecov/patch

extendeddatacrossword.go#L252-L253

Added lines #L252 - L253 were not covered by tests
}
}
}

Expand Down Expand Up @@ -468,3 +470,23 @@
}
return tree.Root()
}

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

Check warning on line 481 in extendeddatacrossword.go

View check run for this annotation

Codecov / codecov/patch

extendeddatacrossword.go#L481

Added line #L481 was not covered by tests
}

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")

Check warning on line 487 in extendeddatacrossword.go

View check run for this annotation

Codecov / codecov/patch

extendeddatacrossword.go#L486-L487

Added lines #L486 - L487 were not covered by tests
}
}

return nil
}
Loading