2016-05-05 13:05:22 -04:00
|
|
|
#![feature(test)]
|
2016-05-10 15:07:49 -04:00
|
|
|
#![deny(missing_docs, missing_debug_implementations,
|
|
|
|
missing_copy_implementations, trivial_casts, trivial_numeric_casts,
|
|
|
|
unsafe_code, unused_import_braces, unused_qualifications)]
|
|
|
|
#![cfg_attr(feature = "dev", feature(plugin))]
|
|
|
|
#![cfg_attr(feature = "dev", plugin(clippy))]
|
2015-10-04 21:32:25 -04:00
|
|
|
|
2016-05-10 15:07:49 -04:00
|
|
|
//! A crate for generating large, cryptographically secure prime numbers.
|
2016-06-01 00:35:51 -04:00
|
|
|
//! These numbers are seeded from the operating system's main source of
|
|
|
|
//! entropy, ensuring proper randomness.
|
2016-05-10 15:07:49 -04:00
|
|
|
//!
|
2016-06-01 00:35:51 -04:00
|
|
|
//! Numbers are verified to be prime by running the following three tests
|
|
|
|
//! during initialization:
|
|
|
|
//!
|
|
|
|
//! 1. Dividing the initial prime number candidate by the first 1,000 prime
|
|
|
|
//! numbers, checking the remainder. Should the remainder ever be zero, then
|
|
|
|
//! add two to the candidate and try again.
|
|
|
|
//!
|
|
|
|
//! 2. Run a Fermat Primality Test on the candidate. If it doesn't pass, add
|
|
|
|
//! two to the candidate and goto Step 1.
|
|
|
|
//!
|
|
|
|
//! 3. Finally, complete five rounds of the Miller-Rabin Primality Test.
|
|
|
|
//! Should any of the tests pass, add two to the candidate and goto Step 1.
|
|
|
|
//!
|
|
|
|
//! The preceding steps mirror those used by GnuPG, a leading PGP implementation
|
|
|
|
//! used by thousands of users all across the world.
|
|
|
|
//!
|
|
|
|
//! The prime numbers must be AT LEAST 512-bits long. Attempting to generate a
|
|
|
|
//! number less than 512-bits long will cause a panic.
|
2016-05-10 15:07:49 -04:00
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! extern crate pumpkin;
|
|
|
|
//!
|
2016-06-01 00:35:51 -04:00
|
|
|
//! use pumpkin::prime;
|
2016-05-10 15:07:49 -04:00
|
|
|
//!
|
|
|
|
//! fn main() {
|
2016-06-01 00:35:51 -04:00
|
|
|
//! // Generate 2, 2048-bit primes
|
|
|
|
//! let p = prime::new(2048);
|
|
|
|
//! let q = prime::new(2048);
|
2016-05-10 15:07:49 -04:00
|
|
|
//!
|
|
|
|
//! let n = p * q;
|
|
|
|
//! println!("{}", n); // Some 4096-bit composite number
|
|
|
|
//! }
|
|
|
|
//! ```
|
2015-10-06 23:12:26 -04:00
|
|
|
|
2016-05-10 15:07:49 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate custom_derive;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate newtype_derive;
|
2015-10-04 21:32:25 -04:00
|
|
|
extern crate ramp;
|
2015-09-28 00:25:30 -04:00
|
|
|
extern crate rand;
|
2016-05-05 13:05:22 -04:00
|
|
|
extern crate test;
|
2015-09-28 00:25:30 -04:00
|
|
|
|
2016-06-01 00:35:51 -04:00
|
|
|
mod common;
|
2016-06-11 15:58:28 -04:00
|
|
|
pub mod error;
|
2016-06-01 00:35:51 -04:00
|
|
|
pub mod prime;
|
|
|
|
pub mod safe_prime;
|
2016-05-05 13:05:22 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use rand::OsRng;
|
2016-06-01 00:35:51 -04:00
|
|
|
use super::{prime, safe_prime};
|
2016-05-05 13:05:22 -04:00
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_generate_512_bit_prime(b: &mut Bencher) {
|
2016-06-01 00:35:51 -04:00
|
|
|
let mut rngesus = OsRng::new().unwrap();
|
|
|
|
b.iter(|| prime::from_rng(512, &mut rngesus));
|
2016-05-05 13:05:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_generate_1024_bit_prime(b: &mut Bencher) {
|
2016-06-01 00:35:51 -04:00
|
|
|
let mut rngesus = OsRng::new().unwrap();
|
|
|
|
b.iter(|| prime::from_rng(1024, &mut rngesus));
|
2016-05-05 13:05:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_generate_2048_bit_prime(b: &mut Bencher) {
|
2016-06-01 00:35:51 -04:00
|
|
|
let mut rngesus = OsRng::new().unwrap();
|
|
|
|
b.iter(|| prime::from_rng(2048, &mut rngesus));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2016-06-11 15:58:28 -04:00
|
|
|
fn bench_generate_512_bit_safe_prime(b: &mut Bencher) {
|
2016-06-01 00:35:51 -04:00
|
|
|
let mut rngesus = OsRng::new().unwrap();
|
2016-06-11 15:58:28 -04:00
|
|
|
b.iter(|| safe_prime::from_rng(512, &mut rngesus));
|
2016-05-05 13:05:22 -04:00
|
|
|
}
|
|
|
|
}
|