pub struct Reduced<'a>(/* private fields */);
Expand description
Modular arithmetic.
Examples
let ring = ConstDivisor::new(UBig::from(10000u32));
let x = ring.reduce(12345);
let y = ring.reduce(55443);
assert_eq!((x - y).residue(), UBig::from(6902u32));
Implementations§
source§impl Reduced<'_>
impl Reduced<'_>
source§impl<'a> Reduced<'a>
impl<'a> Reduced<'a>
sourcepub fn inv(&self) -> Option<Reduced<'a>>
pub fn inv(&self) -> Option<Reduced<'a>>
Multiplicative inverse.
Examples
// A Mersenne prime.
let p = UBig::from(2u8).pow(127) - UBig::ONE;
let ring = ConstDivisor::new(p.clone());
// Fermat's little theorem: a^(p-2) = a^-1 (mod p)
let a = ring.reduce(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> Reduced<'a>
impl<'a> Reduced<'a>
sourcepub fn pow(&self, exp: &UBig) -> Reduced<'a>
pub fn pow(&self, exp: &UBig) -> Reduced<'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 = ConstDivisor::new(p.clone());
// Fermat's little theorem: a^(p-1) = 1 (mod p)
let a = ring.reduce(123);
assert_eq!(a.pow(&(p - UBig::ONE)), ring.reduce(1));
Trait Implementations§
source§impl<'a> AddAssign<&Reduced<'a>> for Reduced<'a>
impl<'a> AddAssign<&Reduced<'a>> for Reduced<'a>
source§fn add_assign(&mut self, rhs: &Reduced<'a>)
fn add_assign(&mut self, rhs: &Reduced<'a>)
Performs the
+=
operation. Read moresource§impl<'a> AddAssign<Reduced<'a>> for Reduced<'a>
impl<'a> AddAssign<Reduced<'a>> for Reduced<'a>
source§fn add_assign(&mut self, rhs: Reduced<'a>)
fn add_assign(&mut self, rhs: Reduced<'a>)
Performs the
+=
operation. Read moresource§impl<'a> DivAssign<&Reduced<'a>> for Reduced<'a>
impl<'a> DivAssign<&Reduced<'a>> for Reduced<'a>
source§fn div_assign(&mut self, rhs: &Reduced<'a>)
fn div_assign(&mut self, rhs: &Reduced<'a>)
Performs the
/=
operation. Read moresource§impl<'a> DivAssign<Reduced<'a>> for Reduced<'a>
impl<'a> DivAssign<Reduced<'a>> for Reduced<'a>
source§fn div_assign(&mut self, rhs: Reduced<'a>)
fn div_assign(&mut self, rhs: Reduced<'a>)
Performs the
/=
operation. Read moresource§impl<'a> MulAssign<&Reduced<'a>> for Reduced<'a>
impl<'a> MulAssign<&Reduced<'a>> for Reduced<'a>
source§fn mul_assign(&mut self, rhs: &Reduced<'a>)
fn mul_assign(&mut self, rhs: &Reduced<'a>)
Performs the
*=
operation. Read moresource§impl<'a> MulAssign<Reduced<'a>> for Reduced<'a>
impl<'a> MulAssign<Reduced<'a>> for Reduced<'a>
source§fn mul_assign(&mut self, rhs: Reduced<'a>)
fn mul_assign(&mut self, rhs: Reduced<'a>)
Performs the
*=
operation. Read moresource§impl PartialEq<Reduced<'_>> for Reduced<'_>
impl PartialEq<Reduced<'_>> for Reduced<'_>
source§impl<'a> SubAssign<&Reduced<'a>> for Reduced<'a>
impl<'a> SubAssign<&Reduced<'a>> for Reduced<'a>
source§fn sub_assign(&mut self, rhs: &Reduced<'a>)
fn sub_assign(&mut self, rhs: &Reduced<'a>)
Performs the
-=
operation. Read moresource§impl<'a> SubAssign<Reduced<'a>> for Reduced<'a>
impl<'a> SubAssign<Reduced<'a>> for Reduced<'a>
source§fn sub_assign(&mut self, rhs: Reduced<'a>)
fn sub_assign(&mut self, rhs: Reduced<'a>)
Performs the
-=
operation. Read moreimpl Eq for Reduced<'_>
Auto Trait Implementations§
impl<'a> RefUnwindSafe for Reduced<'a>
impl<'a> Send for Reduced<'a>
impl<'a> Sync for Reduced<'a>
impl<'a> Unpin for Reduced<'a>
impl<'a> UnwindSafe for Reduced<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more