1use num_bigint::BigUint;
2use num_traits::{FromPrimitive, ToPrimitive, Zero};
34// little endian.
5pub fn limbs_to_biguint(x: &[u32], limb_size: usize) -> BigUint {
6let mut result = BigUint::zero();
7let base = BigUint::from_u32(1 << limb_size).unwrap();
8for limb in x.iter().rev() {
9 result = result * &base + BigUint::from_u32(*limb).unwrap();
10 }
11 result
12}
1314// 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> {
19let mut result = vec![0; num_limbs];
20let base = BigUint::from_u32(1 << limb_size).unwrap();
21for r in result.iter_mut() {
22*r = (x.clone() % &base).to_u32().unwrap();
23 x /= &base;
24 }
25assert!(x.is_zero());
26 result
27}