1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
use crate::field::*;
use crate::traits::*;
use crate::fp::*;
use crate::extension_towers::fp2::*;
use crate::extension_towers::fp6_as_3_over_2::*;
use crate::extension_towers::fp12_as_2_over3_over_2::*;
use crate::weierstrass::*;
use crate::weierstrass::curve::*;
use crate::pairings::bls12::*;
use crate::pairings::TwistType;
use crate::integers::MaxFieldUint;

struct Bls12_381Extension2;

impl FieldExtension for Bls12_381Extension2 {
    const EXTENSION_DEGREE: usize = 2;
    
    type Element = Fp<'static, U384Repr, PrimeField<U384Repr>>;

    #[inline(always)]
    fn multiply_by_non_residue(&self, el: &mut Self::Element) {
        // non-residue is equal to -1, so we just negate
        el.negate();
    }
}

struct Bls12_381Extension6;

impl FieldExtension for Bls12_381Extension6 {
    const EXTENSION_DEGREE: usize = 3;
    
    type Element = Fp2<'static, U384Repr, PrimeField<U384Repr>>;

    #[inline(always)]
    fn multiply_by_non_residue(&self, el: &mut Self::Element) {
        // manually unroll multiplication by (1, 1)
        let v0 = el.c0.clone();
        let mut v1 = el.c1.clone();

        el.c1.add_assign(&el.c0);

        // non-residue c0 + c1
        let mut t0 = Fp::one(el.extension_field.field);
        t0.double();

        el.c1.mul_assign(&t0);
        el.c1.sub_assign(&v0);
        el.c1.sub_assign(&v1);
        el.c0 = v0;

        v1.mul_by_nonresidue(&Bls12_381Extension2);
        el.c0.add_assign(&v1);
   
    }
}

const REPR_ZERO: U384Repr = U384Repr([0,0,0,0,0,0]);

pub const BLS12_381_MODULUS_UINT: MaxFieldUint = MaxFieldUint::from_limbs(
    [
        0xb9feffffffffaaab,0x1eabfffeb153ffff,0x6730d2a0f6b0f624,0x64774b84f38512bf,
        0x4b1ba7b6434bacd7,0x1a0111ea397fe69a, 0x0, 0x0,
        0x0, 0x0, 0x0, 0x0,
        0x0, 0x0, 0x0, 0x0
    ]
);

pub const BLS12_381_MODULUS: U384Repr = U384Repr([0xb9feffffffffaaab,0x1eabfffeb153ffff,0x6730d2a0f6b0f624,0x64774b84f38512bf,0x4b1ba7b6434bacd7,0x1a0111ea397fe69a]);
const BLS12_381_R: U384Repr = U384Repr([0x760900000002fffd,0xebf4000bc40c0002,0x5f48985753c758ba,0x77ce585370525745,0x5c071a97a256ec6d,0x15f65ec3fa80e493]);
const BLS12_381_R2: U384Repr = U384Repr([0xf4df1f341c341746,0x0a76e6a609d104f1,0x8de5476c4c95b6d5,0x67eb88a9939d83c0,0x9a793e85b519952d,0x11988fe592cae3aa]);
const BLS12_381_MONT_INV: u64 = 0x89f3fffcfffcfffd;

pub const BLS12_381_FIELD: PrimeField<U384Repr> = PrimeField::<U384Repr> {
    mont_power: 384,
    modulus_bits: 381,
    modulus: BLS12_381_MODULUS,
    mont_r: BLS12_381_R,
    mont_r2: BLS12_381_R2,
    mont_inv: BLS12_381_MONT_INV,  
};

const BLS12_381_FP_NON_RESIDUE_REPR: U384Repr = U384Repr([0x43f5fffffffcaaae,0x32b7fff2ed47fffd,0x07e83a49a2e99d69,0xeca8f3318332bb7a,0xef148d1ea0f4c069,0x040ab3263eff0206]);

const BLS12_381_FP_NON_RESIDUE: decl_fp!(U384Repr) = repr_into_fp!(
    BLS12_381_FP_NON_RESIDUE_REPR, 
    U384Repr,
    BLS12_381_FIELD
);

