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: usizeBase-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>
impl<N: ArraySize, A: Allocator> BinaryMerkleTree<Array<u8, N>, A>
Sourcepub fn new<F, H>(
elements: &[F],
batch_size: usize,
alloc: &A,
) -> Result<Self, Error>where
F: Field,
H: ParallelHashSuite<LeafHash: OutputSizeUser<OutputSize = N>>,
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.
Sourcepub fn from_leaves<F, H, ParIter>(
leaves: ParIter,
n_items_per_input: usize,
alloc: &A,
) -> Selfwhere
F: Field,
H: ParallelHashSuite<LeafHash: OutputSizeUser<OutputSize = N>>,
ParIter: IndexedParallelIterator<Item: IntoIterator<Item = F, IntoIter: Send>>,
pub fn from_leaves<F, H, ParIter>(
leaves: ParIter,
n_items_per_input: usize,
alloc: &A,
) -> Selfwhere
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) 1Each 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.
Trait Implementations§
Auto Trait Implementations§
impl<D, A> Freeze for BinaryMerkleTree<D, A>
impl<D, A> RefUnwindSafe for BinaryMerkleTree<D, A>
impl<D, A> Send for BinaryMerkleTree<D, A>
impl<D, A> Sync for BinaryMerkleTree<D, A>
impl<D, A> Unpin for BinaryMerkleTree<D, A>
impl<D, A> UnsafeUnpin for BinaryMerkleTree<D, A>
impl<D, A> UnwindSafe for BinaryMerkleTree<D, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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