← LibraryComputing in the Integers Modulo nEngineering · MathematicsLesson 52/385← PrevNext →
ArticlePublished 7 Aug 20263 min readBy Kevin Jogin

Engineering  /  Mathematics  — Integer Algorithms

Computing in the Integers Modulo n

Implementing arithmetic in Z_n: representative choice, reduction after each operation, inversion, and the cost of each primitive.

Page KV-MATH-0323Reading time 3 minReviewed 2026-08-07Author Kevin Jogin

Executive summary

Working in Z_n means keeping every intermediate reduced. The operations inherit their cost from integer arithmetic plus a reduction, and the discipline of reducing early is what keeps operand size bounded.

Inversion is the expensive primitive and the only one requiring a gcd computation.

Learning objectives

  1. Choose a canonical representative and maintain it.
  2. State the cost of each modular primitive.
  3. Recognise when inversion can be avoided.

01Representatives and reduction

Elements of Z_n are stored as integers in [0, n). Every operation is followed by a reduction restoring that range, which for addition is a conditional subtraction and for multiplication is a division with remainder.

  1. AdditionO(ℓ)Add, then subtract n once if the result reaches n
  2. SubtractionO(ℓ)Subtract, then add n once if negative
  3. MultiplicationO(ℓ²)Multiply to 2ℓ bits, then reduce
  4. InversionO(ℓ²)Extended Euclid; same order but a much larger constant
  5. ExponentiationO(k · ℓ²)k squarings and up to k multiplications

02Inversion and its avoidance

The inverse of a modulo n exists exactly when gcd(a, n) = 1 and is computed by extended Euclid, which returns s with as + nt = 1, so s mod n is the inverse.

For prime moduli Fermat's little theorem offers an alternative: a^{p−2} mod p. This is asymptotically worse — a full exponentiation rather than a gcd — but it is branch-free and constant-time, which matters when resisting timing attacks.

Inversion strategies
MethodCostConstant time?
Extended EuclidO(ℓ²), small constantNo, branches on operand values
Fermat exponentiationO(ℓ³)Yes, with a fixed exponentiation ladder
Batch inversionOne inversion + 3n multiplicationsInherits from the single inversion

03Choosing the modulus representation

Cryptographic implementations rarely store residues in plain form. Montgomery representation multiplies every element by a fixed power of two modulo n, which makes reduction a shift-and-add rather than a division, at the cost of conversion on entry and exit.

  1. Convert in

    Multiply each operand by R mod n, once at the start of a computation.

  2. Operate

    Montgomery multiplication of the transformed values needs no division, only multiplications and shifts.

  3. Convert out

    A single Montgomery reduction at the end recovers the ordinary representative.

The transformation pays for itself whenever more than a handful of modular multiplications share a modulus, which is every exponentiation. For a single multiplication it is a loss.

04Frequently asked questions

Is reducing after every operation always necessary?

Not always, and lazy reduction is a real optimisation. Sums can be allowed to grow while headroom remains in the representation, reducing only before a multiplication. This requires careful bookkeeping of the maximum possible magnitude at each point.

Why is inversion so much more expensive in practice than multiplication?

Because extended Euclid is inherently sequential and data-dependent, with a loop count depending on the operands and poor instruction-level parallelism. Its asymptotic class matches multiplication but its constant is an order of magnitude larger.

Does Montgomery representation change any results?

No, it is a change of representative only. Every value in Montgomery form corresponds to exactly one residue class, and converting out recovers the ordinary answer.

Sources and method

Structural reference: Victor Shoup, A Computational Introduction to Number Theory and Algebra, Version 1, Cambridge University Press, 2005 — book pages 48-51.

This page carries the durable method layer only: definitions, constructions, algorithms, complexity results and selection criteria, authored originally for KEVOS. No text is transcribed or paraphrased from the source, and no numeric tables or benchmark data are reproduced — these are routed to live authoritative sources instead.

Author: Kevin Jogin. Last reviewed 2026-08-07.

Continue learning

Integer Division with RemainderArticle · MathematicsNEXT LESSON →Modular Exponentiation by Repeated SquaringArticle · MathematicsInteger MultiplicationArticle · MathematicsFaster Integer Arithmetic: Karatsuba and BeyondArticle · Mathematics