arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
ark-curves-cherry-picked/bls12_377/src/curves/g1.rs
bls12_377/src/curves/g1.rsRaw
  1use ark_ec::models::{
  2    short_weierstrass::{Affine as SWAffine, SWCurveConfig},
  3    twisted_edwards::{
  4        Affine as TEAffine, MontCurveConfig, Projective as TEProjective, TECurveConfig,
  5    },
  6    CurveConfig,
  7};
  8use ark_ff::{Field, MontFp, Zero};
  9use core::ops::Neg;
 10
 11use crate::{Fq, Fr};
 12
 13#[derive(Clone, Default, PartialEq, Eq)]
 14pub struct Config;
 15
 16impl CurveConfig for Config {
 17    type BaseField = Fq;
 18    type ScalarField = Fr;
 19
 20    /// COFACTOR = (x - 1)^2 / 3  = 30631250834960419227450344600217059328
 21    const COFACTOR: &'static [u64] = &[0x0, 0x170b5d4430000000];
 22
 23    /// COFACTOR_INV = COFACTOR^{-1} mod r
 24    /// = 5285428838741532253824584287042945485047145357130994810877
 25    const COFACTOR_INV: Fr = MontFp!("5285428838741532253824584287042945485047145357130994810877");
 26}
 27
 28impl SWCurveConfig for Config {
 29    /// COEFF_A = 0
 30    const COEFF_A: Fq = Fq::ZERO;
 31
 32    /// COEFF_B = 1
 33    const COEFF_B: Fq = Fq::ONE;
 34
 35    /// AFFINE_GENERATOR_COEFFS = (G1_GENERATOR_X, G1_GENERATOR_Y)
 36    const GENERATOR: G1SWAffine = G1SWAffine::new_unchecked(G1_GENERATOR_X, G1_GENERATOR_Y);
 37
 38    #[inline(always)]
 39    fn mul_by_a(_: Self::BaseField) -> Self::BaseField {
 40        Self::BaseField::zero()
 41    }
 42}
 43
 44pub type G1SWAffine = SWAffine<Config>;
 45pub type G1TEAffine = TEAffine<Config>;
 46pub type G1TEProjective = TEProjective<Config>;
 47
 48/// Bls12_377::G1 also has a twisted Edwards form.
 49/// It can be obtained via the following script, implementing
 50/// 1. SW -> Montgomery -> TE1 transformation: <https://en.wikipedia.org/wiki/Montgomery_curve>
 51/// 2. TE1 -> TE2 normalization (enforcing `a = -1`)
 52/// ``` sage
 53/// # modulus
 54/// p = 0x1ae3a4617c510eac63b05c06ca1493b1a22d9f300f5138f1ef3622fba094800170b5d44300000008508c00000000001
 55/// Fp = Zmod(p)
 56///
 57/// #####################################################
 58/// # Weierstrass curve: y² = x³ + A * x + B
 59/// #####################################################
 60/// # curve y^2 = x^3 + 1
 61/// WA = Fp(0)
 62/// WB = Fp(1)
 63///
 64/// #####################################################
 65/// # Montgomery curve: By² = x³ + A * x² + x
 66/// #####################################################
 67/// # root for x^3 + 1 = 0
 68/// alpha = -1
 69/// # s = 1 / (sqrt(3alpha^2 + a))
 70/// s = 1/(Fp(3).sqrt())
 71///
 72/// # MA = 3 * alpha * s
 73/// MA = Fp(228097355113300204138531148905234651262148041026195375645000724271212049151994375092458297304264351187709081232384)
 74/// # MB = s
 75/// MB = Fp(10189023633222963290707194929886294091415157242906428298294512798502806398782149227503530278436336312243746741931)
 76///
 77/// # #####################################################
 78/// # # Twisted Edwards curve 1: a * x² + y² = 1 + d * x² * y²
 79/// # #####################################################
 80/// # We first convert to TE form obtaining a curve with a != -1, and then
 81/// # apply a transformation to obtain a TE curve with a = -1.
 82/// # a = (MA+2)/MB
 83/// TE1a = Fp(61134141799337779744243169579317764548490943457438569789767076791016838392692895365021181670618017873462480451583)
 84/// # b = (MA-2)/MB
 85/// TE1d = Fp(197530284213631314266409564115575768987902569297476090750117185875703629955647927409947706468955342250977841006588)
 86///
 87/// # #####################################################
 88/// # # Twisted Edwards curve 2: a * x² + y² = 1 + d * x² * y²
 89/// # #####################################################
 90/// # a = -1
 91/// TE2a = Fp(-1)
 92/// # b = -TE1d/TE1a
 93/// TE2d = Fp(122268283598675559488486339158635529096981886914877139579534153582033676785385790730042363341236035746924960903179)
 94/// ```
 95impl TECurveConfig for Config {
 96    /// COEFF_A = -1
 97    const COEFF_A: Fq = MontFp!("-1");
 98
 99    /// COEFF_D = 122268283598675559488486339158635529096981886914877139579534153582033676785385790730042363341236035746924960903179 mod q
100    const COEFF_D: Fq = MontFp!("122268283598675559488486339158635529096981886914877139579534153582033676785385790730042363341236035746924960903179");
101
102    /// AFFINE_GENERATOR_COEFFS = (GENERATOR_X, GENERATOR_Y)
103    const GENERATOR: G1TEAffine = G1TEAffine::new_unchecked(TE_GENERATOR_X, TE_GENERATOR_Y);
104
105    type MontCurveConfig = Config;
106
107    /// Multiplication by `a` is multiply by `-1`.
108    #[inline(always)]
109    fn mul_by_a(elem: Self::BaseField) -> Self::BaseField {
110        elem.neg()
111    }
112}
113
114// BLS12-377::G1 also has a Montgomery form.
115// BLS12-377::G1 also has a twisted Edwards form.
116// It can be obtained via the following script, implementing
117// SW -> Montgomery transformation: <https://en.wikipedia.org/wiki/Montgomery_curve>
118// ``` sage
119// # modulus
120// p=0x1ae3a4617c510eac63b05c06ca1493b1a22d9f300f5138f1ef3622fba094800170b5d44300000008508c00000000001
121// Fp=Zmod(p)
122//
123// #####################################################
124// # Weierstrass curve: y² = x³ + A * x + B
125// #####################################################
126// # curve y^2 = x^3 + 1
127// WA=Fp(0)
128// WB=Fp(1)
129//
130// #####################################################
131// # Montgomery curve: By² = x³ + A * x² + x
132// #####################################################
133// # root for x^3 + 1 = 0
134// alpha = -1
135// # s = 1 / (sqrt(3alpha^2 + a))
136// s = 1/(Fp(3).sqrt())
137//
138// # MA = 3 * alpha * s
139// MA=Fp(228097355113300204138531148905234651262148041026195375645000724271212049151994375092458297304264351187709081232384)
140// # MB = s
141// MB=Fp(10189023633222963290707194929886294091415157242906428298294512798502806398782149227503530278436336312243746741931)
142// ```
143impl MontCurveConfig for Config {
144    /// COEFF_A = 228097355113300204138531148905234651262148041026195375645000724271212049151994375092458297304264351187709081232384
145    const COEFF_A: Fq = MontFp!("228097355113300204138531148905234651262148041026195375645000724271212049151994375092458297304264351187709081232384");
146
147    /// COEFF_B = 10189023633222963290707194929886294091415157242906428298294512798502806398782149227503530278436336312243746741931
148    const COEFF_B: Fq = MontFp!("10189023633222963290707194929886294091415157242906428298294512798502806398782149227503530278436336312243746741931");
149
150    type TECurveConfig = Config;
151}
152
153/// G1_GENERATOR_X =
154/// 81937999373150964239938255573465948239988671502647976594219695644855304257327692006745978603320413799295628339695
155pub const G1_GENERATOR_X: Fq = MontFp!("81937999373150964239938255573465948239988671502647976594219695644855304257327692006745978603320413799295628339695");
156
157/// G1_GENERATOR_Y =
158/// 241266749859715473739788878240585681733927191168601896383759122102112907357779751001206799952863815012735208165030
159pub const G1_GENERATOR_Y: Fq = MontFp!("241266749859715473739788878240585681733927191168601896383759122102112907357779751001206799952863815012735208165030");
160
161// The generator for twisted Edward form is the same SW generator converted into
162// the normalized TE form (TE2).
163//``` sage
164// # following scripts in previous section
165// #####################################################
166// # Weierstrass curve generator
167// #####################################################
168// Wx = Fp(81937999373150964239938255573465948239988671502647976594219695644855304257327692006745978603320413799295628339695)
169// Wy = Fp(241266749859715473739788878240585681733927191168601896383759122102112907357779751001206799952863815012735208165030)
170//
171// assert(Wy^2 - Wx^3 - WA * Wx - WB == 0)
172//
173// #####################################################
174// # Montgomery curve generator
175// #####################################################
176// # x = s * (x - alpha)
177// Mx = Fp(251803586774461569862800610331871502335378228972505599912537082323947581271784390797244487924068052270360793200630)
178// # y = s * y
179// My = Fp(77739247071951651095607889637653357561348174979132042929587539214321586851215673796661346812932566642719051699820)
180//
181// assert(MB * My^2 == Mx^3+ MA * Mx^2 + Mx)
182//
183// # #####################################################
184// # # Twisted Edwards curve 1 generator
185// # #####################################################
186// # x = Mx/My
187// TE1x = Fp(82241236807150726090333472814441006963902378430536027612759193445733851062772474760677400112551677454953925168208)
188// # y = (Mx - 1)/(Mx+1)
189// TE1y = Fp(6177051365529633638563236407038680211609544222665285371549726196884440490905471891908272386851767077598415378235)
190//
191// assert( TE1a * TE1x^2 + TE1y^2 == 1 + TE1d * TE1x^2 * TE1y^2 )
192//
193//
194// # #####################################################
195// # # Twisted Edwards curve 2 generator
196// # #####################################################
197// beta = (-TE1a).sqrt()
198// # x = TE1x * sqrt(-TE1a)
199// TE2x = Fp(71222569531709137229370268896323705690285216175189308202338047559628438110820800641278662592954630774340654489393)
200// # y = TE1y
201// TE2y = Fp(6177051365529633638563236407038680211609544222665285371549726196884440490905471891908272386851767077598415378235)
202//
203// assert( TE2a * TE2x^2 + TE2y^2 == 1 + TE2d * TE2x^2 * TE2y^2 )
204// ```
205/// TE_GENERATOR_X =
206/// 71222569531709137229370268896323705690285216175189308202338047559628438110820800641278662592954630774340654489393
207pub const TE_GENERATOR_X: Fq = MontFp!("71222569531709137229370268896323705690285216175189308202338047559628438110820800641278662592954630774340654489393");
208
209/// TE_GENERATOR_Y =
210/// 6177051365529633638563236407038680211609544222665285371549726196884440490905471891908272386851767077598415378235
211pub const TE_GENERATOR_Y: Fq = MontFp!("6177051365529633638563236407038680211609544222665285371549726196884440490905471891908272386851767077598415378235");