Struct IndexedMerkleTree

Source
pub struct IndexedMerkleTree {
    pub nodes: Vec<Node>,
}
Expand description

Represents an indexed merkle tree.

This structure encapsulates a merkle tree where Nodes are indexed, facilitating efficient updates and proofs, especially non-membership proofs.

Fields:

  • nodes: A vector of Node elements, representing the nodes of the indexed merkle tree.

Fields§

§nodes: Vec<Node>

Implementations§

Source§

impl IndexedMerkleTree

Source

pub fn new(nodes: Vec<Node>) -> MerkleTreeResult<Self>

Creates a new IndexedMerkleTree from a given nodes vector.

§Arguments
  • nodes - A vector of Node elements from which the Merkle tree will be built.
§Returns

A MerkleTreeResult<Self> representing the initialized tree or an error.

Source

pub fn new_with_size(size: usize) -> MerkleTreeResult<Self>

Creates a new IndexedMerkleTree of a given size

§Arguments
  • size - The number of nodes in the tree.
§Returns

A MerkleTreeResult<Self> representing the initialized tree or an error.

Source

pub fn get_root(&self) -> MerkleTreeResult<&Node>

§Returns

The current root node of the Indexed Merkle tree.

Source

pub fn get_commitment(&self) -> MerkleTreeResult<Hash>

§Returns

The current commitment (hash of the root node) of the Indexed Merkle tree.

Source

pub fn find_node_index(&self, node: &Node) -> Option<usize>

Finds the index of a specific node in the indexed Merkle Tree.

This function searches through the tree’s nodes to find the index of a given node. It compares leaf nodes by label and inner nodes by hash. The function returns the node’s index if found, or None if the node is not present in the tree.

§Arguments
  • node - A reference to the Node whose index is being searched for.
§Returns

An Option<usize> indicating the index of the node if found.

Source

pub fn find_leaf_by_label(&self, label: &Hash) -> Option<Node>

Searches for a leaf node by its label in the indexed Merkle Tree.

This function iterates through the tree’s nodes to find a leaf node that matches the given label. If a matching leaf is found, it is returned, otherwise the function returns None.

§Arguments
  • label - A reference to the string label of the leaf node to find.
§Returns

An Option<Node> representing the found leaf node, if any.

Source

pub fn double_tree_size(&mut self)

Doubles the size of the Merkle Tree.

This function adds as many new inactive nodes to the tree as it currently contains, effectively doubling its size. This is necessary when no inactive node is available for the insertion of a new node. Each new node is marked inactive and initialized with default values.

Source

pub fn generate_membership_proof( &self, index: usize, ) -> MerkleTreeResult<MerkleProof>

Generates a membership proof for a node at a given index in the indexed merkle tree.

This function constructs a path of hashes leading from a specific node to the root of the tree, serving as a merkle proof of the node’s membership in the tree. If the index is invalid, it returns an error, because the node doesnt exist and cant be proved as a member.

§Arguments
  • index - The index of the node in the tree for which the proof is to be generated.
§Returns

A Result<MerkleProof, MerkleTreeError> containing the membership proof or an error.

Source

pub fn generate_non_membership_proof( &self, node: &Node, ) -> MerkleTreeResult<NonMembershipProof>

Generates a non-membership proof for a given node in the indexed merkle tree.

This function verifies that a node is not part of the tree. It does so by finding a place in the tree where the given node should exist based on its label and proving it is not there. Suitable for scenarios where proving the absence of data is required, e.g. important for guaranteeing uniqueness.

§Arguments
  • node - A reference to the Node for which the non-membership proof is required.
§Returns

A MerkleTreeResult<NonMembershipProof> containing the non-membership proof and the index of the “closest” valid node, or an error.

Source

pub fn update_node( &mut self, index: usize, new_node: Node, ) -> MerkleTreeResult<UpdateProof>

Updates the node with the given index in the merkle tree, returning the update proof.

This function first generates a proof of membership for the old node state, updates the node, recalculates the root, and then generates a new proof of membership for the updated node. It returns both the old and new proofs along with the updated tree.

§Arguments
  • index - The index of the node to be updated.
  • new_node - The new state of the node.
§Returns

A MerkleTreeResult<UpdateProof, MerkleTreeError> containing the the old root, the old proof, the new root and the new proof.

Source

pub fn insert_node( &mut self, new_node: &mut Node, ) -> MerkleTreeResult<InsertProof>

Inserts a node into the merkle tree, returning the insertion proof.

This function starts with a non-membership check to ensure that the index (i.e. the label) does not yet exist in the tree and thus to determine the index of the node to be changed. It then generates two update proofs: one for updating the next pointer of the existing node, and another for the actual insertion of the new node, i.e. updating an inactive and therefore empty leaf node. If there are no more empty leaf nodes, the capacity in the tree is doubled.

§Arguments
  • new_node - The new node to be inserted.
§Returns

A MerkleTreeResult<InsertProof> containing the non-membership proof and two update proofs.

Trait Implementations§

Source§

impl Clone for IndexedMerkleTree

Source§

fn clone(&self) -> IndexedMerkleTree

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'de> Deserialize<'de> for IndexedMerkleTree

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for IndexedMerkleTree

Source§

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

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

impl Serialize for IndexedMerkleTree

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,