math/pgen.c (pgen_test): Use random witnesses only.
[catacomb] / math / pgen.c
1 /* -*-c-*-
2 *
3 * Prime generation glue
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "fibrand.h"
36 #include "grand.h"
37 #include "mp.h"
38 #include "mprand.h"
39 #include "pgen.h"
40 #include "pfilt.h"
41 #include "rabin.h"
42
43 /*----- Standard prime filter ---------------------------------------------*/
44
45 /* --- @pgen_filter@ --- */
46
47 int pgen_filter(int rq, pgen_event *ev, void *p)
48 {
49 pgen_filterctx *f = p;
50 int rc = PGEN_FAIL;
51
52 switch (rq) {
53 case PGEN_BEGIN:
54 rc = pfilt_create(&f->f, ev->m);
55 mp_drop(ev->m);
56 break;
57 case PGEN_TRY:
58 mp_drop(ev->m);
59 break;
60 case PGEN_DONE:
61 pfilt_destroy(&f->f);
62 return (PGEN_DONE);
63 default:
64 rc = PGEN_ABORT;
65 break;
66 }
67
68 if (rc == PGEN_FAIL && !((f->step | f->f.m->v[0]) & 1))
69 rc = pfilt_step(&f->f, 1);
70 while (rc == PGEN_FAIL)
71 rc = pfilt_step(&f->f, f->step);
72 ev->m = MP_COPY(f->f.m);
73 return (rc);
74 }
75
76 /* --- @pgen_jump@ --- *
77 *
78 * Similar to the standard @pgen_filter@, but jumps in large steps rather
79 * than small ones.
80 */
81
82 int pgen_jump(int rq, pgen_event *ev, void *p)
83 {
84 pgen_jumpctx *f = p;
85 int rc = PGEN_ABORT;
86
87 switch (rq) {
88 case PGEN_BEGIN: {
89 mp *g = MP_NEW;
90 mp_gcd(&g, 0, 0, ev->m, f->j->m);
91 if (MP_CMP(g, >, MP_ONE)) {
92 mp_drop(g);
93 return (PGEN_ABORT);
94 }
95 mp_drop(g);
96 rc = pfilt_create(&f->f, ev->m);
97 mp_drop(ev->m);
98 } break;
99 case PGEN_TRY:
100 mp_drop(ev->m);
101 rc = pfilt_jump(&f->f, f->j);
102 break;
103 case PGEN_DONE:
104 pfilt_destroy(&f->f);
105 return (PGEN_DONE);
106 }
107
108 while (rc == PGEN_FAIL)
109 rc = pfilt_jump(&f->f, f->j);
110 ev->m = MP_COPY(f->f.m);
111 return (rc);
112 }
113
114 /*----- Standard prime test -----------------------------------------------*/
115
116 /* --- @pgen_test@ --- */
117
118 int pgen_test(int rq, pgen_event *ev, void *p)
119 {
120 rabin *r = p;
121 mp *a = MP_NEW;
122 int rc = PGEN_ABORT;
123
124 switch (rq) {
125 case PGEN_BEGIN:
126 rabin_create(r, ev->m);
127 rc = PGEN_TRY;
128 break;
129 case PGEN_TRY:
130 a = mprand_range(a, ev->m, ev->r, 0);
131 rc = rabin_rtest(r, a);
132 break;
133 case PGEN_DONE:
134 rabin_destroy(r);
135 rc = PGEN_DONE;
136 break;
137 }
138
139 mp_drop(a);
140 return (rc);
141 }
142
143 /*----- The main driver ---------------------------------------------------*/
144
145 /* --- @pgen@ --- *
146 *
147 * Arguments: @const char *name@ = name of the value being searched for
148 * @mp *d@ = destination for the result integer
149 * @mp *m@ = start value to pass to stepper
150 * @pgen_proc *event@ = event handler function
151 * @void *ectx@ = context argument for event andler
152 * @unsigned steps@ = number of steps to take in search
153 * @pgen_proc *step@ = stepper function to use
154 * @void *sctx@ = context argument for stepper
155 * @unsigned tests@ = number of tests to make
156 * @pgen_proc *test@ = tester function to use
157 * @void *tctx@ = context argument for tester
158 *
159 * Returns: Pointer to final result, or null.
160 *
161 * Use: A generalized prime-number search skeleton. Yes, that's a
162 * scary number of arguments.
163 */
164
165 mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx,
166 unsigned steps, pgen_proc *step, void *sctx,
167 unsigned tests, pgen_proc *test, void *tctx)
168 {
169 pgen_event ev;
170 int rq, rc;
171 pgen_proc *proc;
172 void *ctx;
173 int p;
174
175 enum { P_STEP, P_TEST };
176
177 /* --- Set up the initial event block --- */
178
179 ev.name = name;
180 if (m)
181 ev.m = MP_COPY(m);
182 else
183 ev.m = 0;
184 ev.steps = 0;
185 ev.tests = 0;
186 ev.r = fibrand_create(0);
187
188 /* --- Tell the event handler we're under way --- */
189
190 if (event && event(PGEN_BEGIN, &ev, ectx) == PGEN_ABORT) {
191 ev.r->ops->destroy(ev.r);
192 return (0);
193 }
194
195 /* --- Set up for the initial call --- */
196
197 proc = step; ctx = sctx; p = P_STEP; rq = PGEN_BEGIN;
198
199 /* --- Enter the great maelstrom of state transitions --- */
200
201 for (;;) {
202 unsigned act = 0;
203
204 #define A_STEP 1u
205 #define A_TEST 2u
206 #define A_EVENT 4u
207 #define A_ENDTEST 8u
208 #define A_ENDSTEP 16u
209 #define A_DONE 32u
210
211 /* --- Call the procedure and decide what to do next --- */
212
213 rc = proc(rq, &ev, ctx);
214 switch (rc) {
215 case PGEN_TRY:
216 if (p == P_TEST)
217 rq = PGEN_TRY;
218 else {
219 act |= A_EVENT;
220 proc = test; ctx = tctx; p = P_TEST;
221 rq = PGEN_BEGIN;
222 }
223 break;
224 case PGEN_PASS:
225 act |= A_TEST | A_EVENT;
226 if (p == P_TEST)
227 rq = PGEN_TRY;
228 else {
229 proc = test; ctx = tctx; p = P_TEST;
230 rq = PGEN_BEGIN;
231 }
232 break;
233 case PGEN_FAIL:
234 act |= A_STEP;
235 if (p == P_TEST) {
236 act |= A_ENDTEST | A_EVENT;
237 proc = step; ctx = sctx; p = P_STEP;
238 }
239 rq = PGEN_TRY;
240 break;
241 case PGEN_DONE:
242 act |= A_EVENT | A_DONE | A_ENDSTEP;
243 if (p == P_TEST)
244 act |= A_ENDTEST;
245 break;
246 case PGEN_ABORT:
247 act |= A_EVENT | A_DONE;
248 if (p == P_TEST || rq == PGEN_TRY)
249 act |= A_ENDSTEP;
250 if (p == P_TEST && rq != PGEN_BEGIN)
251 act |= A_ENDTEST;
252 break;
253 default:
254 assert(((void)"Invalid response from function", 0));
255 break;
256 }
257
258 /* --- If decrementing counters is requested, do that --- */
259
260 if ((act & A_STEP) && steps) {
261 ev.steps++;
262 if (ev.steps == steps) {
263 act |= A_EVENT | A_ENDSTEP | A_DONE;
264 rc = PGEN_ABORT;
265 }
266 ev.tests = 0;
267 }
268
269 if ((act & A_TEST) && tests) {
270 ev.tests++;
271 if (ev.tests == tests) {
272 act |= A_ENDTEST | A_ENDSTEP | A_DONE;
273 rc = PGEN_DONE;
274 }
275 }
276
277 /* --- Report an event if so directed --- */
278
279 if ((act & A_EVENT) && event && event(rc, &ev, ectx) == PGEN_ABORT) {
280 rc = PGEN_ABORT;
281 if (!(act & A_DONE)) {
282 act |= A_ENDSTEP | A_DONE;
283 if (p == P_TEST && rq != PGEN_BEGIN)
284 act |= A_ENDTEST;
285 }
286 }
287
288 /* --- Close down tester and stepper functions --- */
289
290 if (act & A_ENDTEST)
291 test(PGEN_DONE, &ev, tctx);
292 if (act & A_ENDSTEP)
293 step(PGEN_DONE, &ev, sctx);
294
295 /* --- Stop the entire test if necessary --- */
296
297 if (act & A_DONE)
298 break;
299 }
300
301 /* --- Tidy up and return --- */
302
303 if (rc == PGEN_ABORT) {
304 mp_drop(ev.m);
305 ev.m = 0;
306 }
307 ev.r->ops->destroy(ev.r);
308 mp_drop(d);
309
310 return (ev.m);
311 }
312
313 /* --- @pgen_primep@ --- *
314 *
315 * Arguments: @mp *p@ = a number to check
316 * @grand *gr@ = a random number source
317 *
318 * Returns: Nonzero if @p@ is really prime.
319 *
320 * Use: Checks the primality of @p@. If @p@ is prime, then this
321 * function returns nonzero; if @p@ is really composite then it
322 * %%\emph{probably}%% returns zero, but might not.
323 *
324 * Currently, this function uses the Baillie--PSW test, which
325 * combines a single Miller--Rabin test with witness 2 with a
326 * single Frobenius test with parameters chosen using
327 * Selfridge's `Method A'. No composites are known which pass
328 * this test, though it's conjectured that infinitely many
329 * exist.
330 */
331
332 int pgen_primep(mp *p, grand *gr)
333 {
334 rabin r;
335 int rc;
336
337 if (MP_NEGP(p)) return (0);
338 switch (pfilt_smallfactor(p)) {
339 case PGEN_DONE: return (1);
340 case PGEN_FAIL: return (0);
341 }
342 rabin_create(&r, p); rc = rabin_test(&r, MP_TWO); rabin_destroy(&r);
343 if (rc == PGEN_FAIL) return (0);
344 rc = pgen_granfrob(p, 0, 0); if (rc == PGEN_FAIL) return (0);
345 return (1);
346 }
347
348 /*----- Test rig ----------------------------------------------------------*/
349
350 #ifdef TEST_RIG
351
352 #include <mLib/testrig.h>
353
354 static int t_primep(dstr *v)
355 {
356 mp *m = *(mp **)v[0].buf;
357 int e = *(int *)v[1].buf;
358 int r;
359 grand *rng;
360 int ok = 1;
361
362 rng = fibrand_create(0);
363 r = pgen_primep(m, rng);
364 GR_DESTROY(rng);
365 if (e != r) {
366 fputs("\n*** primep failed", stderr);
367 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
368 fprintf(stderr, "\nexpected %d", e);
369 fprintf(stderr, "\nreported %d", r);
370 fputc('\n', stderr);
371 ok = 0;
372 }
373
374 mp_drop(m);
375 assert(mparena_count(MPARENA_GLOBAL) == 0);
376 return (ok);
377 }
378
379 static int verify(dstr *v)
380 {
381 mp *m = *(mp **)v[0].buf;
382 mp *q = *(mp **)v[1].buf;
383 mp *p;
384 int ok = 1;
385
386 pgen_filterctx pf;
387 rabin r;
388
389 pf.step = 2;
390 p = pgen("p", MP_NEW, m, pgen_evspin, 0, 0, pgen_filter, &pf,
391 rabin_iters(mp_bits(m)), pgen_test, &r);
392 if (!p || !MP_EQ(p, q)) {
393 fputs("\n*** pgen failed", stderr);
394 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
395 fputs("\np = ", stderr); mp_writefile(p, stderr, 10);
396 fputs("\nq = ", stderr); mp_writefile(q, stderr, 10);
397 fputc('\n', stderr);
398 ok = 0;
399 }
400
401 mp_drop(m);
402 mp_drop(q);
403 mp_drop(p);
404 assert(mparena_count(MPARENA_GLOBAL) == 0);
405 return (ok);
406 }
407
408 static test_chunk tests[] = {
409 { "pgen", verify, { &type_mp, &type_mp, 0 } },
410 { "primep", t_primep, { &type_mp, &type_int, 0 } },
411 { 0, 0, { 0 } }
412 };
413
414 int main(int argc, char *argv[])
415 {
416 sub_init();
417 test_run(argc, argv, tests, SRCDIR "/t/pgen");
418 return (0);
419 }
420 #endif
421
422 /*----- That's all, folks -------------------------------------------------*/