gchq/CyberChef · error · OperationError

Modulus must be greater than zero

Error message

Modulus must be greater than zero

What it means

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.

Source

Thrown at src/core/operations/ModularExponentiation.mjs:104

            // Case 3: exponent missing - take from input
            base = baseParam;
            exp  = inputVal;
            if (!exp) {
                throw new OperationError("Exponent must be defined");
            }
        } else if (!inputVal) {
            // Case 4: base and exponent both missing
            throw new OperationError("Base and Exponent must be defined");
        } else throw new OperationError("Ambiguous input: specify either Base or Exponent when using Input");

        // Parse numbers
        const baseBI = parseBigInt(base, "Base");
        const expBI  = parseBigInt(exp, "Exponent");
        const modBI  = parseBigInt(mod, "Modulus");

        // Check for invalid modulus (parseBigInt eliminates negatives)
        if (modBI === 0n) {
            throw new OperationError("Modulus must be greater than zero");
        }

        return modPow(baseBI, expBI, modBI).toString();
    }
}

export default ModularExponentiation;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Set the Modulus argument to a positive integer (decimal like '23' or hex like '0x17').
  2. If deriving modulus from input, ensure the Input field contains a positive number and leave the Modulus box populated.
  3. Validate the modulus string is a positive value before constructing the recipe.

Example fix

// before
chef.bake("Modular Exponentiation", ["5", "0", "3"]);  // modulus 0 -> error
// after
chef.bake("Modular Exponentiation", ["5", "23", "3"]);  // 5^3 mod 23 = 6
Defensive patterns

Strategy: validation

Validate before calling

function validModulus(modStr) {
  const v = (modStr ?? "").trim();
  if (!/^(0x[0-9a-f]+|[+-]?[0-9]+)$/i.test(v)) return false;
  const bi = BigInt(v);
  return bi > 0n;
}
// call: if (!validModulus(args[1])) abort();

Type guard

const isPositiveBigIntStr = (s) => {
  const v = (s ?? "").trim();
  if (!/^(0x[0-9a-f]+|[+-]?[0-9]+)$/i.test(v)) return false;
  try { return BigInt(v) > 0n; } catch { return false; }
};

Try / catch

try {
  chef.bake("Modular Exponentiation", [base, mod, exp]);
} catch (e) {
  if (e.message === "Modulus must be greater than zero") {
    // prompt user for a positive modulus
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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