1 #ifndef CADO_POLY_H_
2 #define CADO_POLY_H_
3 
4 #include <stdio.h>      // FILE
5 #include <gmp.h>
6 
7 #ifdef __cplusplus
8 #include "params.h"
9 #endif
10 #include "mpz_poly.h"
11 
12 /* The maximum degree of polynomials supported. Used for statically
13    allocating storage (i.e. "mpz_t poly[MAX_DEGREE]") */
14 #define MAX_DEGREE 10
15 
16 #define NB_POLYS_MAX 8 /* maximal number of polynomials in multiple fields */
17 
18 struct cado_poly_s {
19   mpz_t n;        /* number to factor */
20   double skew;    /* skewness from poly file, if given, otherwise 0. */
21 
22   int nb_polys;   /* number of polynomials used, 2 in most cases */
23   mpz_poly pols[NB_POLYS_MAX];
24 };
25 typedef struct cado_poly_s cado_poly[1];
26 typedef struct cado_poly_s * cado_poly_ptr;
27 typedef const struct cado_poly_s * cado_poly_srcptr;
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 // This reads a file created by polyselect and fill in the structure
34 // accordingly. Return 1 if success, 0 if failure (and diagnostic on
35 // stderr)
36 extern int cado_poly_read (cado_poly_ptr, const char *filename);
37 extern int cado_poly_read_stream (cado_poly_ptr, FILE *);
38 extern int cado_poly_read_next_poly_from_stream (cado_poly_ptr, FILE *);
39 extern void cado_poly_set (cado_poly_ptr p, cado_poly_srcptr q);
40 extern void cado_poly_swap (cado_poly_ptr p, cado_poly_ptr q);
41 
42 void cado_poly_fprintf (FILE *, cado_poly_srcptr, const char *);
43 void cado_poly_fprintf_info (FILE *, double, double, double, double,
44                              unsigned int, const char *);
45 void cado_poly_fprintf_MurphyE (FILE *, double, double, double, double,
46                                 const char *);
47 /* More functions for printing cado_poly are defined in polyselect/ as only
48    binaries in polyselect/ used them and some functions (like L2_skewness, ...)
49    only defined in polyselect/ are needed.
50  */
51 
52 extern void cado_poly_init (cado_poly_ptr);
53 extern void cado_poly_clear (cado_poly_ptr);
54 
55 extern int cado_poly_check_mapping(mpz_poly_ptr G, cado_poly_srcptr cpoly,
56         mpz_srcptr N);
57 
58 // Compute m as the common root of f and g mod N.
59 // N is taken as a third argument; it can be a strict factor of the N
60 // stored in the polynomial.
61 // If this fails, then in most of the cases we have found a factor of N
62 // that is given instead of m.
63 // The return value tells whether it worked (and then m is the common
64 // root) or it failed (and then m is a factor of N).
65 extern int cado_poly_getm(mpz_ptr, cado_poly_srcptr, mpz_srcptr);
66 
67 /* Return the rational side or -1 if two algebraic side */
68 extern int cado_poly_get_ratside (cado_poly_srcptr);
69 
70 #ifdef __cplusplus
71 }
72 #endif
73 
74 #ifdef __cplusplus
75 /* Same idea as for cxx_mpz and friends */
76 struct cxx_cado_poly {
77     cado_poly x;
cxx_cado_polycxx_cado_poly78     cxx_cado_poly() { cado_poly_init(x); }
cxx_cado_polycxx_cado_poly79     cxx_cado_poly(cado_poly_srcptr f) { cado_poly_init(x); cado_poly_set(x, f); }
configure_switchescxx_cado_poly80     static void configure_switches(cxx_param_list &) {}
configure_aliasescxx_cado_poly81     static void configure_aliases(cxx_param_list & pl) {
82         param_list_configure_alias(pl, "skew", "S");
83     }
declare_usagecxx_cado_poly84     static void declare_usage(cxx_param_list & pl) {
85         param_list_decl_usage(pl, "poly", "polynomial file");
86         param_list_decl_usage(pl, "skew", "skewness");
87     }
88 
cxx_cado_polycxx_cado_poly89     cxx_cado_poly(cxx_param_list & pl) {
90         cado_poly_init(x);
91         const char *tmp;
92         if ((tmp = param_list_lookup_string(pl, "poly")) == NULL) {
93             fprintf(stderr, "Error: -poly is missing\n");
94             param_list_print_usage(pl, NULL, stderr);
95             cado_poly_clear(x);
96             exit(EXIT_FAILURE);
97         }
98         if (!cado_poly_read(x, tmp)) {
99             fprintf(stderr, "Error reading polynomial file %s\n", tmp);
100             cado_poly_clear(x);
101             exit(EXIT_FAILURE);
102         }
103         /* -skew (or -S) may override (or set) the skewness given in the
104          * polynomial file */
105         param_list_parse_double(pl, "skew", &(x->skew));
106         if (x->skew <= 0.0) {
107             fprintf(stderr, "Error, please provide a positive skewness\n");
108             cado_poly_clear(x);
109             exit(EXIT_FAILURE);
110         }
111     }
112 
~cxx_cado_polycxx_cado_poly113     ~cxx_cado_poly() { cado_poly_clear(x); }
cxx_cado_polycxx_cado_poly114     cxx_cado_poly(cxx_cado_poly const & o) {
115         cado_poly_init(x);
116         cado_poly_set(x, o.x);
117     }
118     cxx_cado_poly & operator=(cxx_cado_poly const & o) {
119         cado_poly_set(x, o.x);
120         return *this;
121     }
122 #if __cplusplus >= 201103L
cxx_cado_polycxx_cado_poly123     cxx_cado_poly(cxx_cado_poly && o) {
124         cado_poly_init(x);
125         cado_poly_swap(x, o.x);
126     }
127     cxx_cado_poly& operator=(cxx_cado_poly && o) {
128         cado_poly_swap(x, o.x);
129         return *this;
130     }
131 #endif
cado_poly_ptrcxx_cado_poly132     operator cado_poly_ptr() { return x; }
cado_poly_srcptrcxx_cado_poly133     operator cado_poly_srcptr() const { return x; }
134     cado_poly_ptr operator->() { return x; }
135     cado_poly_srcptr operator->() const { return x; }
136 };
137 #if GNUC_VERSION_ATLEAST(4,3,0)
138 extern void cado_poly_init(cxx_cado_poly & pl) __attribute__((error("cado_poly_init must not be called on a cado_poly reference -- it is the caller's business (via a ctor)")));
139 extern void cado_poly_clear(cxx_cado_poly & pl) __attribute__((error("cado_poly_clear must not be called on a cado_poly reference -- it is the caller's business (via a dtor)")));
140 #endif
141 
142 
143 
144 #endif
145 #endif	/* CADO_POLY_H_ */
146