2015-10-13 00:23:01 -04:00
|
|
|
# Pumpkin
|
|
|
|
|
2016-05-05 13:05:22 -04:00
|
|
|
A random number generator for generating large prime numbers, suitable for cryptography.
|
2015-10-13 00:23:01 -04:00
|
|
|
|
|
|
|
## What's up with the name?
|
|
|
|
|
|
|
|
Since I began writing this library around Halloween of 2015, I wanted to choose
|
|
|
|
a name that was vaguely related to the holiday. Because "pumpkin" and "prime"
|
2016-05-05 13:05:22 -04:00
|
|
|
both begin with the letter 'p', I decided to use that.
|
2015-10-13 00:23:01 -04:00
|
|
|
|
|
|
|
## Purpose
|
|
|
|
|
2016-05-05 13:05:22 -04:00
|
|
|
`pumpkin` is a cryptographically-secure, random number generator, useful for
|
|
|
|
generating large prime numbers (at least 512-bits long). On the back-end,
|
|
|
|
`pumpkin` uses the wonderful [ramp](https://crates.io/crates/ramp) library for
|
|
|
|
storing the large numbers. `pumpkin` generates primes very quickly. In our
|
|
|
|
testing, primes were generated anywhere between 1s and 5s on average, though
|
|
|
|
of course your mileage may vary.
|
2015-10-13 00:23:01 -04:00
|
|
|
|
2015-10-22 17:14:05 -04:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
Add the following to your `Cargo.toml` file:
|
|
|
|
|
|
|
|
```
|
2016-05-05 13:05:22 -04:00
|
|
|
pumpkin = "0.3.*"
|
2015-10-22 17:14:05 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
Note that `pumpkin` requires the `nightly` Rust compiler.
|
|
|
|
|
2015-10-13 00:23:01 -04:00
|
|
|
## Example
|
|
|
|
|
2015-10-13 00:24:09 -04:00
|
|
|
```rust
|
2015-10-13 00:23:01 -04:00
|
|
|
extern crate pumpkin;
|
|
|
|
|
|
|
|
use pumpkin::Prime;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let p = Prime::new(2048); // Generate a new 2048-bit prime number
|
|
|
|
let q = Prime::new(2048);
|
|
|
|
let e = p * q;
|
|
|
|
|
2015-10-21 20:58:10 -04:00
|
|
|
println!("{}", e);
|
2015-10-13 00:23:01 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 75222035638256552797269351238215022250546763213674706... Some massive
|
|
|
|
* 4096-bit number.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-05-05 13:05:22 -04:00
|
|
|
You can also initialize your own `OsRng` and generate `Prime`s from that.
|
2015-10-22 17:14:05 -04:00
|
|
|
|
|
|
|
```rust
|
|
|
|
extern crate pumpkin;
|
|
|
|
extern crate rand;
|
|
|
|
|
|
|
|
use pumpkin::Prime;
|
|
|
|
|
|
|
|
use rand::OsRng;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut rngesus = match OsRng::new() {
|
|
|
|
Ok(rng) => rng,
|
|
|
|
Err(e) => panic!("Error trying to initializing RNG: {}", e)
|
|
|
|
};
|
|
|
|
|
|
|
|
let p = Prime::from_rng(2048, &mut rngesus);
|
|
|
|
let q = Prime::from_rng(2048, &mut rngesus);
|
|
|
|
|
|
|
|
let e = p * q;
|
|
|
|
|
|
|
|
println!("{}", e);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 75222035638256552797269351238215022250546763213674706... Some massive
|
|
|
|
* 4096-bit number.
|
|
|
|
*/
|
|
|
|
}
|
2015-10-22 17:21:42 -04:00
|
|
|
```
|
2015-10-22 17:14:05 -04:00
|
|
|
|
2015-10-13 00:23:01 -04:00
|
|
|
## Explanation
|
2016-05-05 13:05:22 -04:00
|
|
|
`Primes` are generated in much the same way as primes generated by `GnuPG`:
|
2015-10-13 00:24:59 -04:00
|
|
|
|
2016-05-05 13:05:22 -04:00
|
|
|
1) Create a large candidate number of a given bit-length. All `Primes` must
|
|
|
|
be at least 512-bits long.
|
2015-10-13 00:23:01 -04:00
|
|
|
|
|
|
|
2) Divide the candidate number by the first 1,000 prime numbers.
|
|
|
|
|
|
|
|
3) Test the candidate number with [Fermat's Little
|
|
|
|
Theorem](https://www.wikiwand.com/en/Fermat's_little_theorem).
|
|
|
|
|
|
|
|
4) Finally, run five iterations of the [Miller-Rabin Primality
|
|
|
|
Test](https://www.wikiwand.com/en/Miller%E2%80%93Rabin_primality_test).
|
|
|
|
|
2016-05-05 13:05:22 -04:00
|
|
|
`Primes` are seeded by `rand::OsRng`, which receives its entropy via the
|
|
|
|
operating system's entropy source (such as `/dev/urandom`). Thus, because we
|
2015-10-13 00:23:01 -04:00
|
|
|
can be confident that the generated candidate number is truly random (or as
|
|
|
|
close to truly random as the user can hope), we don't need to do more than five
|
|
|
|
iterations of the Miller-Rabin test to ensure primality.
|
|
|
|
|
2016-05-05 13:05:22 -04:00
|
|
|
`Primes` are simple "newtype" structs; that is, it is a tuple-like struct
|
|
|
|
surrounding `ramp's` `Int` type. `Primes` have all of the basic algebraic and logical
|
2015-10-13 00:23:01 -04:00
|
|
|
operators implemented, thus allowing you to do any operation that you would
|
|
|
|
require.
|
|
|
|
|
|
|
|
## Contributing
|
|
|
|
|
|
|
|
`pumpkin` is dual-licenced under the MIT and Unlicense. Should you wish to
|
|
|
|
contribute updates to the project, please consider signing the included `WAVER`
|
|
|
|
file with your cryptographic digital signature (as allowed by your country's
|
|
|
|
laws). Doing so will release your changes back into the public domain to be used
|
|
|
|
freely by all. I did so with this project, and it would mean a lot if you did
|
|
|
|
too!
|
2016-05-05 13:05:22 -04:00
|
|
|
|
|
|
|
To sign the `WAIVER`, execute the following commands:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ gpg -sba -u $YOUR_SIGNING_KEY WAIVER
|
|
|
|
$ cat WAIVER.asc >> WAIVER.sigs && rm WAIVER.asc
|
|
|
|
```
|