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