Skip to content

PowerModList ​

Every xx in [0,m)[0, m) with xr≡as(modm)x^r \equiv a^s \pmod m — all the values as/ra^{s/r} can take modulo mm.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguagePowerModList!
PowerModList(a, s/r, m)every xx in [0,m)[0, m) with xr≡as(modm)x^r \equiv a^s \pmod m, ascending
SageMathenumeratio_power_mod_list(a, s/r, m)SymPynthroot_mod(pow(ZZ.convert(a), S(s/r).p, ZZ.convert(m)), S(s/r).q, m, True)
PowerModList(a, k, m)an integer exponent gives the single value {ak mod m}\{a^k \bmod m\}
SageMathenumeratio_power_mod_list(a, k, m)SymPynthroot_mod(pow(ZZ.convert(a), S(k).p, ZZ.convert(m)), S(k).q, m, True)
PowerModList(a, -1, m)the modular inverse {a−1}\{a^{-1}\}, or {}\{\} when gcd⁡(a,m)≠1\gcd(a, m) \ne 1
SageMathenumeratio_power_mod_list(a, -1, m)SymPynthroot_mod(pow(ZZ.convert(a), S(-1).p, ZZ.convert(m)), S(-1).q, m, True)
Typed signatures
TypePackageApplies
(integer, rational, integer) -> list<integer>compute-enginealways
(number, number, number) -> list<number>number-theoryreplaces residuesalways
(number, number, number) -> list<number>residuesreplaces compute-enginealways

Domain: Modular arithmetic

Details
  • The problem splits over the prime powers of mm by the Chinese remainder theorem: a root mod mm is one root per channel pep^e, every combination, so the count is the product of the channel counts.
  • Mod a prime pp the units are cyclic of order p−1p - 1, so xr≡bx^r \equiv b has exactly gcd⁡(r,p−1)\gcd(r, p-1) roots or none. One root is built Sylow subgroup by Sylow subgroup — only the primes dividing rr need a discrete log — so p−1p - 1 is never factored and a 30-digit prime costs a millisecond.
  • Roots are Hensel-lifted up each prime power: a root with rxr−1≢0(modp)r x^{r-1} \not\equiv 0 \pmod p lifts uniquely; a singular one (the 2-adic channel of a square root, or p∣xp \mid x) lifts to all pp of its lifts or to none.
  • The modulus has to be factored, by trial division and Pollard's rho under a step budget. A product of two large primes is out of reach, and the call stays unevaluated — which is the whole security of the Rabin cryptosystem.
  • At most 100 000 roots are listed; past that the call stays unevaluated rather than build the list.
  • Beyond Wolfram, a rational a=u/va = u/v with gcd⁡(v,m)=1\gcd(v, m) = 1 is read in Z/m\mathbb{Z}/m as u⋅v−1u \cdot v^{-1}, the image of Z(m)\mathbb{Z}_{(m)}; a denominator sharing a factor with mm has no image, and the list is empty. See RationalReconstruction for the way back.
  • Threads over lists in any argument.
  • Over the Gaussian integers — beyond Wolfram, whose PowerModList takes integers only — mm factors into Gaussian prime powers: a split prime maps onto Z/pe\mathbb{Z}/p^e by i↦−1i \mapsto \sqrt{-1}, an inert pp has residue field Fp2\mathbb{F}_{p^2} and lifts by Hensel, and 1+i1 + i lifts by testing both residues.

Examples

7 more kept as data, checked against the oracles
Basic #

the square roots of 3 modulo 11: 52=255^2 = 25 and 62=366^2 = 36 are both ≡3\equiv 3

live ↗
InPowerModList(3,12,11)\mathrm{PowerModList}(3, \frac{1}{2}, 11)Out[5,6]\bigl\lbrack5, 6\bigr\rbrack

the cube roots of unity modulo 7, since 3∣63 \mid 6

live ↗
InPowerModList(1,13,7)\mathrm{PowerModList}(1, \frac{1}{3}, 7)Out[1,2,4]\bigl\lbrack1, 2, 4\bigr\rbrack

an integer exponent is an ordinary power: 210=10242^{10} = 1024

live ↗
InPowerModList(2,10,1 000)\mathrm{PowerModList}(2, 10, 1\,000)Out[24]\bigl\lbrack24\bigr\rbrack
Scope #

