Skip to content

Commit

Permalink
fix the parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
weikengchen committed May 29, 2024
1 parent 3a265f9 commit 716b728
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
10 changes: 9 additions & 1 deletion ec/src/models/short_weierstrass/affine.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ark_serialize::{
CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate,
batch_check_helper, CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError,
Valid, Validate,
};
use ark_std::{
borrow::Borrow,
Expand Down Expand Up @@ -382,6 +383,13 @@ impl<P: SWCurveConfig> Valid for Affine<P> {
Err(SerializationError::InvalidData)
}
}

fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
batch_check_helper(batch)
}
}

impl<P: SWCurveConfig> CanonicalDeserialize for Affine<P> {
Expand Down
10 changes: 9 additions & 1 deletion ec/src/models/twisted_edwards/affine.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ark_serialize::{
CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate,
batch_check_helper, CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError,
Valid, Validate,
};
use ark_std::{
borrow::Borrow,
Expand Down Expand Up @@ -339,6 +340,13 @@ impl<P: TECurveConfig> Valid for Affine<P> {
Err(SerializationError::InvalidData)
}
}

fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
batch_check_helper(batch)
}
}

impl<P: TECurveConfig> CanonicalDeserialize for Affine<P> {
Expand Down
10 changes: 9 additions & 1 deletion ec/src/pairing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ark_ff::{AdditiveGroup, CyclotomicMultSubgroup, Field, One, PrimeField};
use ark_serialize::{
CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError, Valid, Validate,
batch_check_helper, CanonicalDeserialize, CanonicalSerialize, Compress, SerializationError,
Valid, Validate,
};
use ark_std::{
borrow::Borrow,
Expand Down Expand Up @@ -165,6 +166,13 @@ impl<P: Pairing> Valid for PairingOutput<P> {
Err(SerializationError::InvalidData)
}
}

fn batch_check<'a>(batch: impl Iterator<Item = &'a Self>) -> Result<(), SerializationError>
where
Self: 'a,
{
batch_check_helper(batch)
}
}

impl<P: Pairing> CanonicalDeserialize for PairingOutput<P> {
Expand Down
37 changes: 27 additions & 10 deletions serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,8 @@ pub trait Valid: Sized {
where
Self: 'a,
{
#[cfg(feature = "parallel")]
{
use rayon::{iter::ParallelBridge, prelude::ParallelIterator};
batch.par_bridge().try_for_each(|e| e.check())?;
}
#[cfg(not(feature = "parallel"))]
{
for item in batch {
item.check()?;
}
for item in batch {
item.check()?;
}
Ok(())
}
Expand Down Expand Up @@ -241,3 +233,28 @@ pub fn buffer_bit_byte_size(modulus_bits: usize) -> (usize, usize) {
pub const fn buffer_byte_size(modulus_bits: usize) -> usize {
(modulus_bits + 7) / 8
}

pub fn batch_check_helper<'a, T>(
batch: impl Iterator<Item = &'a T>,
) -> Result<(), SerializationError>
where
T: 'a + Valid + Send + Clone,
{
#[cfg(feature = "parallel")]
{
use ark_std::vec::Vec;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
batch
.map(|x| (*x).clone())
.collect::<Vec<T>>()
.into_par_iter()
.try_for_each(|e| e.check())?;
}
#[cfg(not(feature = "parallel"))]
{
for item in batch {
item.check()?;
}
}
Ok(())
}

0 comments on commit 716b728

Please sign in to comment.