Function modinverse::modinverse

source ·
pub fn modinverse<T: Copy + Integer>(a: T, m: T) -> Option<T>
Expand description

Calculates the modular multiplicative inverse x of an integer a such that ax ≡ 1 (mod m).

Such an integer may not exist. If so, this function will return None. Otherwise, the inverse will be returned wrapped up in a Some.

use modinverse::modinverse;

let does_exist = modinverse(3, 26);
let does_not_exist = modinverse(4, 32);

match does_exist {
  Some(x) => assert_eq!(x, 9),
  None => panic!("modinverse() didn't work as expected"),
}

match does_not_exist {
  Some(x) => panic!("modinverse() found an inverse when it shouldn't have"),
  None => {},
}