Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix append_logic_xor test #780

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 2 additions & 185 deletions tests/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn append_logic_xor() {
// 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,
true => (a & bit_mask) ^ (b & bit_mask),
false => a ^ b,
};

Expand Down Expand Up @@ -238,9 +238,7 @@ fn append_logic_xor() {
// used by all tests
let label = b"append_logic_xor";

// FIXME: One test used to fail when using "0xdea1" as randomness here. It
// needs to be tackled soon or later. See issue #777.
let mut rng = StdRng::seed_from_u64(0xded1);
let mut rng = StdRng::seed_from_u64(0xdea1);
let capacity = 1 << 8;
let pp = PublicParameters::setup(capacity, &mut rng)
.expect("Creation of public parameter shouldn't fail");
Expand Down Expand Up @@ -372,184 +370,3 @@ fn append_logic_xor() {
&"Sanity check should pass",
);
}

#[ignore = "see issue #777"]
#[test]
fn append_logic_xor_failing_test() {
#[derive(Default)]
pub struct TestCircuit<const BIT_PAIRS: usize> {
a: BlsScalar,
b: BlsScalar,
result: BlsScalar,
}

impl<const BIT_PAIRS: usize> TestCircuit<BIT_PAIRS> {
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<const BIT_PAIRS: usize> Circuit for TestCircuit<BIT_PAIRS> {
fn circuit<C>(&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::<BIT_PAIRS>(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)
.expect("Creation of public parameter shouldn't fail");
let (prover, verifier) = Compiler::compile::<TestCircuit<0>>(&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::<TestCircuit<BIT_PAIRS_16>>(&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<BIT_PAIRS_16> = TestCircuit { a, b, result };

check_satisfied_circuit(&prover, &verifier, &pi, &circuit, &mut rng, &msg);

// Test random works: (THIS ONE FAILS, see issue #777)
let a = BlsScalar::random(&mut rng);
let b = BlsScalar::random(&mut rng);
let circuit: TestCircuit<BIT_PAIRS_16> = TestCircuit::new(a, b);
check_satisfied_circuit(&prover, &verifier, &pi, &circuit, &mut rng, &msg); // actually, this line

// 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<BIT_PAIRS_16> = TestCircuit {
a,
b,
result: wrong_result,
};
check_unsatisfied_circuit(&prover, &circuit_unsatisfied, &mut rng, &msg);
// sanity check
let circuit_satisfied: TestCircuit<BIT_PAIRS_16> = 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::<TestCircuit<BIT_PAIRS_128>>(&pp, label)
.expect("Circuit should compile");

// Test sanity:
let a = -BlsScalar::one();
let b = BlsScalar::zero();
let result = -BlsScalar::one();
let circuit: TestCircuit<BIT_PAIRS_128> = 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<BIT_PAIRS_128> = 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<BIT_PAIRS_128> = TestCircuit {
a,
b,
result: wrong_result,
};
check_unsatisfied_circuit(&prover, &circuit, &mut rng, &msg);
// sanity check
let circuit_satisfied: TestCircuit<BIT_PAIRS_128> = TestCircuit {
a,
b,
result: right_result,
};
check_satisfied_circuit(
&prover,
&verifier,
&pi,
&circuit_satisfied,
&mut rng,
&"Sanity check should pass",
);
}
Loading