refactor the Miller-Rabin test code

also use pow(b, 2, n) rather than b**2 % n which is a bit more efficient
use ZZ(base) in isprime