Skip to content

Commit

Permalink
Switch to gen_safe_prime() for another keygen speed boost.
Browse files Browse the repository at this point in the history
gen_safe_prime_exact() starts from gen_prime_exact(), which always sets
the high bit.  Since gen_safe_prime_exact() may actually return (p<<1)+1,
this actually significantly increases the odds of generating a too-large
key and forcing us to re-start the keygen loop.
  • Loading branch information
zrax committed Dec 7, 2023
1 parent cb489f2 commit 7f90b81
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/bin/moulars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn write_progress_pip(out: &mut io::Stdout) {
let _ = out.flush();
}

#[allow(clippy::too_many_lines)]
fn main() -> ExitCode {
// See https://docs.rs/env_logger/latest/env_logger/index.html for
// details on fine-tuning logging behavior beyond the defaults.
Expand Down Expand Up @@ -83,9 +84,9 @@ fn main() -> ExitCode {
let mut rng = rand::thread_rng();
let mut stdout = io::stdout();
loop {
let key_n: BigUint = rng.gen_safe_prime_exact(512);
let key_n: BigUint = rng.gen_safe_prime(512);
write_progress_pip(&mut stdout);
let key_k: BigUint = rng.gen_safe_prime_exact(512);
let key_k: BigUint = rng.gen_safe_prime(512);
write_progress_pip(&mut stdout);
let key_x = key_g.to_biguint().unwrap().modpow(&key_k, &key_n);
write_progress_pip(&mut stdout);
Expand All @@ -97,9 +98,8 @@ fn main() -> ExitCode {
let bytes_x = key_x.to_bytes_be();

if bytes_n.len() != 64 || bytes_k.len() != 64 || bytes_x.len() != 64 {
// We generated a bad length key. Somehow, this can happen
// despite the "exactly 512 bits" requested above. So now
// we need to start over :(
// We generated a bad length key, so now we need to
// start over :(
continue;
}

Expand Down

0 comments on commit 7f90b81

Please sign in to comment.