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
Merge pull request #2119 from smichr/isprime

refactor the Miller-Rabin test code