Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Batched Proof for multiple openings and Path Pruning #130

Merged
merged 39 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
aef2cd9
implemented multipath
intx4 Jan 15, 2024
9e54b60
reverted index logic in verify
intx4 Jan 15, 2024
bd8a30d
fixed compile errors
intx4 Jan 17, 2024
97deaf8
unittests passing
intx4 Jan 17, 2024
156d43c
changed signature of generate_multi_proof to sort indexes
intx4 Jan 17, 2024
4380907
cargo fmt
intx4 Jan 17, 2024
1bfb025
implemented unit test for multi_proof internals
intx4 Jan 17, 2024
561228a
renamed multi_proof specific test
intx4 Jan 17, 2024
6a24769
cargo fmt
intx4 Jan 17, 2024
804aa03
commnts in unittest
intx4 Jan 17, 2024
3766440
changed function signatures
intx4 Jan 17, 2024
ff2de46
cargo fmt
intx4 Jan 17, 2024
5548bd7
modified use of BtreeSet and HashMap from ark_std crate
intx4 Jan 18, 2024
d849e80
modified multipath to derivative(PartialEq...)
intx4 Jan 18, 2024
42a2f45
keep leaves as iterator in multipath verify
intx4 Jan 18, 2024
46a5377
auth_paths to peekable in multipath.verify
intx4 Jan 18, 2024
2eb01db
updated syntax for iterators in multipath.verify
intx4 Jan 18, 2024
7f5ac1d
updated syntax for iterators in multipath.verify
intx4 Jan 18, 2024
387e018
updated syntax for iterators in multipath.verify
intx4 Jan 18, 2024
decf749
shortend init for multipath
intx4 Jan 18, 2024
d08af2a
fixed build issues and implementing benches
intx4 Jan 18, 2024
e93e622
implemented benches for proof and multiproof
intx4 Jan 18, 2024
8ab9692
removed extra loop in decompress
intx4 Jan 18, 2024
0719d36
removed explicit decompress and merged decompression into multipath v…
intx4 Jan 18, 2024
e303028
removed multi_path.compress and merged compression step in generate_m…
intx4 Jan 19, 2024
3d6f9be
implemented prefix_decode_path
intx4 Jan 19, 2024
fe685f8
removed redundant code with new helper functions
intx4 Jan 19, 2024
aad2785
added doc for get_leaf_sibling_hash and made it pub. Renamed compute_…
intx4 Jan 19, 2024
76846e1
cargo fmt
intx4 Jan 22, 2024
13b2593
changed multiproof.verify to use "insert_with" instead of "insert" (t…
intx4 Jan 24, 2024
c4cd0af
cargo fmt
intx4 Jan 24, 2024
97e7d51
Merge branch 'main' into main
intx4 Feb 3, 2024
f036146
Merge remote-tracking branch 'upstream/main'
intx4 Feb 15, 2024
1537337
Use iterators in prefix_encode_path
Cesar199999 Mar 23, 2024
b400a40
Allow function-specific sample size in merkle tree benches
Cesar199999 Mar 23, 2024
2d28bcf
Merge pull request #1 from HungryCatsStudio/merkle-multiproofs-optimi…
intx4 Mar 25, 2024
6fdca79
removed redundant imports (nightly build tests)
intx4 Mar 25, 2024
98efb10
cargo fmt
intx4 Mar 25, 2024
d6059c6
nightly build fix
intx4 Mar 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 152 additions & 2 deletions crypto-primitives/benches/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod bytes_mt_benches {
use ark_std::{test_rng, UniformRand};
use criterion::Criterion;
use std::borrow::Borrow;
use std::iter::zip;

use crate::NUM_LEAVES;

Expand Down Expand Up @@ -56,11 +57,160 @@ mod bytes_mt_benches {
});
}

