Skip to content

Commit

Permalink
test: Streamline tests (easy) (#318)
Browse files Browse the repository at this point in the history
* refactor: Refactor test usages for HyperKZG

Put the tests of the same scheme "together"

* refactor: Remove testing boilerplate

- Refactored tests for improved modularity and readability.
- Streamlined testing procedure by calling one function within another.
  • Loading branch information
huitseeker committed Feb 12, 2024
1 parent b40871a commit c1af06f
Showing 1 changed file with 36 additions and 86 deletions.
122 changes: 36 additions & 86 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,24 +1291,24 @@ mod tests {
test_ivc_nontrivial_with::<Secp256k1Engine>();
}

fn test_ivc_nontrivial_with_compression_with<E1, EE1, EE2>()
fn test_ivc_nontrivial_with_some_compression_with<E1, S1, S2>()
where
E1: CurveCycleEquipped,
EE1: EvaluationEngineTrait<E1>,
EE2: EvaluationEngineTrait<Dual<E1>>,
// this is due to the reliance on Abomonation
<E1::Scalar as PrimeField>::Repr: Abomonation,
<<Dual<E1> as Engine>::Scalar as PrimeField>::Repr: Abomonation,
S1: RelaxedR1CSSNARKTrait<E1>,
S2: RelaxedR1CSSNARKTrait<Dual<E1>>,
{
let circuit_primary = TrivialCircuit::default();
let circuit_secondary = CubicCircuit::default();

// produce public parameters
// produce public parameters, which we'll maybe use with a preprocessing compressed SNARK
let pp = PublicParams::<E1>::setup(
&circuit_primary,
&circuit_secondary,
&*default_ck_hint(),
&*default_ck_hint(),
&*S1::ck_floor(),
&*S2::ck_floor(),
);

let num_steps = 3;
Expand Down Expand Up @@ -1351,22 +1351,36 @@ mod tests {
vec![<Dual<E1> as Engine>::Scalar::from(2460515u64)]
);

// run the compressed snark
// produce the prover and verifier keys for compressed snark
let (pk, vk) = CompressedSNARK::<_, S<E1, EE1>, S<_, EE2>>::setup(&pp).unwrap();
let (pk, vk) = CompressedSNARK::<_, S1, S2>::setup(&pp).unwrap();

// produce a compressed SNARK
let res = CompressedSNARK::<_, S<E1, EE1>, S<_, EE2>>::prove(&pp, &pk, &recursive_snark);
let res = CompressedSNARK::<_, S1, S2>::prove(&pp, &pk, &recursive_snark);
assert!(res.is_ok());
let compressed_snark = res.unwrap();

// verify the compressed SNARK
let res = compressed_snark.verify(
&vk,
num_steps,
&[<E1 as Engine>::Scalar::ONE],
&[<Dual<E1> as Engine>::Scalar::ZERO],
);
assert!(res.is_ok());
compressed_snark
.verify(
&vk,
num_steps,
&[<E1 as Engine>::Scalar::ONE],
&[<Dual<E1> as Engine>::Scalar::ZERO],
)
.unwrap();
}

fn test_ivc_nontrivial_with_compression_with<E1, EE1, EE2>()
where
E1: CurveCycleEquipped,
EE1: EvaluationEngineTrait<E1>,
EE2: EvaluationEngineTrait<Dual<E1>>,
// this is due to the reliance on Abomonation
<E1::Scalar as PrimeField>::Repr: Abomonation,
<<Dual<E1> as Engine>::Scalar as PrimeField>::Repr: Abomonation,
{
test_ivc_nontrivial_with_some_compression_with::<E1, S<_, EE1>, S<_, EE2>>()
}

#[test]
Expand All @@ -1375,8 +1389,7 @@ mod tests {
test_ivc_nontrivial_with_compression_with::<Bn256Engine, EE<_>, EE<_>>();
test_ivc_nontrivial_with_compression_with::<Secp256k1Engine, EE<_>, EE<_>>();
test_ivc_nontrivial_with_compression_with::<Bn256EngineZM, ZMPCS<Bn256, _>, EE<_>>();

test_ivc_nontrivial_with_spark_compression_with::<
test_ivc_nontrivial_with_compression_with::<
Bn256EngineKZG,
provider::hyperkzg::EvaluationEngine<Bn256, _>,
EE<_>,
Expand All @@ -1392,75 +1405,7 @@ mod tests {
<E1::Scalar as PrimeField>::Repr: Abomonation,
<<Dual<E1> as Engine>::Scalar as PrimeField>::Repr: Abomonation,
{
let circuit_primary = TrivialCircuit::default();
let circuit_secondary = CubicCircuit::default();

// produce public parameters, which we'll use with a spark-compressed SNARK
let pp = PublicParams::<E1>::setup(
&circuit_primary,
&circuit_secondary,
&*SPrime::<E1, EE1>::ck_floor(),
&*SPrime::<_, EE2>::ck_floor(),
);

let num_steps = 3;

// produce a recursive SNARK
let mut recursive_snark = RecursiveSNARK::<E1>::new(
&pp,
&circuit_primary,
&circuit_secondary,
&[<E1 as Engine>::Scalar::ONE],
&[<Dual<E1> as Engine>::Scalar::ZERO],
)
.unwrap();

for _i in 0..num_steps {
let res = recursive_snark.prove_step(&pp, &circuit_primary, &circuit_secondary);
assert!(res.is_ok());
}

// verify the recursive SNARK
let res = recursive_snark.verify(
&pp,
num_steps,
&[<E1 as Engine>::Scalar::ONE],
&[<Dual<E1> as Engine>::Scalar::ZERO],
);
assert!(res.is_ok());

let (zn_primary, zn_secondary) = res.unwrap();

// sanity: check the claimed output with a direct computation of the same
assert_eq!(zn_primary, vec![<E1 as Engine>::Scalar::ONE]);
let mut zn_secondary_direct = vec![<Dual<E1> as Engine>::Scalar::ZERO];
for _i in 0..num_steps {
zn_secondary_direct = CubicCircuit::default().output(&zn_secondary_direct);
}
assert_eq!(zn_secondary, zn_secondary_direct);
assert_eq!(
zn_secondary,
vec![<Dual<E1> as Engine>::Scalar::from(2460515u64)]
);

// run the compressed snark with Spark compiler
// produce the prover and verifier keys for compressed snark
let (pk, vk) = CompressedSNARK::<_, SPrime<E1, EE1>, SPrime<_, EE2>>::setup(&pp).unwrap();

// produce a compressed SNARK
let res =
CompressedSNARK::<_, SPrime<E1, EE1>, SPrime<_, EE2>>::prove(&pp, &pk, &recursive_snark);
assert!(res.is_ok());
let compressed_snark = res.unwrap();

// verify the compressed SNARK
let res = compressed_snark.verify(
&vk,
num_steps,
&[<E1 as Engine>::Scalar::ONE],
&[<Dual<E1> as Engine>::Scalar::ZERO],
);
assert!(res.is_ok());
test_ivc_nontrivial_with_some_compression_with::<E1, SPrime<_, EE1>, SPrime<_, EE2>>()
}

#[test]
Expand All @@ -1469,6 +1414,11 @@ mod tests {
test_ivc_nontrivial_with_spark_compression_with::<Bn256Engine, EE<_>, EE<_>>();
test_ivc_nontrivial_with_spark_compression_with::<Secp256k1Engine, EE<_>, EE<_>>();
test_ivc_nontrivial_with_spark_compression_with::<Bn256EngineZM, ZMPCS<Bn256, _>, EE<_>>();
test_ivc_nontrivial_with_spark_compression_with::<
Bn256EngineKZG,
provider::hyperkzg::EvaluationEngine<Bn256, _>,
EE<_>,
>();
}

fn test_ivc_nondet_with_compression_with<E1, EE1, EE2>()
Expand Down

1 comment on commit c1af06f

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Table of Contents

Overview

This benchmark report shows the Arecibo GPU benchmarks.
NVIDIA L4
Intel(R) Xeon(R) CPU @ 2.20GHz
32 vCPUs
125 GB RAM
Workflow run: https://github.com/lurk-lab/arecibo/actions/runs/7875212965

Benchmark Results

RecursiveSNARK-NIVC-2

ref=b40871a ref=c1af06f
Prove-NumCons-6540 52.82 ms (✅ 1.00x) 52.89 ms (✅ 1.00x slower)
Verify-NumCons-6540 33.00 ms (✅ 1.00x) 33.02 ms (✅ 1.00x slower)
Prove-NumCons-1028888 342.44 ms (✅ 1.00x) 345.61 ms (✅ 1.01x slower)
Verify-NumCons-1028888 253.17 ms (✅ 1.00x) 254.89 ms (✅ 1.01x slower)

CompressedSNARK-NIVC-Commitments-2

ref=b40871a ref=c1af06f
Prove-NumCons-6540 13.87 s (✅ 1.00x) 13.92 s (✅ 1.00x slower)
Verify-NumCons-6540 78.64 ms (✅ 1.00x) 78.12 ms (✅ 1.01x faster)
Prove-NumCons-1028888 109.93 s (✅ 1.00x) 110.56 s (✅ 1.01x slower)
Verify-NumCons-1028888 769.88 ms (✅ 1.00x) 770.55 ms (✅ 1.00x slower)

Made with criterion-table

Please sign in to comment.