gchq/CyberChef · error · OperationError
Modulus must be defined
Error message
Modulus must be defined
What it means
Thrown by the Modular Exponentiation operation when the 'Modulus' argument (args[1]) is empty or whitespace-only. run() trims modStr and rejects a falsy result at ModularExponentiation.mjs:68 before base/exponent resolution. The default modulus is '1', so this only fires when the field is explicitly cleared or set to blanks.
Source
Thrown at src/core/operations/ModularExponentiation.mjs:68
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [baseStr, modStr, expStr] = args;
// Trim everything so "" and " " count as empty
const baseParam = baseStr?.trim();
const expParam = expStr?.trim();
const modParam = modStr?.trim();
const inputVal = input?.trim();
const mod = modParam;
if (!mod) {
throw new OperationError("Modulus must be defined");
}
// Base *or* Exponent (but not both) are taken from the Input
// if their boxes are empty.
let base, exp;
if (baseParam && expParam) {
// Case 1: base and exponent both given as parameters
base = baseParam;
exp = expParam;
} else if (!baseParam && expParam) {
// Case 2: base missing - take from input
base = inputVal;
exp = expParam;
if (!base) {
throw new OperationError("Base must be defined");
}
} else if (baseParam && !expParam) {
// Case 3: exponent missing - take from inputView on GitHub (pinned to 4290ea7539)
Solutions
- Provide a non-empty, positive integer Modulus (e.g. the default '1', or your RSA/DH modulus).
- When building recipes programmatically, default modulus to '1' if blank.
- Validate modStr.trim() is non-empty before running.
Example fix
// before
modExp.run('', ['', '', '']); // modulus blank -> throws
// after
modExp.run('2', ['', '15', '']); // modulus 15 -> computes 2^input mod 15 Defensive patterns
Strategy: validation
Validate before calling
function definedModulus(m) {
const v = String(m ?? '').trim();
if (!v) throw new Error('Modulus must be defined');
return v;
}
const mod = definedModulus(args[1]); Type guard
function isDefinedModulus(v: unknown): v is string {
return typeof v === 'string' && v.trim().length > 0;
} Prevention
- Keep the default modulus ('1') unless you intentionally change it.
- Treat a blank/whitespace modulus as invalid at recipe build time.
- Remember a later check also rejects modulus == 0 after parsing.
When it happens
Trigger: Invoking Modular Exponentiation with args[1] ('Modulus') set to '', ' ', or omitted. Raised at ModularExponentiation.mjs:67-69. A later check (line 103) separately rejects a modulus that parses to 0.
Common situations: User cleared the Modulus field expecting it to be optional; a recipe JSON with a missing/blank modulus; copy/paste that left only whitespace.
Related errors
- Base must be defined
- Exponent must be defined
- Base and Exponent must be defined
- Letter ${letter} is not included in LS47
- Unknown padding type: ${padding}
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/1d34f3ba6c3cdaca.
Report an issue: GitHub.