openvm_mod_circuit_builder/
utils.rs

1use num_bigint::BigUint;
2use num_traits::{FromPrimitive, ToPrimitive, Zero};
3
4// little endian.
5pub fn limbs_to_biguint(x: &[u32], limb_size: usize) -> BigUint {
6    let mut result = BigUint::zero();
7    let base = BigUint::from_u32(1 << limb_size).unwrap();
8    for limb in x.iter().rev() {
9        result = result * &base + BigUint::from_u32(*limb).unwrap();
10    }
11    result
12}
13
14// Use this when num_limbs is not a constant.
15// little endian.
16// Warning: This function only returns the last NUM_LIMBS*LIMB_SIZE bits of
17//          the input, while the input can have more than that.
18pub fn biguint_to_limbs_vec(mut x: BigUint, limb_size: usize, num_limbs: usize) -> Vec<u32> {
19    let mut result = vec![0; num_limbs];
20    let base = BigUint::from_u32(1 << limb_size).unwrap();
21    for r in result.iter_mut() {
22        *r = (x.clone() % &base).to_u32().unwrap();
23        x /= &base;
24    }
25    assert!(x.is_zero());
26    result
27}