Struct MerkleMountainRange

Source
#[repr(C)]
pub struct MerkleMountainRange<const MAX_N: u64>
where [(); { _ }]:,
{ /* private fields */ }
Expand description

Merkle Mountain Range variant that has pre-hashed leaves with arbitrary number of elements.

This can be considered a general case of UnbalancedMerkleTree. The root and proofs are identical for both. UnbalancedMerkleTree is more efficient and should be preferred when possible, while this data structure is designed for aggregating data incrementally over long periods of time.

MAX_N generic constant defines the maximum number of elements supported and controls stack usage.

Implementations§

Source§

impl<const MAX_N: u64> MerkleMountainRange<MAX_N>
where [(); { _ }]:,

Source

pub fn new() -> Self

Create an empty instance

Source

pub fn from_peaks(peaks: &MmrPeaks<MAX_N>) -> Option<Self>

Create a new instance from previously collected peaks.

Returns None if input is invalid.

Source

pub fn as_bytes(&self) -> &MerkleMountainRangeBytes<MAX_N>
where [(); { _ }]:,

Get byte representation of Merkle Mountain Range

Source

pub unsafe fn from_bytes(bytes: &MerkleMountainRangeBytes<MAX_N>) -> &Self
where [(); { _ }]:,

Create an instance from byte representation.

§Safety

Bytes must be previously created by Self::as_bytes().

Source

pub fn num_leaves(&self) -> u64

Get number of leaves aggregated in Merkle Mountain Range so far

Source

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

Calculate the root of Merkle Mountain Range.

In case MMR contains a single leaf hash, that leaf hash is returned, None is returned if there were no leafs added yet.

Source

pub fn peaks(&self) -> MmrPeaks<MAX_N>

Get peaks of Merkle Mountain Range

Source

pub fn add_leaf(&mut self, leaf: &[u8; 32]) -> bool

Add leaf to Merkle Mountain Range.

There is a more efficient version Self::add_leaves() in case multiple leaves are available.

Returns true on success, false if too many leafs were added.

Source

pub fn add_leaves<'a, Item, Iter>(&mut self, leaves: Iter) -> bool
where Item: Into<[u8; 32]>, Iter: IntoIterator<Item = Item> + 'a,

Add many leaves to Merkle Mountain Range.

This is a more efficient version of Self::add_leaf() in case multiple leaves are available.

Returns true on success, false if too many leaves were added.

Source

pub fn add_leaf_and_compute_proof( &mut self, leaf: &[u8; 32], ) -> Option<([u8; 32], Vec<[u8; 32]>)>

Add leaf to Merkle Mountain Range and generate inclusion proof.

Returns Some((root, proof)) on success, None if too many leafs were added.

Source

pub fn add_leaf_and_compute_proof_in<'proof>( &mut self, leaf: &[u8; 32], proof: &'proof mut [MaybeUninit<[u8; 32]>; { _ }], ) -> Option<([u8; 32], &'proof mut [[u8; 32]])>

Add leaf to Merkle Mountain Range and generate inclusion proof.

Returns Some((root, proof)) on success, None if too many leafs were added.

Source

pub fn add_leaf_and_compute_proof_inner( &mut self, leaf: &[u8; 32], proof: &mut [MaybeUninit<[u8; 32]>; { _ }], ) -> Option<([u8; 32], usize)>

Source

pub fn verify( root: &[u8; 32], proof: &[[u8; 32]], leaf_index: u64, leaf: [u8; 32], num_leaves: u64, ) -> bool

Verify a Merkle proof for a leaf at the given index.

NOTE: MAX_N constant doesn’t matter here and can be anything that is >= 1.

Trait Implementations§

Source§

impl<const MAX_N: u64> Clone for MerkleMountainRange<MAX_N>
where [(); { _ }]:,

Source§

fn clone(&self) -> MerkleMountainRange<MAX_N>

Returns a duplicate 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<const MAX_N: u64> Debug for MerkleMountainRange<MAX_N>
where [(); { _ }]:,

Source§

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

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

impl<const MAX_N: u64> Default for MerkleMountainRange<MAX_N>
where [(); { _ }]:,

Source§

fn default() -> Self

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

impl<const MAX_N: u64> Copy for MerkleMountainRange<MAX_N>
where [(); { _ }]:,

Auto Trait Implementations§

§

impl<const MAX_N: u64> Freeze for MerkleMountainRange<MAX_N>

§

impl<const MAX_N: u64> RefUnwindSafe for MerkleMountainRange<MAX_N>

§

impl<const MAX_N: u64> Send for MerkleMountainRange<MAX_N>

§

impl<const MAX_N: u64> Sync for MerkleMountainRange<MAX_N>

§

impl<const MAX_N: u64> Unpin for MerkleMountainRange<MAX_N>

§

impl<const MAX_N: u64> UnwindSafe for MerkleMountainRange<MAX_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> 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.

§

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