From 667a7b841eefa71828c01e28267eab88bd85d000 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Thu, 16 Mar 2023 10:30:36 -0600 Subject: [PATCH] Move-only: restructure merkle tree source for extraction. --- zcash_primitives/src/merkle_tree.rs | 148 +++++++++++++--------------- 1 file changed, 71 insertions(+), 77 deletions(-) diff --git a/zcash_primitives/src/merkle_tree.rs b/zcash_primitives/src/merkle_tree.rs index f11a43c51..f831e2a56 100644 --- a/zcash_primitives/src/merkle_tree.rs +++ b/zcash_primitives/src/merkle_tree.rs @@ -150,33 +150,6 @@ impl CommitmentTree { } } -/// Reads a `CommitmentTree` from its serialized form. -pub fn read_commitment_tree( - mut reader: R, -) -> io::Result> { - let left = Optional::read(&mut reader, Node::read)?; - let right = Optional::read(&mut reader, Node::read)?; - let parents = Vector::read(&mut reader, |r| Optional::read(r, Node::read))?; - - Ok(CommitmentTree { - left, - right, - parents, - }) -} - -/// Serializes this tree as an array of bytes. -pub fn write_commitment_tree( - tree: &CommitmentTree, - mut writer: W, -) -> io::Result<()> { - Optional::write(&mut writer, tree.left.as_ref(), |w, n| n.write(w))?; - Optional::write(&mut writer, tree.right.as_ref(), |w, n| n.write(w))?; - Vector::write(&mut writer, &tree.parents, |w, e| { - Optional::write(w, e.as_ref(), |w, n| n.write(w)) - }) -} - impl CommitmentTree { /// Adds a leaf node to the tree. /// @@ -257,6 +230,33 @@ impl CommitmentTree { } } +/// Reads a `CommitmentTree` from its serialized form. +pub fn read_commitment_tree( + mut reader: R, +) -> io::Result> { + let left = Optional::read(&mut reader, Node::read)?; + let right = Optional::read(&mut reader, Node::read)?; + let parents = Vector::read(&mut reader, |r| Optional::read(r, Node::read))?; + + Ok(CommitmentTree { + left, + right, + parents, + }) +} + +/// Serializes this tree as an array of bytes. +pub fn write_commitment_tree( + tree: &CommitmentTree, + mut writer: W, +) -> io::Result<()> { + Optional::write(&mut writer, tree.left.as_ref(), |w, n| n.write(w))?; + Optional::write(&mut writer, tree.right.as_ref(), |w, n| n.write(w))?; + Vector::write(&mut writer, &tree.parents, |w, e| { + Optional::write(w, e.as_ref(), |w, n| n.write(w)) + }) +} + /// An updatable witness to a path from a position in a particular [`CommitmentTree`]. /// /// Appending the same commitments in the same order to both the original @@ -304,62 +304,12 @@ impl IncrementalWitness { cursor: None, } } -} -/// Reads an `IncrementalWitness` from its serialized form. -#[allow(clippy::redundant_closure)] -pub fn read_incremental_witness( - mut reader: R, -) -> io::Result> { - let tree = read_commitment_tree(&mut reader)?; - let filled = Vector::read(&mut reader, |r| Node::read(r))?; - let cursor = Optional::read(&mut reader, read_commitment_tree)?; - - let mut witness = IncrementalWitness { - tree, - filled, - cursor_depth: 0, - cursor, - }; - - witness.cursor_depth = witness.next_depth(); - - Ok(witness) -} - -/// Serializes this `IncrementalWitness` as an array of bytes. -pub fn write_incremental_witness( - witness: &IncrementalWitness, - mut writer: W, -) -> io::Result<()> { - write_commitment_tree(&witness.tree, &mut writer)?; - Vector::write(&mut writer, &witness.filled, |w, n| n.write(w))?; - Optional::write(&mut writer, witness.cursor.as_ref(), |w, t| { - write_commitment_tree(t, w) - }) -} - -impl IncrementalWitness { /// Returns the position of the witnessed leaf node in the commitment tree. pub fn position(&self) -> usize { self.tree.size() - 1 } -} -impl IncrementalWitness { - fn filler(&self) -> PathFiller { - let cursor_root = self - .cursor - .as_ref() - .map(|c| c.root_inner(self.cursor_depth, PathFiller::empty())); - - PathFiller { - queue: self.filled.iter().cloned().chain(cursor_root).collect(), - } - } -} - -impl IncrementalWitness { /// Finds the next "depth" of an unfilled subtree. fn next_depth(&self) -> u8 { let mut skip: u8 = self @@ -401,6 +351,17 @@ impl IncrementalWitness { } impl IncrementalWitness { + fn filler(&self) -> PathFiller { + let cursor_root = self + .cursor + .as_ref() + .map(|c| c.root_inner(self.cursor_depth, PathFiller::empty())); + + PathFiller { + queue: self.filled.iter().cloned().chain(cursor_root).collect(), + } + } + /// Tracks a leaf node that has been added to the underlying tree. /// /// Returns an error if the tree is full. @@ -482,6 +443,39 @@ impl IncrementalWitness { } } +/// Reads an `IncrementalWitness` from its serialized form. +#[allow(clippy::redundant_closure)] +pub fn read_incremental_witness( + mut reader: R, +) -> io::Result> { + let tree = read_commitment_tree(&mut reader)?; + let filled = Vector::read(&mut reader, |r| Node::read(r))?; + let cursor = Optional::read(&mut reader, read_commitment_tree)?; + + let mut witness = IncrementalWitness { + tree, + filled, + cursor_depth: 0, + cursor, + }; + + witness.cursor_depth = witness.next_depth(); + + Ok(witness) +} + +/// Serializes this `IncrementalWitness` as an array of bytes. +pub fn write_incremental_witness( + witness: &IncrementalWitness, + mut writer: W, +) -> io::Result<()> { + write_commitment_tree(&witness.tree, &mut writer)?; + Vector::write(&mut writer, &witness.filled, |w, n| n.write(w))?; + Optional::write(&mut writer, witness.cursor.as_ref(), |w, t| { + write_commitment_tree(t, w) + }) +} + /// A path from a position in a particular commitment tree to the root of that tree. #[derive(Clone, Debug, PartialEq, Eq)] pub struct MerklePath {