Skip to content

Commit

Permalink
refactor: Refactor Supernova test suite and streamline functions (#372)
Browse files Browse the repository at this point in the history
- Refactored test functions within the supernova snark module to reduce redundancy.
- Consolidated `test_compression_with` and `test_compression_with_circuit_size_difference_with` functions into a single function.
- Introduced a 'circuit factory' function to generate test circuits.
- Removed `test_nivc_trivial_with_compression_with` method, unified under `test_compression_with`.
- Adjusted individual test cases to conform to new structured changes.
  • Loading branch information
huitseeker authored Mar 22, 2024
1 parent 09eb1eb commit 5f615d7
Showing 1 changed file with 31 additions and 70 deletions.
101 changes: 31 additions & 70 deletions src/supernova/snark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,65 +458,6 @@ mod test {
}
}

fn test_nivc_trivial_with_compression_with<E1, S1, S2>()
where
E1: CurveCycleEquipped,
S1: BatchedRelaxedR1CSSNARKTrait<E1>,
S2: RelaxedR1CSSNARKTrait<Dual<E1>>,
<E1::Scalar as PrimeField>::Repr: Abomonation,
<<Dual<E1> as Engine>::Scalar as PrimeField>::Repr: Abomonation,
{
const NUM_STEPS: usize = 6;

let secondary_circuit = TrivialSecondaryCircuit::default();
let test_circuits = TestCircuit::new(NUM_STEPS);

let pp = PublicParams::setup(&test_circuits[0], &*S1::ck_floor(), &*S2::ck_floor());

let z0_primary = vec![E1::Scalar::from(17u64)];
let z0_secondary = vec![<Dual<E1> as Engine>::Scalar::ZERO];

let mut recursive_snark = RecursiveSNARK::new(
&pp,
&test_circuits[0],
&test_circuits[0],
&secondary_circuit,
&z0_primary,
&z0_secondary,
)
.unwrap();

for circuit in test_circuits.iter().take(NUM_STEPS) {
recursive_snark
.prove_step(&pp, circuit, &secondary_circuit)
.unwrap();

recursive_snark
.verify(&pp, &z0_primary, &z0_secondary)
.unwrap();
}

let (prover_key, verifier_key) = CompressedSNARK::<_, S1, S2>::setup(&pp).unwrap();

let compressed_snark = CompressedSNARK::prove(&pp, &prover_key, &recursive_snark).unwrap();

compressed_snark
.verify(&pp, &verifier_key, &z0_primary, &z0_secondary)
.unwrap();
}

#[test]
fn test_nivc_trivial_with_compression() {
// ppSNARK
test_nivc_trivial_with_compression_with::<PallasEngine, S1PP<_>, S2<_>>();
test_nivc_trivial_with_compression_with::<Bn256EngineIPA, S1PP<_>, S2<_>>();
test_nivc_trivial_with_compression_with::<Secp256k1Engine, S1PP<_>, S2<_>>();
// classic SNARK
test_nivc_trivial_with_compression_with::<PallasEngine, S1<_>, S2<_>>();
test_nivc_trivial_with_compression_with::<Bn256EngineIPA, S1<_>, S2<_>>();
test_nivc_trivial_with_compression_with::<Secp256k1Engine, S1<_>, S2<_>>();
}

#[derive(Clone)]
struct BigPowerCircuit<E> {
_p: PhantomData<E>,
Expand Down Expand Up @@ -637,18 +578,19 @@ mod test {
}
}

