#random-number #miller-rabin #primality-test #checking #primes #strong #size

no-std crypto-primes

Random prime number generation and primality checking library

12 releases (6 breaking)

0.7.0-pre.0 Feb 22, 2025
0.6.1 Feb 17, 2025
0.6.0-pre.2 Oct 19, 2024
0.6.0-pre.0 Dec 29, 2023
0.2.0 Mar 6, 2023

#89 in Cryptography

Download history 709/week @ 2024-11-30 672/week @ 2024-12-07 869/week @ 2024-12-14 173/week @ 2024-12-21 215/week @ 2024-12-28 433/week @ 2025-01-04 461/week @ 2025-01-11 609/week @ 2025-01-18 740/week @ 2025-01-25 418/week @ 2025-02-01 555/week @ 2025-02-08 930/week @ 2025-02-15 685/week @ 2025-02-22 1054/week @ 2025-03-01 1203/week @ 2025-03-08 573/week @ 2025-03-15

3,687 downloads per month
Used in 19 crates (5 directly)

Apache-2.0 OR MIT and maybe LGPL-3.0+

170KB
2.5K SLoC

Prime number tools for crypto-bigint

crate Docs License Build Status Coverage

This library implements prime number generation and primality checking for crypto-bigint integers. In particular:

  • Generating random primes and safe primes of given bit size;
  • Sieving iterator;
  • Miller-Rabin test;
  • Strong and extra strong Lucas tests, and Lucas-V test.

The library is no-std compatible and contains no unsafe code.

Most users will be using the small set of functions exported from the top level, providing "pre-packaged" prime finding functionality with sane defaults.

Example

Find a 196 bit prime returned in a 256-bit long crypto_bigint::U256:

use crypto_bigint::U256;
let prime = crypto_primes::generate_prime::<U256>(196);
assert!(crypto_primes::is_prime(&prime));

Find a 64 bit safe prime returned in a crypto_bigint::U1024:

use crypto_bigint::U1024;
let prime = crypto_primes::generate_safe_prime::<U1024>(64);
assert!(crypto_primes::is_safe_prime(&prime));

Advanced

Advanced users can use the hazmat module in the library to build a custom prime finding solution that best fit their needs, e.g. by picking different Lucas bases or running Miller-Rabin tests with particular bases.

Features

The following features are available:

  • default-rng: Use the OS default CSPRNG, OsRng. Enabled by default.
  • multicore: Enables additional parallel prime finding functions. Disabled by default.

Dependencies

~1–6.5MB
~137K SLoC