pub struct Residue64<'a> { /* private fields */ }Expand description
A residue with an odd modulus that fits in 2^64.
§Fast modular multiplication
Residue64 provides fast modular multiplication using Montgomery multiplication.
Since this method provides modular multiplication without division,
it is approximately twice as fast.
§Usage
use lib_modulo::Modulus64;
// runtime-specified *odd* modulus
let modulus = 5;
let modulus = Modulus64::new(modulus); // slow
let n = modulus.residue(2) * modulus.residue(3); // fast
assert_eq!(n.get(), 1);Two residues with different modulus can interact, but the result will be meaningless.
It is highly recommended to use a block to ensure that Modulus64, therefore Residue64s, are dropped.
Implementations§
Source§impl<'a> Residue64<'a>
impl<'a> Residue64<'a>
Sourcepub const fn into_raw(self) -> Raw64
pub const fn into_raw(self) -> Raw64
Extract the internal representation of self.
use lib_modulo::{Modulus64, Raw64};
let modulus = Modulus64::new(1001);
// save memory
let residues: Vec<Raw64> = (1..=1000).map(|x| modulus.residue(x).into_raw()).collect();Sourcepub const fn get(&self) -> u64
pub const fn get(&self) -> u64
Returns the residue.
§Example
use lib_modulo::Modulus64;
let modulus = Modulus64::new(5);
let n = modulus.residue(7);
assert_eq!(n.get(), 2);Sourcepub const fn modulus(&self) -> u64
pub const fn modulus(&self) -> u64
Returns the modulus.
§Example
use lib_modulo::Modulus64;
let modulus = Modulus64::new(5);
let n = modulus.residue(7);
assert_eq!(n.modulus(), 5);Sourcepub const fn is_zero(self) -> bool
pub const fn is_zero(self) -> bool
Checks whether self is 0.
§Example
use lib_modulo::Modulus64;
let modulus = Modulus64::new(3);
assert_eq!(modulus.residue(6).get(), 0);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) = gcd(a, 0)is defined to bea.
§Time complexity
O(log self)
§Example
use lib_modulo::Modulus64;
// 998_244_353 is a prime number, so modular inverse of n exists iff n != 0 (mod 998_244_353)
let modulus = Modulus64::new(998_244_353);
for n in 1..500_000 {
let n = modulus.residue(n);
assert!(n.try_inv().is_ok_and(|i| (i * n).get() == 1));
}
// 0 n = 0 != 1 for any integer n
assert!(modulus.residue(0).try_inv().is_err());Trait Implementations§
Source§impl<'a> AddAssign for Residue64<'a>
impl<'a> AddAssign for Residue64<'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 Residue64<'a>
impl<'a> MulAssign for Residue64<'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 Residue64<'a>
impl<'a> SubAssign for Residue64<'a>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-= operation. Read moreimpl<'a> Copy for Residue64<'a>
impl<'a> Eq for Residue64<'a>
impl<'a> StructuralPartialEq for Residue64<'a>
Auto Trait Implementations§
impl<'a> Freeze for Residue64<'a>
impl<'a> RefUnwindSafe for Residue64<'a>
impl<'a> Send for Residue64<'a>
impl<'a> Sync for Residue64<'a>
impl<'a> Unpin for Residue64<'a>
impl<'a> UnsafeUnpin for Residue64<'a>
impl<'a> UnwindSafe for Residue64<'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