Skip to main content

Modulus32Any

Struct Modulus32Any 

Source
pub struct Modulus32Any { /* private fields */ }
Expand description

A modulus in [2, 2^32), including even values.

§Fast modular multiplication

Provides fast modular multiplication using Barrett multiplication. This works for any modulus (including even ones) and places no restrictions on the operands, but is generally slower than Residue32.

Unlike Montgomery or Plantard methods, this operates directly on standard integer representations (i.e., no transformation is required).

§Usage

use lib_modulo::Modulus32Any;

let modulus = Modulus32Any::new(14).unwrap();
assert_eq!(modulus.mul(3, 5), 1)

Implementations§

Source§

impl Modulus32Any

Source

pub const fn new(n: u32) -> Result<Self, InvalidModulus>

Creates new instance with the given modulus.

§Error

Returns error if the given modulus is 0 or 1.

§Example
use lib_modulo::{Modulus32Any, InvalidModulus};

// even number is available
let modulus = Modulus32Any::new(2).unwrap();
// odd number is also available
let modulus = Modulus32Any::new(3).unwrap();
// division by 0 is undefined
assert_eq!(Modulus32Any::new(0), Err(InvalidModulus::Zero));
// division by 1 is meaningless and NOT available for performance
assert_eq!(Modulus32Any::new(1), Err(InvalidModulus::One));
Source

pub const fn modulus(&self) -> u32

Returns the modulus.

Source

pub const fn residue32(&self, x: u32) -> u32

Calculates residue of x modulo self.

Source

pub const fn residue64(&self, x: u64) -> u64

Calculates residue of x modulo self.

Source

pub const fn can_divide(&self, x: u32) -> bool

Checks if x is divisible by self.

Source

pub const fn mul(&self, a: u32, b: u32) -> u32

Performs a * b modulo self.

§Example
use lib_modulo::Modulus32Any;

// even number is available
let modulus = Modulus32Any::new(1 << 8).unwrap();
assert_eq!(modulus.mul(u32::MAX, u32::MAX), 1);
Source

pub const fn carrying_mul(&self, a: u32, b: u32, c: u32) -> u32

Performs a * b + c modulo self.

§Example
use lib_modulo::Modulus32Any;

let modulus = Modulus32Any::new(2357).unwrap();
assert_eq!(
    modulus.carrying_mul(123, 456, 789),
    (123 * 456 + 789) % 2357
);
Source

pub const fn carrying_mul_add(&self, a: u32, b: u32, c: u32, d: u32) -> u32

Performs a * b + c + d modulo self.

§Example
use lib_modulo::Modulus32Any;

// even number is available
let modulus = Modulus32Any::new(123_456).unwrap();
assert_eq!(
    modulus.carrying_mul_add(u32::MAX, u32::MAX, u32::MAX, u32::MAX),
    (u64::MAX % 123_456) as u32
);
Source

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

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

§Time Complexity

O(log x)

§Example
use lib_modulo::Modulus32Any;

let modulus = Modulus32Any::new(123_456).unwrap();

assert_eq!(modulus.pow(123_456 * 100 + 1, 1000), 1)
Source

pub const fn inv(&self, x: u32) -> Result<u32, u32>

Calculates the modular inverse of x, using extended gcd algorithm.

Modular inverse can be defined if and only if x and the modulus is coprime.

  • Ok(_) : the modular inverse.
  • Err(_): the GCD of x and the modulus, where gcd(0, a) is defined to be a.
§Time complexity

O(log x)

§Example
use lib_modulo::Modulus32Any;

let modulus = Modulus32Any::new(3 * 5).unwrap();
for (a, inv_a) in [(1, 1), (2, 8), (4, 4), (7, 13), (11, 11), (14, 14)] {
    assert_eq!(modulus.mul(a, inv_a), 1);
    assert_eq!(modulus.inv(a), Ok(inv_a));
}
for a in [3, 6, 9, 12] {
    assert_eq!(modulus.inv(a), Err(3));
}
for a in [5, 10] {
    assert_eq!(modulus.inv(a), Err(5));
}
// gcd(0, a) is defined t be `a`
assert_eq!(modulus.inv(0), Err(15));
assert_eq!(modulus.inv(15 * 99), Err(15));

Trait Implementations§

Source§

impl Clone for Modulus32Any

Source§

fn clone(&self) -> Modulus32Any

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 Debug for Modulus32Any

Source§

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

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

impl Hash for Modulus32Any

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 PartialEq for Modulus32Any

Source§

fn eq(&self, other: &Modulus32Any) -> 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 Copy for Modulus32Any

Source§

impl Eq for Modulus32Any

Source§

impl StructuralPartialEq for Modulus32Any

Auto Trait Implementations§

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.