arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
ark-curves-cherry-picked/bls12_381/src/curves/g2.rs
bls12_381/src/curves/g2.rsRaw
  1use ark_ec::{
  2    bls12,
  3    bls12::Bls12Parameters,
  4    models::{ModelParameters, SWModelParameters},
  5    short_weierstrass_jacobian::GroupAffine,
  6    AffineCurve,
  7};
  8use ark_ff::{BigInt, Field, MontFp, QuadExt, Zero};
  9
 10use crate::*;
 11
 12pub type G2Affine = bls12::G2Affine<crate::Parameters>;
 13pub type G2Projective = bls12::G2Projective<crate::Parameters>;
 14
 15#[derive(Clone, Default, PartialEq, Eq)]
 16pub struct Parameters;
 17
 18impl ModelParameters for Parameters {
 19    type BaseField = Fq2;
 20    type ScalarField = Fr;
 21
 22    /// COFACTOR = (x^8 - 4 x^7 + 5 x^6) - (4 x^4 + 6 x^3 - 4 x^2 - 4 x + 13) //
 23    /// 9
 24    /// = 305502333931268344200999753193121504214466019254188142667664032982267604182971884026507427359259977847832272839041616661285803823378372096355777062779109
 25    #[rustfmt::skip]
 26    const COFACTOR: &'static [u64] = &[
 27        0xcf1c38e31c7238e5,
 28        0x1616ec6e786f0c70,
 29        0x21537e293a6691ae,
 30        0xa628f1cb4d9e82ef,
 31        0xa68a205b2e5a7ddf,
 32        0xcd91de4547085aba,
 33        0x91d50792876a202,
 34        0x5d543a95414e7f1,
 35    ];
 36
 37    /// COFACTOR_INV = COFACTOR^{-1} mod r
 38    /// 26652489039290660355457965112010883481355318854675681319708643586776743290055
 39    const COFACTOR_INV: Fr = MontFp!(
 40        Fr,
 41        "26652489039290660355457965112010883481355318854675681319708643586776743290055"
 42    );
 43}
 44
 45impl SWModelParameters for Parameters {
 46    /// COEFF_A = [0, 0]
 47    const COEFF_A: Fq2 = QuadExt!(g1::Parameters::COEFF_A, g1::Parameters::COEFF_A,);
 48
 49    /// COEFF_B = [4, 4]
 50    const COEFF_B: Fq2 = QuadExt!(g1::Parameters::COEFF_B, g1::Parameters::COEFF_B,);
 51
 52    /// AFFINE_GENERATOR_COEFFS = (G2_GENERATOR_X, G2_GENERATOR_Y)
 53    const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
 54        (G2_GENERATOR_X, G2_GENERATOR_Y);
 55
 56    #[inline(always)]
 57    fn mul_by_a(_: &Self::BaseField) -> Self::BaseField {
 58        Self::BaseField::zero()
 59    }
 60
 61    fn is_in_correct_subgroup_assuming_on_curve(point: &G2Affine) -> bool {
 62        // Algorithm from Section 4 of https://eprint.iacr.org/2021/1130.
 63        //
 64        // Checks that [p]P = [X]P
 65
 66        let mut x_times_point = point.mul(BigInt::new([crate::Parameters::X[0], 0, 0, 0]));
 67        if crate::Parameters::X_IS_NEGATIVE {
 68            x_times_point = -x_times_point;
 69        }
 70
 71        let p_times_point = p_power_endomorphism(point);
 72
 73        x_times_point.eq(&p_times_point)
 74    }
 75}
 76
 77pub const G2_GENERATOR_X: Fq2 = QuadExt!(G2_GENERATOR_X_C0, G2_GENERATOR_X_C1);
 78pub const G2_GENERATOR_Y: Fq2 = QuadExt!(G2_GENERATOR_Y_C0, G2_GENERATOR_Y_C1);
 79
 80/// G2_GENERATOR_X_C0 =
 81/// 352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160
 82pub const G2_GENERATOR_X_C0: Fq = MontFp!(Fq, "352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160");
 83
 84/// G2_GENERATOR_X_C1 =
 85/// 3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758
 86pub const G2_GENERATOR_X_C1: Fq = MontFp!(Fq, "3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758");
 87
 88/// G2_GENERATOR_Y_C0 =
 89/// 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905
 90pub const G2_GENERATOR_Y_C0: Fq = MontFp!(Fq, "1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905");
 91
 92/// G2_GENERATOR_Y_C1 =
 93/// 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582
 94pub const G2_GENERATOR_Y_C1: Fq = MontFp!(Fq, "927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582");
 95
 96// psi(x,y) = (x**p * PSI_X, y**p * PSI_Y) is the Frobenius composed
 97// with the quadratic twist and its inverse
 98
 99// PSI_X = 1/(u+1)^((p-1)/3)
100pub const P_POWER_ENDOMORPHISM_COEFF_0 : Fq2 = QuadExt!(
101    FQ_ZERO,
102    MontFp!(
103        Fq,
104        "4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437"
105    )
106);
107
108// PSI_Y = 1/(u+1)^((p-1)/2)
109pub const P_POWER_ENDOMORPHISM_COEFF_1: Fq2 = QuadExt!(
110    MontFp!(
111        Fq,
112        "2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530"),
113    MontFp!(
114        Fq,
115        "1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257")
116);
117
118pub fn p_power_endomorphism(p: &GroupAffine<Parameters>) -> GroupAffine<Parameters> {
119    // The p-power endomorphism for G2 is defined as follows:
120    // 1. Note that G2 is defined on curve E': y^2 = x^3 + 4(u+1).
121    //    To map a point (x, y) in E' to (s, t) in E,
122    //    one set s = x / ((u+1) ^ (1/3)), t = y / ((u+1) ^ (1/2)),
123    //    because E: y^2 = x^3 + 4.
124    // 2. Apply the Frobenius endomorphism (s, t) => (s', t'),
125    //    another point on curve E, where s' = s^p, t' = t^p.
126    // 3. Map the point from E back to E'; that is,
127    //    one set x' = s' * ((u+1) ^ (1/3)), y' = t' * ((u+1) ^ (1/2)).
128    //
129    // To sum up, it maps
130    // (x,y) -> (x^p / ((u+1)^((p-1)/3)), y^p / ((u+1)^((p-1)/2)))
131    // as implemented in the code as follows.
132
133    let mut res = *p;
134    res.x.frobenius_map(1);
135    res.y.frobenius_map(1);
136
137    let tmp_x = res.x.clone();
138    res.x.c0 = -P_POWER_ENDOMORPHISM_COEFF_0.c1 * &tmp_x.c1;
139    res.x.c1 = P_POWER_ENDOMORPHISM_COEFF_0.c1 * &tmp_x.c0;
140    res.y *= P_POWER_ENDOMORPHISM_COEFF_1;
141
142    res
143}