BigNum

Struct BigNum 

Source
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); // 579

Tuple Fields§

§0: BigInt

Implementations§

Source§

impl BigNum

Source

pub fn is(value: BigInt) -> Self

Constructs a new BigNum directly from a BigInt.

Source

pub fn from_str(s: &str) -> Result<Self, BigIntError>

Creates a BigNum from a decimal string.

§Errors

Returns BigIntError::InvalidFormat if parsing fails.

Source

pub fn to_string(&self) -> String

Converts this BigNum to a decimal string.

Source

pub fn from_bytes_le(bytes: &[u8]) -> Self

Creates a BigNum from a little-endian byte slice.

Source

pub fn to_bytes_le(&self) -> (Sign, Vec<u8>)

Converts this BigNum into a tuple of (Sign, Vec<u8>) in little-endian byte order.

Source

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.

Source

pub fn to_f64(&self) -> Option<f64>

Converts this BigNum to an f64, returning None if it doesn’t fit.

Source

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.

Source

pub fn add(&self, other: &BigNum) -> BigNum

Adds two BigNums by reference and returns the result.

Source

pub fn sub(&self, other: &BigNum) -> BigNum

Subtracts two BigNums by reference and returns the result.

Source

pub fn mul(&self, other: &BigNum) -> BigNum

Multiplies two BigNums by reference and returns the result.

Source

pub fn div(&self, other: &Self) -> Self

Performs integer division (floor) between two BigNums by reference and returns the result.

§Panics

May panic if other is zero.

Source

pub fn rem(&self, other: &Self) -> Self

Returns the remainder of dividing this BigNum by another.

§Panics

May panic if other is zero.

Source

pub fn sqrt(&self) -> BigNum

Returns the integer square root (floor) of this BigNum.

Source

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).

Source

pub fn bits(&self) -> u64

Returns the number of bits needed to represent this BigNum.

Source

pub fn pow(&self, exp: u32) -> Self

Raises this BigNum to the power of exp using exponentiation by squaring.

Source

pub fn mod_pow(&self, exponent: &Self, modulus: &Self) -> BigNum

Computes (self^exponent) mod modulus using modular exponentiation.

Source

pub fn bitand(&self, other: &Self) -> Self

Computes the bitwise AND of self & other.

Source

pub fn bitor(&self, other: &Self) -> Self

Computes the bitwise OR of self | other.

Source

pub fn bitxor(&self, other: &Self) -> Self

Computes the bitwise XOR of self ^ other.

Source

pub fn not(&self) -> Self

Computes the bitwise NOT of self.

Source

pub fn is_zero(&self) -> bool

Checks if this BigNum is zero.

Source

pub fn inner(&self) -> &BigInt

Returns an immutable reference to the underlying BigInt.

Trait Implementations§

Source§

impl Add for BigNum

Source§

type Output = BigNum

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign for BigNum

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl BitAnd for BigNum

Source§

type Output = BigNum

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl BitOr for BigNum

Source§

type Output = BigNum

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl BitXor for BigNum

Source§

type Output = BigNum

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl Clone for BigNum

Source§

fn clone(&self) -> BigNum

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BigNum

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BigNum

Source§

fn default() -> BigNum

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for BigNum

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for BigNum

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Allows printing a BigNum with format! or println!.

Source§

impl Div for BigNum

Source§

type Output = BigNum

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl DivAssign for BigNum

Source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
Source§

impl From<&BigInt> for BigNum

Source§

fn from(value: &BigInt) -> Self

Converts to this type from the input type.
Source§

impl From<&BigNum> for BigNum

Source§

fn from(value: &BigNum) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for BigNum

Source§

fn from(num: i32) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for BigNum

Source§

