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::{
mnt4,
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 = mnt4::G1Affine<crate::Parameters>;
pub type G1Projective = mnt4::G1Projective<crate::Parameters>;
@@ -13,7 +13,7 @@ pub type G1Prepared = mnt4::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;
@@ -22,21 +22,20 @@ impl ModelParameters for Parameters {
/// 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 = 2
/// Reference: <https://github.com/scipr-lab/libff/blob/c927821ebe02e0a24b5e0f9170cec5e211a35f08/libff/algebra/curves/mnt/mnt4/mnt4_init.cpp#L116>
const COEFF_A: Fq = MontFp!(Fq, "2");
const COEFF_A: Fq = MontFp!("2");
/// COEFF_B = 423894536526684178289416011533888240029318103673896002803341544124054745019340795360841685
/// Reference: <https://github.com/scipr-lab/libff/blob/c927821ebe02e0a24b5e0f9170cec5e211a35f08/libff/algebra/curves/mnt/mnt4/mnt4_init.cpp#L117>
const COEFF_B: Fq = MontFp!(Fq, "423894536526684178289416011533888240029318103673896002803341544124054745019340795360841685");
const COEFF_B: Fq = MontFp!("423894536526684178289416011533888240029318103673896002803341544124054745019340795360841685");
/// 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
@@ -45,13 +44,11 @@ impl SWModelParameters for Parameters {
/// G1_GENERATOR_X
/// Reference: <https://github.com/scipr-lab/libff/blob/c927821ebe02e0a24b5e0f9170cec5e211a35f08/libff/algebra/curves/mnt/mnt4/mnt4_init.cpp#L137>
pub const G1_GENERATOR_X: Fq = MontFp!(
Fq,
"60760244141852568949126569781626075788424196370144486719385562369396875346601926534016838"
);
/// G1_GENERATOR_Y
/// Reference: <https://github.com/scipr-lab/libff/blob/c927821ebe02e0a24b5e0f9170cec5e211a35f08/libff/algebra/curves/mnt/mnt4/mnt4_init.cpp#L138>
pub const G1_GENERATOR_Y: Fq = MontFp!(
Fq,
"363732850702582978263902770815145784459747722357071843971107674179038674942891694705904306"
);

View File

@@ -1,11 +1,11 @@
use ark_ec::{
mnt4,
mnt4::MNT4Parameters,
models::{ModelParameters, SWModelParameters},
models::{short_weierstrass::SWCurveConfig, CurveConfig},
};
use ark_ff::{MontFp, QuadExt};
use ark_ff::{Field, MontFp};
use crate::{Fq, Fq2, Fr, FQ_ZERO, G1_COEFF_A_NON_RESIDUE};
use crate::{Fq, Fq2, Fr, G1_COEFF_A_NON_RESIDUE};
pub type G2Affine = mnt4::G2Affine<crate::Parameters>;
pub type G2Projective = mnt4::G2Projective<crate::Parameters>;
@@ -14,7 +14,7 @@ pub type G2Prepared = mnt4::G2Prepared<crate::Parameters>;
#[derive(Clone, Default, PartialEq, Eq)]
pub struct Parameters;
impl ModelParameters for Parameters {
impl CurveConfig for Parameters {
type BaseField = Fq2;
type ScalarField = Fr;
@@ -31,7 +31,7 @@ impl ModelParameters for Parameters {
/// COFACTOR^(-1) mod r =
/// 475922286169261325753349249653048451545124878207887910632124039320641839552134835598065665
const COFACTOR_INV: Fr = MontFp!(Fr, "475922286169261325753349249653048451545124878207887910632124039320641839552134835598065665");
const COFACTOR_INV: Fr = MontFp!("475922286169261325753349249653048451545124878207887910632124039320641839552134835598065665");
}
/// MUL_BY_A_C0 = NONRESIDUE * COEFF_A
@@ -40,7 +40,7 @@ pub const MUL_BY_A_C0: Fq = G1_COEFF_A_NON_RESIDUE;
/// MUL_BY_A_C1 = NONRESIDUE * COEFF_A
pub const MUL_BY_A_C1: Fq = G1_COEFF_A_NON_RESIDUE;
impl SWModelParameters for Parameters {
impl SWCurveConfig for Parameters {
const COEFF_A: Fq2 = crate::Parameters::TWIST_COEFF_A;
// B coefficient of MNT4-298 G2 =
// ```
@@ -51,23 +51,22 @@ impl SWModelParameters for Parameters {
// =
// (0, 67372828414711144619833451280373307321534573815811166723479321465776723059456513877937430)
// ```
const COEFF_B: Fq2 = QuadExt!(
FQ_ZERO,
MontFp!(Fq, "67372828414711144619833451280373307321534573815811166723479321465776723059456513877937430"),
const COEFF_B: Fq2 = Fq2::new(
Fq::ZERO,
MontFp!("67372828414711144619833451280373307321534573815811166723479321465776723059456513877937430"),
);
/// 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: &Fq2) -> Fq2 {
QuadExt!(MUL_BY_A_C0 * &elt.c0, MUL_BY_A_C1 * &elt.c1,)
Fq2::new(MUL_BY_A_C0 * &elt.c0, MUL_BY_A_C1 * &elt.c1)
}
}
const G2_GENERATOR_X: Fq2 = QuadExt!(G2_GENERATOR_X_C0, G2_GENERATOR_X_C1);
const G2_GENERATOR_Y: Fq2 = QuadExt!(G2_GENERATOR_Y_C0, G2_GENERATOR_Y_C1);
const G2_GENERATOR_X: Fq2 = Fq2::new(G2_GENERATOR_X_C0, G2_GENERATOR_X_C1);
const G2_GENERATOR_Y: Fq2 = Fq2::new(G2_GENERATOR_Y_C0, G2_GENERATOR_Y_C1);
// Generator of G2
// These are two Fq elements each because X and Y (and Z) are elements of Fq^2
@@ -76,21 +75,17 @@ const G2_GENERATOR_Y: Fq2 = QuadExt!(G2_GENERATOR_Y_C0, G2_GENERATOR_Y_C1);
// Y = 37437409008528968268352521034936931842973546441370663118543015118291998305624025037512482,
// 424621479598893882672393190337420680597584695892317197646113820787463109735345923009077489,
pub const G2_GENERATOR_X_C0: Fq = MontFp!(
Fq,
"438374926219350099854919100077809681842783509163790991847867546339851681564223481322252708"
);
pub const G2_GENERATOR_X_C1: Fq = MontFp!(
Fq,
"37620953615500480110935514360923278605464476459712393277679280819942849043649216370485641"
);
pub const G2_GENERATOR_Y_C0: Fq = MontFp!(
Fq,
"37437409008528968268352521034936931842973546441370663118543015118291998305624025037512482"
);
pub const G2_GENERATOR_Y_C1: Fq = MontFp!(
Fq,
"424621479598893882672393190337420680597584695892317197646113820787463109735345923009077489"
);

View File

@@ -1,7 +1,7 @@
use ark_ec::models::mnt4::{MNT4Parameters, MNT4};
use ark_ff::{biginteger::BigInteger320, BigInt, Fp2, MontFp, QuadExt};
use ark_ff::{biginteger::BigInteger320, BigInt, Field, MontFp};
use crate::{Fq, Fq2Config, Fq4Config, Fr};
use crate::{Fq, Fq2, Fq2Config, Fq4Config, Fr};
pub mod g1;
pub mod g2;
@@ -19,14 +19,14 @@ pub type MNT4_298 = MNT4<Parameters>;
pub struct Parameters;
impl MNT4Parameters for Parameters {
const TWIST: Fp2<Self::Fp2Config> = QuadExt!(FQ_ZERO, FQ_ONE);
const TWIST: Fq2 = Fq2::new(Fq::ZERO, Fq::ONE);
// A coefficient of MNT4-298 G2 =
// ```
// mnt4298_twist_coeff_a = mnt4298_Fq2(mnt4298_G1::coeff_a * non_residue, mnt6298_Fq::zero());
// = (A_COEFF * NONRESIDUE, ZERO)
// = (34, ZERO)
// ```
const TWIST_COEFF_A: Fp2<Self::Fp2Config> = QuadExt!(G1_COEFF_A_NON_RESIDUE, FQ_ZERO);
const TWIST_COEFF_A: Fq2 = Fq2::new(G1_COEFF_A_NON_RESIDUE, Fq::ZERO);
const ATE_LOOP_COUNT: &'static [u64] = &[993502997770534912, 5071219579242586943, 2027349];
const ATE_IS_LOOP_COUNT_NEG: bool = false;
@@ -43,8 +43,4 @@ impl MNT4Parameters for Parameters {
}
// 34
pub const G1_COEFF_A_NON_RESIDUE: Fq = MontFp!(Fq, "34");
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");
pub const G1_COEFF_A_NON_RESIDUE: Fq = MontFp!("34");

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::fp2::{Fp2, Fp2Config},
MontFp,
Field, MontFp,
};
use crate::{Fq, FQ_ONE};
use crate::Fq;
pub type Fq2 = Fp2<Fq2Config>;
@@ -14,12 +14,12 @@ impl Fp2Config for Fq2Config {
/// The quadratic non-residue (17) used to construct the extension is
/// the same as that used in [`libff`](https://github.com/scipr-lab/libff/blob/c927821ebe02e0a24b5e0f9170cec5e211a35f08/libff/algebra/curves/mnt/mnt4/mnt4_init.cpp#L102).
const NONRESIDUE: Fq = MontFp!(Fq, "17");
const NONRESIDUE: Fq = MontFp!("17");
/// Precomputed coefficients:
/// `[1, 475922286169261325753349249653048451545124879242694725395555128576210262817955800483758080]`
const FROBENIUS_COEFF_FP2_C1: &'static [Self::Fp] = &[
FQ_ONE,
MontFp!(Fq, "475922286169261325753349249653048451545124879242694725395555128576210262817955800483758080"),
Fq::ONE,
MontFp!("475922286169261325753349249653048451545124879242694725395555128576210262817955800483758080"),
];
}

View File

@@ -1,9 +1,9 @@
use ark_ff::{
fields::fp4::{Fp4, Fp4Config},
MontFp, QuadExt,
Field, MontFp,
};
use crate::{Fq, Fq2, Fq2Config, FQ_ONE, FQ_ZERO};
use crate::{Fq, Fq2, Fq2Config};
pub type Fq4 = Fp4<Fq4Config>;
@@ -12,7 +12,7 @@ pub struct Fq4Config;
impl Fp4Config for Fq4Config {
type Fp2Config = Fq2Config;
const NONRESIDUE: Fq2 = QuadExt!(FQ_ZERO, FQ_ONE);
const NONRESIDUE: Fq2 = Fq2::new(Fq::ZERO, Fq::ONE);
// Coefficients for the Frobenius automorphism.
// c1[0] = 1,
@@ -23,9 +23,9 @@ impl Fp4Config for Fq4Config {
// These are calculated as
// `FROBENIUS_COEFF_FP4_C1[i] = Fp2Config::NONRESIDUE^((q^i - 1) / 4)`.
const FROBENIUS_COEFF_FP4_C1: &'static [Fq] = &[
FQ_ONE,
MontFp!(Fq, "7684163245453501615621351552473337069301082060976805004625011694147890954040864167002308"),
MontFp!(Fq, "475922286169261325753349249653048451545124879242694725395555128576210262817955800483758080"),
MontFp!(Fq, "468238122923807824137727898100575114475823797181717920390930116882062371863914936316755773"),
Fq::ONE,
MontFp!("7684163245453501615621351552473337069301082060976805004625011694147890954040864167002308"),
MontFp!("475922286169261325753349249653048451545124879242694725395555128576210262817955800483758080"),
MontFp!("468238122923807824137727898100575114475823797181717920390930116882062371863914936316755773"),
];
}

View File

@@ -1,7 +1,7 @@
use ark_algebra_test_templates::{
fields::*, generate_field_serialization_test, generate_field_test,
};
use ark_ff::{Field, One, PrimeField, SquareRootField, UniformRand, Zero};
use ark_ff::{Field, One, PrimeField, UniformRand, Zero};
use ark_serialize::{buffer_bit_byte_size, CanonicalSerialize};
use ark_std::{rand::Rng, test_rng};
use core::ops::{AddAssign, MulAssign, SubAssign};