| Crates.io | crypto-primes |
| lib.rs | crypto-primes |
| version | 0.7.0-pre.3 |
| created_at | 2023-01-21 04:51:48.871695+00 |
| updated_at | 2025-09-13 18:33:26.142011+00 |
| description | Random prime number generation and primality checking library |
| homepage | |
| repository | https://github.com/entropyxyz/crypto-primes |
| max_upload_size | |
| id | 763976 |
| size | 452,777 |
crypto-bigintThis library implements prime number generation and primality checking for crypto-bigint integers.
At a high level, provides two primality tests that can be used by themselves or to generate random primes:
The generated primes can have additional constraints imposed on them, like having certain bits set, or requiring the primes to be safe.
Advanced users can use the primality test components from the hazmat module to build a custom prime finding solution that best fit their needs:
The library is no-std compatible and contains no unsafe code.
Find a 196 bit prime returned in a 256-bit long crypto_bigint::U256:
use crypto_bigint::U256;
use rand_core::{OsRng, TryRngCore};
use crypto_primes::{Flavor, is_prime, random_prime};
let prime = random_prime::<U256, _>(&mut OsRng.unwrap_mut(), Flavor::Any, 196);
assert!(is_prime(Flavor::Any, &prime));
Find a 64 bit safe prime returned in a crypto_bigint::U1024:
use crypto_bigint::U1024;
use rand_core::{OsRng, TryRngCore};
use crypto_primes::{Flavor, is_prime, random_prime};
let prime = random_prime::<U1024, _>(&mut OsRng.unwrap_mut(), Flavor::Safe, 64);
assert!(is_prime(Flavor::Safe, &prime));
random_prime() returns primes with MSB set.
If a different behavior is desired, it can be done by manually creating a sieve:
use crypto_primes::{
Flavor,
hazmat::{SetBits, SmallFactorsSieveFactory},
is_prime, random_prime, sieve_and_find,
};
use crypto_bigint::U256;
use rand_core::{OsRng, TryRngCore};
let flavor = Flavor::Any;
let factory = SmallFactorsSieveFactory::<U256>::new(flavor, 256, SetBits::TwoMsb).unwrap();
let prime = sieve_and_find(
&mut OsRng.unwrap_mut(),
factory,
|_rng, candidate| is_prime(flavor, candidate)
).unwrap().unwrap();
assert!(is_prime(flavor, &prime));
The following features are available:
multicore: Enables additional parallel prime finding functions. Disabled by default.