Skip to content

PowerMod ​

Modular exponentiation: a^b mod m, computed without forming a^b directly.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaModular exponentiationWikidataQ1228841Wolfram LanguagePowerMod!Rosetta CodeModular exponentiation
PowerMod(a, b, m)modular exponentiation, ab mod ma^b \bmod m.
SageMathenumeratio_power_mod(a, b, m)SymPypow(ZZ.convert(a), ZZ.convert(b), ZZ.convert(m))Oscarpowermod(ZZ(a), ZZ(b), ZZ(m))Juliapowermod(ZZ(a), ZZ(b), ZZ(m))Rustpowermod(a, b, m)
PowerMod(a, 1/r, m)the least x≥0x \ge 0 with xr≡a(modm)x^r \equiv a \pmod m; more generally s/rs/r for xr≡asx^r \equiv a^s
SageMathenumeratio_power_mod(a, 1/r, m)SymPypow(ZZ.convert(a), ZZ.convert(1/r), ZZ.convert(m))Oscarpowermod(ZZ(a), ZZ(1/r), ZZ(m))Juliapowermod(ZZ(a), ZZ(1/r), ZZ(m))Rustpowermod(a, 1/r, m)
PowerMod(u/v, b, m)a rational base, read in Z/m\mathbb{Z}/m as u⋅v−1u \cdot v^{-1}, a list of bases, and an exponent of 0 at any modulus
SageMathenumeratio_power_mod(u/v, b, m)SymPypow(ZZ.convert(u/v), ZZ.convert(b), ZZ.convert(m))Oscarpowermod(ZZ(u/v), ZZ(b), ZZ(m))Juliapowermod(ZZ(u/v), ZZ(b), ZZ(m))Rustpowermod(u/v, b, m)
Typed signatures
TypePackageApplies
(integer, rational, integer) -> integercompute-enginealways
(number, number, number) -> numbernumber-theoryreplaces residuesalways
(number, number, number) -> numberresiduesreplaces compute-enginealways

Domain: Modular arithmetic

Details
  • Computed by repeated squaring, without ever forming aba^b directly -- efficient even for huge bb.
  • A negative bb gives the modular inverse of aa raised to ∣b∣|b|, when it exists.
  • The inverse is undefined whenever gcd⁡(a,m)≠1\gcd(a,m)\neq1; compute-engine leaves such calls unevaluated.
  • Equal to Mod(ab,m)\mathrm{Mod}(a^b, m) for positive bb, just far more efficient. See Mod.
  • A rational exponent s/rs/r gives the least xx with xr≡asx^r \equiv a^s — the first element of PowerModList — and stays unevaluated when there is none.
  • Threads over lists in any argument.
  • Gaussian integers are reduced as Mod reduces them; a rational-integer modulus must be positive, and a result that comes out real is reported in [0,m)[0, m).

Plot

Examples

32 more kept as data, checked against the oracles
Basic #
live ↗
InPowerMod(2,10,3)\mathrm{PowerMod}(2, 10, 3)Out11
live ↗
InPowerMod(3,50,11)\mathrm{PowerMod}(3, 50, 11)Out11
live ↗
InPowerMod(3,2,7)\mathrm{PowerMod}(3, 2, 7)Out22

A negative exponent gives the modular inverse

live ↗
InPowerMod(2,−1,7)\mathrm{PowerMod}(2, -1, 7)Out44
Scope #

3−1≡53^{-1} \equiv 5, and 52=25≡45^2 = 25 \equiv 4

live ↗
InPowerMod(3,−2,7)\mathrm{PowerMod}(3, -2, 7)Out44

a square root of 3≡1(mod2)3 \equiv 1 \pmod 2

live ↗
InPowerMod(3,12,2)\mathrm{PowerMod}(3, \frac{1}{2}, 2)Out11

threads over a list of bases

live ↗
InPowerMod([2,3,4],2,5)\mathrm{PowerMod}(\bigl\lbrack2, 3, 4\bigr\rbrack, 2, 5)Out[4,4,1]\bigl\lbrack4, 4, 1\bigr\rbrack

a0=1a^0 = 1 regardless of aa or mm's sign, so this is Mod(1,−14)\mathrm{Mod}(1, -14) even though m<0m<0

live ↗
InPowerMod(−2,0,−14)\mathrm{PowerMod}(-2, 0, -14)Out−13-13

a rational exponent is a modular root: the least of 2,52, 5

live ↗
InPowerMod(4,12,7)\mathrm{PowerMod}(4, \frac{1}{2}, 7)Out22

threads over lists; the period is the order of 2 mod 5

live ↗
InPowerMod(2,[10,11,12,13,14],5)\mathrm{PowerMod}(2, \bigl\lbrack10, 11, 12, 13, 14\bigr\rbrack, 5)Out[4,3,1,2,4]\bigl\lbrack4, 3, 1, 2, 4\bigr\rbrack

