Skip to content

Commit

Permalink
incrementalmerkletree proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
conradoplg committed Jul 9, 2021
1 parent fd4f4e1 commit 42a8d6f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions zebra-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ zcash_history = { git = "https://github.com/zcash/librustzcash.git", rev = "0c3e
zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "0c3ed159985affa774e44d10172d4471d798a85a" }
bigint = "4"
uint = "0.9.0"
incrementalmerkletree = "0.1.0"

proptest = { version = "0.10", optional = true }
proptest-derive = { version = "0.3.0", optional = true }
Expand Down
32 changes: 32 additions & 0 deletions zebra-chain/src/sapling/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,36 @@ impl From<Vec<jubjub::Fq>> for NoteCommitmentTree {
}
}

#[derive(Clone)]
struct Node {
inner: [u8; 32],
}

impl incrementalmerkletree::Hashable for Node {
fn empty_leaf() -> Self {
Self {
inner: jubjub::Fq::one().to_bytes(),
}
}

fn combine(level: incrementalmerkletree::Altitude, a: &Self, b: &Self) -> Self {
Self {
inner: merkle_crh_sapling(level.into(), a.inner, b.inner),
}
}

fn empty_root(level: incrementalmerkletree::Altitude) -> Self {
Self {
inner: EMPTY_ROOTS[usize::from(level)],
}
}
}

#[cfg(test)]
mod tests {

use hex::FromHex;
use incrementalmerkletree::Frontier;

use super::*;

Expand Down Expand Up @@ -348,6 +374,9 @@ mod tests {

let mut incremental_tree = NoteCommitmentTree::default();

let mut alt_tree =
incrementalmerkletree::bridgetree::Frontier::<Node, { MERKLE_DEPTH as u8 }>::new();

for (i, cm_u) in commitments.iter().enumerate() {
let bytes = <[u8; 32]>::from_hex(cm_u).unwrap();

Expand All @@ -357,7 +386,10 @@ mod tests {

let _ = incremental_tree.append(cm_u);

alt_tree.append(&Node { inner: cm_u.into() });

assert_eq!(hex::encode(incremental_tree.hash()), roots[i]);
assert_eq!(hex::encode(alt_tree.root().inner), roots[i]);

assert_eq!(
hex::encode((NoteCommitmentTree::from(leaves.clone())).hash()),
Expand Down

0 comments on commit 42a8d6f

Please sign in to comment.