ark-curves-cherry-picked/bls12_377/src/fields/fq2.rs
bls12_377/src/fields/fq2.rsRaw
1use super::*;
2use ark_ff::{field_new, fields::*};
3
4pub type Fq2 = Fp2<Fq2Parameters>;
5
6pub struct Fq2Parameters;
7
8impl Fp2Parameters for Fq2Parameters {
9 type Fp = Fq;
10
11 /// NONRESIDUE = -5
12 #[rustfmt::skip]
13 const NONRESIDUE: Fq = field_new!(Fq, "-5");
14
15 /// QUADRATIC_NONRESIDUE = U
16 #[rustfmt::skip]
17 const QUADRATIC_NONRESIDUE: (Fq, Fq) = (FQ_ZERO, FQ_ONE);
18
19 /// Coefficients for the Frobenius automorphism.
20 #[rustfmt::skip]
21 const FROBENIUS_COEFF_FP2_C1: &'static [Fq] = &[
22 // NONRESIDUE**(((q^0) - 1) / 2)
23 FQ_ONE,
24 // NONRESIDUE**(((q^1) - 1) / 2)
25 field_new!(Fq, "-1"),
26 ];
27
28 // Mul by -5
29 #[inline(always)]
30 fn mul_fp_by_nonresidue(fe: &Self::Fp) -> Self::Fp {
31 let original = fe;
32 let mut fe = -fe.double();
33 fe.double_in_place();
34 fe - original
35 }
36
37 // x + -5 * y, computed as x - 5*y
38 #[inline(always)]
39 fn add_and_mul_fp_by_nonresidue(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
40 // c becomes 5 * y
41 let mut c = y.double();
42 c.double_in_place();
43 c += y;
44 *x - c
45 }
46
47 // x + y + (-5 * y), computed as x - 4*y
48 #[inline(always)]
49 fn add_and_mul_fp_by_nonresidue_plus_one(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
50 // c becomes 4 * y
51 let mut c = y.double();
52 c.double_in_place();
53 *x - c
54 }
55
56 // x - (-5 * y), computed as x + 5*y
57 #[inline(always)]
58 fn sub_and_mul_fp_by_nonresidue(x: &Self::Fp, y: &Self::Fp) -> Self::Fp {
59 // c becomes 5 * y
60 let mut c = y.double();
61 c.double_in_place();
62 c += y;
63 *x + c
64 }
65}
66
67pub const FQ2_ZERO: Fq2 = field_new!(Fq2, FQ_ZERO, FQ_ZERO);
68pub const FQ2_ONE: Fq2 = field_new!(Fq2, FQ_ONE, FQ_ZERO);