Registry / serialization / miller-rabin

miller-rabin

JSON →
library1.0.1jsnpmunverified

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-rabin
INSTALL
IMPORT
SIG · MILLER-RABIN
M
miller-rabin
serializationjavascriptv1.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.

millerRabin
import millerRabin from 'miller-rabin';
const millerRabin = require('miller-rabin');
While primarily a CommonJS module, modern Node.js ESM projects can use a default import. Direct `require()` is the native CommonJS method.
MillerRabin
import MillerRabin from 'miller-rabin';
const { MillerRabin } = require('miller-rabin');
The primary export is a constructor function (or factory function), often imported as the default. Named imports are not typically used for this package's main functionality.
BN
import BN from 'bn.js';
Although `bn.js` is a dependency, it is typically imported directly if its functionality is needed outside of `miller-rabin`'s internal usage. `miller-rabin` itself expects `BN` instances as input for large numbers.

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.

import millerRabin from 'miller-rabin'; import BN from 'bn.js'; // Test a small number let isPrimeSmall = millerRabin.test(new BN(7), 5); // 5 rounds console.log(`Is 7 prime? ${isPrimeSmall}`); // Expected: true // Test a larger number (e.g., a 256-bit number) // In a real application, this number would come from somewhere. // For demonstration, let's create a large prime-like number. // Using a known prime for demonstration purposes. const largeNumberStr = '115792089237316195423570985008687907853269984665640564039457584007913129639937'; // A known large prime const largeNumber = new BN(largeNumberStr, 10); // The number of rounds (iterations) affects the probability of error. // A higher number of rounds decreases the chance of a composite number being misidentified as prime. const numberOfRounds = 64; const testResult = millerRabin.test(largeNumber, numberOfRounds); console.log(` Testing: ${largeNumber.toString()} Number of rounds: ${numberOfRounds}`); console.log(`Is it probably prime? ${testResult}`); const compositeNumber = new BN('99999999999999999999999999999999999999999999999999999999999999999999999999991', 10); // A large composite const testCompositeResult = millerRabin.test(compositeNumber, numberOfRounds); console.log(` Testing: ${compositeNumber.toString()} Is it probably prime? ${testCompositeResult}`); // Expected: false
Debug
Known issues
gotchaThe Miller-Rabin algorithm is a probabilistic primality test, meaning it can sometimes identify a composite number as 'probably prime' (a false positive). While the probability of error is extremely low and decreases exponentially with more test rounds, it is not a deterministic proof of primality. A prime number will never be incorrectly identified as composite. [1, 10]
fix
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.
affects: >=1.0.0
breakingThis package is a CommonJS (CJS) module. Attempting to use `require('miller-rabin')` directly in a Node.js project configured for ES Modules (`"type": "module"` in `package.json`) will result in an `ERR_REQUIRE_ESM` error. [18]
fix
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]
affects: >=4.0.0
deprecatedThe `miller-rabin` package (indutny/miller-rabin) has not been updated in 8 years, with version 4.0.1 being its last release. This means it may lack performance optimizations, bug fixes, or security updates present in newer JavaScript primality testing libraries that leverage native `BigInt` or are more actively maintained. [4]
fix
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]
affects: >=4.0.0
gotchaThis library relies on the `bn.js` library for big integer arithmetic. Inputs to `millerRabin.test()` must be instances of `bn.js.BN`. Passing standard JavaScript `Number` or `BigInt` primitives directly will lead to errors or incorrect results. [6]
fix
Always wrap your numbers in `new BN('yourNumberString', 10)` before passing them to the `millerRabin.test()` function, as shown in the quickstart example.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: num.toArray is not a function
The input `num` to `millerRabin.test` was not an instance of `bn.js.BN`. This typically happens when passing a raw `number` or native `BigInt`.
fix
Ensure that any number passed to `millerRabin.test()` is first converted to a `bn.js.BN` instance: `new BN(myNumber, 10)`.
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.
You are attempting to import this CommonJS package using `require()` within a Node.js project configured as an ES Module (`"type": "module"` in `package.json`).
fix
Use a dynamic `import()` statement for `miller-rabin`: `const millerRabin = await import('miller-rabin');` or switch your project to CommonJS if possible. [18]
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
bn.jsrequiredProvides arbitrary-precision integer arithmetic for handling large numbers beyond standard JavaScript `Number` limits.
brorandrequiredUsed for generating cryptographically secure random numbers required by the Miller-Rabin test's probabilistic nature.
Agent activity
6 hits · last 30 days
node
6
Resources
miller-rabin — npm install miller-rabin · libregistry