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