pub const BLS12_381_FP_ZERO: decl_fp!(U384Repr) = repr_into_fp!(
    REPR_ZERO, 
    U384Repr,
    BLS12_381_FIELD
);

pub const BLS12_381_FP_ONE: decl_fp!(U384Repr) = repr_into_fp!(
    BLS12_381_R, 
    U384Repr,
    BLS12_381_FIELD
);
 
const BLS12_381_EXTENSION_2_FROB_COEFF_0_REPR: U384Repr = U384Repr([0x760900000002fffd,0xebf4000bc40c0002,0x5f48985753c758ba,0x77ce585370525745,0x5c071a97a256ec6d,0x15f65ec3fa80e493]);
const BLS12_381_EXTENSION_2_FROB_COEFF_1_REPR: U384Repr = U384Repr([0x43f5fffffffcaaae,0x32b7fff2ed47fffd,0x07e83a49a2e99d69,0xeca8f3318332bb7a,0xef148d1ea0f4c069,0x040ab3263eff0206]);

const BLS12_381_EXTENSION_2_FROB_COEFF_0: decl_fp!(U384Repr) = repr_into_fp!(
    BLS12_381_EXTENSION_2_FROB_COEFF_0_REPR, 
    U384Repr,
    BLS12_381_FIELD
);

const BLS12_381_EXTENSION_2_FROB_COEFF_1: decl_fp!(U384Repr) = repr_into_fp!(
    BLS12_381_EXTENSION_2_FROB_COEFF_1_REPR, 
    U384Repr,
    BLS12_381_FIELD
);


pub const BLS12_381_EXTENSION_2_FIELD: Extension2<'static, U384Repr, PrimeField<U384Repr>> = 
    Extension2::<'static, U384Repr, PrimeField<U384Repr>> {
        field: &BLS12_381_FIELD,
        non_residue: BLS12_381_FP_NON_RESIDUE,
        frobenius_coeffs_c1: [BLS12_381_EXTENSION_2_FROB_COEFF_0, BLS12_381_EXTENSION_2_FROB_COEFF_1],
        frobenius_coeffs_are_calculated: true
    };


const BLS12_381_FP2_NON_RESIDUE_C0_REPR: U384Repr = U384Repr([0x760900000002fffd,0xebf4000bc40c0002,0x5f48985753c758ba,0x77ce585370525745,0x5c071a97a256ec6d,0x15f65ec3fa80e493]);
const BLS12_381_FP2_NON_RESIDUE_C1_REPR: U384Repr = U384Repr([0x760900000002fffd,0xebf4000bc40c0002,0x5f48985753c758ba,0x77ce585370525745,0x5c071a97a256ec6d,0x15f65ec3fa80e493]);

const BLS12_381_FP2_NON_RESIDUE_C0: decl_fp!(U384Repr) = repr_into_fp!(
    BLS12_381_FP2_NON_RESIDUE_C1_REPR, 
    U384Repr,
    BLS12_381_FIELD
);

const BLS12_381_FP2_NON_RESIDUE_C1: decl_fp!(U384Repr) = repr_into_fp!(
    BLS12_381_FP2_NON_RESIDUE_C1_REPR, 
    U384Repr,
    BLS12_381_FIELD
);

const BLS12_381_FP2_NON_RESIDUE: decl_fp2!(U384Repr) = repr_into_fp2!(
    BLS12_381_FP2_NON_RESIDUE_C0, 
    BLS12_381_FP2_NON_RESIDUE_C1,
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

pub const BLS12_381_SUBGROUP_ORDER: [u64; 4] = [
    0xffffffff00000001,
    0x53bda402fffe5bfe,
    0x3339d80809a1d805,
    0x73eda753299d7d48
];

const BLS12_381_X: [u64; 1] = [0xd201000000010000];
const BLS12_381_X_IS_NEGATIVE: bool = true;

const BLS12_381_B_FOR_G1_REPR: U384Repr = U384Repr([0xaa270000000cfff3,0x53cc0032fc34000a,0x478fe97a6b0a807f,0xb1d37ebee6ba24d7,0x8ec9733bbf78ab2f,0x09d645513d83de7e]);
pub const BLS12_381_B_FOR_G1: Fp<'static, U384Repr, PrimeField<U384Repr>> = 
    Fp::<'static, U384Repr, PrimeField<U384Repr>> {
        field: &BLS12_381_FIELD,
        repr: BLS12_381_B_FOR_G1_REPR
    };    

