From 25a3d9baa7d2da66e0d42026bd04f6ad527d7d4c Mon Sep 17 00:00:00 2001 From: kilic Date: Wed, 10 Jan 2024 12:46:59 +0300 Subject: [PATCH] update `halo2curves` to v0.6.0 replace `maybe-rayon` with `rayon` v1.8 --- .github/workflows/ci.yml | 2 +- README.md | 9 ++--- halo2_proofs/Cargo.toml | 7 ++-- halo2_proofs/README.md | 6 +-- halo2_proofs/benches/commit_zk.rs | 9 +---- halo2_proofs/examples/vector-mul.rs | 34 ---------------- halo2_proofs/src/dev.rs | 5 --- halo2_proofs/src/multicore.rs | 39 +++---------------- halo2_proofs/src/plonk/permutation/keygen.rs | 25 +++--------- halo2_proofs/src/plonk/verifier/batch.rs | 7 ++-- .../src/poly/kzg/multiopen/shplonk.rs | 5 +-- .../src/poly/kzg/multiopen/shplonk/prover.rs | 5 +-- 12 files changed, 28 insertions(+), 125 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 56efd9a3d8..cdce1546c0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: - feature_set: basic features: batch,dev-graph,gadget-traces - feature_set: all - features: batch,dev-graph,gadget-traces,multicore,test-dev-graph,thread-safe-region,sanity-checks,circuit-params + features: batch,dev-graph,gadget-traces,test-dev-graph,thread-safe-region,sanity-checks,circuit-params steps: - uses: actions/checkout@v3 diff --git a/README.md b/README.md index ad803dbae8..3c7aa59572 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,11 @@ minor version bump. ## Controlling parallelism -`halo2` currently uses [rayon](https://github.com/rayon-rs/rayon) for parallel computation. -The `RAYON_NUM_THREADS` environment variable can be used to set the number of threads. +`halo2` currently uses [rayon](https://github.com/rayon-rs/rayon) for parallel computation. The `RAYON_NUM_THREADS` environment variable can be used to set the number of threads. -You can disable `rayon` by disabling the `"multicore"` feature. -Warning! Halo2 will lose access to parallelism if you disable the `"multicore"` feature. -This will significantly degrade performance. +When compiling to WASM-targets, notice that since version `1.7`, `rayon` will fallback automatically (with no need to handle features) to require `getrandom` in order to be able to work. For more info related to WASM-compilation. + +See: [Rayon: Usage with WebAssembly](https://github.com/rayon-rs/rayon#usage-with-webassembly) for more ## License diff --git a/halo2_proofs/Cargo.toml b/halo2_proofs/Cargo.toml index 2ab33ba5d7..30c0d13f49 100644 --- a/halo2_proofs/Cargo.toml +++ b/halo2_proofs/Cargo.toml @@ -51,15 +51,15 @@ harness = false backtrace = { version = "0.3", optional = true } ff = "0.13" group = "0.13" -halo2curves = { version = "0.5.0", default-features = false } +halo2curves = { version = "^0.6", default-features = false } rand_core = { version = "0.6", default-features = false } tracing = "0.1" blake2b_simd = "1" # MSRV 1.66.0 sha3 = "0.9.1" rand_chacha = "0.3" -maybe-rayon = { version = "0.1.0", default-features = false } serde = { version = "1", optional = true, features = ["derive"] } serde_derive = { version = "1", optional = true} +rayon = "1.8" # Developer tooling dependencies plotters = { version = "0.3.0", default-features = false, optional = true } @@ -80,8 +80,7 @@ serde_json = "1" getrandom = { version = "0.2", features = ["js"] } [features] -default = ["batch", "multicore", "bits"] -multicore = ["maybe-rayon/threads", "halo2curves/multicore"] +default = ["batch","bits"] dev-graph = ["plotters", "tabbycat"] test-dev-graph = [ "dev-graph", diff --git a/halo2_proofs/README.md b/halo2_proofs/README.md index 7aeebeb849..bdb9a63639 100644 --- a/halo2_proofs/README.md +++ b/halo2_proofs/README.md @@ -15,9 +15,9 @@ minor version bump. computation. The `RAYON_NUM_THREADS` environment variable can be used to set the number of threads. -You can disable `rayon` by disabling the `"multicore"` feature. -Warning! Halo2 will lose access to parallelism if you disable the `"multicore"` feature. -This will significantly degrade performance. +When compiling to WASM-targets, notice that since version `1.7`, `rayon` will fallback automatically (with no need to handle features) to require `getrandom` in order to be able to work. For more info related to WASM-compilation. + +See: [Rayon: Usage with WebAssembly](https://github.com/rayon-rs/rayon#usage-with-webassembly) for more ## License diff --git a/halo2_proofs/benches/commit_zk.rs b/halo2_proofs/benches/commit_zk.rs index a6a33a934b..d06bed0f97 100644 --- a/halo2_proofs/benches/commit_zk.rs +++ b/halo2_proofs/benches/commit_zk.rs @@ -7,16 +7,9 @@ use halo2curves::pasta::pallas::Scalar; use rand_chacha::rand_core::RngCore; use rand_chacha::ChaCha20Rng; use rand_core::SeedableRng; +use rayon::current_num_threads; use std::{collections::HashMap, iter}; -#[cfg(feature = "multicore")] -use maybe_rayon::current_num_threads; - -#[cfg(not(feature = "multicore"))] -fn current_num_threads() -> usize { - 1 -} - fn rand_poly_serial(mut rng: ChaCha20Rng, domain: usize) -> Vec { // Sample a random polynomial of degree n - 1 let mut random_poly = vec![Scalar::zero(); 1 << domain]; diff --git a/halo2_proofs/examples/vector-mul.rs b/halo2_proofs/examples/vector-mul.rs index 4b971dc97f..01728fdf36 100644 --- a/halo2_proofs/examples/vector-mul.rs +++ b/halo2_proofs/examples/vector-mul.rs @@ -178,40 +178,6 @@ impl NumericInstructions for FieldChip { let config = self.config(); assert_eq!(a.len(), b.len()); - #[cfg(feature = "thread-safe-region")] - { - use maybe_rayon::prelude::{ - IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator, - }; - layouter.assign_region( - || "mul", - |region: Region<'_, F>| { - let thread_safe_region = std::sync::Mutex::new(region); - a.par_iter() - .zip(b.par_iter()) - .enumerate() - .map(|(i, (a, b))| { - let mut region = thread_safe_region.lock().unwrap(); - - config.s_mul.enable(&mut region, i)?; - - a.0.copy_advice(|| "lhs", &mut region, config.advice[0], i)?; - b.0.copy_advice(|| "rhs", &mut region, config.advice[1], i)?; - - let value = a.0.value().copied() * b.0.value(); - - // Finally, we do the assignment to the output, returning a - // variable to be used in another part of the circuit. - region - .assign_advice(|| "lhs * rhs", config.advice[2], i, || value) - .map(Number) - }) - .collect() - }, - ) - } - - #[cfg(not(feature = "thread-safe-region"))] layouter.assign_region( || "mul", |mut region: Region<'_, F>| { diff --git a/halo2_proofs/src/dev.rs b/halo2_proofs/src/dev.rs index 6df373bf66..0790aa7d5c 100644 --- a/halo2_proofs/src/dev.rs +++ b/halo2_proofs/src/dev.rs @@ -20,7 +20,6 @@ use crate::{ }, }; -#[cfg(feature = "multicore")] use crate::multicore::{ IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator, ParallelIterator, ParallelSliceMut, @@ -1177,7 +1176,6 @@ impl + Ord> MockProver { /// Returns `Ok(())` if this `MockProver` is satisfied, or a list of errors indicating /// the reasons that the circuit is not satisfied. /// Constraints and lookup are checked at `usable_rows`, parallelly. - #[cfg(feature = "multicore")] pub fn verify_par(&self) -> Result<(), Vec> { self.verify_at_rows_par(self.usable_rows.clone(), self.usable_rows.clone()) } @@ -1185,7 +1183,6 @@ impl + Ord> MockProver { /// Returns `Ok(())` if this `MockProver` is satisfied, or a list of errors indicating /// the reasons that the circuit is not satisfied. /// Constraints are only checked at `gate_row_ids`, and lookup inputs are only checked at `lookup_input_row_ids`, parallelly. - #[cfg(feature = "multicore")] pub fn verify_at_rows_par>( &self, gate_row_ids: I, @@ -1653,7 +1650,6 @@ impl + Ord> MockProver { /// ```ignore /// assert_eq!(prover.verify_par(), Ok(())); /// ``` - #[cfg(feature = "multicore")] pub fn assert_satisfied_par(&self) { if let Err(errs) = self.verify_par() { for err in errs { @@ -1675,7 +1671,6 @@ impl + Ord> MockProver { /// ```ignore /// assert_eq!(prover.verify_at_rows_par(), Ok(())); /// ``` - #[cfg(feature = "multicore")] pub fn assert_satisfied_at_rows_par>( &self, gate_row_ids: I, diff --git a/halo2_proofs/src/multicore.rs b/halo2_proofs/src/multicore.rs index 60502f07d2..04f748b9e1 100644 --- a/halo2_proofs/src/multicore.rs +++ b/halo2_proofs/src/multicore.rs @@ -1,29 +1,17 @@ -#[cfg(all( - feature = "multicore", - target_arch = "wasm32", - not(target_feature = "atomics") -))] +#[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))] compile_error!( "The multicore feature flag is not supported on wasm32 architectures without atomics" ); -pub use maybe_rayon::{ - iter::{IntoParallelIterator, IntoParallelRefMutIterator, ParallelIterator}, - join, scope, Scope, -}; - -#[cfg(feature = "multicore")] -pub use maybe_rayon::{ +pub use rayon::{ current_num_threads, iter::{IndexedParallelIterator, IntoParallelRefIterator}, + iter::{IntoParallelIterator, IntoParallelRefMutIterator, ParallelIterator}, + join, scope, slice::ParallelSliceMut, + Scope, }; -#[cfg(not(feature = "multicore"))] -pub fn current_num_threads() -> usize { - 1 -} - pub trait TryFoldAndReduce { /// Implements `iter.try_fold().try_reduce()` for `rayon::iter::ParallelIterator`, /// falling back on `Iterator::try_fold` when the `multicore` feature flag is @@ -38,12 +26,11 @@ pub trait TryFoldAndReduce { ) -> Result; } -#[cfg(feature = "multicore")] impl TryFoldAndReduce for I where T: Send + Sync, E: Send + Sync, - I: maybe_rayon::iter::ParallelIterator>, + I: rayon::iter::ParallelIterator>, { fn try_fold_and_reduce( self, @@ -54,17 +41,3 @@ where .try_reduce(&identity, |a, b| fold_op(a, Ok(b))) } } - -#[cfg(not(feature = "multicore"))] -impl TryFoldAndReduce for I -where - I: std::iter::Iterator>, -{ - fn try_fold_and_reduce( - mut self, - identity: impl Fn() -> T + Send + Sync, - fold_op: impl Fn(T, Result) -> Result + Send + Sync, - ) -> Result { - self.try_fold(identity(), fold_op) - } -} diff --git a/halo2_proofs/src/plonk/permutation/keygen.rs b/halo2_proofs/src/plonk/permutation/keygen.rs index 9451cebd1f..0d78f00ac5 100644 --- a/halo2_proofs/src/plonk/permutation/keygen.rs +++ b/halo2_proofs/src/plonk/permutation/keygen.rs @@ -11,8 +11,11 @@ use crate::{ }, }; -#[cfg(feature = "multicore")] -use crate::multicore::{IndexedParallelIterator, ParallelIterator}; +#[cfg(feature = "thread-safe-region")] +use crate::multicore::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator}; + +#[cfg(not(feature = "thread-safe-region"))] +use crate::multicore::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; #[cfg(feature = "thread-safe-region")] use std::collections::{BTreeSet, HashMap}; @@ -133,21 +136,12 @@ impl Assembly { &self.columns } - #[cfg(feature = "multicore")] /// Returns mappings of the copies. pub fn mapping( &self, ) -> impl Iterator + '_> { - use crate::multicore::IntoParallelRefIterator; - self.mapping.iter().map(|c| c.par_iter().copied()) } - - #[cfg(not(feature = "multicore"))] - /// Returns mappings of the copies. - pub fn mapping(&self) -> impl Iterator + '_> { - self.mapping.iter().map(|c| c.iter().copied()) - } } #[cfg(feature = "thread-safe-region")] @@ -315,25 +309,16 @@ impl Assembly { &self.columns } - #[cfg(feature = "multicore")] /// Returns mappings of the copies. pub fn mapping( &self, ) -> impl Iterator + '_> { - use crate::multicore::IntoParallelIterator; - (0..self.num_cols).map(move |i| { (0..self.col_len) .into_par_iter() .map(move |j| self.mapping_at_idx(i, j)) }) } - - #[cfg(not(feature = "multicore"))] - /// Returns mappings of the copies. - pub fn mapping(&self) -> impl Iterator + '_> { - (0..self.num_cols).map(move |i| (0..self.col_len).map(move |j| self.mapping_at_idx(i, j))) - } } pub(crate) fn build_pk<'params, C: CurveAffine, P: Params<'params, C>>( diff --git a/halo2_proofs/src/plonk/verifier/batch.rs b/halo2_proofs/src/plonk/verifier/batch.rs index 173d552e5f..ba3e2419e6 100644 --- a/halo2_proofs/src/plonk/verifier/batch.rs +++ b/halo2_proofs/src/plonk/verifier/batch.rs @@ -5,7 +5,9 @@ use rand_core::OsRng; use super::{verify_proof, VerificationStrategy}; use crate::{ - multicore::{IntoParallelIterator, TryFoldAndReduce}, + multicore::{ + IndexedParallelIterator, IntoParallelIterator, ParallelIterator, TryFoldAndReduce, + }, plonk::{Error, VerifyingKey}, poly::{ commitment::{Params, MSM}, @@ -19,9 +21,6 @@ use crate::{ transcript::{Blake2bRead, TranscriptReadBuffer}, }; -#[cfg(feature = "multicore")] -use crate::multicore::{IndexedParallelIterator, ParallelIterator}; - /// A proof verification strategy that returns the proof's MSM. /// /// `BatchVerifier` handles the accumulation of the MSMs for the batched proofs. diff --git a/halo2_proofs/src/poly/kzg/multiopen/shplonk.rs b/halo2_proofs/src/poly/kzg/multiopen/shplonk.rs index bed814cc68..d0814e83e3 100644 --- a/halo2_proofs/src/poly/kzg/multiopen/shplonk.rs +++ b/halo2_proofs/src/poly/kzg/multiopen/shplonk.rs @@ -1,16 +1,13 @@ mod prover; mod verifier; -use crate::multicore::IntoParallelIterator; +use crate::multicore::{IntoParallelIterator, ParallelIterator}; use crate::{poly::query::Query, transcript::ChallengeScalar}; use ff::Field; pub use prover::ProverSHPLONK; use std::collections::BTreeSet; pub use verifier::VerifierSHPLONK; -#[cfg(feature = "multicore")] -use crate::multicore::ParallelIterator; - #[derive(Clone, Copy, Debug)] struct U {} type ChallengeU = ChallengeScalar; diff --git a/halo2_proofs/src/poly/kzg/multiopen/shplonk/prover.rs b/halo2_proofs/src/poly/kzg/multiopen/shplonk/prover.rs index b18bf4919f..5001d69094 100644 --- a/halo2_proofs/src/poly/kzg/multiopen/shplonk/prover.rs +++ b/halo2_proofs/src/poly/kzg/multiopen/shplonk/prover.rs @@ -12,7 +12,7 @@ use crate::poly::query::{PolynomialPointer, ProverQuery}; use crate::poly::{Coeff, Polynomial}; use crate::transcript::{EncodedChallenge, TranscriptWrite}; -use crate::multicore::IntoParallelIterator; +use crate::multicore::{IntoParallelIterator, ParallelIterator}; use ff::Field; use group::Curve; use halo2curves::pairing::Engine; @@ -23,9 +23,6 @@ use std::io; use std::marker::PhantomData; use std::ops::MulAssign; -#[cfg(feature = "multicore")] -use crate::multicore::ParallelIterator; - fn div_by_vanishing(poly: Polynomial, roots: &[F]) -> Vec { let poly = roots .iter()