arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
ark-curves-cherry-picked/bls12_377/src/fields/fr.rs
bls12_377/src/fields/fr.rsRaw
  1//! Bls12-377 scalar field.
  2///
  3/// Roots of unity computed from modulus and R using this sage code:
  4///
  5/// ```ignore
  6/// q = 8444461749428370424248824938781546531375899335154063827935233455917409239041
  7/// R = 6014086494747379908336260804527802945383293308637734276299549080986809532403 # Montgomery R
  8/// s = 47
  9/// o = q - 1
 10/// F = GF(q)
 11/// g = F.multiplicative_generator()
 12/// g = F.multiplicative_generator()
 13/// assert g.multiplicative_order() == o
 14/// g2 = g ** (o/2**s)
 15/// assert g2.multiplicative_order() == 2**s
 16/// def into_chunks(val, width, n):
 17///     return [int(int(val) // (2 ** (width * i)) % 2 ** width) for i in range(n)]
 18/// print("Gen: ", g * R % q)
 19/// print("Gen: ", into_chunks(g * R % q, 64, 4))
 20/// print("2-adic gen: ", into_chunks(g2 * R % q, 64, 4))
 21/// ```
 22use ark_ff::{biginteger::BigInteger256 as BigInteger, fields::*};
 23
 24pub type Fr = Fp256<FrParameters>;
 25
 26pub struct FrParameters;
 27
 28impl Fp256Parameters for FrParameters {}
 29impl FftParameters for FrParameters {
 30    type BigInt = BigInteger;
 31
 32    const TWO_ADICITY: u32 = 47;
 33
 34    #[rustfmt::skip]
 35    const TWO_ADIC_ROOT_OF_UNITY: BigInteger = BigInteger([
 36        12646347781564978760u64,
 37        6783048705277173164u64,
 38        268534165941069093u64,
 39        1121515446318641358u64,
 40    ]);
 41}
 42impl FpParameters for FrParameters {
 43    /// MODULUS = 8444461749428370424248824938781546531375899335154063827935233455917409239041
 44    #[rustfmt::skip]
 45    const MODULUS: BigInteger = BigInteger([
 46        725501752471715841u64,
 47        6461107452199829505u64,
 48        6968279316240510977u64,
 49        1345280370688173398u64,
 50    ]);
 51
 52    const MODULUS_BITS: u32 = 253;
 53
 54    const CAPACITY: u32 = Self::MODULUS_BITS - 1;
 55
 56    const REPR_SHAVE_BITS: u32 = 3;
 57
 58    /// R = 6014086494747379908336260804527802945383293308637734276299549080986809532403
 59    #[rustfmt::skip]
 60    const R: BigInteger = BigInteger([
 61        9015221291577245683u64,
 62        8239323489949974514u64,
 63        1646089257421115374u64,
 64        958099254763297437u64,
 65    ]);
 66
 67    #[rustfmt::skip]
 68    const R2: BigInteger = BigInteger([
 69        2726216793283724667u64,
 70        14712177743343147295u64,
 71        12091039717619697043u64,
 72        81024008013859129u64,
 73    ]);
 74
 75    const INV: u64 = 725501752471715839u64;
 76
 77    /// GENERATOR = 22
 78    /// Encoded in Montgomery form, so the value is
 79    /// (22 * R) % q =
 80    /// 5642976643016801619665363617888466827793962762719196659561577942948671127251
 81    #[rustfmt::skip]
 82    const GENERATOR: BigInteger = BigInteger([
 83        2984901390528151251u64,
 84        10561528701063790279u64,
 85        5476750214495080041u64,
 86        898978044469942640u64,
 87    ]);
 88
 89    /// (r - 1)/2 =
 90    /// 4222230874714185212124412469390773265687949667577031913967616727958704619520
 91    #[rustfmt::skip]
 92    const MODULUS_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
 93        0x8508c00000000000,
 94        0xacd53b7f68000000,
 95        0x305a268f2e1bd800,
 96        0x955b2af4d1652ab,
 97    ]);
 98
 99    // T and T_MINUS_ONE_DIV_TWO, where r - 1 = 2^s * t
100    // For T coprime to 2
101
102    /// t = (r - 1) / 2^s =
103    /// 60001509534603559531609739528203892656505753216962260608619555
104    #[rustfmt::skip]
105    const T: BigInteger = BigInteger([
106        0xedfda00000021423,
107        0x9a3cb86f6002b354,
108        0xcabd34594aacc168,
109        0x2556,
110    ]);
111
112    /// (t - 1) / 2 =
113    /// 30000754767301779765804869764101946328252876608481130304309777
114    #[rustfmt::skip]
115    const T_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
116        0x76fed00000010a11,
117        0x4d1e5c37b00159aa,
118        0x655e9a2ca55660b4,
119        0x12ab,
120    ]);
121}