pub struct MerkleTree { /* private fields */ }Expand description
Represents a Merkle Tree, providing functionalities to build a binary tree from digests of the leaf nodes, get the root commitment, generate inclusion proofs, and verify them.
Implementations§
Source§impl MerkleTree
impl MerkleTree
Sourcepub fn new(leaf_nodes: Vec<Hash>) -> Result<Self, DecdsError>
pub fn new(leaf_nodes: Vec<Hash>) -> Result<Self, DecdsError>
Creates a new Merkle Tree from a vector of BLAKE3 hashes representing the leaf nodes.
§Arguments
leaf_nodes- AVec<blake3::Hash>where each hash is a leaf node of the tree.
§Returns
Result<Self, DecdsError>- Returns aMerkleTreeinstance if successful, or aDecdsError::NoLeafNodesToBuildMerkleTreeOnifleaf_nodesis empty.
Sourcepub fn get_root_commitment(&self) -> Hash
pub fn get_root_commitment(&self) -> Hash
Returns the root commitment (hash) of the Merkle Tree.
§Returns
blake3::Hash- The BLAKE3 hash of the Merkle root.
Sourcepub fn generate_proof(&self, leaf_index: usize) -> Result<Vec<Hash>, DecdsError>
pub fn generate_proof(&self, leaf_index: usize) -> Result<Vec<Hash>, DecdsError>
Generates a Merkle inclusion proof for a given leaf node at leaf_index.
This proof consists of the sibling hashes required to reconstruct the path from the leaf node to the Merkle root.
§Arguments
leaf_index- The index of the leaf node for which to generate the proof.
§Returns
Result<Vec<blake3::Hash>, DecdsError>- Returns aVec<blake3::Hash>representing the Merkle proof if successful. ReturnsDecdsError::InvalidLeafNodeIndexifleaf_indexis out of bounds.
Sourcepub fn verify_proof(
leaf_index: usize,
leaf_node: Hash,
proof: &[Hash],
root_hash: Hash,
) -> bool
pub fn verify_proof( leaf_index: usize, leaf_node: Hash, proof: &[Hash], root_hash: Hash, ) -> bool
Verifies a Merkle inclusion proof for a given leaf node against a provided Merkle root hash.
§Arguments
leaf_index- The index of the leaf node in the original set.leaf_node- The BLAKE3 hash of the leaf node to verify.proof- A slice ofblake3::Hashrepresenting the Merkle proof.root_hash- The expected root hash of the Merkle Tree.
§Returns
bool-trueif the proof is valid and the leaf node is included in the tree with the given root hash,falseotherwise.
Auto Trait Implementations§
impl Freeze for MerkleTree
impl RefUnwindSafe for MerkleTree
impl Send for MerkleTree
impl Sync for MerkleTree
impl Unpin 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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more