arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
ark-curves-cherry-picked/bn254/src/curves/g1.rs
bn254/src/curves/g1.rsRaw
 1use ark_ec::{
 2    bn,
 3    models::{short_weierstrass::SWCurveConfig, CurveConfig},
 4    scalar_mul::glv::GLVConfig,
 5    short_weierstrass::{Affine, Projective},
 6};
 7use ark_ff::{AdditiveGroup, BigInt, Field, MontFp, PrimeField, Zero};
 8
 9use crate::{Fq, Fr};
10
11#[derive(Clone, Default, PartialEq, Eq)]
12pub struct Config;
13
14pub type G1Affine = Affine<Config>;
15
16impl CurveConfig for Config {
17    type BaseField = Fq;
18    type ScalarField = Fr;
19
20    /// COFACTOR = 1
21    const COFACTOR: &'static [u64] = &[0x1];
22
23    /// COFACTOR_INV = COFACTOR^{-1} mod r = 1
24    const COFACTOR_INV: Fr = Fr::ONE;
25}
26
27impl SWCurveConfig for Config {
28    /// COEFF_A = 0
29    const COEFF_A: Fq = Fq::ZERO;
30
31    /// COEFF_B = 3
32    const COEFF_B: Fq = MontFp!("3");
33
34    /// AFFINE_GENERATOR_COEFFS = (G1_GENERATOR_X, G1_GENERATOR_Y)
35    const GENERATOR: G1Affine = G1Affine::new_unchecked(G1_GENERATOR_X, G1_GENERATOR_Y);
36
37    #[inline(always)]
38    fn mul_by_a(_: Self::BaseField) -> Self::BaseField {
39        Self::BaseField::zero()
40    }
41
42    #[inline]
43    fn mul_projective(
44        p: &bn::G1Projective<crate::Config>,
45        scalar: &[u64],
46    ) -> bn::G1Projective<crate::Config> {
47        let s = Self::ScalarField::from_sign_and_limbs(true, scalar);
48        GLVConfig::glv_mul_projective(*p, s)
49    }
50}
51
52impl GLVConfig for Config {
53    const ENDO_COEFFS: &'static [Self::BaseField] = &[MontFp!(
54        "21888242871839275220042445260109153167277707414472061641714758635765020556616"
55    )];
56
57    const LAMBDA: Self::ScalarField =
58        MontFp!("21888242871839275217838484774961031246154997185409878258781734729429964517155");
59
60    const SCALAR_DECOMP_COEFFS: [(bool, <Self::ScalarField as PrimeField>::BigInt); 4] = [
61        (false, BigInt!("147946756881789319000765030803803410728")),
62        (true, BigInt!("9931322734385697763")),
63        (false, BigInt!("9931322734385697763")),
64        (false, BigInt!("147946756881789319010696353538189108491")),
65    ];
66
67    fn endomorphism(p: &Projective<Self>) -> Projective<Self> {
68        let mut res = (*p).clone();
69        res.x *= Self::ENDO_COEFFS[0];
70        res
71    }
72    fn endomorphism_affine(p: &Affine<Self>) -> Affine<Self> {
73        let mut res = (*p).clone();
74        res.x *= Self::ENDO_COEFFS[0];
75        res
76    }
77}
78
79/// G1_GENERATOR_X = 1
80pub const G1_GENERATOR_X: Fq = Fq::ONE;
81
82/// G1_GENERATOR_Y = 2
83pub const G1_GENERATOR_Y: Fq = MontFp!("2");