gchq/CyberChef · error · OperationError

Modulus must be greater than zero

Error message

Modulus must be greater than zero

What it means

Thrown by Modular Inverse after parsing: the modulus parses to a BigInt that is <= 0n (zero or negative). The multiplicative inverse is only defined for a positive modulus, so any non-positive value is rejected. Unlike Modular Exponentiation, this check also catches negative values because parseBigInt accepts signed decimals.

Source

Thrown at src/core/operations/ModularInverse.mjs:87

            // Case 2: value missing - take from input
            a = inputVal;
            m = mParam;
            if (!a) throw new OperationError("Value (a) must be defined");
        } else if (aParam && !mParam) {
            // Case 3: modulus missing - take from input
            a = aParam;
            m = inputVal;
            if (!m) throw new OperationError("Modulus (m) must be defined");
        } else if (!aParam && !mParam) {
            // Case 4: value and modulus both missing
            throw new OperationError("Value (a) and Modulus (m) must be defined");
        }

        const aBI = parseBigInt(a, "Value (a)");
        const mBI = parseBigInt(m, "Modulus (m)");

        if (mBI <= 0n) {
            throw new OperationError("Modulus must be greater than zero");
        }

        const aNorm = ((aBI % mBI) + mBI) % mBI;
        const [g, x] = egcd(aNorm, mBI);

        if (g !== 1n && g !== -1n) {
            throw new OperationError("Inverse does not exist because gcd(a, m) ≠ 1");
        }

        let inv = x;
        if (g === -1n) inv = -inv;

        inv = ((inv % mBI) + mBI) % mBI;


        return inv.toString();
    }
}

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Set the modulus to a positive integer greater than zero.
  2. If the modulus is computed dynamically, abs() it or clamp to >=1 before passing.
  3. Audit upstream operations that feed into Modulus to ensure they never emit 0 or negatives.

Example fix

// before
chef.bake("Modular Inverse", ["3", "-7"], "");  // mBI <= 0n
// after
chef.bake("Modular Inverse", ["3", "7"], "");
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

const isPositiveModulusStr = (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 Inverse", [a, m]); }
catch (e) { if (e.message === "Modulus must be greater than zero") fixModulus(); else throw e; }

Prevention

When it happens

Trigger: run(input, args) where the resolved modulus string is '0', '-1', '-7', '0x0', or any decimal/hex that parses to a non-positive BigInt. Reached after the emptiness checks pass.

Common situations: Negative modulus passed from an upstream operation that subtracts; modulus defaulting to 0 in a generated recipe; sign error in computed parameters; copy-paste of a negative coordinate into the modulus field.

Related errors


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