zebra_chain/orchard/
tree.rs

1//! Note Commitment Trees.
2//!
3//! A note commitment tree is an incremental Merkle tree of fixed depth
4//! used to store note commitments that Action
5//! transfers produce. Just as the unspent transaction output set (UTXO
6//! set) used in Bitcoin, it is used to express the existence of value and
7//! the capability to spend it. However, unlike the UTXO set, it is not
8//! the job of this tree to protect against double-spending, as it is
9//! append-only.
10//!
11//! A root of a note commitment tree is associated with each treestate.
12
13use std::{
14    default::Default,
15    fmt,
16    hash::{Hash, Hasher},
17    io,
18};
19
20use bitvec::prelude::*;
21use halo2::pasta::{group::ff::PrimeField, pallas};
22use hex::ToHex;
23use incrementalmerkletree::{frontier::NonEmptyFrontier, Hashable};
24use lazy_static::lazy_static;
25use thiserror::Error;
26use zcash_primitives::merkle_tree::HashSer;
27
28use super::sinsemilla::*;
29
30use crate::{
31    serialization::{
32        serde_helpers, ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize,
33    },
34    subtree::{NoteCommitmentSubtreeIndex, TRACKED_SUBTREE_HEIGHT},
35};
36
37pub mod legacy;
38use legacy::LegacyNoteCommitmentTree;
39
40/// The type that is used to update the note commitment tree.
41///
42/// Unfortunately, this is not the same as `orchard::NoteCommitment`.
43pub type NoteCommitmentUpdate = pallas::Base;
44
45pub(super) const MERKLE_DEPTH: u8 = 32;
46
47/// MerkleCRH^Orchard Hash Function
48///
49/// Used to hash incremental Merkle tree hash values for Orchard.
50///
51/// MerkleCRH^Orchard: {0..MerkleDepth^Orchard − 1} × P𝑥 × P𝑥 → P𝑥
52///
53/// MerkleCRH^Orchard(layer, left, right) := 0 if hash == ⊥; hash otherwise
54///
55/// where hash = SinsemillaHash("z.cash:Orchard-MerkleCRH", l || left || right),
56/// l = I2LEBSP_10(MerkleDepth^Orchard − 1 − layer),  and left, right, and
57/// the output are the x-coordinates of Pallas affine points.
58///
59/// <https://zips.z.cash/protocol/protocol.pdf#orchardmerklecrh>
60/// <https://zips.z.cash/protocol/protocol.pdf#constants>
61fn merkle_crh_orchard(layer: u8, left: pallas::Base, right: pallas::Base) -> pallas::Base {
62    let mut s = bitvec![u8, Lsb0;];
63
64    // Prefix: l = I2LEBSP_10(MerkleDepth^Orchard − 1 − layer)
65    let l = MERKLE_DEPTH - 1 - layer;
66    s.extend_from_bitslice(&BitArray::<_, Lsb0>::from([l, 0])[0..10]);
67    s.extend_from_bitslice(&BitArray::<_, Lsb0>::from(left.to_repr())[0..255]);
68    s.extend_from_bitslice(&BitArray::<_, Lsb0>::from(right.to_repr())[0..255]);
69
70    match sinsemilla_hash(b"z.cash:Orchard-MerkleCRH", &s) {
71        Some(h) => h,
72        None => pallas::Base::zero(),
73    }
74}
75
76lazy_static! {
77    /// List of "empty" Orchard note commitment nodes, one for each layer.
78    ///
79    /// The list is indexed by the layer number (0: root; MERKLE_DEPTH: leaf).
80    ///
81    /// <https://zips.z.cash/protocol/protocol.pdf#constants>
82    pub(super) static ref EMPTY_ROOTS: Vec<pallas::Base> = {
83        // The empty leaf node. This is layer 32.
84        let mut v = vec![NoteCommitmentTree::uncommitted()];
85
86        // Starting with layer 31 (the first internal layer, after the leaves),
87        // generate the empty roots up to layer 0, the root.
88        for layer in (0..MERKLE_DEPTH).rev()
89        {
90            // The vector is generated from the end, pushing new nodes to its beginning.
91            // For this reason, the layer below is v[0].
92            let next = merkle_crh_orchard(layer, v[0], v[0]);
93            v.insert(0, next);
94        }
95
96        v
97
98    };
99}
100
101/// Orchard note commitment tree root node hash.
102///
103/// The root hash in LEBS2OSP256(rt) encoding of the Orchard note commitment
104/// tree corresponding to the final Orchard treestate of this block. A root of a
105/// note commitment tree is associated with each treestate.
106#[derive(Clone, Copy, Default, Eq, Serialize, Deserialize)]
107pub struct Root(#[serde(with = "serde_helpers::Base")] pub(crate) pallas::Base);
108
109impl fmt::Debug for Root {
110    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111        f.debug_tuple("Root")
112            .field(&hex::encode(self.0.to_repr()))
113            .finish()
114    }
115}
116
117impl From<Root> for [u8; 32] {
118    fn from(root: Root) -> Self {
119        root.0.into()
120    }
121}
122
123impl From<&Root> for [u8; 32] {
124    fn from(root: &Root) -> Self {
125        (*root).into()
126    }
127}
128
129impl Hash for Root {
130    fn hash<H: Hasher>(&self, state: &mut H) {
131        self.0.to_repr().hash(state)
132    }
133}
134
135impl PartialEq for Root {
136    fn eq(&self, other: &Self) -> bool {
137        // TODO: should we compare canonical forms here using `.to_repr()`?
138        self.0 == other.0
139    }
140}
141
142impl TryFrom<[u8; 32]> for Root {
143    type Error = SerializationError;
144
145    fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
146        let possible_point = pallas::Base::from_repr(bytes);
147
148        if possible_point.is_some().into() {
149            Ok(Self(possible_point.unwrap()))
150        } else {
151            Err(SerializationError::Parse(
152                "Invalid pallas::Base value for Orchard note commitment tree root",
153            ))
154        }
155    }
156}
157
158impl ZcashSerialize for Root {
159    fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
160        writer.write_all(&<[u8; 32]>::from(*self)[..])?;
161
162        Ok(())
163    }
164}
165
166impl ZcashDeserialize for Root {
167    fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
168        Self::try_from(reader.read_32_bytes()?)
169    }
170}
171
172/// A node of the Orchard Incremental Note Commitment Tree.
173#[derive(Copy, Clone, Eq, PartialEq, Default)]
174pub struct Node(pallas::Base);
175
176impl Node {
177    /// Calls `to_repr()` on inner value.
178    pub fn to_repr(&self) -> [u8; 32] {
179        self.0.to_repr()
180    }
181
182    /// Return the node bytes in big-endian byte-order suitable for printing out byte by byte.
183    ///
184    /// `zcashd`'s `z_getsubtreesbyindex` does not reverse the byte order of subtree roots.
185    pub fn bytes_in_display_order(&self) -> [u8; 32] {
186        self.to_repr()
187    }
188}
189
190impl TryFrom<&[u8]> for Node {
191    type Error = &'static str;
192
193    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
194        <[u8; 32]>::try_from(bytes)
195            .map_err(|_| "wrong byte slice len")?
196            .try_into()
197    }
198}
199
200impl TryFrom<[u8; 32]> for Node {
201    type Error = &'static str;
202
203    fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
204        Option::<pallas::Base>::from(pallas::Base::from_repr(bytes))
205            .map(Node)
206            .ok_or("invalid Pallas field element")
207    }
208}
209
210impl fmt::Display for Node {
211    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
212        f.write_str(&self.encode_hex::<String>())
213    }
214}
215
216impl fmt::Debug for Node {
217    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
218        f.debug_tuple("orchard::Node")
219            .field(&self.encode_hex::<String>())
220            .finish()
221    }
222}
223
224impl ToHex for &Node {
225    fn encode_hex<T: FromIterator<char>>(&self) -> T {
226        self.bytes_in_display_order().encode_hex()
227    }
228
229    fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
230        self.bytes_in_display_order().encode_hex_upper()
231    }
232}
233
234impl ToHex for Node {
235    fn encode_hex<T: FromIterator<char>>(&self) -> T {
236        (&self).encode_hex()
237    }
238
239    fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
240        (&self).encode_hex_upper()
241    }
242}
243
244/// Required to serialize [`NoteCommitmentTree`]s in a format compatible with `zcashd`.
245///
246/// Zebra stores Orchard note commitment trees as [`Frontier`][1]s while the
247/// [`z_gettreestate`][2] RPC requires [`CommitmentTree`][3]s. Implementing
248/// [`HashSer`] for [`Node`]s allows the conversion.
249///
250/// [1]: incrementalmerkletree::frontier::Frontier
251/// [2]: https://zcash.github.io/rpc/z_gettreestate.html
252/// [3]: incrementalmerkletree::frontier::CommitmentTree
253impl HashSer for Node {
254    fn read<R: io::Read>(mut reader: R) -> io::Result<Self> {
255        let mut repr = [0u8; 32];
256        reader.read_exact(&mut repr)?;
257        let maybe_node = pallas::Base::from_repr(repr).map(Self);
258
259        <Option<_>>::from(maybe_node).ok_or_else(|| {
260            io::Error::new(
261                io::ErrorKind::InvalidInput,
262                "Non-canonical encoding of Pallas base field value.",
263            )
264        })
265    }
266
267    fn write<W: io::Write>(&self, mut writer: W) -> io::Result<()> {
268        writer.write_all(&self.0.to_repr())
269    }
270}
271
272impl Hashable for Node {
273    fn empty_leaf() -> Self {
274        Self(NoteCommitmentTree::uncommitted())
275    }
276
277    /// Combine two nodes to generate a new node in the given level.
278    /// Level 0 is the layer above the leaves (layer 31).
279    /// Level 31 is the root (layer 0).
280    fn combine(level: incrementalmerkletree::Level, a: &Self, b: &Self) -> Self {
281        let layer = MERKLE_DEPTH - 1 - u8::from(level);
282        Self(merkle_crh_orchard(layer, a.0, b.0))
283    }
284
285    /// Return the node for the level below the given level. (A quirk of the API)
286    fn empty_root(level: incrementalmerkletree::Level) -> Self {
287        let layer_below = usize::from(MERKLE_DEPTH) - usize::from(level);
288        Self(EMPTY_ROOTS[layer_below])
289    }
290}
291
292impl From<pallas::Base> for Node {
293    fn from(x: pallas::Base) -> Self {
294        Node(x)
295    }
296}
297
298impl serde::Serialize for Node {
299    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
300    where
301        S: serde::Serializer,
302    {
303        self.0.to_repr().serialize(serializer)
304    }
305}
306
307impl<'de> serde::Deserialize<'de> for Node {
308    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
309    where
310        D: serde::Deserializer<'de>,
311    {
312        let bytes = <[u8; 32]>::deserialize(deserializer)?;
313        Option::<pallas::Base>::from(pallas::Base::from_repr(bytes))
314            .map(Node)
315            .ok_or_else(|| serde::de::Error::custom("invalid Pallas field element"))
316    }
317}
318
319#[derive(Error, Copy, Clone, Debug, Eq, PartialEq, Hash)]
320#[allow(missing_docs)]
321pub enum NoteCommitmentTreeError {
322    #[error("The note commitment tree is full")]
323    FullTree,
324}
325
326/// Orchard Incremental Note Commitment Tree
327///
328/// Note that the default value of the [`Root`] type is `[0, 0, 0, 0]`. However, this value differs
329/// from the default value of the root of the default tree which is the hash of the root's child
330/// nodes. The default tree is the empty tree which has all leaves empty.
331#[derive(Debug, Serialize, Deserialize)]
332#[serde(into = "LegacyNoteCommitmentTree")]
333#[serde(from = "LegacyNoteCommitmentTree")]
334pub struct NoteCommitmentTree {
335    /// The tree represented as a Frontier.
336    ///
337    /// A Frontier is a subset of the tree that allows to fully specify it.
338    /// It consists of nodes along the rightmost (newer) branch of the tree that
339    /// has non-empty nodes. Upper (near root) empty nodes of the branch are not
340    /// stored.
341    ///
342    /// # Consensus
343    ///
344    /// > [NU5 onward] A block MUST NOT add Orchard note commitments that would result in the Orchard note
345    /// > commitment tree exceeding its capacity of 2^(MerkleDepth^Orchard) leaf nodes.
346    ///
347    /// <https://zips.z.cash/protocol/protocol.pdf#merkletree>
348    ///
349    /// Note: MerkleDepth^Orchard = MERKLE_DEPTH = 32.
350    inner: incrementalmerkletree::frontier::Frontier<Node, MERKLE_DEPTH>,
351
352    /// A cached root of the tree.
353    ///
354    /// Every time the root is computed by [`Self::root`] it is cached here,
355    /// and the cached value will be returned by [`Self::root`] until the tree is
356    /// changed by [`Self::append`]. This greatly increases performance
357    /// because it avoids recomputing the root when the tree does not change
358    /// between blocks. In the finalized state, the tree is read from
359    /// disk for every block processed, which would also require recomputing
360    /// the root even if it has not changed (note that the cached root is
361    /// serialized with the tree). This is particularly important since we decided
362    /// to instantiate the trees from the genesis block, for simplicity.
363    ///
364    /// We use a [`RwLock`](std::sync::RwLock) for this cache, because it is
365    /// only written once per tree update. Each tree has its own cached root, a
366    /// new lock is created for each clone.
367    cached_root: std::sync::RwLock<Option<Root>>,
368}
369
370impl NoteCommitmentTree {
371    /// Adds a note commitment x-coordinate to the tree.
372    ///
373    /// The leaves of the tree are actually a base field element, the
374    /// x-coordinate of the commitment, the data that is actually stored on the
375    /// chain and input into the proof.
376    ///
377    /// Returns an error if the tree is full.
378    #[allow(clippy::unwrap_in_result)]
379    pub fn append(&mut self, cm_x: NoteCommitmentUpdate) -> Result<(), NoteCommitmentTreeError> {
380        if self.inner.append(cm_x.into()) {
381            // Invalidate cached root
382            let cached_root = self
383                .cached_root
384                .get_mut()
385                .expect("a thread that previously held exclusive lock access panicked");
386
387            *cached_root = None;
388
389            Ok(())
390        } else {
391            Err(NoteCommitmentTreeError::FullTree)
392        }
393    }
394
395    /// Returns frontier of non-empty tree, or `None` if the tree is empty.
396    fn frontier(&self) -> Option<&NonEmptyFrontier<Node>> {
397        self.inner.value()
398    }
399
400    /// Returns the position of the most recently appended leaf in the tree.
401    ///
402    /// This method is used for debugging, use `incrementalmerkletree::Address` for tree operations.
403    pub fn position(&self) -> Option<u64> {
404        let Some(tree) = self.frontier() else {
405            // An empty tree doesn't have a previous leaf.
406            return None;
407        };
408
409        Some(tree.position().into())
410    }
411
412    /// Returns true if this tree has at least one new subtree, when compared with `prev_tree`.
413    pub fn contains_new_subtree(&self, prev_tree: &Self) -> bool {
414        // Use -1 for the index of the subtree with no notes, so the comparisons are valid.
415        let index = self.subtree_index().map_or(-1, |index| i32::from(index.0));
416        let prev_index = prev_tree
417            .subtree_index()
418            .map_or(-1, |index| i32::from(index.0));
419
420        // This calculation can't overflow, because we're using i32 for u16 values.
421        let index_difference = index - prev_index;
422
423        // There are 4 cases we need to handle:
424        // - lower index: never a new subtree
425        // - equal index: sometimes a new subtree
426        // - next index: sometimes a new subtree
427        // - greater than the next index: always a new subtree
428        //
429        // To simplify the function, we deal with the simple cases first.
430
431        // There can't be any new subtrees if the current index is strictly lower.
432        if index < prev_index {
433            return false;
434        }
435
436        // There is at least one new subtree, even if there is a spurious index difference.
437        if index_difference > 1 {
438            return true;
439        }
440
441        // If the indexes are equal, there can only be a new subtree if `self` just completed it.
442        if index == prev_index {
443            return self.is_complete_subtree();
444        }
445
446        // If `self` is the next index, check if the last note completed a subtree.
447        if self.is_complete_subtree() {
448            return true;
449        }
450
451        // Then check for spurious index differences.
452        //
453        // There is one new subtree somewhere in the trees. It is either:
454        // - a new subtree at the end of the previous tree, or
455        // - a new subtree in this tree (but not at the end).
456        //
457        // Spurious index differences happen because the subtree index only increases when the
458        // first note is added to the new subtree. So we need to exclude subtrees completed by the
459        // last note commitment in the previous tree.
460        //
461        // We also need to exclude empty previous subtrees, because the index changes to zero when
462        // the first note is added, but a subtree wasn't completed.
463        if prev_tree.is_complete_subtree() || prev_index == -1 {
464            return false;
465        }
466
467        // A new subtree was completed by a note commitment that isn't in the previous tree.
468        true
469    }
470
471    /// Returns true if the most recently appended leaf completes the subtree
472    pub fn is_complete_subtree(&self) -> bool {
473        let Some(tree) = self.frontier() else {
474            // An empty tree can't be a complete subtree.
475            return false;
476        };
477
478        tree.position()
479            .is_complete_subtree(TRACKED_SUBTREE_HEIGHT.into())
480    }
481
482    /// Returns the subtree index at [`TRACKED_SUBTREE_HEIGHT`].
483    /// This is the number of complete or incomplete subtrees that are currently in the tree.
484    /// Returns `None` if the tree is empty.
485    #[allow(clippy::unwrap_in_result)]
486    pub fn subtree_index(&self) -> Option<NoteCommitmentSubtreeIndex> {
487        let tree = self.frontier()?;
488
489        let index = incrementalmerkletree::Address::above_position(
490            TRACKED_SUBTREE_HEIGHT.into(),
491            tree.position(),
492        )
493        .index()
494        .try_into()
495        .expect("fits in u16");
496
497        Some(index)
498    }
499
500    /// Returns the number of leaf nodes required to complete the subtree at
501    /// [`TRACKED_SUBTREE_HEIGHT`].
502    ///
503    /// Returns `2^TRACKED_SUBTREE_HEIGHT` if the tree is empty.
504    #[allow(clippy::unwrap_in_result)]
505    pub fn remaining_subtree_leaf_nodes(&self) -> usize {
506        let remaining = match self.frontier() {
507            // If the subtree has at least one leaf node, the remaining number of nodes can be
508            // calculated using the maximum subtree position and the current position.
509            Some(tree) => {
510                let max_position = incrementalmerkletree::Address::above_position(
511                    TRACKED_SUBTREE_HEIGHT.into(),
512                    tree.position(),
513                )
514                .max_position();
515
516                max_position - tree.position().into()
517            }
518            // If the subtree has no nodes, the remaining number of nodes is the number of nodes in
519            // a subtree.
520            None => {
521                let subtree_address = incrementalmerkletree::Address::above_position(
522                    TRACKED_SUBTREE_HEIGHT.into(),
523                    // This position is guaranteed to be in the first subtree.
524                    0.into(),
525                );
526
527                assert_eq!(
528                    subtree_address.position_range_start(),
529                    0.into(),
530                    "address is not in the first subtree"
531                );
532
533                subtree_address.position_range_end()
534            }
535        };
536
537        u64::from(remaining).try_into().expect("fits in usize")
538    }
539
540    /// Returns subtree index and root if the most recently appended leaf completes the subtree
541    pub fn completed_subtree_index_and_root(&self) -> Option<(NoteCommitmentSubtreeIndex, Node)> {
542        if !self.is_complete_subtree() {
543            return None;
544        }
545
546        let index = self.subtree_index()?;
547        let root = self.frontier()?.root(Some(TRACKED_SUBTREE_HEIGHT.into()));
548
549        Some((index, root))
550    }
551
552    /// Returns the current root of the tree, used as an anchor in Orchard
553    /// shielded transactions.
554    pub fn root(&self) -> Root {
555        if let Some(root) = self.cached_root() {
556            // Return cached root.
557            return root;
558        }
559
560        // Get exclusive access, compute the root, and cache it.
561        let mut write_root = self
562            .cached_root
563            .write()
564            .expect("a thread that previously held exclusive lock access panicked");
565        let read_root = write_root.as_ref().cloned();
566        match read_root {
567            // Another thread got write access first, return cached root.
568            Some(root) => root,
569            None => {
570                // Compute root and cache it.
571                let root = self.recalculate_root();
572                *write_root = Some(root);
573                root
574            }
575        }
576    }
577
578    /// Returns the current root of the tree, if it has already been cached.
579    #[allow(clippy::unwrap_in_result)]
580    pub fn cached_root(&self) -> Option<Root> {
581        *self
582            .cached_root
583            .read()
584            .expect("a thread that previously held exclusive lock access panicked")
585    }
586
587    /// Calculates and returns the current root of the tree, ignoring any caching.
588    pub fn recalculate_root(&self) -> Root {
589        Root(self.inner.root().0)
590    }
591
592    /// Get the Pallas-based Sinsemilla hash / root node of this merkle tree of
593    /// note commitments.
594    pub fn hash(&self) -> [u8; 32] {
595        self.root().into()
596    }
597
598    /// An as-yet unused Orchard note commitment tree leaf node.
599    ///
600    /// Distinct for Orchard, a distinguished hash value of:
601    ///
602    /// Uncommitted^Orchard = I2LEBSP_l_MerkleOrchard(2)
603    pub fn uncommitted() -> pallas::Base {
604        pallas::Base::one().double()
605    }
606
607    /// Count of note commitments added to the tree.
608    ///
609    /// For Orchard, the tree is capped at 2^32.
610    pub fn count(&self) -> u64 {
611        self.inner
612            .value()
613            .map_or(0, |x| u64::from(x.position()) + 1)
614    }
615
616    /// Checks if the tree roots and inner data structures of `self` and `other` are equal.
617    ///
618    /// # Panics
619    ///
620    /// If they aren't equal, with a message explaining the differences.
621    ///
622    /// Only for use in tests.
623    #[cfg(any(test, feature = "proptest-impl"))]
624    pub fn assert_frontier_eq(&self, other: &Self) {
625        // It's technically ok for the cached root not to be preserved,
626        // but it can result in expensive cryptographic operations,
627        // so we fail the tests if it happens.
628        assert_eq!(self.cached_root(), other.cached_root());
629
630        // Check the data in the internal data structure
631        assert_eq!(self.inner, other.inner);
632
633        // Check the RPC serialization format (not the same as the Zebra database format)
634        assert_eq!(self.to_rpc_bytes(), other.to_rpc_bytes());
635    }
636
637    /// Serializes [`Self`] to a format compatible with `zcashd`'s RPCs.
638    pub fn to_rpc_bytes(&self) -> Vec<u8> {
639        // Convert the tree from [`Frontier`](incrementalmerkletree::frontier::Frontier) to
640        // [`CommitmentTree`](merkle_tree::CommitmentTree).
641        let tree = incrementalmerkletree::frontier::CommitmentTree::from_frontier(&self.inner);
642
643        let mut rpc_bytes = vec![];
644
645        zcash_primitives::merkle_tree::write_commitment_tree(&tree, &mut rpc_bytes)
646            .expect("serializable tree");
647
648        rpc_bytes
649    }
650}
651
652impl Clone for NoteCommitmentTree {
653    /// Clones the inner tree, and creates a new `RwLock` with the cloned root data.
654    fn clone(&self) -> Self {
655        let cached_root = self.cached_root();
656
657        Self {
658            inner: self.inner.clone(),
659            cached_root: std::sync::RwLock::new(cached_root),
660        }
661    }
662}
663
664impl Default for NoteCommitmentTree {
665    fn default() -> Self {
666        Self {
667            inner: incrementalmerkletree::frontier::Frontier::empty(),
668            cached_root: Default::default(),
669        }
670    }
671}
672
673impl Eq for NoteCommitmentTree {}
674
675impl PartialEq for NoteCommitmentTree {
676    fn eq(&self, other: &Self) -> bool {
677        if let (Some(root), Some(other_root)) = (self.cached_root(), other.cached_root()) {
678            // Use cached roots if available
679            root == other_root
680        } else {
681            // Avoid expensive root recalculations which use multiple cryptographic hashes
682            self.inner == other.inner
683        }
684    }
685}
686
687impl From<Vec<pallas::Base>> for NoteCommitmentTree {
688    /// Compute the tree from a whole bunch of note commitments at once.
689    fn from(values: Vec<pallas::Base>) -> Self {
690        let mut tree = Self::default();
691
692        if values.is_empty() {
693            return tree;
694        }
695
696        for cm_x in values {
697            let _ = tree.append(cm_x);
698        }
699
700        tree
701    }
702}