You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.7 KiB

25 years ago
25 years ago
25 years ago
  1. // Integer factorization and primality testing.
  2. #ifndef _CL_IF_H
  3. #define _CL_IF_H
  4. #include "cln/number.h"
  5. #include "cln/integer.h"
  6. namespace cln {
  7. // Table of primes > 2, < 2^16
  8. const uint32 cl_small_prime_table_limit = 65536;
  9. const int cl_small_prime_table_size = 6541;
  10. extern uint16 cl_small_prime_table[cl_small_prime_table_size];
  11. // Given 0 < d <= cl_small_prime_table_limit, return the smallest index i
  12. // such that cl_small_prime_table[i] >= d. (Or i = cl_small_prime_table_size
  13. // if none exists.)
  14. inline uintL cl_small_prime_table_search (uint32 d)
  15. {
  16. var uintL i1 = 0;
  17. var uintL i2 = cl_small_prime_table_size;
  18. if (cl_small_prime_table[i1] >= d)
  19. return i1;
  20. loop {
  21. // Here i1 < i2 and
  22. // cl_small_prime_table[i1] < d <= cl_small_prime_table[i2].
  23. var uintL i3 = floor(i1+i2,2);
  24. if (i3 == i1) // (i2-i1 == 1) ?
  25. return i2;
  26. if (cl_small_prime_table[i3] >= d)
  27. i2 = i3;
  28. else
  29. i1 = i3;
  30. }
  31. }
  32. // Trial division.
  33. // Divides n > 0 by the primes in the range d1 <= d <= d2
  34. // (0 < d1 <= d2 <= min(isqrt(n),cl_small_prime_table_limit))
  35. // and returns the divisor d if found, or 0 if no divisor found.
  36. extern uint32 cl_trialdivision (uint32 n, uint32 d1, uint32 d2);
  37. extern uint32 cl_trialdivision (uint32 nhi, uint32 nlo, uint32 d1, uint32 d2);
  38. extern uint32 cl_trialdivision (const cl_I& n, uint32 d1, uint32 d2);
  39. // Miller-Rabin compositeness test.
  40. // Performs count times the Miller-Rabin test on n > 1 odd.
  41. // Returns true if n looks like a prime (with error probability < 4^-count).
  42. // Returns false if n is definitely composite, and then sets factor = some
  43. // nontrivial factor or 0.
  44. extern cl_boolean cl_miller_rabin_test (const cl_I& n, int count, cl_I* factor);
  45. } // namespace cln
  46. #endif /* _CL_IF_H */