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