-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
15 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,27 @@ | ||
use num::{One, Integer}; | ||
use num::bigint::{BigUint, RandBigInt}; | ||
use rand::Rng; | ||
|
||
use utils::primes::tests::PrimeTest; | ||
|
||
pub mod tests; | ||
|
||
pub fn generate_prime<T: Rng + RandBigInt>(gen: &mut T, bits: usize) -> BigUint { | ||
/// Generate new prime number with given bit size via given random number generator. | ||
/// | ||
/// Currently this function give guarantee that it ever ends. In case of bad `Rng` engine | ||
/// this could fall into endless loop. | ||
/// | ||
/// This function doesn't reseed `Rng` so You must provide autoreseedable engine, check out | ||
/// [`ReseedingRng`](http://doc.rust-lang.org/rand/rand/reseeding/struct.ReseedingRng.html). | ||
pub fn generate_prime<T: Rng + RandBigInt>(gen: &mut T, bits: usize) -> Option<BigUint> { | ||
loop { | ||
let int = gen.gen_biguint(bits); | ||
let mut int = gen.gen_biguint(bits); | ||
|
||
if int.is_even() { int = int + BigUint::one(); } | ||
|
||
if tests::Fermat(gen).test_loop(&int, 50).is_composite() { continue } | ||
if tests::MillerRabin(gen).test_loop(&int, 50).is_composite() { continue } | ||
|
||
return int | ||
return Some(int) | ||
} | ||
} |