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 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.

Source

Thrown at src/core/operations/RandomPrime.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. For real use, prefer >= 256 (cryptographically meaningful sizes).
  3. Confirm bits is a number, not a string that becomes NaN.

Example fix

// before
//   bits: 1
// after
//   bits: 512
Defensive patterns

Strategy: validation

Validate before calling

if (!Number.isInteger(bits) || bits < 2) throw new Error('Bit length must be an integer >= 2');

Type guard

const validBits = b => Number.isInteger(b) && b >= 2 && b <= 4096;

Try / catch

try { randomPrime(bits); } catch (e) { if (/at least 2/.test(e.message)) bits = 256; else throw e; }

Prevention

When it happens

Trigger: Entering bits = 0 or 1; a negative bit length; a malformed recipe defaulting to 0.

Common situations: Typo; testing with tiny values; recipe missing the bits argument.

Related errors


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