const BLS12_381_B_FOR_G2_C0_REPR: U384Repr = U384Repr([0xaa270000000cfff3,0x53cc0032fc34000a,0x478fe97a6b0a807f,0xb1d37ebee6ba24d7,0x8ec9733bbf78ab2f,0x09d645513d83de7e]);
const BLS12_381_B_FOR_G2_C1_REPR: U384Repr = U384Repr([0xaa270000000cfff3,0x53cc0032fc34000a,0x478fe97a6b0a807f,0xb1d37ebee6ba24d7,0x8ec9733bbf78ab2f,0x09d645513d83de7e]);

const BLS12_381_B_FOR_G2_C0: Fp<'static, U384Repr, PrimeField<U384Repr>> = 
    Fp::<'static, U384Repr, PrimeField<U384Repr>> {
        field: &BLS12_381_FIELD,
        repr: BLS12_381_B_FOR_G2_C0_REPR
    };  

const BLS12_381_B_FOR_G2_C1: Fp<'static, U384Repr, PrimeField<U384Repr>> = 
    Fp::<'static, U384Repr, PrimeField<U384Repr>> {
        field: &BLS12_381_FIELD,
        repr: BLS12_381_B_FOR_G2_C1_REPR
    };    

pub const BLS12_381_B_FOR_G2: Fp2<'static, U384Repr, PrimeField<U384Repr>> = 
    Fp2::<'static, U384Repr, PrimeField<U384Repr>> {
        c0: BLS12_381_B_FOR_G2_C0,
        c1: BLS12_381_B_FOR_G2_C1,
        extension_field: &BLS12_381_EXTENSION_2_FIELD
    };

pub const BLS12_381_G1_CURVE_PARAMETERS: CurveOverFpParameters<'static, U384Repr, PrimeField<U384Repr>> = 
    CurveOverFpParameters::<'static, U384Repr, PrimeField<U384Repr>> {
        field: &BLS12_381_FIELD
    };

pub const BLS12_381_G2_CURVE_PARAMETERS: CurveOverFp2Parameters<'static, U384Repr, PrimeField<U384Repr>> = 
    CurveOverFp2Parameters::<'static, U384Repr, PrimeField<U384Repr>> {
        field: &BLS12_381_EXTENSION_2_FIELD
    };

pub const BLS12_381_FP2_ZERO: decl_fp2!(U384Repr) = repr_into_fp2!(
    BLS12_381_FP_ZERO, 
    BLS12_381_FP_ZERO,
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

pub const BLS12_381_FP2_ONE: decl_fp2!(U384Repr) = repr_into_fp2!(
    BLS12_381_FP_ONE, 
    BLS12_381_FP_ZERO,
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP6_FROB_C1_0: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x760900000002fffd,0xebf4000bc40c0002,0x5f48985753c758ba,0x77ce585370525745,0x5c071a97a256ec6d,0x15f65ec3fa80e493]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP6_FROB_C1_1: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0xcd03c9e48671f071,0x5dab22461fcda5d2,0x587042afd3851b95,0x8eb60ebe01bacb9e,0x03f97d6e83d050d2,0x18f0206554638741]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP6_FROB_C1_2: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x30f1361b798a64e8,0xf3b8ddab7ece5a2a,0x16a8ca3ac61577f7,0xc26a2ff874fd029b,0x3636b76660701c6e,0x051ba4ab241b6160]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP6_FROB_C1_3: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x760900000002fffd,0xebf4000bc40c0002,0x5f48985753c758ba,0x77ce585370525745,0x5c071a97a256ec6d,0x15f65ec3fa80e493]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP6_FROB_C1_4: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;
const BLS12_381_FP6_FROB_C1_5: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;

