replace own pollard_rho with something that works

This commit is contained in:
theBreadCompany 2024-12-07 18:57:08 +01:00
parent ca5cc46cc6
commit fdfc1bac45
6 changed files with 221 additions and 79 deletions

209
Cargo.lock generated
View file

@ -8,6 +8,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "az"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973"
[[package]]
name = "byteorder"
version = "1.5.0"
@ -21,19 +27,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "ec_crypto"
version = "0.1.0"
name = "discrete-logarithm"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40719d0398fbf7202e332c9caccc91d42a5da4e3f4bece86d823a75e0a73b34a"
dependencies = [
"gcd",
"num",
"rand",
"primal",
"rug",
"thiserror",
]
[[package]]
name = "gcd"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d758ba1b47b00caf47f24925c0074ecb20d6dfcffe7f6d53395c0465674841a"
name = "ec_crypto"
version = "0.1.0"
dependencies = [
"discrete-logarithm",
"num",
"rand",
"rug",
]
[[package]]
name = "getrandom"
@ -46,12 +58,34 @@ dependencies = [
"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]]
name = "libc"
version = "0.2.167"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc"
[[package]]
name = "libm"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa"
[[package]]
name = "num"
version = "0.4.3"
@ -134,6 +168,52 @@ dependencies = [
"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]]
name = "proc-macro2"
version = "1.0.92"
@ -182,6 +262,24 @@ dependencies = [
"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]]
name = "syn"
version = "2.0.90"
@ -193,6 +291,26 @@ dependencies = [
"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]]
name = "unicode-ident"
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"
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]]
name = "zerocopy"
version = "0.7.35"

View file

@ -5,5 +5,6 @@ edition = "2021"
[dependencies]
rand = "0.8.5"
gcd = "2.3.0"
num = "0.4.3"
discrete-logarithm = "1.0.0"
rug = "1.26.1"

View file

@ -1,4 +1,5 @@
use core::str;
use std::u32;
use num::{pow::Pow, Integer};
use rand::{distributions::Standard, prelude::Distribution};
@ -42,6 +43,8 @@ pub struct Person {
private_key: Option<u32>,
pub public_key: Option<u32>,
pub shared_key: Option<u32>,
pub gen: Option<u32>,
pub r#mod: Option<u32>,
}
impl Person {
pub fn new(name: &str) -> Self {
@ -50,14 +53,18 @@ impl Person {
private_key: None,
public_key: None,
shared_key: None,
gen: None,
r#mod: None,
}
}
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());
let public = mod_pow(start, private.into(), r#mod);
self.public_key = Some(public);
self.gen = Some(start);
self.r#mod = Some(r#mod);
eprintln!(
"[{}] private: {} -> public: {}",
self.name,
@ -71,7 +78,7 @@ impl Person {
}
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 gen = curve.random().1;

View file

@ -1,7 +1,8 @@
use keygen::Person;
use discrete_logarithm::discrete_log_pollard_rho;
use rug::Integer;
mod keygen;
mod pollard_rho;
mod utils;
fn main() {
@ -9,15 +10,13 @@ fn main() {
let mut bob = Person::new("bob");
Person::diffie_hellman(&mut alice, &mut bob);
alice.send(b"Hello World", &bob);
/*
let mut n = 0;
while n % 2 == 0 {
n = rand::random::<u16>();
}
let n_primediv = pollard_rho::pollard_rho(n.into());
let n_primediv = discrete_log_pollard_rho(&Integer::from(alice.r#mod.unwrap())
, &Integer::from(alice.public_key.unwrap()), &Integer::from(alice.gen.unwrap()), None);
eprintln!(
"Generated random number {}, got prime divisor {}",
n, n_primediv
"[{}] Calculated private key {} from public key {}",
alice.name,
n_primediv.unwrap(),
alice.public_key.unwrap()
);
*/
}

View file

@ -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
}

View file

@ -1,5 +1,3 @@
use std::ops::Div;
use num::Integer;
/**
@ -27,16 +25,4 @@ where
exp /= T::from(2);
}
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
}
}