Lecture from: 17.04.2025 | Video: Homelab
Motivation for Advanced Primality Tests
Simple approaches to primality testing, such as checking for small divisors or relying on basic properties like for randomly chosen , can be unreliable.
For instance, a test declaring prime if a randomly chosen satisfies , and composite otherwise, has a significant error probability for composite numbers.
The probability of incorrectly declaring a composite as prime (i.e., choosing an such that ) is . For certain composite numbers, this value can be very close to 1. Consider for a prime ; here, , and the error probability becomes , which approaches 1 for large .
This high error rate necessitates more sophisticated methods, leading us to tests based on Fermat’s Little Theorem and related group-theoretic principles.
The Fermat Primality Test
The Fermat primality test leverages consequences of Lagrange’s theorem in finite groups, specifically within the multiplicative group of integers modulo .
Group Theoretic Foundations
Consider the set . This set forms a finite abelian group under multiplication modulo .
The group properties are:
- Closure: If , then , so .
- Associativity: Multiplication modulo is associative.
- Identity Element: serves as the identity, as .
- Inverse Element: For each , since , Bézout’s identity guarantees the existence of integers such that . Thus, , meaning is the multiplicative inverse of in .
The order of this group, denoted , is given by Euler’s totient function, . For example, for , . The order is .
A fundamental result from group theory is Lagrange’s Theorem, which states that if is a subgroup of a finite group , then the order of , , divides the order of , .
An important consequence relates to the order of an element , denoted , which is the smallest positive integer such that (the identity element). The set forms a cyclic subgroup generated by , with order . By Lagrange’s Theorem, must divide .
Applying this to , for any , divides . This implies that . This result is known as Euler’s Totient Theorem.
Fermat’s Little Theorem
Fermat’s Little Theorem is a special case of Euler’s Totient Theorem. If is a prime number, then every integer such that is coprime to .
Thus, , and its order is .
Euler’s theorem then directly yields Fermat’s Little Theorem: If is prime and is an integer not divisible by (i.e., ), then .
The Fermat Test Algorithm
Fermat’s Little Theorem provides a basis for a primality test. If were prime, then should hold for any not divisible by .
The Fermat primality test for a given integer proceeds as follows:
- Choose a random integer in the range . (Values and are avoided as and are often for composites too.)
- Compute . If , then has a non-trivial factor (or a factor of ), so is composite.
- If , compute (typically using fast modular exponentiation).
- If , then violates Fermat’s Little Theorem, so must be composite. Such an is called a Fermat witness to the compositeness of .
- If , then behaves like a prime for this base . In this case, is declared “probably prime”.
Error Analysis of the Fermat Test
If is prime, then by Fermat’s Little Theorem, for all . So, a prime number will always pass the test (be declared “probably prime”), assuming which is true for these .
If is composite, the test can still pass. If for a composite and a base with , then is called a Fermat liar for , and is a Fermat pseudoprime to base .
The critical weakness of the Fermat test lies with Carmichael numbers. These are composite numbers such that for all integers with .
For such numbers, the Fermat test will declare them “probably prime” for any chosen base coprime to , making the test completely ineffective. The smallest Carmichael number is .
Formally, let . This set is the set of Fermat liars in . It can be shown that is a subgroup of .
A composite number is a Carmichael number if and only if .
If is composite and not a Carmichael number, then is a proper subgroup of . By Lagrange’s Theorem, the order of this subgroup must divide , so .
The probability of error (picking an that is a Fermat liar) is if we consider choices from or more simply, if is chosen uniformly from , the probability of picking a liar is . If is chosen from , the error probability is at most , which is generally less than .
It’s commonly stated that for a non-Carmichael composite , the probability of error is at most . Repeating the test times with independent random bases reduces this error probability to at most .
The Miller-Rabin Primality Test
The Miller-Rabin test is a more sophisticated probabilistic primality test that strengthens the Fermat test by also checking for non-trivial square roots of 1 modulo . This allows it to correctly identify Carmichael numbers as composite.
The Key Principle: Square Roots of Unity Modulo a Prime
A crucial property used by the Miller-Rabin test is: if is a prime number, the only solutions to the congruence are and . This is because implies . Since is prime, must divide or must divide . Thus, or . If is composite, there can be other solutions. For example, if (distinct primes), then can have four solutions. The Miller-Rabin test exploits this: if it finds an such that , then must be composite.
Algorithmic Setup
Let be an odd integer we want to test for primality (even numbers are easily handled).
First, write in the form , where is odd and (since is even).
For a chosen base (where ), consider the sequence of values:
If is prime, then by Fermat’s Little Theorem, , so .
Now, consider this sequence .
If is prime, one of two conditions must hold:
- .
- Or, there exists some such that . If this is true, then , and all subsequent terms for will also be 1.
If neither of these conditions is met, cannot be prime. Specifically, if but for all , , and also , then there must be some for whose predecessor was not . This would be a non-trivial square root of , proving is composite. If , then is composite by Fermat’s test itself.
The Miller-Rabin Test Algorithm
Given an odd integer to test and a base :
- Handle trivial cases: If , it is prime. If is even (and ) or , it is composite. (This algorithm assumes is odd and .)
- Find integers and (odd) such that .
- Compute .
- If or (which is ), then passes the test for this base and is declared “probably prime”.
- Otherwise (if and ), proceed to iterate times (for from to ):
- Set . (This computes ).
- If , then passes the test for base and is declared “probably prime”. (We found an ).
- If , then the previous value of (which was ) was neither nor , but its square is . Thus, has a non-trivial square root of , so is composite.
- If the loop completes (meaning was never in step 5.2, and never in step 5.3 after ), then is composite. (This implies that either , or but we have , which makes a non-trivial square root of 1, or for any and .)
Strength and Error Bound of Miller-Rabin Test
The Miller-Rabin test is a significant improvement over the Fermat test.
If is prime, it will always correctly declare “probably prime” for any base . If is composite, the probability that the Miller-Rabin test incorrectly declares “probably prime” (i.e., is a strong liar for ) is at most . This bound holds for all composite , including Carmichael numbers.
By repeating the test times with independent random bases , the probability of error (falsely declaring a composite as prime) can be reduced to at most . This makes it a very reliable probabilistic primality test for practical purposes.
In summary, while the Fermat test is conceptually simpler and faster for a single iteration if is used directly, its failure on Carmichael numbers is a critical flaw. The Miller-Rabin test, by more deeply investigating the structure of square roots of unity modulo , provides a much stronger guarantee of correctness, effectively handling Carmichael numbers and offering a low, bounded error probability for all composite numbers.
Continue here: 19 Long-Path Problem and Reduction to Hamiltonian Cycle, Solving Hamiltonian Cycle using DP, Long-Path via Randomized Coloring