crypto-random-prime
Version:
Generate a cryptographically-random probable prime number that passes the Miller-Rabin test with the given number of bits of entropy.
18 lines (17 loc) • 441 B
JavaScript
/* IMPORT */
import isProbablyPrime from 'crypto-miller-rabin';
import randomBigInt from 'crypto-random-bigint';
/* MAIN */
const randomPrime = (bits, k = 8) => {
let n = randomBigInt(bits);
if ((n & 1n) === 0n) { // Even number
n += 1n; // Next odd number
}
while (true) {
if (isProbablyPrime(n, k))
return n;
n += 2n; // Next odd number
}
};
/* EXPORT */
export default randomPrime;