3 * Prime number generation
5 * (c) 2005 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- Filters -----------------------------------------------------------*/
33 PyTypeObject
*pfilt_pytype
;
35 static PyObject
*pfilt_pywrap(pfilt
*f
)
38 o
= PyObject_New(pfilt_pyobj
, pfilt_pytype
);
40 o
->st
= pfilt_step(f
, 0);
41 return ((PyObject
*)o
);
44 static PyObject
*pfilt_pymake(PyTypeObject
*ty
, PyObject
*xobj
)
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
);
55 return ((PyObject
*)o
);
58 static PyObject
*pfilt_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
60 static const char *const kwlist
[] = { "x", 0 };
63 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", KWLIST
, &xobj
))
65 return (pfilt_pymake(ty
, xobj
));
68 static void pfilt_pydealloc(PyObject
*me
)
69 { pfilt_destroy(PFILT_F(me
)); FREEOBJ(me
); }
71 static PyObject
*pfmeth_step(PyObject
*me
, PyObject
*arg
)
75 if (!PyArg_ParseTuple(arg
, "O&:step", convmpw
, &x
)) return (0);
76 PFILT_ST(me
) = pfilt_step(PFILT_F(me
), x
);
80 static PyObject
*pfmeth_muladd(PyObject
*me
, PyObject
*arg
)
85 if (!PyArg_ParseTuple(arg
, "O&O&:muladd", convmpw
, &m
, convmpw
, &a
))
87 o
= PyObject_New(pfilt_pyobj
, pfilt_pytype
);
88 o
->st
= pfilt_muladd(&o
->f
, PFILT_F(me
), m
, a
);
89 return ((PyObject
*)o
);
92 static CONVFUNC(pfilt
, pfilt
*, PFILT_F
)
94 static PyObject
*pfmeth_jump(PyObject
*me
, PyObject
*arg
)
98 if (!PyArg_ParseTuple(arg
, "O&:jump", convpfilt
, &f
)) return (0);
99 PFILT_ST(me
) = pfilt_jump(PFILT_F(me
), f
);
103 static PyObject
*meth__PrimeFilter_smallfactor(PyObject
*me
, PyObject
*arg
)
108 if (!PyArg_ParseTuple(arg
, "OO&:smallfactor", &me
, convmp
, &x
)) goto end
;
109 rc
= PyInt_FromLong(pfilt_smallfactor(x
));
115 static int pfilt_pynonzerop(PyObject
*me
)
116 { return (PFILT_ST(me
) != PGEN_FAIL
); }
118 static PyObject
*pfilt_pyint(PyObject
*me
)
123 if (!mp_tolong_checked(PFILT_F(me
)->m
, &l
, 0)) rc
= PyInt_FromLong(l
);
124 else rc
= mp_topylong(PFILT_F(me
)->m
);
128 static PyObject
*pfilt_pylong(PyObject
*me
)
129 { return (mp_topylong(PFILT_F(me
)->m
)); }
131 static PyObject
*pfget_x(PyObject
*me
, void *hunoz
)
132 { return (mp_pywrap(MP_COPY(PFILT_F(me
)->m
))); }
134 static PyObject
*pfget_status(PyObject
*me
, void *hunoz
)
135 { return (PyInt_FromLong(PFILT_ST(me
))); }
137 static 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")
145 static 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)")
154 static PyNumberMethods pfilt_pynumber
= {
156 0, /* @nb_subtract@ */
157 0, /* @nb_multiply@ */
159 0, /* @nb_remainder@ */
162 0, /* @nb_negative@ */
163 0, /* @nb_positive@ */
164 0, /* @nb_absolute@ */
165 pfilt_pynonzerop
, /* @nb_nonzero@ */
173 pfilt_pyint
, /* @nb_int@ */
174 pfilt_pylong
, /* @nb_long@ */
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@ */
191 0, /* @nb_floor_divide@ */
192 0, /* @nb_true_divide@ */
193 0, /* @nb_inplace_floor_divide@ */
194 0, /* @nb_inplace_true_divide@ */
197 static PyTypeObject pfilt_pytype_skel
= {
198 PyObject_HEAD_INIT(0) 0, /* Header */
199 "PrimeFilter", /* @tp_name@ */
200 sizeof(pfilt_pyobj
), /* @tp_basicsize@ */
201 0, /* @tp_itemsize@ */
203 pfilt_pydealloc
, /* @tp_dealloc@ */
205 0, /* @tp_getattr@ */
206 0, /* @tp_setattr@ */
207 0, /* @tp_compare@ */
209 &pfilt_pynumber
, /* @tp_as_number@ */
210 0, /* @tp_as_sequence@ */
211 0, /* @tp_as_mapping@ */
215 0, /* @tp_getattro@ */
216 0, /* @tp_setattro@ */
217 0, /* @tp_as_buffer@ */
218 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
222 "PrimeFilter(X): small-primes filter.",
224 0, /* @tp_traverse@ */
226 0, /* @tp_richcompare@ */
227 0, /* @tp_weaklistoffset@ */
229 0, /* @tp_iternext@ */
230 pfilt_pymethods
, /* @tp_methods@ */
231 0, /* @tp_members@ */
232 pfilt_pygetset
, /* @tp_getset@ */
235 0, /* @tp_descr_get@ */
236 0, /* @tp_descr_set@ */
237 0, /* @tp_dictoffset@ */
239 PyType_GenericAlloc
, /* @tp_alloc@ */
240 pfilt_pynew
, /* @tp_new@ */
245 /*----- Rabin-Miller testing ----------------------------------------------*/
247 typedef struct rabin_pyobj
{
252 static PyTypeObject
*rabin_pytype
;
253 #define RABIN_R(o) (&((rabin_pyobj *)(o))->r)
255 static PyObject
*rabin_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
259 static const char *const kwlist
[] = { "x", 0 };
261 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convmp
, &x
))
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
);
267 return ((PyObject
*)o
);
270 static void rabin_pydealloc(PyObject
*me
)
272 rabin_destroy(RABIN_R(me
));
276 static PyObject
*rmeth_test(PyObject
*me
, PyObject
*arg
)
281 if (!PyArg_ParseTuple(arg
, "O&:test", convmp
, &w
)) goto end
;
282 rc
= PyInt_FromLong(rabin_test(RABIN_R(me
), w
));
288 static PyObject
*rmeth_rtest(PyObject
*me
, PyObject
*arg
)
293 if (!PyArg_ParseTuple(arg
, "O&:rtest", convmp
, &w
)) goto end
;
294 rc
= PyInt_FromLong(rabin_rtest(RABIN_R(me
), w
));
300 static PyObject
*rget_niters(PyObject
*me
, void *hunoz
)
301 { return (PyInt_FromLong(rabin_iters(mp_bits(RABIN_R(me
)->mm
.m
)))); }
303 static PyObject
*rget_x(PyObject
*me
, void *hunoz
)
304 { return (mp_pywrap(MP_COPY(RABIN_R(me
)->mm
.m
))); }
306 static PyObject
*meth__RabinMiller_iters(PyObject
*me
, PyObject
*arg
)
310 if (!PyArg_ParseTuple(arg
, "OO&:iters", &me
, convuint
, &n
)) return (0);
311 return (PyInt_FromLong(rabin_iters(n
)));
314 static 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")
322 static PyMethodDef rabin_pymethods
[] = {
323 #define METHNAME(name) rmeth_##name
324 METH (test
, "R.test(W) -> PGST")
325 METH (rtest
, "R.rtest(W) -> PGST")
330 static PyTypeObject rabin_pytype_skel
= {
331 PyObject_HEAD_INIT(0) 0, /* Header */
332 "RabinMiller", /* @tp_name@ */
333 sizeof(rabin_pyobj
), /* @tp_basicsize@ */
334 0, /* @tp_itemsize@ */
336 rabin_pydealloc
, /* @tp_dealloc@ */
338 0, /* @tp_getattr@ */
339 0, /* @tp_setattr@ */
340 0, /* @tp_compare@ */
342 0, /* @tp_as_number@ */
343 0, /* @tp_as_sequence@ */
344 0, /* @tp_as_mapping@ */
348 0, /* @tp_getattro@ */
349 0, /* @tp_setattro@ */
350 0, /* @tp_as_buffer@ */
351 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
355 "RabinMiller(X): Rabin-Miller strong primality test.",
357 0, /* @tp_traverse@ */
359 0, /* @tp_richcompare@ */
360 0, /* @tp_weaklistoffset@ */
362 0, /* @tp_iternext@ */
363 rabin_pymethods
, /* @tp_methods@ */
364 0, /* @tp_members@ */
365 rabin_pygetset
, /* @tp_getset@ */
368 0, /* @tp_descr_get@ */
369 0, /* @tp_descr_set@ */
370 0, /* @tp_dictoffset@ */
372 PyType_GenericAlloc
, /* @tp_alloc@ */
373 rabin_pynew
, /* @tp_new@ */
378 /*----- Events ------------------------------------------------------------*/
380 typedef struct pgevent_pyobj
{
386 static PyTypeObject
*pgevent_pytype
;
387 #define PGEVENT_EV(o) (((pgevent_pyobj *)(o))->ev)
389 static PyObject
*pgevent_pywrap(pgen_event
*ev
)
391 pgevent_pyobj
*o
= PyObject_New(pgevent_pyobj
, pgevent_pytype
);
392 o
->ev
= ev
; o
->r
= 0;
393 return ((PyObject
*)o
);
396 static CONVFUNC(pgevent
, pgen_event
*, PGEVENT_EV
)
398 static void pgevent_kill(PyObject
*me
)
400 pgevent_pyobj
*ev
= (pgevent_pyobj
*)me
;
403 if (ev
->r
) GRAND_R(ev
->r
) = 0;
406 static void pgevent_pydealloc(PyObject
*me
)
408 pgevent_pyobj
*ev
= (pgevent_pyobj
*)me
;
409 Py_XDECREF(ev
->r
); FREEOBJ(me
);
412 #define PGEVENT_CHECK(me) do { \
413 if (!PGEVENT_EV(me)) { \
414 PyErr_SetString(PyExc_ValueError, "event object is no longer valid"); \
419 static PyObject
*peget_name(PyObject
*me
, void *hunoz
)
420 { PGEVENT_CHECK(me
); return (PyString_FromString(PGEVENT_EV(me
)->name
)); }
422 static PyObject
*peget_x(PyObject
*me
, void *hunoz
)
423 { PGEVENT_CHECK(me
); return (mp_pywrap(MP_COPY(PGEVENT_EV(me
)->m
))); }
425 static PyObject
*peget_steps(PyObject
*me
, void *hunoz
)
426 { PGEVENT_CHECK(me
); return (PyInt_FromLong(PGEVENT_EV(me
)->steps
)); }
428 static PyObject
*peget_tests(PyObject
*me
, void *hunoz
)
429 { PGEVENT_CHECK(me
); return (PyInt_FromLong(PGEVENT_EV(me
)->tests
)); }
431 static PyObject
*peget_rng(PyObject
*me
, void *hunoz
)
433 pgevent_pyobj
*ev
= (pgevent_pyobj
*)me
;
436 if (!ev
->r
) ev
->r
= grand_pywrap(ev
->ev
->r
, 0);
437 Py_INCREF(ev
->r
); return ((PyObject
*)ev
->r
);
440 static int peset_x(PyObject
*me
, PyObject
*xobj
, void *hunoz
)
443 pgen_event
*ev
= PGEVENT_EV(me
);
445 if (!xobj
) NIERR("__del__");
447 if ((x
= getmp(xobj
)) == 0) goto end
;
456 static 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")
467 static PyTypeObject pgevent_pytype_skel
= {
468 PyObject_HEAD_INIT(0) 0, /* Header */
469 "PrimeGenEvent", /* @tp_name@ */
470 sizeof(pgevent_pyobj
), /* @tp_basicsize@ */
471 0, /* @tp_itemsize@ */
473 pgevent_pydealloc
, /* @tp_dealloc@ */
475 0, /* @tp_getattr@ */
476 0, /* @tp_setattr@ */
477 0, /* @tp_compare@ */
479 0, /* @tp_as_number@ */
480 0, /* @tp_as_sequence@ */
481 0, /* @tp_as_mapping@ */
485 0, /* @tp_getattro@ */
486 0, /* @tp_setattro@ */
487 0, /* @tp_as_buffer@ */
488 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
492 "Prime-generation event.",
494 0, /* @tp_traverse@ */
496 0, /* @tp_richcompare@ */
497 0, /* @tp_weaklistoffset@ */
499 0, /* @tp_iternext@ */
500 0, /* @tp_methods@ */
501 0, /* @tp_members@ */
502 pgevent_pygetset
, /* @tp_getset@ */
505 0, /* @tp_descr_get@ */
506 0, /* @tp_descr_set@ */
507 0, /* @tp_dictoffset@ */
509 PyType_GenericAlloc
, /* @tp_alloc@ */
510 abstract_pynew
, /* @tp_new@ */
515 /*----- Event handlers ----------------------------------------------------*/
517 PyTypeObject
*pgev_pytype
;
519 typedef struct pgstep_pyobj
{
524 static PyTypeObject
*pgstep_pytype
;
525 #define PGSTEP_STEP(o) (((pgstep_pyobj *)(o))->f.step)
527 typedef struct pgjump_pyobj
{
533 static PyTypeObject
*pgjump_pytype
;
534 #define PGJUMP_FOBJ(o) (((pgjump_pyobj *)(o))->fobj)
535 #define PGJUMP_J(o) (&((pgjump_pyobj *)(o))->j)
537 typedef struct pgtest_pyobj
{
542 static PyTypeObject
*pgtest_pytype
;
544 static int pgev_python(int rq
, pgen_event
*ev
, void *p
)
551 static const char *const meth
[] =
552 { "pg_abort", "pg_done", "pg_begin", "pg_try", "pg_fail", "pg_pass" };
555 if (rq
> N(meth
)) SYSERR("event code out of range");
556 pyev
= pgevent_pywrap(ev
);
557 if ((rc
= PyObject_CallMethod(pg
->obj
, (/*unconst*/ char *)meth
[rq
],
562 else if ((l
= PyInt_AsLong(rc
)) == -1 && PyErr_Occurred())
564 else if (l
< PGEN_ABORT
|| l
> PGEN_PASS
)
565 VALERR("return code out of range");
569 if (PyErr_Occurred())
570 stash_exception(pg
->exc
, "exception from `pgen' handler");
579 static PyObject
*pgev_pywrap(const pgev
*pg
)
583 o
= PyObject_New(pgev_pyobj
, pgev_pytype
);
585 return ((PyObject
*)o
);
588 int convpgev(PyObject
*o
, void *p
)
593 pg
->ev
= *PGEV_PG(o
);
595 pg
->ev
.proc
= pgev_python
;
597 pg
->obj
= o
; Py_INCREF(o
);
602 void droppgev(pypgev
*pg
)
604 if (pg
->ev
.proc
== pgev_python
)
605 { assert(pg
->ev
.ctx
== pg
); Py_DECREF(pg
->obj
); }
608 static PyObject
*pgmeth_common(PyObject
*me
, PyObject
*arg
, int rq
)
611 pgev
*pg
= PGEV_PG(me
);
614 if (!PyArg_ParseTuple(arg
, "O&", convpgevent
, &ev
)) goto end
;
615 rc
= PyInt_FromLong(!pg
->proc ? rq
: pg
->proc(rq
, ev
, pg
->ctx
));
620 #define PGMETH(lc, uc) \
621 static PyObject *pgmeth_pg_##lc(PyObject *me, PyObject *arg) \
622 { return pgmeth_common(me, arg, PGEN_##uc); }
631 static PyObject
*pgev_stdev(pgen_proc
*proc
)
632 { pgev pg
; pg
.proc
= proc
; pg
.ctx
= 0; return (pgev_pywrap(&pg
)); }
634 static PyMethodDef pgev_pymethods
[] = {
635 #define METHNAME(name) pgmeth_##name
636 METH (pg_abort
, "E.pg_abort(EV) -> PGST -- prime generation aborted")
637 METH (pg_done
, "E.pg_done(EV) -> PGST -- prime generation finished")
638 METH (pg_begin
, "E.pg_begin(EV) -> PGST -- commence stepping/testing")
639 METH (pg_try
, "E.pg_try(EV) -> PGST -- found new candidate")
640 METH (pg_pass
, "E.pg_pass(EV) -> PGST -- passed primality test")
641 METH (pg_fail
, "E.pg_fail(EV) -> PGST -- failed primality test")
646 static PyTypeObject pgev_pytype_skel
= {
647 PyObject_HEAD_INIT(0) 0, /* Header */
648 "PrimeGenBuiltinHandler", /* @tp_name@ */
649 sizeof(pgev_pyobj
), /* @tp_basicsize@ */
650 0, /* @tp_itemsize@ */
652 0, /* @tp_dealloc@ */
654 0, /* @tp_getattr@ */
655 0, /* @tp_setattr@ */
656 0, /* @tp_compare@ */
658 0, /* @tp_as_number@ */
659 0, /* @tp_as_sequence@ */
660 0, /* @tp_as_mapping@ */
664 0, /* @tp_getattro@ */
665 0, /* @tp_setattro@ */
666 0, /* @tp_as_buffer@ */
667 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
671 "Built-in prime-generation event handler, base class.",
673 0, /* @tp_traverse@ */
675 0, /* @tp_richcompare@ */
676 0, /* @tp_weaklistoffset@ */
678 0, /* @tp_iternext@ */
679 pgev_pymethods
, /* @tp_methods@ */
680 0, /* @tp_members@ */
684 0, /* @tp_descr_get@ */
685 0, /* @tp_descr_set@ */
686 0, /* @tp_dictoffset@ */
688 PyType_GenericAlloc
, /* @tp_alloc@ */
689 abstract_pynew
, /* @tp_new@ */
694 static PyObject
*pgstep_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
697 pgstep_pyobj
*rc
= 0;
698 static const char *const kwlist
[] = { "step", 0 };
700 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convmpw
, &s
))
702 rc
= (pgstep_pyobj
*)ty
->tp_alloc(ty
, 0);
704 rc
->pg
.proc
= pgen_filter
;
707 return ((PyObject
*)rc
);
710 static PyObject
*psget_step(PyObject
*me
, void *hunoz
)
711 { return (PyInt_FromLong(PGSTEP_STEP(me
))); }
713 static PyGetSetDef pgstep_pygetset
[] = {
714 #define GETSETNAME(op, name) ps##op##_##name
715 GET (step
, "S.step -> step size for the stepper")
720 static PyTypeObject pgstep_pytype_skel
= {
721 PyObject_HEAD_INIT(0) 0, /* Header */
722 "PrimeGenStepper", /* @tp_name@ */
723 sizeof(pgstep_pyobj
), /* @tp_basicsize@ */
724 0, /* @tp_itemsize@ */
726 0, /* @tp_dealloc@ */
728 0, /* @tp_getattr@ */
729 0, /* @tp_setattr@ */
730 0, /* @tp_compare@ */
732 0, /* @tp_as_number@ */
733 0, /* @tp_as_sequence@ */
734 0, /* @tp_as_mapping@ */
738 0, /* @tp_getattro@ */
739 0, /* @tp_setattro@ */
740 0, /* @tp_as_buffer@ */
741 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
745 "PrimeGenStepper(STEP): simple stepper with small-factors filter.",
747 0, /* @tp_traverse@ */
749 0, /* @tp_richcompare@ */
750 0, /* @tp_weaklistoffset@ */
752 0, /* @tp_iternext@ */
753 0, /* @tp_methods@ */
754 0, /* @tp_members@ */
755 pgstep_pygetset
, /* @tp_getset@ */
758 0, /* @tp_descr_get@ */
759 0, /* @tp_descr_set@ */
760 0, /* @tp_dictoffset@ */
762 PyType_GenericAlloc
, /* @tp_alloc@ */
763 pgstep_pynew
, /* @tp_new@ */
768 static PyObject
*pgjump_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
771 pgjump_pyobj
*rc
= 0;
772 static const char *const kwlist
[] = { "jump", 0 };
774 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", KWLIST
, &o
) ||
775 (fobj
= pfilt_pymake(pfilt_pytype
, o
)) == 0)
777 rc
= (pgjump_pyobj
*)ty
->tp_alloc(ty
, 0);
779 rc
->j
.j
= PFILT_F(fobj
);
780 rc
->pg
.proc
= pgen_jump
;
783 return ((PyObject
*)rc
);
786 static void pgjump_pydealloc(PyObject
*me
)
788 Py_DECREF(PGJUMP_FOBJ(me
));
792 static PyObject
*pjget_jump(PyObject
*me
, void *hunoz
)
793 { RETURN_OBJ(PGJUMP_FOBJ(me
)); }
795 static PyGetSetDef pgjump_pygetset
[] = {
796 #define GETSETNAME(op, name) pj##op##_##name
797 GET (jump
, "S.jump -> jump size for the stepper")
802 static PyTypeObject pgjump_pytype_skel
= {
803 PyObject_HEAD_INIT(0) 0, /* Header */
804 "PrimeGenJumper", /* @tp_name@ */
805 sizeof(pgjump_pyobj
), /* @tp_basicsize@ */
806 0, /* @tp_itemsize@ */
808 pgjump_pydealloc
, /* @tp_dealloc@ */
810 0, /* @tp_getattr@ */
811 0, /* @tp_setattr@ */
812 0, /* @tp_compare@ */
814 0, /* @tp_as_number@ */
815 0, /* @tp_as_sequence@ */
816 0, /* @tp_as_mapping@ */
820 0, /* @tp_getattro@ */
821 0, /* @tp_setattro@ */
822 0, /* @tp_as_buffer@ */
823 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
827 "PrimeGenJumper(JUMP): "
828 "stepper for larger steps with small-factors filter.",
830 0, /* @tp_traverse@ */
832 0, /* @tp_richcompare@ */
833 0, /* @tp_weaklistoffset@ */
835 0, /* @tp_iternext@ */
836 0, /* @tp_methods@ */
837 0, /* @tp_members@ */
838 pgjump_pygetset
, /* @tp_getset@ */
841 0, /* @tp_descr_get@ */
842 0, /* @tp_descr_set@ */
843 0, /* @tp_dictoffset@ */
845 PyType_GenericAlloc
, /* @tp_alloc@ */
846 pgjump_pynew
, /* @tp_new@ */
851 static PyObject
*pgtest_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
853 pgtest_pyobj
*rc
= 0;
854 static const char *const kwlist
[] = { 0 };
856 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", KWLIST
)) goto end
;
857 rc
= (pgtest_pyobj
*)ty
->tp_alloc(ty
, 0);
858 rc
->pg
.proc
= pgen_test
;
861 return ((PyObject
*)rc
);
864 static PyTypeObject pgtest_pytype_skel
= {
865 PyObject_HEAD_INIT(0) 0, /* Header */
866 "PrimeGenTester", /* @tp_name@ */
867 sizeof(pgtest_pyobj
), /* @tp_basicsize@ */
868 0, /* @tp_itemsize@ */
870 0, /* @tp_dealloc@ */
872 0, /* @tp_getattr@ */
873 0, /* @tp_setattr@ */
874 0, /* @tp_compare@ */
876 0, /* @tp_as_number@ */
877 0, /* @tp_as_sequence@ */
878 0, /* @tp_as_mapping@ */
882 0, /* @tp_getattro@ */
883 0, /* @tp_setattro@ */
884 0, /* @tp_as_buffer@ */
885 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
889 "PrimeGenTester(): Rabin-Miller tester.",
891 0, /* @tp_traverse@ */
893 0, /* @tp_richcompare@ */
894 0, /* @tp_weaklistoffset@ */
896 0, /* @tp_iternext@ */
897 0, /* @tp_methods@ */
898 0, /* @tp_members@ */
902 0, /* @tp_descr_get@ */
903 0, /* @tp_descr_set@ */
904 0, /* @tp_dictoffset@ */
906 PyType_GenericAlloc
, /* @tp_alloc@ */
907 pgtest_pynew
, /* @tp_new@ */
912 /*----- Prime generation functions ----------------------------------------*/
914 void pgenerr(struct excinfo
*exc
)
916 if (exc
->ty
) RESTORE_EXCINFO(exc
);
917 else PyErr_SetString(PyExc_ValueError
, "prime generation failed");
920 static PyObject
*meth_pgen(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
926 pgen_filterctx fc
= { 2 };
928 struct excinfo exc
= EXCINFO_INIT
;
929 pypgev step
= { { 0 } }, test
= { { 0 } }, evt
= { { 0 } };
930 unsigned nsteps
= 0, ntests
= 0;
931 static const char *const kwlist
[] =
932 { "start", "name", "stepper", "tester", "event", "nsteps", "ntests", 0 };
934 step
.exc
= &exc
; step
.ev
.proc
= pgen_filter
; step
.ev
.ctx
= &fc
;
935 test
.exc
= &exc
; test
.ev
.proc
= pgen_test
; test
.ev
.ctx
= &tc
;
937 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|sO&O&O&O&O&:pgen", KWLIST
,
938 convmp
, &x
, &p
, convpgev
, &step
,
939 convpgev
, &test
, convpgev
, &evt
,
940 convuint
, &nsteps
, convuint
, &ntests
))
942 if (!ntests
) ntests
= rabin_iters(mp_bits(x
));
943 if ((r
= pgen(p
, MP_NEW
, x
, evt
.ev
.proc
, evt
.ev
.ctx
,
944 nsteps
, step
.ev
.proc
, step
.ev
.ctx
,
945 ntests
, test
.ev
.proc
, test
.ev
.ctx
)) == 0)
947 rc
= mp_pywrap(r
); r
= 0;
949 mp_drop(r
); mp_drop(x
);
950 droppgev(&step
); droppgev(&test
); droppgev(&evt
);
954 static PyObject
*meth_strongprime_setup(PyObject
*me
,
955 PyObject
*arg
, PyObject
*kw
)
959 grand
*r
= &rand_global
;
963 struct excinfo exc
= EXCINFO_INIT
;
964 pypgev evt
= { { 0 } };
966 static const char *const kwlist
[] =
967 { "nbits", "name", "event", "rng", "nsteps", 0 };
970 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|sO&O&O&", KWLIST
,
971 convuint
, &nbits
, &name
,
972 convpgev
, &evt
, convgrand
, &r
,
975 if ((x
= strongprime_setup(name
, MP_NEW
, &f
, nbits
,
976 r
, n
, evt
.ev
.proc
, evt
.ev
.ctx
)) == 0)
978 rc
= Py_BuildValue("(NN)", mp_pywrap(x
), pfilt_pywrap(&f
));
986 static PyObject
*meth_strongprime(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
989 grand
*r
= &rand_global
;
993 struct excinfo exc
= EXCINFO_INIT
;
994 pypgev evt
= { { 0 } };
996 static const char *const kwlist
[] =
997 { "nbits", "name", "event", "rng", "nsteps", 0 };
1000 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|sO&O&O&", KWLIST
,
1001 convuint
, &nbits
, &name
,
1002 convpgev
, &evt
, convgrand
, &r
,
1005 if ((x
= strongprime(name
, MP_NEW
, nbits
,
1006 r
, n
, evt
.ev
.proc
, evt
.ev
.ctx
)) == 0)
1016 static PyObject
*meth_limlee(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1019 struct excinfo exc
= EXCINFO_INIT
;
1020 pypgev ie
= { { 0 } }, oe
= { { 0 } };
1022 grand
*r
= &rand_global
;
1025 PyObject
*rc
= 0, *vec
;
1026 static const char *const kwlist
[] =
1027 { "pbits", "qbits", "name", "event", "ievent", "rng", "nsteps", 0 };
1030 ie
.exc
= oe
.exc
= &exc
;
1031 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&|sO&O&O&O&:limlee", KWLIST
,
1032 convuint
, &pl
, convuint
, &ql
,
1033 &p
, convpgev
, &oe
, convpgev
, &ie
,
1034 convgrand
, &r
, convuint
, &on
))
1036 if ((x
= limlee(p
, MP_NEW
, MP_NEW
, ql
, pl
, r
, on
,
1037 oe
.ev
.proc
, oe
.ev
.ctx
, ie
.ev
.proc
, ie
.ev
.ctx
,
1040 vec
= PyList_New(nf
);
1041 for (i
= 0; i
< nf
; i
++)
1042 PyList_SET_ITEM(vec
, i
, mp_pywrap(v
[i
]));
1044 rc
= Py_BuildValue("(NN)", mp_pywrap(x
), vec
);
1046 droppgev(&oe
); droppgev(&ie
);
1050 /*----- Global stuff ------------------------------------------------------*/
1052 static PyMethodDef methods
[] = {
1053 #define METHNAME(name) meth_##name
1054 METH (_PrimeFilter_smallfactor
, "smallfactor(X) -> PGRC")
1055 METH (_RabinMiller_iters
, "iters(NBITS) -> NITERS")
1057 "pgen(START, [name = 'p'], [stepper = PrimeGenStepper(2)],\n"
1058 " [tester = PrimeGenTester()], [event = pgen_nullev],\n"
1059 " [nsteps = 0], [ntests = RabinMiller.iters(START.nbits)]) -> P")
1060 KWMETH(strongprime_setup
,
1061 "strongprime_setup(NBITS, [name = 'p'], [event = pgen_nullev],\n"
1062 " [rng = rand], [nsteps = 0]) -> (START, JUMP)")
1064 "strongprime(NBITS, [name = 'p'], [event = pgen_nullev],\n"
1065 " [rng = rand], [nsteps = 0]) -> P")
1067 "limlee(PBITS, QBITS, [name = 'p'], [event = pgen_nullev],\n"
1068 " [ievent = pgen_nullev], [rng = rand], [nsteps = 0]) "
1074 void pgen_pyinit(void)
1076 INITTYPE(pfilt
, root
);
1077 INITTYPE(rabin
, root
);
1078 INITTYPE(pgevent
, root
);
1079 INITTYPE(pgev
, root
);
1080 INITTYPE(pgstep
, pgev
);
1081 INITTYPE(pgjump
, pgev
);
1082 INITTYPE(pgtest
, pgev
);
1083 addmethods(methods
);
1086 static PyObject
*obj
;
1088 void pgen_pyinsert(PyObject
*mod
)
1090 INSERT("PrimeFilter", pfilt_pytype
);
1091 INSERT("RabinMiller", rabin_pytype
);
1092 INSERT("PrimeGenEvent", pgevent_pytype
);
1093 INSERT("PrimeGenBuiltinHandler", pgev_pytype
);
1094 INSERT("PrimeGenStepper", pgstep_pytype
);
1095 INSERT("PrimeGenJumper", pgjump_pytype
);
1096 INSERT("PrimeGenTester", pgtest_pytype
);
1097 INSERT("pgen_nullev", obj
= pgev_stdev(0));
1098 INSERT("pgen_stdev", pgev_stdev(pgen_ev
));
1099 INSERT("pgen_spinev", pgev_stdev(pgen_evspin
));
1100 INSERT("pgen_subev", pgev_stdev(pgen_subev
));
1103 /*----- That's all, folks -------------------------------------------------*/