{"record":{"id":"deb608cea1383d2a","repo":"gchq/CyberChef","slug":"failed-to-generate-prime-after-maxattempts-atte-deb608","errorCode":null,"errorMessage":"Failed to generate prime after ${maxAttempts} attempts. Try a different bit length.","messagePattern":"Failed to generate prime after (.+?) attempts\\. Try a different bit length\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/RandomPrime.mjs","lineNumber":141,"sourceCode":"            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.`);\n            }\n        }\n\n        // Return only the prime for pipeability\n        if (outputFormat === \"Hexadecimal\") {\n            return \"0x\" + n.toString(16);\n        } else {\n            return n.toString();\n        }\n    }\n}\n\nexport default GeneratePrime;\n","sourceCodeStart":123,"sourceCodeEnd":155,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/RandomPrime.mjs#L123-L155","documentation":"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.","triggerScenarios":"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.","commonSituations":"Running in a worker with a degraded CSPRNG; requesting large primes repeatedly; flaky test environments.","solutions":["Retry the operation (transient RNG/statistical failures usually clear).","Try a different, more standard bit length (e.g. 1024, 2048).","Verify the runtime provides a working cryptographic random source (crypto.getRandomValues / Node crypto).","If scripting, wrap generation in a small retry loop with a backoff."],"exampleFix":"// before\n//   single call to RandomPrime with bits=4096 that exhausted 10000 attempts\n// after\n//   for (let ok=false, n=0; !ok && n<5; n++) {\n//     try { result = runRandomPrime(4096); ok=true; } catch(e){}\n//   }","handlingStrategy":"retry","validationCode":"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'); }","typeGuard":null,"tryCatchPattern":"try { return runRandomPrime(bits, grade); } catch (e) { if (/Failed to generate prime/.test(e.message)) { /* retry with different bits */ } else throw e; }","preventionTips":["Retry transient prime-generation failures a few times.","Prefer standard bit lengths (1024/2048/4096).","Verify the runtime RNG is healthy."],"tags":["crypto","prime","retry","random"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}