snarkvm_curves/edwards_bls12/
fr.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 snarkvm_fields::{
17    FftParameters,
18    FieldParameters,
19    Fp256,
20    Fp256Parameters,
21    PoseidonDefaultParameters,
22    PoseidonDefaultParametersEntry,
23};
24use snarkvm_utilities::biginteger::BigInteger256 as BigInteger;
25
26pub type Fr = Fp256<FrParameters>;
27
28#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
29pub struct FrParameters;
30
31impl Fp256Parameters for FrParameters {}
32
33impl FftParameters for FrParameters {
34    type BigInteger = BigInteger;
35
36    // `cargo doc` will fail without this attribute
37    #[doc(hidden)]
38    const POWERS_OF_ROOTS_OF_UNITY: &'static [BigInteger] = unimplemented!();
39    const TWO_ADICITY: u32 = 1;
40    #[rustfmt::skip]
41    const TWO_ADIC_ROOT_OF_UNITY: BigInteger = BigInteger([
42        15170730761708361161u64,
43        13670723686578117817u64,
44        12803492266614043665u64,
45        50861023252832611u64,
46    ]);
47}
48
49impl FieldParameters for FrParameters {
50    #[rustfmt::skip]
51    const CAPACITY: u32 = Self::MODULUS_BITS - 1;
52    /// 70865795004005329077606947863872807680085016823885970091001235374859923341923
53    #[rustfmt::skip]
54    const GENERATOR: BigInteger = BigInteger([
55        11289572479685143826u64,
56        11383637369941080925u64,
57        2288212753973340071u64,
58        82014976407880291u64,
59    ]);
60    #[rustfmt::skip]
61    const INV: u64 = 9659935179256617473u64;
62    /// MODULUS = 2111115437357092606062206234695386632838870926408408195193685246394721360383
63    #[rustfmt::skip]
64    const MODULUS: BigInteger = BigInteger([
65        13356249993388743167u64,
66        5950279507993463550u64,
67        10965441865914903552u64,
68        336320092672043349u64,
69    ]);
70    #[rustfmt::skip]
71    const MODULUS_BITS: u32 = 251;
72    #[rustfmt::skip]
73    const MODULUS_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
74        6678124996694371583u64,
75        2975139753996731775u64,
76        14706092969812227584u64,
77        168160046336021674u64,
78    ]);
79    #[rustfmt::skip]
80    const R: BigInteger = BigInteger([
81        16632263305389933622u64,
82        10726299895124897348u64,
83        16608693673010411502u64,
84        285459069419210737u64,
85    ]);
86    #[rustfmt::skip]
87    const R2: BigInteger = BigInteger([
88        3987543627614508126u64,
89        17742427666091596403u64,
90        14557327917022607905u64,
91        322810149704226881u64,
92    ]);
93    #[rustfmt::skip]
94    const REPR_SHAVE_BITS: u32 = 5;
95    #[rustfmt::skip]
96    const T: BigInteger = BigInteger([
97        6678124996694371583,
98        2975139753996731775,
99        14706092969812227584,
100        168160046336021674
101    ]);
102    #[rustfmt::skip]
103    const T_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
104        12562434535201961599,
105        1487569876998365887,
106        7353046484906113792,
107        84080023168010837
108    ]);
109}
110
111impl PoseidonDefaultParameters for FrParameters {
112    const PARAMS_OPT_FOR_CONSTRAINTS: [PoseidonDefaultParametersEntry; 7] = [
113        PoseidonDefaultParametersEntry::new(2, 17, 8, 31, 0),
114        PoseidonDefaultParametersEntry::new(3, 5, 8, 56, 0),
115        PoseidonDefaultParametersEntry::new(4, 5, 8, 56, 0),
116        PoseidonDefaultParametersEntry::new(5, 5, 8, 57, 0),
117        PoseidonDefaultParametersEntry::new(6, 3, 8, 84, 0),
118        PoseidonDefaultParametersEntry::new(7, 3, 8, 84, 0),
119        PoseidonDefaultParametersEntry::new(8, 3, 8, 84, 0),
120    ];
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126    use snarkvm_fields::{FftField, Field};
127
128    #[test]
129    fn test_two_adic_root_of_unity() {
130        let expected = Fr::multiplicative_generator().pow(FrParameters::T);
131        assert_eq!(expected, Fr::two_adic_root_of_unity());
132    }
133}