{
  "id": "modular-multiplicative-inverse",
  "title": "Modular Multiplicative Inverse",
  "lang": "en",
  "source_lang": "en",
  "summary": "A modular multiplicative inverse of an integer is a number whose product with it is congruent to 1 modulo a specified modulus.",
  "category": "mathematics",
  "subcategory": "algebra",
  "type": "concept",
  "aliases": [
    "Multiplicative inverse modulo n",
    "modular inverse"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/modular-multiplicative-inverse/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/modular-multiplicative-inverse/",
      "json": "https://aiwiki.page/zh/modular-multiplicative-inverse.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "A modular multiplicative inverse of an integer \\(a\\) modulo an integer \\(n>1\\) is an integer \\(b\\) such that\n\n\\[ab\\equiv 1\\pmod n.\\]\n\nEquivalently, \\(n\\) divides \\(ab-1\\). Such an inverse exists exactly when \\(a\\) and \\(n\\) are relatively prime, meaning that their greatest common divisor is 1. When it exists, the inverse is unique modulo \\(n\\) and is commonly written \\(a^{-1}\\pmod n\\). Modular inverses make division possible in modular arithmetic and are fundamental to computational number theory and cryptography."
    },
    {
      "heading": "Definition and examples",
      "text": "For example,\n\n\\[3\\cdot 5=15\\equiv 1\\pmod 7,\\]\n\nso 5 is an inverse of 3 modulo 7. Every integer congruent to 5 modulo 7, such as \\(-2\\), 12, or 19, is also an inverse. Thus the inverse is unique as an equivalence class, not as an ordinary integer. A standard representative is chosen from \\(0,1,\\ldots,n-1\\).\n\nBy contrast, 2 has no inverse modulo 6. Every product \\(2b\\) is even, whereas every integer congruent to 1 modulo 6 is odd.\n\nThe notation \\(a^{-1}\\) in this setting does not generally denote the ordinary reciprocal \\(1/a\\) in the rational numbers. It denotes an inverse with respect to multiplication modulo the specified modulus. The modulus is essential: the inverse of 3 modulo 7 is 5, while its inverse modulo 11 is 4."
    },
    {
      "heading": "Existence and uniqueness",
      "text": "The basic criterion is\n\n\\[a \\text{ is invertible modulo }n \\quad\\Longleftrightarrow\\quad \\gcd(a,n)=1.\\]\n\nIf \\(b\\) is an inverse, then \\(ab\\equiv1\\pmod n\\), so there is an integer \\(k\\) such that\n\n\\[ab-kn=1.\\]\n\nAny common divisor of \\(a\\) and \\(n\\) must therefore divide 1, proving that their greatest common divisor is 1.\n\nConversely, Bézout’s identity states that if \\(\\gcd(a,n)=1\\), there are integers \\(x\\) and \\(y\\) satisfying\n\n\\[ax+ny=1.\\]\n\nReducing this equation modulo \\(n\\) gives \\(ax\\equiv1\\pmod n\\), so \\(x\\) is an inverse.\n\nFor uniqueness, suppose \\(ab\\equiv1\\pmod n\\) and \\(ac\\equiv1\\pmod n\\). Multiplying the second congruence by \\(b\\) and using \\(ba\\equiv1\\pmod n\\) gives \\(c\\equiv b\\pmod n\\).\n\nThese statements concern multiplicative inverses. Every residue has an additive inverse, namely \\(-a\\pmod n\\), whether or not it has a multiplicative inverse."
    },
    {
      "heading": "Computing modular inverses",
      "text": "Extended Euclidean algorithm\n\nThe standard general-purpose method is the extended Euclidean algorithm. It augments the Euclidean algorithm by computing coefficients \\(x,y\\) such that\n\n\\[ax+ny=\\gcd(a,n).\\]\n\nIf the greatest common divisor is 1, the coefficient \\(x\\), reduced modulo \\(n\\), is the required inverse. Otherwise, no inverse exists.\n\nFor example, to invert 17 modulo 43:\n\n\\[\\begin{aligned} 43&=2\\cdot17+9,\\\\ 17&=1\\cdot9+8,\\\\ 9&=1\\cdot8+1. \\end{aligned}\\]\n\nBack-substitution gives\n\n\\[\\begin{aligned} 1&=9-8\\\\ &=2\\cdot9-17\\\\ &=2\\cdot43-5\\cdot17. \\end{aligned}\\]\n\nHence\n\n\\[17^{-1}\\equiv-5\\equiv38\\pmod{43}.\\]\n\nAfter reducing \\(a\\) modulo \\(n\\), the algorithm takes \\(O(\\log n)\\) division steps. This is an arithmetic-operation count; its bit complexity also depends on the methods used for arithmetic on large integers.\n\nExponentiation\n\nFor a prime modulus \\(p\\), Fermat’s little theorem gives\n\n\\[a^{p-1}\\equiv1\\pmod p\\]\n\nwhenever \\(p\\) does not divide \\(a\\). Therefore,\n\n\\[a^{-1}\\equiv a^{p-2}\\pmod p.\\]\n\nFor \\(p=2\\), the only nonzero residue is 1, which is its own inverse.\n\nMore generally, Euler’s theorem gives\n\n\\[a^{\\varphi(n)}\\equiv1\\pmod n\\]\n\nwhen \\(\\gcd(a,n)=1\\), where Euler’s totient function \\(\\varphi(n)\\) counts the invertible residue classes modulo \\(n\\). Consequently,\n\n\\[a^{-1}\\equiv a^{\\varphi(n)-1}\\pmod n.\\]\n\nRepeated squaring computes these powers efficiently. For a general composite modulus, however, finding \\(\\varphi(n)\\) may require information not already available, such as a factorization of \\(n\\). The extended Euclidean algorithm requires no factorization.\n\nLifting an inverse to higher powers\n\nAn inverse modulo a prime power can be refined using a modular analogue of Newton’s method. If\n\n\\[ax\\equiv1\\pmod{p^k},\\]\n\ndefine\n\n\\[x'=x(2-ax).\\]\n\nThen\n\n\\[1-ax'=(1-ax)^2,\\]\n\nso\n\n\\[ax'\\equiv1\\pmod{p^{2k}}.\\]\n\nEach refinement doubles the exponent of the modulus to which the inverse is valid. This is useful for arithmetic modulo large prime powers, including powers of 2."
    },
    {
      "heading": "Modular division and linear congruences",
      "text": "If \\(a\\) is invertible modulo \\(n\\), the congruence\n\n\\[ax\\equiv b\\pmod n\\]\n\nhas the unique solution\n\n\\[x\\equiv a^{-1}b\\pmod n.\\]\n\nThis is the modular counterpart of dividing both sides of an equation by \\(a\\).\n\nWithout an inverse, division cannot be performed in this way. Nevertheless, the congruence may still have solutions. Let \\(d=\\gcd(a,n)\\). Then:\n\n- If \\(d\\) does not divide \\(b\\), there are no solutions.\n- If \\(d\\) divides \\(b\\), there are exactly \\(d\\) distinct solutions modulo \\(n\\).\n\nIn the second case, dividing the integer coefficients and modulus by \\(d\\) gives\n\n\\[\\frac{a}{d}x\\equiv\\frac{b}{d}\\pmod{n/d},\\]\n\nwhose coefficient is invertible modulo \\(n/d\\).\n\nFor example,\n\n\\[6x\\equiv8\\pmod{14}\\]\n\nreduces to\n\n\\[3x\\equiv4\\pmod7.\\]\n\nSince \\(3^{-1}\\equiv5\\pmod7\\), this gives \\(x\\equiv6\\pmod7\\), corresponding to the two solutions \\(x\\equiv6\\) and \\(x\\equiv13\\pmod{14}\\).\n\nThis distinction also governs cancellation: cancelling a factor that is not invertible generally requires changing the modulus."
    },
    {
      "heading": "Algebraic interpretation",
      "text": "The residue classes modulo \\(n\\) form the ring \\(\\mathbb Z/n\\mathbb Z\\). Its invertible elements are called units. Under multiplication they form an abelian group, written\n\n\\[(\\mathbb Z/n\\mathbb Z)^\\times,\\]\n\nwith \\(\\varphi(n)\\) elements.\n\nIf \\(n=p\\) is prime, every nonzero residue is invertible, and \\(\\mathbb Z/p\\mathbb Z\\) is a finite field. If \\(n\\) is composite, some nonzero residues are not invertible. Such residues are zero divisors: if \\(d=\\gcd(a,n)>1\\) and \\(a\\not\\equiv0\\pmod n\\), then the nonzero residue \\(n/d\\) satisfies\n\n\\[a(n/d)\\equiv0\\pmod n.\\]\n\nThe Chinese remainder theorem gives a componentwise description. If \\(n=rs\\) with \\(\\gcd(r,s)=1\\), then \\(a\\) is invertible modulo \\(n\\) exactly when it is invertible modulo both \\(r\\) and \\(s\\). Its inverse modulo \\(n\\) can be reconstructed from those two inverses."
    },
    {
      "heading": "Applications and implementation considerations",
      "text": "Modular inverses occur throughout exact computation:\n\n- Cryptography: In RSA, the private exponent is chosen as an inverse of the public exponent modulo an appropriate exponent modulus.\n- Reconstruction from residues: Constructive formulas for the Chinese remainder theorem use inverses to combine congruences.\n- Linear algebra: A matrix over \\(\\mathbb Z/n\\mathbb Z\\) is invertible exactly when its determinant is a unit. For an integer matrix \\(A\\), this means \\(\\gcd(\\det A,n)=1\\).\n- Polynomial computation: Division and interpolation over finite fields use multiplicative inverses of nonzero coefficients and evaluation-point differences.\n- Batch inversion: A collection of invertible residues can be inverted using one inversion and a linear number of multiplications, by forming prefix products and then working backward.\n\nImplementations must distinguish failure of invertibility from a valid result. Negative coefficients returned by the extended Euclidean algorithm are normally reduced to a standard nonnegative residue. Fixed-width arithmetic must also account for overflow in intermediate products.\n\nIn cryptographic computations involving secrets, the timing and memory-access patterns of an inversion algorithm can matter. Ordinary Euclidean implementations may follow data-dependent execution paths, so cryptographic implementations may instead use constant-time inversion methods."
    }
  ],
  "links": [
    "integer",
    "greatest-common-divisor",
    "modular-arithmetic",
    "number-theory",
    "equivalence-class",
    "rational-number",
    "bezouts-identity",
    "extended-euclidean-algorithm",
    "euclidean-algorithm",
    "prime-number",
    "fermats-little-theorem",
    "eulers-theorem-number-theory",
    "eulers-totient-function",
    "newtons-method",
    "ring-mathematics",
    "abelian-group",
    "finite-field",
    "chinese-remainder-theorem",
    "rsa-cryptosystem",
    "matrix",
    "determinant",
    "interpolation"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}