Skip to content

Commit

Permalink
Implement RSA lib
Browse files Browse the repository at this point in the history
  • Loading branch information
0xphen committed Nov 7, 2023
1 parent 3806304 commit 5960208
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions miller-rabin-primality-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use std::ops::Div;

use num_bigint::{BigInt, BigUint, ToBigInt};
use num_traits::{Pow, Zero};

pub struct MRPT;

impl MRPT {
pub fn is_prime(p: BigUint) -> bool {
pub fn is_prime(p: &BigUint) -> bool {
let one_biguint: BigUint = BigUint::from(1u32);
let one_bigint: BigInt = BigInt::from(1u32);
let negative_one_bigint: BigInt = BigInt::from(-1i32);
let two_biguint: BigUint = BigUint::from(2u32);

//Step 1: derive m and k
let (k, m) = MRPT::derive_k_and_m(&p);
let (k, m) = MRPT::derive_k_and_m(p);

// step 2: select `a`
// we choose any value of a in the range 1 < a < p - 1.
Expand Down Expand Up @@ -183,14 +185,14 @@ mod tests {
fn is_prime() {
let (_s, p) = SimpleDiffieHellman::generate_safe_prime_and_sophie_prime();

let is_prime = MRPT::is_prime(p);
let is_prime = MRPT::is_prime(&p);
assert_eq!(is_prime, true);
}

#[test]
fn not_prime() {
let p = BigUint::from(88u32);
let is_prime = MRPT::is_prime(p);
let is_prime = MRPT::is_prime(&p);

assert_eq!(is_prime, false);
}
Expand Down
8 changes: 4 additions & 4 deletions utils/src/modular_inverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use num_traits::{One, Zero};

use super::relative_prime;

pub fn mod_inverse(mut a: BigInt, mut m: BigInt) -> Option<BigInt> {
pub fn mod_inverse(mut a: BigInt, mut m: BigInt) -> BigInt {
if !relative_prime::is_co_prime(&a, &m) {
return None;
panic!("{:?} and {:?} are not not co-prime", a.clone(), m.clone());
}

let m0 = m.clone();
Expand All @@ -32,7 +32,7 @@ pub fn mod_inverse(mut a: BigInt, mut m: BigInt) -> Option<BigInt> {
x += m0;
}

Some(x)
x
}

#[cfg(test)]
Expand All @@ -44,6 +44,6 @@ mod tests {
fn find_mod_inverse() {
let a = 3.to_bigint().unwrap();
let m = 11.to_bigint().unwrap();
assert_eq!(mod_inverse(a, m), Some(4.to_bigint().unwrap()));
assert_eq!(mod_inverse(a, m), 4.to_bigint().unwrap());
}
}

0 comments on commit 5960208

Please sign in to comment.