Skip to content

Commit

Permalink
refactor: Refactor Supernova test suite and streamline functions
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 committed Mar 19, 2024
1 parent 09eb1eb commit 32424a2
Showing 1 changed file with 69 additions and 107 deletions.
176 changes: 69 additions & 107 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,62 +578,83 @@ mod test {
}
}

fn test_compression_with_circuit_size_difference_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,
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 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 secondary_circuit = TrivialSecondaryCircuit::default();
let test_circuits = circuits_factory(num_steps);

let (prover_key, verifier_key) = CompressedSNARK::<_, S1, S2>::setup(&pp).unwrap();
let pp = PublicParams::setup(&test_circuits[0], &*S1::ck_floor(), &*S2::ck_floor());

let compressed_snark = CompressedSNARK::prove(&pp, &prover_key, &recursive_snark).unwrap();
let z0_primary = vec![E1::Scalar::from(17u64)];
let z0_secondary = vec![<Dual<E1> as Engine>::Scalar::ZERO];

compressed_snark
.verify(&pp, &verifier_key, &z0_primary, &z0_secondary)
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() {
const NUM_STEPS: usize = 6;

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

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

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

#[test]
fn test_compression_with_circuit_size_difference() {
// 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<_>>();
// 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<_>>();
const NUM_STEPS: usize = 4;

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

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

0 comments on commit 32424a2

Please sign in to comment.