pub struct TieredSmt { /* private fields */ }
Expand description

Tiered (compacted) Sparse Merkle tree mapping 256-bit keys to 256-bit values. Both keys and values are represented by 4 field elements.

Leaves in the tree can exist only on specific depths called “tiers”. These depths are: 16, 32, 48, and 64. Initially, when a tree is empty, it is equivalent to an empty Sparse Merkle tree of depth 64 (i.e., leaves at depth 64 are set to [ZERO; 4]). As non-empty values are inserted into the tree they are added to the first available tier.

For example, when the first key-value pair is inserted, it will be stored in a node at depth 16 such that the 16 most significant bits of the key determine the position of the node at depth 16. If another value with a key sharing the same 16-bit prefix is inserted, both values move into the next tier (depth 32). This process is repeated until values end up at the bottom tier (depth 64). If multiple values have keys with a common 64-bit prefix, such key-value pairs are stored in a sorted list at the bottom tier.

To differentiate between internal and leaf nodes, node values are computed as follows:

  • Internal nodes: hash(left_child, right_child).
  • Leaf node at depths 16, 32, or 64: hash(key, value, domain=depth).
  • Leaf node at depth 64: hash([key_0, value_0, …, key_n, value_n], domain=64).

Implementations§

source§

impl TieredSmt

source

pub const TIER_SIZE: u8 = 16u8

The number of levels between tiers.

source

pub const TIER_DEPTHS: [u8; 4] = _

Depths at which leaves can exist in a tiered SMT.

source

pub const MAX_DEPTH: u8 = 64u8

Maximum node depth. This is also the bottom tier of the tree.

source

pub const EMPTY_VALUE: Word = super::EMPTY_WORD

Value of an empty leaf.

source

pub fn with_entries<R, I>(entries: R) -> Result<Self, MerkleError>where R: IntoIterator<IntoIter = I>, I: Iterator<Item = (RpoDigest, Word)> + ExactSizeIterator,

Returns a new TieredSmt instantiated with the specified key-value pairs.

Errors

Returns an error if the provided entries contain multiple values for the same key.

source

pub const fn root(&self) -> RpoDigest

Returns the root of this Merkle tree.

source

pub fn get_node(&self, index: NodeIndex) -> Result<RpoDigest, MerkleError>

Returns a node at the specified index.

Errors

Returns an error if:

  • The specified index depth is 0 or greater than 64.
  • The node with the specified index does not exists in the Merkle tree. This is possible when a leaf node with the same index prefix exists at a tier higher than the requested node.
source

pub fn get_path(&self, index: NodeIndex) -> Result<MerklePath, MerkleError>

Returns a Merkle path from the node at the specified index to the root.

The node itself is not included in the path.

Errors

Returns an error if:

  • The specified index depth is 0 or greater than 64.
  • The node with the specified index does not exists in the Merkle tree. This is possible when a leaf node with the same index prefix exists at a tier higher than the node to which the path is requested.
source

pub fn get_value(&self, key: RpoDigest) -> Word

Returns the value associated with the specified key.

If nothing was inserted into this tree for the specified key, [ZERO; 4] is returned.

source

pub fn prove(&self, key: RpoDigest) -> TieredSmtProof

Returns a proof for a key-value pair defined by the specified key.

The proof can be used to attest membership of this key-value pair in a Tiered Sparse Merkle Tree defined by the same root as this tree.

source

pub fn insert(&mut self, key: RpoDigest, value: Word) -> Word

Inserts the provided value into the tree under the specified key and returns the value previously stored under this key.

If the value for the specified key was not previously set, [ZERO; 4] is returned.

source

pub fn iter(&self) -> impl Iterator<Item = &(RpoDigest, Word)>

Returns an iterator over all key-value pairs in this TieredSmt.

source

pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo> + '_

Returns an iterator over all inner nodes of this TieredSmt (i.e., nodes not at depths 16 32, 48, or 64).

The iterator order is unspecified.

source

pub fn upper_leaves( &self ) -> impl Iterator<Item = (RpoDigest, RpoDigest, Word)> + '_

Returns an iterator over upper leaves (i.e., depth = 16, 32, or 48) for this TieredSmt where each yielded item is a (node, key, value) tuple.

The iterator order is unspecified.

source

pub fn upper_leaf_nodes(&self) -> impl Iterator<Item = (&NodeIndex, &RpoDigest)>

Returns an iterator over upper leaves (i.e., depth = 16, 32, or 48) for this TieredSmt where each yielded item is a (node_index, value) tuple.

source

pub fn bottom_leaves( &self ) -> impl Iterator<Item = (RpoDigest, Vec<(RpoDigest, Word)>)> + '_

Returns an iterator over bottom leaves (i.e., depth = 64) of this TieredSmt.

Each yielded item consists of the hash of the leaf and its contents, where contents is a vector containing key-value pairs of entries storied in this leaf.

The iterator order is unspecified.

Trait Implementations§

source§

impl Clone for TieredSmt

source§

fn clone(&self) -> TieredSmt

Returns a copy 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 Debug for TieredSmt

source§

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

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

impl Default for TieredSmt

source§

fn default() -> Self

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

impl<T: KvMap<RpoDigest, StoreNode>> From<&TieredSmt> for MerkleStore<T>

source§

fn from(value: &TieredSmt) -> Self

Converts to this type from the input type.
source§

impl PartialEq for TieredSmt

source§

fn eq(&self, other: &TieredSmt) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for TieredSmt

source§

impl StructuralEq for TieredSmt

source§

impl StructuralPartialEq for TieredSmt

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.