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

Add GLV for simple curves: BW6-767, Grumpkin, secp256k1, secq256k1 #778

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 39 additions & 1 deletion curves/bw6_767/src/curves/g1.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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, <Self::ScalarField as PrimeField>::BigInt); 4] = [
(
false,
BigInt!("1155048275357884106335086113613464118768280431093290937003"),
),
(
true,
BigInt!("1155048275357884106335086113613464118783412807316232579754"),
),
(
false,
BigInt!("2310096550715768212670172227226928237551693238409523516757"),
),
(
false,
BigInt!("1155048275357884106335086113613464118768280431093290937003"),
),
];

fn endomorphism(p: &Projective<Self>) -> Projective<Self> {
let mut res = (*p).clone();
res.x *= Self::ENDO_COEFFS[0];
res
}

fn endomorphism_affine(p: &Affine<Self>) -> Affine<Self> {
let mut res = (*p).clone();
res.x *= Self::ENDO_COEFFS[0];
res
}
}

/// G1_GENERATOR_X =
/// 127687253511432941835499154999732953539969793860764514205013635996439242747457934431893570832266740963864950713809357287070846939000367049554519743864924323440810949629217677483481194663331926309250818003412838087592587472550707218
pub const G1_GENERATOR_X: Fq = MontFp!("127687253511432941835499154999732953539969793860764514205013635996439242747457934431893570832266740963864950713809357287070846939000367049554519743864924323440810949629217677483481194663331926309250818003412838087592587472550707218");
Expand Down
40 changes: 39 additions & 1 deletion curves/bw6_767/src/curves/g2.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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, <Self::ScalarField as PrimeField>::BigInt); 4] = [
(
false,
BigInt!("1155048275357884106335086113613464118783412807316232579754"),
),
(
true,
BigInt!("1155048275357884106335086113613464118768280431093290937003"),
),
(
false,
BigInt!("1155048275357884106335086113613464118768280431093290937003"),
),
(
false,
BigInt!("2310096550715768212670172227226928237551693238409523516757"),
),
];

fn endomorphism(p: &Projective<Self>) -> Projective<Self> {
let mut res = (*p).clone();
res.x *= Self::ENDO_COEFFS[0];
res
}

fn endomorphism_affine(p: &Affine<Self>) -> Affine<Self> {
let mut res = (*p).clone();
res.x *= Self::ENDO_COEFFS[0];
res
}
}

/// G2_GENERATOR_X =
/// 370611171465172359348863648443534520144617072349884185652206813771489664034831143983178049920510836078361116088420840622225267322852644540540617123958979924966938307707664543525950567252218300954395355151658118858470703533448342222
pub const G2_GENERATOR_X: Fq = MontFp!("370611171465172359348863648443534520144617072349884185652206813771489664034831143983178049920510836078361116088420840622225267322852644540540617123958979924966938307707664543525950567252218300954395355151658118858470703533448342222");
Expand Down
2 changes: 2 additions & 0 deletions curves/bw6_767/src/curves/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ test_group!(g1; G1Projective; sw);
test_group!(g2; G2Projective; sw);
test_group!(pairing_output; ark_ec::pairing::PairingOutput<BW6_767>; msm);
test_pairing!(pairing; crate::BW6_767);
test_group!(g1_glv; G1Projective; glv);
test_group!(g2_glv; G2Projective; glv);
29 changes: 28 additions & 1 deletion curves/grumpkin/src/curves/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, <Self::ScalarField as PrimeField>::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");

Expand Down
1 change: 1 addition & 0 deletions curves/grumpkin/src/curves/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ use crate::Projective;
use ark_algebra_test_templates::*;

test_group!(g1; Projective; sw);
test_group!(g1_glv; Projective; glv);
29 changes: 28 additions & 1 deletion curves/secp256k1/src/curves/mod.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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, <Self::ScalarField as PrimeField>::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 =
Expand Down
1 change: 1 addition & 0 deletions curves/secp256k1/src/curves/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ use crate::Projective;
use ark_algebra_test_templates::*;

test_group!(g1; Projective; sw);
test_group!(gl_glv; Projective; glv);
29 changes: 28 additions & 1 deletion curves/secq256k1/src/curves/mod.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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, <Self::ScalarField as PrimeField>::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 =
Expand Down
1 change: 1 addition & 0 deletions curves/secq256k1/src/curves/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ use crate::Projective;
use ark_algebra_test_templates::*;

test_group!(g1; Projective; sw);
test_group!(g1_glv; Projective; glv);
Loading