Primality Checking, Introduction to NP-completeness




CS255

Chris Pollett

Apr 11, 2018

Outline

Testing for Primes

Miller Rabin Primality Testing

Miller Rabin continued

Miller-Rabin(n, s)
1 for j = 1 to s
2    a = Random(1, n - 1)
3    if Witness(a, n):
4        return "Composite"
5 return "Prime"

Error Rate

Theorem. If `n` is composite, then the number of witnesses to compositeness is at least `(n-1)/2`.

Proof. We show the number of non-witnesses is at most `(n-1)/2`. First, any non-witness must be in `ZZ_n^(star)` as it must satisfy `a^(n-1) equiv 1 mod n`, i.e., `a cdot a^(n-2) equiv 1 mod n`; thus, it has an inverse. So we know `gcd(a,n) | 1` and hence `gcd(a, n) = 1`. Next we show that all non-witnesses are contained in a proper subgroup of `ZZ_n^(star)`. This implies the Theorem. There two cases to consider:

  1. There is an `x in ZZ_n^(star)` such that `x^(n-1) ne 1 mod n`. In this case, we note all the `b` such that `b^(n-1) equiv 1 mod n` form a group. To see this note, `1` is in this group. As `b \cdot b^{n-2} equiv 1 mod n` and
    `(b^{n-2})^{n-1} equiv b^{(n-2)cdot(n-1)} equiv (b^{n-1})^{n-2} equiv 1^{n-2} equiv 1 mod n`.
    the inverse of `b`, `b^{-1} = b^{n-2}`, satisfies `(b^{-1})^{n-1} equiv 1 mod n`. Finally, given `b`, `c` satisfying the property,
    `(b \cdot c)^{n-1} equiv (b^{n-1}) (c^{n-1}) equiv 1 cdot 1 equiv 1 mod n`,
    So this set is a group. Since it does not contain `x`, it is a proper subgroup of `ZZ_n^(star)`.
  2. The number `n` is Carmichael number. In this case, `x^(n-1) equiv 1 mod n` for all `x in ZZ_n^(star)`. i.e, such that `gcd(x, n) = 1`. We show this case after the In-class Exercise.

In-Class Exercise

Solution.

Assumptions: `a cdot b` and `a mod b` are computable in time proportional to `|a| \cdot |b|` (can do grade school algorithms).

Exp-Mod(a, x, n)
   c := (Exp-Mod(a, floor(x/2), n))^2 mod n
   if x is even return c;
   else
     return a * c mod n;

Using the algorithm we have:

Exp-Mod(3, 560, 561) 
= (Exp-Mod(3, 280, 561))^2 mod 561
= ((Exp-Mod(3, 140, 561))^2 mod 561)^2 mod 561
= (((Exp-Mod(3, 70, 561))^2 mod 561)^2 mod 561)^2 mod 561
= ((((Exp-Mod(3, 35, 561))^2 mod 561)^2 mod 561)^2 mod 561)^2 mod 561
Let c = Exp-Mod(3, 35, 561) then
c=
= 3 * (Exp-Mod(3, 17, 561))^2 mod 561
= 3 * (3 * (Exp-Mod(3, 8, 561))^2 mod 561)^2 mod 561
... to save writing we note Exp-Mod(3, 8, 561) = 3^4 * 3^4 = 81*81 = 390 mod 561
= 3 * (3 * (390)^2 mod 561)^2 mod 561
= 3 * (207 mod 561) ^2 mod 561
= 78 mod 561
Plugging in for c in the above
= ((((c)^2 mod 561)^2 mod 561)^2 mod 561)^2 mod 561
= (((474)^2 mod 561)^2 mod 561)^2 mod 561
= ((276)^2 mod 561)^2 mod 561
= 441^2 mod 561
= 375 mod 561.

Notice this isn't 1, but that's okay as `gcd(3, 561) = 3 ne 1`.

Miller-Rabin Correctness -- The Carmichael Number Case

Miller-Rabin Correctness -- Finish The Carmichael Number Case