1 // cl_miller_rabin_test().
12 #include "cln/modinteger.h"
16 cl_boolean cl_miller_rabin_test (const cl_I& n, int count, cl_I* factor)
18 // [Cohen], section 8.2, algorithm 8.2.2.
19 var cl_modint_ring R = find_modint_ring(n); // Z/nZ
21 var uintL e = ord2(m);
24 var cl_MI one = R->one();
25 var cl_MI minusone = R->uminus(one);
26 for (int i = 0; i < count; i++) {
27 // Choosing aa small makes the expt_pos faster.
30 : i <= cl_small_prime_table_size
31 ? (cl_I) (unsigned int) cl_small_prime_table[i-1] // small prime
32 : 2+random_I(n-2)); // or random >=2, <n
36 var cl_MI a = R->canonhom(aa);
37 var cl_MI b = R->expt_pos(a,m); // b = a^m
40 for (uintL s = e; s > 0; s--) {
43 var cl_MI new_b = R->square(b);
45 // (b-1)*(b+1) == 0 mod n, hence n not prime.
47 *factor = gcd(R->retract(b)-1,n);
52 // b = a^(2^e*m) = a^(n-1), but b != -1 mod n, hence n not prime.
54 var cl_I g = gcd(aa,n);