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.
|
|
|
|
//! `Primes` are seeded from the operating system's main source of entropy,
|
|
|
|
//! ensuring proper randomness.
|
|
|
|
//!
|
|
|
|
//! `Primes` must be AT LEAST 512-bits long. Attempting to generate a `Prime`
|
|
|
|
//! less than 512-bits long will cause a panic.
|
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! extern crate pumpkin;
|
|
|
|
//!
|
|
|
|
//! use pumpkin::Prime;
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! // Generate 2048-bit primes
|
|
|
|
//! let p = Prime::new(2048);
|
|
|
|
//! let q = Prime::new(2048);
|
|
|
|
//!
|
|
|
|
//! 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
|
|
|
|
2015-10-12 22:01:09 -04:00
|
|
|
mod prime;
|
|
|
|
pub use prime::Prime;
|
2016-05-22 17:29:52 +12:00
|
|
|
pub use prime::SafePrime;
|
2016-05-05 13:05:22 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use rand::OsRng;
|
|
|
|
use super::*;
|
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_new_small_prime() {
|
|
|
|
Prime::new(511);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_new_small_prime_from_rng() {
|
2016-05-10 15:07:49 -04:00
|
|
|
let mut rngesus = OsRng::new().unwrap();
|
2016-05-05 13:05:22 -04:00
|
|
|
|
|
|
|
Prime::from_rng(511, &mut rngesus);
|
|
|
|
}
|
|
|
|
|
2016-05-10 23:30:18 -04:00
|
|
|
#[test]
|
|
|
|
fn test_should_destructure() {
|
|
|
|
let Prime(n) = Prime::new(512);
|
|
|
|
}
|
|
|
|
|
2016-05-05 13:05:22 -04:00
|
|
|
#[bench]
|
|
|
|
fn bench_generate_512_bit_prime(b: &mut Bencher) {
|
|
|
|
b.iter(|| Prime::new(512));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_generate_1024_bit_prime(b: &mut Bencher) {
|
|
|
|
b.iter(|| Prime::new(1024));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_generate_2048_bit_prime(b: &mut Bencher) {
|
|
|
|
b.iter(|| Prime::new(2048));
|
|
|
|
}
|
|
|
|
}
|