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;
36 mp
*mp_frompylong(PyObject
*obj
)
39 PyLongObject
*l
= (PyLongObject
*)obj
;
50 assert(MPW_BITS
>= SHIFT
);
51 bits
= (unsigned long)sz
* SHIFT
;
52 w
= (bits
+ MPW_BITS
- 1)/MPW_BITS
;
53 x
= mp_new(w
, l
->ob_size
< 0 ? MP_NEG
: 0);
55 for (i
= 0; i
< sz
; i
++) {
56 r
|= (mpd
)l
->ob_digit
[i
] << b
;
58 while (b
>= MPW_BITS
) {
73 PyObject
*mp_topylong(mp
*x
)
75 unsigned long bits
= mp_bits(x
);
76 int sz
= (bits
+ SHIFT
- 1)/SHIFT
;
77 PyLongObject
*l
= _PyLong_New(sz
);
83 assert(MPW_BITS
>= SHIFT
);
84 while (i
< sz
&& p
< x
->vl
) {
87 while (i
< sz
&& b
>= SHIFT
) {
88 l
->ob_digit
[i
++] = r
& MASK
;
94 l
->ob_digit
[i
++] = r
& MASK
;
97 l
->ob_size
= (x
->f
& MP_NEG
) ?
-sz
: sz
;
98 return ((PyObject
*)l
);
101 mp
*mp_frompyobject(PyObject
*o
, int radix
)
105 if (PyString_Check(o
)) {
108 sc
.buf
= PyString_AS_STRING(o
);
109 sc
.lim
= sc
.buf
+ PyString_GET_SIZE(o
);
110 x
= mp_read(MP_NEW
, radix
, &mptext_stringops
, &sc
);
112 if (sc
.buf
< sc
.lim
) { MP_DROP(x
); return (0); }
115 if ((x
= tomp(o
)) != 0)
120 PyObject
*mp_topystring(mp
*x
, int radix
, const char *xpre
,
121 const char *pre
, const char *post
)
123 int len
= mptext_len(x
, radix
) + 1;
126 size_t xprelen
= xpre ?
strlen(xpre
) : 0;
127 size_t prelen
= pre ?
strlen(pre
) : 0;
128 size_t postlen
= post ?
strlen(post
) : 0;
131 o
= PyString_FromStringAndSize(0, len
+ 1 + xprelen
+ prelen
+ postlen
);
132 p
= PyString_AS_STRING(o
);
134 if (xpre
) { memcpy(sc
.buf
, xpre
, xprelen
); sc
.buf
+= xprelen
; }
135 if (MP_NEGP(x
)) { *sc
.buf
++ = '-'; x
= mp_neg(x
, x
); }
136 if (pre
) { memcpy(sc
.buf
, pre
, prelen
); sc
.buf
+= prelen
; }
137 sc
.lim
= sc
.buf
+ len
;
138 mp_write(x
, radix
, &mptext_stringops
, &sc
);
139 if (post
) { memcpy(sc
.buf
, post
, postlen
); sc
.buf
+= postlen
; }
141 _PyString_Resize(&o
, sc
.buf
- p
);
145 static int good_radix_p(int r
, int readp
)
147 return ((r
>= -255 && r
<= -2) ||
149 (r
>= 2 && r
<= 62));
152 PyObject
*mp_pywrap(mp
*x
)
154 mp_pyobj
*z
= PyObject_New(mp_pyobj
, mp_pytype
);
156 return ((PyObject
*)z
);
159 PyObject
*gf_pywrap(mp
*x
)
161 mp_pyobj
*z
= PyObject_New(mp_pyobj
, gf_pytype
);
163 return ((PyObject
*)z
);
166 int mp_tolong_checked(mp
*x
, long *l
, int must
)
168 static mp
*longmin
= 0, *longmax
= 0;
172 longmin
= mp_fromlong(MP_NEW
, LONG_MIN
);
173 longmax
= mp_fromlong(MP_NEW
, LONG_MAX
);
175 if (MP_CMP(x
, <, longmin
) || MP_CMP(x
, >, longmax
)) {
176 if (must
) 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 mp
*implicitmp(PyObject
*o
)
283 static mp
*implicitgf(PyObject
*o
)
294 static int mpbinop(PyObject
*x
, PyObject
*y
, mp
**xx
, mp
**yy
)
296 if ((*xx
= implicitmp(x
)) == 0)
298 if ((*yy
= implicitmp(y
)) == 0) {
305 static int gfbinop(PyObject
*x
, PyObject
*y
, mp
**xx
, mp
**yy
)
307 if ((*xx
= implicitgf(x
)) == 0)
309 if ((*yy
= implicitgf(y
)) == 0) {
316 #define gf_and mp_and
318 #define gf_xor mp_xor
319 #define BINOP(pre, name) \
320 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
322 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
323 zz = pre##_##name(MP_NEW, xx, yy); \
324 MP_DROP(xx); MP_DROP(yy); \
325 return (pre##_pywrap(zz)); \
341 static mp
*mp_abs(mp
*d
, mp
*x
)
344 return (mp_neg(d
, x
));
350 #define UNOP(pre, name) \
351 static PyObject *pre##_py##name(PyObject *x) \
352 { return mp_pywrap(pre##_##name(MP_NEW, MP_X(x))); }
358 static PyObject
*mp_pyid(PyObject
*x
) { RETURN_OBJ(x
); }
360 #define gf_lsr mp_lsr
361 #define SHIFTOP(pre, name, rname) \
362 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
366 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
367 if (mp_tolong_checked(yy, &n, 1)) goto end; \
369 z = pre##_pywrap(mp_##rname(MP_NEW, xx, -n)); \
371 z = pre##_pywrap(mp_##name(MP_NEW, xx, n)); \
373 MP_DROP(xx); MP_DROP(yy); \
376 SHIFTOP(mp
, lsl2c
, lsr2c
)
377 SHIFTOP(mp
, lsr2c
, lsl2c
)
378 SHIFTOP(gf
, lsl
, lsr
)
379 SHIFTOP(gf
, lsr
, lsl
)
382 #define DIVOP(pre, name, qq, rr, gather) \
383 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
386 INIT_##qq(q) INIT_##rr(r) \
387 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
389 ZDIVERR("division by zero"); \
390 pre##_div(ARG_##qq(q), ARG_##rr(r), xx, yy); \
393 MP_DROP(xx); MP_DROP(yy); \
396 #define INIT_YES(p) mp *p = MP_NEW;
398 #define ARG_YES(p) &p
400 DIVOP(mp
, divmod
, YES
, YES
,
401 Py_BuildValue("(NN)", mp_pywrap(q
), mp_pywrap(r
)))
402 DIVOP(mp
, div
, YES
, NO
, mp_pywrap(q
))
403 DIVOP(mp
, mod
, NO
, YES
, mp_pywrap(r
))
404 DIVOP(gf
, divmod
, YES
, YES
,
405 Py_BuildValue("(NN)", gf_pywrap(q
), gf_pywrap(r
)))
406 DIVOP(gf
, div
, YES
, NO
, gf_pywrap(q
))
407 DIVOP(gf
, mod
, NO
, YES
, gf_pywrap(r
))
414 static mp
*mp_modinv_checked(mp
*d
, mp
*x
, mp
*p
)
417 mp_gcd(&g
, 0, &d
, p
, x
);
418 if (!MP_EQ(g
, MP_ONE
)) {
419 MP_DROP(g
); MP_DROP(d
);
420 PyErr_SetString(PyExc_ZeroDivisionError
, "no modular inverse");
423 MP_DROP(g
); return (d
);
426 static PyObject
*mp_pyexp(PyObject
*x
, PyObject
*y
, PyObject
*z
)
428 mp
*xx
= 0, *yy
= 0, *zz
= 0;
432 if ((xx
= implicitmp(x
)) == 0 || (yy
= implicitmp(y
)) == 0 ||
433 (z
&& z
!= Py_None
&& (zz
= tomp(z
)) == 0)) {
434 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
437 if (!z
|| z
== Py_None
) {
438 if (MP_NEGP(yy
)) VALERR("negative exponent");
439 r
= mp_exp(MP_NEW
, xx
, yy
);
441 if (!MP_POSP(zz
)) VALERR("modulus must be positive");
443 if ((xx
= mp_modinv_checked(xx
, xx
, zz
)) == 0) goto end
;
448 mpmont_create(&mm
, zz
);
449 r
= mpmont_exp(&mm
, MP_NEW
, xx
, yy
);
453 mpbarrett_create(&mb
, zz
);
454 r
= mpbarrett_exp(&mb
, MP_NEW
, xx
, yy
);
455 mpbarrett_destroy(&mb
);
460 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
464 static mp
*gf_modinv_checked(mp
*d
, mp
*x
, mp
*p
)
467 gf_gcd(&g
, 0, &d
, p
, x
);
468 if (!MP_EQ(g
, MP_ONE
)) {
469 MP_DROP(g
); MP_DROP(d
);
470 PyErr_SetString(PyExc_ZeroDivisionError
, "no modular inverse");
473 MP_DROP(g
); return (d
);
476 #define BASEOP(name, radix, pre) \
477 static PyObject *mp_py##name(PyObject *x) \
478 { return mp_topystring(MP_X(x), radix, 0, pre, 0); }
480 BASEOP(hex
, 16, "0x");
483 static int mp_pynonzerop(PyObject
*x
) { return !MP_ZEROP(MP_X(x
)); }
485 static PyObject
*mp_pyint(PyObject
*x
)
488 if (!mp_tolong_checked(MP_X(x
), &l
, 0)) return (PyInt_FromLong(l
));
489 else return mp_topylong(MP_X(x
));
491 static PyObject
*mp_pylong(PyObject
*x
)
492 { return (mp_topylong(MP_X(x
))); }
493 static PyObject
*mp_pyfloat(PyObject
*x
)
495 PyObject
*l
= mp_topylong(MP_X(x
));
496 double f
= PyLong_AsDouble(l
);
498 return (PyFloat_FromDouble(f
));
501 #define COERCE(pre, PRE) \
502 static int pre##_pycoerce(PyObject **x, PyObject **y) \
506 if (PRE##_PYCHECK(*y)) { \
507 Py_INCREF(*x); Py_INCREF(*y); \
510 if ((z = tomp(*y)) != 0) { \
512 *y = pre##_pywrap(z); \
521 static int mp_pycompare(PyObject
*x
, PyObject
*y
)
522 { return mp_cmp(MP_X(x
), MP_X(y
)); }
524 static PyObject
*mp_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
530 char *kwlist
[] = { "x", "radix", 0 };
532 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|i:new", kwlist
, &x
, &radix
))
534 if (MP_PYCHECK(x
)) RETURN_OBJ(x
);
535 if (!good_radix_p(radix
, 1)) VALERR("bad radix");
536 if ((z
= mp_frompyobject(x
, radix
)) == 0) {
537 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to mp",
538 x
->ob_type
->tp_name
);
541 zz
= (mp_pyobj
*)ty
->tp_alloc(ty
, 0);
544 return ((PyObject
*)zz
);
547 static long mp_pyhash(PyObject
*me
)
550 PyObject
*l
= mp_topylong(MP_X(me
)); h
= PyObject_Hash(l
);
551 Py_DECREF(l
); return (h
);
554 static PyObject
*mpmeth_jacobi(PyObject
*me
, PyObject
*arg
)
559 if (!PyArg_ParseTuple(arg
, "O&:jacobi", convmp
, &y
)) goto end
;
560 z
= PyInt_FromLong(mp_jacobi(y
, MP_X(me
)));
566 #define BITOP(pre, name, c) \
567 static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \
570 if (!PyArg_ParseTuple(arg, "O&:" #name, convulong, &i)) return (0); \
571 return (pre##_pywrap(mp_##name##c(MP_NEW, MP_X(me), i))); \
573 BITOP(mp
, setbit
, 2c
);
574 BITOP(mp
, clearbit
, 2c
);
576 BITOP(gf
, clearbit
, );
579 static PyObject
*mpmeth_testbit(PyObject
*me
, PyObject
*arg
)
582 if (!PyArg_ParseTuple(arg
, "O&:testbit", convulong
, &i
)) return (0);
583 return (getbool(mp_testbit2c(MP_X(me
), i
)));
586 static PyObject
*gfmeth_testbit(PyObject
*me
, PyObject
*arg
)
589 if (!PyArg_ParseTuple(arg
, "O&:testbit", convulong
, &i
)) return (0);
590 return (getbool(mp_testbit(MP_X(me
), i
)));
593 static PyObject
*mpmeth_odd(PyObject
*me
, PyObject
*arg
)
598 if (!PyArg_ParseTuple(arg
, ":odd")) return (0);
599 t
= mp_odd(MP_NEW
, MP_X(me
), &s
);
600 return (Py_BuildValue("(lN)", (long)s
, mp_pywrap(t
)));
603 static PyObject
*mpmeth_sqr(PyObject
*me
, PyObject
*arg
)
605 if (!PyArg_ParseTuple(arg
, ":sqr")) return (0);
606 return (mp_pywrap(mp_sqr(MP_NEW
, MP_X(me
))));
609 static PyObject
*mpmeth_sqrt(PyObject
*me
, PyObject
*arg
)
611 if (!PyArg_ParseTuple(arg
, ":sqrt")) return (0);
612 if (MP_NEGP(MP_X(me
))) VALERR("negative root");
613 return (mp_pywrap(mp_sqrt(MP_NEW
, MP_X(me
))));
618 static PyObject
*mpmeth_gcd(PyObject
*me
, PyObject
*arg
)
620 mp
*y
= 0, *zz
= MP_NEW
;
623 if (!PyArg_ParseTuple(arg
, "O&:gcd", convmp
, &y
)) goto end
;
624 mp_gcd(&zz
, 0, 0, MP_X(me
), y
);
631 static PyObject
*mpmeth_gcdx(PyObject
*me
, PyObject
*arg
)
634 mp
*yy
= 0, *zz
= MP_NEW
, *uu
= MP_NEW
, *vv
= MP_NEW
;
636 if (!PyArg_ParseTuple(arg
, "O&:gcdx", convmp
, &yy
)) goto end
;
637 mp_gcd(&zz
, &uu
, &vv
, MP_X(me
), yy
);
638 z
= Py_BuildValue("(NNN)",
639 mp_pywrap(zz
), mp_pywrap(uu
), mp_pywrap(vv
));
645 static PyObject
*mpmeth_modinv(PyObject
*me
, PyObject
*arg
)
648 mp
*yy
= 0, *zz
= MP_NEW
;
650 if (!PyArg_ParseTuple(arg
, "O&:modinv", convmp
, &yy
) ||
651 (zz
= mp_modinv_checked(MP_NEW
, yy
, MP_X(me
))) == 0)
659 static PyObject
*mpmeth_tostring(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
662 char *kwlist
[] = { "radix", 0 };
663 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|i:tostring", kwlist
, &radix
))
665 if (!good_radix_p(radix
, 0)) VALERR("bad radix");
666 return (mp_topystring(MP_X(me
), radix
, 0, 0, 0));
671 static PyObject
*mpmeth_modsqrt(PyObject
*me
, PyObject
*arg
)
674 mp
*yy
= 0, *zz
= MP_NEW
;
676 if (!PyArg_ParseTuple(arg
, "O&:modsqrt", convmp
, &yy
)) goto end
;
677 if ((zz
= mp_modsqrt(MP_NEW
, yy
, MP_X(me
))) == 0)
678 VALERR("no modular square root");
685 #define STOREOP(name, c) \
686 static PyObject *mpmeth_##name(PyObject *me, \
687 PyObject *arg, PyObject *kw) \
690 char *kwlist[] = { "len", 0 }; \
693 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|l:" #name, \
697 len = mp_octets##c(MP_X(me)); \
700 rc = bytestring_pywrap(0, len); \
701 mp_##name(MP_X(me), PyString_AS_STRING(rc), len); \
707 STOREOP(storel2c
, 2c
)
708 STOREOP(storeb2c
, 2c
)
711 #define BUFOP(ty, pyty) \
712 static PyObject *meth__##pyty##_frombuf(PyObject *me, PyObject *arg) \
720 if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end; \
721 buf_init(&b, p, sz); \
722 if ((x = buf_getmp(&b)) == 0) VALERR("malformed data"); \
723 rc = Py_BuildValue("(NN)", ty##_pywrap(x), \
724 bytestring_pywrapbuf(&b)); \
732 static PyObject
*mpmeth_tobuf(PyObject
*me
, PyObject
*arg
)
739 if (!PyArg_ParseTuple(arg
, ":tobuf")) return (0);
741 n
= mp_octets(x
) + 3;
742 rc
= bytestring_pywrap(0, n
);
743 buf_init(&b
, PyString_AS_STRING(rc
), n
);
746 _PyString_Resize(&rc
, BLEN(&b
));
750 static PyObject
*mpmeth_primep(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
752 grand
*r
= &rand_global
;
753 char *kwlist
[] = { "rng", 0 };
756 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&", kwlist
, convgrand
, &r
))
758 rc
= getbool(pgen_primep(MP_X(me
), r
));
763 static PyObject
*mpget_nbits(PyObject
*me
, void *hunoz
)
764 { return (PyInt_FromLong(mp_bits(MP_X(me
)))); }
766 static PyObject
*mpget_noctets(PyObject
*me
, void *hunoz
)
767 { return (PyInt_FromLong(mp_octets(MP_X(me
)))); }
769 static PyObject
*mpget_noctets2c(PyObject
*me
, void *hunoz
)
770 { return (PyInt_FromLong(mp_octets2c(MP_X(me
)))); }
772 static PyGetSetDef mp_pygetset
[] = {
773 #define GETSETNAME(op, func) mp##op##_##func
774 GET (nbits
, "X.nbits -> bit length of X")
775 GET (noctets
, "X.noctets -> octet length of X")
776 GET (noctets2c
, "X.noctets2c -> two's complement octet length of X")
781 static PyMethodDef mp_pymethods
[] = {
782 #define METHNAME(func) mpmeth_##func
783 METH (jacobi
, "X.jacobi(Y) -> Jacobi symbol (Y/X) (NB inversion!)")
784 METH (setbit
, "X.setbit(N) -> X with bit N set")
785 METH (clearbit
, "X.clearbit(N) -> X with bit N clear")
786 METH (testbit
, "X.testbit(N) -> true/false if bit N set/clear in X")
787 METH (odd
, "X.odd() -> S, T where X = 2^S T with T odd")
788 METH (sqr
, "X.sqr() -> X^2")
789 METH (sqrt
, "X.sqrt() -> largest integer <= sqrt(X)")
790 METH (gcd
, "X.gcd(Y) -> gcd(X, Y)")
792 "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)")
793 METH (modinv
, "X.modinv(Y) -> multiplicative inverse of Y mod X")
794 METH (modsqrt
, "X.modsqrt(Y) -> square root of Y mod X, if X prime")
795 KWMETH(primep
, "X.primep(rng = rand) -> true/false if X is prime")
796 KWMETH(tostring
, "X.tostring(radix = 10) -> STR")
797 KWMETH(storel
, "X.storel(len = -1) -> little-endian bytes")
798 KWMETH(storeb
, "X.storeb(len = -1) -> big-endian bytes")
800 "X.storel2c(len = -1) -> little-endian bytes, two's complement")
802 "X.storeb2c(len = -1) -> big-endian bytes, two's complement")
803 METH (tobuf
, "X.tobuf() -> buffer format")
808 static PyNumberMethods mp_pynumber
= {
809 mp_pyadd
, /* @nb_add@ */
810 mp_pysub
, /* @nb_subtract@ */
811 mp_pymul
, /* @nb_multiply@ */
813 mp_pymod
, /* @nb_remainder@ */
814 mp_pydivmod
, /* @nb_divmod@ */
815 mp_pyexp
, /* @nb_power@ */
816 mp_pyneg
, /* @nb_negative@ */
817 mp_pyid
, /* @nb_positive@ */
818 mp_pyabs
, /* @nb_absolute@ */
819 mp_pynonzerop
, /* @nb_nonzero@ */
820 mp_pynot2c
, /* @nb_invert@ */
821 mp_pylsl2c
, /* @nb_lshift@ */
822 mp_pylsr2c
, /* @nb_rshift@ */
823 mp_pyand2c
, /* @nb_and@ */
824 mp_pyxor2c
, /* @nb_xor@ */
825 mp_pyor2c
, /* @nb_or@ */
826 mp_pycoerce
, /* @nb_coerce@ */
827 mp_pyint
, /* @nb_int@ */
828 mp_pylong
, /* @nb_long@ */
829 mp_pyfloat
, /* @nb_float@ */
830 mp_pyoct
, /* @nb_oct@ */
831 mp_pyhex
, /* @nb_hex@ */
833 0, /* @nb_inplace_add@ */
834 0, /* @nb_inplace_subtract@ */
835 0, /* @nb_inplace_multiply@ */
836 0, /* @nb_inplace_divide@ */
837 0, /* @nb_inplace_remainder@ */
838 0, /* @nb_inplace_power@ */
839 0, /* @nb_inplace_lshift@ */
840 0, /* @nb_inplace_rshift@ */
841 0, /* @nb_inplace_and@ */
842 0, /* @nb_inplace_xor@ */
843 0, /* @nb_inplace_or@ */
845 mp_pydiv
, /* @nb_floor_divide@ */
846 0, /* @nb_true_divide@ */
847 0, /* @nb_inplace_floor_divide@ */
848 0, /* @nb_inplace_true_divide@ */
851 static PyTypeObject mp_pytype_skel
= {
852 PyObject_HEAD_INIT(0) 0, /* Header */
853 "MP", /* @tp_name@ */
854 sizeof(mp_pyobj
), /* @tp_basicsize@ */
855 0, /* @tp_itemsize@ */
857 mp_pydealloc
, /* @tp_dealloc@ */
859 0, /* @tp_getattr@ */
860 0, /* @tp_setattr@ */
861 mp_pycompare
, /* @tp_compare@ */
862 mp_pyrepr
, /* @tp_repr@ */
863 &mp_pynumber
, /* @tp_as_number@ */
864 0, /* @tp_as_sequence@ */
865 0, /* @tp_as_mapping@ */
866 mp_pyhash
, /* @tp_hash@ */
868 mp_pystr
, /* @tp_str@ */
869 0, /* @tp_getattro@ */
870 0, /* @tp_setattro@ */
871 0, /* @tp_as_buffer@ */
872 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
873 Py_TPFLAGS_CHECKTYPES
|
877 "Multiprecision integers, similar to `long' but more efficient and\n\
878 versatile. Support all the standard arithmetic operations.\n\
880 Constructor mp(X, radix = R) attempts to convert X to an `mp'. If\n\
881 X is a string, it's read in radix-R form, or we look for a prefix\n\
882 if R = 0. Other acceptable things are ints and longs.\n\
886 * Use `//' for division. MPs don't have `/' division.",
888 0, /* @tp_traverse@ */
890 0, /* @tp_richcompare@ */
891 0, /* @tp_weaklistoffset@ */
893 0, /* @tp_iternext@ */
894 mp_pymethods
, /* @tp_methods@ */
895 0, /* @tp_members@ */
896 mp_pygetset
, /* @tp_getset@ */
899 0, /* @tp_descr_get@ */
900 0, /* @tp_descr_set@ */
901 0, /* @tp_dictoffset@ */
903 PyType_GenericAlloc
, /* @tp_alloc@ */
904 mp_pynew
, /* @tp_new@ */
909 static PyObject
*meth__MP_fromstring(PyObject
*me
,
910 PyObject
*arg
, PyObject
*kw
)
918 char *kwlist
[] = { "class", "x", "radix", 0 };
920 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "Os#|i:fromstring",
921 kwlist
, &me
, &p
, &len
, &r
))
923 if (!good_radix_p(r
, 1)) VALERR("bad radix");
924 sc
.buf
= p
; sc
.lim
= p
+ len
;
925 if ((zz
= mp_read(MP_NEW
, r
, &mptext_stringops
, &sc
)) == 0)
926 VALERR("bad integer");
927 z
= Py_BuildValue("(Ns#)", mp_pywrap(zz
), sc
.buf
, (int)(sc
.lim
- sc
.buf
));
932 static PyObject
*meth__MP_factorial(PyObject
*me
, PyObject
*arg
)
936 if (!PyArg_ParseTuple(arg
, "OO&:factorial", &me
, convulong
, &i
))
942 static PyObject
*meth__MP_fibonacci(PyObject
*me
, PyObject
*arg
)
946 if (!PyArg_ParseTuple(arg
, "Ol:fibonacci", &me
, &i
)) return (0);
951 #define LOADOP(pre, py, name) \
952 static PyObject *meth__##py##_##name(PyObject *me, PyObject *arg) \
956 if (!PyArg_ParseTuple(arg, "Os#:" #name, &me, &p, &len)) return (0); \
957 return (pre##_pywrap(mp_##name(MP_NEW, p, len))); \
959 LOADOP(mp
, MP
, loadl
)
960 LOADOP(mp
, MP
, loadb
)
961 LOADOP(mp
, MP
, loadl2c
)
962 LOADOP(mp
, MP
, loadb2c
)
963 LOADOP(gf
, GF
, loadl
)
964 LOADOP(gf
, GF
, loadb
)
967 /*----- Products of small integers ----------------------------------------*/
969 typedef struct mpmul_pyobj
{
975 #define MPMUL_LIVEP(o) (((mpmul_pyobj *)(o))->livep)
976 #define MPMUL_PY(o) (&((mpmul_pyobj *)(o))->mm)
978 static void mpmul_pydealloc(PyObject
*me
)
981 mp_drop(mpmul_done(MPMUL_PY(me
)));
985 static PyObject
*mmmeth_factor(PyObject
*me
, PyObject
*arg
)
990 if (!MPMUL_LIVEP(me
)) VALERR("MPMul object invalid");
991 if (PyTuple_Size(arg
) != 1)
992 i
= PyObject_GetIter(arg
);
994 if ((q
= PyTuple_GetItem(arg
, 0)) == 0) goto end
;
995 if ((i
= PyObject_GetIter(q
)) == 0) {
996 PyErr_Clear(); /* that's ok */
997 i
= PyObject_GetIter(arg
);
1001 while ((q
= PyIter_Next(i
)) != 0) {
1002 x
= getmp(q
); Py_DECREF(q
); if (!x
) {
1006 mpmul_add(MPMUL_PY(me
), x
);
1015 static PyObject
*mmmeth_done(PyObject
*me
, PyObject
*arg
)
1019 if (!PyArg_ParseTuple(arg
, ":done")) goto end
;
1020 if (!MPMUL_LIVEP(me
)) VALERR("MPMul object invalid");
1021 x
= mpmul_done(MPMUL_PY(me
));
1022 MPMUL_LIVEP(me
) = 0;
1023 return (mp_pywrap(x
));
1028 static PyObject
*mpmul_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1032 if (kw
) TYERR("keyword arguments not allowed here");
1033 mm
= (mpmul_pyobj
*)ty
->tp_alloc(ty
, 0);
1034 mpmul_init(&mm
->mm
);
1036 if (mmmeth_factor((PyObject
*)mm
, arg
) == 0) {
1040 return ((PyObject
*)mm
);
1045 static PyObject
*mmget_livep(PyObject
*me
, void *hunoz
)
1046 { return (getbool(MPMUL_LIVEP(me
))); }
1048 static PyGetSetDef mpmul_pygetset
[] = {
1049 #define GETSETNAME(op, name) mm##op##_##name
1050 GET (livep
, "MM.livep -> flag: object still valid?")
1055 static PyMethodDef mpmul_pymethods
[] = {
1056 #define METHNAME(name) mmmeth_##name
1057 METH (factor
, "MM.factor(ITERABLE) or MM.factor(I, ...)")
1058 METH (done
, "MM.done() -> PRODUCT")
1063 static PyTypeObject
*mpmul_pytype
, mpmul_pytype_skel
= {
1064 PyObject_HEAD_INIT(0) 0, /* Header */
1065 "MPMul", /* @tp_name@ */
1066 sizeof(mpmul_pyobj
), /* @tp_basicsize@ */
1067 0, /* @tp_itemsize@ */
1069 mpmul_pydealloc
, /* @tp_dealloc@ */
1071 0, /* @tp_getattr@ */
1072 0, /* @tp_setattr@ */
1073 0, /* @tp_compare@ */
1075 0, /* @tp_as_number@ */
1076 0, /* @tp_as_sequence@ */
1077 0, /* @tp_as_mapping@ */
1081 0, /* @tp_getattro@ */
1082 0, /* @tp_setattro@ */
1083 0, /* @tp_as_buffer@ */
1084 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1085 Py_TPFLAGS_BASETYPE
,
1088 "An object for multiplying many small integers.",
1090 0, /* @tp_traverse@ */
1092 0, /* @tp_richcompare@ */
1093 0, /* @tp_weaklistoffset@ */
1095 0, /* @tp_iternext@ */
1096 mpmul_pymethods
, /* @tp_methods@ */
1097 0, /* @tp_members@ */
1098 mpmul_pygetset
, /* @tp_getset@ */
1101 0, /* @tp_descr_get@ */
1102 0, /* @tp_descr_set@ */
1103 0, /* @tp_dictoffset@ */
1105 PyType_GenericAlloc
, /* @tp_alloc@ */
1106 mpmul_pynew
, /* @tp_new@ */
1111 /*----- Montgomery reduction ----------------------------------------------*/
1113 typedef struct mpmont_pyobj
{
1118 #define MPMONT_PY(o) (&((mpmont_pyobj *)(o))->mm)
1120 static PyObject
*mmmeth_int(PyObject
*me
, PyObject
*arg
)
1124 mpmont
*mm
= MPMONT_PY(me
);
1126 if (!PyArg_ParseTuple(arg
, "O&:in", convmp
, &yy
))
1128 mp_div(0, &yy
, yy
, mm
->m
);
1129 z
= mp_pywrap(mpmont_mul(mm
, MP_NEW
, yy
, mm
->r2
));
1131 if (yy
) MP_DROP(yy
);
1135 static PyObject
*mmmeth_mul(PyObject
*me
, PyObject
*arg
)
1138 mp
*yy
= 0, *zz
= 0;
1140 if (!PyArg_ParseTuple(arg
, "O&O&:mul", convmp
, &yy
, convmp
, &zz
))
1142 rc
= mp_pywrap(mpmont_mul(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1144 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1148 static PyObject
*mmmeth_exp(PyObject
*me
, PyObject
*arg
)
1151 mp
*yy
= 0, *zz
= 0;
1153 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1156 if ((yy
= mp_modinv_checked(yy
, yy
, MPMONT_PY(me
)->m
)) == 0) goto end
;
1157 zz
= mp_neg(zz
, zz
);
1159 rc
= mp_pywrap(mpmont_exp(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1161 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1165 static PyObject
*mmmeth_expr(PyObject
*me
, PyObject
*arg
)
1168 mp
*yy
= 0, *zz
= 0;
1170 if (!PyArg_ParseTuple(arg
, "O&O&:expr", convmp
, &yy
, convmp
, &zz
))
1173 yy
= mpmont_reduce(MPMONT_PY(me
), yy
, yy
);
1174 if ((yy
= mp_modinv_checked(yy
, yy
, MPMONT_PY(me
)->m
)) == 0) goto end
;
1175 yy
= mpmont_mul(MPMONT_PY(me
), yy
, yy
, MPMONT_PY(me
)->r2
);
1176 zz
= mp_neg(zz
, zz
);
1178 rc
= mp_pywrap(mpmont_expr(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1180 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1184 static PyObject
*mm_mexpr_id(PyObject
*me
)
1185 { return mp_pywrap(MP_COPY(MPMONT_PY(me
)->r
)); }
1187 static int mm_mexpr_fill(void *p
, PyObject
*me
, PyObject
*x
, PyObject
*y
)
1189 mp
*xx
= 0, *yy
= 0;
1190 mp_expfactor
*f
= p
;
1191 mpmont
*mm
= MPMONT_PY(me
);
1193 if ((xx
= getmp(x
)) == 0 || (yy
= getmp(y
)) == 0)
1196 xx
= mpmont_reduce(mm
, xx
, xx
);
1197 if ((xx
= mp_modinv_checked(xx
, xx
, yy
)) == 0)
1199 xx
= mpmont_mul(mm
, xx
, xx
, mm
->r2
);
1200 yy
= mp_neg(yy
, yy
);
1207 mp_drop(xx
); mp_drop(yy
);
1211 static PyObject
*mm_mexpr(PyObject
*me
, void *v
, int n
)
1212 { return mp_pywrap(mpmont_mexpr(MPMONT_PY(me
), MP_NEW
, v
, n
)); }
1214 static void mp_mexp_drop(void *p
)
1216 mp_expfactor
*f
= p
;
1221 static PyObject
*mmmeth_mexpr(PyObject
*me
, PyObject
*arg
)
1223 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1224 mm_mexpr_id
, mm_mexpr_fill
, mm_mexpr
, mp_mexp_drop
);
1227 static PyObject
*mp_mexp_id(PyObject
*me
)
1228 { return mp_pywrap(MP_ONE
); }
1230 static int mp_mexp_fill(void *p
, PyObject
*me
, PyObject
*x
, PyObject
*y
)
1232 mp
*xx
= 0, *yy
= 0;
1233 mp_expfactor
*f
= p
;
1235 if ((xx
= getmp(x
)) == 0 || (yy
= getmp(y
)) == 0)
1238 if ((xx
= mp_modinv_checked(xx
, xx
, yy
)) == 0)
1240 yy
= mp_neg(yy
, yy
);
1247 mp_drop(xx
); mp_drop(yy
);
1251 static PyObject
*mm_mexp(PyObject
*me
, void *v
, int n
)
1252 { return mp_pywrap(mpmont_mexp(MPMONT_PY(me
), MP_NEW
, v
, n
)); }
1254 static PyObject
*mmmeth_mexp(PyObject
*me
, PyObject
*arg
)
1256 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1257 mp_mexp_id
, mp_mexp_fill
, mm_mexp
, mp_mexp_drop
);
1260 #define mmmeth_ext mmmeth_reduce
1261 static PyObject
*mmmeth_reduce(PyObject
*me
, PyObject
*arg
)
1266 if (!PyArg_ParseTuple(arg
, "O&", convmp
, &yy
)) goto end
;
1267 z
= mp_pywrap(mpmont_reduce(MPMONT_PY(me
), MP_NEW
, yy
));
1272 static void mpmont_pydealloc(PyObject
*me
)
1274 mpmont_destroy(MPMONT_PY(me
));
1278 static PyObject
*mpmont_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1280 mpmont_pyobj
*mm
= 0;
1281 char *kwlist
[] = { "m", 0 };
1284 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convmp
, &xx
))
1286 if (!MP_POSP(xx
) || !MP_ODDP(xx
)) VALERR("m must be positive and odd");
1287 mm
= (mpmont_pyobj
*)ty
->tp_alloc(ty
, 0);
1288 mpmont_create(&mm
->mm
, xx
);
1290 if (xx
) MP_DROP(xx
);
1291 return ((PyObject
*)mm
);
1294 static PyObject
*mmget_m(PyObject
*me
, void *hunoz
)
1295 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->m
))); }
1297 static PyObject
*mmget_r(PyObject
*me
, void *hunoz
)
1298 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->r
))); }
1300 static PyObject
*mmget_r2(PyObject
*me
, void *hunoz
)
1301 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->r2
))); }
1303 static PyGetSetDef mpmont_pygetset
[] = {
1304 #define GETSETNAME(op, name) mm##op##_##name
1305 GET (m
, "M.m -> modulus for reduction")
1306 GET (r
, "M.r -> multiplicative identity")
1307 GET (r2
, "M.r2 -> M.r^2, Montgomerization factor")
1312 static PyMethodDef mpmont_pymethods
[] = {
1313 #define METHNAME(name) mmmeth_##name
1314 METH (int, "M.int(X) -> XR")
1315 METH (mul
, "M.mul(XR, YR) -> ZR where Z = X Y")
1316 METH (expr
, "M.expr(XR, N) -> ZR where Z = X^N mod M.m")
1318 M.mexpr([(XR0, N0), (XR1, N1), ...]) = ZR where Z = X0^N0 X1^N1 ... mod M.m\n\
1319 \t(the list may be flattened if this more convenient.)")
1320 METH (reduce
, "M.reduce(XR) -> X")
1321 METH (ext
, "M.ext(XR) -> X")
1322 METH (exp
, "M.exp(X, N) -> X^N mod M.m")
1324 M.mexp([(X0, N0), (X1, N1), ...]) = X0^N0 X1^N1 ... mod M.m\n\
1325 \t(the list may be flattened if this more convenient.)")
1330 static PyTypeObject
*mpmont_pytype
, mpmont_pytype_skel
= {
1331 PyObject_HEAD_INIT(0) 0, /* Header */
1332 "MPMont", /* @tp_name@ */
1333 sizeof(mpmont_pyobj
), /* @tp_basicsize@ */
1334 0, /* @tp_itemsize@ */
1336 mpmont_pydealloc
, /* @tp_dealloc@ */
1338 0, /* @tp_getattr@ */
1339 0, /* @tp_setattr@ */
1340 0, /* @tp_compare@ */
1342 0, /* @tp_as_number@ */
1343 0, /* @tp_as_sequence@ */
1344 0, /* @tp_as_mapping@ */
1348 0, /* @tp_getattro@ */
1349 0, /* @tp_setattro@ */
1350 0, /* @tp_as_buffer@ */
1351 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1352 Py_TPFLAGS_BASETYPE
,
1355 "A Montgomery reduction context.",
1357 0, /* @tp_traverse@ */
1359 0, /* @tp_richcompare@ */
1360 0, /* @tp_weaklistoffset@ */
1362 0, /* @tp_iternext@ */
1363 mpmont_pymethods
, /* @tp_methods@ */
1364 0, /* @tp_members@ */
1365 mpmont_pygetset
, /* @tp_getset@ */
1368 0, /* @tp_descr_get@ */
1369 0, /* @tp_descr_set@ */
1370 0, /* @tp_dictoffset@ */
1372 PyType_GenericAlloc
, /* @tp_alloc@ */
1373 mpmont_pynew
, /* @tp_new@ */
1378 /*----- Barrett reduction -------------------------------------------------*/
1380 typedef struct mpbarrett_pyobj
{
1385 #define MPBARRETT_PY(o) (&((mpbarrett_pyobj *)(o))->mb)
1387 static PyObject
*mbmeth_exp(PyObject
*me
, PyObject
*arg
)
1390 mp
*yy
= 0, *zz
= 0;
1392 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1395 if ((yy
= mp_modinv_checked(yy
, yy
, MPBARRETT_PY(me
)->m
)) == 0) goto end
;
1396 zz
= mp_neg(zz
, zz
);
1398 rc
= mp_pywrap(mpbarrett_exp(MPBARRETT_PY(me
), MP_NEW
, yy
, zz
));
1400 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1404 static PyObject
*mb_mexp(PyObject
*me
, void *v
, int n
)
1405 { return mp_pywrap(mpbarrett_mexp(MPBARRETT_PY(me
), MP_NEW
, v
, n
)); }
1407 static PyObject
*mbmeth_mexp(PyObject
*me
, PyObject
*arg
)
1409 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1410 mp_mexp_id
, mp_mexp_fill
, mb_mexp
, mp_mexp_drop
);
1413 static PyObject
*mbmeth_reduce(PyObject
*me
, PyObject
*arg
)
1418 if (!PyArg_ParseTuple(arg
, "O&:reduce", convmp
, &yy
))
1420 z
= mp_pywrap(mpbarrett_reduce(MPBARRETT_PY(me
), MP_NEW
, yy
));
1425 static void mpbarrett_pydealloc(PyObject
*me
)
1427 mpbarrett_destroy(MPBARRETT_PY(me
));
1431 static PyObject
*mpbarrett_pynew(PyTypeObject
*ty
,
1432 PyObject
*arg
, PyObject
*kw
)
1434 mpbarrett_pyobj
*mb
= 0;
1435 char *kwlist
[] = { "m", 0 };
1438 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convmp
, &xx
))
1440 if (!MP_POSP(xx
)) VALERR("m must be positive");
1441 mb
= (mpbarrett_pyobj
*)ty
->tp_alloc(ty
, 0);
1442 mpbarrett_create(&mb
->mb
, xx
);
1444 if (xx
) MP_DROP(xx
);
1445 return ((PyObject
*)mb
);
1448 static PyObject
*mbget_m(PyObject
*me
, void *hunoz
)
1449 { return (mp_pywrap(MP_COPY(MPBARRETT_PY(me
)->m
))); }
1451 static PyGetSetDef mpbarrett_pygetset
[] = {
1452 #define GETSETNAME(op, name) mb##op##_##name
1453 GET (m
, "B.m -> modulus for reduction")
1458 static PyMethodDef mpbarrett_pymethods
[] = {
1459 #define METHNAME(name) mbmeth_##name
1460 METH (reduce
, "B.reduce(X) -> X mod B.m")
1461 METH (exp
, "B.exp(X, N) -> X^N mod B.m")
1463 B.mexp([(X0, N0), (X1, N1), ...]) = X0^N0 X1^N1 ... mod B.m\n\
1464 \t(the list may be flattened if this more convenient.)")
1469 static PyTypeObject
*mpbarrett_pytype
, mpbarrett_pytype_skel
= {
1470 PyObject_HEAD_INIT(0) 0, /* Header */
1471 "MPBarrett", /* @tp_name@ */
1472 sizeof(mpbarrett_pyobj
), /* @tp_basicsize@ */
1473 0, /* @tp_itemsize@ */
1475 mpbarrett_pydealloc
, /* @tp_dealloc@ */
1477 0, /* @tp_getattr@ */
1478 0, /* @tp_setattr@ */
1479 0, /* @tp_compare@ */
1481 0, /* @tp_as_number@ */
1482 0, /* @tp_as_sequence@ */
1483 0, /* @tp_as_mapping@ */
1487 0, /* @tp_getattro@ */
1488 0, /* @tp_setattro@ */
1489 0, /* @tp_as_buffer@ */
1490 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1491 Py_TPFLAGS_BASETYPE
,
1494 "A Barrett reduction context.",
1496 0, /* @tp_traverse@ */
1498 0, /* @tp_richcompare@ */
1499 0, /* @tp_weaklistoffset@ */
1501 0, /* @tp_iternext@ */
1502 mpbarrett_pymethods
, /* @tp_methods@ */
1503 0, /* @tp_members@ */
1504 mpbarrett_pygetset
, /* @tp_getset@ */
1507 0, /* @tp_descr_get@ */
1508 0, /* @tp_descr_set@ */
1509 0, /* @tp_dictoffset@ */
1511 PyType_GenericAlloc
, /* @tp_alloc@ */
1512 mpbarrett_pynew
, /* @tp_new@ */
1517 /*----- Nice prime reduction ----------------------------------------------*/
1519 typedef struct mpreduce_pyobj
{
1524 #define MPREDUCE_PY(o) (&((mpreduce_pyobj *)(o))->mr)
1526 static PyObject
*mrmeth_exp(PyObject
*me
, PyObject
*arg
)
1529 mp
*yy
= 0, *zz
= 0;
1531 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1534 if ((yy
= mp_modinv_checked(yy
, yy
, MPREDUCE_PY(me
)->p
)) == 0) goto end
;
1535 zz
= mp_neg(zz
, zz
);
1537 rc
= mp_pywrap(mpreduce_exp(MPREDUCE_PY(me
), MP_NEW
, yy
, zz
));
1539 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1543 static PyObject
*mrmeth_reduce(PyObject
*me
, PyObject
*arg
)
1548 if (!PyArg_ParseTuple(arg
, "O&:reduce", convmp
, &yy
)) goto end
;
1549 z
= mp_pywrap(mpreduce_do(MPREDUCE_PY(me
), MP_NEW
, yy
));
1554 static void mpreduce_pydealloc(PyObject
*me
)
1556 mpreduce_destroy(MPREDUCE_PY(me
));
1560 static PyObject
*mpreduce_pynew(PyTypeObject
*ty
,
1561 PyObject
*arg
, PyObject
*kw
)
1563 mpreduce_pyobj
*mr
= 0;
1565 char *kwlist
[] = { "m", 0 };
1568 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convmp
, &xx
))
1570 if (!MP_POSP(xx
)) VALERR("m must be positive");
1571 if (mpreduce_create(&r
, xx
)) VALERR("bad modulus (must be 2^k - ...)");
1572 mr
= (mpreduce_pyobj
*)ty
->tp_alloc(ty
, 0);
1575 if (xx
) MP_DROP(xx
);
1576 return ((PyObject
*)mr
);
1579 static PyObject
*mrget_m(PyObject
*me
, void *hunoz
)
1580 { return (mp_pywrap(MP_COPY(MPREDUCE_PY(me
)->p
))); }
1582 static PyGetSetDef mpreduce_pygetset
[] = {
1583 #define GETSETNAME(op, name) mr##op##_##name
1584 GET (m
, "R.m -> modulus for reduction")
1589 static PyMethodDef mpreduce_pymethods
[] = {
1590 #define METHNAME(name) mrmeth_##name
1591 METH (reduce
, "R.reduce(X) -> X mod B.m")
1592 METH (exp
, "R.exp(X, N) -> X^N mod B.m")
1597 static PyTypeObject
*mpreduce_pytype
, mpreduce_pytype_skel
= {
1598 PyObject_HEAD_INIT(0) 0, /* Header */
1599 "MPReduce", /* @tp_name@ */
1600 sizeof(mpreduce_pyobj
), /* @tp_basicsize@ */
1601 0, /* @tp_itemsize@ */
1603 mpreduce_pydealloc
, /* @tp_dealloc@ */
1605 0, /* @tp_getattr@ */
1606 0, /* @tp_setattr@ */
1607 0, /* @tp_compare@ */
1609 0, /* @tp_as_number@ */
1610 0, /* @tp_as_sequence@ */
1611 0, /* @tp_as_mapping@ */
1615 0, /* @tp_getattro@ */
1616 0, /* @tp_setattro@ */
1617 0, /* @tp_as_buffer@ */
1618 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1619 Py_TPFLAGS_BASETYPE
,
1622 "A reduction context for reduction modulo primes of special form.",
1624 0, /* @tp_traverse@ */
1626 0, /* @tp_richcompare@ */
1627 0, /* @tp_weaklistoffset@ */
1629 0, /* @tp_iternext@ */
1630 mpreduce_pymethods
, /* @tp_methods@ */
1631 0, /* @tp_members@ */
1632 mpreduce_pygetset
, /* @tp_getset@ */
1635 0, /* @tp_descr_get@ */
1636 0, /* @tp_descr_set@ */
1637 0, /* @tp_dictoffset@ */
1639 PyType_GenericAlloc
, /* @tp_alloc@ */
1640 mpreduce_pynew
, /* @tp_new@ */
1645 /*----- Chinese Remainder Theorem solution --------------------------------*/
1647 typedef struct mpcrt_pyobj
{
1652 #define MPCRT_PY(o) (&((mpcrt_pyobj *)(o))->c)
1654 static PyObject
*mcmeth_solve(PyObject
*me
, PyObject
*arg
)
1656 mpcrt
*c
= MPCRT_PY(me
);
1657 PyObject
*q
= 0, *x
, *z
= 0;
1660 int i
= 0, n
= c
->k
;
1663 if (PyTuple_Size(arg
) == n
)
1665 else if (!PyArg_ParseTuple(arg
, "O:solve", &q
))
1668 if (!PySequence_Check(q
)) TYERR("want a sequence of residues");
1669 if (PySequence_Size(q
) != n
) VALERR("residue count mismatch");
1670 v
= xmalloc(n
* sizeof(*v
));
1671 for (i
= 0; i
< n
; i
++) {
1672 if ((x
= PySequence_GetItem(q
, i
)) == 0) goto end
;
1673 xx
= getmp(x
); Py_DECREF(x
); if (!xx
) goto end
;
1676 z
= mp_pywrap(mpcrt_solve(c
, MP_NEW
, v
));
1680 for (i
= 0; i
< n
; i
++)
1689 static void mpcrt_pydealloc(PyObject
*me
)
1691 mpcrt
*c
= MPCRT_PY(me
);
1696 static PyObject
*mpcrt_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1700 char *kwlist
[] = { "mv", 0 };
1701 PyObject
*q
= 0, *x
;
1705 if (PyTuple_Size(arg
) > 1)
1707 else if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", kwlist
, &q
))
1710 if (!PySequence_Check(q
)) TYERR("want a sequence of moduli");
1711 n
= PySequence_Size(q
);
1712 if (PyErr_Occurred()) goto end
;
1713 if (!n
) VALERR("want at least one modulus");
1714 v
= xmalloc(n
* sizeof(*v
));
1715 for (i
= 0; i
< n
; i
++) {
1716 if ((x
= PySequence_GetItem(q
, i
)) == 0) goto end
;
1717 xx
= getmp(x
); Py_DECREF(x
); if (!xx
) goto end
;
1718 v
[i
].m
= xx
; v
[i
].n
= 0; v
[i
].ni
= 0; v
[i
].nni
= 0;
1720 c
= (mpcrt_pyobj
*)ty
->tp_alloc(ty
, 0);
1721 mpcrt_create(&c
->c
, v
, n
, 0);
1723 return ((PyObject
*)c
);
1728 for (i
= 0; i
< n
; i
++)
1736 static PyObject
*mcget_product(PyObject
*me
, void *hunoz
)
1737 { return (mp_pywrap(MP_COPY(MPCRT_PY(me
)->mb
.m
))); }
1739 static PyObject
*mcget_moduli(PyObject
*me
, void *hunoz
)
1743 mpcrt
*c
= MPCRT_PY(me
);
1745 if ((q
= PyList_New(c
->k
)) == 0) return (0);
1746 for (i
= 0; i
< c
->k
; i
++)
1747 PyList_SetItem(q
, i
, mp_pywrap(c
->v
[i
].m
));
1751 static PyGetSetDef mpcrt_pygetset
[] = {
1752 #define GETSETNAME(op, name) mc##op##_##name
1753 GET (product
, "C.product -> product of moduli")
1754 GET (moduli
, "C.moduli -> list of individual moduli")
1759 static PyMethodDef mpcrt_pymethods
[] = {
1760 #define METHNAME(name) mcmeth_##name
1761 METH (solve
, "C.solve([R0, R1]) -> X mod C.product")
1766 static PyTypeObject
*mpcrt_pytype
, mpcrt_pytype_skel
= {
1767 PyObject_HEAD_INIT(0) 0, /* Header */
1768 "MPCRT", /* @tp_name@ */
1769 sizeof(mpcrt_pyobj
), /* @tp_basicsize@ */
1770 0, /* @tp_itemsize@ */
1772 mpcrt_pydealloc
, /* @tp_dealloc@ */
1774 0, /* @tp_getattr@ */
1775 0, /* @tp_setattr@ */
1776 0, /* @tp_compare@ */
1778 0, /* @tp_as_number@ */
1779 0, /* @tp_as_sequence@ */
1780 0, /* @tp_as_mapping@ */
1784 0, /* @tp_getattro@ */
1785 0, /* @tp_setattro@ */
1786 0, /* @tp_as_buffer@ */
1787 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1788 Py_TPFLAGS_BASETYPE
,
1791 "A context for the solution of Chinese Remainder Theorem problems.",
1793 0, /* @tp_traverse@ */
1795 0, /* @tp_richcompare@ */
1796 0, /* @tp_weaklistoffset@ */
1798 0, /* @tp_iternext@ */
1799 mpcrt_pymethods
, /* @tp_methods@ */
1800 0, /* @tp_members@ */
1801 mpcrt_pygetset
, /* @tp_getset@ */
1804 0, /* @tp_descr_get@ */
1805 0, /* @tp_descr_set@ */
1806 0, /* @tp_dictoffset@ */
1808 PyType_GenericAlloc
, /* @tp_alloc@ */
1809 mpcrt_pynew
, /* @tp_new@ */
1814 /*----- Binary polynomials ------------------------------------------------*/
1816 static PyObject
*gf_pyrepr(PyObject
*o
)
1817 { return mp_topystring(MP_X(o
), 16, "GF(", "0x", "L)"); }
1819 static PyObject
*gf_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
1825 if (gfbinop(x
, y
, &xx
, &yy
)) RETURN_NOTIMPL
;
1827 case Py_EQ
: b
= MP_EQ(xx
, yy
); break;
1828 case Py_NE
: b
= !MP_EQ(xx
, yy
); break;
1833 case Py_LT
: b
= xl
< yl
; break;
1834 case Py_LE
: b
= xl
<= yl
; break;
1835 case Py_GT
: b
= xl
> yl
; break;
1836 case Py_GE
: b
= xl
>= yl
; break;
1841 MP_DROP(xx
); MP_DROP(yy
);
1842 return (getbool(b
));
1845 static PyObject
*gf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1851 char *kwlist
[] = { "x", "radix", 0 };
1853 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|i:gf", kwlist
, &x
, &radix
))
1855 if (GF_PYCHECK(x
)) RETURN_OBJ(x
);
1856 if (!good_radix_p(radix
, 1)) VALERR("radix out of range");
1857 if ((z
= mp_frompyobject(x
, radix
)) == 0) {
1858 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to gf",
1859 x
->ob_type
->tp_name
);
1864 VALERR("gf cannot be negative");
1866 zz
= (mp_pyobj
*)ty
->tp_alloc(ty
, 0);
1869 return ((PyObject
*)zz
);
1872 static long gf_pyhash(PyObject
*me
)
1874 long i
= mp_tolong(MP_X(me
));
1875 i
^= 0xc7ecd67c; /* random perturbance */
1881 static PyObject
*gf_pyexp(PyObject
*x
, PyObject
*y
, PyObject
*z
)
1883 mp
*xx
= 0, *yy
= 0, *zz
= 0;
1887 if ((xx
= tomp(x
)) == 0 || (yy
= tomp(y
)) == 0 ||
1888 (z
&& z
!= Py_None
&& (zz
= tomp(z
)) == 0)) {
1889 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
1892 if (!z
|| z
== Py_None
) {
1893 if (MP_NEGP(yy
)) VALERR("negative exponent");
1894 r
= gf_exp(MP_NEW
, xx
, yy
);
1897 if (MP_ZEROP(zz
)) ZDIVERR("zero modulus");
1899 if ((xx
= gf_modinv_checked(xx
, xx
, zz
)) == 0) goto end
;
1900 yy
= mp_neg(yy
, yy
);
1902 gfreduce_create(&gr
, zz
);
1903 r
= gfreduce_exp(&gr
, MP_NEW
, xx
, yy
);
1904 gfreduce_destroy(&gr
);
1908 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
1912 static PyObject
*gfmeth_sqr(PyObject
*me
, PyObject
*arg
)
1914 if (!PyArg_ParseTuple(arg
, ":sqr")) return (0);
1915 return (gf_pywrap(gf_sqr(MP_NEW
, MP_X(me
))));
1918 static PyObject
*gfmeth_gcd(PyObject
*me
, PyObject
*arg
)
1921 mp
*yy
= 0, *zz
= MP_NEW
;
1923 if (!PyArg_ParseTuple(arg
, "O&:gcd", convgf
, &yy
)) goto end
;
1924 gf_gcd(&zz
, 0, 0, MP_X(me
), yy
);
1927 if (yy
) MP_DROP(yy
);
1931 static PyObject
*gfmeth_gcdx(PyObject
*me
, PyObject
*arg
)
1934 mp
*yy
= 0, *zz
= MP_NEW
, *uu
= MP_NEW
, *vv
= MP_NEW
;
1936 if (!PyArg_ParseTuple(arg
, "O&:gcdx", convgf
, &yy
))
1938 gf_gcd(&zz
, &uu
, &vv
, MP_X(me
), yy
);
1939 z
= Py_BuildValue("(NNN)",
1940 gf_pywrap(zz
), gf_pywrap(uu
), gf_pywrap(vv
));
1942 if (yy
) MP_DROP(yy
);
1946 static PyObject
*gfmeth_modinv(PyObject
*me
, PyObject
*arg
)
1949 mp
*yy
= 0, *zz
= MP_NEW
;
1951 if (!PyArg_ParseTuple(arg
, "O&:modinv", convgf
, &yy
) ||
1952 (zz
= gf_modinv_checked(MP_NEW
, yy
, MP_X(me
))) == 0)
1956 if (yy
) MP_DROP(yy
);
1960 static PyObject
*gfmeth_irreduciblep(PyObject
*me
, PyObject
*arg
)
1962 if (!PyArg_ParseTuple(arg
, ":irreduciblep")) return (0);
1963 return getbool(gf_irreduciblep(MP_X(me
)));
1966 static PyObject
*gfget_degree(PyObject
*me
, void *hunoz
)
1967 { return (PyInt_FromLong(mp_bits(MP_X(me
)) - 1)); }
1969 static PyGetSetDef gf_pygetset
[] = {
1970 #define GETSETNAME(op, name) gf##op##_##name
1971 GET (degree
, "X.degree -> polynomial degree of X")
1973 #define GETSETNAME(op, name) mp##op##_##name
1974 GET (nbits
, "X.nbits -> bit length of X")
1975 GET (noctets
, "X.noctets -> octet length of X")
1980 static PyMethodDef gf_pymethods
[] = {
1981 #define METHNAME(func) gfmeth_##func
1982 METH (setbit
, "X.setbit(N) -> X with bit N set")
1983 METH (clearbit
, "X.clearbit(N) -> X with bit N clear")
1984 METH (testbit
, "X.testbit(N) -> true/false if bit N set/clear in X")
1985 METH (sqr
, "X.sqr() -> X^2")
1986 METH (gcd
, "X.gcd(Y) -> gcd(X, Y)")
1988 "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)")
1989 METH (modinv
, "X.modinv(Y) -> multiplicative inverse of Y mod X")
1990 METH (irreduciblep
, "X.irreduciblep() -> true/false")
1992 #define METHNAME(func) mpmeth_##func
1993 KWMETH(tostring
, "X.tostring(radix = 10) -> STR")
1994 KWMETH(storel
, "X.storel(len = -1) -> little-endian bytes")
1995 KWMETH(storeb
, "X.storeb(len = -1) -> big-endian bytes")
1997 "X.storel2c(len = -1) -> little-endian bytes, two's complement")
1999 "X.storeb2c(len = -1) -> big-endian bytes, two's complement")
2000 METH (tobuf
, "X.tobuf() -> buffer format")
2005 static PyNumberMethods gf_pynumber
= {
2006 gf_pyadd
, /* @nb_add@ */
2007 gf_pysub
, /* @nb_subtract@ */
2008 gf_pymul
, /* @nb_multiply@ */
2009 0, /* @nb_divide@ */
2010 gf_pymod
, /* @nb_remainder@ */
2011 gf_pydivmod
, /* @nb_divmod@ */
2012 gf_pyexp
, /* @nb_power@ */
2013 mp_pyid
, /* @nb_negative@ */
2014 mp_pyid
, /* @nb_positive@ */
2015 mp_pyid
, /* @nb_absolute@ */
2016 mp_pynonzerop
, /* @nb_nonzero@ */
2017 0 /* doesn't make any sense */, /* @nb_invert@ */
2018 gf_pylsl
, /* @nb_lshift@ */
2019 gf_pylsr
, /* @nb_rshift@ */
2020 gf_pyand
, /* @nb_and@ */
2021 gf_pyxor
, /* @nb_xor@ */
2022 gf_pyor
, /* @nb_or@ */
2023 gf_pycoerce
, /* @nb_coerce@ */
2024 mp_pyint
, /* @nb_int@ */
2025 mp_pylong
, /* @nb_long@ */
2026 0 /* doesn't make any sense */, /* @nb_float@ */
2027 mp_pyoct
, /* @nb_oct@ */
2028 mp_pyhex
, /* @nb_hex@ */
2030 0, /* @nb_inplace_add@ */
2031 0, /* @nb_inplace_subtract@ */
2032 0, /* @nb_inplace_multiply@ */
2033 0, /* @nb_inplace_divide@ */
2034 0, /* @nb_inplace_remainder@ */
2035 0, /* @nb_inplace_power@ */
2036 0, /* @nb_inplace_lshift@ */
2037 0, /* @nb_inplace_rshift@ */
2038 0, /* @nb_inplace_and@ */
2039 0, /* @nb_inplace_xor@ */
2040 0, /* @nb_inplace_or@ */
2042 gf_pydiv
, /* @nb_floor_divide@ */
2043 0, /* @nb_true_divide@ */
2044 0, /* @nb_inplace_floor_divide@ */
2045 0, /* @nb_inplace_true_divide@ */
2048 static PyTypeObject gf_pytype_skel
= {
2049 PyObject_HEAD_INIT(0) 0, /* Header */
2050 "GF", /* @tp_name@ */
2051 sizeof(mp_pyobj
), /* @tp_basicsize@ */
2052 0, /* @tp_itemsize@ */
2054 mp_pydealloc
, /* @tp_dealloc@ */
2056 0, /* @tp_getattr@ */
2057 0, /* @tp_setattr@ */
2058 0, /* @tp_compare@ */
2059 gf_pyrepr
, /* @tp_repr@ */
2060 &gf_pynumber
, /* @tp_as_number@ */
2061 0, /* @tp_as_sequence@ */
2062 0, /* @tp_as_mapping@ */
2063 gf_pyhash
, /* @tp_hash@ */
2065 mp_pyhex
, /* @tp_str@ */
2066 0, /* @tp_getattro@ */
2067 0, /* @tp_setattro@ */
2068 0, /* @tp_as_buffer@ */
2069 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2070 Py_TPFLAGS_CHECKTYPES
|
2071 Py_TPFLAGS_BASETYPE
,
2074 "Binary polynomials. Support almost all the standard arithmetic\n\
2077 Constructor gf(X, radix = R) attempts to convert X to a `gf'. If\n\
2078 X is a string, it's read in radix-R form, or we look for a prefix\n\
2079 if R = 0. Other acceptable things are ints and longs.\n\
2081 The name is hopelessly wrong from a technical point of view, but\n\
2082 but it's much easier to type than `p2' or `c2' or whatever.\n\
2086 * Use `//' for division. GFs don't have `/' division.",
2088 0, /* @tp_traverse@ */
2090 gf_pyrichcompare
, /* @tp_richcompare@ */
2091 0, /* @tp_weaklistoffset@ */
2093 0, /* @tp_iternext@ */
2094 gf_pymethods
, /* @tp_methods@ */
2095 0, /* @tp_members@ */
2096 gf_pygetset
, /* @tp_getset@ */
2099 0, /* @tp_descr_get@ */
2100 0, /* @tp_descr_set@ */
2101 0, /* @tp_dictoffset@ */
2103 PyType_GenericAlloc
, /* @tp_alloc@ */
2104 gf_pynew
, /* @tp_new@ */
2109 static PyObject
*meth__GF_fromstring(PyObject
*me
,
2110 PyObject
*arg
, PyObject
*kw
)
2117 mptext_stringctx sc
;
2118 char *kwlist
[] = { "class", "x", "radix", 0 };
2120 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "Os#|i:fromstring",
2121 kwlist
, &me
, &p
, &len
, &r
))
2123 if (!good_radix_p(r
, 1)) VALERR("bad radix");
2124 sc
.buf
= p
; sc
.lim
= p
+ len
;
2125 if ((zz
= mp_read(MP_NEW
, r
, &mptext_stringops
, &sc
)) == 0 ||
2127 if (zz
) MP_DROP(zz
);
2128 VALERR("bad binary polynomial");
2130 z
= Py_BuildValue("(Ns#)", gf_pywrap(zz
), sc
.buf
, (int)(sc
.lim
- sc
.buf
));
2135 /*----- Sparse poly reduction ---------------------------------------------*/
2137 typedef struct gfreduce_pyobj
{
2142 #define GFREDUCE_PY(o) (&((gfreduce_pyobj *)(o))->mr)
2144 static PyObject
*grmeth_exp(PyObject
*me
, PyObject
*arg
)
2147 mp
*yy
= 0, *zz
= 0;
2149 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convgf
, &yy
, convgf
, &zz
))
2152 if ((yy
= gf_modinv_checked(yy
, yy
, GFREDUCE_PY(me
)->p
)) == 0) goto end
;
2153 zz
= mp_neg(zz
, zz
);
2155 rc
= gf_pywrap(gfreduce_exp(GFREDUCE_PY(me
), MP_NEW
, yy
, zz
));
2157 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
2161 static PyObject
*grmeth_trace(PyObject
*me
, PyObject
*arg
)
2166 if (!PyArg_ParseTuple(arg
, "O&:trace", convgf
, &xx
)) goto end
;
2167 rc
= PyInt_FromLong(gfreduce_trace(GFREDUCE_PY(me
), xx
));
2169 if (xx
) MP_DROP(xx
);
2173 static PyObject
*grmeth_halftrace(PyObject
*me
, PyObject
*arg
)
2178 if (!PyArg_ParseTuple(arg
, "O&:halftrace", convgf
, &xx
)) goto end
;
2179 rc
= gf_pywrap(gfreduce_halftrace(GFREDUCE_PY(me
), MP_NEW
, xx
));
2181 if (xx
) MP_DROP(xx
);
2185 static PyObject
*grmeth_sqrt(PyObject
*me
, PyObject
*arg
)
2190 if (!PyArg_ParseTuple(arg
, "O&:sqrt", convgf
, &xx
)) goto end
;
2191 if ((yy
= gfreduce_sqrt(GFREDUCE_PY(me
), MP_NEW
, xx
)) == 0)
2192 VALERR("no modular square root");
2195 if (xx
) MP_DROP(xx
);
2199 static PyObject
*grmeth_quadsolve(PyObject
*me
, PyObject
*arg
)
2204 if (!PyArg_ParseTuple(arg
, "O&:quadsolve", convgf
, &xx
)) goto end
;
2205 if ((yy
= gfreduce_quadsolve(GFREDUCE_PY(me
), MP_NEW
, xx
)) == 0)
2206 VALERR("no solution found");
2209 if (xx
) MP_DROP(xx
);
2213 static PyObject
*grmeth_reduce(PyObject
*me
, PyObject
*arg
)
2218 if (!PyArg_ParseTuple(arg
, "O&:reduce", convgf
, &yy
)) goto end
;
2219 z
= gf_pywrap(gfreduce_do(GFREDUCE_PY(me
), MP_NEW
, yy
));
2224 static void gfreduce_pydealloc(PyObject
*me
)
2226 gfreduce_destroy(GFREDUCE_PY(me
));
2230 static PyObject
*gfreduce_pynew(PyTypeObject
*ty
,
2231 PyObject
*arg
, PyObject
*kw
)
2233 gfreduce_pyobj
*mr
= 0;
2235 char *kwlist
[] = { "m", 0 };
2238 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convgf
, &xx
))
2240 if (MP_ZEROP(xx
)) ZDIVERR("modulus is zero!");
2241 gfreduce_create(&r
, xx
);
2242 mr
= (gfreduce_pyobj
*)ty
->tp_alloc(ty
, 0);
2245 if (xx
) MP_DROP(xx
);
2246 return ((PyObject
*)mr
);
2249 static PyObject
*grget_m(PyObject
*me
, void *hunoz
)
2250 { return (gf_pywrap(MP_COPY(GFREDUCE_PY(me
)->p
))); }
2252 static PyGetSetDef gfreduce_pygetset
[] = {
2253 #define GETSETNAME(op, name) gr##op##_##name
2254 GET (m
, "R.m -> reduction polynomial")
2259 static PyMethodDef gfreduce_pymethods
[] = {
2260 #define METHNAME(name) grmeth_##name
2261 METH (reduce
, "R.reduce(X) -> X mod B.m")
2262 METH (trace
, "R.trace(X) -> Tr(X) = x + x^2 + ... + x^{2^{m - 1}}")
2263 METH (halftrace
, "R.halftrace(X) -> x + x^{2^2} + ... + x^{2^{m - 1}}")
2264 METH (sqrt
, "R.sqrt(X) -> Y where Y^2 = X mod R")
2265 METH (quadsolve
, "R.quadsolve(X) -> Y where Y^2 + Y = X mod R")
2266 METH (exp
, "R.exp(X, N) -> X^N mod B.m")
2271 static PyTypeObject
*gfreduce_pytype
, gfreduce_pytype_skel
= {
2272 PyObject_HEAD_INIT(0) 0, /* Header */
2273 "GFReduce", /* @tp_name@ */
2274 sizeof(gfreduce_pyobj
), /* @tp_basicsize@ */
2275 0, /* @tp_itemsize@ */
2277 gfreduce_pydealloc
, /* @tp_dealloc@ */
2279 0, /* @tp_getattr@ */
2280 0, /* @tp_setattr@ */
2281 0, /* @tp_compare@ */
2283 0, /* @tp_as_number@ */
2284 0, /* @tp_as_sequence@ */
2285 0, /* @tp_as_mapping@ */
2289 0, /* @tp_getattro@ */
2290 0, /* @tp_setattro@ */
2291 0, /* @tp_as_buffer@ */
2292 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2293 Py_TPFLAGS_BASETYPE
,
2296 "A reduction context for reduction modulo sparse irreducible polynomials.",
2298 0, /* @tp_traverse@ */
2300 0, /* @tp_richcompare@ */
2301 0, /* @tp_weaklistoffset@ */
2303 0, /* @tp_iternext@ */
2304 gfreduce_pymethods
, /* @tp_methods@ */
2305 0, /* @tp_members@ */
2306 gfreduce_pygetset
, /* @tp_getset@ */
2309 0, /* @tp_descr_get@ */
2310 0, /* @tp_descr_set@ */
2311 0, /* @tp_dictoffset@ */
2313 PyType_GenericAlloc
, /* @tp_alloc@ */
2314 gfreduce_pynew
, /* @tp_new@ */
2319 /*----- Normal/poly transformation ----------------------------------------*/
2321 typedef struct gfn_pyobj
{
2327 static PyTypeObject
*gfn_pytype
, gfn_pytype_skel
;
2329 #define GFN_P(o) (((gfn_pyobj *)(o))->p)
2330 #define GFN_PTON(o) (&((gfn_pyobj *)(o))->pton)
2331 #define GFN_NTOP(o) (&((gfn_pyobj *)(o))->ntop)
2333 static PyObject
*gfn_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2335 mp
*p
= 0, *beta
= 0;
2337 char *kwlist
[] = { "p", "beta", 0 };
2339 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:new", kwlist
,
2340 convgf
, &p
, convgf
, &beta
))
2342 gg
= PyObject_New(gfn_pyobj
, ty
);
2343 if (gfn_create(p
, beta
, &gg
->ntop
, &gg
->pton
)) {
2346 VALERR("can't invert transformation matrix");
2352 return ((PyObject
*)gg
);
2355 static PyObject
*gfnget_p(PyObject
*me
, void *hunoz
)
2356 { return (gf_pywrap(MP_COPY(GFN_P(me
)))); }
2358 static PyObject
*gfnget_beta(PyObject
*me
, void *hunoz
)
2360 gfn
*n
= GFN_NTOP(me
);
2361 mp
*x
= n
->r
[n
->n
- 1];
2362 return (gf_pywrap(MP_COPY(x
)));
2365 #define XFORMOP(name, NAME) \
2366 static PyObject *gfnmeth_##name(PyObject *me, PyObject *arg) \
2371 if (!PyArg_ParseTuple(arg, "O&:" #name, convgf, &xx)) goto end; \
2372 z = gfn_transform(GFN_##NAME(me), MP_NEW, xx); \
2375 if (!z) return (0); \
2376 return (mp_pywrap(z)); \
2382 static void gfn_pydealloc(PyObject
*me
)
2384 gfn_destroy(GFN_PTON(me
));
2385 gfn_destroy(GFN_NTOP(me
));
2389 static PyGetSetDef gfn_pygetset
[] = {
2390 #define GETSETNAME(op, name) gfn##op##_##name
2391 GET (p
, "X.p -> polynomial basis, as polynomial")
2392 GET (beta
, "X.beta -> normal basis element, in poly form")
2397 static PyMethodDef gfn_pymethods
[] = {
2398 #define METHNAME(name) gfnmeth_##name
2399 METH (pton
, "X.pton(A) -> normal-basis representation of A")
2400 METH (ntop
, "X.ntop(A) -> polynomial-basis representation of A")
2405 static PyTypeObject gfn_pytype_skel
= {
2406 PyObject_HEAD_INIT(0) 0, /* Header */
2407 "GFN", /* @tp_name@ */
2408 sizeof(gfn_pyobj
), /* @tp_basicsize@ */
2409 0, /* @tp_itemsize@ */
2411 gfn_pydealloc
, /* @tp_dealloc@ */
2413 0, /* @tp_getattr@ */
2414 0, /* @tp_setattr@ */
2415 0, /* @tp_compare@ */
2417 0, /* @tp_as_number@ */
2418 0, /* @tp_as_sequence@ */
2419 0, /* @tp_as_mapping@ */
2423 0, /* @tp_getattro@ */
2424 0, /* @tp_setattro@ */
2425 0, /* @tp_as_buffer@ */
2426 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2427 Py_TPFLAGS_BASETYPE
,
2430 "An object for transforming elements of binary fields between polynomial\n\
2431 and normal basis representations.",
2433 0, /* @tp_traverse@ */
2435 0, /* @tp_richcompare@ */
2436 0, /* @tp_weaklistoffset@ */
2438 0, /* @tp_iternext@ */
2439 gfn_pymethods
, /* @tp_methods@ */
2440 0, /* @tp_members@ */
2441 gfn_pygetset
, /* @tp_getset@ */
2444 0, /* @tp_descr_get@ */
2445 0, /* @tp_descr_set@ */
2446 0, /* @tp_dictoffset@ */
2448 PyType_GenericAlloc
, /* @tp_alloc@ */
2449 gfn_pynew
, /* @tp_new@ */
2454 /*----- Glue --------------------------------------------------------------*/
2456 static PyMethodDef methods
[] = {
2457 #define METHNAME(func) meth_##func
2458 KWMETH(_MP_fromstring
, "\
2459 fromstring(STR, radix = 0) -> (X, REST)\n\
2461 Parse STR as a large integer, according to radix. If radix is zero,\n\
2462 read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\
2463 or `R_' for other radix R.")
2464 KWMETH(_GF_fromstring
, "\
2465 fromstring(STR, radix = 0) -> (X, REST)\n\
2467 Parse STR as a binary polynomial, according to radix. If radix is zero,\n\
2468 read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\
2469 or `R_' for other radix R.")
2470 METH (_MP_factorial
, "\
2471 factorial(I) -> I!: compute factorial")
2472 METH (_MP_fibonacci
, "\
2473 fibonacci(I) -> F(I): compute Fibonacci number")
2475 loadl(STR) -> X: read little-endian bytes")
2477 loadb(STR) -> X: read big-endian bytes")
2478 METH (_MP_loadl2c
, "\
2479 loadl2c(STR) -> X: read little-endian bytes, two's complement")
2480 METH (_MP_loadb2c
, "\
2481 loadb2c(STR) -> X: read big-endian bytes, two's complement")
2482 METH (_MP_frombuf
, "\
2483 frombuf(STR) -> (X, REST): read buffer format")
2485 loadl(STR) -> X: read little-endian bytes")
2487 loadb(STR) -> X: read big-endian bytes")
2488 METH (_GF_frombuf
, "\
2489 frombuf(STR) -> (X, REST): read buffer format")
2494 void mp_pyinit(void)
2498 INITTYPE(mpmul
, root
);
2499 INITTYPE(mpmont
, root
);
2500 INITTYPE(mpbarrett
, root
);
2501 INITTYPE(mpreduce
, root
);
2502 INITTYPE(mpcrt
, root
);
2503 INITTYPE(gfreduce
, root
);
2504 INITTYPE(gfn
, root
);
2505 addmethods(methods
);
2508 void mp_pyinsert(PyObject
*mod
)
2510 INSERT("MP", mp_pytype
);
2511 INSERT("MPMul", mpmul_pytype
);
2512 INSERT("MPMont", mpmont_pytype
);
2513 INSERT("MPBarrett", mpbarrett_pytype
);
2514 INSERT("MPReduce", mpreduce_pytype
);
2515 INSERT("MPCRT", mpcrt_pytype
);
2516 INSERT("GF", gf_pytype
);
2517 INSERT("GFReduce", gfreduce_pytype
);
2518 INSERT("GFN", gfn_pytype
);
2521 /*----- That's all, folks -------------------------------------------------*/