Overview
NaryProof<N, MAX_DEPTH> is an N-ary Merkle inclusion proof that proves a specific leaf exists at a given index in a tree with a specific root hash.
Type Definition
Generic Parameters
The branching factor (arity) of the tree. Common values are 2 (binary), 3 (ternary), 4 (quaternary).
The maximum depth of the tree. Determines the maximum tree size that can be proven.
Fields
The Merkle root this proof verifies against. A
Hash is a 32-byte array ([u8; 32]).The leaf hash being proved.
The 0-based index of the leaf in the tree.
Number of leaves in the tree this proof was generated from.
Number of valid levels in the
levels array. Only levels[..level_count] are used during verification.Proof levels from leaf to root. Each level contains the sibling hashes needed to reconstruct the parent hash at that level.
Derive Traits
DebugClonePartialEqEqwincode::SchemaWrite(withwincodefeature)wincode::SchemaRead(withwincodefeature)serde::Serialize(withserdefeature)serde::Deserialize(withserdefeature)
Methods
verify
Ok(true) if the proof is valid.
Validation checks:
- Tree size must be non-zero
- Leaf index must be less than tree size
- Level count must match expected depth for the tree size
- Level count must not exceed
MAX_DEPTH - Each level’s position must match the expected position based on the leaf index
- Sibling counts must be valid
verify_against
Generating Proofs
Proofs are generated from aTreeSnapshot using the generate_proof method:
Serialization
With wincode feature
With serde feature
Verification Workflow
- Generate proof: Create a snapshot and call
generate_proof(leaf_index) - Transmit proof: Serialize and send the proof to a verifier
- Verify proof: Deserialize and call
verify()orverify_against() - Interpret result:
Ok(true)means the leaf exists at the claimed index with the claimed root
Error Cases
Verification returnsErr(TreeError) if:
tree_size == 0orleaf_index >= tree_size→TreeError::MathErrorlevel_countdoesn’t match expected depth →TreeError::InvalidProofDepthlevel_count > MAX_DEPTH→TreeError::InvalidProofDepth- Position or sibling count validation fails →
TreeError::MathError
Ok(false) if:
- The reconstructed root doesn’t match
self.root - The proof has been tampered with
See Also
- ProofLevel - Individual proof level structure
- ConsistencyProof - Proof that one tree is a prefix of another
- TreeSnapshot - Proof generation methods
