Skip to main content

lilium_transcript/
utils.rs

1use ark_ff::{BigInteger, Field, PrimeField};
2
3/// casts element from one field into the other, assuming that bias
4/// is acceptable.
5pub fn cycle_cast<F1, F2>(x: F1) -> F2
6where
7    F1: Field,
8    F2: Field,
9{
10    assert_eq!(
11        F1::BasePrimeField::MODULUS_BIT_SIZE,
12        F2::BasePrimeField::MODULUS_BIT_SIZE,
13        "can't cast between fields of sizes differeing by a bit or more"
14    );
15    assert_eq!(
16        F1::extension_degree(),
17        F2::extension_degree(),
18        "can't cast between fields of different extension degree"
19    );
20    let elems: Vec<F2::BasePrimeField> = x
21        .to_base_prime_field_elements()
22        .map(|x| {
23            let bytes = x.into_bigint().to_bytes_le();
24            F2::BasePrimeField::from_le_bytes_mod_order(&bytes)
25        })
26        .collect();
27    F2::from_base_prime_field_elems(&elems).unwrap()
28}