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
)
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 VALERR("mp out of range for int");
183 /*----- Python interface --------------------------------------------------*/
185 static void mp_pydealloc(PyObject
*o
)
191 static PyObject
*mp_pyrepr(PyObject
*o
)
192 { return mp_topystring(MP_X(o
), 10, "MP(", 0, "L)"); }
194 static PyObject
*mp_pystr(PyObject
*o
)
195 { return mp_topystring(MP_X(o
), 10, 0, 0, 0); }
197 mp
*tomp(PyObject
*o
)
204 else if (MP_PYCHECK(o
) || GF_PYCHECK(o
))
205 return (MP_COPY(MP_X(o
)));
206 else if (FE_PYCHECK(o
))
207 return (F_OUT(FE_F(o
), MP_NEW
, FE_X(o
)));
208 else if (PFILT_PYCHECK(o
))
209 return (MP_COPY(PFILT_F(o
)->m
));
210 else if (ECPT_PYCHECK(o
)) {
216 } else if (GE_PYCHECK(o
)) {
217 if ((x
= G_TOINT(GE_G(o
), MP_NEW
, GE_X(o
))) == 0)
220 } else if (PyInt_Check(o
))
221 return (mp_fromlong(MP_NEW
, PyInt_AS_LONG(o
)));
222 else if ((l
= PyNumber_Long(o
)) != 0) {
223 x
= mp_frompylong(l
);
232 mp
*getmp(PyObject
*o
)
236 if ((x
= tomp(o
)) == 0) {
237 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to mp",
238 o
->ob_type
->tp_name
);
243 int convmp(PyObject
*o
, void *p
)
246 if ((x
= getmp(o
)) == 0) return (0);
251 mp
*getgf(PyObject
*o
)
255 if ((x
= tomp(o
)) == 0) {
256 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to gf",
257 o
->ob_type
->tp_name
);
262 int convgf(PyObject
*o
, void *p
)
265 if ((x
= getgf(o
)) == 0) return (0);
270 static mp
*implicitmp(PyObject
*o
)
281 static mp
*implicitgf(PyObject
*o
)
292 static int mpbinop(PyObject
*x
, PyObject
*y
, mp
**xx
, mp
**yy
)
294 if ((*xx
= implicitmp(x
)) == 0)
296 if ((*yy
= implicitmp(y
)) == 0) {
303 static int gfbinop(PyObject
*x
, PyObject
*y
, mp
**xx
, mp
**yy
)
305 if ((*xx
= implicitgf(x
)) == 0)
307 if ((*yy
= implicitgf(y
)) == 0) {
314 #define gf_and mp_and
316 #define gf_xor mp_xor
317 #define BINOP(pre, name) \
318 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
320 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
321 zz = pre##_##name(MP_NEW, xx, yy); \
322 MP_DROP(xx); MP_DROP(yy); \
323 return (pre##_pywrap(zz)); \
339 static mp
*mp_abs(mp
*d
, mp
*x
)
342 return (mp_neg(d
, x
));
348 #define UNOP(pre, name) \
349 static PyObject *pre##_py##name(PyObject *x) \
350 { return mp_pywrap(pre##_##name(MP_NEW, MP_X(x))); }
356 static PyObject
*mp_pyid(PyObject
*x
) { RETURN_OBJ(x
); }
358 #define gf_lsr mp_lsr
359 #define SHIFTOP(pre, name, rname) \
360 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
364 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
365 if (mp_tolong_checked(yy, &n)) goto end; \
367 z = pre##_pywrap(mp_##rname(MP_NEW, xx, -n)); \
369 z = pre##_pywrap(mp_##name(MP_NEW, xx, n)); \
371 MP_DROP(xx); MP_DROP(yy); \
374 SHIFTOP(mp
, lsl2c
, lsr2c
)
375 SHIFTOP(mp
, lsr2c
, lsl2c
)
376 SHIFTOP(gf
, lsl
, lsr
)
377 SHIFTOP(gf
, lsr
, lsl
)
380 #define DIVOP(pre, name, qq, rr, gather) \
381 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
384 INIT_##qq(q) INIT_##rr(r) \
385 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
387 ZDIVERR("division by zero"); \
388 pre##_div(ARG_##qq(q), ARG_##rr(r), xx, yy); \
391 MP_DROP(xx); MP_DROP(yy); \
394 #define INIT_YES(p) mp *p = MP_NEW;
396 #define ARG_YES(p) &p
398 DIVOP(mp
, divmod
, YES
, YES
,
399 Py_BuildValue("(NN)", mp_pywrap(q
), mp_pywrap(r
)))
400 DIVOP(mp
, div
, YES
, NO
, mp_pywrap(q
))
401 DIVOP(mp
, mod
, NO
, YES
, mp_pywrap(r
))
402 DIVOP(gf
, divmod
, YES
, YES
,
403 Py_BuildValue("(NN)", gf_pywrap(q
), gf_pywrap(r
)))
404 DIVOP(gf
, div
, YES
, NO
, gf_pywrap(q
))
405 DIVOP(gf
, mod
, NO
, YES
, gf_pywrap(r
))
412 static mp
*mp_modinv_checked(mp
*d
, mp
*x
, mp
*p
)
415 mp_gcd(&g
, 0, &d
, p
, x
);
416 if (!MP_EQ(g
, MP_ONE
)) {
417 MP_DROP(g
); MP_DROP(d
);
418 PyErr_SetString(PyExc_ZeroDivisionError
, "no modular inverse");
421 MP_DROP(g
); return (d
);
424 static PyObject
*mp_pyexp(PyObject
*x
, PyObject
*y
, PyObject
*z
)
426 mp
*xx
= 0, *yy
= 0, *zz
= 0;
430 if ((xx
= implicitmp(x
)) == 0 || (yy
= implicitmp(y
)) == 0 ||
431 (z
&& z
!= Py_None
&& (zz
= tomp(z
)) == 0)) {
432 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
435 if (!z
|| z
== Py_None
) {
436 if (MP_NEGP(yy
)) VALERR("negative exponent");
437 r
= mp_exp(MP_NEW
, xx
, yy
);
439 if (!MP_POSP(zz
)) VALERR("modulus must be positive");
441 if ((xx
= mp_modinv_checked(xx
, xx
, zz
)) == 0) goto end
;
446 mpmont_create(&mm
, zz
);
447 r
= mpmont_exp(&mm
, MP_NEW
, xx
, yy
);
451 mpbarrett_create(&mb
, zz
);
452 r
= mpbarrett_exp(&mb
, MP_NEW
, xx
, yy
);
453 mpbarrett_destroy(&mb
);
458 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
462 static mp
*gf_modinv_checked(mp
*d
, mp
*x
, mp
*p
)
465 gf_gcd(&g
, 0, &d
, p
, x
);
466 if (!MP_EQ(g
, MP_ONE
)) {
467 MP_DROP(g
); MP_DROP(d
);
468 PyErr_SetString(PyExc_ZeroDivisionError
, "no modular inverse");
471 MP_DROP(g
); return (d
);
474 #define BASEOP(name, radix, pre) \
475 static PyObject *mp_py##name(PyObject *x) \
476 { return mp_topystring(MP_X(x), radix, 0, pre, 0); }
478 BASEOP(hex
, 16, "0x");
481 static int mp_pynonzerop(PyObject
*x
) { return !MP_ZEROP(MP_X(x
)); }
483 static PyObject
*mp_pyint(PyObject
*x
)
486 if (mp_tolong_checked(MP_X(x
), &l
)) return (0);
487 return (PyInt_FromLong(l
));
489 static PyObject
*mp_pylong(PyObject
*x
)
490 { return (mp_topylong(MP_X(x
))); }
491 static PyObject
*mp_pyfloat(PyObject
*x
)
493 PyObject
*l
= mp_topylong(MP_X(x
));
494 double f
= PyLong_AsDouble(l
);
496 return (PyFloat_FromDouble(f
));
499 #define COERCE(pre, PRE) \
500 static int pre##_pycoerce(PyObject **x, PyObject **y) \
504 if (PRE##_PYCHECK(*y)) { \
505 Py_INCREF(*x); Py_INCREF(*y); \
508 if ((z = tomp(*y)) != 0) { \
510 *y = pre##_pywrap(z); \
519 static int mp_pycompare(PyObject
*x
, PyObject
*y
)
520 { return mp_cmp(MP_X(x
), MP_X(y
)); }
522 static PyObject
*mp_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
528 char *kwlist
[] = { "x", "radix", 0 };
530 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|i:new", kwlist
, &x
, &radix
))
532 if (MP_PYCHECK(x
)) RETURN_OBJ(x
);
533 if (!good_radix_p(radix
, 1)) VALERR("bad radix");
534 if ((z
= mp_frompyobject(x
, radix
)) == 0) {
535 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to mp",
536 x
->ob_type
->tp_name
);
539 zz
= (mp_pyobj
*)ty
->tp_alloc(ty
, 0);
542 return ((PyObject
*)zz
);
545 static long mp_pyhash(PyObject
*me
)
548 PyObject
*l
= mp_topylong(MP_X(me
)); h
= PyObject_Hash(l
);
549 Py_DECREF(l
); return (h
);
552 static PyObject
*mpmeth_jacobi(PyObject
*me
, PyObject
*arg
)
557 if (!PyArg_ParseTuple(arg
, "O&:jacobi", convmp
, &y
)) goto end
;
558 z
= PyInt_FromLong(mp_jacobi(y
, MP_X(me
)));
564 #define BITOP(pre, name, c) \
565 static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \
568 if (!PyArg_ParseTuple(arg, "O&:" #name, convulong, &i)) return (0); \
569 return (pre##_pywrap(mp_##name##c(MP_NEW, MP_X(me), i))); \
571 BITOP(mp
, setbit
, 2c
);
572 BITOP(mp
, clearbit
, 2c
);
574 BITOP(gf
, clearbit
, );
577 static PyObject
*mpmeth_testbit(PyObject
*me
, PyObject
*arg
)
580 if (!PyArg_ParseTuple(arg
, "O&:testbit", convulong
, &i
)) return (0);
581 return (getbool(mp_testbit2c(MP_X(me
), i
)));
584 static PyObject
*gfmeth_testbit(PyObject
*me
, PyObject
*arg
)
587 if (!PyArg_ParseTuple(arg
, "O&:testbit", convulong
, &i
)) return (0);
588 return (getbool(mp_testbit(MP_X(me
), i
)));
591 static PyObject
*mpmeth_odd(PyObject
*me
, PyObject
*arg
)
596 if (!PyArg_ParseTuple(arg
, ":odd")) return (0);
597 t
= mp_odd(MP_NEW
, MP_X(me
), &s
);
598 return (Py_BuildValue("(lN)", (long)s
, mp_pywrap(t
)));
601 static PyObject
*mpmeth_sqr(PyObject
*me
, PyObject
*arg
)
603 if (!PyArg_ParseTuple(arg
, ":sqr")) return (0);
604 return (mp_pywrap(mp_sqr(MP_NEW
, MP_X(me
))));
607 static PyObject
*mpmeth_sqrt(PyObject
*me
, PyObject
*arg
)
609 if (!PyArg_ParseTuple(arg
, ":sqrt")) return (0);
610 if (MP_NEGP(MP_X(me
))) VALERR("negative root");
611 return (mp_pywrap(mp_sqrt(MP_NEW
, MP_X(me
))));
616 static PyObject
*mpmeth_gcd(PyObject
*me
, PyObject
*arg
)
618 mp
*y
= 0, *zz
= MP_NEW
;
621 if (!PyArg_ParseTuple(arg
, "O&:gcd", convmp
, &y
)) goto end
;
622 mp_gcd(&zz
, 0, 0, MP_X(me
), y
);
629 static PyObject
*mpmeth_gcdx(PyObject
*me
, PyObject
*arg
)
632 mp
*yy
= 0, *zz
= MP_NEW
, *uu
= MP_NEW
, *vv
= MP_NEW
;
634 if (!PyArg_ParseTuple(arg
, "O&:gcdx", convmp
, &yy
)) goto end
;
635 mp_gcd(&zz
, &uu
, &vv
, MP_X(me
), yy
);
636 z
= Py_BuildValue("(NNN)",
637 mp_pywrap(zz
), mp_pywrap(uu
), mp_pywrap(vv
));
643 static PyObject
*mpmeth_modinv(PyObject
*me
, PyObject
*arg
)
646 mp
*yy
= 0, *zz
= MP_NEW
;
648 if (!PyArg_ParseTuple(arg
, "O&:modinv", convmp
, &yy
) ||
649 (zz
= mp_modinv_checked(MP_NEW
, yy
, MP_X(me
))) == 0)
657 static PyObject
*mpmeth_tostring(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
660 char *kwlist
[] = { "radix", 0 };
661 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|i:tostring", kwlist
, &radix
))
663 if (!good_radix_p(radix
, 0)) VALERR("bad radix");
664 return (mp_topystring(MP_X(me
), radix
, 0, 0, 0));
669 static PyObject
*mpmeth_modsqrt(PyObject
*me
, PyObject
*arg
)
672 mp
*yy
= 0, *zz
= MP_NEW
;
674 if (!PyArg_ParseTuple(arg
, "O&:modsqrt", convmp
, &yy
)) goto end
;
675 if ((zz
= mp_modsqrt(MP_NEW
, yy
, MP_X(me
))) == 0)
676 VALERR("no modular square root");
683 #define STOREOP(name, c) \
684 static PyObject *mpmeth_##name(PyObject *me, \
685 PyObject *arg, PyObject *kw) \
688 char *kwlist[] = { "len", 0 }; \
691 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|l:" #name, \
695 len = mp_octets##c(MP_X(me)); \
698 rc = bytestring_pywrap(0, len); \
699 mp_##name(MP_X(me), PyString_AS_STRING(rc), len); \
705 STOREOP(storel2c
, 2c
)
706 STOREOP(storeb2c
, 2c
)
709 #define BUFOP(ty, pyty) \
710 static PyObject *meth__##pyty##_frombuf(PyObject *me, PyObject *arg) \
718 if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end; \
719 buf_init(&b, p, sz); \
720 if ((x = buf_getmp(&b)) == 0) VALERR("malformed data"); \
721 rc = Py_BuildValue("(NN)", ty##_pywrap(x), \
722 bytestring_pywrapbuf(&b)); \
730 static PyObject
*mpmeth_tobuf(PyObject
*me
, PyObject
*arg
)
737 if (!PyArg_ParseTuple(arg
, ":tobuf")) return (0);
739 n
= mp_octets(x
) + 3;
740 rc
= bytestring_pywrap(0, n
);
741 buf_init(&b
, PyString_AS_STRING(rc
), n
);
744 _PyString_Resize(&rc
, BLEN(&b
));
748 static PyObject
*mpmeth_primep(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
750 grand
*r
= &rand_global
;
751 char *kwlist
[] = { "rng", 0 };
754 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&", kwlist
, convgrand
, &r
))
756 rc
= getbool(pgen_primep(MP_X(me
), r
));
761 static PyObject
*mpget_nbits(PyObject
*me
, void *hunoz
)
762 { return (PyInt_FromLong(mp_bits(MP_X(me
)))); }
764 static PyObject
*mpget_noctets(PyObject
*me
, void *hunoz
)
765 { return (PyInt_FromLong(mp_octets(MP_X(me
)))); }
767 static PyObject
*mpget_noctets2c(PyObject
*me
, void *hunoz
)
768 { return (PyInt_FromLong(mp_octets2c(MP_X(me
)))); }
770 static PyGetSetDef mp_pygetset
[] = {
771 #define GETSETNAME(op, func) mp##op##_##func
772 GET (nbits
, "X.nbits -> bit length of X")
773 GET (noctets
, "X.noctets -> octet length of X")
774 GET (noctets2c
, "X.noctets2c -> two's complement octet length of X")
779 static PyMethodDef mp_pymethods
[] = {
780 #define METHNAME(func) mpmeth_##func
781 METH (jacobi
, "X.jacobi(Y) -> Jacobi symbol (Y/X) (NB inversion!)")
782 METH (setbit
, "X.setbit(N) -> X with bit N set")
783 METH (clearbit
, "X.clearbit(N) -> X with bit N clear")
784 METH (testbit
, "X.testbit(N) -> true/false if bit N set/clear in X")
785 METH (odd
, "X.odd() -> S, T where X = 2^S T with T odd")
786 METH (sqr
, "X.sqr() -> X^2")
787 METH (sqrt
, "X.sqrt() -> largest integer <= sqrt(X)")
788 METH (gcd
, "X.gcd(Y) -> gcd(X, Y)")
790 "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)")
791 METH (modinv
, "X.modinv(Y) -> multiplicative inverse of Y mod X")
792 METH (modsqrt
, "X.modsqrt(Y) -> square root of Y mod X, if X prime")
793 KWMETH(primep
, "X.primep(rng = rand) -> true/false if X is prime")
794 KWMETH(tostring
, "X.tostring(radix = 10) -> STR")
795 KWMETH(storel
, "X.storel(len = -1) -> little-endian bytes")
796 KWMETH(storeb
, "X.storeb(len = -1) -> big-endian bytes")
798 "X.storel2c(len = -1) -> little-endian bytes, two's complement")
800 "X.storeb2c(len = -1) -> big-endian bytes, two's complement")
801 METH (tobuf
, "X.tobuf() -> buffer format")
806 static PyNumberMethods mp_pynumber
= {
807 mp_pyadd
, /* @nb_add@ */
808 mp_pysub
, /* @nb_subtract@ */
809 mp_pymul
, /* @nb_multiply@ */
811 mp_pymod
, /* @nb_remainder@ */
812 mp_pydivmod
, /* @nb_divmod@ */
813 mp_pyexp
, /* @nb_power@ */
814 mp_pyneg
, /* @nb_negative@ */
815 mp_pyid
, /* @nb_positive@ */
816 mp_pyabs
, /* @nb_absolute@ */
817 mp_pynonzerop
, /* @nb_nonzero@ */
818 mp_pynot2c
, /* @nb_invert@ */
819 mp_pylsl2c
, /* @nb_lshift@ */
820 mp_pylsr2c
, /* @nb_rshift@ */
821 mp_pyand2c
, /* @nb_and@ */
822 mp_pyxor2c
, /* @nb_xor@ */
823 mp_pyor2c
, /* @nb_or@ */
824 mp_pycoerce
, /* @nb_coerce@ */
825 mp_pyint
, /* @nb_int@ */
826 mp_pylong
, /* @nb_long@ */
827 mp_pyfloat
, /* @nb_float@ */
828 mp_pyoct
, /* @nb_oct@ */
829 mp_pyhex
, /* @nb_hex@ */
831 0, /* @nb_inplace_add@ */
832 0, /* @nb_inplace_subtract@ */
833 0, /* @nb_inplace_multiply@ */
834 0, /* @nb_inplace_divide@ */
835 0, /* @nb_inplace_remainder@ */
836 0, /* @nb_inplace_power@ */
837 0, /* @nb_inplace_lshift@ */
838 0, /* @nb_inplace_rshift@ */
839 0, /* @nb_inplace_and@ */
840 0, /* @nb_inplace_xor@ */
841 0, /* @nb_inplace_or@ */
843 mp_pydiv
, /* @nb_floor_divide@ */
844 0, /* @nb_true_divide@ */
845 0, /* @nb_inplace_floor_divide@ */
846 0, /* @nb_inplace_true_divide@ */
849 static PyTypeObject mp_pytype_skel
= {
850 PyObject_HEAD_INIT(0) 0, /* Header */
851 "catacomb.MP", /* @tp_name@ */
852 sizeof(mp_pyobj
), /* @tp_basicsize@ */
853 0, /* @tp_itemsize@ */
855 mp_pydealloc
, /* @tp_dealloc@ */
857 0, /* @tp_getattr@ */
858 0, /* @tp_setattr@ */
859 mp_pycompare
, /* @tp_compare@ */
860 mp_pyrepr
, /* @tp_repr@ */
861 &mp_pynumber
, /* @tp_as_number@ */
862 0, /* @tp_as_sequence@ */
863 0, /* @tp_as_mapping@ */
864 mp_pyhash
, /* @tp_hash@ */
866 mp_pystr
, /* @tp_str@ */
867 0, /* @tp_getattro@ */
868 0, /* @tp_setattro@ */
869 0, /* @tp_as_buffer@ */
870 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
871 Py_TPFLAGS_CHECKTYPES
|
875 "Multiprecision integers, similar to `long' but more efficient and\n\
876 versatile. Support all the standard arithmetic operations.\n\
878 Constructor mp(X, radix = R) attempts to convert X to an `mp'. If\n\
879 X is a string, it's read in radix-R form, or we look for a prefix\n\
880 if R = 0. Other acceptable things are ints and longs.\n\
884 * Use `//' for division. MPs don't have `/' division.",
886 0, /* @tp_traverse@ */
888 0, /* @tp_richcompare@ */
889 0, /* @tp_weaklistoffset@ */
891 0, /* @tp_iternext@ */
892 mp_pymethods
, /* @tp_methods@ */
893 0, /* @tp_members@ */
894 mp_pygetset
, /* @tp_getset@ */
897 0, /* @tp_descr_get@ */
898 0, /* @tp_descr_set@ */
899 0, /* @tp_dictoffset@ */
901 PyType_GenericAlloc
, /* @tp_alloc@ */
902 mp_pynew
, /* @tp_new@ */
907 static PyObject
*meth__MP_fromstring(PyObject
*me
,
908 PyObject
*arg
, PyObject
*kw
)
916 char *kwlist
[] = { "class", "x", "radix", 0 };
918 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "Os#|i:fromstring",
919 kwlist
, &me
, &p
, &len
, &r
))
921 if (!good_radix_p(r
, 1)) VALERR("bad radix");
922 sc
.buf
= p
; sc
.lim
= p
+ len
;
923 if ((zz
= mp_read(MP_NEW
, r
, &mptext_stringops
, &sc
)) == 0)
924 SYNERR("bad integer");
925 z
= Py_BuildValue("(Ns#)", mp_pywrap(zz
), sc
.buf
, (int)(sc
.lim
- sc
.buf
));
930 #define LOADOP(pre, py, name) \
931 static PyObject *meth__##py##_##name(PyObject *me, PyObject *arg) \
935 if (!PyArg_ParseTuple(arg, "Os#:" #name, &me, &p, &len)) return (0); \
936 return (pre##_pywrap(mp_##name(MP_NEW, p, len))); \
938 LOADOP(mp
, MP
, loadl
)
939 LOADOP(mp
, MP
, loadb
)
940 LOADOP(mp
, MP
, loadl2c
)
941 LOADOP(mp
, MP
, loadb2c
)
942 LOADOP(gf
, GF
, loadl
)
943 LOADOP(gf
, GF
, loadb
)
946 /*----- Products of small integers ----------------------------------------*/
948 typedef struct mpmul_pyobj
{
954 #define MPMUL_LIVEP(o) (((mpmul_pyobj *)(o))->livep)
955 #define MPMUL_PY(o) (&((mpmul_pyobj *)(o))->mm)
957 static void mpmul_pydealloc(PyObject
*me
)
960 mp_drop(mpmul_done(MPMUL_PY(me
)));
964 static PyObject
*mmmeth_factor(PyObject
*me
, PyObject
*arg
)
969 if (!MPMUL_LIVEP(me
)) VALERR("MPMul object invalid");
970 if (PyTuple_Size(arg
) != 1)
971 i
= PyObject_GetIter(arg
);
973 if ((q
= PyTuple_GetItem(arg
, 0)) == 0) goto end
;
974 if ((i
= PyObject_GetIter(q
)) == 0) {
975 PyErr_Clear(); /* that's ok */
976 i
= PyObject_GetIter(arg
);
980 while ((q
= PyIter_Next(i
)) != 0) {
981 x
= getmp(q
); Py_DECREF(q
); if (!x
) {
985 mpmul_add(MPMUL_PY(me
), x
);
994 static PyObject
*mmmeth_done(PyObject
*me
, PyObject
*arg
)
998 if (!PyArg_ParseTuple(arg
, ":done")) goto end
;
999 if (!MPMUL_LIVEP(me
)) VALERR("MPMul object invalid");
1000 x
= mpmul_done(MPMUL_PY(me
));
1001 MPMUL_LIVEP(me
) = 0;
1002 return (mp_pywrap(x
));
1007 static PyObject
*mpmul_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1011 if (kw
) TYERR("keyword arguments not allowed here");
1012 mm
= (mpmul_pyobj
*)ty
->tp_alloc(ty
, 0);
1013 mpmul_init(&mm
->mm
);
1015 if (mmmeth_factor((PyObject
*)mm
, arg
) == 0) {
1019 return ((PyObject
*)mm
);
1024 static PyObject
*mmget_livep(PyObject
*me
, void *hunoz
)
1025 { return (getbool(MPMUL_LIVEP(me
))); }
1027 static PyGetSetDef mpmul_pygetset
[] = {
1028 #define GETSETNAME(op, name) mm##op##_##name
1029 GET (livep
, "MM.livep -> flag: object still valid?")
1034 static PyMethodDef mpmul_pymethods
[] = {
1035 #define METHNAME(name) mmmeth_##name
1036 METH (factor
, "MM.factor(ITERABLE) or MM.factor(I, ...)")
1037 METH (done
, "MM.done() -> PRODUCT")
1042 static PyTypeObject
*mpmul_pytype
, mpmul_pytype_skel
= {
1043 PyObject_HEAD_INIT(0) 0, /* Header */
1044 "catacomb.MPMul", /* @tp_name@ */
1045 sizeof(mpmul_pyobj
), /* @tp_basicsize@ */
1046 0, /* @tp_itemsize@ */
1048 mpmul_pydealloc
, /* @tp_dealloc@ */
1050 0, /* @tp_getattr@ */
1051 0, /* @tp_setattr@ */
1052 0, /* @tp_compare@ */
1054 0, /* @tp_as_number@ */
1055 0, /* @tp_as_sequence@ */
1056 0, /* @tp_as_mapping@ */
1060 0, /* @tp_getattro@ */
1061 0, /* @tp_setattro@ */
1062 0, /* @tp_as_buffer@ */
1063 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1064 Py_TPFLAGS_BASETYPE
,
1067 "An object for multiplying many small integers.",
1069 0, /* @tp_traverse@ */
1071 0, /* @tp_richcompare@ */
1072 0, /* @tp_weaklistoffset@ */
1074 0, /* @tp_iternext@ */
1075 mpmul_pymethods
, /* @tp_methods@ */
1076 0, /* @tp_members@ */
1077 mpmul_pygetset
, /* @tp_getset@ */
1080 0, /* @tp_descr_get@ */
1081 0, /* @tp_descr_set@ */
1082 0, /* @tp_dictoffset@ */
1084 PyType_GenericAlloc
, /* @tp_alloc@ */
1085 mpmul_pynew
, /* @tp_new@ */
1090 /*----- Montgomery reduction ----------------------------------------------*/
1092 typedef struct mpmont_pyobj
{
1097 #define MPMONT_PY(o) (&((mpmont_pyobj *)(o))->mm)
1099 static PyObject
*mmmeth_int(PyObject
*me
, PyObject
*arg
)
1103 mpmont
*mm
= MPMONT_PY(me
);
1105 if (!PyArg_ParseTuple(arg
, "O&:in", convmp
, &yy
))
1107 mp_div(0, &yy
, yy
, mm
->m
);
1108 z
= mp_pywrap(mpmont_mul(mm
, MP_NEW
, yy
, mm
->r2
));
1110 if (yy
) MP_DROP(yy
);
1114 static PyObject
*mmmeth_mul(PyObject
*me
, PyObject
*arg
)
1117 mp
*yy
= 0, *zz
= 0;
1119 if (!PyArg_ParseTuple(arg
, "O&O&:mul", convmp
, &yy
, convmp
, &zz
))
1121 rc
= mp_pywrap(mpmont_mul(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1123 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1127 static PyObject
*mmmeth_exp(PyObject
*me
, PyObject
*arg
)
1130 mp
*yy
= 0, *zz
= 0;
1132 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1135 if ((yy
= mp_modinv_checked(yy
, yy
, MPMONT_PY(me
)->m
)) == 0) goto end
;
1136 zz
= mp_neg(zz
, zz
);
1138 rc
= mp_pywrap(mpmont_exp(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1140 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1144 static PyObject
*mmmeth_expr(PyObject
*me
, PyObject
*arg
)
1147 mp
*yy
= 0, *zz
= 0;
1149 if (!PyArg_ParseTuple(arg
, "O&O&:expr", convmp
, &yy
, convmp
, &zz
))
1152 yy
= mpmont_reduce(MPMONT_PY(me
), yy
, yy
);
1153 if ((yy
= mp_modinv_checked(yy
, yy
, MPMONT_PY(me
)->m
)) == 0) goto end
;
1154 yy
= mpmont_mul(MPMONT_PY(me
), yy
, yy
, MPMONT_PY(me
)->r2
);
1155 zz
= mp_neg(zz
, zz
);
1157 rc
= mp_pywrap(mpmont_expr(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1159 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1163 static PyObject
*mm_mexpr_id(PyObject
*me
)
1164 { return mp_pywrap(MP_COPY(MPMONT_PY(me
)->r
)); }
1166 static int mm_mexpr_fill(void *p
, PyObject
*me
, PyObject
*x
, PyObject
*y
)
1168 mp
*xx
= 0, *yy
= 0;
1169 mp_expfactor
*f
= p
;
1170 mpmont
*mm
= MPMONT_PY(me
);
1172 if ((xx
= getmp(x
)) == 0 || (yy
= getmp(y
)) == 0)
1175 xx
= mpmont_reduce(mm
, xx
, xx
);
1176 if ((xx
= mp_modinv_checked(xx
, xx
, yy
)) == 0)
1178 xx
= mpmont_mul(mm
, xx
, xx
, mm
->r2
);
1179 yy
= mp_neg(yy
, yy
);
1186 mp_drop(xx
); mp_drop(yy
);
1190 static PyObject
*mm_mexpr(PyObject
*me
, void *v
, int n
)
1191 { return mp_pywrap(mpmont_mexpr(MPMONT_PY(me
), MP_NEW
, v
, n
)); }
1193 static void mp_mexp_drop(void *p
)
1195 mp_expfactor
*f
= p
;
1200 static PyObject
*mmmeth_mexpr(PyObject
*me
, PyObject
*arg
)
1202 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1203 mm_mexpr_id
, mm_mexpr_fill
, mm_mexpr
, mp_mexp_drop
);
1206 static PyObject
*mp_mexp_id(PyObject
*me
)
1207 { return mp_pywrap(MP_ONE
); }
1209 static int mp_mexp_fill(void *p
, PyObject
*me
, PyObject
*x
, PyObject
*y
)
1211 mp
*xx
= 0, *yy
= 0;
1212 mp_expfactor
*f
= p
;
1214 if ((xx
= getmp(x
)) == 0 || (yy
= getmp(y
)) == 0)
1217 if ((xx
= mp_modinv_checked(xx
, xx
, yy
)) == 0)
1219 yy
= mp_neg(yy
, yy
);
1226 mp_drop(xx
); mp_drop(yy
);
1230 static PyObject
*mm_mexp(PyObject
*me
, void *v
, int n
)
1231 { return mp_pywrap(mpmont_mexp(MPMONT_PY(me
), MP_NEW
, v
, n
)); }
1233 static PyObject
*mmmeth_mexp(PyObject
*me
, PyObject
*arg
)
1235 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1236 mp_mexp_id
, mp_mexp_fill
, mm_mexp
, mp_mexp_drop
);
1239 #define mmmeth_ext mmmeth_reduce
1240 static PyObject
*mmmeth_reduce(PyObject
*me
, PyObject
*arg
)
1245 if (!PyArg_ParseTuple(arg
, "O&", convmp
, &yy
)) goto end
;
1246 z
= mp_pywrap(mpmont_reduce(MPMONT_PY(me
), MP_NEW
, yy
));
1251 static void mpmont_pydealloc(PyObject
*me
)
1253 mpmont_destroy(MPMONT_PY(me
));
1257 static PyObject
*mpmont_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1259 mpmont_pyobj
*mm
= 0;
1260 char *kwlist
[] = { "m", 0 };
1263 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convmp
, &xx
))
1265 if (!MP_POSP(xx
) || !MP_ODDP(xx
)) VALERR("m must be positive and odd");
1266 mm
= (mpmont_pyobj
*)ty
->tp_alloc(ty
, 0);
1267 mpmont_create(&mm
->mm
, xx
);
1269 if (xx
) MP_DROP(xx
);
1270 return ((PyObject
*)mm
);
1273 static PyObject
*mmget_m(PyObject
*me
, void *hunoz
)
1274 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->m
))); }
1276 static PyObject
*mmget_r(PyObject
*me
, void *hunoz
)
1277 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->r
))); }
1279 static PyObject
*mmget_r2(PyObject
*me
, void *hunoz
)
1280 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->r2
))); }
1282 static PyGetSetDef mpmont_pygetset
[] = {
1283 #define GETSETNAME(op, name) mm##op##_##name
1284 GET (m
, "M.m -> modulus for reduction")
1285 GET (r
, "M.r -> multiplicative identity")
1286 GET (r2
, "M.r2 -> M.r^2, Montgomerization factor")
1291 static PyMethodDef mpmont_pymethods
[] = {
1292 #define METHNAME(name) mmmeth_##name
1293 METH (int, "M.out(X) -> XR")
1294 METH (mul
, "M.mul(XR, YR) -> ZR where Z = X Y")
1295 METH (expr
, "M.expr(XR, N) -> ZR where Z = X^N mod M.m")
1297 B.mexp([(XR0, N0), (XR1, N1), ...]) = ZR where Z = X0^N0 X1^N1 mod B.m\n\
1298 \t(the list may be flattened if this more convenient.)")
1299 METH (reduce
, "M.reduce(XR) -> X")
1300 METH (ext
, "M.ext(XR) -> X")
1301 METH (exp
, "M.exp(X, N) -> X^N mod M.m")
1303 B.mexp([(X0, N0), (X1, N1), ...]) = X0^N0 X1^N1 mod B.m\n\
1304 \t(the list may be flattened if this more convenient.)")
1309 static PyTypeObject
*mpmont_pytype
, mpmont_pytype_skel
= {
1310 PyObject_HEAD_INIT(0) 0, /* Header */
1311 "catacomb.MPMont", /* @tp_name@ */
1312 sizeof(mpmont_pyobj
), /* @tp_basicsize@ */
1313 0, /* @tp_itemsize@ */
1315 mpmont_pydealloc
, /* @tp_dealloc@ */
1317 0, /* @tp_getattr@ */
1318 0, /* @tp_setattr@ */
1319 0, /* @tp_compare@ */
1321 0, /* @tp_as_number@ */
1322 0, /* @tp_as_sequence@ */
1323 0, /* @tp_as_mapping@ */
1327 0, /* @tp_getattro@ */
1328 0, /* @tp_setattro@ */
1329 0, /* @tp_as_buffer@ */
1330 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1331 Py_TPFLAGS_BASETYPE
,
1334 "A Montgomery reduction context.",
1336 0, /* @tp_traverse@ */
1338 0, /* @tp_richcompare@ */
1339 0, /* @tp_weaklistoffset@ */
1341 0, /* @tp_iternext@ */
1342 mpmont_pymethods
, /* @tp_methods@ */
1343 0, /* @tp_members@ */
1344 mpmont_pygetset
, /* @tp_getset@ */
1347 0, /* @tp_descr_get@ */
1348 0, /* @tp_descr_set@ */
1349 0, /* @tp_dictoffset@ */
1351 PyType_GenericAlloc
, /* @tp_alloc@ */
1352 mpmont_pynew
, /* @tp_new@ */
1357 /*----- Barrett reduction -------------------------------------------------*/
1359 typedef struct mpbarrett_pyobj
{
1364 #define MPBARRETT_PY(o) (&((mpbarrett_pyobj *)(o))->mb)
1366 static PyObject
*mbmeth_exp(PyObject
*me
, PyObject
*arg
)
1369 mp
*yy
= 0, *zz
= 0;
1371 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1374 if ((yy
= mp_modinv_checked(yy
, yy
, MPBARRETT_PY(me
)->m
)) == 0) goto end
;
1375 zz
= mp_neg(zz
, zz
);
1377 rc
= mp_pywrap(mpbarrett_exp(MPBARRETT_PY(me
), MP_NEW
, yy
, zz
));
1379 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1383 static PyObject
*mb_mexp(PyObject
*me
, void *v
, int n
)
1384 { return mp_pywrap(mpbarrett_mexp(MPBARRETT_PY(me
), MP_NEW
, v
, n
)); }
1386 static PyObject
*mbmeth_mexp(PyObject
*me
, PyObject
*arg
)
1388 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1389 mp_mexp_id
, mp_mexp_fill
, mb_mexp
, mp_mexp_drop
);
1392 static PyObject
*mbmeth_reduce(PyObject
*me
, PyObject
*arg
)
1397 if (!PyArg_ParseTuple(arg
, "O&:reduce", convmp
, &yy
))
1399 z
= mp_pywrap(mpbarrett_reduce(MPBARRETT_PY(me
), MP_NEW
, yy
));
1404 static void mpbarrett_pydealloc(PyObject
*me
)
1406 mpbarrett_destroy(MPBARRETT_PY(me
));
1410 static PyObject
*mpbarrett_pynew(PyTypeObject
*ty
,
1411 PyObject
*arg
, PyObject
*kw
)
1413 mpbarrett_pyobj
*mb
= 0;
1414 char *kwlist
[] = { "m", 0 };
1417 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convmp
, &xx
))
1419 if (!MP_POSP(xx
)) VALERR("m must be positive");
1420 mb
= (mpbarrett_pyobj
*)ty
->tp_alloc(ty
, 0);
1421 mpbarrett_create(&mb
->mb
, xx
);
1423 if (xx
) MP_DROP(xx
);
1424 return ((PyObject
*)mb
);
1427 static PyObject
*mbget_m(PyObject
*me
, void *hunoz
)
1428 { return (mp_pywrap(MP_COPY(MPBARRETT_PY(me
)->m
))); }
1430 static PyGetSetDef mpbarrett_pygetset
[] = {
1431 #define GETSETNAME(op, name) mb##op##_##name
1432 GET (m
, "B.m -> modulus for reduction")
1437 static PyMethodDef mpbarrett_pymethods
[] = {
1438 #define METHNAME(name) mbmeth_##name
1439 METH (reduce
, "B.reduce(X) -> X mod B.m")
1440 METH (exp
, "B.exp(X, N) -> X^N mod B.m")
1442 B.mexp([(X0, N0), (X1, N1), ...]) = X0^N0 X1^N1 mod B.m\n\
1443 \t(the list may be flattened if this more convenient.)")
1448 static PyTypeObject
*mpbarrett_pytype
, mpbarrett_pytype_skel
= {
1449 PyObject_HEAD_INIT(0) 0, /* Header */
1450 "catacomb.MPBarrett", /* @tp_name@ */
1451 sizeof(mpbarrett_pyobj
), /* @tp_basicsize@ */
1452 0, /* @tp_itemsize@ */
1454 mpbarrett_pydealloc
, /* @tp_dealloc@ */
1456 0, /* @tp_getattr@ */
1457 0, /* @tp_setattr@ */
1458 0, /* @tp_compare@ */
1460 0, /* @tp_as_number@ */
1461 0, /* @tp_as_sequence@ */
1462 0, /* @tp_as_mapping@ */
1466 0, /* @tp_getattro@ */
1467 0, /* @tp_setattro@ */
1468 0, /* @tp_as_buffer@ */
1469 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1470 Py_TPFLAGS_BASETYPE
,
1473 "A Barrett reduction context.",
1475 0, /* @tp_traverse@ */
1477 0, /* @tp_richcompare@ */
1478 0, /* @tp_weaklistoffset@ */
1480 0, /* @tp_iternext@ */
1481 mpbarrett_pymethods
, /* @tp_methods@ */
1482 0, /* @tp_members@ */
1483 mpbarrett_pygetset
, /* @tp_getset@ */
1486 0, /* @tp_descr_get@ */
1487 0, /* @tp_descr_set@ */
1488 0, /* @tp_dictoffset@ */
1490 PyType_GenericAlloc
, /* @tp_alloc@ */
1491 mpbarrett_pynew
, /* @tp_new@ */
1496 /*----- Nice prime reduction ----------------------------------------------*/
1498 typedef struct mpreduce_pyobj
{
1503 #define MPREDUCE_PY(o) (&((mpreduce_pyobj *)(o))->mr)
1505 static PyObject
*mrmeth_exp(PyObject
*me
, PyObject
*arg
)
1508 mp
*yy
= 0, *zz
= 0;
1510 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1513 if ((yy
= mp_modinv_checked(yy
, yy
, MPREDUCE_PY(me
)->p
)) == 0) goto end
;
1514 zz
= mp_neg(zz
, zz
);
1516 rc
= mp_pywrap(mpreduce_exp(MPREDUCE_PY(me
), MP_NEW
, yy
, zz
));
1518 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1522 static PyObject
*mrmeth_reduce(PyObject
*me
, PyObject
*arg
)
1527 if (!PyArg_ParseTuple(arg
, "O&:reduce", convmp
, &yy
)) goto end
;
1528 z
= mp_pywrap(mpreduce_do(MPREDUCE_PY(me
), MP_NEW
, yy
));
1533 static void mpreduce_pydealloc(PyObject
*me
)
1535 mpreduce_destroy(MPREDUCE_PY(me
));
1539 static PyObject
*mpreduce_pynew(PyTypeObject
*ty
,
1540 PyObject
*arg
, PyObject
*kw
)
1542 mpreduce_pyobj
*mr
= 0;
1544 char *kwlist
[] = { "m", 0 };
1547 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convmp
, &xx
))
1549 if (!MP_POSP(xx
)) VALERR("m must be positive");
1550 if (mpreduce_create(&r
, xx
)) VALERR("bad modulus (must be 2^k - ...)");
1551 mr
= (mpreduce_pyobj
*)ty
->tp_alloc(ty
, 0);
1554 if (xx
) MP_DROP(xx
);
1555 return ((PyObject
*)mr
);
1558 static PyObject
*mrget_m(PyObject
*me
, void *hunoz
)
1559 { return (mp_pywrap(MP_COPY(MPREDUCE_PY(me
)->p
))); }
1561 static PyGetSetDef mpreduce_pygetset
[] = {
1562 #define GETSETNAME(op, name) mr##op##_##name
1563 GET (m
, "R.m -> modulus for reduction")
1568 static PyMethodDef mpreduce_pymethods
[] = {
1569 #define METHNAME(name) mrmeth_##name
1570 METH (reduce
, "R.reduce(X) -> X mod B.m")
1571 METH (exp
, "R.exp(X, N) -> X^N mod B.m")
1576 static PyTypeObject
*mpreduce_pytype
, mpreduce_pytype_skel
= {
1577 PyObject_HEAD_INIT(0) 0, /* Header */
1578 "catacomb.MPReduce", /* @tp_name@ */
1579 sizeof(mpreduce_pyobj
), /* @tp_basicsize@ */
1580 0, /* @tp_itemsize@ */
1582 mpreduce_pydealloc
, /* @tp_dealloc@ */
1584 0, /* @tp_getattr@ */
1585 0, /* @tp_setattr@ */
1586 0, /* @tp_compare@ */
1588 0, /* @tp_as_number@ */
1589 0, /* @tp_as_sequence@ */
1590 0, /* @tp_as_mapping@ */
1594 0, /* @tp_getattro@ */
1595 0, /* @tp_setattro@ */
1596 0, /* @tp_as_buffer@ */
1597 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1598 Py_TPFLAGS_BASETYPE
,
1601 "A reduction context for reduction modulo primes of special form.",
1603 0, /* @tp_traverse@ */
1605 0, /* @tp_richcompare@ */
1606 0, /* @tp_weaklistoffset@ */
1608 0, /* @tp_iternext@ */
1609 mpreduce_pymethods
, /* @tp_methods@ */
1610 0, /* @tp_members@ */
1611 mpreduce_pygetset
, /* @tp_getset@ */
1614 0, /* @tp_descr_get@ */
1615 0, /* @tp_descr_set@ */
1616 0, /* @tp_dictoffset@ */
1618 PyType_GenericAlloc
, /* @tp_alloc@ */
1619 mpreduce_pynew
, /* @tp_new@ */
1624 /*----- Chinese Remainder Theorem solution --------------------------------*/
1626 typedef struct mpcrt_pyobj
{
1631 #define MPCRT_PY(o) (&((mpcrt_pyobj *)(o))->c)
1633 static PyObject
*mcmeth_solve(PyObject
*me
, PyObject
*arg
)
1635 mpcrt
*c
= MPCRT_PY(me
);
1636 PyObject
*q
= 0, *x
, *z
= 0;
1639 int i
= 0, n
= c
->k
;
1642 if (PyTuple_Size(arg
) == n
)
1644 else if (!PyArg_ParseTuple(arg
, "O:solve", &q
))
1647 if (!PySequence_Check(q
)) TYERR("want a sequence of residues");
1648 if (PySequence_Size(q
) != n
) VALERR("residue count mismatch");
1649 v
= xmalloc(n
* sizeof(*v
));
1650 for (i
= 0; i
< n
; i
++) {
1651 if ((x
= PySequence_GetItem(q
, i
)) == 0) goto end
;
1652 xx
= getmp(x
); Py_DECREF(x
); if (!xx
) goto end
;
1655 z
= mp_pywrap(mpcrt_solve(c
, MP_NEW
, v
));
1659 for (i
= 0; i
< n
; i
++)
1668 static void mpcrt_pydealloc(PyObject
*me
)
1670 mpcrt
*c
= MPCRT_PY(me
);
1675 static PyObject
*mpcrt_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1679 char *kwlist
[] = { "mv", 0 };
1680 PyObject
*q
= 0, *x
;
1684 if (PyTuple_Size(arg
) > 1)
1686 else if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", kwlist
, &q
))
1689 if (!PySequence_Check(q
)) TYERR("want a sequence of moduli");
1690 n
= PySequence_Size(q
);
1691 if (PyErr_Occurred()) goto end
;
1692 if (!n
) VALERR("want at least one modulus");
1693 v
= xmalloc(n
* sizeof(*v
));
1694 for (i
= 0; i
< n
; i
++) {
1695 if ((x
= PySequence_GetItem(q
, i
)) == 0) goto end
;
1696 xx
= getmp(x
); Py_DECREF(x
); if (!xx
) goto end
;
1697 v
[i
].m
= xx
; v
[i
].n
= 0; v
[i
].ni
= 0; v
[i
].nni
= 0;
1699 c
= (mpcrt_pyobj
*)ty
->tp_alloc(ty
, 0);
1700 mpcrt_create(&c
->c
, v
, n
, 0);
1702 return ((PyObject
*)c
);
1707 for (i
= 0; i
< n
; i
++)
1715 static PyObject
*mcget_product(PyObject
*me
, void *hunoz
)
1716 { return (mp_pywrap(MP_COPY(MPCRT_PY(me
)->mb
.m
))); }
1718 static PyObject
*mcget_moduli(PyObject
*me
, void *hunoz
)
1722 mpcrt
*c
= MPCRT_PY(me
);
1724 if ((q
= PyList_New(c
->k
)) == 0) return (0);
1725 for (i
= 0; i
< c
->k
; i
++)
1726 PyList_SetItem(q
, i
, mp_pywrap(c
->v
[i
].m
));
1730 static PyGetSetDef mpcrt_pygetset
[] = {
1731 #define GETSETNAME(op, name) mc##op##_##name
1732 GET (product
, "C.product -> product of moduli")
1733 GET (moduli
, "C.moduli -> list of individual moduli")
1738 static PyMethodDef mpcrt_pymethods
[] = {
1739 #define METHNAME(name) mcmeth_##name
1740 METH (solve
, "C.solve([R0, R1]) -> X mod C.product")
1745 static PyTypeObject
*mpcrt_pytype
, mpcrt_pytype_skel
= {
1746 PyObject_HEAD_INIT(0) 0, /* Header */
1747 "catacomb.MPCRT", /* @tp_name@ */
1748 sizeof(mpcrt_pyobj
), /* @tp_basicsize@ */
1749 0, /* @tp_itemsize@ */
1751 mpcrt_pydealloc
, /* @tp_dealloc@ */
1753 0, /* @tp_getattr@ */
1754 0, /* @tp_setattr@ */
1755 0, /* @tp_compare@ */
1757 0, /* @tp_as_number@ */
1758 0, /* @tp_as_sequence@ */
1759 0, /* @tp_as_mapping@ */
1763 0, /* @tp_getattro@ */
1764 0, /* @tp_setattro@ */
1765 0, /* @tp_as_buffer@ */
1766 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1767 Py_TPFLAGS_BASETYPE
,
1770 "A context for the solution of Chinese Remainder Theorem problems.",
1772 0, /* @tp_traverse@ */
1774 0, /* @tp_richcompare@ */
1775 0, /* @tp_weaklistoffset@ */
1777 0, /* @tp_iternext@ */
1778 mpcrt_pymethods
, /* @tp_methods@ */
1779 0, /* @tp_members@ */
1780 mpcrt_pygetset
, /* @tp_getset@ */
1783 0, /* @tp_descr_get@ */
1784 0, /* @tp_descr_set@ */
1785 0, /* @tp_dictoffset@ */
1787 PyType_GenericAlloc
, /* @tp_alloc@ */
1788 mpcrt_pynew
, /* @tp_new@ */
1793 /*----- Binary polynomials ------------------------------------------------*/
1795 static PyObject
*gf_pyrepr(PyObject
*o
)
1796 { return mp_topystring(MP_X(o
), 16, "GF(", "0x", "L)"); }
1798 static PyObject
*gf_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
1804 if (gfbinop(x
, y
, &xx
, &yy
)) RETURN_NOTIMPL
;
1806 case Py_EQ
: b
= MP_EQ(xx
, yy
); break;
1807 case Py_NE
: b
= !MP_EQ(xx
, yy
); break;
1812 case Py_LT
: b
= xl
< yl
; break;
1813 case Py_LE
: b
= xl
<= yl
; break;
1814 case Py_GT
: b
= xl
> yl
; break;
1815 case Py_GE
: b
= xl
>= yl
; break;
1820 MP_DROP(xx
); MP_DROP(yy
);
1821 return (getbool(b
));
1824 static PyObject
*gf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1830 char *kwlist
[] = { "x", "radix", 0 };
1832 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|i:gf", kwlist
, &x
, &radix
))
1834 if (GF_PYCHECK(x
)) RETURN_OBJ(x
);
1835 if (!good_radix_p(radix
, 1)) VALERR("radix out of range");
1836 if ((z
= mp_frompyobject(x
, radix
)) == 0) {
1837 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to gf",
1838 x
->ob_type
->tp_name
);
1843 VALERR("gf cannot be negative");
1845 zz
= (mp_pyobj
*)ty
->tp_alloc(ty
, 0);
1848 return ((PyObject
*)zz
);
1851 static long gf_pyhash(PyObject
*me
)
1853 long i
= mp_tolong(MP_X(me
));
1854 i
^= 0xc7ecd67c; /* random perturbance */
1860 static PyObject
*gf_pyexp(PyObject
*x
, PyObject
*y
, PyObject
*z
)
1862 mp
*xx
= 0, *yy
= 0, *zz
= 0;
1866 if ((xx
= tomp(x
)) == 0 || (yy
= tomp(y
)) == 0 ||
1867 (z
&& z
!= Py_None
&& (zz
= tomp(z
)) == 0)) {
1868 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
1871 if (!z
|| z
== Py_None
) {
1872 if (MP_NEGP(yy
)) VALERR("negative exponent");
1873 r
= gf_exp(MP_NEW
, xx
, yy
);
1876 if (MP_ZEROP(zz
)) ZDIVERR("zero modulus");
1878 if ((xx
= gf_modinv_checked(xx
, xx
, zz
)) == 0) goto end
;
1879 yy
= mp_neg(yy
, yy
);
1881 gfreduce_create(&gr
, zz
);
1882 r
= gfreduce_exp(&gr
, MP_NEW
, xx
, yy
);
1883 gfreduce_destroy(&gr
);
1887 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
1891 static PyObject
*gfmeth_sqr(PyObject
*me
, PyObject
*arg
)
1893 if (!PyArg_ParseTuple(arg
, ":sqr")) return (0);
1894 return (gf_pywrap(gf_sqr(MP_NEW
, MP_X(me
))));
1897 static PyObject
*gfmeth_gcd(PyObject
*me
, PyObject
*arg
)
1900 mp
*yy
= 0, *zz
= MP_NEW
;
1902 if (!PyArg_ParseTuple(arg
, "O&:gcd", convgf
, &yy
)) goto end
;
1903 gf_gcd(&zz
, 0, 0, MP_X(me
), yy
);
1906 if (yy
) MP_DROP(yy
);
1910 static PyObject
*gfmeth_gcdx(PyObject
*me
, PyObject
*arg
)
1913 mp
*yy
= 0, *zz
= MP_NEW
, *uu
= MP_NEW
, *vv
= MP_NEW
;
1915 if (!PyArg_ParseTuple(arg
, "O&:gcdx", convgf
, &yy
))
1917 gf_gcd(&zz
, &uu
, &vv
, MP_X(me
), yy
);
1918 z
= Py_BuildValue("(NNN)",
1919 gf_pywrap(zz
), gf_pywrap(uu
), gf_pywrap(vv
));
1921 if (yy
) MP_DROP(yy
);
1925 static PyObject
*gfmeth_modinv(PyObject
*me
, PyObject
*arg
)
1928 mp
*yy
= 0, *zz
= MP_NEW
;
1930 if (!PyArg_ParseTuple(arg
, "O&:modinv", convgf
, &yy
) ||
1931 (zz
= gf_modinv_checked(MP_NEW
, yy
, MP_X(me
))) == 0)
1935 if (yy
) MP_DROP(yy
);
1939 static PyObject
*gfmeth_irreduciblep(PyObject
*me
, PyObject
*arg
)
1941 if (!PyArg_ParseTuple(arg
, ":irreduciblep")) return (0);
1942 return getbool(gf_irreduciblep(MP_X(me
)));
1945 static PyObject
*gfget_degree(PyObject
*me
, void *hunoz
)
1946 { return (PyInt_FromLong(mp_bits(MP_X(me
)) - 1)); }
1948 static PyGetSetDef gf_pygetset
[] = {
1949 #define GETSETNAME(op, name) gf##op##_##name
1950 GET (degree
, "X.degree -> polynomial degree of X")
1952 #define GETSETNAME(op, name) mp##op##_##name
1953 GET (nbits
, "X.nbits -> bit length of X")
1954 GET (noctets
, "X.noctets -> octet length of X")
1959 static PyMethodDef gf_pymethods
[] = {
1960 #define METHNAME(func) gfmeth_##func
1961 METH (setbit
, "X.setbit(N) -> X with bit N set")
1962 METH (clearbit
, "X.clearbit(N) -> X with bit N clear")
1963 METH (testbit
, "X.testbit(N) -> true/false if bit N set/clear in X")
1964 METH (sqr
, "X.sqr() -> X^2")
1965 METH (gcd
, "X.gcd(Y) -> gcd(X, Y)")
1967 "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)")
1968 METH (modinv
, "X.modinv(Y) -> multiplicative inverse of Y mod X")
1969 METH (irreduciblep
, "X.irreduciblep() -> true/false")
1971 #define METHNAME(func) mpmeth_##func
1972 KWMETH(tostring
, "X.tostring(radix = 10) -> STR")
1973 KWMETH(storel
, "X.storel(len = -1) -> little-endian bytes")
1974 KWMETH(storeb
, "X.storeb(len = -1) -> big-endian bytes")
1976 "X.storel2c(len = -1) -> little-endian bytes, two's complement")
1978 "X.storeb2c(len = -1) -> big-endian bytes, two's complement")
1979 METH (tobuf
, "X.tobuf() -> buffer format")
1984 static PyNumberMethods gf_pynumber
= {
1985 gf_pyadd
, /* @nb_add@ */
1986 gf_pysub
, /* @nb_subtract@ */
1987 gf_pymul
, /* @nb_multiply@ */
1988 0, /* @nb_divide@ */
1989 gf_pymod
, /* @nb_remainder@ */
1990 gf_pydivmod
, /* @nb_divmod@ */
1991 gf_pyexp
, /* @nb_power@ */
1992 mp_pyid
, /* @nb_negative@ */
1993 mp_pyid
, /* @nb_positive@ */
1994 mp_pyid
, /* @nb_absolute@ */
1995 mp_pynonzerop
, /* @nb_nonzero@ */
1996 0 /* doesn't make any sense */, /* @nb_invert@ */
1997 gf_pylsl
, /* @nb_lshift@ */
1998 gf_pylsr
, /* @nb_rshift@ */
1999 gf_pyand
, /* @nb_and@ */
2000 gf_pyxor
, /* @nb_xor@ */
2001 gf_pyor
, /* @nb_or@ */
2002 gf_pycoerce
, /* @nb_coerce@ */
2003 mp_pyint
, /* @nb_int@ */
2004 mp_pylong
, /* @nb_long@ */
2005 0 /* doesn't make any sense */, /* @nb_float@ */
2006 mp_pyoct
, /* @nb_oct@ */
2007 mp_pyhex
, /* @nb_hex@ */
2009 0, /* @nb_inplace_add@ */
2010 0, /* @nb_inplace_subtract@ */
2011 0, /* @nb_inplace_multiply@ */
2012 0, /* @nb_inplace_divide@ */
2013 0, /* @nb_inplace_remainder@ */
2014 0, /* @nb_inplace_power@ */
2015 0, /* @nb_inplace_lshift@ */
2016 0, /* @nb_inplace_rshift@ */
2017 0, /* @nb_inplace_and@ */
2018 0, /* @nb_inplace_xor@ */
2019 0, /* @nb_inplace_or@ */
2021 gf_pydiv
, /* @nb_floor_divide@ */
2022 0, /* @nb_true_divide@ */
2023 0, /* @nb_inplace_floor_divide@ */
2024 0, /* @nb_inplace_true_divide@ */
2027 static PyTypeObject gf_pytype_skel
= {
2028 PyObject_HEAD_INIT(0) 0, /* Header */
2029 "catacomb.GF", /* @tp_name@ */
2030 sizeof(mp_pyobj
), /* @tp_basicsize@ */
2031 0, /* @tp_itemsize@ */
2033 mp_pydealloc
, /* @tp_dealloc@ */
2035 0, /* @tp_getattr@ */
2036 0, /* @tp_setattr@ */
2037 0, /* @tp_compare@ */
2038 gf_pyrepr
, /* @tp_repr@ */
2039 &gf_pynumber
, /* @tp_as_number@ */
2040 0, /* @tp_as_sequence@ */
2041 0, /* @tp_as_mapping@ */
2042 gf_pyhash
, /* @tp_hash@ */
2044 mp_pyhex
, /* @tp_str@ */
2045 0, /* @tp_getattro@ */
2046 0, /* @tp_setattro@ */
2047 0, /* @tp_as_buffer@ */
2048 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2049 Py_TPFLAGS_CHECKTYPES
|
2050 Py_TPFLAGS_BASETYPE
,
2053 "Binary polynomials. Support almost all the standard arithmetic\n\
2056 Constructor gf(X, radix = R) attempts to convert X to a `gf'. If\n\
2057 X is a string, it's read in radix-R form, or we look for a prefix\n\
2058 if R = 0. Other acceptable things are ints and longs.\n\
2060 The name is hopelessly wrong from a technical point of view, but\n\
2061 but it's much easier to type than `p2' or `c2' or whatever.\n\
2065 * Use `//' for division. GFs don't have `/' division.",
2067 0, /* @tp_traverse@ */
2069 gf_pyrichcompare
, /* @tp_richcompare@ */
2070 0, /* @tp_weaklistoffset@ */
2072 0, /* @tp_iternext@ */
2073 gf_pymethods
, /* @tp_methods@ */
2074 0, /* @tp_members@ */
2075 gf_pygetset
, /* @tp_getset@ */
2078 0, /* @tp_descr_get@ */
2079 0, /* @tp_descr_set@ */
2080 0, /* @tp_dictoffset@ */
2082 PyType_GenericAlloc
, /* @tp_alloc@ */
2083 gf_pynew
, /* @tp_new@ */
2088 static PyObject
*meth__GF_fromstring(PyObject
*me
,
2089 PyObject
*arg
, PyObject
*kw
)
2096 mptext_stringctx sc
;
2097 char *kwlist
[] = { "class", "x", "radix", 0 };
2099 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "Os#|i:fromstring",
2100 kwlist
, &me
, &p
, &len
, &r
))
2102 if (!good_radix_p(r
, 1)) VALERR("bad radix");
2103 sc
.buf
= p
; sc
.lim
= p
+ len
;
2104 if ((zz
= mp_read(MP_NEW
, r
, &mptext_stringops
, &sc
)) == 0 ||
2106 if (zz
) MP_DROP(zz
);
2107 SYNERR("bad binary polynomial");
2109 z
= Py_BuildValue("(Ns#)", gf_pywrap(zz
), sc
.buf
, (int)(sc
.lim
- sc
.buf
));
2114 /*----- Sparse poly reduction ---------------------------------------------*/
2116 typedef struct gfreduce_pyobj
{
2121 #define GFREDUCE_PY(o) (&((gfreduce_pyobj *)(o))->mr)
2123 static PyObject
*grmeth_exp(PyObject
*me
, PyObject
*arg
)
2126 mp
*yy
= 0, *zz
= 0;
2128 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convgf
, &yy
, convgf
, &zz
))
2131 if ((yy
= gf_modinv_checked(yy
, yy
, GFREDUCE_PY(me
)->p
)) == 0) goto end
;
2132 zz
= mp_neg(zz
, zz
);
2134 rc
= gf_pywrap(gfreduce_exp(GFREDUCE_PY(me
), MP_NEW
, yy
, zz
));
2136 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
2140 static PyObject
*grmeth_trace(PyObject
*me
, PyObject
*arg
)
2145 if (!PyArg_ParseTuple(arg
, "O&:trace", convgf
, &xx
)) goto end
;
2146 rc
= PyInt_FromLong(gfreduce_trace(GFREDUCE_PY(me
), xx
));
2148 if (xx
) MP_DROP(xx
);
2152 static PyObject
*grmeth_halftrace(PyObject
*me
, PyObject
*arg
)
2157 if (!PyArg_ParseTuple(arg
, "O&:halftrace", convgf
, &xx
)) goto end
;
2158 rc
= gf_pywrap(gfreduce_halftrace(GFREDUCE_PY(me
), MP_NEW
, xx
));
2160 if (xx
) MP_DROP(xx
);
2164 static PyObject
*grmeth_sqrt(PyObject
*me
, PyObject
*arg
)
2169 if (!PyArg_ParseTuple(arg
, "O&:sqrt", convgf
, &xx
)) goto end
;
2170 if ((yy
= gfreduce_sqrt(GFREDUCE_PY(me
), MP_NEW
, xx
)) == 0)
2171 VALERR("no modular square root");
2174 if (xx
) MP_DROP(xx
);
2178 static PyObject
*grmeth_quadsolve(PyObject
*me
, PyObject
*arg
)
2183 if (!PyArg_ParseTuple(arg
, "O&:quadsolve", convgf
, &xx
)) goto end
;
2184 if ((yy
= gfreduce_quadsolve(GFREDUCE_PY(me
), MP_NEW
, xx
)) == 0)
2185 VALERR("no solution found");
2188 if (xx
) MP_DROP(xx
);
2192 static PyObject
*grmeth_reduce(PyObject
*me
, PyObject
*arg
)
2197 if (!PyArg_ParseTuple(arg
, "O&:reduce", convgf
, &yy
)) goto end
;
2198 z
= gf_pywrap(gfreduce_do(GFREDUCE_PY(me
), MP_NEW
, yy
));
2203 static void gfreduce_pydealloc(PyObject
*me
)
2205 gfreduce_destroy(GFREDUCE_PY(me
));
2209 static PyObject
*gfreduce_pynew(PyTypeObject
*ty
,
2210 PyObject
*arg
, PyObject
*kw
)
2212 gfreduce_pyobj
*mr
= 0;
2214 char *kwlist
[] = { "m", 0 };
2217 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", kwlist
, convgf
, &xx
))
2219 if (MP_ZEROP(xx
)) ZDIVERR("modulus is zero!");
2220 gfreduce_create(&r
, xx
);
2221 mr
= (gfreduce_pyobj
*)ty
->tp_alloc(ty
, 0);
2224 if (xx
) MP_DROP(xx
);
2225 return ((PyObject
*)mr
);
2228 static PyObject
*grget_m(PyObject
*me
, void *hunoz
)
2229 { return (gf_pywrap(MP_COPY(GFREDUCE_PY(me
)->p
))); }
2231 static PyGetSetDef gfreduce_pygetset
[] = {
2232 #define GETSETNAME(op, name) gr##op##_##name
2233 GET (m
, "R.m -> reduction polynomial")
2238 static PyMethodDef gfreduce_pymethods
[] = {
2239 #define METHNAME(name) grmeth_##name
2240 METH (reduce
, "R.reduce(X) -> X mod B.m")
2241 METH (trace
, "R.trace(X) -> Tr(X) = x + x^2 + ... + x^{2^{m - 1}}")
2242 METH (halftrace
, "R.halftrace(X) -> x + x^{2^2} + ... + x^{2^{m - 1}}")
2243 METH (sqrt
, "R.sqrt(X) -> Y where Y^2 = X mod R")
2244 METH (quadsolve
, "R.quadsolve(X) -> Y where Y^2 + Y = X mod R")
2245 METH (exp
, "R.exp(X, N) -> X^N mod B.m")
2250 static PyTypeObject
*gfreduce_pytype
, gfreduce_pytype_skel
= {
2251 PyObject_HEAD_INIT(0) 0, /* Header */
2252 "catacomb.GFReduce", /* @tp_name@ */
2253 sizeof(gfreduce_pyobj
), /* @tp_basicsize@ */
2254 0, /* @tp_itemsize@ */
2256 gfreduce_pydealloc
, /* @tp_dealloc@ */
2258 0, /* @tp_getattr@ */
2259 0, /* @tp_setattr@ */
2260 0, /* @tp_compare@ */
2262 0, /* @tp_as_number@ */
2263 0, /* @tp_as_sequence@ */
2264 0, /* @tp_as_mapping@ */
2268 0, /* @tp_getattro@ */
2269 0, /* @tp_setattro@ */
2270 0, /* @tp_as_buffer@ */
2271 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2272 Py_TPFLAGS_BASETYPE
,
2275 "A reduction context for reduction modulo sparse irreducible polynomials.",
2277 0, /* @tp_traverse@ */
2279 0, /* @tp_richcompare@ */
2280 0, /* @tp_weaklistoffset@ */
2282 0, /* @tp_iternext@ */
2283 gfreduce_pymethods
, /* @tp_methods@ */
2284 0, /* @tp_members@ */
2285 gfreduce_pygetset
, /* @tp_getset@ */
2288 0, /* @tp_descr_get@ */
2289 0, /* @tp_descr_set@ */
2290 0, /* @tp_dictoffset@ */
2292 PyType_GenericAlloc
, /* @tp_alloc@ */
2293 gfreduce_pynew
, /* @tp_new@ */
2298 /*----- Normal/poly transformation ----------------------------------------*/
2300 typedef struct gfn_pyobj
{
2306 static PyTypeObject
*gfn_pytype
, gfn_pytype_skel
;
2308 #define GFN_P(o) (((gfn_pyobj *)(o))->p)
2309 #define GFN_PTON(o) (&((gfn_pyobj *)(o))->pton)
2310 #define GFN_NTOP(o) (&((gfn_pyobj *)(o))->ntop)
2312 static PyObject
*gfn_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2314 mp
*p
= 0, *beta
= 0;
2316 char *kwlist
[] = { "p", "beta", 0 };
2318 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:new", kwlist
,
2319 convgf
, &p
, convgf
, &beta
))
2321 gg
= PyObject_New(gfn_pyobj
, ty
);
2322 if (gfn_create(p
, beta
, &gg
->ntop
, &gg
->pton
)) {
2325 VALERR("can't invert transformation matrix");
2331 return ((PyObject
*)gg
);
2334 static PyObject
*gfnget_p(PyObject
*me
, void *hunoz
)
2335 { return (gf_pywrap(MP_COPY(GFN_P(me
)))); }
2337 static PyObject
*gfnget_beta(PyObject
*me
, void *hunoz
)
2339 gfn
*n
= GFN_NTOP(me
);
2340 mp
*x
= n
->r
[n
->n
- 1];
2341 return (gf_pywrap(MP_COPY(x
)));
2344 #define XFORMOP(name, NAME) \
2345 static PyObject *gfnmeth_##name(PyObject *me, PyObject *arg) \
2350 if (!PyArg_ParseTuple(arg, "O&:" #name, convgf, &xx)) goto end; \
2351 z = gfn_transform(GFN_##NAME(me), MP_NEW, xx); \
2354 if (!z) return (0); \
2355 return (mp_pywrap(z)); \
2361 static void gfn_pydealloc(PyObject
*me
)
2363 gfn_destroy(GFN_PTON(me
));
2364 gfn_destroy(GFN_NTOP(me
));
2368 static PyGetSetDef gfn_pygetset
[] = {
2369 #define GETSETNAME(op, name) gfn##op##_##name
2370 GET (p
, "X.p -> polynomial basis, as polynomial")
2371 GET (beta
, "X.beta -> normal basis element, in poly form")
2376 static PyMethodDef gfn_pymethods
[] = {
2377 #define METHNAME(name) gfnmeth_##name
2378 METH (pton
, "X.pton(A) -> normal-basis representation of A")
2379 METH (ntop
, "X.ntop(A) -> polynomial-basis representation of A")
2384 static PyTypeObject gfn_pytype_skel
= {
2385 PyObject_HEAD_INIT(0) 0, /* Header */
2386 "catacomb.GFN", /* @tp_name@ */
2387 sizeof(gfn_pyobj
), /* @tp_basicsize@ */
2388 0, /* @tp_itemsize@ */
2390 gfn_pydealloc
, /* @tp_dealloc@ */
2392 0, /* @tp_getattr@ */
2393 0, /* @tp_setattr@ */
2394 0, /* @tp_compare@ */
2396 0, /* @tp_as_number@ */
2397 0, /* @tp_as_sequence@ */
2398 0, /* @tp_as_mapping@ */
2402 0, /* @tp_getattro@ */
2403 0, /* @tp_setattro@ */
2404 0, /* @tp_as_buffer@ */
2405 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2406 Py_TPFLAGS_BASETYPE
,
2409 "An object for transforming elements of binary fields between polynomial\n\
2410 and normal basis representations.",
2412 0, /* @tp_traverse@ */
2414 0, /* @tp_richcompare@ */
2415 0, /* @tp_weaklistoffset@ */
2417 0, /* @tp_iternext@ */
2418 gfn_pymethods
, /* @tp_methods@ */
2419 0, /* @tp_members@ */
2420 gfn_pygetset
, /* @tp_getset@ */
2423 0, /* @tp_descr_get@ */
2424 0, /* @tp_descr_set@ */
2425 0, /* @tp_dictoffset@ */
2427 PyType_GenericAlloc
, /* @tp_alloc@ */
2428 gfn_pynew
, /* @tp_new@ */
2433 /*----- Glue --------------------------------------------------------------*/
2435 static PyMethodDef methods
[] = {
2436 #define METHNAME(func) meth_##func
2437 KWMETH(_MP_fromstring
, "\
2438 fromstring(STR, radix = 0) -> (X, REST)\n\
2440 Parse STR as a large integer, according to radix. If radix is zero,\n\
2441 read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\
2442 or `R_' for other radix R.")
2443 KWMETH(_GF_fromstring
, "\
2444 fromstring(STR, radix = 0) -> (X, REST)\n\
2446 Parse STR as a binary polynomial, according to radix. If radix is zero,\n\
2447 read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\
2448 or `R_' for other radix R.")
2450 loadl(STR) -> X: read little-endian bytes")
2452 loadb(STR) -> X: read big-endian bytes")
2453 METH (_MP_loadl2c
, "\
2454 loadl2c(STR) -> X: read little-endian bytes, two's complement")
2455 METH (_MP_loadb2c
, "\
2456 loadb2c(STR) -> X: read big-endian bytes, two's complement")
2457 METH (_MP_frombuf
, "\
2458 frombuf(STR) -> (X, REST): read buffer format")
2460 loadl(STR) -> X: read little-endian bytes")
2462 loadb(STR) -> X: read big-endian bytes")
2463 METH (_GF_frombuf
, "\
2464 frombuf(STR) -> (X, REST): read buffer format")
2469 void mp_pyinit(void)
2473 INITTYPE(mpmul
, root
);
2474 INITTYPE(mpmont
, root
);
2475 INITTYPE(mpbarrett
, root
);
2476 INITTYPE(mpreduce
, root
);
2477 INITTYPE(mpcrt
, root
);
2478 INITTYPE(gfreduce
, root
);
2479 INITTYPE(gfn
, root
);
2480 addmethods(methods
);
2483 void mp_pyinsert(PyObject
*mod
)
2485 INSERT("MP", mp_pytype
);
2486 INSERT("MPMul", mpmul_pytype
);
2487 INSERT("MPMont", mpmont_pytype
);
2488 INSERT("MPBarrett", mpbarrett_pytype
);
2489 INSERT("MPReduce", mpreduce_pytype
);
2490 INSERT("MPCRT", mpcrt_pytype
);
2491 INSERT("GF", gf_pytype
);
2492 INSERT("GFReduce", gfreduce_pytype
);
2493 INSERT("GFN", gfn_pytype
);
2496 /*----- That's all, folks -------------------------------------------------*/