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_std::ops::Neg;
  2
  3use ark_ec::{
  4    bls12,
  5    bls12::Bls12Parameters,
  6    models::CurveConfig,
  7    short_weierstrass::{Affine, Projective, SWCurveConfig},
  8    AffineRepr, CurveGroup, Group,
  9};
 10use ark_ff::{Field, MontFp, Zero};
 11
 12use crate::*;
 13
 14pub type G2Affine = bls12::G2Affine<crate::Parameters>;
 15pub type G2Projective = bls12::G2Projective<crate::Parameters>;
 16
 17#[derive(Clone, Default, PartialEq, Eq)]
 18pub struct Parameters;
 19
 20impl CurveConfig for Parameters {
 21    type BaseField = Fq2;
 22    type ScalarField = Fr;
 23
 24    /// COFACTOR = (x^8 - 4 x^7 + 5 x^6) - (4 x^4 + 6 x^3 - 4 x^2 - 4 x + 13) //
 25    /// 9
 26    /// = 305502333931268344200999753193121504214466019254188142667664032982267604182971884026507427359259977847832272839041616661285803823378372096355777062779109
 27    #[rustfmt::skip]
 28    const COFACTOR: &'static [u64] = &[
 29        0xcf1c38e31c7238e5,
 30        0x1616ec6e786f0c70,
 31        0x21537e293a6691ae,
 32        0xa628f1cb4d9e82ef,
 33        0xa68a205b2e5a7ddf,
 34        0xcd91de4547085aba,
 35        0x91d50792876a202,
 36        0x5d543a95414e7f1,
 37    ];
 38
 39    /// COFACTOR_INV = COFACTOR^{-1} mod r
 40    /// 26652489039290660355457965112010883481355318854675681319708643586776743290055
 41    const COFACTOR_INV: Fr =
 42        MontFp!("26652489039290660355457965112010883481355318854675681319708643586776743290055");
 43}
 44
 45impl SWCurveConfig for Parameters {
 46    /// COEFF_A = [0, 0]
 47    const COEFF_A: Fq2 = Fq2::new(g1::Parameters::COEFF_A, g1::Parameters::COEFF_A);
 48
 49    /// COEFF_B = [4, 4]
 50    const COEFF_B: Fq2 = Fq2::new(g1::Parameters::COEFF_B, g1::Parameters::COEFF_B);
 51
 52    /// AFFINE_GENERATOR_COEFFS = (G2_GENERATOR_X, G2_GENERATOR_Y)
 53    const GENERATOR: G2Affine = G2Affine::new_unchecked(G2_GENERATOR_X, G2_GENERATOR_Y);
 54
 55    #[inline(always)]
 56    fn mul_by_a(_: Self::BaseField) -> Self::BaseField {
 57        Self::BaseField::zero()
 58    }
 59
 60    fn is_in_correct_subgroup_assuming_on_curve(point: &G2Affine) -> bool {
 61        // Algorithm from Section 4 of https://eprint.iacr.org/2021/1130.
 62        //
 63        // Checks that [p]P = [X]P
 64
 65        let mut x_times_point = point.mul_bigint(crate::Parameters::X);
 66        if crate::Parameters::X_IS_NEGATIVE {
 67            x_times_point = -x_times_point;
 68        }
 69
 70        let p_times_point = p_power_endomorphism(point);
 71
 72        x_times_point.eq(&p_times_point)
 73    }
 74
 75    #[inline]
 76    fn clear_cofactor(p: &G2Affine) -> G2Affine {
 77        // Based on Section 4.1 of https://eprint.iacr.org/2017/419.pdf
 78        // [h(ψ)]P = [x^2 − x − 1]P + [x − 1]ψ(P) + (ψ^2)(2P)
 79
 80        // x = -15132376222941642752
 81        // When multiplying, use -c1 instead, and then negate the result. That's much
 82        // more efficient, since the scalar -c1 has less limbs and a much lower Hamming
 83        // weight.
 84        let x: &'static [u64] = crate::Parameters::X;
 85        let p_projective = p.into_group();
 86
 87        // [x]P
 88        let x_p = Parameters::mul_affine(p, &x).neg();
 89        // ψ(P)
 90        let psi_p = p_power_endomorphism(&p);
 91        // (ψ^2)(2P)
 92        let mut psi2_p2 = double_p_power_endomorphism(&p_projective.double());
 93
 94        // tmp = [x]P + ψ(P)
 95        let mut tmp = x_p.clone();
 96        tmp += &psi_p;
 97
 98        // tmp2 = [x^2]P + [x]ψ(P)
 99        let mut tmp2: Projective<Parameters> = tmp;
100        tmp2 = tmp2.mul_bigint(x).neg();
101
102        // add up all the terms
103        psi2_p2 += tmp2;
104        psi2_p2 -= x_p;
105        psi2_p2 += &-psi_p;
106        (psi2_p2 - p_projective).into_affine()
107    }
108}
109
110pub const G2_GENERATOR_X: Fq2 = Fq2::new(G2_GENERATOR_X_C0, G2_GENERATOR_X_C1);
111pub const G2_GENERATOR_Y: Fq2 = Fq2::new(G2_GENERATOR_Y_C0, G2_GENERATOR_Y_C1);
112
113/// G2_GENERATOR_X_C0 =
114/// 352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160
115pub const G2_GENERATOR_X_C0: Fq = MontFp!("352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160");
116
117/// G2_GENERATOR_X_C1 =
118/// 3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758
119pub const G2_GENERATOR_X_C1: Fq = MontFp!("3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758");
120
121/// G2_GENERATOR_Y_C0 =
122/// 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905
123pub const G2_GENERATOR_Y_C0: Fq = MontFp!("1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905");
124
125/// G2_GENERATOR_Y_C1 =
126/// 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582
127pub const G2_GENERATOR_Y_C1: Fq = MontFp!("927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582");
128
129// psi(x,y) = (x**p * PSI_X, y**p * PSI_Y) is the Frobenius composed
130// with the quadratic twist and its inverse
131
132// PSI_X = 1/(u+1)^((p-1)/3)
133pub const P_POWER_ENDOMORPHISM_COEFF_0 : Fq2 = Fq2::new(
134    Fq::ZERO,
135    MontFp!(
136                "4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939437"
137    )
138);
139
140// PSI_Y = 1/(u+1)^((p-1)/2)
141pub const P_POWER_ENDOMORPHISM_COEFF_1: Fq2 = Fq2::new(
142    MontFp!(
143                "2973677408986561043442465346520108879172042883009249989176415018091420807192182638567116318576472649347015917690530"),
144    MontFp!(
145                "1028732146235106349975324479215795277384839936929757896155643118032610843298655225875571310552543014690878354869257")
146);
147
148pub const DOUBLE_P_POWER_ENDOMORPHISM: Fq2 = Fq2::new(
149    MontFp!("4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436"),
150    Fq::ZERO
151);
152
153pub fn p_power_endomorphism(p: &Affine<Parameters>) -> Affine<Parameters> {
154    // The p-power endomorphism for G2 is defined as follows:
155    // 1. Note that G2 is defined on curve E': y^2 = x^3 + 4(u+1).
156    //    To map a point (x, y) in E' to (s, t) in E,
157    //    one set s = x / ((u+1) ^ (1/3)), t = y / ((u+1) ^ (1/2)),
158    //    because E: y^2 = x^3 + 4.
159    // 2. Apply the Frobenius endomorphism (s, t) => (s', t'),
160    //    another point on curve E, where s' = s^p, t' = t^p.
161    // 3. Map the point from E back to E'; that is,
162    //    one set x' = s' * ((u+1) ^ (1/3)), y' = t' * ((u+1) ^ (1/2)).
163    //
164    // To sum up, it maps
165    // (x,y) -> (x^p / ((u+1)^((p-1)/3)), y^p / ((u+1)^((p-1)/2)))
166    // as implemented in the code as follows.
167
168    let mut res = *p;
169    res.x.frobenius_map(1);
170    res.y.frobenius_map(1);
171
172    let tmp_x = res.x.clone();
173    res.x.c0 = -P_POWER_ENDOMORPHISM_COEFF_0.c1 * &tmp_x.c1;
174    res.x.c1 = P_POWER_ENDOMORPHISM_COEFF_0.c1 * &tmp_x.c0;
175    res.y *= P_POWER_ENDOMORPHISM_COEFF_1;
176
177    res
178}
179
180/// For a p-power endomorphism psi(P), compute psi(psi(P))
181pub fn double_p_power_endomorphism(p: &Projective<Parameters>) -> Projective<Parameters> {
182    let mut res = *p;
183
184    res.x *= DOUBLE_P_POWER_ENDOMORPHISM;
185    res.y = res.y.neg();
186
187    res
188}
189
190#[cfg(test)]
191mod test {
192
193    use super::*;
194    use ark_std::UniformRand;
195
196    #[test]
197    fn test_cofactor_clearing() {
198        // multiplying by h_eff and clearing the cofactor by the efficient
199        // endomorphism-based method should yield the same result.
200        let h_eff: &'static [u64] = &[
201            0xe8020005aaa95551,
202            0x59894c0adebbf6b4,
203            0xe954cbc06689f6a3,
204            0x2ec0ec69d7477c1a,
205            0x6d82bf015d1212b0,
206            0x329c2f178731db95,
207            0x9986ff031508ffe1,
208            0x88e2a8e9145ad768,
209            0x584c6a0ea91b3528,
210            0xbc69f08f2ee75b3,
211        ];
212
213        let mut rng = ark_std::test_rng();
214        const SAMPLES: usize = 10;
215        for _ in 0..SAMPLES {
216            let p = Affine::<g2::Parameters>::rand(&mut rng);
217            let optimised = p.clear_cofactor().into_group();
218            let naive = g2::Parameters::mul_affine(&p, h_eff);
219            assert_eq!(optimised, naive);
220        }
221    }
222}