const BLS12_381_FP6_FROB_C2_0: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x760900000002fffd,0xebf4000bc40c0002,0x5f48985753c758ba,0x77ce585370525745,0x5c071a97a256ec6d,0x15f65ec3fa80e493]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP6_FROB_C2_1: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x890dc9e4867545c3,0x2af322533285a5d5,0x50880866309b7e2c,0xa20d1b8c7e881024,0x14e4f04fe2db9068,0x14e56d3f1564853a]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP6_FROB_C2_2: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0xcd03c9e48671f071,0x5dab22461fcda5d2,0x587042afd3851b95,0x8eb60ebe01bacb9e,0x03f97d6e83d050d2,0x18f0206554638741]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP6_FROB_C2_3: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x43f5fffffffcaaae,0x32b7fff2ed47fffd,0x07e83a49a2e99d69,0xeca8f3318332bb7a,0xef148d1ea0f4c069,0x040ab3263eff0206]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP6_FROB_C2_4: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;
const BLS12_381_FP6_FROB_C2_5: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;

pub const BLS12_381_EXTENSION_6_FIELD: Extension3Over2<'static, U384Repr, PrimeField<U384Repr>> = 
    Extension3Over2::<'static, U384Repr, PrimeField<U384Repr>> {
        non_residue: BLS12_381_FP2_NON_RESIDUE,
        field: &BLS12_381_EXTENSION_2_FIELD,
        frobenius_coeffs_c1: [BLS12_381_FP6_FROB_C1_0, BLS12_381_FP6_FROB_C1_1, BLS12_381_FP6_FROB_C1_2, BLS12_381_FP6_FROB_C1_3, BLS12_381_FP6_FROB_C1_4, BLS12_381_FP6_FROB_C1_5],
        frobenius_coeffs_c2: [BLS12_381_FP6_FROB_C2_0, BLS12_381_FP6_FROB_C2_1, BLS12_381_FP6_FROB_C2_2, BLS12_381_FP6_FROB_C2_3, BLS12_381_FP6_FROB_C2_4, BLS12_381_FP6_FROB_C2_5],
        frobenius_coeffs_are_calculated: true
    };


const BLS12_381_FP12_FROB_C1_0: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x760900000002fffd,0xebf4000bc40c0002,0x5f48985753c758ba,0x77ce585370525745,0x5c071a97a256ec6d,0x15f65ec3fa80e493]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP12_FROB_C1_1: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x07089552b319d465,0xc6695f92b50a8313,0x97e83cccd117228f,0xa35baecab2dc29ee,0x1ce393ea5daace4d,0x08f2220fb0fb66eb]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0xb2f66aad4ce5d646,0x5842a06bfc497cec,0xcf4895d42599d394,0xc11b9cba40a8e8d0,0x2e3813cbe5a0de89,0x110eefda88847faf]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP12_FROB_C1_2: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0xecfb361b798dba3a,0xc100ddb891865a2c,0x0ec08ff1232bda8e,0xd5c13cc6f1ca4721,0x47222a47bf7b5c04,0x0110f184e51c5f59]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP12_FROB_C1_3: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x3e2f585da55c9ad1,0x4294213d86c18183,0x382844c88b623732,0x92ad2afd19103e18,0x1d794e4fac7cf0b9,0x0bd592fc7d825ec8]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x7bcfa7a25aa30fda,0xdc17dec12a927e7c,0x2f088dd86b4ebef1,0xd1ca2087da74d4a7,0x2da2596696cebc1d,0x0e2b7eedbbfd87d2]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP12_FROB_C1_4: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;
const BLS12_381_FP12_FROB_C1_5: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;

const BLS12_381_FP12_FROB_C1_6: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x43f5fffffffcaaae,0x32b7fff2ed47fffd,0x07e83a49a2e99d69,0xeca8f3318332bb7a,0xef148d1ea0f4c069,0x040ab3263eff0206]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_FP12_FROB_C1_7: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;
const BLS12_381_FP12_FROB_C1_8: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;
const BLS12_381_FP12_FROB_C1_9: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;
const BLS12_381_FP12_FROB_C1_10: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;
const BLS12_381_FP12_FROB_C1_11: decl_fp2!(U384Repr) = BLS12_381_FP2_ZERO;

