math/pgen.c (pgen_test): Use random witnesses only.
[catacomb] / math / pgen.c
CommitLineData
0f5ec153 1/* -*-c-*-
2 *
581c854e 3 * Prime generation glue
0f5ec153 4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
0f5ec153 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.
45c0fd36 16 *
0f5ec153 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.
45c0fd36 21 *
0f5ec153 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
0f5ec153 28/*----- Header files ------------------------------------------------------*/
29
581c854e 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"
0f5ec153 37#include "mp.h"
581c854e 38#include "mprand.h"
0f5ec153 39#include "pgen.h"
581c854e 40#include "pfilt.h"
0f5ec153 41#include "rabin.h"
42
581c854e 43/*----- Standard prime filter ---------------------------------------------*/
0f5ec153 44
581c854e 45/* --- @pgen_filter@ --- */
0f5ec153 46
581c854e 47int pgen_filter(int rq, pgen_event *ev, void *p)
0f5ec153 48{
581c854e 49 pgen_filterctx *f = p;
57541e97 50 int rc = PGEN_FAIL;
581c854e 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);
581c854e 59 break;
60 case PGEN_DONE:
61 pfilt_destroy(&f->f);
62 return (PGEN_DONE);
57541e97 63 default:
64 rc = PGEN_ABORT;
65 break;
0f5ec153 66 }
45c0fd36 67
57541e97 68 if (rc == PGEN_FAIL && !((f->step | f->f.m->v[0]) & 1))
69 rc = pfilt_step(&f->f, 1);
581c854e 70 while (rc == PGEN_FAIL)
71 rc = pfilt_step(&f->f, f->step);
72 ev->m = MP_COPY(f->f.m);
0f5ec153 73 return (rc);
74}
75
581c854e 76/* --- @pgen_jump@ --- *
0f5ec153 77 *
581c854e 78 * Similar to the standard @pgen_filter@, but jumps in large steps rather
79 * than small ones.
0f5ec153 80 */
81
581c854e 82int pgen_jump(int rq, pgen_event *ev, void *p)
0f5ec153 83{
581c854e 84 pgen_jumpctx *f = p;
85 int rc = PGEN_ABORT;
0f5ec153 86
581c854e 87 switch (rq) {
8b021c3f 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);
581c854e 96 rc = pfilt_create(&f->f, ev->m);
97 mp_drop(ev->m);
8b021c3f 98 } break;
581c854e 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 }
45c0fd36 107
581c854e 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}
0f5ec153 113
581c854e 114/*----- Standard prime test -----------------------------------------------*/
0f5ec153 115
581c854e 116/* --- @pgen_test@ --- */
0f5ec153 117
581c854e 118int pgen_test(int rq, pgen_event *ev, void *p)
119{
120 rabin *r = p;
a938be51 121 mp *a = MP_NEW;
581c854e 122 int rc = PGEN_ABORT;
0f5ec153 123
581c854e 124 switch (rq) {
125 case PGEN_BEGIN:
126 rabin_create(r, ev->m);
127 rc = PGEN_TRY;
128 break;
283b9af0 129 case PGEN_TRY:
a938be51
MW
130 a = mprand_range(a, ev->m, ev->r, 0);
131 rc = rabin_rtest(r, a);
283b9af0 132 break;
581c854e 133 case PGEN_DONE:
134 rabin_destroy(r);
135 rc = PGEN_DONE;
136 break;
0f5ec153 137 }
138
a938be51 139 mp_drop(a);
0f5ec153 140 return (rc);
141}
142
581c854e 143/*----- The main driver ---------------------------------------------------*/
144
145/* --- @pgen@ --- *
bd98b2df 146 *
581c854e 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
bd98b2df 158 *
581c854e 159 * Returns: Pointer to final result, or null.
bd98b2df 160 *
581c854e 161 * Use: A generalized prime-number search skeleton. Yes, that's a
162 * scary number of arguments.
bd98b2df 163 */
164
581c854e 165mp *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)
bd98b2df 168{
581c854e 169 pgen_event ev;
170 int rq, rc;
171 pgen_proc *proc;
172 void *ctx;
4108c8d2 173 int p;
174
175 enum { P_STEP, P_TEST };
bd98b2df 176
581c854e 177 /* --- Set up the initial event block --- */
bd98b2df 178
581c854e 179 ev.name = name;
180 if (m)
181 ev.m = MP_COPY(m);
182 else
183 ev.m = 0;
283b9af0 184 ev.steps = 0;
185 ev.tests = 0;
581c854e 186 ev.r = fibrand_create(0);
bd98b2df 187
581c854e 188 /* --- Tell the event handler we're under way --- */
bd98b2df 189
4108c8d2 190 if (event && event(PGEN_BEGIN, &ev, ectx) == PGEN_ABORT) {
191 ev.r->ops->destroy(ev.r);
581c854e 192 return (0);
4108c8d2 193 }
bd98b2df 194
581c854e 195 /* --- Set up for the initial call --- */
bd98b2df 196
4108c8d2 197 proc = step; ctx = sctx; p = P_STEP; rq = PGEN_BEGIN;
0f5ec153 198
581c854e 199 /* --- Enter the great maelstrom of state transitions --- */
0f5ec153 200
581c854e 201 for (;;) {
202 unsigned act = 0;
203
4108c8d2 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
581c854e 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:
4108c8d2 216 if (p == P_TEST)
581c854e 217 rq = PGEN_TRY;
218 else {
219 act |= A_EVENT;
4108c8d2 220 proc = test; ctx = tctx; p = P_TEST;
581c854e 221 rq = PGEN_BEGIN;
222 }
223 break;
224 case PGEN_PASS:
225 act |= A_TEST | A_EVENT;
4108c8d2 226 if (p == P_TEST)
581c854e 227 rq = PGEN_TRY;
228 else {
4108c8d2 229 proc = test; ctx = tctx; p = P_TEST;
581c854e 230 rq = PGEN_BEGIN;
231 }
232 break;
233 case PGEN_FAIL:
234 act |= A_STEP;
4108c8d2 235 if (p == P_TEST) {
581c854e 236 act |= A_ENDTEST | A_EVENT;
4108c8d2 237 proc = step; ctx = sctx; p = P_STEP;
581c854e 238 }
239 rq = PGEN_TRY;
240 break;
241 case PGEN_DONE:
242 act |= A_EVENT | A_DONE | A_ENDSTEP;
4108c8d2 243 if (p == P_TEST)
581c854e 244 act |= A_ENDTEST;
245 break;
246 case PGEN_ABORT:
247 act |= A_EVENT | A_DONE;
4108c8d2 248 if (p == P_TEST || rq == PGEN_TRY)
581c854e 249 act |= A_ENDSTEP;
4108c8d2 250 if (p == P_TEST && rq != PGEN_BEGIN)
581c854e 251 act |= A_ENDTEST;
252 break;
253 default:
254 assert(((void)"Invalid response from function", 0));
255 break;
256 }
0f5ec153 257
581c854e 258 /* --- If decrementing counters is requested, do that --- */
0f5ec153 259
581c854e 260 if ((act & A_STEP) && steps) {
283b9af0 261 ev.steps++;
262 if (ev.steps == steps) {
581c854e 263 act |= A_EVENT | A_ENDSTEP | A_DONE;
264 rc = PGEN_ABORT;
265 }
283b9af0 266 ev.tests = 0;
581c854e 267 }
0f5ec153 268
581c854e 269 if ((act & A_TEST) && tests) {
283b9af0 270 ev.tests++;
271 if (ev.tests == tests) {
581c854e 272 act |= A_ENDTEST | A_ENDSTEP | A_DONE;
273 rc = PGEN_DONE;
274 }
0f5ec153 275 }
0f5ec153 276
581c854e 277 /* --- Report an event if so directed --- */
bd98b2df 278
581c854e 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;
8501f5f0 283 if (p == P_TEST && rq != PGEN_BEGIN)
581c854e 284 act |= A_ENDTEST;
285 }
45c0fd36 286 }
bd98b2df 287
581c854e 288 /* --- Close down tester and stepper functions --- */
0f5ec153 289
581c854e 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);
eab06f16 308 mp_drop(d);
581c854e 309
310 return (ev.m);
0f5ec153 311}
312
34e4f738 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.
6001a9ff
MW
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.
34e4f738 330 */
331
332int pgen_primep(mp *p, grand *gr)
333{
34e4f738 334 rabin r;
6001a9ff 335 int rc;
34e4f738 336
a69a3efd 337 if (MP_NEGP(p)) return (0);
34e4f738 338 switch (pfilt_smallfactor(p)) {
339 case PGEN_DONE: return (1);
340 case PGEN_FAIL: return (0);
341 }
6001a9ff
MW
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);
34e4f738 346}
347
581c854e 348/*----- Test rig ----------------------------------------------------------*/
0f5ec153 349
350#ifdef TEST_RIG
351
352#include <mLib/testrig.h>
353
d31f4a79
MW
354static 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);
45c0fd36 376 return (ok);
d31f4a79
MW
377}
378
0f5ec153 379static int verify(dstr *v)
380{
381 mp *m = *(mp **)v[0].buf;
581c854e 382 mp *q = *(mp **)v[1].buf;
383 mp *p;
0f5ec153 384 int ok = 1;
385
581c854e 386 pgen_filterctx pf;
387 rabin r;
0f5ec153 388
581c854e 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);
22bab86c 392 if (!p || !MP_EQ(p, q)) {
581c854e 393 fputs("\n*** pgen failed", stderr);
0f5ec153 394 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
581c854e 395 fputs("\np = ", stderr); mp_writefile(p, stderr, 10);
396 fputs("\nq = ", stderr); mp_writefile(q, stderr, 10);
0f5ec153 397 fputc('\n', stderr);
398 ok = 0;
399 }
400
401 mp_drop(m);
581c854e 402 mp_drop(q);
eab06f16 403 mp_drop(p);
d94f85ac 404 assert(mparena_count(MPARENA_GLOBAL) == 0);
0f5ec153 405 return (ok);
406}
407
408static test_chunk tests[] = {
409 { "pgen", verify, { &type_mp, &type_mp, 0 } },
d31f4a79 410 { "primep", t_primep, { &type_mp, &type_int, 0 } },
0f5ec153 411 { 0, 0, { 0 } }
412};
413
414int main(int argc, char *argv[])
415{
416 sub_init();
0f00dc4c 417 test_run(argc, argv, tests, SRCDIR "/t/pgen");
0f5ec153 418 return (0);
419}
0f5ec153 420#endif
421
422/*----- That's all, folks -------------------------------------------------*/