7 #include "cl_numtheory.h"
15 cl_boolean isprobprime (const cl_I& n)
19 // With a Miller-Rabin count = 50 the final error probability is
22 // Step 1: Trial division (rules out 87% of all numbers quickly).
23 const uint32 trialdivide_limit = 70;
24 var uintL l = integer_length(n);
26 var uint32 nn = cl_I_to_UL(n);
27 if (nn <= cl_small_prime_table_limit) {
29 var uintL i = cl_small_prime_table_search(nn);
30 if (i < cl_small_prime_table_size
31 && ((unsigned int) cl_small_prime_table[i] == nn
37 if ((nn % 2) == 0 || cl_trialdivision(nn,1,trialdivide_limit))
39 // For small n, only few Miller-Rabin tests are needed.
40 if (nn < 2000U) count = 1; // {2}
41 else if (nn < 1300000U) count = 2; // {2,3}
42 else if (nn < 25000000U) count = 3; // {2,3,5}
43 else if (nn < 3200000000U) count = 4; // {2,3,5,7}
45 var uint32 nhi = cl_I_to_UL(ldb(n,cl_byte(32,32)));
46 var uint32 nlo = cl_I_to_UL(ldb(n,cl_byte(32,0)));
47 if ((nlo % 2) == 0 || cl_trialdivision(nhi,nlo,1,trialdivide_limit))
50 if (evenp(n) || cl_trialdivision(n,1,trialdivide_limit))
53 // Step 2: Miller-Rabin test.
54 return cl_miller_rabin_test(n,count,NULL);