rand.c, pgen.c: Invalidate random generators from pgen events.
[catacomb-python] / pgen.c
CommitLineData
d7ab1bab 1/* -*-c-*-
2 *
d7ab1bab 3 * Prime number generation
4 *
5 * (c) 2005 Straylight/Edgeware
6 */
7
b2687a0a 8/*----- Licensing notice --------------------------------------------------*
d7ab1bab 9 *
10 * This file is part of the Python interface to Catacomb.
11 *
12 * Catacomb/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
b2687a0a 16 *
d7ab1bab 17 * Catacomb/Python 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 General Public License for more details.
b2687a0a 21 *
d7ab1bab 22 * You should have received a copy of the GNU General Public License
23 * along with Catacomb/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27/*----- Header files ------------------------------------------------------*/
28
29#include "catacomb-python.h"
30
31/*----- Filters -----------------------------------------------------------*/
32
33PyTypeObject *pfilt_pytype;
34
35static PyObject *pfilt_pywrap(pfilt *f)
36{
37 pfilt_pyobj *o = 0;
38 o = PyObject_New(pfilt_pyobj, pfilt_pytype);
39 o->f = *f;
40 o->st = pfilt_step(f, 0);
b2687a0a 41 return ((PyObject *)o);
d7ab1bab 42}
43
44static PyObject *pfilt_pymake(PyTypeObject *ty, PyObject *xobj)
45{
46 mp *x = 0;
47 pfilt_pyobj *o = 0;
48
49 if (PFILT_PYCHECK(xobj)) RETURN_OBJ(xobj);
50 if ((x = getmp(xobj)) == 0) goto end;
51 o = (pfilt_pyobj *)ty->tp_alloc(ty, 0);
52 o->st = pfilt_create(&o->f, x);
53end:
54 mp_drop(x);
55 return ((PyObject *)o);
56}
57
58static PyObject *pfilt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
59{
60 char *kwlist[] = { "x", 0 };
61 PyObject *xobj;
62
63 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &xobj))
64 return (0);
65 return (pfilt_pymake(ty, xobj));
66}
67
68static void pfilt_pydealloc(PyObject *me)
3aa33042 69 { pfilt_destroy(PFILT_F(me)); FREEOBJ(me); }
d7ab1bab 70
71static PyObject *pfmeth_step(PyObject *me, PyObject *arg)
72{
73 mpw x;
74
75 if (!PyArg_ParseTuple(arg, "O&:step", convmpw, &x)) return (0);
76 PFILT_ST(me) = pfilt_step(PFILT_F(me), x);
77 RETURN_ME;
78}
79
80static PyObject *pfmeth_muladd(PyObject *me, PyObject *arg)
81{
82 mpw m, a;
83 pfilt_pyobj *o = 0;
84
85 if (!PyArg_ParseTuple(arg, "O&O&:muladd", convmpw, &m, convmpw, &a))
86 return (0);
87 o = PyObject_New(pfilt_pyobj, pfilt_pytype);
88 o->st = pfilt_muladd(&o->f, PFILT_F(me), m, a);
89 return ((PyObject *)o);
90}
91
92static CONVFUNC(pfilt, pfilt *, PFILT_F)
93
94static PyObject *pfmeth_jump(PyObject *me, PyObject *arg)
95{
96 pfilt *f;
97
98 if (!PyArg_ParseTuple(arg, "O&:jump", convpfilt, &f)) return (0);
99 PFILT_ST(me) = pfilt_jump(PFILT_F(me), f);
100 RETURN_ME;
101}
102
103static PyObject *meth__PrimeFilter_smallfactor(PyObject *me, PyObject *arg)
104{
105 mp *x = 0;
106 PyObject *rc = 0;
107
108 if (!PyArg_ParseTuple(arg, "OO&:smallfactor", &me, convmp, &x)) goto end;
109 rc = PyInt_FromLong(pfilt_smallfactor(x));
110end:
111 mp_drop(x);
112 return (rc);
113}
114
115static int pfilt_pynonzerop(PyObject *me)
116 { return (PFILT_ST(me) != PGEN_FAIL); }
117
118static PyObject *pfilt_pyint(PyObject *me)
119{
120 long l;
121 PyObject *rc = 0;
122
bc243788
MW
123 if (!mp_tolong_checked(PFILT_F(me)->m, &l, 0)) rc = PyInt_FromLong(l);
124 else rc = mp_topylong(PFILT_F(me)->m);
d7ab1bab 125 return (rc);
126}
127
128static PyObject *pfilt_pylong(PyObject *me)
f368b46e 129 { return (mp_topylong(PFILT_F(me)->m)); }
d7ab1bab 130
131static PyObject *pfget_x(PyObject *me, void *hunoz)
132 { return (mp_pywrap(MP_COPY(PFILT_F(me)->m))); }
133
134static PyObject *pfget_status(PyObject *me, void *hunoz)
135 { return (PyInt_FromLong(PFILT_ST(me))); }
136
137static PyGetSetDef pfilt_pygetset[] = {
138#define GETSETNAME(op, name) pf##op##_##name
139 GET (x, "F.x -> current position of filter")
140 GET (status, "F.status -> primality status of filter")
141#undef GETSETNAME
142 { 0 }
143};
144
145static PyMethodDef pfilt_pymethods[] = {
146#define METHNAME(name) pfmeth_##name
147 METH (step, "F.step(N)")
148 METH (muladd, "F.muladd(M, A)")
149 METH (jump, "F.jump(FF)")
150#undef METHNAME
151 { 0 }
152};
153
154static PyNumberMethods pfilt_pynumber = {
155 0, /* @nb_add@ */
156 0, /* @nb_subtract@ */
157 0, /* @nb_multiply@ */
158 0, /* @nb_divide@ */
159 0, /* @nb_remainder@ */
160 0, /* @nb_divmod@ */
161 0, /* @nb_power@ */
162 0, /* @nb_negative@ */
163 0, /* @nb_positive@ */
164 0, /* @nb_absolute@ */
165 pfilt_pynonzerop, /* @nb_nonzero@ */
166 0, /* @nb_invert@ */
167 0, /* @nb_lshift@ */
168 0, /* @nb_rshift@ */
169 0, /* @nb_and@ */
170 0, /* @nb_xor@ */
171 0, /* @nb_or@ */
172 0, /* @nb_coerce@ */
173 pfilt_pyint, /* @nb_int@ */
174 pfilt_pylong, /* @nb_long@ */
175 0, /* @nb_float@ */
176 0, /* @nb_oct@ */
177 0, /* @nb_hex@ */
178
179 0, /* @nb_inplace_add@ */
180 0, /* @nb_inplace_subtract@ */
181 0, /* @nb_inplace_multiply@ */
182 0, /* @nb_inplace_divide@ */
183 0, /* @nb_inplace_remainder@ */
184 0, /* @nb_inplace_power@ */
185 0, /* @nb_inplace_lshift@ */
186 0, /* @nb_inplace_rshift@ */
187 0, /* @nb_inplace_and@ */
188 0, /* @nb_inplace_xor@ */
189 0, /* @nb_inplace_or@ */
190
191 0, /* @nb_floor_divide@ */
192 0, /* @nb_true_divide@ */
193 0, /* @nb_inplace_floor_divide@ */
194 0, /* @nb_inplace_true_divide@ */
195};
196
197static PyTypeObject pfilt_pytype_skel = {
6d4db0bf 198 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 199 "PrimeFilter", /* @tp_name@ */
d7ab1bab 200 sizeof(pfilt_pyobj), /* @tp_basicsize@ */
201 0, /* @tp_itemsize@ */
202
203 pfilt_pydealloc, /* @tp_dealloc@ */
204 0, /* @tp_print@ */
205 0, /* @tp_getattr@ */
206 0, /* @tp_setattr@ */
207 0, /* @tp_compare@ */
208 0, /* @tp_repr@ */
209 &pfilt_pynumber, /* @tp_as_number@ */
210 0, /* @tp_as_sequence@ */
211 0, /* @tp_as_mapping@ */
212 0, /* @tp_hash@ */
213 0, /* @tp_call@ */
214 0, /* @tp_str@ */
215 0, /* @tp_getattro@ */
216 0, /* @tp_setattro@ */
217 0, /* @tp_as_buffer@ */
218 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
219 Py_TPFLAGS_BASETYPE,
220
221 /* @tp_doc@ */
06cd26e8 222"PrimeFilter(X): small-primes filter.",
d7ab1bab 223
224 0, /* @tp_traverse@ */
225 0, /* @tp_clear@ */
226 0, /* @tp_richcompare@ */
227 0, /* @tp_weaklistoffset@ */
228 0, /* @tp_iter@ */
963a6148 229 0, /* @tp_iternext@ */
d7ab1bab 230 pfilt_pymethods, /* @tp_methods@ */
231 0, /* @tp_members@ */
232 pfilt_pygetset, /* @tp_getset@ */
233 0, /* @tp_base@ */
234 0, /* @tp_dict@ */
235 0, /* @tp_descr_get@ */
236 0, /* @tp_descr_set@ */
237 0, /* @tp_dictoffset@ */
238 0, /* @tp_init@ */
239 PyType_GenericAlloc, /* @tp_alloc@ */
240 pfilt_pynew, /* @tp_new@ */
3aa33042 241 0, /* @tp_free@ */
d7ab1bab 242 0 /* @tp_is_gc@ */
243};
244
245/*----- Rabin-Miller testing ----------------------------------------------*/
246
247typedef struct rabin_pyobj {
248 PyObject_HEAD
249 rabin r;
250} rabin_pyobj;
251
252static PyTypeObject *rabin_pytype;
253#define RABIN_R(o) (&((rabin_pyobj *)(o))->r)
254
255static PyObject *rabin_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
256{
257 mp *x = 0;
258 rabin_pyobj *o = 0;
259 char *kwlist[] = { "x", 0 };
260
261 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", kwlist, convmp, &x))
262 goto end;
263 if (!MP_POSP(x) || MP_EVENP(x)) VALERR("must be positive and odd");
264 o = (rabin_pyobj *)ty->tp_alloc(ty, 0);
265 rabin_create(&o->r, x);
266end:
267 return ((PyObject *)o);
268}
269
270static void rabin_pydealloc(PyObject *me)
271{
272 rabin_destroy(RABIN_R(me));
3aa33042 273 FREEOBJ(me);
d7ab1bab 274}
275
276static PyObject *rmeth_test(PyObject *me, PyObject *arg)
277{
278 mp *w = 0;
279 PyObject *rc = 0;
280
281 if (!PyArg_ParseTuple(arg, "O&:test", convmp, &w)) goto end;
282 rc = PyInt_FromLong(rabin_test(RABIN_R(me), w));
283end:
284 mp_drop(w);
285 return (rc);
286}
287
288static PyObject *rmeth_rtest(PyObject *me, PyObject *arg)
289{
290 mp *w = 0;
291 PyObject *rc = 0;
292
293 if (!PyArg_ParseTuple(arg, "O&:rtest", convmp, &w)) goto end;
294 rc = PyInt_FromLong(rabin_rtest(RABIN_R(me), w));
295end:
296 mp_drop(w);
297 return (rc);
298}
299
300static PyObject *rget_niters(PyObject *me, void *hunoz)
301 { return (PyInt_FromLong(rabin_iters(mp_bits(RABIN_R(me)->mm.m)))); }
302
303static PyObject *rget_x(PyObject *me, void *hunoz)
304 { return (mp_pywrap(MP_COPY(RABIN_R(me)->mm.m))); }
305
306static PyObject *meth__RabinMiller_iters(PyObject *me, PyObject *arg)
307{
308 unsigned n;
309
310 if (!PyArg_ParseTuple(arg, "OO&:iters", &me, convuint, &n)) return (0);
311 return (PyInt_FromLong(rabin_iters(n)));
312}
313
314static PyGetSetDef rabin_pygetset[] = {
315#define GETSETNAME(op, name) r##op##_##name
316 GET (x, "R.x -> number under test")
317 GET (niters, "R.niters -> suggested number of tests")
318#undef GETSETNAME
319 { 0 }
320};
321
322static PyMethodDef rabin_pymethods[] = {
323#define METHNAME(name) rmeth_##name
324 METH (test, "R.test(W) -> PGST")
325 METH (rtest, "R.rtest(W) -> PGST")
326#undef METHNAME
327 { 0 }
328};
329
330static PyTypeObject rabin_pytype_skel = {
6d4db0bf 331 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 332 "RabinMiller", /* @tp_name@ */
d7ab1bab 333 sizeof(rabin_pyobj), /* @tp_basicsize@ */
334 0, /* @tp_itemsize@ */
335
336 rabin_pydealloc, /* @tp_dealloc@ */
337 0, /* @tp_print@ */
338 0, /* @tp_getattr@ */
339 0, /* @tp_setattr@ */
340 0, /* @tp_compare@ */
341 0, /* @tp_repr@ */
342 0, /* @tp_as_number@ */
343 0, /* @tp_as_sequence@ */
344 0, /* @tp_as_mapping@ */
345 0, /* @tp_hash@ */
346 0, /* @tp_call@ */
347 0, /* @tp_str@ */
348 0, /* @tp_getattro@ */
349 0, /* @tp_setattro@ */
350 0, /* @tp_as_buffer@ */
351 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
352 Py_TPFLAGS_BASETYPE,
353
354 /* @tp_doc@ */
06cd26e8 355"RabinMiller(X): Rabin-Miller strong primality test.",
d7ab1bab 356
357 0, /* @tp_traverse@ */
358 0, /* @tp_clear@ */
359 0, /* @tp_richcompare@ */
360 0, /* @tp_weaklistoffset@ */
361 0, /* @tp_iter@ */
963a6148 362 0, /* @tp_iternext@ */
d7ab1bab 363 rabin_pymethods, /* @tp_methods@ */
364 0, /* @tp_members@ */
365 rabin_pygetset, /* @tp_getset@ */
366 0, /* @tp_base@ */
367 0, /* @tp_dict@ */
368 0, /* @tp_descr_get@ */
369 0, /* @tp_descr_set@ */
370 0, /* @tp_dictoffset@ */
371 0, /* @tp_init@ */
372 PyType_GenericAlloc, /* @tp_alloc@ */
373 rabin_pynew, /* @tp_new@ */
3aa33042 374 0, /* @tp_free@ */
d7ab1bab 375 0 /* @tp_is_gc@ */
376};
377
378/*----- Events ------------------------------------------------------------*/
379
380typedef struct pgevent_pyobj {
381 PyObject_HEAD
d65d80d7 382 PyObject *r;
d7ab1bab 383 pgen_event *ev;
384} pgevent_pyobj;
385
386static PyTypeObject *pgevent_pytype;
387#define PGEVENT_EV(o) (((pgevent_pyobj *)(o))->ev)
388
389static PyObject *pgevent_pywrap(pgen_event *ev)
390{
391 pgevent_pyobj *o = PyObject_New(pgevent_pyobj, pgevent_pytype);
d65d80d7 392 o->ev = ev; o->r = 0;
d7ab1bab 393 return ((PyObject *)o);
394}
395
396static CONVFUNC(pgevent, pgen_event *, PGEVENT_EV)
397
d65d80d7
MW
398static void pgevent_kill(PyObject *me)
399{
400 pgevent_pyobj *ev = (pgevent_pyobj *)me;
401
402 ev->ev = 0;
403 if (ev->r) GRAND_R(ev->r) = 0;
404}
405
406static void pgevent_pydealloc(PyObject *me)
407{
408 pgevent_pyobj *ev = (pgevent_pyobj *)me;
409 if (ev->r) Py_DECREF(ev->r);
410 FREEOBJ(me);
411}
d7ab1bab 412
413#define PGEVENT_CHECK(me) do { \
414 if (!PGEVENT_EV(me)) { \
d65d80d7 415 PyErr_SetString(PyExc_ValueError, "event object is no longer valid"); \
d7ab1bab 416 return (0); \
417 } \
418} while (0)
419
420static PyObject *peget_name(PyObject *me, void *hunoz)
421 { PGEVENT_CHECK(me); return (PyString_FromString(PGEVENT_EV(me)->name)); }
422
423static PyObject *peget_x(PyObject *me, void *hunoz)
424 { PGEVENT_CHECK(me); return (mp_pywrap(MP_COPY(PGEVENT_EV(me)->m))); }
425
426static PyObject *peget_steps(PyObject *me, void *hunoz)
427 { PGEVENT_CHECK(me); return (PyInt_FromLong(PGEVENT_EV(me)->steps)); }
428
429static PyObject *peget_tests(PyObject *me, void *hunoz)
430 { PGEVENT_CHECK(me); return (PyInt_FromLong(PGEVENT_EV(me)->tests)); }
431
432static PyObject *peget_rng(PyObject *me, void *hunoz)
d65d80d7
MW
433{
434 pgevent_pyobj *ev = (pgevent_pyobj *)me;
435
436 PGEVENT_CHECK(me);
437 if (!ev->r) ev->r = grand_pywrap(ev->ev->r, 0);
438 Py_INCREF(ev->r); return ((PyObject *)ev->r);
439}
d7ab1bab 440
441static int peset_x(PyObject *me, PyObject *xobj, void *hunoz)
442{
443 mp *x = 0;
444 pgen_event *ev = PGEVENT_EV(me);
445 int rc = -1;
446 PGEVENT_CHECK(me);
447 if ((x = getmp(xobj)) == 0) goto end;
448 mp_drop(ev->m);
449 ev->m = MP_COPY(x);
450 rc = 0;
451end:
452 mp_drop(x);
453 return (rc);
454}
455
456static PyGetSetDef pgevent_pygetset[] = {
457#define GETSETNAME(op, name) pe##op##_##name
458 GET (name, "EV.name -> value being generated")
459 GETSET(x, "EV.x -> value under test")
460 GET (steps, "EV.steps -> number of steps left")
461 GET (tests, "EV.tests -> tests before passing")
462 GET (rng, "EV.rng -> (noncrypto) random number generator")
463#undef GETSETNAME
464 { 0 }
465};
466
467static PyTypeObject pgevent_pytype_skel = {
6d4db0bf 468 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 469 "PrimeGenEvent", /* @tp_name@ */
d7ab1bab 470 sizeof(pgevent_pyobj), /* @tp_basicsize@ */
471 0, /* @tp_itemsize@ */
472
473 pgevent_pydealloc, /* @tp_dealloc@ */
474 0, /* @tp_print@ */
475 0, /* @tp_getattr@ */
476 0, /* @tp_setattr@ */
477 0, /* @tp_compare@ */
478 0, /* @tp_repr@ */
479 0, /* @tp_as_number@ */
480 0, /* @tp_as_sequence@ */
481 0, /* @tp_as_mapping@ */
482 0, /* @tp_hash@ */
483 0, /* @tp_call@ */
484 0, /* @tp_str@ */
485 0, /* @tp_getattro@ */
486 0, /* @tp_setattro@ */
487 0, /* @tp_as_buffer@ */
488 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
489 Py_TPFLAGS_BASETYPE,
490
491 /* @tp_doc@ */
492"Prime-generation event.",
493
494 0, /* @tp_traverse@ */
495 0, /* @tp_clear@ */
496 0, /* @tp_richcompare@ */
497 0, /* @tp_weaklistoffset@ */
498 0, /* @tp_iter@ */
963a6148 499 0, /* @tp_iternext@ */
d7ab1bab 500 0, /* @tp_methods@ */
501 0, /* @tp_members@ */
502 pgevent_pygetset, /* @tp_getset@ */
503 0, /* @tp_base@ */
504 0, /* @tp_dict@ */
505 0, /* @tp_descr_get@ */
506 0, /* @tp_descr_set@ */
507 0, /* @tp_dictoffset@ */
508 0, /* @tp_init@ */
509 PyType_GenericAlloc, /* @tp_alloc@ */
510 abstract_pynew, /* @tp_new@ */
3aa33042 511 0, /* @tp_free@ */
d7ab1bab 512 0 /* @tp_is_gc@ */
513};
514
515/*----- Event handlers ----------------------------------------------------*/
516
517PyTypeObject *pgev_pytype;
518
519typedef struct pgstep_pyobj {
520 PGEV_HEAD
521 pgen_filterctx f;
522} pgstep_pyobj;
523
524static PyTypeObject *pgstep_pytype;
525#define PGSTEP_STEP(o) (((pgstep_pyobj *)(o))->f.step)
526
527typedef struct pgjump_pyobj {
528 PGEV_HEAD
529 PyObject *fobj;
530 pgen_jumpctx j;
531} pgjump_pyobj;
532
533static PyTypeObject *pgjump_pytype;
534#define PGJUMP_FOBJ(o) (((pgjump_pyobj *)(o))->fobj)
535#define PGJUMP_J(o) (&((pgjump_pyobj *)(o))->j)
536
537typedef struct pgtest_pyobj {
538 PGEV_HEAD
539 rabin r;
540} pgtest_pyobj;
541
542static PyTypeObject *pgtest_pytype;
543
544static int pgev_python(int rq, pgen_event *ev, void *p)
545{
546 PyObject *py = p;
547 PyObject *pyev = 0;
548 PyObject *rc = 0;
549 int st = PGEN_ABORT;
550 long l;
551 char *meth[] = {
552 "pg_abort", "pg_done", "pg_begin", "pg_try", "pg_fail", "pg_pass"
553 };
554
555 Py_INCREF(py);
556 rq++;
557 if (rq > N(meth)) SYSERR("event code out of range");
558 pyev = pgevent_pywrap(ev);
559 if ((rc = PyObject_CallMethod(py, meth[rq], "(O)", pyev)) == 0)
560 goto end;
561 if (rc == Py_None)
562 st = PGEN_TRY;
563 else if ((l = PyInt_AsLong(rc)) == -1 && PyErr_Occurred())
564 goto end;
565 else if (l < PGEN_ABORT || l > PGEN_PASS)
566 VALERR("return code out of range");
b2687a0a 567 else
d7ab1bab 568 st = l;
569end:
570 if (pyev) {
571 pgevent_kill(pyev);
572 Py_DECREF(pyev);
573 }
574 Py_XDECREF(rc);
575 Py_DECREF(py);
576 return (st);
577}
578
579static PyObject *pgev_pywrap(const pgev *pg)
580{
581 pgev_pyobj *o;
582
583 o = PyObject_New(pgev_pyobj, pgev_pytype);
584 o->pg = *pg;
585 return ((PyObject *)o);
586}
587
588int convpgev(PyObject *o, void *p)
589{
590 pgev *pg = p;
591
592 if (PGEV_PYCHECK(o))
593 *pg = *PGEV_PG(o);
594 else {
595 pg->proc = pgev_python;
596 pg->ctx = o;
597 Py_INCREF(o);
598 }
599 return (1);
600}
601
602void droppgev(pgev *p)
603{
604 if (p->proc == pgev_python) {
605 PyObject *py = p->ctx;
606 Py_DECREF(py);
607 }
608}
609
610static PyObject *pgmeth_common(PyObject *me, PyObject *arg, int rq)
611{
612 pgen_event *ev;
613 pgev *pg = PGEV_PG(me);
614 PyObject *rc = 0;
615
616 if (!PyArg_ParseTuple(arg, "O&", convpgevent, &ev)) goto end;
617 rc = PyInt_FromLong(!pg->proc ? rq : pg->proc(rq, ev, pg->ctx));
618end:
619 return (rc);
620}
621
622#define PGMETH(lc, uc) \
623 static PyObject *pgmeth_pg_##lc(PyObject *me, PyObject *arg) \
624 { return pgmeth_common(me, arg, PGEN_##uc); }
625PGMETH(abort, ABORT)
626PGMETH(done, DONE)
627PGMETH(begin, BEGIN)
628PGMETH(try, TRY)
629PGMETH(pass, PASS)
630PGMETH(fail, FAIL)
631#undef PGMETH
632
633static PyObject *pgev_stdev(pgen_proc *proc)
634 { pgev pg; pg.proc = proc; pg.ctx = 0; return (pgev_pywrap(&pg)); }
635
636static PyMethodDef pgev_pymethods[] = {
637#define METHNAME(name) pgmeth_##name
638 METH (pg_abort, "E.pg_abort() -> PGRC -- prime generation aborted")
639 METH (pg_done, "E.pg_done() -> PGRC -- prime generation finished")
640 METH (pg_begin, "E.pg_begin() -> PGRC -- commence stepping/testing")
641 METH (pg_try, "E.pg_try() -> PGRC -- found new candidate")
642 METH (pg_pass, "E.pg_pass() -> PGRC -- passed primality test")
643 METH (pg_fail, "E.pg_fail() -> PGRC -- failed primality test")
644#undef METHNAME
645 { 0 }
646};
647
648static PyTypeObject pgev_pytype_skel = {
6d4db0bf 649 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 650 "PrimeGenBuiltinHandler", /* @tp_name@ */
d7ab1bab 651 sizeof(pgev_pyobj), /* @tp_basicsize@ */
652 0, /* @tp_itemsize@ */
653
3aa33042 654 0, /* @tp_dealloc@ */
d7ab1bab 655 0, /* @tp_print@ */
656 0, /* @tp_getattr@ */
657 0, /* @tp_setattr@ */
658 0, /* @tp_compare@ */
659 0, /* @tp_repr@ */
660 0, /* @tp_as_number@ */
661 0, /* @tp_as_sequence@ */
662 0, /* @tp_as_mapping@ */
663 0, /* @tp_hash@ */
664 0, /* @tp_call@ */
665 0, /* @tp_str@ */
666 0, /* @tp_getattro@ */
667 0, /* @tp_setattro@ */
668 0, /* @tp_as_buffer@ */
669 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
670 Py_TPFLAGS_BASETYPE,
671
672 /* @tp_doc@ */
673"Built-in prime-generation event handler, base class.",
674
675 0, /* @tp_traverse@ */
676 0, /* @tp_clear@ */
677 0, /* @tp_richcompare@ */
678 0, /* @tp_weaklistoffset@ */
679 0, /* @tp_iter@ */
963a6148 680 0, /* @tp_iternext@ */
d7ab1bab 681 pgev_pymethods, /* @tp_methods@ */
682 0, /* @tp_members@ */
683 0, /* @tp_getset@ */
684 0, /* @tp_base@ */
685 0, /* @tp_dict@ */
686 0, /* @tp_descr_get@ */
687 0, /* @tp_descr_set@ */
688 0, /* @tp_dictoffset@ */
689 0, /* @tp_init@ */
690 PyType_GenericAlloc, /* @tp_alloc@ */
691 abstract_pynew, /* @tp_new@ */
3aa33042 692 0, /* @tp_free@ */
d7ab1bab 693 0 /* @tp_is_gc@ */
694};
695
696static PyObject *pgstep_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
697{
698 mpw s;
699 pgstep_pyobj *rc = 0;
700 char *kwlist[] = { "step", 0 };
701
702 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", kwlist, convmpw, &s))
703 goto end;
704 rc = (pgstep_pyobj *)ty->tp_alloc(ty, 0);
705 rc->f.step = s;
706 rc->pg.proc = pgen_filter;
707 rc->pg.ctx = &rc->f;
708end:
709 return ((PyObject *)rc);
710}
711
712static PyObject *psget_step(PyObject *me, void *hunoz)
713 { return (PyInt_FromLong(PGSTEP_STEP(me))); }
714
715static PyGetSetDef pgstep_pygetset[] = {
716#define GETSETNAME(op, name) ps##op##_##name
b2687a0a 717 GET (step, "S.step -> step size for the stepper")
d7ab1bab 718#undef GETSETNAME
719 { 0 }
720};
721
722static PyTypeObject pgstep_pytype_skel = {
6d4db0bf 723 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 724 "PrimeGenStepper", /* @tp_name@ */
d7ab1bab 725 sizeof(pgstep_pyobj), /* @tp_basicsize@ */
726 0, /* @tp_itemsize@ */
727
728 0, /* @tp_dealloc@ */
729 0, /* @tp_print@ */
730 0, /* @tp_getattr@ */
731 0, /* @tp_setattr@ */
732 0, /* @tp_compare@ */
733 0, /* @tp_repr@ */
734 0, /* @tp_as_number@ */
735 0, /* @tp_as_sequence@ */
736 0, /* @tp_as_mapping@ */
737 0, /* @tp_hash@ */
738 0, /* @tp_call@ */
739 0, /* @tp_str@ */
740 0, /* @tp_getattro@ */
741 0, /* @tp_setattro@ */
742 0, /* @tp_as_buffer@ */
743 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
744 Py_TPFLAGS_BASETYPE,
745
746 /* @tp_doc@ */
06cd26e8 747"PrimeGenStepper(STEP): simple stepper with small-factors filter.",
d7ab1bab 748
749 0, /* @tp_traverse@ */
750 0, /* @tp_clear@ */
751 0, /* @tp_richcompare@ */
752 0, /* @tp_weaklistoffset@ */
753 0, /* @tp_iter@ */
963a6148 754 0, /* @tp_iternext@ */
d7ab1bab 755 0, /* @tp_methods@ */
756 0, /* @tp_members@ */
757 pgstep_pygetset, /* @tp_getset@ */
758 0, /* @tp_base@ */
759 0, /* @tp_dict@ */
760 0, /* @tp_descr_get@ */
761 0, /* @tp_descr_set@ */
762 0, /* @tp_dictoffset@ */
763 0, /* @tp_init@ */
764 PyType_GenericAlloc, /* @tp_alloc@ */
765 pgstep_pynew, /* @tp_new@ */
3aa33042 766 0, /* @tp_free@ */
d7ab1bab 767 0 /* @tp_is_gc@ */
768};
769
770static PyObject *pgjump_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
771{
772 PyObject *o, *fobj;
773 pgjump_pyobj *rc = 0;
774 char *kwlist[] = { "jump", 0 };
775
776 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &o) ||
777 (fobj = pfilt_pymake(pfilt_pytype, o)) == 0)
778 goto end;
779 rc = (pgjump_pyobj *)ty->tp_alloc(ty, 0);
780 rc->fobj = fobj;
781 rc->j.j = PFILT_F(fobj);
782 rc->pg.proc = pgen_jump;
783 rc->pg.ctx = &rc->j;
784end:
785 return ((PyObject *)rc);
786}
787
788static void pgjump_pydealloc(PyObject *me)
789{
790 Py_DECREF(PGJUMP_FOBJ(me));
3aa33042 791 FREEOBJ(me);
d7ab1bab 792}
793
794static PyObject *pjget_jump(PyObject *me, void *hunoz)
795 { RETURN_OBJ(PGJUMP_FOBJ(me)); }
796
797static PyGetSetDef pgjump_pygetset[] = {
798#define GETSETNAME(op, name) pj##op##_##name
b2687a0a 799 GET (jump, "S.jump -> jump size for the stepper")
d7ab1bab 800#undef GETSETNAME
801 { 0 }
802};
803
804static PyTypeObject pgjump_pytype_skel = {
6d4db0bf 805 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 806 "PrimeGenJumper", /* @tp_name@ */
d7ab1bab 807 sizeof(pgjump_pyobj), /* @tp_basicsize@ */
808 0, /* @tp_itemsize@ */
809
810 pgjump_pydealloc, /* @tp_dealloc@ */
811 0, /* @tp_print@ */
812 0, /* @tp_getattr@ */
813 0, /* @tp_setattr@ */
814 0, /* @tp_compare@ */
815 0, /* @tp_repr@ */
816 0, /* @tp_as_number@ */
817 0, /* @tp_as_sequence@ */
818 0, /* @tp_as_mapping@ */
819 0, /* @tp_hash@ */
820 0, /* @tp_call@ */
821 0, /* @tp_str@ */
822 0, /* @tp_getattro@ */
823 0, /* @tp_setattro@ */
824 0, /* @tp_as_buffer@ */
825 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
826 Py_TPFLAGS_BASETYPE,
827
828 /* @tp_doc@ */
06cd26e8 829"PrimeGenJumper(JUMP): stepper for larger steps with small-factors filter.",
d7ab1bab 830
831 0, /* @tp_traverse@ */
832 0, /* @tp_clear@ */
833 0, /* @tp_richcompare@ */
834 0, /* @tp_weaklistoffset@ */
835 0, /* @tp_iter@ */
963a6148 836 0, /* @tp_iternext@ */
d7ab1bab 837 0, /* @tp_methods@ */
838 0, /* @tp_members@ */
839 pgjump_pygetset, /* @tp_getset@ */
840 0, /* @tp_base@ */
841 0, /* @tp_dict@ */
842 0, /* @tp_descr_get@ */
843 0, /* @tp_descr_set@ */
844 0, /* @tp_dictoffset@ */
845 0, /* @tp_init@ */
846 PyType_GenericAlloc, /* @tp_alloc@ */
847 pgjump_pynew, /* @tp_new@ */
3aa33042 848 0, /* @tp_free@ */
d7ab1bab 849 0 /* @tp_is_gc@ */
850};
851
852static PyObject *pgtest_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
853{
854 pgtest_pyobj *rc = 0;
855 char *kwlist[] = { 0 };
856
857 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) goto end;
858 rc = (pgtest_pyobj *)ty->tp_alloc(ty, 0);
859 rc->pg.proc = pgen_test;
860 rc->pg.ctx = &rc->r;
861end:
862 return ((PyObject *)rc);
863}
864
865static PyTypeObject pgtest_pytype_skel = {
6d4db0bf 866 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 867 "PrimeGenTester", /* @tp_name@ */
d7ab1bab 868 sizeof(pgtest_pyobj), /* @tp_basicsize@ */
869 0, /* @tp_itemsize@ */
870
871 0, /* @tp_dealloc@ */
872 0, /* @tp_print@ */
873 0, /* @tp_getattr@ */
874 0, /* @tp_setattr@ */
875 0, /* @tp_compare@ */
876 0, /* @tp_repr@ */
877 0, /* @tp_as_number@ */
878 0, /* @tp_as_sequence@ */
879 0, /* @tp_as_mapping@ */
880 0, /* @tp_hash@ */
881 0, /* @tp_call@ */
882 0, /* @tp_str@ */
883 0, /* @tp_getattro@ */
884 0, /* @tp_setattro@ */
885 0, /* @tp_as_buffer@ */
886 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
887 Py_TPFLAGS_BASETYPE,
888
889 /* @tp_doc@ */
06cd26e8 890"PrimeGenTester(): Rabin-Miller tester.",
d7ab1bab 891
892 0, /* @tp_traverse@ */
893 0, /* @tp_clear@ */
894 0, /* @tp_richcompare@ */
895 0, /* @tp_weaklistoffset@ */
896 0, /* @tp_iter@ */
963a6148 897 0, /* @tp_iternext@ */
d7ab1bab 898 0, /* @tp_methods@ */
899 0, /* @tp_members@ */
900 0, /* @tp_getset@ */
901 0, /* @tp_base@ */
902 0, /* @tp_dict@ */
903 0, /* @tp_descr_get@ */
904 0, /* @tp_descr_set@ */
905 0, /* @tp_dictoffset@ */
906 0, /* @tp_init@ */
907 PyType_GenericAlloc, /* @tp_alloc@ */
908 pgtest_pynew, /* @tp_new@ */
3aa33042 909 0, /* @tp_free@ */
d7ab1bab 910 0 /* @tp_is_gc@ */
911};
912
913/*----- Prime generation functions ----------------------------------------*/
914
915void pgenerr(void)
916{
917 if (!PyErr_Occurred())
918 PyErr_SetString(PyExc_ValueError, "prime generation failed");
919}
920
921static PyObject *meth_pgen(PyObject *me, PyObject *arg, PyObject *kw)
922{
923 mp *x = 0;
924 mp *r = 0;
925 PyObject *rc = 0;
926 char *p = "p";
927 pgen_filterctx fc = { 2 };
928 rabin tc;
929 pgev step = { 0 }, test = { 0 }, evt = { 0 };
930 unsigned nsteps = 0, ntests = 0;
931 char *kwlist[] = { "start", "name", "stepper", "tester", "event",
932 "nsteps", "ntests", 0 };
933
934 step.proc = pgen_filter; step.ctx = &fc;
935 test.proc = pgen_test; test.ctx = &tc;
936 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&O&O&:pgen", kwlist,
937 convmp, &x, &p, convpgev, &step,
938 convpgev, &test, convpgev, &evt,
939 convuint, &nsteps, convuint, &ntests))
940 goto end;
941 if (!ntests) ntests = rabin_iters(mp_bits(x));
942 if ((r = pgen(p, MP_NEW, x, evt.proc, evt.ctx,
943 nsteps, step.proc, step.ctx,
944 ntests, test.proc, test.ctx)) == 0)
945 PGENERR;
946 if (PyErr_Occurred()) goto end;
947 rc = mp_pywrap(r);
948 r = 0;
949end:
950 mp_drop(r); mp_drop(x);
951 droppgev(&step); droppgev(&test); droppgev(&evt);
b2687a0a 952 return (rc);
d7ab1bab 953}
954
955static PyObject *meth_strongprime_setup(PyObject *me,
956 PyObject *arg, PyObject *kw)
957{
958 mp *x = 0;
959 pfilt f;
960 grand *r = &rand_global;
961 unsigned nbits;
962 char *name = "p";
963 unsigned n = 0;
964 pgev evt = { 0 };
965 PyObject *rc = 0;
966 char *kwlist[] = { "nbits", "name", "event", "rng", "nsteps", 0 };
967
968 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&", kwlist,
969 convuint, &nbits, &name,
970 convpgev, &evt, convgrand, &r,
971 convuint, &n))
972 goto end;
973 if ((x = strongprime_setup(name, MP_NEW, &f, nbits,
974 r, n, evt.proc, evt.ctx)) == 0)
975 PGENERR;
976 rc = Py_BuildValue("(NN)", mp_pywrap(x), pfilt_pywrap(&f));
977 x = 0;
978end:
979 mp_drop(x);
980 droppgev(&evt);
981 return (rc);
982}
983
984static PyObject *meth_strongprime(PyObject *me, PyObject *arg, PyObject *kw)
985{
986 mp *x = 0;
987 grand *r = &rand_global;
988 unsigned nbits;
989 char *name = "p";
990 unsigned n = 0;
991 pgev evt = { 0 };
992 PyObject *rc = 0;
993 char *kwlist[] = { "nbits", "name", "event", "rng", "nsteps", 0 };
994
995 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|sO&O&O&", kwlist,
996 convuint, &nbits, &name,
997 convpgev, &evt, convgrand, &r,
998 convuint, &n))
999 goto end;
1000 if ((x = strongprime(name, MP_NEW, nbits,
1001 r, n, evt.proc, evt.ctx)) == 0)
1002 PGENERR;
1003 rc = mp_pywrap(x);
1004 x = 0;
1005end:
1006 mp_drop(x);
1007 droppgev(&evt);
1008 return (rc);
1009}
1010
1011static PyObject *meth_limlee(PyObject *me, PyObject *arg, PyObject *kw)
1012{
1013 char *p = "p";
1014 pgev ie = { 0 }, oe = { 0 };
1015 unsigned ql, pl;
1016 grand *r = &rand_global;
1017 unsigned on = 0;
1018 size_t i, nf = 0;
1019 PyObject *rc = 0, *vec;
1020 char *kwlist[] = { "pbits", "qbits", "name", "event", "ievent",
1021 "rng", "nsteps", 0 };
1022 mp *x = 0, **v = 0;
1023
1024 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|sO&O&O&O&:limlee", kwlist,
1025 convuint, &pl, convuint, &ql,
1026 &p, convpgev, &oe, convpgev, &ie,
1027 convgrand, &r, convuint, &on))
1028 goto end;
1029 if ((x = limlee(p, MP_NEW, MP_NEW, ql, pl, r, on,
1030 oe.proc, oe.ctx, ie.proc, ie.ctx, &nf, &v)) == 0)
1031 PGENERR;
1032 vec = PyList_New(nf);
1033 for (i = 0; i < nf; i++)
1034 PyList_SetItem(vec, i, mp_pywrap(v[i]));
1035 xfree(v);
1036 rc = Py_BuildValue("(NN)", mp_pywrap(x), vec);
1037end:
1038 droppgev(&oe); droppgev(&ie);
1039 return (rc);
1040}
1041
1042/*----- Global stuff ------------------------------------------------------*/
1043
1044static PyMethodDef methods[] = {
1045#define METHNAME(name) meth_##name
1046 METH (_PrimeFilter_smallfactor, "smallfactor(X) -> PGRC")
1047 METH (_RabinMiller_iters, "iters(NBITS) -> NITERS")
1048 KWMETH(pgen, "\
1049pgen(START, [name = 'p', stepper = PrimeGenStepper(2),\n\
1050 tester = PrimeGenTester(), event = pgen_nullev,\n\
1051 nsteps = 0, ntests = RabinMiller.iters(START.nbits)]) -> P")
1052 KWMETH(strongprime_setup, "\
1053strongprime_setup(NBITS, [name = 'p', event = pgen_nullev,\n\
b2687a0a 1054 rng = rand, nsteps = 0]) -> (START, JUMP)")
d7ab1bab 1055 KWMETH(strongprime, "\
986bcc44
MW
1056strongprime(NBITS, [name = 'p', event = pgen_nullev,\n\
1057 rng = rand, nsteps = 0]) -> P")
d7ab1bab 1058 KWMETH(limlee, "\
1059limlee(PBITS, QBITS, [name = 'p', event = pgen_nullev,\n\
1060 ievent = pgen_nullev, rng = rand, nsteps = 0]) -> (P, [Q, ...])")
1061#undef METHNAME
1062 { 0 }
1063};
1064
1065void pgen_pyinit(void)
1066{
1067 INITTYPE(pfilt, root);
1068 INITTYPE(rabin, root);
1069 INITTYPE(pgevent, root);
1070 INITTYPE(pgev, root);
1071 INITTYPE(pgstep, pgev);
1072 INITTYPE(pgjump, pgev);
1073 INITTYPE(pgtest, pgev);
1074 addmethods(methods);
1075}
1076
1077static PyObject *obj;
1078
1079void pgen_pyinsert(PyObject *mod)
1080{
1081 INSERT("PrimeFilter", pfilt_pytype);
1082 INSERT("RabinMiller", rabin_pytype);
1083 INSERT("PrimeGenEvent", pgevent_pytype);
1084 INSERT("PrimeGenBuiltinHandler", pgev_pytype);
1085 INSERT("PrimeGenStepper", pgstep_pytype);
1086 INSERT("PrimeGenJumper", pgjump_pytype);
1087 INSERT("PrimeGenTester", pgtest_pytype);
1088 INSERT("pgen_nullev", obj = pgev_stdev(0));
1089 INSERT("pgen_stdev", pgev_stdev(pgen_ev));
1090 INSERT("pgen_spinev", pgev_stdev(pgen_evspin));
1091 INSERT("pgen_subev", pgev_stdev(pgen_subev));
1092}
1093
1094/*----- That's all, folks -------------------------------------------------*/