0f5ec153 |
1 | /* -*-c-*- |
2 | * |
34e4f738 |
3 | * $Id: pgen.c,v 1.9 2004/04/01 12:50:09 mdw Exp $ |
0f5ec153 |
4 | * |
581c854e |
5 | * Prime generation glue |
0f5ec153 |
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.c,v $ |
34e4f738 |
33 | * Revision 1.9 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 | * |
283b9af0 |
40 | * Revision 1.8 2002/01/13 13:42:53 mdw |
41 | * More efficient Rabin-Miller test: with random witnesses, skip redundant |
42 | * Montgomerization. (Being bijective, it can't affect the distribution.) |
43 | * |
eab06f16 |
44 | * Revision 1.7 2001/02/03 16:05:32 mdw |
45 | * Now @mp_drop@ checks its argument is non-NULL before attempting to free |
46 | * it. Note that the macro version @MP_DROP@ doesn't do this. |
47 | * |
22bab86c |
48 | * Revision 1.6 2000/10/08 12:11:22 mdw |
49 | * Use @MP_EQ@ instead of @MP_CMP@. |
50 | * |
8b021c3f |
51 | * Revision 1.5 2000/06/17 11:52:36 mdw |
52 | * Signal a pgen abort if the jump and base share a common factor. |
53 | * |
581c854e |
54 | * Revision 1.4 1999/12/22 16:01:11 mdw |
55 | * Same file, completely different code. Main interface for new prime- |
56 | * search system. |
0f5ec153 |
57 | * |
58 | */ |
59 | |
60 | /*----- Header files ------------------------------------------------------*/ |
61 | |
581c854e |
62 | #include <assert.h> |
63 | #include <stdio.h> |
64 | #include <stdlib.h> |
65 | #include <string.h> |
66 | |
67 | #include "fibrand.h" |
68 | #include "grand.h" |
0f5ec153 |
69 | #include "mp.h" |
581c854e |
70 | #include "mprand.h" |
0f5ec153 |
71 | #include "pgen.h" |
581c854e |
72 | #include "pfilt.h" |
0f5ec153 |
73 | #include "rabin.h" |
74 | |
581c854e |
75 | /*----- Standard prime filter ---------------------------------------------*/ |
0f5ec153 |
76 | |
581c854e |
77 | /* --- @pgen_filter@ --- */ |
0f5ec153 |
78 | |
581c854e |
79 | int pgen_filter(int rq, pgen_event *ev, void *p) |
0f5ec153 |
80 | { |
581c854e |
81 | pgen_filterctx *f = p; |
82 | int rc = PGEN_ABORT; |
83 | |
84 | switch (rq) { |
85 | case PGEN_BEGIN: |
86 | rc = pfilt_create(&f->f, ev->m); |
87 | mp_drop(ev->m); |
88 | break; |
89 | case PGEN_TRY: |
90 | mp_drop(ev->m); |
91 | if (!((f->step | f->f.m->v[0]) & 1)) |
92 | rc = pfilt_step(&f->f, 1); |
0f5ec153 |
93 | else |
581c854e |
94 | rc = pfilt_step(&f->f, f->step); |
95 | break; |
96 | case PGEN_DONE: |
97 | pfilt_destroy(&f->f); |
98 | return (PGEN_DONE); |
0f5ec153 |
99 | } |
581c854e |
100 | |
101 | while (rc == PGEN_FAIL) |
102 | rc = pfilt_step(&f->f, f->step); |
103 | ev->m = MP_COPY(f->f.m); |
0f5ec153 |
104 | return (rc); |
105 | } |
106 | |
581c854e |
107 | /* --- @pgen_jump@ --- * |
0f5ec153 |
108 | * |
581c854e |
109 | * Similar to the standard @pgen_filter@, but jumps in large steps rather |
110 | * than small ones. |
0f5ec153 |
111 | */ |
112 | |
581c854e |
113 | int pgen_jump(int rq, pgen_event *ev, void *p) |
0f5ec153 |
114 | { |
581c854e |
115 | pgen_jumpctx *f = p; |
116 | int rc = PGEN_ABORT; |
0f5ec153 |
117 | |
581c854e |
118 | switch (rq) { |
8b021c3f |
119 | case PGEN_BEGIN: { |
120 | mp *g = MP_NEW; |
121 | mp_gcd(&g, 0, 0, ev->m, f->j->m); |
122 | if (MP_CMP(g, >, MP_ONE)) { |
123 | mp_drop(g); |
124 | return (PGEN_ABORT); |
125 | } |
126 | mp_drop(g); |
581c854e |
127 | rc = pfilt_create(&f->f, ev->m); |
128 | mp_drop(ev->m); |
8b021c3f |
129 | } break; |
581c854e |
130 | case PGEN_TRY: |
131 | mp_drop(ev->m); |
132 | rc = pfilt_jump(&f->f, f->j); |
133 | break; |
134 | case PGEN_DONE: |
135 | pfilt_destroy(&f->f); |
136 | return (PGEN_DONE); |
137 | } |
138 | |
139 | while (rc == PGEN_FAIL) |
140 | rc = pfilt_jump(&f->f, f->j); |
141 | ev->m = MP_COPY(f->f.m); |
142 | return (rc); |
143 | } |
0f5ec153 |
144 | |
581c854e |
145 | /*----- Standard prime test -----------------------------------------------*/ |
0f5ec153 |
146 | |
581c854e |
147 | /* --- @pgen_test@ --- */ |
0f5ec153 |
148 | |
581c854e |
149 | int pgen_test(int rq, pgen_event *ev, void *p) |
150 | { |
151 | rabin *r = p; |
152 | int rc = PGEN_ABORT; |
0f5ec153 |
153 | |
581c854e |
154 | switch (rq) { |
155 | case PGEN_BEGIN: |
156 | rabin_create(r, ev->m); |
157 | rc = PGEN_TRY; |
158 | break; |
283b9af0 |
159 | case PGEN_TRY: |
160 | if (!ev->tests) |
161 | rc = rabin_rtest(r, MP_TWO); |
162 | else { |
163 | mp *a = mprand_range(MP_NEW, ev->m, ev->r, 0); |
164 | rc = rabin_rtest(r, a); |
165 | mp_drop(a); |
166 | } |
167 | break; |
581c854e |
168 | case PGEN_DONE: |
169 | rabin_destroy(r); |
170 | rc = PGEN_DONE; |
171 | break; |
0f5ec153 |
172 | } |
173 | |
0f5ec153 |
174 | return (rc); |
175 | } |
176 | |
581c854e |
177 | /*----- The main driver ---------------------------------------------------*/ |
178 | |
179 | /* --- @pgen@ --- * |
bd98b2df |
180 | * |
581c854e |
181 | * Arguments: @const char *name@ = name of the value being searched for |
182 | * @mp *d@ = destination for the result integer |
183 | * @mp *m@ = start value to pass to stepper |
184 | * @pgen_proc *event@ = event handler function |
185 | * @void *ectx@ = context argument for event andler |
186 | * @unsigned steps@ = number of steps to take in search |
187 | * @pgen_proc *step@ = stepper function to use |
188 | * @void *sctx@ = context argument for stepper |
189 | * @unsigned tests@ = number of tests to make |
190 | * @pgen_proc *test@ = tester function to use |
191 | * @void *tctx@ = context argument for tester |
bd98b2df |
192 | * |
581c854e |
193 | * Returns: Pointer to final result, or null. |
bd98b2df |
194 | * |
581c854e |
195 | * Use: A generalized prime-number search skeleton. Yes, that's a |
196 | * scary number of arguments. |
bd98b2df |
197 | */ |
198 | |
581c854e |
199 | mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx, |
200 | unsigned steps, pgen_proc *step, void *sctx, |
201 | unsigned tests, pgen_proc *test, void *tctx) |
bd98b2df |
202 | { |
581c854e |
203 | pgen_event ev; |
204 | int rq, rc; |
205 | pgen_proc *proc; |
206 | void *ctx; |
bd98b2df |
207 | |
581c854e |
208 | /* --- Set up the initial event block --- */ |
bd98b2df |
209 | |
581c854e |
210 | ev.name = name; |
211 | if (m) |
212 | ev.m = MP_COPY(m); |
213 | else |
214 | ev.m = 0; |
283b9af0 |
215 | ev.steps = 0; |
216 | ev.tests = 0; |
581c854e |
217 | ev.r = fibrand_create(0); |
bd98b2df |
218 | |
581c854e |
219 | /* --- Tell the event handler we're under way --- */ |
bd98b2df |
220 | |
581c854e |
221 | if (event && event(PGEN_BEGIN, &ev, ectx) == PGEN_ABORT) |
222 | return (0); |
bd98b2df |
223 | |
581c854e |
224 | /* --- Set up for the initial call --- */ |
bd98b2df |
225 | |
581c854e |
226 | proc = step; ctx = sctx; rq = PGEN_BEGIN; |
0f5ec153 |
227 | |
581c854e |
228 | /* --- Enter the great maelstrom of state transitions --- */ |
0f5ec153 |
229 | |
581c854e |
230 | for (;;) { |
231 | unsigned act = 0; |
232 | |
233 | enum { |
234 | A_STEP = 1u, |
235 | A_TEST = 2u, |
236 | A_EVENT = 4u, |
237 | A_ENDTEST = 8u, |
238 | A_ENDSTEP = 16u, |
239 | A_DONE = 32u |
240 | }; |
241 | |
242 | /* --- Call the procedure and decide what to do next --- */ |
243 | |
244 | rc = proc(rq, &ev, ctx); |
245 | switch (rc) { |
246 | case PGEN_TRY: |
247 | if (proc == test) |
248 | rq = PGEN_TRY; |
249 | else { |
250 | act |= A_EVENT; |
251 | proc = test; ctx = tctx; |
252 | rq = PGEN_BEGIN; |
253 | } |
254 | break; |
255 | case PGEN_PASS: |
256 | act |= A_TEST | A_EVENT; |
257 | if (proc == test) |
258 | rq = PGEN_TRY; |
259 | else { |
260 | proc = test; ctx = tctx; |
261 | rq = PGEN_BEGIN; |
262 | } |
263 | break; |
264 | case PGEN_FAIL: |
265 | act |= A_STEP; |
266 | if (proc == test) { |
267 | act |= A_ENDTEST | A_EVENT; |
268 | proc = step; ctx = sctx; |
269 | } |
270 | rq = PGEN_TRY; |
271 | break; |
272 | case PGEN_DONE: |
273 | act |= A_EVENT | A_DONE | A_ENDSTEP; |
274 | if (proc == test) |
275 | act |= A_ENDTEST; |
276 | break; |
277 | case PGEN_ABORT: |
278 | act |= A_EVENT | A_DONE; |
279 | if (proc == test || rq == PGEN_TRY) |
280 | act |= A_ENDSTEP; |
281 | if (proc == test && rq == PGEN_BEGIN) |
282 | act |= A_ENDTEST; |
283 | break; |
284 | default: |
285 | assert(((void)"Invalid response from function", 0)); |
286 | break; |
287 | } |
0f5ec153 |
288 | |
581c854e |
289 | /* --- If decrementing counters is requested, do that --- */ |
0f5ec153 |
290 | |
581c854e |
291 | if ((act & A_STEP) && steps) { |
283b9af0 |
292 | ev.steps++; |
293 | if (ev.steps == steps) { |
581c854e |
294 | act |= A_EVENT | A_ENDSTEP | A_DONE; |
295 | rc = PGEN_ABORT; |
296 | } |
283b9af0 |
297 | ev.tests = 0; |
581c854e |
298 | } |
0f5ec153 |
299 | |
581c854e |
300 | if ((act & A_TEST) && tests) { |
283b9af0 |
301 | ev.tests++; |
302 | if (ev.tests == tests) { |
581c854e |
303 | act |= A_ENDTEST | A_ENDSTEP | A_DONE; |
304 | rc = PGEN_DONE; |
305 | } |
0f5ec153 |
306 | } |
0f5ec153 |
307 | |
581c854e |
308 | /* --- Report an event if so directed --- */ |
bd98b2df |
309 | |
581c854e |
310 | if ((act & A_EVENT) && event && event(rc, &ev, ectx) == PGEN_ABORT) { |
311 | rc = PGEN_ABORT; |
312 | if (!(act & A_DONE)) { |
313 | act |= A_ENDSTEP | A_DONE; |
314 | if (proc == test) |
315 | act |= A_ENDTEST; |
316 | } |
317 | } |
bd98b2df |
318 | |
581c854e |
319 | /* --- Close down tester and stepper functions --- */ |
0f5ec153 |
320 | |
581c854e |
321 | if (act & A_ENDTEST) |
322 | test(PGEN_DONE, &ev, tctx); |
323 | if (act & A_ENDSTEP) |
324 | step(PGEN_DONE, &ev, sctx); |
325 | |
326 | /* --- Stop the entire test if necessary --- */ |
327 | |
328 | if (act & A_DONE) |
329 | break; |
330 | } |
331 | |
332 | /* --- Tidy up and return --- */ |
333 | |
334 | if (rc == PGEN_ABORT) { |
335 | mp_drop(ev.m); |
336 | ev.m = 0; |
337 | } |
338 | ev.r->ops->destroy(ev.r); |
eab06f16 |
339 | mp_drop(d); |
581c854e |
340 | |
341 | return (ev.m); |
0f5ec153 |
342 | } |
343 | |
34e4f738 |
344 | /* --- @pgen_primep@ --- * |
345 | * |
346 | * Arguments: @mp *p@ = a number to check |
347 | * @grand *gr@ = a random number source |
348 | * |
349 | * Returns: Nonzero if @p@ is really prime. |
350 | */ |
351 | |
352 | int pgen_primep(mp *p, grand *gr) |
353 | { |
354 | int i = rabin_iters(mp_bits(p)); |
355 | rabin r; |
356 | mp *x = MP_NEW; |
357 | |
358 | if (MP_ISNEG(p)) return (0); |
359 | switch (pfilt_smallfactor(p)) { |
360 | case PGEN_DONE: return (1); |
361 | case PGEN_FAIL: return (0); |
362 | } |
363 | rabin_create(&r, p); |
364 | while (i) { |
365 | x = mprand_range(x, p, gr, 0); |
366 | if (rabin_rtest(&r, x) == PGEN_FAIL) |
367 | break; |
368 | i--; |
369 | } |
370 | MP_DROP(x); |
371 | rabin_destroy(&r); |
372 | return (!i); |
373 | } |
374 | |
581c854e |
375 | /*----- Test rig ----------------------------------------------------------*/ |
0f5ec153 |
376 | |
377 | #ifdef TEST_RIG |
378 | |
379 | #include <mLib/testrig.h> |
380 | |
381 | static int verify(dstr *v) |
382 | { |
383 | mp *m = *(mp **)v[0].buf; |
581c854e |
384 | mp *q = *(mp **)v[1].buf; |
385 | mp *p; |
0f5ec153 |
386 | int ok = 1; |
387 | |
581c854e |
388 | pgen_filterctx pf; |
389 | rabin r; |
0f5ec153 |
390 | |
581c854e |
391 | pf.step = 2; |
392 | p = pgen("p", MP_NEW, m, pgen_evspin, 0, 0, pgen_filter, &pf, |
393 | rabin_iters(mp_bits(m)), pgen_test, &r); |
22bab86c |
394 | if (!p || !MP_EQ(p, q)) { |
581c854e |
395 | fputs("\n*** pgen failed", stderr); |
0f5ec153 |
396 | fputs("\nm = ", stderr); mp_writefile(m, stderr, 10); |
581c854e |
397 | fputs("\np = ", stderr); mp_writefile(p, stderr, 10); |
398 | fputs("\nq = ", stderr); mp_writefile(q, stderr, 10); |
0f5ec153 |
399 | fputc('\n', stderr); |
400 | ok = 0; |
401 | } |
402 | |
403 | mp_drop(m); |
581c854e |
404 | mp_drop(q); |
eab06f16 |
405 | mp_drop(p); |
d94f85ac |
406 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
0f5ec153 |
407 | return (ok); |
408 | } |
409 | |
410 | static test_chunk tests[] = { |
411 | { "pgen", verify, { &type_mp, &type_mp, 0 } }, |
412 | { 0, 0, { 0 } } |
413 | }; |
414 | |
415 | int main(int argc, char *argv[]) |
416 | { |
417 | sub_init(); |
418 | test_run(argc, argv, tests, SRCDIR "/tests/pgen"); |
419 | return (0); |
420 | } |
0f5ec153 |
421 | #endif |
422 | |
423 | /*----- That's all, folks -------------------------------------------------*/ |