Skip to content

Commit

Permalink
refactor: rename msm functions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnevadoc committed Jul 19, 2024
1 parent c654139 commit e04d418
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
6 changes: 3 additions & 3 deletions benches/msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use criterion::{BenchmarkId, Criterion};
use ff::Field;
use group::prime::PrimeCurveAffine;
use halo2curves::bn256::{Fr as Scalar, G1Affine as Point};
use halo2curves::msm::{best_multiexp, multiexp_serial};
use halo2curves::msm::{msm_best, msm_serial};
use rand_core::SeedableRng;
use rand_xorshift::XorShiftRng;
use rayon::current_thread_index;
Expand Down Expand Up @@ -94,7 +94,7 @@ fn msm(c: &mut Criterion) {
assert!(k < 64);
let n: usize = 1 << k;
let mut acc = Point::identity().into();
b.iter(|| multiexp_serial(&coeffs[..n], &bases[..n], &mut acc));
b.iter(|| msm_serial(&coeffs[..n], &bases[..n], &mut acc));
})
.sample_size(10);
}
Expand All @@ -104,7 +104,7 @@ fn msm(c: &mut Criterion) {
assert!(k < 64);
let n: usize = 1 << k;
b.iter(|| {
best_multiexp(&coeffs[..n], &bases[..n]);
msm_best(&coeffs[..n], &bases[..n]);
})
})
.sample_size(SAMPLE_SIZE);
Expand Down
23 changes: 13 additions & 10 deletions src/msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,10 @@ impl<C: CurveAffine> Schedule<C> {
}
}

pub fn serial_multiexp<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C], acc: &mut C::Curve) {
/// Performs a multi-scalar multiplication operation.
///
/// This function will panic if coeffs and bases have a different length.
pub fn msm_serial<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C], acc: &mut C::Curve) {
let coeffs: Vec<_> = coeffs.iter().map(|a| a.to_repr()).collect();

let c = if bases.len() < 4 {
Expand Down Expand Up @@ -464,12 +467,12 @@ pub fn serial_multiexp<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C], acc: &
}
}

/// Performs a multi-exponentiation operation.
/// Performs a multi-scalar multiplication operation.
///
/// This function will panic if coeffs and bases have a different length.
///
/// This will use multithreading if beneficial.
pub fn parallel_multiexp<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C]) -> C::Curve {
pub fn msm_parallel<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C]) -> C::Curve {
assert_eq!(coeffs.len(), bases.len());

let num_threads = rayon::current_num_threads();
Expand All @@ -486,22 +489,22 @@ pub fn parallel_multiexp<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C]) -> C
.zip(results.iter_mut())
{
scope.spawn(move |_| {
serial_multiexp(coeffs, bases, acc);
msm_serial(coeffs, bases, acc);
});
}
});
results.iter().fold(C::Curve::identity(), |a, b| a + b)
} else {
let mut acc = C::Curve::identity();
serial_multiexp(coeffs, bases, &mut acc);
msm_serial(coeffs, bases, &mut acc);
acc
}
}
///

/// This function will panic if coeffs and bases have a different length.
///
/// This will use multithreading if beneficial.
pub fn best_multiexp<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C]) -> C::Curve {
pub fn msm_best<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C]) -> C::Curve {
assert_eq!(coeffs.len(), bases.len());

// TODO: consider adjusting it with emprical data?
Expand All @@ -514,7 +517,7 @@ pub fn best_multiexp<C: CurveAffine>(coeffs: &[C::Scalar], bases: &[C]) -> C::Cu
};

if c < 10 {
return parallel_multiexp(coeffs, bases);
return msm_parallel(coeffs, bases);
}

// coeffs to byte representation
Expand Down Expand Up @@ -651,11 +654,11 @@ mod test {
let scalars = &scalars[..1 << k];

let t0 = start_timer!(|| format!("cyclone indep k={}", k));
let e0 = super::best_multiexp(scalars, points);
let e0 = super::msm_best(scalars, points);
end_timer!(t0);

let t1 = start_timer!(|| format!("older k={}", k));
let e1 = super::parallel_multiexp(scalars, points);
let e1 = super::msm_parallel(scalars, points);
end_timer!(t1);
assert_eq!(e0, e1);
}
Expand Down

0 comments on commit e04d418

Please sign in to comment.