ark-curves-cherry-picked/bls12_377/src/fields/fq2.rs
bls12_377/src/fields/fq2.rsRaw
1use ark_ff::{fields::*, MontFp};
2
3use crate::*;
4
5pub type Fq2 = Fp2<Fq2Config>;
6
7pub struct Fq2Config;
8
9impl Fp2Config for Fq2Config {
10 type Fp = Fq;
11
12 /// NONRESIDUE = -5
13 const NONRESIDUE: Fq = MontFp!("-5");
14
15 /// Coefficients for the Frobenius automorphism.
16 const FROBENIUS_COEFF_FP2_C1: &'static [Fq] = &[
17 // NONRESIDUE**(((q^0) - 1) / 2)
18 Fq::ONE,
19 // NONRESIDUE**(((q^1) - 1) / 2)
20 MontFp!("-1"),
21 ];
22
23 #[inline(always)]
24 fn mul_fp_by_nonresidue_in_place(fe: &mut Self::Fp) -> &mut Self::Fp {
25 let original = *fe;
26 fe.neg_in_place();
27 fe.double_in_place();
28 fe.double_in_place();
29 *fe -= original;
30 fe
31 }
32
33 #[inline(always)]
34 fn sub_and_mul_fp_by_nonresidue(y: &mut Self::Fp, x: &Self::Fp) {
35 let mut original = *y;
36 original += x;
37 y.double_in_place().double_in_place();
38 *y += original;
39 }
40
41 #[inline(always)]
42 fn add_and_mul_fp_by_nonresidue_plus_one(y: &mut Self::Fp, x: &Self::Fp) {
43 y.double_in_place().double_in_place().neg_in_place();
44 *y += x;
45 }
46
47 fn add_and_mul_fp_by_nonresidue_in_place(y: &mut Self::Fp, x: &Self::Fp) {
48 let mut original = *y;
49 original.double_in_place().double_in_place();
50 original += &*y;
51 *y = *x;
52 *y -= original;
53 }
54}