Registry / auth-security / crypto-miller-rabin

crypto-miller-rabin

JSON →
library1.0.1jsnpmunverified

This package provides an efficient JavaScript/TypeScript implementation of the Miller-Rabin primality test. It is a probabilistic algorithm used to determine if a large number (represented as a `BigInt`) is *probably* prime, rather than definitively prime. The current stable version is 1.0.1, indicating a stable API with infrequent updates typical for a specialized mathematical algorithm. Key differentiators include its focus solely on the Miller-Rabin test, making it a lightweight option for applications requiring primality testing without a broader cryptographic suite. It ships with TypeScript types, facilitating its use in modern TypeScript projects and ensuring type safety. The library is suitable for scenarios where a high probability of primality is sufficient, such as in certain cryptographic key generation processes or number theory applications.

npm install crypto-miller-rabin
INSTALL
IMPORT
SIG · CRYPTO-MILLER-RABI
C
crypto-miller-rabin
auth-securityjavascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

isProbablyPrime
import isProbablyPrime from 'crypto-miller-rabin';
import { isProbablyPrime } from 'crypto-miller-rabin';
The primary function `isProbablyPrime` is exported as a default export. Using a named import will result in a TypeError.
isProbablyPrime
import isProbablyPrime from 'crypto-miller-rabin';
const isProbablyPrime = require('crypto-miller-rabin');
The package is designed for ES Modules (ESM). While Node.js can often resolve default ESM exports via CommonJS `require`, using native ESM `import` is the recommended and most reliable pattern to avoid potential module resolution issues.
isProbablyPrime
import isProbablyPrime from 'crypto-miller-rabin';
The library ships with TypeScript types, providing type safety and auto-completion when used in TypeScript projects. No separate type package (@types/...) is required.

This quickstart demonstrates how to import and use the `isProbablyPrime` function with several `BigInt` examples and explains the role of the `rounds` parameter for confidence.

import isProbablyPrime from 'crypto-miller-rabin'; // This example demonstrates how to use the Miller-Rabin primality test // to check if several BigInts are probably prime. // The second argument 'rounds' determines the number of iterations, // impacting the confidence level of the test. More rounds mean a lower // probability of a composite number being misidentified as prime. const testNumbers = [ 428619803581219889005329334991561182527277683715078274359377824192296037302435017260422513n, // Known large probable prime 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999989n, // Large number, likely prime 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890n, // Clearly composite (ends in 0) 7n, // Small prime 9n, // Small composite 101n, // Another small prime 121n // Another small composite (11*11) ]; const rounds = 30; // A reasonable number of rounds for general use console.log(`Performing Miller-Rabin tests with ${rounds} rounds:`); testNumbers.forEach((num) => { const result = isProbablyPrime(num, rounds); console.log(`Is ${num} probably prime? ${result}`); });
Debug
Known issues
gotchaThe Miller-Rabin test is a probabilistic algorithm, meaning it provides a high probability that a number is prime, but never absolute certainty. Increasing the number of 'rounds' (iterations) significantly reduces the probability of a composite number being incorrectly identified as prime, but never eliminates it entirely.
fix
Always understand the probabilistic nature of the algorithm. For cryptographic applications requiring high assurance, consult security best practices for the recommended number of rounds (e.g., 40-64 rounds) or use a deterministic primality test if absolute certainty is required for smaller numbers.
affects: >=1.0.0
gotchaThe `isProbablyPrime` function expects `BigInt` inputs. Passing standard JavaScript numbers will lead to incorrect results or errors for large integers due to JavaScript's `Number` type precision limitations.
fix
Ensure all numerical inputs are explicitly cast to `BigInt` (e.g., by appending `n` to integer literals like `123n`) or are generated as `BigInt`s from other operations.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0 , crypto_miller_rabin__WEBPACK_IMPORTED_MODULE_0__.isProbablyPrime) is not a function
Attempting to import `isProbablyPrime` as a named export when it is a default export.
fix
Change `import { isProbablyPrime } from 'crypto-miller-rabin';` to `import isProbablyPrime from 'crypto-miller-rabin';`.
SyntaxError: Cannot use import statement outside a module
Attempting to use ES Module `import` syntax in a CommonJS environment without proper configuration (e.g., `"type": "module"` in `package.json`).
fix
Ensure your project is configured for ES Modules by adding `"type": "module"` to your `package.json` file, or if targeting older Node.js/environments, use a transpiler like Babel or TypeScript to compile to CommonJS.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
38 hits · last 30 days
node
32
OpenAI (training)
1
Resources
crypto-miller-rabin — npm install crypto-miller-rabin · libregistry