arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
ark-curves-cherry-picked/bls12_381/src/curves/g1.rs
bls12_381/src/curves/g1.rsRaw
 1use ark_ec::{
 2    bls12,
 3    bls12::Bls12Parameters,
 4    models::{ModelParameters, SWModelParameters},
 5    short_weierstrass_jacobian::GroupAffine,
 6    AffineCurve, ProjectiveCurve,
 7};
 8use ark_ff::{biginteger::BigInteger256, MontFp, Zero};
 9use ark_std::ops::Neg;
10
11use crate::*;
12
13pub type G1Affine = bls12::G1Affine<crate::Parameters>;
14pub type G1Projective = bls12::G1Projective<crate::Parameters>;
15
16#[derive(Clone, Default, PartialEq, Eq)]
17pub struct Parameters;
18
19impl ModelParameters for Parameters {
20    type BaseField = Fq;
21    type ScalarField = Fr;
22
23    /// COFACTOR = (x - 1)^2 / 3  = 76329603384216526031706109802092473003
24    const COFACTOR: &'static [u64] = &[0x8c00aaab0000aaab, 0x396c8c005555e156];
25
26    /// COFACTOR_INV = COFACTOR^{-1} mod r
27    /// = 52435875175126190458656871551744051925719901746859129887267498875565241663483
28    const COFACTOR_INV: Fr = MontFp!(
29        Fr,
30        "52435875175126190458656871551744051925719901746859129887267498875565241663483"
31    );
32}
33
34impl SWModelParameters for Parameters {
35    /// COEFF_A = 0
36    const COEFF_A: Fq = MontFp!(Fq, "0");
37
38    /// COEFF_B = 4
39    const COEFF_B: Fq = MontFp!(Fq, "4");
40
41    /// AFFINE_GENERATOR_COEFFS = (G1_GENERATOR_X, G1_GENERATOR_Y)
42    const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
43        (G1_GENERATOR_X, G1_GENERATOR_Y);
44
45    #[inline(always)]
46    fn mul_by_a(_: &Self::BaseField) -> Self::BaseField {
47        Self::BaseField::zero()
48    }
49
50    #[inline]
51    fn is_in_correct_subgroup_assuming_on_curve(p: &G1Affine) -> bool {
52        // Algorithm from Section 6 of https://eprint.iacr.org/2021/1130.
53        //
54        // Check that endomorphism_p(P) == -[X^2]P
55
56        let x = BigInteger256::new([crate::Parameters::X[0], 0, 0, 0]);
57
58        // An early-out optimization described in Section 6.
59        // If uP == P but P != point of infinity, then the point is not in the right
60        // subgroup.
61        let x_times_p = p.mul(x);
62        if x_times_p.eq(p) && !p.infinity {
63            return false;
64        }
65
66        let minus_x_squared_times_p = x_times_p.mul(x).neg();
67        let endomorphism_p = endomorphism(p);
68        minus_x_squared_times_p.eq(&endomorphism_p)
69    }
70}
71
72/// G1_GENERATOR_X =
73/// 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507
74pub const G1_GENERATOR_X: Fq = MontFp!(Fq, "3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507");
75
76/// G1_GENERATOR_Y =
77/// 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569
78pub const G1_GENERATOR_Y: Fq = MontFp!(Fq, "1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569");
79
80/// BETA is a non-trivial cubic root of unity in Fq.
81pub const BETA: Fq = MontFp!(Fq, "793479390729215512621379701633421447060886740281060493010456487427281649075476305620758731620350");
82
83pub fn endomorphism(p: &GroupAffine<Parameters>) -> GroupAffine<Parameters> {
84    // Endomorphism of the points on the curve.
85    // endomorphism_p(x,y) = (BETA * x, y)
86    // where BETA is a non-trivial cubic root of unity in Fq.
87    let mut res = (*p).clone();
88    res.x *= BETA;
89    res
90}