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 /// COFACTOR = (x - 1)^2 / 3 = 30631250834960419227450344600217059328
23 const COFACTOR: &'static [u64] = &[0x0, 0x170b5d4430000000];
24
25 /// COFACTOR_INV = COFACTOR^{-1} mod r
26 /// = 5285428838741532253824584287042945485047145357130994810877
27 #[rustfmt::skip]
28 const COFACTOR_INV: Fr = field_new!(Fr, "5285428838741532253824584287042945485047145357130994810877");
29}
30
31impl SWModelParameters for Parameters {
32 /// COEFF_A = 0
33 const COEFF_A: Fq = FQ_ZERO;
34
35 /// COEFF_B = 1
36 #[rustfmt::skip]
37 const COEFF_B: Fq = FQ_ONE;
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 /// AFFINE_GENERATOR_COEFFS = (GENERATOR_X, GENERATOR_Y)
110 const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
111 (TE_GENERATOR_X, TE_GENERATOR_Y);
112
113 type MontgomeryModelParameters = Parameters;
114
115 /// Multiplication by `a` is multiply by `-1`.
116 #[inline(always)]
117 fn mul_by_a(elem: &Self::BaseField) -> Self::BaseField {
118 elem.neg()
119 }
120}
121
122// BLS12-377::G1 also has a Montgomery form.
123// BLS12-377::G1 also has a twisted Edwards form.
124// It can be obtained via the following script, implementing
125// SW -> Montgomery transformation: <https://en.wikipedia.org/wiki/Montgomery_curve>
126// ``` sage
127//
128// # modulus
129// p=0x1ae3a4617c510eac63b05c06ca1493b1a22d9f300f5138f1ef3622fba094800170b5d44300000008508c00000000001
130// Fp=Zmod(p)
131//
132// #####################################################
133// # Weierstrass curve: y² = x³ + A * x + B
134// #####################################################
135// # curve y^2 = x^3 + 1
136// WA=Fp(0)
137// WB=Fp(1)
138//
139// #####################################################
140// # Montgomery curve: By² = x³ + A * x² + x
141// #####################################################
142// # root for x^3 + 1 = 0
143// alpha = -1
144// # s = 1 / (sqrt(3alpha^2 + a))
145// s = 1/(Fp(3).sqrt())
146//
147// # MA = 3 * alpha * s
148// MA=Fp(228097355113300204138531148905234651262148041026195375645000724271212049151994375092458297304264351187709081232384)
149// # MB = s
150// MB=Fp(10189023633222963290707194929886294091415157242906428298294512798502806398782149227503530278436336312243746741931)
151// ```
152impl MontgomeryModelParameters for Parameters {
153 /// COEFF_A = 228097355113300204138531148905234651262148041026195375645000724271212049151994375092458297304264351187709081232384
154 #[rustfmt::skip]
155 const COEFF_A: Fq = field_new!(Fq, "228097355113300204138531148905234651262148041026195375645000724271212049151994375092458297304264351187709081232384");
156
157 /// COEFF_B = 10189023633222963290707194929886294091415157242906428298294512798502806398782149227503530278436336312243746741931
158 #[rustfmt::skip]
159 const COEFF_B: Fq = field_new!(Fq, "10189023633222963290707194929886294091415157242906428298294512798502806398782149227503530278436336312243746741931");
160
161 type TEModelParameters = Parameters;
162}
163
164/// G1_GENERATOR_X =
165/// 81937999373150964239938255573465948239988671502647976594219695644855304257327692006745978603320413799295628339695
166#[rustfmt::skip]
167pub const G1_GENERATOR_X: Fq = field_new!(Fq, "81937999373150964239938255573465948239988671502647976594219695644855304257327692006745978603320413799295628339695");
168
169/// G1_GENERATOR_Y =
170/// 241266749859715473739788878240585681733927191168601896383759122102112907357779751001206799952863815012735208165030
171#[rustfmt::skip]
172pub const G1_GENERATOR_Y: Fq = field_new!(Fq, "241266749859715473739788878240585681733927191168601896383759122102112907357779751001206799952863815012735208165030");
173
174// The generator for twisted Edward form is the same SW generator converted into the normalized TE form (TE2).
175// ``` sage
176// # following scripts in previous section
177// #####################################################
178// # Weierstrass curve generator
179// #####################################################
180// Wx = Fp(81937999373150964239938255573465948239988671502647976594219695644855304257327692006745978603320413799295628339695)
181// Wy = Fp(241266749859715473739788878240585681733927191168601896383759122102112907357779751001206799952863815012735208165030)
182//
183// assert(Wy^2 - Wx^3 - WA * Wx - WB == 0)
184//
185// #####################################################
186// # Montgomery curve generator
187// #####################################################
188// # x = s * (x - alpha)
189// Mx = Fp(251803586774461569862800610331871502335378228972505599912537082323947581271784390797244487924068052270360793200630)
190// # y = s * y
191// My = Fp(77739247071951651095607889637653357561348174979132042929587539214321586851215673796661346812932566642719051699820)
192//
193// assert(MB * My^2 == Mx^3+ MA * Mx^2 + Mx)
194//
195// # #####################################################
196// # # Twisted Edwards curve 1 generator
197// # #####################################################
198// # x = Mx/My
199// TE1x = Fp(82241236807150726090333472814441006963902378430536027612759193445733851062772474760677400112551677454953925168208)
200// # y = (Mx - 1)/(Mx+1)
201// TE1y = Fp(6177051365529633638563236407038680211609544222665285371549726196884440490905471891908272386851767077598415378235)
202//
203// assert( TE1a * TE1x^2 + TE1y^2 == 1 + TE1d * TE1x^2 * TE1y^2 )
204//
205//
206// # #####################################################
207// # # Twisted Edwards curve 2 generator
208// # #####################################################
209// beta = (-TE1a).sqrt()
210// # x = TE1x * sqrt(-TE1a)
211// TE2x = Fp(71222569531709137229370268896323705690285216175189308202338047559628438110820800641278662592954630774340654489393)
212// # y = TE1y
213// TE2y = Fp(6177051365529633638563236407038680211609544222665285371549726196884440490905471891908272386851767077598415378235)
214//
215// assert( TE2a * TE2x^2 + TE2y^2 == 1 + TE2d * TE2x^2 * TE2y^2 )
216// ```
217/// TE_GENERATOR_X =
218/// 71222569531709137229370268896323705690285216175189308202338047559628438110820800641278662592954630774340654489393
219#[rustfmt::skip]
220pub const TE_GENERATOR_X: Fq = field_new!(Fq, "71222569531709137229370268896323705690285216175189308202338047559628438110820800641278662592954630774340654489393");
221
222/// TE_GENERATOR_Y =
223/// 6177051365529633638563236407038680211609544222665285371549726196884440490905471891908272386851767077598415378235
224#[rustfmt::skip]
225pub const TE_GENERATOR_Y: Fq = field_new!(Fq, "6177051365529633638563236407038680211609544222665285371549726196884440490905471891908272386851767077598415378235");