Skip to main content

Modular

Struct Modular 

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

An element of the ring ℤ/nℤ — an integer taken modulo a fixed modulus. This is the arbitrary-modulus generalisation of crate::Word8/…/crate::Word64 (whose modulus is a power of two): arithmetic WRAPS into [0, modulus), the cyclic group where overflow is the point, not a bug. The number-theory / crypto substrate — modular exponentiation and the extended-Euclid inverse live here, so RSA-style and finite-field code is exact.

INVARIANT: 0 ≤ value < modulus and modulus ≥ 1 (built via Modular::new, which reduces into range), so equal residues share one representation (Eq/Hash are structural). NOT ordered (ℤ/nℤ has no canonical total order).

Implementations§

Source§

impl Modular

Source

pub fn new(value: BigInt, modulus: BigInt) -> Option<Modular>

Reduce value into the ring ℤ/modulusℤ. None for a non-positive modulus.

Source

pub fn from_i64(value: i64, modulus: i64) -> Option<Modular>

Convenience over machine integers (None if modulus ≤ 0).

Source

pub fn value(&self) -> &BigInt

The canonical representative in [0, modulus).

Source

pub fn modulus(&self) -> &BigInt

Source

pub fn is_zero(&self) -> bool

Source

pub fn add(&self, other: &Modular) -> Option<Modular>

self + otherNone when the moduli differ (a ring mismatch, like a Word width mismatch).

Source

pub fn sub(&self, other: &Modular) -> Option<Modular>

Source

pub fn mul(&self, other: &Modular) -> Option<Modular>

Source

pub fn negated(&self) -> Modular

The additive inverse −self (i.e. modulus − value, reduced).

Source

pub fn pow(&self, exp: u64) -> Modular

self^exp by fast modular exponentiation (square-and-multiply). 0^0 == 1.

Source

pub fn inverse(&self) -> Option<Modular>

The multiplicative inverse self⁻¹ (extended Euclid). None unless gcd(value, modulus) == 1 (the value must be a unit of the ring).

Source

pub fn div(&self, other: &Modular) -> Option<Modular>

self / other = self · other⁻¹None on a modulus mismatch or a non-invertible divisor.

Trait Implementations§

Source§

impl Clone for Modular

Source§

fn clone(&self) -> Modular

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

Source§

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

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

impl Display for Modular

Source§

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

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

impl Hash for Modular

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 Modular

Source§

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

Source§

impl StructuralPartialEq for Modular

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.