TExcelTAD
Home › Number Theory & Cryptography › The Order of an Element

The Order of an Element

Euler's theorem says aφ(n)≡1a^{\varphi(n)} \equiv 1, but often a smaller power already gives 1. The smallest such exponent, the order of aa, 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 gcd⁡(a,n)=1\gcd(a, n) = 1, the multiplicative order ord⁡n(a)\operatorname{ord}_n(a) is the smallest k≥1k \ge 1 with ak≡1(modn)a^k \equiv 1 \pmod{n}.

python · try it yourself
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

  1. ak≡1a^k \equiv 1 if and only if ord⁡n(a)∣k\operatorname{ord}_n(a) \mid k. (Write k=q⋅ord⁡+rk = q \cdot \operatorname{ord} + r; then ar≡1a^r \equiv 1 with r<ord⁡r < \operatorname{ord}, so r=0r = 0.)
  2. Hence ord⁡n(a)∣φ(n)\operatorname{ord}_n(a) \mid \varphi(n).
  3. ord⁡(aj)=ord⁡(a)/gcd⁡(j,ord⁡(a))\operatorname{ord}(a^j) = \operatorname{ord}(a) / \gcd(j, \operatorname{ord}(a)).

Fact 2 gives a fast algorithm when φ(n)\varphi(n) can be factored: start from φ(n)\varphi(n) and divide out each prime factor as long as the power is still 1.

python · try it yourself
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 1/n1/n (with gcd⁡(n,10)=1\gcd(n, 10) = 1) repeats with period ord⁡n(10)\operatorname{ord}_n(10): after kk long-division steps the remainder returns to 1 exactly when 10k≡110^k \equiv 1.

python · try it yourself
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 n−1n - 1 (as for 7, 17 and 97), 10 is a primitive root modulo nn and the decimal is "full reptend".

Order and factoring: Shor's idea

If you know the order rr of a random aa modulo NN and rr is even, then (ar/2−1)(ar/2+1)≡0(modN)(a^{r/2} - 1)(a^{r/2} + 1) \equiv 0 \pmod{N}, and gcd⁡(ar/2−1,N)\gcd(a^{r/2} - 1, N) is often a proper factor. Classically, finding rr 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).

python · try it yourself
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 qq; implementations check that received values yy satisfy yq≡1y^q \equiv 1, 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 x↦ax mod px \mapsto ax \bmod p cycles through all p−1p - 1 values only when aa is a primitive root.

Exercises

Exercise 1: By hand

Find ord⁡11(3)\operatorname{ord}_{11}(3) and ord⁡11(2)\operatorname{ord}_{11}(2).

Answer

5 and 10.

Solution

Powers of 3 modulo 11: 3,9,27≡5,15≡4,12≡13, 9, 27 \equiv 5, 15 \equiv 4, 12 \equiv 1, so the order is 5. Powers of 2: 2,4,8,5,10,9,7,3,6,12, 4, 8, 5, 10, 9, 7, 3, 6, 1, order 10. Both divide φ(11)=10\varphi(11) = 10.

Exercise 2: Possible orders

Which orders can elements have modulo 31?

Answer

1, 2, 3, 5, 6, 10, 15, 30.

Solution

Orders divide φ(31)=30\varphi(31) = 30, whose divisors are 1, 2, 3, 5, 6, 10, 15 and 30. Because the unit group modulo a prime is cyclic, each divisor dd actually occurs, for exactly φ(d)\varphi(d) elements.

Exercise 3: Count by order (code)

For p=31p = 31, count how many elements have each order and compare with φ(d)\varphi(d).

Answer

The counts are φ(d)\varphi(d): 1, 1, 2, 4, 2, 4, 8, 8.

Solution
python · try it yourself
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 ord⁡n(a)=6\operatorname{ord}_n(a) = 6, then ak≡1(modn)a^k \equiv 1 \pmod{n} exactly when…

  1. k=6k = 6
  2. 6∣k6 \mid k
  3. k∣6k \mid 6
  4. k>6k > 6

The order of any unit modulo nn divides…

  1. nn
  2. φ(n)\varphi(n)
  3. n+1n + 1
  4. log⁡n\log n

The period of the repeating decimal of 1/131/13 is…

  1. 12
  2. 6
  3. 13
  4. 3