a general exponent s/rs/r: every xx with x2≡23x^2 \equiv 2^3

live ↗
InPowerModList(2,32,17)\mathrm{PowerModList}(2, \frac{3}{2}, 17)Out[5,12]\bigl\lbrack5, 12\bigr\rbrack

the three cube roots of 22=42^2 = 4, since 3∣303 \mid 30

live ↗
InPowerModList(2,23,31)\mathrm{PowerModList}(2, \frac{2}{3}, 31)Out[16,18,28]\bigl\lbrack16, 18, 28\bigr\rbrack

exponent −1-1 is the modular inverse: 3⋅5=15≡13 \cdot 5 = 15 \equiv 1

live ↗
InPowerModList(3,−1,7)\mathrm{PowerModList}(3, -1, 7)Out[5]\bigl\lbrack5\bigr\rbrack

a prime power: the root 22 of x2≡−1(mod5)x^2 \equiv -1 \pmod 5, Hensel-lifted to 545^4

live ↗
InPowerModList(−1,12,625)\mathrm{PowerModList}(-1, \frac{1}{2}, 625)Out[182,443]\bigl\lbrack182, 443\bigr\rbrack

a rational base reads as 2⋅3−12 \cdot 3^{-1} in Z/23\mathbb{Z}/23

live ↗
InPowerModList(23,12,23)\mathrm{PowerModList}(\frac{2}{3}, \frac{1}{2}, 23)Out[4,19]\bigl\lbrack4, 19\bigr\rbrack

threads over lists: the squares mod 5 are exactly 1 and 4

live ↗
InPowerModList([1,2,3,4],12,5)\mathrm{PowerModList}(\bigl\lbrack1, 2, 3, 4\bigr\rbrack, \frac{1}{2}, 5)Out[[1,4],[],[],[2,3]]\bigl\lbrack\bigl\lbrack1, 4\bigr\rbrack, \bigl\lbrack\bigr\rbrack, \bigl\lbrack\bigr\rbrack, \bigl\lbrack2, 3\bigr\rbrack\bigr\rbrack

−1\sqrt{-1} modulo the 31-digit prime 1030+5710^{30} + 57 — no scan, no factoring of p−1p - 1

live ↗
InPowerModList(−1,12,1030+57)\mathrm{PowerModList}(-1, \frac{1}{2}, 10^{30}+57)Out[164 543 371 520 667 882 579 352 850 009,835 456 628 479 332 117 420 647 150 048]\bigl\lbrack164\,543\,371\,520\,667\,882\,579\,352\,850\,009, 835\,456\,628\,479\,332\,117\,420\,647\,150\,048\bigr\rbrack

the cube roots of 2 modulo the Mersenne prime 289−12^{89} - 1; the first is 2302^{30}, since 290=2⋅289≡22^{90} = 2 \cdot 2^{89} \equiv 2

live ↗
InPowerModList(2,13,289−1)\mathrm{PowerModList}(2, \frac{1}{3}, 2^{89}-1)Out[1 073 741 824,205 880 356 524 696 485 265 270 985,413 089 663 117 993 651 110 549 302]\bigl\lbrack1\,073\,741\,824, 205\,880\,356\,524\,696\,485\,265\,270\,985, 413\,089\,663\,117\,993\,651\,110\,549\,302\bigr\rbrack

forty Hensel steps up from −1≡2(mod5)\sqrt{-1} \equiv 2 \pmod 5

live ↗
InPowerModList(−1,12,540)\mathrm{PowerModList}(-1, \frac{1}{2}, 5^{40})Out[2 224 618 918 409 236 552 857 702 057,6 870 328 099 320 045 826 292 688 568]\bigl\lbrack2\,224\,618\,918\,409\,236\,552\,857\,702\,057, 6\,870\,328\,099\,320\,045\,826\,292\,688\,568\bigr\rbrack

the square roots of 1 modulo the product of the first 16 primes: one per ±\pm sign over the 15 odd channels, 2152^{15}

live ↗
InLength(PowerModList(1,12,2×3×5×7×11×13×17×19×23×29×31×37×41×43×47×53))\mathrm{Length}(\mathrm{PowerModList}(1, \frac{1}{2}, 2×3×5×7×11×13×17×19×23×29×31×37×41×43×47×53))Out32 76832\,768

