From 477fe1ea4a5cb304078f9072922b26af1d68f535 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Thu, 6 May 2021 15:14:43 +0200 Subject: [PATCH] Minor fixes --- src/field/field.rs | 4 ++++ src/field/lagrange.rs | 10 +++++----- src/fri/mod.rs | 2 +- src/merkle_tree.rs | 4 +--- src/plonk_challenger.rs | 2 +- src/polynomial/commitment.rs | 17 +++++------------ src/polynomial/old_polynomial.rs | 18 +++++++++--------- src/polynomial/polynomial.rs | 2 +- src/proof.rs | 3 ++- 9 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/field/field.rs b/src/field/field.rs index 8497861f..0249610f 100644 --- a/src/field/field.rs +++ b/src/field/field.rs @@ -269,6 +269,10 @@ pub trait Field: fn rand() -> Self { Self::rand_from_rng(&mut OsRng) } + + fn rand_vec(n: usize) -> Vec { + (0..n).map(|_| Self::rand()).collect() + } } /// An iterator over the powers of a certain base element `b`: `b^0, b^1, b^2, ...`. diff --git a/src/field/lagrange.rs b/src/field/lagrange.rs index 7694faa2..8911feb0 100644 --- a/src/field/lagrange.rs +++ b/src/field/lagrange.rs @@ -77,8 +77,8 @@ mod tests { type F = CrandallField; for deg in 0..10 { - let domain = (0..deg).map(|_| F::rand()).collect::>(); - let coeffs = (0..deg).map(|_| F::rand()).collect(); + let domain = F::rand_vec(deg); + let coeffs = F::rand_vec(deg); let coeffs = PolynomialCoeffs { coeffs }; let points = eval_naive(&coeffs, &domain); @@ -94,7 +94,7 @@ mod tests { let deg = 1 << deg_log; let g = F::primitive_root_of_unity(deg_log); let domain = F::cyclic_subgroup_known_order(g, deg); - let coeffs = (0..deg).map(|_| F::rand()).collect(); + let coeffs = F::rand_vec(deg); let coeffs = PolynomialCoeffs { coeffs }; let points = eval_naive(&coeffs, &domain); @@ -108,8 +108,8 @@ mod tests { for deg in 0..10 { let points = deg + 5; - let domain = (0..points).map(|_| F::rand()).collect::>(); - let coeffs = (0..deg).map(|_| F::rand()).collect(); + let domain = F::rand_vec(points); + let coeffs = F::rand_vec(deg); let coeffs = PolynomialCoeffs { coeffs }; let points = eval_naive(&coeffs, &domain); diff --git a/src/fri/mod.rs b/src/fri/mod.rs index a822f5de..af931f28 100644 --- a/src/fri/mod.rs +++ b/src/fri/mod.rs @@ -72,7 +72,7 @@ mod tests { type F = CrandallField; let n = 1 << degree_log; - let coeffs = PolynomialCoeffs::new((0..n).map(|_| F::rand()).collect()).lde(rate_bits); + let coeffs = PolynomialCoeffs::new(F::rand_vec(n)).lde(rate_bits); let coset_lde = coeffs.clone().coset_fft(F::MULTIPLICATIVE_GROUP_GENERATOR); let config = FriConfig { num_query_rounds, diff --git a/src/merkle_tree.rs b/src/merkle_tree.rs index e7d535e0..db41b8a3 100644 --- a/src/merkle_tree.rs +++ b/src/merkle_tree.rs @@ -90,9 +90,7 @@ mod tests { use super::*; fn random_data(n: usize, k: usize) -> Vec> { - (0..n) - .map(|_| (0..k).map(|_| F::rand()).collect()) - .collect() + (0..n).map(|_| F::rand_vec(k)).collect() } fn verify_all_leaves( diff --git a/src/plonk_challenger.rs b/src/plonk_challenger.rs index 05eaea16..0354eda2 100644 --- a/src/plonk_challenger.rs +++ b/src/plonk_challenger.rs @@ -258,7 +258,7 @@ mod tests { // Generate random input messages. let inputs_per_round: Vec> = num_inputs_per_round .iter() - .map(|&n| (0..n).map(|_| F::rand()).collect::>()) + .map(|&n| F::rand_vec(n)) .collect(); let mut challenger = Challenger::new(); diff --git a/src/polynomial/commitment.rs b/src/polynomial/commitment.rs index 0d1e14a3..40012383 100644 --- a/src/polynomial/commitment.rs +++ b/src/polynomial/commitment.rs @@ -32,11 +32,7 @@ impl ListPolynomialCommitment { .chain(if fri_config.blinding { // If blinding, salt with two random elements to each leaf vector. (0..2) - .map(|_| { - (0..(degree << fri_config.rate_bits)) - .map(|_| F::rand()) - .collect() - }) + .map(|_| F::rand_vec(degree << fri_config.rate_bits)) .collect() } else { Vec::new() @@ -150,6 +146,7 @@ impl ListPolynomialCommitment { pub struct OpeningProof { merkle_root: Hash, fri_proof: FriProof, + // TODO: Get the degree from `CommonCircuitData` instead. quotient_degree: usize, } @@ -197,10 +194,6 @@ mod tests { use crate::field::crandall_field::CrandallField; use anyhow::Result; - fn rand_vec(n: usize) -> Vec { - (0..n).map(|_| F::rand()).collect() - } - fn gen_random_test_case( k: usize, degree_log: usize, @@ -209,11 +202,11 @@ mod tests { let degree = 1 << degree_log; let polys = (0..k) - .map(|_| PolynomialCoeffs::new(rand_vec(degree))) + .map(|_| PolynomialCoeffs::new(F::rand_vec(degree))) .collect(); - let mut points = rand_vec::(num_points); + let mut points = F::rand_vec(num_points); while points.iter().any(|&x| x.exp_usize(degree).is_one()) { - points = rand_vec(num_points); + points = F::rand_vec(num_points); } (polys, points) diff --git a/src/polynomial/old_polynomial.rs b/src/polynomial/old_polynomial.rs index dff12e2b..2e3dfc04 100644 --- a/src/polynomial/old_polynomial.rs +++ b/src/polynomial/old_polynomial.rs @@ -417,8 +417,8 @@ mod test { type F = CrandallField; let mut rng = thread_rng(); let (a_deg, b_deg) = (rng.gen_range(1, 10_000), rng.gen_range(1, 10_000)); - let a = Polynomial((0..a_deg).map(|_| F::rand()).collect()); - let b = Polynomial((0..b_deg).map(|_| F::rand()).collect()); + let a = Polynomial(F::rand_vec(a_deg)); + let b = Polynomial(F::rand_vec(b_deg)); let m1 = a.mul(&b); let m2 = a.mul(&b); for _ in 0..1000 { @@ -434,7 +434,7 @@ mod test { let mut rng = thread_rng(); let a_deg = rng.gen_range(1, 1_000); let n = rng.gen_range(1, 1_000); - let a = Polynomial((0..a_deg).map(|_| F::rand()).collect()); + let a = Polynomial(F::rand_vec(a_deg)); let b = a.inv_mod_xn(n); let mut m = a.mul(&b); m.drain(n..); @@ -455,8 +455,8 @@ mod test { type F = CrandallField; let mut rng = thread_rng(); let (a_deg, b_deg) = (rng.gen_range(1, 10_000), rng.gen_range(1, 10_000)); - let a = Polynomial((0..a_deg).map(|_| F::rand()).collect()); - let b = Polynomial((0..b_deg).map(|_| F::rand()).collect()); + let a = Polynomial(F::rand_vec(a_deg)); + let b = Polynomial(F::rand_vec(b_deg)); let (q, r) = a.polynomial_long_division(&b); for _ in 0..1000 { let x = F::rand(); @@ -469,8 +469,8 @@ mod test { type F = CrandallField; let mut rng = thread_rng(); let (a_deg, b_deg) = (rng.gen_range(1, 10_000), rng.gen_range(1, 10_000)); - let a = Polynomial((0..a_deg).map(|_| F::rand()).collect()); - let b = Polynomial((0..b_deg).map(|_| F::rand()).collect()); + let a = Polynomial(F::rand_vec(a_deg)); + let b = Polynomial(F::rand_vec(b_deg)); let (q, r) = a.polynomial_division(&b); for _ in 0..1000 { let x = F::rand(); @@ -483,7 +483,7 @@ mod test { type F = CrandallField; let mut rng = thread_rng(); let a_deg = rng.gen_range(1, 10_000); - let a = Polynomial((0..a_deg).map(|_| F::rand()).collect()); + let a = Polynomial(F::rand_vec(a_deg)); let b = Polynomial::from(vec![F::rand()]); let (q, r) = a.polynomial_division(&b); for _ in 0..1000 { @@ -498,7 +498,7 @@ mod test { let mut rng = thread_rng(); let a_deg = rng.gen_range(1, 10_000); let n = rng.gen_range(1, a_deg); - let mut a = Polynomial((0..a_deg).map(|_| F::rand()).collect()); + let mut a = Polynomial(F::rand_vec(a_deg)); a.trim(); let z_h = { let mut z_h_vec = vec![F::ZERO; n + 1]; diff --git a/src/polynomial/polynomial.rs b/src/polynomial/polynomial.rs index 5dedd1bb..113a6d2a 100644 --- a/src/polynomial/polynomial.rs +++ b/src/polynomial/polynomial.rs @@ -148,7 +148,7 @@ mod tests { let k = 8; let n = 1 << k; - let poly = PolynomialCoeffs::new((0..n).map(|_| F::rand()).collect()); + let poly = PolynomialCoeffs::new(F::rand_vec(n)); let shift = F::rand(); let coset_evals = poly.clone().coset_fft(shift).values; diff --git a/src/proof.rs b/src/proof.rs index cfdaaccb..64bc339a 100644 --- a/src/proof.rs +++ b/src/proof.rs @@ -90,7 +90,8 @@ pub struct FriQueryStep { pub merkle_proof: MerkleProof, } -/// Evaluations and Merkle proof produced by the prover in a FRI query step. +/// Evaluations and Merkle proofs of the original set of polynomials, +/// before they are combined into a composition polynomial. // TODO: Implement FriInitialTreeProofTarget pub struct FriInitialTreeProof { pub evals_proofs: Vec<(Vec, MerkleProof)>,