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
impl Modular
Sourcepub fn new(value: BigInt, modulus: BigInt) -> Option<Modular>
pub fn new(value: BigInt, modulus: BigInt) -> Option<Modular>
Reduce value into the ring ℤ/modulusℤ. None for a non-positive modulus.
Sourcepub fn from_i64(value: i64, modulus: i64) -> Option<Modular>
pub fn from_i64(value: i64, modulus: i64) -> Option<Modular>
Convenience over machine integers (None if modulus ≤ 0).
pub fn modulus(&self) -> &BigInt
pub fn is_zero(&self) -> bool
Sourcepub fn add(&self, other: &Modular) -> Option<Modular>
pub fn add(&self, other: &Modular) -> Option<Modular>
self + other — None when the moduli differ (a ring mismatch, like a Word width mismatch).
pub fn sub(&self, other: &Modular) -> Option<Modular>
pub fn mul(&self, other: &Modular) -> Option<Modular>
Sourcepub fn pow(&self, exp: u64) -> Modular
pub fn pow(&self, exp: u64) -> Modular
self^exp by fast modular exponentiation (square-and-multiply). 0^0 == 1.