pub fn merkle_tree_generate_proof(c: &mut Criterion) {
let mut rng = test_rng();
let leaves: Vec<_> = (0..NUM_LEAVES)
.map(|_| {
let rnd = BigInteger256::rand(&mut rng);
to_uncompressed_bytes!(rnd).unwrap()
})
.collect();
let leaf_crh_params = <LeafH as CRHScheme>::setup(&mut rng).unwrap();
let two_to_one_params = <CompressH as TwoToOneCRHScheme>::setup(&mut rng)
.unwrap()
.clone();

let tree = Sha256MerkleTree::new(
&leaf_crh_params.clone(),
&two_to_one_params.clone(),
&leaves,
)
.unwrap();
c.bench_function("Merkle Tree Generate Proof (Leaves as [u8])", move |b| {
b.iter(|| {
for (i, _) in leaves.iter().enumerate() {
tree.generate_proof(i).unwrap();
}
})
});
}

pub fn merkle_tree_verify_proof(c: &mut Criterion) {
let mut rng = test_rng();
let leaves: Vec<_> = (0..NUM_LEAVES)
.map(|_| {
let rnd = BigInteger256::rand(&mut rng);
to_uncompressed_bytes!(rnd).unwrap()
})
.collect();
let leaf_crh_params = <LeafH as CRHScheme>::setup(&mut rng).unwrap();
let two_to_one_params = <CompressH as TwoToOneCRHScheme>::setup(&mut rng)
.unwrap()
.clone();

let tree = Sha256MerkleTree::new(
&leaf_crh_params.clone(),
&two_to_one_params.clone(),
&leaves,
)
.unwrap();

let root = tree.root();

let proofs: Vec<_> = leaves
.iter()
.enumerate()
.map(|(i, _)| tree.generate_proof(i).unwrap())
.collect();

c.bench_function("Merkle Tree Verify Proof (Leaves as [u8])", move |b| {
b.iter(|| {
for (proof, leaf) in zip(proofs.clone(), leaves.clone()) {
proof
.verify(&leaf_crh_params, &two_to_one_params, &root, leaf.as_slice())
.unwrap();
}
})
});
}

pub fn merkle_tree_generate_multi_proof(c: &mut Criterion) {
let mut rng = test_rng();
let leaves: Vec<_> = (0..NUM_LEAVES)
.map(|_| {
let rnd = BigInteger256::rand(&mut rng);
to_uncompressed_bytes!(rnd).unwrap()
})
.collect();
let leaf_crh_params = <LeafH as CRHScheme>::setup(&mut rng).unwrap();
let two_to_one_params = <CompressH as TwoToOneCRHScheme>::setup(&mut rng)
.unwrap()
.clone();

let tree = Sha256MerkleTree::new(
&leaf_crh_params.clone(),
&two_to_one_params.clone(),
&leaves,
)
.unwrap();
c.bench_function(
"Merkle Tree Generate Multi Proof (Leaves as [u8])",
move |b| {
b.iter(|| {
tree.generate_multi_proof((0..leaves.len()).collect::<Vec<_>>())
.unwrap();
})
},
);
}

pub fn merkle_tree_verify_multi_proof(c: &mut Criterion) {
let mut rng = test_rng();
let leaves: Vec<_> = (0..NUM_LEAVES)
.map(|_| {
let rnd = BigInteger256::rand(&mut rng);
to_uncompressed_bytes!(rnd).unwrap()
})
.collect();
let leaf_crh_params = <LeafH as CRHScheme>::setup(&mut rng).unwrap();
let two_to_one_params = <CompressH as TwoToOneCRHScheme>::setup(&mut rng)
.unwrap()
.clone();

let tree = Sha256MerkleTree::new(
&leaf_crh_params.clone(),
&two_to_one_params.clone(),
&leaves,
)
.unwrap();

let root = tree.root();

let multi_proof = tree
.generate_multi_proof((0..leaves.len()).collect::<Vec<_>>())
.unwrap();

c.bench_function(
"Merkle Tree Verify Multi Proof (Leaves as [u8])",
move |b| {
b.iter(|| {
multi_proof.verify(&leaf_crh_params, &two_to_one_params, &root, leaves.clone())
})
},
);
}

