diff --git a/CHANGELOG.md b/CHANGELOG.md index 70384f7a7..6032800d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index 6e02c06cf..99fa901d2 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -635,7 +635,11 @@ impl ScalarMul for Projective

{ } } -impl VariableBaseMSM for Projective

{} +impl VariableBaseMSM for Projective

{ + fn msm(bases: &[Self::MulBase], bigints: &[Self::ScalarField]) -> Result { + P::msm(bases, bigints) + } +} impl>> core::iter::Sum for Projective

{ fn sum>(iter: I) -> Self { diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index 2de3041ce..3f5e0e9ed 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -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; @@ -105,6 +105,16 @@ pub trait SWCurveConfig: super::CurveConfig { res } + /// Default implementation for multi scalar multiplication + fn msm( + bases: &[Affine], + scalars: &[Self::ScalarField], + ) -> Result, 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. diff --git a/test-templates/src/msm.rs b/test-templates/src/msm.rs index 253198d47..45705cb80 100644 --- a/test-templates/src/msm.rs +++ b/test-templates/src/msm.rs @@ -3,6 +3,7 @@ use ark_ec::{ ScalarMul, }; use ark_ff::{PrimeField, UniformRand}; +use ark_std::vec::Vec; fn naive_var_base_msm(bases: &[G::MulBase], scalars: &[G::ScalarField]) -> G { let mut acc = G::zero();