fn from(num: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for BigNum

Source§

fn from(num: u64) -> Self

Converts to this type from the input type.
Source§

impl FromPrimitive for BigNum

Source§

fn from_i64(n: i64) -> Option<Self>

Converts an 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>

Converts an 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>

Converts an 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>

Converts an 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>

Converts an 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>

Converts an 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>

Converts an i128 to return an optional value of this type. If the value cannot be represented by this type, then None is returned. Read more
Source§

fn from_usize(n: usize) -> Option<Self>

Converts a 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>

Converts an 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>

Converts an 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>

Converts an 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>

Converts an u128 to return an optional value of this type. If the value cannot be represented by this type, then None is returned. Read more
Source§

fn from_f32(n: f32) -> Option<Self>

Converts a f32 to return an optional value of this type. If the value cannot be represented by this type, then None is returned.
Source§

fn from_f64(n: f64) -> Option<Self>

Converts a f64 to return an optional value of this type. If the value cannot be represented by this type, then None is returned. Read more
Source§

impl Integer for BigNum

Source§

fn div_floor(&self, other: &Self) -> Self

Floored integer division. Read more
Source§

fn mod_floor(&self, other: &Self) -> Self

Floored integer modulo, satisfying: Read more
Source§

fn gcd(&self, other: &Self) -> Self

Greatest Common Divisor (GCD). Read more
Source§

fn lcm(&self, other: &Self) -> Self

Lowest Common Multiple (LCM). Read more
Source§

fn divides(&self, other: &Self) -> bool

👎Deprecated: Please use is_multiple_of instead
Deprecated, use is_multiple_of instead.
Source§

fn is_multiple_of(&self, other: &Self) -> bool

Returns true if self is a multiple of other. Read more
Source§

fn is_even(&self) -> bool

Returns true if the number is even. Read more
Source§

fn is_odd(&self) -> bool

Returns true if the number is odd. Read more
Source§

fn div_rem(&self, other: &Self) -> (Self, Self)

Simultaneous truncated integer division and modulus. Returns (quotient, remainder). Read more
Source§

fn div_ceil(&self, other: &Self) -> Self

Ceiled integer division. Read more
Source§

fn gcd_lcm(&self, other: &Self) -> (Self, Self)

Greatest Common Divisor (GCD) and Lowest Common Multiple (LCM) together. Read more
Source§

fn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self>
where Self: Clone,

Greatest common divisor and Bézout coefficients. Read more
Source§

fn extended_gcd_lcm(&self, other: &Self) -> (ExtendedGcd<Self>, Self)
where Self: Clone + Signed,

Greatest common divisor, least common multiple, and Bézout coefficients.
Source§

fn div_mod_floor(&self, other: &Self) -> (Self, Self)

Simultaneous floored integer division and modulus. Returns (quotient, remainder). Read more
Source§

fn next_multiple_of(&self, other: &Self) -> Self
where Self: Clone,

Rounds up to nearest multiple of argument. Read more
Source§

fn prev_multiple_of(&self, other: &Self) -> Self
where Self: Clone,

Rounds down to nearest multiple of argument. Read more
Source§

fn dec(&mut self)
where Self: Clone,

Decrements self by one. Read more
Source§

fn inc(&mut self)
where Self: Clone,

Increments self by one. Read more
Source§

impl Mul<&BigNum> for BigNum

Source§

type Output = BigNum

The resulting type after applying the * operator.
Source§

fn mul(self, other: &BigNum) -> BigNum

Performs the * operation. Read more
Source§

impl Mul<BigNum> for &BigNum

Source§

type Output = BigNum

The resulting type after applying the * operator.
Source§

fn mul(self, other: BigNum) -> BigNum

Performs the * operation. Read more
Source§

impl Mul for &BigNum

Source§

type Output = BigNum

The resulting type after applying the * operator.
Source§

fn mul(self, other: &BigNum) -> BigNum

Performs the * operation. Read more
Source§

impl Mul for BigNum

Source§

type Output = BigNum

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl MulAssign for BigNum

Source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
Source§

impl Neg for BigNum

Source§

type Output = BigNum

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Not for BigNum

Source§

type Output = BigNum

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl Num for BigNum

Source§

type FromStrRadixErr = ParseBigIntError

Source§

fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>

Convert from a string and radix (typically 2..=36). Read more
Source§

impl One for BigNum

Source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
Source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Returns true if self is equal to the multiplicative identity. Read more
Source§

impl Ord for BigNum

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for BigNum

Source§

fn eq(&self, other: &BigNum) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for BigNum

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Rem for BigNum

Source§

type Output = BigNum

The resulting type after applying the % operator.
Source§

fn rem(self, other: Self) -> Self::Output

Performs the % operation. Read more
Source§

impl RemAssign for BigNum

Source§

fn rem_assign(&mut self, other: Self)

Performs the %= operation. Read more
Source§

impl Serialize for BigNum

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Signed for BigNum

Source§

fn abs(&self) -> Self

Computes the absolute value. Read more
Source§

fn abs_sub(&self, other: &Self) -> Self

The positive difference of two numbers. Read more
Source§

fn signum(&self) -> Self

Returns the sign of the number. Read more
Source§

fn is_positive(&self) -> bool

Returns true if the number is positive and false if the number is zero or negative.
Source§

fn is_negative(&self) -> bool

Returns true if the number is negative and false if the number is zero or positive.
Source§

impl Sub for BigNum

Source§

type Output = BigNum

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign for BigNum

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl Zero for BigNum

Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
Source§

impl Eq for BigNum

Source§

impl StructuralPartialEq for BigNum

Auto Trait Implementations§

§

impl Freeze for BigNum

§

impl RefUnwindSafe for BigNum

§

impl Send for BigNum

§

impl Sync for BigNum

§

impl Unpin for BigNum

§

impl UnwindSafe for BigNum

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> NumAssign for T
where T: Num + NumAssignOps,

Source§

impl<T, Rhs> NumAssignOps<Rhs> for T
where T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,

Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,