pub struct BigNum(pub BigInt);Expand description
A wrapper struct around BigInt providing additional utilities
such as modular exponentiation, easy conversions, and more.
§Example
use astra_num::BigNum;
let a = BigNum::from(123u32);
let b = BigNum::from(456u32);
let c = a + b;
println!("{}", c); // 579Tuple Fields§
§0: BigIntImplementations§
Source§impl BigNum
impl BigNum
Sourcepub fn from_str(s: &str) -> Result<Self, BigIntError>
pub fn from_str(s: &str) -> Result<Self, BigIntError>
Creates a BigNum from a decimal string.
§Errors
Returns BigIntError::InvalidFormat if parsing fails.
Sourcepub fn from_bytes_le(bytes: &[u8]) -> Self
pub fn from_bytes_le(bytes: &[u8]) -> Self
Creates a BigNum from a little-endian byte slice.
Sourcepub fn to_bytes_le(&self) -> (Sign, Vec<u8>)
pub fn to_bytes_le(&self) -> (Sign, Vec<u8>)
Converts this BigNum into a tuple of (Sign, Vec<u8>) in little-endian byte order.
Sourcepub fn from_radix_be(bytes: &[u8], radix: u32) -> Result<BigNum, BigNumError>
pub fn from_radix_be(bytes: &[u8], radix: u32) -> Result<BigNum, BigNumError>
Creates a BigNum by parsing a byte slice with the given radix in big-endian order.
§Errors
Returns BigNumError::ConversionError if parsing fails for the given radix.
Sourcepub fn to_f64(&self) -> Option<f64>
pub fn to_f64(&self) -> Option<f64>
Converts this BigNum to an f64, returning None if it doesn’t fit.
Sourcepub fn from_f64(value: f64) -> Result<BigNum, BigNumError>
pub fn from_f64(value: f64) -> Result<BigNum, BigNumError>
Creates a BigNum from an f64. May lose precision or return an error for NaN/infinity.
§Errors
Returns BigNumError::ConversionError if the value is not representable as an integer.
Sourcepub fn add(&self, other: &BigNum) -> BigNum
pub fn add(&self, other: &BigNum) -> BigNum
Adds two BigNums by reference and returns the result.
Sourcepub fn sub(&self, other: &BigNum) -> BigNum
pub fn sub(&self, other: &BigNum) -> BigNum
Subtracts two BigNums by reference and returns the result.
Sourcepub fn mul(&self, other: &BigNum) -> BigNum
pub fn mul(&self, other: &BigNum) -> BigNum
Multiplies two BigNums by reference and returns the result.
Sourcepub fn sqrt_precise(&self) -> Result<BigNum, String>
pub fn sqrt_precise(&self) -> Result<BigNum, String>
Computes a more precise integer square root using Newton’s method.
§Errors
Returns an error string if the number is negative (since real sqrt is not defined for negative values).
Sourcepub fn pow(&self, exp: u32) -> Self
pub fn pow(&self, exp: u32) -> Self
Raises this BigNum to the power of exp using exponentiation by squaring.
Trait Implementations§
Source§impl AddAssign for BigNum
impl AddAssign for BigNum
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+= operation. Read moreSource§impl<'de> Deserialize<'de> for BigNum
impl<'de> Deserialize<'de> for BigNum
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl DivAssign for BigNum
impl DivAssign for BigNum
Source§fn div_assign(&mut self, other: Self)
fn div_assign(&mut self, other: Self)
/= operation. Read moreSource§impl FromPrimitive for BigNum
impl FromPrimitive for BigNum
Source§fn from_i64(n: i64) -> Option<Self>
fn from_i64(n: i64) -> Option<Self>
i64 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u64(n: u64) -> Option<Self>
fn from_u64(n: u64) -> Option<Self>
u64 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_isize(n: isize) -> Option<Self>
fn from_isize(n: isize) -> Option<Self>
isize to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i8(n: i8) -> Option<Self>
fn from_i8(n: i8) -> Option<Self>
i8 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i16(n: i16) -> Option<Self>
fn from_i16(n: i16) -> Option<Self>
i16 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i32(n: i32) -> Option<Self>
fn from_i32(n: i32) -> Option<Self>
i32 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i128(n: i128) -> Option<Self>
fn from_i128(n: i128) -> Option<Self>
i128 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned. Read moreSource§fn from_usize(n: usize) -> Option<Self>
fn from_usize(n: usize) -> Option<Self>
usize to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u8(n: u8) -> Option<Self>
fn from_u8(n: u8) -> Option<Self>
u8 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u16(n: u16) -> Option<Self>
fn from_u16(n: u16) -> Option<Self>
u16 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u32(n: u32) -> Option<Self>
fn from_u32(n: u32) -> Option<Self>
u32 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u128(n: u128) -> Option<Self>
fn from_u128(n: u128) -> Option<Self>
u128 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned. Read moreSource§impl Integer for BigNum
impl Integer for BigNum
Source§fn divides(&self, other: &Self) -> bool
fn divides(&self, other: &Self) -> bool
is_multiple_of instead.Source§fn is_multiple_of(&self, other: &Self) -> bool
fn is_multiple_of(&self, other: &Self) -> bool
Source§fn div_rem(&self, other: &Self) -> (Self, Self)
fn div_rem(&self, other: &Self) -> (Self, Self)
(quotient, remainder). Read moreSource§fn gcd_lcm(&self, other: &Self) -> (Self, Self)
fn gcd_lcm(&self, other: &Self) -> (Self, Self)
Source§fn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self>where
Self: Clone,
fn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self>where
Self: Clone,
Source§fn extended_gcd_lcm(&self, other: &Self) -> (ExtendedGcd<Self>, Self)
fn extended_gcd_lcm(&self, other: &Self) -> (ExtendedGcd<Self>, Self)
Source§fn div_mod_floor(&self, other: &Self) -> (Self, Self)
fn div_mod_floor(&self, other: &Self) -> (Self, Self)
(quotient, remainder). Read moreSource§fn next_multiple_of(&self, other: &Self) -> Selfwhere
Self: Clone,
fn next_multiple_of(&self, other: &Self) -> Selfwhere
Self: Clone,
Source§fn prev_multiple_of(&self, other: &Self) -> Selfwhere
Self: Clone,
fn prev_multiple_of(&self, other: &Self) -> Selfwhere
Self: Clone,
Source§impl MulAssign for BigNum
impl MulAssign for BigNum
Source§fn mul_assign(&mut self, other: Self)
fn mul_assign(&mut self, other: Self)
*= operation. Read moreSource§impl Num for BigNum
impl Num for BigNum
type FromStrRadixErr = ParseBigIntError
Source§fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
2..=36). Read moreSource§impl Ord for BigNum
impl Ord for BigNum
Source§impl PartialOrd for BigNum
impl PartialOrd for BigNum
Source§impl RemAssign for BigNum
impl RemAssign for BigNum
Source§fn rem_assign(&mut self, other: Self)
fn rem_assign(&mut self, other: Self)
%= operation. Read moreSource§impl Signed for BigNum
impl Signed for BigNum
Source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Source§impl SubAssign for BigNum
impl SubAssign for BigNum
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
-= operation. Read more