add WIP p generator
This commit is contained in:
+41
-3
@@ -5,11 +5,15 @@ use sha2::{Digest,Sha512};
|
||||
use regex::Regex;
|
||||
use crc32fast;
|
||||
use rand::{rngs::StdRng, RngCore, SeedableRng};
|
||||
use num_bigint::BigUint;
|
||||
use num::{BigUint, one};
|
||||
|
||||
mod big_array;
|
||||
mod primality_test;
|
||||
mod prime_utils;
|
||||
use big_array::BigArray;
|
||||
|
||||
use crate::prime_utils::prime_utils::is_prime_default;
|
||||
|
||||
const MAX_FRAME_PAYLOAD:u16=508;
|
||||
const MAX_FRAME_PAYLOAD_U:usize=MAX_FRAME_PAYLOAD as usize;
|
||||
const HEADER_SIZE:u16 = 12;
|
||||
@@ -40,16 +44,50 @@ struct StrPacket {
|
||||
|
||||
fn pow_mod(num: BigUint, pow: BigUint, modulo: BigUint) -> BigUint {
|
||||
let mut result = BigUint::from(1u8);
|
||||
let one = BigUint::from(1u8);
|
||||
let mut i = BigUint::from(0u8);
|
||||
while i < pow {
|
||||
result = (result * &num) % &modulo;
|
||||
i += &one;
|
||||
i += one::<BigUint>();
|
||||
}
|
||||
|
||||
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() {
|
||||
let port = "1337";
|
||||
let timeout = 100; //ms
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user