← LibraryModular Inversion and Simultaneous InversionEngineering · MathematicsLesson 224/385← PrevNext →
ArticlePublished 7 Aug 20262 min readBy Kevin Joginmodular inversesimultaneous inversionMontgomery trickbatch inversion

Euclidean Algorithms and Congruences

Modular Inversion and Simultaneous Inversion

Computing modular inverses, and Montgomery's trick for inverting many elements at the cost of one inversion plus multiplications.

Engineering / MathematicsEuclidean Algorithms and Congruences2 min readKV-MATH-0522

Modular inversion is substantially more expensive than modular multiplication. When many inverses are needed, Montgomery's simultaneous inversion trick replaces all but one of them with multiplications — a decisive saving in elliptic curve work.

Single inversion

The inverse of a modulo N is obtained from the extended Euclidean algorithm. An alternative for prime moduli is exponentiation by p - 2, using Fermat's little theorem.

Single modular inversion methods
MethodCostApplicability
Extended EuclideanRoughly a GCDAny modulus
ExponentiationAbout log p multiplicationsPrime modulus only
Binary extendedShifts and subtractionsAny odd modulus; good without fast division

Simultaneous inversion

Montgomery's trick inverts k elements using one inversion and about 3k multiplications.

Montgomery's simultaneous inversion

  1. Accumulate prefixesCompute running products of the inputs, storing each partial product.
  2. Invert onceInvert the final total product — the only inversion performed.
  3. UnwindWalk backwards, recovering each individual inverse by multiplying the running inverse by the stored prefix, then removing that element from the running inverse.
Cost = 1 inversion + about 3k multiplicationsAgainst k inversions for the naive approach.

The failure mode is the point

Avoiding inversion entirely

Projective coordinates on elliptic curves defer inversion by carrying a denominator through the group law, requiring only one inversion at the very end. This is the standard approach where inversion is expensive — see elliptic curve arithmetic modulo N.

Source. Henri Cohen, A Course in Computational Algebraic Number Theory, Springer GTM 138 — 1.3.3. Structural reference unverified: the source file was not available during authoring; chapter and section numbers are taken from the published edition and have not been checked against a physical copy.

Continue learning

Solving Quadratic CongruencesArticle · MathematicsNEXT LESSON →Finite Field Element RepresentationArticle · MathematicsSquare Roots Modulo a Prime: the Shanks-Tonelli AlgorithmArticle · MathematicsFinite Field Multiplication and InversionArticle · Mathematics