pub struct Modulo<'a>(_);
Expand description
Modular arithmetic.
Examples
let ring = ModuloRing::new(UBig::from(10000u32));
let x = ring.convert(12345);
let y = ring.convert(55443);
assert_eq!((x - y).residue(), 6902);
Implementations
sourceimpl<'a> Modulo<'a>
impl<'a> Modulo<'a>
sourcepub fn inv(self) -> Option<Modulo<'a>>
pub fn inv(self) -> Option<Modulo<'a>>
Multiplicative inverse.
Examples
// A Mersenne prime.
let p = UBig::from(2u8).pow(127) - UBig::ONE;
let ring = ModuloRing::new(p.clone());
// Fermat's little theorem: a^(p-2) = a^-1 (mod p)
let a = ring.convert(123);
let ainv = a.clone().inv().unwrap();
assert_eq!(ainv, a.pow(&(p - UBig::from(2u8))));
assert_eq!((a * ainv).residue(), 1);
sourceimpl<'a> Modulo<'a>
impl<'a> Modulo<'a>
sourcepub fn pow(&self, exp: &UBig) -> Modulo<'a>
pub fn pow(&self, exp: &UBig) -> Modulo<'a>
Exponentiation.
If you want use negative exponent, you can first use inv() to convert the base to its inverse, and then call this method.
Examples
// A Mersenne prime.
let p = UBig::from(2u8).pow(607) - UBig::ONE;
let ring = ModuloRing::new(p.clone());
// Fermat's little theorem: a^(p-1) = 1 (mod p)
let a = ring.convert(123);
assert_eq!(a.pow(&(p - UBig::ONE)), ring.convert(1));
Trait Implementations
sourceimpl<'a> AddAssign<&Modulo<'a>> for Modulo<'a>
impl<'a> AddAssign<&Modulo<'a>> for Modulo<'a>
sourcefn add_assign(&mut self, rhs: &Modulo<'a>)
fn add_assign(&mut self, rhs: &Modulo<'a>)
Performs the +=
operation. Read more
sourceimpl<'a> AddAssign<Modulo<'a>> for Modulo<'a>
impl<'a> AddAssign<Modulo<'a>> for Modulo<'a>
sourcefn add_assign(&mut self, rhs: Modulo<'a>)
fn add_assign(&mut self, rhs: Modulo<'a>)
Performs the +=
operation. Read more
sourceimpl<'a> MulAssign<&Modulo<'a>> for Modulo<'a>
impl<'a> MulAssign<&Modulo<'a>> for Modulo<'a>
sourcefn mul_assign(&mut self, rhs: &Modulo<'a>)
fn mul_assign(&mut self, rhs: &Modulo<'a>)
Performs the *=
operation. Read more
sourceimpl<'a> MulAssign<Modulo<'a>> for Modulo<'a>
impl<'a> MulAssign<Modulo<'a>> for Modulo<'a>
sourcefn mul_assign(&mut self, rhs: Modulo<'a>)
fn mul_assign(&mut self, rhs: Modulo<'a>)
Performs the *=
operation. Read more
sourceimpl PartialEq<Modulo<'_>> for Modulo<'_>
impl PartialEq<Modulo<'_>> for Modulo<'_>
sourceimpl<'a> SubAssign<&Modulo<'a>> for Modulo<'a>
impl<'a> SubAssign<&Modulo<'a>> for Modulo<'a>
sourcefn sub_assign(&mut self, rhs: &Modulo<'a>)
fn sub_assign(&mut self, rhs: &Modulo<'a>)
Performs the -=
operation. Read more
sourceimpl<'a> SubAssign<Modulo<'a>> for Modulo<'a>
impl<'a> SubAssign<Modulo<'a>> for Modulo<'a>
sourcefn sub_assign(&mut self, rhs: Modulo<'a>)
fn sub_assign(&mut self, rhs: Modulo<'a>)
Performs the -=
operation. Read more
impl Eq for Modulo<'_>
Auto Trait Implementations
impl<'a> RefUnwindSafe for Modulo<'a>
impl<'a> Send for Modulo<'a>
impl<'a> Sync for Modulo<'a>
impl<'a> Unpin for Modulo<'a>
impl<'a> UnwindSafe for Modulo<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more