Catch up with algebra (#106)

Co-authored-by: Pratyush Mishra <pratyushmishra@berkeley.edu>
This commit is contained in:
Marcin
2022-07-29 21:16:16 +02:00
committed by GitHub
parent 76579d0fbb
commit 93e64df895
120 changed files with 934 additions and 1653 deletions

View File

@@ -1,10 +1,10 @@
use ark_ec::{
mnt6,
models::{ModelParameters, SWModelParameters},
models::{short_weierstrass::SWCurveConfig, CurveConfig},
};
use ark_ff::MontFp;
use ark_ff::{Field, MontFp};
use crate::{Fq, Fr, FR_ONE};
use crate::{Fq, Fr};
pub type G1Affine = mnt6::G1Affine<crate::Parameters>;
pub type G1Projective = mnt6::G1Projective<crate::Parameters>;
@@ -13,7 +13,7 @@ pub type G1Prepared = mnt6::G1Prepared<crate::Parameters>;
#[derive(Clone, Default, PartialEq, Eq)]
pub struct Parameters;
impl ModelParameters for Parameters {
impl CurveConfig for Parameters {
type BaseField = Fq;
type ScalarField = Fr;
@@ -21,19 +21,18 @@ impl ModelParameters for Parameters {
const COFACTOR: &'static [u64] = &[1];
/// COFACTOR^(-1) mod r = 1
const COFACTOR_INV: Fr = FR_ONE;
const COFACTOR_INV: Fr = Fr::ONE;
}
impl SWModelParameters for Parameters {
impl SWCurveConfig for Parameters {
/// COEFF_A = 11
const COEFF_A: Fq = MontFp!(Fq, "11");
const COEFF_A: Fq = MontFp!("11");
/// COEFF_B = 0x7DA285E70863C79D56446237CE2E1468D14AE9BB64B2BB01B10E60A5D5DFE0A25714B7985993F62F03B22A9A3C737A1A1E0FCF2C43D7BF847957C34CCA1E3585F9A80A95F401867C4E80F4747FDE5ABA7505BA6FCF2485540B13DFC8468A
const COEFF_B: Fq = MontFp!(Fq, "11625908999541321152027340224010374716841167701783584648338908235410859267060079819722747939267925389062611062156601938166010098747920378738927832658133625454260115409075816187555055859490253375704728027944315501122723426879114");
const COEFF_B: Fq = MontFp!("11625908999541321152027340224010374716841167701783584648338908235410859267060079819722747939267925389062611062156601938166010098747920378738927832658133625454260115409075816187555055859490253375704728027944315501122723426879114");
/// AFFINE_GENERATOR_COEFFS = (G1_GENERATOR_X, G1_GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
(G1_GENERATOR_X, G1_GENERATOR_Y);
const GENERATOR: G1Affine = G1Affine::new_unchecked(G1_GENERATOR_X, G1_GENERATOR_Y);
}
// Generator of G1
@@ -41,8 +40,8 @@ impl SWModelParameters for Parameters {
// Y = 27460508402331965149626600224382137254502975979168371111640924721589127725376473514838234361114855175488242007431439074223827742813911899817930728112297763448010814764117701403540298764970469500339646563344680868495474127850569,
/// G1_GENERATOR_X =
/// 3458420969484235708806261200128850544017070333833944116801482064540723268149235477762870414664917360605949659630933184751526227993647030875167687492714052872195770088225183259051403087906158701786758441889742618916006546636728,
pub const G1_GENERATOR_X: Fq = MontFp!(Fq, "3458420969484235708806261200128850544017070333833944116801482064540723268149235477762870414664917360605949659630933184751526227993647030875167687492714052872195770088225183259051403087906158701786758441889742618916006546636728");
pub const G1_GENERATOR_X: Fq = MontFp!("3458420969484235708806261200128850544017070333833944116801482064540723268149235477762870414664917360605949659630933184751526227993647030875167687492714052872195770088225183259051403087906158701786758441889742618916006546636728");
/// G1_GENERATOR_Y =
/// 27460508402331965149626600224382137254502975979168371111640924721589127725376473514838234361114855175488242007431439074223827742813911899817930728112297763448010814764117701403540298764970469500339646563344680868495474127850569,
pub const G1_GENERATOR_Y: Fq = MontFp!(Fq, "27460508402331965149626600224382137254502975979168371111640924721589127725376473514838234361114855175488242007431439074223827742813911899817930728112297763448010814764117701403540298764970469500339646563344680868495474127850569");
pub const G1_GENERATOR_Y: Fq = MontFp!("27460508402331965149626600224382137254502975979168371111640924721589127725376473514838234361114855175488242007431439074223827742813911899817930728112297763448010814764117701403540298764970469500339646563344680868495474127850569");

View File

@@ -1,11 +1,11 @@
use ark_ec::{
mnt6,
mnt6::MNT6Parameters,
models::{ModelParameters, SWModelParameters},
models::{short_weierstrass::SWCurveConfig, CurveConfig},
};
use ark_ff::{CubicExt, MontFp};
use ark_ff::{Field, MontFp};
use crate::{g1, Fq, Fq3, Fr, FQ_ZERO};
use crate::{g1, Fq, Fq3, Fr};
pub type G2Affine = mnt6::G2Affine<crate::Parameters>;
pub type G2Projective = mnt6::G2Projective<crate::Parameters>;
@@ -14,7 +14,7 @@ pub type G2Prepared = mnt6::G2Prepared<crate::Parameters>;
#[derive(Clone, Default, PartialEq, Eq)]
pub struct Parameters;
impl ModelParameters for Parameters {
impl CurveConfig for Parameters {
type BaseField = Fq3;
type ScalarField = Fr;
@@ -50,23 +50,23 @@ impl ModelParameters for Parameters {
/// COFACTOR^(-1) mod r =
/// 6983081827986492233724035798540106188028451653325658178630583820170892135428517795509815627298389820236345161981341515817589065927929152555581161598204976128690232061758269440757592419606754539638220064054062394397574161203200
const COFACTOR_INV: Fr = MontFp!(Fr, "6983081827986492233724035798540106188028451653325658178630583820170892135428517795509815627298389820236345161981341515817589065927929152555581161598204976128690232061758269440757592419606754539638220064054062394397574161203200");
const COFACTOR_INV: Fr = MontFp!("6983081827986492233724035798540106188028451653325658178630583820170892135428517795509815627298389820236345161981341515817589065927929152555581161598204976128690232061758269440757592419606754539638220064054062394397574161203200");
}
/// MUL_BY_A_C0 = NONRESIDUE * COEFF_A
/// = 11 * 11
/// = 121
pub const MUL_BY_A_C0: Fq = MontFp!(Fq, "121");
pub const MUL_BY_A_C0: Fq = MontFp!("121");
/// MUL_BY_A_C1 = NONRESIDUE * COEFF_A
/// = 11 * 11
/// = 121
pub const MUL_BY_A_C1: Fq = MontFp!(Fq, "121");
pub const MUL_BY_A_C1: Fq = MontFp!("121");
/// MUL_BY_A_C2 = COEFF_A
pub const MUL_BY_A_C2: Fq = g1::Parameters::COEFF_A;
impl SWModelParameters for Parameters {
impl SWCurveConfig for Parameters {
const COEFF_A: Fq3 = crate::Parameters::TWIST_COEFF_A;
// B coefficient of MNT6-753 G2 =
// ```
@@ -78,19 +78,18 @@ impl SWModelParameters for Parameters {
// (2189526091197672465268098090392210500740714959757583916377481826443393499947557697773546040576162515434508768057245887856591913752342600919117433675080691499697020523783784738694360040853591723916201150207746019687604267190251,
// 0, 0)
// ```
const COEFF_B: Fq3 = CubicExt!(
MontFp!(Fq, "2189526091197672465268098090392210500740714959757583916377481826443393499947557697773546040576162515434508768057245887856591913752342600919117433675080691499697020523783784738694360040853591723916201150207746019687604267190251"),
FQ_ZERO,
FQ_ZERO,
const COEFF_B: Fq3 = Fq3::new(
MontFp!("2189526091197672465268098090392210500740714959757583916377481826443393499947557697773546040576162515434508768057245887856591913752342600919117433675080691499697020523783784738694360040853591723916201150207746019687604267190251"),
Fq::ZERO,
Fq::ZERO,
);
/// AFFINE_GENERATOR_COEFFS = (G2_GENERATOR_X, G2_GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
(G2_GENERATOR_X, G2_GENERATOR_Y);
const GENERATOR: G2Affine = G2Affine::new_unchecked(G2_GENERATOR_X, G2_GENERATOR_Y);
#[inline(always)]
fn mul_by_a(elt: &Fq3) -> Fq3 {
CubicExt!(
Fq3::new(
MUL_BY_A_C0 * &elt.c1,
MUL_BY_A_C1 * &elt.c2,
MUL_BY_A_C2 * &elt.c0,
@@ -98,8 +97,8 @@ impl SWModelParameters for Parameters {
}
}
const G2_GENERATOR_X: Fq3 = CubicExt!(G2_GENERATOR_X_C0, G2_GENERATOR_X_C1, G2_GENERATOR_X_C2);
const G2_GENERATOR_Y: Fq3 = CubicExt!(G2_GENERATOR_Y_C0, G2_GENERATOR_Y_C1, G2_GENERATOR_Y_C2);
const G2_GENERATOR_X: Fq3 = Fq3::new(G2_GENERATOR_X_C0, G2_GENERATOR_X_C1, G2_GENERATOR_X_C2);
const G2_GENERATOR_Y: Fq3 = Fq3::new(G2_GENERATOR_Y_C0, G2_GENERATOR_Y_C1, G2_GENERATOR_Y_C2);
// Generator of G2
// These are three Fq elements each because X and Y (and Z) are elements of Fq^3
@@ -109,10 +108,10 @@ const G2_GENERATOR_Y: Fq3 = CubicExt!(G2_GENERATOR_Y_C0, G2_GENERATOR_Y_C1, G2_G
// Y = 2540920530670785421282147216459500299597350984927286541981768941513322907384197363939300669100157141915897390694710534916701460991329498878429407641200901974650893207493883271892985923686300670742888673128384350189165542294615,
// 7768974215205248225654340523113146529854477025417883273460270519532499370133542215655437897583245920162220909271982265882784840026754554720358946490360213245668334549692889019612343620295335698052097726325099648573158597797497,
// 21014872727619291834131369222699267167761185012487859171850226473555446863681002782100371394603357586906967186931035615146288030444598977758226767063525819170917389755555854704165900869058188909090444447822088242504281789869689,
pub const G2_GENERATOR_X_C0: Fq = MontFp!(Fq, "27250797394340459586637772414334383652934225310678303542554641987990991970766156209996739240400887081904395745019996048910447071686918567661896491214767494514394154061111870331668445455228882471000120574964265209669155206168252");
pub const G2_GENERATOR_X_C1: Fq = MontFp!(Fq, "35762481056967998715733586393399457882827322353696313323665483142561285210083843314423554450886956650265947502285422529615273790981238406393402603210224104850580302463396274854098657541573494421834514772635884262388058080180368");
pub const G2_GENERATOR_X_C2: Fq = MontFp!(Fq, "36955296703808958167583270646821654948157955258947892285629161090141878438357164213613114995903637211606408001037026832604054121847388692538440756596264746452765613740820430501353237866984394057660379098674983614861254438847846");
pub const G2_GENERATOR_X_C0: Fq = MontFp!("27250797394340459586637772414334383652934225310678303542554641987990991970766156209996739240400887081904395745019996048910447071686918567661896491214767494514394154061111870331668445455228882471000120574964265209669155206168252");
pub const G2_GENERATOR_X_C1: Fq = MontFp!("35762481056967998715733586393399457882827322353696313323665483142561285210083843314423554450886956650265947502285422529615273790981238406393402603210224104850580302463396274854098657541573494421834514772635884262388058080180368");
pub const G2_GENERATOR_X_C2: Fq = MontFp!("36955296703808958167583270646821654948157955258947892285629161090141878438357164213613114995903637211606408001037026832604054121847388692538440756596264746452765613740820430501353237866984394057660379098674983614861254438847846");
pub const G2_GENERATOR_Y_C0: Fq = MontFp!(Fq, "2540920530670785421282147216459500299597350984927286541981768941513322907384197363939300669100157141915897390694710534916701460991329498878429407641200901974650893207493883271892985923686300670742888673128384350189165542294615");
pub const G2_GENERATOR_Y_C1: Fq = MontFp!(Fq, "7768974215205248225654340523113146529854477025417883273460270519532499370133542215655437897583245920162220909271982265882784840026754554720358946490360213245668334549692889019612343620295335698052097726325099648573158597797497");
pub const G2_GENERATOR_Y_C2: Fq = MontFp!(Fq, "21014872727619291834131369222699267167761185012487859171850226473555446863681002782100371394603357586906967186931035615146288030444598977758226767063525819170917389755555854704165900869058188909090444447822088242504281789869689");
pub const G2_GENERATOR_Y_C0: Fq = MontFp!("2540920530670785421282147216459500299597350984927286541981768941513322907384197363939300669100157141915897390694710534916701460991329498878429407641200901974650893207493883271892985923686300670742888673128384350189165542294615");
pub const G2_GENERATOR_Y_C1: Fq = MontFp!("7768974215205248225654340523113146529854477025417883273460270519532499370133542215655437897583245920162220909271982265882784840026754554720358946490360213245668334549692889019612343620295335698052097726325099648573158597797497");
pub const G2_GENERATOR_Y_C2: Fq = MontFp!("21014872727619291834131369222699267167761185012487859171850226473555446863681002782100371394603357586906967186931035615146288030444598977758226767063525819170917389755555854704165900869058188909090444447822088242504281789869689");

View File

@@ -1,8 +1,8 @@
use ark_ec::models::{
mnt6::{MNT6Parameters, MNT6},
SWModelParameters,
short_weierstrass::SWCurveConfig,
};
use ark_ff::{biginteger::BigInteger768, BigInt, CubicExt, Fp3, MontFp};
use ark_ff::{biginteger::BigInteger768, BigInt, Field, Fp3};
use crate::{Fq, Fq3Config, Fq6Config, Fr};
@@ -22,7 +22,7 @@ pub type MNT6_753 = MNT6<Parameters>;
pub struct Parameters;
impl MNT6Parameters for Parameters {
const TWIST: Fp3<Self::Fp3Config> = CubicExt!(FQ_ZERO, FQ_ONE, FQ_ZERO);
const TWIST: Fp3<Self::Fp3Config> = Fp3::new(Fq::ZERO, Fq::ONE, Fq::ZERO);
// A coefficient of MNT6-753 G2 =
// ```
// mnt6753_twist_coeff_a = mnt6753_Fq3(mnt6753_Fq::zero(), mnt6753_Fq::zero(),
@@ -30,7 +30,7 @@ impl MNT6Parameters for Parameters {
// = (ZERO, ZERO, A_COEFF);
// ```
const TWIST_COEFF_A: Fp3<Self::Fp3Config> =
CubicExt!(FQ_ZERO, FQ_ZERO, g1::Parameters::COEFF_A,);
Fp3::new(Fq::ZERO, Fq::ZERO, g1::Parameters::COEFF_A);
// https://github.com/o1-labs/snarky/blob/9c21ab2bb23874604640740d646a932e813432c3/snarkette/mnt6753.ml
const ATE_LOOP_COUNT: &'static [u64] = &[
@@ -66,8 +66,3 @@ impl MNT6Parameters for Parameters {
type G1Parameters = self::g1::Parameters;
type G2Parameters = self::g2::Parameters;
}
pub const FQ_ZERO: Fq = MontFp!(Fq, "0");
pub const FQ_ONE: Fq = MontFp!(Fq, "1");
pub const FR_ZERO: Fr = MontFp!(Fr, "0");
pub const FR_ONE: Fr = MontFp!(Fr, "1");

View File

@@ -1,6 +1,6 @@
use ark_algebra_test_templates::{
curves::*, generate_bilinearity_test, generate_g1_test, generate_g2_test,
generate_product_of_pairings_test, groups::*, msm::*,
generate_product_of_pairings_test, msm::*,
};
use ark_ec::{AffineCurve, PairingEngine, ProjectiveCurve};
use ark_ff::{Field, One, PrimeField, UniformRand};

View File

@@ -1,9 +1,9 @@
use ark_ff::{
fields::fp3::{Fp3, Fp3Config},
CubicExt, MontFp,
Field, MontFp,
};
use crate::{fq::Fq, FQ_ONE, FQ_ZERO};
use crate::fq::Fq;
pub type Fq3 = Fp3<Fq3Config>;
@@ -12,7 +12,7 @@ pub struct Fq3Config;
impl Fp3Config for Fq3Config {
type Fp = Fq;
const NONRESIDUE: Fq = MontFp!(Fq, "11");
const NONRESIDUE: Fq = MontFp!("11");
const TWO_ADICITY: u32 = 30;
@@ -56,10 +56,10 @@ impl Fp3Config for Fq3Config {
];
/// (11^T, 0, 0)
const QUADRATIC_NONRESIDUE_TO_T: Fq3 = CubicExt!(
MontFp!(Fq, "22168644070733283197994897338612733221095941481265408161807376791727499343083607817089033595478370212662133368413166734396127674284827734481031659015434501966360165723728649019457855887066657739809176476252080335185730833468062"),
FQ_ZERO,
FQ_ZERO,
const QUADRATIC_NONRESIDUE_TO_T: Fq3 = Fq3::new(
MontFp!("22168644070733283197994897338612733221095941481265408161807376791727499343083607817089033595478370212662133368413166734396127674284827734481031659015434501966360165723728649019457855887066657739809176476252080335185730833468062"),
Fq::ZERO,
Fq::ZERO,
);
// Coefficients for the Frobenius automorphism.
@@ -67,14 +67,14 @@ impl Fp3Config for Fq3Config {
// c1[1] = 24129022407817241407134263419936114379815707076943508280977368156625538709102831814843582780138963119807143081677569721953561801075623741378629346409604471234573396989178424163772589090105392407118197799904755622897541183052132
// c1[2] = 17769468560101711995209951371304522748355002843010440790806134764399814103468274958215310983651375801610927890210888755369611256415970113691066895445191924931148019336171640277697829047741006062493737919155152541323243293107868,
const FROBENIUS_COEFF_FP3_C1: &'static [Fq] = &[
FQ_ONE,
MontFp!(Fq, "24129022407817241407134263419936114379815707076943508280977368156625538709102831814843582780138963119807143081677569721953561801075623741378629346409604471234573396989178424163772589090105392407118197799904755622897541183052132"),
MontFp!(Fq, "17769468560101711995209951371304522748355002843010440790806134764399814103468274958215310983651375801610927890210888755369611256415970113691066895445191924931148019336171640277697829047741006062493737919155152541323243293107868"),
Fq::ONE,
MontFp!("24129022407817241407134263419936114379815707076943508280977368156625538709102831814843582780138963119807143081677569721953561801075623741378629346409604471234573396989178424163772589090105392407118197799904755622897541183052132"),
MontFp!("17769468560101711995209951371304522748355002843010440790806134764399814103468274958215310983651375801610927890210888755369611256415970113691066895445191924931148019336171640277697829047741006062493737919155152541323243293107868"),
];
// c2 = {c1[0], c1[2], c1[1]}
const FROBENIUS_COEFF_FP3_C2: &'static [Fq] = &[
FQ_ONE,
Fq::ONE,
Self::FROBENIUS_COEFF_FP3_C1[2],
Self::FROBENIUS_COEFF_FP3_C1[1],
];

View File

@@ -1,9 +1,9 @@
use ark_ff::{
fields::fp6_2over3::{Fp6, Fp6Config},
CubicExt, MontFp,
Field, MontFp,
};
use crate::{Fq, Fq3, Fq3Config, FQ_ONE, FQ_ZERO};
use crate::{Fq, Fq3, Fq3Config};
pub type Fq6 = Fp6<Fq6Config>;
@@ -12,7 +12,7 @@ pub struct Fq6Config;
impl Fp6Config for Fq6Config {
type Fp3Config = Fq3Config;
const NONRESIDUE: Fq3 = CubicExt!(FQ_ZERO, FQ_ONE, FQ_ZERO);
const NONRESIDUE: Fq3 = Fq3::new(Fq::ZERO, Fq::ONE, Fq::ZERO);
// Coefficients for the Frobenius automorphism.
// c1[0] = 1,
@@ -22,11 +22,11 @@ impl Fp6Config for Fq6Config {
// c1[4] = 17769468560101711995209951371304522748355002843010440790806134764399814103468274958215310983651375801610927890210888755369611256415970113691066895445191924931148019336171640277697829047741006062493737919155152541323243293107868
// c1[5] = 17769468560101711995209951371304522748355002843010440790806134764399814103468274958215310983651375801610927890210888755369611256415970113691066895445191924931148019336171640277697829047741006062493737919155152541323243293107869
const FROBENIUS_COEFF_FP6_C1: &'static [Fq] = &[
FQ_ONE,
MontFp!(Fq, "24129022407817241407134263419936114379815707076943508280977368156625538709102831814843582780138963119807143081677569721953561801075623741378629346409604471234573396989178424163772589090105392407118197799904755622897541183052133"),
MontFp!(Fq, "24129022407817241407134263419936114379815707076943508280977368156625538709102831814843582780138963119807143081677569721953561801075623741378629346409604471234573396989178424163772589090105392407118197799904755622897541183052132"),
MontFp!(Fq, "41898490967918953402344214791240637128170709919953949071783502921025352812571106773058893763790338921418070971888458477323173057491593855069696241854796396165721416325350064441470418137846398469611935719059908164220784476160000"),
MontFp!(Fq, "17769468560101711995209951371304522748355002843010440790806134764399814103468274958215310983651375801610927890210888755369611256415970113691066895445191924931148019336171640277697829047741006062493737919155152541323243293107868"),
MontFp!(Fq, "17769468560101711995209951371304522748355002843010440790806134764399814103468274958215310983651375801610927890210888755369611256415970113691066895445191924931148019336171640277697829047741006062493737919155152541323243293107869"),
Fq::ONE,
MontFp!("24129022407817241407134263419936114379815707076943508280977368156625538709102831814843582780138963119807143081677569721953561801075623741378629346409604471234573396989178424163772589090105392407118197799904755622897541183052133"),
MontFp!("24129022407817241407134263419936114379815707076943508280977368156625538709102831814843582780138963119807143081677569721953561801075623741378629346409604471234573396989178424163772589090105392407118197799904755622897541183052132"),
MontFp!("41898490967918953402344214791240637128170709919953949071783502921025352812571106773058893763790338921418070971888458477323173057491593855069696241854796396165721416325350064441470418137846398469611935719059908164220784476160000"),
MontFp!("17769468560101711995209951371304522748355002843010440790806134764399814103468274958215310983651375801610927890210888755369611256415970113691066895445191924931148019336171640277697829047741006062493737919155152541323243293107868"),
MontFp!("17769468560101711995209951371304522748355002843010440790806134764399814103468274958215310983651375801610927890210888755369611256415970113691066895445191924931148019336171640277697829047741006062493737919155152541323243293107869"),
];
}

View File

@@ -2,7 +2,7 @@ use ark_algebra_test_templates::{
fields::*, generate_field_serialization_test, generate_field_test,
};
use ark_ff::{
fields::{models::fp6_2over3::*, quadratic_extension::QuadExtConfig, SquareRootField},
fields::{models::fp6_2over3::*, quadratic_extension::QuadExtConfig},
Field, PrimeField,
};
use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize};
@@ -11,7 +11,7 @@ use core::ops::{AddAssign, MulAssign, SubAssign};
use crate::*;
generate_field_test!(mnt6_753; fq3; fq6; mont(12, 12); );
generate_field_test!(mnt6_753; fq3; fq6_2_on_3; mont(12, 12); );
generate_field_serialization_test!(mnt6_753;);
#[test]