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