Struct SparseMerkleTree

Source
pub struct SparseMerkleTree<P> { /* private fields */ }
Expand description

Paddable sparse Merkle tree.

Implementations§

Source§

impl<P: Clone + Default + Mergeable + Paddable + ProofExtractable> SparseMerkleTree<P>

Source

pub fn new(height: usize) -> SparseMerkleTree<P>

The constructor.

Panics if the input height exceeds MAX_HEIGHT.

Source

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.

Source

pub fn get_height(&self) -> usize

Returns the height of the SMT.

Source

pub fn get_nodes_num(&self) -> usize

Returns the number of nodes in the SMT.

Source

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.

Source

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.

Source

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.

Source

pub fn get_root_ref(&self) -> usize

Returns the reference to the root ndoe.

Source

pub fn get_root_raw(&self) -> &P

Returns the raw data of the root.

Source

pub fn get_root(&self) -> <P as ProofExtractable>::ProofNode

Returns the data of the root that is visible in the Merkle proof.

Source

pub fn get_closest_ancestor_ref_index( &self, idx: &TreeIndex, ) -> (usize, TreeIndex)

Source

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.

Source

pub fn get_index_ref_pairs(&self) -> Vec<(TreeIndex, usize)>

Returns the index-reference pairs of all tree nodes in a BFS order.

Source

pub fn get_index_node_pairs(&self) -> Vec<(TreeIndex, &TreeNode<P>)>

Returns the index-node pairs of all tree nodes.

Source

pub fn get_leaves(&self) -> Vec<(TreeIndex, &TreeNode<P>)>

Returns the index-node pairs of all leaf nodes.

Source

pub fn get_paddings(&self) -> Vec<(TreeIndex, &TreeNode<P>)>

Returns the index-node pairs of all padding nodes.

Source

pub fn get_internals(&self) -> Vec<(TreeIndex, &TreeNode<P>)>

Returns the index-node pairs of all internal nodes.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<P: Default> Default for SparseMerkleTree<P>

Source§

fn default() -> SparseMerkleTree<P>

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V