arnaucube
arnaucube/NovaPublic Self-hosted repository
Nova/src/spartan/math.rs
src/spartan/math.rsRaw
 1pub trait Math {
 2  fn pow2(self) -> usize;
 3  fn get_bits(self, num_bits: usize) -> Vec<bool>;
 4  fn log_2(self) -> usize;
 5}
 6
 7impl Math for usize {
 8  #[inline]
 9  fn pow2(self) -> usize {
10    let base: usize = 2;
11    base.pow(self as u32)
12  }
13
14  /// Returns the num_bits from n in a canonical order
15  fn get_bits(self, num_bits: usize) -> Vec<bool> {
16    (0..num_bits)
17      .map(|shift_amount| ((self & (1 << (num_bits - shift_amount - 1))) > 0))
18      .collect::<Vec<bool>>()
19  }
20
21  fn log_2(self) -> usize {
22    assert_ne!(self, 0);
23
24    if self.is_power_of_two() {
25      (1usize.leading_zeros() - self.leading_zeros()) as usize
26    } else {
27      (0usize.leading_zeros() - self.leading_zeros()) as usize
28    }
29  }
30}