Skip to content

feat: implement Merkle Tree proof verification (issue #332)#364

Open
spiffamani wants to merge 1 commit into
SoroLabs:mainfrom
spiffamani:feat/merkle-proof-verification
Open

feat: implement Merkle Tree proof verification (issue #332)#364
spiffamani wants to merge 1 commit into
SoroLabs:mainfrom
spiffamani:feat/merkle-proof-verification

Conversation

@spiffamani
Copy link
Copy Markdown

Closes #332

Summary

Implements Merkle Tree proof generation and verification for SoroScope state commitments. The existing merkle_tree.rs had broken code (orphaned blocks outside functions, duplicate function bodies, no proof logic) — this PR fixes the entire file and adds the requested verify_proof() function.


Changes

core/src/merkle_tree.rs

  • Fixed broken file — removed orphaned code blocks and duplicate function bodies
  • Added ProofNode struct — holds a sibling hash and which side the path node sits on at each level
  • Added MerkleProof struct — holds the leaf, the full proof path, and the root it was generated against
  • Rewrote build() — now stores all tree levels internally so proofs can be generated after building
  • Added generate_proof(leaf_index) — walks from the leaf up to the root collecting sibling hashes at each level to produce a complete inclusion proof
  • Added verify_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 returns true only if the computed root matches the supplied root
  • Hashing convention: pairs are sorted before combining (min || max), which is the OpenZeppelin/standard approach and makes proofs order-independent while preventing second-preimage attacks
  • Odd nodes at any level are promoted by hashing with themselves hash(x || x)

core/src/lib.rs

  • Exposed merkle_tree as a public module

Tests (20 total)

Build tests

  • test_build_single_leaf — root equals the hash of the single leaf
  • test_build_two_leaves — root is non-zero
  • test_build_even_leaves — 4 leaves builds correctly
  • test_build_odd_leaves — 3 leaves builds correctly (odd promotion)
  • test_build_empty_returns_error — empty input returns error
  • test_same_leaves_same_root — deterministic root
  • test_different_leaves_different_root — different data produces different root
  • test_get_root_hex_length — hex string is 64 characters

generate_proof tests

  • test_generate_proof_before_build_returns_error
  • test_generate_proof_out_of_range_returns_error
  • test_generate_proof_single_leaf_has_no_nodes
  • test_generate_proof_two_leaves_has_one_node

verify_proof tests

  • test_verify_proof_valid_two_leaves — valid proof verifies correctly
  • test_verify_proof_valid_right_leaf — right-side leaf verifies correctly
  • test_verify_proof_valid_four_leaves_all_indices — all 4 indices valid
  • test_verify_proof_valid_odd_leaf_count — all 3 indices valid with odd tree
  • test_verify_proof_valid_single_leaf — single leaf proof verifies
  • test_verify_proof_valid_large_tree — all 8 indices valid in an 8-leaf tree
  • test_verify_proof_tampered_leaf_fails — swapped leaf returns false
  • test_verify_proof_tampered_sibling_fails — corrupted sibling returns false
  • test_verify_proof_wrong_root_fails — wrong root returns false
  • test_verify_proof_cross_tree_fails — proof from tree A does not verify against tree B root

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 30, 2026

@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! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Core: Implement Merkle Tree proof verification

1 participant