-
Notifications
You must be signed in to change notification settings - Fork 499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Speed up random poly generation #742
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -4,6 +4,13 @@ use ff::Field; | |||||||
use group::Curve; | ||||||||
use rand_core::RngCore; | ||||||||
|
||||||||
#[cfg(feature = "multicore")] | ||||||||
use maybe_rayon::{current_num_threads, prelude::*}; | ||||||||
#[cfg(feature = "multicore")] | ||||||||
use rand_chacha::ChaCha20Rng; | ||||||||
#[cfg(feature = "multicore")] | ||||||||
use rand_core::SeedableRng; | ||||||||
|
||||||||
use super::Argument; | ||||||||
use crate::{ | ||||||||
arithmetic::{eval_polynomial, CurveAffine}, | ||||||||
|
@@ -42,10 +49,43 @@ impl<C: CurveAffine> Argument<C> { | |||||||
transcript: &mut T, | ||||||||
) -> Result<Committed<C>, Error> { | ||||||||
// Sample a random polynomial of degree n - 1 | ||||||||
let mut random_poly = domain.empty_coeff(); | ||||||||
for coeff in random_poly.iter_mut() { | ||||||||
*coeff = C::Scalar::random(&mut rng); | ||||||||
} | ||||||||
#[cfg(feature = "multicore")] | ||||||||
let random_poly = { | ||||||||
let n_threads = current_num_threads(); | ||||||||
let n = 1usize << domain.k as usize; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving this to
Suggested change
|
||||||||
let n_chunks = n_threads + if n % n_threads != 0 { 1 } else { 0 }; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
let mut rand_vec = vec![C::Scalar::ZERO; n]; | ||||||||
|
||||||||
let mut thread_seeds: Vec<ChaCha20Rng> = (0..n_chunks) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
.into_iter() | ||||||||
.map(|_| { | ||||||||
let mut seed = [0u8; 32]; | ||||||||
rng.fill_bytes(&mut seed); | ||||||||
ChaCha20Rng::from_seed(seed) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this could safely use |
||||||||
}) | ||||||||
.collect(); | ||||||||
|
||||||||
thread_seeds | ||||||||
.par_iter_mut() | ||||||||
.zip_eq(rand_vec.par_chunks_mut(n / n_threads)) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
.for_each(|(mut rng, chunk)| { | ||||||||
chunk | ||||||||
.iter_mut() | ||||||||
.for_each(|v| *v = C::Scalar::random(&mut rng)) | ||||||||
}); | ||||||||
|
||||||||
domain.coeff_from_vec(rand_vec) | ||||||||
}; | ||||||||
|
||||||||
#[cfg(not(feature = "multicore"))] | ||||||||
let random_poly = { | ||||||||
let mut random_poly = domain.empty_coeff(); | ||||||||
for coeff in random_poly.iter_mut() { | ||||||||
*coeff = C::Scalar::random(&mut rng); | ||||||||
} | ||||||||
random_poly | ||||||||
}; | ||||||||
|
||||||||
// Sample a random blinding factor | ||||||||
let random_blind = Blind(C::Scalar::random(rng)); | ||||||||
|
||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than expanding this inline here, we should just add an
EvaluationDomain::random_coeff<R: RngCore>(&self, rng: R)
method. This would also avoid the need to makeEvaluationDomain.k
pub(crate)
.