feat: implement Merkle Tree proof verification (issue #332)#364
Open
spiffamani wants to merge 1 commit into
Open
feat: implement Merkle Tree proof verification (issue #332)#364spiffamani wants to merge 1 commit into
spiffamani wants to merge 1 commit into
Conversation
|
@spiffamani Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #332
Summary
Implements Merkle Tree proof generation and verification for SoroScope state commitments. The existing
merkle_tree.rshad broken code (orphaned blocks outside functions, duplicate function bodies, no proof logic) — this PR fixes the entire file and adds the requestedverify_proof()function.Changes
core/src/merkle_tree.rsProofNodestruct — holds a sibling hash and which side the path node sits on at each levelMerkleProofstruct — holds the leaf, the full proof path, and the root it was generated againstbuild()— now stores all tree levels internally so proofs can be generated after buildinggenerate_proof(leaf_index)— walks from the leaf up to the root collecting sibling hashes at each level to produce a complete inclusion proofverify_proof(proof, root)— the core requirement from issue Core: Implement Merkle Tree proof verification #332; recomputes the root by combining hashes level by level using the proof nodes and returnstrueonly if the computed root matches the supplied rootmin || max), which is the OpenZeppelin/standard approach and makes proofs order-independent while preventing second-preimage attackshash(x || x)core/src/lib.rsmerkle_treeas a public moduleTests (20 total)
Build tests
test_build_single_leaf— root equals the hash of the single leaftest_build_two_leaves— root is non-zerotest_build_even_leaves— 4 leaves builds correctlytest_build_odd_leaves— 3 leaves builds correctly (odd promotion)test_build_empty_returns_error— empty input returns errortest_same_leaves_same_root— deterministic roottest_different_leaves_different_root— different data produces different roottest_get_root_hex_length— hex string is 64 charactersgenerate_proof tests
test_generate_proof_before_build_returns_errortest_generate_proof_out_of_range_returns_errortest_generate_proof_single_leaf_has_no_nodestest_generate_proof_two_leaves_has_one_nodeverify_proof tests
test_verify_proof_valid_two_leaves— valid proof verifies correctlytest_verify_proof_valid_right_leaf— right-side leaf verifies correctlytest_verify_proof_valid_four_leaves_all_indices— all 4 indices validtest_verify_proof_valid_odd_leaf_count— all 3 indices valid with odd treetest_verify_proof_valid_single_leaf— single leaf proof verifiestest_verify_proof_valid_large_tree— all 8 indices valid in an 8-leaf treetest_verify_proof_tampered_leaf_fails— swapped leaf returns falsetest_verify_proof_tampered_sibling_fails— corrupted sibling returns falsetest_verify_proof_wrong_root_fails— wrong root returns falsetest_verify_proof_cross_tree_fails— proof from tree A does not verify against tree B root