acir_field/
lib.rs

1#![forbid(unsafe_code)]
2#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))]
3
4mod field_element;
5mod generic_ark;
6
7pub use generic_ark::AcirField;
8
9/// Temporarily exported generic field to aid migration to `AcirField`
10pub use field_element::FieldElement as GenericFieldElement;
11use num_bigint::BigInt;
12
13pub fn truncate_to<F: AcirField>(input: &F, bits: u32) -> F {
14    let num_bits = input.num_bits();
15    if bits >= num_bits {
16        *input
17    } else if num_bits < 128 {
18        let mask = 2u128.pow(bits) - 1;
19        F::from(input.to_u128() & mask)
20    } else {
21        let input_int = BigInt::from_bytes_be(num_bigint::Sign::Plus, &input.to_be_bytes());
22        let modulus = BigInt::from(2u32).pow(bits);
23        let result = input_int % modulus;
24        F::from_be_bytes_reduce(&result.to_bytes_be().1)
25    }
26}
27
28cfg_if::cfg_if! {
29    if #[cfg(feature = "bls12_381")] {
30        pub type FieldElement = field_element::FieldElement<ark_bls12_381::Fr>;
31    } else {
32        pub type FieldElement = field_element::FieldElement<ark_bn254::Fr>;
33    }
34}
35
36// This is needed because features are additive through the dependency graph; if a dependency turns on the bn254, then it
37// will be turned on in all crates that depend on it
38#[macro_export]
39macro_rules! assert_unique_feature {
40    () => {};
41    ($first:tt $(,$rest:tt)*) => {
42        $(
43            #[cfg(all(feature = $first, feature = $rest))]
44            compile_error!(concat!("features \"", $first, "\" and \"", $rest, "\" cannot be used together"));
45        )*
46        assert_unique_feature!($($rest),*);
47    }
48}
49// https://internals.rust-lang.org/t/mutually-exclusive-feature-flags/8601/7
50// If another field/feature is added, we add it here too
51assert_unique_feature!("bn254", "bls12_381");