replace own pollard_rho with something that works
This commit is contained in:
parent
ca5cc46cc6
commit
fdfc1bac45
6 changed files with 221 additions and 79 deletions
209
Cargo.lock
generated
209
Cargo.lock
generated
|
@ -8,6 +8,12 @@ version = "1.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "az"
|
||||||
|
version = "1.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "byteorder"
|
name = "byteorder"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
|
@ -21,19 +27,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ec_crypto"
|
name = "discrete-logarithm"
|
||||||
version = "0.1.0"
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "40719d0398fbf7202e332c9caccc91d42a5da4e3f4bece86d823a75e0a73b34a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"gcd",
|
"primal",
|
||||||
"num",
|
"rug",
|
||||||
"rand",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "gcd"
|
name = "ec_crypto"
|
||||||
version = "2.3.0"
|
version = "0.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
dependencies = [
|
||||||
checksum = "1d758ba1b47b00caf47f24925c0074ecb20d6dfcffe7f6d53395c0465674841a"
|
"discrete-logarithm",
|
||||||
|
"num",
|
||||||
|
"rand",
|
||||||
|
"rug",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
|
@ -46,12 +58,34 @@ dependencies = [
|
||||||
"wasi",
|
"wasi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "gmp-mpfr-sys"
|
||||||
|
version = "1.6.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b0205cd82059bc63b63cf516d714352a30c44f2c74da9961dfda2617ae6b5918"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"windows-sys",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hamming"
|
||||||
|
version = "0.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65043da274378d68241eb9a8f8f8aa54e349136f7b8e12f63e3ef44043cc30e1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.167"
|
version = "0.2.167"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc"
|
checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libm"
|
||||||
|
version = "0.2.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num"
|
name = "num"
|
||||||
version = "0.4.3"
|
version = "0.4.3"
|
||||||
|
@ -134,6 +168,52 @@ dependencies = [
|
||||||
"zerocopy",
|
"zerocopy",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "primal"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b1e5f354948532e6017fc91f9a5ff5ba1be0dabd3a0c9e9c417969cd4c1ad6e8"
|
||||||
|
dependencies = [
|
||||||
|
"primal-check",
|
||||||
|
"primal-estimate",
|
||||||
|
"primal-sieve",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "primal-bit"
|
||||||
|
version = "0.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "252429dbb8aeacc3233df500dc3a6a367bf28eb3a711272884d7540a7b636055"
|
||||||
|
dependencies = [
|
||||||
|
"hamming",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "primal-check"
|
||||||
|
version = "0.3.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dc0d895b311e3af9902528fbb8f928688abbd95872819320517cc24ca6b2bd08"
|
||||||
|
dependencies = [
|
||||||
|
"num-integer",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "primal-estimate"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a432100a0b3a61085e75b5f89e9f42de73c0acb7dea5038b893697918105d822"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "primal-sieve"
|
||||||
|
version = "0.3.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e982796d82203351983d3602a8d6372d1d7894e86960047ba0d4b7426a5edd3"
|
||||||
|
dependencies = [
|
||||||
|
"primal-bit",
|
||||||
|
"primal-estimate",
|
||||||
|
"smallvec",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.92"
|
version = "1.0.92"
|
||||||
|
@ -182,6 +262,24 @@ dependencies = [
|
||||||
"getrandom",
|
"getrandom",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rug"
|
||||||
|
version = "1.26.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "97ae2c1089ec0575193eb9222881310cc1ed8bce3646ef8b81b44b518595b79d"
|
||||||
|
dependencies = [
|
||||||
|
"az",
|
||||||
|
"gmp-mpfr-sys",
|
||||||
|
"libc",
|
||||||
|
"libm",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "smallvec"
|
||||||
|
version = "1.13.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.90"
|
version = "2.0.90"
|
||||||
|
@ -193,6 +291,26 @@ dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "1.0.69"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "1.0.69"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.14"
|
version = "1.0.14"
|
||||||
|
@ -205,6 +323,79 @@ version = "0.11.0+wasi-snapshot-preview1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.52.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-targets"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||||
|
dependencies = [
|
||||||
|
"windows_aarch64_gnullvm",
|
||||||
|
"windows_aarch64_msvc",
|
||||||
|
"windows_i686_gnu",
|
||||||
|
"windows_i686_gnullvm",
|
||||||
|
"windows_i686_msvc",
|
||||||
|
"windows_x86_64_gnu",
|
||||||
|
"windows_x86_64_gnullvm",
|
||||||
|
"windows_x86_64_msvc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnu"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnu"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnullvm"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_msvc"
|
||||||
|
version = "0.52.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zerocopy"
|
name = "zerocopy"
|
||||||
version = "0.7.35"
|
version = "0.7.35"
|
||||||
|
|
|
@ -5,5 +5,6 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
gcd = "2.3.0"
|
|
||||||
num = "0.4.3"
|
num = "0.4.3"
|
||||||
|
discrete-logarithm = "1.0.0"
|
||||||
|
rug = "1.26.1"
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use core::str;
|
use core::str;
|
||||||
|
use std::u32;
|
||||||
|
|
||||||
use num::{pow::Pow, Integer};
|
use num::{pow::Pow, Integer};
|
||||||
use rand::{distributions::Standard, prelude::Distribution};
|
use rand::{distributions::Standard, prelude::Distribution};
|
||||||
|
@ -42,6 +43,8 @@ pub struct Person {
|
||||||
private_key: Option<u32>,
|
private_key: Option<u32>,
|
||||||
pub public_key: Option<u32>,
|
pub public_key: Option<u32>,
|
||||||
pub shared_key: Option<u32>,
|
pub shared_key: Option<u32>,
|
||||||
|
pub gen: Option<u32>,
|
||||||
|
pub r#mod: Option<u32>,
|
||||||
}
|
}
|
||||||
impl Person {
|
impl Person {
|
||||||
pub fn new(name: &str) -> Self {
|
pub fn new(name: &str) -> Self {
|
||||||
|
@ -50,14 +53,18 @@ impl Person {
|
||||||
private_key: None,
|
private_key: None,
|
||||||
public_key: None,
|
public_key: None,
|
||||||
shared_key: None,
|
shared_key: None,
|
||||||
|
gen: None,
|
||||||
|
r#mod: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gen_keys(&mut self, start: u32, r#mod: u32) {
|
pub fn gen_keys(&mut self, start: u32, r#mod: u32) {
|
||||||
let private = rand::random::<u32>();
|
let private = rand::random::<u32>() % r#mod;
|
||||||
self.private_key = Some(private.into());
|
self.private_key = Some(private.into());
|
||||||
let public = mod_pow(start, private.into(), r#mod);
|
let public = mod_pow(start, private.into(), r#mod);
|
||||||
self.public_key = Some(public);
|
self.public_key = Some(public);
|
||||||
|
self.gen = Some(start);
|
||||||
|
self.r#mod = Some(r#mod);
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"[{}] private: {} -> public: {}",
|
"[{}] private: {} -> public: {}",
|
||||||
self.name,
|
self.name,
|
||||||
|
@ -71,7 +78,7 @@ impl Person {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn diffie_hellman(p1: &mut Self, p2: &mut Self) {
|
pub fn diffie_hellman(p1: &mut Self, p2: &mut Self) {
|
||||||
let m: u32 = 17;
|
let m: u32 = 1109;
|
||||||
let curve = EllipticCurve::new(0, 7, m);
|
let curve = EllipticCurve::new(0, 7, m);
|
||||||
let gen = curve.random().1;
|
let gen = curve.random().1;
|
||||||
|
|
||||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -1,7 +1,8 @@
|
||||||
use keygen::Person;
|
use keygen::Person;
|
||||||
|
use discrete_logarithm::discrete_log_pollard_rho;
|
||||||
|
use rug::Integer;
|
||||||
|
|
||||||
mod keygen;
|
mod keygen;
|
||||||
mod pollard_rho;
|
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -9,15 +10,13 @@ fn main() {
|
||||||
let mut bob = Person::new("bob");
|
let mut bob = Person::new("bob");
|
||||||
Person::diffie_hellman(&mut alice, &mut bob);
|
Person::diffie_hellman(&mut alice, &mut bob);
|
||||||
alice.send(b"Hello World", &bob);
|
alice.send(b"Hello World", &bob);
|
||||||
/*
|
|
||||||
let mut n = 0;
|
let n_primediv = discrete_log_pollard_rho(&Integer::from(alice.r#mod.unwrap())
|
||||||
while n % 2 == 0 {
|
, &Integer::from(alice.public_key.unwrap()), &Integer::from(alice.gen.unwrap()), None);
|
||||||
n = rand::random::<u16>();
|
|
||||||
}
|
|
||||||
let n_primediv = pollard_rho::pollard_rho(n.into());
|
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"Generated random number {}, got prime divisor {}",
|
"[{}] Calculated private key {} from public key {}",
|
||||||
n, n_primediv
|
alice.name,
|
||||||
|
n_primediv.unwrap(),
|
||||||
|
alice.public_key.unwrap()
|
||||||
);
|
);
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,42 +0,0 @@
|
||||||
use crate::utils::{is_prime, mod_pow};
|
|
||||||
use gcd::Gcd;
|
|
||||||
/**
|
|
||||||
* Calculate the prime divisor for some number `n`
|
|
||||||
*
|
|
||||||
* Uses cycle finding
|
|
||||||
*/
|
|
||||||
pub fn pollard_rho(n: u32) -> u32 {
|
|
||||||
if n == 1 {
|
|
||||||
return 1;
|
|
||||||
} // 1 only has 1 as prime divisor
|
|
||||||
if n % 2 == 0 {
|
|
||||||
return 2;
|
|
||||||
} // even numbers have at least 2 as prime divisor
|
|
||||||
if is_prime(n) {
|
|
||||||
return n;
|
|
||||||
} // need to detect to be able to decide if we have bad random numbers or the
|
|
||||||
|
|
||||||
let mut x = rand::random::<u32>() % n; // cycle finding: tortoise
|
|
||||||
let mut y = x; // cycle finding: hare
|
|
||||||
|
|
||||||
let c = rand::random::<u32>() % n; // random number to add to the cycle finding moves
|
|
||||||
let mut div = 1; // divisor - 1 applies to all numbers and is our failed and start value
|
|
||||||
|
|
||||||
while div == 1 {
|
|
||||||
// tortoise move
|
|
||||||
x = (mod_pow(x, 2, n) + c + n) % n;
|
|
||||||
// hare move
|
|
||||||
y = (mod_pow(y, 2, n) + c + n) % n;
|
|
||||||
y = (mod_pow(y, 2, n) + c + n) % n;
|
|
||||||
// divisor is the greatest common divisor between |x-y| and n
|
|
||||||
div = u32::try_from((i32::try_from(x).unwrap() - i32::try_from(y).unwrap()).abs())
|
|
||||||
.unwrap()
|
|
||||||
.gcd(n);
|
|
||||||
// bad random numbers, try again
|
|
||||||
if div == n {
|
|
||||||
return pollard_rho(n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
div
|
|
||||||
}
|
|
14
src/utils.rs
14
src/utils.rs
|
@ -1,5 +1,3 @@
|
||||||
use std::ops::Div;
|
|
||||||
|
|
||||||
use num::Integer;
|
use num::Integer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,15 +26,3 @@ where
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* very primitive prime checker
|
|
||||||
*/
|
|
||||||
pub fn is_prime(n: u32) -> bool {
|
|
||||||
for i in (3..=(n as f32).sqrt() as u32).step_by(2) {
|
|
||||||
if n % i == 0 {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue