Skip to content

Commit b37a4df

Browse files
committed
Use rayon for parallelization
1 parent 5ee2810 commit b37a4df

5 files changed

Lines changed: 52 additions & 30 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ rand_core = "0.9"
2020
bitvec = "1"
2121
deepfold = { path = "deepfold/deepfold" }
2222
util = { path = "deepfold/util" }
23+
rayon = { version = "1.12", optional = true }
24+
25+
[features]
26+
default = [] # Add "rayon" here if you want it to be enabled by default
27+
rayon = ["dep:rayon"]
2328

2429
[dev-dependencies]
2530
criterion = "0.3"

src/lib.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(feature = "rayon")]
2+
use rayon::prelude::*;
3+
14
mod keys;
25
pub mod polynomials;
36

@@ -672,8 +675,13 @@ pub fn verify_pok<
672675
}
673676

674677
pub fn interpolate_polynomial<F: Field>(evaluations: &[F]) -> MultilinearPolynomial<F> {
675-
let mut coefficients: Vec<F> = vec![F::ZERO; evaluations.len()];
676-
for (index, evaluation) in evaluations.iter().enumerate() {
678+
#[cfg(feature = "rayon")]
679+
let iter_1 = (0..evaluations.len()).into_par_iter();
680+
#[cfg(not(feature = "rayon"))]
681+
let iter_1 = 0..evaluations.len();
682+
683+
let coefficients = iter_1
684+
.map(|coef_index|
677685
// Add eq(i,j) * evaluations[j] to the coefficients.
678686
//
679687
// We can interpret index as a sequence of bits, specifying the
@@ -687,21 +695,17 @@ pub fn interpolate_polynomial<F: Field>(evaluations: &[F]) -> MultilinearPolynom
687695
// - Evaluation point bit is 0, and coef bit is 1, -1.
688696
// - Evaluation point bit is 1, and coef bit is 0, 0.
689697
// - Evaluation point bit is 1, and coef bit is 1, 1.
690-
for (coef_index, coefficient) in coefficients.iter_mut().enumerate() {
691-
// If there is any bit where evaluation is 1, and coef is 0, set to 0.
692-
if (!coef_index) & index != 0 {
693-
continue;
694-
}
695-
696-
let mut value = *evaluation;
697-
// For every bit where evaluation is 0, and coef is 1, invert.
698+
// If there is any bit where evaluation is 1, and coef is 0, set to 0.
699+
evaluations.iter().copied().enumerate()
700+
.filter(|(index, _)| (!coef_index) & index == 0)
701+
.map(|(index, evaluation)|
698702
if ((!index) & coef_index).count_ones() % 2 == 1 {
699-
value = -value;
703+
-evaluation
704+
} else {
705+
evaluation
700706
}
701-
702-
*coefficient += value;
703-
}
704-
}
707+
).sum())
708+
.collect();
705709
MultilinearPolynomial::new(coefficients)
706710
}
707711

src/polynomials/piop_polynomials.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! These are the polynomials that are custom for our PIOP.
2+
#[cfg(feature = "rayon")]
3+
use rayon::prelude::*;
24
use std::{marker::PhantomData, ops::MulAssign};
35

46
use crate::{
@@ -15,7 +17,7 @@ mod q;
1517

1618
pub use mask::Mask;
1719
pub use q::Q;
18-
use spongefish::{ProofResult, VerifierState, codecs::arkworks_algebra::{FieldToUnitDeserialize}};
20+
use spongefish::{codecs::arkworks_algebra::FieldToUnitDeserialize, ProofResult, VerifierState};
1921

2022
#[derive(Clone)]
2123
pub struct P<
@@ -43,7 +45,7 @@ pub struct P<
4345
j_invariant_checkers: [PK::Checker; 2],
4446
}
4547

