Skip to content

Commit

Permalink
fix: use expected_test for ECC constraint counts (#217)
Browse files Browse the repository at this point in the history
Closes #196
  • Loading branch information
huitseeker authored Dec 22, 2023
1 parent 1032602 commit c3d81da
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions src/gadgets/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ mod tests {
},
traits::snark::default_ck_hint,
};
use expect_test::{expect, Expect};
use ff::{Field, PrimeFieldBits};
use pasta_curves::{arithmetic::CurveAffine, group::Curve, pallas, vesta};
use rand::rngs::OsRng;
Expand Down Expand Up @@ -1013,26 +1014,32 @@ mod tests {

#[test]
fn test_ecc_circuit_ops() {
test_ecc_circuit_ops_with::<PallasEngine, VestaEngine>(2704, 2692);
test_ecc_circuit_ops_with::<VestaEngine, PallasEngine>(2704, 2692);
test_ecc_circuit_ops_with::<PallasEngine, VestaEngine>(&expect!["2704"], &expect!["2692"]);
test_ecc_circuit_ops_with::<VestaEngine, PallasEngine>(&expect!["2704"], &expect!["2692"]);

test_ecc_circuit_ops_with::<Bn256Engine, GrumpkinEngine>(2738, 2724);
test_ecc_circuit_ops_with::<GrumpkinEngine, Bn256Engine>(2738, 2724);
test_ecc_circuit_ops_with::<Bn256Engine, GrumpkinEngine>(&expect!["2738"], &expect!["2724"]);
test_ecc_circuit_ops_with::<GrumpkinEngine, Bn256Engine>(&expect!["2738"], &expect!["2724"]);

test_ecc_circuit_ops_with::<Secp256k1Engine, Secq256k1Engine>(2670, 2660);
test_ecc_circuit_ops_with::<Secq256k1Engine, Secp256k1Engine>(2670, 2660);
test_ecc_circuit_ops_with::<Secp256k1Engine, Secq256k1Engine>(
&expect!["2670"],
&expect!["2660"],
);
test_ecc_circuit_ops_with::<Secq256k1Engine, Secp256k1Engine>(
&expect!["2670"],
&expect!["2660"],
);
}

fn test_ecc_circuit_ops_with<E1, E2>(expected_constraints: usize, expected_variables: usize)
fn test_ecc_circuit_ops_with<E1, E2>(expected_constraints: &Expect, expected_variables: &Expect)
where
E1: Engine<Base = <E2 as Engine>::Scalar>,
E2: Engine<Base = <E1 as Engine>::Scalar>,
{
// First create the shape
let mut cs: TestShapeCS<E2> = TestShapeCS::new();
let _ = synthesize_smul::<E1, _>(cs.namespace(|| "synthesize"));
assert_eq!(cs.num_constraints(), expected_constraints);
assert_eq!(cs.num_aux(), expected_variables);
expected_constraints.assert_eq(&cs.num_constraints().to_string());
expected_variables.assert_eq(&cs.num_aux().to_string());
let (shape, ck) = cs.r1cs_shape_and_key(&*default_ck_hint());

// Then the satisfying assignment
Expand Down Expand Up @@ -1130,28 +1137,40 @@ mod tests {

#[test]
fn test_ecc_circuit_add_negation() {
test_ecc_circuit_add_negation_with::<PallasEngine, VestaEngine>(39, 34);
test_ecc_circuit_add_negation_with::<VestaEngine, PallasEngine>(39, 34);
test_ecc_circuit_add_negation_with::<PallasEngine, VestaEngine>(&expect!["39"], &expect!["34"]);
test_ecc_circuit_add_negation_with::<VestaEngine, PallasEngine>(&expect!["39"], &expect!["34"]);

test_ecc_circuit_add_negation_with::<Bn256Engine, GrumpkinEngine>(39, 34);
test_ecc_circuit_add_negation_with::<GrumpkinEngine, Bn256Engine>(39, 34);
test_ecc_circuit_add_negation_with::<Bn256Engine, GrumpkinEngine>(
&expect!["39"],
&expect!["34"],
);
test_ecc_circuit_add_negation_with::<GrumpkinEngine, Bn256Engine>(
&expect!["39"],
&expect!["34"],
);

test_ecc_circuit_add_negation_with::<Secp256k1Engine, Secq256k1Engine>(39, 34);
test_ecc_circuit_add_negation_with::<Secq256k1Engine, Secp256k1Engine>(39, 34);
test_ecc_circuit_add_negation_with::<Secp256k1Engine, Secq256k1Engine>(
&expect!["39"],
&expect!["34"],
);
test_ecc_circuit_add_negation_with::<Secq256k1Engine, Secp256k1Engine>(
&expect!["39"],
&expect!["34"],
);
}

fn test_ecc_circuit_add_negation_with<E1, E2>(
expected_constraints: usize,
expected_variables: usize,
expected_constraints: &Expect,
expected_variables: &Expect,
) where
E1: Engine<Base = <E2 as Engine>::Scalar>,
E2: Engine<Base = <E1 as Engine>::Scalar>,
{
// First create the shape
let mut cs: TestShapeCS<E2> = TestShapeCS::new();
let _ = synthesize_add_negation::<E1, _>(cs.namespace(|| "synthesize add equal"));
assert_eq!(cs.num_constraints(), expected_constraints);
assert_eq!(cs.num_aux(), expected_variables);
expected_constraints.assert_eq(&cs.num_constraints().to_string());
expected_variables.assert_eq(&cs.num_aux().to_string());
let (shape, ck) = cs.r1cs_shape_and_key(&*default_ck_hint());

// Then the satisfying assignment
Expand Down

0 comments on commit c3d81da

Please sign in to comment.