Skip to content

Fixed gcd_extended and mod_inverse for modular exponential #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/math/modular_exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
///
/// # Returns
///
/// A tuple (gcd, x) such that:
/// A tuple (gcd, x1, x2) such that:
/// gcd - the greatest common divisor of a and m.
/// x - the coefficient such that `a * x` is equivalent to `gcd` modulo `m`.
pub fn gcd_extended(a: i64, m: i64) -> (i64, i64) {
/// x1, x2 - the coefficients such that `a * x1 + m * x2` is equivalent to `gcd` modulo `m`.
pub fn gcd_extended(a: i64, m: i64) -> (i64, i64, i64) {
if a == 0 {
(m, 0)
(m, 0, 1)
} else {
let (gcd, x1) = gcd_extended(m % a, a);
let x = x1 - (m / a) * x1;
(gcd, x)
let (gcd, x1, x2) = gcd_extended(m % a, a);
let x = x2 - (m / a) * x1;
(gcd, x, x1)
}
}

Expand All @@ -36,7 +36,7 @@ pub fn gcd_extended(a: i64, m: i64) -> (i64, i64) {
///
/// Panics if the inverse does not exist (i.e., `b` and `m` are not coprime).
pub fn mod_inverse(b: i64, m: i64) -> i64 {
let (gcd, x) = gcd_extended(b, m);
let (gcd, x, _) = gcd_extended(b, m);
if gcd != 1 {
panic!("Inverse does not exist");
} else {
Expand Down Expand Up @@ -96,6 +96,15 @@ mod tests {
assert_eq!(modular_exponential(123, 45, 67), 62); // 123^45 % 67
}

#[test]
fn test_modular_inverse() {
assert_eq!(mod_inverse(7, 13), 2); // Inverse of 7 mod 13 is 2
assert_eq!(mod_inverse(5, 31), 25); // Inverse of 5 mod 31 is 25
assert_eq!(mod_inverse(10, 11), 10); // Inverse of 10 mod 1 is 10
assert_eq!(mod_inverse(123, 67), 6); // Inverse of 123 mod 67 is 6
assert_eq!(mod_inverse(9, 17), 2); // Inverse of 9 mod 17 is 2
}

#[test]
fn test_modular_exponential_negative() {
assert_eq!(
Expand All @@ -111,8 +120,8 @@ mod tests {
mod_inverse(10, 11).pow(8) % 11
); // Inverse of 10 mod 11 is 10, 10^8 % 11 = 10
assert_eq!(
modular_exponential(123, -45, 67),
mod_inverse(123, 67).pow(45) % 67
modular_exponential(123, -5, 67),
mod_inverse(123, 67).pow(5) % 67
); // Inverse of 123 mod 67 is calculated via the function
}

Expand Down