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 static PyObject
*meth__MP_product(PyObject
*me
, PyObject
*arg
)
907 if (PyTuple_Size(arg
) != 2) {
908 i
= PyObject_GetIter(arg
);
911 if ((q
= PyTuple_GetItem(arg
, 1)) == 0) return (0);
912 if ((i
= PyObject_GetIter(q
)) == 0) {
913 PyErr_Clear(); /* that's ok */
914 i
= PyObject_GetIter(arg
);
919 while ((q
= PyIter_Next(i
)) != 0) {
920 x
= getmp(q
); Py_DECREF(q
); if (!x
) {
921 MP_DROP(mpmul_done(&m
));
930 return (mp_pywrap(x
));
933 #define LOADOP(pre, py, name) \
934 static PyObject *meth__##py##_##name(PyObject *me, PyObject *arg) \
938 if (!PyArg_ParseTuple(arg, "Os#:" #name, &me, &p, &len)) return (0); \
939 return (pre##_pywrap(mp_##name(MP_NEW, p, len))); \
941 LOADOP(mp
, MP
, loadl
)
942 LOADOP(mp
, MP
, loadb
)
943 LOADOP(mp
, MP
, loadl2c
)
944 LOADOP(mp
, MP
, loadb2c
)
945 LOADOP(gf
, GF
, loadl
)
946 LOADOP(gf
, GF
, loadb
)
949 /*----- Montgomery reduction ----------------------------------------------*/
951 typedef struct mpmont_pyobj
{
956 #define MPMONT_PY(o) (&((mpmont_pyobj *)(o))->mm)
958 static PyObject
*mmmeth_int(PyObject
*me
, PyObject
*arg
)
962 mpmont
*mm
= MPMONT_PY(me
);
964 if (!PyArg_ParseTuple(arg
, "O&:in", convmp
, &yy
))
966 mp_div(0, &yy
, yy
, mm
->m
);
967 z
= mp_pywrap(mpmont_mul(mm
, MP_NEW
, yy
, mm
->r2
));
973 static PyObject
*mmmeth_mul(PyObject
*me
, PyObject
*arg
)
978 if (!PyArg_ParseTuple(arg
, "O&O&:mul", convmp
, &yy
, convmp
, &zz
))
980 rc
= mp_pywrap(mpmont_mul(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
982 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
986 static PyObject
*mmmeth_exp(PyObject
*me
, PyObject
*arg
)
991 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
994 if ((yy
= mp_modinv_checked(yy
, yy
, MPMONT_PY(me
)->m
)) == 0) goto end
;
997 rc
= mp_pywrap(mpmont_exp(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
999 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1003 static PyObject
*mmmeth_expr(PyObject
*me
, PyObject
*arg
)
1006 mp
*yy
= 0, *zz
= 0;
1008 if (!PyArg_ParseTuple(arg
, "O&O&:expr", convmp
, &yy
, convmp
, &zz
))
1011 yy
= mpmont_reduce(MPMONT_PY(me
), yy
, yy
);
1012 if ((yy
= mp_modinv_checked(yy
, yy
, MPMONT_PY(me
)->m
)) == 0) goto end
;
1013 yy
= mpmont_mul(MPMONT_PY(me
), yy
, yy
, MPMONT_PY(me
)->r2
);
1014 zz
= mp_neg(zz
, zz
);
1016 rc
= mp_pywrap(mpmont_expr(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1018 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1022 static PyObject
*mm_mexpr_id(PyObject
*me
)
1023 { return mp_pywrap(MP_COPY(MPMONT_PY(me
)->r
)); }
1025 static int mm_mexpr_fill(void *p
, PyObject
*me
, PyObject
*x
, PyObject
*y
)
1027 mp
*xx
= 0, *yy
= 0;
1028 mp_expfactor
*f
= p
;
1029 mpmont
*mm
= MPMONT_PY(me
);
1031 if ((xx
= getmp(x
)) == 0 || (yy
= getmp(y
)) == 0)
1034 xx
= mpmont_reduce(mm
, xx
, xx
);
1035 if ((xx
= mp_modinv_checked(xx
, xx
, yy
)) == 0)
1037 xx
= mpmont_mul(mm
, xx
, xx
, mm
->r2
);
1038 yy
= mp_neg(yy
, yy
);
1045 mp_drop(xx
); mp_drop(yy
);
1049 static PyObject
*mm_mexpr(PyObject
*me
, void *v
, int n
)
1050 { return mp_pywrap(mpmont_mexpr(MPMONT_PY(me
), MP_NEW
, v
, n
)); }
1052 static void mp_mexp_drop(void *p
)
1054 mp_expfactor
*f
= p
;
1059 static PyObject
*mmmeth_mexpr(PyObject
*me
, PyObject
*arg
)
1061 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1062 mm_mexpr_id
, mm_mexpr_fill
, mm_mexpr
, mp_mexp_drop
);
1065 static PyObject
*mp_mexp_id(PyObject
*me
)
1066 { return mp_pywrap(MP_ONE
); }
1068 static int mp_mexp_fill(void *p
, PyObject
*me
, PyObject
*x
, PyObject
*y
)
1070 mp
*xx
= 0, *yy
= 0;
1071 mp_expfactor
*f
= p
;
1073 if ((xx
= getmp(x
)) == 0 || (yy
= getmp(y
)) == 0)
1076 if ((xx
= mp_modinv_checked(xx
, xx
, yy
)) == 0)
1078 yy
= mp_neg(yy
, yy
);
1085 mp_drop(xx
); mp_drop(yy
);
1089 static PyObject
*mm_mexp(PyObject
*me
, void *v
, int n
)
1090 { return mp_pywrap(mpmont_mexp(MPMONT_PY(me
), MP_NEW
, v
, n
)); }
1092 static PyObject
*mmmeth_mexp(PyObject
*me
, PyObject
*arg
)
1094 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1095 mp_mexp_id
, mp_mexp_fill
, mm_mexp
, mp_mexp_drop
);
1098 #define mmmeth_ext mmmeth_reduce
1099 static PyObject
*mmmeth_reduce(PyObject
*me
, PyObject
*arg
)
1104 if (!PyArg_ParseTuple(arg
, "O&", convmp
, &yy
)) goto end
;
1105 z
= mp_pywrap(mpmont_reduce(MPMONT_PY(me
), MP_NEW
, yy
));
1110 static void mpmont_pydealloc(PyObject
*me
)
1112 mpmont_destroy(MPMONT_PY(me
));
1116 static PyObject
*mpmont_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1118 mpmont_pyobj
*mm
= 0;
1119 char *kwlist
[] = { "m", 0 };
1122 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convmp
, &xx
))
1124 if (!MP_POSP(xx
) || !MP_ODDP(xx
)) VALERR("m must be positive and odd");
1125 mm
= (mpmont_pyobj
*)ty
->tp_alloc(ty
, 0);
1126 mpmont_create(&mm
->mm
, xx
);
1128 if (xx
) MP_DROP(xx
);
1129 return ((PyObject
*)mm
);
1132 static PyObject
*mmget_m(PyObject
*me
, void *hunoz
)
1133 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->m
))); }
1135 static PyObject
*mmget_r(PyObject
*me
, void *hunoz
)
1136 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->r
))); }
1138 static PyObject
*mmget_r2(PyObject
*me
, void *hunoz
)
1139 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->r2
))); }
1141 static PyGetSetDef mpmont_pygetset
[] = {
1142 #define GETSETNAME(op, name) mm##op##_##name
1143 GET (m
, "M.m -> modulus for reduction")
1144 GET (r
, "M.r -> multiplicative identity")
1145 GET (r2
, "M.r2 -> M.r^2, Montgomerization factor")
1150 static PyMethodDef mpmont_pymethods
[] = {
1151 #define METHNAME(name) mmmeth_##name
1152 METH (int, "M.out(X) -> XR")
1153 METH (mul
, "M.mul(XR, YR) -> ZR where Z = X Y")
1154 METH (expr
, "M.expr(XR, N) -> ZR where Z = X^N mod M.m")
1156 B.mexp([(XR0, N0), (XR1, N1), ...]) = ZR where Z = X0^N0 X1^N1 mod B.m\n\
1157 \t(the list may be flattened if this more convenient.)")
1158 METH (reduce
, "M.reduce(XR) -> X")
1159 METH (ext
, "M.ext(XR) -> X")
1160 METH (exp
, "M.exp(X, N) -> X^N mod M.m")
1162 B.mexp([(X0, N0), (X1, N1), ...]) = X0^N0 X1^N1 mod B.m\n\
1163 \t(the list may be flattened if this more convenient.)")
1168 static PyTypeObject
*mpmont_pytype
, mpmont_pytype_skel
= {
1169 PyObject_HEAD_INIT(0) 0, /* Header */
1170 "catacomb.MPMont", /* @tp_name@ */
1171 sizeof(mpmont_pyobj
), /* @tp_basicsize@ */
1172 0, /* @tp_itemsize@ */
1174 mpmont_pydealloc
, /* @tp_dealloc@ */
1176 0, /* @tp_getattr@ */
1177 0, /* @tp_setattr@ */
1178 0, /* @tp_compare@ */
1180 0, /* @tp_as_number@ */
1181 0, /* @tp_as_sequence@ */
1182 0, /* @tp_as_mapping@ */
1186 0, /* @tp_getattro@ */
1187 0, /* @tp_setattro@ */
1188 0, /* @tp_as_buffer@ */
1189 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1190 Py_TPFLAGS_BASETYPE
,
1193 "A Montgomery reduction context.",
1195 0, /* @tp_traverse@ */
1197 0, /* @tp_richcompare@ */
1198 0, /* @tp_weaklistoffset@ */
1200 0, /* @tp_iternext@ */
1201 mpmont_pymethods
, /* @tp_methods@ */
1202 0, /* @tp_members@ */
1203 mpmont_pygetset
, /* @tp_getset@ */
1206 0, /* @tp_descr_get@ */
1207 0, /* @tp_descr_set@ */
1208 0, /* @tp_dictoffset@ */
1210 PyType_GenericAlloc
, /* @tp_alloc@ */
1211 mpmont_pynew
, /* @tp_new@ */
1216 /*----- Barrett reduction -------------------------------------------------*/
1218 typedef struct mpbarrett_pyobj
{
1223 #define MPBARRETT_PY(o) (&((mpbarrett_pyobj *)(o))->mb)
1225 static PyObject
*mbmeth_exp(PyObject
*me
, PyObject
*arg
)
1228 mp
*yy
= 0, *zz
= 0;
1230 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1233 if ((yy
= mp_modinv_checked(yy
, yy
, MPBARRETT_PY(me
)->m
)) == 0) goto end
;
1234 zz
= mp_neg(zz
, zz
);
1236 rc
= mp_pywrap(mpbarrett_exp(MPBARRETT_PY(me
), MP_NEW
, yy
, zz
));
1238 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1242 static PyObject
*mb_mexp(PyObject
*me
, void *v
, int n
)
1243 { return mp_pywrap(mpbarrett_mexp(MPBARRETT_PY(me
), MP_NEW
, v
, n
)); }
1245 static PyObject
*mbmeth_mexp(PyObject
*me
, PyObject
*arg
)
1247 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1248 mp_mexp_id
, mp_mexp_fill
, mb_mexp
, mp_mexp_drop
);
1251 static PyObject
*mbmeth_reduce(PyObject
*me
, PyObject
*arg
)
1256 if (!PyArg_ParseTuple(arg
, "O&:reduce", convmp
, &yy
))
1258 z
= mp_pywrap(mpbarrett_reduce(MPBARRETT_PY(me
), MP_NEW
, yy
));
1263 static void mpbarrett_pydealloc(PyObject
*me
)
1265 mpbarrett_destroy(MPBARRETT_PY(me
));
1269 static PyObject
*mpbarrett_pynew(PyTypeObject
*ty
,
1270 PyObject
*arg
, PyObject
*kw
)
1272 mpbarrett_pyobj
*mb
= 0;
1273 char *kwlist
[] = { "m", 0 };
1276 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convmp
, &xx
))
1278 if (!MP_POSP(xx
)) VALERR("m must be positive");
1279 mb
= (mpbarrett_pyobj
*)ty
->tp_alloc(ty
, 0);
1280 mpbarrett_create(&mb
->mb
, xx
);
1282 if (xx
) MP_DROP(xx
);
1283 return ((PyObject
*)mb
);
1286 static PyObject
*mbget_m(PyObject
*me
, void *hunoz
)
1287 { return (mp_pywrap(MP_COPY(MPBARRETT_PY(me
)->m
))); }
1289 static PyGetSetDef mpbarrett_pygetset
[] = {
1290 #define GETSETNAME(op, name) mb##op##_##name
1291 GET (m
, "B.m -> modulus for reduction")
1296 static PyMethodDef mpbarrett_pymethods
[] = {
1297 #define METHNAME(name) mbmeth_##name
1298 METH (reduce
, "B.reduce(X) -> X mod B.m")
1299 METH (exp
, "B.exp(X, N) -> X^N mod B.m")
1301 B.mexp([(X0, N0), (X1, N1), ...]) = X0^N0 X1^N1 mod B.m\n\
1302 \t(the list may be flattened if this more convenient.)")
1307 static PyTypeObject
*mpbarrett_pytype
, mpbarrett_pytype_skel
= {
1308 PyObject_HEAD_INIT(0) 0, /* Header */
1309 "catacomb.MPBarrett", /* @tp_name@ */
1310 sizeof(mpbarrett_pyobj
), /* @tp_basicsize@ */
1311 0, /* @tp_itemsize@ */
1313 mpbarrett_pydealloc
, /* @tp_dealloc@ */
1315 0, /* @tp_getattr@ */
1316 0, /* @tp_setattr@ */
1317 0, /* @tp_compare@ */
1319 0, /* @tp_as_number@ */
1320 0, /* @tp_as_sequence@ */
1321 0, /* @tp_as_mapping@ */
1325 0, /* @tp_getattro@ */
1326 0, /* @tp_setattro@ */
1327 0, /* @tp_as_buffer@ */
1328 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1329 Py_TPFLAGS_BASETYPE
,
1332 "A Barrett reduction context.",
1334 0, /* @tp_traverse@ */
1336 0, /* @tp_richcompare@ */
1337 0, /* @tp_weaklistoffset@ */
1339 0, /* @tp_iternext@ */
1340 mpbarrett_pymethods
, /* @tp_methods@ */
1341 0, /* @tp_members@ */
1342 mpbarrett_pygetset
, /* @tp_getset@ */
1345 0, /* @tp_descr_get@ */
1346 0, /* @tp_descr_set@ */
1347 0, /* @tp_dictoffset@ */
1349 PyType_GenericAlloc
, /* @tp_alloc@ */
1350 mpbarrett_pynew
, /* @tp_new@ */
1355 /*----- Nice prime reduction ----------------------------------------------*/
1357 typedef struct mpreduce_pyobj
{
1362 #define MPREDUCE_PY(o) (&((mpreduce_pyobj *)(o))->mr)
1364 static PyObject
*mrmeth_exp(PyObject
*me
, PyObject
*arg
)
1367 mp
*yy
= 0, *zz
= 0;
1369 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1372 if ((yy
= mp_modinv_checked(yy
, yy
, MPREDUCE_PY(me
)->p
)) == 0) goto end
;
1373 zz
= mp_neg(zz
, zz
);
1375 rc
= mp_pywrap(mpreduce_exp(MPREDUCE_PY(me
), MP_NEW
, yy
, zz
));
1377 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1381 static PyObject
*mrmeth_reduce(PyObject
*me
, PyObject
*arg
)
1386 if (!PyArg_ParseTuple(arg
, "O&:reduce", convmp
, &yy
)) goto end
;
1387 z
= mp_pywrap(mpreduce_do(MPREDUCE_PY(me
), MP_NEW
, yy
));
1392 static void mpreduce_pydealloc(PyObject
*me
)
1394 mpreduce_destroy(MPREDUCE_PY(me
));
1398 static PyObject
*mpreduce_pynew(PyTypeObject
*ty
,
1399 PyObject
*arg
, PyObject
*kw
)
1401 mpreduce_pyobj
*mr
= 0;
1403 char *kwlist
[] = { "m", 0 };
1406 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convmp
, &xx
))
1408 if (!MP_POSP(xx
)) VALERR("m must be positive");
1409 if (mpreduce_create(&r
, xx
)) VALERR("bad modulus (must be 2^k - ...)");
1410 mr
= (mpreduce_pyobj
*)ty
->tp_alloc(ty
, 0);
1413 if (xx
) MP_DROP(xx
);
1414 return ((PyObject
*)mr
);
1417 static PyObject
*mrget_m(PyObject
*me
, void *hunoz
)
1418 { return (mp_pywrap(MP_COPY(MPREDUCE_PY(me
)->p
))); }
1420 static PyGetSetDef mpreduce_pygetset
[] = {
1421 #define GETSETNAME(op, name) mr##op##_##name
1422 GET (m
, "R.m -> modulus for reduction")
1427 static PyMethodDef mpreduce_pymethods
[] = {
1428 #define METHNAME(name) mrmeth_##name
1429 METH (reduce
, "R.reduce(X) -> X mod B.m")
1430 METH (exp
, "R.exp(X, N) -> X^N mod B.m")
1435 static PyTypeObject
*mpreduce_pytype
, mpreduce_pytype_skel
= {
1436 PyObject_HEAD_INIT(0) 0, /* Header */
1437 "catacomb.MPReduce", /* @tp_name@ */
1438 sizeof(mpreduce_pyobj
), /* @tp_basicsize@ */
1439 0, /* @tp_itemsize@ */
1441 mpreduce_pydealloc
, /* @tp_dealloc@ */
1443 0, /* @tp_getattr@ */
1444 0, /* @tp_setattr@ */
1445 0, /* @tp_compare@ */
1447 0, /* @tp_as_number@ */
1448 0, /* @tp_as_sequence@ */
1449 0, /* @tp_as_mapping@ */
1453 0, /* @tp_getattro@ */
1454 0, /* @tp_setattro@ */
1455 0, /* @tp_as_buffer@ */
1456 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1457 Py_TPFLAGS_BASETYPE
,
1460 "A reduction context for reduction modulo primes of special form.",
1462 0, /* @tp_traverse@ */
1464 0, /* @tp_richcompare@ */
1465 0, /* @tp_weaklistoffset@ */
1467 0, /* @tp_iternext@ */
1468 mpreduce_pymethods
, /* @tp_methods@ */
1469 0, /* @tp_members@ */
1470 mpreduce_pygetset
, /* @tp_getset@ */
1473 0, /* @tp_descr_get@ */
1474 0, /* @tp_descr_set@ */
1475 0, /* @tp_dictoffset@ */
1477 PyType_GenericAlloc
, /* @tp_alloc@ */
1478 mpreduce_pynew
, /* @tp_new@ */
1483 /*----- Chinese Remainder Theorem solution --------------------------------*/
1485 typedef struct mpcrt_pyobj
{
1490 #define MPCRT_PY(o) (&((mpcrt_pyobj *)(o))->c)
1492 static PyObject
*mcmeth_solve(PyObject
*me
, PyObject
*arg
)
1494 mpcrt
*c
= MPCRT_PY(me
);
1495 PyObject
*q
= 0, *x
, *z
= 0;
1498 int i
= 0, n
= c
->k
;
1501 if (PyTuple_Size(arg
) == n
)
1503 else if (!PyArg_ParseTuple(arg
, "O:solve", &q
))
1506 if (!PySequence_Check(q
)) TYERR("want a sequence of residues");
1507 if (PySequence_Size(q
) != n
) VALERR("residue count mismatch");
1508 v
= xmalloc(n
* sizeof(*v
));
1509 for (i
= 0; i
< n
; i
++) {
1510 if ((x
= PySequence_GetItem(q
, i
)) == 0) goto end
;
1511 xx
= getmp(x
); Py_DECREF(x
); if (!xx
) goto end
;
1514 z
= mp_pywrap(mpcrt_solve(c
, MP_NEW
, v
));
1518 for (i
= 0; i
< n
; i
++)
1527 static void mpcrt_pydealloc(PyObject
*me
)
1529 mpcrt
*c
= MPCRT_PY(me
);
1534 static PyObject
*mpcrt_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1538 char *kwlist
[] = { "mv", 0 };
1539 PyObject
*q
= 0, *x
;
1543 if (PyTuple_Size(arg
) > 1)
1545 else if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", kwlist
, &q
))
1548 if (!PySequence_Check(q
)) TYERR("want a sequence of moduli");
1549 n
= PySequence_Size(q
);
1550 if (PyErr_Occurred()) goto end
;
1551 if (!n
) VALERR("want at least one modulus");
1552 v
= xmalloc(n
* sizeof(*v
));
1553 for (i
= 0; i
< n
; i
++) {
1554 if ((x
= PySequence_GetItem(q
, i
)) == 0) goto end
;
1555 xx
= getmp(x
); Py_DECREF(x
); if (!xx
) goto end
;
1556 v
[i
].m
= xx
; v
[i
].n
= 0; v
[i
].ni
= 0; v
[i
].nni
= 0;
1558 c
= (mpcrt_pyobj
*)ty
->tp_alloc(ty
, 0);
1559 mpcrt_create(&c
->c
, v
, n
, 0);
1561 return ((PyObject
*)c
);
1566 for (i
= 0; i
< n
; i
++)
1574 static PyObject
*mcget_product(PyObject
*me
, void *hunoz
)
1575 { return (mp_pywrap(MP_COPY(MPCRT_PY(me
)->mb
.m
))); }
1577 static PyObject
*mcget_moduli(PyObject
*me
, void *hunoz
)
1581 mpcrt
*c
= MPCRT_PY(me
);
1583 if ((q
= PyList_New(c
->k
)) == 0) return (0);
1584 for (i
= 0; i
< c
->k
; i
++)
1585 PyList_SetItem(q
, i
, mp_pywrap(c
->v
[i
].m
));
1589 static PyGetSetDef mpcrt_pygetset
[] = {
1590 #define GETSETNAME(op, name) mc##op##_##name
1591 GET (product
, "C.product -> product of moduli")
1592 GET (moduli
, "C.moduli -> list of individual moduli")
1597 static PyMethodDef mpcrt_pymethods
[] = {
1598 #define METHNAME(name) mcmeth_##name
1599 METH (solve
, "C.solve([R0, R1]) -> X mod C.product")
1604 static PyTypeObject
*mpcrt_pytype
, mpcrt_pytype_skel
= {
1605 PyObject_HEAD_INIT(0) 0, /* Header */
1606 "catacomb.MPCRT", /* @tp_name@ */
1607 sizeof(mpcrt_pyobj
), /* @tp_basicsize@ */
1608 0, /* @tp_itemsize@ */
1610 mpcrt_pydealloc
, /* @tp_dealloc@ */
1612 0, /* @tp_getattr@ */
1613 0, /* @tp_setattr@ */
1614 0, /* @tp_compare@ */
1616 0, /* @tp_as_number@ */
1617 0, /* @tp_as_sequence@ */
1618 0, /* @tp_as_mapping@ */
1622 0, /* @tp_getattro@ */
1623 0, /* @tp_setattro@ */
1624 0, /* @tp_as_buffer@ */
1625 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1626 Py_TPFLAGS_BASETYPE
,
1629 "A context for the solution of Chinese Remainder Theorem problems.",
1631 0, /* @tp_traverse@ */
1633 0, /* @tp_richcompare@ */
1634 0, /* @tp_weaklistoffset@ */
1636 0, /* @tp_iternext@ */
1637 mpcrt_pymethods
, /* @tp_methods@ */
1638 0, /* @tp_members@ */
1639 mpcrt_pygetset
, /* @tp_getset@ */
1642 0, /* @tp_descr_get@ */
1643 0, /* @tp_descr_set@ */
1644 0, /* @tp_dictoffset@ */
1646 PyType_GenericAlloc
, /* @tp_alloc@ */
1647 mpcrt_pynew
, /* @tp_new@ */
1652 /*----- Binary polynomials ------------------------------------------------*/
1654 static PyObject
*gf_pyrepr(PyObject
*o
)
1655 { return mp_topystring(MP_X(o
), 16, "GF(", "0x", "L)"); }
1657 static PyObject
*gf_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
1663 if (mpbinop(x
, y
, &xx
, &yy
)) RETURN_NOTIMPL
;
1665 case Py_EQ
: b
= MP_EQ(xx
, yy
); break;
1666 case Py_NE
: b
= !MP_EQ(xx
, yy
); break;
1671 case Py_LT
: b
= xl
< yl
; break;
1672 case Py_LE
: b
= xl
<= yl
; break;
1673 case Py_GT
: b
= xl
> yl
; break;
1674 case Py_GE
: b
= xl
>= yl
; break;
1679 MP_DROP(xx
); MP_DROP(yy
);
1680 return (getbool(b
));
1683 static PyObject
*gf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1689 char *kwlist
[] = { "x", "radix", 0 };
1691 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|i:gf", kwlist
, &x
, &radix
))
1693 if (GF_PYCHECK(x
)) RETURN_OBJ(x
);
1694 if (!good_radix_p(radix
, 1)) VALERR("radix out of range");
1695 if ((z
= mp_frompyobject(x
, radix
)) == 0) {
1696 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to gf",
1697 x
->ob_type
->tp_name
);
1702 VALERR("gf cannot be negative");
1704 zz
= (mp_pyobj
*)ty
->tp_alloc(ty
, 0);
1707 return ((PyObject
*)zz
);
1710 static long gf_pyhash(PyObject
*me
)
1712 long i
= mp_tolong(MP_X(me
));
1713 i
^= 0xc7ecd67c; /* random perturbance */
1719 static PyObject
*gf_pyexp(PyObject
*x
, PyObject
*y
, PyObject
*z
)
1721 mp
*xx
= 0, *yy
= 0, *zz
= 0;
1725 if ((xx
= tomp(x
)) == 0 || (yy
= tomp(y
)) == 0 ||
1726 (z
&& z
!= Py_None
&& (zz
= tomp(z
)) == 0)) {
1727 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
1730 if (!z
|| z
== Py_None
) {
1731 if (MP_NEGP(yy
)) VALERR("negative exponent");
1732 r
= gf_exp(MP_NEW
, xx
, yy
);
1735 if (MP_ZEROP(zz
)) ZDIVERR("zero modulus");
1737 if ((xx
= gf_modinv_checked(xx
, xx
, zz
)) == 0) goto end
;
1738 yy
= mp_neg(yy
, yy
);
1740 gfreduce_create(&gr
, zz
);
1741 r
= gfreduce_exp(&gr
, MP_NEW
, xx
, yy
);
1742 gfreduce_destroy(&gr
);
1746 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
1750 static PyObject
*gfmeth_sqr(PyObject
*me
, PyObject
*arg
)
1752 if (!PyArg_ParseTuple(arg
, ":sqr")) return (0);
1753 return (gf_pywrap(gf_sqr(MP_NEW
, MP_X(me
))));
1756 static PyObject
*gfmeth_gcd(PyObject
*me
, PyObject
*arg
)
1759 mp
*yy
= 0, *zz
= MP_NEW
;
1761 if (!PyArg_ParseTuple(arg
, "O&:gcd", convgf
, &yy
)) goto end
;
1762 gf_gcd(&zz
, 0, 0, MP_X(me
), yy
);
1765 if (yy
) MP_DROP(yy
);
1769 static PyObject
*gfmeth_gcdx(PyObject
*me
, PyObject
*arg
)
1772 mp
*yy
= 0, *zz
= MP_NEW
, *uu
= MP_NEW
, *vv
= MP_NEW
;
1774 if (!PyArg_ParseTuple(arg
, "O&:gcdx", convgf
, &yy
))
1776 gf_gcd(&zz
, &uu
, &vv
, MP_X(me
), yy
);
1777 z
= Py_BuildValue("(NNN)",
1778 gf_pywrap(zz
), gf_pywrap(uu
), gf_pywrap(vv
));
1780 if (yy
) MP_DROP(yy
);
1784 static PyObject
*gfmeth_modinv(PyObject
*me
, PyObject
*arg
)
1787 mp
*yy
= 0, *zz
= MP_NEW
;
1789 if (!PyArg_ParseTuple(arg
, "O&:modinv", convgf
, &yy
) ||
1790 (zz
= gf_modinv_checked(MP_NEW
, yy
, MP_X(me
))) == 0)
1794 if (yy
) MP_DROP(yy
);
1798 static PyObject
*gfmeth_irreduciblep(PyObject
*me
, PyObject
*arg
)
1800 if (!PyArg_ParseTuple(arg
, ":irreduciblep")) return (0);
1801 return getbool(gf_irreduciblep(MP_X(me
)));
1804 static PyObject
*gfget_degree(PyObject
*me
, void *hunoz
)
1805 { return (PyInt_FromLong(mp_bits(MP_X(me
)) - 1)); }
1807 static PyGetSetDef gf_pygetset
[] = {
1808 #define GETSETNAME(op, name) gf##op##_##name
1809 GET (degree
, "X.degree -> polynomial degree of X")
1811 #define GETSETNAME(op, name) mp##op##_##name
1812 GET (nbits
, "X.nbits -> bit length of X")
1813 GET (noctets
, "X.noctets -> octet length of X")
1818 static PyMethodDef gf_pymethods
[] = {
1819 #define METHNAME(func) gfmeth_##func
1820 METH (setbit
, "X.setbit(N) -> X with bit N set")
1821 METH (clearbit
, "X.clearbit(N) -> X with bit N clear")
1822 METH (testbit
, "X.testbit(N) -> true/false if bit N set/clear in X")
1823 METH (sqr
, "X.sqr() -> X^2")
1824 METH (gcd
, "X.gcd(Y) -> gcd(X, Y)")
1826 "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)")
1827 METH (modinv
, "X.modinv(Y) -> multiplicative inverse of Y mod X")
1828 METH (irreduciblep
, "X.irreduciblep() -> true/false")
1830 #define METHNAME(func) mpmeth_##func
1831 KWMETH(tostring
, "X.tostring(radix = 10) -> STR")
1832 KWMETH(storel
, "X.storel(len = -1) -> little-endian bytes")
1833 KWMETH(storeb
, "X.storeb(len = -1) -> big-endian bytes")
1835 "X.storel2c(len = -1) -> little-endian bytes, two's complement")
1837 "X.storeb2c(len = -1) -> big-endian bytes, two's complement")
1838 METH (tobuf
, "X.tobuf() -> buffer format")
1843 static PyNumberMethods gf_pynumber
= {
1844 gf_pyadd
, /* @nb_add@ */
1845 gf_pysub
, /* @nb_subtract@ */
1846 gf_pymul
, /* @nb_multiply@ */
1847 0, /* @nb_divide@ */
1848 gf_pymod
, /* @nb_remainder@ */
1849 gf_pydivmod
, /* @nb_divmod@ */
1850 gf_pyexp
, /* @nb_power@ */
1851 mp_pyid
, /* @nb_negative@ */
1852 mp_pyid
, /* @nb_positive@ */
1853 mp_pyid
, /* @nb_absolute@ */
1854 mp_pynonzerop
, /* @nb_nonzero@ */
1855 0 /* doesn't make any sense */, /* @nb_invert@ */
1856 gf_pylsl
, /* @nb_lshift@ */
1857 gf_pylsr
, /* @nb_rshift@ */
1858 gf_pyand
, /* @nb_and@ */
1859 gf_pyxor
, /* @nb_xor@ */
1860 gf_pyor
, /* @nb_or@ */
1861 gf_pycoerce
, /* @nb_coerce@ */
1862 mp_pyint
, /* @nb_int@ */
1863 mp_pylong
, /* @nb_long@ */
1864 0 /* doesn't make any sense */, /* @nb_float@ */
1865 mp_pyoct
, /* @nb_oct@ */
1866 mp_pyhex
, /* @nb_hex@ */
1868 0, /* @nb_inplace_add@ */
1869 0, /* @nb_inplace_subtract@ */
1870 0, /* @nb_inplace_multiply@ */
1871 0, /* @nb_inplace_divide@ */
1872 0, /* @nb_inplace_remainder@ */
1873 0, /* @nb_inplace_power@ */
1874 0, /* @nb_inplace_lshift@ */
1875 0, /* @nb_inplace_rshift@ */
1876 0, /* @nb_inplace_and@ */
1877 0, /* @nb_inplace_xor@ */
1878 0, /* @nb_inplace_or@ */
1880 gf_pydiv
, /* @nb_floor_divide@ */
1881 0, /* @nb_true_divide@ */
1882 0, /* @nb_inplace_floor_divide@ */
1883 0, /* @nb_inplace_true_divide@ */
1886 static PyTypeObject gf_pytype_skel
= {
1887 PyObject_HEAD_INIT(0) 0, /* Header */
1888 "catacomb.GF", /* @tp_name@ */
1889 sizeof(mp_pyobj
), /* @tp_basicsize@ */
1890 0, /* @tp_itemsize@ */
1892 mp_pydealloc
, /* @tp_dealloc@ */
1894 0, /* @tp_getattr@ */
1895 0, /* @tp_setattr@ */
1896 0, /* @tp_compare@ */
1897 gf_pyrepr
, /* @tp_repr@ */
1898 &gf_pynumber
, /* @tp_as_number@ */
1899 0, /* @tp_as_sequence@ */
1900 0, /* @tp_as_mapping@ */
1901 gf_pyhash
, /* @tp_hash@ */
1903 mp_pyhex
, /* @tp_str@ */
1904 0, /* @tp_getattro@ */
1905 0, /* @tp_setattro@ */
1906 0, /* @tp_as_buffer@ */
1907 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1908 Py_TPFLAGS_CHECKTYPES
|
1909 Py_TPFLAGS_BASETYPE
,
1912 "Binary polynomials. Support almost all the standard arithmetic\n\
1915 Constructor gf(X, radix = R) attempts to convert X to a `gf'. If\n\
1916 X is a string, it's read in radix-R form, or we look for a prefix\n\
1917 if R = 0. Other acceptable things are ints and longs.\n\
1919 The name is hopelessly wrong from a technical point of view, but\n\
1920 but it's much easier to type than `p2' or `c2' or whatever.\n\
1924 * Use `//' for division. GFs don't have `/' division.",
1926 0, /* @tp_traverse@ */
1928 gf_pyrichcompare
, /* @tp_richcompare@ */
1929 0, /* @tp_weaklistoffset@ */
1931 0, /* @tp_iternext@ */
1932 gf_pymethods
, /* @tp_methods@ */
1933 0, /* @tp_members@ */
1934 gf_pygetset
, /* @tp_getset@ */
1937 0, /* @tp_descr_get@ */
1938 0, /* @tp_descr_set@ */
1939 0, /* @tp_dictoffset@ */
1941 PyType_GenericAlloc
, /* @tp_alloc@ */
1942 gf_pynew
, /* @tp_new@ */
1947 static PyObject
*meth__GF_fromstring(PyObject
*me
,
1948 PyObject
*arg
, PyObject
*kw
)
1955 mptext_stringctx sc
;
1956 char *kwlist
[] = { "class", "x", "radix", 0 };
1958 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "Os#|i:fromstring",
1959 kwlist
, &me
, &p
, &len
, &r
))
1961 if (!good_radix_p(r
, 1)) VALERR("radix out of range");
1962 sc
.buf
= p
; sc
.lim
= p
+ len
;
1963 if ((zz
= mp_read(MP_NEW
, r
, &mptext_stringops
, &sc
)) == 0 || MP_NEGP(zz
))
1964 z
= Py_BuildValue("(Os#)", Py_None
, p
, len
);
1966 z
= Py_BuildValue("(Ns#)", gf_pywrap(zz
),
1967 sc
.buf
, (int)(sc
.lim
- sc
.buf
));
1972 /*----- Sparse poly reduction ---------------------------------------------*/
1974 typedef struct gfreduce_pyobj
{
1979 #define GFREDUCE_PY(o) (&((gfreduce_pyobj *)(o))->mr)
1981 static PyObject
*grmeth_exp(PyObject
*me
, PyObject
*arg
)
1984 mp
*yy
= 0, *zz
= 0;
1986 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convgf
, &yy
, convgf
, &zz
))
1989 if ((yy
= gf_modinv_checked(yy
, yy
, GFREDUCE_PY(me
)->p
)) == 0) goto end
;
1990 zz
= mp_neg(zz
, zz
);
1992 rc
= gf_pywrap(gfreduce_exp(GFREDUCE_PY(me
), MP_NEW
, yy
, zz
));
1994 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1998 static PyObject
*grmeth_reduce(PyObject
*me
, PyObject
*arg
)
2003 if (!PyArg_ParseTuple(arg
, "O&:reduce", convgf
, &yy
)) goto end
;
2004 z
= gf_pywrap(gfreduce_do(GFREDUCE_PY(me
), MP_NEW
, yy
));
2009 static void gfreduce_pydealloc(PyObject
*me
)
2011 gfreduce_destroy(GFREDUCE_PY(me
));
2015 static PyObject
*gfreduce_pynew(PyTypeObject
*ty
,
2016 PyObject
*arg
, PyObject
*kw
)
2018 gfreduce_pyobj
*mr
= 0;
2020 char *kwlist
[] = { "m", 0 };
2023 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convgf
, &xx
))
2025 if (MP_ZEROP(xx
)) ZDIVERR("modulus is zero!");
2026 gfreduce_create(&r
, xx
);
2027 mr
= (gfreduce_pyobj
*)ty
->tp_alloc(ty
, 0);
2030 if (xx
) MP_DROP(xx
);
2031 return ((PyObject
*)mr
);
2034 static PyObject
*grget_m(PyObject
*me
, void *hunoz
)
2035 { return (gf_pywrap(MP_COPY(GFREDUCE_PY(me
)->p
))); }
2037 static PyGetSetDef gfreduce_pygetset
[] = {
2038 #define GETSETNAME(op, name) gr##op##_##name
2039 GET (m
, "R.m -> reduction polynomial")
2044 static PyMethodDef gfreduce_pymethods
[] = {
2045 #define METHNAME(name) grmeth_##name
2046 METH (reduce
, "R.reduce(X) -> X mod B.m")
2047 METH (exp
, "R.exp(X, N) -> X^N mod B.m")
2052 static PyTypeObject
*gfreduce_pytype
, gfreduce_pytype_skel
= {
2053 PyObject_HEAD_INIT(0) 0, /* Header */
2054 "catacomb.GFReduce", /* @tp_name@ */
2055 sizeof(gfreduce_pyobj
), /* @tp_basicsize@ */
2056 0, /* @tp_itemsize@ */
2058 gfreduce_pydealloc
, /* @tp_dealloc@ */
2060 0, /* @tp_getattr@ */
2061 0, /* @tp_setattr@ */
2062 0, /* @tp_compare@ */
2064 0, /* @tp_as_number@ */
2065 0, /* @tp_as_sequence@ */
2066 0, /* @tp_as_mapping@ */
2070 0, /* @tp_getattro@ */
2071 0, /* @tp_setattro@ */
2072 0, /* @tp_as_buffer@ */
2073 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2074 Py_TPFLAGS_BASETYPE
,
2077 "A reduction context for reduction modulo sparse irreducible polynomials.",
2079 0, /* @tp_traverse@ */
2081 0, /* @tp_richcompare@ */
2082 0, /* @tp_weaklistoffset@ */
2084 0, /* @tp_iternext@ */
2085 gfreduce_pymethods
, /* @tp_methods@ */
2086 0, /* @tp_members@ */
2087 gfreduce_pygetset
, /* @tp_getset@ */
2090 0, /* @tp_descr_get@ */
2091 0, /* @tp_descr_set@ */
2092 0, /* @tp_dictoffset@ */
2094 PyType_GenericAlloc
, /* @tp_alloc@ */
2095 gfreduce_pynew
, /* @tp_new@ */
2100 /*----- Normal/poly transformation ----------------------------------------*/
2102 typedef struct gfn_pyobj
{
2108 static PyTypeObject
*gfn_pytype
, gfn_pytype_skel
;
2110 #define GFN_P(o) (((gfn_pyobj *)(o))->p)
2111 #define GFN_PTON(o) (&((gfn_pyobj *)(o))->pton)
2112 #define GFN_NTOP(o) (&((gfn_pyobj *)(o))->ntop)
2114 static PyObject
*gfn_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2116 mp
*p
= 0, *beta
= 0;
2118 char *kwlist
[] = { "p", "beta", 0 };
2120 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:new", kwlist
,
2121 convgf
, &p
, convgf
, &beta
))
2123 gg
= PyObject_New(gfn_pyobj
, ty
);
2124 if (gfn_create(p
, beta
, &gg
->ntop
, &gg
->pton
)) {
2127 VALERR("can't invert transformation matrix");
2133 return ((PyObject
*)gg
);
2136 static PyObject
*gfnget_p(PyObject
*me
, void *hunoz
)
2137 { return (gf_pywrap(MP_COPY(GFN_P(me
)))); }
2139 static PyObject
*gfnget_beta(PyObject
*me
, void *hunoz
)
2141 gfn
*n
= GFN_NTOP(me
);
2142 mp
*x
= n
->r
[n
->n
- 1];
2143 return (gf_pywrap(MP_COPY(x
)));
2146 #define XFORMOP(name, NAME) \
2147 static PyObject *gfnmeth_##name(PyObject *me, PyObject *arg) \
2152 if (!PyArg_ParseTuple(arg, "O&:" #name, convgf, &xx)) goto end; \
2153 z = gfn_transform(GFN_##NAME(me), MP_NEW, xx); \
2156 if (!z) return (0); \
2157 return (mp_pywrap(z)); \
2163 static void gfn_pydealloc(PyObject
*me
)
2165 gfn_destroy(GFN_PTON(me
));
2166 gfn_destroy(GFN_NTOP(me
));
2170 static PyGetSetDef gfn_pygetset
[] = {
2171 #define GETSETNAME(op, name) gfn##op##_##name
2172 GET (p
, "X.p -> polynomial basis, as polynomial")
2173 GET (beta
, "X.beta -> normal basis element, in poly form")
2178 static PyMethodDef gfn_pymethods
[] = {
2179 #define METHNAME(name) gfnmeth_##name
2180 METH (pton
, "X.pton(A) -> normal-basis representation of A")
2181 METH (ntop
, "X.ntop(A) -> polynomial-basis representation of A")
2186 static PyTypeObject gfn_pytype_skel
= {
2187 PyObject_HEAD_INIT(0) 0, /* Header */
2188 "catacomb.GFN", /* @tp_name@ */
2189 sizeof(gfn_pyobj
), /* @tp_basicsize@ */
2190 0, /* @tp_itemsize@ */
2192 gfn_pydealloc
, /* @tp_dealloc@ */
2194 0, /* @tp_getattr@ */
2195 0, /* @tp_setattr@ */
2196 0, /* @tp_compare@ */
2198 0, /* @tp_as_number@ */
2199 0, /* @tp_as_sequence@ */
2200 0, /* @tp_as_mapping@ */
2204 0, /* @tp_getattro@ */
2205 0, /* @tp_setattro@ */
2206 0, /* @tp_as_buffer@ */
2207 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2208 Py_TPFLAGS_BASETYPE
,
2211 "An object for transforming elements of binary fields between polynomial\n\
2212 and normal basis representations.",
2214 0, /* @tp_traverse@ */
2216 0, /* @tp_richcompare@ */
2217 0, /* @tp_weaklistoffset@ */
2219 0, /* @tp_iternext@ */
2220 gfn_pymethods
, /* @tp_methods@ */
2221 0, /* @tp_members@ */
2222 gfn_pygetset
, /* @tp_getset@ */
2225 0, /* @tp_descr_get@ */
2226 0, /* @tp_descr_set@ */
2227 0, /* @tp_dictoffset@ */
2229 PyType_GenericAlloc
, /* @tp_alloc@ */
2230 gfn_pynew
, /* @tp_new@ */
2235 /*----- Glue --------------------------------------------------------------*/
2237 static PyMethodDef methods
[] = {
2238 #define METHNAME(func) meth_##func
2239 KWMETH(_MP_fromstring
, "\
2240 fromstring(STR, radix = 0) -> (X, REST)\n\
2242 Parse STR as a large integer, according to radix. If radix is zero,\n\
2243 read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\
2244 or `R_' for other radix R.")
2245 KWMETH(_GF_fromstring
, "\
2246 fromstring(STR, radix = 0) -> (X, REST)\n\
2248 Parse STR as a binary polynomial, according to radix. If radix is zero,\n\
2249 read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\
2250 or `R_' for other radix R.")
2252 loadl(STR) -> X: read little-endian bytes")
2254 loadb(STR) -> X: read big-endian bytes")
2255 METH (_MP_loadl2c
, "\
2256 loadl2c(STR) -> X: read little-endian bytes, two's complement")
2257 METH (_MP_loadb2c
, "\
2258 loadb2c(STR) -> X: read big-endian bytes, two's complement")
2259 METH (_MP_frombuf
, "\
2260 frombuf(STR) -> (X, REST): read buffer format")
2261 METH (_MP_product
, "\
2262 product(ITER) -> X: product of things iterated over")
2264 loadl(STR) -> X: read little-endian bytes")
2266 loadb(STR) -> X: read big-endian bytes")
2267 METH (_GF_frombuf
, "\
2268 frombuf(STR) -> (X, REST): read buffer format")
2273 void mp_pyinit(void)
2277 INITTYPE(mpmont
, root
);
2278 INITTYPE(mpbarrett
, root
);
2279 INITTYPE(mpreduce
, root
);
2280 INITTYPE(mpcrt
, root
);
2281 INITTYPE(gfreduce
, root
);
2282 INITTYPE(gfn
, root
);
2283 addmethods(methods
);
2286 void mp_pyinsert(PyObject
*mod
)
2288 INSERT("MP", mp_pytype
);
2289 INSERT("MPMont", mpmont_pytype
);
2290 INSERT("MPBarrett", mpbarrett_pytype
);
2291 INSERT("MPReduce", mpreduce_pytype
);
2292 INSERT("MPCRT", mpcrt_pytype
);
2293 INSERT("GF", gf_pytype
);
2294 INSERT("GFReduce", gfreduce_pytype
);
2295 INSERT("GFN", gfn_pytype
);
2298 /*----- That's all, folks -------------------------------------------------*/