{"id":13016,"library":"crypto-miller-rabin","title":"Miller-Rabin Primality Test","description":"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.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/fabiospampinato/crypto-miller-rabin","tags":["javascript","crypto","miller","rabin","prime","primality","test","typescript"],"install":[{"cmd":"npm install crypto-miller-rabin","lang":"bash","label":"npm"},{"cmd":"yarn add crypto-miller-rabin","lang":"bash","label":"yarn"},{"cmd":"pnpm add crypto-miller-rabin","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary function `isProbablyPrime` is exported as a default export. Using a named import will result in a TypeError.","wrong":"import { isProbablyPrime } from 'crypto-miller-rabin';","symbol":"isProbablyPrime","correct":"import isProbablyPrime from 'crypto-miller-rabin';"},{"note":"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.","wrong":"const isProbablyPrime = require('crypto-miller-rabin');","symbol":"isProbablyPrime","correct":"import isProbablyPrime from 'crypto-miller-rabin';"},{"note":"The library ships with TypeScript types, providing type safety and auto-completion when used in TypeScript projects. No separate type package (@types/...) is required.","symbol":"isProbablyPrime","correct":"import isProbablyPrime from 'crypto-miller-rabin';"}],"quickstart":{"code":"import isProbablyPrime from 'crypto-miller-rabin';\n\n// This example demonstrates how to use the Miller-Rabin primality test\n// to check if several BigInts are probably prime.\n// The second argument 'rounds' determines the number of iterations,\n// impacting the confidence level of the test. More rounds mean a lower\n// probability of a composite number being misidentified as prime.\n\nconst testNumbers = [\n  428619803581219889005329334991561182527277683715078274359377824192296037302435017260422513n, // Known large probable prime\n  999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999989n, // Large number, likely prime\n  1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890n, // Clearly composite (ends in 0)\n  7n,   // Small prime\n  9n,   // Small composite\n  101n, // Another small prime\n  121n  // Another small composite (11*11)\n];\n\nconst rounds = 30; // A reasonable number of rounds for general use\n\nconsole.log(`Performing Miller-Rabin tests with ${rounds} rounds:`);\n\ntestNumbers.forEach((num) => {\n  const result = isProbablyPrime(num, rounds);\n  console.log(`Is ${num} probably prime? ${result}`);\n});","lang":"typescript","description":"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."},"warnings":[{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change `import { isProbablyPrime } from 'crypto-miller-rabin';` to `import isProbablyPrime from 'crypto-miller-rabin';`.","cause":"Attempting to import `isProbablyPrime` as a named export when it is a default export.","error":"TypeError: (0 , crypto_miller_rabin__WEBPACK_IMPORTED_MODULE_0__.isProbablyPrime) is not a function"},{"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.","cause":"Attempting to use ES Module `import` syntax in a CommonJS environment without proper configuration (e.g., `\"type\": \"module\"` in `package.json`).","error":"SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}