Why don't we just apply Fermat's little theorem directly in Miller-Rabin primality test?

84 Views Asked by At

Fermat's little theorem states that for $2$ integers $a$ and $n$ such that $n$ is a prime that doesn't divide $a$, $a^{n-1}\equiv 1\pmod{n}$. So in Miller-Rabin test, if we want to test the primality of an integer $p$ (assuming $p>2$, $p$ is odd, $q$ is an odd integer, and $k$ is a positive integer where $p-1=2^{k}q$), we know that this must hold: $a^{p-1}\equiv 1\pmod{p}$ for any integer $a$ not divisible by $p$.

$\because\, \sqrt{1}$ has $2$ solutions $\bmod p$ ($1$ and $-1$)

$\therefore$ either $a^q\equiv 1\pmod{p}$ or $a^{2^{j}q}\equiv p-1\pmod{p}$ (for some $j$ where $0\leq j<k$). So that when this element is squared further, it will always give 1 up to $a^{p-1}$.

My question is why don't we just calculate $a^{p-1}$ directly and check if it is congruent to $1\pmod{p}$? Knowing that if $a^{p-1}\equiv 1\pmod{p}$, then for sure either $a^q\equiv 1\pmod{p}$ or $a^{2^{j}q}\equiv p-1\pmod{p}$ (for some $0\leq j<k$).