The `miller-rabin` package provides an implementation of the probabilistic Miller-Rabin primality test algorithm for JavaScript. This algorithm efficiently determines if a given large number is likely prime, although it carries a small, exponentially decreasing chance of falsely identifying a composite number as prime (a 'strong liar'). The current stable version, 4.0.1, was last published 8 years ago, indicating a maintenance-only or abandoned status. It relies on the `bn.js` library for arbitrary-precision integer arithmetic, rather than native JavaScript `BigInt` introduced in later Node.js and browser versions. While functional, developers initiating new projects requiring primality testing might consider more recently updated alternatives that leverage native `BigInt` for potentially better performance and modern API integration. The package's release cadence is effectively non-existent, given its age.
npm install miller-rabinVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `miller-rabin` to test both small and large numbers for primality using the `BN` class from its dependency `bn.js`, specifying the number of test rounds.
Increase the `rounds` parameter when calling `millerRabin.test()` for higher confidence, especially with cryptographic applications where false positives are unacceptable. For full deterministic primality, consider a different algorithm or a highly trusted pre-computed list for numbers below a certain threshold.
In ES Module projects, use dynamic `import()`: `const millerRabin = await import('miller-rabin');` or ensure your build setup correctly transpiles CJS imports. Alternatively, configure your `package.json` for CommonJS or use `.cjs` file extensions for CJS modules if in a mixed environment. [16, 17, 18]For new projects or if encountering performance issues with very large numbers, consider more modern and actively maintained primality testing libraries that utilize native `BigInt` (e.g., `latonv/MillerRabinPrimality` or `crypto-miller-rabin` on npm) for potentially better performance and compatibility with modern JavaScript features. [1, 3]
Always wrap your numbers in `new BN('yourNumberString', 10)` before passing them to the `millerRabin.test()` function, as shown in the quickstart example.Ensure that any number passed to `millerRabin.test()` is first converted to a `bn.js.BN` instance: `new BN(myNumber, 10)`.
Use a dynamic `import()` statement for `miller-rabin`: `const millerRabin = await import('miller-rabin');` or switch your project to CommonJS if possible. [18]