{"record":{"id":"84c8cb6c8f06d3fd","repo":"gchq/CyberChef","slug":"bit-length-limited-to-4096-bits-for-performance-re","errorCode":null,"errorMessage":"Bit length limited to 4096 bits for performance reasons","messagePattern":"Bit length limited to 4096 bits for performance reasons","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"warning","filePath":"src/core/operations/GeneratePrime.mjs","lineNumber":127,"sourceCode":"                value: [\"Decimal\", \"Hexadecimal\"]\n            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {string}\n     */\n    run(input, args) {\n        const [bits, cryptoGrade, outputFormat] = args;\n\n        if (bits < 2) {\n            throw new OperationError(\"Bit length must be at least 2\");\n        }\n\n        if (bits > 4096) {\n            throw new OperationError(\"Bit length limited to 4096 bits for performance reasons\");\n        }\n\n        const rounds = cryptoGrade ? 40 : 7;\n        let attempts = 0;\n        const maxAttempts = 10000;\n\n        let n = randBigInt(bits);\n\n        while (!isProbablePrime(n, rounds)) {\n            n = randBigInt(bits);\n            attempts++;\n\n            if (attempts > maxAttempts) {\n                throw new OperationError(`Failed to generate prime after ${maxAttempts} attempts. Try a different bit length.`);\n            }\n        }\n\n        // Return only the prime for pipeability","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/GeneratePrime.mjs#L109-L145","documentation":"Thrown by GeneratePrime when the requested bit length exceeds 4096. The cap exists for performance reasons — Miller-Rabin probable-prime testing on very large numbers is expensive in the browser/Node environment.","triggerScenarios":"Passing bits > 4096 (e.g. 8192) expecting RSA-key-sized primes.","commonSituations":"User accustomed to 8192-bit RSA trying to generate equally large primes here.","solutions":["Set bits to 4096 or less.","For larger primes, use a dedicated native crypto tool or OpenSSL outside CyberChef."],"exampleFix":"// before\nargs = [8192, true, \"Hexadecimal\"];\n// after\nargs = [4096, true, \"Hexadecimal\"];","handlingStrategy":"validation","validationCode":"if (bits > 4096) {\n  // cap or reject; explain the 4096-bit performance limit\n}","typeGuard":"function isWithinPrimeBitCap(n) {\n  return Number.isInteger(n) && n >= 2 && n <= 4096;\n}","tryCatchPattern":null,"preventionTips":["Inform users that 4096 is the hard cap for in-browser prime generation.","Direct users needing larger primes to native tooling (OpenSSL)."],"tags":["prime","crypto","argument-validation","limit","performance","user-input"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}