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