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 Residue64<'_>
impl Residue64<'_>
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();
// `Residue64` and `raw64` can interact.
// The caller must ensure that both operands shares the same modulus.
let double_sum = residues.into_iter().fold(modulus.residue(0), |sum, r| r + sum + r);
assert_eq!(double_sum, modulus.residue((1 + 1000) * 1000));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 inv(self) -> Result<Self, u64>
pub const fn 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.inv().is_ok_and(|i| (i * n).get() == 1));
}
// 0 n = 0 != 1 for any integer n
assert!(modulus.residue(0).inv().is_err());Trait Implementations§
Source§impl AddAssign<Raw64> for Residue64<'_>
impl AddAssign<Raw64> for Residue64<'_>
Source§fn add_assign(&mut self, rhs: Raw64)
fn add_assign(&mut self, rhs: Raw64)
Performs the += operation.
§Caution
The caller must ensure that both operands shares the same modulus.
Source§impl AddAssign for Residue64<'_>
impl AddAssign for Residue64<'_>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl MulAssign<Raw64> for Residue64<'_>
impl MulAssign<Raw64> for Residue64<'_>
Source§fn mul_assign(&mut self, rhs: Raw64)
fn mul_assign(&mut self, rhs: Raw64)
Performs the *= operation.
§Caution
The caller must ensure that both operands shares the same modulus.
Source§impl MulAssign for Residue64<'_>
impl MulAssign for Residue64<'_>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl SubAssign<Raw64> for Residue64<'_>
impl SubAssign<Raw64> for Residue64<'_>
Source§fn sub_assign(&mut self, rhs: Raw64)
fn sub_assign(&mut self, rhs: Raw64)
Performs the -= operation.
§Caution
The caller must ensure that both operands shares the same modulus.
Source§impl SubAssign for Residue64<'_>
impl SubAssign for Residue64<'_>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more