Pollard's rho algorithm for computing discrete logs.
[u/mdw/catacomb] / pgen.h
1 /* -*-c-*-
2 *
3 * $Id: pgen.h,v 1.6 2000/06/17 11:52:12 mdw Exp $
4 *
5 * Prime generation glue
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: pgen.h,v $
33 * Revision 1.6 2000/06/17 11:52:12 mdw
34 * Add the GCD filter.
35 *
36 * Revision 1.5 2000/02/12 18:21:03 mdw
37 * Overhaul of key management (again).
38 *
39 * Revision 1.4 1999/12/22 16:01:11 mdw
40 * Same file, completely different code. Main interface for new prime-
41 * search system.
42 *
43 */
44
45 #ifndef CATACOMB_PGEN_H
46 #define CATACOMB_PGEN_H
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 /*----- Header files ------------------------------------------------------*/
53
54 #ifndef CATACOMB_GRAND_H
55 # include "grand.h"
56 #endif
57
58 #ifndef CATACOMB_MP_H
59 # include "mp.h"
60 #endif
61
62 #ifndef CATACOMB_PFILT_H
63 # include "pfilt.h"
64 #endif
65
66 #ifndef CATACOMB_RABIN_H
67 # include "rabin.h"
68 #endif
69
70 /*----- Event handling ----------------------------------------------------*
71 *
72 * Different programs and architectures will want to show progress of prime
73 * searches and similar processes in different ways. Of course, for simple
74 * searches, it's possible to use the @pfilt@ and @rabin@ functions and
75 * maintain control over the general control flow throughout the search.
76 *
77 * For more complex cases, this sort of control is undesirable. It's
78 * possible to specify an event handler which is informed in abstract about
79 * the search. The event handler can also request the search be aborted.
80 */
81
82 /* --- Event code constants --- *
83 *
84 * You're allowed to rely on the values of @PGEN_DONE@ and @PGEN_ABORT@.
85 */
86
87 enum {
88 PGEN_BEGIN = 1, /* Search for value begins */
89 PGEN_TRY, /* A new candidate has appeared */
90 PGEN_FAIL, /* The candidate failed the test */
91 PGEN_PASS, /* The candidate passed a test */
92 PGEN_DONE = 0, /* A good value has been found */
93 PGEN_ABORT = -1 /* The search has been aborted */
94 };
95
96 /* --- Event information --- *
97 *
98 * Note that the pseudorandom number generator supplied is not
99 * cryptographically strong.
100 */
101
102 typedef struct pgen_event {
103 const char *name; /* Which quantity is being found */
104 mp *m; /* Current value under test */
105 unsigned steps; /* Number of candidates left */
106 unsigned tests; /* Tests left before passing */
107 grand *r; /* Source of random numbers */
108 } pgen_event;
109
110 /*----- Prime search parameters -------------------------------------------*
111 *
112 * The prime search is parameterized in a large number of ways, although this
113 * isn't so much of a surprise given the different sorts of properties
114 * required from prime numbers in cryptographic applications.
115 *
116 * There are two main things which need to be configured: stepping, and
117 * testing. (Filtering is done as part of stepping.)
118 *
119 * The functions here provide a toolkit for constructing stepping and testing
120 * routines. In a lot of cases, the functions can be used directly; in
121 * others, simple bits of glue need be written.
122 *
123 * Two types of functions are defined: steppers and testers, but their
124 * interfaces are substantially similar. Each is given a request code, a
125 * context block and an event block. It is meant to update its context and
126 * the event block and return an event code.
127 *
128 * A call with a request of @PGEN_BEGIN@ asks the stepper or tester to
129 * initialize itself using the information in its event block and context. A
130 * return of @PGEN_FAIL@ reports an immediate failure; @PGEN_ABORT@ reports a
131 * fatal problem; @PGEN_DONE@ reports immediate success. @PGEN_TRY@ reports
132 * successful initialization and requests test iterations.
133 *
134 * A call to a stepper with a request of @PGEN_TRY@ asks it to step to the
135 * next possible candidate, replacing the value @m@ in the event block with
136 * the new candidate. A call to a tester with a request of @PGEN_TRY@
137 * runs one pass of the test. It should return @PGEN_FAIL@ to report a
138 * failure, @PGEN_PASS@ to report a success and request another iteration,
139 * @PGEN_DONE@ to report final acceptance and @PGEN_ABORT@ to terminate the
140 * search unsuccessfully. Note that even if the search is aborted, a
141 * shutdown request is still made.
142 *
143 * A call with a request of @PGEN_DONE@ closes down the stepper or tester.
144 * After a successful initialization (i.e., a return of something other than
145 * @PGEN_ABORT@), a shutdown call is guaranteed. The return code is ignored.
146 */
147
148 typedef int pgen_proc(int /*rq*/, pgen_event */*ev*/, void */*p*/);
149
150 /*----- Simple handler functions ------------------------------------------*/
151
152 /* --- @pgen_filter@ --- *
153 *
154 * A prime generation context contains the information required for the
155 * simple prime filter and tester presented here.
156 */
157
158 typedef struct pgen_filterctx {
159 unsigned step; /* Step size (set by client) */
160 pfilt f; /* The rapid prime filter */
161 } pgen_filterctx;
162
163 extern int pgen_filter(int /*rq*/, pgen_event */*ev*/, void */*p*/);
164
165 /* --- @pgen_jump@ --- *
166 *
167 * Similar to the standard @pgen_filter@, but jumps in large steps rather
168 * than small ones.
169 */
170
171 typedef struct pgen_jumpctx {
172 const pfilt *j;
173 pfilt f;
174 } pgen_jumpctx;
175
176 extern int pgen_jump(int /*rq*/, pgen_event */*ev*/, void */*p*/);
177
178 /* --- @pgen_test@ --- *
179 *
180 * Runs the Rabin-Miller primality test. The context block is simply a
181 * @rabin@ context.
182 */
183
184 extern int pgen_test(int /*rq*/, pgen_event */*ev*/, void */*p*/);
185
186 /*----- Safe prime functions ----------------------------------------------*/
187
188 /* --- @pgen_safestep@ --- *
189 *
190 * Steps two numbers, %$q$% and %$p = 2q + 1$%, such that neither has any
191 * small factors. %$p$% is put in the event block.
192 */
193
194 typedef struct pgen_safestepctx {
195 pfilt q, p;
196 } pgen_safestepctx;
197
198 extern int pgen_safestep(int /*rq*/, pgen_event */*ev*/, void */*p*/);
199
200 /* --- @pgen_safejump@ --- *
201 *
202 * Jumps two numbers, %$q$% and %$p = 2q + 1$% such that neither has any
203 * small factors.
204 */
205
206 typedef struct pgen_safejumpctx {
207 pfilt q, jq;
208 pfilt p, jp;
209 } pgen_safejumpctx;
210
211 extern int pgen_safejump(int /*rq*/, pgen_event */*ev*/, void */*p*/);
212
213 /* --- @pgen_safetest@ --- *
214 *
215 * Applies Rabin-Miller tests to %$p$% and %$(p - 1)/2$%.
216 */
217
218 typedef struct pgen_safetestctx {
219 pgen_safestepctx c;
220 rabin q, p;
221 } pgen_safetestctx;
222
223 extern int pgen_safetest(int /*rq*/, pgen_event */*ev*/, void */*p*/);
224
225 /*----- Miscellaneous steppers and testers --------------------------------*/
226
227 typedef struct pgen_gcdstepctx {
228 pfilt p, jp; /* Prime filter and step filter */
229 mp *q, *jq; /* %$p - 1$%, and a step value*/
230 mp *r; /* Other argument for GCD */
231 mp *g; /* GCD output (must be inited) */
232 mp *max; /* Maximum permissible GCD */
233 } pgen_gcdstepctx;
234
235 /* --- @pgen_gcdstep@ --- *
236 *
237 * Steps @p@ and @q@, until @p@ has no small factors, and
238 * %$\gcd(p, r) \le max$%.
239 */
240
241 extern int pgen_gcdstep(int /*rq*/, pgen_event */*ev*/, void */*p*/);
242
243 /*----- Standard event handlers -------------------------------------------*/
244
245 /* --- @pgen_evspin@ --- *
246 *
247 * Displays a spinning baton to show progress.
248 */
249
250 extern int pgen_evspin(int /*rq*/, pgen_event */*ev*/, void */*p*/);
251
252 /* --- @pgen_ev@ --- *
253 *
254 * Traditional event handler, shows dots for each test.
255 */
256
257 extern int pgen_ev(int /*rq*/, pgen_event */*ev*/, void */*p*/);
258
259 /*----- The main driver ---------------------------------------------------*/
260
261 /* --- @pgen@ --- *
262 *
263 * Arguments: @const char *name@ = name of the value being searched for
264 * @mp *d@ = destination for resulting integer
265 * @mp *m@ = start value to pass to stepper
266 * @pgen_proc *event@ = event handler function
267 * @void *ectx@ = context argument for event andler
268 * @unsigned steps@ = number of steps to take in search
269 * @pgen_proc *step@ = stepper function to use
270 * @void *sctx@ = context argument for stepper
271 * @unsigned tests@ = number of tests to make
272 * @pgen_proc *test@ = tester function to use
273 * @void *tctx@ = context argument for tester
274 *
275 * Returns: If successful, @PGEN_DONE@; otherwise @PGEN_ABORT@.
276 *
277 * Use: A generalized prime-number search skeleton. Yes, that's a
278 * scary number of arguments.
279 */
280
281 extern mp *pgen(const char */*name*/, mp */*d*/, mp */*m*/,
282 pgen_proc */*event*/, void */*ectx*/,
283 unsigned /*steps*/, pgen_proc */*step*/, void */*sctx*/,
284 unsigned /*tests*/, pgen_proc */*test*/, void */*tctx*/);
285
286 /*----- That's all, folks -------------------------------------------------*/
287
288 #ifdef __cplusplus
289 }
290 #endif
291
292 #endif