{"record":{"id":"64a5299eebfee8ca","repo":"gchq/CyberChef","slug":"failed-to-generate-prime-after-maxattempts-atte","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/GeneratePrime.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/GeneratePrime.mjs#L123-L155","documentation":"Thrown by GeneratePrime when the random-search loop exceeds maxAttempts (10000) iterations without finding a probable prime. Each iteration generates a fresh random bigint of the requested bit length and tests it with Miller-Rabin. Smaller bit lengths have denser prime distributions; failure here is statistically unlikely but possible, especially at edge bit lengths or with low-round (non-crypto-grade) testing.","triggerScenarios":"Statistically rare run of 10000 consecutive composites at the chosen bit length. More plausible at bit lengths where the random generator or rounding produces biased candidates.","commonSituations":"Unlucky RNG streak, or a faulty/rand-poor environment reducing prime density.","solutions":["Retry the operation — failure is transient and statistical.","Try a different (typically larger or more standard) bit length such as a power of two.","Enable crypto-grade testing (more rounds) only if correctness matters; it does not reduce attempts but improves confidence."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for (let attempt = 0; attempt < 3; attempt++) {\n  try {\n    return generatePrime(bits, cryptoGrade, outputFormat);\n  } catch (e) {\n    if (!/Failed to generate prime/.test(e.message)) throw e;\n  }\n}\nthrow new Error(\"Prime generation failed after retries\");","preventionTips":["Retry the operation a few times; failure is statistical and transient.","Try a standard bit length (power of two) if a custom length repeatedly fails."],"tags":["prime","crypto","retry","rng","runtime"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}