From 460267d248c443bc917ea6cc1cc1800a979ec804 Mon Sep 17 00:00:00 2001 From: xevisalle Date: Wed, 18 Oct 2023 12:17:22 +0200 Subject: [PATCH] nit fixes --- tests/logic.rs | 182 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 181 insertions(+), 1 deletion(-) diff --git a/tests/logic.rs b/tests/logic.rs index 9e8a8fcb..208df48e 100644 --- a/tests/logic.rs +++ b/tests/logic.rs @@ -191,7 +191,6 @@ fn append_logic_and() { ); } -#[ignore = "see issue #777"] #[test] fn append_logic_xor() { #[derive(Default)] @@ -241,6 +240,187 @@ fn append_logic_xor() { // FIXME: One test used to fail when using "0xdea1" as randomness here. It // needs to be tackled soon or later. + let mut rng = StdRng::seed_from_u64(0xded1); + let capacity = 1 << 8; + let pp = PublicParameters::setup(capacity, &mut 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![]; + + // Test with bits = 0 + // + // Test default works + let msg = "Default circuit verification should pass"; + let circuit = TestCircuit::<0>::default(); + check_satisfied_circuit(&prover, &verifier, &pi, &circuit, &mut rng, &msg); + + // Test comparing 0 bits is always zero + let msg = "Circuit verification of satisfied circuit should pass"; + let a = BlsScalar::random(&mut rng); + let b = BlsScalar::random(&mut rng); + let circuit: TestCircuit<0> = TestCircuit { + a, + b, + result: BlsScalar::zero(), + }; + check_satisfied_circuit(&prover, &verifier, &pi, &circuit, &mut rng, &msg); + + // Test with bits = 32 + // + // Create new prover and verifier circuit descriptions + 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 = TestCircuit { a, b, result }; + + check_satisfied_circuit(&prover, &verifier, &pi, &circuit, &mut rng, &msg); + + // Test random works: + let a = BlsScalar::random(&mut rng); + let b = BlsScalar::random(&mut rng); + let circuit: TestCircuit = TestCircuit::new(a, b); + check_satisfied_circuit(&prover, &verifier, &pi, &circuit, &mut rng, &msg); + + // Test invalid circuit fails + let msg = "Proof creation of unsatisfied circuit should fail"; + let bit_mask = + BlsScalar::pow_of_2(BIT_PAIRS_16 as u64 * 2) - BlsScalar::one(); + let a = BlsScalar::random(&mut rng); + let b = BlsScalar::random(&mut rng); + let right_result = (a ^ b) & bit_mask; + let c = BlsScalar::random(&mut rng); + let wrong_result = (a ^ c) & bit_mask; + assert_ne!(right_result, wrong_result); + let circuit_unsatisfied: TestCircuit = TestCircuit { + a, + b, + result: wrong_result, + }; + check_unsatisfied_circuit(&prover, &circuit_unsatisfied, &mut rng, &msg); + // sanity check + let circuit_satisfied: TestCircuit = TestCircuit { + a, + b, + result: right_result, + }; + check_satisfied_circuit( + &prover, + &verifier, + &pi, + &circuit_satisfied, + &mut rng, + &"Sanity check should pass", + ); + + // Test with bits = 256 + // + // Create new prover and verifier circuit descriptions + 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 = TestCircuit { a, b, result }; + check_satisfied_circuit(&prover, &verifier, &pi, &circuit, &mut rng, &msg); + + // Test random works: + let msg = "Circuit verification with random values should pass"; + let a = BlsScalar::random(&mut rng); + let b = BlsScalar::random(&mut rng); + let circuit: TestCircuit = TestCircuit::new(a, b); + check_satisfied_circuit(&prover, &verifier, &pi, &circuit, &mut rng, &msg); + + // Test invalid circuit fails + let msg = "Proof creation of unsatisfied circuit should fail"; + let a = BlsScalar::random(&mut rng); + let b = BlsScalar::random(&mut rng); + let right_result = a ^ b; + let c = BlsScalar::random(&mut rng); + let wrong_result = a ^ c; + assert_ne!(right_result, wrong_result); + let circuit: TestCircuit = TestCircuit { + a, + b, + result: wrong_result, + }; + check_unsatisfied_circuit(&prover, &circuit, &mut rng, &msg); + // sanity check + let circuit_satisfied: TestCircuit = TestCircuit { + a, + b, + result: right_result, + }; + check_satisfied_circuit( + &prover, + &verifier, + &pi, + &circuit_satisfied, + &mut rng, + &"Sanity check should pass", + ); +} + +#[ignore = "see issue #777"] +#[test] +fn append_logic_xor_failing_test() { + #[derive(Default)] + pub struct TestCircuit { + a: BlsScalar, + b: BlsScalar, + result: BlsScalar, + } + + 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 incorrect + let result = match bits < 256 { + true => (a ^ b) & bit_mask, + false => a ^ b, + }; + + Self { a, b, result } + } + } + + impl Circuit for TestCircuit { + fn circuit(&self, composer: &mut C) -> Result<(), Error> + where + C: Composer, + { + let w_a = composer.append_witness(self.a); + 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); + + composer.assert_equal(w_result, circuit_result); + + Ok(()) + } + } + + // Compile common circuit descriptions for the prover and verifier to be + // used by all tests + let label = b"append_logic_xor"; + let mut rng = StdRng::seed_from_u64(0xdea1); let capacity = 1 << 8; let pp = PublicParameters::setup(capacity, &mut rng)