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.

86 lines
3.1 KiB

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