the 1000th roots of unity mod 261−12^{61} - 1: gcd⁡(1000,261−2)=50\gcd(1000, 2^{61} - 2) = 50 of them

live ↗
InLength(PowerModList(1,11 000,261−1))\mathrm{Length}(\mathrm{PowerModList}(1, \frac{1}{1\,000}, 2^{61}-1))Out5050

a square root of ii in Z[i]/(7)\mathbb{Z}[i]/(7), the field of 49 elements: (2+2i)2=8i≡i(2 + 2i)^2 = 8i \equiv i

live ↗
InPowerModList(i,12,7)\mathrm{PowerModList}(i, \frac{1}{2}, 7)Out[−2−2i,2+2i]\bigl\lbrack-2-2i, 2+2i\bigr\rbrack

1020+3910^{20} + 39 is inert, so its residue field is Fp2\mathbb{F}_{p^2} with pp of 21 digits

live ↗
InPowerModList(3+4i,12,1020+39)\mathrm{PowerModList}(3+4i, \frac{1}{2}, 10^{20}+39)Out[−2−i,2+i]\bigl\lbrack-2-i, 2+i\bigr\rbrack
Applications #

the split units of Z/15\mathbb{Z}/15: j1↦4j_1 \mapsto 4 is a ring homomorphism R[j]/(j2−1)→Z/15\mathbb{R}[j]/(j^2 - 1) \to \mathbb{Z}/15

live ↗
InPowerModList(1,12,15)\mathrm{PowerModList}(1, \frac{1}{2}, 15)Out[1,4,11,14]\bigl\lbrack1, 4, 11, 14\bigr\rbrack

so the identity (1+j)(1−j)=0(1+j)(1-j) = 0 transports by j↦4j \mapsto 4

live ↗
In(1+4)(1−4) mod 15(1+4)(1-4)\bmod15Out00

imaginary units of Z/65\mathbb{Z}/65 exist because 55 and 1313 are both ≡1(mod4)\equiv 1 \pmod 4

live ↗
InPowerModList(−1,12,65)\mathrm{PowerModList}(-1, \frac{1}{2}, 65)Out[8,18,47,57]\bigl\lbrack8, 18, 47, 57\bigr\rbrack

Rabin decryption: knowing the factors of nn, the four square roots of the ciphertext include the message

live ↗
InPowerModList(PowerMod(123 456 789,2,1 000 003×1 000 033),12,1 000 003×1 000 033)\mathrm{PowerModList}(\mathrm{PowerMod}(123\,456\,789, 2, 1\,000\,003×1\,000\,033), \frac{1}{2}, 1\,000\,003×1\,000\,033)Out[123 456 789,30 305 547 335,969 730 452 764,999 912 543 310]\bigl\lbrack123\,456\,789, 30\,305\,547\,335, 969\,730\,452\,764, 999\,912\,543\,310\bigr\rbrack
Properties #

mod a prime there are exactly gcd⁡(r,p−1)\gcd(r, p - 1) rr-th roots of unity

live ↗
InLength(PowerModList(1,112,1 009))=gcd⁡(12,1 008)\mathrm{Length}(\mathrm{PowerModList}(1, \frac{1}{12}, 1\,009))=\gcd(12, 1\,008)OutTrue\mathrm{True}

for odd mm the square roots of 1 are the 2ω(m)2^{\omega(m)} sign vectors over the CRT channels: 1155=3⋅5⋅7⋅111155 = 3 \cdot 5 \cdot 7 \cdot 11

live ↗
InLength(PowerModList(1,12,1 155))=2PrimeNu(1 155)\mathrm{Length}(\mathrm{PowerModList}(1, \frac{1}{2}, 1\,155))=2^{\mathrm{PrimeNu}(1\,155)}OutTrue\mathrm{True}

PowerMod with a rational exponent is the least element of the list

live ↗
InPowerMod(4,12,7)=First(PowerModList(4,12,7))\mathrm{PowerMod}(4, \frac{1}{2}, 7)=\mathrm{First}(\mathrm{PowerModList}(4, \frac{1}{2}, 7))OutTrue\mathrm{True}
Possible issues #

