-
Notifications
You must be signed in to change notification settings - Fork 400
Refactor MSM: Use VariableBaseMSM trait
#425
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
Changes from 14 commits
f1d8c46
21e694c
ad0f704
97f9214
1408ca6
58fee09
f46cbfd
5e3ad28
6c092a7
574ca9c
bc30532
e5bfe79
288667f
8cced93
27b9e15
73065ac
c02bd9f
3a5075a
996a69a
82cf15f
f11be6f
74c95c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,9 @@ use ark_ff::{ | |
| ToConstraintField, UniformRand, | ||
| }; | ||
|
|
||
| use crate::{models::SWModelParameters as Parameters, AffineCurve, ProjectiveCurve}; | ||
| use crate::{ | ||
| models::SWModelParameters as Parameters, msm::VariableBaseMSM, AffineCurve, ProjectiveCurve, | ||
| }; | ||
|
|
||
| use num_traits::{One, Zero}; | ||
| use zeroize::Zeroize; | ||
|
|
@@ -170,7 +172,7 @@ impl<P: Parameters> Add<Self> for GroupAffine<P> { | |
| impl<'a, P: Parameters> AddAssign<&'a Self> for GroupAffine<P> { | ||
| fn add_assign(&mut self, other: &'a Self) { | ||
| let mut s_proj = GroupProjective::from(*self); | ||
| s_proj.add_assign_mixed(other); | ||
| ProjectiveCurve::add_assign_mixed(&mut s_proj, other); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did this break? i.e. does
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's needed for disambiguation, since we have two traits that define that method |
||
| *self = s_proj.into(); | ||
| } | ||
| } | ||
|
|
@@ -561,7 +563,7 @@ impl<P: Parameters> ProjectiveCurve for GroupProjective<P> { | |
|
|
||
| if self.x == u2 && self.y == s2 { | ||
| // The two points are equal, so we double. | ||
| self.double_in_place(); | ||
| ProjectiveCurve::double_in_place(self); | ||
| } else { | ||
| // If we're adding -a and a together, self.z becomes zero as H becomes zero. | ||
|
|
||
|
|
@@ -670,7 +672,7 @@ impl<'a, P: Parameters> AddAssign<&'a Self> for GroupProjective<P> { | |
|
|
||
| if u1 == u2 && s1 == s2 { | ||
| // The two points are equal, so we double. | ||
| self.double_in_place(); | ||
| ProjectiveCurve::double_in_place(self); | ||
| } else { | ||
| // If we're adding -a and a together, self.z becomes zero as H becomes zero. | ||
|
|
||
|
|
@@ -911,3 +913,17 @@ where | |
| GroupAffine::from(*self).to_field_elements() | ||
| } | ||
| } | ||
|
|
||
| impl<P: Parameters> VariableBaseMSM for GroupProjective<P> { | ||
| type MSMBase = GroupAffine<P>; | ||
|
|
||
| type Scalar = <Self as ProjectiveCurve>::ScalarField; | ||
|
|
||
| fn double_in_place(&mut self) -> &mut Self { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be let's rename this to |
||
| ProjectiveCurve::double_in_place(self) | ||
| } | ||
|
|
||
| fn add_assign_mixed(&mut self, other: &Self::MSMBase) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto for this. |
||
| ProjectiveCurve::add_assign_mixed(self, other) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| use crate::{AffineCurve, ProjectiveCurve}; | ||
| use ark_ff::{BigInteger, PrimeField}; | ||
| use ark_std::{cfg_iter, cfg_iter_mut, vec::Vec}; | ||
| use ark_std::{borrow::Borrow, cfg_iter, cfg_iter_mut, vec::Vec}; | ||
|
|
||
| #[cfg(feature = "parallel")] | ||
| use rayon::prelude::*; | ||
|
|
@@ -81,17 +81,17 @@ impl FixedBase { | |
|
|
||
| // TODO use const-generics for the scalar size and window | ||
| // TODO use iterators of iterators of T::Affine instead of taking owned Vec | ||
| pub fn msm<T: ProjectiveCurve>( | ||
| scalar_size: usize, | ||
| window: usize, | ||
| table: &[Vec<T::Affine>], | ||
| v: &[T::ScalarField], | ||
| ) -> Vec<T> { | ||
| pub fn msm<C, I>(scalar_size: usize, window: usize, table: &[Vec<C::Affine>], v: I) -> Vec<C> | ||
| where | ||
| C: ProjectiveCurve, | ||
| I: IntoIterator, | ||
| I::Item: Borrow<C::ScalarField>, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this more generic bound but us much? the However, the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mmmmh, I was trying to emulate @oleganza in curve25519-dalek: https://github.com/dalek-cryptography/curve25519-dalek/blob/main/src/backend/serial/scalar_mul/pippenger.rs.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mmagician, @Pratyush: with a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The impl in curve25519-dalek is not parallelized, which is why they're fine with
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This response is not related to Borrow, is it?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mmaker So is the suggestion to provide an Iterator-based API for MSMs with Scalars, while providing the standard slice-based API for MSMs with bigints? |
||
| { | ||
| let outerc = (scalar_size + window - 1) / window; | ||
| assert!(outerc <= table.len()); | ||
|
|
||
| cfg_iter!(v) | ||
| .map(|e| Self::windowed_mul::<T>(outerc, window, table, e)) | ||
| cfg_into_iter!(v) | ||
| .map(|e| Self::windowed_mul::<C>(outerc, window, table, e.borrow())) | ||
| .collect::<Vec<_>>() | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.