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: edge case namespace merkle tree #277

Merged
merged 3 commits into from
Jan 23, 2024

Conversation

rach-id
Copy link
Member

@rach-id rach-id commented Jan 11, 2024

Overview

This fixes an edge case with the verifyInner for the NMT implementation. It is the fix for OS-CLST-ADV-01 from the Ottersec audit.

Special thanks for Ottersec on providing the test cases and the fix.

Checklist

  • New and updated code has appropriate documentation
  • New and updated code has new and/or updated testing
  • Required CI checks are passing
  • Visual proof for any user facing features like CLI or documentation updates
  • Linked issues closed with keywords

Summary by CodeRabbit

  • Refactor

    • Improved the calculation logic for nodes in the Merkle Tree structure.
  • Tests

    • Added new tests to ensure the integrity of leaf and internal nodes in the Merkle Tree.

@rach-id rach-id added bug Something isn't working audit labels Jan 11, 2024
@rach-id rach-id requested a review from evan-forbes January 11, 2024 13:27
@rach-id rach-id self-assigned this Jan 11, 2024
@rach-id rach-id requested a review from adlerjohn as a code owner January 11, 2024 13:27
Copy link

coderabbitai bot commented Jan 11, 2024

Walkthrough

The modifications involve a refinement in the logic for node calculation within the NamespaceMerkleTree library, adjusting the conditions in an if statement and a proof length check. Additionally, the test suite has expanded with new functions and an import statement in NamespaceMerkleTree.t.sol to enhance verification processes for both leaf and internal nodes of a merkle tree.

Changes

File Path Change Summary
src/lib/tree/namespace/NamespaceMerkleTree.sol Modified node calculation logic by changing the if statement and proof length condition.
src/lib/tree/namespace/test/NamespaceMerkleTree.t.sol Added multiple new test functions for verifying leaf and internal nodes in a merkle tree.

Poem

🐇 "In the forest of code, where the data trees grow,
🍃 A change in the logic, sets new leaves aglow.
🧪 Tests spring up fresh, with assertions so keen,
🎉 Hopping through branches, a coderabbit's dream!"

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 2060371 and ebffa5c.
Files selected for processing (2)
  • src/lib/tree/namespace/NamespaceMerkleTree.sol (2 hunks)
  • src/lib/tree/namespace/test/NamespaceMerkleTree.t.sol (4 hunks)
Additional comments: 17
src/lib/tree/namespace/NamespaceMerkleTree.sol (2)
  • 109-109: The change in the condition within the if statement from (1 << (height - heightOffset - 1)) to (1 << (height - 1)) is intended to correct the edge case identified by Ottersec. Ensure that this change does not introduce any off-by-one errors or other unintended consequences in the calculation of the subtree index.

  • 122-122: The adjustment of the proof's sideNodes length check from (proof.sideNodes.length <= height - 1) to (proof.sideNodes.length <= height - heightOffset - 1) is critical for the integrity of the proof verification. It is important to validate that this change correctly handles the edge case without affecting other valid proofs.

src/lib/tree/namespace/test/NamespaceMerkleTree.t.sol (15)
  • 256-285: The test function testVerifyLeafFiveOfEight is well-structured and follows the pattern of the other test functions. It's testing the verification of a specific leaf in a merkle tree with eight leaves. Ensure that the test cases cover all possible edge cases and that the test data used is representative of real-world scenarios to ensure thorough testing.

  • 287-318: The test function testVerifyLeafFourOfEight is similar to the previous one, testing another leaf's verification in the same merkle tree. It's important to check that the test data changes accordingly and that the assertions are correct for the specific case being tested.

  • 320-351: The test function testVerifyLeafThreeOfEight is consistent with the other test functions in structure and purpose. It's crucial that the test functions are not only correct but also meaningful. The test should fail if the expected behavior is not met, which is a critical aspect of test-driven development.

  • 353-382: The test function testVerifyLeafFiveOfSeven introduces a different number of leaves in the merkle tree, which is good for testing different scenarios. It's important to ensure that the test functions are independent and can be run in any order without affecting each other.

  • 384-408: The test function testVerifyLeafNineOfTen is testing a larger merkle tree with ten leaves. This is a good practice to test the scalability of the verification function. It's also important to ensure that the test functions are efficient and do not take an excessive amount of time to run, especially when dealing with larger data sets.

  • 410-444: The test function testVerifyLeafTwelveOfThirteen is testing an even larger merkle tree. It's important to note that as the number of leaves increases, the complexity of the merkle tree also increases. The tests should be designed to catch any potential issues that could arise from this increased complexity.

  • 446-470: The test function testVerifyLeafThirteenOfThirteen is testing the verification of the last leaf in a merkle tree with thirteen leaves. This is a boundary case and is important for ensuring the correctness of the merkle tree implementation at its limits.

  • 472-496: The test function testVerifyInternalNodeOneAndTwoOfFour is testing the verification of internal nodes, which is a critical part of ensuring the integrity of the merkle tree. It's important to ensure that the internal node verification is as robust as the leaf verification.

  • 498-519: The test function testVerifyInternalNodeOneAndTwoOfThree is another test for internal node verification, this time with a different number of leaves. This variation in test cases helps ensure that the verification function works correctly under different scenarios.

  • 520-520: The test function testVerifyInnerLeafIsRoot is testing a special case where the leaf itself is the root of the merkle tree. This is an edge case and it's good to see it being tested explicitly.

  • 520-520: The test function testVerifyInnerFalseForStartingHeightZero is testing the behavior of the verification function when the starting height is zero. This is an important negative test case to ensure that the function handles incorrect inputs gracefully.

  • 253-523: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [520-561]

The test function testVerifyInnerFalseForTooLargeKey is testing the behavior when the key is larger than the number of leaves. This is another negative test case that is important for ensuring the function's robustness against invalid inputs.

  • 558-564: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [520-581]

The test function testVerifyInnerFalseForIncorrectProofLength is testing the verification function's behavior when the proof length is incorrect. This is a critical test for ensuring that the function does not accept invalid proofs.

  • 520-520: The test function testVerifyInnerOneOfEight is testing the verification of an internal node in a merkle tree with eight leaves. This is a good test for ensuring that the verification function works correctly for internal nodes as well as leaves.

  • 520-520: The test function testVerifyInnerSevenOfEight is similar to the previous test but for a different internal node. It's important to test all internal nodes, especially in larger merkle trees, to ensure complete coverage.

src/lib/tree/namespace/test/NamespaceMerkleTree.t.sol Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ebffa5c and 43ea30c.
Files selected for processing (1)
  • src/lib/tree/namespace/test/NamespaceMerkleTree.t.sol (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/lib/tree/namespace/test/NamespaceMerkleTree.t.sol

@@ -252,6 +252,271 @@ contract NamespaceMerkleTreeTest is DSTest {
assertTrue(isValid);
}

function testVerifyLeafFiveOfEight() external {
Copy link
Member

Choose a reason for hiding this comment

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

as you said we should figure out these tests are needed and document why they are needed so when they fail in the future we can better fix things

so we should create an issue imo to do so later, as I'm not sure we want to block on it due to the time constraints

@rach-id rach-id merged commit 86cbb51 into celestiaorg:master Jan 23, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
audit bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants