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