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