{"record":{"id":"edce1101a235d057","repo":"gchq/CyberChef","slug":"bit-length-limited-to-4096-bits-for-performance-re-edce11","errorCode":null,"errorMessage":"Bit length limited to 4096 bits for performance reasons","messagePattern":"Bit length limited to 4096 bits for performance reasons","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/RandomPrime.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/RandomPrime.mjs#L109-L145","documentation":"Thrown by RandomPrime when the requested bit length exceeds 4096. This is an artificial cap to prevent performance problems: Miller-Rabin testing and big-integer arithmetic on >4096-bit numbers are slow in the browser/worker.","triggerScenarios":"Requesting 8192-bit or larger primes; copy-pasting an RSA-8192 key size into the bit field.","commonSituations":"Assuming modern RSA sizes (8192) are supported; wanting extra security margin; scripted requests with large bit counts.","solutions":["Limit the request to <= 4096 bits.","If you truly need a larger prime, generate it externally with a native crypto library.","Reconsider whether such a large prime is necessary for your use case."],"exampleFix":"// before\n//   bits: 8192\n// after\n//   bits: 4096","handlingStrategy":"validation","validationCode":"if (bits > 4096) throw new Error('Bit length capped at 4096');","typeGuard":"const withinCap = b => Number.isInteger(b) && b <= 4096;","tryCatchPattern":"try { randomPrime(bits); } catch (e) { if (/4096/.test(e.message)) bits = 4096; else throw e; }","preventionTips":["Cap bits at 4096.","Use native tooling for larger primes."],"tags":["crypto","prime","validation","performance"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}