miden_objects/batch/
note_tree.rs

1use miden_crypto::{
2    hash::rpo::RpoDigest,
3    merkle::{MerkleError, SimpleSmt},
4};
5
6use crate::{
7    note::{compute_note_hash, NoteId, NoteMetadata},
8    BATCH_NOTE_TREE_DEPTH,
9};
10
11/// Wrapper over [SimpleSmt<BATCH_NOTE_TREE_DEPTH>] for batch note tree.
12///
13/// Value of each leaf is computed as: `hash(note_id || note_metadata)`.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct BatchNoteTree(SimpleSmt<BATCH_NOTE_TREE_DEPTH>);
16
17impl BatchNoteTree {
18    /// Wrapper around [`SimpleSmt::with_contiguous_leaves`] which populates notes at contiguous
19    /// indices starting at index 0.
20    ///
21    /// # Errors
22    /// Returns an error if the number of entries exceeds the maximum tree capacity, that is
23    /// 2^{depth}.
24    pub fn with_contiguous_leaves<'a>(
25        entries: impl IntoIterator<Item = (NoteId, &'a NoteMetadata)>,
26    ) -> Result<Self, MerkleError> {
27        let leaves = entries
28            .into_iter()
29            .map(|(note_id, metadata)| compute_note_hash(note_id, metadata).into());
30
31        SimpleSmt::with_contiguous_leaves(leaves).map(Self)
32    }
33
34    /// Returns the root of the tree
35    pub fn root(&self) -> RpoDigest {
36        self.0.root()
37    }
38}