Trait bare_metal_modulo::MNum
source · pub trait MNum: Copy + Eq + PartialEq {
type Num: NumType;
// Required methods
fn a(&self) -> Self::Num;
fn m(&self) -> Self::Num;
fn with(&self, new_a: Self::Num) -> Self;
// Provided methods
fn replace(&mut self, new_a: Self::Num) { ... }
fn egcd(a: Self::Num, b: Self::Num) -> (Self::Num, Self::Num, Self::Num)
where Self::Num: Signed { ... }
fn inverse(&self) -> Option<Self>
where Self::Num: Signed { ... }
}
Required Associated Types§
Required Methods§
fn a(&self) -> Self::Num
fn m(&self) -> Self::Num
fn with(&self, new_a: Self::Num) -> Self
Provided Methods§
fn replace(&mut self, new_a: Self::Num)
sourcefn egcd(a: Self::Num, b: Self::Num) -> (Self::Num, Self::Num, Self::Num)where
Self::Num: Signed,
fn egcd(a: Self::Num, b: Self::Num) -> (Self::Num, Self::Num, Self::Num)where Self::Num: Signed,
Extended Euclidean Algorithm for Greatest Common Divisor for GCD.
This is my translation into Rust of Brent Yorgey’s Haskell implementation.
Given two integers a and b, it returns three integer values:
- Greatest Common Divisor (g) of a and b
- Two additional values x and y, where ax + by = g
sourcefn inverse(&self) -> Option<Self>where
Self::Num: Signed,
fn inverse(&self) -> Option<Self>where Self::Num: Signed,
Returns the modular inverse, if it exists. Returns None if it does not exist.
This is my translation into Rust of Brent Yorgey’s Haskell implementation.
Let m = ModNum::new(a, m), where a and m are relatively prime. Then m * m.inverse().unwrap().a() is congruent to 1 (mod m).
Returns None if a and m are not relatively prime.