This question may actually have nothing to do with the Miller-Rabin primality testing procedure; it may only easy analysis of some simple pseudocode.
On p 969 of CLRS (Introduction to Algorithms 3ed), an auxiliary function to Miller-Rabin is presented:
WITNESS(a, n)
let t and u be such that t >= 1, u is odd, and n-1 = 2^t u
x_0 = MODULAR-EXPONENTIATION(a, u, n)
for i = 1 to t
x_i = x_{i-1}^2 mod n
if x_i == 1 and x_{i-1} != 1 and x_{i-1} != n-1
return TRUE
if x_t != 1
return TRUE
return FALSE
I copied the above exactly from the textbook.
Now, knowing only that MODULAR-EXPONENTIATION
returns a result between 0 and n-1, inclusive, I think the pseudocode above is entirely equivalent to
WITNESS(a, n)
let t and u be such that t >= 1, u is odd, and n-1 = 2^t u
x_0 = MODULAR-EXPONENTIATION(a, u, n)
if x_0 == 1 or x_0 == n-1
return FALSE
else
return TRUE
If so, there's probably something else wrong with the original implementation, since if I'm not mistaken Miller-Rabin witnessing does require some sort of looping. Can someone provide a simple counterexample to show that I am wrong?