gchq/CyberChef · error · Error

Modulus cannot be zero

Error message

Modulus cannot be zero

What it means

Thrown by the MOD operation when the 'Modulus' argument (args[0]) evaluates to zero. Division/modulo by zero is undefined, so after constructing a BigNumber from args[0] the operation checks modulus.isZero() and throws a plain Error (not OperationError) at MOD.mjs:51. The default modulus is 2. Note: because this is a plain Error, recipe execution may treat it differently from OperationError.

Source

Thrown at src/core/operations/MOD.mjs:51

            {
                "name": "Delimiter",
                "type": "option",
                "value": ARITHMETIC_DELIM_OPTIONS,
            }
        ];
    }

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {string}
     */
    run(input, args) {
        const modulus = new BigNumber(args[0]);
        const delimiter = args[1];

        if (modulus.isZero()) {
            throw new Error("Modulus cannot be zero");
        }

        const numbers = createNumArray(input, delimiter);
        const results = numbers.map(num => num.mod(modulus));

        return results.join(" ");
    }

}

export default MOD;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Set the Modulus argument to a non-zero number (the default 2 works for parity checks).
  2. If you derived the modulus programmatically, guard against zero before invoking MOD.
  3. Wrap the call in try/catch — note this op throws a plain Error, not OperationError.

Example fix

// before
mod.run('15 4 7', [0, 'Space']);   // modulus zero -> throws

// after
mod.run('15 4 7', [3, 'Space']);    // modulus 3 -> '0 1 1'
Defensive patterns

Strategy: validation

Validate before calling

import BigNumber from 'bignumber.js';
function nonZeroModulus(m) {
  const mod = new BigNumber(m);
  if (mod.isZero() || !mod.isFinite()) {
    throw new Error('Modulus must be a non-zero finite number');
  }
  return mod;
}
const modulus = nonZeroModulus(args[0]);

Type guard

import BigNumber from 'bignumber.js';
function isNonZeroModulus(v: unknown): v is string|number {
  try {
    const b = new BigNumber(v as any);
    return b.isFinite() && !b.isZero();
  } catch { return false; }
}

Try / catch

try {
  mod.run(input, [modulusArg, delim]);
} catch (e) {
  // MOD throws a plain Error, not OperationError
  if (e instanceof Error && /Modulus cannot be zero/.test(e.message)) {
    // fix the modulus and retry
  } else throw e;
}

Prevention

When it happens

Trigger: Running MOD with the Modulus argument set to 0 (or a string BigNumber parses as zero, e.g. '0', '0.0', '-0'). Raised at MOD.mjs:50-52 before any numbers are read from the input.

Common situations: Recipe imported with modulus 0; user cleared the modulus field or typed 0 expecting 'no modulo'; a preceding operation produced 0 and it was wired into the modulus (uncommon, since modulus is an arg not the input).

Related errors


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