arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
ark-curves-cherry-picked/bn254/src/curves/g2.rs
bn254/src/curves/g2.rsRaw
  1use ark_ec::{
  2    models::{short_weierstrass::SWCurveConfig, CurveConfig},
  3    scalar_mul::glv::GLVConfig,
  4    short_weierstrass::{Affine, Projective},
  5};
  6use ark_ff::{AdditiveGroup, BigInt, MontFp, PrimeField, Zero};
  7
  8use crate::{Fq, Fq2, Fr};
  9
 10pub type G2Affine = Affine<Config>;
 11
 12#[derive(Clone, Default, PartialEq, Eq)]
 13pub struct Config;
 14
 15impl CurveConfig for Config {
 16    type BaseField = Fq2;
 17    type ScalarField = Fr;
 18
 19    /// COFACTOR = (36 * X^4) + (36 * X^3) + (30 * X^2) + 6*X + 1
 20    /// 21888242871839275222246405745257275088844257914179612981679871602714643921549
 21    #[rustfmt::skip]
 22    const COFACTOR: &'static [u64] = &[
 23        0x345f2299c0f9fa8d,
 24        0x06ceecda572a2489,
 25        0xb85045b68181585e,
 26        0x30644e72e131a029,
 27    ];
 28
 29    /// COFACTOR_INV = COFACTOR^{-1} mod r
 30    const COFACTOR_INV: Fr =
 31        MontFp!("10944121435919637613327163357776759465618812564592884533313067514031822496649");
 32}
 33
 34impl SWCurveConfig for Config {
 35    /// COEFF_A = [0, 0]
 36    const COEFF_A: Fq2 = Fq2::ZERO;
 37
 38    /// COEFF_B = 3/(u+9)
 39    /// (19485874751759354771024239261021720505790618469301721065564631296452457478373, 266929791119991161246907387137283842545076965332900288569378510910307636690)
 40    const COEFF_B: Fq2 = Fq2::new(
 41        MontFp!("19485874751759354771024239261021720505790618469301721065564631296452457478373"),
 42        MontFp!("266929791119991161246907387137283842545076965332900288569378510910307636690"),
 43    );
 44
 45    /// AFFINE_GENERATOR_COEFFS = (G2_GENERATOR_X, G2_GENERATOR_Y)
 46    const GENERATOR: G2Affine = G2Affine::new_unchecked(G2_GENERATOR_X, G2_GENERATOR_Y);
 47
 48    #[inline(always)]
 49    fn mul_by_a(_: Self::BaseField) -> Self::BaseField {
 50        Self::BaseField::zero()
 51    }
 52}
 53
 54impl GLVConfig for Config {
 55    const ENDO_COEFFS: &'static [Self::BaseField] = &[Fq2::new(
 56        MontFp!("21888242871839275220042445260109153167277707414472061641714758635765020556616"),
 57        Fq::ZERO,
 58    )];
 59
 60    const LAMBDA: Self::ScalarField =
 61        MontFp!("4407920970296243842393367215006156084916469457145843978461");
 62
 63    const SCALAR_DECOMP_COEFFS: [(bool, <Self::ScalarField as PrimeField>::BigInt); 4] = [
 64        (false, BigInt!("147946756881789319010696353538189108491")),
 65        (false, BigInt!("9931322734385697763")),
 66        (true, BigInt!("9931322734385697763")),
 67        (false, BigInt!("147946756881789319000765030803803410728")),
 68    ];
 69
 70    fn endomorphism(p: &Projective<Self>) -> Projective<Self> {
 71        let mut res = (*p).clone();
 72        res.x *= Self::ENDO_COEFFS[0];
 73        res
 74    }
 75
 76    fn endomorphism_affine(p: &Affine<Self>) -> Affine<Self> {
 77        let mut res = (*p).clone();
 78        res.x *= Self::ENDO_COEFFS[0];
 79        res
 80    }
 81}
 82
 83pub const G2_GENERATOR_X: Fq2 = Fq2::new(G2_GENERATOR_X_C0, G2_GENERATOR_X_C1);
 84pub const G2_GENERATOR_Y: Fq2 = Fq2::new(G2_GENERATOR_Y_C0, G2_GENERATOR_Y_C1);
 85
 86/// G2_GENERATOR_X_C0 =
 87/// 10857046999023057135944570762232829481370756359578518086990519993285655852781
 88pub const G2_GENERATOR_X_C0: Fq =
 89    MontFp!("10857046999023057135944570762232829481370756359578518086990519993285655852781");
 90
 91/// G2_GENERATOR_X_C1 =
 92/// 11559732032986387107991004021392285783925812861821192530917403151452391805634
 93pub const G2_GENERATOR_X_C1: Fq =
 94    MontFp!("11559732032986387107991004021392285783925812861821192530917403151452391805634");
 95
 96/// G2_GENERATOR_Y_C0 =
 97/// 8495653923123431417604973247489272438418190587263600148770280649306958101930
 98pub const G2_GENERATOR_Y_C0: Fq =
 99    MontFp!("8495653923123431417604973247489272438418190587263600148770280649306958101930");
100
101/// G2_GENERATOR_Y_C1 =
102/// 4082367875863433681332203403145435568316851327593401208105741076214120093531
103pub const G2_GENERATOR_Y_C1: Fq =
104    MontFp!("4082367875863433681332203403145435568316851327593401208105741076214120093531");