5 * Multiprecision arithmetic
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Python interface to Catacomb.
14 * Catacomb/Python is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Catacomb/Python 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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Catacomb/Python; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
31 #include "catacomb-python.h"
33 /*----- General utilities -------------------------------------------------*/
35 PyTypeObject *mp_pytype = 0;
36 PyTypeObject *gf_pytype = 0;
38 mp *mp_frompylong(PyObject *obj)
41 PyLongObject *l = (PyLongObject *)obj;
52 assert(MPW_BITS >= SHIFT);
53 bits = (unsigned long)sz * SHIFT;
54 w = (bits + MPW_BITS - 1)/MPW_BITS;
55 x = mp_new(w, l->ob_size < 0 ? MP_NEG : 0);
57 for (i = 0; i < sz; i++) {
58 r |= (mpd)l->ob_digit[i] << b;
60 while (b >= MPW_BITS) {
75 PyObject *mp_topylong(mp *x)
77 unsigned long bits = mp_bits(x);
78 int sz = (bits + SHIFT - 1)/SHIFT;
79 PyLongObject *l = _PyLong_New(sz);
85 assert(MPW_BITS >= SHIFT);
86 while (i < sz && p < x->vl) {
89 while (i < sz && b >= SHIFT) {
90 l->ob_digit[i++] = r & MASK;
96 l->ob_digit[i++] = r & MASK;
99 l->ob_size = (x->f & MP_NEG) ? -sz : sz;
100 return ((PyObject *)l);
103 mp *mp_frompyobject(PyObject *o, int radix)
107 if (PyString_Check(o)) {
110 sc.buf = PyString_AS_STRING(o);
111 sc.lim = sc.buf + PyString_GET_SIZE(o);
112 x = mp_read(MP_NEW, radix, &mptext_stringops, &sc);
114 if (sc.buf < sc.lim) { MP_DROP(x); return (0); }
117 if ((x = tomp(o)) != 0)
122 PyObject *mp_topystring(mp *x, int radix, const char *xpre,
123 const char *pre, const char *post)
125 int len = mptext_len(x, radix) + 1;
128 size_t xprelen = xpre ? strlen(xpre) : 0;
129 size_t prelen = pre ? strlen(pre) : 0;
130 size_t postlen = post ? strlen(post) : 0;
133 o = PyString_FromStringAndSize(0, len + 1 + xprelen + prelen + postlen);
134 p = PyString_AS_STRING(o);
136 if (xpre) { memcpy(sc.buf, xpre, xprelen); sc.buf += xprelen; }
137 if (MP_NEGP(x)) { *sc.buf++ = '-'; x = mp_neg(x, x); }
138 if (pre) { memcpy(sc.buf, pre, prelen); sc.buf += prelen; }
139 sc.lim = sc.buf + len;
140 mp_write(x, radix, &mptext_stringops, &sc);
141 if (post) { memcpy(sc.buf, post, postlen); sc.buf += postlen; }
143 _PyString_Resize(&o, sc.buf - p);
147 static int good_radix_p(int r, int readp)
149 return ((r >= -255 && r <= -2) ||
151 (r >= 2 && r <= 62));
154 PyObject *mp_pywrap(mp *x)
156 mp_pyobj *z = PyObject_New(mp_pyobj, mp_pytype);
158 return ((PyObject *)z);
161 PyObject *gf_pywrap(mp *x)
163 mp_pyobj *z = PyObject_New(mp_pyobj, gf_pytype);
165 return ((PyObject *)z);
168 int mp_tolong_checked(mp *x, long *l)
170 static mp *longmin = 0, *longmax = 0;
174 longmin = mp_fromlong(MP_NEW, LONG_MIN);
175 longmax = mp_fromlong(MP_NEW, LONG_MAX);
177 if (MP_CMP(x, <, longmin) || MP_CMP(x, >, longmax))
178 VALERR("mp out of range for int");
185 /*----- Python interface --------------------------------------------------*/
187 static void mp_pydealloc(PyObject *o)
193 static PyObject *mp_pyrepr(PyObject *o)
194 { return mp_topystring(MP_X(o), 10, "MP(", 0, "L)"); }
196 static PyObject *mp_pystr(PyObject *o)
197 { return mp_topystring(MP_X(o), 10, 0, 0, 0); }
199 mp *tomp(PyObject *o)
206 else if (MP_PYCHECK(o) || GF_PYCHECK(o))
207 return (MP_COPY(MP_X(o)));
208 else if (FE_PYCHECK(o))
209 return (F_OUT(FE_F(o), MP_NEW, FE_X(o)));
210 else if (PFILT_PYCHECK(o))
211 return (MP_COPY(PFILT_F(o)->m));
212 else if (ECPT_PYCHECK(o)) {
218 } else if (GE_PYCHECK(o)) {
219 if ((x = G_TOINT(GE_G(o), MP_NEW, GE_X(o))) == 0)
222 } else if (PyInt_Check(o))
223 return (mp_fromlong(MP_NEW, PyInt_AS_LONG(o)));
224 else if ((l = PyNumber_Long(o)) != 0) {
225 x = mp_frompylong(l);
234 mp *getmp(PyObject *o)
238 if ((x = tomp(o)) == 0) {
239 PyErr_Format(PyExc_TypeError, "can't convert %.100s to mp",
240 o->ob_type->tp_name);
245 int convmp(PyObject *o, void *p)
248 if ((x = getmp(o)) == 0) return (0);
253 mp *getgf(PyObject *o)
257 if ((x = tomp(o)) == 0) {
258 PyErr_Format(PyExc_TypeError, "can't convert %.100s to gf",
259 o->ob_type->tp_name);
264 int convgf(PyObject *o, void *p)
267 if ((x = getgf(o)) == 0) return (0);
272 static int mpbinop(PyObject *x, PyObject *y, mp **xx, mp **yy)
274 if ((*xx = tomp(x)) == 0)
276 if ((*yy = tomp(y)) == 0) {
283 #define gf_and mp_and
285 #define gf_xor mp_xor
286 #define BINOP(pre, name) \
287 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
289 if (mpbinop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
290 zz = pre##_##name(MP_NEW, xx, yy); \
291 MP_DROP(xx); MP_DROP(yy); \
292 return (pre##_pywrap(zz)); \
308 static mp *mp_abs(mp *d, mp *x)
311 return (mp_neg(d, x));
317 #define UNOP(pre, name) \
318 static PyObject *pre##_py##name(PyObject *x) \
319 { return mp_pywrap(pre##_##name(MP_NEW, MP_X(x))); }
325 static PyObject *mp_pyid(PyObject *x) { RETURN_OBJ(x); }
327 #define gf_lsr mp_lsr
328 #define SHIFTOP(pre, name, rname) \
329 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
333 if (mpbinop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
334 if (mp_tolong_checked(yy, &n)) goto end; \
336 z = pre##_pywrap(mp_##rname(MP_NEW, xx, -n)); \
338 z = pre##_pywrap(mp_##name(MP_NEW, xx, n)); \
340 MP_DROP(xx); MP_DROP(yy); \
343 SHIFTOP(mp, lsl2c, lsr2c)
344 SHIFTOP(mp, lsr2c, lsl2c)
345 SHIFTOP(gf, lsl, lsr)
346 SHIFTOP(gf, lsr, lsl)
349 #define DIVOP(pre, name, qq, rr, gather) \
350 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
353 INIT_##qq(q) INIT_##rr(r) \
354 if (mpbinop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
356 ZDIVERR("division by zero"); \
357 pre##_div(ARG_##qq(q), ARG_##rr(r), xx, yy); \
360 MP_DROP(xx); MP_DROP(yy); \
363 #define INIT_YES(p) mp *p = MP_NEW;
365 #define ARG_YES(p) &p
367 DIVOP(mp, divmod, YES, YES,
368 Py_BuildValue("(NN)", mp_pywrap(q), mp_pywrap(r)))
369 DIVOP(mp, div, YES, NO, mp_pywrap(q))
370 DIVOP(mp, mod, NO, YES, mp_pywrap(r))
371 DIVOP(gf, divmod, YES, YES,
372 Py_BuildValue("(NN)", gf_pywrap(q), gf_pywrap(r)))
373 DIVOP(gf, div, YES, NO, gf_pywrap(q))
374 DIVOP(gf, mod, NO, YES, gf_pywrap(r))
381 static mp *mp_modinv_checked(mp *d, mp *x, mp *p)
384 mp_gcd(&g, 0, &d, p, x);
385 if (!MP_EQ(g, MP_ONE)) {
386 MP_DROP(g); MP_DROP(d);
387 PyErr_SetString(PyExc_ZeroDivisionError, "no modular inverse");
390 MP_DROP(g); return (d);
393 static PyObject *mp_pyexp(PyObject *x, PyObject *y, PyObject *z)
395 mp *xx = 0, *yy = 0, *zz = 0;
399 if ((xx = tomp(x)) == 0 || (yy = tomp(y)) == 0 ||
400 (z && z != Py_None && (zz = tomp(z)) == 0)) {
401 mp_drop(xx); mp_drop(yy); mp_drop(zz);
404 if (!z || z == Py_None) {
405 if (MP_NEGP(yy)) VALERR("negative exponent");
406 r = mp_exp(MP_NEW, xx, yy);
408 if (!MP_POSP(zz)) VALERR("modulus must be positive");
410 if ((xx = mp_modinv_checked(xx, xx, zz)) == 0) goto end;
415 mpmont_create(&mm, zz);
416 r = mpmont_exp(&mm, MP_NEW, xx, yy);
420 mpbarrett_create(&mb, zz);
421 r = mpbarrett_exp(&mb, MP_NEW, xx, yy);
422 mpbarrett_destroy(&mb);
427 mp_drop(xx); mp_drop(yy); mp_drop(zz);
431 static mp *gf_modinv_checked(mp *d, mp *x, mp *p)
434 gf_gcd(&g, 0, &d, p, x);
435 if (!MP_EQ(g, MP_ONE)) {
436 MP_DROP(g); MP_DROP(d);
437 PyErr_SetString(PyExc_ZeroDivisionError, "no modular inverse");
440 MP_DROP(g); return (d);
443 #define BASEOP(name, radix, pre) \
444 static PyObject *mp_py##name(PyObject *x) \
445 { return mp_topystring(MP_X(x), radix, 0, pre, 0); }
447 BASEOP(hex, 16, "0x");
450 static int mp_pynonzerop(PyObject *x) { return !MP_ZEROP(MP_X(x)); }
452 static PyObject *mp_pyint(PyObject *x)
455 if (mp_tolong_checked(MP_X(x), &l)) return (0);
456 return (PyInt_FromLong(l));
458 static PyObject *mp_pylong(PyObject *x)
459 { return (mp_topylong(MP_X(x))); }
460 static PyObject *mp_pyfloat(PyObject *x)
462 PyObject *l = mp_topylong(MP_X(x));
463 double f = PyLong_AsDouble(l);
465 return (PyFloat_FromDouble(f));
468 #define COERCE(pre, PRE) \
469 static int pre##_pycoerce(PyObject **x, PyObject **y) \
473 if (PRE##_PYCHECK(*y)) { \
474 Py_INCREF(*x); Py_INCREF(*y); \
477 if ((z = tomp(*y)) != 0) { \
479 *y = pre##_pywrap(z); \
488 static int mp_pycompare(PyObject *x, PyObject *y)
489 { return mp_cmp(MP_X(x), MP_X(y)); }
491 static PyObject *mp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
497 char *kwlist[] = { "x", "radix", 0 };
499 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|i:new", kwlist, &x, &radix))
501 if (MP_PYCHECK(x)) RETURN_OBJ(x);
502 if (!good_radix_p(radix, 1)) VALERR("bad radix");
503 if ((z = mp_frompyobject(x, radix)) == 0) {
504 PyErr_Format(PyExc_TypeError, "can't convert %.100s to mp",
505 x->ob_type->tp_name);
508 zz = (mp_pyobj *)ty->tp_alloc(ty, 0);
511 return ((PyObject *)zz);
514 static long mp_pyhash(PyObject *me)
517 PyObject *l = mp_topylong(MP_X(me)); h = PyObject_Hash(l);
518 Py_DECREF(l); return (h);
521 static PyObject *mpmeth_jacobi(PyObject *me, PyObject *arg)
526 if (!PyArg_ParseTuple(arg, "O&:jacobi", convmp, &y)) goto end;
527 if (MP_NEGP(MP_X(me)) || MP_EVENP(MP_X(me)))
528 VALERR("must be positive and odd");
529 z = PyInt_FromLong(mp_jacobi(y, MP_X(me)));
535 #define BITOP(pre, name, c) \
536 static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \
539 if (!PyArg_ParseTuple(arg, "i:" #name, &i)) return (0); \
540 return (pre##_pywrap(mp_##name##c(MP_NEW, MP_X(me), i))); \
542 BITOP(mp, setbit, 2c);
543 BITOP(mp, clearbit, 2c);
545 BITOP(gf, clearbit, );
548 static PyObject *mpmeth_testbit(PyObject *me, PyObject *arg)
551 if (!PyArg_ParseTuple(arg, "i:testbit", &i)) return (0);
552 return (getbool(mp_testbit2c(MP_X(me), i)));
555 static PyObject *gfmeth_testbit(PyObject *me, PyObject *arg)
558 if (!PyArg_ParseTuple(arg, "i:testbit", &i)) return (0);
559 return (getbool(mp_testbit(MP_X(me), i)));
562 static PyObject *mpmeth_odd(PyObject *me, PyObject *arg)
567 if (!PyArg_ParseTuple(arg, ":odd")) return (0);
568 t = mp_odd(MP_NEW, MP_X(me), &s);
569 return (Py_BuildValue("(lN)", (long)s, mp_pywrap(t)));
572 static PyObject *mpmeth_sqr(PyObject *me, PyObject *arg)
574 if (!PyArg_ParseTuple(arg, ":sqr")) return (0);
575 return (mp_pywrap(mp_sqr(MP_NEW, MP_X(me))));
578 static PyObject *mpmeth_sqrt(PyObject *me, PyObject *arg)
580 if (!PyArg_ParseTuple(arg, ":sqrt")) return (0);
581 if (MP_NEGP(MP_X(me))) VALERR("negative root");
582 return (mp_pywrap(mp_sqrt(MP_NEW, MP_X(me))));
587 static PyObject *mpmeth_gcd(PyObject *me, PyObject *arg)
589 mp *y = 0, *zz = MP_NEW;
592 if (!PyArg_ParseTuple(arg, "O&:gcd", convmp, &y)) goto end;
593 mp_gcd(&zz, 0, 0, MP_X(me), y);
600 static PyObject *mpmeth_gcdx(PyObject *me, PyObject *arg)
603 mp *yy = 0, *zz = MP_NEW, *uu = MP_NEW, *vv = MP_NEW;
605 if (!PyArg_ParseTuple(arg, "O&:gcdx", convmp, &yy)) goto end;
606 mp_gcd(&zz, &uu, &vv, MP_X(me), yy);
607 z = Py_BuildValue("(NNN)",
608 mp_pywrap(zz), mp_pywrap(uu), mp_pywrap(vv));
614 static PyObject *mpmeth_modinv(PyObject *me, PyObject *arg)
617 mp *yy = 0, *zz = MP_NEW;
619 if (!PyArg_ParseTuple(arg, "O&:modinv", convmp, &yy) ||
620 (zz = mp_modinv_checked(MP_NEW, yy, MP_X(me))) == 0)
628 static PyObject *mpmeth_tostring(PyObject *me, PyObject *arg, PyObject *kw)
631 char *kwlist[] = { "radix", 0 };
632 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|i:tostring", kwlist, &radix))
634 if (!good_radix_p(radix, 0)) VALERR("bad radix");
635 return (mp_topystring(MP_X(me), radix, 0, 0, 0));
640 static PyObject *mpmeth_modsqrt(PyObject *me, PyObject *arg)
643 mp *yy = 0, *zz = MP_NEW;
645 if (!PyArg_ParseTuple(arg, "O&:modsqrt", convmp, &yy)) goto end;
646 if ((zz = mp_modsqrt(MP_NEW, yy, MP_X(me))) == 0)
647 VALERR("no modular square root");
654 #define STOREOP(name, c) \
655 static PyObject *mpmeth_##name(PyObject *me, \
656 PyObject *arg, PyObject *kw) \
659 char *kwlist[] = { "len", 0 }; \
662 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|l:" #name, \
666 len = mp_octets##c(MP_X(me)); \
669 rc = bytestring_pywrap(0, len); \
670 mp_##name(MP_X(me), PyString_AS_STRING(rc), len); \
676 STOREOP(storel2c, 2c)
677 STOREOP(storeb2c, 2c)
680 #define BUFOP(ty, pyty) \
681 static PyObject *meth__##pyty##_frombuf(PyObject *me, PyObject *arg) \
689 if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end; \
690 buf_init(&b, p, sz); \
691 if ((x = buf_getmp(&b)) == 0) VALERR("malformed data"); \
692 rc = Py_BuildValue("(NN)", ty##_pywrap(x), \
693 bytestring_pywrapbuf(&b)); \
701 static PyObject *mpmeth_tobuf(PyObject *me, PyObject *arg)
708 if (!PyArg_ParseTuple(arg, ":tobuf")) return (0);
710 n = mp_octets(x) + 3;
711 rc = bytestring_pywrap(0, n);
712 buf_init(&b, PyString_AS_STRING(rc), n);
715 _PyString_Resize(&rc, BLEN(&b));
719 static PyObject *mpmeth_primep(PyObject *me, PyObject *arg, PyObject *kw)
721 grand *r = &rand_global;
722 char *kwlist[] = { "rng", 0 };
725 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&", kwlist, convgrand, &r))
727 rc = getbool(pgen_primep(MP_X(me), r));
732 static PyObject *mpget_nbits(PyObject *me, void *hunoz)
733 { return (PyInt_FromLong(mp_bits(MP_X(me)))); }
735 static PyObject *mpget_noctets(PyObject *me, void *hunoz)
736 { return (PyInt_FromLong(mp_octets(MP_X(me)))); }
738 static PyObject *mpget_noctets2c(PyObject *me, void *hunoz)
739 { return (PyInt_FromLong(mp_octets2c(MP_X(me)))); }
741 static PyGetSetDef mp_pygetset[] = {
742 #define GETSETNAME(op, func) mp##op##_##func
743 GET (nbits, "X.nbits -> bit length of X")
744 GET (noctets, "X.noctets -> octet length of X")
745 GET (noctets2c, "X.noctets2c -> two's complement octet length of X")
750 static PyMethodDef mp_pymethods[] = {
751 #define METHNAME(func) mpmeth_##func
752 METH (jacobi, "X.jacobi(Y) -> Jacobi symbol (Y/X) (NB inversion!)")
753 METH (setbit, "X.setbit(N) -> X with bit N set")
754 METH (clearbit, "X.clearbit(N) -> X with bit N clear")
755 METH (testbit, "X.testbit(N) -> true/false if bit N set/clear in X")
756 METH (odd, "X.odd() -> S, T where X = 2^S T with T odd")
757 METH (sqr, "X.sqr() -> X^2")
758 METH (sqrt, "X.sqrt() -> largest integer <= sqrt(X)")
759 METH (gcd, "X.gcd(Y) -> gcd(X, Y)")
761 "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)")
762 METH (modinv, "X.modinv(Y) -> multiplicative inverse of Y mod X")
763 METH (modsqrt, "X.modsqrt(Y) -> square root of Y mod X, if X prime")
764 KWMETH(primep, "X.primep(rng = rand) -> true/false if X is prime")
765 KWMETH(tostring, "X.tostring(radix = 10) -> STR")
766 KWMETH(storel, "X.storel(len = -1) -> little-endian bytes")
767 KWMETH(storeb, "X.storeb(len = -1) -> big-endian bytes")
769 "X.storel2c(len = -1) -> little-endian bytes, two's complement")
771 "X.storeb2c(len = -1) -> big-endian bytes, two's complement")
772 METH (tobuf, "X.tobuf() -> buffer format")
777 static PyNumberMethods mp_pynumber = {
778 mp_pyadd, /* @nb_add@ */
779 mp_pysub, /* @nb_subtract@ */
780 mp_pymul, /* @nb_multiply@ */
782 mp_pymod, /* @nb_remainder@ */
783 mp_pydivmod, /* @nb_divmod@ */
784 mp_pyexp, /* @nb_power@ */
785 mp_pyneg, /* @nb_negative@ */
786 mp_pyid, /* @nb_positive@ */
787 mp_pyabs, /* @nb_absolute@ */
788 mp_pynonzerop, /* @nb_nonzero@ */
789 mp_pynot2c, /* @nb_invert@ */
790 mp_pylsl2c, /* @nb_lshift@ */
791 mp_pylsr2c, /* @nb_rshift@ */
792 mp_pyand2c, /* @nb_and@ */
793 mp_pyxor2c, /* @nb_xor@ */
794 mp_pyor2c, /* @nb_or@ */
795 mp_pycoerce, /* @nb_coerce@ */
796 mp_pyint, /* @nb_int@ */
797 mp_pylong, /* @nb_long@ */
798 mp_pyfloat, /* @nb_float@ */
799 mp_pyoct, /* @nb_oct@ */
800 mp_pyhex, /* @nb_hex@ */
802 0, /* @nb_inplace_add@ */
803 0, /* @nb_inplace_subtract@ */
804 0, /* @nb_inplace_multiply@ */
805 0, /* @nb_inplace_divide@ */
806 0, /* @nb_inplace_remainder@ */
807 0, /* @nb_inplace_power@ */
808 0, /* @nb_inplace_lshift@ */
809 0, /* @nb_inplace_rshift@ */
810 0, /* @nb_inplace_and@ */
811 0, /* @nb_inplace_xor@ */
812 0, /* @nb_inplace_or@ */
814 mp_pydiv, /* @nb_floor_divide@ */
815 0, /* @nb_true_divide@ */
816 0, /* @nb_inplace_floor_divide@ */
817 0, /* @nb_inplace_true_divide@ */
820 static PyTypeObject mp_pytype_skel = {
821 PyObject_HEAD_INIT(0) 0, /* Header */
822 "catacomb.MP", /* @tp_name@ */
823 sizeof(mp_pyobj), /* @tp_basicsize@ */
824 0, /* @tp_itemsize@ */
826 mp_pydealloc, /* @tp_dealloc@ */
828 0, /* @tp_getattr@ */
829 0, /* @tp_setattr@ */
830 mp_pycompare, /* @tp_compare@ */
831 mp_pyrepr, /* @tp_repr@ */
832 &mp_pynumber, /* @tp_as_number@ */
833 0, /* @tp_as_sequence@ */
834 0, /* @tp_as_mapping@ */
835 mp_pyhash, /* @tp_hash@ */
837 mp_pystr, /* @tp_str@ */
838 0, /* @tp_getattro@ */
839 0, /* @tp_setattro@ */
840 0, /* @tp_as_buffer@ */
841 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
842 Py_TPFLAGS_CHECKTYPES |
846 "Multiprecision integers, similar to `long' but more efficient and\n\
847 versatile. Support all the standard arithmetic operations.\n\
849 Constructor mp(X, radix = R) attempts to convert X to an `mp'. If\n\
850 X is a string, it's read in radix-R form, or we look for a prefix\n\
851 if R = 0. Other acceptable things are ints and longs.\n\
855 * Use `//' for division. MPs don't have `/' division.",
857 0, /* @tp_traverse@ */
859 0, /* @tp_richcompare@ */
860 0, /* @tp_weaklistoffset@ */
862 0, /* @tp_iternext@ */
863 mp_pymethods, /* @tp_methods@ */
864 0, /* @tp_members@ */
865 mp_pygetset, /* @tp_getset@ */
868 0, /* @tp_descr_get@ */
869 0, /* @tp_descr_set@ */
870 0, /* @tp_dictoffset@ */
872 PyType_GenericAlloc, /* @tp_alloc@ */
873 mp_pynew, /* @tp_new@ */
878 static PyObject *meth__MP_fromstring(PyObject *me,
879 PyObject *arg, PyObject *kw)
887 char *kwlist[] = { "class", "x", "radix", 0 };
889 if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|i:fromstring",
890 kwlist, &me, &p, &len, &r))
892 if (!good_radix_p(r, 1)) VALERR("bad radix");
893 sc.buf = p; sc.lim = p + len;
894 if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0)
895 SYNERR("bad integer");
896 z = Py_BuildValue("(Ns#)", mp_pywrap(zz), sc.buf, (int)(sc.lim - sc.buf));
901 #define LOADOP(pre, py, name) \
902 static PyObject *meth__##py##_##name(PyObject *me, PyObject *arg) \
906 if (!PyArg_ParseTuple(arg, "Os#:" #name, &me, &p, &len)) return (0); \
907 return (pre##_pywrap(mp_##name(MP_NEW, p, len))); \
909 LOADOP(mp, MP, loadl)
910 LOADOP(mp, MP, loadb)
911 LOADOP(mp, MP, loadl2c)
912 LOADOP(mp, MP, loadb2c)
913 LOADOP(gf, GF, loadl)
914 LOADOP(gf, GF, loadb)
917 /*----- Products of small integers ----------------------------------------*/
919 typedef struct mpmul_pyobj {
925 #define MPMUL_LIVEP(o) (((mpmul_pyobj *)(o))->livep)
926 #define MPMUL_PY(o) (&((mpmul_pyobj *)(o))->mm)
928 static void mpmul_pydealloc(PyObject *me)
931 mp_drop(mpmul_done(MPMUL_PY(me)));
935 static PyObject *mmmeth_factor(PyObject *me, PyObject *arg)
940 if (!MPMUL_LIVEP(me)) VALERR("MPMul object invalid");
941 if (PyTuple_Size(arg) != 1)
942 i = PyObject_GetIter(arg);
944 if ((q = PyTuple_GetItem(arg, 0)) == 0) goto end;
945 if ((i = PyObject_GetIter(q)) == 0) {
946 PyErr_Clear(); /* that's ok */
947 i = PyObject_GetIter(arg);
951 while ((q = PyIter_Next(i)) != 0) {
952 x = getmp(q); Py_DECREF(q); if (!x) {
956 mpmul_add(MPMUL_PY(me), x);
965 static PyObject *mmmeth_done(PyObject *me, PyObject *arg)
969 if (!PyArg_ParseTuple(arg, ":done")) goto end;
970 if (!MPMUL_LIVEP(me)) VALERR("MPMul object invalid");
971 x = mpmul_done(MPMUL_PY(me));
973 return (mp_pywrap(x));
978 static PyObject *mpmul_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
982 if (kw) TYERR("keyword arguments not allowed here");
983 mm = (mpmul_pyobj *)ty->tp_alloc(ty, 0);
986 if (mmmeth_factor((PyObject *)mm, arg) == 0) {
990 return ((PyObject *)mm);
995 static PyObject *mmget_livep(PyObject *me, void *hunoz)
996 { return (getbool(MPMUL_LIVEP(me))); }
998 static PyGetSetDef mpmul_pygetset[] = {
999 #define GETSETNAME(op, name) mm##op##_##name
1000 GET (livep, "MM.livep -> flag: object still valid?")
1005 static PyMethodDef mpmul_pymethods[] = {
1006 #define METHNAME(name) mmmeth_##name
1007 METH (factor, "MM.factor(ITERABLE) or MM.factor(I, ...)")
1008 METH (done, "MM.done() -> PRODUCT")
1013 static PyTypeObject *mpmul_pytype, mpmul_pytype_skel = {
1014 PyObject_HEAD_INIT(0) 0, /* Header */
1015 "catacomb.MPMul", /* @tp_name@ */
1016 sizeof(mpmul_pyobj), /* @tp_basicsize@ */
1017 0, /* @tp_itemsize@ */
1019 mpmul_pydealloc, /* @tp_dealloc@ */
1021 0, /* @tp_getattr@ */
1022 0, /* @tp_setattr@ */
1023 0, /* @tp_compare@ */
1025 0, /* @tp_as_number@ */
1026 0, /* @tp_as_sequence@ */
1027 0, /* @tp_as_mapping@ */
1031 0, /* @tp_getattro@ */
1032 0, /* @tp_setattro@ */
1033 0, /* @tp_as_buffer@ */
1034 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1035 Py_TPFLAGS_BASETYPE,
1038 "An object for multiplying many small integers.",
1040 0, /* @tp_traverse@ */
1042 0, /* @tp_richcompare@ */
1043 0, /* @tp_weaklistoffset@ */
1045 0, /* @tp_iternext@ */
1046 mpmul_pymethods, /* @tp_methods@ */
1047 0, /* @tp_members@ */
1048 mpmul_pygetset, /* @tp_getset@ */
1051 0, /* @tp_descr_get@ */
1052 0, /* @tp_descr_set@ */
1053 0, /* @tp_dictoffset@ */
1055 PyType_GenericAlloc, /* @tp_alloc@ */
1056 mpmul_pynew, /* @tp_new@ */
1061 /*----- Montgomery reduction ----------------------------------------------*/
1063 typedef struct mpmont_pyobj {
1068 #define MPMONT_PY(o) (&((mpmont_pyobj *)(o))->mm)
1070 static PyObject *mmmeth_int(PyObject *me, PyObject *arg)
1074 mpmont *mm = MPMONT_PY(me);
1076 if (!PyArg_ParseTuple(arg, "O&:in", convmp, &yy))
1078 mp_div(0, &yy, yy, mm->m);
1079 z = mp_pywrap(mpmont_mul(mm, MP_NEW, yy, mm->r2));
1081 if (yy) MP_DROP(yy);
1085 static PyObject *mmmeth_mul(PyObject *me, PyObject *arg)
1088 mp *yy = 0, *zz = 0;
1090 if (!PyArg_ParseTuple(arg, "O&O&:mul", convmp, &yy, convmp, &zz))
1092 rc = mp_pywrap(mpmont_mul(MPMONT_PY(me), MP_NEW, yy, zz));
1094 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
1098 static PyObject *mmmeth_exp(PyObject *me, PyObject *arg)
1101 mp *yy = 0, *zz = 0;
1103 if (!PyArg_ParseTuple(arg, "O&O&:exp", convmp, &yy, convmp, &zz))
1106 if ((yy = mp_modinv_checked(yy, yy, MPMONT_PY(me)->m)) == 0) goto end;
1107 zz = mp_neg(zz, zz);
1109 rc = mp_pywrap(mpmont_exp(MPMONT_PY(me), MP_NEW, yy, zz));
1111 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
1115 static PyObject *mmmeth_expr(PyObject *me, PyObject *arg)
1118 mp *yy = 0, *zz = 0;
1120 if (!PyArg_ParseTuple(arg, "O&O&:expr", convmp, &yy, convmp, &zz))
1123 yy = mpmont_reduce(MPMONT_PY(me), yy, yy);
1124 if ((yy = mp_modinv_checked(yy, yy, MPMONT_PY(me)->m)) == 0) goto end;
1125 yy = mpmont_mul(MPMONT_PY(me), yy, yy, MPMONT_PY(me)->r2);
1126 zz = mp_neg(zz, zz);
1128 rc = mp_pywrap(mpmont_expr(MPMONT_PY(me), MP_NEW, yy, zz));
1130 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
1134 static PyObject *mm_mexpr_id(PyObject *me)
1135 { return mp_pywrap(MP_COPY(MPMONT_PY(me)->r)); }
1137 static int mm_mexpr_fill(void *p, PyObject *me, PyObject *x, PyObject *y)
1139 mp *xx = 0, *yy = 0;
1140 mp_expfactor *f = p;
1141 mpmont *mm = MPMONT_PY(me);
1143 if ((xx = getmp(x)) == 0 || (yy = getmp(y)) == 0)
1146 xx = mpmont_reduce(mm, xx, xx);
1147 if ((xx = mp_modinv_checked(xx, xx, yy)) == 0)
1149 xx = mpmont_mul(mm, xx, xx, mm->r2);
1150 yy = mp_neg(yy, yy);
1157 mp_drop(xx); mp_drop(yy);
1161 static PyObject *mm_mexpr(PyObject *me, void *v, int n)
1162 { return mp_pywrap(mpmont_mexpr(MPMONT_PY(me), MP_NEW, v, n)); }
1164 static void mp_mexp_drop(void *p)
1166 mp_expfactor *f = p;
1171 static PyObject *mmmeth_mexpr(PyObject *me, PyObject *arg)
1173 return mexp_common(me, arg, sizeof(mp_expfactor),
1174 mm_mexpr_id, mm_mexpr_fill, mm_mexpr, mp_mexp_drop);
1177 static PyObject *mp_mexp_id(PyObject *me)
1178 { return mp_pywrap(MP_ONE); }
1180 static int mp_mexp_fill(void *p, PyObject *me, PyObject *x, PyObject *y)
1182 mp *xx = 0, *yy = 0;
1183 mp_expfactor *f = p;
1185 if ((xx = getmp(x)) == 0 || (yy = getmp(y)) == 0)
1188 if ((xx = mp_modinv_checked(xx, xx, yy)) == 0)
1190 yy = mp_neg(yy, yy);
1197 mp_drop(xx); mp_drop(yy);
1201 static PyObject *mm_mexp(PyObject *me, void *v, int n)
1202 { return mp_pywrap(mpmont_mexp(MPMONT_PY(me), MP_NEW, v, n)); }
1204 static PyObject *mmmeth_mexp(PyObject *me, PyObject *arg)
1206 return mexp_common(me, arg, sizeof(mp_expfactor),
1207 mp_mexp_id, mp_mexp_fill, mm_mexp, mp_mexp_drop);
1210 #define mmmeth_ext mmmeth_reduce
1211 static PyObject *mmmeth_reduce(PyObject *me, PyObject *arg)
1216 if (!PyArg_ParseTuple(arg, "O&", convmp, &yy)) goto end;
1217 z = mp_pywrap(mpmont_reduce(MPMONT_PY(me), MP_NEW, yy));
1222 static void mpmont_pydealloc(PyObject *me)
1224 mpmont_destroy(MPMONT_PY(me));
1228 static PyObject *mpmont_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1230 mpmont_pyobj *mm = 0;
1231 char *kwlist[] = { "m", 0 };
1234 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", kwlist, convmp, &xx))
1236 if (!MP_POSP(xx) || !MP_ODDP(xx)) VALERR("m must be positive and odd");
1237 mm = (mpmont_pyobj *)ty->tp_alloc(ty, 0);
1238 mpmont_create(&mm->mm, xx);
1240 if (xx) MP_DROP(xx);
1241 return ((PyObject *)mm);
1244 static PyObject *mmget_m(PyObject *me, void *hunoz)
1245 { return (mp_pywrap(MP_COPY(MPMONT_PY(me)->m))); }
1247 static PyObject *mmget_r(PyObject *me, void *hunoz)
1248 { return (mp_pywrap(MP_COPY(MPMONT_PY(me)->r))); }
1250 static PyObject *mmget_r2(PyObject *me, void *hunoz)
1251 { return (mp_pywrap(MP_COPY(MPMONT_PY(me)->r2))); }
1253 static PyGetSetDef mpmont_pygetset[] = {
1254 #define GETSETNAME(op, name) mm##op##_##name
1255 GET (m, "M.m -> modulus for reduction")
1256 GET (r, "M.r -> multiplicative identity")
1257 GET (r2, "M.r2 -> M.r^2, Montgomerization factor")
1262 static PyMethodDef mpmont_pymethods[] = {
1263 #define METHNAME(name) mmmeth_##name
1264 METH (int, "M.out(X) -> XR")
1265 METH (mul, "M.mul(XR, YR) -> ZR where Z = X Y")
1266 METH (expr, "M.expr(XR, N) -> ZR where Z = X^N mod M.m")
1268 B.mexp([(XR0, N0), (XR1, N1), ...]) = ZR where Z = X0^N0 X1^N1 mod B.m\n\
1269 \t(the list may be flattened if this more convenient.)")
1270 METH (reduce, "M.reduce(XR) -> X")
1271 METH (ext, "M.ext(XR) -> X")
1272 METH (exp, "M.exp(X, N) -> X^N mod M.m")
1274 B.mexp([(X0, N0), (X1, N1), ...]) = X0^N0 X1^N1 mod B.m\n\
1275 \t(the list may be flattened if this more convenient.)")
1280 static PyTypeObject *mpmont_pytype, mpmont_pytype_skel = {
1281 PyObject_HEAD_INIT(0) 0, /* Header */
1282 "catacomb.MPMont", /* @tp_name@ */
1283 sizeof(mpmont_pyobj), /* @tp_basicsize@ */
1284 0, /* @tp_itemsize@ */
1286 mpmont_pydealloc, /* @tp_dealloc@ */
1288 0, /* @tp_getattr@ */
1289 0, /* @tp_setattr@ */
1290 0, /* @tp_compare@ */
1292 0, /* @tp_as_number@ */
1293 0, /* @tp_as_sequence@ */
1294 0, /* @tp_as_mapping@ */
1298 0, /* @tp_getattro@ */
1299 0, /* @tp_setattro@ */
1300 0, /* @tp_as_buffer@ */
1301 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1302 Py_TPFLAGS_BASETYPE,
1305 "A Montgomery reduction context.",
1307 0, /* @tp_traverse@ */
1309 0, /* @tp_richcompare@ */
1310 0, /* @tp_weaklistoffset@ */
1312 0, /* @tp_iternext@ */
1313 mpmont_pymethods, /* @tp_methods@ */
1314 0, /* @tp_members@ */
1315 mpmont_pygetset, /* @tp_getset@ */
1318 0, /* @tp_descr_get@ */
1319 0, /* @tp_descr_set@ */
1320 0, /* @tp_dictoffset@ */
1322 PyType_GenericAlloc, /* @tp_alloc@ */
1323 mpmont_pynew, /* @tp_new@ */
1328 /*----- Barrett reduction -------------------------------------------------*/
1330 typedef struct mpbarrett_pyobj {
1335 #define MPBARRETT_PY(o) (&((mpbarrett_pyobj *)(o))->mb)
1337 static PyObject *mbmeth_exp(PyObject *me, PyObject *arg)
1340 mp *yy = 0, *zz = 0;
1342 if (!PyArg_ParseTuple(arg, "O&O&:exp", convmp, &yy, convmp, &zz))
1345 if ((yy = mp_modinv_checked(yy, yy, MPBARRETT_PY(me)->m)) == 0) goto end;
1346 zz = mp_neg(zz, zz);
1348 rc = mp_pywrap(mpbarrett_exp(MPBARRETT_PY(me), MP_NEW, yy, zz));
1350 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
1354 static PyObject *mb_mexp(PyObject *me, void *v, int n)
1355 { return mp_pywrap(mpbarrett_mexp(MPBARRETT_PY(me), MP_NEW, v, n)); }
1357 static PyObject *mbmeth_mexp(PyObject *me, PyObject *arg)
1359 return mexp_common(me, arg, sizeof(mp_expfactor),
1360 mp_mexp_id, mp_mexp_fill, mb_mexp, mp_mexp_drop);
1363 static PyObject *mbmeth_reduce(PyObject *me, PyObject *arg)
1368 if (!PyArg_ParseTuple(arg, "O&:reduce", convmp, &yy))
1370 z = mp_pywrap(mpbarrett_reduce(MPBARRETT_PY(me), MP_NEW, yy));
1375 static void mpbarrett_pydealloc(PyObject *me)
1377 mpbarrett_destroy(MPBARRETT_PY(me));
1381 static PyObject *mpbarrett_pynew(PyTypeObject *ty,
1382 PyObject *arg, PyObject *kw)
1384 mpbarrett_pyobj *mb = 0;
1385 char *kwlist[] = { "m", 0 };
1388 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", kwlist, convmp, &xx))
1390 if (!MP_POSP(xx)) VALERR("m must be positive");
1391 mb = (mpbarrett_pyobj *)ty->tp_alloc(ty, 0);
1392 mpbarrett_create(&mb->mb, xx);
1394 if (xx) MP_DROP(xx);
1395 return ((PyObject *)mb);
1398 static PyObject *mbget_m(PyObject *me, void *hunoz)
1399 { return (mp_pywrap(MP_COPY(MPBARRETT_PY(me)->m))); }
1401 static PyGetSetDef mpbarrett_pygetset[] = {
1402 #define GETSETNAME(op, name) mb##op##_##name
1403 GET (m, "B.m -> modulus for reduction")
1408 static PyMethodDef mpbarrett_pymethods[] = {
1409 #define METHNAME(name) mbmeth_##name
1410 METH (reduce, "B.reduce(X) -> X mod B.m")
1411 METH (exp, "B.exp(X, N) -> X^N mod B.m")
1413 B.mexp([(X0, N0), (X1, N1), ...]) = X0^N0 X1^N1 mod B.m\n\
1414 \t(the list may be flattened if this more convenient.)")
1419 static PyTypeObject *mpbarrett_pytype, mpbarrett_pytype_skel = {
1420 PyObject_HEAD_INIT(0) 0, /* Header */
1421 "catacomb.MPBarrett", /* @tp_name@ */
1422 sizeof(mpbarrett_pyobj), /* @tp_basicsize@ */
1423 0, /* @tp_itemsize@ */
1425 mpbarrett_pydealloc, /* @tp_dealloc@ */
1427 0, /* @tp_getattr@ */
1428 0, /* @tp_setattr@ */
1429 0, /* @tp_compare@ */
1431 0, /* @tp_as_number@ */
1432 0, /* @tp_as_sequence@ */
1433 0, /* @tp_as_mapping@ */
1437 0, /* @tp_getattro@ */
1438 0, /* @tp_setattro@ */
1439 0, /* @tp_as_buffer@ */
1440 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1441 Py_TPFLAGS_BASETYPE,
1444 "A Barrett reduction context.",
1446 0, /* @tp_traverse@ */
1448 0, /* @tp_richcompare@ */
1449 0, /* @tp_weaklistoffset@ */
1451 0, /* @tp_iternext@ */
1452 mpbarrett_pymethods, /* @tp_methods@ */
1453 0, /* @tp_members@ */
1454 mpbarrett_pygetset, /* @tp_getset@ */
1457 0, /* @tp_descr_get@ */
1458 0, /* @tp_descr_set@ */
1459 0, /* @tp_dictoffset@ */
1461 PyType_GenericAlloc, /* @tp_alloc@ */
1462 mpbarrett_pynew, /* @tp_new@ */
1467 /*----- Nice prime reduction ----------------------------------------------*/
1469 typedef struct mpreduce_pyobj {
1474 #define MPREDUCE_PY(o) (&((mpreduce_pyobj *)(o))->mr)
1476 static PyObject *mrmeth_exp(PyObject *me, PyObject *arg)
1479 mp *yy = 0, *zz = 0;
1481 if (!PyArg_ParseTuple(arg, "O&O&:exp", convmp, &yy, convmp, &zz))
1484 if ((yy = mp_modinv_checked(yy, yy, MPREDUCE_PY(me)->p)) == 0) goto end;
1485 zz = mp_neg(zz, zz);
1487 rc = mp_pywrap(mpreduce_exp(MPREDUCE_PY(me), MP_NEW, yy, zz));
1489 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
1493 static PyObject *mrmeth_reduce(PyObject *me, PyObject *arg)
1498 if (!PyArg_ParseTuple(arg, "O&:reduce", convmp, &yy)) goto end;
1499 z = mp_pywrap(mpreduce_do(MPREDUCE_PY(me), MP_NEW, yy));
1504 static void mpreduce_pydealloc(PyObject *me)
1506 mpreduce_destroy(MPREDUCE_PY(me));
1510 static PyObject *mpreduce_pynew(PyTypeObject *ty,
1511 PyObject *arg, PyObject *kw)
1513 mpreduce_pyobj *mr = 0;
1515 char *kwlist[] = { "m", 0 };
1518 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", kwlist, convmp, &xx))
1520 if (!MP_POSP(xx)) VALERR("m must be positive");
1521 if (mpreduce_create(&r, xx)) VALERR("bad modulus (must be 2^k - ...)");
1522 mr = (mpreduce_pyobj *)ty->tp_alloc(ty, 0);
1525 if (xx) MP_DROP(xx);
1526 return ((PyObject *)mr);
1529 static PyObject *mrget_m(PyObject *me, void *hunoz)
1530 { return (mp_pywrap(MP_COPY(MPREDUCE_PY(me)->p))); }
1532 static PyGetSetDef mpreduce_pygetset[] = {
1533 #define GETSETNAME(op, name) mr##op##_##name
1534 GET (m, "R.m -> modulus for reduction")
1539 static PyMethodDef mpreduce_pymethods[] = {
1540 #define METHNAME(name) mrmeth_##name
1541 METH (reduce, "R.reduce(X) -> X mod B.m")
1542 METH (exp, "R.exp(X, N) -> X^N mod B.m")
1547 static PyTypeObject *mpreduce_pytype, mpreduce_pytype_skel = {
1548 PyObject_HEAD_INIT(0) 0, /* Header */
1549 "catacomb.MPReduce", /* @tp_name@ */
1550 sizeof(mpreduce_pyobj), /* @tp_basicsize@ */
1551 0, /* @tp_itemsize@ */
1553 mpreduce_pydealloc, /* @tp_dealloc@ */
1555 0, /* @tp_getattr@ */
1556 0, /* @tp_setattr@ */
1557 0, /* @tp_compare@ */
1559 0, /* @tp_as_number@ */
1560 0, /* @tp_as_sequence@ */
1561 0, /* @tp_as_mapping@ */
1565 0, /* @tp_getattro@ */
1566 0, /* @tp_setattro@ */
1567 0, /* @tp_as_buffer@ */
1568 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1569 Py_TPFLAGS_BASETYPE,
1572 "A reduction context for reduction modulo primes of special form.",
1574 0, /* @tp_traverse@ */
1576 0, /* @tp_richcompare@ */
1577 0, /* @tp_weaklistoffset@ */
1579 0, /* @tp_iternext@ */
1580 mpreduce_pymethods, /* @tp_methods@ */
1581 0, /* @tp_members@ */
1582 mpreduce_pygetset, /* @tp_getset@ */
1585 0, /* @tp_descr_get@ */
1586 0, /* @tp_descr_set@ */
1587 0, /* @tp_dictoffset@ */
1589 PyType_GenericAlloc, /* @tp_alloc@ */
1590 mpreduce_pynew, /* @tp_new@ */
1595 /*----- Chinese Remainder Theorem solution --------------------------------*/
1597 typedef struct mpcrt_pyobj {
1602 #define MPCRT_PY(o) (&((mpcrt_pyobj *)(o))->c)
1604 static PyObject *mcmeth_solve(PyObject *me, PyObject *arg)
1606 mpcrt *c = MPCRT_PY(me);
1607 PyObject *q = 0, *x, *z = 0;
1610 int i = 0, n = c->k;
1613 if (PyTuple_Size(arg) == n)
1615 else if (!PyArg_ParseTuple(arg, "O:solve", &q))
1618 if (!PySequence_Check(q)) TYERR("want a sequence of residues");
1619 if (PySequence_Size(q) != n) VALERR("residue count mismatch");
1620 v = xmalloc(n * sizeof(*v));
1621 for (i = 0; i < n; i++) {
1622 if ((x = PySequence_GetItem(q, i)) == 0) goto end;
1623 xx = getmp(x); Py_DECREF(x); if (!xx) goto end;
1626 z = mp_pywrap(mpcrt_solve(c, MP_NEW, v));
1630 for (i = 0; i < n; i++)
1639 static void mpcrt_pydealloc(PyObject *me)
1641 mpcrt *c = MPCRT_PY(me);
1646 static PyObject *mpcrt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1650 char *kwlist[] = { "mv", 0 };
1651 PyObject *q = 0, *x;
1655 if (PyTuple_Size(arg) > 1)
1657 else if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &q))
1660 if (!PySequence_Check(q)) TYERR("want a sequence of moduli");
1661 n = PySequence_Size(q);
1662 if (PyErr_Occurred()) goto end;
1663 if (!n) VALERR("want at least one modulus");
1664 v = xmalloc(n * sizeof(*v));
1665 for (i = 0; i < n; i++) {
1666 if ((x = PySequence_GetItem(q, i)) == 0) goto end;
1667 xx = getmp(x); Py_DECREF(x); if (!xx) goto end;
1668 v[i].m = xx; v[i].n = 0; v[i].ni = 0; v[i].nni = 0;
1670 c = (mpcrt_pyobj *)ty->tp_alloc(ty, 0);
1671 mpcrt_create(&c->c, v, n, 0);
1673 return ((PyObject *)c);
1678 for (i = 0; i < n; i++)
1686 static PyObject *mcget_product(PyObject *me, void *hunoz)
1687 { return (mp_pywrap(MP_COPY(MPCRT_PY(me)->mb.m))); }
1689 static PyObject *mcget_moduli(PyObject *me, void *hunoz)
1693 mpcrt *c = MPCRT_PY(me);
1695 if ((q = PyList_New(c->k)) == 0) return (0);
1696 for (i = 0; i < c->k; i++)
1697 PyList_SetItem(q, i, mp_pywrap(c->v[i].m));
1701 static PyGetSetDef mpcrt_pygetset[] = {
1702 #define GETSETNAME(op, name) mc##op##_##name
1703 GET (product, "C.product -> product of moduli")
1704 GET (moduli, "C.moduli -> list of individual moduli")
1709 static PyMethodDef mpcrt_pymethods[] = {
1710 #define METHNAME(name) mcmeth_##name
1711 METH (solve, "C.solve([R0, R1]) -> X mod C.product")
1716 static PyTypeObject *mpcrt_pytype, mpcrt_pytype_skel = {
1717 PyObject_HEAD_INIT(0) 0, /* Header */
1718 "catacomb.MPCRT", /* @tp_name@ */
1719 sizeof(mpcrt_pyobj), /* @tp_basicsize@ */
1720 0, /* @tp_itemsize@ */
1722 mpcrt_pydealloc, /* @tp_dealloc@ */
1724 0, /* @tp_getattr@ */
1725 0, /* @tp_setattr@ */
1726 0, /* @tp_compare@ */
1728 0, /* @tp_as_number@ */
1729 0, /* @tp_as_sequence@ */
1730 0, /* @tp_as_mapping@ */
1734 0, /* @tp_getattro@ */
1735 0, /* @tp_setattro@ */
1736 0, /* @tp_as_buffer@ */
1737 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1738 Py_TPFLAGS_BASETYPE,
1741 "A context for the solution of Chinese Remainder Theorem problems.",
1743 0, /* @tp_traverse@ */
1745 0, /* @tp_richcompare@ */
1746 0, /* @tp_weaklistoffset@ */
1748 0, /* @tp_iternext@ */
1749 mpcrt_pymethods, /* @tp_methods@ */
1750 0, /* @tp_members@ */
1751 mpcrt_pygetset, /* @tp_getset@ */
1754 0, /* @tp_descr_get@ */
1755 0, /* @tp_descr_set@ */
1756 0, /* @tp_dictoffset@ */
1758 PyType_GenericAlloc, /* @tp_alloc@ */
1759 mpcrt_pynew, /* @tp_new@ */
1764 /*----- Binary polynomials ------------------------------------------------*/
1766 static PyObject *gf_pyrepr(PyObject *o)
1767 { return mp_topystring(MP_X(o), 16, "GF(", "0x", "L)"); }
1769 static PyObject *gf_pyrichcompare(PyObject *x, PyObject *y, int op)
1775 if (mpbinop(x, y, &xx, &yy)) RETURN_NOTIMPL;
1777 case Py_EQ: b = MP_EQ(xx, yy); break;
1778 case Py_NE: b = !MP_EQ(xx, yy); break;
1783 case Py_LT: b = xl < yl; break;
1784 case Py_LE: b = xl <= yl; break;
1785 case Py_GT: b = xl > yl; break;
1786 case Py_GE: b = xl >= yl; break;
1791 MP_DROP(xx); MP_DROP(yy);
1792 return (getbool(b));
1795 static PyObject *gf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1801 char *kwlist[] = { "x", "radix", 0 };
1803 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|i:gf", kwlist, &x, &radix))
1805 if (GF_PYCHECK(x)) RETURN_OBJ(x);
1806 if (!good_radix_p(radix, 1)) VALERR("radix out of range");
1807 if ((z = mp_frompyobject(x, radix)) == 0) {
1808 PyErr_Format(PyExc_TypeError, "can't convert %.100s to gf",
1809 x->ob_type->tp_name);
1814 VALERR("gf cannot be negative");
1816 zz = (mp_pyobj *)ty->tp_alloc(ty, 0);
1819 return ((PyObject *)zz);
1822 static long gf_pyhash(PyObject *me)
1824 long i = mp_tolong(MP_X(me));
1825 i ^= 0xc7ecd67c; /* random perturbance */
1831 static PyObject *gf_pyexp(PyObject *x, PyObject *y, PyObject *z)
1833 mp *xx = 0, *yy = 0, *zz = 0;
1837 if ((xx = tomp(x)) == 0 || (yy = tomp(y)) == 0 ||
1838 (z && z != Py_None && (zz = tomp(z)) == 0)) {
1839 mp_drop(xx); mp_drop(yy); mp_drop(zz);
1842 if (!z || z == Py_None) {
1843 if (MP_NEGP(yy)) VALERR("negative exponent");
1844 r = gf_exp(MP_NEW, xx, yy);
1847 if (MP_ZEROP(zz)) ZDIVERR("zero modulus");
1849 if ((xx = gf_modinv_checked(xx, xx, zz)) == 0) goto end;
1850 yy = mp_neg(yy, yy);
1852 gfreduce_create(&gr, zz);
1853 r = gfreduce_exp(&gr, MP_NEW, xx, yy);
1854 gfreduce_destroy(&gr);
1858 mp_drop(xx); mp_drop(yy); mp_drop(zz);
1862 static PyObject *gfmeth_sqr(PyObject *me, PyObject *arg)
1864 if (!PyArg_ParseTuple(arg, ":sqr")) return (0);
1865 return (gf_pywrap(gf_sqr(MP_NEW, MP_X(me))));
1868 static PyObject *gfmeth_gcd(PyObject *me, PyObject *arg)
1871 mp *yy = 0, *zz = MP_NEW;
1873 if (!PyArg_ParseTuple(arg, "O&:gcd", convgf, &yy)) goto end;
1874 gf_gcd(&zz, 0, 0, MP_X(me), yy);
1877 if (yy) MP_DROP(yy);
1881 static PyObject *gfmeth_gcdx(PyObject *me, PyObject *arg)
1884 mp *yy = 0, *zz = MP_NEW, *uu = MP_NEW, *vv = MP_NEW;
1886 if (!PyArg_ParseTuple(arg, "O&:gcdx", convgf, &yy))
1888 gf_gcd(&zz, &uu, &vv, MP_X(me), yy);
1889 z = Py_BuildValue("(NNN)",
1890 gf_pywrap(zz), gf_pywrap(uu), gf_pywrap(vv));
1892 if (yy) MP_DROP(yy);
1896 static PyObject *gfmeth_modinv(PyObject *me, PyObject *arg)
1899 mp *yy = 0, *zz = MP_NEW;
1901 if (!PyArg_ParseTuple(arg, "O&:modinv", convgf, &yy) ||
1902 (zz = gf_modinv_checked(MP_NEW, yy, MP_X(me))) == 0)
1906 if (yy) MP_DROP(yy);
1910 static PyObject *gfmeth_irreduciblep(PyObject *me, PyObject *arg)
1912 if (!PyArg_ParseTuple(arg, ":irreduciblep")) return (0);
1913 return getbool(gf_irreduciblep(MP_X(me)));
1916 static PyObject *gfget_degree(PyObject *me, void *hunoz)
1917 { return (PyInt_FromLong(mp_bits(MP_X(me)) - 1)); }
1919 static PyGetSetDef gf_pygetset[] = {
1920 #define GETSETNAME(op, name) gf##op##_##name
1921 GET (degree, "X.degree -> polynomial degree of X")
1923 #define GETSETNAME(op, name) mp##op##_##name
1924 GET (nbits, "X.nbits -> bit length of X")
1925 GET (noctets, "X.noctets -> octet length of X")
1930 static PyMethodDef gf_pymethods[] = {
1931 #define METHNAME(func) gfmeth_##func
1932 METH (setbit, "X.setbit(N) -> X with bit N set")
1933 METH (clearbit, "X.clearbit(N) -> X with bit N clear")
1934 METH (testbit, "X.testbit(N) -> true/false if bit N set/clear in X")
1935 METH (sqr, "X.sqr() -> X^2")
1936 METH (gcd, "X.gcd(Y) -> gcd(X, Y)")
1938 "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)")
1939 METH (modinv, "X.modinv(Y) -> multiplicative inverse of Y mod X")
1940 METH (irreduciblep, "X.irreduciblep() -> true/false")
1942 #define METHNAME(func) mpmeth_##func
1943 KWMETH(tostring, "X.tostring(radix = 10) -> STR")
1944 KWMETH(storel, "X.storel(len = -1) -> little-endian bytes")
1945 KWMETH(storeb, "X.storeb(len = -1) -> big-endian bytes")
1947 "X.storel2c(len = -1) -> little-endian bytes, two's complement")
1949 "X.storeb2c(len = -1) -> big-endian bytes, two's complement")
1950 METH (tobuf, "X.tobuf() -> buffer format")
1955 static PyNumberMethods gf_pynumber = {
1956 gf_pyadd, /* @nb_add@ */
1957 gf_pysub, /* @nb_subtract@ */
1958 gf_pymul, /* @nb_multiply@ */
1959 0, /* @nb_divide@ */
1960 gf_pymod, /* @nb_remainder@ */
1961 gf_pydivmod, /* @nb_divmod@ */
1962 gf_pyexp, /* @nb_power@ */
1963 mp_pyid, /* @nb_negative@ */
1964 mp_pyid, /* @nb_positive@ */
1965 mp_pyid, /* @nb_absolute@ */
1966 mp_pynonzerop, /* @nb_nonzero@ */
1967 0 /* doesn't make any sense */, /* @nb_invert@ */
1968 gf_pylsl, /* @nb_lshift@ */
1969 gf_pylsr, /* @nb_rshift@ */
1970 gf_pyand, /* @nb_and@ */
1971 gf_pyxor, /* @nb_xor@ */
1972 gf_pyor, /* @nb_or@ */
1973 gf_pycoerce, /* @nb_coerce@ */
1974 mp_pyint, /* @nb_int@ */
1975 mp_pylong, /* @nb_long@ */
1976 0 /* doesn't make any sense */, /* @nb_float@ */
1977 mp_pyoct, /* @nb_oct@ */
1978 mp_pyhex, /* @nb_hex@ */
1980 0, /* @nb_inplace_add@ */
1981 0, /* @nb_inplace_subtract@ */
1982 0, /* @nb_inplace_multiply@ */
1983 0, /* @nb_inplace_divide@ */
1984 0, /* @nb_inplace_remainder@ */
1985 0, /* @nb_inplace_power@ */
1986 0, /* @nb_inplace_lshift@ */
1987 0, /* @nb_inplace_rshift@ */
1988 0, /* @nb_inplace_and@ */
1989 0, /* @nb_inplace_xor@ */
1990 0, /* @nb_inplace_or@ */
1992 gf_pydiv, /* @nb_floor_divide@ */
1993 0, /* @nb_true_divide@ */
1994 0, /* @nb_inplace_floor_divide@ */
1995 0, /* @nb_inplace_true_divide@ */
1998 static PyTypeObject gf_pytype_skel = {
1999 PyObject_HEAD_INIT(0) 0, /* Header */
2000 "catacomb.GF", /* @tp_name@ */
2001 sizeof(mp_pyobj), /* @tp_basicsize@ */
2002 0, /* @tp_itemsize@ */
2004 mp_pydealloc, /* @tp_dealloc@ */
2006 0, /* @tp_getattr@ */
2007 0, /* @tp_setattr@ */
2008 0, /* @tp_compare@ */
2009 gf_pyrepr, /* @tp_repr@ */
2010 &gf_pynumber, /* @tp_as_number@ */
2011 0, /* @tp_as_sequence@ */
2012 0, /* @tp_as_mapping@ */
2013 gf_pyhash, /* @tp_hash@ */
2015 mp_pyhex, /* @tp_str@ */
2016 0, /* @tp_getattro@ */
2017 0, /* @tp_setattro@ */
2018 0, /* @tp_as_buffer@ */
2019 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2020 Py_TPFLAGS_CHECKTYPES |
2021 Py_TPFLAGS_BASETYPE,
2024 "Binary polynomials. Support almost all the standard arithmetic\n\
2027 Constructor gf(X, radix = R) attempts to convert X to a `gf'. If\n\
2028 X is a string, it's read in radix-R form, or we look for a prefix\n\
2029 if R = 0. Other acceptable things are ints and longs.\n\
2031 The name is hopelessly wrong from a technical point of view, but\n\
2032 but it's much easier to type than `p2' or `c2' or whatever.\n\
2036 * Use `//' for division. GFs don't have `/' division.",
2038 0, /* @tp_traverse@ */
2040 gf_pyrichcompare, /* @tp_richcompare@ */
2041 0, /* @tp_weaklistoffset@ */
2043 0, /* @tp_iternext@ */
2044 gf_pymethods, /* @tp_methods@ */
2045 0, /* @tp_members@ */
2046 gf_pygetset, /* @tp_getset@ */
2049 0, /* @tp_descr_get@ */
2050 0, /* @tp_descr_set@ */
2051 0, /* @tp_dictoffset@ */
2053 PyType_GenericAlloc, /* @tp_alloc@ */
2054 gf_pynew, /* @tp_new@ */
2059 static PyObject *meth__GF_fromstring(PyObject *me,
2060 PyObject *arg, PyObject *kw)
2067 mptext_stringctx sc;
2068 char *kwlist[] = { "class", "x", "radix", 0 };
2070 if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|i:fromstring",
2071 kwlist, &me, &p, &len, &r))
2073 if (!good_radix_p(r, 1)) VALERR("radix out of range");
2074 sc.buf = p; sc.lim = p + len;
2075 if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0 || MP_NEGP(zz))
2076 z = Py_BuildValue("(Os#)", Py_None, p, len);
2078 z = Py_BuildValue("(Ns#)", gf_pywrap(zz),
2079 sc.buf, (int)(sc.lim - sc.buf));
2084 /*----- Sparse poly reduction ---------------------------------------------*/
2086 typedef struct gfreduce_pyobj {
2091 #define GFREDUCE_PY(o) (&((gfreduce_pyobj *)(o))->mr)
2093 static PyObject *grmeth_exp(PyObject *me, PyObject *arg)
2096 mp *yy = 0, *zz = 0;
2098 if (!PyArg_ParseTuple(arg, "O&O&:exp", convgf, &yy, convgf, &zz))
2101 if ((yy = gf_modinv_checked(yy, yy, GFREDUCE_PY(me)->p)) == 0) goto end;
2102 zz = mp_neg(zz, zz);
2104 rc = gf_pywrap(gfreduce_exp(GFREDUCE_PY(me), MP_NEW, yy, zz));
2106 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
2110 static PyObject *grmeth_reduce(PyObject *me, PyObject *arg)
2115 if (!PyArg_ParseTuple(arg, "O&:reduce", convgf, &yy)) goto end;
2116 z = gf_pywrap(gfreduce_do(GFREDUCE_PY(me), MP_NEW, yy));
2121 static void gfreduce_pydealloc(PyObject *me)
2123 gfreduce_destroy(GFREDUCE_PY(me));
2127 static PyObject *gfreduce_pynew(PyTypeObject *ty,
2128 PyObject *arg, PyObject *kw)
2130 gfreduce_pyobj *mr = 0;
2132 char *kwlist[] = { "m", 0 };
2135 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", kwlist, convgf, &xx))
2137 if (MP_ZEROP(xx)) ZDIVERR("modulus is zero!");
2138 gfreduce_create(&r, xx);
2139 mr = (gfreduce_pyobj *)ty->tp_alloc(ty, 0);
2142 if (xx) MP_DROP(xx);
2143 return ((PyObject *)mr);
2146 static PyObject *grget_m(PyObject *me, void *hunoz)
2147 { return (gf_pywrap(MP_COPY(GFREDUCE_PY(me)->p))); }
2149 static PyGetSetDef gfreduce_pygetset[] = {
2150 #define GETSETNAME(op, name) gr##op##_##name
2151 GET (m, "R.m -> reduction polynomial")
2156 static PyMethodDef gfreduce_pymethods[] = {
2157 #define METHNAME(name) grmeth_##name
2158 METH (reduce, "R.reduce(X) -> X mod B.m")
2159 METH (exp, "R.exp(X, N) -> X^N mod B.m")
2164 static PyTypeObject *gfreduce_pytype, gfreduce_pytype_skel = {
2165 PyObject_HEAD_INIT(0) 0, /* Header */
2166 "catacomb.GFReduce", /* @tp_name@ */
2167 sizeof(gfreduce_pyobj), /* @tp_basicsize@ */
2168 0, /* @tp_itemsize@ */
2170 gfreduce_pydealloc, /* @tp_dealloc@ */
2172 0, /* @tp_getattr@ */
2173 0, /* @tp_setattr@ */
2174 0, /* @tp_compare@ */
2176 0, /* @tp_as_number@ */
2177 0, /* @tp_as_sequence@ */
2178 0, /* @tp_as_mapping@ */
2182 0, /* @tp_getattro@ */
2183 0, /* @tp_setattro@ */
2184 0, /* @tp_as_buffer@ */
2185 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2186 Py_TPFLAGS_BASETYPE,
2189 "A reduction context for reduction modulo sparse irreducible polynomials.",
2191 0, /* @tp_traverse@ */
2193 0, /* @tp_richcompare@ */
2194 0, /* @tp_weaklistoffset@ */
2196 0, /* @tp_iternext@ */
2197 gfreduce_pymethods, /* @tp_methods@ */
2198 0, /* @tp_members@ */
2199 gfreduce_pygetset, /* @tp_getset@ */
2202 0, /* @tp_descr_get@ */
2203 0, /* @tp_descr_set@ */
2204 0, /* @tp_dictoffset@ */
2206 PyType_GenericAlloc, /* @tp_alloc@ */
2207 gfreduce_pynew, /* @tp_new@ */
2212 /*----- Normal/poly transformation ----------------------------------------*/
2214 typedef struct gfn_pyobj {
2220 static PyTypeObject *gfn_pytype, gfn_pytype_skel;
2222 #define GFN_P(o) (((gfn_pyobj *)(o))->p)
2223 #define GFN_PTON(o) (&((gfn_pyobj *)(o))->pton)
2224 #define GFN_NTOP(o) (&((gfn_pyobj *)(o))->ntop)
2226 static PyObject *gfn_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
2228 mp *p = 0, *beta = 0;
2230 char *kwlist[] = { "p", "beta", 0 };
2232 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", kwlist,
2233 convgf, &p, convgf, &beta))
2235 gg = PyObject_New(gfn_pyobj, ty);
2236 if (gfn_create(p, beta, &gg->ntop, &gg->pton)) {
2239 VALERR("can't invert transformation matrix");
2245 return ((PyObject *)gg);
2248 static PyObject *gfnget_p(PyObject *me, void *hunoz)
2249 { return (gf_pywrap(MP_COPY(GFN_P(me)))); }
2251 static PyObject *gfnget_beta(PyObject *me, void *hunoz)
2253 gfn *n = GFN_NTOP(me);
2254 mp *x = n->r[n->n - 1];
2255 return (gf_pywrap(MP_COPY(x)));
2258 #define XFORMOP(name, NAME) \
2259 static PyObject *gfnmeth_##name(PyObject *me, PyObject *arg) \
2264 if (!PyArg_ParseTuple(arg, "O&:" #name, convgf, &xx)) goto end; \
2265 z = gfn_transform(GFN_##NAME(me), MP_NEW, xx); \
2268 if (!z) return (0); \
2269 return (mp_pywrap(z)); \
2275 static void gfn_pydealloc(PyObject *me)
2277 gfn_destroy(GFN_PTON(me));
2278 gfn_destroy(GFN_NTOP(me));
2282 static PyGetSetDef gfn_pygetset[] = {
2283 #define GETSETNAME(op, name) gfn##op##_##name
2284 GET (p, "X.p -> polynomial basis, as polynomial")
2285 GET (beta, "X.beta -> normal basis element, in poly form")
2290 static PyMethodDef gfn_pymethods[] = {
2291 #define METHNAME(name) gfnmeth_##name
2292 METH (pton, "X.pton(A) -> normal-basis representation of A")
2293 METH (ntop, "X.ntop(A) -> polynomial-basis representation of A")
2298 static PyTypeObject gfn_pytype_skel = {
2299 PyObject_HEAD_INIT(0) 0, /* Header */
2300 "catacomb.GFN", /* @tp_name@ */
2301 sizeof(gfn_pyobj), /* @tp_basicsize@ */
2302 0, /* @tp_itemsize@ */
2304 gfn_pydealloc, /* @tp_dealloc@ */
2306 0, /* @tp_getattr@ */
2307 0, /* @tp_setattr@ */
2308 0, /* @tp_compare@ */
2310 0, /* @tp_as_number@ */
2311 0, /* @tp_as_sequence@ */
2312 0, /* @tp_as_mapping@ */
2316 0, /* @tp_getattro@ */
2317 0, /* @tp_setattro@ */
2318 0, /* @tp_as_buffer@ */
2319 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2320 Py_TPFLAGS_BASETYPE,
2323 "An object for transforming elements of binary fields between polynomial\n\
2324 and normal basis representations.",
2326 0, /* @tp_traverse@ */
2328 0, /* @tp_richcompare@ */
2329 0, /* @tp_weaklistoffset@ */
2331 0, /* @tp_iternext@ */
2332 gfn_pymethods, /* @tp_methods@ */
2333 0, /* @tp_members@ */
2334 gfn_pygetset, /* @tp_getset@ */
2337 0, /* @tp_descr_get@ */
2338 0, /* @tp_descr_set@ */
2339 0, /* @tp_dictoffset@ */
2341 PyType_GenericAlloc, /* @tp_alloc@ */
2342 gfn_pynew, /* @tp_new@ */
2347 /*----- Glue --------------------------------------------------------------*/
2349 static PyMethodDef methods[] = {
2350 #define METHNAME(func) meth_##func
2351 KWMETH(_MP_fromstring, "\
2352 fromstring(STR, radix = 0) -> (X, REST)\n\
2354 Parse STR as a large integer, according to radix. If radix is zero,\n\
2355 read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\
2356 or `R_' for other radix R.")
2357 KWMETH(_GF_fromstring, "\
2358 fromstring(STR, radix = 0) -> (X, REST)\n\
2360 Parse STR as a binary polynomial, according to radix. If radix is zero,\n\
2361 read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\
2362 or `R_' for other radix R.")
2364 loadl(STR) -> X: read little-endian bytes")
2366 loadb(STR) -> X: read big-endian bytes")
2367 METH (_MP_loadl2c, "\
2368 loadl2c(STR) -> X: read little-endian bytes, two's complement")
2369 METH (_MP_loadb2c, "\
2370 loadb2c(STR) -> X: read big-endian bytes, two's complement")
2371 METH (_MP_frombuf, "\
2372 frombuf(STR) -> (X, REST): read buffer format")
2374 loadl(STR) -> X: read little-endian bytes")
2376 loadb(STR) -> X: read big-endian bytes")
2377 METH (_GF_frombuf, "\
2378 frombuf(STR) -> (X, REST): read buffer format")
2383 void mp_pyinit(void)
2387 INITTYPE(mpmul, root);
2388 INITTYPE(mpmont, root);
2389 INITTYPE(mpbarrett, root);
2390 INITTYPE(mpreduce, root);
2391 INITTYPE(mpcrt, root);
2392 INITTYPE(gfreduce, root);
2393 INITTYPE(gfn, root);
2394 addmethods(methods);
2397 void mp_pyinsert(PyObject *mod)
2399 INSERT("MP", mp_pytype);
2400 INSERT("MPMul", mpmul_pytype);
2401 INSERT("MPMont", mpmont_pytype);
2402 INSERT("MPBarrett", mpbarrett_pytype);
2403 INSERT("MPReduce", mpreduce_pytype);
2404 INSERT("MPCRT", mpcrt_pytype);
2405 INSERT("GF", gf_pytype);
2406 INSERT("GFReduce", gfreduce_pytype);
2407 INSERT("GFN", gfn_pytype);
2410 /*----- That's all, folks -------------------------------------------------*/