Skip to content

Commit

Permalink
Calculate incremental note commitment trees (#2407)
Browse files Browse the repository at this point in the history
* Make sapling::tree::NoteCommitmentTree incrementally updateable

* Make orchard::tree::NoteCommitmentTree incrementally updateable

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* Changed to incrementalmerkletree implementation; update MerkleCRH^Orchard

* Improvements from review; organize file names (vectors.rs -> tree.rs)

Co-authored-by: Conrado Gouvea <conrado@zfnd.org>
Co-authored-by: teor <teor@riseup.net>
Co-authored-by: Conrado P. L. Gouvea <conradoplg@gmail.com>
  • Loading branch information
4 people authored Jul 15, 2021
1 parent 12c60b5 commit 48cf527
Show file tree
Hide file tree
Showing 10 changed files with 797 additions and 273 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 @@ -46,6 +46,7 @@ zcash_primitives = { git = "https://github.com/zcash/librustzcash.git", rev = "0
bigint = "4"
uint = "0.9.1"
bls12_381 = "0.5.0"
incrementalmerkletree = "0.1.0"

proptest = { version = "0.10", optional = true }
proptest-derive = { version = "0.3.0", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion zebra-chain/src/orchard/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod preallocate;
mod prop;
pub mod test_vectors;
mod test_vectors;
mod tree;
352 changes: 352 additions & 0 deletions zebra-chain/src/orchard/tests/test_vectors.rs

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions zebra-chain/src/orchard/tests/tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use halo2::arithmetic::FieldExt;
use halo2::pasta::pallas;

use crate::orchard::tests::test_vectors;
use crate::orchard::tree::*;

#[test]
fn empty_roots() {
zebra_test::init();

for i in 0..EMPTY_ROOTS.len() {
assert_eq!(
EMPTY_ROOTS[i].to_bytes(),
// The test vector is in reversed order.
test_vectors::EMPTY_ROOTS[MERKLE_DEPTH - i]
);
}
}

#[test]
fn incremental_roots() {
zebra_test::init();

let mut leaves = vec![];

let mut incremental_tree = NoteCommitmentTree::default();

for (i, commitment_set) in test_vectors::COMMITMENTS.iter().enumerate() {
for cm_x_bytes in commitment_set.iter() {
let cm_x = pallas::Base::from_bytes(&cm_x_bytes).unwrap();

leaves.push(cm_x);

let _ = incremental_tree.append(cm_x);
}

assert_eq!(
hex::encode(incremental_tree.hash()),
hex::encode(test_vectors::ROOTS[i].anchor)
);

assert_eq!(
hex::encode((NoteCommitmentTree::from(leaves.clone())).hash()),
hex::encode(test_vectors::ROOTS[i].anchor)
);
}
}
Loading

0 comments on commit 48cf527

Please sign in to comment.