pub fn process_proof(
    key: u64,
    merkle_leaf: b256,
    num_leaves: u64,
    proof: Vec<b256>,
) -> b256 
Expand description

This function will compute and return a Merkle root given a leaf and corresponding proof.

Arguments

  • ‘key’: [u64] - The key or index of the leaf to prove.
  • merkle_leaf: [b256] - The hash of a leaf on the Merkle Tree.
  • ‘num_leaves’: [u64] - The number of leaves in the Merkle Tree.
  • proof: [Vec] - The Merkle proof that will be used to traverse the Merkle Tree and compute a root.

Returns

  • [b256] - The calculated root.

Reverts

  • When an incorrect proof length is provided.
  • When there is one or no leaves and a proof is provided.
  • When the key is greater than or equal to the number of leaves.
  • When the computed height gets larger than the proof.

Examples

use sway_libs::merkle::binary_proof::process_proof;

fn foo() {
    let key = 0;
    let leaf = b256::zero();
    let num_leaves = 3;
    let mut proof = Vec::new();
    proof.push(0xb51fc5c7f5b6393a5b13bb6068de2247ac09df1d3b1bec17627502cb1d1a6ac6);
    let root = process_proof(key, leaf, num_leaves, proof);
    assert(root == 0xed84ee783dcb8999206160218e4fe8a1dc5ccb056e3b98f0a6fa633ca5896a47);
}