Skip to content

fix: off by one slice bug #254

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

Merged
merged 5 commits into from
Nov 8, 2023
Merged
Changes from 3 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
26 changes: 18 additions & 8 deletions src/lib/verifier/DAVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ library DAVerifier {
/// @notice The verifier data length isn't equal to the number of shares in the shares proofs.
UnequalDataLengthAndNumberOfSharesProofs,
/// @notice The number of leaves in the binary merkle proof is not divisible by 4.
InvalidNumberOfLeavesInProof
InvalidNumberOfLeavesInProof,
/// @notice The provided range is invalid.
InvalidRange
}

///////////////
Expand Down Expand Up @@ -107,12 +109,13 @@ library DAVerifier {
uint256 cursor = 0;
for (uint256 i = 0; i < _sharesProof.shareProofs.length; i++) {
uint256 sharesUsed = _sharesProof.shareProofs[i].endKey - _sharesProof.shareProofs[i].beginKey;
(bytes[] memory s, ErrorCodes err) = slice(_sharesProof.data, cursor, cursor + sharesUsed);
if (err != ErrorCodes.NoError) {
return (false, err);
}
if (
!NamespaceMerkleTree.verifyMulti(
_sharesProof.rowRoots[i],
_sharesProof.shareProofs[i],
_sharesProof.namespace,
slice(_sharesProof.data, cursor, cursor + sharesUsed)
_sharesProof.rowRoots[i], _sharesProof.shareProofs[i], _sharesProof.namespace, s
)
) {
return (false, ErrorCodes.InvalidSharesToRowsProof);
Expand Down Expand Up @@ -235,11 +238,18 @@ library DAVerifier {
/// @param _begin The beginning of the range (inclusive).
/// @param _end The ending of the range (exclusive).
/// @return _ the sliced data.
function slice(bytes[] memory _data, uint256 _begin, uint256 _end) internal pure returns (bytes[] memory) {
function slice(bytes[] memory _data, uint256 _begin, uint256 _end)
internal
pure
returns (bytes[] memory, ErrorCodes)
{
if (_begin > _end) {
return (_data, ErrorCodes.InvalidRange);
}
bytes[] memory out = new bytes[](_end-_begin);
for (uint256 i = _begin; i < _end; i++) {
out[i] = _data[i];
out[i - _begin] = _data[i];
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test that hits this codepath and would have caught the bug?

Copy link
Member Author

Choose a reason for hiding this comment

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

}
return out;
return (out, ErrorCodes.NoError);
}
}