gchq/CyberChef · error · OperationError

Failed to generate prime after ${maxAttempts} attempts. Try

Error message

Failed to generate prime after ${maxAttempts} attempts. Try a different bit length.

What it means

Thrown by RandomPrime when the random search fails to find a probable prime within 10000 attempts for the given bit length. Each attempt draws a new random big integer and tests it with Miller-Rabin; persistent failure is rare but possible for adversarial or pathological parameters.

Source

Thrown at src/core/operations/RandomPrime.mjs:141

            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.`);
            }
        }

        // Return only the prime for pipeability
        if (outputFormat === "Hexadecimal") {
            return "0x" + n.toString(16);
        } else {
            return n.toString();
        }
    }
}

export default GeneratePrime;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Retry the operation (transient RNG/statistical failures usually clear).
  2. Try a different, more standard bit length (e.g. 1024, 2048).
  3. Verify the runtime provides a working cryptographic random source (crypto.getRandomValues / Node crypto).
  4. If scripting, wrap generation in a small retry loop with a backoff.

Example fix

// before
//   single call to RandomPrime with bits=4096 that exhausted 10000 attempts
// after
//   for (let ok=false, n=0; !ok && n<5; n++) {
//     try { result = runRandomPrime(4096); ok=true; } catch(e){}
//   }
Defensive patterns

Strategy: retry

Validate before calling

function generate(bits, grade, maxTries=5) { for (let i=0;i<maxTries;i++){ try { return runRandomPrime(bits, grade); } catch(e){ if(!/Failed to generate prime/.test(e.message)) throw e; } } throw new Error('prime generation failed after retries'); }

Try / catch

try { return runRandomPrime(bits, grade); } catch (e) { if (/Failed to generate prime/.test(e.message)) { /* retry with different bits */ } else throw e; }

Prevention

When it happens

Trigger: Extremely small or awkward bit widths combined with RNG behavior; an environment with a weak/broken random source; very large bit lengths where density of primes is lower per draw.

Common situations: Running in a worker with a degraded CSPRNG; requesting large primes repeatedly; flaky test environments.

Related errors


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