Skip to content
Akinbola0xKENZMAN
Work/Privacy & cryptography
AI & Data

Merkle tree over a prime field

A fixed-capacity Merkle tree with inclusion proofs, in Rust.

Role
Sole author
When
Feb 2026 – now
Team
Solo
Stack
Rust

Overview

Leaves are hashed into field elements and paired up level by level. A proof collects the sibling at every level and whether the node was a right child, which is the same shape the Noir circuits above verify.

The code

pub fn get_proof(&self, idx: usize) -> Result<MerkleProof, String> {
    if idx >= self.capacity() {
        return Err(format!("index {} out of range (capacity {})", idx, self.capacity()));
    }
    let mut siblings = Vec::new();
    let mut path_indices = Vec::new();
    let mut current_idx = idx;
    // Collect sibling at each level (all levels except the root level).
    for layer in 0..self.layers.len() - 1 {
        let is_right_child = current_idx % 2 == 1;
        let sibling_idx = if is_right_child { current_idx - 1 } else { current_idx + 1 };
        siblings.push(self.layers[layer][sibling_idx]);
        path_indices.push(is_right_child);
        current_idx /= 2;
    }
    Ok(MerkleProof { siblings, path_indices, leaf: self.layers[0][idx], leaf_index: idx, root: self.root() })
}

How it works

  1. Power-of-two capacity check
  2. Proofs carrying siblings, path bits, leaf, index and root
  3. Bounds-checked proof generation

Stack

Rust