The Order of an Element
Euler's theorem says , but often a smaller power already gives 1. The smallest such exponent, the order of , controls how long a power sequence runs before repeating. It is the period of repeating decimals, the size of the subgroup in Diffie-Hellman and the key to Shor's quantum factoring algorithm.
Definition
For , the multiplicative order is the smallest with .
def order(a, n):
k, x = 1, a % n
while x != 1:
x = x * a % n
k += 1
return k
n = 13
for a in range(1, n):
seq = [pow(a, k, n) for k in range(1, order(a, n) + 1)]
print(f"ord({a:2}) = {order(a, n):2} powers: {seq}")ord( 1) = 1 powers: [1]
ord( 2) = 12 powers: [2, 4, 8, 3, 6, 12, 11, 9, 5, 10, 7, 1]
ord( 3) = 3 powers: [3, 9, 1]
ord( 4) = 6 powers: [4, 3, 12, 9, 10, 1]
ord( 5) = 4 powers: [5, 12, 8, 1]
ord( 6) = 12 powers: [6, 10, 8, 9, 2, 12, 7, 3, 5, 4, 11, 1]
ord( 7) = 12 powers: [7, 10, 5, 9, 11, 12, 6, 3, 8, 4, 2, 1]
ord( 8) = 4 powers: [8, 12, 5, 1]
ord( 9) = 3 powers: [9, 3, 1]
ord(10) = 6 powers: [10, 9, 12, 3, 4, 1]
ord(11) = 12 powers: [11, 4, 5, 3, 7, 12, 2, 9, 8, 10, 6, 1]
ord(12) = 2 powers: [12, 1]
Every order modulo 13 divides 12. Elements of order 12 (here 2, 6, 7, 11) are primitive roots: their powers run through all units.
Key facts
- if and only if . (Write ; then with , so .)
- Hence .
- .
Fact 2 gives a fast algorithm when can be factored: start from and divide out each prime factor as long as the power is still 1.
from sympy import factorint, totient
def fast_order(a, n):
k = int(totient(n))
for p, e in factorint(k).items():
for _ in range(e):
if pow(a, k // p, n) == 1:
k //= p
else:
break
return k
p = 10**9 + 7
print(factorint(p - 1))
print([fast_order(a, p) for a in (2, 3, 5, 7)]){2: 1, 500000003: 1}
[500000003, 500000003, 1000000006, 500000003]
Repeating decimals
The decimal expansion of (with ) repeats with period : after long-division steps the remainder returns to 1 exactly when .
def period(n):
k, r = 1, 10 % n
while r != 1:
r = r * 10 % n
k += 1
return k
for n in (3, 7, 11, 13, 17, 41, 97):
print(f"1/{n}: period {period(n)}")
print(f"1/7 = {1 / 7:.15f}")1/3: period 1
1/7: period 6
1/11: period 2
1/13: period 6
1/17: period 16
1/41: period 5
1/97: period 96
1/7 = 0.142857142857143
When the period is (as for 7, 17 and 97), 10 is a primitive root modulo and the decimal is "full reptend".
Order and factoring: Shor's idea
If you know the order of a random modulo and is even, then , and is often a proper factor. Classically, finding is as hard as factoring; a quantum computer finds it efficiently, which is why RSA would fall to a large quantum computer (Post-Quantum Cryptography).
from math import gcd
N, a = 3233, 3
r, x = 1, a
while x != 1:
x = x * a % N
r += 1
print("order of 3 mod 3233:", r)
if r % 2 == 0:
y = pow(a, r // 2, N)
print("factors:", gcd(y - 1, N), gcd(y + 1, N))order of 3 mod 3233: 260
factors: 61 53
In software
- Diffie-Hellman and DSA work in a subgroup of known prime order ; implementations check that received values satisfy , which rules out small-subgroup attacks (Pohlig-Hellman and Safe Primes).
- The order of a pseudorandom generator's state update is its period: a multiplicative generator cycles through all values only when is a primitive root.
Exercises
Exercise 1: By hand
Find and .
Answer
5 and 10.
Solution
Powers of 3 modulo 11: , so the order is 5. Powers of 2: , order 10. Both divide .
Exercise 2: Possible orders
Which orders can elements have modulo 31?
Answer
1, 2, 3, 5, 6, 10, 15, 30.
Solution
Orders divide , whose divisors are 1, 2, 3, 5, 6, 10, 15 and 30. Because the unit group modulo a prime is cyclic, each divisor actually occurs, for exactly elements.
Exercise 3: Count by order (code)
For , count how many elements have each order and compare with .
Answer
The counts are : 1, 1, 2, 4, 2, 4, 8, 8.
Solution
from collections import Counter
from sympy import n_order, totient
p = 31
c = Counter(n_order(a, p) for a in range(1, p))
print(sorted(c.items()))
print([(d, int(totient(d))) for d in sorted(c)])[(1, 1), (2, 1), (3, 2), (5, 4), (6, 2), (10, 4), (15, 8), (30, 8)]
[(1, 1), (2, 1), (3, 2), (5, 4), (6, 2), (10, 4), (15, 8), (30, 8)]
Test yourself
If , then exactly when…
The order of any unit modulo divides…
The period of the repeating decimal of is…
- 12
- 6
- 13
- 3