1 // Integer factorization and primality testing.
6 #include "cln/number.h"
7 #include "cln/integer.h"
11 // Table of primes > 2, < 2^16
12 const uint32 cl_small_prime_table_limit = 65536;
13 const int cl_small_prime_table_size = 6541;
14 extern uint16 cl_small_prime_table[cl_small_prime_table_size];
16 // Given 0 < d <= cl_small_prime_table_limit, return the smallest index i
17 // such that cl_small_prime_table[i] >= d. (Or i = cl_small_prime_table_size
19 inline uintL cl_small_prime_table_search (uint32 d)
22 var uintL i2 = cl_small_prime_table_size;
23 if (cl_small_prime_table[i1] >= d)
27 // cl_small_prime_table[i1] < d <= cl_small_prime_table[i2].
28 var uintL i3 = floor(i1+i2,2);
29 if (i3 == i1) // (i2-i1 == 1) ?
31 if (cl_small_prime_table[i3] >= d)
39 // Divides n > 0 by the primes in the range d1 <= d <= d2
40 // (0 < d1 <= d2 <= min(isqrt(n),cl_small_prime_table_limit))
41 // and returns the divisor d if found, or 0 if no divisor found.
42 extern uint32 cl_trialdivision (uint32 n, uint32 d1, uint32 d2);
43 extern uint32 cl_trialdivision (uint32 nhi, uint32 nlo, uint32 d1, uint32 d2);
44 extern uint32 cl_trialdivision (const cl_I& n, uint32 d1, uint32 d2);
46 // Miller-Rabin compositeness test.
47 // Performs count times the Miller-Rabin test on n > 1 odd.
48 // Returns true if n looks like a prime (with error probability < 4^-count).
49 // Returns false if n is definitely composite, and then sets factor = some
50 // nontrivial factor or 0.
51 extern cl_boolean cl_miller_rabin_test (const cl_I& n, int count, cl_I* factor);