o1_utils/biguint_helpers.rs
1//! This module provides a set of functions to perform bit operations on big integers.
2//! In particular, it gives XOR and NOT for BigUint.
3use num_bigint::BigUint;
4
5/// Helpers for BigUint
6pub trait BigUintHelpers<Rhs = Self> {
7 /// Returns the minimum number of bits required to represent a BigUint
8 /// As opposed to BigUint::bits, this function returns 1 for the input zero
9 fn bitlen(&self) -> usize;
10
11 /// Creates a BigUint from an hexadecimal string in big endian
12 fn from_hex(s: &str) -> Self;
13}
14
15impl BigUintHelpers for BigUint {
16 fn bitlen(&self) -> usize {
17 if self.to_bytes_le() == [0u8] {
18 1
19 } else {
20 self.bits() as usize
21 }
22 }
23 fn from_hex(s: &str) -> Self {
24 BigUint::parse_bytes(s.as_bytes(), 16).unwrap()
25 }
26}