diff --git a/mpcs/src/basefold.rs b/mpcs/src/basefold.rs index 5c225c75a..be59f42c7 100644 --- a/mpcs/src/basefold.rs +++ b/mpcs/src/basefold.rs @@ -1191,108 +1191,53 @@ mod test { type PcsGoldilocksBaseCode = Basefold; #[test] - fn commit_open_verify_goldilocks_basecode_base() { - // Challenge is over extension field, poly over the base field - run_commit_open_verify::(true, 10, 11); - // Test trivial proof with small num vars - run_commit_open_verify::(true, 4, 6); - } - - #[test] - fn commit_open_verify_goldilocks_rscode_base() { - // Challenge is over extension field, poly over the base field - run_commit_open_verify::(true, 10, 11); - // Test trivial proof with small num vars - run_commit_open_verify::(true, 4, 6); - } - - #[test] - fn commit_open_verify_goldilocks_basecode_2() { - // Both challenge and poly are over extension field - run_commit_open_verify::(false, 10, 11); - // Test trivial proof with small num vars - run_commit_open_verify::(false, 4, 6); - } - - #[test] - fn commit_open_verify_goldilocks_rscode_2() { - // Both challenge and poly are over extension field - run_commit_open_verify::(false, 10, 11); - // Test trivial proof with small num vars - run_commit_open_verify::(false, 4, 6); - } - - #[test] - fn simple_batch_commit_open_verify_goldilocks_basecode_base() { - // Both challenge and poly are over base field - run_simple_batch_commit_open_verify::( - true, 10, 11, 1, - ); - run_simple_batch_commit_open_verify::( - true, 10, 11, 4, - ); - // Test trivial proof with small num vars - run_simple_batch_commit_open_verify::(true, 4, 6, 4); - } - - #[test] - fn simple_batch_commit_open_verify_goldilocks_rscode_base() { - // Both challenge and poly are over base field - run_simple_batch_commit_open_verify::(true, 10, 11, 1); - run_simple_batch_commit_open_verify::(true, 10, 11, 4); - // Test trivial proof with small num vars - run_simple_batch_commit_open_verify::(true, 4, 6, 4); - } - - #[test] - fn simple_batch_commit_open_verify_goldilocks_basecode_2() { - // Both challenge and poly are over extension field - run_simple_batch_commit_open_verify::( - false, 10, 11, 1, - ); - run_simple_batch_commit_open_verify::( - false, 10, 11, 4, - ); - // Test trivial proof with small num vars - run_simple_batch_commit_open_verify::( - false, 4, 6, 4, - ); - } - - #[test] - fn simple_batch_commit_open_verify_goldilocks_rscode_2() { - // Both challenge and poly are over extension field - run_simple_batch_commit_open_verify::( - false, 10, 11, 1, - ); - run_simple_batch_commit_open_verify::( - false, 10, 11, 4, - ); - // Test trivial proof with small num vars - run_simple_batch_commit_open_verify::(false, 4, 6, 4); - } - - #[test] - fn batch_commit_open_verify_goldilocks_basecode_base() { - // Both challenge and poly are over base field - run_batch_commit_open_verify::(true, 10, 11); - } - - #[test] - fn batch_commit_open_verify_goldilocks_rscode_base() { - // Both challenge and poly are over base field - run_batch_commit_open_verify::(true, 10, 11); + fn commit_open_verify_goldilocks() { + for base in [true, false] { + // Challenge is over extension field, poly over the base field + run_commit_open_verify::(base, 10, 11); + // Test trivial proof with small num vars + run_commit_open_verify::(base, 4, 6); + // Challenge is over extension field, poly over the base field + run_commit_open_verify::(base, 10, 11); + // Test trivial proof with small num vars + run_commit_open_verify::(base, 4, 6); + } } #[test] - fn batch_commit_open_verify_goldilocks_basecode_2() { - // Both challenge and poly are over extension field - run_batch_commit_open_verify::(false, 10, 11); + fn simple_batch_commit_open_verify_goldilocks() { + for base in [true, false] { + // Both challenge and poly are over base field + run_simple_batch_commit_open_verify::( + base, 10, 11, 1, + ); + run_simple_batch_commit_open_verify::( + base, 10, 11, 4, + ); + // Test trivial proof with small num vars + run_simple_batch_commit_open_verify::( + base, 4, 6, 4, + ); + // Both challenge and poly are over base field + run_simple_batch_commit_open_verify::( + base, 10, 11, 1, + ); + run_simple_batch_commit_open_verify::( + base, 10, 11, 4, + ); + // Test trivial proof with small num vars + run_simple_batch_commit_open_verify::( + base, 4, 6, 4, + ); + } } #[test] - fn batch_commit_open_verify_goldilocks_rscode_2() { - // Both challenge and poly are over extension field - run_batch_commit_open_verify::(false, 10, 11); + fn batch_commit_open_verify() { + for base in [true, false] { + // Both challenge and poly are over base field + run_batch_commit_open_verify::(base, 10, 11); + run_batch_commit_open_verify::(base, 10, 11); + } } } diff --git a/mpcs/src/lib.rs b/mpcs/src/lib.rs index 19b3d16b6..3bf1310f6 100644 --- a/mpcs/src/lib.rs +++ b/mpcs/src/lib.rs @@ -359,15 +359,79 @@ fn err_too_many_variates(function: &str, upto: usize, got: usize) -> Error { #[cfg(test)] pub mod test_util { - use crate::{Evaluation, PolynomialCommitmentScheme}; use ff_ext::ExtensionField; use itertools::{Itertools, chain}; - use multilinear_extensions::mle::{DenseMultilinearExtension, MultilinearExtension}; - use rand::{prelude::*, rngs::OsRng}; - use rand_chacha::ChaCha8Rng; + use multilinear_extensions::{ + mle::{DenseMultilinearExtension, MultilinearExtension}, + virtual_poly_v2::ArcMultilinearExtension, + }; + use rand::rngs::OsRng; use transcript::Transcript; + pub fn setup_pcs>( + num_vars: usize, + ) -> (Pcs::ProverParam, Pcs::VerifierParam) { + let poly_size = 1 << num_vars; + let param = Pcs::setup(poly_size).unwrap(); + Pcs::trim(param, poly_size).unwrap() + } + + pub fn gen_rand_poly( + num_vars: usize, + base: bool, + ) -> DenseMultilinearExtension { + if base { + DenseMultilinearExtension::random(num_vars, &mut OsRng) + } else { + DenseMultilinearExtension::from_evaluations_ext_vec( + num_vars, + (0..(1 << num_vars)) + .map(|_| E::random(&mut OsRng)) + .collect_vec(), + ) + } + } + + pub fn gen_rand_polys( + num_vars: impl Fn(usize) -> usize, + batch_size: usize, + base: bool, + ) -> Vec> { + (0..batch_size) + .map(|i| gen_rand_poly(num_vars(i), base)) + .collect_vec() + } + + pub fn get_point_from_challenge( + num_vars: usize, + transcript: &mut Transcript, + ) -> Vec { + (0..num_vars) + .map(|_| transcript.get_and_append_challenge(b"Point").elements) + .collect() + } + pub fn get_points_from_challenge( + num_vars: impl Fn(usize) -> usize, + num_points: usize, + transcript: &mut Transcript, + ) -> Vec> { + (0..num_points) + .map(|i| get_point_from_challenge(num_vars(i), transcript)) + .collect() + } + + pub fn commit_polys_individually>( + pp: &Pcs::ProverParam, + polys: &[DenseMultilinearExtension], + transcript: &mut Transcript, + ) -> Vec { + polys + .iter() + .map(|poly| Pcs::commit_and_write(pp, poly, transcript).unwrap()) + .collect_vec() + } + pub fn run_commit_open_verify( base: bool, num_vars_start: usize, @@ -376,30 +440,17 @@ pub mod test_util { Pcs: PolynomialCommitmentScheme, { for num_vars in num_vars_start..num_vars_end { - // Setup - let (pp, vp) = { - let poly_size = 1 << num_vars; - let param = Pcs::setup(poly_size).unwrap(); - Pcs::trim(param, poly_size).unwrap() - }; + let (pp, vp) = setup_pcs::(num_vars); + // Commit and open let (comm, eval, proof, challenge) = { let mut transcript = Transcript::new(b"BaseFold"); - let poly = if base { - DenseMultilinearExtension::random(num_vars, &mut OsRng) - } else { - DenseMultilinearExtension::from_evaluations_ext_vec( - num_vars, - (0..1 << num_vars).map(|_| E::random(&mut OsRng)).collect(), - ) - }; - + let poly = gen_rand_poly(num_vars, base); let comm = Pcs::commit_and_write(&pp, &poly, &mut transcript).unwrap(); - let point = (0..num_vars) - .map(|_| transcript.get_and_append_challenge(b"Point").elements) - .collect::>(); + let point = get_point_from_challenge(num_vars, &mut transcript); let eval = poly.evaluate(point.as_slice()); transcript.append_field_element_ext(&eval); + ( Pcs::get_pure_commitment(&comm), eval, @@ -408,21 +459,16 @@ pub mod test_util { ) }; // Verify - let result = { + { let mut transcript = Transcript::new(b"BaseFold"); Pcs::write_commitment(&comm, &mut transcript).unwrap(); - let point = (0..num_vars) - .map(|_| transcript.get_and_append_challenge(b"Point").elements) - .collect::>(); + let point = get_point_from_challenge(num_vars, &mut transcript); transcript.append_field_element_ext(&eval); - let result = Pcs::verify(&vp, &comm, &point, &eval, &proof, &mut transcript); + Pcs::verify(&vp, &comm, &point, &eval, &proof, &mut transcript).unwrap(); let v_challenge = transcript.read_challenge(); assert_eq!(challenge, v_challenge); - - result - }; - result.unwrap(); + } } } @@ -437,13 +483,8 @@ pub mod test_util { for num_vars in num_vars_start..num_vars_end { let batch_size = 2; let num_points = batch_size >> 1; - let rng = ChaCha8Rng::from_seed([0u8; 32]); - // Setup - let (pp, vp) = { - let poly_size = 1 << num_vars; - let param = Pcs::setup(poly_size).unwrap(); - Pcs::trim(param, poly_size).unwrap() - }; + let (pp, vp) = setup_pcs::(num_vars); + // Batch commit and open let evals = chain![ (0..num_points).map(|point| (point * 2, point)), // Every point matches two polys @@ -452,34 +493,15 @@ pub mod test_util { .unique() .collect_vec(); - let (comms, points, evals, proof, challenge) = { + let (comms, evals, proof, challenge) = { let mut transcript = Transcript::new(b"BaseFold"); - let polys = (0..batch_size) - .map(|i| { - if base { - DenseMultilinearExtension::random(num_vars - (i >> 1), &mut rng.clone()) - } else { - DenseMultilinearExtension::from_evaluations_ext_vec( - num_vars, - (0..1 << num_vars).map(|_| E::random(&mut OsRng)).collect(), - ) - } - }) - .collect_vec(); + let polys = gen_rand_polys(|i| num_vars - (i >> 1), batch_size, base); - let comms = polys - .iter() - .map(|poly| Pcs::commit_and_write(&pp, poly, &mut transcript).unwrap()) - .collect_vec(); + let comms = + commit_polys_individually::(&pp, polys.as_slice(), &mut transcript); - let points = (0..num_points) - .map(|i| { - (0..num_vars - i) - .map(|_| transcript.get_and_append_challenge(b"Point").elements) - .collect::>() - }) - .take(num_points) - .collect_vec(); + let points = + get_points_from_challenge(|i| num_vars - i, num_points, &mut transcript); let evals = evals .iter() @@ -499,10 +521,10 @@ pub mod test_util { let proof = Pcs::batch_open(&pp, &polys, &comms, &points, &evals, &mut transcript).unwrap(); - (comms, points, evals, proof, transcript.read_challenge()) + (comms, evals, proof, transcript.read_challenge()) }; // Batch verify - let result = { + { let mut transcript = Transcript::new(b"BaseFold"); let comms = comms .iter() @@ -513,16 +535,9 @@ pub mod test_util { }) .collect_vec(); - let old_points = points; - let points = (0..num_points) - .map(|i| { - (0..num_vars - i) - .map(|_| transcript.get_and_append_challenge(b"Point").elements) - .collect::>() - }) - .take(num_points) - .collect_vec(); - assert_eq!(points, old_points); + let points = + get_points_from_challenge(|i| num_vars - i, num_points, &mut transcript); + let values: Vec = evals .iter() .map(Evaluation::value) @@ -530,14 +545,10 @@ pub mod test_util { .collect::>(); transcript.append_field_element_exts(values.as_slice()); - let result = - Pcs::batch_verify(&vp, &comms, &points, &evals, &proof, &mut transcript); + Pcs::batch_verify(&vp, &comms, &points, &evals, &proof, &mut transcript).unwrap(); let v_challenge = transcript.read_challenge(); assert_eq!(challenge, v_challenge); - result - }; - - result.unwrap(); + } } } @@ -551,52 +562,24 @@ pub mod test_util { Pcs: PolynomialCommitmentScheme, { for num_vars in num_vars_start..num_vars_end { - let rng = ChaCha8Rng::from_seed([0u8; 32]); - // Setup - let (pp, vp) = { - let poly_size = 1 << num_vars; - let param = Pcs::setup(poly_size).unwrap(); - Pcs::trim(param, poly_size).unwrap() - }; + let (pp, vp) = setup_pcs::(num_vars); let (comm, evals, proof, challenge) = { let mut transcript = Transcript::new(b"BaseFold"); - let polys = (0..batch_size) - .map(|_| { - if base { - DenseMultilinearExtension::random(num_vars, &mut rng.clone()) - } else { - DenseMultilinearExtension::from_evaluations_ext_vec( - num_vars, - (0..1 << num_vars).map(|_| E::random(&mut OsRng)).collect(), - ) - } - }) - .collect_vec(); - let comm = Pcs::batch_commit_and_write(&pp, &polys, &mut transcript).unwrap(); - - let point = (0..num_vars) - .map(|_| transcript.get_and_append_challenge(b"Point").elements) - .collect::>(); + let polys = gen_rand_polys(|_| num_vars, batch_size, base); + let comm = + Pcs::batch_commit_and_write(&pp, polys.as_slice(), &mut transcript).unwrap(); + let point = get_point_from_challenge(num_vars, &mut transcript); + let evals = polys.iter().map(|poly| poly.evaluate(&point)).collect_vec(); + transcript.append_field_element_exts(&evals); - let evals = (0..batch_size) - .map(|i| polys[i].evaluate(&point)) + let polys = polys + .iter() + .map(|poly| ArcMultilinearExtension::from(poly.clone())) .collect_vec(); - - transcript.append_field_element_exts(&evals); - let proof = Pcs::simple_batch_open( - &pp, - polys - .into_iter() - .map(|x| x.into()) - .collect::>() - .as_slice(), - &comm, - &point, - &evals, - &mut transcript, - ) - .unwrap(); + let proof = + Pcs::simple_batch_open(&pp, &polys, &comm, &point, &evals, &mut transcript) + .unwrap(); ( Pcs::get_pure_commitment(&comm), evals, @@ -605,25 +588,19 @@ pub mod test_util { ) }; // Batch verify - let result = { + { let mut transcript = Transcript::new(b"BaseFold"); Pcs::write_commitment(&comm, &mut transcript).unwrap(); - let point = (0..num_vars) - .map(|_| transcript.get_and_append_challenge(b"Point").elements) - .collect::>(); - + let point = get_point_from_challenge(num_vars, &mut transcript); transcript.append_field_element_exts(&evals); - let result = - Pcs::simple_batch_verify(&vp, &comm, &point, &evals, &proof, &mut transcript); + Pcs::simple_batch_verify(&vp, &comm, &point, &evals, &proof, &mut transcript) + .unwrap(); let v_challenge = transcript.read_challenge(); assert_eq!(challenge, v_challenge); - result - }; - - result.unwrap(); + } } } }