Computational Number Theory Lecture Notes
Computational Number Theory Lecture Notes
Lecture Notes
N.R.Aravind
—————————
Acknowledgements
The latex source file was built using the LegrandOrangeBook template (copyright
2022, Goro Akechi) available at book-website.com and licensed under the Creative
Commons Attribution-NonCommercial 4.0 License (the “License”). A copy of the
License is available at https://creativecommons.org/licenses/by-nc-sa/4.0.
Minor modifications were made in the use of the template.
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Congruences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.1 Definition and properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2 Arithmetic in Zn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3 Linear congruences and the Chinese Remainder Theorem . . . . . . . . . 22
4 CONTENTS
5 Finite Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.1 Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.1.1 Cayley Tables and Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5.1.2 Direct products and subgroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.1.3 Cosets and Lagrange’s theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.2 Rings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.3 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.4 Finite Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.5 Irreducible polynomials in Zp [x] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.6 Application: Secret sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
I
Polynomials
2 The in One Theorem
Fundamental Variable
of Arith-
metic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.1 Prime numbers . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2 Euclid’s lemma . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3 The fundamental theorem . . . . . . . . . . . . . . . . . 19
3 Congruences . . . . . . . . . . . . . . . . . . . . . . 21
3.1 Definition and properties . . . . . . . . . . . . . . . . . . 21
3.2 Arithmetic in Zn . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3 Linear congruences and the Chinese Remainder Theo-
rem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.4 The ring Zn . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.5 Fermat’s little theorem . . . . . . . . . . . . . . . . . . . 24
3.6 Lagrange’s theorem . . . . . . . . . . . . . . . . . . . . . 25
3.7 Classroom Exercises . . . . . . . . . . . . . . . . . . . . . 25
3.8 Euler’s totient function and Euler’s theorem . . . 26
3.9 Euclid’s algorithm and unique factorization for poly-
nomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.10 Classroom Exercises . . . . . . . . . . . . . . . . . . . . . 27
3.11 The equations xd = 1 and xd = a in Zp . . . . . . 28
3.12 Application: The RSA Algorithm . . . . . . . . . . . 29
3.13 Order and primitive roots . . . . . . . . . . . . . . . . . 29
5 Finite Fields . . . . . . . . . . . . . . . . . . . . . . . 43
5.1 Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.2 Rings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.3 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.4 Finite Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.5 Irreducible polynomials in Zp [x] . . . . . . . . . . . . 47
5.6 Application: Secret sharing . . . . . . . . . . . . . . . . 48
A natural follow-up to the quadratic equation is: what about cubic equations? Solving
the simplest cubic equation x3 = a, boils down to finding cube-roots, and finding
cube-roots numerically was also known for a long time; for example, Aryabhatta
(around AD 500),gave a method for finding both square-roots and cube-roots.
What about general cubic equations? This was first studied by Omar Khayyam (AD
1100), where he gave a geometric solution.
Omar Khayyam divided the cubic equation into various categories so that the
coefficients would be positive, and gave different geometric solutions for them.
12 Chapter 1. Two Equations from Ancient Times
y
P
x
1 2
−q √ −q √
s s
x= 3
+ D+ 3
− D. (1.1)
2 2
q 2 p3
where D = + .
4 27
But how do we obtain this? And what about the more general cubic with a non-zero
x2 term? To answer the latter question first, it turns out that we can reduce the
solution of any cubic polynomial to a solution of a cubic without the x2 term: such
a cubic polynomial is called a depressed cubic.
If we choose r = −a/3, then we obtain a depressed cubic in y, let’s call it g(y). Thus
to solve f (x) = 0, we can solve g(y) = 0 and then find the corresponding roots of f .
1.1 The Cubic Equation 13
History
Around AD 1500, Scipione del Ferro, professor at the University of Bologna,
discovered a formula for depressed cubic equations (cubic equations with a
missing x2 term) and shortly before his death in 1526, communicated it to his
disciple, Antonio del Fiore. After del Ferro’s death, around 1535, Fiore issued
a challenge to another mathematician Tartaglia, with a list of 30 problems
all of whose solutions depended on knowing how to solve the cubic equations
x3 + px = q and x3 = px + q.
Interestingly, Tartaglia had five years earlier, independently figured out solu-
tions to x3 + ax2 = b and x3 = ax2 + b. After accepting the challenge he figured
out shortly how to solve the other kind of cubic equation and thus solved all
the 30 problems posed by Fiore; he himself posed both kinds of equations in
the counter-challenge which Fiore could not solve, and hence won the duel.
Here are two of the equations that Tartaglia solved in the duel: x3 + x = 12
and x3 + 3x = 15.
After the duel, Tartaglia was approached by Gerolamo Cardano, to share
his secret as Cardano was writing a book on arithmetic etc. [Incidentally,
Cardano was also the first to write a book on probability although he made
many mistakes in it.] Initially, Tartaglia refused, but later shared his formula
by means of a poem on the promise that Cardano would keep it secret.
Cardano kept his secret for some time, but a few things changed his mind.
Firstly, he himself figured out how to reduce the most general equation to
a depressed cubic; secondly his student Ferrari figured out how to solve the
biquadratic equation, i.e. an equation of degree four. Thirdly, he visited del
Ferro’s house and examined his manuscripts and was convinced that del Ferro
was the original discoverer of the solution to the cubic. He published the
solutions in his new book Ars Magna, which led to a fallout between Cardano
and Tartaglia.
√
x3 = u + v + 3 3 uvx. (1.2)
√ √ √
Now we notice that if u+v = −q and 3 3 uv = −p, then x = 3 u+ 3 v satisfies f (x) = 0.
p3
Clearly we can find such a pair u, v as roots of the quadratic polynomial z + qz − .
2
27
−q √ −q √ q 2 p3
Thus we find u = + D, v = − D, where D = + . The corresponding
2 2 4 27
√ √
s s
−q −q
root is: x = 3 + D+ 3 − D.
2 2
14 Chapter 1. Two Equations from Ancient Times
Proof. It is both necessary and sufficient to prove that we can express gcd(a, b) as
ax + by for some integers x, y. Let d = gcd(a, b). For a given value of d, we prove
by induction on a + b (over pairs (a, b) with (gcd(a, b) = d) that d can be written
as ax + by. The base case is when a + b = d, i.e. a = d and b = 0. In this clearly
x = 1, y = 0 is a solution. Now consider an arbitrary pair (a, b) with gcd(a, b) = d
and without loss of generality, let a ≥ b. Then gcd(a − b, b) = d and by the induction
hypothesis, we have: d = (a − b)x + by. This implies that d = ax + b(y − x) and thus
d is an integer-linear combination of a, b as desired. This completes the proof. ■
We make some remarks: firstly, the proof can be made algorithmic by finding
successively smaller pairs (a, b) till we reach the pair (d, 0) and work backwards. A
simple recursive algorithm is the following:
Algorithm 1 Recursive-Bezout
1: procedure Simple-Euclid((a, b))
2: if a = 0 then return (0, 1)
3: end if
4: if b = 0 then return (1, 0)
5: end if
6: x ← SIMPLE-EUCLID(a − b, b)(1)
7: y ← SIMPLE-EUCLID(a − b, b)(2)
8: if a > b then return (x, y − x)
9: else return (x − y, y)
10: end if
11: end procedure
Now the second remark: as in the case of Euclid’s algorithm, we can make it more
efficient by considering not just a − b but a − qb for q = ⌊a/b⌋. We first look at
Euclid’s algorithm to thus find the gcd of two numbers.
One of our concerns in this course will be the design of efficient algorithms, often
algorithms running in time polynomial in the input size. How efficient is Euclid’s
algorithm in terms of its input size?
Theorem 1.2 Let m = max(log2 a, log2 b). Then the number of iterations in Euclid’s
algorithm 2m. Further, the time complexity of each iteration is at most O(m log m).
Proof. The last statement follows from the fact that the complexity of each iteration
is essentially the cost of integer division, which has the same complexity as integer
multiplication. In 2020, Harvey and van der Hoeven gave a O(n log n) time algorithm
to multiply two n-bit integers; for comparison, the complexity of FFT-based integer
multiplication is O(n log2 n). Thus the cost of all the basic arithmetic operations
performed on two n-bit integers is O˜(n).
To prove the first statement, let (Ai , Bi ) denote the value of the pair after i iterations,
with (A0 , B0 ) = (max(a, b), min(a, b)). Further, let Ai = Bi qi + ri with 0 < ri < Bi .
Note that Ai+2 = Bi+1 = ri < Bi .
We have Ai = Bi qi + Ai+1 ≥ Bi + Ai+2 > 2Ai+2 . The Ai s reduce by a factor of at least
2 after every two iterations, so that the number of iterations is at most 2 log2 m. ■
How the algorithm works: The key observation is that the pair (Ai+1 , Bi+1 ) is
obtained from (Ai , Bi ) by a linear transformation, namely:
0 1
!
(Ai+1 , Bi+1 ) = (Ai , Bi ) (1.3)
1 −qi
1.3 Classroom Exercises 17
0 1
!
Let M = i . Then we have: (A0 , B0 )M = (gcd(a, b), 0). Thus the first
Q
1 −qi
column of M yields a solution to ax+by = gcd(a, b). Also, the equality A0 x+B0 y = A
is maintained as an invariant after every iteration.
An illustration of the algorithm for a = 3, b = 8:
A B x y u v q
8 3 1 0 0 1 2
3 2 0 1 1 -2 1
2 1 1 -2 -1 3 2
1 0 -1 3 3 -8
Thus, we find that the gcd is 1 and (−1, 3) is a solution to 8x + 3y = 1. We may
check that 8x + 3y = A is valid in each iteration.
Exercise 1.2 Find all integers x, y such that 86x + 197y = 1. ■
Solution: We may find one solution by inspection or using the Euclidean algorithm.
For example, x = 2, y = −1 is a solution. We may now use this to find all solutions:
if (x, y) is a solution, then we have 4(x − 2) + 7(y + 1) = 0. We observe that 7 divides
4(x − 2) and hence 7|(x − 2). Thus, we get 7|(x − 2); let x = 7k + 2, this implies that
y = −4k − 1. Thus, the general solution is given by {(7k + 2, −4k − 1) : k ∈ Z}.
Exercise 1.7 Prove that if d|a and d|b, then d|gcd(a, b). ■
Solution: By Bezout’s lemma, there exist integers x, y such that ax + by = gcd(a, b).
18 Chapter 1. Two Equations from Ancient Times
Since d divides the LHS, it must divide the RHS which proves the claim.
2. The Fundamental Theorem of
Arithmetic
Proof. Suppose for contradiction that p does not divide a and p does not divide
b. Then gcd(p, a) = 1 and by Bezout’s lemma, there exist integers x, y such that
px + ay = 1. Similarly, gcd(p, b) = 1 and there exist integers u, v such that pu + bv = 1.
Multiplying the two relations, we get: (px + ay)(pu + bv) = 1. Expanding the LHS,
we get a contradiction because p divides each term on the LHS, but the RHS is equal
to 1. This proves the lemma. ■
Proof. There are two statements to prove, (a) that every natural number larger
than 1 can be factored into primes and (b) that the factorization is unique (up to
ordering).
20 Chapter 2. The Fundamental Theorem of Arithmetic
We first prove (a) by induction. The first few base cases are 2, 3, 4 which we see have
a prime factorization. The induction step: We consider an arbitrary natural number
n > 1 and inductively assume that 1, 2, . . . , n − 1 have a prime factorization. If n is
prime, then we are done, otherwise let n = ab with 1 < a, b < n. By the induction
hypothesis, a, b have a prime factorization. The two factorizations may be combined
to give a factorization for n. This proves (a).
We now prove (b), also by induction. As before, we may verify that (b) holds for the
base cases 2, 3, 4. For the induction step, we consider an arbitrary n > 1, assuming
that (b) holds for all numbers lesser than n. Suppose that n = p1 p2 . . . pk = q1 q2 . . . qk .
We apply Euclid’s lemma to the product q1 q2 . . . qk to deduce that p1 divides some
qi . Since qi is prime, we have p1 = qi . Now we apply the induction hypothesis to
n/p1 = p2 . . . pk = j̸=i qj and conclude that {p2 , . . . , pk } = {qj |j = ̸ i} (and k −1 = l −1).
Q
Exercise 2.1 Show that if gcd(a, b) = 1, then gcd(ab, c) = gcd(a, c)gcd(b, c). Deduce
that if gcd(a, b) = 1 and a|bc, then a|c. ■
3. Congruences
3.2 Arithmetic in Zn
The basic arithmetic operations of addition and subtraction have time complexity
O(log n); the complexity of multiplication is O(log n log log n); the complexity of find-
ing ab (mod n) (by repeated squaring) is O(log b log n log log n) = O(log2 n log log n).
An example of exponentiation by repeated squaring: we find 3100 modulo 35 as
follows: we first find the values of 3k modulo 35 for k being a power of two.
k 1 2 4 8 16 32 64
3k (mod 35) 3 9 11 16 11 16 11
We now find:
We can rewrite the above equation as ax = b + ny; thus we see that this equation has
a solution if and only if gcd(a, n) divides b. It is usually convenient to divide this
equation on both sides by gcd(a, n); translating this back to congruences, this means
considering Equation 3.1 when (a, n) = 1. In this case, it turns out that the solution
is unique.
Theorem 3.1 Given a ∈ {0, 1, . . . , n − 1} such that gcd(a, n) = 1, the congruence
ax ≡ b ( mod n) has a unique solution modulo n.
Proof. Suppose that x1 , x2 are two solutions to the given congruence equation. Then
ax1 ≡ b (mod n) and ax2 ≡ b (mod n); subtracting we get:
Without the assumption that gcd(a, n) = 1, how many solutions does the linear
congruence 3.1 have? You will figure this out in an exercise below!
3.3 Linear congruences and the Chinese Remainder Theorem 23
Exercise 3.1 Find all the solutions in {0, 1, . . . , 24} to 10x ≡ 15 (mod 25). ■
Exercise 3.2 Suppose that gcd(a, n) divides b. How many distinct solutions to
ax ≡ b (mod n), modulo n, are there? Justify your answer. [Hint: You may be
able to first guess the answer from what you find in the previous exercise.] ■
Proof. The fact that this map is surjective follows from the preceding paragraph:
given (x1 , x2 ), we may use the extended Euclidean algorithm to find y, z such that
ny − mz = x2 − x1 ; then x1 + ny = x2 + mz is a pre-image of (x1 , x2 ).
From the fact that this map is surjective and the fact that the domain and co-domain
are finite sets of the same size, we may already conclude that f is a bijection.
Nevertheless, we may also prove that f is injective, as follows. Let x, y be two
different numbers in {0, 1, . . . , mn − 1}. Then x − y ∈ {1, . . . , mn − 1}, hence x − y is
not divisible by mn. Since gcd(m, n) = 1, we can conclude: x − y is not divisible by
m OR x − y is not divisible by n. In either case we have f (x) ̸= f (y), proving that f
is an injective map. ■
Proof Sketch: The first part follows by a repeated application of Theorem 3.2. For
the second part, we may use the Extended Euclidean algorithm to successively solve
pairs of congruences. ■
Equivalently, we may say: for every natural a such that p does not divide a, we
have:
Proof by mathematical induction on a: We prove Eqn 3.3 for every natural ! number a.
p
The base case is a = 1; for the induction step, write (a + 1)p = ap + p−1 ap−i + 1;
P
i=1
i
observe that every term in the summation is divisible by p and complete using the
induction hypothesis.
Proof by a bijection: We assume that p does not divide a and observe that
{a, 2a, . . . , (p − 1)a} must all have distinct values modulo p (to see this, consider their
differences). This implies that the set of these values modulo p must be the same (pos-
sibly in different order) as {1, 2, . . . , p − 1}. Now we note that a × 2a . . . × (p − 1)a ≡
(p − 1)! (modulo p). We may divide by (p − 1)! on both sides; this is possible because
p does not divide (p − 1)!; this gives us the desired result. ■
Solution: Using the previous exercise, for every x ̸= 0, there is one solution,
thus the number of solutions is p − 1.
4. Let p be prime and a, b be non-zero elements of Zp . Find the number of
solutions of ax + by = 1.
Solution: For every x, there is a unique value of y; thus the number of
solutions is p.
1
If p is prime, then ϕ(pk ) = pk − pk−1 = pk (1 − ). Thus, we obtain:
p
e e
Theorem 3.6 If n = p11 . . . pkk , then
1 1
! !
ϕ(n) = n 1 − ... 1− .
p1 pk
Theorem 3.7 [Euler’s Theorem] If gcd(a, n) = 1, then aϕ(n) ≡ 1 (mod n). Equiva-
lently, if a ∈ Z∗n , then aϕ(n) = 1 in Zn .
Proof. We generalize the second proof of Fermat’s little theorem. Consider the set
Z∗n . Firstly, we claim that if a ∈ Z∗n , then the sets aZ∗n and Z∗n are equal, as subsets
of Zn . To see this observe that if b ∈ Z∗n , then ab ∈ Z)∗n as well. Thus, aZ∗n ⊆ Z∗n .
Also, if b1 , b2 are two distinct elements of Z∗n , then ab1 ̸= ab2 , because b1 − b2 ̸≡ 0
(mod n) and gcd(a, n) = 1.
Let P = x∈Z∗n x, Q = x∈aZ∗n x. We have: P = Q and Q = aϕ(n) P (in Zn ). Since
Q Q
P ∈ Z∗n , we may cancel P on both sides from the equation aϕ(n) P = P to obtain the
theorem statement. ■
with deg(r(x)) < deg(f (x)). We remark that 3.5 does not always hold over Zn for n
composite, for example, if n = 4 and f (x) = 2x, g(x) = x.
3.10 Classroom Exercises 27
The Extended Euclidean algorirthm thus works for polynomials in the same way
as for integers; the running time is linear in the degree of the polynomials and
polynomial in the size of the coefficients. In particular, it finds the gcd of two
polynomials, which we define below.
Definition 3.1 Given two polynomials f (x), g(x) ∈ Zp [x], we define the greatest
common divisor (gcd) of f (x) and g(x) as the unique monic polynomial of largest
degree that divides f (x) and g(x).
Why is the monic polynomial of largest degree that divides both polynomials unique?
To see this, we may argue that if h(x) is one such polynomial and t(x) some polynomial
that also divides both f (x) and g(x), then t(x) must divide h(x). From this, the
desired conclusion may be drawn.
As a consequence of the Euclidean algorithm, we also deduce Bezout’s lemma for
polynomials.
Theorem 3.8 — Bezout’s Lemma. Let p be prime and f (x), g(x) be polynomials in
Zp [x]. Then there exist polynomials u(x), v(x) such that f (x)u(x) + g(x)v(x) =
gcd(f (x), g(x)). Further, the Euclidean algorithm finds u(x), v(x) such that
deg(u(x)) < deg(g(x)) and deg(v(x)) < deg(f (x)).
The final analog of natural numbers for polynomials is unique factorization. We say
that a polynomial f (x) ∈ Zp [x] is irreducible if f (x) = g(x)h(x) implies g(x) = 1 or
h(x) = 1.
Theorem 3.9 Every polynomial f (x) ∈ Zp [x] can be uniquely factorized into
irreducible polynomials.
An illustration of Euclid’s algorithm for f (x) = x3 + x, g(x) = 5x2 − 13x + 6 over Z17 :
A B r q u v s t
x3 + x 5x2 − 13x + 6 −5x − 14 7x + 8 1 0 0 1
5x2 − 13x + 6 −5x − 14 0 −x + 2 0 1 1 −7x − 8
−5x − 14 0 1 −7x − 8 x−2 −7x2 + 5x
Thus, the gcd of x3 + x and 5x2 − 13x + 6 in Z17 [x] is (−5x − 14)/ − 5 = (x − 4). We
further have: (x3 + x)u(x) + (5x2 − 13x + 6)v(x) = −5x − 14, where u(x) = 1 and
v(x) = (−7x − 8).
a. x2 = 2: No solution.
b. x2 = 3: x = 4 and x = 9 are the solutions.
c. x3 = 1: x = 1, x = 3, x = 9 are the solutions.
d. x5 = 2. We combine this with x12 = 1 as follows: We find k such that
5k + 12l = 1. Now, this gives us: x = x5k+12l = 2k .
Theorem 3.10 Let d|(p − 1). Then the polynomial xd − 1 has d distinct roots.
We remark that if f (x) is a polynomial with deg(f ) roots, then for every divisor g(x)
of f (x), the polynomial g(x) must have deg(g)) roots.
Proof. Since gcd(d, p − 1) = 1, we can find (efficiently) a positive integer k such that:
dk ≡ 1 mod (p − 1).
Raising both sides of xd = a to the power k, we obtain xdk = ak and applying Fermat’s
little theorem, we get x = ak . ■
Combining the previous ideas, we can obtain the following corollary to Theorem
3.10.
Corollary 3.1 In Zp , the number of roots of xd − 1 is equal to gcd(d, p − 1).
The above result is obtained by finding k such that dk ≡ gcd(d, p − 1) (mod (p − 1))
and noting that xd = 1 raised to the power of k gives: xgcd(d,p−1) = 1, which has
gcd(d, p − 1) solutions.
Exercise 3.3 Show that the number of roots of xd − a in Zp is either zero or equal
to gcd(d, p − 1). ■
Theorem 3.11 also generalizes as follows, with the theorem statement itself containing
the explanation.
3.12 Application: The RSA Algorithm 29
Theorem 3.12 Let n ∈ N. If gcd(d, ϕ(n)) = 1 and gcd(a, n) = 1, then the equation
xd = a has a unique solution in Zn , given by x = ak , where dk ≡ 1 (mod ϕ(n)).
This definition is well-defined because if a ∈ Z∗n , then aϕ(n) ≡ 1 (mod n), and thus
there must exist a smallest positive ineger d such that ad ≡ 1 (mod n).
As an example, we compute the orders of every element in Z∗7 .
a 1 2 3 4 5 6
ord7 (a) 1 3 6 3 6 2
Observation 2 For every a ∈ Z∗n , we have: ordn (a) divides ϕ(n).
Proof. We have aϕ(n) ≡ 1 (mod n). Let d = ordn (a) and suppose for contradiction
that d does not divide ϕ(n); then we can write ϕ(n) = qd + r where 0 < r < d. Since
ad ≡ 1 (mod n), we obtain ar ≡ 1 (mod n), which is a contradiction to the definition
of d as the order. This proves the observation. ■
In particular, when p is prime and a ∈ Z∗p „ we have ordp (a) divides (p − 1).
Definition 3.3 We call an element a ∈ Z∗n a primitive kth-root of unity if ordn (a) = k.
We call an element a ∈ Z∗n a primitive root if ordn (a) = ϕ(n).
One reason that primitive roots are interesting is that their powers generate Z∗n , that
is if g is a primitive root, then {g, g 2 , . . . , g ϕ(n) } = Z∗n . To see this, note that if g i ≡ g j
(mod n), then g i−j ≡ 1 (mod n); this is not possible if i, j are distinct and less than
ϕ(n) (because ordn (g) = ϕ(n)).
Primitive roots exist only for some moduli; the relevant fact for us is that they exist
when n is prime.
Theorem 3.13 Let p be a prime. Then there is an element g ∈ Z∗p such that
ordp (g) = p − 1.
4. The Quadratic Equation in Zp
Theorem 4.1 Let d|(p − 1) and let a ∈ Zp . Then xd = a is solvable if and only if
a(p−1)/d = 1.
For arbitrary d, i.e. without the assumption that d|(p − 1), we can still answer the
same question.
1. Alice and Bob agree on a function f , whose domain is two- valued, say {H, T }.
2. Alice tosses a coin, say the result is x ∈ {H, T }.
3. Alice sends f (x) to Bob.
4. Now Bob calls Heads or Tails, let y be the value called by Bob.
5. Bob sends y to Alice.
6. Alice now sends x to Bob; both of them know x, y and know who has won the
toss.
The key properties required of the function f for the above protocol to work are:
(a) Given the value of f (x), Bob cannot find out the value of x. This means that
f (H) must equal f (T ), otherwise Bob can compute each of these two values and see
which one was sent by Alice.
The two properties appear to be negations of each other; however the idea is that
the two-valuedness of the domain is indirectly encoded.
1. Bob chooses two large primes p, q and sends their product n = pq to Alice.
2. Alice picks a random number x ∈ Zn ; with high probability, x ∈ Z∗n .
3. Alice sends y = x2 ∈ Zn to Bob.
4. Bob solves the equation x2 = y (with y being known and x being unknown)
separately in Zp and in Zq . Combining the solutions, Bob gets FOUR solutions
in Zn .
5. Now Bob calls one of the four values, say Bob calls z and sends it to Alice.
6. Alice now sends x to Bob.
7. Bob wins the toss if z = x or z = −x (in Zn ), else Alice wins the toss.
(a) Suppose the four square-roots of y in Zn are x, −x, x2 , −x2 . Given these four
values, Bob has exactly a 50% chance of guessing one of x, −x.
(b) Alice cannot switch to one of x2 , −x2 because Alice cannot find the other pair
of square-roots without essentially factoring n. This is because if Alice did find the
value of x2 , then she can factor n by finding gcd(x1 − x2 , n) and gcd(x1 + x2 , n) which
would be the values p, q.
The security of the protocol thus depends on the hardness of factoring an integer.
Currently there is no known polynomial-time algorithm for factoring an integer and
4.3 The Legendre Symbol 33
it is believed that such an algorithm is unlikely. If the numbers p, q are large enough,
then Alice would need hundreds of years to factor n should she wish to do so.
2
!
p
p
5 -1
7 1
11 -1
17 1
3. (a) Does the equation x3 ≡ 2 (mod 19) have solutions?
(b) Does the equation x3 ≡ 2 (mod 17) have solutions?
Solution:
(a) We have p = 19 and 3|(p − 1). So we compute 2( p − 1)/3, i.e. 26 ≡ 64 ≡ 7
(mod 19). Thus, this equation does not have solutions.
(b) We have p = 17 and gcd(3, 16) = 1. Thus, there is a unique solution, given
by x ≡ 2k (mod 17), where 3k ≡ 1 (mod 16).
More generally, given an arbitrary d, to check whether xd = a has solutions, we
first find k such that dk ≡ gcd(d, p − 1) (mod p − 1), and then check whether
the equation xgcd(d,p01)!= ak has !solutions. !
a b ab
4. Given the values of and , find the corresponding values of .
p p ! ! !
p
ab a b
Solution: Because of Euler’s criterion, we find that = . Thus,
p p p
we get! the ! following.
!
a b b
p p p
1 1 1
1 -1 -1
-1 1 -1
-1 -1 1
Solution: Since a is a quadratic residue, we must have: a39 ≡ 1 (mod 79). Multiply-
ing by a on both sides, we get a40 ≡ a (mod 79), so that x = a20 is a square-root of
a.
The above idea generalizes for any prime p ≡ 3 (mod 4) because the condition
a(p−1)/2 ≡ 1 (mod p) is equivalent to a(p+1)/2 ≡ a (mod p) and (p + 1)/2 is even when
p ≡ 3 (mod 4).
2
Proof. It is possible to prove this in several ways. One proof idea is to note that
the numbers in {1, 2, . . . , p − 1} may be paired up as (a, a−1 ), except for 1, (p − 1)
which are their own inverses. The product of each pair is 1, and the product of the
remaining elements is -1.
Another proof idea is to observe that xp−1 − 1 = (x − 1) . . . (x − p + 1) because of
Fermat’s little theorem. Comparing the constant term gives the result. ■
Using
! Wilson’s theorem, we derive the following which we shall use in computing
2
.
p
p−1
Claim 5 Let r = !. Then r2 ≡ −1 (mod p) if p ≡ 1 (mod 4), and r2 ≡ 1 (mod
2
p) if p ≡ 3 (mod 4).
2
!
We now give the value of .
p
Proof. All calculations are in Zp . We write (p−1)! = ST , where S= 1×3×. . .×(p−2)
p − 1
and T = 2 × 4 × . . . × (p − 1). We note that T = 2(p−1)/2 !. Also, we rewrite
2
p−1
S as S = 1 × −2 × 3 × . . . (p − 1)(−1)(p−1)/2+1 = !(−1)⌊(p−1)/4⌋ . Letting r =
2
36 Chapter 4. The Quadratic Equation in Zp
p−1
! and substituting for S, T , we get:
2
The LHS is -1; on the RHS, we know the value of r2 from the previous claim. Thus
we get an expression for 2(p−1)/2 , whose value we may obtain for each of the values
of p modulo 8. ■
3
!
For example, to calculate , we can write:
97
3 97 1
! ! !
= = = 1.
97 3 3
4.7.1 p ≡ 5 (mod 8)
Suppose that p ≡ 5 (mod 8). Then, we can write p − 1 = 4m, where m is odd. For
example, if p = 61, we can write p − 1 = 4m with m = 15.
Consider the number am in Zp . If am = 1, then our earlier method works, i.e. a(m+1)/2
is a square-root of a. Suppose that am ≠ 1. We also know that a2m = a(p−1)/2 = 1.
This implies that am = −1. How can we use this information?
The key idea is to find a quadratic non-residue, i.e. a number r such that r(p−1)/2 = −1.
We can find a such an element by sampling random elements from Z∗p and testing if
they satisfy the condition. Since at least half of the elements in Z∗p are quadratic
non-residues, the probability of success is 1/2.
4.7 The Tonelli-Shanks Algorithm 37
(ab2 )m = 1.
Thus, a2 = 50 × 5 = 16.
4
16 × 5 = −1.
8
3
Finally, we set a4 = a3 r16 = 61 and we have a34 = 1. Thus, we get ar2+4+8+16 = 1.
From this, we find a square-root of a to be: a2 r3+6+12+24 , which is equal to 83.
We now describe the algorithm formally. Since every computation involves a mth
power it is convenient to compute mth powers at the beginning of the algorithm
itself.
In the algorithm, findk(z) is a function that returns the least non-negative integer
k
k such that z 2 = 1. This need not exist for every z ∈ Zp , but it exists for values z
which are mth powers (on which the function is invoked).
The invariant x2 = ab is maintained at the beginning and end of each iteration. The
values of b, k, S, x for our earlier example (a = 2, p = 97) would be as shown in the
table below (with r = 5).
4.8 Hensel Lifting: From Zp to Zpk 39
b k S x
8 4 28 4
64 3 8 15
22 2 64 23
96 1 22 17
1 0 96 83
For an odd prime p, we can now solve this equation as gcd(2b, p) = 1 (assuming
gcd(a, p) = 1).
The same method works for finding a solution modulo p2k given a solution modulo
pk ; this is called Hensel Lifting. Thus, from a solution modulo p, we can find a
solution modulo pk using O(log k) calls to a linear-equation solver.
This method generalizes further to finding the roots of polynomials of arbitrary
degree as well as for factoring polynomials.
We illustrate this with a couple of examples:
Example 1: Solve the equation x2 ≡ 5 (modulo 361).
Solution: Note that 361 = 192 . We first solve x2 ≡ 5 (modulo 19). We find 59 ≡ 1
(modulo 19) so that 5 is a quadratic residue. Multiplying by 5 on both sides, we
obtain 55 ≡ 9 modulo 19.
Now we write x = 19y + 9 and substitute to obtain:
That is:
Solution: We have 289 = 172 . We first solve the equation x3 ≡ 3 (mod 17). The
exponent 3 is co-prime to 16, so we first solve the auxiliary equation 3k + 16l = 1,
which gives us k = 5, l = −1 as a solution. We now raise our congruence equation to
the 5th power on both sides to obtain:
1
Since x16 ≡ 1 (mod 17), this is equivalent to x ≡ (mod 17), which gives us x ≡ 7
35
(mod 17).
From this we solve for y to find y ≡ 9 (mod 17), so that x ≡ 160 (mod 289) is a
solution.
x2 = a
in Zp .
The gcd is found by repeatedly squaring (x − r)(p−1)/1 modulo (x2 − a) (in Zp [x]).
Note that (sx + t)2 ≡ 2stx + s2 a + t2 modulo (x2 − a); this expression may also be
used to find the successive remainders.
If the gcd in step 2 is 1 instead, then we repeat the algorithm with another random
value of r, until success. The probability that the above algorithm succeeds is at
least 1/2.
Example: x2 = 10 in Z41
4.9 A second algorithm for finding square-roots 41
(x − 2)2 ≡ 14 − 4x
(x − 2)4 ≡ (14 − 4x)2 ≡ 28 + 11x
(x − 2)8 ≡ (28 + 11x)2 ≡ x + 26
(x − 2)16 ≡ (x + 26)2 ≡ 11x + 30
Thus, (x − 2)20 ≡ (11x + 28)(11x + 30) ≡ 23x. The gcd of (x2 − 10) and (x − 2)20 − 1
is gcd(x2 − 10, 23x − 1) = (x + 16).
Thus the square-roots of 10 in Z41 are ±16.
5. Finite Fields
In this chapter, we study finite fields which are of importance in several areas of
computer science, such as coding theory, cryptography and complexity theory. This
will also lead us to a factorization algorithm for polynomials in Zp [x].
5.1 Groups
In abstract algebra, the most basic objects are groups.
Definition 5.1 A group is a pair (G, ∗), where G is a set and ∗ is a binary operation
on G which satisfies all of the following properties.
(a) Closure: For all g, h ∈ G, we have g ∗ h ∈ G.
(b) Associativity: For all g, h, k ∈ G, we have: g ∗ (h ∗ k) = (g ∗ h) ∗ k.
(c) Identity: There is a unique element e ∈ G such that for all g ∈ G, we have:
g ∗ e = e ∗ g = g.
(d) Inverse: For every element g ∈ G, there is an element h such that g ∗ h =
h ∗ g = e. This element is usually denoted by g −1 .
Examples of groups:
1. (R, +), (C, +), (Q, +), (Z, +), (Rn , +), (R[x], +)
2. (R \ {0}, ×), (C \ {0}, ×), (Q \ {0}), ×)
3. (Zn , +) (the cyclic group), (Z∗n , ×)
4. For every fixed n, the set of all n × n matrices over R under addition forms a
group.
5. For every fixed n, the set of all non-singular n × n matrices over R under
multiplication forms a group.
6. The set of all permutations of {1, 2, . . . , n} under composition forms a group;
this group is called the symmetric group and is denoted by Sn .
7. (R, ∗) where a ∗ b = a + b + 1.
44 Chapter 5. Finite Fields
Non-examples:
1. The set {0, 1, 2} under addition is not a group because it fails the Closure
property (a).
2. (R, −),(C, −) are not groups because they fail the Associativity property (b).
3. The pair (R, ∗) with ∗ being defined as a ∗ b = 1 is not a group because it fails
to have an Identity element.
4. (2S , ∪), (2S , ∩) are not groups because they fail the existence of an inverse for
every element.
5. The sets R, C, Zn are not groups under multiplication but note that we can
obtain groups from them by removing the elements without an inverse (Zero
in the first two cases, elements having a common factor with n in the third).
Here’s an example: we define a two-element group G = ({a, e}, ∗) (with e being the
identity element) using the following table:
∗ e a + 0 1
e e a Now consider the Cayley table of the group (Z2 , +). 0 0 1
a a e 1 1 0
We may notice that the tables of the two groups are identical except for a changing
of the element names (with 0 in place of e and 1 in place of a, + in place of *).
When this happens for two groups, we call them isomorphic. Here’s a more formal
definition.
Definition 5.2 Two groups (G, ∗) and (H, ·) are said to be isomorphic if there
exists a bijection ϕ : G → H such that the following holds:
For every g1 , g2 , g3 ∈ G, g1 ∗ g2 = g3 if and only if ϕ(g1 ) · ϕ(g2 ) = ϕ(g3 ).
Examples of isomorphism:
From this definition, it is not clear how to construct subgroups of a given group, but
one method is the following. Let S ⊆ G. We define the group generated by S as:
As in the case of direct product, we can easily verify that (< S >, ∗) satisfies the
group axioms, and is hence a subgroup of G. Conversely, if H is a subgroup of G
and < S >= H, then we call < S > a generating set for H.
Examples of subgroups:
Group Subgroup(s)
(R, +) Q, Z
(Z, +) 2Z, 3Z, 4Z, . . .
(Z6 , +) < 3 >= {0, 3} ,< 2 >= {0, 2, 4}
(Z∗p , ×) Quadratic residues
(Z2 × Z2 , +) < (1, 1) >= {(1, 1), (0, 0)}
GLn (R) SLn (R)
GLn (R): Real, invertible n × n matrices; SLn (R): Real n × n matrices with determi-
nant 1
Proof of Theorem 8: Lemma 9 implies that the left cosets of G form a partition of
G. Thus, we can write G = H ∪ g1 ∗ H ∪ . . . ∪ gr−1 ∗ H for some g1 , . . . , gr−1 .
Each coset has the same size, i.e. |H|. Thus, we obtain: r|H| = |G|, i.e. |H| divides
|G|, as desired.
The second part follows by considering the subgroup H = {a, a2 , . . . , ak = e} (for some
k ∈ N). We then have: k divides |G|, and hence a| G| = e. ■
5.2 Rings
Definition 5.6 A ring is a triple (R, +, ·) with +, · being binary operations on R,
satisfying the following properties.
1. (R, +) is an abelian group; its identity element is denoted by 0.
2. (R, ·) is associative and has an identity element which is denoted by 1.
3. For all a, b, c ∈ R, we have: a · (b + c) = a · b + a · c and (a + b) · c = a · c + b · c.
Examples of rings: In each example, the addition and multiplication are the
natural operators.
1. Z, R, C
2. Z[x], R[x]
3. Zn , Zn [x] (for every fixed n)
4. Zn [x]/(f (x)), i.e. the ring of polynomials in Zn [x] modulo f (x)
5. The ring of n × n real matrices (for each fixed n)
5.3 Fields
A field is a ring with the additional property that every non-zero element has a
multiplicative inverse.
Definition 5.7 A field is a triple (F, +, ·) with +, · being binary operations on R,
satisfying the following properties.
1. (F, +) is an abelian group; its identity element is denoted by 0.
2. (F \ {0}, ·) is a group, whose identity element is denoted by 1.
3. For all a, b, c ∈ F , we have: a · (b + c) = a · b + a · c and (a + b) · c = a · c + b · c.
• If F is a field, then the Euclidean algorithm and Bezout’s Lemma work for
polynomials in F [x].
• If F is a field, then Gaussian elimination (and all of linear algebra) work over
F in the same way as for reals.
Theorem 5.1 If F is a field, then F [x]/(f (x)) is a field if and only if f (x) is an
irreducible polynomial in F [x].
Proof. Suppose that f (x) is an irreducible polynomial. Let g(x) ∈ F [x]/(f (x)) be
a polynomial of degree less than deg(f ). Then gcd(g(x), f (x)) = 1 and in F [x],
we can apply Bezout’s Lemma to obtain polynomials u(x) and v(x) such that
f (x)u(x) + g(x)v(x) = 1. That is, g(x)v(x) ≡ 1 (mod (f (x))), so that g(x) has a
multiplicative inverse in F [x]/(f (x)). This shows that F [x]/(f (x)) is a field.
Conversely, suppose that f (x) is not irreducible, let f (x) = g(x)h(x) for a polynomial
g(x) of degree less than deg(f ). Then g(x) does not have a multiplicative inverse in
F [x]/(f (x)) and hence F [x]/(f (x)) is not a field. This completes the proof. ■
•
k
(a): If f (x) is an irreducible polynomial of degree k, then f (x) divides xp − x.
•
k
(b): xp − x = d|k {f (x) : f (x) is monic and irreducible, deg(f) = d}.
Q Q
In this chapter, we’ll see the Cantor-Zassenhaus algorithm for factoring univariate
polynomials in Zp [x].
We divide the algorithm into three components/phases and finally combine them
together into a single algorithm.
In the first phase, given a polynomial f (x) ∈ Z[x], we’ll obtain a polynomial g(x)
which is the square-free part of f (x). The square-free part of a polynomial is defined
as follows. Let f (x) = h1 (x)e1 . . . hk (x)ek with hi (x) being distinct irreducibles and
ei s being natural numbers. Then the square-free part of f (x) is the polynomial
g(x) = h1 (x) . . . hk (x).
In the second phase, we’ll partition the square-free polynomial g(x) into f1 (x)f2 (x) . . . fr (x),
where fi (x) is the product of all monic irreducible factors of g(x) that have degree
equal to i.
In the third phase, we’ll factor each fi (x) into irreducible factors of degree i.
Finally, for each irreducible factor h(x) that divides f (x), we find the largest natural
number r such that h(x)r divides f (x).
We now describe algorithms for each of the three phases.
To deal with this issue, we do the following: we first obtain the largest degree
polynomial h(x) such that h(x)p divides f (x), then find the square-free part of h(x)
recursively. We then multiply this by the square-free part of g(x) to obtain the
square-free part of f (x).
For the first step in the above idea, we use the following claim.
Claim 12 Let f (x) ∈ Zp [x] and let h(x) be the largest degree polynomial such
that f (x) is divisible by h(x)p . Then there exists k such that f (k) (x) = h(x)p and
f (k+1) (x) = 0. Here f (j) (x) denotes the jth derivative of f (x).
To check whether a given polynomial in Zp [x] is a pth power and to finds its pth
root, we will use the following observation.
Claim 13 Let f (x) = ∈ Zp [x]. Then f (x) is equal to g(x)p for some
Pd i
i=0 ai x
polynomial g(x) if aand only if: for every i, ai ̸= 0 implies p|i. Further, in this case,
the polynomial g(x) equals p|i ai xi/p .
P
We now have all the ingredients to find the square-free part of a given polynomial
f (x) ∈ Zp [x].
Repeating this idea, suppose! that we have found f1 (x), . . . , fi (x). Then fi+1 (x) =
f (x) i+1
gcd , xp − x . We note that in finding the gcds, we shall use re-
f1 (x) . . . fi (x)
peated squaring for the power of x and subsequently apply the Euclidean algorithm.
Zp [x]/(h(x)) ∼
= Zp [x]/(h1 (x)) × . . . × Zp [x]/(hk (x)).
Finally, for each h(x) in the list of irreducible factors obtained from Algorithm 8,
we find the largest exponent e such that h(x)e divides f (x). This completes the
description of the factorization algorithm for polynomials in Zp [x].
Remarks: In the above descriptions, we assumed that p is an odd prime. If p = 2,
then a small change is needed in Phase 3; however we skip the details. This method
also:
1. works over any finite field (apart from Zp );
2. can be combined with Hensel lifting to factorize polynomials in Zpk ;
3. can be extended to factorize polynomials in several variables over a finite field.
II
Quadratic Equations in Two Variables
Proof. Suppose for contradiction that n = ab, where a > 1 is odd. Then we have:
2n + 1 = 2ab + 1 = (2b )a + 1, which is divisible by 2b + 1, because more generally if a is
odd, then xa + 1 is divisible by (x + 1) (using the remainder theorem for polynomials).
This contradicts the assumption that 2n + 1 is prime. Therefore n cannot have any
odd divisors and must be a power of 2. ■
A factor of 232 + 1: How did Euler find that 641 was a factor of 232 + 1? Here’s an
n
observation: suppose that p|22 + 1. Then by considering the order of 2 with respect
to p, we note that 2n+1 must divide p − 1, i.e. p ≡ 1 (mod 2n+1 ). Thus it is sufficient
to look for such factors. For n = 5, it is sufficient to look for numbers of the form
56 Chapter 7. Primality Testing: Before 2002
64k + 1. We can speculate that Euler tested small divisibility for small values of k
and found a hit for k = 10.
Let’s prove that 641 does divide 232 + 1. We have: 10 × 26 ≡ −1 (mod 641), that
is: 5 × 27 ≡ −1 (mod 641). Raising both sides to the fourth power, we obtain:
625 × 228 ≡ 1 (mod 641). Since 625 ≡ −16 (mod 641), we obtain: −232 ≡ 1 (mod
641) which is the desired claim.
The largest explicit primes that we know are and have been Mersenne primes, and
the reason for this is that there is an algorithm running in time Õ(p2 ) time to check
whether 2p − 1 is prime.
Some large values of p such that 2p − 1 is prime: 13466917 (record-holder in
2001),32582657 (record-holder in 2006), 57885161 (record-holder in 2013), 82589933
(current record-holder). These primes are also the record-holders for the largest
known prime for those years.
composite.
To prove Claim 18, we observe that the set A is a subgroup of Zn ∗ and combining
this with Lagrange’s theorem, the claim follows.
U0 = 0, U1 = 1, Un = aUn−1 + bUn−2 ;
αn − β n
Un = √ .
d
a b
" #
Claim 20 Let M = .
1 0
Then
" #
Un bUn−1
M =
n
.
Un−1 bUn−2
The Lucas
! primality test: Fix a Lucas sequence U (a, b). Given an integer n, find
d
r= ; this is easier when d is a prime ≡ 1 mod 4. Then find Un−r mod n; if it is
n
zero, then n passes the test. When the sequence is Fn , the test is called Fibonacci
pseudo-primality test.
Remarks:
1. The calculations for Up−r (or for Vp−r ) essentially come from the identity
(x + a)p = xp + a in Zp [x], where we further reduced the term xp modulo some
quadratic polynomial. Writing F (x) = (x + a)p and G(x) = xp + a, the Lucas
test verifies that F (x) = G(x) modulo some quadratic polynomial. This identity,
which is true only for primes, is the starting point of the AKS algorithm, which
checks that F (x) = G(x) modulo h(x) for a bunch of polynomials h(x).
2. It appears that we do not know any composite n ≡ ±2 (mod 5), which passes
both the Fibonacci test as well as the test 2n−1 ≡ 1 (mod n); thus, performing
just these two tests should detect composites effectively for such values.
If you find such a composite n which does pass both tests, you win $620 with
$500 from Selfridge, and $20 from Pomerance. If you prove that no such
composite exists, you stilll win the same amount, with $500 from Pomerance,
and $20 from Selfridge. The remaining $100 comes from Wagstaff in both
cases.
Zn , whereas if n is prime, then there are exactly two square-roots, namely −1, 1, in
Zn .
We also know that if n is prime, then for a < n, we must have: an−1 ≡ 1 (mod
n). Thus, the idea is to consider the numbers a(n−1)/2 , a(n−1)/4 , . . ., until we find
a number c which is not equal to 1. If c ̸= −1, then we know that n is not prime.
However, it may also happen that c = −1 for composites n. The claim however is
that c ̸= −1 with sufficiently large probability if n is composite and if a is a random
element of Zn .
We first describe the algorithm in detail.
Proof. If n is prime, then consider the values of c, b at the end of the while loop. If
the while loop terminates because b = 1, then the value of b must be equal to 1 and
c is a square-root of 1 in Zn . Further c =
̸ 1 since the algorithm terminates the first
time that b becomes 1. Thus c = −1 and the algorithm goes to step 18.
If the while loop terminates because i = t, then by Fermat’s little theorem, the value
t
of b must still be 1 at the end of the while loop, because am2 = ap−1 ≡ 1 (mod p).
As before, the algorithm goes to step 18.
Now, suppose that n is composite. If n is a perfect power of a prime, then the
algorithm outputs COMPOSITE in step 3. Thus, suppose that n is composite and
has at least two prime factors. Let A = {s ∈ Z∗n |sn−1 = 1}. If |A| ≤ |Z∗n |/2, then
with probability at least 1/2, the algorithm will return COMPOSITE in line 13.
Otherwise, using Claim 18, we deduce that A = Z∗n .
Let r ≥ 4 be the number of square-roots of unity in Zn . For d ∈ N, we define the set
Sd = {r ∈ Z∗n : rd = 1}. Note that Sd is a subgroup of Z∗n and also that Sn−1 = A = Z∗n .
We also have: if d = d1 d2 such that gcd(d1 , d2 ) = 1, then Sd is isomorphic to Sd1 × Sd2 .
Thus, we get: Z∗n = Sn−1 is isomorphic to S2t × Sm .
Consider the directed graph G = (V, E) where V = S2t and E = {(a, a2 )|a ∈ S2t }, with
calculations modulo n. Then the underlying graph of G is a tree (with a self-loop at
1); if we fix 1 as the root, then it is a rooted tree T with the parent of vertex a being
a2 . Also by our deduction that Z∗n is isomorphic to S2t × Sm , we find that the set
{am : ainZ∗n } is equal to V .
Thus, in terms of the tree T , the algorithm picks a random vertex in V and traverses
up the tree until it reaches the root. For an element a ∈ V , let T (a) denote the subtree
rooted at a. To show that the algorithm returns COMPOSITE with probability at
least 1/2, we must show that |T (−1)| ≤ |V |/2.
The root vertex has d − 1 children, let them be a1 = −1, a2 , . . . , ad . Ever other node
in the tree has either zero children (if it is not a square in Zn ) or d children (if it is a
square in Zn ). Since d ≥ 4, if we show that the height of T (ai ) is greater than or
equal to the height of T (−1) for each i ≥ 2, this would imply that |T (−1)| ≤ |V |/3.
k
Let T (−1) have height k. That is, there exists an element α ∈ Zn such that α2 = −1.
Let n = pe11 . . . perr . Let ϕ be the natural isomorphism from Zn to Zpe1 × . . . × Zperr ,
1
i.e. ϕ(x) = (x1 , . . . , xr ) where x ≡ xi (mod pei i ). Notice that ϕ(−1) = (−1, . . . , −1),
whereas for i ≥ 2, ϕ(ai ) is a tuple consisting of 1s and -1s, with at least one -1 and
at least one 1. Suppose that ϕ(α) = (α1 , . . . , αr ). Let β be such that ϕ(β)j = αj
whenever ϕ(ai )j = 1 and let ϕ(β)j = 1 whenever ϕ(ai )j = −1. Then we can observe
k
that β 2 = ai . Thus, the height of T (ai ) is greater than or equal to the height of
T (−1). This shows that |T (−1)| ≤ |V |/3 and completes the proof of Claim 21.
■
8. The Integer Factoring Problem
proof of the lemma but we note that an exact expression for the desired probability
(N − 1)(N − 2) . . . (N − k + 1)
is 1 − .
Nk
Lemma 23 Given a function f : {0, 1, . . . , N − 1} → {0, 1, . . . , N − 1} and a sequence
a1 , . . . , am where ai = f (ai−1 ) for every i ≥ 2, there’s an algorithm which can test in
O(m) time whether there exist distinct i, j such that ai = aj and further find such
i, j if they exist.
Proof. The problem described in the statement is known as the cycle detection
problem and the solution that we will describe is known as Floyd’s cycle detection
method.
Let j be the least index such that aj is equal to some previous element, say ai ,
and let L = i − j + 1. Then for every k ≥ i, we have ak = ak+L , while the elements
a1 , . . . , ai−1 appear only once in the sequence. Note that even though the sequence
given to us is only the first m elements, the elements am+1 , . . . are also well-defined
by the relation ai = f (ai−1 ).
We claim that there exists t ≤ m such that at = a2t . Let t = i + T . Then at = ar
where r = T %L and a2t = as , where s = (i + 2T )%L. Thus, if T ≡ i + 2T (modulo L),
then we have at = a2t , and this certainly happens when T ≡ −i (mod L), i.e. when t
is of the form t = qL.
Now, we describe the algorithm. For t = 1, 2, . . ., we consider the pair of elements
at , a2t in the tth iteration. If at =
̸ a2t for t ≤ m, then we conclude that all the elements
are distinct.
Otherwise, the least t for which at = a2t must be the value of L We may know
find the value of i in any number of ways, for example, by considering the pairs
(at−1 , a2t−1 ), (at−2 , a2t−2 ) etc until the values in the pair are different.
The number of iterations in this algorithm is at most 2L ≤ 2m; this completes the
proof of the lemma. ■
The time complexity of the algorithm is O(m) = O(n1/4 ), where we use Floyd’s
algorithm for Step 7. Suppose that n is composite and p is the least prime factor of
8.3 Dixon’s Algorithm 63
√
n. Then p ≤ n and hence by Lemma 22, with probability at least 0.6, there exist
i, j such that ai ≡ aj (mod p) so that gcd(ai − aj , n) ̸= 1 with probability at least 0.6.
972 × 1072 × 1732 × 2062 ≡ 214 × 36 × 232 × 732 (mod n). (8.7)
Thus, we have α2 ≡ β 2 (mod n), where α = 97 × 107 × 173 × 206 ≡ 768 (mod n) and
β = 27 × 33 × 23 × 73 ≡ 1289 (mod n). We find gcd(α − β, n) = 521 which is a divisor
of n.
Now we explain the idea. Firstly, we compute several random squares modulo n.
We then maintain a list of those which have prime factors only in {p1 , . . . , pk }, where
pi is the ith prime number and where the threshold k is fixed in advance. We also
maintain the list of their corresponding factorizations.
Let a number in the list have the factorization pe11 . . . pekk . Consider the exponent
vector (e1 , . . . , ek ). A key idea is that if there are at least k + 1 such exponent vectors,
then there must be a subset of them whose sum is an even number in every co-
ordinate. This is because the vector space Fk2 can have at most k linearly independent
vectors.
We then multiply the corresponding relations; this gives us on the RHS a number
2fk f1 fk
of the form p2f 1 2f2
1 p2 . . . pk , which is a square. Thus, we can set β = p1 . . . pk . The
linear relation among the exponent vectors (modulo 2) can be found by Gaussian
elimination.
We know describe the algorithm in detail.
64 Chapter 8. The Integer Factoring Problem
We first analyze the running time of in terms of n, B, m without fixing the values of
B, m. We do not explicitly include the cost of arithmetic operations in Zn which we
know to be Õ(log2 n).
We thus find that the total time complexity is O(mB + B 2 ) with some additional
poly(log n) factors. Next, we need to fix m, B as functions of n.
The probability that the algorithm succeeds is related to the probability of finding at
least k +1 numbers among a1 , . . . , am which are B-smooth, and further the probability
that the final congruence α2 ≡ β 2 (mod n) gives us a non-trivial factor of n. We’ll
only focus on the first part.
Let S(n, B) denote the number of B-smooth numbers in {1, 2, . . . , n}. Then the
expected number of random elements we must pick in order to find k + 1 numbers
(k + 1)n
that are B-smooth is . Thus, the choice of m will be a constant multiple of
S(n, B)
the above expression. We also need an estimate on S(n, B) which is given below.
n
Lemma 24 If B = n1/u , then S(n, B) ∼ u+o(1) .
u
Writing B = n1/u and applying the above expression, we get the running time in
8.3 Dixon’s Algorithm 65
terms of n, u to be:
T (n, u) = Õ uu+1 n2/u .
Now we can find u that minimizes T (n, u) by taking logarithms and then s differen-
log n log n
tiating with respect to u. This gives us: log u − 2 ∼ 0, so that u ∼ .
√u log log n
Thus, we get the running time to be T (n) = Õ e2 log n log log n . This completes our
analysis of Dixon’s algorithm.
9. Primality Testing: The AKS
algorithm
In 2002, Manindra Agrawal, Neeraj Kayal and Nitin Saxenah came up with the first
(and only) known deterministic polynomial time algorithm. It is now commonly
referred to as the AKS algorithm.
(x + a)n = xn + a (9.1)
holds in Zn [x].
!
p
Proof. ⇒: Let n = p be prime. Then in Zp [x], we have: (x + a)p = xp + p−1 +
P
i=1
!
i
p
ap = xp + a. The last equality follows from the fact that p divides for 1 ≤ i ≤ p − 1
i
and from the fact that ap ≡ a (mod p).
⇐: Let n be composite with a prime factor p and ! let p be the largest power of p
k
n n(n − 1) . . . (n − p + 1)
that divides n. Then the coefficient of xp in is equal to
p p!
which is divisible by p k−1 but not by p . Thus this coefficient is non-zero in Zn , and
k
(x + a)n ̸= x + a. ■
9.3 Correctness
We will show that the algorithm returns PRIME if and only if n is prime. We will
also prove why r exists as in step 3.
If n is prime, then the algorithm clearly does not return COMPOSITE in lines 2,5.
It also does not return COMPOSITE in line 9 because of Lemma 25.
Suppose now that n is composite. If the algorithm does not return COMPOSITE in
line 2, then n must have at least two distinct prime factors; let p denote the least
prime factor of n.
Definition 9.1 Let r be a fixed positive integer. Let m ∈ N, f (x) ∈ Zp [x]. We say
that the pair (m, f (x)) is introspective (with respect to p) if
Examples:
• (1, f (x)) for every f (x), p, r;
• (p, f (x)) for every f (x), p, r;
• (m, x) for every m, p, r;
• (561, x + 1) for p = 3, 7, 11 and r = 4.
The connection of this definition to the algorithm is that in line 9, we are checking
whether the pair (n, x + a) is introspective.
9.3 Correctness 69
The sets I, P are infinite; we first define some finite subsets of them. We define
G to be the set obtained by considering the values of I modulo r. Formally,
G = {a ∈ Zr : a ≡ i mod r for some i ∈ I}. Note that G is a subgroup of Z∗r under
multiplication.
Let h(x) be an irreducible factor of xr − 1 of largest degree. We define R to
be the set obtained by considering the values of P modulo h(x). Formally, R =
{f (x) ∈ Zn [x]/(h(x)) : f (x) ≡ g(x)mod h(x) for some g(x) ∈ P }. We note that R is
a subgroup of (Zn [x]/(h(x)))∗ .
We now make three claims about the sizes of |G|, |R|.
Claim 28 Let t = |G|. Then t > 4 log2 n.
t+k−2
!
Claim 29 |R| ≥ .
k−1
!√t
n2
Claim 30 |R| ≤ .
2
Assuming the claims, the contradiction follows from Claims 29 and 30 after substitut-
ing the known bounds on t, r, k. We skip this calculation and instead give sketches
70 Chapter 9. Primality Testing: The AKS algorithm
Proof. of Claim 28. Let d = ordr (n). Then the elements 1, n, . . . , nd−1 are all distinct
modulo r and from line 3, we have: ordr (n) > 4 log2 n. This proves the claim. ■
Proof. of Claim 29. Consider the polynomials in P of degree less than t. The number
t+k−2
!
of such polynomials is equal to . All these polynomials are distinct modulo
k−1
h(x) if deg(h(x)) ≥ t. Thus it suffices to prove that deg(h(x)) ≥ t. This can be done
by considering the minimal polynomial of an element in Zr that generates G, but we
skip the details. ■
√
Proof. of Claim 30. Let Iˆ = {ni pj |0 ≤ i ≤ j ≤ ⌊ t⌋}. We have |I|
ˆ > t and therefore
there exist distinct m1 , m2 ∈ Iˆ such that m1 ≡ m2 (mod r).
Let f (x) be an arbirary polynomial in R. Since f (x) is introspective, we have:
Also, since m1 ≡ m2 (mod r), we have xm1 ≡ xm2 (mod xr −1), and hence: xm1 ≡ xm2
(mod h(x)).
Thus, we find that f (x)m1 − f (x)m2 ≡ 0 (mod h(x)). Now consider F = Zp [x]/(h(x))
which is a field. Seeing f (x) as an element of F , we have that f (x) is a root of the
polynomial Y m1 − Y m2 .
Since F is a field, the polynomial Y m1√− Y m2 has at most max(m1 , m2 ) roots. Thus
! t
√ n2
|R| ≤ max(m1 , m2 ) ≤ (np) t ≤ . ■
2
With the proof of the claims, we have shown that if n is composite, then the algorithm
will return COMPOSITE.
We now consider the problem of showing that there exists r < 16 log5 n such that
ordr (n) > 4 log2 n. For this, we the following lemma is useful.
Lemma 31 Let n ≥ 1 be a natural number. Then lcm(1, 2, . . . , n) ≥ 2(n−1)/2 .
1
Proof. Let L = lcm(1, 2, . . . , 2n + 1), and let I = 01 xn (1 − x)n dx. Then 0 < I < n
R
4
and LI is a positive integer. Thus, L > 4n , from which the statement in the lemma
may be obtained. ■
Let D = ⌊4 log2 n⌋ and R = log5 n. Suppose that every r ∈ {1, 2, . . . , R} divides some
number nd − 1 for d ≤ D. Then LCM (1, 2, . . . , R) must divide (n − 1)(n2 − 1) . . . (nD −
D2
1). Thus, LCM (1, 2, . . . , R) ≤ n 2 . Applying Lemma 31 for the LHS and substituting
for D, we may obtain the desired result.
This completes the proof sketch of the correctness of the AKS algorithm. ■