add WIP p generator

This commit is contained in:
none 2023-06-05 13:21:35 +02:00
parent 5e0dcf1b13
commit 54a236006e
6 changed files with 288 additions and 12 deletions

View File

@ -58,7 +58,7 @@ fn pow_mod(num: BigUint, pow: BigUint, modulo: BigUint) -> BigUint {
fn main() { fn main() {
let mut randomgen = StdRng::from_entropy(); let mut randomgen = StdRng::from_entropy();
let mut private_key = [0u8; 4096]; let mut private_key = [0u8; 256];
randomgen.fill_bytes(&mut private_key); randomgen.fill_bytes(&mut private_key);
let private_key = BigUint::from_bytes_be(&private_key); let private_key = BigUint::from_bytes_be(&private_key);

86
rust/server/Cargo.lock generated
View File

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "1.0.1" version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04" checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
@ -95,6 +95,17 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "getrandom"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]] [[package]]
name = "itransfer_server" name = "itransfer_server"
version = "0.1.0" version = "0.1.0"
@ -102,7 +113,8 @@ dependencies = [
"bincode", "bincode",
"byteorder", "byteorder",
"crc32fast", "crc32fast",
"num-bigint", "lazy_static",
"num",
"rand", "rand",
"regex", "regex",
"serde", "serde",
@ -110,10 +122,16 @@ dependencies = [
] ]
[[package]] [[package]]
name = "libc" name = "lazy_static"
version = "0.2.144" 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 = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.145"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc86cde3ff845662b8f4ef6cb50ea0e20c524eb3d29ae048287e06a1b3fa6a81"
[[package]] [[package]]
name = "memchr" name = "memchr"
@ -121,6 +139,20 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "num"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43db66d1170d347f9a065114077f7dccb00c1b9478c89384490a3425279a4606"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]
[[package]] [[package]]
name = "num-bigint" name = "num-bigint"
version = "0.4.3" version = "0.4.3"
@ -132,6 +164,15 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "num-complex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02e0d21255c828d6f128a1e41534206671e8c3ea0c62f32291e808dc82cff17d"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.45" version = "0.1.45"
@ -142,6 +183,29 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "num-iter"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.15" version = "0.2.15"
@ -181,6 +245,7 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [ dependencies = [
"libc",
"rand_chacha", "rand_chacha",
"rand_core", "rand_core",
] ]
@ -200,6 +265,9 @@ name = "rand_core"
version = "0.6.4" version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "regex" name = "regex"
@ -277,3 +345,9 @@ name = "version_check"
version = "0.9.4" version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

View File

@ -12,8 +12,9 @@ byteorder = { default-features = false, version = "1.4.3" }
sha2 = { default-features = false, version = "0.10.6" } sha2 = { default-features = false, version = "0.10.6" }
regex = { default-features = true, version = "1.8.3" } regex = { default-features = true, version = "1.8.3" }
crc32fast = { default-features = false, version = "1.3.2" } crc32fast = { default-features = false, version = "1.3.2" }
rand = { default-features = false, features = ["std_rng"], version = "0.8.5" } rand = { default-features = true, features = ["std_rng"], version = "0.8.5" }
num-bigint = { default-features = true, version = "0.4.3" } num = { default-features = true, version = "0.4.0" }
lazy_static = { default-features = false, version = "1.4.0" }
[profile.release] [profile.release]
lto = true # Enable link-time optimization lto = true # Enable link-time optimization

View File

@ -5,11 +5,15 @@ use sha2::{Digest,Sha512};
use regex::Regex; use regex::Regex;
use crc32fast; use crc32fast;
use rand::{rngs::StdRng, RngCore, SeedableRng}; use rand::{rngs::StdRng, RngCore, SeedableRng};
use num_bigint::BigUint; use num::{BigUint, one};
mod big_array; mod big_array;
mod primality_test;
mod prime_utils;
use big_array::BigArray; use big_array::BigArray;
use crate::prime_utils::prime_utils::is_prime_default;
const MAX_FRAME_PAYLOAD:u16=508; const MAX_FRAME_PAYLOAD:u16=508;
const MAX_FRAME_PAYLOAD_U:usize=MAX_FRAME_PAYLOAD as usize; const MAX_FRAME_PAYLOAD_U:usize=MAX_FRAME_PAYLOAD as usize;
const HEADER_SIZE:u16 = 12; const HEADER_SIZE:u16 = 12;
@ -40,16 +44,50 @@ struct StrPacket {
fn pow_mod(num: BigUint, pow: BigUint, modulo: BigUint) -> BigUint { fn pow_mod(num: BigUint, pow: BigUint, modulo: BigUint) -> BigUint {
let mut result = BigUint::from(1u8); let mut result = BigUint::from(1u8);
let one = BigUint::from(1u8);
let mut i = BigUint::from(0u8); let mut i = BigUint::from(0u8);
while i < pow { while i < pow {
result = (result * &num) % &modulo; result = (result * &num) % &modulo;
i += &one; i += one::<BigUint>();
} }
result result
} }
//TODO: make this faster
fn new_p() -> BigUint {
let mut private_key = [0u8; 128];
let mut rng = StdRng::from_entropy();
rng.fill_bytes(&mut private_key);
let mut num = BigUint::from_bytes_be(&private_key);
if is_prime_default(&num) {
return num;
} else {
let higher: BigUint;
loop {
num += one::<BigUint>();
if is_prime_default(&num) {
higher = num;
break;
}
}
num = BigUint::from_bytes_be(&private_key);
let lower: BigUint;
loop {
num -= one::<BigUint>();
if is_prime_default(&num) {
lower = num.clone();
break;
}
}
if &higher - &num > &num - &lower {
return lower;
} else {
return higher;
}
}
}
fn main() { fn main() {
let port = "1337"; let port = "1337";
let timeout = 100; //ms let timeout = 100; //ms

View File

@ -0,0 +1,60 @@
pub mod primality_tests {
use num::{BigUint, Integer, One, Zero};
use rand::RngCore;
fn generate_random_biguint(num_bits: usize) -> BigUint {
let mut rng = rand::thread_rng();
let bytes = num_bits / 8 + 1;
let mut buf = vec![0u8; bytes];
rng.fill_bytes(&mut buf);
BigUint::from_bytes_be(&buf)
}
pub fn is_probably_prime(number: &BigUint, iterations: u32) -> bool {
if number <= &BigUint::one() || number == &BigUint::from(4u32) {
return false;
} else if number <= &BigUint::from(3u32) {
return true;
}
let one = BigUint::one();
let mut d = number - &one;
while d.is_even() {
d /= 2u32;
}
for _ in 0..iterations {
let a = generate_random_biguint((number.bits()-1) as usize);
let mut x = mod_exp(a.clone(), &d, number);
if x == one || x == number - &one {
continue;
}
let mut continue_loop = false;
for _ in 0..(number.bits() - 1) {
x = mod_exp(x.clone(), &BigUint::from(2u32), number);
if x == number - &one {
continue_loop = true;
break;
}
}
if !continue_loop {
return false;
}
}
true
}
fn mod_exp(mut base: BigUint, ex: &BigUint, modulus: &BigUint) -> BigUint {
let mut exp = ex.clone();
let mut result = BigUint::one();
base %= modulus;
while !exp.is_zero() {
if exp.is_odd() {
result = (&result * &base) % modulus;
}
base = base.pow(2) % modulus;
exp /= 2u32;
}
result
}
}

View File

@ -0,0 +1,103 @@
pub mod prime_utils {
use num::{BigUint, One, Zero};
use lazy_static::lazy_static;
use crate::primality_test::primality_tests::is_probably_prime;
#[must_use] pub fn log_2(x: &BigUint) -> u64 {
x.bits() - 1
}
#[must_use] pub fn is_prime_default(number: &BigUint) -> bool {
lazy_static! {
static ref defaultvec: Vec<BigUint> = {
let mut vec = Vec::new();
vec.push(BigUint::from(2u8));
vec.push(BigUint::from(3u8));
vec.push(BigUint::from(5u8));
vec.push(BigUint::from(7u8));
vec.push(BigUint::from(11u8));
vec.push(BigUint::from(13u8));
vec.push(BigUint::from(17u8));
vec.push(BigUint::from(19u8));
vec.push(BigUint::from(23u8));
vec.push(BigUint::from(29u8));
vec.push(BigUint::from(31u8));
vec.push(BigUint::from(37u8));
vec.push(BigUint::from(41u8));
vec.push(BigUint::from(43u8));
vec.push(BigUint::from(47u8));
vec.push(BigUint::from(53u8));
vec
};
}
return is_prime(number, &defaultvec);
}
#[must_use] pub fn is_prime(number: &BigUint, g_primes: &Vec<BigUint>) -> bool {
if BigUint::from(1u8) == *number {
return false;
}
if BigUint::from(4u8) > *number {
return true;
}
if number.sqrt().pow(2) == *number {
return false;
}
let two = BigUint::from(2u8);
// number = 2^a - 1
// a = log2(number + 1)
let a = log_2(&(number+1u8));
if BigUint::from(2u8).pow(a as u32)-BigUint::one() != *number {
let mut i = BigUint::one();
let one = BigUint::one();
let zero = BigUint::zero();
let sqrtnum = number.sqrt()+&one; //fake ceil function
if let Some(max_value) = g_primes.iter().max() {
if max_value > &sqrtnum {
for prime in g_primes {
if prime<&sqrtnum && number%prime == zero {
return false;
}
}
}
}
if !is_probably_prime(number,5) {
return false;
}
loop {
i += &one;
if number%&i == zero {
return false;
}
if i == sqrtnum {
return true;
}
}
}
// 4 12 194
let mut last = BigUint::from(4u8);
for _i in 2..a {
last = (last.pow(2)-&two)%number;
}
last == BigUint::from(0u8)
}
}