3 * Multiprecision arithmetic
5 * (c) 2004 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 /*----- General utilities -------------------------------------------------*/
33 PyTypeObject *mp_pytype = 0;
34 PyTypeObject *gf_pytype = 0;
37 # define PyLong_SHIFT SHIFT
41 # define PyLong_MASK MASK
44 STATIC_ASSERT(MPW_BITS >= PyLong_SHIFT,
45 "Catacomb's limbs are now narrower than than Python's!");
47 mp *mp_frompylong(PyObject *obj)
50 PyLongObject *l = (PyLongObject *)obj;
61 bits = (unsigned long)sz * PyLong_SHIFT;
62 w = (bits + MPW_BITS - 1)/MPW_BITS;
63 x = mp_new(w, Py_SIZE(l) < 0 ? MP_NEG : 0);
65 for (i = 0; i < sz; i++) {
66 r |= (mpd)l->ob_digit[i] << b;
68 while (b >= MPW_BITS) {
83 PyObject *mp_topylong(mp *x)
85 unsigned long bits = mp_bits(x);
86 int sz = (bits + PyLong_SHIFT - 1)/PyLong_SHIFT;
87 PyLongObject *l = _PyLong_New(sz);
93 while (i < sz && p < x->vl) {
96 while (i < sz && b >= PyLong_SHIFT) {
97 l->ob_digit[i++] = r & PyLong_MASK;
102 while (i < sz && r) {
103 l->ob_digit[i++] = r & PyLong_MASK;
106 Py_SIZE(l) = (x->f & MP_NEG) ? -sz : sz;
107 return ((PyObject *)l);
110 mp *mp_frompyobject(PyObject *o, int radix)
118 TEXT_PTRLEN(o, sc.buf, sz); sc.lim = sc.buf + sz;
119 x = mp_read(MP_NEW, radix, &mptext_stringops, &sc);
121 if (sc.buf < sc.lim) { MP_DROP(x); return (0); }
124 if ((x = tomp(o)) != 0)
129 PyObject *mp_topystring(mp *x, int radix, const char *xpre,
130 const char *pre, const char *post)
132 int len = mptext_len(x, radix) + 1;
135 size_t xprelen = xpre ? strlen(xpre) : 0;
136 size_t prelen = pre ? strlen(pre) : 0;
137 size_t postlen = post ? strlen(post) : 0;
140 TEXT_PREPAREWRITE(o, p, len + 1 + xprelen + prelen + postlen);
142 if (xpre) { memcpy(sc.buf, xpre, xprelen); sc.buf += xprelen; }
143 if (MP_NEGP(x)) { *sc.buf++ = '-'; x = mp_neg(x, x); }
144 if (pre) { memcpy(sc.buf, pre, prelen); sc.buf += prelen; }
145 sc.lim = sc.buf + len;
146 mp_write(x, radix, &mptext_stringops, &sc);
147 if (post) { memcpy(sc.buf, post, postlen); sc.buf += postlen; }
149 TEXT_DONEWRITE(o, sc.buf - p);
153 static int good_radix_p(int r, int readp)
155 return ((r >= -255 && r <= -2) ||
157 (r >= 2 && r <= 62));
160 PyObject *mp_pywrap(mp *x)
162 mp_pyobj *z = PyObject_New(mp_pyobj, mp_pytype);
164 return ((PyObject *)z);
167 PyObject *gf_pywrap(mp *x)
169 mp_pyobj *z = PyObject_New(mp_pyobj, gf_pytype);
171 return ((PyObject *)z);
174 int mp_tolong_checked(mp *x, long *l, int must)
176 static mp *longmin = 0, *longmax = 0;
180 longmin = mp_fromlong(MP_NEW, LONG_MIN);
181 longmax = mp_fromlong(MP_NEW, LONG_MAX);
183 if (MP_CMP(x, <, longmin) || MP_CMP(x, >, longmax)) {
184 if (must) VALERR("mp out of range for int");
193 /*----- Python interface --------------------------------------------------*/
195 static void mp_pydealloc(PyObject *o)
201 static PyObject *mp_pyrepr(PyObject *o)
202 { return mp_topystring(MP_X(o), 10, "MP(", 0, "L)"); }
204 static PyObject *mp_pystr(PyObject *o)
205 { return mp_topystring(MP_X(o), 10, 0, 0, 0); }
207 mp *tomp(PyObject *o)
214 else if (MP_PYCHECK(o) || GF_PYCHECK(o))
215 return (MP_COPY(MP_X(o)));
216 else if (FE_PYCHECK(o))
217 return (F_OUT(FE_F(o), MP_NEW, FE_X(o)));
218 else if (PFILT_PYCHECK(o))
219 return (MP_COPY(PFILT_F(o)->m));
220 else if (ECPT_PYCHECK(o)) {
222 if (EC_ATINF(ECPT_P(o))) return (0);
227 } else if (GE_PYCHECK(o)) {
228 if ((x = G_TOINT(GE_G(o), MP_NEW, GE_X(o))) == 0)
231 } else if (PyInt_Check(o))
232 return (mp_fromlong(MP_NEW, PyInt_AS_LONG(o)));
233 else if ((l = PyNumber_Long(o)) != 0) {
234 x = mp_frompylong(l);
243 mp *getmp(PyObject *o)
247 if ((x = tomp(o)) == 0) {
248 PyErr_Format(PyExc_TypeError, "can't convert %.100s to mp",
249 Py_TYPE(o)->tp_name);
254 int convmp(PyObject *o, void *p)
257 if ((x = getmp(o)) == 0) return (0);
262 mp *getgf(PyObject *o)
266 if ((x = tomp(o)) == 0) {
267 PyErr_Format(PyExc_TypeError, "can't convert %.100s to gf",
268 Py_TYPE(o)->tp_name);
273 int convgf(PyObject *o, void *p)
276 if ((x = getgf(o)) == 0) return (0);
281 static mp *implicitmp(PyObject *o)
292 static mp *implicitgf(PyObject *o)
303 static int mpbinop(PyObject *x, PyObject *y, mp **xx, mp **yy)
305 if ((*xx = implicitmp(x)) == 0)
307 if ((*yy = implicitmp(y)) == 0) {
314 static int gfbinop(PyObject *x, PyObject *y, mp **xx, mp **yy)
316 if ((*xx = implicitgf(x)) == 0)
318 if ((*yy = implicitgf(y)) == 0) {
325 #define gf_and mp_and
327 #define gf_xor mp_xor
328 #define BINOP(pre, name) \
329 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
331 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
332 zz = pre##_##name(MP_NEW, xx, yy); \
333 MP_DROP(xx); MP_DROP(yy); \
334 return (pre##_pywrap(zz)); \
350 static mp *mp_abs(mp *d, mp *x)
353 return (mp_neg(d, x));
359 #define UNOP(pre, name) \
360 static PyObject *pre##_py##name(PyObject *x) \
361 { return mp_pywrap(pre##_##name(MP_NEW, MP_X(x))); }
367 static PyObject *mp_pyid(PyObject *x) { RETURN_OBJ(x); }
369 #define gf_lsr mp_lsr
370 #define SHIFTOP(pre, name, rname) \
371 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
375 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
376 if (mp_tolong_checked(yy, &n, 1)) goto end; \
378 z = pre##_pywrap(mp_##rname(MP_NEW, xx, -n)); \
380 z = pre##_pywrap(mp_##name(MP_NEW, xx, n)); \
382 MP_DROP(xx); MP_DROP(yy); \
385 SHIFTOP(mp, lsl2c, lsr2c)
386 SHIFTOP(mp, lsr2c, lsl2c)
387 SHIFTOP(gf, lsl, lsr)
388 SHIFTOP(gf, lsr, lsl)
391 #define DIVOP(pre, name, qq, rr, gather) \
392 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
395 INIT_##qq(q) INIT_##rr(r) \
396 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
398 ZDIVERR("division by zero"); \
399 pre##_div(ARG_##qq(q), ARG_##rr(r), xx, yy); \
402 MP_DROP(xx); MP_DROP(yy); \
405 #define INIT_YES(p) mp *p = MP_NEW;
407 #define ARG_YES(p) &p
409 DIVOP(mp, divmod, YES, YES,
410 Py_BuildValue("(NN)", mp_pywrap(q), mp_pywrap(r)))
411 DIVOP(mp, div, YES, NO, mp_pywrap(q))
412 DIVOP(mp, mod, NO, YES, mp_pywrap(r))
413 DIVOP(gf, divmod, YES, YES,
414 Py_BuildValue("(NN)", gf_pywrap(q), gf_pywrap(r)))
415 DIVOP(gf, div, YES, NO, gf_pywrap(q))
416 DIVOP(gf, mod, NO, YES, gf_pywrap(r))
423 static mp *mp_modinv_checked(mp *d, mp *x, mp *p)
426 mp_gcd(&g, 0, &d, p, x);
427 if (!MP_EQ(g, MP_ONE)) {
428 MP_DROP(g); MP_DROP(d);
429 PyErr_SetString(PyExc_ZeroDivisionError, "no modular inverse");
432 MP_DROP(g); return (d);
435 static PyObject *mp_pyexp(PyObject *x, PyObject *y, PyObject *z)
437 mp *xx = 0, *yy = 0, *zz = 0;
441 if ((xx = implicitmp(x)) == 0 || (yy = implicitmp(y)) == 0 ||
442 (z && z != Py_None && (zz = tomp(z)) == 0)) {
443 mp_drop(xx); mp_drop(yy); mp_drop(zz);
446 if (!z || z == Py_None) {
447 if (MP_NEGP(yy)) VALERR("negative exponent");
448 r = mp_exp(MP_NEW, xx, yy);
450 if (!MP_POSP(zz)) VALERR("modulus must be positive");
452 if ((xx = mp_modinv_checked(xx, xx, zz)) == 0) goto end;
457 mpmont_create(&mm, zz);
458 r = mpmont_exp(&mm, MP_NEW, xx, yy);
462 mpbarrett_create(&mb, zz);
463 r = mpbarrett_exp(&mb, MP_NEW, xx, yy);
464 mpbarrett_destroy(&mb);
469 mp_drop(xx); mp_drop(yy); mp_drop(zz);
473 static mp *gf_modinv_checked(mp *d, mp *x, mp *p)
476 gf_gcd(&g, 0, &d, p, x);
477 if (!MP_EQ(g, MP_ONE)) {
478 MP_DROP(g); MP_DROP(d);
479 PyErr_SetString(PyExc_ZeroDivisionError, "no modular inverse");
482 MP_DROP(g); return (d);
485 #define BASEOP(name, radix, pre) \
486 static PyObject *mp_py##name(PyObject *x) \
487 { return mp_topystring(MP_X(x), radix, 0, pre, 0); }
489 BASEOP(hex, 16, "0x");
492 static int mp_pynonzerop(PyObject *x) { return !MP_ZEROP(MP_X(x)); }
494 static PyObject *mp_pyint(PyObject *x)
497 if (!mp_tolong_checked(MP_X(x), &l, 0)) return (PyInt_FromLong(l));
498 else return mp_topylong(MP_X(x));
500 static PyObject *mp_pylong(PyObject *x)
501 { return (mp_topylong(MP_X(x))); }
502 static PyObject *mp_pyfloat(PyObject *x)
504 PyObject *l = mp_topylong(MP_X(x));
505 double f = PyLong_AsDouble(l);
507 return (PyFloat_FromDouble(f));
510 #define COERCE(pre, PRE) \
511 static int pre##_pycoerce(PyObject **x, PyObject **y) \
515 if (PRE##_PYCHECK(*y)) { \
516 Py_INCREF(*x); Py_INCREF(*y); \
519 if ((z = tomp(*y)) != 0) { \
521 *y = pre##_pywrap(z); \
530 static int mp_pycompare(PyObject *x, PyObject *y)
531 { return mp_cmp(MP_X(x), MP_X(y)); }
533 static PyObject *mp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
539 static const char *const kwlist[] = { "x", "radix", 0 };
541 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|i:new", KWLIST, &x, &radix))
543 if (MP_PYCHECK(x)) RETURN_OBJ(x);
544 if (!good_radix_p(radix, 1)) VALERR("bad radix");
545 if ((z = mp_frompyobject(x, radix)) == 0) {
546 PyErr_Format(PyExc_TypeError, "can't convert %.100s to mp",
547 Py_TYPE(x)->tp_name);
550 zz = (mp_pyobj *)ty->tp_alloc(ty, 0);
553 return ((PyObject *)zz);
556 Py_hash_t mphash(mp *x)
558 PyObject *l = mp_topylong(x);
559 Py_hash_t h = PyObject_Hash(l);
560 Py_DECREF(l); return (h);
563 static Py_hash_t mp_pyhash(PyObject *me) { return (mphash(MP_X(me))); }
565 static PyObject *mpmeth_jacobi(PyObject *me, PyObject *arg)
570 if (!PyArg_ParseTuple(arg, "O&:jacobi", convmp, &y)) goto end;
571 z = PyInt_FromLong(mp_jacobi(y, MP_X(me)));
577 #define BITOP(pre, name, c) \
578 static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \
581 if (!PyArg_ParseTuple(arg, "O&:" #name, convulong, &i)) return (0); \
582 return (pre##_pywrap(mp_##name##c(MP_NEW, MP_X(me), i))); \
584 BITOP(mp, setbit, 2c);
585 BITOP(mp, clearbit, 2c);
587 BITOP(gf, clearbit, );
590 static PyObject *mpmeth_testbit(PyObject *me, PyObject *arg)
593 if (!PyArg_ParseTuple(arg, "O&:testbit", convulong, &i)) return (0);
594 return (getbool(mp_testbit2c(MP_X(me), i)));
597 static PyObject *gfmeth_testbit(PyObject *me, PyObject *arg)
600 if (!PyArg_ParseTuple(arg, "O&:testbit", convulong, &i)) return (0);
601 return (getbool(mp_testbit(MP_X(me), i)));
604 static PyObject *mpmeth_odd(PyObject *me)
609 t = mp_odd(MP_NEW, MP_X(me), &s);
610 return (Py_BuildValue("(lN)", (long)s, mp_pywrap(t)));
613 static PyObject *mpmeth_sqr(PyObject *me)
614 { return (mp_pywrap(mp_sqr(MP_NEW, MP_X(me)))); }
616 static PyObject *mpmeth_sqrt(PyObject *me)
618 if (MP_NEGP(MP_X(me))) VALERR("negative root");
619 return (mp_pywrap(mp_sqrt(MP_NEW, MP_X(me))));
624 static PyObject *mpmeth_gcd(PyObject *me, PyObject *arg)
626 mp *y = 0, *zz = MP_NEW;
629 if (!PyArg_ParseTuple(arg, "O&:gcd", convmp, &y)) goto end;
630 mp_gcd(&zz, 0, 0, MP_X(me), y);
637 static PyObject *mpmeth_gcdx(PyObject *me, PyObject *arg)
640 mp *yy = 0, *zz = MP_NEW, *uu = MP_NEW, *vv = MP_NEW;
642 if (!PyArg_ParseTuple(arg, "O&:gcdx", convmp, &yy)) goto end;
643 mp_gcd(&zz, &uu, &vv, MP_X(me), yy);
644 z = Py_BuildValue("(NNN)",
645 mp_pywrap(zz), mp_pywrap(uu), mp_pywrap(vv));
651 static PyObject *mpmeth_modinv(PyObject *me, PyObject *arg)
654 mp *yy = 0, *zz = MP_NEW;
656 if (!PyArg_ParseTuple(arg, "O&:modinv", convmp, &yy) ||
657 (zz = mp_modinv_checked(MP_NEW, yy, MP_X(me))) == 0)
665 static PyObject *mpmeth_tostring(PyObject *me, PyObject *arg, PyObject *kw)
668 static const char *const kwlist[] = { "radix", 0 };
669 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|i:tostring", KWLIST, &radix))
671 if (!good_radix_p(radix, 0)) VALERR("bad radix");
672 return (mp_topystring(MP_X(me), radix, 0, 0, 0));
677 static PyObject *mpmeth_modsqrt(PyObject *me, PyObject *arg)
680 mp *yy = 0, *zz = MP_NEW;
682 if (!PyArg_ParseTuple(arg, "O&:modsqrt", convmp, &yy)) goto end;
683 if ((zz = mp_modsqrt(MP_NEW, yy, MP_X(me))) == 0)
684 VALERR("no modular square root");
691 static PyObject *mpmeth_leastcongruent(PyObject *me, PyObject *arg)
696 if (!PyArg_ParseTuple(arg, "O&O&:leastcongruent", convmp, &b, convmp, &m))
698 z = mp_leastcongruent(MP_NEW, b, MP_X(me), m);
704 #define STOREOP(name, c) \
705 static PyObject *mpmeth_##name(PyObject *me, \
706 PyObject *arg, PyObject *kw) \
709 static const char *const kwlist[] = { "len", 0 }; \
712 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|l:" #name, \
716 len = mp_octets##c(MP_X(me)); \
719 rc = bytestring_pywrap(0, len); \
720 mp_##name(MP_X(me), BIN_PTR(rc), len); \
726 STOREOP(storel2c, 2c)
727 STOREOP(storeb2c, 2c)
731 static PyObject *ty##meth_frombuf(PyObject *me, PyObject *arg) \
738 if (!PyArg_ParseTuple(arg, "O&:frombuf", convbin, &in)) goto end; \
739 buf_init(&b, (/*unconst*/ void *)in.p, in.sz); \
740 if ((x = buf_getmp(&b)) == 0) VALERR("malformed data"); \
741 rc = Py_BuildValue("(NN)", ty##_pywrap(x), \
742 bytestring_pywrapbuf(&b)); \
750 static PyObject *mpmeth_tobuf(PyObject *me)
758 n = mp_octets(x) + 3;
759 rc = bytestring_pywrap(0, n);
760 buf_init(&b, BIN_PTR(rc), n);
763 BIN_SETLEN(rc, BLEN(&b));
767 static PyObject *mpmeth_primep(PyObject *me, PyObject *arg, PyObject *kw)
769 grand *r = &rand_global;
770 static const char *const kwlist[] = { "rng", 0 };
773 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&", KWLIST, convgrand, &r))
775 rc = getbool(pgen_primep(MP_X(me), r));
780 static PyObject *mpmeth_fromstring(PyObject *me,
781 PyObject *arg, PyObject *kw)
789 static const char *const kwlist[] = { "x", "radix", 0 };
791 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|i:fromstring", KWLIST,
794 if (!good_radix_p(r, 1)) VALERR("bad radix");
795 sc.buf = p; sc.lim = p + len;
796 if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0)
797 VALERR("bad integer");
798 z = Py_BuildValue("(Ns#)", mp_pywrap(zz),
799 sc.buf, (Py_ssize_t)(sc.lim - sc.buf));
804 static PyObject *mpmeth_factorial(PyObject *me, PyObject *arg)
808 if (!PyArg_ParseTuple(arg, "O&:factorial", convulong, &i)) return (0);
813 static PyObject *mpmeth_fibonacci(PyObject *me, PyObject *arg)
817 if (!PyArg_ParseTuple(arg, "l:fibonacci", &i)) return (0);
822 #define LOADOP(pre, name) \
823 static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \
826 if (!PyArg_ParseTuple(arg, "O&:" #name, convbin, &in)) return (0); \
827 return (pre##_pywrap(mp_##name(MP_NEW, in.p, in.sz))); \
837 static PyObject *mpget_nbits(PyObject *me, void *hunoz)
838 { return (PyInt_FromLong(mp_bits(MP_X(me)))); }
840 static PyObject *mpget_noctets(PyObject *me, void *hunoz)
841 { return (PyInt_FromLong(mp_octets(MP_X(me)))); }
843 static PyObject *mpget_noctets2c(PyObject *me, void *hunoz)
844 { return (PyInt_FromLong(mp_octets2c(MP_X(me)))); }
846 static const PyGetSetDef mp_pygetset[] = {
847 #define GETSETNAME(op, func) mp##op##_##func
848 GET (nbits, "X.nbits -> bit length of X")
849 GET (noctets, "X.noctets -> octet length of X")
850 GET (noctets2c, "X.noctets2c -> two's complement octet length of X")
855 static const PyMethodDef mp_pymethods[] = {
856 #define METHNAME(func) mpmeth_##func
857 METH (jacobi, "X.jacobi(Y) -> Jacobi symbol (Y|X) (NB inversion!)")
858 METH (setbit, "X.setbit(N) -> X with bit N set")
859 METH (clearbit, "X.clearbit(N) -> X with bit N clear")
860 METH (testbit, "X.testbit(N) -> true/false if bit N set/clear in X")
861 NAMETH(odd, "X.odd() -> S, T where X = 2^S T with T odd")
862 NAMETH(sqr, "X.sqr() -> X^2")
863 NAMETH(sqrt, "X.sqrt() -> largest integer <= sqrt(X)")
864 METH (gcd, "X.gcd(Y) -> gcd(X, Y)")
865 METH (gcdx, "X.gcdx(Y) -> (gcd(X, Y), U, V) "
866 "with X U + Y V = gcd(X, Y)")
867 METH (modinv, "X.modinv(Y) -> multiplicative inverse of Y mod X")
868 METH (modsqrt, "X.modsqrt(Y) -> square root of Y mod X, if X prime")
869 METH (leastcongruent, "X.leastcongruent(B, M) -> "
870 "smallest Z >= B with Z == X (mod M)")
871 KWMETH(primep, "X.primep([rng = rand]) -> X is prime?")
872 KWMETH(tostring, "X.tostring([radix = 10]) -> STR")
873 KWMETH(storel, "X.storel([len = -1]) -> little-endian bytes")
874 KWMETH(storeb, "X.storeb([len = -1]) -> big-endian bytes")
875 KWMETH(storel2c, "X.storel2c([len = -1]) -> "
876 "little-endian bytes, two's complement")
877 KWMETH(storeb2c, "X.storeb2c([len = -1]) -> "
878 "big-endian bytes, two's complement")
879 NAMETH(tobuf, "X.tobuf() -> buffer format")
880 KWSMTH(fromstring, "fromstring(STR, [radix = 0]) -> (X, REST)\n"
881 " Parse STR as a large integer, according to RADIX. If RADIX is\n"
882 " zero, read a prefix from STR to decide radix: allow `0b' for binary,\n"
883 " `0' or `0o' for octal, `0x' for hex, or `R_' for other radix R.")
884 SMTH (factorial, "factorial(I) -> I!: compute factorial")
885 SMTH (fibonacci, "fibonacci(I) -> F(I): compute Fibonacci number")
886 SMTH (loadl, "loadl(STR) -> X: read little-endian bytes")
887 SMTH (loadb, "loadb(STR) -> X: read big-endian bytes")
888 SMTH (loadl2c, "loadl2c(STR) -> X: "
889 "read little-endian bytes, two's complement")
890 SMTH (loadb2c, "loadb2c(STR) -> X: "
891 "read big-endian bytes, two's complement")
892 SMTH (frombuf, "frombuf(STR) -> (X, REST): read buffer format")
897 static const PyNumberMethods mp_pynumber = {
898 mp_pyadd, /* @nb_add@ */
899 mp_pysub, /* @nb_subtract@ */
900 mp_pymul, /* @nb_multiply@ */
902 mp_pymod, /* @nb_remainder@ */
903 mp_pydivmod, /* @nb_divmod@ */
904 mp_pyexp, /* @nb_power@ */
905 mp_pyneg, /* @nb_negative@ */
906 mp_pyid, /* @nb_positive@ */
907 mp_pyabs, /* @nb_absolute@ */
908 mp_pynonzerop, /* @nb_nonzero@ */
909 mp_pynot2c, /* @nb_invert@ */
910 mp_pylsl2c, /* @nb_lshift@ */
911 mp_pylsr2c, /* @nb_rshift@ */
912 mp_pyand2c, /* @nb_and@ */
913 mp_pyxor2c, /* @nb_xor@ */
914 mp_pyor2c, /* @nb_or@ */
915 mp_pycoerce, /* @nb_coerce@ */
916 mp_pyint, /* @nb_int@ */
917 mp_pylong, /* @nb_long@ */
918 mp_pyfloat, /* @nb_float@ */
919 mp_pyoct, /* @nb_oct@ */
920 mp_pyhex, /* @nb_hex@ */
922 0, /* @nb_inplace_add@ */
923 0, /* @nb_inplace_subtract@ */
924 0, /* @nb_inplace_multiply@ */
925 0, /* @nb_inplace_divide@ */
926 0, /* @nb_inplace_remainder@ */
927 0, /* @nb_inplace_power@ */
928 0, /* @nb_inplace_lshift@ */
929 0, /* @nb_inplace_rshift@ */
930 0, /* @nb_inplace_and@ */
931 0, /* @nb_inplace_xor@ */
932 0, /* @nb_inplace_or@ */
934 mp_pydiv, /* @nb_floor_divide@ */
935 0, /* @nb_true_divide@ */
936 0, /* @nb_inplace_floor_divide@ */
937 0, /* @nb_inplace_true_divide@ */
940 static const PyTypeObject mp_pytype_skel = {
941 PyVarObject_HEAD_INIT(0, 0) /* Header */
942 "MP", /* @tp_name@ */
943 sizeof(mp_pyobj), /* @tp_basicsize@ */
944 0, /* @tp_itemsize@ */
946 mp_pydealloc, /* @tp_dealloc@ */
948 0, /* @tp_getattr@ */
949 0, /* @tp_setattr@ */
950 mp_pycompare, /* @tp_compare@ */
951 mp_pyrepr, /* @tp_repr@ */
952 PYNUMBER(mp), /* @tp_as_number@ */
953 0, /* @tp_as_sequence@ */
954 0, /* @tp_as_mapping@ */
955 mp_pyhash, /* @tp_hash@ */
957 mp_pystr, /* @tp_str@ */
958 0, /* @tp_getattro@ */
959 0, /* @tp_setattro@ */
960 0, /* @tp_as_buffer@ */
961 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
962 Py_TPFLAGS_CHECKTYPES |
966 "Multiprecision integers, similar to `long' but more efficient and\n"
967 "versatile. Support all the standard arithmetic operations, with\n"
968 "implicit conversions from `PrimeFilter', and other objects which\n"
969 "convert to `long'.\n"
971 "Constructor MP(X, [radix = R]) attempts to convert X to an `MP'. If\n"
972 "X is a string, it's read in radix-R form, or we look for a prefix\n"
973 "if R = 0. Other acceptable things are field elements, elliptic curve\n"
974 "points, group elements, Python `int' and `long' objects, and anything\n"
975 "with an integer conversion.\n"
979 " * Use `//' for integer division: `/' gives exact rational division.",
981 0, /* @tp_traverse@ */
983 0, /* @tp_richcompare@ */
984 0, /* @tp_weaklistoffset@ */
986 0, /* @tp_iternext@ */
987 PYMETHODS(mp), /* @tp_methods@ */
988 0, /* @tp_members@ */
989 PYGETSET(mp), /* @tp_getset@ */
992 0, /* @tp_descr_get@ */
993 0, /* @tp_descr_set@ */
994 0, /* @tp_dictoffset@ */
996 PyType_GenericAlloc, /* @tp_alloc@ */
997 mp_pynew, /* @tp_new@ */
1002 /*----- Products of small integers ----------------------------------------*/
1004 static PyTypeObject *mpmul_pytype;
1006 typedef struct mpmul_pyobj {
1012 #define MPMUL_LIVEP(o) (((mpmul_pyobj *)(o))->livep)
1013 #define MPMUL_PY(o) (&((mpmul_pyobj *)(o))->mm)
1015 static void mpmul_pydealloc(PyObject *me)
1017 if (MPMUL_LIVEP(me))
1018 mp_drop(mpmul_done(MPMUL_PY(me)));
1022 static PyObject *mmmeth_factor(PyObject *me, PyObject *arg)
1027 if (!MPMUL_LIVEP(me)) VALERR("MPMul object invalid");
1028 if (PyTuple_GET_SIZE(arg) != 1)
1029 i = PyObject_GetIter(arg);
1031 if ((q = PyTuple_GET_ITEM(arg, 0)) == 0) goto end;
1032 if ((i = PyObject_GetIter(q)) == 0) {
1033 PyErr_Clear(); /* that's ok */
1034 i = PyObject_GetIter(arg);
1038 while ((q = PyIter_Next(i)) != 0) {
1039 x = getmp(q); Py_DECREF(q); if (!x) {
1043 mpmul_add(MPMUL_PY(me), x);
1052 static PyObject *mmmeth_done(PyObject *me)
1056 if (!MPMUL_LIVEP(me)) VALERR("MPMul object invalid");
1057 x = mpmul_done(MPMUL_PY(me));
1058 MPMUL_LIVEP(me) = 0;
1059 return (mp_pywrap(x));
1064 static PyObject *mpmul_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1068 if (kw) TYERR("keyword arguments not allowed here");
1069 mm = (mpmul_pyobj *)ty->tp_alloc(ty, 0);
1070 mpmul_init(&mm->mm);
1072 if (mmmeth_factor((PyObject *)mm, arg) == 0) {
1076 return ((PyObject *)mm);
1081 static PyObject *mmget_livep(PyObject *me, void *hunoz)
1082 { return (getbool(MPMUL_LIVEP(me))); }
1084 static const PyGetSetDef mpmul_pygetset[] = {
1085 #define GETSETNAME(op, name) mm##op##_##name
1086 GET (livep, "MM.livep -> flag: object still valid?")
1091 static const PyMethodDef mpmul_pymethods[] = {
1092 #define METHNAME(name) mmmeth_##name
1093 METH (factor, "MM.factor(ITERABLE) or MM.factor(I, ...)")
1094 NAMETH(done, "MM.done() -> PRODUCT")
1099 static const PyTypeObject mpmul_pytype_skel = {
1100 PyVarObject_HEAD_INIT(0, 0) /* Header */
1101 "MPMul", /* @tp_name@ */
1102 sizeof(mpmul_pyobj), /* @tp_basicsize@ */
1103 0, /* @tp_itemsize@ */
1105 mpmul_pydealloc, /* @tp_dealloc@ */
1107 0, /* @tp_getattr@ */
1108 0, /* @tp_setattr@ */
1109 0, /* @tp_compare@ */
1111 0, /* @tp_as_number@ */
1112 0, /* @tp_as_sequence@ */
1113 0, /* @tp_as_mapping@ */
1117 0, /* @tp_getattro@ */
1118 0, /* @tp_setattro@ */
1119 0, /* @tp_as_buffer@ */
1120 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1121 Py_TPFLAGS_BASETYPE,
1124 "MPMul(N_0, N_1, ....): an object for multiplying many small integers.",
1126 0, /* @tp_traverse@ */
1128 0, /* @tp_richcompare@ */
1129 0, /* @tp_weaklistoffset@ */
1131 0, /* @tp_iternext@ */
1132 PYMETHODS(mpmul), /* @tp_methods@ */
1133 0, /* @tp_members@ */
1134 PYGETSET(mpmul), /* @tp_getset@ */
1137 0, /* @tp_descr_get@ */
1138 0, /* @tp_descr_set@ */
1139 0, /* @tp_dictoffset@ */
1141 PyType_GenericAlloc, /* @tp_alloc@ */
1142 mpmul_pynew, /* @tp_new@ */
1147 /*----- Montgomery reduction ----------------------------------------------*/
1149 static PyTypeObject *mpmont_pytype;
1151 typedef struct mpmont_pyobj {
1156 #define MPMONT_PY(o) (&((mpmont_pyobj *)(o))->mm)
1158 static PyObject *mmmeth_int(PyObject *me, PyObject *arg)
1162 mpmont *mm = MPMONT_PY(me);
1164 if (!PyArg_ParseTuple(arg, "O&:in", convmp, &yy))
1166 mp_div(0, &yy, yy, mm->m);
1167 z = mp_pywrap(mpmont_mul(mm, MP_NEW, yy, mm->r2));
1169 if (yy) MP_DROP(yy);
1173 static PyObject *mmmeth_mul(PyObject *me, PyObject *arg)
1176 mp *yy = 0, *zz = 0;
1178 if (!PyArg_ParseTuple(arg, "O&O&:mul", convmp, &yy, convmp, &zz))
1180 rc = mp_pywrap(mpmont_mul(MPMONT_PY(me), MP_NEW, yy, zz));
1182 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
1186 static PyObject *mmmeth_exp(PyObject *me, PyObject *arg)
1189 mp *yy = 0, *zz = 0;
1191 if (!PyArg_ParseTuple(arg, "O&O&:exp", convmp, &yy, convmp, &zz))
1194 if ((yy = mp_modinv_checked(yy, yy, MPMONT_PY(me)->m)) == 0) goto end;
1195 zz = mp_neg(zz, zz);
1197 rc = mp_pywrap(mpmont_exp(MPMONT_PY(me), MP_NEW, yy, zz));
1199 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
1203 static PyObject *mmmeth_expr(PyObject *me, PyObject *arg)
1206 mp *yy = 0, *zz = 0;
1208 if (!PyArg_ParseTuple(arg, "O&O&:expr", convmp, &yy, convmp, &zz))
1211 yy = mpmont_reduce(MPMONT_PY(me), yy, yy);
1212 if ((yy = mp_modinv_checked(yy, yy, MPMONT_PY(me)->m)) == 0) goto end;
1213 yy = mpmont_mul(MPMONT_PY(me), yy, yy, MPMONT_PY(me)->r2);
1214 zz = mp_neg(zz, zz);
1216 rc = mp_pywrap(mpmont_expr(MPMONT_PY(me), MP_NEW, yy, zz));
1218 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
1222 static PyObject *mm_mexpr_id(PyObject *me)
1223 { return mp_pywrap(MP_COPY(MPMONT_PY(me)->r)); }
1225 static int mm_mexpr_fill(void *p, PyObject *me, PyObject *x, PyObject *y)
1227 mp *xx = 0, *yy = 0;
1228 mp_expfactor *f = p;
1229 mpmont *mm = MPMONT_PY(me);
1231 if ((xx = getmp(x)) == 0 || (yy = getmp(y)) == 0)
1234 xx = mpmont_reduce(mm, xx, xx);
1235 if ((xx = mp_modinv_checked(xx, xx, yy)) == 0)
1237 xx = mpmont_mul(mm, xx, xx, mm->r2);
1238 yy = mp_neg(yy, yy);
1245 mp_drop(xx); mp_drop(yy);
1249 static PyObject *mm_mexpr(PyObject *me, void *v, int n)
1250 { return mp_pywrap(mpmont_mexpr(MPMONT_PY(me), MP_NEW, v, n)); }
1252 static void mp_mexp_drop(void *p)
1254 mp_expfactor *f = p;
1259 static PyObject *mmmeth_mexpr(PyObject *me, PyObject *arg)
1261 return mexp_common(me, arg, sizeof(mp_expfactor),
1262 mm_mexpr_id, mm_mexpr_fill, mm_mexpr, mp_mexp_drop);
1265 static PyObject *mp_mexp_id(PyObject *me)
1266 { return mp_pywrap(MP_ONE); }
1268 static int mp_mexp_fill(void *p, PyObject *me, PyObject *x, PyObject *y)
1270 mp *xx = 0, *yy = 0;
1271 mp_expfactor *f = p;
1273 if ((xx = getmp(x)) == 0 || (yy = getmp(y)) == 0)
1276 if ((xx = mp_modinv_checked(xx, xx, yy)) == 0)
1278 yy = mp_neg(yy, yy);
1285 mp_drop(xx); mp_drop(yy);
1289 static PyObject *mm_mexp(PyObject *me, void *v, int n)
1290 { return mp_pywrap(mpmont_mexp(MPMONT_PY(me), MP_NEW, v, n)); }
1292 static PyObject *mmmeth_mexp(PyObject *me, PyObject *arg)
1294 return mexp_common(me, arg, sizeof(mp_expfactor),
1295 mp_mexp_id, mp_mexp_fill, mm_mexp, mp_mexp_drop);
1298 #define mmmeth_ext mmmeth_reduce
1299 static PyObject *mmmeth_reduce(PyObject *me, PyObject *arg)
1304 if (!PyArg_ParseTuple(arg, "O&", convmp, &yy)) goto end;
1305 z = mp_pywrap(mpmont_reduce(MPMONT_PY(me), MP_NEW, yy));
1310 static void mpmont_pydealloc(PyObject *me)
1312 mpmont_destroy(MPMONT_PY(me));
1316 static PyObject *mpmont_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1318 mpmont_pyobj *mm = 0;
1319 static const char *const kwlist[] = { "m", 0 };
1322 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convmp, &xx))
1324 if (!MP_POSP(xx) || !MP_ODDP(xx)) VALERR("m must be positive and odd");
1325 mm = (mpmont_pyobj *)ty->tp_alloc(ty, 0);
1326 mpmont_create(&mm->mm, xx);
1328 if (xx) MP_DROP(xx);
1329 return ((PyObject *)mm);
1332 static PyObject *mmget_m(PyObject *me, void *hunoz)
1333 { return (mp_pywrap(MP_COPY(MPMONT_PY(me)->m))); }
1335 static PyObject *mmget_r(PyObject *me, void *hunoz)
1336 { return (mp_pywrap(MP_COPY(MPMONT_PY(me)->r))); }
1338 static PyObject *mmget_r2(PyObject *me, void *hunoz)
1339 { return (mp_pywrap(MP_COPY(MPMONT_PY(me)->r2))); }
1341 static const PyGetSetDef mpmont_pygetset[] = {
1342 #define GETSETNAME(op, name) mm##op##_##name
1343 GET (m, "M.m -> modulus for reduction")
1344 GET (r, "M.r -> multiplicative identity")
1345 GET (r2, "M.r2 -> M.r^2, Montgomerization factor")
1350 static const PyMethodDef mpmont_pymethods[] = {
1351 #define METHNAME(name) mmmeth_##name
1352 METH (int, "M.int(X) -> XR")
1353 METH (mul, "M.mul(XR, YR) -> ZR where Z = X Y")
1354 METH (expr, "M.expr(XR, N) -> ZR where Z = X^N mod M.m")
1355 METH (mexpr, "M.mexpr([(XR0, N0), (XR1, N1), ...]) = ZR "
1356 "where Z = X0^N0 X1^N1 ... mod M.m\n"
1357 "\t(the list may be flattened if this more convenient.)")
1358 METH (reduce, "M.reduce(XR) -> X")
1359 METH (ext, "M.ext(XR) -> X")
1360 METH (exp, "M.exp(X, N) -> X^N mod M.m")
1361 METH (mexp, "M.mexp([(X0, N0), (X1, N1), ...]) = "
1362 "X0^N0 X1^N1 ... mod M.m\n"
1363 "\t(the list may be flattened if this more convenient.)")
1368 static const PyTypeObject mpmont_pytype_skel = {
1369 PyVarObject_HEAD_INIT(0, 0) /* Header */
1370 "MPMont", /* @tp_name@ */
1371 sizeof(mpmont_pyobj), /* @tp_basicsize@ */
1372 0, /* @tp_itemsize@ */
1374 mpmont_pydealloc, /* @tp_dealloc@ */
1376 0, /* @tp_getattr@ */
1377 0, /* @tp_setattr@ */
1378 0, /* @tp_compare@ */
1380 0, /* @tp_as_number@ */
1381 0, /* @tp_as_sequence@ */
1382 0, /* @tp_as_mapping@ */
1386 0, /* @tp_getattro@ */
1387 0, /* @tp_setattro@ */
1388 0, /* @tp_as_buffer@ */
1389 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1390 Py_TPFLAGS_BASETYPE,
1393 "MPMont(N): a Montgomery reduction context.",
1395 0, /* @tp_traverse@ */
1397 0, /* @tp_richcompare@ */
1398 0, /* @tp_weaklistoffset@ */
1400 0, /* @tp_iternext@ */
1401 PYMETHODS(mpmont), /* @tp_methods@ */
1402 0, /* @tp_members@ */
1403 PYGETSET(mpmont), /* @tp_getset@ */
1406 0, /* @tp_descr_get@ */
1407 0, /* @tp_descr_set@ */
1408 0, /* @tp_dictoffset@ */
1410 PyType_GenericAlloc, /* @tp_alloc@ */
1411 mpmont_pynew, /* @tp_new@ */
1416 /*----- Barrett reduction -------------------------------------------------*/
1418 static PyTypeObject *mpbarrett_pytype;
1420 typedef struct mpbarrett_pyobj {
1425 #define MPBARRETT_PY(o) (&((mpbarrett_pyobj *)(o))->mb)
1427 static PyObject *mbmeth_exp(PyObject *me, PyObject *arg)
1430 mp *yy = 0, *zz = 0;
1432 if (!PyArg_ParseTuple(arg, "O&O&:exp", convmp, &yy, convmp, &zz))
1435 if ((yy = mp_modinv_checked(yy, yy, MPBARRETT_PY(me)->m)) == 0) goto end;
1436 zz = mp_neg(zz, zz);
1438 rc = mp_pywrap(mpbarrett_exp(MPBARRETT_PY(me), MP_NEW, yy, zz));
1440 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
1444 static PyObject *mb_mexp(PyObject *me, void *v, int n)
1445 { return mp_pywrap(mpbarrett_mexp(MPBARRETT_PY(me), MP_NEW, v, n)); }
1447 static PyObject *mbmeth_mexp(PyObject *me, PyObject *arg)
1449 return mexp_common(me, arg, sizeof(mp_expfactor),
1450 mp_mexp_id, mp_mexp_fill, mb_mexp, mp_mexp_drop);
1453 static PyObject *mbmeth_reduce(PyObject *me, PyObject *arg)
1458 if (!PyArg_ParseTuple(arg, "O&:reduce", convmp, &yy))
1460 z = mp_pywrap(mpbarrett_reduce(MPBARRETT_PY(me), MP_NEW, yy));
1465 static void mpbarrett_pydealloc(PyObject *me)
1467 mpbarrett_destroy(MPBARRETT_PY(me));
1471 static PyObject *mpbarrett_pynew(PyTypeObject *ty,
1472 PyObject *arg, PyObject *kw)
1474 mpbarrett_pyobj *mb = 0;
1475 static const char *const kwlist[] = { "m", 0 };
1478 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convmp, &xx))
1480 if (!MP_POSP(xx)) VALERR("m must be positive");
1481 mb = (mpbarrett_pyobj *)ty->tp_alloc(ty, 0);
1482 mpbarrett_create(&mb->mb, xx);
1484 if (xx) MP_DROP(xx);
1485 return ((PyObject *)mb);
1488 static PyObject *mbget_m(PyObject *me, void *hunoz)
1489 { return (mp_pywrap(MP_COPY(MPBARRETT_PY(me)->m))); }
1491 static const PyGetSetDef mpbarrett_pygetset[] = {
1492 #define GETSETNAME(op, name) mb##op##_##name
1493 GET (m, "B.m -> modulus for reduction")
1498 static const PyMethodDef mpbarrett_pymethods[] = {
1499 #define METHNAME(name) mbmeth_##name
1500 METH (reduce, "B.reduce(X) -> X mod B.m")
1501 METH (exp, "B.exp(X, N) -> X^N mod B.m")
1502 METH (mexp, "B.mexp([(X0, N0), (X1, N1), ...]) = "
1503 "X0^N0 X1^N1 ... mod B.m\n"
1504 "\t(the list may be flattened if this more convenient.)")
1509 static const PyTypeObject mpbarrett_pytype_skel = {
1510 PyVarObject_HEAD_INIT(0, 0) /* Header */
1511 "MPBarrett", /* @tp_name@ */
1512 sizeof(mpbarrett_pyobj), /* @tp_basicsize@ */
1513 0, /* @tp_itemsize@ */
1515 mpbarrett_pydealloc, /* @tp_dealloc@ */
1517 0, /* @tp_getattr@ */
1518 0, /* @tp_setattr@ */
1519 0, /* @tp_compare@ */
1521 0, /* @tp_as_number@ */
1522 0, /* @tp_as_sequence@ */
1523 0, /* @tp_as_mapping@ */
1527 0, /* @tp_getattro@ */
1528 0, /* @tp_setattro@ */
1529 0, /* @tp_as_buffer@ */
1530 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1531 Py_TPFLAGS_BASETYPE,
1534 "MPBarrett(N): a Barrett reduction context.",
1536 0, /* @tp_traverse@ */
1538 0, /* @tp_richcompare@ */
1539 0, /* @tp_weaklistoffset@ */
1541 0, /* @tp_iternext@ */
1542 PYMETHODS(mpbarrett), /* @tp_methods@ */
1543 0, /* @tp_members@ */
1544 PYGETSET(mpbarrett), /* @tp_getset@ */
1547 0, /* @tp_descr_get@ */
1548 0, /* @tp_descr_set@ */
1549 0, /* @tp_dictoffset@ */
1551 PyType_GenericAlloc, /* @tp_alloc@ */
1552 mpbarrett_pynew, /* @tp_new@ */
1557 /*----- Nice prime reduction ----------------------------------------------*/
1559 static PyTypeObject *mpreduce_pytype;
1561 typedef struct mpreduce_pyobj {
1566 #define MPREDUCE_PY(o) (&((mpreduce_pyobj *)(o))->mr)
1568 static PyObject *mrmeth_exp(PyObject *me, PyObject *arg)
1571 mp *yy = 0, *zz = 0;
1573 if (!PyArg_ParseTuple(arg, "O&O&:exp", convmp, &yy, convmp, &zz))
1576 if ((yy = mp_modinv_checked(yy, yy, MPREDUCE_PY(me)->p)) == 0) goto end;
1577 zz = mp_neg(zz, zz);
1579 rc = mp_pywrap(mpreduce_exp(MPREDUCE_PY(me), MP_NEW, yy, zz));
1581 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
1585 static PyObject *mrmeth_reduce(PyObject *me, PyObject *arg)
1590 if (!PyArg_ParseTuple(arg, "O&:reduce", convmp, &yy)) goto end;
1591 z = mp_pywrap(mpreduce_do(MPREDUCE_PY(me), MP_NEW, yy));
1596 static void mpreduce_pydealloc(PyObject *me)
1598 mpreduce_destroy(MPREDUCE_PY(me));
1602 static PyObject *mpreduce_pynew(PyTypeObject *ty,
1603 PyObject *arg, PyObject *kw)
1605 mpreduce_pyobj *mr = 0;
1607 static const char *const kwlist[] = { "m", 0 };
1610 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convmp, &xx))
1612 if (!MP_POSP(xx)) VALERR("m must be positive");
1613 if (mpreduce_create(&r, xx)) VALERR("bad modulus (must be 2^k - ...)");
1614 mr = (mpreduce_pyobj *)ty->tp_alloc(ty, 0);
1617 if (xx) MP_DROP(xx);
1618 return ((PyObject *)mr);
1621 static PyObject *mrget_m(PyObject *me, void *hunoz)
1622 { return (mp_pywrap(MP_COPY(MPREDUCE_PY(me)->p))); }
1624 static const PyGetSetDef mpreduce_pygetset[] = {
1625 #define GETSETNAME(op, name) mr##op##_##name
1626 GET (m, "R.m -> modulus for reduction")
1631 static const const PyMethodDef mpreduce_pymethods[] = {
1632 #define METHNAME(name) mrmeth_##name
1633 METH (reduce, "R.reduce(X) -> X mod B.m")
1634 METH (exp, "R.exp(X, N) -> X^N mod B.m")
1639 static const PyTypeObject mpreduce_pytype_skel = {
1640 PyVarObject_HEAD_INIT(0, 0) /* Header */
1641 "MPReduce", /* @tp_name@ */
1642 sizeof(mpreduce_pyobj), /* @tp_basicsize@ */
1643 0, /* @tp_itemsize@ */
1645 mpreduce_pydealloc, /* @tp_dealloc@ */
1647 0, /* @tp_getattr@ */
1648 0, /* @tp_setattr@ */
1649 0, /* @tp_compare@ */
1651 0, /* @tp_as_number@ */
1652 0, /* @tp_as_sequence@ */
1653 0, /* @tp_as_mapping@ */
1657 0, /* @tp_getattro@ */
1658 0, /* @tp_setattro@ */
1659 0, /* @tp_as_buffer@ */
1660 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1661 Py_TPFLAGS_BASETYPE,
1664 "MPReduce(N): a reduction context for reduction modulo Solinas primes.",
1666 0, /* @tp_traverse@ */
1668 0, /* @tp_richcompare@ */
1669 0, /* @tp_weaklistoffset@ */
1671 0, /* @tp_iternext@ */
1672 PYMETHODS(mpreduce), /* @tp_methods@ */
1673 0, /* @tp_members@ */
1674 PYGETSET(mpreduce), /* @tp_getset@ */
1677 0, /* @tp_descr_get@ */
1678 0, /* @tp_descr_set@ */
1679 0, /* @tp_dictoffset@ */
1681 PyType_GenericAlloc, /* @tp_alloc@ */
1682 mpreduce_pynew, /* @tp_new@ */
1687 /*----- Chinese Remainder Theorem solution --------------------------------*/
1689 static PyTypeObject *mpcrt_pytype;
1691 typedef struct mpcrt_pyobj {
1696 #define MPCRT_PY(o) (&((mpcrt_pyobj *)(o))->c)
1698 static PyObject *mcmeth_solve(PyObject *me, PyObject *arg)
1700 mpcrt *c = MPCRT_PY(me);
1701 PyObject *q = 0, *x, *z = 0;
1704 Py_ssize_t i = 0, n = c->k;
1707 if (PyTuple_GET_SIZE(arg) == n)
1709 else if (!PyArg_ParseTuple(arg, "O:solve", &q))
1712 if (!PySequence_Check(q)) TYERR("want a sequence of residues");
1713 i = PySequence_Size(q); if (i < 0) goto end;
1714 if (i != n) VALERR("residue count mismatch");
1715 v = xmalloc(n * sizeof(*v));
1716 for (i = 0; i < n; i++) {
1717 if ((x = PySequence_GetItem(q, i)) == 0) goto end;
1718 xx = getmp(x); Py_DECREF(x); if (!xx) goto end;
1721 z = mp_pywrap(mpcrt_solve(c, MP_NEW, v));
1725 for (i = 0; i < n; i++)
1734 static void mpcrt_pydealloc(PyObject *me)
1736 mpcrt *c = MPCRT_PY(me);
1741 static PyObject *mpcrt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1744 Py_ssize_t n, i = 0, j;
1745 static const char *const kwlist[] = { "mv", 0 };
1746 PyObject *q = 0, *x;
1747 mp *xx = MP_NEW, *y = MP_NEW, *g = MP_NEW;
1751 if (PyTuple_GET_SIZE(arg) > 1)
1753 else if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", KWLIST, &q))
1756 if (!PySequence_Check(q)) TYERR("want a sequence of moduli");
1757 n = PySequence_Size(q); if (n < 0) goto end;
1758 if (!n) VALERR("want at least one modulus");
1759 v = xmalloc(n * sizeof(*v));
1760 for (i = 0; i < n; i++) {
1761 if ((x = PySequence_GetItem(q, i)) == 0) goto end;
1762 xx = getmp(x); Py_DECREF(x); if (!xx) goto end;
1763 if (MP_CMP(xx, <=, MP_ZERO)) VALERR("moduli must be positive");
1764 v[i].m = xx; v[i].n = 0; v[i].ni = 0; v[i].nni = 0; xx = MP_NEW;
1767 for (j = 0; j < i; j++) mpmul_add(&mm, v[j].m);
1768 xx = mpmul_done(&mm);
1769 for (j = 0; j < i; j++) {
1770 mp_div(&y, 0, xx, v[j].m);
1771 mp_gcd(&g, 0, 0, y, v[j].m);
1772 if (!MP_EQ(g, MP_ONE)) VALERR("moduli must be pairwise coprime");
1775 c = (mpcrt_pyobj *)ty->tp_alloc(ty, 0);
1776 mpcrt_create(&c->c, v, n, 0);
1778 mp_drop(xx); mp_drop(y); mp_drop(g);
1779 return ((PyObject *)c);
1784 for (i = 0; i < n; i++)
1789 mp_drop(xx); mp_drop(y); mp_drop(g);
1793 static PyObject *mcget_product(PyObject *me, void *hunoz)
1794 { return (mp_pywrap(MP_COPY(MPCRT_PY(me)->mb.m))); }
1796 static PyObject *mcget_moduli(PyObject *me, void *hunoz)
1800 mpcrt *c = MPCRT_PY(me);
1802 if ((q = PyList_New(c->k)) == 0) return (0);
1803 for (i = 0; i < c->k; i++)
1804 PyList_SET_ITEM(q, i, mp_pywrap(c->v[i].m));
1808 static const PyGetSetDef mpcrt_pygetset[] = {
1809 #define GETSETNAME(op, name) mc##op##_##name
1810 GET (product, "C.product -> product of moduli")
1811 GET (moduli, "C.moduli -> list of individual moduli")
1816 static const PyMethodDef mpcrt_pymethods[] = {
1817 #define METHNAME(name) mcmeth_##name
1818 METH (solve, "C.solve([R0, R1]) -> X mod C.product")
1823 static const PyTypeObject mpcrt_pytype_skel = {
1824 PyVarObject_HEAD_INIT(0, 0) /* Header */
1825 "MPCRT", /* @tp_name@ */
1826 sizeof(mpcrt_pyobj), /* @tp_basicsize@ */
1827 0, /* @tp_itemsize@ */
1829 mpcrt_pydealloc, /* @tp_dealloc@ */
1831 0, /* @tp_getattr@ */
1832 0, /* @tp_setattr@ */
1833 0, /* @tp_compare@ */
1835 0, /* @tp_as_number@ */
1836 0, /* @tp_as_sequence@ */
1837 0, /* @tp_as_mapping@ */
1841 0, /* @tp_getattro@ */
1842 0, /* @tp_setattro@ */
1843 0, /* @tp_as_buffer@ */
1844 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1845 Py_TPFLAGS_BASETYPE,
1848 "MPCRT(SEQ): a context for solving Chinese Remainder Theorem problems.",
1850 0, /* @tp_traverse@ */
1852 0, /* @tp_richcompare@ */
1853 0, /* @tp_weaklistoffset@ */
1855 0, /* @tp_iternext@ */
1856 PYMETHODS(mpcrt), /* @tp_methods@ */
1857 0, /* @tp_members@ */
1858 PYGETSET(mpcrt), /* @tp_getset@ */
1861 0, /* @tp_descr_get@ */
1862 0, /* @tp_descr_set@ */
1863 0, /* @tp_dictoffset@ */
1865 PyType_GenericAlloc, /* @tp_alloc@ */
1866 mpcrt_pynew, /* @tp_new@ */
1871 /*----- Binary polynomials ------------------------------------------------*/
1873 static PyObject *gf_pyrepr(PyObject *o)
1874 { return mp_topystring(MP_X(o), 16, "GF(", "0x", "L)"); }
1876 static PyObject *gf_pyrichcompare(PyObject *x, PyObject *y, int op)
1882 if (gfbinop(x, y, &xx, &yy)) RETURN_NOTIMPL;
1884 case Py_EQ: b = MP_EQ(xx, yy); break;
1885 case Py_NE: b = !MP_EQ(xx, yy); break;
1890 case Py_LT: b = xl < yl; break;
1891 case Py_LE: b = xl <= yl; break;
1892 case Py_GT: b = xl > yl; break;
1893 case Py_GE: b = xl >= yl; break;
1898 MP_DROP(xx); MP_DROP(yy);
1899 return (getbool(b));
1902 static PyObject *gf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1908 static const char *const kwlist[] = { "x", "radix", 0 };
1910 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|i:gf", KWLIST, &x, &radix))
1912 if (GF_PYCHECK(x)) RETURN_OBJ(x);
1913 if (!good_radix_p(radix, 1)) VALERR("radix out of range");
1914 if ((z = mp_frompyobject(x, radix)) == 0) {
1915 PyErr_Format(PyExc_TypeError, "can't convert %.100s to gf",
1916 Py_TYPE(x)->tp_name);
1921 VALERR("gf cannot be negative");
1923 zz = (mp_pyobj *)ty->tp_alloc(ty, 0);
1926 return ((PyObject *)zz);
1929 static PyObject *gf_pyexp(PyObject *x, PyObject *y, PyObject *z)
1931 mp *xx = 0, *yy = 0, *zz = 0;
1935 if ((xx = tomp(x)) == 0 || (yy = tomp(y)) == 0 ||
1936 (z && z != Py_None && (zz = tomp(z)) == 0)) {
1937 mp_drop(xx); mp_drop(yy); mp_drop(zz);
1940 if (!z || z == Py_None) {
1941 if (MP_NEGP(yy)) VALERR("negative exponent");
1942 r = gf_exp(MP_NEW, xx, yy);
1945 if (MP_ZEROP(zz)) ZDIVERR("zero modulus");
1947 if ((xx = gf_modinv_checked(xx, xx, zz)) == 0) goto end;
1948 yy = mp_neg(yy, yy);
1950 gfreduce_create(&gr, zz);
1951 r = gfreduce_exp(&gr, MP_NEW, xx, yy);
1952 gfreduce_destroy(&gr);
1956 mp_drop(xx); mp_drop(yy); mp_drop(zz);
1960 static PyObject *gfmeth_sqr(PyObject *me)
1961 { return (gf_pywrap(gf_sqr(MP_NEW, MP_X(me)))); }
1963 static PyObject *gfmeth_gcd(PyObject *me, PyObject *arg)
1966 mp *yy = 0, *zz = MP_NEW;
1968 if (!PyArg_ParseTuple(arg, "O&:gcd", convgf, &yy)) goto end;
1969 gf_gcd(&zz, 0, 0, MP_X(me), yy);
1972 if (yy) MP_DROP(yy);
1976 static PyObject *gfmeth_gcdx(PyObject *me, PyObject *arg)
1979 mp *yy = 0, *zz = MP_NEW, *uu = MP_NEW, *vv = MP_NEW;
1981 if (!PyArg_ParseTuple(arg, "O&:gcdx", convgf, &yy))
1983 gf_gcd(&zz, &uu, &vv, MP_X(me), yy);
1984 z = Py_BuildValue("(NNN)",
1985 gf_pywrap(zz), gf_pywrap(uu), gf_pywrap(vv));
1987 if (yy) MP_DROP(yy);
1991 static PyObject *gfmeth_modinv(PyObject *me, PyObject *arg)
1994 mp *yy = 0, *zz = MP_NEW;
1996 if (!PyArg_ParseTuple(arg, "O&:modinv", convgf, &yy) ||
1997 (zz = gf_modinv_checked(MP_NEW, yy, MP_X(me))) == 0)
2001 if (yy) MP_DROP(yy);
2005 static PyObject *gfmeth_fromstring(PyObject *me,
2006 PyObject *arg, PyObject *kw)
2013 mptext_stringctx sc;
2014 static const char *const kwlist[] = { "x", "radix", 0 };
2016 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|i:fromstring", KWLIST,
2019 if (!good_radix_p(r, 1)) VALERR("bad radix");
2020 sc.buf = p; sc.lim = p + len;
2021 if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0 ||
2023 if (zz) MP_DROP(zz);
2024 VALERR("bad binary polynomial");
2026 z = Py_BuildValue("(Ns#)", gf_pywrap(zz),
2027 sc.buf, (Py_ssize_t)(sc.lim - sc.buf));
2032 static PyObject *gfmeth_irreduciblep(PyObject *me)
2033 { return getbool(gf_irreduciblep(MP_X(me))); }
2035 static PyObject *gfget_degree(PyObject *me, void *hunoz)
2036 { return (PyInt_FromLong(mp_bits(MP_X(me)) - 1)); }
2038 static const PyGetSetDef gf_pygetset[] = {
2039 #define GETSETNAME(op, name) gf##op##_##name
2040 GET (degree, "X.degree -> polynomial degree of X")
2042 #define GETSETNAME(op, name) mp##op##_##name
2043 GET (nbits, "X.nbits -> bit length of X")
2044 GET (noctets, "X.noctets -> octet length of X")
2049 static const PyMethodDef gf_pymethods[] = {
2050 #define METHNAME(func) gfmeth_##func
2051 METH (setbit, "X.setbit(N) -> X with bit N set")
2052 METH (clearbit, "X.clearbit(N) -> X with bit N clear")
2053 METH (testbit, "X.testbit(N) -> true/false if bit N set/clear in X")
2054 NAMETH(sqr, "X.sqr() -> X^2")
2055 METH (gcd, "X.gcd(Y) -> gcd(X, Y)")
2056 METH (gcdx, "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)")
2057 METH (modinv, "X.modinv(Y) -> multiplicative inverse of Y mod X")
2058 NAMETH(irreduciblep, "X.irreduciblep() -> true/false")
2059 KWSMTH(fromstring, "fromstring(STR, [radix = 0]) -> (X, REST)\n"
2060 " Parse STR as a binary polynomial, according to RADIX. If RADIX is\n"
2061 " zero, read a prefix from STR to decide radix: allow `0b' for binary,\n"
2062 " `0' or `0o' for octal, `0x' for hex, or `R_' for other radix R.")
2063 SMTH (loadl, "loadl(STR) -> X: read little-endian bytes")
2064 SMTH (loadb, "loadb(STR) -> X: read big-endian bytes")
2065 SMTH (frombuf, "frombuf(STR) -> (X, REST): read buffer format")
2067 #define METHNAME(func) mpmeth_##func
2068 KWMETH(tostring, "X.tostring([radix = 10]) -> STR")
2069 KWMETH(storel, "X.storel([len = -1]) -> little-endian bytes")
2070 KWMETH(storeb, "X.storeb([len = -1]) -> big-endian bytes")
2071 KWMETH(storel2c, "X.storel2c([len = -1]) -> "
2072 "little-endian bytes, two's complement")
2073 KWMETH(storeb2c, "X.storeb2c([len = -1]) -> "
2074 "big-endian bytes, two's complement")
2075 NAMETH(tobuf, "X.tobuf() -> buffer format")
2080 static const PyNumberMethods gf_pynumber = {
2081 gf_pyadd, /* @nb_add@ */
2082 gf_pysub, /* @nb_subtract@ */
2083 gf_pymul, /* @nb_multiply@ */
2084 0, /* @nb_divide@ */
2085 gf_pymod, /* @nb_remainder@ */
2086 gf_pydivmod, /* @nb_divmod@ */
2087 gf_pyexp, /* @nb_power@ */
2088 mp_pyid, /* @nb_negative@ */
2089 mp_pyid, /* @nb_positive@ */
2090 mp_pyid, /* @nb_absolute@ */
2091 mp_pynonzerop, /* @nb_nonzero@ */
2092 0 /* doesn't make any sense */, /* @nb_invert@ */
2093 gf_pylsl, /* @nb_lshift@ */
2094 gf_pylsr, /* @nb_rshift@ */
2095 gf_pyand, /* @nb_and@ */
2096 gf_pyxor, /* @nb_xor@ */
2097 gf_pyor, /* @nb_or@ */
2098 gf_pycoerce, /* @nb_coerce@ */
2099 mp_pyint, /* @nb_int@ */
2100 mp_pylong, /* @nb_long@ */
2101 0 /* doesn't make any sense */, /* @nb_float@ */
2102 mp_pyoct, /* @nb_oct@ */
2103 mp_pyhex, /* @nb_hex@ */
2105 0, /* @nb_inplace_add@ */
2106 0, /* @nb_inplace_subtract@ */
2107 0, /* @nb_inplace_multiply@ */
2108 0, /* @nb_inplace_divide@ */
2109 0, /* @nb_inplace_remainder@ */
2110 0, /* @nb_inplace_power@ */
2111 0, /* @nb_inplace_lshift@ */
2112 0, /* @nb_inplace_rshift@ */
2113 0, /* @nb_inplace_and@ */
2114 0, /* @nb_inplace_xor@ */
2115 0, /* @nb_inplace_or@ */
2117 gf_pydiv, /* @nb_floor_divide@ */
2118 0, /* @nb_true_divide@ */
2119 0, /* @nb_inplace_floor_divide@ */
2120 0, /* @nb_inplace_true_divide@ */
2123 static const PyTypeObject gf_pytype_skel = {
2124 PyVarObject_HEAD_INIT(0, 0) /* Header */
2125 "GF", /* @tp_name@ */
2126 sizeof(mp_pyobj), /* @tp_basicsize@ */
2127 0, /* @tp_itemsize@ */
2129 mp_pydealloc, /* @tp_dealloc@ */
2131 0, /* @tp_getattr@ */
2132 0, /* @tp_setattr@ */
2133 0, /* @tp_compare@ */
2134 gf_pyrepr, /* @tp_repr@ */
2135 PYNUMBER(gf), /* @tp_as_number@ */
2136 0, /* @tp_as_sequence@ */
2137 0, /* @tp_as_mapping@ */
2138 mp_pyhash, /* @tp_hash@ */
2140 mp_pyhex, /* @tp_str@ */
2141 0, /* @tp_getattro@ */
2142 0, /* @tp_setattro@ */
2143 0, /* @tp_as_buffer@ */
2144 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2145 Py_TPFLAGS_CHECKTYPES |
2146 Py_TPFLAGS_BASETYPE,
2149 "Binary polynomials. Support almost all the standard arithmetic\n"
2152 "Constructor GF(X, [radix = R]) attempts to convert X to a `GF'. If\n"
2153 "X is a string, it's read in radix-R form, or we look for a prefix\n"
2154 "if R = 0. Other acceptable things are field elements, elliptic curve\n"
2155 "points, group elements, Python `int' and `long' objects, and anything\n"
2156 "with an integer conversion.\n"
2158 "The name is hopelessly wrong from a technical point of view, but\n"
2159 "but it's much easier to type than `p2' or `c2' or whatever.\n"
2163 " * Use `//' for Euclidean division: `/' gives exact rational division.",
2165 0, /* @tp_traverse@ */
2167 gf_pyrichcompare, /* @tp_richcompare@ */
2168 0, /* @tp_weaklistoffset@ */
2170 0, /* @tp_iternext@ */
2171 PYMETHODS(gf), /* @tp_methods@ */
2172 0, /* @tp_members@ */
2173 PYGETSET(gf), /* @tp_getset@ */
2176 0, /* @tp_descr_get@ */
2177 0, /* @tp_descr_set@ */
2178 0, /* @tp_dictoffset@ */
2180 PyType_GenericAlloc, /* @tp_alloc@ */
2181 gf_pynew, /* @tp_new@ */
2186 /*----- Sparse poly reduction ---------------------------------------------*/
2188 static PyTypeObject *gfreduce_pytype;
2190 typedef struct gfreduce_pyobj {
2195 #define GFREDUCE_PY(o) (&((gfreduce_pyobj *)(o))->mr)
2197 static PyObject *grmeth_exp(PyObject *me, PyObject *arg)
2200 mp *yy = 0, *zz = 0;
2202 if (!PyArg_ParseTuple(arg, "O&O&:exp", convgf, &yy, convgf, &zz))
2205 if ((yy = gf_modinv_checked(yy, yy, GFREDUCE_PY(me)->p)) == 0) goto end;
2206 zz = mp_neg(zz, zz);
2208 rc = gf_pywrap(gfreduce_exp(GFREDUCE_PY(me), MP_NEW, yy, zz));
2210 if (yy) MP_DROP(yy); if (zz) MP_DROP(zz);
2214 static PyObject *grmeth_trace(PyObject *me, PyObject *arg)
2219 if (!PyArg_ParseTuple(arg, "O&:trace", convgf, &xx)) goto end;
2220 rc = PyInt_FromLong(gfreduce_trace(GFREDUCE_PY(me), xx));
2222 if (xx) MP_DROP(xx);
2226 static PyObject *grmeth_halftrace(PyObject *me, PyObject *arg)
2231 if (!PyArg_ParseTuple(arg, "O&:halftrace", convgf, &xx)) goto end;
2232 rc = gf_pywrap(gfreduce_halftrace(GFREDUCE_PY(me), MP_NEW, xx));
2234 if (xx) MP_DROP(xx);
2238 static PyObject *grmeth_sqrt(PyObject *me, PyObject *arg)
2243 if (!PyArg_ParseTuple(arg, "O&:sqrt", convgf, &xx)) goto end;
2244 if ((yy = gfreduce_sqrt(GFREDUCE_PY(me), MP_NEW, xx)) == 0)
2245 VALERR("no modular square root");
2248 if (xx) MP_DROP(xx);
2252 static PyObject *grmeth_quadsolve(PyObject *me, PyObject *arg)
2257 if (!PyArg_ParseTuple(arg, "O&:quadsolve", convgf, &xx)) goto end;
2258 if ((yy = gfreduce_quadsolve(GFREDUCE_PY(me), MP_NEW, xx)) == 0)
2259 VALERR("no solution found");
2262 if (xx) MP_DROP(xx);
2266 static PyObject *grmeth_reduce(PyObject *me, PyObject *arg)
2271 if (!PyArg_ParseTuple(arg, "O&:reduce", convgf, &yy)) goto end;
2272 z = gf_pywrap(gfreduce_do(GFREDUCE_PY(me), MP_NEW, yy));
2277 static void gfreduce_pydealloc(PyObject *me)
2279 gfreduce_destroy(GFREDUCE_PY(me));
2283 static PyObject *gfreduce_pynew(PyTypeObject *ty,
2284 PyObject *arg, PyObject *kw)
2286 gfreduce_pyobj *mr = 0;
2288 static const char *const kwlist[] = { "m", 0 };
2291 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convgf, &xx))
2293 if (MP_ZEROP(xx)) ZDIVERR("modulus is zero!");
2294 gfreduce_create(&r, xx);
2295 mr = (gfreduce_pyobj *)ty->tp_alloc(ty, 0);
2298 if (xx) MP_DROP(xx);
2299 return ((PyObject *)mr);
2302 static PyObject *grget_m(PyObject *me, void *hunoz)
2303 { return (gf_pywrap(MP_COPY(GFREDUCE_PY(me)->p))); }
2305 static const PyGetSetDef gfreduce_pygetset[] = {
2306 #define GETSETNAME(op, name) gr##op##_##name
2307 GET (m, "R.m -> reduction polynomial")
2312 static const PyMethodDef gfreduce_pymethods[] = {
2313 #define METHNAME(name) grmeth_##name
2314 METH (reduce, "R.reduce(X) -> X mod B.m")
2315 METH (trace, "R.trace(X) -> Tr(X) = x + x^2 + ... + x^{2^{m - 1}}")
2316 METH (halftrace, "R.halftrace(X) -> x + x^{2^2} + ... + x^{2^{m - 1}}")
2317 METH (sqrt, "R.sqrt(X) -> Y where Y^2 = X mod R")
2318 METH (quadsolve, "R.quadsolve(X) -> Y where Y^2 + Y = X mod R")
2319 METH (exp, "R.exp(X, N) -> X^N mod B.m")
2324 static const PyTypeObject gfreduce_pytype_skel = {
2325 PyVarObject_HEAD_INIT(0, 0) /* Header */
2326 "GFReduce", /* @tp_name@ */
2327 sizeof(gfreduce_pyobj), /* @tp_basicsize@ */
2328 0, /* @tp_itemsize@ */
2330 gfreduce_pydealloc, /* @tp_dealloc@ */
2332 0, /* @tp_getattr@ */
2333 0, /* @tp_setattr@ */
2334 0, /* @tp_compare@ */
2336 0, /* @tp_as_number@ */
2337 0, /* @tp_as_sequence@ */
2338 0, /* @tp_as_mapping@ */
2342 0, /* @tp_getattro@ */
2343 0, /* @tp_setattro@ */
2344 0, /* @tp_as_buffer@ */
2345 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2346 Py_TPFLAGS_BASETYPE,
2349 "GFReduce(N): a context for reduction modulo sparse polynomials.",
2351 0, /* @tp_traverse@ */
2353 0, /* @tp_richcompare@ */
2354 0, /* @tp_weaklistoffset@ */
2356 0, /* @tp_iternext@ */
2357 PYMETHODS(gfreduce), /* @tp_methods@ */
2358 0, /* @tp_members@ */
2359 PYGETSET(gfreduce), /* @tp_getset@ */
2362 0, /* @tp_descr_get@ */
2363 0, /* @tp_descr_set@ */
2364 0, /* @tp_dictoffset@ */
2366 PyType_GenericAlloc, /* @tp_alloc@ */
2367 gfreduce_pynew, /* @tp_new@ */
2372 /*----- Normal/poly transformation ----------------------------------------*/
2374 static PyTypeObject *gfn_pytype;
2376 typedef struct gfn_pyobj {
2382 #define GFN_P(o) (((gfn_pyobj *)(o))->p)
2383 #define GFN_PTON(o) (&((gfn_pyobj *)(o))->pton)
2384 #define GFN_NTOP(o) (&((gfn_pyobj *)(o))->ntop)
2386 static PyObject *gfn_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
2388 mp *p = 0, *beta = 0;
2390 static const char *const kwlist[] = { "p", "beta", 0 };
2392 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", KWLIST,
2393 convgf, &p, convgf, &beta))
2395 gg = PyObject_New(gfn_pyobj, ty);
2397 if (gfn_create(p, beta, &gg->ntop, &gg->pton)) {
2400 VALERR("can't invert transformation matrix");
2406 return ((PyObject *)gg);
2409 static PyObject *gfnget_p(PyObject *me, void *hunoz)
2410 { return (gf_pywrap(MP_COPY(GFN_P(me)))); }
2412 static PyObject *gfnget_beta(PyObject *me, void *hunoz)
2414 gfn *n = GFN_NTOP(me);
2415 mp *x = n->r[n->n - 1];
2416 return (gf_pywrap(MP_COPY(x)));
2419 #define XFORMOP(name, NAME) \
2420 static PyObject *gfnmeth_##name(PyObject *me, PyObject *arg) \
2425 if (!PyArg_ParseTuple(arg, "O&:" #name, convgf, &xx)) goto end; \
2426 z = gfn_transform(GFN_##NAME(me), MP_NEW, xx); \
2429 if (!z) return (0); \
2430 return (gf_pywrap(z)); \
2436 static void gfn_pydealloc(PyObject *me)
2440 gfn_destroy(GFN_PTON(me));
2441 gfn_destroy(GFN_NTOP(me));
2446 static const PyGetSetDef gfn_pygetset[] = {
2447 #define GETSETNAME(op, name) gfn##op##_##name
2448 GET (p, "X.p -> polynomial basis, as polynomial")
2449 GET (beta, "X.beta -> normal basis element, in poly form")
2454 static const PyMethodDef gfn_pymethods[] = {
2455 #define METHNAME(name) gfnmeth_##name
2456 METH (pton, "X.pton(A) -> normal-basis representation of A")
2457 METH (ntop, "X.ntop(A) -> polynomial-basis representation of A")
2462 static const PyTypeObject gfn_pytype_skel = {
2463 PyVarObject_HEAD_INIT(0, 0) /* Header */
2464 "GFN", /* @tp_name@ */
2465 sizeof(gfn_pyobj), /* @tp_basicsize@ */
2466 0, /* @tp_itemsize@ */
2468 gfn_pydealloc, /* @tp_dealloc@ */
2470 0, /* @tp_getattr@ */
2471 0, /* @tp_setattr@ */
2472 0, /* @tp_compare@ */
2474 0, /* @tp_as_number@ */
2475 0, /* @tp_as_sequence@ */
2476 0, /* @tp_as_mapping@ */
2480 0, /* @tp_getattro@ */
2481 0, /* @tp_setattro@ */
2482 0, /* @tp_as_buffer@ */
2483 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2484 Py_TPFLAGS_BASETYPE,
2487 "GFN(P, BETA): an object for transforming elements of binary fields\n"
2488 " between polynomial and normal basis representations.",
2490 0, /* @tp_traverse@ */
2492 0, /* @tp_richcompare@ */
2493 0, /* @tp_weaklistoffset@ */
2495 0, /* @tp_iternext@ */
2496 PYMETHODS(gfn), /* @tp_methods@ */
2497 0, /* @tp_members@ */
2498 PYGETSET(gfn), /* @tp_getset@ */
2501 0, /* @tp_descr_get@ */
2502 0, /* @tp_descr_set@ */
2503 0, /* @tp_dictoffset@ */
2505 PyType_GenericAlloc, /* @tp_alloc@ */
2506 gfn_pynew, /* @tp_new@ */
2511 /*----- Glue --------------------------------------------------------------*/
2513 static const struct nameval consts[] = {
2518 void mp_pyinit(void)
2522 INITTYPE(mpmul, root);
2523 INITTYPE(mpmont, root);
2524 INITTYPE(mpbarrett, root);
2525 INITTYPE(mpreduce, root);
2526 INITTYPE(mpcrt, root);
2527 INITTYPE(gfreduce, root);
2528 INITTYPE(gfn, root);
2531 void mp_pyinsert(PyObject *mod)
2533 INSERT("MP", mp_pytype);
2534 INSERT("MPMul", mpmul_pytype);
2535 INSERT("MPMont", mpmont_pytype);
2536 INSERT("MPBarrett", mpbarrett_pytype);
2537 INSERT("MPReduce", mpreduce_pytype);
2538 INSERT("MPCRT", mpcrt_pytype);
2539 INSERT("GF", gf_pytype);
2540 INSERT("GFReduce", gfreduce_pytype);
2541 INSERT("GFN", gfn_pytype);
2542 setconstants(mod, consts);
2545 /*----- That's all, folks -------------------------------------------------*/