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(), UBig::from(6902u32));
Implementations§
source§impl<'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(), UBig::ONE);
source§impl<'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 a 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§
source§impl<'a> AddAssign<&Modulo<'a>> for Modulo<'a>
impl<'a> AddAssign<&Modulo<'a>> for Modulo<'a>
source§fn add_assign(&mut self, rhs: &Modulo<'a>)
fn add_assign(&mut self, rhs: &Modulo<'a>)
Performs the
+=
operation. Read moresource§impl<'a> AddAssign<Modulo<'a>> for Modulo<'a>
impl<'a> AddAssign<Modulo<'a>> for Modulo<'a>
source§fn add_assign(&mut self, rhs: Modulo<'a>)
fn add_assign(&mut self, rhs: Modulo<'a>)
Performs the
+=
operation. Read moresource§impl<'a> DivAssign<&Modulo<'a>> for Modulo<'a>
impl<'a> DivAssign<&Modulo<'a>> for Modulo<'a>
source§fn div_assign(&mut self, rhs: &Modulo<'a>)
fn div_assign(&mut self, rhs: &Modulo<'a>)
Performs the
/=
operation. Read moresource§impl<'a> DivAssign<Modulo<'a>> for Modulo<'a>
impl<'a> DivAssign<Modulo<'a>> for Modulo<'a>
source§fn div_assign(&mut self, rhs: Modulo<'a>)
fn div_assign(&mut self, rhs: Modulo<'a>)
Performs the
/=
operation. Read moresource§impl<'a> MulAssign<&Modulo<'a>> for Modulo<'a>
impl<'a> MulAssign<&Modulo<'a>> for Modulo<'a>
source§fn mul_assign(&mut self, rhs: &Modulo<'a>)
fn mul_assign(&mut self, rhs: &Modulo<'a>)
Performs the
*=
operation. Read moresource§impl<'a> MulAssign<Modulo<'a>> for Modulo<'a>
impl<'a> MulAssign<Modulo<'a>> for Modulo<'a>
source§fn mul_assign(&mut self, rhs: Modulo<'a>)
fn mul_assign(&mut self, rhs: Modulo<'a>)
Performs the
*=
operation. Read moresource§impl<'a> SubAssign<&Modulo<'a>> for Modulo<'a>
impl<'a> SubAssign<&Modulo<'a>> for Modulo<'a>
source§fn sub_assign(&mut self, rhs: &Modulo<'a>)
fn sub_assign(&mut self, rhs: &Modulo<'a>)
Performs the
-=
operation. Read moresource§impl<'a> SubAssign<Modulo<'a>> for Modulo<'a>
impl<'a> SubAssign<Modulo<'a>> for Modulo<'a>
source§fn sub_assign(&mut self, rhs: Modulo<'a>)
fn sub_assign(&mut self, rhs: Modulo<'a>)
Performs the
-=
operation. Read more