From 648ea51ff4892eb59f7ba71e35824c1414acb3ee Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Fri, 16 Dec 2022 09:18:53 -0500 Subject: [PATCH 1/8] arc --- arithmetic/src/multilinear_polynomial.rs | 26 +++++++------- arithmetic/src/virtual_polynomial.rs | 28 +++++++-------- hyperplonk/src/selectors.rs | 4 +-- hyperplonk/src/snark.rs | 27 ++++++++------- hyperplonk/src/structs.rs | 6 ++-- hyperplonk/src/utils.rs | 16 ++++----- hyperplonk/src/witness.rs | 4 +-- subroutines/benches/iop_bench.rs | 8 ++--- subroutines/benches/pcs_bench.rs | 4 +-- subroutines/src/pcs/mod.rs | 10 ++---- .../src/pcs/multilinear_kzg/batching.rs | 16 ++++----- subroutines/src/pcs/multilinear_kzg/mod.rs | 10 +++--- subroutines/src/poly_iop/perm_check/mod.rs | 34 +++++++++---------- subroutines/src/poly_iop/perm_check/util.rs | 16 ++++----- subroutines/src/poly_iop/prod_check/mod.rs | 26 +++++++------- subroutines/src/poly_iop/prod_check/util.rs | 28 +++++++-------- subroutines/src/poly_iop/sum_check/mod.rs | 8 ++--- subroutines/src/poly_iop/sum_check/prover.rs | 9 +++-- 18 files changed, 140 insertions(+), 140 deletions(-) diff --git a/arithmetic/src/multilinear_polynomial.rs b/arithmetic/src/multilinear_polynomial.rs index 3b7a4be..e7d569c 100644 --- a/arithmetic/src/multilinear_polynomial.rs +++ b/arithmetic/src/multilinear_polynomial.rs @@ -4,19 +4,19 @@ use ark_poly::MultilinearExtension; use ark_std::{end_timer, rand::RngCore, start_timer}; #[cfg(feature = "parallel")] use rayon::prelude::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator}; -use std::rc::Rc; +use std::sync::Arc; pub use ark_poly::DenseMultilinearExtension; /// Sample a random list of multilinear polynomials. /// Returns /// - the list of polynomials, -/// - its sum of polynomial evaluations over the boolean hypercube. +/// - its sum of polynomial evaluations over the boolean hypeArcube. pub fn random_mle_list( nv: usize, degree: usize, rng: &mut R, -) -> (Vec>>, F) { +) -> (Vec>>, F) { let start = start_timer!(|| "sample random mle list"); let mut multiplicands = Vec::with_capacity(degree); for _ in 0..degree { @@ -37,7 +37,7 @@ pub fn random_mle_list( let list = multiplicands .into_iter() - .map(|x| Rc::new(DenseMultilinearExtension::from_evaluations_vec(nv, x))) + .map(|x| Arc::new(DenseMultilinearExtension::from_evaluations_vec(nv, x))) .collect(); end_timer!(start); @@ -49,7 +49,7 @@ pub fn random_zero_mle_list( nv: usize, degree: usize, rng: &mut R, -) -> Vec>> { +) -> Vec>> { let start = start_timer!(|| "sample random zero mle list"); let mut multiplicands = Vec::with_capacity(degree); @@ -65,7 +65,7 @@ pub fn random_zero_mle_list( let list = multiplicands .into_iter() - .map(|x| Rc::new(DenseMultilinearExtension::from_evaluations_vec(nv, x))) + .map(|x| Arc::new(DenseMultilinearExtension::from_evaluations_vec(nv, x))) .collect(); end_timer!(start); @@ -81,12 +81,12 @@ pub fn identity_permutation(num_vars: usize, num_chunks: usize) - pub fn identity_permutation_mles( num_vars: usize, num_chunks: usize, -) -> Vec>> { +) -> Vec>> { let mut res = vec![]; for i in 0..num_chunks { let shift = (i * (1 << num_vars)) as u64; let s_id_vec = (shift..shift + (1u64 << num_vars)).map(F::from).collect(); - res.push(Rc::new(DenseMultilinearExtension::from_evaluations_vec( + res.push(Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_vars, s_id_vec, ))); } @@ -113,12 +113,12 @@ pub fn random_permutation_mles( num_vars: usize, num_chunks: usize, rng: &mut R, -) -> Vec>> { +) -> Vec>> { let s_perm_vec = random_permutation(num_vars, num_chunks, rng); let mut res = vec![]; let n = 1 << num_vars; for i in 0..num_chunks { - res.push(Rc::new(DenseMultilinearExtension::from_evaluations_vec( + res.push(Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_vars, s_perm_vec[i * n..i * n + n].to_vec(), ))); @@ -203,8 +203,8 @@ fn fix_variables_no_par( /// merge a set of polynomials. Returns an error if the /// polynomials do not share a same number of nvs. pub fn merge_polynomials( - polynomials: &[Rc>], -) -> Result>, ArithErrors> { + polynomials: &[Arc>], +) -> Result>, ArithErrors> { let nv = polynomials[0].num_vars(); for poly in polynomials.iter() { if nv != poly.num_vars() { @@ -220,7 +220,7 @@ pub fn merge_polynomials( scalars.extend_from_slice(poly.to_evaluations().as_slice()); } scalars.extend_from_slice(vec![F::zero(); (1 << merged_nv) - scalars.len()].as_ref()); - Ok(Rc::new(DenseMultilinearExtension::from_evaluations_vec( + Ok(Arc::new(DenseMultilinearExtension::from_evaluations_vec( merged_nv, scalars, ))) } diff --git a/arithmetic/src/virtual_polynomial.rs b/arithmetic/src/virtual_polynomial.rs index 8094337..7c64c44 100644 --- a/arithmetic/src/virtual_polynomial.rs +++ b/arithmetic/src/virtual_polynomial.rs @@ -11,7 +11,7 @@ use ark_std::{ start_timer, }; use rayon::prelude::*; -use std::{cmp::max, collections::HashMap, marker::PhantomData, ops::Add, rc::Rc}; +use std::{cmp::max, collections::HashMap, marker::PhantomData, ops::Add, sync::Arc}; #[rustfmt::skip] /// A virtual polynomial is a sum of products of multilinear polynomials; @@ -48,7 +48,7 @@ pub struct VirtualPolynomial { pub products: Vec<(F, Vec)>, /// Stores multilinear extensions in which product multiplicand can refer /// to. - pub flattened_ml_extensions: Vec>>, + pub flattened_ml_extensions: Vec>>, /// Pointers to the above poly extensions raw_pointers_lookup_table: HashMap<*const DenseMultilinearExtension, usize>, } @@ -71,7 +71,7 @@ impl Add for &VirtualPolynomial { let start = start_timer!(|| "virtual poly add"); let mut res = self.clone(); for products in other.products.iter() { - let cur: Vec>> = products + let cur: Vec>> = products .1 .iter() .map(|&x| other.flattened_ml_extensions[x].clone()) @@ -102,8 +102,8 @@ impl VirtualPolynomial { } /// Creates an new virtual polynomial from a MLE and its coefficient. - pub fn new_from_mle(mle: &Rc>, coefficient: F) -> Self { - let mle_ptr: *const DenseMultilinearExtension = Rc::as_ptr(mle); + pub fn new_from_mle(mle: &Arc>, coefficient: F) -> Self { + let mle_ptr: *const DenseMultilinearExtension = Arc::as_ptr(mle); let mut hm = HashMap::new(); hm.insert(mle_ptr, 0); @@ -129,10 +129,10 @@ impl VirtualPolynomial { /// `coefficient`. pub fn add_mle_list( &mut self, - mle_list: impl IntoIterator>>, + mle_list: impl IntoIterator>>, coefficient: F, ) -> Result<(), ArithErrors> { - let mle_list: Vec>> = mle_list.into_iter().collect(); + let mle_list: Vec>> = mle_list.into_iter().collect(); let mut indexed_product = Vec::with_capacity(mle_list.len()); if mle_list.is_empty() { @@ -151,7 +151,7 @@ impl VirtualPolynomial { ))); } - let mle_ptr: *const DenseMultilinearExtension = Rc::as_ptr(&mle); + let mle_ptr: *const DenseMultilinearExtension = Arc::as_ptr(&mle); if let Some(index) = self.raw_pointers_lookup_table.get(&mle_ptr) { indexed_product.push(*index) } else { @@ -171,7 +171,7 @@ impl VirtualPolynomial { /// Returns an error if the MLE has a different `num_vars` from self. pub fn mul_by_mle( &mut self, - mle: Rc>, + mle: Arc>, coefficient: F, ) -> Result<(), ArithErrors> { let start = start_timer!(|| "mul by mle"); @@ -183,7 +183,7 @@ impl VirtualPolynomial { ))); } - let mle_ptr: *const DenseMultilinearExtension = Rc::as_ptr(&mle); + let mle_ptr: *const DenseMultilinearExtension = Arc::as_ptr(&mle); // check if this mle already exists in the virtual polynomial let mle_index = match self.raw_pointers_lookup_table.get(&mle_ptr) { @@ -350,11 +350,11 @@ pub fn eq_eval(x: &[F], y: &[F]) -> Result { /// eq(x,y) = \prod_i=1^num_var (x_i * r_i + (1-x_i)*(1-r_i)) pub fn build_eq_x_r( r: &[F], -) -> Result>, ArithErrors> { +) -> Result>, ArithErrors> { let evals = build_eq_x_r_vec(r)?; let mle = DenseMultilinearExtension::from_evaluations_vec(r.len(), evals); - Ok(Rc::new(mle)) + Ok(Arc::new(mle)) } /// This function build the eq(x, r) polynomial for any given r, and output the /// evaluation of eq(x, r) in its vector form. @@ -511,7 +511,7 @@ mod test { // eq(x,y) = \prod_i=1^num_var (x_i * y_i + (1-x_i)*(1-y_i)) // over r, which is // eq(x,y) = \prod_i=1^num_var (x_i * r_i + (1-x_i)*(1-r_i)) - fn build_eq_x_r_for_test(r: &[F]) -> Rc> { + fn build_eq_x_r_for_test(r: &[F]) -> Arc> { let start = start_timer!(|| "zero check naive build eq_x_r"); // we build eq(x,r) from its evaluations @@ -545,7 +545,7 @@ mod test { let mle = DenseMultilinearExtension::from_evaluations_vec(num_var, eval); - let res = Rc::new(mle); + let res = Arc::new(mle); end_timer!(start); res } diff --git a/hyperplonk/src/selectors.rs b/hyperplonk/src/selectors.rs index eb6c68b..ec10cec 100644 --- a/hyperplonk/src/selectors.rs +++ b/hyperplonk/src/selectors.rs @@ -2,7 +2,7 @@ use crate::{build_mle, errors::HyperPlonkErrors}; use ark_ff::PrimeField; use ark_poly::DenseMultilinearExtension; use ark_std::log2; -use std::rc::Rc; +use std::sync::Arc; /// A row of selector of width `#selectors` #[derive(Debug, Clone, PartialEq, Eq)] @@ -67,7 +67,7 @@ impl SelectorRow { /// output mle(a1,b1,c1, ...), mle(a2,b2,c2, ...), ... pub fn build_mles( matrix: &[Self], - ) -> Result>>, HyperPlonkErrors> { + ) -> Result>>, HyperPlonkErrors> { build_mle!(matrix) } } diff --git a/hyperplonk/src/snark.rs b/hyperplonk/src/snark.rs index ce685f3..816683c 100644 --- a/hyperplonk/src/snark.rs +++ b/hyperplonk/src/snark.rs @@ -9,7 +9,10 @@ use arithmetic::{evaluate_opt, gen_eval_point, VPAuxInfo}; use ark_ec::PairingEngine; use ark_poly::DenseMultilinearExtension; use ark_std::{end_timer, log2, start_timer, One, Zero}; -use std::{marker::PhantomData, rc::Rc}; +use rayon::iter::IntoParallelRefIterator; +#[cfg(feature = "parallel")] +use rayon::iter::ParallelIterator; +use std::{marker::PhantomData, sync::Arc}; use subroutines::{ pcs::prelude::{Commitment, PolynomialCommitmentScheme}, poly_iop::{ @@ -28,7 +31,7 @@ where // we cannot bound PCS::Polynomial with a property trait bound. PCS: PolynomialCommitmentScheme< E, - Polynomial = Rc>, + Polynomial = Arc>, Point = Vec, Evaluation = E::Fr, Commitment = Commitment, @@ -56,7 +59,7 @@ where let mut perm_comms = vec![]; let chunk_size = 1 << num_vars; for i in 0..index.num_witness_columns() { - let perm_oracle = Rc::new(DenseMultilinearExtension::from_evaluations_slice( + let perm_oracle = Arc::new(DenseMultilinearExtension::from_evaluations_slice( num_vars, &index.permutation[i * chunk_size..(i + 1) * chunk_size], )); @@ -66,10 +69,10 @@ where } // build selector oracles and commit to it - let selector_oracles: Vec>> = index + let selector_oracles: Vec>> = index .selectors .iter() - .map(|s| Rc::new(DenseMultilinearExtension::from(s))) + .map(|s| Arc::new(DenseMultilinearExtension::from(s))) .collect(); let selector_commitments = selector_oracles @@ -168,13 +171,13 @@ where // ======================================================================= let step = start_timer!(|| "commit witnesses"); - let witness_polys: Vec>> = witnesses + let witness_polys: Vec>> = witnesses .iter() - .map(|w| Rc::new(DenseMultilinearExtension::from(w))) + .map(|w| Arc::new(DenseMultilinearExtension::from(w))) .collect(); let witness_commits = witness_polys - .iter() + .par_iter() .map(|x| PCS::commit(&pk.pcs_param, x).unwrap()) .collect::>(); for w_com in witness_commits.iter() { @@ -663,15 +666,15 @@ mod tests { let w1 = WitnessColumn(vec![ E::Fr::zero(), E::Fr::one(), - E::Fr::from(2u64), - E::Fr::from(3u64), + E::Fr::from(2u128), + E::Fr::from(3u128), ]); // w2 := [0^5, 1^5, 2^5, 3^5] let w2 = WitnessColumn(vec![ E::Fr::zero(), E::Fr::one(), - E::Fr::from(32u64), - E::Fr::from(243u64), + E::Fr::from(32u128), + E::Fr::from(243u128), ]); // public input = w1 let pi = w1.clone(); diff --git a/hyperplonk/src/structs.rs b/hyperplonk/src/structs.rs index df961e1..a31d99c 100644 --- a/hyperplonk/src/structs.rs +++ b/hyperplonk/src/structs.rs @@ -5,7 +5,7 @@ use ark_ec::PairingEngine; use ark_ff::PrimeField; use ark_poly::DenseMultilinearExtension; use ark_std::log2; -use std::rc::Rc; +use std::sync::Arc; use subroutines::{ pcs::PolynomialCommitmentScheme, poly_iop::prelude::{PermutationCheck, ZeroCheck}, @@ -126,9 +126,9 @@ pub struct HyperPlonkProvingKey>>, + pub permutation_oracles: Vec>>, /// The preprocessed selector polynomials - pub selector_oracles: Vec>>, + pub selector_oracles: Vec>>, /// Commitments to the preprocessed selector polynomials pub selector_commitments: Vec, /// Commitments to the preprocessed permutation polynomials diff --git a/hyperplonk/src/utils.rs b/hyperplonk/src/utils.rs index 69a6d5a..bccc796 100644 --- a/hyperplonk/src/utils.rs +++ b/hyperplonk/src/utils.rs @@ -6,7 +6,7 @@ use arithmetic::{evaluate_opt, VirtualPolynomial}; use ark_ec::PairingEngine; use ark_ff::PrimeField; use ark_poly::DenseMultilinearExtension; -use std::{borrow::Borrow, rc::Rc}; +use std::{borrow::Borrow, sync::Arc}; use subroutines::pcs::{prelude::Commitment, PolynomialCommitmentScheme}; use transcript::IOPTranscript; @@ -32,7 +32,7 @@ where E: PairingEngine, PCS: PolynomialCommitmentScheme< E, - Polynomial = Rc>, + Polynomial = Arc>, Point = Vec, Evaluation = E::Fr, Commitment = Commitment, @@ -104,7 +104,7 @@ macro_rules! build_mle { for row in $rows.iter() { cur_coeffs.push(row.0[i]) } - res.push(Rc::new(DenseMultilinearExtension::from_evaluations_vec( + res.push(Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_vars, cur_coeffs, ))) } @@ -178,8 +178,8 @@ pub(crate) fn prover_sanity_check( pub(crate) fn build_f( gates: &CustomizedGates, num_vars: usize, - selector_mles: &[Rc>], - witness_mles: &[Rc>], + selector_mles: &[Arc>], + witness_mles: &[Arc>], ) -> Result, HyperPlonkErrors> { // TODO: check that selector and witness lengths match what is in // the gate definition @@ -306,7 +306,7 @@ mod test { // 1, 0 |-> 0 // 1, 1 |-> 5 let ql_eval = vec![F::zero(), F::from(2u64), F::zero(), F::from(5u64)]; - let ql = Rc::new(DenseMultilinearExtension::from_evaluations_vec(2, ql_eval)); + let ql = Arc::new(DenseMultilinearExtension::from_evaluations_vec(2, ql_eval)); // W1 = x1x2 + x1 whose evaluations are // 0, 0 |-> 0 @@ -314,7 +314,7 @@ mod test { // 1, 0 |-> 1 // 1, 1 |-> 2 let w_eval = vec![F::zero(), F::zero(), F::from(1u64), F::from(2u64)]; - let w1 = Rc::new(DenseMultilinearExtension::from_evaluations_vec(2, w_eval)); + let w1 = Arc::new(DenseMultilinearExtension::from_evaluations_vec(2, w_eval)); // W2 = x1 + x2 whose evaluations are // 0, 0 |-> 0 @@ -322,7 +322,7 @@ mod test { // 1, 0 |-> 1 // 1, 1 |-> 2 let w_eval = vec![F::zero(), F::one(), F::from(1u64), F::from(2u64)]; - let w2 = Rc::new(DenseMultilinearExtension::from_evaluations_vec(2, w_eval)); + let w2 = Arc::new(DenseMultilinearExtension::from_evaluations_vec(2, w_eval)); // Example: // q_L(X) * W_1(X)^5 - W_2(X) diff --git a/hyperplonk/src/witness.rs b/hyperplonk/src/witness.rs index e9aaa78..6a6d73c 100644 --- a/hyperplonk/src/witness.rs +++ b/hyperplonk/src/witness.rs @@ -2,7 +2,7 @@ use crate::{build_mle, errors::HyperPlonkErrors}; use ark_ff::PrimeField; use ark_poly::DenseMultilinearExtension; use ark_std::log2; -use std::rc::Rc; +use std::sync::Arc; /// A row of witnesses of width `#wires` #[derive(Debug, Clone)] @@ -71,7 +71,7 @@ impl WitnessRow { /// output mle(a1,b1,c1, ...), mle(a2,b2,c2, ...), ... pub fn build_mles( matrix: &[Self], - ) -> Result>>, HyperPlonkErrors> { + ) -> Result>>, HyperPlonkErrors> { build_mle!(matrix) } } diff --git a/subroutines/benches/iop_bench.rs b/subroutines/benches/iop_bench.rs index 0f70477..aa5c26e 100644 --- a/subroutines/benches/iop_bench.rs +++ b/subroutines/benches/iop_bench.rs @@ -2,7 +2,7 @@ use arithmetic::{identity_permutation_mles, VPAuxInfo, VirtualPolynomial}; use ark_bls12_381::{Bls12_381, Fr}; use ark_poly::{DenseMultilinearExtension, MultilinearExtension}; use ark_std::test_rng; -use std::{marker::PhantomData, rc::Rc, time::Instant}; +use std::{marker::PhantomData, sync::Arc, time::Instant}; use subroutines::{ pcs::{prelude::MultilinearKzgPCS, PolynomialCommitmentScheme}, poly_iop::prelude::{ @@ -144,7 +144,7 @@ fn bench_permutation_check() -> Result<(), PolyIOPErrors> { 10 }; - let ws = vec![Rc::new(DenseMultilinearExtension::rand(nv, &mut rng))]; + let ws = vec![Arc::new(DenseMultilinearExtension::rand(nv, &mut rng))]; // identity map let perms = identity_permutation_mles(nv, 1); @@ -218,8 +218,8 @@ fn bench_prod_check() -> Result<(), PolyIOPErrors> { let f: DenseMultilinearExtension = DenseMultilinearExtension::rand(nv, &mut rng); let mut g = f.clone(); g.evaluations.reverse(); - let fs = vec![Rc::new(f)]; - let gs = vec![Rc::new(g)]; + let fs = vec![Arc::new(f)]; + let gs = vec![Arc::new(g)]; let proof = { let start = Instant::now(); diff --git a/subroutines/benches/pcs_bench.rs b/subroutines/benches/pcs_bench.rs index 87bb49f..cfbd8d2 100644 --- a/subroutines/benches/pcs_bench.rs +++ b/subroutines/benches/pcs_bench.rs @@ -1,7 +1,7 @@ use ark_bls12_381::{Bls12_381, Fr}; use ark_ff::UniformRand; use ark_poly::{DenseMultilinearExtension, MultilinearExtension}; -use ark_std::{rc::Rc, test_rng}; +use ark_std::{sync::Arc, test_rng}; use std::time::Instant; use subroutines::pcs::{ prelude::{MultilinearKzgPCS, PCSError, PolynomialCommitmentScheme}, @@ -27,7 +27,7 @@ fn bench_pcs() -> Result<(), PCSError> { 2 }; - let poly = Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)); + let poly = Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)); let (ck, vk) = uni_params.trim(nv)?; let point: Vec<_> = (0..nv).map(|_| Fr::rand(&mut rng)).collect(); diff --git a/subroutines/src/pcs/mod.rs b/subroutines/src/pcs/mod.rs index 7a3eddb..1750570 100644 --- a/subroutines/src/pcs/mod.rs +++ b/subroutines/src/pcs/mod.rs @@ -17,19 +17,13 @@ use transcript::IOPTranscript; /// Note that for our usage of PCS, we do not require the hiding property. pub trait PolynomialCommitmentScheme { /// Prover parameters - type ProverParam: Clone; + type ProverParam: Clone + Sync; /// Verifier parameters type VerifierParam: Clone + CanonicalSerialize + CanonicalDeserialize; /// Structured reference string type SRS: Clone + Debug; /// Polynomial and its associated types - type Polynomial: Clone - + Debug - + Hash - + PartialEq - + Eq - + CanonicalSerialize - + CanonicalDeserialize; + type Polynomial: Clone + Debug + Hash + PartialEq + Eq; /// Polynomial input domain type Point: Clone + Ord + Debug + Sync + Hash + PartialEq + Eq; /// Polynomial Evaluation diff --git a/subroutines/src/pcs/multilinear_kzg/batching.rs b/subroutines/src/pcs/multilinear_kzg/batching.rs index 9b4e3f5..c2f6d33 100644 --- a/subroutines/src/pcs/multilinear_kzg/batching.rs +++ b/subroutines/src/pcs/multilinear_kzg/batching.rs @@ -17,7 +17,7 @@ use arithmetic::{build_eq_x_r_vec, DenseMultilinearExtension, VPAuxInfo, Virtual use ark_ec::{msm::VariableBaseMSM, PairingEngine, ProjectiveCurve}; use ark_ff::PrimeField; use ark_std::{end_timer, log2, start_timer, One, Zero}; -use std::{marker::PhantomData, rc::Rc}; +use std::{marker::PhantomData, sync::Arc}; use transcript::IOPTranscript; #[derive(Clone, Debug, Default, PartialEq, Eq)] @@ -53,7 +53,7 @@ where E: PairingEngine, PCS: PolynomialCommitmentScheme< E, - Polynomial = Rc>, + Polynomial = Arc>, Point = Vec, Evaluation = E::Fr, >, @@ -79,7 +79,7 @@ where for (j, &f_i_eval) in f_i.iter().enumerate() { tilde_g_eval[j] = f_i_eval * eq_t_i_list[index]; } - tilde_gs.push(Rc::new(DenseMultilinearExtension::from_evaluations_vec( + tilde_gs.push(Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_var, tilde_g_eval, ))); @@ -90,7 +90,7 @@ where let mut tilde_eqs = vec![]; for point in points.iter() { let eq_b_zi = build_eq_x_r_vec(point)?; - tilde_eqs.push(Rc::new(DenseMultilinearExtension::from_evaluations_vec( + tilde_eqs.push(Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_var, eq_b_zi, ))); } @@ -131,7 +131,7 @@ where g_prime_evals[j] += tilde_g_eval * eq_i_a2; } } - let g_prime = Rc::new(DenseMultilinearExtension::from_evaluations_vec( + let g_prime = Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_var, g_prime_evals, )); @@ -169,7 +169,7 @@ where E: PairingEngine, PCS: PolynomialCommitmentScheme< E, - Polynomial = Rc>, + Polynomial = Arc>, Point = Vec, Evaluation = E::Fr, Commitment = Commitment, @@ -265,7 +265,7 @@ mod tests { fn test_multi_open_helper( ml_params: &MultilinearUniversalParams, - polys: &[Rc>], + polys: &[Arc>], rng: &mut R, ) -> Result<(), PCSError> { let merged_nv = get_batched_nv(polys[0].num_vars(), polys.len()); @@ -323,7 +323,7 @@ mod tests { for num_poly in 5..6 { for nv in 15..16 { let polys1: Vec<_> = (0..num_poly) - .map(|_| Rc::new(DenseMultilinearExtension::rand(nv, &mut rng))) + .map(|_| Arc::new(DenseMultilinearExtension::rand(nv, &mut rng))) .collect(); test_multi_open_helper(&ml_params, &polys1, &mut rng)?; } diff --git a/subroutines/src/pcs/multilinear_kzg/mod.rs b/subroutines/src/pcs/multilinear_kzg/mod.rs index cf8d7b2..a895535 100644 --- a/subroutines/src/pcs/multilinear_kzg/mod.rs +++ b/subroutines/src/pcs/multilinear_kzg/mod.rs @@ -27,9 +27,9 @@ use ark_std::{ end_timer, format, marker::PhantomData, rand::{CryptoRng, RngCore}, - rc::Rc, start_timer, string::ToString, + sync::Arc, vec, vec::Vec, One, Zero, @@ -59,7 +59,7 @@ impl PolynomialCommitmentScheme for MultilinearKzgPCS { type VerifierParam = MultilinearVerifierParam; type SRS = MultilinearUniversalParams; // Polynomial and its associated types - type Polynomial = Rc>; + type Polynomial = Arc>; type Point = Vec; type Evaluation = E::Fr; // Commitments and proofs @@ -356,7 +356,7 @@ mod tests { fn test_single_helper( params: &MultilinearUniversalParams, - poly: &Rc>, + poly: &Arc>, rng: &mut R, ) -> Result<(), PCSError> { let nv = poly.num_vars(); @@ -385,11 +385,11 @@ mod tests { let params = MultilinearKzgPCS::::gen_srs_for_testing(&mut rng, 10)?; // normal polynomials - let poly1 = Rc::new(DenseMultilinearExtension::rand(8, &mut rng)); + let poly1 = Arc::new(DenseMultilinearExtension::rand(8, &mut rng)); test_single_helper(¶ms, &poly1, &mut rng)?; // single-variate polynomials - let poly2 = Rc::new(DenseMultilinearExtension::rand(1, &mut rng)); + let poly2 = Arc::new(DenseMultilinearExtension::rand(1, &mut rng)); test_single_helper(¶ms, &poly2, &mut rng)?; Ok(()) diff --git a/subroutines/src/poly_iop/perm_check/mod.rs b/subroutines/src/poly_iop/perm_check/mod.rs index a26e029..b6310e7 100644 --- a/subroutines/src/poly_iop/perm_check/mod.rs +++ b/subroutines/src/poly_iop/perm_check/mod.rs @@ -8,7 +8,7 @@ use crate::{ use ark_ec::PairingEngine; use ark_poly::DenseMultilinearExtension; use ark_std::{end_timer, start_timer}; -use std::rc::Rc; +use std::sync::Arc; use transcript::IOPTranscript; /// A permutation subclaim consists of @@ -95,7 +95,7 @@ where impl PermutationCheck for PolyIOP where E: PairingEngine, - PCS: PolynomialCommitmentScheme>>, + PCS: PolynomialCommitmentScheme>>, { type PermutationCheckSubClaim = PermutationCheckSubClaim; type PermutationProof = Self::ProductCheckProof; @@ -192,19 +192,19 @@ mod test { use ark_ec::PairingEngine; use ark_poly::{DenseMultilinearExtension, MultilinearExtension}; use ark_std::test_rng; - use std::{marker::PhantomData, rc::Rc}; + use std::{marker::PhantomData, sync::Arc}; type KZG = MultilinearKzgPCS; fn test_permutation_check_helper( pcs_param: &PCS::ProverParam, - fxs: &[Rc>], - gxs: &[Rc>], - perms: &[Rc>], + fxs: &[Arc>], + gxs: &[Arc>], + perms: &[Arc>], ) -> Result<(), PolyIOPErrors> where E: PairingEngine, - PCS: PolynomialCommitmentScheme>>, + PCS: PolynomialCommitmentScheme>>, { let nv = fxs[0].num_vars; // what's AuxInfo used for? @@ -257,8 +257,8 @@ mod test { // good path: (w1, w2) is a permutation of (w1, w2) itself under the identify // map let ws = vec![ - Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)), - Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)), + Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)), + Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)), ]; // perms is the identity map test_permutation_check_helper::(&pcs_param, &ws, &ws, &id_perms)?; @@ -267,8 +267,8 @@ mod test { { // good path: f = (w1, w2) is a permutation of g = (w2, w1) itself under a map let mut fs = vec![ - Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)), - Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)), + Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)), + Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)), ]; let gs = fs.clone(); fs.reverse(); @@ -281,8 +281,8 @@ mod test { { // bad path 1: w is a not permutation of w itself under a random map let ws = vec![ - Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)), - Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)), + Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)), + Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)), ]; // perms is a random map let perms = random_permutation_mles(nv, 2, &mut rng); @@ -296,12 +296,12 @@ mod test { { // bad path 2: f is a not permutation of g under a identity map let fs = vec![ - Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)), - Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)), + Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)), + Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)), ]; let gs = vec![ - Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)), - Rc::new(DenseMultilinearExtension::rand(nv, &mut rng)), + Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)), + Arc::new(DenseMultilinearExtension::rand(nv, &mut rng)), ]; // s_perm is the identity map diff --git a/subroutines/src/poly_iop/perm_check/util.rs b/subroutines/src/poly_iop/perm_check/util.rs index 3feee68..5ffd699 100644 --- a/subroutines/src/poly_iop/perm_check/util.rs +++ b/subroutines/src/poly_iop/perm_check/util.rs @@ -5,7 +5,7 @@ use arithmetic::identity_permutation_mles; use ark_ff::PrimeField; use ark_poly::DenseMultilinearExtension; use ark_std::{end_timer, start_timer}; -use std::rc::Rc; +use std::sync::Arc; /// Returns the evaluations of two list of MLEs: /// - numerators = (a1, ..., ak) @@ -24,13 +24,13 @@ use std::rc::Rc; pub(super) fn computer_nums_and_denoms( beta: &F, gamma: &F, - fxs: &[Rc>], - gxs: &[Rc>], - perms: &[Rc>], + fxs: &[Arc>], + gxs: &[Arc>], + perms: &[Arc>], ) -> Result< ( - Vec>>, - Vec>>, + Vec>>, + Vec>>, ), PolyIOPErrors, > { @@ -54,11 +54,11 @@ pub(super) fn computer_nums_and_denoms( numerator_evals.push(numerator); denominator_evals.push(denominator); } - numerators.push(Rc::new(DenseMultilinearExtension::from_evaluations_vec( + numerators.push(Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_vars, numerator_evals, ))); - denominators.push(Rc::new(DenseMultilinearExtension::from_evaluations_vec( + denominators.push(Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_vars, denominator_evals, ))); diff --git a/subroutines/src/poly_iop/prod_check/mod.rs b/subroutines/src/poly_iop/prod_check/mod.rs index 27f55ed..30d4b9a 100644 --- a/subroutines/src/poly_iop/prod_check/mod.rs +++ b/subroutines/src/poly_iop/prod_check/mod.rs @@ -14,7 +14,7 @@ use ark_ec::PairingEngine; use ark_ff::{One, PrimeField, Zero}; use ark_poly::DenseMultilinearExtension; use ark_std::{end_timer, start_timer}; -use std::rc::Rc; +use std::sync::Arc; use transcript::IOPTranscript; mod util; @@ -142,7 +142,7 @@ pub struct ProductCheckProof< impl ProductCheck for PolyIOP where E: PairingEngine, - PCS: PolynomialCommitmentScheme>>, + PCS: PolynomialCommitmentScheme>>, { type ProductCheckSubClaim = ProductCheckSubClaim; type ProductCheckProof = ProductCheckProof; @@ -257,12 +257,12 @@ mod test { use ark_ec::PairingEngine; use ark_poly::{DenseMultilinearExtension, MultilinearExtension}; use ark_std::test_rng; - use std::{marker::PhantomData, rc::Rc}; + use std::{marker::PhantomData, sync::Arc}; fn check_frac_poly( - frac_poly: &Rc>, - fs: &[Rc>], - gs: &[Rc>], + frac_poly: &Arc>, + fs: &[Arc>], + gs: &[Arc>], ) where E: PairingEngine, { @@ -285,14 +285,14 @@ mod test { // fs and gs are guaranteed to have the same product // fs and hs doesn't have the same product fn test_product_check_helper( - fs: &[Rc>], - gs: &[Rc>], - hs: &[Rc>], + fs: &[Arc>], + gs: &[Arc>], + hs: &[Arc>], pcs_param: &PCS::ProverParam, ) -> Result<(), PolyIOPErrors> where E: PairingEngine, - PCS: PolynomialCommitmentScheme>>, + PCS: PolynomialCommitmentScheme>>, { let mut transcript = as ProductCheck>::init_transcript(); transcript.append_message(b"testing", b"initializing transcript for testing")?; @@ -352,11 +352,11 @@ mod test { let f2: DenseMultilinearExtension = DenseMultilinearExtension::rand(nv, &mut rng); let mut g2 = f2.clone(); g2.evaluations.reverse(); - let fs = vec![Rc::new(f1), Rc::new(f2)]; - let gs = vec![Rc::new(g2), Rc::new(g1)]; + let fs = vec![Arc::new(f1), Arc::new(f2)]; + let gs = vec![Arc::new(g2), Arc::new(g1)]; let mut hs = vec![]; for _ in 0..fs.len() { - hs.push(Rc::new(DenseMultilinearExtension::rand( + hs.push(Arc::new(DenseMultilinearExtension::rand( fs[0].num_vars, &mut rng, ))); diff --git a/subroutines/src/poly_iop/prod_check/util.rs b/subroutines/src/poly_iop/prod_check/util.rs index 1f7e909..0351e3d 100644 --- a/subroutines/src/poly_iop/prod_check/util.rs +++ b/subroutines/src/poly_iop/prod_check/util.rs @@ -5,7 +5,7 @@ use arithmetic::{get_index, VirtualPolynomial}; use ark_ff::PrimeField; use ark_poly::DenseMultilinearExtension; use ark_std::{end_timer, start_timer}; -use std::rc::Rc; +use std::sync::Arc; use transcript::IOPTranscript; /// Compute multilinear fractional polynomial s.t. frac(x) = f1(x) * ... * fk(x) @@ -14,9 +14,9 @@ use transcript::IOPTranscript; /// The caller needs to sanity-check that the number of polynomials and /// variables match in fxs and gxs; and gi(x) has no zero entries. pub(super) fn compute_frac_poly( - fxs: &[Rc>], - gxs: &[Rc>], -) -> Result>, PolyIOPErrors> { + fxs: &[Arc>], + gxs: &[Arc>], +) -> Result>, PolyIOPErrors> { let start = start_timer!(|| "compute frac(x)"); let mut f_evals = vec![F::one(); 1 << fxs[0].num_vars]; @@ -41,7 +41,7 @@ pub(super) fn compute_frac_poly( } end_timer!(start); - Ok(Rc::new(DenseMultilinearExtension::from_evaluations_vec( + Ok(Arc::new(DenseMultilinearExtension::from_evaluations_vec( fxs[0].num_vars, f_evals, ))) @@ -55,8 +55,8 @@ pub(super) fn compute_frac_poly( /// The caller needs to check num_vars matches in f and g /// Cost: linear in N. pub(super) fn compute_product_poly( - frac_poly: &Rc>, -) -> Result>, PolyIOPErrors> { + frac_poly: &Arc>, +) -> Result>, PolyIOPErrors> { let start = start_timer!(|| "compute evaluations of prod polynomial"); let num_vars = frac_poly.num_vars; let frac_evals = &frac_poly.evaluations; @@ -96,7 +96,7 @@ pub(super) fn compute_product_poly( prod_x_evals.push(F::zero()); end_timer!(start); - Ok(Rc::new(DenseMultilinearExtension::from_evaluations_vec( + Ok(Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_vars, prod_x_evals, ))) @@ -111,10 +111,10 @@ pub(super) fn compute_product_poly( /// /// Cost: O(N) pub(super) fn prove_zero_check( - fxs: &[Rc>], - gxs: &[Rc>], - frac_poly: &Rc>, - prod_x: &Rc>, + fxs: &[Arc>], + gxs: &[Arc>], + frac_poly: &Arc>, + prod_x: &Arc>, alpha: &F, transcript: &mut IOPTranscript, ) -> Result<(IOPProof, VirtualPolynomial), PolyIOPErrors> { @@ -135,10 +135,10 @@ pub(super) fn prove_zero_check( p2_evals[x] = prod_x.evaluations[x1]; } } - let p1 = Rc::new(DenseMultilinearExtension::from_evaluations_vec( + let p1 = Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_vars, p1_evals, )); - let p2 = Rc::new(DenseMultilinearExtension::from_evaluations_vec( + let p2 = Arc::new(DenseMultilinearExtension::from_evaluations_vec( num_vars, p2_evals, )); diff --git a/subroutines/src/poly_iop/sum_check/mod.rs b/subroutines/src/poly_iop/sum_check/mod.rs index 3f3ab93..bd61daa 100644 --- a/subroutines/src/poly_iop/sum_check/mod.rs +++ b/subroutines/src/poly_iop/sum_check/mod.rs @@ -9,7 +9,7 @@ use arithmetic::{VPAuxInfo, VirtualPolynomial}; use ark_ff::PrimeField; use ark_poly::DenseMultilinearExtension; use ark_std::{end_timer, start_timer}; -use std::{fmt::Debug, rc::Rc}; +use std::{fmt::Debug, sync::Arc}; use transcript::IOPTranscript; mod prover; @@ -127,7 +127,7 @@ impl SumCheck for PolyIOP { type SumCheckProof = IOPProof; type VirtualPolynomial = VirtualPolynomial; type VPAuxInfo = VPAuxInfo; - type MultilinearExtension = Rc>; + type MultilinearExtension = Arc>; type SumCheckSubClaim = SumCheckSubClaim; type Transcript = IOPTranscript; @@ -210,7 +210,7 @@ mod test { use ark_ff::UniformRand; use ark_poly::{DenseMultilinearExtension, MultilinearExtension}; use ark_std::test_rng; - use std::rc::Rc; + use std::sync::Arc; fn test_sumcheck( nv: usize, @@ -326,7 +326,7 @@ mod test { fn test_shared_reference() -> Result<(), PolyIOPErrors> { let mut rng = test_rng(); let ml_extensions: Vec<_> = (0..5) - .map(|_| Rc::new(DenseMultilinearExtension::::rand(8, &mut rng))) + .map(|_| Arc::new(DenseMultilinearExtension::::rand(8, &mut rng))) .collect(); let mut poly = VirtualPolynomial::new(8); poly.add_mle_list( diff --git a/subroutines/src/poly_iop/sum_check/prover.rs b/subroutines/src/poly_iop/sum_check/prover.rs index a7d1e37..a3cdb52 100644 --- a/subroutines/src/poly_iop/sum_check/prover.rs +++ b/subroutines/src/poly_iop/sum_check/prover.rs @@ -10,7 +10,7 @@ use ark_ff::PrimeField; use ark_poly::DenseMultilinearExtension; use ark_std::{end_timer, start_timer, vec::Vec}; use rayon::prelude::IntoParallelIterator; -use std::rc::Rc; +use std::sync::Arc; #[cfg(feature = "parallel")] use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator}; @@ -138,7 +138,7 @@ impl SumCheckProver for IOPProverState { } } else { for (t, e) in products_sum.iter_mut().enumerate() { - let t = F::from(t as u64); + let t = F::from(t as u128); let products = (0..1 << (self.poly.aux_info.num_variables - self.round)) .into_par_iter() .map(|b| { @@ -149,6 +149,9 @@ impl SumCheckProver for IOPProverState { let mut product = *coefficient; for &f in products.iter().take(num_mles) { let table = &flattened_ml_extensions[f]; // f's range is checked in init + // TODO: Could be done faster by cashing the results from the + // previous t and adding the diff + // Also possible to use Karatsuba multiplication product *= table[b << 1] + (table[(b << 1) + 1] - table[b << 1]) * t; } @@ -188,7 +191,7 @@ impl SumCheckProver for IOPProverState { // update prover's state to the partial evaluated polynomial self.poly.flattened_ml_extensions = flattened_ml_extensions .iter() - .map(|x| Rc::new(x.clone())) + .map(|x| Arc::new(x.clone())) .collect(); // end_timer!(compute_sum); From 81073dfee104055d67160fe82361d3c29beea525 Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Fri, 16 Dec 2022 09:29:28 -0500 Subject: [PATCH 2/8] batch inversion --- hyperplonk/src/mock.rs | 4 ++-- subroutines/src/poly_iop/prod_check/util.rs | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/hyperplonk/src/mock.rs b/hyperplonk/src/mock.rs index 7fa4372..75e4b4c 100644 --- a/hyperplonk/src/mock.rs +++ b/hyperplonk/src/mock.rs @@ -238,8 +238,8 @@ mod test { MultilinearKzgPCS::::gen_srs_for_testing(&mut rng, SUPPORTED_SIZE)?; let nv = MAX_NUM_VARS; - let vanilla_gate = CustomizedGates::vanilla_plonk_gate(); - test_mock_circuit_zkp_helper(nv, &vanilla_gate, &pcs_srs)?; + let turboplonk_gate = CustomizedGates::jellyfish_turbo_plonk_gate(); + test_mock_circuit_zkp_helper(nv, &turboplonk_gate, &pcs_srs)?; Ok(()) } diff --git a/subroutines/src/poly_iop/prod_check/util.rs b/subroutines/src/poly_iop/prod_check/util.rs index 0351e3d..be52ba2 100644 --- a/subroutines/src/poly_iop/prod_check/util.rs +++ b/subroutines/src/poly_iop/prod_check/util.rs @@ -2,7 +2,7 @@ use crate::poly_iop::{errors::PolyIOPErrors, structs::IOPProof, zero_check::ZeroCheck, PolyIOP}; use arithmetic::{get_index, VirtualPolynomial}; -use ark_ff::PrimeField; +use ark_ff::{batch_inversion, PrimeField}; use ark_poly::DenseMultilinearExtension; use ark_std::{end_timer, start_timer}; use std::sync::Arc; @@ -31,13 +31,15 @@ pub(super) fn compute_frac_poly( *g_eval *= gi; } } + batch_inversion(&mut g_evals[..]); + for (f_eval, g_eval) in f_evals.iter_mut().zip(g_evals.iter()) { if *g_eval == F::zero() { return Err(PolyIOPErrors::InvalidParameters( "gxs has zero entries in the boolean hypercube".to_string(), )); } - *f_eval /= g_eval; + *f_eval *= g_eval; } end_timer!(start); From 5d6985b79984c4acb29d9c6e5880e42ab0a7673c Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Fri, 16 Dec 2022 10:03:30 -0500 Subject: [PATCH 3/8] more parallelization --- hyperplonk/src/snark.rs | 2 +- subroutines/src/poly_iop/sum_check/prover.rs | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/hyperplonk/src/snark.rs b/hyperplonk/src/snark.rs index 816683c..ff18ec2 100644 --- a/hyperplonk/src/snark.rs +++ b/hyperplonk/src/snark.rs @@ -76,7 +76,7 @@ where .collect(); let selector_commitments = selector_oracles - .iter() + .par_iter() .map(|poly| PCS::commit(&pcs_prover_param, poly)) .collect::, _>>()?; diff --git a/subroutines/src/poly_iop/sum_check/prover.rs b/subroutines/src/poly_iop/sum_check/prover.rs index a3cdb52..4145dde 100644 --- a/subroutines/src/poly_iop/sum_check/prover.rs +++ b/subroutines/src/poly_iop/sum_check/prover.rs @@ -9,7 +9,7 @@ use arithmetic::{fix_variables, VirtualPolynomial}; use ark_ff::PrimeField; use ark_poly::DenseMultilinearExtension; use ark_std::{end_timer, start_timer, vec::Vec}; -use rayon::prelude::IntoParallelIterator; +use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator}; use std::sync::Arc; #[cfg(feature = "parallel")] @@ -71,7 +71,7 @@ impl SumCheckProver for IOPProverState { let mut flattened_ml_extensions: Vec> = self .poly .flattened_ml_extensions - .iter() + .par_iter() .map(|x| x.as_ref().clone()) .collect(); @@ -132,9 +132,7 @@ impl SumCheckProver for IOPProverState { } }) .collect::>(); - for val in evals.iter() { - *e += val - } + *e += evals.par_iter().sum::(); } } else { for (t, e) in products_sum.iter_mut().enumerate() { @@ -161,10 +159,7 @@ impl SumCheckProver for IOPProverState { tmp }) .collect::>(); - - for i in products.iter() { - *e += i - } + *e += products.par_iter().sum::(); } } } @@ -190,7 +185,7 @@ impl SumCheckProver for IOPProverState { // update prover's state to the partial evaluated polynomial self.poly.flattened_ml_extensions = flattened_ml_extensions - .iter() + .par_iter() .map(|x| Arc::new(x.clone())) .collect(); From c48c1b97a574852b68e4fd803db40a01c57a3a9d Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Fri, 16 Dec 2022 11:12:44 -0500 Subject: [PATCH 4/8] more parallelization --- arithmetic/src/multilinear_polynomial.rs | 26 +++++-------------- .../src/pcs/multilinear_kzg/batching.rs | 17 +++++++----- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/arithmetic/src/multilinear_polynomial.rs b/arithmetic/src/multilinear_polynomial.rs index e7d569c..42e2fa3 100644 --- a/arithmetic/src/multilinear_polynomial.rs +++ b/arithmetic/src/multilinear_polynomial.rs @@ -160,16 +160,9 @@ fn fix_one_variable_helper(data: &[F], nv: usize, point: &F) -> Vec } #[cfg(feature = "parallel")] - if nv >= 13 { - // on my computer we parallelization doesn't help till nv >= 13 - res.par_iter_mut().enumerate().for_each(|(i, x)| { - *x = data[i << 1] + (data[(i << 1) + 1] - data[i << 1]) * point; - }); - } else { - for i in 0..(1 << (nv - 1)) { - res[i] = data[i << 1] + (data[(i << 1) + 1] - data[i << 1]) * point; - } - } + res.par_iter_mut().enumerate().for_each(|(i, x)| { + *x = data[i << 1] + (data[(i << 1) + 1] - data[i << 1]) * point; + }); res } @@ -279,16 +272,9 @@ fn fix_last_variable_helper(data: &[F], nv: usize, point: &F) -> Vec= 13 { - // on my computer we parallelization doesn't help till nv >= 13 - res.par_iter_mut().enumerate().for_each(|(i, x)| { - *x = data[i] + (data[i + half_len] - data[i]) * point; - }); - } else { - for b in 0..(1 << (nv - 1)) { - res[b] = data[b] + (data[b + half_len] - data[b]) * point; - } - } + res.par_iter_mut().enumerate().for_each(|(i, x)| { + *x = data[i] + (data[i + half_len] - data[i]) * point; + }); res } diff --git a/subroutines/src/pcs/multilinear_kzg/batching.rs b/subroutines/src/pcs/multilinear_kzg/batching.rs index c2f6d33..238c82b 100644 --- a/subroutines/src/pcs/multilinear_kzg/batching.rs +++ b/subroutines/src/pcs/multilinear_kzg/batching.rs @@ -17,6 +17,7 @@ use arithmetic::{build_eq_x_r_vec, DenseMultilinearExtension, VPAuxInfo, Virtual use ark_ec::{msm::VariableBaseMSM, PairingEngine, ProjectiveCurve}; use ark_ff::PrimeField; use ark_std::{end_timer, log2, start_timer, One, Zero}; +use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; use std::{marker::PhantomData, sync::Arc}; use transcript::IOPTranscript; @@ -87,13 +88,15 @@ where end_timer!(timer); let timer = start_timer!(|| format!("compute tilde eq for {} points", points.len())); - let mut tilde_eqs = vec![]; - for point in points.iter() { - let eq_b_zi = build_eq_x_r_vec(point)?; - tilde_eqs.push(Arc::new(DenseMultilinearExtension::from_evaluations_vec( - num_var, eq_b_zi, - ))); - } + let tilde_eqs: Vec::Fr>>> = points + .par_iter() + .map(|point| { + let eq_b_zi = build_eq_x_r_vec(point).unwrap(); + Arc::new(DenseMultilinearExtension::from_evaluations_vec( + num_var, eq_b_zi, + )) + }) + .collect(); end_timer!(timer); // built the virtual polynomial for SumCheck From 44833a0c01952555f8039e48c6c33e165828d4c3 Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Fri, 16 Dec 2022 14:01:32 -0500 Subject: [PATCH 5/8] remove unncessary timer --- arithmetic/src/virtual_polynomial.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/arithmetic/src/virtual_polynomial.rs b/arithmetic/src/virtual_polynomial.rs index 7c64c44..b1be41a 100644 --- a/arithmetic/src/virtual_polynomial.rs +++ b/arithmetic/src/virtual_polynomial.rs @@ -364,8 +364,6 @@ pub fn build_eq_x_r( /// over r, which is /// eq(x,y) = \prod_i=1^num_var (x_i * r_i + (1-x_i)*(1-r_i)) pub fn build_eq_x_r_vec(r: &[F]) -> Result, ArithErrors> { - let start = start_timer!(|| format!("build eq_x_r of size {}", r.len())); - // we build eq(x,r) from its evaluations // we want to evaluate eq(x,r) over x \in {0, 1}^num_vars // for example, with num_vars = 4, x is a binary vector of 4, then @@ -380,7 +378,6 @@ pub fn build_eq_x_r_vec(r: &[F]) -> Result, ArithErrors> { let mut eval = Vec::new(); build_eq_x_r_helper(r, &mut eval)?; - end_timer!(start); Ok(eval) } @@ -512,8 +509,6 @@ mod test { // over r, which is // eq(x,y) = \prod_i=1^num_var (x_i * r_i + (1-x_i)*(1-r_i)) fn build_eq_x_r_for_test(r: &[F]) -> Arc> { - let start = start_timer!(|| "zero check naive build eq_x_r"); - // we build eq(x,r) from its evaluations // we want to evaluate eq(x,r) over x \in {0, 1}^num_vars // for example, with num_vars = 4, x is a binary vector of 4, then @@ -546,7 +541,6 @@ mod test { let mle = DenseMultilinearExtension::from_evaluations_vec(num_var, eval); let res = Arc::new(mle); - end_timer!(start); res } } From 35620b945bc70e7ba7c5d3bb012b580644e521fa Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Fri, 16 Dec 2022 14:17:53 -0500 Subject: [PATCH 6/8] tmp commit for benchmark multithread --- hyperplonk/benches/bench.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/hyperplonk/benches/bench.rs b/hyperplonk/benches/bench.rs index 1a0ea15..ef22090 100644 --- a/hyperplonk/benches/bench.rs +++ b/hyperplonk/benches/bench.rs @@ -37,15 +37,15 @@ fn main() -> Result<(), HyperPlonkErrors> { }, } }; - bench_jellyfish_plonk(&pcs_srs, thread)?; - println!(); + // bench_jellyfish_plonk(&pcs_srs, thread)?; + // println!(); bench_vanilla_plonk(&pcs_srs, thread)?; println!(); - for degree in MIN_CUSTOM_DEGREE..=MAX_CUSTOM_DEGREE { - bench_high_degree_plonk(&pcs_srs, degree, thread)?; - println!(); - } - println!(); + // for degree in MIN_CUSTOM_DEGREE..=MAX_CUSTOM_DEGREE { + // bench_high_degree_plonk(&pcs_srs, degree, thread)?; + // println!(); + // } + // println!(); Ok(()) } From c18d5d65785b0ebf139e0407bc5cfe59b542f5cc Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Fri, 16 Dec 2022 14:23:46 -0500 Subject: [PATCH 7/8] uncomment --- hyperplonk/benches/bench.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/hyperplonk/benches/bench.rs b/hyperplonk/benches/bench.rs index ef22090..1a0ea15 100644 --- a/hyperplonk/benches/bench.rs +++ b/hyperplonk/benches/bench.rs @@ -37,15 +37,15 @@ fn main() -> Result<(), HyperPlonkErrors> { }, } }; - // bench_jellyfish_plonk(&pcs_srs, thread)?; - // println!(); + bench_jellyfish_plonk(&pcs_srs, thread)?; + println!(); bench_vanilla_plonk(&pcs_srs, thread)?; println!(); - // for degree in MIN_CUSTOM_DEGREE..=MAX_CUSTOM_DEGREE { - // bench_high_degree_plonk(&pcs_srs, degree, thread)?; - // println!(); - // } - // println!(); + for degree in MIN_CUSTOM_DEGREE..=MAX_CUSTOM_DEGREE { + bench_high_degree_plonk(&pcs_srs, degree, thread)?; + println!(); + } + println!(); Ok(()) } From 65b19382f5ad55083c279afb3f96d6c3a5b51742 Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Mon, 19 Dec 2022 12:32:28 -0500 Subject: [PATCH 8/8] typofix --- arithmetic/src/multilinear_polynomial.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetic/src/multilinear_polynomial.rs b/arithmetic/src/multilinear_polynomial.rs index 42e2fa3..af12e06 100644 --- a/arithmetic/src/multilinear_polynomial.rs +++ b/arithmetic/src/multilinear_polynomial.rs @@ -11,7 +11,7 @@ pub use ark_poly::DenseMultilinearExtension; /// Sample a random list of multilinear polynomials. /// Returns /// - the list of polynomials, -/// - its sum of polynomial evaluations over the boolean hypeArcube. +/// - its sum of polynomial evaluations over the boolean hypercube. pub fn random_mle_list( nv: usize, degree: usize,