pub struct Residue32<'a> { /* private fields */ }Expand description
Residue with odd modulus which is no more than 2_654_435_769.
§Fast modular multiplication
Residue32 provides fast modular multiplication called Plantard multiplication.
This method saves one multiplication when either of two values of a multiplication is used multiple times.
Therefore, Residue32::pow will be faster than that using Montgomery multiplication.
§Usage
use lib_modulo::Modulus32;
// set modulus
let modulus = Modulus32::new(3);
// performs modular arithmetics
let one = modulus.residue(1);
let two = modulus.residue(2);
let five = modulus.residue(5);
assert_eq!(two * five, one)Two residues with different modulus can interact, but the result will be meaningless.
It is highly recommended to use a block to ensure that Modulus32, therefore Residue32s, are dropped.
Implementations§
Source§impl<'a> Residue32<'a>
impl<'a> Residue32<'a>
Sourcepub const fn is_zero(self) -> bool
pub const fn is_zero(self) -> bool
Checks whether self is 0.
§Example
use lib_modulo::Modulus32;
let modulus = Modulus32::new(5);
assert!(modulus.residue(10).is_zero())Sourcepub const fn get(self) -> u64
pub const fn get(self) -> u64
Returns the residue.
§Example
use lib_modulo::Modulus32;
let modulus = Modulus32::new(7);
assert_eq!(modulus.residue(10).get(), 3)Sourcepub const fn modulus(&self) -> u64
pub const fn modulus(&self) -> u64
Returns the modulus.
§Example
use lib_modulo::Modulus32;
let modulus = Modulus32::new(11);
assert_eq!(modulus.residue(2).modulus(), 11);Sourcepub const fn try_inv(self) -> Result<Self, u64>
pub const fn try_inv(self) -> Result<Self, u64>
Calculates the modular inverse of self, using extended binary GCD algorithm.
Modular inverse can be defined if and only if self and the modulus is coprime.
Ok(x):xis the modular inverse.Err(x):xis the GCD ofselfand themodulus, wheregcd(0, a)is defined to bea.
§Time complexity
O(log self)
§Example
use lib_modulo::Modulus32;
let modulus = Modulus32::new(3 * 5);
let residue = modulus.residue(2);
assert!(residue.try_inv().is_ok_and(|inv| (inv * residue).get() == 1));
let residue = modulus.residue(6);
assert!(residue.try_inv().is_err_and(|gcd| gcd == 3));Trait Implementations§
Source§impl<'a> AddAssign for Residue32<'a>
impl<'a> AddAssign for Residue32<'a>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+= operation. Read moreSource§impl<'a> MulAssign for Residue32<'a>
impl<'a> MulAssign for Residue32<'a>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the
*= operation. Read moreSource§impl<'a> SubAssign for Residue32<'a>
impl<'a> SubAssign for Residue32<'a>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-= operation. Read moreimpl<'a> Copy for Residue32<'a>
impl<'a> Eq for Residue32<'a>
impl<'a> StructuralPartialEq for Residue32<'a>
Auto Trait Implementations§
impl<'a> Freeze for Residue32<'a>
impl<'a> RefUnwindSafe for Residue32<'a>
impl<'a> Send for Residue32<'a>
impl<'a> Sync for Residue32<'a>
impl<'a> Unpin for Residue32<'a>
impl<'a> UnsafeUnpin for Residue32<'a>
impl<'a> UnwindSafe for Residue32<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more