Skip to main content

zebra_chain/sprout/
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::fmt;
14
15use byteorder::{BigEndian, ByteOrder};
16use incrementalmerkletree::frontier::Frontier;
17use lazy_static::lazy_static;
18use thiserror::Error;
19
20use super::commitment::NoteCommitment;
21
22pub mod legacy;
23use legacy::LegacyNoteCommitmentTree;
24
25#[cfg(any(test, feature = "proptest-impl"))]
26use proptest_derive::Arbitrary;
27
28/// Sprout note commitment trees have a max depth of 29.
29///
30/// <https://zips.z.cash/protocol/protocol.pdf#constants>
31pub(super) const MERKLE_DEPTH: u8 = 29;
32
33/// [MerkleCRH^Sprout] Hash Function.
34///
35/// Creates nodes of the note commitment tree.
36///
37/// MerkleCRH^Sprout(layer, left, right) := SHA256Compress(left || right).
38///
39/// Note: the implementation of MerkleCRH^Sprout does not use the `layer`
40/// argument from the definition above since the argument does not affect the output.
41///
42/// [MerkleCRH^Sprout]: https://zips.z.cash/protocol/protocol.pdf#merklecrh
43fn merkle_crh_sprout(left: [u8; 32], right: [u8; 32]) -> [u8; 32] {
44    let mut other_block = [0u8; 64];
45    other_block[..32].copy_from_slice(&left[..]);
46    other_block[32..].copy_from_slice(&right[..]);
47
48    // H256: SHA-256 initial state.
49    // https://github.com/RustCrypto/hashes/blob/master/sha2/src/consts.rs#L170
50    let mut state = [
51        0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
52        0x5be0cd19,
53    ];
54
55    sha2::compress256(&mut state, &[other_block.into()]);
56
57    // Yes, SHA-256 does big endian here.
58    // https://github.com/RustCrypto/hashes/blob/master/sha2/src/sha256.rs#L40
59    let mut derived_bytes = [0u8; 32];
60    BigEndian::write_u32_into(&state, &mut derived_bytes);
61
62    derived_bytes
63}
64
65lazy_static! {
66    /// List of "empty" Sprout note commitment roots (nodes), one for each layer.
67    ///
68    /// The list is indexed by the layer number (0: root; `MERKLE_DEPTH`: leaf).
69    pub(super) static ref EMPTY_ROOTS: Vec<[u8; 32]> = {
70        // The empty leaf node at layer `MERKLE_DEPTH`.
71        let mut v = vec![NoteCommitmentTree::uncommitted()];
72
73        // Starting with layer `MERKLE_DEPTH` - 1 (the first internal layer, after the leaves),
74        // generate the empty roots up to layer 0, the root.
75        for _ in 0..MERKLE_DEPTH {
76            // The vector is generated from the end, pushing new nodes to its beginning.
77            // For this reason, the layer below is v[0].
78            v.insert(0, merkle_crh_sprout(v[0], v[0]));
79        }
80
81        v
82    };
83}
84
85/// Sprout note commitment tree root node hash.
86///
87/// The root hash in LEBS2OSP256(rt) encoding of the Sprout note
88/// commitment tree corresponding to the final Sprout treestate of
89/// this block. A root of a note commitment tree is associated with
90/// each treestate.
91#[derive(Clone, Copy, Default, Eq, PartialEq, Serialize, Deserialize, Hash)]
92#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
93pub struct Root([u8; 32]);
94
95impl Root {
96    /// Return the bytes in big-endian byte order as required
97    /// by RPCs such as `getrawtransaction`.
98    pub fn bytes_in_display_order(&self) -> [u8; 32] {
99        let mut root: [u8; 32] = self.into();
100        root.reverse();
101        root
102    }
103}
104
105impl fmt::Debug for Root {
106    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107        f.debug_tuple("Root").field(&hex::encode(self.0)).finish()
108    }
109}
110
111impl From<[u8; 32]> for Root {
112    fn from(bytes: [u8; 32]) -> Root {
113        Self(bytes)
114    }
115}
116
117impl From<Root> for [u8; 32] {
118    fn from(rt: Root) -> [u8; 32] {
119        rt.0
120    }
121}
122
123impl From<&[u8; 32]> for Root {
124    fn from(bytes: &[u8; 32]) -> Root {
125        (*bytes).into()
126    }
127}
128
129impl From<&Root> for [u8; 32] {
130    fn from(root: &Root) -> Self {
131        (*root).into()
132    }
133}
134
135/// A node of the Sprout note commitment tree.
136#[derive(Clone, Copy, Eq, PartialEq)]
137pub struct Node([u8; 32]);
138
139impl fmt::Debug for Node {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        f.debug_tuple("Node").field(&hex::encode(self.0)).finish()
142    }
143}
144
145impl incrementalmerkletree::Hashable for Node {
146    /// Returns an empty leaf.
147    fn empty_leaf() -> Self {
148        Self(NoteCommitmentTree::uncommitted())
149    }
150
151    /// Combines two nodes to generate a new node using [MerkleCRH^Sprout].
152    ///
153    /// Note that Sprout does not use the `level` argument.
154    ///
155    /// [MerkleCRH^Sprout]: https://zips.z.cash/protocol/protocol.pdf#sproutmerklecrh
156    fn combine(_level: incrementalmerkletree::Level, a: &Self, b: &Self) -> Self {
157        Self(merkle_crh_sprout(a.0, b.0))
158    }
159
160    /// Returns the node for the level below the given level. (A quirk of the API)
161    fn empty_root(level: incrementalmerkletree::Level) -> Self {
162        let layer_below = usize::from(MERKLE_DEPTH) - usize::from(level);
163        Self(EMPTY_ROOTS[layer_below])
164    }
165}
166
167impl From<NoteCommitment> for Node {
168    fn from(cm: NoteCommitment) -> Self {
169        Node(cm.into())
170    }
171}
172
173impl serde::Serialize for Node {
174    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
175    where
176        S: serde::Serializer,
177    {
178        self.0.serialize(serializer)
179    }
180}
181
182impl<'de> serde::Deserialize<'de> for Node {
183    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
184    where
185        D: serde::Deserializer<'de>,
186    {
187        let bytes = <[u8; 32]>::deserialize(deserializer)?;
188        let cm = NoteCommitment::from(bytes);
189        let node = Node::from(cm);
190
191        Ok(node)
192    }
193}
194
195#[derive(Error, Copy, Clone, Debug, Eq, PartialEq, Hash)]
196#[allow(missing_docs)]
197pub enum NoteCommitmentTreeError {
198    #[error("the note commitment tree is full")]
199    FullTree,
200}
201
202/// [Sprout Note Commitment Tree].
203///
204/// An incremental Merkle tree of fixed depth used to store Sprout note commitments.
205/// It is used to express the existence of value and the capability to spend it. It is _not_ the
206/// job of this tree to protect against double-spending, as it is append-only; double-spending
207/// is prevented by maintaining the [nullifier set] for each shielded pool.
208///
209/// Internally this wraps [`incrementalmerkletree::frontier::Frontier`], so that we can maintain and increment
210/// the full tree with only the minimal amount of non-empty nodes/leaves required.
211///
212/// Note that the default value of the [`Root`] type is `[0, 0, 0, 0]`. However, this value differs
213/// from the default value of the root of the default tree (which is the empty tree) since it is the
214/// pair-wise root-hash of the tree's empty leaves at the tree's root level.
215///
216/// [Sprout Note Commitment Tree]: https://zips.z.cash/protocol/protocol.pdf#merkletree
217/// [nullifier set]: https://zips.z.cash/protocol/protocol.pdf#nullifierset
218#[derive(Debug, Serialize, Deserialize)]
219#[serde(into = "LegacyNoteCommitmentTree")]
220#[serde(from = "LegacyNoteCommitmentTree")]
221pub struct NoteCommitmentTree {
222    /// The tree represented as a [`incrementalmerkletree::frontier::Frontier`].
223    ///
224    /// A [`incrementalmerkletree::frontier::Frontier`] is a subset of the tree that allows to fully specify it. It
225    /// consists of nodes along the rightmost (newer) branch of the tree that
226    /// has non-empty nodes. Upper (near root) empty nodes of the branch are not
227    /// stored.
228    ///
229    /// # Consensus
230    ///
231    /// > A block MUST NOT add Sprout note commitments that would result in the Sprout note commitment tree
232    /// > exceeding its capacity of 2^(MerkleDepth^Sprout) leaf nodes.
233    ///
234    /// <https://zips.z.cash/protocol/protocol.pdf#merkletree>
235    ///
236    /// Note: MerkleDepth^Sprout = MERKLE_DEPTH = 29.
237    inner: Frontier<Node, MERKLE_DEPTH>,
238
239    /// A cached root of the tree.
240    ///
241    /// Every time the root is computed by [`Self::root`], it is cached here,
242    /// and the cached value will be returned by [`Self::root`] until the tree
243    /// is changed by [`Self::append`]. This greatly increases performance
244    /// because it avoids recomputing the root when the tree does not change
245    /// between blocks. In the finalized state, the tree is read from disk for
246    /// every block processed, which would also require recomputing the root
247    /// even if it has not changed (note that the cached root is serialized with
248    /// the tree). This is particularly important since we decided to
249    /// instantiate the trees from the genesis block, for simplicity.
250    ///
251    /// We use a [`RwLock`](std::sync::RwLock) for this cache, because it is
252    /// only written once per tree update. Each tree has its own cached root, a
253    /// new lock is created for each clone.
254    cached_root: std::sync::RwLock<Option<Root>>,
255}
256
257impl NoteCommitmentTree {
258    /// Appends a note commitment to the leafmost layer of the tree.
259    ///
260    /// Returns an error if the tree is full.
261    #[allow(clippy::unwrap_in_result)]
262    pub fn append(&mut self, cm: NoteCommitment) -> Result<(), NoteCommitmentTreeError> {
263        if self.inner.append(cm.into()) {
264            // Invalidate cached root
265            let cached_root = self
266                .cached_root
267                .get_mut()
268                .expect("a thread that previously held exclusive lock access panicked");
269
270            *cached_root = None;
271
272            Ok(())
273        } else {
274            Err(NoteCommitmentTreeError::FullTree)
275        }
276    }
277
278    /// Returns the current root of the tree; used as an anchor in Sprout
279    /// shielded transactions.
280    pub fn root(&self) -> Root {
281        if let Some(root) = self.cached_root() {
282            // Return cached root.
283            return root;
284        }
285
286        // Get exclusive access, compute the root, and cache it.
287        let mut write_root = self
288            .cached_root
289            .write()
290            .expect("a thread that previously held exclusive lock access panicked");
291        let read_root = write_root.as_ref().cloned();
292        match read_root {
293            // Another thread got write access first, return cached root.
294            Some(root) => root,
295            None => {
296                // Compute root and cache it.
297                let root = self.recalculate_root();
298                *write_root = Some(root);
299                root
300            }
301        }
302    }
303
304    /// Returns the current root of the tree, if it has already been cached.
305    #[allow(clippy::unwrap_in_result)]
306    pub fn cached_root(&self) -> Option<Root> {
307        *self
308            .cached_root
309            .read()
310            .expect("a thread that previously held exclusive lock access panicked")
311    }
312
313    /// Calculates and returns the current root of the tree, ignoring any caching.
314    pub fn recalculate_root(&self) -> Root {
315        Root(self.inner.root().0)
316    }
317
318    /// Returns a hash of the Sprout note commitment tree root.
319    pub fn hash(&self) -> [u8; 32] {
320        self.root().into()
321    }
322
323    /// Returns an as-yet unused leaf node value of a Sprout note commitment tree.
324    ///
325    /// Uncommitted^Sprout = \[0\]^(l^[Sprout_Merkle]).
326    ///
327    /// [Sprout_Merkle]: https://zips.z.cash/protocol/protocol.pdf#constants
328    pub fn uncommitted() -> [u8; 32] {
329        [0; 32]
330    }
331
332    /// Counts the note commitments in the tree.
333    ///
334    /// For Sprout, the tree is [capped at 2^29 leaf nodes][spec].
335    ///
336    /// [spec]: https://zips.z.cash/protocol/protocol.pdf#merkletree
337    pub fn count(&self) -> u64 {
338        self.inner
339            .value()
340            .map_or(0, |x| u64::from(x.position()) + 1)
341    }
342
343    /// Checks if the tree roots and inner data structures of `self` and `other` are equal.
344    ///
345    /// # Panics
346    ///
347    /// If they aren't equal, with a message explaining the differences.
348    ///
349    /// Only for use in tests.
350    #[cfg(any(test, feature = "proptest-impl"))]
351    pub fn assert_frontier_eq(&self, other: &Self) {
352        // It's technically ok for the cached root not to be preserved,
353        // but it can result in expensive cryptographic operations,
354        // so we fail the tests if it happens.
355        assert_eq!(self.cached_root(), other.cached_root());
356
357        // Check the data in the internal data structure
358        assert_eq!(self.inner, other.inner);
359    }
360}
361
362impl Clone for NoteCommitmentTree {
363    /// Clones the inner tree, and creates a new `RwLock` with the cloned root data.
364    fn clone(&self) -> Self {
365        let cached_root = self.cached_root();
366
367        Self {
368            inner: self.inner.clone(),
369            cached_root: std::sync::RwLock::new(cached_root),
370        }
371    }
372}
373
374impl Default for NoteCommitmentTree {
375    fn default() -> Self {
376        Self {
377            inner: Frontier::empty(),
378            cached_root: Default::default(),
379        }
380    }
381}
382
383impl Eq for NoteCommitmentTree {}
384
385impl PartialEq for NoteCommitmentTree {
386    fn eq(&self, other: &Self) -> bool {
387        if let (Some(root), Some(other_root)) = (self.cached_root(), other.cached_root()) {
388            // Use cached roots if available
389            root == other_root
390        } else {
391            // Avoid expensive root recalculations which use multiple cryptographic hashes
392            self.inner == other.inner
393        }
394    }
395}
396
397impl From<Vec<NoteCommitment>> for NoteCommitmentTree {
398    /// Builds the tree from a vector of commitments at once.
399    fn from(values: Vec<NoteCommitment>) -> Self {
400        let mut tree = Self::default();
401
402        if values.is_empty() {
403            return tree;
404        }
405
406        for cm in values {
407            let _ = tree.append(cm);
408        }
409
410        tree
411    }
412}