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

Get the residue in range 0..n in an n-element ring.

Examples
let ring = ModuloRing::new(UBig::from(100u8));
let x = ring.convert(-1234);
assert_eq!(x.residue(), 66);

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);

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

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Formats the value using the given formatter.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter.

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

Formats the value using the given formatter.

Equality within a ring.

Panics

Panics if the two values are from different rings.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Performs the -= operation. Read more

Formats the value using the given formatter.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.