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