pub struct SparseMerkleTree<P> { /* private fields */ }
Expand description
Paddable sparse Merkle tree.
Implementations§
Source§impl<P: Clone + Default + Mergeable + Paddable + ProofExtractable> SparseMerkleTree<P>
impl<P: Clone + Default + Mergeable + Paddable + ProofExtractable> SparseMerkleTree<P>
Sourcepub fn new(height: usize) -> SparseMerkleTree<P>
pub fn new(height: usize) -> SparseMerkleTree<P>
The constructor.
Panics if the input height exceeds MAX_HEIGHT.
Sourcepub fn new_merkle_tree(list: &[P]) -> SparseMerkleTree<P>
pub fn new_merkle_tree(list: &[P]) -> SparseMerkleTree<P>
A simple Merkle tree constructor, where all items are added next to each other from left to right. Note that zero padding secret is used and the height depends on the input list size. Use this helper constructor only when simulating a plain Merkle tree.
Sourcepub fn get_height(&self) -> usize
pub fn get_height(&self) -> usize
Returns the height of the SMT.
Sourcepub fn get_nodes_num(&self) -> usize
pub fn get_nodes_num(&self) -> usize
Returns the number of nodes in the SMT.
Sourcepub fn get_node_by_ref(&self, link: usize) -> &TreeNode<P>
pub fn get_node_by_ref(&self, link: usize) -> &TreeNode<P>
Returns the tree node by reference.
Panics if the reference is out of range.
Sourcepub fn get_node_raw_by_refs(&self, list: &[usize]) -> Vec<&P>
pub fn get_node_raw_by_refs(&self, list: &[usize]) -> Vec<&P>
Returns the tree node by references.
Panics if the reference is out of range.
Sourcepub fn get_node_proof_by_refs(&self, list: &[usize]) -> Vec<P::ProofNode>
pub fn get_node_proof_by_refs(&self, list: &[usize]) -> Vec<P::ProofNode>
Returns the tree node by references.
Panics if the reference is out of range.
Sourcepub fn get_root_ref(&self) -> usize
pub fn get_root_ref(&self) -> usize
Returns the reference to the root ndoe.
Sourcepub fn get_root_raw(&self) -> &P
pub fn get_root_raw(&self) -> &P
Returns the raw data of the root.
Sourcepub fn get_root(&self) -> <P as ProofExtractable>::ProofNode
pub fn get_root(&self) -> <P as ProofExtractable>::ProofNode
Returns the data of the root that is visible in the Merkle proof.
pub fn get_closest_ancestor_ref_index( &self, idx: &TreeIndex, ) -> (usize, TreeIndex)
Sourcepub fn get_leaf_by_index(&self, idx: &TreeIndex) -> Option<&TreeNode<P>>
pub fn get_leaf_by_index(&self, idx: &TreeIndex) -> Option<&TreeNode<P>>
Returns the tree node of a queried tree index.
Panics if the the height of the input index doesn’t match with the tree height.
If the node doesn’t exist, return None
.
Sourcepub fn get_index_ref_pairs(&self) -> Vec<(TreeIndex, usize)>
pub fn get_index_ref_pairs(&self) -> Vec<(TreeIndex, usize)>
Returns the index-reference pairs of all tree nodes in a BFS order.
Sourcepub fn get_index_node_pairs(&self) -> Vec<(TreeIndex, &TreeNode<P>)>
pub fn get_index_node_pairs(&self) -> Vec<(TreeIndex, &TreeNode<P>)>
Returns the index-node pairs of all tree nodes.
Sourcepub fn get_leaves(&self) -> Vec<(TreeIndex, &TreeNode<P>)>
pub fn get_leaves(&self) -> Vec<(TreeIndex, &TreeNode<P>)>
Returns the index-node pairs of all leaf nodes.
Sourcepub fn get_paddings(&self) -> Vec<(TreeIndex, &TreeNode<P>)>
pub fn get_paddings(&self) -> Vec<(TreeIndex, &TreeNode<P>)>
Returns the index-node pairs of all padding nodes.
Sourcepub fn get_internals(&self) -> Vec<(TreeIndex, &TreeNode<P>)>
pub fn get_internals(&self) -> Vec<(TreeIndex, &TreeNode<P>)>
Returns the index-node pairs of all internal nodes.
Sourcepub fn check_index_list_validity(
&self,
list: &[(TreeIndex, P)],
) -> Option<TreeError>
pub fn check_index_list_validity( &self, list: &[(TreeIndex, P)], ) -> Option<TreeError>
Check if the tree indexes in the list are all valid and sorted.
If the height of some index doesn’t match with the height of the tree, return TreeError::HeightNotMatch.
If the indexes are not in order, return TreeError::IndexNotSorted.
If there are duplicated indexes in the list, return TreeError::IndexDuplicated.
Sourcepub fn construct_smt_nodes(
&mut self,
list: &[(TreeIndex, P)],
secret: &Secret,
) -> Option<TreeError>
pub fn construct_smt_nodes( &mut self, list: &[(TreeIndex, P)], secret: &Secret, ) -> Option<TreeError>
Construct SMT from the input list of sorted index-value pairs, index being the sorting key.
If the height of some index in the input list doesn’t match with the height of the tree, return TreeError::HeightNotMatch.
If the indexes in the input list are not in order, return TreeError::IndexNotSorted.
If there are duplicated indexes in the list, return TreeError::IndexDuplicated.
Sourcepub fn build(&mut self, list: &[(TreeIndex, P)], secret: &Secret)
pub fn build(&mut self, list: &[(TreeIndex, P)], secret: &Secret)
Build SMT from the input list of sorted index-value pairs, index being the sorting key.
Panics if the input list is not valid.
Sourcepub fn update(&mut self, key: &TreeIndex, value: P, secret: &Secret)
pub fn update(&mut self, key: &TreeIndex, value: P, secret: &Secret)
Update the tree by modifying the leaf node of a certain tree index.
Panics if the height of the input index doesn’t match with that of the tree.
Sourcepub fn get_merkle_path_ref(&self, idx: &TreeIndex) -> Option<Vec<usize>>
pub fn get_merkle_path_ref(&self, idx: &TreeIndex) -> Option<Vec<usize>>
Returns the references to the input leaf node and siblings of nodes long the Merkle path from the root to the leaf.
The result is a list of references [leaf, sibling, ..., sibling]
.
If the input leaf node doesn’t exist, return None
.
Panics if the height of the input index is different from the height of the tree.
Sourcepub fn get_merkle_path_ref_batch(
&self,
list: &[TreeIndex],
) -> Option<Vec<usize>>
pub fn get_merkle_path_ref_batch( &self, list: &[TreeIndex], ) -> Option<Vec<usize>>
Returns the references to the input leaves and siblings of nodes long the batched Merkle paths from the root to the leaves.
The result is a list of references [leaf, ..., leaf, sibling, ..., sibling]
.
If the root or some input leaf node doesn’t exist, return None
.
If the input list is empty, return an empty vector.
Panics if the input list is not valid.
Sourcepub fn get_closest_index_by_dir(
&self,
ancestor_ref: usize,
ancestor_idx: TreeIndex,
dir: ChildDir,
) -> Option<TreeIndex>
pub fn get_closest_index_by_dir( &self, ancestor_ref: usize, ancestor_idx: TreeIndex, dir: ChildDir, ) -> Option<TreeIndex>
Returns the tree index of closest left/right (depending on input direction) node in the tree.
Sourcepub fn get_padding_proof_by_dir_index_ref_pairs(
idx: &TreeIndex,
dir: ChildDir,
) -> Vec<(TreeIndex, usize)>
pub fn get_padding_proof_by_dir_index_ref_pairs( idx: &TreeIndex, dir: ChildDir, ) -> Vec<(TreeIndex, usize)>
Returns the index-reference pairs to necessary padding nodes to prove that the input index is the left/right (depending on the input direction) most real leaf in the tree. Note that the reference is the offset from the end of the sibling list.
Sourcepub fn get_padding_proof_batch_index_ref_pairs(
left_idx: &TreeIndex,
right_idx: &TreeIndex,
) -> Vec<(TreeIndex, usize)>
pub fn get_padding_proof_batch_index_ref_pairs( left_idx: &TreeIndex, right_idx: &TreeIndex, ) -> Vec<(TreeIndex, usize)>
Returns the index-reference pairs to necessary padding nodes to prove that there are no other real leaf nodes between the input indexes in the tree. Note that the reference is the offset from the end of the sibling list.
Panics if the input indexes don’t have the same height or not in the right order.
Trait Implementations§
Source§impl<P: Debug> Debug for SparseMerkleTree<P>
impl<P: Debug> Debug for SparseMerkleTree<P>
Source§impl<P: Default> Default for SparseMerkleTree<P>
impl<P: Default> Default for SparseMerkleTree<P>
Source§fn default() -> SparseMerkleTree<P>
fn default() -> SparseMerkleTree<P>
Auto Trait Implementations§
impl<P> Freeze for SparseMerkleTree<P>
impl<P> RefUnwindSafe for SparseMerkleTree<P>where
P: RefUnwindSafe,
impl<P> Send for SparseMerkleTree<P>where
P: Send,
impl<P> Sync for SparseMerkleTree<P>where
P: Sync,
impl<P> Unpin for SparseMerkleTree<P>where
P: Unpin,
impl<P> UnwindSafe for SparseMerkleTree<P>where
P: UnwindSafe,
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
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>
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>
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