gchq/CyberChef · error · OperationError

Bit length must be at least 2

Error message

Bit length must be at least 2

What it means

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.

Source

Thrown at src/core/operations/GeneratePrime.mjs:123

            },
            {
                name: "Output format",
                type: "option",
                value: ["Decimal", "Hexadecimal"]
            }
        ];
    }

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {string}
     */
    run(input, args) {
        const [bits, cryptoGrade, outputFormat] = args;

        if (bits < 2) {
            throw new OperationError("Bit length must be at least 2");
        }

        if (bits > 4096) {
            throw new OperationError("Bit length limited to 4096 bits for performance reasons");
        }

        const rounds = cryptoGrade ? 40 : 7;
        let attempts = 0;
        const maxAttempts = 10000;

        let n = randBigInt(bits);

        while (!isProbablePrime(n, rounds)) {
            n = randBigInt(bits);
            attempts++;

            if (attempts > maxAttempts) {
                throw new OperationError(`Failed to generate prime after ${maxAttempts} attempts. Try a different bit length.`);

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Set bits to an integer >= 2.
  2. Ensure the bits ingredient is numeric and not NaN.

Example fix

// before
args = [1, true, "Hexadecimal"];
// after
args = [256, true, "Hexadecimal"];
Defensive patterns

Strategy: validation

Validate before calling

if (!Number.isInteger(bits) || bits < 2) {
  // reject; bits must be an integer >= 2
}

Type guard

function isValidPrimeBits(n) {
  return Number.isInteger(n) && n >= 2;
}

Prevention

When it happens

Trigger: Passing bits = 0, 1, a negative number, or a value coercing to < 2.

Common situations: Default/empty bits field, or a UI control allowing sub-2 values.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/70cce8efb1e2940b. Report an issue: GitHub.