Skip to content

Commit

Permalink
Allow to overwrite the default implementation of msm (#528)
Browse files Browse the repository at this point in the history
Co-authored-by: mmagician <[email protected]>
Co-authored-by: Pratyush Mishra <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2022
1 parent d42f71c commit 6e3e804
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Pending

- [\#528](https://github.com/arkworks-rs/algebra/pull/528) (`ark-ec`) Allow to overwrite the default implementation of the `msm` function provided by the `VariableBaseMSM` trait by a specialized version in `SWCurveConfig`.
- [\#521](https://github.com/arkworks-rs/algebra/pull/521) (`ark-poly`) Change `DensePolynomial::evaluate_over_domain` to not truncate terms higher than the size of the domain.

### Breaking changes
Expand Down
6 changes: 5 additions & 1 deletion ec/src/models/short_weierstrass/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,11 @@ impl<P: SWCurveConfig> ScalarMul for Projective<P> {
}
}

impl<P: SWCurveConfig> VariableBaseMSM for Projective<P> {}
impl<P: SWCurveConfig> VariableBaseMSM for Projective<P> {
fn msm(bases: &[Self::MulBase], bigints: &[Self::ScalarField]) -> Result<Self, usize> {
P::msm(bases, bigints)
}
}

impl<P: SWCurveConfig, T: Borrow<Affine<P>>> core::iter::Sum<T> for Projective<P> {
fn sum<I: Iterator<Item = T>>(iter: I) -> Self {
Expand Down
12 changes: 11 additions & 1 deletion ec/src/models/short_weierstrass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ark_std::io::{Read, Write};

use ark_ff::fields::Field;

use crate::{AffineRepr, Group};
use crate::{scalar_mul::variable_base::VariableBaseMSM, AffineRepr, Group};

use num_traits::Zero;

Expand Down Expand Up @@ -105,6 +105,16 @@ pub trait SWCurveConfig: super::CurveConfig {
res
}

/// Default implementation for multi scalar multiplication
fn msm(
bases: &[Affine<Self>],
scalars: &[Self::ScalarField],
) -> Result<Projective<Self>, usize> {
(bases.len() == scalars.len())
.then(|| VariableBaseMSM::msm_unchecked(bases, scalars))
.ok_or(usize::min(bases.len(), scalars.len()))
}

/// If uncompressed, serializes both x and y coordinates as well as a bit for whether it is
/// infinity. If compressed, serializes x coordinate with two bits to encode whether y is
/// positive, negative, or infinity.
Expand Down
1 change: 1 addition & 0 deletions test-templates/src/msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ark_ec::{
ScalarMul,
};
use ark_ff::{PrimeField, UniformRand};
use ark_std::vec::Vec;

fn naive_var_base_msm<G: ScalarMul>(bases: &[G::MulBase], scalars: &[G::ScalarField]) -> G {
let mut acc = G::zero();
Expand Down

0 comments on commit 6e3e804

Please sign in to comment.