Struct BalancedHashedMerkleTree

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

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

This can be considered a general case of UnbalancedHashedMerkleTree. The root and proofs are identical for both in case the number of leaves is a power of two. For the number of leaves that is a power of two UnbalancedHashedMerkleTree is useful when a single proof needs to be generated and the number of leaves is very large (it can generate proofs with very little RAM usage compared to this version).

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.

Implementations§

Source§

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

Source

pub fn new(leaves: &'a [[u8; 32]; N]) -> 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>, leaves: &'a [[u8; 32]; N], ) -> &'b mut Self

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

Source

pub fn new_boxed(leaves: &'a [[u8; 32]; N]) -> Box<Self>

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

Source

pub fn compute_root_only(leaves: &[[u8; 32]; N]) -> [u8; 32]
where [(); { _ }]:,

Compute Merkle Tree Root.

This is functionally equivalent to creating an instance first and calling Self::root() method, but is faster and avoids heap allocation when root is the only thing that is needed.

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; 32]; { _ }]> + TrustedLen
where [(); { _ }]:,

Iterator over proofs in the same order as provided leaf hashes

Source

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

Verify previously generated proof

Trait Implementations§

Source§

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

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, const N: usize> Freeze for BalancedHashedMerkleTree<'a, N>

§

impl<'a, const N: usize> RefUnwindSafe for BalancedHashedMerkleTree<'a, N>

§

impl<'a, const N: usize> Send for BalancedHashedMerkleTree<'a, N>

§

impl<'a, const N: usize> Sync for BalancedHashedMerkleTree<'a, N>

§

impl<'a, const N: usize> Unpin for BalancedHashedMerkleTree<'a, N>

§

impl<'a, const N: usize> UnwindSafe for BalancedHashedMerkleTree<'a, N>

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.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.