Skip to main content
Version: v0.30.0

Merkle Trees


Returns the root of the tree from the provided leaf and its hash path, using a Pedersen hash.

fn compute_merkle_root(leaf : Field, index : Field, hash_path: [Field]) -> Field


// these values are for this example only
index = "0"
priv_key = "0x000000000000000000000000000000000000000000000000000000616c696365"
secret = "0x1929ea3ab8d9106a899386883d9428f8256cfedb3c4f6b66bf4aa4d28a79988f"
note_hash_path = [
fn main(index: Field, priv_key: Field, secret: Field, note_hash_path: [Field; 3]) {

let pubkey = std::scalar_mul::fixed_base_embedded_curve(priv_key);
let pubkey_x = pubkey[0];
let pubkey_y = pubkey[1];
let note_commitment = std::hash::pedersen(&[pubkey_x, pubkey_y, secret]);

let root = std::merkle::compute_merkle_root(note_commitment[0], index, note_hash_path.as_slice());

To check merkle tree membership:

  1. Include a merkle root as a program input.
  2. Compute the merkle root of a given leaf, index and hash path.
  3. Assert the merkle roots are equal.

For more info about merkle trees, see the Wikipedia page.