fn test_compression_with_circuit_size_difference_with<E1, S1, S2>()
fn test_compression_with<E1, S1, S2, F, C>(num_steps: usize, circuits_factory: F)
where
E1: CurveCycleEquipped,
S1: BatchedRelaxedR1CSSNARKTrait<E1>,
S2: RelaxedR1CSSNARKTrait<Dual<E1>>,
<E1::Scalar as PrimeField>::Repr: Abomonation,
<<Dual<E1> as Engine>::Scalar as PrimeField>::Repr: Abomonation,
C: NonUniformCircuit<E1, C1 = C, C2 = TrivialSecondaryCircuit<<Dual<E1> as Engine>::Scalar>>
+ StepCircuit<E1::Scalar>,
F: Fn(usize) -> Vec<C>,
{
const NUM_STEPS: usize = 4;

let secondary_circuit = TrivialSecondaryCircuit::default();
let test_circuits = BigTestCircuit::new(NUM_STEPS);
let test_circuits = circuits_factory(num_steps);

let pp = PublicParams::setup(&test_circuits[0], &*S1::ck_floor(), &*S2::ck_floor());

Expand All @@ -665,7 +607,7 @@ mod test {
)
.unwrap();

for circuit in test_circuits.iter().take(NUM_STEPS) {
for circuit in test_circuits.iter().take(num_steps) {
recursive_snark
.prove_step(&pp, circuit, &secondary_circuit)
.unwrap();
Expand All @@ -684,15 +626,34 @@ mod test {
.unwrap();
}

#[test]
fn test_nivc_trivial_with_compression() {
const NUM_STEPS: usize = 6;

// ppSNARK
test_compression_with::<PallasEngine, S1PP<_>, S2<_>, _, _>(NUM_STEPS, TestCircuit::new);

test_compression_with::<Bn256EngineIPA, S1PP<_>, S2<_>, _, _>(NUM_STEPS, TestCircuit::new);
test_compression_with::<Secp256k1Engine, S1PP<_>, S2<_>, _, _>(NUM_STEPS, TestCircuit::new);

// classic SNARK
test_compression_with::<PallasEngine, S1<_>, S2<_>, _, _>(NUM_STEPS, TestCircuit::new);
test_compression_with::<Bn256EngineIPA, S1<_>, S2<_>, _, _>(NUM_STEPS, TestCircuit::new);
test_compression_with::<Secp256k1Engine, S1<_>, S2<_>, _, _>(NUM_STEPS, TestCircuit::new);
}

#[test]
fn test_compression_with_circuit_size_difference() {
const NUM_STEPS: usize = 4;

// ppSNARK
test_compression_with_circuit_size_difference_with::<PallasEngine, S1PP<_>, S2<_>>();
test_compression_with_circuit_size_difference_with::<Bn256EngineIPA, S1PP<_>, S2<_>>();
test_compression_with_circuit_size_difference_with::<Secp256k1Engine, S1PP<_>, S2<_>>();
test_compression_with::<PallasEngine, S1PP<_>, S2<_>, _, _>(NUM_STEPS, BigTestCircuit::new);
test_compression_with::<Bn256EngineIPA, S1PP<_>, S2<_>, _, _>(NUM_STEPS, BigTestCircuit::new);
test_compression_with::<Secp256k1Engine, S1PP<_>, S2<_>, _, _>(NUM_STEPS, BigTestCircuit::new);

// classic SNARK
test_compression_with_circuit_size_difference_with::<PallasEngine, S1<_>, S2<_>>();
test_compression_with_circuit_size_difference_with::<Bn256EngineIPA, S1<_>, S2<_>>();
test_compression_with_circuit_size_difference_with::<Secp256k1Engine, S1<_>, S2<_>>();
test_compression_with::<PallasEngine, S1<_>, S2<_>, _, _>(NUM_STEPS, BigTestCircuit::new);
test_compression_with::<Bn256EngineIPA, S1<_>, S2<_>, _, _>(NUM_STEPS, BigTestCircuit::new);
test_compression_with::<Secp256k1Engine, S1<_>, S2<_>, _, _>(NUM_STEPS, BigTestCircuit::new);
}
}

1 comment on commit 5f615d7

@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/8383762633

Benchmark Results

RecursiveSNARK-NIVC-2

ref=09eb1eb ref=5f615d7
Prove-NumCons-6540 47.13 ms (✅ 1.00x) 47.07 ms (✅ 1.00x faster)
Verify-NumCons-6540 35.46 ms (✅ 1.00x) 35.10 ms (✅ 1.01x faster)
Prove-NumCons-1028888 344.22 ms (✅ 1.00x) 345.82 ms (✅ 1.00x slower)
Verify-NumCons-1028888 253.91 ms (✅ 1.00x) 257.07 ms (✅ 1.01x slower)

CompressedSNARK-NIVC-Commitments-2

ref=09eb1eb ref=5f615d7
Prove-NumCons-6540 13.53 s (✅ 1.00x) 13.52 s (✅ 1.00x faster)
Verify-NumCons-6540 62.15 ms (✅ 1.00x) 61.98 ms (✅ 1.00x faster)
Prove-NumCons-1028888 58.01 s (✅ 1.00x) 58.57 s (✅ 1.01x slower)
Verify-NumCons-1028888 61.79 ms (✅ 1.00x) 61.97 ms (✅ 1.00x slower)

Made with criterion-table

Please sign in to comment.