Skip to main content

BinaryMerkleTree

Struct BinaryMerkleTree 

Source
pub struct BinaryMerkleTree<D: Send, A: Allocator = GlobalAllocator> {
    pub log_len: usize,
    pub inner_nodes: A::Vec<D>,
}
Expand description

A binary Merkle tree that commits batches of vectors.

§Overview

The entries sharing an index across a batch are hashed together into one leaf digest. A binary tree is then folded over those leaf digests.

All committed vectors must have the same length, and that length must be a power of two.

The nodes are drawn from an Allocator, so a prover can back the whole tree with pooled memory instead of the global heap. The default is GlobalAllocator, which is a plain Vec.

Fields§

§log_len: usize

Base-2 logarithm of the number of leaves.

§inner_nodes: A::Vec<D>

The inner nodes, arranged as a flattened array of layers with the root at the end.

Implementations§

Source§

impl<N: ArraySize, A: Allocator> BinaryMerkleTree<Array<u8, N>, A>

Source

pub fn new<F, H>( elements: &[F], batch_size: usize, alloc: &A, ) -> Result<Self, Error>
where F: Field, H: ParallelHashSuite<LeafHash: OutputSizeUser<OutputSize = N>>,

Commits a slice of values, cutting it into consecutive leaves.

§Arguments
  • elements - the values to commit, in leaf order.
  • batch_size - how many consecutive values are hashed together into one leaf.
  • alloc - the allocator the tree’s nodes are drawn from.
§Errors
  • The value count is not a multiple of the batch size.
  • The resulting leaf count is not a power of two.
Source

pub fn from_leaves<F, H, ParIter>( leaves: ParIter, n_items_per_input: usize, alloc: &A, ) -> Self
where F: Field, H: ParallelHashSuite<LeafHash: OutputSizeUser<OutputSize = N>>, ParIter: IndexedParallelIterator<Item: IntoIterator<Item = F, IntoIter: Send>>,

Commits leaves drawn from a parallel iterator, one iterator item per leaf.

§Overview

The tree is laid out as one flat buffer of layers, widest first:

    [ leaf digests | layer 1 | ... | root ]
       2^log_len      2^(log_len-1)    1

Each layer is written into the buffer’s spare capacity. It is then read back as the input to the layer above it.

§Arguments
  • leaves - one iterator per leaf, each yielding that leaf’s values.
  • n_items_per_input - how many values every leaf iterator yields.
  • alloc - the allocator the tree’s nodes are drawn from.
§Panics

Panics unless the number of leaves is a power of two.

Source§

impl<D: Clone + Send, A: Allocator> BinaryMerkleTree<D, A>

Source

pub fn root(&self) -> D

Clones the root digest, which sits last in the flattened layers.

Source

pub fn layer(&self, layer_depth: usize) -> Result<&[D], Error>

Borrows one whole layer of the tree, counting depth from the root.

§Errors
  • The depth lies below the leaves.
Source

pub fn branch(&self, index: usize, layer_depth: usize) -> Result<Vec<D>, Error>

Collects the sibling digests opening a leaf up to the layer at layer_depth.

§Errors
  • The leaf index lies outside the tree.
  • The depth lies below the leaves.

Trait Implementations§

Source§

impl<D: Debug + Send, A: Allocator> Debug for BinaryMerkleTree<D, A>

Written through the node slice rather than the buffer, so no allocator has to be Debug itself for a tree over it to be.

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<D, A> Freeze for BinaryMerkleTree<D, A>
where <A as Allocator>::Vec<D>: Freeze,

§

impl<D, A> RefUnwindSafe for BinaryMerkleTree<D, A>
where <A as Allocator>::Vec<D>: RefUnwindSafe,

§

impl<D, A> Send for BinaryMerkleTree<D, A>

§

impl<D, A> Sync for BinaryMerkleTree<D, A>
where <A as Allocator>::Vec<D>: Sync,

§

impl<D, A> Unpin for BinaryMerkleTree<D, A>
where <A as Allocator>::Vec<D>: Unpin,

§

impl<D, A> UnsafeUnpin for BinaryMerkleTree<D, A>
where <A as Allocator>::Vec<D>: UnsafeUnpin,

§

impl<D, A> UnwindSafe for BinaryMerkleTree<D, A>
where <A as Allocator>::Vec<D>: 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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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
§

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

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more