Skip to content

Commit

Permalink
Pass a rng instance in prover
Browse files Browse the repository at this point in the history
It makes the tests reproducible
  • Loading branch information
dannywillems committed Feb 26, 2024
1 parent 0298a54 commit 890a527
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
25 changes: 15 additions & 10 deletions msm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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,
Expand All @@ -53,13 +52,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::<Fp>::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<BN254> = PairingSRS::create(x, domain.d1.size as usize);
srs.full_srs.add_lagrange_basis(domain.d1);
Expand All @@ -68,11 +69,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
Expand All @@ -82,32 +84,36 @@ 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::<Fp>::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<BN254> = PairingSRS::create(x, domain.d1.size as usize);
srs.full_srs.add_lagrange_basis(domain.d1);

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.
Expand Down Expand Up @@ -146,9 +152,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;
Expand All @@ -173,11 +177,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.
Expand Down
6 changes: 5 additions & 1 deletion msm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub fn generate_random_msm_witness() -> BuilderEnv<BN254G1Affine> {
}

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::<Fp>::create(DOMAIN_SIZE).unwrap();

Expand All @@ -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");
Expand Down
6 changes: 5 additions & 1 deletion msm/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use poly_commitment::{
evaluation_proof::DensePolynomialOrEvaluations,
OpenProof, SRS,
};
use rand::{CryptoRng, RngCore};
use rayon::iter::IntoParallelIterator;
use rayon::iter::ParallelIterator;

Expand All @@ -24,14 +25,17 @@ pub fn prove<
EFqSponge: Clone + FqSponge<G::BaseField, G, G::ScalarField>,
EFrSponge: FrSponge<G::ScalarField>,
Column,
RNG,
>(
domain: EvaluationDomains<G::ScalarField>,
srs: &OpeningProof::SRS,
inputs: Witness<G>,
_constraints: Vec<Expr<ConstantExpr<G::ScalarField>, Column>>,
rng: &mut RNG,
) -> Proof<G, OpeningProof>
where
OpeningProof::SRS: Sync,
RNG: RngCore + CryptoRng,
{
// Interpolate all columns on d1, using trait Into.
let polys: WitnessColumns<DensePolynomial<G::ScalarField>> = {
Expand Down Expand Up @@ -219,7 +223,7 @@ where
v,
u,
fq_sponge_before_evaluations,
&mut rand::rngs::OsRng,
rng,
);
// -- End opening proof - Preparing the structures

Expand Down

0 comments on commit 890a527

Please sign in to comment.