a 301-digit base

live ↗
InPowerMod(10300+1,7,5)\mathrm{PowerMod}(10^{300}+1, 7, 5)Out11

a square root of 3 modulo the prime 1030+5710^{30} + 57

live ↗
InPowerMod(3,12,1030+57)\mathrm{PowerMod}(3, \frac{1}{2}, 10^{30}+57)Out492 767 688 934 650 018 614 948 489 645492\,767\,688\,934\,650\,018\,614\,948\,489\,645

a rational base: 2⋅3−1=2⋅5≡3(mod7)2 \cdot 3^{-1} = 2 \cdot 5 \equiv 3 \pmod 7

live ↗
InPowerMod(23,1,7)\mathrm{PowerMod}(\frac{2}{3}, 1, 7)Out33

Gaussian integers: (2+i)2=3+4i≡i(mod3)(2+i)^2 = 3 + 4i \equiv i \pmod 3

live ↗
InPowerMod(2+i,2,3)\mathrm{PowerMod}(2+i, 2, 3)Outii

a Gaussian modulus, and an exponent of 103010^{30}

live ↗
InPowerMod(1+2i,1030,7+2i)\mathrm{PowerMod}(1+2i, 10^{30}, 7+2i)Out1+2i1+2i

11−7i11 - 7i is a unit mod 7+4i7 + 4i, so it has negative powers

live ↗
InPowerMod(11−7i,−4,7+4i)\mathrm{PowerMod}(11-7i, -4, 7+4i)Out−2−i-2-i

Sum of squares mod 77 over a residue range

live ↗
In∑a7PowerMod(a,2,7)\sum_{a}^7\mathrm{PowerMod}(a, 2, 7)Out1414
live ↗
InFindInstance(PowerMod(a,b,17)=1∧0<a<17∧0<b<17,[a,b],Z,2)\mathrm{FindInstance}(\mathrm{PowerMod}(a, b, 17)=1∧0\lt a\lt17∧0\lt b\lt17, \bigl\lbrack a, b\bigr\rbrack, ℤ, 2)Out[[Rule(a,1),Rule(b,1)],[Rule(a,1),Rule(b,2)]]\bigl\lbrack\bigl\lbrack\mathrm{Rule}(a, 1), \mathrm{Rule}(b, 1)\bigr\rbrack, \bigl\lbrack\mathrm{Rule}(a, 1), \mathrm{Rule}(b, 2)\bigr\rbrack\bigr\rbrack
Applications #

One step of RSA-style modular exponentiation: 713 mod 337^{13} \bmod 33

live ↗
InPowerMod(7,13,33)\mathrm{PowerMod}(7, 13, 33)Out1313

341=11⋅31341 = 11 \cdot 31 passes Fermat's test to base 2 — the least pseudoprime

live ↗
InPowerMod(2,340,341)\mathrm{PowerMod}(2, 340, 341)Out11

Diffie–Hellman: both parties reach the shared key 56⋅15 mod 235^{6 \cdot 15} \bmod 23

live ↗
InPowerMod(PowerMod(5,6,23),15,23)=PowerMod(PowerMod(5,15,23),6,23)\mathrm{PowerMod}(\mathrm{PowerMod}(5, 6, 23), 15, 23)=\mathrm{PowerMod}(\mathrm{PowerMod}(5, 15, 23), 6, 23)OutTrue\mathrm{True}
Properties #

By definition ab mod ma^b \bmod m, computed without ever forming aba^b directly

live ↗
InPowerMod(2,10,3)=210 mod 3\mathrm{PowerMod}(2, 10, 3)=2^{10}\bmod3OutTrue\mathrm{True}

Euler's theorem: aφ(m)≡1a^{\varphi(m)} \equiv 1 for aa coprime to mm

live ↗
InPowerMod(7,Totient(19),19)\mathrm{PowerMod}(7, \mathrm{Totient}(19), 19)Out11

Congruent bases (7≡127\equiv12) give the same power

live ↗
In[PowerMod(7,2,5),PowerMod(12,2,5)]\bigl\lbrack\mathrm{PowerMod}(7, 2, 5), \mathrm{PowerMod}(12, 2, 5)\bigr\rbrackOut[4,4]\bigl\lbrack4, 4\bigr\rbrack

Same as reducing 232^3 afterwards

live ↗
In[PowerMod(2,3,5),8 mod 5]\bigl\lbrack\mathrm{PowerMod}(2, 3, 5), 8\bmod5\bigr\rbrackOut[3,3]\bigl\lbrack3, 3\bigr\rbrack

Exponent −1-1 gives the modular inverse

