Skip to content

Commit

Permalink
Add parallel versions of generate_prime and generate_safe_prime
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdplm committed Nov 5, 2024
1 parent 2934030 commit 61a860b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ pub use traits::RandomPrimeWithRng;

#[cfg(feature = "default-rng")]
pub use presets::{generate_prime, generate_safe_prime, is_prime, is_safe_prime};
#[cfg(all(feature = "default-rng", feature = "rayon"))]
pub use presets::{par_generate_prime, par_generate_safe_prime};
#[cfg(feature = "rayon")]
pub use presets::{par_generate_prime_with_rng, par_generate_safe_prime_with_rng};
29 changes: 29 additions & 0 deletions src/presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ pub fn generate_prime<T: Integer + RandomBits + RandomMod>(bit_length: u32) -> T
generate_prime_with_rng(&mut OsRng, bit_length)
}

/// Returns a random prime of size `bit_length` using [`OsRng`] as the RNG.
///
/// See [`is_prime_with_rng`] for details about the performed checks.
///
/// Uses [`rayon`] to parallelize the prime search using `corecount/2` threads (but minimum 2).
///
/// Panics if `bit_length` is less than 2, or greater than the bit size of the target `Uint`.
///
/// Panics if the platform is unable to spawn threads.
#[cfg(all(feature = "default-rng", feature = "rayon"))]
pub fn par_generate_prime<T: Integer + RandomBits + RandomMod>(bit_length: u32, threadcount: usize) -> T {
par_generate_prime_with_rng(&mut OsRng, bit_length, threadcount)
}

/// Returns a random safe prime (that is, such that `(n - 1) / 2` is also prime) of size
/// `bit_length` using [`OsRng`] as the RNG.
///
Expand All @@ -28,6 +42,21 @@ pub fn generate_safe_prime<T: Integer + RandomBits + RandomMod>(bit_length: u32)
generate_safe_prime_with_rng(&mut OsRng, bit_length)
}

/// Returns a random safe prime (that is, such that `(n - 1) / 2` is also prime) of size
/// `bit_length` using [`OsRng`] as the RNG.
///
/// See [`is_prime_with_rng`] for details about the performed checks.
///
/// Uses [`rayon`] to parallelize the prime search using `corecount/2` threads (but minimum 2).
///
/// Panics if `bit_length` is less than 3, or greater than the bit size of the target `Uint`.
///
/// Panics if the platform is unable to spawn threads.
#[cfg(all(feature = "default-rng", feature = "rayon"))]
pub fn par_generate_safe_prime<T: Integer + RandomBits + RandomMod>(bit_length: u32, threadcount: usize) -> T {
par_generate_safe_prime_with_rng(&mut OsRng, bit_length, threadcount)
}

/// Probabilistically checks if the given number is prime using [`OsRng`] as the RNG.
///
/// See [`is_prime_with_rng`] for details about the performed checks.
Expand Down

0 comments on commit 61a860b

Please sign in to comment.