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.

90 lines
3.2 KiB

25 years ago
25 years ago
25 years ago
  1. // Number theoretic operations.
  2. #ifndef _CL_NUMTHEORY_H
  3. #define _CL_NUMTHEORY_H
  4. #include "cln/number.h"
  5. #include "cln/integer.h"
  6. #include "cln/modinteger.h"
  7. #include "cln/condition.h"
  8. namespace cln {
  9. // jacobi(a,b) returns the Jacobi symbol
  10. // ( a )
  11. // ( --- )
  12. // ( b )
  13. // a, b must be integers, b > 0, b odd. The result is 0 iff gcd(a,b) > 1.
  14. extern int jacobi (sint32 a, sint32 b);
  15. extern int jacobi (const cl_I& a, const cl_I& b);
  16. // isprobprime(n), n integer > 0,
  17. // returns true when n is probably prime.
  18. // This is pretty quick, but no caching is done.
  19. extern cl_boolean isprobprime (const cl_I& n);
  20. // nextprobprime(x) returns the smallest probable prime >= x.
  21. extern const cl_I nextprobprime (const cl_R& x);
  22. #if 0
  23. // primitive_root(R) of R = Z/pZ, with p a probable prime,
  24. // returns
  25. // either a generator of (Z/pZ)^*, assuming p is prime, or
  26. // a proof that p is not prime, maybe even a non-trivial factor of p.
  27. struct primitive_root_t {
  28. cl_composite_condition* condition;
  29. cl_MI gen;
  30. // Constructors.
  31. primitive_root_t (cl_composite_condition* c) : condition (c) {}
  32. primitive_root_t (const cl_MI& g) : condition (NULL), gen (g) {}
  33. };
  34. extern const primitive_root_t primitive_root (const cl_modint_ring& R);
  35. #endif
  36. // sqrt_mod_p(R,x) where x is an element of R = Z/pZ, with p a probable prime,
  37. // returns
  38. // either the square roots of x in R, assuming p is prime, or
  39. // a proof that p is not prime, maybe even a non-trivial factor of p.
  40. struct sqrt_mod_p_t {
  41. cl_composite_condition* condition;
  42. // If no condition:
  43. int solutions; // 0,1,2
  44. cl_I factor; // zero or non-trivial factor of p
  45. cl_MI solution[2]; // max. 2 solutions
  46. // Constructors.
  47. sqrt_mod_p_t () {}
  48. sqrt_mod_p_t (cl_composite_condition* c) : condition (c) {}
  49. sqrt_mod_p_t (int s) : condition (NULL), solutions (s) {}
  50. sqrt_mod_p_t (int s, const cl_MI& x0) : condition (NULL), solutions (s)
  51. { solution[0] = x0; }
  52. sqrt_mod_p_t (int s, const cl_MI& x0, const cl_MI& x1) : condition (NULL), solutions (s)
  53. { solution[0] = x0; solution[1] = x1; }
  54. };
  55. extern const sqrt_mod_p_t sqrt_mod_p (const cl_modint_ring& R, const cl_MI& x);
  56. // cornacchia1(d,p) solves x^2 + d*y^2 = p.
  57. // cornacchia4(d,p) solves x^2 + d*y^2 = 4*p.
  58. // d is an integer > 0, p is a probable prime.
  59. // It returns
  60. // either a nonnegative solution (x,y), if it exists, assuming p is prime, or
  61. // a proof that p is not prime, maybe even a non-trivial factor of p.
  62. struct cornacchia_t {
  63. cl_composite_condition* condition;
  64. // If no condition:
  65. int solutions; // 0,1
  66. // If solutions=1 and d > 4 (d > 64 for cornacchia4):
  67. // All solutions are (x,y), (-x,y), (x,-y), (-x,-y).
  68. cl_I solution_x; // x >= 0
  69. cl_I solution_y; // y >= 0
  70. // Constructors.
  71. cornacchia_t () {}
  72. cornacchia_t (cl_composite_condition* c) : condition (c) {}
  73. cornacchia_t (int s) : condition (NULL), solutions (s) {}
  74. cornacchia_t (int s, const cl_I& x, const cl_I& y) : condition (NULL), solutions (s), solution_x (x), solution_y (y) {}
  75. };
  76. extern const cornacchia_t cornacchia1 (const cl_I& d, const cl_I& p);
  77. extern const cornacchia_t cornacchia4 (const cl_I& d, const cl_I& p);
  78. } // namespace cln
  79. #endif /* _CL_NUMTHEORY_H */