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

113 pifac tests #459

Merged
merged 16 commits into from
Aug 16, 2023
70 changes: 56 additions & 14 deletions src/zkp/pifac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,21 @@ mod tests {
&mut rng2,
)?;

assert!(mixed_proof
.verify(mixed_input, &(), &mut transcript())
.is_err());

let regular_sized_p = prime_gen::try_get_prime_from_pool_insecure(&mut rng2).unwrap();
let modulus = &regular_sized_p * &small_q;
let mixed_input = CommonInput::new(&setup_params, &modulus);
let mixed_proof = PiFacProof::prove(
input,
ProverSecret::new(&regular_sized_p, &small_q),
&(),
&mut transcript(),
&mut rng2,
)?;

assert!(mixed_proof
.verify(mixed_input, &(), &mut transcript())
.is_err());
Expand All @@ -467,23 +482,37 @@ mod tests {
with_random_no_small_factors_proof(&mut rng, correct_factors)
}

/// TODO: This test does not currently work. It should be addressed as part
/// of issue #113.
// #[test]
#[allow(dead_code)]
fn test_modulus_cannot_have_small_factors() -> Result<()> {
#[test]
fn test_modulus_cannot_have_large_factors() -> Result<()> {
marsella marked this conversation as resolved.
Show resolved Hide resolved
let mut rng = init_testing();
let (not_p0, _) = prime_gen::get_prime_pair_from_pool_insecure(&mut rng).unwrap();
let regular_sized_q = prime_gen::try_get_prime_from_pool_insecure(&mut rng).unwrap();
let (regular_sized_p, regular_sized_q) =
prime_gen::get_prime_pair_from_pool_insecure(&mut rng).unwrap();
let setup_params = VerifiedRingPedersen::gen(&mut rng, &())?;

let small_fac_p = &not_p0 * &BigNumber::from(2u64);
let modulus = &small_fac_p * &regular_sized_q;
let small_fac_p = BigNumber::from(2u64);
let small_fac_q = BigNumber::from(2u64);
let big_q = &regular_sized_p * &regular_sized_q;
let big_p = big_q.clone();
let modulus = &small_fac_p * &big_q;

let small_fac_input = CommonInput::new(&setup_params, &modulus);
let small_fac_proof = PiFacProof::prove(
small_fac_input,
ProverSecret::new(&small_fac_p, &big_q),
&(),
&mut transcript(),
&mut rng,
)?;

assert!(small_fac_proof
.verify(small_fac_input, &(), &mut transcript())
.is_err());

let modulus = &big_p * &small_fac_q;
let small_fac_input = CommonInput::new(&setup_params, &modulus);
let small_fac_proof = PiFacProof::prove(
small_fac_input,
ProverSecret::new(&small_fac_p, &regular_sized_q),
ProverSecret::new(&big_p, &small_fac_q),
&(),
&mut transcript(),
&mut rng,
Expand All @@ -492,17 +521,18 @@ mod tests {
assert!(small_fac_proof
.verify(small_fac_input, &(), &mut transcript())
.is_err());

Ok(())
}

#[test]
fn proof_elements_should_be_correct() -> Result<()> {
marsella marked this conversation as resolved.
Show resolved Hide resolved
let mut rng = init_testing();
let (p0, q0) = prime_gen::get_prime_pair_from_pool_insecure(&mut rng).unwrap();
let N0 = &p0 * &q0;
let modulus = &p0 * &q0;
let setup_params = VerifiedRingPedersen::gen(&mut rng, &())?;

let input = CommonInput::new(&setup_params, &N0);
let input = CommonInput::new(&setup_params, &modulus);
let proof = PiFacProof::prove(
input,
ProverSecret::new(&p0, &q0),
Expand All @@ -517,33 +547,42 @@ mod tests {
.verify(input, &(), &mut transcript())
.is_err());
let mut incorrect_proof = proof.clone();

incorrect_proof.q_masked = random_bignumber.clone();
assert!(incorrect_proof
.verify(input, &(), &mut transcript())
.is_err());

let mut incorrect_proof = proof.clone();
let scheme = VerifiedRingPedersen::gen(&mut rng, &())?;
let (random_commitment, random_commmitment_randomness) =
scheme.scheme().commit(&random_bignumber, 256, &mut rng);
let (random_commitment, _) = scheme.scheme().commit(&random_bignumber, ELL, &mut rng);
incorrect_proof.p_commitment = random_commitment.clone();
assert!(incorrect_proof
.verify(input, &(), &mut transcript())
.is_err());

let mut incorrect_proof = proof.clone();
incorrect_proof.q_commitment = random_commitment.clone();
assert!(incorrect_proof
.verify(input, &(), &mut transcript())
.is_err());

let mut incorrect_proof = proof.clone();
let (random_commitment, random_commmitment_randomness) =
scheme
.scheme()
.commit(&random_bignumber, ELL + EPSILON, &mut rng);
incorrect_proof.p_mask_commitment = random_commitment.clone();
marsella marked this conversation as resolved.
Show resolved Hide resolved
assert!(incorrect_proof
.verify(input, &(), &mut transcript())
.is_err());

let mut incorrect_proof = proof.clone();
incorrect_proof.q_mask_commitment = random_commitment.clone();
assert!(incorrect_proof
.verify(input, &(), &mut transcript())
.is_err());

let mut incorrect_proof = proof.clone();
incorrect_proof.q_link_commitment = random_commitment.clone();
marsella marked this conversation as resolved.
Show resolved Hide resolved
assert!(incorrect_proof
Expand All @@ -554,17 +593,20 @@ mod tests {
assert!(incorrect_proof
.verify(input, &(), &mut transcript())
.is_err());

let mut incorrect_proof = proof.clone();
let random_masked_randomness = random_commmitment_randomness.as_masked();
incorrect_proof.masked_p_commitment_randomness = random_masked_randomness.clone();
assert!(incorrect_proof
.verify(input, &(), &mut transcript())
.is_err());

let mut incorrect_proof = proof.clone();
incorrect_proof.masked_q_commitment_randomness = random_masked_randomness.clone();
assert!(incorrect_proof
.verify(input, &(), &mut transcript())
.is_err());

let mut incorrect_proof = proof.clone();
incorrect_proof.masked_p_link = random_masked_randomness.clone();
assert!(incorrect_proof
Expand Down