{
  "id": "modular-exponentiation",
  "title": "Modular Exponentiation",
  "lang": "en",
  "source_lang": "en",
  "summary": "Modular exponentiation computes the remainder of an integer power without constructing the full power, enabling efficient calculations in number theory and cryptography.",
  "category": "mathematics",
  "subcategory": "algebra",
  "type": "concept",
  "aliases": [
    "Modular powering",
    "exponentiation modulo an integer"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/modular-exponentiation/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/modular-exponentiation/",
      "json": "https://aiwiki.page/zh/modular-exponentiation.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Modular exponentiation is the computation of an integer power followed by reduction modulo a positive integer. Given a base \\(a\\), a nonnegative exponent \\(e\\), and a modulus \\(m\\), it finds the unique integer \\(r\\) satisfying\n\n\\[r\\equiv a^e\\pmod m,\\qquad 0\\le r<m.\\]\n\nIts defining computational advantage is that intermediate results can be reduced modulo \\(m\\), avoiding construction of the potentially enormous integer \\(a^e\\). It is a fundamental operation in number theory, computational mathematics, and public-key cryptography."
    },
    {
      "heading": "Definition and basic properties",
      "text": "In modular arithmetic, integers that differ by a multiple of \\(m\\) represent the same residue class. Modular exponentiation depends only on the residue class of the base:\n\n\\[a\\equiv b\\pmod m \\quad\\Longrightarrow\\quad a^e\\equiv b^e\\pmod m\\]\n\nfor every nonnegative integer \\(e\\).\n\nThe usual exponent laws remain valid:\n\n\\[a^{u+v}\\equiv a^u a^v\\pmod m, \\qquad a^{uv}\\equiv (a^u)^v\\pmod m.\\]\n\nIn particular, the identity\n\n\\[(xy)\\bmod m = \\bigl((x\\bmod m)(y\\bmod m)\\bigr)\\bmod m\\]\n\nallows reduction after every multiplication.\n\nFor \\(e=0\\), modular exponentiation algorithms use the empty-product convention \\(a^0=1\\), including when \\(a=0\\). The returned residue is therefore \\(1\\bmod m\\). If \\(m=1\\), every result is \\(0\\). A modulus of zero is not permitted.\n\nFor example,\n\n\\[7^4=2401\\equiv1\\pmod{100}.\\]\n\nConsequently,\n\n\\[7^{100}\\equiv(7^4)^{25}\\equiv1\\pmod{100},\\]\n\nwithout needing to construct the 85-digit integer \\(7^{100}\\)."
    },
    {
      "heading": "Binary exponentiation",
      "text": "The standard efficient algorithm is binary exponentiation, also called exponentiation by squaring or square-and-multiply. It uses the identities\n\n\\[a^{2k}=(a^k)^2,\\qquad a^{2k+1}=a(a^k)^2.\\]\n\nRather than performing approximately \\(e\\) multiplications, it processes the digits of \\(e\\) in binary notation, requiring only a number of modular multiplications proportional to the bit length of \\(e\\).\n\nA right-to-left version is:\n\nmodular_power(a, e, m):\n    require e >= 0 and m >= 1\n    result = 1 mod m\n    base = a mod m\n\n    while e > 0:\n        if e is odd:\n            result = (result * base) mod m\n        e = floor(e / 2)\n        base = (base * base) mod m\n\n    return result\n\nAt the start of each iteration, the loop maintains\n\n\\[\\text{result}\\cdot\\text{base}^{\\,e} \\equiv a_0^{e_0}\\pmod m,\\]\n\nwhere \\(a_0\\) and \\(e_0\\) are the original inputs. When the remaining exponent becomes zero, this invariant establishes that result is the required residue. A formal correctness argument follows by mathematical induction over the iterations.\n\nA left-to-right version scans exponent bits from most significant to least significant. Starting with \\(r=1\\bmod m\\), it squares \\(r\\) for each bit and then multiplies by \\(a\\) if that bit is \\(1\\).\n\nWorked example\n\nTo compute \\(7^{13}\\bmod23\\), write\n\n\\[13=8+4+1.\\]\n\nSuccessive squaring gives\n\n\\[\\begin{aligned} 7^1&\\equiv7\\pmod{23},\\\\ 7^2&\\equiv49\\equiv3\\pmod{23},\\\\ 7^4&\\equiv3^2\\equiv9\\pmod{23},\\\\ 7^8&\\equiv9^2\\equiv12\\pmod{23}. \\end{aligned}\\]\n\nMultiplying the powers corresponding to the nonzero binary digits yields\n\n\\[7^{13}\\equiv12\\cdot9\\cdot7\\equiv20\\pmod{23}.\\]"
    },
    {
      "heading": "Computational complexity",
      "text": "For \\(e>0\\), let\n\n\\[L=\\lfloor\\log_2 e\\rfloor+1\\]\n\nbe the exponent’s bit length. Binary exponentiation uses \\(O(L)\\) modular multiplications, in big-O notation. An implementation that initializes its accumulator with the leading power can use \\(L-1\\) squarings and one additional multiplication for each remaining nonzero exponent bit.\n\nThis operation count is not the same as bit complexity. Let \\(n\\) be the bit length of \\(m\\). Reduced residues occupy at most \\(n\\) bits, while their unreduced products can occupy approximately \\(2n\\) bits. With schoolbook multiplication and conventional division-based reduction, each modular multiplication costs \\(O(n^2)\\) bit operations, giving an overall bound of\n\n\\[O(Ln^2),\\]\n\napart from initial input reduction.\n\nMore generally, if \\(C_{\\mathrm{mod}}(n)\\) is the cost of multiplying and reducing two \\(n\\)-bit residues, the running time is\n\n\\[O\\!\\left(L\\,C_{\\mathrm{mod}}(n)\\right).\\]\n\nFaster multiplication and reduction methods improve this cost.\n\nThe basic algorithm stores only a constant number of large integers, although it also needs access to the exponent and temporary workspace. Window-based variants trade additional storage and precomputation for fewer multiplications."
    },
    {
      "heading": "Faster implementation methods",
      "text": "Addition chains and window methods\n\nAn addition chain for \\(e\\) is a sequence beginning with \\(1\\) and ending with \\(e\\), in which each new term is the sum of two earlier terms. Each term corresponds to a power that can be obtained by multiplying two previously computed powers.\n\nBinary exponentiation provides one such chain, but not always the shortest. For example, the chain\n\n\\[1,2,3,6,12,15\\]\n\ncomputes \\(a^{15}\\) in five multiplications, whereas the straightforward binary chain takes six.\n\nFixed-window and sliding-window methods process several exponent bits together and precompute selected powers of the base. They can reduce the multiplication count for large exponents, especially when precomputed values can be reused.\n\nModular reduction\n\nMontgomery multiplication replaces repeated division by the modulus with operations in a transformed representation. It requires an auxiliary radix coprime to \\(m\\). With a power-of-two radix, this means that \\(m\\) must be odd.\n\nBarrett reduction instead uses a precomputed approximation to the reciprocal of the modulus to accelerate reduction. It does not require an odd modulus.\n\nBoth techniques are particularly useful when many multiplications share the same modulus, as happens within a modular exponentiation."
    },
    {
      "heading": "Exponent reduction and periodicity",
      "text": "Reducing the base modulo \\(m\\) is always valid. Reducing the exponent requires additional conditions.\n\nIf the greatest common divisor satisfies \\(\\gcd(a,m)=1\\), Euler’s theorem gives\n\n\\[a^{\\varphi(m)}\\equiv1\\pmod m,\\]\n\nwhere \\(\\varphi(m)\\) counts the residue classes coprime to \\(m\\). Thus, for nonnegative \\(e\\),\n\n\\[a^e\\equiv a^{\\,e\\bmod\\varphi(m)}\\pmod m.\\]\n\nFor a prime modulus \\(p\\) with \\(p\\nmid a\\), Fermat’s little theorem specializes this to\n\n\\[a^{p-1}\\equiv1\\pmod p.\\]\n\nMore precisely, the powers of an invertible residue repeat with period equal to its multiplicative order: the smallest positive integer \\(t\\) such that \\(a^t\\equiv1\\pmod m\\). This is an instance of the order of an element in group theory.\n\nIf \\(a\\) and \\(m\\) are not coprime, reduction modulo \\(\\varphi(m)\\) can fail. For example,\n\n\\[2^4\\bmod8=0,\\]\n\nbut reducing the exponent modulo \\(\\varphi(8)=4\\) would incorrectly give\n\n\\[2^0\\bmod8=1.\\]\n\nFor any fixed base and modulus, the sequence of nonnegative powers is eventually periodic, but it need not be periodic from exponent zero.\n\nWhen a modulus is decomposed into pairwise coprime factors, powers can be computed separately modulo those factors and recombined using the Chinese remainder theorem."
    },
    {
      "heading": "Negative exponents",
      "text": "A negative exponent is defined modulo \\(m>1\\) only when the base has a multiplicative inverse. Such an inverse exists exactly when\n\n\\[\\gcd(a,m)=1.\\]\n\nWriting the inverse as \\(a^{-1}\\), one defines\n\n\\[a^{-k}\\bmod m=(a^{-1})^k\\bmod m\\]\n\nfor positive \\(k\\).\n\nThe inverse can be found using the extended Euclidean algorithm. For example,\n\n\\[3^{-1}\\equiv5\\pmod7\\]\n\nbecause \\(3\\cdot5\\equiv1\\pmod7\\), so\n\n\\[3^{-2}\\equiv5^2\\equiv4\\pmod7.\\]\n\nIf the inverse does not exist, an ordinary single-valued modular power with a negative exponent is undefined."
    },
    {
      "heading": "Applications and security",
      "text": "Modular exponentiation is central to several computational tasks:\n\n- Public-key cryptography. RSA uses exponentiation modulo a product of large primes. Diffie–Hellman key exchange in multiplicative groups uses powers of a shared base modulo a suitable prime.\n- Primality testing. Tests such as the Miller–Rabin test examine modular powers for properties expected of prime moduli.\n- Computational number theory. Modular powers occur in factorization methods, order computations, and calculations in finite fields.\n- Discrete logarithms. Computing \\(g^x\\bmod m\\) is efficient, while recovering \\(x\\) from an appropriate group element can be difficult in suitably chosen groups. This contrast underlies discrete-logarithm-based cryptographic systems.\n\nA mathematically correct implementation is not necessarily secure when an exponent is secret. Ordinary square-and-multiply performs different operations for different exponent bits; timing, power consumption, electromagnetic emissions, or memory-access patterns may reveal information about those bits.\n\nCryptographic implementations therefore use carefully designed operation schedules, constant-time table selection, and, where appropriate, blinding. A fixed multiplication count alone does not establish resistance to leakage: the underlying multiplication, reduction, and memory operations must also be considered.\n\nInteger representation matters as well. In fixed-width machine arithmetic, a product may overflow before the reduction is performed. Implementations use suitably wide intermediates, multiprecision arithmetic, or modular multiplication procedures that avoid overflow. Computing a large integer power approximately with floating-point arithmetic and then taking its remainder is not a substitute for exact modular exponentiation."
    },
    {
      "heading": "Algebraic generalizations",
      "text": "Exponentiation by squaring relies on associativity and an identity element, not specifically on integer multiplication. The same method therefore applies to elements of a ring, including matrices and polynomials reduced modulo another polynomial.\n\nFor matrices with entries reduced modulo \\(m\\), binary powering efficiently computes \\(A^e\\). This supports calculations involving recurrence relations and repeated discrete transformations. Unlike scalars, matrices need not commute with one another, but powers of one fixed matrix still satisfy the identities required by the algorithm."
    }
  ],
  "links": [
    "integer",
    "number-theory",
    "modular-arithmetic",
    "algorithm",
    "binary-number",
    "mathematical-induction",
    "big-o-notation",
    "greatest-common-divisor",
    "eulers-theorem-number-theory",
    "prime-number",
    "fermats-little-theorem",
    "group-theory",
    "chinese-remainder-theorem",
    "euclidean-algorithm",
    "rsa-cryptosystem",
    "finite-field",
    "floating-point-arithmetic",
    "ring-mathematics",
    "matrix",
    "recurrence-relation"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}