{"record":{"id":"b0122cfc53b967d4","repo":"gchq/CyberChef","slug":"modulus-must-be-greater-than-zero","errorCode":null,"errorMessage":"Modulus must be greater than zero","messagePattern":"Modulus must be greater than zero","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ModularExponentiation.mjs","lineNumber":104,"sourceCode":"            // Case 3: exponent missing - take from input\n            base = baseParam;\n            exp  = inputVal;\n            if (!exp) {\n                throw new OperationError(\"Exponent must be defined\");\n            }\n        } else if (!inputVal) {\n            // Case 4: base and exponent both missing\n            throw new OperationError(\"Base and Exponent must be defined\");\n        } else throw new OperationError(\"Ambiguous input: specify either Base or Exponent when using Input\");\n\n        // Parse numbers\n        const baseBI = parseBigInt(base, \"Base\");\n        const expBI  = parseBigInt(exp, \"Exponent\");\n        const modBI  = parseBigInt(mod, \"Modulus\");\n\n        // Check for invalid modulus (parseBigInt eliminates negatives)\n        if (modBI === 0n) {\n            throw new OperationError(\"Modulus must be greater than zero\");\n        }\n\n        return modPow(baseBI, expBI, modBI).toString();\n    }\n}\n\nexport default ModularExponentiation;\n","sourceCodeStart":86,"sourceCodeEnd":112,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ModularExponentiation.mjs#L86-L112","documentation":"Thrown by Modular Exponentiation when the Modulus argument parses to BigInt 0n. Modular exponentiation is defined as base^exp mod modulus; a zero modulus means division by zero, so the operation rejects it. parseBigInt accepts decimal (incl. signed) and 0x-hex strings, so '0' or '0x0' both land here.","triggerScenarios":"run(input, args) called with the Modulus argument (args[1]) set to '0', '0x0', '00', or any whitespace-padded variant that trims to '0'. The earlier 'Modulus must be defined' check at line 67 already rejected empty/blank modulus, so this fires only on an explicitly-supplied zero value.","commonSituations":"Leaving the default Modulus='1' field but overriding it to 0 in a recipe; copying an RSA/Diffie-Hellman parameter set where the modulus field was blank-filled with 0; recipe JSON that was hand-edited and set modulus to 0.","solutions":["Set the Modulus argument to a positive integer (decimal like '23' or hex like '0x17').","If deriving modulus from input, ensure the Input field contains a positive number and leave the Modulus box populated.","Validate the modulus string is a positive value before constructing the recipe."],"exampleFix":"// before\nchef.bake(\"Modular Exponentiation\", [\"5\", \"0\", \"3\"]);  // modulus 0 -> error\n// after\nchef.bake(\"Modular Exponentiation\", [\"5\", \"23\", \"3\"]);  // 5^3 mod 23 = 6","handlingStrategy":"validation","validationCode":"function validModulus(modStr) {\n  const v = (modStr ?? \"\").trim();\n  if (!/^(0x[0-9a-f]+|[+-]?[0-9]+)$/i.test(v)) return false;\n  const bi = BigInt(v);\n  return bi > 0n;\n}\n// call: if (!validModulus(args[1])) abort();","typeGuard":"const isPositiveBigIntStr = (s) => {\n  const v = (s ?? \"\").trim();\n  if (!/^(0x[0-9a-f]+|[+-]?[0-9]+)$/i.test(v)) return false;\n  try { return BigInt(v) > 0n; } catch { return false; }\n};","tryCatchPattern":"try {\n  chef.bake(\"Modular Exponentiation\", [base, mod, exp]);\n} catch (e) {\n  if (e.message === \"Modulus must be greater than zero\") {\n    // prompt user for a positive modulus\n  } else throw e;\n}","preventionTips":["Always set Modulus to a known positive constant in saved recipes.","When computing modulus dynamically, clamp with max(1n, value).","Validate modulus strings with a regex before baking."],"tags":["crypto","modular-arithmetic","argument-validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}