live ↗
In[PowerMod(2,−1,5),ModularInverse(2,5)]\bigl\lbrack\mathrm{PowerMod}(2, -1, 5), \mathrm{ModularInverse}(2, 5)\bigr\rbrackOut[3,3]\bigl\lbrack3, 3\bigr\rbrack

Modulo 2, an even base gives 00

live ↗
In[2∣8,PowerMod(8,3,2)=0]\bigl\lbrack2\mid8, \mathrm{PowerMod}(8, 3, 2)=0\bigr\rbrackOut[True,True]\bigl\lbrack\mathrm{True}, \mathrm{True}\bigr\rbrack

Euler: aφ(n)≡1a^{\varphi(n)}\equiv1 for coprime aa

live ↗
InPowerMod(7,Totient(19),19)=1\mathrm{PowerMod}(7, \mathrm{Totient}(19), 19)=1OutTrue\mathrm{True}

Fermat: 216≡1(mod17)2^{16}\equiv1 \pmod{17}

live ↗
InPowerMod(2,16,17)\mathrm{PowerMod}(2, 16, 17)Out11

Threads over a list of bases

live ↗
InPowerMod([5,−5],2,3)\mathrm{PowerMod}(\bigl\lbrack5, -5\bigr\rbrack, 2, 3)Out[1,1]\bigl\lbrack1, 1\bigr\rbrack

Negative modulus: result stays unevaluated per element

live ↗
InPowerMod([5,−5],2,−3)\mathrm{PowerMod}(\bigl\lbrack5, -5\bigr\rbrack, 2, -3)Out[PowerMod(5,2,−3),PowerMod(−5,2,−3)]\bigl\lbrack\mathrm{PowerMod}(5, 2, -3), \mathrm{PowerMod}(-5, 2, -3)\bigr\rbrack
Possible issues #

No inverse exists when gcd⁡(a,m)≠1\gcd(a,m)\neq1 (here gcd⁡(2,4)=2\gcd(2,4)=2), so the call is left unevaluated

live ↗
InPowerMod(2,−1,4)\mathrm{PowerMod}(2, -1, 4)OutPowerMod(2,−1,4)\mathrm{PowerMod}(2, -1, 4)

A nonzero exponent with a negative modulus still declines, as in Wolfram, unlike the exponent-0 case above

live ↗
InPowerMod(−2,3,−14)\mathrm{PowerMod}(-2, 3, -14)OutPowerMod(−2,3,−14)\mathrm{PowerMod}(-2, 3, -14)

2 is not a square mod 5, so there is no root

live ↗
InPowerMod(2,12,5)\mathrm{PowerMod}(2, \frac{1}{2}, 5)OutPowerMod(2,12,5)\mathrm{PowerMod}(2, \frac{1}{2}, 5)
About
Examples66
Checked againstWolfram Language 60 agree6 differSageMath 48 agree2 differ6 unevaluated8 errorSymPy 29 agree1 differ7 unevaluated27 errorOscar 18 agreeJulia 22 agree42 errorRust 23 agree1 differ6 unevaluated7 error
CrosswalkWikipediaWikidataWolfram LanguageSageMathSymPyOscarJuliaRustRosetta Code

Implementation

mappedoscarpackages/oracle/src/mappings.ts
mappedwolframpackages/oracle/src/mappings.ts

For a negative exponent and a Gaussian modulus m, Wolfram checks invertibility of a mod N(m) (a rational integer) rather than mod m itself, so it occasionally declines a case ours answers — e.g. PowerMod[11 - 7I, -4, 7 + 4I] errors since 11 - 7i shares a factor with N(7 + 4i) = 65, though gcd(11 - 7i, 7 + 4i) = 1.

mappedsagepackages/oracle/src/mappings.ts

Sage's power_mod takes a negative exponent, like ours; no rational base or exponent. The helper reduces into the sign of the modulus and runs over the Gaussian integers, where power_mod has no method.

mappedrustpackages/oracle/src/mappings.ts

Sage's power_mod takes a negative exponent, like ours; no rational base or exponent.

mappedsympypackages/oracle/src/mappings.ts

SymPy has no PowerMod function; this is Python's three-argument pow over SymPy's ZZ integers (pure-Python ones, as the runner pins), which takes a negative exponent through the modular inverse and gives the result the sign of the modulus, where ours declines a modulus below 1 unless the exponent is 0. The emit varies with the operands: a rational exponent s/r is the least root of x^r = a^s through nthroot_mod, as PowerModList (None where there is no root); a symbolic, rational or Gaussian operand goes to pow on SymPy numbers, which holds it (Mod((2 + I)**2, 3)) or ignores the modulus for a rational base. A list threads over whichever argument holds it.

mappedjuliapackages/oracle/src/mappings.ts

See also: Mod, PowerModList, ModularInverse