From 1fb7eeb03e8b62c0c85bae2d7b17cc83ef2d148d Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Fri, 3 Sep 2021 17:15:50 -0700 Subject: [PATCH] variable-sized tests --- src/gadgets/permutation.rs | 112 +++++++++++++++---------------------- src/gates/switch.rs | 18 +++--- 2 files changed, 54 insertions(+), 76 deletions(-) diff --git a/src/gadgets/permutation.rs b/src/gadgets/permutation.rs index fd96f43a..85db5419 100644 --- a/src/gadgets/permutation.rs +++ b/src/gadgets/permutation.rs @@ -394,79 +394,14 @@ mod tests { use crate::plonk::circuit_data::CircuitConfig; use crate::plonk::verifier::verify; - #[test] - fn test_permutation_2x2() -> Result<()> { + fn test_permutation_good(size: usize) -> Result<()> { type F = CrandallField; let config = CircuitConfig::large_zk_config(); let pw = PartialWitness::new(); let mut builder = CircuitBuilder::::new(config); - let one = F::ONE; - let two = F::from_canonical_usize(2); - let seven = F::from_canonical_usize(7); - let eight = F::from_canonical_usize(8); - - let one_two = vec![builder.constant(one), builder.constant(two)]; - let seven_eight = vec![builder.constant(seven), builder.constant(eight)]; - - let a = vec![one_two.clone(), seven_eight.clone()]; - let b = vec![seven_eight, one_two]; - - builder.assert_permutation(a, b); - - let data = builder.build(); - let proof = data.prove(pw).unwrap(); - - verify(proof, &data.verifier_only, &data.common) - } - - #[test] - fn test_permutation_4x4() -> Result<()> { - type F = CrandallField; - let config = CircuitConfig::large_zk_config(); - - let pw = PartialWitness::new(); - let mut builder = CircuitBuilder::::new(config); - - let one = F::ONE; - let two = F::from_canonical_usize(2); - let three = F::from_canonical_usize(3); - let four = F::from_canonical_usize(4); - let five = F::from_canonical_usize(5); - let six = F::from_canonical_usize(6); - let seven = F::from_canonical_usize(7); - let eight = F::from_canonical_usize(8); - - let one_two = vec![builder.constant(one), builder.constant(two)]; - let three_four = vec![builder.constant(three), builder.constant(four)]; - let five_six = vec![builder.constant(five), builder.constant(six)]; - let seven_eight = vec![builder.constant(seven), builder.constant(eight)]; - - let a = vec![ - one_two.clone(), - three_four.clone(), - five_six.clone(), - seven_eight.clone(), - ]; - let b = vec![seven_eight, one_two, five_six, three_four]; - - builder.assert_permutation(a, b); - - let data = builder.build(); - let proof = data.prove(pw).unwrap(); - - verify(proof, &data.verifier_only, &data.common) - } - - #[test] - fn test_permutation_6x6() -> Result<()> { - type F = CrandallField; - let config = CircuitConfig::large_config(); - let pw = PartialWitness::new(config.num_wires); - let mut builder = CircuitBuilder::::new(config); - - let lst: Vec = (0..12).map(|n| F::from_canonical_usize(n)).collect(); + let lst: Vec = (0..size * 2).map(|n| F::from_canonical_usize(n)).collect(); let a: Vec> = lst[..] .windows(2) .map(|pair| vec![builder.constant(pair[0]), builder.constant(pair[1])]) @@ -481,4 +416,47 @@ mod tests { verify(proof, &data.verifier_only, &data.common) } + + fn test_permutation_bad(size: usize) -> Result<()> { + type F = CrandallField; + let config = CircuitConfig::large_zk_config(); + + let pw = PartialWitness::new(); + let mut builder = CircuitBuilder::::new(config); + + let lst1: Vec = (0..size * 2).map(|_| F::rand()).collect(); + let lst2: Vec = (0..size * 2).map(|_| F::rand()).collect(); + let a: Vec> = lst1[..] + .windows(2) + .map(|pair| vec![builder.constant(pair[0]), builder.constant(pair[1])]) + .collect(); + let b: Vec> = lst2[..] + .windows(2) + .map(|pair| vec![builder.constant(pair[0]), builder.constant(pair[1])]) + .collect(); + + builder.assert_permutation(a, b); + + let data = builder.build(); + let proof = data.prove(pw).unwrap(); + + verify(proof, &data.verifier_only, &data.common) + } + + #[test] + fn test_permutations_good() -> Result<()> { + for n in 2..9 { + test_permutation_good(n).unwrap() + } + + Ok(()) + } + + #[test] + #[should_panic] + fn test_permutations_bad() -> () { + for n in 2..9 { + test_permutation_bad(n).unwrap() + } + } } diff --git a/src/gates/switch.rs b/src/gates/switch.rs index 51145b3c..e1657dc4 100644 --- a/src/gates/switch.rs +++ b/src/gates/switch.rs @@ -365,7 +365,7 @@ mod tests { type F = CrandallField; type FF = QuarticCrandallField; const D: usize = 4; - const chunk_size: usize = 4; + const CHUNK_SIZE: usize = 4; let num_copies = 3; /// Returns the local wires for a switch gate given the inputs and the switch booleans. @@ -382,11 +382,11 @@ mod tests { let switch = switch_bools[c]; switches.push(F::from_bool(switch)); - let mut first_input_chunk = Vec::with_capacity(chunk_size); - let mut second_input_chunk = Vec::with_capacity(chunk_size); - let mut first_output_chunk = Vec::with_capacity(chunk_size); - let mut second_output_chunk = Vec::with_capacity(chunk_size); - for e in 0..chunk_size { + let mut first_input_chunk = Vec::with_capacity(CHUNK_SIZE); + let mut second_input_chunk = Vec::with_capacity(CHUNK_SIZE); + let mut first_output_chunk = Vec::with_capacity(CHUNK_SIZE); + let mut second_output_chunk = Vec::with_capacity(CHUNK_SIZE); + for e in 0..CHUNK_SIZE { let first_input = first_inputs[c][e]; let second_input = second_inputs[c][e]; let first_output = if switch { second_input } else { first_input }; @@ -406,12 +406,12 @@ mod tests { v.iter().map(|&x| x.into()).collect::>() } - let first_inputs: Vec> = (0..num_copies).map(|_| F::rand_vec(chunk_size)).collect(); - let second_inputs: Vec> = (0..num_copies).map(|_| F::rand_vec(chunk_size)).collect(); + let first_inputs: Vec> = (0..num_copies).map(|_| F::rand_vec(CHUNK_SIZE)).collect(); + let second_inputs: Vec> = (0..num_copies).map(|_| F::rand_vec(CHUNK_SIZE)).collect(); let switch_bools = vec![true, false, true]; let gate = SwitchGate:: { - chunk_size, + chunk_size: CHUNK_SIZE, num_copies, _phantom: PhantomData, };