halo2curves/bn256/
fr.rs

1use core::convert::TryInto;
2
3use halo2derive::impl_field;
4use rand::RngCore;
5use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
6
7impl_field!(
8    bn256_scalar,
9    Fr,
10    modulus = "30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001",
11    mul_gen = "7",
12    zeta = "30644e72e131a029048b6e193fd84104cc37a73fec2bc5e9b8ca0b2d36636f23",
13    from_uniform = [64, 48],
14    endian = "little",
15);
16
17crate::extend_field_legendre!(Fr);
18crate::impl_binops_calls!(Fr);
19crate::impl_binops_additive!(Fr, Fr);
20crate::impl_binops_multiplicative!(Fr, Fr);
21crate::field_bits!(Fr);
22crate::serialize_deserialize_primefield!(Fr);
23
24#[cfg(feature = "bn256-table")]
25pub use table::FR_TABLE;
26#[cfg(not(feature = "bn256-table"))]
27crate::impl_from_u64!(Fr);
28#[cfg(feature = "bn256-table")]
29impl From<u64> for Fr {
30    fn from(val: u64) -> Fr {
31        if val < 65536 {
32            FR_TABLE[val as usize]
33        } else {
34            Self([val, 0, 0, 0]) * Fr::R2
35        }
36    }
37}
38crate::impl_from_bool!(Fr);
39
40#[cfg(feature = "bn256-table")]
41#[rustfmt::skip]
42mod table;
43#[cfg(feature = "bn256-table")]
44#[cfg(test)]
45mod table_tests;
46
47#[cfg(test)]
48mod test {
49    use super::Fr;
50    use crate::{
51        arith_test, constants_test, from_uniform_bytes_test, legendre_test, serde_test, test,
52    };
53
54    constants_test!(Fr);
55
56    arith_test!(Fr);
57    legendre_test!(Fr);
58    test!(arith, Fr, sqrt_test, 1000);
59
60    serde_test!(Fr PrimeFieldBits);
61    from_uniform_bytes_test!(Fr, 1000, L 64, L 48);
62}