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 bridgetree::NonEmptyFrontier;
22use halo2::pasta::{group::ff::PrimeField, pallas};
23use hex::ToHex;
24use incrementalmerkletree::Hashable;
25use lazy_static::lazy_static;
26use thiserror::Error;
27use zcash_primitives::merkle_tree::HashSer;
28
29use super::sinsemilla::*;
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 `orchard::NoteCommitment`.
44pub type NoteCommitmentUpdate = pallas::Base;
45
46pub(super) const MERKLE_DEPTH: u8 = 32;
47
48/// MerkleCRH^Orchard Hash Function
49///
50/// Used to hash incremental Merkle tree hash values for Orchard.
51///
52/// MerkleCRH^Orchard: {0..MerkleDepth^Orchard ā 1} Ć Pš„ Ć Pš„ ā Pš„
53///
54/// MerkleCRH^Orchard(layer, left, right) := 0 if hash == ā„; hash otherwise
55///
56/// where hash = SinsemillaHash("z.cash:Orchard-MerkleCRH", l || left || right),
57/// l = I2LEBSP_10(MerkleDepth^Orchard ā 1 ā layer), and left, right, and
58/// the output are the x-coordinates of Pallas affine points.
59///
60/// <https://zips.z.cash/protocol/protocol.pdf#orchardmerklecrh>
61/// <https://zips.z.cash/protocol/protocol.pdf#constants>
62fn merkle_crh_orchard(layer: u8, left: pallas::Base, right: pallas::Base) -> pallas::Base {
63 let mut s = bitvec![u8, Lsb0;];
64
65 // Prefix: l = I2LEBSP_10(MerkleDepth^Orchard ā 1 ā layer)
66 let l = MERKLE_DEPTH - 1 - layer;
67 s.extend_from_bitslice(&BitArray::<_, Lsb0>::from([l, 0])[0..10]);
68 s.extend_from_bitslice(&BitArray::<_, Lsb0>::from(left.to_repr())[0..255]);
69 s.extend_from_bitslice(&BitArray::<_, Lsb0>::from(right.to_repr())[0..255]);
70
71 match sinsemilla_hash(b"z.cash:Orchard-MerkleCRH", &s) {
72 Some(h) => h,
73 None => pallas::Base::zero(),
74 }
75}
76
77lazy_static! {
78 /// List of "empty" Orchard note commitment nodes, one for each layer.
79 ///
80 /// The list is indexed by the layer number (0: root; MERKLE_DEPTH: leaf).
81 ///
82 /// <https://zips.z.cash/protocol/protocol.pdf#constants>
83 pub(super) static ref EMPTY_ROOTS: Vec<pallas::Base> = {
84 // The empty leaf node. This is layer 32.
85 let mut v = vec![NoteCommitmentTree::uncommitted()];
86
87 // Starting with layer 31 (the first internal layer, after the leaves),
88 // generate the empty roots up to layer 0, the root.
89 for layer in (0..MERKLE_DEPTH).rev()
90 {
91 // The vector is generated from the end, pushing new nodes to its beginning.
92 // For this reason, the layer below is v[0].
93 let next = merkle_crh_orchard(layer, v[0], v[0]);
94 v.insert(0, next);
95 }
96
97 v
98
99 };
100}
101
102/// Orchard note commitment tree root node hash.
103///
104/// The root hash in LEBS2OSP256(rt) encoding of the Orchard note commitment
105/// tree corresponding to the final Orchard treestate of this block. A root of a
106/// note commitment tree is associated with each treestate.
107#[derive(Clone, Copy, Default, Eq, Serialize, Deserialize)]
108pub struct Root(#[serde(with = "serde_helpers::Base")] pub(crate) pallas::Base);
109
110impl fmt::Debug for Root {
111 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112 f.debug_tuple("Root")
113 .field(&hex::encode(self.0.to_repr()))
114 .finish()
115 }
116}
117
118impl From<Root> for [u8; 32] {
119 fn from(root: Root) -> Self {
120 root.0.into()
121 }
122}
123
124impl From<&Root> for [u8; 32] {
125 fn from(root: &Root) -> Self {
126 (*root).into()
127 }
128}
129
130impl Hash for Root {
131 fn hash<H: Hasher>(&self, state: &mut H) {
132 self.0.to_repr().hash(state)
133 }
134}
135
136impl PartialEq for Root {
137 fn eq(&self, other: &Self) -> bool {
138 // TODO: should we compare canonical forms here using `.to_repr()`?
139 self.0 == other.0
140 }
141}
142
143impl TryFrom<[u8; 32]> for Root {
144 type Error = SerializationError;
145
146 fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
147 let possible_point = pallas::Base::from_repr(bytes);
148
149 if possible_point.is_some().into() {
150 Ok(Self(possible_point.unwrap()))
151 } else {
152 Err(SerializationError::Parse(
153 "Invalid pallas::Base value for Orchard note commitment tree root",
154 ))
155 }
156 }
157}
158
159impl ZcashSerialize for Root {
160 fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
161 writer.write_all(&<[u8; 32]>::from(*self)[..])?;
162
163 Ok(())
164 }
165}
166
167impl ZcashDeserialize for Root {
168 fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
169 Self::try_from(reader.read_32_bytes()?)
170 }
171}
172
173/// A node of the Orchard Incremental Note Commitment Tree.
174#[derive(Copy, Clone, Eq, PartialEq, Default)]
175pub struct Node(pallas::Base);
176
177impl Node {
178 /// Calls `to_repr()` on inner value.
179 pub fn to_repr(&self) -> [u8; 32] {
180 self.0.to_repr()
181 }
182
183 /// Return the node bytes in big-endian byte-order suitable for printing out byte by byte.
184 ///
185 /// `zcashd`'s `z_getsubtreesbyindex` does not reverse the byte order of subtree roots.
186 pub fn bytes_in_display_order(&self) -> [u8; 32] {
187 self.to_repr()
188 }
189}
190
191impl TryFrom<&[u8]> for Node {
192 type Error = &'static str;
193
194 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
195 <[u8; 32]>::try_from(bytes)
196 .map_err(|_| "wrong byte slice len")?
197 .try_into()
198 }
199}
200
201impl TryFrom<[u8; 32]> for Node {
202 type Error = &'static str;
203
204 fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
205 Option::<pallas::Base>::from(pallas::Base::from_repr(bytes))
206 .map(Node)
207 .ok_or("invalid Pallas field element")
208 }
209}
210
211impl fmt::Display for Node {
212 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
213 f.write_str(&self.encode_hex::<String>())
214 }
215}
216
217impl fmt::Debug for Node {
218 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
219 f.debug_tuple("orchard::Node")
220 .field(&self.encode_hex::<String>())
221 .finish()
222 }
223}
224
225impl ToHex for &Node {
226 fn encode_hex<T: FromIterator<char>>(&self) -> T {
227 self.bytes_in_display_order().encode_hex()
228 }
229
230 fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
231 self.bytes_in_display_order().encode_hex_upper()
232 }
233}
234
235impl ToHex for Node {
236 fn encode_hex<T: FromIterator<char>>(&self) -> T {
237 (&self).encode_hex()
238 }
239
240 fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
241 (&self).encode_hex_upper()
242 }
243}
244
245/// Required to serialize [`NoteCommitmentTree`]s in a format compatible with `zcashd`.
246///
247/// Zebra stores Orchard note commitment trees as [`Frontier`][1]s while the
248/// [`z_gettreestate`][2] RPC requires [`CommitmentTree`][3]s. Implementing
249/// [`HashSer`] for [`Node`]s allows the conversion.
250///
251/// [1]: bridgetree::Frontier
252/// [2]: https://zcash.github.io/rpc/z_gettreestate.html
253/// [3]: incrementalmerkletree::frontier::CommitmentTree
254impl HashSer for Node {
255 fn read<R: io::Read>(mut reader: R) -> io::Result<Self> {
256 let mut repr = [0u8; 32];
257 reader.read_exact(&mut repr)?;
258 let maybe_node = pallas::Base::from_repr(repr).map(Self);
259
260 <Option<_>>::from(maybe_node).ok_or_else(|| {
261 io::Error::new(
262 io::ErrorKind::InvalidInput,
263 "Non-canonical encoding of Pallas base field value.",
264 )
265 })
266 }
267
268 fn write<W: io::Write>(&self, mut writer: W) -> io::Result<()> {
269 writer.write_all(&self.0.to_repr())
270 }
271}
272
273impl Hashable for Node {
274 fn empty_leaf() -> Self {
275 Self(NoteCommitmentTree::uncommitted())
276 }
277
278 /// Combine two nodes to generate a new node in the given level.
279 /// Level 0 is the layer above the leaves (layer 31).
280 /// Level 31 is the root (layer 0).
281 fn combine(level: incrementalmerkletree::Level, a: &Self, b: &Self) -> Self {
282 let layer = MERKLE_DEPTH - 1 - u8::from(level);
283 Self(merkle_crh_orchard(layer, a.0, b.0))
284 }
285
286 /// Return the node for the level below the given level. (A quirk of the API)
287 fn empty_root(level: incrementalmerkletree::Level) -> Self {
288 let layer_below = usize::from(MERKLE_DEPTH) - usize::from(level);
289 Self(EMPTY_ROOTS[layer_below])
290 }
291}
292
293impl From<pallas::Base> for Node {
294 fn from(x: pallas::Base) -> Self {
295 Node(x)
296 }
297}
298
299impl serde::Serialize for Node {
300 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
301 where
302 S: serde::Serializer,
303 {
304 self.0.to_repr().serialize(serializer)
305 }
306}
307
308impl<'de> serde::Deserialize<'de> for Node {
309 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
310 where
311 D: serde::Deserializer<'de>,
312 {
313 let bytes = <[u8; 32]>::deserialize(deserializer)?;
314 Option::<pallas::Base>::from(pallas::Base::from_repr(bytes))
315 .map(Node)
316 .ok_or_else(|| serde::de::Error::custom("invalid Pallas field element"))
317 }
318}
319
320#[derive(Error, Copy, Clone, Debug, Eq, PartialEq, Hash)]
321#[allow(missing_docs)]
322pub enum NoteCommitmentTreeError {
323 #[error("The note commitment tree is full")]
324 FullTree,
325}
326
327/// Orchard Incremental Note Commitment Tree
328///
329/// Note that the default value of the [`Root`] type is `[0, 0, 0, 0]`. However, this value differs
330/// from the default value of the root of the default tree which is the hash of the root's child
331/// nodes. The default tree is the empty tree which has all leaves empty.
332#[derive(Debug, Serialize, Deserialize)]
333#[serde(into = "LegacyNoteCommitmentTree")]
334#[serde(from = "LegacyNoteCommitmentTree")]
335pub struct NoteCommitmentTree {
336 /// The tree represented as a Frontier.
337 ///
338 /// A Frontier is a subset of the tree that allows to fully specify it.
339 /// It consists of nodes along the rightmost (newer) branch of the tree that
340 /// has non-empty nodes. Upper (near root) empty nodes of the branch are not
341 /// stored.
342 ///
343 /// # Consensus
344 ///
345 /// > [NU5 onward] A block MUST NOT add Orchard note commitments that would result in the Orchard note
346 /// > commitment tree exceeding its capacity of 2^(MerkleDepth^Orchard) leaf nodes.
347 ///
348 /// <https://zips.z.cash/protocol/protocol.pdf#merkletree>
349 ///
350 /// Note: MerkleDepth^Orchard = MERKLE_DEPTH = 32.
351 inner: bridgetree::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,
356 /// and the cached value will be returned by [`Self::root`] until the tree is
357 /// changed by [`Self::append`]. This greatly increases performance
358 /// because it avoids recomputing the root when the tree does not change
359 /// between blocks. In the finalized state, the tree is read from
360 /// disk for every block processed, which would also require recomputing
361 /// the root even if it has not changed (note that the cached root is
362 /// serialized with the tree). This is particularly important since we decided
363 /// to instantiate the trees from the genesis block, for simplicity.
364 ///
365 /// We use a [`RwLock`](std::sync::RwLock) for this cache, because it is
366 /// only written once per tree update. Each tree has its own cached root, a
367 /// new lock is created for each clone.
368 cached_root: std::sync::RwLock<Option<Root>>,
369}
370
371impl NoteCommitmentTree {
372 /// Adds a note commitment x-coordinate to the tree.
373 ///
374 /// The leaves of the tree are actually a base field element, the
375 /// x-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_x: NoteCommitmentUpdate) -> Result<(), NoteCommitmentTreeError> {
381 if self.inner.append(cm_x.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` if the tree is empty.
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 Orchard
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(self.inner.root().0)
591 }
592
593 /// Get the Pallas-based Sinsemilla hash / 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 Orchard note commitment tree leaf node.
600 ///
601 /// Distinct for Orchard, a distinguished hash value of:
602 ///
603 /// Uncommitted^Orchard = I2LEBSP_l_MerkleOrchard(2)
604 pub fn uncommitted() -> pallas::Base {
605 pallas::Base::one().double()
606 }
607
608 /// Count of note commitments added to the tree.
609 ///
610 /// For Orchard, 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 compatible with `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` with the cloned root data.
655 fn clone(&self) -> Self {
656 let cached_root = self.cached_root();
657
658 Self {
659 inner: self.inner.clone(),
660 cached_root: std::sync::RwLock::new(cached_root),
661 }
662 }
663}
664
665impl Default for NoteCommitmentTree {
666 fn default() -> Self {
667 Self {
668 inner: bridgetree::Frontier::empty(),
669 cached_root: Default::default(),
670 }
671 }
672}
673
674impl Eq for NoteCommitmentTree {}
675
676impl PartialEq for NoteCommitmentTree {
677 fn eq(&self, other: &Self) -> bool {
678 if let (Some(root), Some(other_root)) = (self.cached_root(), other.cached_root()) {
679 // Use cached roots if available
680 root == other_root
681 } else {
682 // Avoid expensive root recalculations which use multiple cryptographic hashes
683 self.inner == other.inner
684 }
685 }
686}
687
688impl From<Vec<pallas::Base>> for NoteCommitmentTree {
689 /// Compute the tree from a whole bunch of note commitments at once.
690 fn from(values: Vec<pallas::Base>) -> Self {
691 let mut tree = Self::default();
692
693 if values.is_empty() {
694 return tree;
695 }
696
697 for cm_x in values {
698 let _ = tree.append(cm_x);
699 }
700
701 tree
702 }
703}