Skip to content

Commit

Permalink
Change test setup to compile with circuit
Browse files Browse the repository at this point in the history
With the new setup one can specify the circuit for the compililation
  • Loading branch information
moCello committed Mar 13, 2023
1 parent e676741 commit 0b0888b
Show file tree
Hide file tree
Showing 9 changed files with 520 additions and 436 deletions.
284 changes: 56 additions & 228 deletions tests/append_gate.rs

Large diffs are not rendered by default.

405 changes: 405 additions & 0 deletions tests/append_gate_bak.rs

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions tests/assert_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ fn assert_equal_point() {
let label = b"assert_equal_point";
let rng = &mut StdRng::seed_from_u64(0xdecaf);
let capacity = 1 << 4;
let (prover, verifier) = setup(capacity, rng, label);
let (prover, verifier) =
setup(capacity, rng, label, &TestCircuit::default());

// Test default works:
// GENERATOR = GENERATOR
Expand Down Expand Up @@ -139,7 +140,8 @@ fn assert_equal_public_point() {
let label = b"assert_equal_public_point";
let rng = &mut StdRng::seed_from_u64(0xfeed);
let capacity = 1 << 4;
let (prover, verifier) = setup(capacity, rng, label);
let (prover, verifier) =
setup(capacity, rng, label, &TestCircuit::default());

// Test default works:
// GENERATOR = GENERATOR
Expand Down
231 changes: 35 additions & 196 deletions tests/assert_bls.rs → tests/assert_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ fn assert_equal() {
let label = b"assert_equal_constant_without_pi";
let rng = &mut StdRng::seed_from_u64(0xc1adde);
let capacity = 1 << 4;
let (prover, verifier) = setup(capacity, rng, label);
let (prover, verifier) =
setup(capacity, rng, label, &TestCircuit::default());

// public input to be used by all tests
let pi = vec![];
Expand Down Expand Up @@ -100,7 +101,8 @@ fn assert_equal() {
}

#[test]
fn assert_equal_constant_without_pi() {
fn assert_equal_constant() {
#[derive(Default)]
pub struct TestCircuit {
scalar: BlsScalar,
constant: BlsScalar,
Expand All @@ -121,16 +123,6 @@ fn assert_equal_constant_without_pi() {
}
}

impl Default for TestCircuit {
fn default() -> Self {
Self {
scalar: BlsScalar::zero(),
constant: BlsScalar::zero(),
public: None,
}
}
}

impl Circuit for TestCircuit {
fn circuit<C>(&self, composer: &mut C) -> Result<(), Error>
where
Expand All @@ -148,12 +140,14 @@ fn assert_equal_constant_without_pi() {
}
}

// Compile common circuit descriptions for the prover and verifier to be
// used by all tests
let label = b"assert_equal_constant_without_pi";
// Test: public = None, constant = zero
//
// Compile common circuit descriptions for the prover and verifier
let label = b"assert_equal_constant";
let rng = &mut StdRng::seed_from_u64(0xfa11);
let capacity = 1 << 4;
let (prover, verifier) = setup(capacity, rng, label);
let (prover, verifier) =
setup(capacity, rng, label, &TestCircuit::default());

// Test default works:
// 0 = 0 + None
Expand All @@ -179,68 +173,19 @@ fn assert_equal_constant_without_pi() {
let public = None;
let circuit = TestCircuit::new(scalar, constant, public);
check_unsatisfied_circuit(&prover, &circuit, rng, &msg);
}

#[test]
fn assert_equal_constant_with_pi() {
pub struct TestCircuit {
scalar: BlsScalar,
constant: BlsScalar,
public: Option<BlsScalar>,
}

impl TestCircuit {
pub fn new(
scalar: BlsScalar,
constant: BlsScalar,
public: Option<BlsScalar>,
) -> Self {
Self {
scalar,
constant,
public,
}
}
}

impl Default for TestCircuit {
fn default() -> Self {
Self {
scalar: BlsScalar::zero(),
constant: BlsScalar::zero(),
public: Some(BlsScalar::zero()),
}
}
}

impl Circuit for TestCircuit {
fn circuit<C>(&self, composer: &mut C) -> Result<(), Error>
where
C: Composer,
{
let w_scalar = composer.append_witness(self.scalar);

composer.assert_equal_constant(
w_scalar,
self.constant,
self.public,
);

Ok(())
}
}

// Compile common circuit descriptions for the prover and verifier to be
// used by all tests
let label = b"assert_equal_constant_with_pi";
let rng = &mut StdRng::seed_from_u64(0xfa11);
let capacity = 1 << 4;
let (prover, verifier) = setup(capacity, rng, label);
// Test: public = Some(_), constant = zero
//
// Compile new circuit descriptions for the prover and verifier
let scalar = BlsScalar::zero();
let constant = BlsScalar::zero();
let public = Some(BlsScalar::zero());
let circuit = TestCircuit::new(scalar, constant, public);
let (prover, verifier) = setup(capacity, rng, label, &circuit);

// Test default works:
// 0 = 0 + 0
let msg = "Default circuit verification should pass";
let circuit = TestCircuit::default();
let pi = vec![BlsScalar::zero()];
check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg);

Expand Down Expand Up @@ -271,77 +216,24 @@ fn assert_equal_constant_with_pi() {
let public = Some(BlsScalar::zero());
let circuit = TestCircuit::new(scalar, constant, public);
check_unsatisfied_circuit(&prover, &circuit, rng, &msg);
}

#[test]
fn assert_equal_random_constant_without_pi() {
const CONSTANT: BlsScalar = BlsScalar::from_raw([0x42, 0x42, 0x42, 0x42]);

pub struct TestCircuit {
scalar: BlsScalar,
constant: BlsScalar,
public: Option<BlsScalar>,
}

impl TestCircuit {
pub fn new(
scalar: BlsScalar,
constant: BlsScalar,
public: Option<BlsScalar>,
) -> Self {
Self {
scalar,
constant,
public,
}
}
}

impl Default for TestCircuit {
fn default() -> Self {
Self {
scalar: CONSTANT,
constant: CONSTANT,
public: None,
}
}
}

impl Circuit for TestCircuit {
fn circuit<C>(&self, composer: &mut C) -> Result<(), Error>
where
C: Composer,
{
let w_scalar = composer.append_witness(self.scalar);

composer.assert_equal_constant(
w_scalar,
self.constant,
self.public,
);

Ok(())
}
}

// Compile common circuit descriptions for the prover and verifier to be
// used by all tests
let label = b"assert_equal_random_constant_without_pi";
let rng = &mut StdRng::seed_from_u64(0xfa11);
let capacity = 1 << 4;
let (prover, verifier) = setup(capacity, rng, label);
// Test: public = None, constant = random
//
// Compile new circuit descriptions for the prover and verifier
let constant = BlsScalar::random(rng);
let scalar = constant.clone();
let public = None;
let circuit = TestCircuit::new(scalar, constant, public);
let (prover, verifier) = setup(capacity, rng, label, &circuit);

// Test default works:
// x = x + None
let msg = "Default circuit verification should pass";
let circuit = TestCircuit::default();
let pi = vec![];
check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg);

// Test public input doesn't match
let msg = "Satisfied circuit should not verify because pi length is not the same as in circuit description";
let scalar = CONSTANT;
let constant = CONSTANT;
let public_value = BlsScalar::zero();
let public = Some(public_value);
let pi = vec![public_value];
Expand All @@ -355,78 +247,26 @@ fn assert_equal_random_constant_without_pi() {
let public = None;
let circuit = TestCircuit::new(scalar, constant, public);
check_unsatisfied_circuit(&prover, &circuit, rng, &msg);
}

#[test]
fn assert_equal_random_constant_with_pi() {
const CONSTANT: BlsScalar = BlsScalar::from_raw([0x42, 0x42, 0x42, 0x42]);

pub struct TestCircuit {
scalar: BlsScalar,
constant: BlsScalar,
public: Option<BlsScalar>,
}

impl TestCircuit {
pub fn new(
scalar: BlsScalar,
constant: BlsScalar,
public: Option<BlsScalar>,
) -> Self {
Self {
scalar,
constant,
public,
}
}
}

impl Default for TestCircuit {
fn default() -> Self {
Self {
scalar: CONSTANT,
constant: CONSTANT,
public: Some(BlsScalar::zero()),
}
}
}

impl Circuit for TestCircuit {
fn circuit<C>(&self, composer: &mut C) -> Result<(), Error>
where
C: Composer,
{
let w_scalar = composer.append_witness(self.scalar);

composer.assert_equal_constant(
w_scalar,
self.constant,
self.public,
);

Ok(())
}
}

// Compile common circuit descriptions for the prover and verifier to be
// used by all tests
let label = b"assert_equal_random_constant_with_pi";
let rng = &mut StdRng::seed_from_u64(0xfa11);
let capacity = 1 << 4;
let (prover, verifier) = setup(capacity, rng, label);
// Test: public = Some(_), constant = random
//
// Compile new circuit descriptions for the prover and verifier
let constant = BlsScalar::random(rng);
let scalar = constant.clone();
let public = Some(BlsScalar::zero());
let circuit = TestCircuit::new(scalar, constant, public);
let (prover, verifier) = setup(capacity, rng, label, &circuit);

// Test default works:
// 0 = 0 + 0
let msg = "Default circuit verification should pass";
let circuit = TestCircuit::default();
let pi = vec![BlsScalar::zero()];
check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg);

// Test:
// witness = constant + pi
let msg = "Satisfied circuit should verify";
let scalar = BlsScalar::random(rng);
let constant = CONSTANT;
let public_value = scalar - constant;
let public = Some(public_value);
let pi = vec![public_value];
Expand All @@ -435,8 +275,7 @@ fn assert_equal_random_constant_with_pi() {

// Test public input doesn't match
let msg = "Satisfied circuit should not verify because pi length is not the same as in circuit description";
let scalar = CONSTANT;
let constant = CONSTANT;
let scalar = constant.clone();
let public = None;
let pi = vec![];
let circuit = TestCircuit::new(scalar, constant, public);
Expand Down
3 changes: 2 additions & 1 deletion tests/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ fn component_boolean() {
let label = b"component_boolean";
let rng = &mut StdRng::seed_from_u64(0xfade);
let capacity = 1 << 4;
let (prover, verifier) = setup(capacity, rng, label);
let (prover, verifier) =
setup(capacity, rng, label, &TestCircuit::default());

// public inputs to be used by all tests
let pi = vec![];
Expand Down
3 changes: 2 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ pub(crate) fn setup<C, R>(
capacity: usize,
rng: &mut R,
label: &[u8],
circuit: &C,
) -> (Prover<C>, Verifier<C>)
where
C: Circuit,
R: RngCore + CryptoRng,
{
let pp = PublicParameters::setup(capacity, rng)
.expect("Creation of public parameter shouldn't fail");
Compiler::compile::<C>(&pp, label).expect("It should be possible to create the prover and verifier circuit descriptions")
Compiler::compile_with_circuit(&pp, label, circuit).expect("It should be possible to create the prover and verifier circuit descriptions")
}

// Check that proof creation and verification of a satisfied circuit passes
Expand Down
9 changes: 6 additions & 3 deletions tests/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ fn component_add_point() {
let label = b"component_add_point";
let rng = &mut StdRng::seed_from_u64(0xcafe);
let capacity = 1 << 4;
let (prover, verifier) = setup(capacity, rng, label);
let (prover, verifier) =
setup(capacity, rng, label, &TestCircuit::default());

// Test default works:
let msg = "Default circuit verification should pass";
Expand Down Expand Up @@ -158,7 +159,8 @@ fn component_mul_generator() {
let label = b"component_mul_generator";
let rng = &mut StdRng::seed_from_u64(0xbead);
let capacity = 1 << 9;
let (prover, verifier) = setup(capacity, rng, label);
let (prover, verifier) =
setup(capacity, rng, label, &TestCircuit::default());

// generator point and pi are the same for all tests
let generator = dusk_jubjub::GENERATOR_EXTENDED;
Expand Down Expand Up @@ -261,7 +263,8 @@ fn component_mul_point() {
let label = b"component_mul_point";
let rng = &mut StdRng::seed_from_u64(0xdeed);
let capacity = 1 << 11;
let (prover, verifier) = setup(capacity, rng, label);
let (prover, verifier) =
setup(capacity, rng, label, &TestCircuit::default());

// Test default works:
let msg = "Default circuit verification should pass";
Expand Down
Loading

0 comments on commit 0b0888b

Please sign in to comment.