Skip to content
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

Allow to overwrite default impl of msm in TwistedEdwards form #567

Merged
merged 3 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Pending

- [\#567](https://github.com/arkworks-rs/algebra/pull/567) (`ark-ec`) Allow to overwrite the default implementation of the `msm` function provided by the `VariableBaseMSM` trait by a specialized version in `SWCurveConfig`.
achimcc marked this conversation as resolved.
Show resolved Hide resolved

### Breaking changes

### Features
Expand Down
6 changes: 5 additions & 1 deletion ec/src/models/twisted_edwards/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,8 @@ impl<P: TECurveConfig> ScalarMul for Projective<P> {
}
}

impl<P: TECurveConfig> VariableBaseMSM for Projective<P> {}
impl<P: TECurveConfig> VariableBaseMSM for Projective<P> {
fn msm(bases: &[Self::MulBase], bigints: &[Self::ScalarField]) -> Result<Self, usize> {
P::msm(bases, bigints)
}
}
12 changes: 11 additions & 1 deletion ec/src/models/twisted_edwards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ark_serialize::{
};
use ark_std::io::{Read, Write};

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

use ark_ff::fields::Field;
Expand Down Expand Up @@ -85,6 +85,16 @@ pub trait TECurveConfig: 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.
/// If compressed, serializes y coordinate with a bit to encode whether x is positive.
#[inline]
Expand Down