diff --git a/CHANGELOG.md b/CHANGELOG.md index f615f589..a9b5cbfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix panic when creating proof for circuit with different circuit size [#760] +- Fix panic when testing in debug mode [#763] + +### Removed + +- Remove 'setup' funcion from common test module [#763] + +### Changed + +- Change range and logic component to be generic over the const `BIT_PAIRS` [#763] ## [0.14.1] - 2022-06-28 @@ -482,6 +491,7 @@ is necessary since `rkyv/validation` was required as a bound. - Proof system module. +[#763]: https://github.com/dusk-network/plonk/issues/763 [#760]: https://github.com/dusk-network/plonk/issues/760 [#752]: https://github.com/dusk-network/plonk/pull/752 [#738]: https://github.com/dusk-network/plonk/issues/738 diff --git a/README.md b/README.md index 0ddc2806..c5b3001e 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ use rand_core::OsRng; // Implement a circuit that checks: // 1) a + b = c where C is a PI // 2) a < 2^6 -// 3) b < 2^5 +// 3) b < 2^4 // 4) a * b = d where D is a PI // 5) JubJub::GENERATOR * e(JubJubScalar) = f where F is a Public Input #[derive(Debug, Default)] @@ -50,8 +50,10 @@ impl Circuit for TestCircuit { composer.append_gate(constraint); // Check that a and b are in range - composer.component_range(a, 6); - composer.component_range(b, 5); + const HALF_SIX: usize = 3; + composer.component_range::(a); + const HALF_FOUR: usize = 2; + composer.component_range::(b); // Make second constraint a * b = d let constraint = diff --git a/benches/plonk.rs b/benches/plonk.rs index 6affff9a..f5dbdabf 100644 --- a/benches/plonk.rs +++ b/benches/plonk.rs @@ -57,8 +57,8 @@ impl Circuit for BenchCircuit { composer.gate_add(Constraint::new().left(1).right(1).a(w_a).b(w_b)); composer.component_add_point(w_z, w_z); - composer.append_logic_and(w_a, w_b, 254); - composer.append_logic_xor(w_a, w_b, 254); + composer.append_logic_and::<128>(w_a, w_b); + composer.append_logic_xor::<128>(w_a, w_b); composer.component_boolean(C::ONE); composer.component_decomposition::<254>(w_a); composer.component_mul_generator( @@ -66,7 +66,7 @@ impl Circuit for BenchCircuit { dusk_jubjub::GENERATOR_EXTENDED, )?; composer.component_mul_point(w_y, w_z); - composer.component_range(w_a, 254); + composer.component_range::<128>(w_a); composer.component_select(C::ONE, w_a, w_b); composer.component_select_identity(C::ONE, w_z); composer.component_select_one(C::ONE, w_a); diff --git a/src/composer.rs b/src/composer.rs index ba8df417..045fbe92 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -105,10 +105,10 @@ pub trait Composer: Sized + Index { self.append_custom_gate_internal(constraint) } - /// Performs a logical AND or XOR op between the inputs provided for the - /// specified number of bits (counting from the least significant bit). + /// Performs a logical AND or XOR op between the inputs provided for + /// `num_bits = BIT_PAIRS * 2` bits (counting from the least significant). /// - /// Each logic gate adds `(num_bits / 2) + 1` gates to the circuit to + /// Each logic gate adds `BIT_PAIRS + 1` gates to the circuit to /// perform the whole operation. /// /// ## Constraint @@ -116,22 +116,15 @@ pub trait Composer: Sized + Index { /// `a` and `b`. /// - is_component_xor = 0 -> Performs AND between the first `num_bits` for /// `a` and `b`. - /// - /// # Panics - /// This function will panic if the num_bits specified is not even, ie. - /// `num_bits % 2 != 0`. - fn append_logic_component( + fn append_logic_component( &mut self, a: Witness, b: Witness, - num_bits: usize, is_component_xor: bool, ) -> Witness { // the bits are iterated as chunks of two; hence, we require an even // number - debug_assert_eq!(num_bits & 1, 0); - - let num_bits = cmp::min(num_bits, 256); + let num_bits = cmp::min(BIT_PAIRS * 2, 256); let num_quads = num_bits >> 1; let bls_four = BlsScalar::from(4u64); @@ -272,7 +265,13 @@ pub trait Composer: Sized + Index { let width = 2; let wnaf_entries = scalar.compute_windowed_naf(width); - debug_assert_eq!(wnaf_entries.len(), bits); + // this will pass as long as `compute_windowed_naf` returns an array + // with 256 elements + debug_assert_eq!( + wnaf_entries.len(), + bits, + "the wnaf_entries array is expected to be 256 elements long" + ); // initialize the accumulators let mut scalar_acc = vec![BlsScalar::zero()]; @@ -581,35 +580,25 @@ pub trait Composer: Sized + Index { } /// Adds a logical AND gate that performs the bitwise AND between two values - /// for the specified first `num_bits` returning a [`Witness`] + /// specified first `num_bits = BIT_PAIRS * 2` bits returning a [`Witness`] /// holding the result. - /// - /// # Panics - /// - /// If the `num_bits` specified in the fn params is odd. - fn append_logic_and( + fn append_logic_and( &mut self, a: Witness, b: Witness, - num_bits: usize, ) -> Witness { - self.append_logic_component(a, b, num_bits, false) + self.append_logic_component::(a, b, false) } /// Adds a logical XOR gate that performs the XOR between two values for the - /// specified first `num_bits` returning a [`Witness`] holding the - /// result. - /// - /// # Panics - /// - /// If the `num_bits` specified in the fn params is odd. - fn append_logic_xor( + /// specified first `num_bits = BIT_PAIRS * 2` bits returning a [`Witness`] + /// holding the result. + fn append_logic_xor( &mut self, a: Witness, b: Witness, - num_bits: usize, ) -> Witness { - self.append_logic_component(a, b, num_bits, true) + self.append_logic_component::(a, b, true) } /// Constrain `a` to be equal to `constant + pi`. @@ -913,18 +902,18 @@ pub trait Composer: Sized + Index { } /// Adds a range-constraint gate that checks and constrains a [`Witness`] - /// to be encoded in at most `num_bits`, which means that it will be within - /// the range `[0, 2^num_bits[`. - /// - /// This function adds min(1, `num_bits/4`) gates to the circuit description - /// in order to add the range constraint. + /// to be encoded in at most `num_bits = BIT_PAIRS * 2` bits, which means + /// that the underlying [`BlsScalar`] of the [`Witness`] will be within the + /// range `[0, 2^num_bits[`, where `num_bits` is dividable by two. /// - ///# Panics - /// This function will panic if the num_bits specified is not even, ie. - /// `num_bits % 2 != 0`. - fn component_range(&mut self, witness: Witness, num_bits: usize) { - // number of bits must be even - debug_assert_eq!(num_bits % 2, 0); + /// This function adds: + /// (num_bits - 1)/8 + 9 gates, when num_bits > 0, + /// and 7 gates, when num_bits = 0 + /// to the circuit description. + fn component_range(&mut self, witness: Witness) { + // the bits are iterated as chunks of two; hence, we require an even + // number + let num_bits = cmp::min(BIT_PAIRS * 2, 256); // if num_bits = 0 constrain witness to 0 if num_bits == 0 { diff --git a/tests/append_gate.rs b/tests/append_gate.rs index 33c18303..0722e534 100644 --- a/tests/append_gate.rs +++ b/tests/append_gate.rs @@ -9,52 +9,28 @@ use rand::rngs::StdRng; use rand::SeedableRng; mod common; -use common::{check_satisfied_circuit, check_unsatisfied_circuit, setup}; +use common::{check_satisfied_circuit, check_unsatisfied_circuit}; #[test] fn append_gate() { #[derive(Default)] pub struct TestCircuit { - q_l: BlsScalar, - q_r: BlsScalar, - q_m: BlsScalar, - q_k: BlsScalar, - q_o: BlsScalar, a: BlsScalar, b: BlsScalar, d: BlsScalar, o: BlsScalar, public: BlsScalar, - constant: BlsScalar, } impl TestCircuit { pub fn new( - q_l: BlsScalar, - q_r: BlsScalar, - q_m: BlsScalar, - q_k: BlsScalar, - q_o: BlsScalar, a: BlsScalar, b: BlsScalar, d: BlsScalar, o: BlsScalar, public: BlsScalar, - constant: BlsScalar, ) -> Self { - Self { - q_l, - q_r, - q_m, - q_k, - q_o, - a, - b, - d, - o, - public, - constant, - } + Self { a, b, d, o, public } } } @@ -69,17 +45,17 @@ fn append_gate() { let w_o = composer.append_witness(self.o); let constraint = Constraint::new() - .left(self.q_l) - .right(self.q_r) - .mult(self.q_m) - .fourth(self.q_k) - .output(self.q_o) + .left(1) + .right(1) + .mult(1) + .fourth(1) + .output(1) .a(w_a) .b(w_b) .d(w_d) .o(w_o) .public(self.public) - .constant(self.constant); + .constant(BlsScalar::zero()); composer.append_gate(constraint); @@ -91,24 +67,20 @@ fn append_gate() { let rng = &mut StdRng::seed_from_u64(0x1ab); let capacity = 1 << 4; - // Test: public = zero, constant = zero, selectors = one + // Test: constant = zero, selectors = one // // Compile common circuit descriptions for the prover and verifier - let q_l = BlsScalar::one(); - let q_r = BlsScalar::one(); - let q_m = BlsScalar::one(); - let q_k = BlsScalar::one(); - let q_o = BlsScalar::one(); let public = BlsScalar::zero(); - let constant = BlsScalar::zero(); let a = BlsScalar::zero(); let b = BlsScalar::zero(); let d = BlsScalar::zero(); let o = BlsScalar::zero(); let pi = vec![public]; - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + let circuit = TestCircuit::new(a, b, d, o, public); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // Test default works: let msg = "Default circuit verification should pass"; @@ -121,8 +93,7 @@ fn append_gate() { let b = BlsScalar::one(); let d = BlsScalar::one(); let o = -BlsScalar::from(4); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); + let circuit = TestCircuit::new(a, b, d, o, public); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test satisfied circuit: @@ -132,8 +103,7 @@ fn append_gate() { let b = BlsScalar::zero(); let d = BlsScalar::zero(); let o = -BlsScalar::one(); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); + let circuit = TestCircuit::new(a, b, d, o, public); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test satisfied circuit: @@ -143,8 +113,7 @@ fn append_gate() { let b = BlsScalar::one(); let d = BlsScalar::zero(); let o = -BlsScalar::one(); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); + let circuit = TestCircuit::new(a, b, d, o, public); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test satisfied circuit: @@ -154,8 +123,7 @@ fn append_gate() { let b = BlsScalar::one(); let d = BlsScalar::zero(); let o = -BlsScalar::from(3u64); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); + let circuit = TestCircuit::new(a, b, d, o, public); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test satisfied circuit: @@ -165,8 +133,7 @@ fn append_gate() { let b = BlsScalar::zero(); let d = BlsScalar::one(); let o = BlsScalar::zero(); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); + let circuit = TestCircuit::new(a, b, d, o, public); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test satisfied circuit: @@ -178,8 +145,7 @@ fn append_gate() { let public = BlsScalar::from(42); let o = -(a + b + a * b + d + public); let pi = vec![public]; - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); + let circuit = TestCircuit::new(a, b, d, o, public); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test unsatisfied circuit: @@ -189,8 +155,7 @@ fn append_gate() { let d = BlsScalar::random(rng); let o = BlsScalar::random(rng); let public = BlsScalar::random(rng); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); + let circuit = TestCircuit::new(a, b, d, o, public); check_unsatisfied_circuit(&prover, &circuit, rng, &msg); // Test unsatisfied circuit: @@ -201,21 +166,7 @@ fn append_gate() { let d = BlsScalar::one(); let o = BlsScalar::one(); let public = BlsScalar::one(); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); - check_unsatisfied_circuit(&prover, &circuit, rng, &msg); - - // Test circuit where circuit description doesn't match - // q_l·a + q_r·b + q_m·a·b + q_o·o + q_4·d + public + constant = 0 - let msg = "Proof creation of circuit that has different constant than in description should fail"; - let a = BlsScalar::zero(); - let b = BlsScalar::zero(); - let d = BlsScalar::zero(); - let o = BlsScalar::zero(); - let public = BlsScalar::from(2u64); - let constant = -BlsScalar::from(2u64); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); + let circuit = TestCircuit::new(a, b, d, o, public); check_unsatisfied_circuit(&prover, &circuit, rng, &msg); // Test unsatisfied circuit @@ -225,7 +176,6 @@ fn append_gate() { let d = BlsScalar::one(); let o = BlsScalar::one(); let public = BlsScalar::one(); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, q_o, a, b, d, o, public, constant); + let circuit = TestCircuit::new(a, b, d, o, public); check_unsatisfied_circuit(&prover, &circuit, rng, &msg); } diff --git a/tests/assert_point.rs b/tests/assert_point.rs index aebdf833..ca1d938e 100644 --- a/tests/assert_point.rs +++ b/tests/assert_point.rs @@ -9,7 +9,7 @@ use rand::rngs::StdRng; use rand::SeedableRng; mod common; -use common::{check_satisfied_circuit, check_unsatisfied_circuit, setup}; +use common::{check_satisfied_circuit, check_unsatisfied_circuit}; #[test] fn assert_equal_point() { @@ -51,8 +51,10 @@ 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, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // Test default works: // GENERATOR = GENERATOR @@ -140,8 +142,10 @@ 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, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // Test default works: // GENERATOR = GENERATOR diff --git a/tests/assert_scalar.rs b/tests/assert_scalar.rs index f492dad0..4ae8a691 100644 --- a/tests/assert_scalar.rs +++ b/tests/assert_scalar.rs @@ -11,7 +11,7 @@ use rand::SeedableRng; mod common; use common::{ check_satisfied_circuit, check_satisfied_circuit_fails, - check_unsatisfied_circuit, setup, + check_unsatisfied_circuit, }; #[test] @@ -55,8 +55,10 @@ 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, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // public input to be used by all tests let pi = vec![]; @@ -146,8 +148,10 @@ fn assert_equal_constant() { 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, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // Test default works: // 0 = 0 + None @@ -181,7 +185,9 @@ fn assert_equal_constant() { let constant = BlsScalar::zero(); let public = Some(BlsScalar::zero()); let circuit = TestCircuit::new(scalar, constant, public); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + let (prover, verifier) = + Compiler::compile_with_circuit(&pp, label, &circuit) + .expect("Circuit should compile"); // Test default works: // 0 = 0 + 0 @@ -224,7 +230,9 @@ fn assert_equal_constant() { let scalar = constant.clone(); let public = None; let circuit = TestCircuit::new(scalar, constant, public); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + let (prover, verifier) = + Compiler::compile_with_circuit(&pp, label, &circuit) + .expect("Circuit should compile"); // Test default works: // x = x + None @@ -255,7 +263,9 @@ fn assert_equal_constant() { let scalar = constant.clone(); let public = Some(BlsScalar::zero()); let circuit = TestCircuit::new(scalar, constant, public); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + let (prover, verifier) = + Compiler::compile_with_circuit(&pp, label, &circuit) + .expect("Circuit should compile"); // Test default works: // 0 = 0 + 0 diff --git a/tests/boolean.rs b/tests/boolean.rs index 39a8c056..5789ddb7 100644 --- a/tests/boolean.rs +++ b/tests/boolean.rs @@ -9,7 +9,7 @@ use rand::rngs::StdRng; use rand::SeedableRng; mod common; -use common::{check_satisfied_circuit, check_unsatisfied_circuit, setup}; +use common::{check_satisfied_circuit, check_unsatisfied_circuit}; #[test] fn component_boolean() { @@ -47,8 +47,10 @@ 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, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // public inputs to be used by all tests let pi = vec![]; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index beaa7302..4504dc2b 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -7,23 +7,6 @@ use dusk_plonk::prelude::*; use rand::{CryptoRng, RngCore}; -// Create the circuit description for both the prover and the verifier, -// the `capacity` is a power of two and larger than the amount of gates in `C` -pub(crate) fn setup( - capacity: usize, - rng: &mut R, - label: &[u8], - circuit: &C, -) -> (Prover, Verifier) -where - C: Circuit, - R: RngCore + CryptoRng, -{ - let pp = PublicParameters::setup(capacity, rng) - .expect("Creation of public parameter shouldn't fail"); - 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 // and that the public inputs are as expected pub(crate) fn check_satisfied_circuit( diff --git a/tests/composer.rs b/tests/composer.rs index fc37d84b..79ecb863 100644 --- a/tests/composer.rs +++ b/tests/composer.rs @@ -64,7 +64,7 @@ fn circuit_with_all_gates() { composer.gate_add(Constraint::new().left(1).right(1).a(w_a).b(w_b)); composer.component_add_point(w_z, w_z); - composer.append_logic_and(w_a, w_b, 254); + composer.append_logic_and::<128>(w_a, w_b); composer.component_boolean(Builder::ONE); composer.component_decomposition::<254>(w_a); composer.component_mul_generator( @@ -72,13 +72,13 @@ fn circuit_with_all_gates() { dusk_jubjub::GENERATOR_EXTENDED, )?; composer.component_mul_point(w_y, w_z); - composer.component_range(w_a, 254); + composer.component_range::<128>(w_a); composer.component_select(Builder::ONE, w_a, w_b); composer.component_select_identity(Builder::ONE, w_z); composer.component_select_one(Builder::ONE, w_a); composer.component_select_point(Builder::ONE, w_z, w_z); composer.component_select_zero(Builder::ONE, w_a); - composer.append_logic_xor(w_a, w_b, 254); + composer.append_logic_xor::<128>(w_a, w_b); Ok(()) } diff --git a/tests/decomposition.rs b/tests/decomposition.rs index 27289127..0c0a4a03 100644 --- a/tests/decomposition.rs +++ b/tests/decomposition.rs @@ -9,7 +9,7 @@ use rand::rngs::StdRng; use rand::SeedableRng; mod common; -use common::{check_satisfied_circuit, check_unsatisfied_circuit, setup}; +use common::{check_satisfied_circuit, check_unsatisfied_circuit}; #[test] fn component_decomposition() { @@ -60,7 +60,10 @@ fn component_decomposition() { // Compile new circuit descriptions for the prover and verifier const N1: usize = 1; let circuit = TestCircuit::::default(); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // Test default works: let msg = "Default circuit verification should pass"; @@ -86,7 +89,9 @@ fn component_decomposition() { // Compile new circuit descriptions for the prover and verifier const N64: usize = 64; let circuit = TestCircuit::::default(); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + let (prover, verifier) = + Compiler::compile_with_circuit(&pp, label, &circuit) + .expect("Circuit should compile"); // Test default works: let msg = "Default circuit verification should pass"; @@ -129,7 +134,9 @@ fn component_decomposition() { // Compile new circuit descriptions for the prover and verifier const N256: usize = 256; let circuit = TestCircuit::::default(); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + let (prover, verifier) = + Compiler::compile_with_circuit(&pp, label, &circuit) + .expect("Circuit should compile"); // Test random works: let msg = "Verification of satisfied circuit should pass"; diff --git a/tests/ecc.rs b/tests/ecc.rs index dae23b82..f2c6cb6b 100644 --- a/tests/ecc.rs +++ b/tests/ecc.rs @@ -9,7 +9,7 @@ use rand::rngs::StdRng; use rand::SeedableRng; mod common; -use common::{check_satisfied_circuit, check_unsatisfied_circuit, setup}; +use common::{check_satisfied_circuit, check_unsatisfied_circuit}; #[test] fn component_add_point() { @@ -60,8 +60,10 @@ 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, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // Test default works: let msg = "Default circuit verification should pass"; @@ -159,8 +161,10 @@ 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, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // generator point and pi are the same for all tests let generator = dusk_jubjub::GENERATOR_EXTENDED; @@ -263,8 +267,10 @@ 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, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // Test default works: let msg = "Default circuit verification should pass"; diff --git a/tests/gate_add_mul.rs b/tests/gate_add_mul.rs index 5a219b88..719252e6 100644 --- a/tests/gate_add_mul.rs +++ b/tests/gate_add_mul.rs @@ -9,47 +9,32 @@ use rand::rngs::StdRng; use rand::SeedableRng; mod common; -use common::{check_satisfied_circuit, check_unsatisfied_circuit, setup}; +use common::{check_satisfied_circuit, check_unsatisfied_circuit}; #[test] fn gate_add_mul() { #[derive(Default)] pub struct TestCircuit { - q_l: BlsScalar, - q_r: BlsScalar, - q_m: BlsScalar, - q_k: BlsScalar, a: BlsScalar, b: BlsScalar, d: BlsScalar, public: BlsScalar, - constant: BlsScalar, result: BlsScalar, } impl TestCircuit { pub fn new( - q_l: BlsScalar, - q_r: BlsScalar, - q_m: BlsScalar, - q_k: BlsScalar, a: BlsScalar, b: BlsScalar, d: BlsScalar, public: BlsScalar, - constant: BlsScalar, result: BlsScalar, ) -> Self { Self { - q_l, - q_r, - q_m, - q_k, a, b, d, public, - constant, result, } } @@ -67,15 +52,15 @@ fn gate_add_mul() { let result_expected = composer.append_witness(self.result); let constraint = Constraint::new() - .left(self.q_l) - .right(self.q_r) - .mult(self.q_m) - .fourth(self.q_k) + .left(1) + .right(1) + .mult(1) + .fourth(1) .a(w_a) .b(w_b) .d(w_d) .public(self.public) - .constant(self.constant); + .constant(BlsScalar::one()); let result_add = composer.gate_add(constraint); let result_mul = composer.gate_mul(constraint); @@ -96,47 +81,43 @@ fn gate_add_mul() { // Test: public = zero, constant = zero, selectors = one // // Compile common circuit descriptions for the prover and verifier - let q_l = BlsScalar::one(); - let q_r = BlsScalar::one(); - let q_m = BlsScalar::one(); - let q_k = BlsScalar::one(); let public = BlsScalar::zero(); - let constant = BlsScalar::zero(); + const CONST: BlsScalar = BlsScalar::one(); let a = BlsScalar::zero(); let b = BlsScalar::zero(); let d = BlsScalar::zero(); - let result = q_l * a + q_r * b + q_m * a * b + q_k * d + public + constant; + let result = a + b + a * b + d + public + CONST; let pi = vec![public, public]; - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, a, b, d, public, constant, result); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + let circuit = TestCircuit::new(a, b, d, public, result); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // Test default works: let msg = "Default circuit verification should pass"; check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test satisfied circuit: - // q_l·a + q_r·b + q_m·a·b + q_o·o + q_4·d + public + constant = 0 + // a + b + a·b + d + public + 1 = result let msg = "Verification of satisfied circuit should pass"; let a = BlsScalar::one(); let b = BlsScalar::one(); let d = BlsScalar::one(); - let result = q_l * a + q_r * b + q_m * a * b + q_k * d + public + constant; - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, a, b, d, public, constant, result); + let result = a + b + a * b + d + public + CONST; + let circuit = TestCircuit::new(a, b, d, public, result); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test satisfied circuit: - // q_l·a + q_r·b + q_m·a·b + q_o·o + q_4·d + public + constant = 0 + // a + b + a·b + d + public + 1 = result let msg = "Verification of satisfied circuit should pass"; let a = BlsScalar::random(rng); let b = BlsScalar::random(rng); let d = BlsScalar::random(rng); let public = BlsScalar::random(rng); let pi = vec![public, public]; - let result = q_l * a + q_r * b + q_m * a * b + q_k * d + public + constant; - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, a, b, d, public, constant, result); + let result = a + b + a * b + d + public + CONST; + let circuit = TestCircuit::new(a, b, d, public, result); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test unsatisfied circuit: @@ -145,39 +126,29 @@ fn gate_add_mul() { let b = BlsScalar::random(rng); let d = BlsScalar::random(rng); let public = BlsScalar::random(rng); - let result = q_l * a - + q_r * b - + q_m * a * b - + q_k * d - + public - + constant - + BlsScalar::one(); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, a, b, d, public, constant, result); + let result = a + b + a * b + d + public + CONST + BlsScalar::one(); + let circuit = TestCircuit::new(a, b, d, public, result); check_unsatisfied_circuit(&prover, &circuit, rng, &msg); // Test unsatisfied circuit: - // q_l·a + q_r·b + q_m·a·b + q_o·o + q_4·d + public + constant = 0 + // a + b + a·b + d + public + 1 = result let msg = "Proof creation of unsatisfied circuit should fail"; let a = BlsScalar::one(); let b = BlsScalar::one(); let d = BlsScalar::one(); let public = BlsScalar::one(); - let result = BlsScalar::from(6); - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, a, b, d, public, constant, result); + let result = BlsScalar::from(42); + let circuit = TestCircuit::new(a, b, d, public, result); check_unsatisfied_circuit(&prover, &circuit, rng, &msg); // Test circuit where circuit description doesn't match - // q_l·a + q_r·b + q_m·a·b + q_o·o + q_4·d + public + constant = 0 let msg = "Proof creation of circuit that has different constant than in description should fail"; let a = BlsScalar::zero(); let b = BlsScalar::zero(); let d = BlsScalar::zero(); let public = BlsScalar::from(2u64); - let constant = -BlsScalar::from(2u64); - let result = q_l * a + q_r * b + q_m * a * b + q_k * d + public + constant; - let circuit = - TestCircuit::new(q_l, q_r, q_m, q_k, a, b, d, public, constant, result); + let incorrect_constant = -BlsScalar::from(2u64); + let result = a + b + a * b + d + public + incorrect_constant; + let circuit = TestCircuit::new(a, b, d, public, result); check_unsatisfied_circuit(&prover, &circuit, rng, &msg); } diff --git a/tests/logic.rs b/tests/logic.rs index 050eddac..ed418013 100644 --- a/tests/logic.rs +++ b/tests/logic.rs @@ -4,39 +4,40 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. +use core::cmp; use dusk_plonk::prelude::*; use rand::rngs::StdRng; use rand::SeedableRng; mod common; -use common::{check_satisfied_circuit, check_unsatisfied_circuit, setup}; +use common::{check_satisfied_circuit, check_unsatisfied_circuit}; #[test] fn append_logic_and() { #[derive(Default)] - pub struct TestCircuit { + pub struct TestCircuit { a: BlsScalar, b: BlsScalar, result: BlsScalar, - bits: usize, } - impl TestCircuit { - pub fn new(a: BlsScalar, b: BlsScalar, bits: usize) -> Self { + impl TestCircuit { + pub fn new(a: BlsScalar, b: BlsScalar) -> Self { + let bits = cmp::min(BIT_PAIRS * 2, 256); let bit_mask = BlsScalar::pow_of_2(bits as u64) - BlsScalar::one(); - // BlsScalar are max 255 bits long so a bit_mask with more than 255 - // bits will be overflowing and therefore incorrect - let result = match bits < 256 { + // BlsScalar are max 255 bits long which means that a bit_mask with + // more than 255 bits will be overflowing and therefore incorrect + let result = match bits <= 255 { true => a & b & bit_mask, false => a & b, }; - Self { a, b, result, bits } + Self { a, b, result } } } - impl Circuit for TestCircuit { + impl Circuit for TestCircuit { fn circuit(&self, composer: &mut C) -> Result<(), Error> where C: Composer, @@ -45,7 +46,8 @@ fn append_logic_and() { let w_b = composer.append_witness(self.b); let w_result = composer.append_witness(self.result); - let circuit_result = composer.append_logic_and(w_a, w_b, self.bits); + let circuit_result = + composer.append_logic_and::(w_a, w_b); composer.assert_equal(w_result, circuit_result); @@ -58,8 +60,10 @@ fn append_logic_and() { let label = b"append_logic_and"; let rng = &mut StdRng::seed_from_u64(0x1ead); let capacity = 1 << 8; - let (prover, verifier) = - setup(capacity, rng, label, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // Common public input vector to be used by all tests let pi = vec![]; @@ -68,65 +72,62 @@ fn append_logic_and() { // // Test default works let msg = "Default circuit verification should pass"; - let circuit = TestCircuit::default(); + let circuit = TestCircuit::<0>::default(); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test comparing 0 bits is always zero let msg = "Circuit verification of satisfied circuit should pass"; let a = BlsScalar::random(rng); let b = BlsScalar::random(rng); - let circuit = TestCircuit { + let circuit = TestCircuit::<0> { a, b, result: BlsScalar::zero(), - bits: 0, }; check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test with bits = 32 // // Create new prover and verifier circuit descriptions - let bits = 32; - let a = BlsScalar::zero(); - let b = BlsScalar::zero(); - let circuit = TestCircuit::new(a, b, bits); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + const BIT_PAIRS_16: usize = 16; + let (prover, verifier) = + Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // Test sanity: let a = BlsScalar::from(0x0f0f_0ff0_0f0f_0ff0); let b = BlsScalar::from(0xffff_0000_0000_ffff); let result = BlsScalar::from(0x0000_0ff0); - let circuit = TestCircuit { a, b, result, bits }; + let circuit: TestCircuit = TestCircuit { a, b, result }; check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test random works: let a = BlsScalar::random(rng); let b = BlsScalar::random(rng); - let circuit = TestCircuit::new(a, b, bits); + let circuit: TestCircuit = TestCircuit::new(a, b); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test invalid circuit fails let msg = "Proof creation of unsatisfied circuit should fail"; - let bit_mask = BlsScalar::pow_of_2(bits as u64) - BlsScalar::one(); + let bit_mask = + BlsScalar::pow_of_2(BIT_PAIRS_16 as u64 * 2) - BlsScalar::one(); let a = BlsScalar::random(rng); let b = BlsScalar::random(rng); let right_result = a & b & bit_mask; let c = BlsScalar::random(rng); let wrong_result = a & c & bit_mask; assert_ne!(right_result, wrong_result); - let circuit_unsatisfied = TestCircuit { + let circuit_unsatisfied: TestCircuit = TestCircuit { a, b, result: wrong_result, - bits, }; check_unsatisfied_circuit(&prover, &circuit_unsatisfied, rng, &msg); // sanity check - let circuit_satisfied = TestCircuit { + let circuit_satisfied: TestCircuit = TestCircuit { a, b, result: right_result, - bits, }; check_satisfied_circuit( &prover, @@ -140,24 +141,23 @@ fn append_logic_and() { // Test with bits = 256 // // Create new circuit description for the prover and verifier - let bits = 256; - let a = BlsScalar::zero(); - let b = BlsScalar::zero(); - let circuit = TestCircuit::new(a, b, bits); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + const BIT_PAIRS_128: usize = 128; + let (prover, verifier) = + Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // Test sanity: let a = -BlsScalar::one(); let b = -BlsScalar::one(); let result = -BlsScalar::one(); - let circuit = TestCircuit { a, b, result, bits }; + let circuit: TestCircuit = TestCircuit { a, b, result }; check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test random works: let msg = "Circuit verification with random values should pass"; let a = BlsScalar::random(rng); let b = BlsScalar::random(rng); - let circuit = TestCircuit::new(a, b, bits); + let circuit: TestCircuit = TestCircuit::new(a, b); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test invalid circuit fails @@ -168,19 +168,17 @@ fn append_logic_and() { let c = BlsScalar::random(rng); let wrong_result = a & c; assert_ne!(right_result, wrong_result); - let circuit = TestCircuit { + let circuit: TestCircuit = TestCircuit { a, b, result: wrong_result, - bits, }; check_unsatisfied_circuit(&prover, &circuit, rng, &msg); // sanity check - let circuit_satisfied = TestCircuit { + let circuit_satisfied: TestCircuit = TestCircuit { a, b, result: right_result, - bits, }; check_satisfied_circuit( &prover, @@ -190,36 +188,20 @@ fn append_logic_and() { rng, &"Sanity check should pass", ); - - // Test with odd bits = 55 - // - // Create new prover and verifier circuit descriptions - let bits = 55; - let a = BlsScalar::zero(); - let b = BlsScalar::zero(); - let circuit = TestCircuit::new(a, b, bits); - let (prover, _) = setup(capacity, rng, label, &circuit); - - // Test random fails: - let msg = "Circuit verification with odd bits should fail"; - let a = BlsScalar::random(rng); - let b = BlsScalar::random(rng); - let circuit = TestCircuit::new(a, b, bits); - check_unsatisfied_circuit(&prover, &circuit, rng, &msg); } #[test] fn append_logic_xor() { #[derive(Default)] - pub struct TestCircuit { + pub struct TestCircuit { a: BlsScalar, b: BlsScalar, result: BlsScalar, - bits: usize, } - impl TestCircuit { - pub fn new(a: BlsScalar, b: BlsScalar, bits: usize) -> Self { + impl TestCircuit { + pub fn new(a: BlsScalar, b: BlsScalar) -> Self { + let bits = cmp::min(BIT_PAIRS * 2, 256); let bit_mask = BlsScalar::pow_of_2(bits as u64) - BlsScalar::one(); // BlsScalar are max 255 bits long so a bit_mask with more than 255 @@ -229,11 +211,11 @@ fn append_logic_xor() { false => a ^ b, }; - Self { a, b, result, bits } + Self { a, b, result } } } - impl Circuit for TestCircuit { + impl Circuit for TestCircuit { fn circuit(&self, composer: &mut C) -> Result<(), Error> where C: Composer, @@ -242,7 +224,8 @@ fn append_logic_xor() { let w_b = composer.append_witness(self.b); let w_result = composer.append_witness(self.result); - let circuit_result = composer.append_logic_xor(w_a, w_b, self.bits); + let circuit_result = + composer.append_logic_xor::(w_a, w_b); composer.assert_equal(w_result, circuit_result); @@ -255,8 +238,10 @@ fn append_logic_xor() { let label = b"append_logic_xor"; let rng = &mut StdRng::seed_from_u64(0xdea1); let capacity = 1 << 8; - let (prover, verifier) = - setup(capacity, rng, label, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // Common values to be used by all tests let pi = vec![]; @@ -265,65 +250,63 @@ fn append_logic_xor() { // // Test default works let msg = "Default circuit verification should pass"; - let circuit = TestCircuit::default(); + let circuit = TestCircuit::<0>::default(); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test comparing 0 bits is always zero let msg = "Circuit verification of satisfied circuit should pass"; let a = BlsScalar::random(rng); let b = BlsScalar::random(rng); - let circuit = TestCircuit { + let circuit: TestCircuit<0> = TestCircuit { a, b, result: BlsScalar::zero(), - bits: 0, }; check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test with bits = 32 // // Create new prover and verifier circuit descriptions - let bits = 32; - let a = BlsScalar::zero(); - let b = BlsScalar::zero(); - let circuit = TestCircuit::new(a, b, bits); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + const BIT_PAIRS_16: usize = 16; + let (prover, verifier) = + Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // Test sanity: let a = BlsScalar::from(0x0f0f_0ff0_0f0f_0ff0); let b = BlsScalar::from(0xffff_0000_0000_ffff); let result = BlsScalar::from(0x0f0f_f00f); - let circuit = TestCircuit { a, b, result, bits }; + let circuit: TestCircuit = TestCircuit { a, b, result }; + check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test random works: let a = BlsScalar::random(rng); let b = BlsScalar::random(rng); - let circuit = TestCircuit::new(a, b, bits); + let circuit: TestCircuit = TestCircuit::new(a, b); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test invalid circuit fails let msg = "Proof creation of unsatisfied circuit should fail"; - let bit_mask = BlsScalar::pow_of_2(bits as u64) - BlsScalar::one(); + let bit_mask = + BlsScalar::pow_of_2(BIT_PAIRS_16 as u64 * 2) - BlsScalar::one(); let a = BlsScalar::random(rng); let b = BlsScalar::random(rng); let right_result = (a ^ b) & bit_mask; let c = BlsScalar::random(rng); let wrong_result = (a ^ c) & bit_mask; assert_ne!(right_result, wrong_result); - let circuit_unsatisfied = TestCircuit { + let circuit_unsatisfied: TestCircuit = TestCircuit { a, b, result: wrong_result, - bits, }; check_unsatisfied_circuit(&prover, &circuit_unsatisfied, rng, &msg); // sanity check - let circuit_satisfied = TestCircuit { + let circuit_satisfied: TestCircuit = TestCircuit { a, b, result: right_result, - bits, }; check_satisfied_circuit( &prover, @@ -337,24 +320,23 @@ fn append_logic_xor() { // Test with bits = 256 // // Create new prover and verifier circuit descriptions - let bits = 256; - let a = BlsScalar::zero(); - let b = BlsScalar::zero(); - let circuit = TestCircuit::new(a, b, bits); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + const BIT_PAIRS_128: usize = 128; + let (prover, verifier) = + Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // Test sanity: let a = -BlsScalar::one(); let b = BlsScalar::zero(); let result = -BlsScalar::one(); - let circuit = TestCircuit { a, b, result, bits }; + let circuit: TestCircuit = TestCircuit { a, b, result }; check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test random works: let msg = "Circuit verification with random values should pass"; let a = BlsScalar::random(rng); let b = BlsScalar::random(rng); - let circuit = TestCircuit::new(a, b, bits); + let circuit: TestCircuit = TestCircuit::new(a, b); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test invalid circuit fails @@ -365,19 +347,17 @@ fn append_logic_xor() { let c = BlsScalar::random(rng); let wrong_result = a ^ c; assert_ne!(right_result, wrong_result); - let circuit = TestCircuit { + let circuit: TestCircuit = TestCircuit { a, b, result: wrong_result, - bits, }; check_unsatisfied_circuit(&prover, &circuit, rng, &msg); // sanity check - let circuit_satisfied = TestCircuit { + let circuit_satisfied: TestCircuit = TestCircuit { a, b, result: right_result, - bits, }; check_satisfied_circuit( &prover, @@ -387,20 +367,4 @@ fn append_logic_xor() { rng, &"Sanity check should pass", ); - - // Test with odd bits = 55 - // - // Create new prover and verifier circuit descriptions - let bits = 55; - let a = BlsScalar::zero(); - let b = BlsScalar::zero(); - let circuit = TestCircuit::new(a, b, bits); - let (prover, _) = setup(capacity, rng, label, &circuit); - - // Test random fails: - let msg = "Circuit verification with odd bits should fail"; - let a = BlsScalar::random(rng); - let b = BlsScalar::random(rng); - let circuit = TestCircuit::new(a, b, bits); - check_unsatisfied_circuit(&prover, &circuit, rng, &msg); } diff --git a/tests/range.rs b/tests/range.rs index 928f231c..91fc7722 100644 --- a/tests/range.rs +++ b/tests/range.rs @@ -9,30 +9,29 @@ use rand::rngs::StdRng; use rand::SeedableRng; mod common; -use common::{check_satisfied_circuit, check_unsatisfied_circuit, setup}; +use common::{check_satisfied_circuit, check_unsatisfied_circuit}; #[test] fn range() { #[derive(Default)] - pub struct TestCircuit { + pub struct TestCircuit { a: BlsScalar, - bits: usize, } - impl TestCircuit { - pub fn new(a: BlsScalar, bits: usize) -> Self { - Self { a, bits } + impl TestCircuit { + pub fn new(a: BlsScalar) -> Self { + Self { a } } } - impl Circuit for TestCircuit { + impl Circuit for TestCircuit { fn circuit(&self, composer: &mut C) -> Result<(), Error> where C: Composer, { let w_a = composer.append_witness(self.a); - composer.component_range(w_a, self.bits); + composer.component_range::(w_a); Ok(()) } @@ -43,8 +42,10 @@ fn range() { let label = b"component_range"; let rng = &mut StdRng::seed_from_u64(0xb1eeb); let capacity = 1 << 6; - let (prover, verifier) = - setup(capacity, rng, label, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // public input to be used by all tests let pi = vec![]; @@ -54,15 +55,14 @@ fn range() { // Test default works: // 0 < 2^0 let msg = "Default circuit verification should pass"; - let circuit = TestCircuit::default(); + let circuit = TestCircuit::<0>::default(); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test: // 1 < 2^0 let msg = "Verification of satisfied circuit should pass"; - let bits = 0; let a = BlsScalar::one(); - let circuit = TestCircuit::new(a, bits); + let circuit: TestCircuit<0> = TestCircuit::new(a); check_unsatisfied_circuit(&prover, &circuit, rng, &msg); // Test: @@ -70,97 +70,79 @@ fn range() { let msg = "Unsatisfied circuit should fail"; let a = BlsScalar::random(rng); assert!(a != BlsScalar::zero()); - let circuit = TestCircuit::new(a, bits); + let circuit: TestCircuit<0> = TestCircuit::new(a); check_unsatisfied_circuit(&prover, &circuit, rng, &msg); // Test bits = 2 // // Compile new circuit descriptions for the prover and verifier - let bits = 2; - let a = BlsScalar::one(); - let circuit = TestCircuit::new(a, bits); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + const BIT_PAIRS_1: usize = 1; + let (prover, verifier) = + Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // Test: // 1 < 2^2 let msg = "Verification of a satisfied circuit should pass"; - let circuit = TestCircuit::new(a, bits); + let a = BlsScalar::one(); + let circuit: TestCircuit = TestCircuit::new(a); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test fails: // 4 !< 2^2 let msg = "Proof creation of an unsatisfied circuit should fail"; let a = BlsScalar::from(4); - let circuit = TestCircuit::new(a, bits); - check_unsatisfied_circuit(&prover, &circuit, rng, &msg); - - // Test bits = 4 - // - // Compile new circuit descriptions for the prover and verifier - let bits = 4; - let a = BlsScalar::from(15); - let circuit = TestCircuit::new(a, bits); - let (prover, verifier) = setup(capacity, rng, label, &circuit); - - // Test: - // 15 < 2^4 - let msg = "Verification of a satisfied circuit should pass"; - let circuit = TestCircuit::new(a, bits); - check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); - - // Test fails: - // 16 !< 2^4 - let msg = "Proof creation of an unsatisfied circuit should fail"; - let a = BlsScalar::from(16); - let circuit = TestCircuit::new(a, bits); + let circuit: TestCircuit = TestCircuit::new(a); check_unsatisfied_circuit(&prover, &circuit, rng, &msg); // Test bits = 74 // // Compile new circuit descriptions for the prover and verifier - let bits = 74; - let a = BlsScalar::pow_of_2(73); - let circuit = TestCircuit::new(a, bits); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + const BIT_PAIRS_37: usize = 37; + let (prover, verifier) = + Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // Test: // 2^73 < 2^74 let msg = "Verification of a satisfied circuit should pass"; - let circuit = TestCircuit::new(a, bits); + let a = BlsScalar::pow_of_2(73); + let circuit: TestCircuit = TestCircuit::new(a); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test: // 2^74 - 1 < 2^74 let msg = "Verification of a satisfied circuit should pass"; let a = BlsScalar::pow_of_2(74) - BlsScalar::one(); - let circuit = TestCircuit::new(a, bits); + let circuit: TestCircuit = TestCircuit::new(a); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test fails: // 2^74 !< 2^74 let msg = "Proof creation of an unsatisfied circuit should fail"; let a = BlsScalar::pow_of_2(74); - let circuit = TestCircuit::new(a, bits); + let circuit: TestCircuit = TestCircuit::new(a); check_unsatisfied_circuit(&prover, &circuit, rng, &msg); // Test bits = 256 // // Compile new circuit descriptions for the prover and verifier - let bits = 256; - let a = BlsScalar::pow_of_2(255); - let circuit = TestCircuit::new(a, bits); - let (prover, verifier) = setup(capacity, rng, label, &circuit); + const BIT_PAIRS_128: usize = 128; + let (prover, verifier) = + Compiler::compile::>(&pp, label) + .expect("Circuit should compile"); // Test: // 2^255 < 2^256 let msg = "Verification of a satisfied circuit should pass"; - let circuit = TestCircuit::new(a, bits); + let a = BlsScalar::pow_of_2(255); + let circuit: TestCircuit = TestCircuit::new(a); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); // Test: // -bls(1) < 2^256 let msg = "Verification of a satisfied circuit should pass"; let a = -BlsScalar::one(); - let circuit = TestCircuit::new(a, bits); + let circuit: TestCircuit = TestCircuit::new(a); check_satisfied_circuit(&prover, &verifier, &pi, &circuit, rng, &msg); } diff --git a/tests/select_bls.rs b/tests/select_bls.rs index 6fc83efb..0b61d63d 100644 --- a/tests/select_bls.rs +++ b/tests/select_bls.rs @@ -9,7 +9,7 @@ use rand::rngs::StdRng; use rand::SeedableRng; mod common; -use common::{check_satisfied_circuit, check_unsatisfied_circuit, setup}; +use common::{check_satisfied_circuit, check_unsatisfied_circuit}; #[test] fn component_select() { @@ -71,8 +71,10 @@ fn component_select() { let label = b"component_select"; let rng = &mut StdRng::seed_from_u64(0xbeef); let capacity = 1 << 5; - let (prover, verifier) = - setup(capacity, rng, label, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // public inputs to be used by all tests let pi = vec![]; @@ -230,8 +232,10 @@ fn component_select_one() { let label = b"component_select_one"; let rng = &mut StdRng::seed_from_u64(0xfee); let capacity = 1 << 5; - let (prover, verifier) = - setup(capacity, rng, label, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // public inputs to be used by all tests let pi = vec![]; @@ -378,8 +382,10 @@ fn component_select_zero() { let label = b"component_select_zero"; let rng = &mut StdRng::seed_from_u64(0xca11); let capacity = 1 << 5; - let (prover, verifier) = - setup(capacity, rng, label, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // public inputs to be used by all tests let pi = vec![]; diff --git a/tests/select_point.rs b/tests/select_point.rs index b767c3da..17120630 100644 --- a/tests/select_point.rs +++ b/tests/select_point.rs @@ -9,7 +9,7 @@ use rand::rngs::StdRng; use rand::SeedableRng; mod common; -use common::{check_satisfied_circuit, check_unsatisfied_circuit, setup}; +use common::{check_satisfied_circuit, check_unsatisfied_circuit}; #[test] fn component_select_point() { @@ -71,8 +71,10 @@ fn component_select_point() { let label = b"component_select_point"; let rng = &mut StdRng::seed_from_u64(0xce11); let capacity = 1 << 5; - let (prover, verifier) = - setup(capacity, rng, label, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // public inputs to be used by all tests let pi = vec![]; @@ -249,8 +251,10 @@ fn component_select_identity() { let label = b"component_select_one"; let rng = &mut StdRng::seed_from_u64(0xfee); let capacity = 1 << 5; - let (prover, verifier) = - setup(capacity, rng, label, &TestCircuit::default()); + let pp = PublicParameters::setup(capacity, rng) + .expect("Creation of public parameter shouldn't fail"); + let (prover, verifier) = Compiler::compile::(&pp, label) + .expect("Circuit should compile"); // public inputs to be used by all tests let pi = vec![];