Skip to content

Commit

Permalink
refactor: Parametrization of lib.rs Tests for Various Evaluation Engi…
Browse files Browse the repository at this point in the history
…nes (lurk-lang#234)

[microsoft/171](microsoft/Nova#171) introduced enhanced genericity in tests to facilitate unit testing across diverse curve cycles.  Since then, the lib.rs tests, being designed to verify certain behaviors of the decider SNARK, tend to embed the use of a RelaxedR1CSSnark<G, EE>. They do this under the alias (where the test carefully chooses a specific import path for RelaxedR1CSSnark):

```rust
type S1<G> = RelaxedR1CSSnark<G, ipa_pc::EvaluationEgine<G>>
```
Effectively, this constrains the PCS utilized by that decider to the IPA, even if the only thing the test function needed to be specific about is `RelaxedR1CSSnark`.

Hence, these generic tests then had to accommodate constraints facilitating the use of the IPA, specifically:

```
<G::CE as CommitmentEngineTrait<G>>::CommitmentKey: CommitmentKeyExtTrait<G>
```
This is because the IPA operates exclusively with a "splittable" key, a concept delineated by the CommitmentKeyExtTrait.

However, two core adjustments are required:

- For the inclusion of different PCSs (for instance, Zeromorph), it's imperative to render `EE<G>: EvaluationEngineTrait<G>` configurable.
- Parametrizing the tests based on the `EvaluationEngineTrait` implementation eliminates boundaries associated with `CommitmentKeyExtTrait`. Such boundaries matching the `EvaluationEngine` and `CommitmentEngine` only materialize when the scheme is instantiated (i.e., during test invocation), a pointwhere they are seamlessly met.

The present PR effects this parametrization, effectively making the lib.rs tests functions variable based on the evaluation engine in use.
  • Loading branch information
huitseeker authored Oct 17, 2023
1 parent 90a7952 commit 83982b3
Showing 1 changed file with 40 additions and 31 deletions.
71 changes: 40 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,14 +806,14 @@ type CE<G> = <G as Group>::CE;
#[cfg(test)]
mod tests {
use crate::provider::bn256_grumpkin::{bn256, grumpkin};
use crate::provider::pedersen::CommitmentKeyExtTrait;
use crate::provider::secp_secq::{secp256k1, secq256k1};
use crate::traits::evaluation::EvaluationEngineTrait;
use core::fmt::Write;

use super::*;
type EE<G> = provider::ipa_pc::EvaluationEngine<G>;
type S<G1> = spartan::snark::RelaxedR1CSSNARK<G1, EE<G1>>;
type SPrime<G1> = spartan::ppsnark::RelaxedR1CSSNARK<G1, EE<G1>>;
type S<G, EE> = spartan::snark::RelaxedR1CSSNARK<G, EE>;
type SPrime<G, EE> = spartan::ppsnark::RelaxedR1CSSNARK<G, EE>;

use ::bellpepper_core::{num::AllocatedNum, ConstraintSystem, SynthesisError};
use core::marker::PhantomData;
Expand Down Expand Up @@ -1086,13 +1086,12 @@ mod tests {
test_ivc_nontrivial_with::<secp256k1::Point, secq256k1::Point>();
}

fn test_ivc_nontrivial_with_compression_with<G1, G2>()
fn test_ivc_nontrivial_with_compression_with<G1, G2, E1, E2>()
where
G1: Group<Base = <G2 as Group>::Scalar>,
G2: Group<Base = <G1 as Group>::Scalar>,
// this is due to the reliance on CommitmentKeyExtTrait as a bound in ipa_pc
<G1::CE as CommitmentEngineTrait<G1>>::CommitmentKey: CommitmentKeyExtTrait<G1>,
<G2::CE as CommitmentEngineTrait<G2>>::CommitmentKey: CommitmentKeyExtTrait<G2>,
E1: EvaluationEngineTrait<G1>,
E2: EvaluationEngineTrait<G2>,
{
let circuit_primary = TrivialCircuit::default();
let circuit_secondary = CubicCircuit::default();
Expand Down Expand Up @@ -1153,10 +1152,11 @@ mod tests {
assert_eq!(zn_secondary, vec![<G2 as Group>::Scalar::from(2460515u64)]);

// produce the prover and verifier keys for compressed snark
let (pk, vk) = CompressedSNARK::<_, _, _, _, S<G1>, S<G2>>::setup(&pp).unwrap();
let (pk, vk) = CompressedSNARK::<_, _, _, _, S<G1, E1>, S<G2, E2>>::setup(&pp).unwrap();

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

Expand All @@ -1175,18 +1175,17 @@ mod tests {
type G1 = pasta_curves::pallas::Point;
type G2 = pasta_curves::vesta::Point;

test_ivc_nontrivial_with_compression_with::<G1, G2>();
test_ivc_nontrivial_with_compression_with::<bn256::Point, grumpkin::Point>();
test_ivc_nontrivial_with_compression_with::<secp256k1::Point, secq256k1::Point>();
test_ivc_nontrivial_with_compression_with::<G1, G2, EE<_>, EE<_>>();
test_ivc_nontrivial_with_compression_with::<bn256::Point, grumpkin::Point, EE<_>, EE<_>>();
test_ivc_nontrivial_with_compression_with::<secp256k1::Point, secq256k1::Point, EE<_>, EE<_>>();
}

fn test_ivc_nontrivial_with_spark_compression_with<G1, G2>()
fn test_ivc_nontrivial_with_spark_compression_with<G1, G2, E1, E2>()
where
G1: Group<Base = <G2 as Group>::Scalar>,
G2: Group<Base = <G1 as Group>::Scalar>,
// this is due to the reliance on CommitmentKeyExtTrait as a bound in ipa_pc
<G1::CE as CommitmentEngineTrait<G1>>::CommitmentKey: CommitmentKeyExtTrait<G1>,
<G2::CE as CommitmentEngineTrait<G2>>::CommitmentKey: CommitmentKeyExtTrait<G2>,
E1: EvaluationEngineTrait<G1>,
E2: EvaluationEngineTrait<G2>,
{
let circuit_primary = TrivialCircuit::default();
let circuit_secondary = CubicCircuit::default();
Expand Down Expand Up @@ -1249,11 +1248,15 @@ mod tests {
// run the compressed snark with Spark compiler

// produce the prover and verifier keys for compressed snark
let (pk, vk) = CompressedSNARK::<_, _, _, _, SPrime<G1>, SPrime<G2>>::setup(&pp).unwrap();
let (pk, vk) =
CompressedSNARK::<_, _, _, _, SPrime<G1, E1>, SPrime<G2, E2>>::setup(&pp).unwrap();

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

Expand All @@ -1272,18 +1275,23 @@ mod tests {
type G1 = pasta_curves::pallas::Point;
type G2 = pasta_curves::vesta::Point;

test_ivc_nontrivial_with_spark_compression_with::<G1, G2>();
test_ivc_nontrivial_with_spark_compression_with::<bn256::Point, grumpkin::Point>();
test_ivc_nontrivial_with_spark_compression_with::<secp256k1::Point, secq256k1::Point>();
test_ivc_nontrivial_with_spark_compression_with::<G1, G2, EE<_>, EE<_>>();
test_ivc_nontrivial_with_spark_compression_with::<bn256::Point, grumpkin::Point, EE<_>, EE<_>>(
);
test_ivc_nontrivial_with_spark_compression_with::<
secp256k1::Point,
secq256k1::Point,
EE<_>,
EE<_>,
>();
}

fn test_ivc_nondet_with_compression_with<G1, G2>()
fn test_ivc_nondet_with_compression_with<G1, G2, E1, E2>()
where
G1: Group<Base = <G2 as Group>::Scalar>,
G2: Group<Base = <G1 as Group>::Scalar>,
// this is due to the reliance on CommitmentKeyExtTrait as a bound in ipa_pc
<G1::CE as CommitmentEngineTrait<G1>>::CommitmentKey: CommitmentKeyExtTrait<G1>,
<G2::CE as CommitmentEngineTrait<G2>>::CommitmentKey: CommitmentKeyExtTrait<G2>,
E1: EvaluationEngineTrait<G1>,
E2: EvaluationEngineTrait<G2>,
{
// y is a non-deterministic advice representing the fifth root of the input at a step.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -1400,10 +1408,11 @@ mod tests {
assert!(res.is_ok());

// produce the prover and verifier keys for compressed snark
let (pk, vk) = CompressedSNARK::<_, _, _, _, S<G1>, S<G2>>::setup(&pp).unwrap();
let (pk, vk) = CompressedSNARK::<_, _, _, _, S<G1, E1>, S<G2, E2>>::setup(&pp).unwrap();

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

Expand All @@ -1417,9 +1426,9 @@ mod tests {
type G1 = pasta_curves::pallas::Point;
type G2 = pasta_curves::vesta::Point;

test_ivc_nondet_with_compression_with::<G1, G2>();
test_ivc_nondet_with_compression_with::<bn256::Point, grumpkin::Point>();
test_ivc_nondet_with_compression_with::<secp256k1::Point, secq256k1::Point>();
test_ivc_nondet_with_compression_with::<G1, G2, EE<_>, EE<_>>();
test_ivc_nondet_with_compression_with::<bn256::Point, grumpkin::Point, EE<_>, EE<_>>();
test_ivc_nondet_with_compression_with::<secp256k1::Point, secq256k1::Point, EE<_>, EE<_>>();
}

fn test_ivc_base_with<G1, G2>()
Expand Down

0 comments on commit 83982b3

Please sign in to comment.