#math #rsa

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

#851 in Cryptography

Download history 789/week @ 2023-08-07 790/week @ 2023-08-14 1064/week @ 2023-08-21 771/week @ 2023-08-28 767/week @ 2023-09-04 756/week @ 2023-09-11 724/week @ 2023-09-18 644/week @ 2023-09-25 672/week @ 2023-10-02 949/week @ 2023-10-09 470/week @ 2023-10-16 1160/week @ 2023-10-23 597/week @ 2023-10-30 424/week @ 2023-11-06 828/week @ 2023-11-13 542/week @ 2023-11-20

2,423 downloads per month
Used in 8 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