From 1666e382865a45ee13c3e4ae90dbb04c59f1ab25 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Wed, 30 Nov 2022 09:38:55 +0100 Subject: [PATCH 01/18] add msm_bigint to SWCurveConfig trait --- ec/src/models/short_weierstrass/mod.rs | 13 +++++++++++-- test-templates/src/msm.rs | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index 2de3041ce..8742bb950 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -4,9 +4,9 @@ use ark_serialize::{ }; use ark_std::io::{Read, Write}; -use ark_ff::fields::Field; +use ark_ff::{fields::Field, PrimeField}; -use crate::{AffineRepr, Group}; +use crate::{AffineRepr, Group, scalar_mul::variable_base::VariableBaseMSM}; use num_traits::Zero; @@ -105,6 +105,15 @@ pub trait SWCurveConfig: super::CurveConfig { res } + /// Default implementation for multi scalar multiplication + fn msm_bigint( + bases: &[Affine], + bigints: &[::BigInt], + ) -> Projective + { + VariableBaseMSM::msm_bigint(bases, bigints) + } + /// 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 4bbc780c0..7b68b2cae 100644 --- a/test-templates/src/msm.rs +++ b/test-templates/src/msm.rs @@ -3,6 +3,8 @@ use ark_ec::{ ScalarMul, }; use ark_ff::{PrimeField, UniformRand}; +use ark_std::{borrow::Borrow, iterable::Iterable, vec::Vec}; + fn naive_var_base_msm(bases: &[G::MulBase], scalars: &[G::ScalarField]) -> G { let mut acc = G::zero(); From cf5935376c861b851a9f8c9db868284961ec1cf6 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Wed, 30 Nov 2022 09:40:38 +0100 Subject: [PATCH 02/18] refer to msm from trait in Projective impl --- ec/src/models/short_weierstrass/group.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index 84dfc71d3..3e3bf9586 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -634,7 +634,14 @@ impl ScalarMul for Projective

{ } } -impl VariableBaseMSM for Projective

{} +impl VariableBaseMSM for Projective

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

