{"record":{"id":"3b8fda9b1a721a3c","repo":"gchq/CyberChef","slug":"bit-length-must-be-at-least-2-3b8fda","errorCode":null,"errorMessage":"Bit length must be at least 2","messagePattern":"Bit length must be at least 2","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/RandomPrime.mjs","lineNumber":123,"sourceCode":"            },\n            {\n                name: \"Output format\",\n                type: \"option\",\n                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.`);","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/RandomPrime.mjs#L105-L141","documentation":"Thrown by the RandomPrime operation when the requested prime bit length is less than 2. Primes require at least 2 bits (the value 2 = binary 10). Smaller bit widths cannot represent a prime.","triggerScenarios":"Entering bits = 0 or 1; a negative bit length; a malformed recipe defaulting to 0.","commonSituations":"Typo; testing with tiny values; recipe missing the bits argument.","solutions":["Set bits to an integer >= 2.","For real use, prefer >= 256 (cryptographically meaningful sizes).","Confirm bits is a number, not a string that becomes NaN."],"exampleFix":"// before\n//   bits: 1\n// after\n//   bits: 512","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(bits) || bits < 2) throw new Error('Bit length must be an integer >= 2');","typeGuard":"const validBits = b => Number.isInteger(b) && b >= 2 && b <= 4096;","tryCatchPattern":"try { randomPrime(bits); } catch (e) { if (/at least 2/.test(e.message)) bits = 256; else throw e; }","preventionTips":["Validate bits is an integer in [2,4096].","Default to cryptographically meaningful sizes (>=256)."],"tags":["crypto","prime","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}