#modular #inverse #integer #finding #math #mod #multiplicative

modinverse

Small library for finding the modular multiplicative inverses

2 releases

Uses old Rust 2015

0.1.1 Dec 8, 2018
0.1.0 Feb 8, 2017

#1590 in Algorithms

Download history 348/week @ 2024-04-14 547/week @ 2024-04-21 632/week @ 2024-04-28 1049/week @ 2024-05-05 684/week @ 2024-05-12 608/week @ 2024-05-19 289/week @ 2024-05-26 454/week @ 2024-06-02 377/week @ 2024-06-09 402/week @ 2024-06-16 349/week @ 2024-06-23 278/week @ 2024-06-30 294/week @ 2024-07-07 509/week @ 2024-07-14 354/week @ 2024-07-21 367/week @ 2024-07-28

1,537 downloads per month
Used in 9 crates

Unlicense

5KB

rust-modinverse

Small library for finding the modular multiplicative inverses. Also has an implementation of the extended Euclidean algorithm built in.

modinverse

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 => {},
}

egcd

Finds the greatest common denominator of two integers a and b, and two integers x and y such that ax + by is the greatest common denominator of a and b (Bézout coefficients).

This function is an implementation of the extended Euclidean algorithm.

use modinverse::egcd;

let a = 26;
let b = 3;
let (g, x, y) = egcd(a, b);

assert_eq!(g, 1);
assert_eq!(x, -1);
assert_eq!(y, 9);
assert_eq!((a * x) + (b * y), g);

Dependencies

~205KB