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