const BLS12_381_FP6_ZERO: Fp6<'static, U384Repr, PrimeField<U384Repr>> = 
    Fp6::<'static, U384Repr, PrimeField<U384Repr>> {
        c0: BLS12_381_FP2_ZERO,
        c1: BLS12_381_FP2_ZERO,
        c2: BLS12_381_FP2_ZERO,
        extension_field: &BLS12_381_EXTENSION_6_FIELD
    };

pub const BLS12_381_EXTENSION_12_FIELD: Extension2Over3Over2<'static, U384Repr, PrimeField<U384Repr>> = 
Extension2Over3Over2::<'static, U384Repr, PrimeField<U384Repr>> {
    non_residue: BLS12_381_FP6_ZERO,
    field: &BLS12_381_EXTENSION_6_FIELD,
    frobenius_coeffs_c1: [
            BLS12_381_FP12_FROB_C1_0, BLS12_381_FP12_FROB_C1_1,
            BLS12_381_FP12_FROB_C1_2, BLS12_381_FP12_FROB_C1_3,
            BLS12_381_FP12_FROB_C1_4, BLS12_381_FP12_FROB_C1_5,
            BLS12_381_FP12_FROB_C1_6, BLS12_381_FP12_FROB_C1_7,
            BLS12_381_FP12_FROB_C1_8, BLS12_381_FP12_FROB_C1_9,
            BLS12_381_FP12_FROB_C1_10, BLS12_381_FP12_FROB_C1_11
        ],
    frobenius_coeffs_are_calculated: true
};   

pub const BLS12_381_G1_CURVE: WeierstrassCurve<'static, CurveOverFpParameters<'static, U384Repr, PrimeField<U384Repr>>> = 
    WeierstrassCurve::<'static, CurveOverFpParameters<'static, U384Repr, PrimeField<U384Repr>>> {
        a: BLS12_381_FP_ZERO,
        b: BLS12_381_B_FOR_G1,
        curve_type: CurveType::AIsZero,
        subgroup_order_repr: &BLS12_381_SUBGROUP_ORDER,
        params: &BLS12_381_G1_CURVE_PARAMETERS
    };   

pub const BLS12_381_G2_CURVE: WeierstrassCurve<'static, CurveOverFp2Parameters<'static, U384Repr, PrimeField<U384Repr>>> = 
    WeierstrassCurve::<'static, CurveOverFp2Parameters<'static, U384Repr, PrimeField<U384Repr>>> {
        a: BLS12_381_FP2_ZERO,
        b: BLS12_381_B_FOR_G2,
        curve_type: CurveType::AIsZero,
        subgroup_order_repr: &BLS12_381_SUBGROUP_ORDER,
        params: &BLS12_381_G2_CURVE_PARAMETERS
    };   

const BLS12_381_G1_GENERATOR_X: decl_fp!(U384Repr) = repr_into_fp!(
    U384Repr([0x5cb38790fd530c16,0x7817fc679976fff5,0x154f95c7143ba1c1,0xf0ae6acdf3d0e747,0xedce6ecc21dbf440,0x120177419e0bfb75]), 
    U384Repr,
    BLS12_381_FIELD
);
    
const BLS12_381_G1_GENERATOR_Y: decl_fp!(U384Repr) = repr_into_fp!(
    U384Repr([0xbaac93d50ce72271,0x8c22631a7918fd8e,0xdd595f13570725ce,0x51ac582950405194,0x0e1c8c3fad0059c0,0x0bbc3efc5008a26a]), 
    U384Repr,
    BLS12_381_FIELD
);

const BLS12_381_G2_GENERATOR_X: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0xf5f28fa202940a10,0xb3f5fb2687b4961a,0xa1a893b53e2ae580,0x9894999d1a3caee9,0x6f67b7631863366b,0x058191924350bcd7]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0xa5a9c0759e23f606,0xaaa0c59dbccd60c3,0x3bb17e18e2867806,0x1b1ab6cc8541b367,0xc2b6ed0ef2158547,0x11922a097360edf3]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

