snarkvm_curves/bls12_377/
fq2.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use serde::{Deserialize, Serialize};
17
18use snarkvm_fields::{Field, Fp2, Fp2Parameters, field};
19use snarkvm_utilities::biginteger::BigInteger384 as BigInteger;
20
21use crate::bls12_377::Fq;
22
23pub type Fq2 = Fp2<Fq2Parameters>;
24
25#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
26pub struct Fq2Parameters;
27
28impl Fp2Parameters for Fq2Parameters {
29    type Fp = Fq;
30
31    /// Coefficients for the Frobenius automorphism.
32    const FROBENIUS_COEFF_FP2_C1: [Fq; 2] = [
33        // NONRESIDUE**(((q^0) - 1) / 2)
34        field!(
35            Fq,
36            BigInteger([
37                0x2cdffffffffff68,
38                0x51409f837fffffb1,
39                0x9f7db3a98a7d3ff2,
40                0x7b4e97b76e7c6305,
41                0x4cf495bf803c84e8,
42                0x8d6661e2fdf49a,
43            ])
44        ),
45        // NONRESIDUE**(((q^1) - 1) / 2)
46        field!(
47            Fq,
48            BigInteger([
49                0x823ac00000000099,
50                0xc5cabdc0b000004f,
51                0x7f75ae862f8c080d,
52                0x9ed4423b9278b089,
53                0x79467000ec64c452,
54                0x120d3e434c71c50,
55            ])
56        ),
57    ];
58    /// NONRESIDUE = -5
59    const NONRESIDUE: Fq = field!(
60        Fq,
61        BigInteger([
62            0xfc0b8000000002fa,
63            0x97d39cf6e000018b,
64            0x2072420fbfa05044,
65            0xcbbcbd50d97c3802,
66            0xbaf1ec35813f9eb,
67            0x9974a2c0945ad2,
68        ])
69    );
70    /// QUADRATIC_NONRESIDUE = U
71    const QUADRATIC_NONRESIDUE: (Fq, Fq) = (
72        field!(Fq, BigInteger([0, 0, 0, 0, 0, 0])),
73        field!(
74            Fq,
75            BigInteger([
76                202099033278250856u64,
77                5854854902718660529u64,
78                11492539364873682930u64,
79                8885205928937022213u64,
80                5545221690922665192u64,
81                39800542322357402u64,
82            ])
83        ),
84    );
85
86    #[inline(always)]
87    fn mul_fp_by_nonresidue(fe: &Self::Fp) -> Self::Fp {
88        let original = fe;
89        let mut fe = -fe.double();
90        fe.double_in_place();
91        fe - original
92    }
93}