Skip to content

Commit

Permalink
Merge pull request #1 from MVPWorkshop/option
Browse files Browse the repository at this point in the history
changed codebase to work with Option instead of OnceCell
  • Loading branch information
milosdjurica authored Jan 15, 2025
2 parents 4a22c2c + fa79f97 commit 6fe83ec
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 157 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bincode = { version = "1.3", optional = true, default-features = false }
rayon = "1.10"
halo2curves = { version = "0.6.0", features = ["bits", "derive_serde"] }
# once_cell = { version = "1.18.0", default-features = false }
once_cell = "1.18.0"
# once_cell = "1.18.0"


[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
16 changes: 8 additions & 8 deletions benches/compressed-snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn bench_compressed_snark_internal<S1: RelaxedR1CSSNARKTrait<E1>, S2: RelaxedR1C
let c_secondary = TrivialCircuit::default();

// Produce public parameters
let pp = PublicParams::<E1, E2, C1, C2>::setup(
let mut pp = PublicParams::<E1, E2, C1, C2>::setup(
&c_primary,
&c_secondary,
&*S1::ck_floor(),
Expand All @@ -76,12 +76,12 @@ fn bench_compressed_snark_internal<S1: RelaxedR1CSSNARKTrait<E1>, S2: RelaxedR1C
.unwrap();

// Produce prover and verifier keys for CompressedSNARK
let (pk, vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&pp).unwrap();
let (pk, mut vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&mut pp).unwrap();

// produce a recursive SNARK
let num_steps = 3;
let mut recursive_snark: RecursiveSNARK<E1, E2, C1, C2> = RecursiveSNARK::new(
&pp,
&mut pp,
&c_primary,
&c_secondary,
&[<E1 as Engine>::Scalar::from(2u64)],
Expand All @@ -90,12 +90,12 @@ fn bench_compressed_snark_internal<S1: RelaxedR1CSSNARKTrait<E1>, S2: RelaxedR1C
.unwrap();

for i in 0..num_steps {
let res = recursive_snark.prove_step(&pp, &c_primary, &c_secondary);
let res = recursive_snark.prove_step(&mut pp, &c_primary, &c_secondary);
assert!(res.is_ok());

// verify the recursive snark at each step of recursion
let res = recursive_snark.verify(
&pp,
&mut pp,
i + 1,
&[<E1 as Engine>::Scalar::from(2u64)],
&[<E2 as Engine>::Scalar::from(2u64)],
Expand All @@ -107,14 +107,14 @@ fn bench_compressed_snark_internal<S1: RelaxedR1CSSNARKTrait<E1>, S2: RelaxedR1C
group.bench_function("Prove", |b| {
b.iter(|| {
assert!(CompressedSNARK::<_, _, _, _, S1, S2>::prove(
black_box(&pp),
black_box(&mut pp),
black_box(&pk),
black_box(&recursive_snark),
)
.is_ok());
})
});
let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &pk, &recursive_snark);
let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&mut pp, &pk, &recursive_snark);
assert!(res.is_ok());
let compressed_snark = res.unwrap();

Expand All @@ -123,7 +123,7 @@ fn bench_compressed_snark_internal<S1: RelaxedR1CSSNARKTrait<E1>, S2: RelaxedR1C
b.iter(|| {
assert!(black_box(&compressed_snark)
.verify(
black_box(&vk),
black_box(&mut vk),
black_box(num_steps),
black_box(&[<E1 as Engine>::Scalar::from(2u64)]),
black_box(&[<E2 as Engine>::Scalar::from(2u64)]),
Expand Down
4 changes: 2 additions & 2 deletions benches/ppsnark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn bench_ppsnark(c: &mut Criterion) {
let input = vec![<E as Engine>::Scalar::from(42)];

// produce keys
let (pk, vk) =
let (pk, mut vk) =
DirectSNARK::<E, S, NonTrivialCircuit<<E as Engine>::Scalar>>::setup(c.clone()).unwrap();

// Bench time to produce a ppSNARK;
Expand All @@ -75,7 +75,7 @@ fn bench_ppsnark(c: &mut Criterion) {
let ppsnark = DirectSNARK::prove(&pk, c.clone(), &input).unwrap();
group.bench_function("Verify", |b| {
b.iter(|| {
assert!(ppsnark.verify(black_box(&vk), black_box(&io),).is_ok());
assert!(ppsnark.verify(black_box(&mut vk), black_box(&io),).is_ok());
});
});
group.finish();
Expand Down
12 changes: 6 additions & 6 deletions benches/recursive-snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn bench_recursive_snark(c: &mut Criterion) {
let c_secondary = TrivialCircuit::default();

// Produce public parameters
let pp = PublicParams::<E1, E2, C1, C2>::setup(
let mut pp = PublicParams::<E1, E2, C1, C2>::setup(
&c_primary,
&c_secondary,
&*default_ck_hint(),
Expand All @@ -84,7 +84,7 @@ fn bench_recursive_snark(c: &mut Criterion) {
// a lot of zeros in the satisfying assignment
let num_warmup_steps = 10;
let mut recursive_snark: RecursiveSNARK<E1, E2, C1, C2> = RecursiveSNARK::new(
&pp,
&mut pp,
&c_primary,
&c_secondary,
&[<E1 as Engine>::Scalar::from(2u64)],
Expand All @@ -93,12 +93,12 @@ fn bench_recursive_snark(c: &mut Criterion) {
.unwrap();

for i in 0..num_warmup_steps {
let res = recursive_snark.prove_step(&pp, &c_primary, &c_secondary);
let res = recursive_snark.prove_step(&mut pp, &c_primary, &c_secondary);
assert!(res.is_ok());

// verify the recursive snark at each step of recursion
let res = recursive_snark.verify(
&pp,
&mut pp,
i + 1,
&[<E1 as Engine>::Scalar::from(2u64)],
&[<E2 as Engine>::Scalar::from(2u64)],
Expand All @@ -111,7 +111,7 @@ fn bench_recursive_snark(c: &mut Criterion) {
// produce a recursive SNARK for a step of the recursion
assert!(black_box(&mut recursive_snark.clone())
.prove_step(
black_box(&pp),
black_box(&mut pp),
black_box(&c_primary),
black_box(&c_secondary),
)
Expand All @@ -124,7 +124,7 @@ fn bench_recursive_snark(c: &mut Criterion) {
b.iter(|| {
assert!(black_box(&recursive_snark)
.verify(
black_box(&pp),
black_box(&mut pp),
black_box(num_warmup_steps),
black_box(&[<E1 as Engine>::Scalar::from(2u64)]),
black_box(&[<E2 as Engine>::Scalar::from(2u64)]),
Expand Down
6 changes: 3 additions & 3 deletions benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn bench_recursive_snark(c: &mut Criterion) {

// Produce public parameters
let ttc = TrivialCircuit::default();
let pp = PublicParams::<E1, E2, C1, C2>::setup(
let mut pp = PublicParams::<E1, E2, C1, C2>::setup(
&circuit_primary,
&ttc,
&*default_ck_hint(),
Expand All @@ -170,7 +170,7 @@ fn bench_recursive_snark(c: &mut Criterion) {
group.bench_function("Prove", |b| {
b.iter(|| {
let mut recursive_snark = RecursiveSNARK::new(
black_box(&pp),
black_box(&mut pp),
black_box(&circuit_primary),
black_box(&circuit_secondary),
black_box(&z0_primary),
Expand All @@ -181,7 +181,7 @@ fn bench_recursive_snark(c: &mut Criterion) {
// produce a recursive SNARK for a step of the recursion
assert!(recursive_snark
.prove_step(
black_box(&pp),
black_box(&mut pp),
black_box(&circuit_primary),
black_box(&circuit_secondary),
)
Expand Down
14 changes: 7 additions & 7 deletions examples/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn main() {
// produce public parameters
let start = Instant::now();
println!("Producing public parameters...");
let pp = PublicParams::<
let mut pp = PublicParams::<
E1,
E2,
AndCircuit<<E1 as Engine>::GE>,
Expand Down Expand Up @@ -264,7 +264,7 @@ fn main() {
println!("Generating a RecursiveSNARK...");
let mut recursive_snark: RecursiveSNARK<E1, E2, C1, C2> =
RecursiveSNARK::<E1, E2, C1, C2>::new(
&pp,
&mut pp,
&circuits[0],
&circuit_secondary,
&[<E1 as Engine>::Scalar::zero()],
Expand All @@ -274,7 +274,7 @@ fn main() {

let start = Instant::now();
for circuit_primary in circuits.iter() {
let res = recursive_snark.prove_step(&pp, circuit_primary, &circuit_secondary);
let res = recursive_snark.prove_step(&mut pp, circuit_primary, &circuit_secondary);
assert!(res.is_ok());
}
println!(
Expand All @@ -286,7 +286,7 @@ fn main() {
// verify the recursive SNARK
println!("Verifying a RecursiveSNARK...");
let res = recursive_snark.verify(
&pp,
&mut pp,
num_steps,
&[<E1 as Engine>::Scalar::ZERO],
&[<E2 as Engine>::Scalar::ZERO],
Expand All @@ -296,11 +296,11 @@ fn main() {

// produce a compressed SNARK
println!("Generating a CompressedSNARK using Spartan with HyperKZG...");
let (pk, vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&pp).unwrap();
let (pk, mut vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&mut pp).unwrap();

let start = Instant::now();

let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &pk, &recursive_snark);
let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&mut pp, &pk, &recursive_snark);
println!(
"CompressedSNARK::prove: {:?}, took {:?}",
res.is_ok(),
Expand All @@ -324,7 +324,7 @@ fn main() {
println!("Verifying a CompressedSNARK...");
let start = Instant::now();
let res = compressed_snark.verify(
&vk,
&mut vk,
num_steps,
&[<E1 as Engine>::Scalar::ZERO],
&[<E2 as Engine>::Scalar::ZERO],
Expand Down
14 changes: 7 additions & 7 deletions examples/hashchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn main() {
// produce public parameters
let start = Instant::now();
println!("Producing public parameters...");
let pp = PublicParams::<
let mut pp = PublicParams::<
E1,
E2,
HashChainCircuit<<E1 as Engine>::GE>,
Expand Down Expand Up @@ -159,7 +159,7 @@ fn main() {
);
let mut recursive_snark: RecursiveSNARK<E1, E2, C1, C2> =
RecursiveSNARK::<E1, E2, C1, C2>::new(
&pp,
&mut pp,
&circuits[0],
&circuit_secondary,
&[<E1 as Engine>::Scalar::zero()],
Expand All @@ -169,7 +169,7 @@ fn main() {

for (i, circuit_primary) in circuits.iter().enumerate() {
let start = Instant::now();
let res = recursive_snark.prove_step(&pp, circuit_primary, &circuit_secondary);
let res = recursive_snark.prove_step(&mut pp, circuit_primary, &circuit_secondary);
assert!(res.is_ok());

println!("RecursiveSNARK::prove {} : took {:?} ", i, start.elapsed());
Expand All @@ -178,7 +178,7 @@ fn main() {
// verify the recursive SNARK
println!("Verifying a RecursiveSNARK...");
let res = recursive_snark.verify(
&pp,
&mut pp,
num_steps,
&[<E1 as Engine>::Scalar::ZERO],
&[<E2 as Engine>::Scalar::ZERO],
Expand All @@ -188,11 +188,11 @@ fn main() {

// produce a compressed SNARK
println!("Generating a CompressedSNARK using Spartan with HyperKZG...");
let (pk, vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&pp).unwrap();
let (pk, mut vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&mut pp).unwrap();

let start = Instant::now();

let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &pk, &recursive_snark);
let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&mut pp, &pk, &recursive_snark);
println!(
"CompressedSNARK::prove: {:?}, took {:?}",
res.is_ok(),
Expand All @@ -216,7 +216,7 @@ fn main() {
println!("Verifying a CompressedSNARK...");
let start = Instant::now();
let res = compressed_snark.verify(
&vk,
&mut vk,
num_steps,
&[<E1 as Engine>::Scalar::ZERO],
&[<E2 as Engine>::Scalar::ZERO],
Expand Down
14 changes: 7 additions & 7 deletions examples/minroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn main() {
// produce public parameters
let start = Instant::now();
println!("Producing public parameters...");
let pp = PublicParams::<
let mut pp = PublicParams::<
E1,
E2,
MinRootCircuit<<E1 as Engine>::GE>,
Expand Down Expand Up @@ -224,7 +224,7 @@ fn main() {
println!("Generating a RecursiveSNARK...");
let mut recursive_snark: RecursiveSNARK<E1, E2, C1, C2> =
RecursiveSNARK::<E1, E2, C1, C2>::new(
&pp,
&mut pp,
&minroot_circuits[0],
&circuit_secondary,
&z0_primary,
Expand All @@ -234,7 +234,7 @@ fn main() {

for (i, circuit_primary) in minroot_circuits.iter().enumerate() {
let start = Instant::now();
let res = recursive_snark.prove_step(&pp, circuit_primary, &circuit_secondary);
let res = recursive_snark.prove_step(&mut pp, circuit_primary, &circuit_secondary);
assert!(res.is_ok());
println!(
"RecursiveSNARK::prove_step {}: {:?}, took {:?} ",
Expand All @@ -247,7 +247,7 @@ fn main() {
// verify the recursive SNARK
println!("Verifying a RecursiveSNARK...");
let start = Instant::now();
let res = recursive_snark.verify(&pp, num_steps, &z0_primary, &z0_secondary);
let res = recursive_snark.verify(&mut pp, num_steps, &z0_primary, &z0_secondary);
println!(
"RecursiveSNARK::verify: {:?}, took {:?}",
res.is_ok(),
Expand All @@ -257,11 +257,11 @@ fn main() {

// produce a compressed SNARK
println!("Generating a CompressedSNARK using Spartan with HyperKZG...");
let (pk, vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&pp).unwrap();
let (pk, mut vk) = CompressedSNARK::<_, _, _, _, S1, S2>::setup(&mut pp).unwrap();

let start = Instant::now();

let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&pp, &pk, &recursive_snark);
let res = CompressedSNARK::<_, _, _, _, S1, S2>::prove(&mut pp, &pk, &recursive_snark);
println!(
"CompressedSNARK::prove: {:?}, took {:?}",
res.is_ok(),
Expand All @@ -284,7 +284,7 @@ fn main() {
// verify the compressed SNARK
println!("Verifying a CompressedSNARK...");
let start = Instant::now();
let res = compressed_snark.verify(&vk, num_steps, &z0_primary, &z0_secondary);
let res = compressed_snark.verify(&mut vk, num_steps, &z0_primary, &z0_secondary);
println!(
"CompressedSNARK::verify: {:?}, took {:?}",
res.is_ok(),
Expand Down
Loading

0 comments on commit 6fe83ec

Please sign in to comment.