Skip to main content

Modulo

Struct Modulo 

Source
pub struct Modulo<'a, U> { /* private fields */ }
Expand description

Modulo with a runtime-specified odd modulus.

§Usage

use lib_modulo::Context64;

// runtime-specified *odd* modulus
let modulus = 5;

let ctx = Context64::new(modulus); // slow
let n = ctx.modulo(2) * ctx.modulo(3); // fast
assert_eq!(n.get(), 1);

§Caution

Modulo values created from different Contexts can technically interact, but the results will be meaningless. It is recommended to use a block to ensure that each Context is dropped before another one is introduced.

Implementations§

Source§

impl<'a> Modulo<'a, u64>

Source

pub const fn get(&self) -> u64

Returns value.

§Example
use lib_modulo::Context;

let n = 101;
let ctx = Context::<u64>::new(n);

let n = ctx.modulo(99);

assert_eq!(n.get(), 99);
assert_eq!(n.modulus(), 101);
Source

pub const fn modulus(&self) -> u64

Returns modulus.

§Example
use lib_modulo::Context;

let n = 101;
let ctx = Context::<u64>::new(n);

let n = ctx.modulo(99);

assert_eq!(n.get(), 99);
assert_eq!(n.modulus(), 101);
Source

pub const fn is_zero(self) -> bool

Returns true if self is 0.

§Example
use lib_modulo::Context;

for n in (1..100_000).step_by(2) {
    let ctx = Context::<u64>::new(n);
    assert!(ctx.modulo(0).is_zero());
}
Source

pub const fn zero(ctx: &'a Context<u64>) -> Self

Returns 0.

§Example
use lib_modulo::{Context, Modulo};

for n in (1..100_000).step_by(2) {
    let ctx = Context::<u64>::new(n);
    assert_eq!(Modulo::<'_, u64>::zero(&ctx).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::Context;

let n = 12_345;
let ctx = Context::<u64>::new(n);

let mut pow10 = 1;
for i in 0..1_000 {
    assert_eq!(ctx.modulo(10).pow(i).get(), pow10);
    pow10 = pow10 * 10 % n;
}
Source

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) : 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::Context;

// 998_244_353 is a prime number, so modular inverse of n exists iff n != 0 (mod 998_244_353)
let ctx = Context::<u64>::new(998_244_353);

for n in 1..500_000 {
    let n = ctx.modulo(n);
    assert!(n.try_inv().is_ok_and(|i| (i * n).get() == 1));
}
// 0 n = 0 != 1 for any integer n
assert!(ctx.modulo(0).try_inv().is_err());
Source§

impl<'a> Modulo<'a, u32>

Source

pub const fn get(&self) -> u32

Returns value.

§Example
use lib_modulo::Context;

let n = 101;
let ctx = Context::<u32>::new(n);

let n = ctx.modulo(99);

assert_eq!(n.get(), 99);
assert_eq!(n.modulus(), 101);
Source

pub const fn modulus(&self) -> u32

Returns modulus.

§Example
use lib_modulo::Context;

let n = 101;
let ctx = Context::<u32>::new(n);

let n = ctx.modulo(99);

assert_eq!(n.get(), 99);
assert_eq!(n.modulus(), 101);
Source

pub const fn is_zero(self) -> bool

Returns true if self is 0.

§Example
use lib_modulo::Context;

for n in (1..100_000).step_by(2) {
    let ctx = Context::<u32>::new(n);
    assert!(ctx.modulo(0).is_zero());
}
Source

pub const fn zero(ctx: &'a Context<u32>) -> Self

Returns 0.

§Example
use lib_modulo::{Context, Modulo};

for n in (1..100_000).step_by(2) {
    let ctx = Context::<u32>::new(n);
    assert_eq!(Modulo::<'_, u32>::zero(&ctx).get(), 0);
}
Source

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

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

§Time complexity

O(log exp)

§Example
use lib_modulo::Context;

let n = 12_345;
let ctx = Context::<u32>::new(n);

let mut pow10 = 1;
for i in 0..1_000 {
    assert_eq!(ctx.modulo(10).pow(i).get(), pow10);
    pow10 = pow10 * 10 % n;
}
Source

pub const fn try_inv(self) -> Result<Self, u32>

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::Context;

// 998_244_353 is a prime number, so modular inverse of n exists iff n != 0 (mod 998_244_353)
let ctx = Context::<u32>::new(998_244_353);

for n in 1..500_000 {
    let n = ctx.modulo(n);
    assert!(n.try_inv().is_ok_and(|i| (i * n).get() == 1));
}
// 0 n = 0 != 1 for any integer n
assert!(ctx.modulo(0).try_inv().is_err());

Trait Implementations§

Source§

impl<'a> Add for Modulo<'a, u32>

Source§

type Output = Modulo<'a, u32>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<'a> Add for Modulo<'a, u64>

Source§

type Output = Modulo<'a, u64>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<'a, U> AddAssign for Modulo<'a, U>
where Self: Add<Output = Self> + Copy,

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<'a, U: Clone> Clone for Modulo<'a, U>

Source§

fn clone(&self) -> Modulo<'a, U>

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<'a, U: Debug> Debug for Modulo<'a, U>

Source§

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

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

impl<'a, U: Hash> Hash for Modulo<'a, U>

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 for Modulo<'a, u32>

Source§

type Output = Modulo<'a, u32>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<'a> Mul for Modulo<'a, u64>

Source§

type Output = Modulo<'a, u64>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<'a, U> MulAssign for Modulo<'a, U>
where Self: Mul<Output = Self> + Copy,

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl<'a> Neg for Modulo<'a, u32>

Source§

type Output = Modulo<'a, u32>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<'a> Neg for Modulo<'a, u64>

Source§

type Output = Modulo<'a, u64>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<'a, U: PartialEq> PartialEq for Modulo<'a, U>

Source§

fn eq(&self, other: &Modulo<'a, U>) -> 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<'a> Sub for Modulo<'a, u32>

Source§

type Output = Modulo<'a, u32>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<'a> Sub for Modulo<'a, u64>

Source§

type Output = Modulo<'a, u64>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<'a, U> SubAssign for Modulo<'a, U>
where Self: Sub<Output = Self> + Copy,

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<'a, U: Copy> Copy for Modulo<'a, U>

Source§

impl<'a, U: Eq> Eq for Modulo<'a, U>

Source§

impl<'a, U> StructuralPartialEq for Modulo<'a, U>

Auto Trait Implementations§

§

impl<'a, U> Freeze for Modulo<'a, U>
where U: Freeze,

§

impl<'a, U> RefUnwindSafe for Modulo<'a, U>
where U: RefUnwindSafe,

§

impl<'a, U> Send for Modulo<'a, U>
where U: Send + Sync,

§

impl<'a, U> Sync for Modulo<'a, U>
where U: Sync,

§

impl<'a, U> Unpin for Modulo<'a, U>
where U: Unpin,

§

impl<'a, U> UnsafeUnpin for Modulo<'a, U>
where U: UnsafeUnpin,

§

impl<'a, U> UnwindSafe for Modulo<'a, U>

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.