Skip to main content

Residue32

Struct Residue32 

Source
pub struct Residue32<'a> { /* private fields */ }
Expand description

Residue with odd modulus which is no more than 2_654_435_769.

§Fast modular multiplication

Residue32 provides fast modular multiplication called Plantard multiplication. This method saves one multiplication when either of two values of a multiplication is used multiple times. Therefore, Residue32::pow will be faster than that using Montgomery multiplication.

§Usage

use lib_modulo::Modulus32;

// set modulus
let modulus = Modulus32::new(3);

// performs modular arithmetics
let one = modulus.residue(1);
let two = modulus.residue(2);
let five = modulus.residue(5);
assert_eq!(two * five, one)

Two residues with different modulus can interact, but the result will be meaningless. It is highly recommended to use a block to ensure that Modulus32, therefore Residue32s, are dropped.

Implementations§

Source§

impl<'a> Residue32<'a>

Source

pub const fn is_zero(self) -> bool

Checks whether self is 0.

§Example
use lib_modulo::Modulus32;

let modulus = Modulus32::new(5);
assert!(modulus.residue(10).is_zero())
Source

pub const fn get(self) -> u64

Returns the residue.

§Example
use lib_modulo::Modulus32;

let modulus = Modulus32::new(7);
assert_eq!(modulus.residue(10).get(), 3)
Source

pub const fn modulus(&self) -> u64

Returns the modulus.

§Example
use lib_modulo::Modulus32;

let modulus = Modulus32::new(11);
assert_eq!(modulus.residue(2).modulus(), 11);
Source

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

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

§Time complexity

Θ(log exp)

§Example
use lib_modulo::Modulus32;

let modulus = Modulus32::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 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) is defined to be a.
§Time complexity

O(log self)

§Example
use lib_modulo::Modulus32;

let modulus = Modulus32::new(3 * 5);

let residue = modulus.residue(2);
assert!(residue.try_inv().is_ok_and(|inv| (inv * residue).get() == 1));

let residue = modulus.residue(6);
assert!(residue.try_inv().is_err_and(|gcd| gcd == 3));

Trait Implementations§

Source§

impl<'a> Add for Residue32<'a>

Source§

type Output = Residue32<'a>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<'a> AddAssign for Residue32<'a>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<'a> Clone for Residue32<'a>

Source§

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

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> Debug for Residue32<'a>

Source§

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

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

impl<'a> Hash for Residue32<'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 for Residue32<'a>

Source§

type Output = Residue32<'a>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<'a> MulAssign for Residue32<'a>

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl<'a> Neg for Residue32<'a>

Source§

type Output = Residue32<'a>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<'a> PartialEq for Residue32<'a>

Source§

fn eq(&self, other: &Residue32<'a>) -> 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 Residue32<'a>

Source§

type Output = Residue32<'a>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<'a> SubAssign for Residue32<'a>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<'a> Copy for Residue32<'a>

Source§

impl<'a> Eq for Residue32<'a>

Source§

impl<'a> StructuralPartialEq for Residue32<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Residue32<'a>

§

impl<'a> RefUnwindSafe for Residue32<'a>

§

impl<'a> Send for Residue32<'a>

§

impl<'a> Sync for Residue32<'a>

§

impl<'a> Unpin for Residue32<'a>

§

impl<'a> UnsafeUnpin for Residue32<'a>

§

impl<'a> UnwindSafe for Residue32<'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.