Skip to content
Snippets Groups Projects
Commit d76c3150 authored by Oulawi's avatar Oulawi
Browse files

Main does something

parent da21f26f
No related branches found
No related tags found
No related merge requests found
mod jacobi;
mod prime_gen;
mod gcd;
use num_bigint::BigUint; use num_bigint::BigUint;
use super::prime_gen;
use super::gcd;
use super::jacobi;
pub fn rand_astar(n: &BigUInt) -> BigUInt {
pub fn rand_astar(n: &BigUint) -> BigUint {
loop { loop {
candidate = gen_large_number(n.size(), false); let candidate = prime_gen::gen_large_number(n.bits() as usize, false);
if candidate >= n { if &candidate >= n {
continue; continue;
} }
if gcd::compute(n, &candidate) != 1 { if gcd::compute(n, &candidate) != BigUint::from(1u32) {
continue; continue;
} }
if jacobi::compute(&candidate, n) == -1 { if jacobi::compute(&candidate, n) == -1 {
...@@ -21,11 +22,11 @@ pub fn rand_astar(n: &BigUInt) -> BigUInt { ...@@ -21,11 +22,11 @@ pub fn rand_astar(n: &BigUInt) -> BigUInt {
pub fn rand_zstar(n: &BigUint) -> BigUint { pub fn rand_zstar(n: &BigUint) -> BigUint {
loop { loop {
candidate = gen_large_number(n.size(), false); let candidate = prime_gen::gen_large_number(n.bits() as usize, false);
if candidate >= n { if &candidate >= n {
continue; continue;
} }
if gcd::compute(n, &candidate) != 1 { if gcd::compute(n, &candidate) != BigUint::from(1u32) {
continue; continue;
} }
return candidate; return candidate;
......
use super::astarzstar;
use num_bigint::{BigUint}; use num_bigint::{BigUint};
use rand::prelude::*
type message = Vec<Bool>; type Message = Vec<bool>;
type ciphertext = Vec<BigUint>; type Ciphertext = Vec<BigUint>;
pub fn encrypt(message: &Message, n: &BigUint, y: &BigUint) -> Ciphertext {
let mut res: Ciphertext = Vec::<BigUint>::with_capacity(message.len());
pub fn encrypt(message: &message, n: &BigUint, y: &BigUint) {
let mut res: ciphertext = Vec<BigUint>::with_capacity(message.len());
let mut rng = rand::thread_rng();
for i in 0..message.len() { for i in 0..message.len() {
let xi = rand_zn(n); let xi = astarzstar::rand_zstar(n);
if message[i] { if message[i] {
res[i] = (y*xi*xi) % n; res.push((y*&xi*&xi) % n);
} else { } else {
res[i] = xi.modpow(&BigUint::from(2u32), n); res.push(xi.modpow(&BigUint::from(2u32), n));
} }
} }
return res;
} }
mod prime_gen; mod prime_gen;
use rand::seq::SliceRandom; mod astarzstar;
mod crypto_system;
mod gcd;
mod jacobi;
mod quadratic_residues;
fn main() { fn main() {
let mut rng = rand::thread_rng();
let p1 = prime_gen::gen_prime(512); let p1 = prime_gen::gen_prime(512);
let mut p2 = prime_gen::gen_prime(512); let mut p2 = prime_gen::gen_prime(512);
while p1 == p2 { while &p1 == &p2 {
p2 = prime_gen::gen_prime(512) p2 = prime_gen::gen_prime(512)
} }
//p1 and p2 are unequal primes (private key) //p1 and p2 are unequal primes (private key)
let n = p1*p2; //n is the public key together with some non-residue y to be computed let n = &p1*&p2; //n is the public key together with some non-residue y to be computed
let mut y = astar(n).choose(rng); //astar (vec<bigUInt>) is the coprimes of n with jacobi symbol 1 let mut y = astarzstar::rand_astar(&n); //astar (vec<bigUInt>) is the coprimes of n with jacobi symbol 1
while is_residue(n, y) { while quadratic_residues::is_quadratic_residue_n(&y, &p1, &p2) {
//We regenerate y until its not a quadratic residue //We regenerate y until its not a quadratic residue
y = astar(n).choose(rng); y = astarzstar::rand_astar(&n);
} }
//Now (n, y) is the public key //Now (n, y) is the public key
let message = "Hello world".to_string(); let message: Vec<bool> = vec![true, true, false, false, true];
let cipher = encrypt(&n, &y, &message); let cipher = crypto_system::encrypt(&message, &n, &y);
for i in 0..5 {
println!("{}", cipher[i]);
}
} }
...@@ -4,5 +4,12 @@ use num_bigint::{BigUint}; ...@@ -4,5 +4,12 @@ use num_bigint::{BigUint};
// quadratic residue check // quadratic residue check
pub fn is_quadratic_residue(a: &BigUint, p: &BigUint) -> bool { pub fn is_quadratic_residue(a: &BigUint, p: &BigUint) -> bool {
a.modpow((p-1)/2, p) == 1 if a.modpow(&((p-1u32)/2u32), p) == BigUint::from(1u32) {
return true;
}
return false;
}
pub fn is_quadratic_residue_n(a: &BigUint, p1: &BigUint, p2: &BigUint) -> bool {
return is_quadratic_residue(a, p1) && is_quadratic_residue(a, p2);
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment