Remove unnecessary mod_inverse function and use library provided one
This commit is contained in:
parent
3be97adffc
commit
36d25f2dd5
1 changed files with 3 additions and 29 deletions
32
src/main.rs
32
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<BigUint> {
|
||||
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");
|
||||
|
|
Loading…
Add table
Reference in a new issue