pub struct MerkleTree { /* private fields */ }Expand description
A Merkle tree over field element leaves.
The tree is stored as a flat array where nodes[1] is the
root, nodes[2i] and nodes[2i+1] are children of nodes[i],
and leaves occupy nodes[n..2n] where n = 2^depth.
§Examples
use field_cat::F101;
use proof_cat::commit::merkle::MerkleTree;
let values = [F101::new(10), F101::new(20), F101::new(30), F101::new(40)];
let tree = MerkleTree::from_field_values(&values);
// Open leaf 2 and verify the opening.
let proof = tree.open(2)?;
assert!(MerkleTree::verify_opening(
&tree.root(), 2, &F101::new(30), &proof,
));
// A wrong value fails verification.
assert!(!MerkleTree::verify_opening(
&tree.root(), 2, &F101::new(99), &proof,
));Implementations§
Source§impl MerkleTree
impl MerkleTree
Sourcepub fn from_field_values<F: FieldBytes>(values: &[F]) -> Self
pub fn from_field_values<F: FieldBytes>(values: &[F]) -> Self
Build a Merkle tree from a slice of field elements.
Pads to the next power of two with distinct padding leaves.
Sourcepub fn root(&self) -> MerkleRoot
pub fn root(&self) -> MerkleRoot
The root commitment.
Sourcepub fn leaf_count(&self) -> usize
pub fn leaf_count(&self) -> usize
The number of actual (non-padding) leaves.
Sourcepub fn open(&self, index: usize) -> Result<MerkleProof, Error>
pub fn open(&self, index: usize) -> Result<MerkleProof, Error>
Generate an opening proof for the leaf at index.
§Errors
Returns Error::LeafIndexOutOfBounds if index >= leaf_count.
Sourcepub fn verify_opening<F: FieldBytes>(
root: &MerkleRoot,
index: usize,
value: &F,
proof: &MerkleProof,
) -> bool
pub fn verify_opening<F: FieldBytes>( root: &MerkleRoot, index: usize, value: &F, proof: &MerkleProof, ) -> bool
Verify an opening proof against a root and leaf value.
Recomputes the root from the leaf hash and sibling path, then checks it matches the expected root.
Trait Implementations§
Source§impl Clone for MerkleTree
impl Clone for MerkleTree
Source§fn clone(&self) -> MerkleTree
fn clone(&self) -> MerkleTree
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for MerkleTree
impl RefUnwindSafe for MerkleTree
impl Send for MerkleTree
impl Sync for MerkleTree
impl Unpin for MerkleTree
impl UnsafeUnpin for MerkleTree
impl UnwindSafe for MerkleTree
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more