diff --git a/src/lib.rs b/src/lib.rs index 5a25bab4..02856912 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ pub use int::PrimInt; pub use ops::checked::{ CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub, }; -pub use ops::inv::Inv; +pub use ops::inv::{Inv, ModInverse}; pub use ops::mul_add::{MulAdd, MulAddAssign}; pub use ops::saturating::Saturating; pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingShl, WrappingShr, WrappingSub}; diff --git a/src/ops/inv.rs b/src/ops/inv.rs index 7087d09d..7f4eda6c 100644 --- a/src/ops/inv.rs +++ b/src/ops/inv.rs @@ -45,3 +45,15 @@ impl<'a> Inv for &'a f64 { 1.0 / *self } } + + +/// Generic trait to implement modular inverse +pub trait ModInverse: Sized { + /// Function to calculate the [modular multiplicative + /// inverse](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) of an integer *a* modulo *m*. + /// + /// TODO: references + /// Returns the modular inverse of `self`. + /// If none exists it returns `None`. + fn mod_inverse(self, m: R) -> Option; +}