A random prime number generator for Deno.
This module can generate a pseudorandom prime number with a bigint
type from a:
- desired bit-length (e.g. 1024-bit); and
- an optional number of primality tests
NOTE: This module is currently exposed to a variable time attack and therefore shouldn't be used for cryptography until further notice.
Created by Jacob Ian Matthews - Website | GitHub
randomPrime(bitlength, tests)
Where:
randomPrime()
returns abigint
.bitlength
is anumber
that is >= 8.tests
is an optionalnumber
with a default value of 5.
Example:
import { randomPrime } from "https://deno.land/x/random_prime/mod.ts";
// Generate a random 1024-bit prime number with 5 primality tests
var prime: bigint = randomPrime(1024)
// Generate a random 2048-bit prime number with 10 primality tests
var prime: bigint = randomPrime(2048, 10)
isProbablePrime(candidate, tests)
Where:
isProbablePrime()
returns aboolean
.candidate
is abigint
.tests
is an optionalnumber
with a default value of 10.
Example:
import { isProbablePrime } from "https://deno.land/x/random_prime/mod.ts";
// Have a number to test for primality
const candidate: bigint = 167n;
// Check if candidate is a probable prime with 10 primality tests
isProbablePrime(candidate) ? console.log("probable prime") : console.log("composite");
Increasing the number of primality tests and increasing the bit-length will decrease the speed of prime number generation.
The probability that a composite number is incorrectly classified as a prime number decreases with an increased number of primality tests such that:
P(misclassified)=(1/4)^t
Where:
t
is the number of tests.
MIT License.
The Miller-Rabin Primality Test algorithm was sourced from:
Menezes, A., P. van Oorschot and S. Vanstone. 1996. Handbook of Applied Cryptography. Boca Raton, Florida: Taylor & Francis Group, LLC.