{"record":{"id":"70cce8efb1e2940b","repo":"gchq/CyberChef","slug":"bit-length-must-be-at-least-2","errorCode":null,"errorMessage":"Bit length must be at least 2","messagePattern":"Bit length must be at least 2","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/GeneratePrime.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/GeneratePrime.mjs#L105-L141","documentation":"Thrown by GeneratePrime when the requested bit length is less than 2. A prime number must be at least 2 (the value 2 itself is 2 bits at minimum representation). This is a lower-bound sanity check on the bits argument before entering the prime-generation loop.","triggerScenarios":"Passing bits = 0, 1, a negative number, or a value coercing to < 2.","commonSituations":"Default/empty bits field, or a UI control allowing sub-2 values.","solutions":["Set bits to an integer >= 2.","Ensure the bits ingredient is numeric and not NaN."],"exampleFix":"// before\nargs = [1, true, \"Hexadecimal\"];\n// after\nargs = [256, true, \"Hexadecimal\"];","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(bits) || bits < 2) {\n  // reject; bits must be an integer >= 2\n}","typeGuard":"function isValidPrimeBits(n) {\n  return Number.isInteger(n) && n >= 2;\n}","tryCatchPattern":null,"preventionTips":["Default the bits field to a standard value (e.g. 256, 512).","Reject zero, negative, or fractional bit lengths at the input boundary."],"tags":["prime","crypto","argument-validation","numeric","user-input"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}