Department of Electrical Engineering, IIT Bombay
October 12, 2025
First efficient algorithms were developed in the 70s
Probabilistic algorithms with following guarantees
A deterministic polynomial-time algorithm was found in 2002 by Agrawal, Kayal, Saxena (AKS)
Fermat’s little theorem: If p is a prime and a is any integer not divisible by p, then a^{p-1} = 1 \bmod p.
The Primality Test
If a^{N-1} = 1 \bmod N, we get no information about the primality of N
Suppose a \in \{1,2,\ldots,N-1\} and a \notin \mathbb{Z}_N^*
Integers in \{1,\ldots,N-1\} not belonging to \mathbb{Z}_N^* are rare
If N is prime, then there are no such integers
For composite N = p_1^{e_1} \cdots p_k^{e_k}, we have |\mathbb{Z}_N^*| =\phi(N) =\prod_i p_i^{e_i-1}(p_i-1)
If p_1,p_2,\ldots,p_k are large primes, then the fraction of \{1,2,\ldots,N-1\} in \mathbb{Z}_N^* is close to 1
We say that an integer a \in \mathbb{Z}_N^* is a witness for compositeness of N if a^{N-1} \neq 1 \bmod N
\gcd(a, N) = 1 \implies \gcd(a^{N-1}, N) = 1
So the a’s in \mathbb{Z}_N^* may or may not be witnesses
Theorem If there exists a witness in \mathbb{Z}_N^* that N is composite, then at least half the elements of \mathbb{Z}_N^* are witnesses that N is composite
If a witness exists, the following primality test fails to detect the compositeness of N with probability at most 2^{-t}
Theorem: Suppose there exists a witness that N is composite. Then at least half the elements of \mathbb{Z}_N^* are witnesses that N is composite.
Proof
We say that x \in \mathbb{Z}_N^* is a square root of 1 modulo N if x^2 = 1 \bmod N.
Lemma: If N is an odd prime, then the only square roots of 1 modulo N are \pm 1 \bmod N.
For an odd prime N, we have N-1 = 2^r u where r \ge 1 and u is odd
Suppose a^{N-1} = a^{2^ru} = 1 \bmod N
Consider the sequence a^u \bmod N,a^{2u} \bmod N, \ldots, a^{2^r u} \bmod N
It can only be in one of the following forms
For an odd integer N, if we do not observe this sequence then N is composite
In other words, when N is an odd prime, the sequence a^u \bmod N,a^{2u} \bmod N, \ldots, a^{2^r u} \bmod N satisfies the following conditions
We say that a \in \mathbb{Z}_N^* is a strong witness that N is composite if
a^u \neq 1 \bmod N and
a^{2^i u} \neq -1 \bmod N for all i \in \{0,1,2,\ldots,r-1\}.
If we can find even one strong witness, we can conclude that N is composite.
Theorem Let N be an odd number that is not a prime power. Then at least half the elements of \mathbb{Z}_N^* are strong witnesses that N is composite.
Let \textsf{Bad} \subseteq \mathbb{Z}_N^* be the set of elements that are not strong witnesses
We can define a set \textsf{Bad}' such that
Input: Odd integer N > 1 and parameter 1^t
Output: A decision as to whether N is prime or composite \begin{align*} & \text{{Compute $r \ge 1$ and odd $u$ such that $N-1 = 2^ru$}}\\ & \textbf{if } \text{$N$ is a perfect power, \textbf{return} ``composite''}\\ & \textbf{for } j=1 \text{ to } t:\\ &\quad a \leftarrow \{1,\ldots,N-1\}\\ &\quad \textbf{if } \text{$a^u \neq \pm 1 \bmod N$ and}\\ &\quad \quad \text{$a^{2^iu} \neq -1 \bmod N$ for $i \in \{1,\ldots,r-1\}$ } \\ &\quad \quad \text{\textbf{return} ``composite''}\\ &\quad \textbf{end if } \\ &\textbf{end for } \\ & \textbf{return } \text{``prime''} \end{align*}