From 5deb6fe1f849347b6a2174fd40e042be3e931243 Mon Sep 17 00:00:00 2001 From: weikengchen Date: Tue, 13 Feb 2024 21:53:58 +0800 Subject: [PATCH 1/2] update the glv --- curves/bw6_767/src/curves/g1.rs | 40 +++++++++++++++++++++++++++- curves/bw6_767/src/curves/g2.rs | 40 +++++++++++++++++++++++++++- curves/bw6_767/src/curves/tests.rs | 2 ++ curves/grumpkin/src/curves/mod.rs | 29 +++++++++++++++++++- curves/grumpkin/src/curves/tests.rs | 1 + curves/secp256k1/src/curves/mod.rs | 29 +++++++++++++++++++- curves/secp256k1/src/curves/tests.rs | 1 + curves/secq256k1/src/curves/mod.rs | 29 +++++++++++++++++++- curves/secq256k1/src/curves/tests.rs | 1 + 9 files changed, 167 insertions(+), 5 deletions(-) diff --git a/curves/bw6_767/src/curves/g1.rs b/curves/bw6_767/src/curves/g1.rs index 51d4077a1..acc22bc66 100644 --- a/curves/bw6_767/src/curves/g1.rs +++ b/curves/bw6_767/src/curves/g1.rs @@ -1,8 +1,9 @@ use ark_ec::{ models::{short_weierstrass::SWCurveConfig, CurveConfig}, + scalar_mul::glv::GLVConfig, short_weierstrass::{Affine, Projective}, }; -use ark_ff::{AdditiveGroup, MontFp}; +use ark_ff::{AdditiveGroup, BigInt, MontFp, PrimeField}; use crate::{Fq, Fr}; @@ -50,6 +51,43 @@ impl SWCurveConfig for Config { } } +impl GLVConfig for Config { + const ENDO_COEFFS: &'static [Self::BaseField] = &[ + MontFp!("451452499708746243421442696394275804592767119751118962106882058158528025766103643615697202253207413006991058800455542766924935899310685166148099708594514571753800103096705086912881023032622324847956780035251378028187894066092550170") + ]; + const LAMBDA: Self::ScalarField = (MontFp!("4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436")); + const SCALAR_DECOMP_COEFFS: [(bool, ::BigInt); 4] = [ + ( + false, + BigInt!("1155048275357884106335086113613464118768280431093290937003"), + ), + ( + true, + BigInt!("1155048275357884106335086113613464118783412807316232579754"), + ), + ( + false, + BigInt!("2310096550715768212670172227226928237551693238409523516757"), + ), + ( + false, + BigInt!("1155048275357884106335086113613464118768280431093290937003"), + ), + ]; + + fn endomorphism(p: &Projective) -> Projective { + let mut res = (*p).clone(); + res.x *= Self::ENDO_COEFFS[0]; + res + } + + fn endomorphism_affine(p: &Affine) -> Affine { + let mut res = (*p).clone(); + res.x *= Self::ENDO_COEFFS[0]; + res + } +} + /// G1_GENERATOR_X = /// 127687253511432941835499154999732953539969793860764514205013635996439242747457934431893570832266740963864950713809357287070846939000367049554519743864924323440810949629217677483481194663331926309250818003412838087592587472550707218 pub const G1_GENERATOR_X: Fq = MontFp!("127687253511432941835499154999732953539969793860764514205013635996439242747457934431893570832266740963864950713809357287070846939000367049554519743864924323440810949629217677483481194663331926309250818003412838087592587472550707218"); diff --git a/curves/bw6_767/src/curves/g2.rs b/curves/bw6_767/src/curves/g2.rs index d56c85fd0..ab13d890a 100644 --- a/curves/bw6_767/src/curves/g2.rs +++ b/curves/bw6_767/src/curves/g2.rs @@ -1,8 +1,9 @@ +use ark_ec::scalar_mul::glv::GLVConfig; use ark_ec::{ models::{short_weierstrass::SWCurveConfig, CurveConfig}, short_weierstrass::{Affine, Projective}, }; -use ark_ff::{AdditiveGroup, MontFp}; +use ark_ff::{AdditiveGroup, BigInt, MontFp, PrimeField}; use crate::{Fq, Fr}; @@ -51,6 +52,43 @@ impl SWCurveConfig for Config { } } +impl GLVConfig for Config { + const ENDO_COEFFS: &'static [Self::BaseField] = &[ + MontFp!("451452499708746243421442696394275804592767119751118962106882058158528025766103643615697202253207413006991058800455542766924935899310685166148099708594514571753800103096705086912881023032622324847956780035251378028187894066092550170") + ]; + const LAMBDA: Self::ScalarField = (MontFp!("793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350")); + const SCALAR_DECOMP_COEFFS: [(bool, ::BigInt); 4] = [ + ( + false, + BigInt!("1155048275357884106335086113613464118783412807316232579754"), + ), + ( + true, + BigInt!("1155048275357884106335086113613464118768280431093290937003"), + ), + ( + false, + BigInt!("1155048275357884106335086113613464118768280431093290937003"), + ), + ( + false, + BigInt!("2310096550715768212670172227226928237551693238409523516757"), + ), + ]; + + fn endomorphism(p: &Projective) -> Projective { + let mut res = (*p).clone(); + res.x *= Self::ENDO_COEFFS[0]; + res + } + + fn endomorphism_affine(p: &Affine) -> Affine { + let mut res = (*p).clone(); + res.x *= Self::ENDO_COEFFS[0]; + res + } +} + /// G2_GENERATOR_X = /// 370611171465172359348863648443534520144617072349884185652206813771489664034831143983178049920510836078361116088420840622225267322852644540540617123958979924966938307707664543525950567252218300954395355151658118858470703533448342222 pub const G2_GENERATOR_X: Fq = MontFp!("370611171465172359348863648443534520144617072349884185652206813771489664034831143983178049920510836078361116088420840622225267322852644540540617123958979924966938307707664543525950567252218300954395355151658118858470703533448342222"); diff --git a/curves/bw6_767/src/curves/tests.rs b/curves/bw6_767/src/curves/tests.rs index 949d9893f..bc79f130b 100644 --- a/curves/bw6_767/src/curves/tests.rs +++ b/curves/bw6_767/src/curves/tests.rs @@ -6,3 +6,5 @@ test_group!(g1; G1Projective; sw); test_group!(g2; G2Projective; sw); test_group!(pairing_output; ark_ec::pairing::PairingOutput; msm); test_pairing!(pairing; crate::BW6_767); +test_group!(g1_glv; G1Projective; glv); +test_group!(g2_glv; G2Projective; glv); diff --git a/curves/grumpkin/src/curves/mod.rs b/curves/grumpkin/src/curves/mod.rs index 735fe2c78..ab523ba04 100644 --- a/curves/grumpkin/src/curves/mod.rs +++ b/curves/grumpkin/src/curves/mod.rs @@ -2,11 +2,12 @@ // https://github.com/AztecProtocol/barretenberg/blob/97ccf76c42db581a8b8f8bfbcffe8ca015a3dd22/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp use crate::{fq::Fq, fr::Fr}; +use ark_ec::scalar_mul::glv::GLVConfig; use ark_ec::{ models::CurveConfig, short_weierstrass::{self as sw, SWCurveConfig}, }; -use ark_ff::{AdditiveGroup, Field, MontFp, Zero}; +use ark_ff::{AdditiveGroup, BigInt, Field, MontFp, PrimeField, Zero}; #[cfg(test)] mod tests; @@ -44,6 +45,32 @@ impl SWCurveConfig for GrumpkinConfig { } } +impl GLVConfig for GrumpkinConfig { + const ENDO_COEFFS: &'static [Self::BaseField] = &[MontFp!( + "21888242871839275217838484774961031246154997185409878258781734729429964517155" + )]; + const LAMBDA: Self::ScalarField = + (MontFp!("21888242871839275220042445260109153167277707414472061641714758635765020556616")); + const SCALAR_DECOMP_COEFFS: [(bool, ::BigInt); 4] = [ + (false, BigInt!("9931322734385697762")), + (false, BigInt!("147946756881789319010696353538189108491")), + (false, BigInt!("147946756881789319000765030803803410729")), + (true, BigInt!("9931322734385697762")), + ]; + + fn endomorphism(p: &Projective) -> Projective { + let mut res = (*p).clone(); + res.x *= Self::ENDO_COEFFS[0]; + res + } + + fn endomorphism_affine(p: &Affine) -> Affine { + let mut res = (*p).clone(); + res.x *= Self::ENDO_COEFFS[0]; + res + } +} + /// G_GENERATOR_X = 1 pub const G_GENERATOR_X: Fq = MontFp!("1"); diff --git a/curves/grumpkin/src/curves/tests.rs b/curves/grumpkin/src/curves/tests.rs index f7bc50cd2..19fc73128 100755 --- a/curves/grumpkin/src/curves/tests.rs +++ b/curves/grumpkin/src/curves/tests.rs @@ -2,3 +2,4 @@ use crate::Projective; use ark_algebra_test_templates::*; test_group!(g1; Projective; sw); +test_group!(g1_glv; Projective; glv); diff --git a/curves/secp256k1/src/curves/mod.rs b/curves/secp256k1/src/curves/mod.rs index 5adc6d1c9..e55f14aea 100644 --- a/curves/secp256k1/src/curves/mod.rs +++ b/curves/secp256k1/src/curves/mod.rs @@ -1,8 +1,9 @@ +use ark_ec::scalar_mul::glv::GLVConfig; use ark_ec::{ models::CurveConfig, short_weierstrass::{self as sw, SWCurveConfig}, }; -use ark_ff::{AdditiveGroup, Field, MontFp, Zero}; +use ark_ff::{AdditiveGroup, BigInt, Field, MontFp, PrimeField, Zero}; use crate::{fq::Fq, fr::Fr}; @@ -43,6 +44,32 @@ impl SWCurveConfig for Config { } } +impl GLVConfig for Config { + const ENDO_COEFFS: &'static [Self::BaseField] = &[MontFp!( + "60197513588986302554485582024885075108884032450952339817679072026166228089408" + )]; + const LAMBDA: Self::ScalarField = + (MontFp!("78074008874160198520644763525212887401909906723592317393988542598630163514318")); + const SCALAR_DECOMP_COEFFS: [(bool, ::BigInt); 4] = [ + (false, BigInt!("64502973549206556628585045361533709077")), + (false, BigInt!("367917413016453100223835821029139468248")), + (false, BigInt!("303414439467246543595250775667605759171")), + (true, BigInt!("64502973549206556628585045361533709077")), + ]; + + fn endomorphism(p: &Projective) -> Projective { + let mut res = (*p).clone(); + res.x *= Self::ENDO_COEFFS[0]; + res + } + + fn endomorphism_affine(p: &Affine) -> Affine { + let mut res = (*p).clone(); + res.x *= Self::ENDO_COEFFS[0]; + res + } +} + /// G_GENERATOR_X = /// 55066263022277343669578718895168534326250603453777594175500187360389116729240 pub const G_GENERATOR_X: Fq = diff --git a/curves/secp256k1/src/curves/tests.rs b/curves/secp256k1/src/curves/tests.rs index f7bc50cd2..2835ba8af 100755 --- a/curves/secp256k1/src/curves/tests.rs +++ b/curves/secp256k1/src/curves/tests.rs @@ -2,3 +2,4 @@ use crate::Projective; use ark_algebra_test_templates::*; test_group!(g1; Projective; sw); +test_group!(gl_glv; Projective; glv); diff --git a/curves/secq256k1/src/curves/mod.rs b/curves/secq256k1/src/curves/mod.rs index e2ca1a7b9..1794c2b9f 100644 --- a/curves/secq256k1/src/curves/mod.rs +++ b/curves/secq256k1/src/curves/mod.rs @@ -1,8 +1,9 @@ +use ark_ec::scalar_mul::glv::GLVConfig; use ark_ec::{ models::CurveConfig, short_weierstrass::{self as sw, SWCurveConfig}, }; -use ark_ff::{AdditiveGroup, Field, MontFp, Zero}; +use ark_ff::{AdditiveGroup, BigInt, Field, MontFp, PrimeField, Zero}; use crate::{fq::Fq, fr::Fr}; @@ -43,6 +44,32 @@ impl SWCurveConfig for Config { } } +impl GLVConfig for Config { + const ENDO_COEFFS: &'static [Self::BaseField] = &[MontFp!( + "78074008874160198520644763525212887401909906723592317393988542598630163514318" + )]; + const LAMBDA: Self::ScalarField = + (MontFp!("60197513588986302554485582024885075108884032450952339817679072026166228089408")); + const SCALAR_DECOMP_COEFFS: [(bool, ::BigInt); 4] = [ + (false, BigInt!("64502973549206556628585045361533709078")), + (false, BigInt!("367917413016453100223835821029139468249")), + (false, BigInt!("303414439467246543595250775667605759171")), + (true, BigInt!("64502973549206556628585045361533709078")), + ]; + + fn endomorphism(p: &Projective) -> Projective { + let mut res = (*p).clone(); + res.x *= Self::ENDO_COEFFS[0]; + res + } + + fn endomorphism_affine(p: &Affine) -> Affine { + let mut res = (*p).clone(); + res.x *= Self::ENDO_COEFFS[0]; + res + } +} + /// G_GENERATOR_X = /// 53718550993811904772965658690407829053653678808745171666022356150019200052646 pub const G_GENERATOR_X: Fq = diff --git a/curves/secq256k1/src/curves/tests.rs b/curves/secq256k1/src/curves/tests.rs index f7bc50cd2..19fc73128 100755 --- a/curves/secq256k1/src/curves/tests.rs +++ b/curves/secq256k1/src/curves/tests.rs @@ -2,3 +2,4 @@ use crate::Projective; use ark_algebra_test_templates::*; test_group!(g1; Projective; sw); +test_group!(g1_glv; Projective; glv); From be71d044ae41d96fbfe5e34fb264b46ca568990b Mon Sep 17 00:00:00 2001 From: weikengchen Date: Tue, 13 Feb 2024 22:05:25 +0800 Subject: [PATCH 2/2] update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9bfa6806..68b5d920c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ - [\#736](https://github.com/arkworks-rs/algebra/pull/736) (`ark-ff`) Deprecate `divn()`, and use `core::ops::{Shr, ShrAssign}` instead. - [\#739](https://github.com/arkworks-rs/algebra/pull/739) (`ark-ff`) Deprecate `muln()`, and use `core::ops::{Shl, ShlAssign}` instead. - [\#771](https://github.com/arkworks-rs/algebra/pull/771) (`ark-ec`) Omit expensive scalar multiplication in `is_in_correct_subgroup_assuming_on_curve()` for short Weierstrass curves of cofactor one. +- [\#778](https://github.com/arkworks-rs/algebra/pull/778) (`ark-bw6-767`, `ark-grumpkin`, `ark-secp256k1`, `ark-secq25k1`) Add the GLV implementation for BW6-767, Grumpkin, secp256k1, and secq256k1. ### Bugfixes