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