# Primality Testing

## The Problem of Recognizing Primes

A prime number is an integer greater than $1$ whose only positive divisors are

$$
1
\quad\text{and}\quad
p.
$$

Prime numbers are the fundamental building blocks of arithmetic because every integer factors uniquely into primes.

One of the oldest computational problems in number theory is therefore:

Given an integer $n$, determine whether $n$ is prime.

This is the primality testing problem.

For small integers, direct trial division suffices. For very large integers, especially those used in cryptography, sophisticated algorithms are required.

Modern primality testing combines algebra, modular arithmetic, analytic number theory, and computational complexity theory.

## Trial Division

The simplest method tests divisibility by integers up to

$$
\sqrt{n}.
$$

If no divisor is found, then $n$ is prime.

This works because if

$$
n=ab,
$$

then at least one of $a$ or $b$ satisfies

$$
a\leq\sqrt{n}.
$$

Although conceptually simple, trial division is extremely inefficient for large integers. Its running time grows exponentially in the bit length of the input.

Thus more advanced methods are necessary.

## Fermat’s Little Theorem

One of the first major tools in primality testing is Fermat’s little theorem.

If $p$ is prime and

$$
a\not\equiv0\pmod p,
$$

then

$$
a^{p-1}\equiv1\pmod p
$$

This gives a necessary condition for primality.

If an integer $n$ fails the congruence

$$
a^{n-1}\equiv1\pmod n,
$$

then $n$ is certainly composite.

Numbers satisfying the congruence for a particular base $a$ are called pseudoprimes to base $a$.

## Carmichael Numbers

Unfortunately, some composite numbers satisfy Fermat congruences for many bases.

A Carmichael number satisfies

$$
a^{n-1}\equiv1\pmod n
$$

for every $a$ coprime to $n$.

The smallest example is

$$
561=3\cdot11\cdot17.
$$

Carmichael numbers show that Fermat testing alone cannot provide a reliable primality test.

## Miller-Rabin Primality Test

The Miller-Rabin test strengthens Fermat’s criterion.

Suppose

$$
n-1=2^sd
$$

with $d$ odd.

For a chosen base $a$, one examines the sequence

$$
a^d,
\quad
a^{2d},
\quad
a^{4d},
\ldots,
\quad
a^{2^sd}
\pmod n.
$$

If $n$ is prime, this sequence must satisfy specific structural conditions.

Failure of these conditions proves compositeness.

The Miller-Rabin test is probabilistic:

- if it declares composite, the answer is certain,
- if it declares probable prime, the answer is extremely likely but not absolutely guaranteed.

Repeating the test with multiple bases reduces error probability exponentially.

In practice, Miller-Rabin is extremely effective and widely used.

## Deterministic Variants

For integers below certain bounds, carefully chosen bases make Miller-Rabin deterministic.

For example, all $64$-bit integers can be tested deterministically using only finitely many bases.

Thus practical implementations often combine deterministic small-base testing with probabilistic methods.

## Solovay-Strassen Test

Another probabilistic method uses Euler’s criterion and quadratic residues.

If $p$ is prime, then

$$
a^{(p-1)/2}
\equiv
\left(\frac{a}{p}\right)
\pmod p,
$$

where

$$
\left(\frac{a}{p}\right)
$$

is the Legendre symbol.

The Solovay-Strassen test compares these quantities modulo $n$.

Although historically important, it is generally slower and less practical than Miller-Rabin.

## Lucas Tests

Lucas primality tests use recurrence sequences and properties of finite groups.

These tests are particularly effective for numbers of special forms.

Many practical primality-proving algorithms combine Miller-Rabin tests with Lucas tests.

The resulting hybrid methods are extremely reliable.

## AKS Primality Test

A major breakthrough occurred in 2002 when entity["people","Manindra Agrawal","Indian computer scientist"], entity["people","Neeraj Kayal","Indian computer scientist"], and entity["people","Nitin Saxena","Indian computer scientist"] discovered the AKS primality test.

The AKS algorithm proved that primality testing belongs to deterministic polynomial time.

The key identity is:

$$
(x-a)^p\equiv x^p-a\pmod p
$$

for prime $p$.

The algorithm verifies suitable polynomial congruences modulo $n$.

This result solved a long-standing theoretical problem:

$$
\text{PRIMES}\in\mathbf{P}.
$$

Although AKS is theoretically profound, it is usually slower in practice than probabilistic methods.

## Elliptic Curve Primality Proving

Elliptic curves provide some of the fastest practical primality-proving algorithms.

The elliptic curve primality proving method (ECPP) uses arithmetic on elliptic curves over finite fields.

These algorithms can produce certificates of primality that may be verified efficiently.

ECPP is widely used for proving primality of very large integers.

## Special-Form Primes

Some primes possess forms allowing especially efficient tests.

### Mersenne Primes

Numbers of the form

$$
2^p-1
$$

are called Mersenne numbers.

The Lucas-Lehmer test determines primality of Mersenne numbers very efficiently.

The largest known primes are often Mersenne primes discovered through distributed computation projects.

### Fermat Numbers

Numbers of the form

$$
2^{2^n}+1
$$

are Fermat numbers.

Only a few are known to be prime.

## Complexity Considerations

Primality testing differs fundamentally from factorization.

Primality can be tested efficiently, but factoring large integers remains computationally difficult.

This asymmetry underlies RSA cryptography.

Given a large integer $N$:

- testing whether $N$ is prime is easy,
- factoring $N$ may be extremely hard.

This distinction is one of the central computational facts of modern number theory.

## Cryptographic Applications

Prime numbers are essential in cryptography.

Examples include:

| System | Role of Primes |
|---|---|
| RSA | Large secret primes |
| Diffie-Hellman | Prime finite fields |
| Elliptic curves | Prime-order groups |
| Digital signatures | Prime moduli |

Practical cryptography therefore depends heavily on fast and reliable primality testing.

## Random Prime Generation

Cryptographic systems often require random large primes.

The standard method is:

1. generate a random odd integer,
2. apply probabilistic primality tests,
3. repeat until a probable prime appears.

The prime number theorem implies that primes occur frequently enough for this approach to be efficient.

## Conceptual Importance

Primality testing illustrates a major theme of computational number theory:

deep theoretical mathematics can lead to highly practical algorithms.

The subject combines:

- modular arithmetic,
- finite fields,
- algebraic geometry,
- complexity theory,
- probability.

Primality testing also reveals an important distinction between existence and construction. Although primes are infinite and theoretically well understood, recognizing them efficiently required centuries of mathematical development.

