decaf377/ark_curve/
bls12_377.rs

1use crate::fields::{fp::Fp, fq::Fq};
2use ark_ec::{
3    bls12::{Bls12, Bls12Config, TwistType},
4    models::short_weierstrass::SWCurveConfig,
5    models::CurveConfig,
6    short_weierstrass::Affine,
7};
8use ark_ff::{fields::models::fp2::Fp2Config, Field, Fp12Config, Fp2, Fp6, Fp6Config};
9
10pub struct F2Config;
11
12impl Fp2Config for F2Config {
13    type Fp = Fp;
14
15    const NONRESIDUE: Fp = Fp::QUADRATIC_NON_RESIDUE;
16
17    const FROBENIUS_COEFF_FP2_C1: &'static [Fp] = &[Fp::ONE, Fp::MINUS_ONE];
18}
19
20#[derive(Debug, Clone, Copy)]
21pub struct F6Config;
22
23impl Fp6Config for F6Config {
24    type Fp2Config = F2Config;
25
26    const NONRESIDUE: Fp2<Self::Fp2Config> = Fp2::new(Fp::ZERO, Fp::ONE);
27
28    const FROBENIUS_COEFF_FP6_C1: &'static [Fp2<Self::Fp2Config>] = &[
29        Fp2::new(Fp::ONE, Fp::ZERO),
30        Fp2::new(
31            Fp::from_montgomery_limbs([
32                6382252053795993818,
33                1383562296554596171,
34                11197251941974877903,
35                6684509567199238270,
36                6699184357838251020,
37                19987743694136192,
38            ]),
39            Fp::ZERO,
40        ),
41        Fp2::new(
42            Fp::from_montgomery_limbs([
43                15766275933608376691,
44                15635974902606112666,
45                1934946774703877852,
46                18129354943882397960,
47                15437979634065614942,
48                101285514078273488,
49            ]),
50            Fp::ZERO,
51        ),
52        Fp2::new(Fp::MINUS_ONE, Fp::ZERO),
53        Fp2::new(
54            Fp::from_montgomery_limbs([
55                3203870859294639911,
56                276961138506029237,
57                9479726329337356593,
58                13645541738420943632,
59                7584832609311778094,
60                101110569012358506,
61            ]),
62            Fp::ZERO,
63        ),
64        Fp2::new(
65            Fp::from_montgomery_limbs([
66                12266591053191808654,
67                4471292606164064357,
68                295287422898805027,
69                2200696361737783943,
70                17292781406793965788,
71                19812798628221209,
72            ]),
73            Fp::ZERO,
74        ),
75    ];
76
77    const FROBENIUS_COEFF_FP6_C2: &'static [Fp2<Self::Fp2Config>] = &[
78        Fp2::new(Fp::ONE, Fp::ZERO),
79        Fp2::new(
80            Fp::from_montgomery_limbs([
81                15766275933608376691,
82                15635974902606112666,
83                1934946774703877852,
84                18129354943882397960,
85                15437979634065614942,
86                101285514078273488,
87            ]),
88            Fp::ZERO,
89        ),
90        Fp2::new(
91            Fp::from_montgomery_limbs([
92                3203870859294639911,
93                276961138506029237,
94                9479726329337356593,
95                13645541738420943632,
96                7584832609311778094,
97                101110569012358506,
98            ]),
99            Fp::ZERO,
100        ),
101        Fp2::new(Fp::ONE, Fp::ZERO),
102        Fp2::new(
103            Fp::from_montgomery_limbs([
104                15766275933608376691,
105                15635974902606112666,
106                1934946774703877852,
107                18129354943882397960,
108                15437979634065614942,
109                101285514078273488,
110            ]),
111            Fp::ZERO,
112        ),
113        Fp2::new(
114            Fp::from_montgomery_limbs([
115                3203870859294639911,
116                276961138506029237,
117                9479726329337356593,
118                13645541738420943632,
119                7584832609311778094,
120                101110569012358506,
121            ]),
122            Fp::ZERO,
123        ),
124    ];
125}
126
127#[derive(Debug, Clone, Copy)]
128pub struct F12Config;
129
130impl Fp12Config for F12Config {
131    type Fp6Config = F6Config;
132
133    const NONRESIDUE: Fp6<Self::Fp6Config> = Fp6::new(Fp2::ZERO, Fp2::ONE, Fp2::ZERO);
134
135    const FROBENIUS_COEFF_FP12_C1: &'static [Fp2<F2Config>] = &[
136        Fp2::new(Fp::ONE, Fp::ZERO),
137        Fp2::new(
138            Fp::from_montgomery_limbs([
139                7981638599956744862,
140                11830407261614897732,
141                6308788297503259939,
142                10596665404780565693,
143                11693741422477421038,
144                61545186993886319,
145            ]),
146            Fp::ZERO,
147        ),
148        Fp2::new(
149            Fp::from_montgomery_limbs([
150                6382252053795993818,
151                1383562296554596171,
152                11197251941974877903,
153                6684509567199238270,
154                6699184357838251020,
155                19987743694136192,
156            ]),
157            Fp::ZERO,
158        ),
159        Fp2::new(
160            Fp::from_montgomery_limbs([
161                10965161018967488287,
162                18251363109856037426,
163                7036083669251591763,
164                16109345360066746489,
165                4679973768683352764,
166                96952949334633821,
167            ]),
168            Fp::ZERO,
169        ),
170        Fp2::new(
171            Fp::from_montgomery_limbs([
172                15766275933608376691,
173                15635974902606112666,
174                1934946774703877852,
175                18129354943882397960,
176                15437979634065614942,
177                101285514078273488,
178            ]),
179            Fp::ZERO,
180        ),
181        Fp2::new(
182            Fp::from_montgomery_limbs([
183                2983522419010743425,
184                6420955848241139694,
185                727295371748331824,
186                5512679955286180796,
187                11432976419915483342,
188                35407762340747501,
189            ]),
190            Fp::ZERO,
191        ),
192        Fp2::new(Fp::MINUS_ONE, Fp::ZERO),
193        Fp2::new(
194            Fp::from_montgomery_limbs([
195                1604484313133888867,
196                8276860247155279292,
197                14368189973808974556,
198                9733385900839616209,
199                2590275544672608076,
200                59553125712608379,
201            ]),
202            Fp::ZERO,
203        ),
204        Fp2::new(
205            Fp::from_montgomery_limbs([
206                3203870859294639911,
207                276961138506029237,
208                9479726329337356593,
209                13645541738420943632,
210                7584832609311778094,
211                101110569012358506,
212            ]),
213            Fp::ZERO,
214        ),
215        Fp2::new(
216            Fp::from_montgomery_limbs([
217                17067705967832697058,
218                1855904398914139597,
219                13640894602060642732,
220                4220705945553435413,
221                9604043198466676350,
222                24145363371860877,
223            ]),
224            Fp::ZERO,
225        ),
226        Fp2::new(
227            Fp::from_montgomery_limbs([
228                12266591053191808654,
229                4471292606164064357,
230                295287422898805027,
231                2200696361737783943,
232                17292781406793965788,
233                19812798628221209,
234            ]),
235            Fp::ZERO,
236        ),
237        Fp2::new(
238            Fp::from_montgomery_limbs([
239                6602600494079890304,
240                13686311660529037330,
241                1502938825854351055,
242                14817371350334001107,
243                2851040547234545772,
244                85690550365747197,
245            ]),
246            Fp::ZERO,
247        ),
248    ];
249}
250
251pub struct OurG1Config;
252
253impl CurveConfig for OurG1Config {
254    type BaseField = Fp;
255
256    type ScalarField = Fq;
257
258    const COFACTOR: &'static [u64] = &[0x0, 0x170b5d4430000000];
259
260    const COFACTOR_INV: Self::ScalarField = Fq::from_montgomery_limbs([
261        2013239619100046060,
262        4201184776506987597,
263        2526766393982337036,
264        1114629510922847535,
265    ]);
266}
267
268const G1_GENERATOR_X: Fp = Fp::from_montgomery_limbs([
269    2742467569752756724,
270    14217256487979144792,
271    6635299530028159197,
272    8509097278468658840,
273    14518893593143693938,
274    46181716169194829,
275]);
276
277const G1_GENERATOR_Y: Fp = Fp::from_montgomery_limbs([
278    9336971515457667571,
279    28021381849722296,
280    18085035374859187530,
281    14013031479170682136,
282    3369780711397861396,
283    35370409237953649,
284]);
285
286impl SWCurveConfig for OurG1Config {
287    const COEFF_A: Self::BaseField = Fp::ZERO;
288
289    const COEFF_B: Self::BaseField = Fp::ONE;
290
291    const GENERATOR: Affine<Self> = Affine::new_unchecked(G1_GENERATOR_X, G1_GENERATOR_Y);
292}
293
294pub struct OurG2Config;
295
296impl CurveConfig for OurG2Config {
297    type BaseField = Fp2<F2Config>;
298
299    type ScalarField = Fq;
300
301    const COFACTOR: &'static [u64] = &[
302        0x0000000000000001,
303        0x452217cc90000000,
304        0xa0f3622fba094800,
305        0xd693e8c36676bd09,
306        0x8c505634fae2e189,
307        0xfbb36b00e1dcc40c,
308        0xddd88d99a6f6a829,
309        0x26ba558ae9562a,
310    ];
311
312    const COFACTOR_INV: Self::ScalarField = Fq::from_montgomery_limbs([
313        15499857013495546999,
314        4613531467548868169,
315        14546778081091178013,
316        549402535258503313,
317    ]);
318}
319
320pub const G2_GENERATOR_X: Fp2<F2Config> = Fp2::new(
321    Fp::from_montgomery_limbs([
322        7534593107747697243,
323        7390176809662624395,
324        16990527120569264207,
325        2168572232730518502,
326        9443417493680878057,
327        109821976444144002,
328    ]),
329    Fp::from_montgomery_limbs([
330        6846220294590070585,
331        17925825951095956135,
332        15355657819052935248,
333        16808496983586309946,
334        18438381910454061441,
335        78904498268135389,
336    ]),
337);
338
339pub const G2_GENERATOR_Y: Fp2<F2Config> = Fp2::new(
340    Fp::from_montgomery_limbs([
341        15398259615690998543,
342        413927750809907693,
343        6945668964135547374,
344        3622202639115414553,
345        11542235856284301842,
346        111174645670174930,
347    ]),
348    Fp::from_montgomery_limbs([
349        6296061721506977525,
350        16832990956758385678,
351        2538166719760928425,
352        9449086974571632418,
353        3122185334549858583,
354        25052933797626130,
355    ]),
356);
357
358impl SWCurveConfig for OurG2Config {
359    const COEFF_A: Self::BaseField = Fp2::new(OurG1Config::COEFF_A, OurG1Config::COEFF_A);
360
361    const COEFF_B: Self::BaseField = Fp2::new(
362        Fp::ZERO,
363        Fp::from_montgomery_limbs([
364            9255502405446297221,
365            10229180150694123945,
366            9215585410771530959,
367            13357015519562362907,
368            5437107869987383107,
369            16259554076827459,
370        ]),
371    );
372
373    const GENERATOR: Affine<Self> = Affine::new_unchecked(G2_GENERATOR_X, G2_GENERATOR_Y);
374}
375
376/// A marker struct for our implementation of BLS12-377 over our backend fields, using Arkworks.
377pub struct Config;
378
379impl Bls12Config for Config {
380    const X: &'static [u64] = &[0x8508c00000000001];
381    /// `x` is positive
382    const X_IS_NEGATIVE: bool = false;
383
384    const TWIST_TYPE: TwistType = TwistType::D;
385
386    type Fp = Fp;
387
388    type Fp2Config = F2Config;
389
390    type Fp6Config = F6Config;
391
392    type Fp12Config = F12Config;
393
394    type G1Config = OurG1Config;
395
396    type G2Config = OurG2Config;
397}
398
399pub type Bls12_377 = Bls12<Config>;