Skip to main content

Residue64

Struct Residue64 

Source
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<'_>

Source

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

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

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

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

pub const fn pow(self, exp: u64) -> Self

Raises self to the power of exp, using exponentiation by squaring.

§Time complexity

O(log exp)

§Example
use lib_modulo::Modulus64;

let modulus = Modulus64::new(1001);
let residue = modulus.residue(2);
for exp in 0..64 {
    assert_eq!(residue.pow(exp).get(), (1 << exp) % 1001)
}
Source

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) : x is the modular inverse.
  • Err(x): x is the GCD of self and the modulus, where gcd(0, a) = gcd(a, 0) is defined to be a.
§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<'a> Add<Raw64> for Residue64<'a>

Source§

fn add(self, rhs: Raw64) -> Self::Output

Performs the + operation.

§Caution

The caller must ensure that both operands shares the same modulus.

Source§

type Output = Residue64<'a>

The resulting type after applying the + operator.
Source§

impl<'a> Add<Residue64<'a>> for Raw64

Source§

fn add(self, rhs: Residue64<'a>) -> Self::Output

Performs the + operation.

§Caution

The caller must ensure that both operands shares the same modulus.

Source§

type Output = Residue64<'a>

The resulting type after applying the + operator.
Source§

impl Add for Residue64<'_>

Source§

type Output = Residue64<'_>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl AddAssign<Raw64> for Residue64<'_>

Source§

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<'_>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<'a> Clone for Residue64<'a>

Source§

fn clone(&self) -> Residue64<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Residue64<'a>

Source§

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

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

impl<'a> From<Residue64<'a>> for Raw64

Source§

fn from(residue: Residue64<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> Hash for Residue64<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> Mul<Raw64> for Residue64<'a>

Source§

fn mul(self, rhs: Raw64) -> Self::Output

Performs the * operation.

§Caution

The caller must ensure that both operands shares the same modulus.

Source§

type Output = Residue64<'a>

The resulting type after applying the * operator.
Source§

impl<'a> Mul<Residue64<'a>> for Raw64

Source§

fn mul(self, rhs: Residue64<'a>) -> Self::Output

Performs the * operation.

§Caution

The caller must ensure that both operands shares the same modulus.

Source§

type Output = Residue64<'a>

The resulting type after applying the * operator.
Source§

impl Mul for Residue64<'_>

Source§

type Output = Residue64<'_>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
Source§

impl MulAssign<Raw64> for Residue64<'_>

Source§

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<'_>

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl Neg for Residue64<'_>

Source§

type Output = Residue64<'_>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<'a> PartialEq for Residue64<'a>

Source§

fn eq(&self, other: &Residue64<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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<'a> Sub<Raw64> for Residue64<'a>

Source§

fn sub(self, rhs: Raw64) -> Self::Output

Performs the - operation.

§Caution

The caller must ensure that both operands shares the same modulus.

Source§

type Output = Residue64<'a>

The resulting type after applying the - operator.
Source§

impl<'a> Sub<Residue64<'a>> for Raw64

Source§

fn sub(self, rhs: Residue64<'a>) -> Self::Output

Performs the - operation.

§Caution

The caller must ensure that both operands shares the same modulus.

Source§

type Output = Residue64<'a>

The resulting type after applying the - operator.
Source§

impl Sub for Residue64<'_>

Source§

type Output = Residue64<'_>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl SubAssign<Raw64> for Residue64<'_>

Source§

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<'_>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<'a> Copy for Residue64<'a>

Source§

impl<'a> Eq for Residue64<'a>

Source§

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