Skip to main content

SPsPDem

Strong Pseudoprime Test Demonstration

How the Strong Pseudoprime Test Works

The strong pseudoprime test (also known as the Miller-Rabin test) is a probabilistic primality test that determines whether a number is likely prime or definitely composite:
  1. 1First, we write m1=2jdm - 1 = 2^j \cdot d where dd is odd, factoring out all powers of 2
  2. 2We compute admodma^d \bmod m using fast modular exponentiation
  3. 3If ad±1(modm)a^d \equiv \pm 1 \pmod{m}, then mm passes the test (strong probable prime)
  4. 4Otherwise, we repeatedly square the result: a2d,a4d,a8d,a^{2d}, a^{4d}, a^{8d}, \ldots up to a2j1da^{2^{j-1}d}
  5. 5If any of these intermediate values equals 1(modm)-1 \pmod{m}, then mm passes the test
  6. 6If we find a value that squares to 11 but is not ±1\pm 1, then mm is definitely composite
  7. 7Key insight: If x21(modn)x^2 \equiv 1 \pmod{n} but x≢±1(modn)x \not\equiv \pm 1 \pmod{n}, then nn is composite
  8. 8This happens because (x1)(x+1)0(modn)(x-1)(x+1) \equiv 0 \pmod{n}, so gcd(x1,n)\gcd(x-1, n) gives a non-trivial factor
  9. 9The test is based on Fermat's Little Theorem
  10. 10If mm fails the test for any base aa, then mm is definitely composite
  11. 11If mm passes for many random bases, it is a strong probable prime with respect to base aa
  12. 12This test is widely used in cryptography for generating large prime numbers