From 63b45011f13a43928bedab90e195fc6285956da4 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Mon, 26 Feb 2024 21:28:04 +0100 Subject: [PATCH] Pass a rng instance in prover It makes the tests reproducible --- msm/src/lib.rs | 25 +++++++++++++++---------- msm/src/main.rs | 6 +++++- msm/src/prover.rs | 6 +++++- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/msm/src/lib.rs b/msm/src/lib.rs index bf785be2f8..4ea688b749 100644 --- a/msm/src/lib.rs +++ b/msm/src/lib.rs @@ -45,7 +45,6 @@ mod tests { use ark_ff::UniformRand; use kimchi::circuits::domains::EvaluationDomains; use poly_commitment::pairing_proof::PairingSRS; - use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng}; use crate::{ columns::Column, mvlookup::Lookup, proof::Witness, prover::prove, verifier::verify, @@ -54,13 +53,15 @@ mod tests { #[test] fn test_completeness() { + let mut rng = o1_utils::tests::make_test_rng(); + // Include tests for completeness for MVLookup as the random witness // includes all arguments let domain_size = 1 << 8; let domain = EvaluationDomains::::create(domain_size).unwrap(); // Trusted setup toxic waste - let x = Fp::rand(&mut rand::rngs::OsRng); + let x = Fp::rand(&mut rng); let mut srs: PairingSRS = PairingSRS::create(x, domain.d1.size as usize); srs.full_srs.add_lagrange_basis(domain.d1); @@ -69,11 +70,12 @@ mod tests { let constraints: Vec<_> = vec![]; // generate the proof - let proof = prove::<_, OpeningProof, BaseSponge, ScalarSponge, Column>( + let proof = prove::<_, OpeningProof, BaseSponge, ScalarSponge, Column, _>( domain, &srs, witness, constraints, + &mut rng, ); // verify the proof @@ -83,12 +85,14 @@ mod tests { #[test] fn test_soundness() { + let mut rng = o1_utils::tests::make_test_rng(); + // We generate two different witness and two different proofs. let domain_size = 1 << 8; let domain = EvaluationDomains::::create(domain_size).unwrap(); // Trusted setup toxic waste - let x = Fp::rand(&mut rand::rngs::OsRng); + let x = Fp::rand(&mut rng); let mut srs: PairingSRS = PairingSRS::create(x, domain.d1.size as usize); srs.full_srs.add_lagrange_basis(domain.d1); @@ -96,19 +100,21 @@ mod tests { let witness = Witness::random(domain); let constraints = vec![]; // generate the proof - let proof = prove::<_, OpeningProof, BaseSponge, ScalarSponge, Column>( + let proof = prove::<_, OpeningProof, BaseSponge, ScalarSponge, Column, _>( domain, &srs, witness, constraints.clone(), + &mut rng, ); let witness_prime = Witness::random(domain); - let proof_prime = prove::<_, OpeningProof, BaseSponge, ScalarSponge, Column>( + let proof_prime = prove::<_, OpeningProof, BaseSponge, ScalarSponge, Column, _>( domain, &srs, witness_prime, constraints, + &mut rng, ); // Swap the opening proof. The verification should fail. @@ -147,9 +153,7 @@ mod tests { #[test] #[ignore] fn test_soundness_mvlookup() { - let seed: [u8; 32] = thread_rng().gen(); - eprintln!("Seed: {:?}", seed); - let mut rng = StdRng::from_seed(seed); + let mut rng = o1_utils::tests::make_test_rng(); // We generate two different witness and two different proofs. let domain_size = 1 << 8; @@ -174,11 +178,12 @@ mod tests { // Overwriting the first looked up value witness.mvlookups[0].f[0][0] = wrong_looked_up_value; // generate the proof - let proof = prove::<_, OpeningProof, BaseSponge, ScalarSponge, Column>( + let proof = prove::<_, OpeningProof, BaseSponge, ScalarSponge, Column, _>( domain, &srs, witness, constraints, + &mut rng, ); let verifies = verify::<_, OpeningProof, BaseSponge, ScalarSponge>(domain, &srs, &proof); // FIXME: At the moment, it does verify. It should not. We are missing constraints. diff --git a/msm/src/main.rs b/msm/src/main.rs index b889de78ad..fa8cba0c70 100644 --- a/msm/src/main.rs +++ b/msm/src/main.rs @@ -30,6 +30,9 @@ pub fn generate_random_msm_witness() -> BuilderEnv { } pub fn main() { + // FIXME: use a proper RNG + let mut rng = o1_utils::tests::make_test_rng(); + println!("Creating the domain and SRS"); let domain = EvaluationDomains::::create(DOMAIN_SIZE).unwrap(); @@ -40,11 +43,12 @@ pub fn main() { println!("Generating the proof"); let constraints = vec![]; - let proof = prove::<_, OpeningProof, BaseSponge, ScalarSponge, Column>( + let proof = prove::<_, OpeningProof, BaseSponge, ScalarSponge, Column, _>( domain, &srs, witness, constraints, + &mut rng, ); println!("Verifying the proof"); diff --git a/msm/src/prover.rs b/msm/src/prover.rs index 49b9a886e9..59e9186edd 100644 --- a/msm/src/prover.rs +++ b/msm/src/prover.rs @@ -12,6 +12,7 @@ use poly_commitment::{ evaluation_proof::DensePolynomialOrEvaluations, OpenProof, SRS, }; +use rand::{CryptoRng, RngCore}; use rayon::iter::IntoParallelIterator; use rayon::iter::ParallelIterator; @@ -24,14 +25,17 @@ pub fn prove< EFqSponge: Clone + FqSponge, EFrSponge: FrSponge, Column, + RNG, >( domain: EvaluationDomains, srs: &OpeningProof::SRS, inputs: Witness, _constraints: Vec, Column>>, + rng: &mut RNG, ) -> Proof where OpeningProof::SRS: Sync, + RNG: RngCore + CryptoRng, { // Interpolate all columns on d1, using trait Into. let evaluations: WitnessColumns>> = inputs @@ -224,7 +228,7 @@ where v, u, fq_sponge_before_evaluations, - &mut rand::rngs::OsRng, + rng, ); // -- End opening proof - Preparing the structures