const BLS12_381_G2_GENERATOR_Y: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x4c730af860494c4a,0x597cfa1f5e369c5a,0xe7e6856caa0a635a,0xbbefb5e96e0d495f,0x07d3a975f0ef25a2,0x0083fd8e7e80dae5]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0xadc0fc92df64b05d,0x18aa270a2b1461dc,0x86adac6a3be4eba0,0x79495c4ec93da33a,0xe7175850a43ccaed,0x0b2bc2a163de1bf2]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

pub const BLS12_381_G1_GENERATOR: CurvePoint<'static, CurveOverFpParameters<'static, U384Repr, PrimeField<U384Repr>>> = 
    CurvePoint::<'static, CurveOverFpParameters<'static, U384Repr, PrimeField<U384Repr>>> 
    {
        curve: &BLS12_381_G1_CURVE,
        x: BLS12_381_G1_GENERATOR_X,
        y: BLS12_381_G1_GENERATOR_Y,
        z: BLS12_381_FP_ONE,
    };

pub const BLS12_381_G2_GENERATOR: CurvePoint<'static, CurveOverFp2Parameters<'static, U384Repr, PrimeField<U384Repr>>> = 
    CurvePoint::<'static, CurveOverFp2Parameters<'static, U384Repr, PrimeField<U384Repr>>>
    {
        curve: &BLS12_381_G2_CURVE,
        x: BLS12_381_G2_GENERATOR_X,
        y: BLS12_381_G2_GENERATOR_Y,
        z: BLS12_381_FP2_ONE,
    };

pub const BLS12_381_PAIRING_ENGINE: Bls12Instance<
    'static, 
    U384Repr, 
    PrimeField<U384Repr>, 
    CurveOverFpParameters<'static, U384Repr, PrimeField<U384Repr>>,
    CurveOverFp2Parameters<'static, U384Repr, PrimeField<U384Repr>>
> = Bls12Instance::<
    'static, 
    U384Repr, 
    PrimeField<U384Repr>, 
    CurveOverFpParameters<'static, U384Repr, PrimeField<U384Repr>>,
    CurveOverFp2Parameters<'static, U384Repr, PrimeField<U384Repr>>
> {
    x: &BLS12_381_X,
    x_is_negative: BLS12_381_X_IS_NEGATIVE,
    twist_type: TwistType::M,
    base_field: &BLS12_381_FIELD,
    curve: &BLS12_381_G1_CURVE,
    curve_twist: &BLS12_381_G2_CURVE,
    fp2_extension: &BLS12_381_EXTENSION_2_FIELD,
    fp6_extension: &BLS12_381_EXTENSION_6_FIELD,
    fp12_extension: &BLS12_381_EXTENSION_12_FIELD,
    prefer_naf: false,
    x_naf: Vec::new()
};


// isogeny for field-to-curve mapping

pub const BLS12_381_G1_ISOGENY_A: decl_fp!(U384Repr) = repr_into_fp!(
    U384Repr([0x2f65aa0e9af5aa51,0x86464c2d1e8416c3,0xb85ce591b7bd31e2,0x27e11c91b5f24e7c,0x28376eda6bfc1835,0x155455c3e5071d85]), 
    U384Repr,
    BLS12_381_FIELD
);

pub const BLS12_381_G1_ISOGENY_B: decl_fp!(U384Repr) = repr_into_fp!(
    U384Repr([0xfb996971fe22a1e0,0x9aa93eb35b742d6f,0x8c476013de99c5c4,0x873e27c3a221e571,0xca72b5e45a52d888,0x06824061418a386b]), 
    U384Repr,
    BLS12_381_FIELD
);

pub const BLS12_381_G1_CURVE_ISOGENY: WeierstrassCurve<'static, CurveOverFpParameters<'static, U384Repr, PrimeField<U384Repr>>> = 
    WeierstrassCurve::<'static, CurveOverFpParameters<'static, U384Repr, PrimeField<U384Repr>>> {
        a: BLS12_381_G1_ISOGENY_A,
        b: BLS12_381_G1_ISOGENY_B,
        curve_type: CurveType::Generic,
        subgroup_order_repr: &BLS12_381_SUBGROUP_ORDER,
        params: &BLS12_381_G1_CURVE_PARAMETERS
    };  