46-
pub trait Checker<F: Field> {
48+
pub trait Checker<F: Field>: Sync {
4749
fn eval_at_x(&self, x: F) -> F;
4850
fn fix_variable(&mut self, x: F);
4951
fn value(&self) -> F;
@@ -66,8 +68,7 @@ impl<F: Field> Checker<F> for ACChecker<F> {
6668
fn eval_at_x(&self, _x: F) -> F {
6769
F::ZERO
6870
}
69-
fn fix_variable(&mut self, _x: F) {
70-
}
71+
fn fix_variable(&mut self, _x: F) {}
7172
fn value(&self) -> F {
7273
F::ZERO
7374
}
@@ -227,8 +228,11 @@ impl<
227228
if self.variable_count() == VARIABLE_COUNT {
228229
let x = f;
229230
let i_hypercube_variables_mask = (1 << LOG_2_PATH_LENGTH) - 1;
230-
(0..(1 << LOG_2_PATH_LENGTH))
231-
.map(HypercubePoint)
231+
#[cfg(feature = "rayon")]
232+
let iter = (0..(1 << LOG_2_PATH_LENGTH)).into_par_iter();
233+
#[cfg(not(feature = "rayon"))]
234+
let iter = 0..(1 << LOG_2_PATH_LENGTH);
235+
iter.map(HypercubePoint)
232236
.map(|j| {
233237
// We can compute i, since we already know that Q will be zero otherwise
234238
let i_hypercube_remainder =
@@ -261,8 +265,11 @@ impl<
261265
let i_hypercube_variables_mask = (1 << remaining_i_vars) - 1;
262266
// Evaluate i
263267
// We can sum over j, and determine the qualifying i from this
264-
(0..(1 << LOG_2_PATH_LENGTH))
265-
.map(HypercubePoint)
268+
#[cfg(feature = "rayon")]
269+
let iter = (0..(1 << LOG_2_PATH_LENGTH)).into_par_iter();
270+
#[cfg(not(feature = "rayon"))]
271+
let iter = 0..(1 << LOG_2_PATH_LENGTH);
272+
iter.map(HypercubePoint)
266273
.map(|j| {
267274
// Now compute j, the first couple of variables are free,
268275
// the others are determined by i.
@@ -295,8 +302,11 @@ impl<
295302
debug_assert!(self.b_0_j.variable_count() > 0);
296303
debug_assert!(self.b_1_j.variable_count() > 0);
297304
let variables_j = self.b_1_j.variable_count() - 1;
298-
(0usize..(1 << variables_j))
299-
.map(HypercubePoint)
305+
#[cfg(feature = "rayon")]
306+
let iter = (0..(1 << variables_j)).into_par_iter();
307+
#[cfg(not(feature = "rayon"))]
308+
let iter = 0..(1 << variables_j);
309+
iter.map(HypercubePoint)
300310
.map(|j_hypercube_remainder| {
301311
let b_0_i = self.b_0_i.get_as_const();
302312
let b_1_i = self.b_1_i.get_as_const();
@@ -396,7 +406,8 @@ impl<
396406
self.e_0
397407
// x has been absorbed in e_0
398408
* self.q.eval(q_eval_point)
399-
* (self.a_a_j * b_0_j + self.a_a_i * b_0_i) * (b_0_j - b_0_i) - (b_1_i + b_1_j)
409+
* (self.a_a_j * b_0_j + self.a_a_i * b_0_i) * (b_0_j - b_0_i)
410+
- (b_1_i + b_1_j)
400411
+ self.mask.eval(point)
401412
+ self
402413
.j_invariant_checkers

src/polynomials/piop_polynomials/j_invariant_checking.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ impl<F: Field> JInvariantChecker<F> {
174174
let mut val = d5 * (F::from(4) * c - d1) - F::ONE;
175175
val = val * h + c * d4 - F::ONE;
176176
val = val * h + j * d2 * (F::from(4) * c - d1)
177-
+ F::from(-256)
178-
* (d3 * (F::from(9) * c - d1) + F::from(27) * d2 * (d1 - c));
177+
- F::from(256)
178+
* (-d1 * d3 + F::from(3 * 3) * c * d3 - F::from(9 * 3) * d2 * d1
179+
+ F::from(27) * d2 * c);
179180
val = val * h + d3 - d1.pow([2]);
180181
val = val * h + d2 - c.pow([2]);
181182
val = val * h + d1 - a.pow([2]);

0 commit comments

Comments
 (0)