criterion_group! {
name = mt_create;
config = Criterion::default().sample_size(10);
config = Criterion::default().sample_size(100);
targets = merkle_tree_create
}

criterion_group! {
name = mt_proof;
config = Criterion::default().sample_size(100);
targets = merkle_tree_generate_proof, merkle_tree_generate_multi_proof
}

criterion_group! {
name = mt_verify;
config = Criterion::default().sample_size(10);
targets = merkle_tree_verify_proof, merkle_tree_verify_multi_proof
}
}

criterion_main!(crate::bytes_mt_benches::mt_create,);
criterion_main!(
bytes_mt_benches::mt_create,
bytes_mt_benches::mt_proof,
bytes_mt_benches::mt_verify
);
1 change: 0 additions & 1 deletion crypto-primitives/src/commitment/blake2s/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use ark_relations::r1cs::{Namespace, SynthesisError};
use crate::{
commitment::{blake2s, CommitmentGadget},
prf::blake2s::constraints::{evaluate_blake2s, OutputVar},
Vec,
};
use ark_ff::{Field, PrimeField};
use ark_r1cs_std::prelude::*;
Expand Down
1 change: 0 additions & 1 deletion crypto-primitives/src/commitment/pedersen/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
commitment::pedersen::{Commitment, Parameters, Randomness},
crh::pedersen::Window,
Vec,
};
use ark_ec::CurveGroup;
use ark_ff::{
Expand Down
2 changes: 1 addition & 1 deletion crypto-primitives/src/commitment/pedersen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{crh::CRHScheme, Error, Vec};
use crate::{crh::CRHScheme, Error};
use ark_ec::CurveGroup;
use ark_ff::{BitIteratorLE, Field, PrimeField, ToConstraintField};
use ark_serialize::CanonicalSerialize;
Expand Down
16 changes: 5 additions & 11 deletions crypto-primitives/src/crh/bowe_hopwood/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ use ark_ec::twisted_edwards::{Projective as TEProjective, TECurveConfig};
use ark_ec::CurveConfig;
use core::{borrow::Borrow, iter, marker::PhantomData};

use crate::{
crh::{
bowe_hopwood::{Parameters, CHUNK_SIZE},
pedersen::{self, Window},
CRHSchemeGadget, TwoToOneCRHSchemeGadget,
},
Vec,
use crate::crh::{
bowe_hopwood::{Parameters, CHUNK_SIZE},
pedersen::{self, Window},
CRHSchemeGadget, TwoToOneCRHSchemeGadget,
};
use ark_ff::Field;
use ark_r1cs_std::{
alloc::AllocVar, groups::curves::twisted_edwards::AffineVar, prelude::*, uint8::UInt8,
};
use ark_r1cs_std::{groups::curves::twisted_edwards::AffineVar, prelude::*};
use ark_relations::r1cs::{Namespace, SynthesisError};

use crate::crh::bowe_hopwood::{TwoToOneCRH, CRH};
use ark_r1cs_std::boolean::Boolean;

type ConstraintF<P> = <<P as CurveConfig>::BaseField as Field>::BasePrimeField;

Expand Down
2 changes: 1 addition & 1 deletion crypto-primitives/src/crh/bowe_hopwood/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! specific Twisted Edwards (TE) curves. See [Section 5.4.17 of the Zcash protocol specification](https://raw.githubusercontent.com/zcash/zips/master/protocol/protocol.pdf#concretepedersenhash) for a formal description of this hash function, specialized for the Jubjub curve.
//! The implementation in this repository is generic across choice of TE curves.

use crate::{Error, Vec};
use crate::Error;
use ark_std::rand::Rng;
use ark_std::{
fmt::{Debug, Formatter, Result as FmtResult},
Expand Down
4 changes: 1 addition & 3 deletions crypto-primitives/src/crh/injective_map/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use ark_ec::{
};
use ark_ff::fields::{Field, PrimeField};
use ark_r1cs_std::{
fields::fp::FpVar,
groups::{curves::twisted_edwards::AffineVar as TEVar, CurveVar},
prelude::*,
fields::fp::FpVar, groups::curves::twisted_edwards::AffineVar as TEVar, prelude::*,
};
use ark_relations::r1cs::SynthesisError;

Expand Down
1 change: 0 additions & 1 deletion crypto-primitives/src/crh/injective_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use ark_ec::{
};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::borrow::Borrow;
use ark_std::vec::Vec;
#[cfg(feature = "r1cs")]
pub mod constraints;

Expand Down
9 changes: 3 additions & 6 deletions crypto-primitives/src/crh/pedersen/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
crh::{
pedersen::{Parameters, Window},
CRHSchemeGadget as CRHGadgetTrait,
},
Vec,
use crate::crh::{
pedersen::{Parameters, Window},
CRHSchemeGadget as CRHGadgetTrait,
};
use ark_ec::CurveGroup;
use ark_ff::Field;
Expand Down
2 changes: 1 addition & 1 deletion crypto-primitives/src/crh/pedersen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Error, Vec};
use crate::Error;
use ark_std::rand::Rng;
use ark_std::{
fmt::{Debug, Formatter, Result as FmtResult},
Expand Down
2 changes: 1 addition & 1 deletion crypto-primitives/src/crh/poseidon/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::crh::poseidon::{TwoToOneCRH, CRH};
use crate::crh::CRHScheme;
use crate::crh::{
CRHSchemeGadget as CRHGadgetTrait, TwoToOneCRHSchemeGadget as TwoToOneCRHGadgetTrait,
};
use crate::sponge::constraints::CryptographicSpongeVar;
use crate::sponge::poseidon::constraints::PoseidonSpongeVar;
use crate::sponge::poseidon::PoseidonConfig;
use crate::{crh::CRHScheme, Vec};

use crate::sponge::Absorb;
use ark_ff::PrimeField;
Expand Down
12 changes: 2 additions & 10 deletions crypto-primitives/src/crh/sha256/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use ark_r1cs_std::{
R1CSVar,
};
use ark_relations::r1cs::{ConstraintSystemRef, Namespace, SynthesisError};
use ark_std::{vec, vec::Vec};

const STATE_LEN: usize = 8;

Expand Down Expand Up @@ -383,17 +382,10 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::crh::{
sha256::{digest::Digest, Sha256},
CRHScheme, CRHSchemeGadget, TwoToOneCRHScheme, TwoToOneCRHSchemeGadget,
};
use crate::crh::{sha256::digest::Digest, CRHScheme, TwoToOneCRHScheme};

use ark_bls12_377::Fr;
use ark_r1cs_std::R1CSVar;
use ark_relations::{
ns,
r1cs::{ConstraintSystem, Namespace},
};
use ark_relations::{ns, r1cs::ConstraintSystem};
use ark_std::rand::RngCore;

const TEST_LENGTHS: &[usize] = &[
Expand Down
2 changes: 1 addition & 1 deletion crypto-primitives/src/crh/sha256/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::crh::{CRHScheme, TwoToOneCRHScheme};
use crate::{Error, Vec};
use crate::Error;

use ark_std::rand::Rng;

Expand Down
2 changes: 1 addition & 1 deletion crypto-primitives/src/encryption/elgamal/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ark_ff::{
Zero,
};
use ark_serialize::CanonicalSerialize;
use ark_std::{borrow::Borrow, marker::PhantomData, vec::Vec};
use ark_std::{borrow::Borrow, marker::PhantomData};

pub type ConstraintF<C> = <<C as CurveGroup>::BaseField as Field>::BasePrimeField;

Expand Down
1 change: 0 additions & 1 deletion crypto-primitives/src/merkle_tree/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use ark_r1cs_std::prelude::*;
use ark_relations::r1cs::{Namespace, SynthesisError};
use ark_std::borrow::Borrow;
use ark_std::fmt::Debug;
use ark_std::vec::Vec;

pub trait DigestVarConverter<From, To: ?Sized> {
type TargetType: Borrow<To>;
Expand Down
Loading
Loading