pub const BLS12_381_G1_SWU_Z: decl_fp!(U384Repr) = repr_into_fp!(
    U384Repr([0x886c00000023ffdc,0x0f70008d3090001d,0x77672417ed5828c3,0x9dac23e943dc1740,0x50553f1b9c131521,0x078c712fbe0ab6e8]), 
    U384Repr,
    BLS12_381_FIELD
);

pub const BLS12_381_G1_MAPPING_H_EFF: [u64; 1] = [
    0xd201000000010001
];

pub const BLS12_381_G2_SWU_Z: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x87ebfffffff9555c,0x656fffe5da8ffffa,0x0fd0749345d33ad2,0xd951e663066576f4,0xde291a3d41e980d3,0x0815664c7dfe040d]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x43f5fffffffcaaae,0x32b7fff2ed47fffd,0x07e83a49a2e99d69,0xeca8f3318332bb7a,0xef148d1ea0f4c069,0x040ab3263eff0206]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

pub const BLS12_381_G2_ISOGENY_A: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000,0x0000000000000000]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0xe53a000003135242,0x01080c0fdef80285,0xe7889edbe340f6bd,0x0b51375126310601,0x02d6985717c744ab,0x1220b4e979ea5467]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

pub const BLS12_381_G2_ISOGENY_B: decl_fp2!(U384Repr) = repr_into_fp2!(
    repr_into_fp!(
        U384Repr([0x22ea00000cf89db2,0x6ec832df71380aa4,0x6e1b94403db5a66e,0x75bf3c53a79473ba,0x3dd3a569412c0a34,0x125cdb5e74dc4fd1]), 
        U384Repr,
        BLS12_381_FIELD
    ), 
    repr_into_fp!(
        U384Repr([0x22ea00000cf89db2,0x6ec832df71380aa4,0x6e1b94403db5a66e,0x75bf3c53a79473ba,0x3dd3a569412c0a34,0x125cdb5e74dc4fd1]), 
        U384Repr,
        BLS12_381_FIELD
    ),
    U384Repr,
    BLS12_381_EXTENSION_2_FIELD
);

pub const BLS12_381_G2_CURVE_ISOGENY: WeierstrassCurve<'static, CurveOverFp2Parameters<'static, U384Repr, PrimeField<U384Repr>>> = 
    WeierstrassCurve::<'static, CurveOverFp2Parameters<'static, U384Repr, PrimeField<U384Repr>>> {
        a: BLS12_381_G2_ISOGENY_A,
        b: BLS12_381_G2_ISOGENY_B,
        curve_type: CurveType::AIsZero,
        subgroup_order_repr: &BLS12_381_SUBGROUP_ORDER,
        params: &BLS12_381_G2_CURVE_PARAMETERS
    };   

pub const BLS12_381_G2_MAPPING_H_EFF: [u64; 10] = [
    0xe8020005aaa95551,
    0x59894c0adebbf6b4,
    0xe954cbc06689f6a3,
    0x2ec0ec69d7477c1a,
    0x6d82bf015d1212b0,
    0x329c2f178731db95,
    0x9986ff031508ffe1,
    0x88e2a8e9145ad768,
    0x584c6a0ea91b3528,
    0x0bc69f08f2ee75b3
];


#[cfg(feature = "mappings")]
pub mod mapping {
    use crate::weierstrass::*;
    use crate::weierstrass::curve::*;
    use crate::fp::Fp;
    use crate::errors::ApiError;
    use super::*;
    use crate::mapping::simple_swu::*;
    use crate::mapping::isogeny::*;

