Skip to content

Commit

Permalink
update halo2curves to v0.6.0
Browse files Browse the repository at this point in the history
replace `maybe-rayon` with `rayon` v1.8
  • Loading branch information
kilic committed Jan 10, 2024
1 parent 0b60c88 commit 25a3d9b
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions halo2_proofs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 1 addition & 8 deletions halo2_proofs/benches/commit_zk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scalar> {
// Sample a random polynomial of degree n - 1
let mut random_poly = vec![Scalar::zero(); 1 << domain];
Expand Down
34 changes: 0 additions & 34 deletions halo2_proofs/examples/vector-mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,40 +178,6 @@ impl<F: Field> NumericInstructions<F> for FieldChip<F> {
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>| {
Expand Down
5 changes: 0 additions & 5 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::{
},
};

#[cfg(feature = "multicore")]
use crate::multicore::{
IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator, ParallelIterator,
ParallelSliceMut,
Expand Down Expand Up @@ -1177,15 +1176,13 @@ impl<F: FromUniformBytes<64> + Ord> MockProver<F> {
/// 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<VerifyFailure>> {
self.verify_at_rows_par(self.usable_rows.clone(), self.usable_rows.clone())
}

/// 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<I: Clone + Iterator<Item = usize>>(
&self,
gate_row_ids: I,
Expand Down Expand Up @@ -1653,7 +1650,6 @@ impl<F: FromUniformBytes<64> + Ord> MockProver<F> {
/// ```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 {
Expand All @@ -1675,7 +1671,6 @@ impl<F: FromUniformBytes<64> + Ord> MockProver<F> {
/// ```ignore
/// assert_eq!(prover.verify_at_rows_par(), Ok(()));
/// ```
#[cfg(feature = "multicore")]
pub fn assert_satisfied_at_rows_par<I: Clone + Iterator<Item = usize>>(
&self,
gate_row_ids: I,
Expand Down
39 changes: 6 additions & 33 deletions halo2_proofs/src/multicore.rs
Original file line number Diff line number Diff line change
@@ -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!(

Check failure on line 2 in halo2_proofs/src/multicore.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-wasi

The multicore feature flag is not supported on wasm32 architectures without atomics

Check failure on line 2 in halo2_proofs/src/multicore.rs

View workflow job for this annotation

GitHub Actions / Build target wasm32-unknown-unknown

The multicore feature flag is not supported on wasm32 architectures without atomics
"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<T, E> {
/// Implements `iter.try_fold().try_reduce()` for `rayon::iter::ParallelIterator`,
/// falling back on `Iterator::try_fold` when the `multicore` feature flag is
Expand All @@ -38,12 +26,11 @@ pub trait TryFoldAndReduce<T, E> {
) -> Result<T, E>;
}

#[cfg(feature = "multicore")]
impl<T, E, I> TryFoldAndReduce<T, E> for I
where
T: Send + Sync,
E: Send + Sync,
I: maybe_rayon::iter::ParallelIterator<Item = Result<T, E>>,
I: rayon::iter::ParallelIterator<Item = Result<T, E>>,
{
fn try_fold_and_reduce(
self,
Expand All @@ -54,17 +41,3 @@ where
.try_reduce(&identity, |a, b| fold_op(a, Ok(b)))
}
}

#[cfg(not(feature = "multicore"))]
impl<T, E, I> TryFoldAndReduce<T, E> for I
where
I: std::iter::Iterator<Item = Result<T, E>>,
{
fn try_fold_and_reduce(
mut self,
identity: impl Fn() -> T + Send + Sync,
fold_op: impl Fn(T, Result<T, E>) -> Result<T, E> + Send + Sync,
) -> Result<T, E> {
self.try_fold(identity(), fold_op)
}
}
25 changes: 5 additions & 20 deletions halo2_proofs/src/plonk/permutation/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -133,21 +136,12 @@ impl Assembly {
&self.columns
}

#[cfg(feature = "multicore")]
/// Returns mappings of the copies.
pub fn mapping(
&self,
) -> impl Iterator<Item = impl IndexedParallelIterator<Item = (usize, usize)> + '_> {
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<Item = impl Iterator<Item = (usize, usize)> + '_> {
self.mapping.iter().map(|c| c.iter().copied())
}
}

#[cfg(feature = "thread-safe-region")]
Expand Down Expand Up @@ -315,25 +309,16 @@ impl Assembly {
&self.columns
}

#[cfg(feature = "multicore")]
/// Returns mappings of the copies.
pub fn mapping(
&self,
) -> impl Iterator<Item = impl IndexedParallelIterator<Item = (usize, usize)> + '_> {
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<Item = impl Iterator<Item = (usize, usize)> + '_> {
(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>>(
Expand Down
7 changes: 3 additions & 4 deletions halo2_proofs/src/plonk/verifier/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions halo2_proofs/src/poly/kzg/multiopen/shplonk.rs
Original file line number Diff line number Diff line change
@@ -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<F> = ChallengeScalar<F, U>;
Expand Down
5 changes: 1 addition & 4 deletions halo2_proofs/src/poly/kzg/multiopen/shplonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<F: Field>(poly: Polynomial<F, Coeff>, roots: &[F]) -> Vec<F> {
let poly = roots
.iter()
Expand Down

0 comments on commit 25a3d9b

Please sign in to comment.