From 36d25f2dd5c6754c8b916778794465f6beef754e Mon Sep 17 00:00:00 2001 From: Andrea Date: Mon, 11 Nov 2024 09:28:57 +0100 Subject: [PATCH] Remove unnecessary mod_inverse function and use library provided one --- src/main.rs | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index bd9a648..93d229c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ use colog; use log::info; -use num_bigint::{BigInt, BigUint, ToBigInt}; +use num_bigint::BigUint; use num_primes::Generator; use num_traits::cast::ToPrimitive; -use num_traits::{One, Zero}; +use num_traits::One; use std::io; pub struct RSA { @@ -30,7 +30,7 @@ impl RSA { let e = num_bigint::BigUint::from(65537u32); // Calculate private key - let d = mod_inverse(&e, &phi) + let d = BigUint::modinv(&e, &phi) .expect("Failed to compute modular inverse - e and phi must be coprime"); info!("Generated RSA key pair of {} bits", key_size); @@ -67,32 +67,6 @@ impl RSA { } } -fn mod_inverse(a: &BigUint, m: &BigUint) -> Option { - let mut t = BigInt::zero(); - let mut newt = BigInt::one(); - let mut r = m.to_bigint().unwrap(); - let mut newr = a.to_bigint().unwrap(); - - while !newr.is_zero() { - let quotient = &r / &newr; - let (temp_t, temp_r) = (t.clone(), r.clone()); - t = newt.clone(); - r = newr.clone(); - newt = temp_t - "ient * &newt; - newr = temp_r - "ient * &newr; - } - - if r > BigInt::one() { - return None; - } - - while t < BigInt::zero() { - t = t + &m.to_bigint().unwrap(); - } - - Some(t.to_biguint().unwrap()) -} - fn main() { colog::init(); info!("RSA Encryption/Decryption");