the cubes mod 7 are only 0,1,60, 1, 6, so 2 has no cube root

live ↗
InPowerModList(2,13,7)\mathrm{PowerModList}(2, \frac{1}{3}, 7)Out[]\bigl\lbrack\bigr\rbrack

no square root of −1-1: 3≡3(mod4)3 \equiv 3 \pmod 4 blocks its channel

live ↗
InPowerModList(−1,12,15)\mathrm{PowerModList}(-1, \frac{1}{2}, 15)Out[]\bigl\lbrack\bigr\rbrack

no inverse when gcd⁡(a,m)≠1\gcd(a, m) \ne 1, so the list is empty

live ↗
InPowerModList(2,−1,4)\mathrm{PowerModList}(2, -1, 4)Out[]\bigl\lbrack\bigr\rbrack

the 2-adic channel breaks the 2ω(m)2^{\omega(m)} law: Z/2a\mathbb{Z}/2^a has four square roots of 1 for a≥3a \ge 3

live ↗
InPowerModList(1,12,8)\mathrm{PowerModList}(1, \frac{1}{2}, 8)Out[1,3,5,7]\bigl\lbrack1, 3, 5, 7\bigr\rbrack

a product of two 21-digit primes cannot be factored in budget, so even 4\sqrt 4 stays unevaluated — finding the other two roots is as hard as factoring

live ↗
InPowerModList(4,12,(1020+39)(1020+129))\mathrm{PowerModList}(4, \frac{1}{2}, (10^{20}+39)(10^{20}+129))OutPowerModList(4,12,10 000 000 000 000 000 016 800 000 000 000 000 005 031)\mathrm{PowerModList}(4, \frac{1}{2}, 10\,000\,000\,000\,000\,000\,016\,800\,000\,000\,000\,000\,005\,031)

x2≡0(mod340)x^2 \equiv 0 \pmod{3^{40}} has 3203^{20} roots — too many to list

live ↗
InPowerModList(0,12,340)\mathrm{PowerModList}(0, \frac{1}{2}, 3^{40})OutPowerModList(0,12,12 157 665 459 056 928 801)\mathrm{PowerModList}(0, \frac{1}{2}, 12\,157\,665\,459\,056\,928\,801)
Neat examples #

the moduli with four or more square roots of 1 come in patterns: mod 24 every unit is one, since (Z/24)×≅C23(\mathbb{Z}/24)^\times \cong C_2^3

live ↗
InPowerModList(1,12,[8,15,21,24])\mathrm{PowerModList}(1, \frac{1}{2}, \bigl\lbrack8, 15, 21, 24\bigr\rbrack)Out[[1,3,5,7],[1,4,11,14],[1,8,13,20],[1,5,7,11,13,17,19,23]]\bigl\lbrack\bigl\lbrack1, 3, 5, 7\bigr\rbrack, \bigl\lbrack1, 4, 11, 14\bigr\rbrack, \bigl\lbrack1, 8, 13, 20\bigr\rbrack, \bigl\lbrack1, 5, 7, 11, 13, 17, 19, 23\bigr\rbrack\bigr\rbrack
About
Examples37
Checked againstWolfram Language 26 agree10 differ1 errorSageMath 25 agree1 differ9 errorSymPy 26 agree1 differ9 errormathlib4 1 agreeOscar 1 agreeJulia 1 agreeRust 1 agree
CrosswalkWolfram LanguageSageMathSymPy

Implementation

mappedwolframpackages/oracle/src/mappings.ts

Sage reaches this through Zmod(m)(a).nth_root(b, all=True) (run.ts's SAGE_PREAMBLE); no one-liner in SymPy.

mappedsagepackages/oracle/src/mappings.ts

Sage reaches this through Zmod(m)(a).nth_root(b, all=True) (run.ts's SAGE_PREAMBLE); no one-liner in SymPy.

mappedsympypackages/oracle/src/mappings.ts

x^r = a^s (mod m) is nthroot_mod's x^n = a (mod p) with n = r and a = pow(a, s, m) over ZZ, which takes a negative s through the modular inverse. SymPy takes integer residues, so a rational or Gaussian base is an error there. A list threads over the base only.

See also: PowerMod, ModularInverse, MultiplicativeOrder, PrimitiveRootList, RationalReconstruction