Skip to content

Commit

Permalink
feat: workflow and trait pattern primes
Browse files Browse the repository at this point in the history
  • Loading branch information
emirsoyturk committed Apr 20, 2024
1 parent 232e663 commit b36ac2d
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 44 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/rust-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Rust CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: "Set up Rust"
uses: actions/chekout@v2
- name: "Install cargo-audit"
run: cargo install cargo-audit
- name: "Build"
run: cargo build --verbose
- name: "Test"
run: cargo test --verbose
- name: "Clippy"
run: cargo clippy --verbose -- -D warnings
- name: "Audit"
run: cargo audit
3 changes: 2 additions & 1 deletion src/ciphers/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod aes;
pub mod aes;
// pub mod rc5;
5 changes: 1 addition & 4 deletions src/fields/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ where
}

pub fn inv(&self, m: &FieldElement<F>) -> Option<FieldElement<F>> {
match F::inv(&self.value, &m.value) {
Some(value) => Some(FieldElement::<F> { value }),
None => None,
}
F::inv(&self.value, &m.value).map(|value| FieldElement::<F> { value })
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/hash/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod md5;
// pub mod md5;
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// pub mod arithmetization;
pub mod ciphers;
pub mod fields;
pub mod hash;
pub mod pk;
pub mod polynomial;
pub mod prime;
pub mod primes;

fn main() {}
10 changes: 5 additions & 5 deletions src/pk/rsa.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use num_bigint::BigUint;
use num_traits::ToPrimitive;

use crate::prime::prime::Prime;
use crate::primes::prime::Prime;

pub struct RSA {
e: BigUint,
Expand All @@ -11,13 +11,13 @@ pub struct RSA {

impl RSA {
pub fn new() -> Result<Self, &'static str> {
let p = Prime::random_prime();
let q = Prime::random_prime();
let p = BigUint::random_prime();
let q = BigUint::random_prime();

let n: BigUint = &p * &q;
let fi = (&p - BigUint::from(1u64)) * (&q - BigUint::from(1u64));
let e = Prime::random_prime();
let d = match Prime::mod_inv(e.clone(), fi.clone()) {
let e = BigUint::random_prime();
let d = match e.mod_inv(&fi) {
Some(d) => d,
None => return Err("e has no modular inverse"),
};
Expand Down
46 changes: 38 additions & 8 deletions src/polynomial/lagrange.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use cryptography::Point;
use cryptography::Number;
use cryptography::Point;

pub struct LagrangeInterpolation {

}
pub struct LagrangeInterpolation {}

impl LagrangeInterpolation {
pub fn new() -> LagrangeInterpolation {
LagrangeInterpolation{}
LagrangeInterpolation {}
}

pub fn interpolate(&mut self, points: Vec<Point>, x: Number) -> Number {
Expand All @@ -27,21 +25,53 @@ impl LagrangeInterpolation {
}
}

impl Default for LagrangeInterpolation {
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lagrange_float_32() {
let mut lagrange = LagrangeInterpolation::new();
let points = vec![Point{x: Number::from(1.0 as f32), y: Number::from(1.0 as f32)}, Point{x: Number::from(2.0 as f32), y: Number::from(2.0 as f32)}, Point{x: Number::from(3.0 as f32), y: Number::from(3.0 as f32)}];
let points = vec![
Point {
x: Number::from(1.0 as f32),
y: Number::from(1.0 as f32),
},
Point {
x: Number::from(2.0 as f32),
y: Number::from(2.0 as f32),
},
Point {
x: Number::from(3.0 as f32),
y: Number::from(3.0 as f32),
},
];
let result = lagrange.interpolate(points, Number::from(4.0 as f32));
assert_eq!(result, Number::from(4.0 as f64));
}
#[test]
fn test_lagrange_float_64() {
let mut lagrange = LagrangeInterpolation::new();
let points = vec![Point{x: Number::from(1.0 as f64), y: Number::from(1.0 as f64)}, Point{x: Number::from(2.0 as f64), y: Number::from(2.0 as f64)}, Point{x: Number::from(3.0 as f64), y: Number::from(3.0 as f64)}];
let points = vec![
Point {
x: Number::from(1.0 as f64),
y: Number::from(1.0 as f64),
},
Point {
x: Number::from(2.0 as f64),
y: Number::from(2.0 as f64),
},
Point {
x: Number::from(3.0 as f64),
y: Number::from(3.0 as f64),
},
];
let result = lagrange.interpolate(points, Number::from(4.0 as f64));
assert_eq!(result, Number::from(4.0 as f64));
}
}
}
File renamed without changes.
51 changes: 27 additions & 24 deletions src/prime/prime.rs → src/primes/prime.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use num_bigint::{BigInt, BigUint};
use num_traits::{One, Zero};

pub struct Prime {}
pub trait Prime {
fn egcd(a: BigInt, b: BigInt) -> (BigInt, BigInt, BigInt);
fn mod_inv(&self, other: &BigUint) -> Option<BigUint>;
fn is_prime(&self, k: usize) -> bool;
fn random_prime() -> BigUint;
}

impl Prime {
impl Prime for BigUint {
fn egcd(a: BigInt, b: BigInt) -> (BigInt, BigInt, BigInt) {
if a.is_zero() {
(b.clone(), BigInt::zero(), BigInt::one())
Expand All @@ -13,9 +18,9 @@ impl Prime {
}
}

pub fn mod_inv(a: BigUint, m: BigUint) -> Option<BigUint> {
let a = BigInt::from_biguint(num_bigint::Sign::Plus, a);
let m = BigInt::from_biguint(num_bigint::Sign::Plus, m);
fn mod_inv(&self, other: &BigUint) -> Option<BigUint> {
let a = BigInt::from_biguint(num_bigint::Sign::Plus, self.clone());
let m = BigInt::from_biguint(num_bigint::Sign::Plus, other.clone());
let (g, x, _) = Self::egcd(a.clone(), m.clone());
if g != BigInt::one() {
None
Expand All @@ -24,44 +29,45 @@ impl Prime {
}
}

pub fn is_prime(n: &BigUint, k: usize) -> bool {
if n % BigUint::from(2u64) == BigUint::from(0u64) {
return n == &BigUint::from(2u64);
fn is_prime(&self, k: usize) -> bool {
if self % BigUint::from(2u64) == BigUint::from(0u64) {
return self == &BigUint::from(2u64);
}
if n == &BigUint::from(1u64) {
if self == &BigUint::from(1u64) {
return false;
}
let mut d = n - BigUint::from(1u64);
let mut d = self - BigUint::from(1u64);
let mut r = 0;
while d.clone() % BigUint::from(2u64) == BigUint::from(0u64) {
d >>= 1;
r += 1;
}
for _ in 0..k {
let a = BigUint::from(2u64)
+ BigUint::from_bytes_be(&rand::random::<[u8; 32]>()) % (n - BigUint::from(4u64));
let mut x = a.modpow(&d, n);
if x == BigUint::from(1u64) || x == n - BigUint::from(1u64) {
+ BigUint::from_bytes_be(&rand::random::<[u8; 32]>())
% (self - BigUint::from(4u64));
let mut x = a.modpow(&d, self);
if x == BigUint::from(1u64) || x == self - BigUint::from(1u64) {
continue;
}
for _ in 0..r - 1 {
x = x.modpow(&BigUint::from(2u64), n);
x = x.modpow(&BigUint::from(2u64), self);
if x == BigUint::from(1u64) {
return false;
}
if x == n - BigUint::from(1u64) {
if x == self - BigUint::from(1u64) {
break;
}
}
if x != n - BigUint::from(1u64) {
if x != self - BigUint::from(1u64) {
return false;
}
}

true
}

pub fn random_prime() -> BigUint {
fn random_prime() -> BigUint {
loop {
let p = BigUint::from_bytes_be(&rand::random::<[u8; 32]>());
if Self::is_prime(&p, 100) {
Expand All @@ -78,20 +84,17 @@ mod tests {
#[test]
fn test_mod_inv() {
assert_eq!(
Prime::mod_inv(BigUint::from(5u64), BigUint::from(12u64)),
BigUint::from(5u64).mod_inv(&BigUint::from(12u64)),
Some(BigUint::from(5u64))
);
assert_eq!(
Prime::mod_inv(BigUint::from(17u64), BigUint::from(12u64)),
BigUint::from(17u64).mod_inv(&BigUint::from(12u64)),
Some(BigUint::from(5u64))
);
assert_eq!(
Prime::mod_inv(BigUint::from(103u64), BigUint::from(12u64)),
BigUint::from(103u64).mod_inv(&BigUint::from(12u64)),
Some(BigUint::from(7u64))
);
assert_eq!(
Prime::mod_inv(BigUint::from(32u64), BigUint::from(4u64)),
None
);
assert_eq!(BigUint::from(32u64).mod_inv(&BigUint::from(4u64)), None);
}
}

0 comments on commit b36ac2d

Please sign in to comment.