    fn coerce_static<'a, T: Sized, U: Sized + 'static>(_: &'a T, to_borrow: &'static U) -> &'a U {
        &*to_borrow
    }

    pub fn fp_to_g1(
        el: &Fp<'static, U384Repr, PrimeField<U384Repr>>
    ) -> Result<CurvePoint<'static, CurveOverFpParameters<'static, U384Repr, PrimeField<U384Repr>>>, ApiError>
    {
        let (swu, iso) = &*crate::mapping::parameters::BLS12_G1_MAPPING_PARAMS;
        let (x_prime, y_prime) = simplified_swu_fp(
            el, 
            &swu, 
            coerce_static(el, &BLS12_381_G1_CURVE_ISOGENY)
        );

        let (x, y) = apply_isogeny_map(
            &x_prime,
            &y_prime,
            &iso,
            &BLS12_381_G1_CURVE_PARAMETERS
        );

        let point = CurvePoint::point_from_xy(&BLS12_381_G1_CURVE, x, y);
        if point.is_on_curve() == false {
            return Err(ApiError::InputError("mapped point is not on the curve".to_owned()));
        }

        let mut cofactor_cleared = point.mul(&BLS12_381_G1_MAPPING_H_EFF[..]);
        cofactor_cleared.normalize();

        Ok(cofactor_cleared)
    }

    pub fn fp2_to_g2(
        el: &Fp2<'static, U384Repr, PrimeField<U384Repr>>
    ) -> Result<CurvePoint<'static, CurveOverFp2Parameters<'static, U384Repr, PrimeField<U384Repr>>>, ApiError>
    {
        let (swu, iso) = &*crate::mapping::parameters::BLS12_G2_MAPPING_PARAMS;
        let (x_prime, y_prime) = simplified_swu_fp2(
            el, 
            &swu, 
            coerce_static(el, &BLS12_381_G2_CURVE_ISOGENY)
        );

        let (x, y) = apply_isogeny_map(
            &x_prime,
            &y_prime,
            &iso,
            &BLS12_381_G2_CURVE_PARAMETERS
        );

        let point = CurvePoint::point_from_xy(&BLS12_381_G2_CURVE, x, y);
        if point.is_on_curve() == false {
            return Err(ApiError::InputError("mapped point is not on the curve".to_owned()));
        }

        let mut cofactor_cleared = point.wnaf_mul_with_window_size(&BLS12_381_G2_MAPPING_H_EFF[..], 5);
        cofactor_cleared.normalize();

        Ok(cofactor_cleared)
    }
}


#[cfg(test)]
mod test {
    use super::*;
    use crate::public_interface::decode_g1::serialize_g1_point;

    #[test]
    fn test_engine_bilinearity() {
        use crate::weierstrass::Group;
        use crate::pairings::PairingEngine;

        let p = BLS12_381_G1_GENERATOR.clone();
        let q = BLS12_381_G2_GENERATOR.clone();

        let mut p2 = p.mul(vec![12345678]);
        p2.normalize();

        let mut q2 = q.mul(vec![12345678]);
        q2.normalize();

        let ans1 = BLS12_381_PAIRING_ENGINE.pair(&[p.clone()], &[q2]).unwrap();
        let ans2 = BLS12_381_PAIRING_ENGINE.pair(&[p2], &[q.clone()]).unwrap();
        let ans3 = BLS12_381_PAIRING_ENGINE.pair(&[p], &[q]).unwrap();
        let ans3 = ans3.pow(&vec![12345678]);

        assert!(ans1 == ans2);
        assert!(ans1 == ans3);
    }

    fn output_test_vector(input: &[u8], output: &[u8]) {
        println!("Input: 0x{}", hex::encode(input));
        println!("Output: 0x{}", hex::encode(output));
    }

    fn serialize_scalar(repr: &[u64], len: usize) -> Vec<u8> {
        let a = MaxFieldUint::from(repr);

        let mut res = vec![0u8; a.as_ref().len() * 8];
        a.to_big_endian(&mut res);

        res.reverse();
        res.truncate(len);
        res.reverse();

        res
    }


    #[test]
    fn test_g1_mul_by_zero() {
        let mut input_encoding = serialize_g1_point(48, &BLS12_381_G1_GENERATOR).expect("must serialize a generator");
        let scalar = [0u64];
        let scalar_encoding = serialize_scalar(&scalar, 32);
        input_encoding.extend(scalar_encoding);

        let mut out = BLS12_381_G1_GENERATOR.mul(&scalar);
        out.normalize();

        let output_encoding = serialize_g1_point(48, &out).expect("must serialize a generator");

        output_test_vector(&input_encoding, &output_encoding);
    }
}