{ fn sum>(iter: I) -> Self { From ebe10d9c5a90a5c4f70ad5216b875955662ea90d Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Wed, 30 Nov 2022 11:37:11 +0100 Subject: [PATCH 03/18] remove unused imports --- test-templates/src/msm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-templates/src/msm.rs b/test-templates/src/msm.rs index 7b68b2cae..3c1a5a7cf 100644 --- a/test-templates/src/msm.rs +++ b/test-templates/src/msm.rs @@ -3,7 +3,7 @@ use ark_ec::{ ScalarMul, }; use ark_ff::{PrimeField, UniformRand}; -use ark_std::{borrow::Borrow, iterable::Iterable, vec::Vec}; +use ark_std::vec::Vec; fn naive_var_base_msm(bases: &[G::MulBase], scalars: &[G::ScalarField]) -> G { From d996417c1210a4728cc9c0d076414fbf06e18fa8 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Wed, 30 Nov 2022 11:37:47 +0100 Subject: [PATCH 04/18] fmt --- ec/src/models/short_weierstrass/group.rs | 10 +++++----- ec/src/models/short_weierstrass/mod.rs | 13 ++++++------- test-templates/src/msm.rs | 1 - 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index 3e3bf9586..b58fb0061 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -636,11 +636,11 @@ impl ScalarMul for Projective

{ impl VariableBaseMSM for Projective

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

{ diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index 8742bb950..b19dd93bf 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, PrimeField}; -use crate::{AffineRepr, Group, scalar_mul::variable_base::VariableBaseMSM}; +use crate::{scalar_mul::variable_base::VariableBaseMSM, AffineRepr, Group}; use num_traits::Zero; @@ -107,12 +107,11 @@ pub trait SWCurveConfig: super::CurveConfig { /// Default implementation for multi scalar multiplication fn msm_bigint( - bases: &[Affine], - bigints: &[::BigInt], - ) -> Projective - { - VariableBaseMSM::msm_bigint(bases, bigints) - } + bases: &[Affine], + bigints: &[::BigInt], + ) -> Projective { + VariableBaseMSM::msm_bigint(bases, bigints) + } /// 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 diff --git a/test-templates/src/msm.rs b/test-templates/src/msm.rs index 3c1a5a7cf..259b6376d 100644 --- a/test-templates/src/msm.rs +++ b/test-templates/src/msm.rs @@ -5,7 +5,6 @@ use ark_ec::{ 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(); From c394b079333f34f7b3165725eb559967c44ac3c1 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Wed, 30 Nov 2022 17:01:38 +0100 Subject: [PATCH 05/18] conditional compilation with specialization feature --- ec/src/models/short_weierstrass/group.rs | 13 ++++++++++++- ec/src/models/short_weierstrass/mod.rs | 8 -------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index b58fb0061..d7e1217d5 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -634,7 +634,16 @@ impl ScalarMul for Projective

{ } } -impl VariableBaseMSM for Projective

{ +#[cfg(feature = "specialization")] +pub trait CustomMSM: SWCurveConfig { + fn msm_bigint( + bases: &[Affine], + bigints: &[::BigInt], + ) -> Projective; +} + +#[cfg(feature = "specialization")] +impl VariableBaseMSM for Projective

where P: CustomMSM { fn msm_bigint( bases: &[Self::MulBase], bigints: &[::BigInt], @@ -643,6 +652,8 @@ impl VariableBaseMSM for Projective

{ } } +impl VariableBaseMSM for Projective

{} + impl>> core::iter::Sum for Projective

{ fn sum>(iter: I) -> Self { iter.fold(Projective::zero(), |sum, x| sum + x.borrow()) diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index b19dd93bf..c4ddff9c0 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -105,14 +105,6 @@ pub trait SWCurveConfig: super::CurveConfig { res } - /// Default implementation for multi scalar multiplication - fn msm_bigint( - bases: &[Affine], - bigints: &[::BigInt], - ) -> Projective { - VariableBaseMSM::msm_bigint(bases, bigints) - } - /// 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. From d08539bc622a98dc68f60ee9a25fc2f3060e0c23 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Thu, 1 Dec 2022 08:26:59 +0100 Subject: [PATCH 06/18] Revert "conditional compilation with specialization feature" This reverts commit c394b079333f34f7b3165725eb559967c44ac3c1. --- ec/src/models/short_weierstrass/group.rs | 13 +------------ ec/src/models/short_weierstrass/mod.rs | 8 ++++++++ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index d7e1217d5..b58fb0061 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -634,16 +634,7 @@ impl ScalarMul for Projective

{ } } -#[cfg(feature = "specialization")] -pub trait CustomMSM: SWCurveConfig { - fn msm_bigint( - bases: &[Affine], - bigints: &[::BigInt], - ) -> Projective; -} - -#[cfg(feature = "specialization")] -impl VariableBaseMSM for Projective

where P: CustomMSM { +impl VariableBaseMSM for Projective

{ fn msm_bigint( bases: &[Self::MulBase], bigints: &[::BigInt], @@ -652,8 +643,6 @@ impl VariableBaseMSM for Projective

where P: CustomMSM { } } -impl VariableBaseMSM for Projective

{} - impl>> core::iter::Sum for Projective

{ fn sum>(iter: I) -> Self { iter.fold(Projective::zero(), |sum, x| sum + x.borrow()) diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index c4ddff9c0..b19dd93bf 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -105,6 +105,14 @@ pub trait SWCurveConfig: super::CurveConfig { res } + /// Default implementation for multi scalar multiplication + fn msm_bigint( + bases: &[Affine], + bigints: &[::BigInt], + ) -> Projective { + VariableBaseMSM::msm_bigint(bases, bigints) + } + /// 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. From d24a36a2f8f7ef8d855a70b15de0888e12dcf96d Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Thu, 1 Dec 2022 08:27:03 +0100 Subject: [PATCH 07/18] Revert "fmt" This reverts commit d996417c1210a4728cc9c0d076414fbf06e18fa8. --- ec/src/models/short_weierstrass/group.rs | 10 +++++----- ec/src/models/short_weierstrass/mod.rs | 13 +++++++------ test-templates/src/msm.rs | 1 + 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index b58fb0061..3e3bf9586 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -636,11 +636,11 @@ impl ScalarMul for Projective

{ impl VariableBaseMSM for Projective

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

{ diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index b19dd93bf..8742bb950 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, PrimeField}; -use crate::{scalar_mul::variable_base::VariableBaseMSM, AffineRepr, Group}; +use crate::{AffineRepr, Group, scalar_mul::variable_base::VariableBaseMSM}; use num_traits::Zero; @@ -107,11 +107,12 @@ pub trait SWCurveConfig: super::CurveConfig { /// Default implementation for multi scalar multiplication fn msm_bigint( - bases: &[Affine], - bigints: &[::BigInt], - ) -> Projective { - VariableBaseMSM::msm_bigint(bases, bigints) - } + bases: &[Affine], + bigints: &[::BigInt], + ) -> Projective + { + VariableBaseMSM::msm_bigint(bases, bigints) + } /// 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 diff --git a/test-templates/src/msm.rs b/test-templates/src/msm.rs index 259b6376d..3c1a5a7cf 100644 --- a/test-templates/src/msm.rs +++ b/test-templates/src/msm.rs @@ -5,6 +5,7 @@ use ark_ec::{ 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(); From 7272ecb2d0973ca9f2a45d3feb8c03cf74fa0fd2 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Mon, 5 Dec 2022 11:46:34 +0100 Subject: [PATCH 08/18] implement unhooked bigint_msm --- ec/src/models/short_weierstrass/group.rs | 10 +++++----- ec/src/models/short_weierstrass/mod.rs | 4 ++-- ec/src/scalar_mul/variable_base/mod.rs | 8 ++++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index 3e3bf9586..f93034a30 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -636,11 +636,11 @@ impl ScalarMul for Projective

{ impl VariableBaseMSM for Projective

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

{ diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index 8742bb950..36dd3961e 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -106,12 +106,12 @@ pub trait SWCurveConfig: super::CurveConfig { } /// Default implementation for multi scalar multiplication - fn msm_bigint( + fn model_msm_bigint( bases: &[Affine], bigints: &[::BigInt], ) -> Projective { - VariableBaseMSM::msm_bigint(bases, bigints) + VariableBaseMSM::unhooked_msm_bigint(bases, bigints) } /// If uncompressed, serializes both x and y coordinates as well as a bit for whether it is diff --git a/ec/src/scalar_mul/variable_base/mod.rs b/ec/src/scalar_mul/variable_base/mod.rs index f8840763c..4600b7c40 100644 --- a/ec/src/scalar_mul/variable_base/mod.rs +++ b/ec/src/scalar_mul/variable_base/mod.rs @@ -44,6 +44,14 @@ pub trait VariableBaseMSM: ScalarMul { fn msm_bigint( bases: &[Self::MulBase], bigints: &[::BigInt], + ) -> Self { + Self::unhooked_msm_bigint(bases, bigints) + } + + /// /// Raw optimized implementation of multi-scalar multiplication, only for usage by curve models. + fn unhooked_msm_bigint( + bases: &[Self::MulBase], + bigints: &[::BigInt], ) -> Self { let size = ark_std::cmp::min(bases.len(), bigints.len()); let scalars = &bigints[..size]; From c58bda4241b5636fa52ca0df94f9996a1210a482 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Mon, 5 Dec 2022 11:49:24 +0100 Subject: [PATCH 09/18] fmt --- ec/src/models/short_weierstrass/mod.rs | 13 ++++++------- test-templates/src/msm.rs | 1 - 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index 36dd3961e..8350c893c 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, PrimeField}; -use crate::{AffineRepr, Group, scalar_mul::variable_base::VariableBaseMSM}; +use crate::{scalar_mul::variable_base::VariableBaseMSM, AffineRepr, Group}; use num_traits::Zero; @@ -107,12 +107,11 @@ pub trait SWCurveConfig: super::CurveConfig { /// Default implementation for multi scalar multiplication fn model_msm_bigint( - bases: &[Affine], - bigints: &[::BigInt], - ) -> Projective - { - VariableBaseMSM::unhooked_msm_bigint(bases, bigints) - } + bases: &[Affine], + bigints: &[::BigInt], + ) -> Projective { + VariableBaseMSM::unhooked_msm_bigint(bases, bigints) + } /// 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 diff --git a/test-templates/src/msm.rs b/test-templates/src/msm.rs index 3c1a5a7cf..259b6376d 100644 --- a/test-templates/src/msm.rs +++ b/test-templates/src/msm.rs @@ -5,7 +5,6 @@ use ark_ec::{ 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(); From 0fee551094df231f4197baf5b6d1a76243b0604a Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Fri, 9 Dec 2022 19:11:03 +0100 Subject: [PATCH 10/18] add description to CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c69b1677c..18a4ee740 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Pending +- [\#528](https://github.com/arkworks-rs/algebra/pull/528) (`ark-ec`) Allow to overwrite the default implementation of the `bigint_msm` function provided by the `VariableBaseMSM` trait by a specialized version in `SWCurveConfig`. + ### Breaking changes - [\#300](https://github.com/arkworks-rs/algebra/pull/300) (`ark-ec`) Change the implementation of `Hash` trait of `GroupProjective` to use the affine coordinates. From abd0b56fae078c21fd5cb6ee62f1cde702aba522 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Fri, 9 Dec 2022 19:12:18 +0100 Subject: [PATCH 11/18] unhooked_msm_bigint -> default_msm_bigint --- ec/src/models/short_weierstrass/mod.rs | 2 +- ec/src/scalar_mul/variable_base/mod.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index 8350c893c..b5acbeaa7 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -110,7 +110,7 @@ pub trait SWCurveConfig: super::CurveConfig { bases: &[Affine], bigints: &[::BigInt], ) -> Projective { - VariableBaseMSM::unhooked_msm_bigint(bases, bigints) + VariableBaseMSM::default_msm_bigint(bases, bigints) } /// If uncompressed, serializes both x and y coordinates as well as a bit for whether it is diff --git a/ec/src/scalar_mul/variable_base/mod.rs b/ec/src/scalar_mul/variable_base/mod.rs index 4600b7c40..eaee9c1d5 100644 --- a/ec/src/scalar_mul/variable_base/mod.rs +++ b/ec/src/scalar_mul/variable_base/mod.rs @@ -45,11 +45,11 @@ pub trait VariableBaseMSM: ScalarMul { bases: &[Self::MulBase], bigints: &[::BigInt], ) -> Self { - Self::unhooked_msm_bigint(bases, bigints) + Self::default_msm_bigint(bases, bigints) } - /// /// Raw optimized implementation of multi-scalar multiplication, only for usage by curve models. - fn unhooked_msm_bigint( + /// Raw optimized implementation of multi-scalar multiplication, only for usage by curve models. + fn default_msm_bigint( bases: &[Self::MulBase], bigints: &[::BigInt], ) -> Self { From 96662cd0256e508b9720def90fe8109690736d05 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Mon, 12 Dec 2022 09:02:31 +0100 Subject: [PATCH 12/18] default_msm_bigint -> msm_bigint_default --- ec/src/scalar_mul/variable_base/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec/src/scalar_mul/variable_base/mod.rs b/ec/src/scalar_mul/variable_base/mod.rs index 160b7a7cf..26284987d 100644 --- a/ec/src/scalar_mul/variable_base/mod.rs +++ b/ec/src/scalar_mul/variable_base/mod.rs @@ -42,7 +42,7 @@ pub trait VariableBaseMSM: ScalarMul { bases: &[Self::MulBase], bigints: &[::BigInt], ) -> Self { - Self::default_msm_bigint(bases, bigints) + Self::msm_bigint_default(bases, bigints) } /// Raw optimized implementation of multi-scalar multiplication, only for usage by curve models. From afa53b561c5e39b1e80694948be211d8c4168325 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Mon, 12 Dec 2022 09:16:24 +0100 Subject: [PATCH 13/18] msm_bigint_default function signature --- ec/src/scalar_mul/variable_base/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ec/src/scalar_mul/variable_base/mod.rs b/ec/src/scalar_mul/variable_base/mod.rs index 26284987d..ff5fa666e 100644 --- a/ec/src/scalar_mul/variable_base/mod.rs +++ b/ec/src/scalar_mul/variable_base/mod.rs @@ -46,7 +46,10 @@ pub trait VariableBaseMSM: ScalarMul { } /// Raw optimized implementation of multi-scalar multiplication, only for usage by curve models. - fn msm_bigint_default() { + fn msm_bigint_default( + bases: &[Self::MulBase], + bigints: &[::BigInt], + ) -> Self { if Self::NEGATION_IS_CHEAP { msm_bigint_wnaf(bases, bigints) } else { From a7126e5aac28ae3a3d59e678f80150f544b52e89 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Mon, 12 Dec 2022 09:17:54 +0100 Subject: [PATCH 14/18] msm_bigint_default in SWCurveConfig --- ec/src/models/short_weierstrass/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index b5acbeaa7..84d637c84 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -110,7 +110,7 @@ pub trait SWCurveConfig: super::CurveConfig { bases: &[Affine], bigints: &[::BigInt], ) -> Projective { - VariableBaseMSM::default_msm_bigint(bases, bigints) + VariableBaseMSM::msm_bigint_default(bases, bigints) } /// If uncompressed, serializes both x and y coordinates as well as a bit for whether it is From f0d25f0bf0b4e9c1ade8aca35d82508ad39b80d6 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Mon, 12 Dec 2022 10:35:16 +0100 Subject: [PATCH 15/18] avoid hooked msm_bigint --- ec/src/models/short_weierstrass/group.rs | 8 ++++---- ec/src/models/short_weierstrass/mod.rs | 12 +++++++----- ec/src/scalar_mul/variable_base/mod.rs | 10 +--------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index 5bf57cfe6..35bb7bd31 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -636,11 +636,11 @@ impl ScalarMul for Projective

{ } impl VariableBaseMSM for Projective

{ - fn msm_bigint( + fn msm( bases: &[Self::MulBase], - bigints: &[::BigInt], - ) -> Self { - P::model_msm_bigint(bases, bigints) + bigints: &[Self::ScalarField], + ) -> Result { + P::model_msm(bases, bigints) } } diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index 84d637c84..eb9ad7d22 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -4,7 +4,7 @@ use ark_serialize::{ }; use ark_std::io::{Read, Write}; -use ark_ff::{fields::Field, PrimeField}; +use ark_ff::fields::Field; use crate::{scalar_mul::variable_base::VariableBaseMSM, AffineRepr, Group}; @@ -106,11 +106,13 @@ pub trait SWCurveConfig: super::CurveConfig { } /// Default implementation for multi scalar multiplication - fn model_msm_bigint( + fn model_msm( bases: &[Affine], - bigints: &[::BigInt], - ) -> Projective { - VariableBaseMSM::msm_bigint_default(bases, bigints) + 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 diff --git a/ec/src/scalar_mul/variable_base/mod.rs b/ec/src/scalar_mul/variable_base/mod.rs index ff5fa666e..0c6f58330 100644 --- a/ec/src/scalar_mul/variable_base/mod.rs +++ b/ec/src/scalar_mul/variable_base/mod.rs @@ -41,14 +41,6 @@ pub trait VariableBaseMSM: ScalarMul { fn msm_bigint( bases: &[Self::MulBase], bigints: &[::BigInt], - ) -> Self { - Self::msm_bigint_default(bases, bigints) - } - - /// Raw optimized implementation of multi-scalar multiplication, only for usage by curve models. - fn msm_bigint_default( - bases: &[Self::MulBase], - bigints: &[::BigInt], ) -> Self { if Self::NEGATION_IS_CHEAP { msm_bigint_wnaf(bases, bigints) @@ -294,4 +286,4 @@ fn make_digits(a: &impl BigInteger, w: usize, num_bits: usize) -> Vec { digits[digits_count - 1] += (carry << w) as i64; digits -} +} \ No newline at end of file From 3e626022a0310b2bdbfa898f07816211c996d57b Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Mon, 12 Dec 2022 10:41:25 +0100 Subject: [PATCH 16/18] fmt --- ec/src/models/short_weierstrass/group.rs | 5 +---- ec/src/scalar_mul/variable_base/mod.rs | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index 35bb7bd31..f5e169031 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -636,10 +636,7 @@ impl ScalarMul for Projective

{ } impl VariableBaseMSM for Projective

{ - fn msm( - bases: &[Self::MulBase], - bigints: &[Self::ScalarField], - ) -> Result { + fn msm(bases: &[Self::MulBase], bigints: &[Self::ScalarField]) -> Result { P::model_msm(bases, bigints) } } diff --git a/ec/src/scalar_mul/variable_base/mod.rs b/ec/src/scalar_mul/variable_base/mod.rs index 0c6f58330..2c139b17e 100644 --- a/ec/src/scalar_mul/variable_base/mod.rs +++ b/ec/src/scalar_mul/variable_base/mod.rs @@ -286,4 +286,4 @@ fn make_digits(a: &impl BigInteger, w: usize, num_bits: usize) -> Vec { digits[digits_count - 1] += (carry << w) as i64; digits -} \ No newline at end of file +} From 451671e9cac02125741ac8d007cf735e08812f20 Mon Sep 17 00:00:00 2001 From: Achim Schneider Date: Mon, 12 Dec 2022 14:00:07 +0100 Subject: [PATCH 17/18] MD: msm_bigint -> msm --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eb238fc7..6032800d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Pending -- [\#528](https://github.com/arkworks-rs/algebra/pull/528) (`ark-ec`) Allow to overwrite the default implementation of the `bigint_msm` function provided by the `VariableBaseMSM` trait by a specialized version in `SWCurveConfig`. +- [\#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 From 642ba2aec7ec5bc6eabd58c182650fe5b928cfe4 Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Mon, 12 Dec 2022 13:03:38 -0800 Subject: [PATCH 18/18] =?UTF-8?q?Change=20`model=5Fmsm`=20=E2=86=92=20`msm?= =?UTF-8?q?`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ec/src/models/short_weierstrass/group.rs | 2 +- ec/src/models/short_weierstrass/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ec/src/models/short_weierstrass/group.rs b/ec/src/models/short_weierstrass/group.rs index f5e169031..99fa901d2 100644 --- a/ec/src/models/short_weierstrass/group.rs +++ b/ec/src/models/short_weierstrass/group.rs @@ -637,7 +637,7 @@ impl ScalarMul for Projective

{ impl VariableBaseMSM for Projective

{ fn msm(bases: &[Self::MulBase], bigints: &[Self::ScalarField]) -> Result { - P::model_msm(bases, bigints) + P::msm(bases, bigints) } } diff --git a/ec/src/models/short_weierstrass/mod.rs b/ec/src/models/short_weierstrass/mod.rs index eb9ad7d22..3f5e0e9ed 100644 --- a/ec/src/models/short_weierstrass/mod.rs +++ b/ec/src/models/short_weierstrass/mod.rs @@ -106,7 +106,7 @@ pub trait SWCurveConfig: super::CurveConfig { } /// Default implementation for multi scalar multiplication - fn model_msm( + fn msm( bases: &[Affine], scalars: &[Self::ScalarField], ) -> Result, usize> {