Struct BalancedHashedMerkleTree

Source
pub struct BalancedHashedMerkleTree<'a, const NUM_LEAVES_LOG_2: u32>
where [(); { _ }]:,
{ /* private fields */ }
Expand description

Merkle Tree variant that has hash-sized leaves and is fully balanced according to configured generic parameter.

This Merkle Tree implementation is best suited for use cases when proofs for all (or most) of the elements need to be generated and the whole tree easily fits into memory. It can also be constructed and proofs can be generated efficiently without heap allocations.

With all parameters of the tree known statically, it results in the most efficient version of the code being generated for a given set of parameters.

NUM_LEAVES_LOG_2 is base-2 logarithm of the number of leaves in a tree.

Implementations§

Source§

impl<'a, const NUM_LEAVES_LOG_2: u32> BalancedHashedMerkleTree<'a, NUM_LEAVES_LOG_2>
where [(); { _ }]:,

Source

pub fn new(leaf_hashes: &'a [[u8; 32]; { _ }]) -> Self

Create a new tree from a fixed set of elements.

The data structure is statically allocated and might be too large to fit on the stack! If that is the case, use new_boxed() method.

Source

pub fn new_in<'b>( instance: &'b mut MaybeUninit<Self>, leaf_hashes: &'a [[u8; 32]; { _ }], ) -> &'b mut Self

Like Self::new(), but used pre-allocated memory for instantiation

Source

pub fn new_boxed(leaf_hashes: &'a [[u8; 32]; { _ }]) -> Box<Self>

Like Self::new(), but creates heap-allocated instance, avoiding excessive stack usage for large trees

Source

pub fn root(&self) -> [u8; 32]

Get the root of Merkle Tree.

In case a tree contains a single leaf hash, that leaf hash is returned.

Source

pub fn all_proofs( &self, ) -> impl ExactSizeIterator<Item = [u8; { _ }]> + TrustedLen
where [(); { _ }]:,

Iterator over proofs in the same order as provided leaf hashes

Source

pub fn verify( root: &[u8; 32], proof: &[u8; { _ }], leaf_index: usize, leaf_hash: [u8; 32], ) -> bool
where [(); { _ }]:,

Verify previously generated proof

Trait Implementations§

Source§

impl<'a, const NUM_LEAVES_LOG_2: u32> Debug for BalancedHashedMerkleTree<'a, NUM_LEAVES_LOG_2>
where [(); { _ }]:,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, const NUM_LEAVES_LOG_2: u32> Freeze for BalancedHashedMerkleTree<'a, NUM_LEAVES_LOG_2>

§

impl<'a, const NUM_LEAVES_LOG_2: u32> RefUnwindSafe for BalancedHashedMerkleTree<'a, NUM_LEAVES_LOG_2>

§

impl<'a, const NUM_LEAVES_LOG_2: u32> Send for BalancedHashedMerkleTree<'a, NUM_LEAVES_LOG_2>

§

impl<'a, const NUM_LEAVES_LOG_2: u32> Sync for BalancedHashedMerkleTree<'a, NUM_LEAVES_LOG_2>

§

impl<'a, const NUM_LEAVES_LOG_2: u32> Unpin for BalancedHashedMerkleTree<'a, NUM_LEAVES_LOG_2>

§

impl<'a, const NUM_LEAVES_LOG_2: u32> UnwindSafe for BalancedHashedMerkleTree<'a, NUM_LEAVES_LOG_2>

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, 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.