1 /* ecm-ecm.h - private header file for GMP-ECM.
2 
3 Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Paul Zimmermann, Alexander Kruppa and Cyril Bouvier.
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or (at your
9 option) any later version.
10 
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14 more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING.  If not, see
18 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
19 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
20 
21 #ifndef _ECM_ECM_H
22 #define _ECM_ECM_H 1
23 
24 #include "config.h"
25 
26 #include <assert.h>
27 #define ASSERT_ALWAYS(expr)   assert (expr)
28 #ifdef WANT_ASSERT
29 #define ASSERT(expr)   assert (expr)
30 #else
31 #define ASSERT(expr)   do {} while (0)
32 #endif
33 
34 #include "ecm.h"
35 
36 /* Structure for candidate usage.  This is much more powerful than using a
37    simple mpz_t to hold the candidate.  This structure also houses the
38    expression (in raw form), and will modify the expression as factors
39    are found (if in looping modes).  Also, since we are warehousing all
40    of the data associated with the candidate, we also store whether the
41    candidate is PRP here (so testing will cease), along with the length
42    of the candidate.  As each factor is found, the candidate will also
43    have the factor removed from it */
44 typedef struct
45 {
46 #if defined (CANDI_DEBUG)
47   unsigned long magic;	/* used for debugging purposes while writing this code */
48 #endif
49   char *cpExpr;		/* if non-NULL, then this is a "simpler" expression than the
50 			   decimal output of n */
51   mpz_t n;		/* the cofactor candidate currently being used to find factors from */
52   unsigned ndigits;	/* the number of digits (decimal) in n */
53   unsigned nexprlen;	/* strlen of expression, 0 if there is NO expression */
54   int isPrp;		/* usually 0, but turns 1 if factor found, and the cofactor is PRP,
55 			   OR if the original candidate was PRP and the user asked to prp check */
56 } mpcandi_t;
57 
58 typedef struct
59 {
60   int  Valid;           /* Is ONLY set to 1 if there is a proper -go <integer> switch.  Otherwise is 0
61                            and if 0, then PM1, PP1 and ECM all ignore it */
62   char *cpOrigExpr;	/* if non-NULL, then this is a "simpler" expression than the
63 			   decimal output of n */
64   mpcandi_t Candi;      /* The value after expression checked */
65   int containsN;        /* 0 for simple number or expression.  1 if the expression "contains" N as
66                            that expression will have to be built for each candidate */
67 } mpgocandi_t;
68 
69 /* auxi.c */
70 unsigned int nb_digits  (const mpz_t);
71 int read_number (mpcandi_t*, FILE*, int);
72 int process_newfactor (mpz_t, int, mpcandi_t*, int, int, int, unsigned int*,
73                        int*, mpz_t, FILE*, int, int);
74 
75 /* Various logging levels */
76 /* OUTPUT_ALWAYS means print always, regardless of verbose value */
77 #define OUTPUT_ALWAYS 0
78 /* OUTPUT_NORMAL means print during normal program execution */
79 #define OUTPUT_NORMAL 1
80 /* OUTPUT_VERBOSE means print if the user requested more verbosity */
81 #define OUTPUT_VERBOSE 2
82 /* OUTPUT_RESVERBOSE is for printing residues (after stage 1 etc) */
83 #define OUTPUT_RESVERBOSE 3
84 /* OUTPUT_DEVVERBOSE is for printing internal parameters (for developers) */
85 #define OUTPUT_DEVVERBOSE 4
86 /* OUTPUT_TRACE is for printing trace data, produces lots of output */
87 #define OUTPUT_TRACE 5
88 /* OUTPUT_ERROR is for printing error messages */
89 #define OUTPUT_ERROR -1
90 
91 #define MAX_NUMBER_PRINT_LEN 1000
92 
93 #define NTT_SIZE_THRESHOLD 30
94 
95 /* auxlib.c */
96 int  test_verbose (int);
97 void set_verbose (int);
98 
99 /* Return codes */
100 /* Bit coded values: 1: error (for example out of memory)
101    2: proper factor found, 4: factor is prime,
102    8: cofactor is prime or 1 */
103 #define ECM_EXIT_ERROR 1
104 #define ECM_COMP_FAC_COMP_COFAC 2
105 #define ECM_PRIME_FAC_COMP_COFAC (2+4)
106 #define ECM_INPUT_NUMBER_FOUND 8
107 #define ECM_COMP_FAC_PRIME_COFAC (2+8)
108 #define ECM_PRIME_FAC_PRIME_COFAC (2+4+8)
109 
110 /* getprime.c */
111 double getprime ();
112 void getprime_clear ();
113 #define WANT_FREE_PRIME_TABLE(p) (p < 0.0)
114 #define FREE_PRIME_TABLE -1.0
115 
116 /* b1_ainc.c */
117 double calc_B1_AutoIncrement(double cur_B1, double incB1val);
118 
119 /* resume.c */
120 int  read_resumefile_line (int *, mpz_t, mpz_t, mpcandi_t *,
121 			   mpz_t, mpz_t,
122 			   mpz_t, mpz_t, int *, int *,
123                            double *, char *, char *, char *, char *, FILE *);
124 int write_resumefile (char *, int, mpz_t, ecm_params params,
125 		      mpcandi_t *, mpz_t, mpz_t,
126 		      const char *);
127 int write_s_in_file (char *, mpz_t);
128 int read_s_from_file (mpz_t, char *, double);
129 
130 /* main.c */
131 int kbnc_z (double *k, unsigned long *b, unsigned long *n, signed long *c,
132             mpz_t z);
133 int kbnc_str (double *k, unsigned long *b, unsigned long *n, signed long *c,
134               char *z, mpz_t num);
135 
136 /* eval.c */
137 int eval (mpcandi_t *n, FILE *fd, int bPrp);
138 int eval_str (mpcandi_t *n, char *cp, int primetest, char **EndChar); /* EndChar can be NULL */
139 void init_expr (void);
140 void free_expr (void);
141 
142 /* candi.c */
143 void mpcandi_t_init (mpcandi_t *n);  /* damn, a C++ class sure would have been nice :(  */
144 void mpcandi_t_free (mpcandi_t *n);
145 int  mpcandi_t_copy (mpcandi_t *to, mpcandi_t *from);
146 int  mpcandi_t_add_candidate (mpcandi_t *n, mpz_t c, const char *cpExpr, int bPrp);
147 int  mpcandi_t_addfoundfactor (mpcandi_t *n, mpz_t f, int displaywarning);
148 int  mpcandi_t_addfoundfactor_d (mpcandi_t *n, double f);
149 /* candi.c   Group Order candidate functions.  */
150 void mpgocandi_t_init(mpgocandi_t *go);
151 void mpgocandi_t_free(mpgocandi_t *go);
152 int  mpgocandi_fixup_with_N(mpgocandi_t *go, mpcandi_t *n);
153 
154 /* random.c */
155 #undef get_random_ul
156 unsigned long get_random_ul (void);
157 #undef pp1_random_seed
158 void pp1_random_seed  (mpz_t, mpz_t, gmp_randstate_t);
159 #undef pm1_random_seed
160 void pm1_random_seed  (mpz_t, mpz_t, gmp_randstate_t);
161 
162 /* memusage.c */
163 long PeakMemusage (void);
164 
165 /* default number of probable prime tests */
166 #define PROBAB_PRIME_TESTS 1
167 
168 /* maximal stage 1 bound = 2^53 - 1, the next prime being 2^53 + 5 */
169 #define MAX_B1 9007199254740991.0
170 
171 /* The checksum for savefile is the product of all mandatory fields, modulo
172    the greatest prime below 2^32 */
173 #define CHKSUMMOD 4294967291U
174 
175 #define ABS(x) ((x) >= 0 ? (x) : -(x))
176 
177 #ifdef HAVE_APRCL
178 #include "aprtcle/mpz_aprcl.h"
179 #define ECM_FAC_PRIME APRTCLE_PRIME
180 #define ECM_FAC_PRP APRTCLE_PRP
181 #else
182 #define mpz_aprtcle(x,y) mpz_probab_prime_p(x,PROBAB_PRIME_TESTS)
183 #define ECM_FAC_PRIME 2 /* mpz_probab_prime_p and aprcl returns 2 when
184                            a number is definitely prime */
185 #define ECM_FAC_PRP 1   /* mpz_probab_prime_p and aprcl returns 1 when
186                            a number is a probable prime */
187 #endif
188 
189 /* Cutoff value: Show APRCL progress only if n has more digits than cutoff */
190 #define APRCL_CUTOFF 400   /* for more than APRCL_CUTOFF digits, print
191                               progress */
192 /* Cutoff value: Use APRCL if n has fewer digits than cutoff2
193                  Use mpz_probab_prime_p if n has more digits than cutoff2 */
194 #define APRCL_CUTOFF2 500 /* for more than APRCL_CUTOFF2 digits, perform
195                               a pseudo-primality test */
196 
197 #endif /* _ECM_ECM_H */
198