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
, Py_SIZE(l
) < 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 Py_SIZE(l
) = (x
->f
& MP_NEG
) ?
-sz
: sz
;
98 return ((PyObject
*)l
);
101 mp
*mp_frompyobject(PyObject
*o
, int radix
)
109 TEXT_PTRLEN(o
, sc
.buf
, sz
); sc
.lim
= sc
.buf
+ sz
;
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 TEXT_PREPAREWRITE(o
, p
, len
+ 1 + xprelen
+ prelen
+ postlen
);
133 if (xpre
) { memcpy(sc
.buf
, xpre
, xprelen
); sc
.buf
+= xprelen
; }
134 if (MP_NEGP(x
)) { *sc
.buf
++ = '-'; x
= mp_neg(x
, x
); }
135 if (pre
) { memcpy(sc
.buf
, pre
, prelen
); sc
.buf
+= prelen
; }
136 sc
.lim
= sc
.buf
+ len
;
137 mp_write(x
, radix
, &mptext_stringops
, &sc
);
138 if (post
) { memcpy(sc
.buf
, post
, postlen
); sc
.buf
+= postlen
; }
140 TEXT_DONEWRITE(o
, sc
.buf
- p
);
144 static int good_radix_p(int r
, int readp
)
146 return ((r
>= -255 && r
<= -2) ||
148 (r
>= 2 && r
<= 62));
151 PyObject
*mp_pywrap(mp
*x
)
153 mp_pyobj
*z
= PyObject_New(mp_pyobj
, mp_pytype
);
155 return ((PyObject
*)z
);
158 PyObject
*gf_pywrap(mp
*x
)
160 mp_pyobj
*z
= PyObject_New(mp_pyobj
, gf_pytype
);
162 return ((PyObject
*)z
);
165 int mp_tolong_checked(mp
*x
, long *l
, int must
)
167 static mp
*longmin
= 0, *longmax
= 0;
171 longmin
= mp_fromlong(MP_NEW
, LONG_MIN
);
172 longmax
= mp_fromlong(MP_NEW
, LONG_MAX
);
174 if (MP_CMP(x
, <, longmin
) || MP_CMP(x
, >, longmax
)) {
175 if (must
) VALERR("mp out of range for int");
184 /*----- Python interface --------------------------------------------------*/
186 static void mp_pydealloc(PyObject
*o
)
192 static PyObject
*mp_pyrepr(PyObject
*o
)
193 { return mp_topystring(MP_X(o
), 10, "MP(", 0, "L)"); }
195 static PyObject
*mp_pystr(PyObject
*o
)
196 { return mp_topystring(MP_X(o
), 10, 0, 0, 0); }
198 mp
*tomp(PyObject
*o
)
205 else if (MP_PYCHECK(o
) || GF_PYCHECK(o
))
206 return (MP_COPY(MP_X(o
)));
207 else if (FE_PYCHECK(o
))
208 return (F_OUT(FE_F(o
), MP_NEW
, FE_X(o
)));
209 else if (PFILT_PYCHECK(o
))
210 return (MP_COPY(PFILT_F(o
)->m
));
211 else if (ECPT_PYCHECK(o
)) {
213 if (EC_ATINF(ECPT_P(o
))) return (0);
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 Py_TYPE(o
)->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 Py_TYPE(o
)->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 static const char *const 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 Py_TYPE(x
)->tp_name
);
541 zz
= (mp_pyobj
*)ty
->tp_alloc(ty
, 0);
544 return ((PyObject
*)zz
);
547 Py_hash_t
mphash(mp
*x
)
549 PyObject
*l
= mp_topylong(x
);
550 Py_hash_t h
= PyObject_Hash(l
);
551 Py_DECREF(l
); return (h
);
554 static Py_hash_t
mp_pyhash(PyObject
*me
) { return (mphash(MP_X(me
))); }
556 static PyObject
*mpmeth_jacobi(PyObject
*me
, PyObject
*arg
)
561 if (!PyArg_ParseTuple(arg
, "O&:jacobi", convmp
, &y
)) goto end
;
562 z
= PyInt_FromLong(mp_jacobi(y
, MP_X(me
)));
568 #define BITOP(pre, name, c) \
569 static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \
572 if (!PyArg_ParseTuple(arg, "O&:" #name, convulong, &i)) return (0); \
573 return (pre##_pywrap(mp_##name##c(MP_NEW, MP_X(me), i))); \
575 BITOP(mp
, setbit
, 2c
);
576 BITOP(mp
, clearbit
, 2c
);
578 BITOP(gf
, clearbit
, );
581 static PyObject
*mpmeth_testbit(PyObject
*me
, PyObject
*arg
)
584 if (!PyArg_ParseTuple(arg
, "O&:testbit", convulong
, &i
)) return (0);
585 return (getbool(mp_testbit2c(MP_X(me
), i
)));
588 static PyObject
*gfmeth_testbit(PyObject
*me
, PyObject
*arg
)
591 if (!PyArg_ParseTuple(arg
, "O&:testbit", convulong
, &i
)) return (0);
592 return (getbool(mp_testbit(MP_X(me
), i
)));
595 static PyObject
*mpmeth_odd(PyObject
*me
)
600 t
= mp_odd(MP_NEW
, MP_X(me
), &s
);
601 return (Py_BuildValue("(lN)", (long)s
, mp_pywrap(t
)));
604 static PyObject
*mpmeth_sqr(PyObject
*me
)
605 { return (mp_pywrap(mp_sqr(MP_NEW
, MP_X(me
)))); }
607 static PyObject
*mpmeth_sqrt(PyObject
*me
)
609 if (MP_NEGP(MP_X(me
))) VALERR("negative root");
610 return (mp_pywrap(mp_sqrt(MP_NEW
, MP_X(me
))));
615 static PyObject
*mpmeth_gcd(PyObject
*me
, PyObject
*arg
)
617 mp
*y
= 0, *zz
= MP_NEW
;
620 if (!PyArg_ParseTuple(arg
, "O&:gcd", convmp
, &y
)) goto end
;
621 mp_gcd(&zz
, 0, 0, MP_X(me
), y
);
628 static PyObject
*mpmeth_gcdx(PyObject
*me
, PyObject
*arg
)
631 mp
*yy
= 0, *zz
= MP_NEW
, *uu
= MP_NEW
, *vv
= MP_NEW
;
633 if (!PyArg_ParseTuple(arg
, "O&:gcdx", convmp
, &yy
)) goto end
;
634 mp_gcd(&zz
, &uu
, &vv
, MP_X(me
), yy
);
635 z
= Py_BuildValue("(NNN)",
636 mp_pywrap(zz
), mp_pywrap(uu
), mp_pywrap(vv
));
642 static PyObject
*mpmeth_modinv(PyObject
*me
, PyObject
*arg
)
645 mp
*yy
= 0, *zz
= MP_NEW
;
647 if (!PyArg_ParseTuple(arg
, "O&:modinv", convmp
, &yy
) ||
648 (zz
= mp_modinv_checked(MP_NEW
, yy
, MP_X(me
))) == 0)
656 static PyObject
*mpmeth_tostring(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
659 static const char *const kwlist
[] = { "radix", 0 };
660 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|i:tostring", KWLIST
, &radix
))
662 if (!good_radix_p(radix
, 0)) VALERR("bad radix");
663 return (mp_topystring(MP_X(me
), radix
, 0, 0, 0));
668 static PyObject
*mpmeth_modsqrt(PyObject
*me
, PyObject
*arg
)
671 mp
*yy
= 0, *zz
= MP_NEW
;
673 if (!PyArg_ParseTuple(arg
, "O&:modsqrt", convmp
, &yy
)) goto end
;
674 if ((zz
= mp_modsqrt(MP_NEW
, yy
, MP_X(me
))) == 0)
675 VALERR("no modular square root");
682 static PyObject
*mpmeth_leastcongruent(PyObject
*me
, PyObject
*arg
)
687 if (!PyArg_ParseTuple(arg
, "O&O&:leastcongruent", convmp
, &b
, convmp
, &m
))
689 z
= mp_leastcongruent(MP_NEW
, b
, MP_X(me
), m
);
695 #define STOREOP(name, c) \
696 static PyObject *mpmeth_##name(PyObject *me, \
697 PyObject *arg, PyObject *kw) \
700 static const char *const kwlist[] = { "len", 0 }; \
703 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|l:" #name, \
707 len = mp_octets##c(MP_X(me)); \
710 rc = bytestring_pywrap(0, len); \
711 mp_##name(MP_X(me), BIN_PTR(rc), len); \
717 STOREOP(storel2c
, 2c
)
718 STOREOP(storeb2c
, 2c
)
722 static PyObject *ty##meth_frombuf(PyObject *me, PyObject *arg) \
729 if (!PyArg_ParseTuple(arg, "O&:frombuf", convbin, &in)) goto end; \
730 buf_init(&b, (/*unconst*/ void *)in.p, in.sz); \
731 if ((x = buf_getmp(&b)) == 0) VALERR("malformed data"); \
732 rc = Py_BuildValue("(NN)", ty##_pywrap(x), \
733 bytestring_pywrapbuf(&b)); \
741 static PyObject
*mpmeth_tobuf(PyObject
*me
)
749 n
= mp_octets(x
) + 3;
750 rc
= bytestring_pywrap(0, n
);
751 buf_init(&b
, BIN_PTR(rc
), n
);
754 BIN_SETLEN(rc
, BLEN(&b
));
758 static PyObject
*mpmeth_primep(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
760 grand
*r
= &rand_global
;
761 static const char *const kwlist
[] = { "rng", 0 };
764 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&", KWLIST
, convgrand
, &r
))
766 rc
= getbool(pgen_primep(MP_X(me
), r
));
771 static PyObject
*mpmeth_fromstring(PyObject
*me
,
772 PyObject
*arg
, PyObject
*kw
)
780 static const char *const kwlist
[] = { "x", "radix", 0 };
782 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|i:fromstring", KWLIST
,
785 if (!good_radix_p(r
, 1)) VALERR("bad radix");
786 sc
.buf
= p
; sc
.lim
= p
+ len
;
787 if ((zz
= mp_read(MP_NEW
, r
, &mptext_stringops
, &sc
)) == 0)
788 VALERR("bad integer");
789 z
= Py_BuildValue("(Ns#)", mp_pywrap(zz
),
790 sc
.buf
, (Py_ssize_t
)(sc
.lim
- sc
.buf
));
795 static PyObject
*mpmeth_factorial(PyObject
*me
, PyObject
*arg
)
799 if (!PyArg_ParseTuple(arg
, "O&:factorial", convulong
, &i
)) return (0);
804 static PyObject
*mpmeth_fibonacci(PyObject
*me
, PyObject
*arg
)
808 if (!PyArg_ParseTuple(arg
, "l:fibonacci", &i
)) return (0);
813 #define LOADOP(pre, name) \
814 static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \
817 if (!PyArg_ParseTuple(arg, "O&:" #name, convbin, &in)) return (0); \
818 return (pre##_pywrap(mp_##name(MP_NEW, in.p, in.sz))); \
828 static PyObject
*mpget_nbits(PyObject
*me
, void *hunoz
)
829 { return (PyInt_FromLong(mp_bits(MP_X(me
)))); }
831 static PyObject
*mpget_noctets(PyObject
*me
, void *hunoz
)
832 { return (PyInt_FromLong(mp_octets(MP_X(me
)))); }
834 static PyObject
*mpget_noctets2c(PyObject
*me
, void *hunoz
)
835 { return (PyInt_FromLong(mp_octets2c(MP_X(me
)))); }
837 static const PyGetSetDef mp_pygetset
[] = {
838 #define GETSETNAME(op, func) mp##op##_##func
839 GET (nbits
, "X.nbits -> bit length of X")
840 GET (noctets
, "X.noctets -> octet length of X")
841 GET (noctets2c
, "X.noctets2c -> two's complement octet length of X")
846 static const PyMethodDef mp_pymethods
[] = {
847 #define METHNAME(func) mpmeth_##func
848 METH (jacobi
, "X.jacobi(Y) -> Jacobi symbol (Y|X) (NB inversion!)")
849 METH (setbit
, "X.setbit(N) -> X with bit N set")
850 METH (clearbit
, "X.clearbit(N) -> X with bit N clear")
851 METH (testbit
, "X.testbit(N) -> true/false if bit N set/clear in X")
852 NAMETH(odd
, "X.odd() -> S, T where X = 2^S T with T odd")
853 NAMETH(sqr
, "X.sqr() -> X^2")
854 NAMETH(sqrt
, "X.sqrt() -> largest integer <= sqrt(X)")
855 METH (gcd
, "X.gcd(Y) -> gcd(X, Y)")
856 METH (gcdx
, "X.gcdx(Y) -> (gcd(X, Y), U, V) "
857 "with X U + Y V = gcd(X, Y)")
858 METH (modinv
, "X.modinv(Y) -> multiplicative inverse of Y mod X")
859 METH (modsqrt
, "X.modsqrt(Y) -> square root of Y mod X, if X prime")
860 METH (leastcongruent
, "X.leastcongruent(B, M) -> "
861 "smallest Z >= B with Z == X (mod M)")
862 KWMETH(primep
, "X.primep([rng = rand]) -> X is prime?")
863 KWMETH(tostring
, "X.tostring([radix = 10]) -> STR")
864 KWMETH(storel
, "X.storel([len = -1]) -> little-endian bytes")
865 KWMETH(storeb
, "X.storeb([len = -1]) -> big-endian bytes")
866 KWMETH(storel2c
, "X.storel2c([len = -1]) -> "
867 "little-endian bytes, two's complement")
868 KWMETH(storeb2c
, "X.storeb2c([len = -1]) -> "
869 "big-endian bytes, two's complement")
870 NAMETH(tobuf
, "X.tobuf() -> buffer format")
871 KWSMTH(fromstring
, "fromstring(STR, [radix = 0]) -> (X, REST)\n"
872 " Parse STR as a large integer, according to RADIX. If RADIX is\n"
873 " zero, read a prefix from STR to decide radix: allow `0b' for binary,\n"
874 " `0' or `0o' for octal, `0x' for hex, or `R_' for other radix R.")
875 SMTH (factorial
, "factorial(I) -> I!: compute factorial")
876 SMTH (fibonacci
, "fibonacci(I) -> F(I): compute Fibonacci number")
877 SMTH (loadl
, "loadl(STR) -> X: read little-endian bytes")
878 SMTH (loadb
, "loadb(STR) -> X: read big-endian bytes")
879 SMTH (loadl2c
, "loadl2c(STR) -> X: "
880 "read little-endian bytes, two's complement")
881 SMTH (loadb2c
, "loadb2c(STR) -> X: "
882 "read big-endian bytes, two's complement")
883 SMTH (frombuf
, "frombuf(STR) -> (X, REST): read buffer format")
888 static const PyNumberMethods mp_pynumber
= {
889 mp_pyadd
, /* @nb_add@ */
890 mp_pysub
, /* @nb_subtract@ */
891 mp_pymul
, /* @nb_multiply@ */
893 mp_pymod
, /* @nb_remainder@ */
894 mp_pydivmod
, /* @nb_divmod@ */
895 mp_pyexp
, /* @nb_power@ */
896 mp_pyneg
, /* @nb_negative@ */
897 mp_pyid
, /* @nb_positive@ */
898 mp_pyabs
, /* @nb_absolute@ */
899 mp_pynonzerop
, /* @nb_nonzero@ */
900 mp_pynot2c
, /* @nb_invert@ */
901 mp_pylsl2c
, /* @nb_lshift@ */
902 mp_pylsr2c
, /* @nb_rshift@ */
903 mp_pyand2c
, /* @nb_and@ */
904 mp_pyxor2c
, /* @nb_xor@ */
905 mp_pyor2c
, /* @nb_or@ */
906 mp_pycoerce
, /* @nb_coerce@ */
907 mp_pyint
, /* @nb_int@ */
908 mp_pylong
, /* @nb_long@ */
909 mp_pyfloat
, /* @nb_float@ */
910 mp_pyoct
, /* @nb_oct@ */
911 mp_pyhex
, /* @nb_hex@ */
913 0, /* @nb_inplace_add@ */
914 0, /* @nb_inplace_subtract@ */
915 0, /* @nb_inplace_multiply@ */
916 0, /* @nb_inplace_divide@ */
917 0, /* @nb_inplace_remainder@ */
918 0, /* @nb_inplace_power@ */
919 0, /* @nb_inplace_lshift@ */
920 0, /* @nb_inplace_rshift@ */
921 0, /* @nb_inplace_and@ */
922 0, /* @nb_inplace_xor@ */
923 0, /* @nb_inplace_or@ */
925 mp_pydiv
, /* @nb_floor_divide@ */
926 0, /* @nb_true_divide@ */
927 0, /* @nb_inplace_floor_divide@ */
928 0, /* @nb_inplace_true_divide@ */
931 static const PyTypeObject mp_pytype_skel
= {
932 PyVarObject_HEAD_INIT(0, 0) /* Header */
933 "MP", /* @tp_name@ */
934 sizeof(mp_pyobj
), /* @tp_basicsize@ */
935 0, /* @tp_itemsize@ */
937 mp_pydealloc
, /* @tp_dealloc@ */
939 0, /* @tp_getattr@ */
940 0, /* @tp_setattr@ */
941 mp_pycompare
, /* @tp_compare@ */
942 mp_pyrepr
, /* @tp_repr@ */
943 PYNUMBER(mp
), /* @tp_as_number@ */
944 0, /* @tp_as_sequence@ */
945 0, /* @tp_as_mapping@ */
946 mp_pyhash
, /* @tp_hash@ */
948 mp_pystr
, /* @tp_str@ */
949 0, /* @tp_getattro@ */
950 0, /* @tp_setattro@ */
951 0, /* @tp_as_buffer@ */
952 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
953 Py_TPFLAGS_CHECKTYPES
|
957 "Multiprecision integers, similar to `long' but more efficient and\n"
958 "versatile. Support all the standard arithmetic operations, with\n"
959 "implicit conversions from `PrimeFilter', and other objects which\n"
960 "convert to `long'.\n"
962 "Constructor MP(X, [radix = R]) attempts to convert X to an `MP'. If\n"
963 "X is a string, it's read in radix-R form, or we look for a prefix\n"
964 "if R = 0. Other acceptable things are field elements, elliptic curve\n"
965 "points, group elements, Python `int' and `long' objects, and anything\n"
966 "with an integer conversion.\n"
970 " * Use `//' for integer division: `/' gives exact rational division.",
972 0, /* @tp_traverse@ */
974 0, /* @tp_richcompare@ */
975 0, /* @tp_weaklistoffset@ */
977 0, /* @tp_iternext@ */
978 PYMETHODS(mp
), /* @tp_methods@ */
979 0, /* @tp_members@ */
980 PYGETSET(mp
), /* @tp_getset@ */
983 0, /* @tp_descr_get@ */
984 0, /* @tp_descr_set@ */
985 0, /* @tp_dictoffset@ */
987 PyType_GenericAlloc
, /* @tp_alloc@ */
988 mp_pynew
, /* @tp_new@ */
993 /*----- Products of small integers ----------------------------------------*/
995 static PyTypeObject
*mpmul_pytype
;
997 typedef struct mpmul_pyobj
{
1003 #define MPMUL_LIVEP(o) (((mpmul_pyobj *)(o))->livep)
1004 #define MPMUL_PY(o) (&((mpmul_pyobj *)(o))->mm)
1006 static void mpmul_pydealloc(PyObject
*me
)
1008 if (MPMUL_LIVEP(me
))
1009 mp_drop(mpmul_done(MPMUL_PY(me
)));
1013 static PyObject
*mmmeth_factor(PyObject
*me
, PyObject
*arg
)
1018 if (!MPMUL_LIVEP(me
)) VALERR("MPMul object invalid");
1019 if (PyTuple_GET_SIZE(arg
) != 1)
1020 i
= PyObject_GetIter(arg
);
1022 if ((q
= PyTuple_GET_ITEM(arg
, 0)) == 0) goto end
;
1023 if ((i
= PyObject_GetIter(q
)) == 0) {
1024 PyErr_Clear(); /* that's ok */
1025 i
= PyObject_GetIter(arg
);
1029 while ((q
= PyIter_Next(i
)) != 0) {
1030 x
= getmp(q
); Py_DECREF(q
); if (!x
) {
1034 mpmul_add(MPMUL_PY(me
), x
);
1043 static PyObject
*mmmeth_done(PyObject
*me
)
1047 if (!MPMUL_LIVEP(me
)) VALERR("MPMul object invalid");
1048 x
= mpmul_done(MPMUL_PY(me
));
1049 MPMUL_LIVEP(me
) = 0;
1050 return (mp_pywrap(x
));
1055 static PyObject
*mpmul_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1059 if (kw
) TYERR("keyword arguments not allowed here");
1060 mm
= (mpmul_pyobj
*)ty
->tp_alloc(ty
, 0);
1061 mpmul_init(&mm
->mm
);
1063 if (mmmeth_factor((PyObject
*)mm
, arg
) == 0) {
1067 return ((PyObject
*)mm
);
1072 static PyObject
*mmget_livep(PyObject
*me
, void *hunoz
)
1073 { return (getbool(MPMUL_LIVEP(me
))); }
1075 static const PyGetSetDef mpmul_pygetset
[] = {
1076 #define GETSETNAME(op, name) mm##op##_##name
1077 GET (livep
, "MM.livep -> flag: object still valid?")
1082 static const PyMethodDef mpmul_pymethods
[] = {
1083 #define METHNAME(name) mmmeth_##name
1084 METH (factor
, "MM.factor(ITERABLE) or MM.factor(I, ...)")
1085 NAMETH(done
, "MM.done() -> PRODUCT")
1090 static const PyTypeObject mpmul_pytype_skel
= {
1091 PyVarObject_HEAD_INIT(0, 0) /* Header */
1092 "MPMul", /* @tp_name@ */
1093 sizeof(mpmul_pyobj
), /* @tp_basicsize@ */
1094 0, /* @tp_itemsize@ */
1096 mpmul_pydealloc
, /* @tp_dealloc@ */
1098 0, /* @tp_getattr@ */
1099 0, /* @tp_setattr@ */
1100 0, /* @tp_compare@ */
1102 0, /* @tp_as_number@ */
1103 0, /* @tp_as_sequence@ */
1104 0, /* @tp_as_mapping@ */
1108 0, /* @tp_getattro@ */
1109 0, /* @tp_setattro@ */
1110 0, /* @tp_as_buffer@ */
1111 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1112 Py_TPFLAGS_BASETYPE
,
1115 "MPMul(N_0, N_1, ....): an object for multiplying many small integers.",
1117 0, /* @tp_traverse@ */
1119 0, /* @tp_richcompare@ */
1120 0, /* @tp_weaklistoffset@ */
1122 0, /* @tp_iternext@ */
1123 PYMETHODS(mpmul
), /* @tp_methods@ */
1124 0, /* @tp_members@ */
1125 PYGETSET(mpmul
), /* @tp_getset@ */
1128 0, /* @tp_descr_get@ */
1129 0, /* @tp_descr_set@ */
1130 0, /* @tp_dictoffset@ */
1132 PyType_GenericAlloc
, /* @tp_alloc@ */
1133 mpmul_pynew
, /* @tp_new@ */
1138 /*----- Montgomery reduction ----------------------------------------------*/
1140 static PyTypeObject
*mpmont_pytype
;
1142 typedef struct mpmont_pyobj
{
1147 #define MPMONT_PY(o) (&((mpmont_pyobj *)(o))->mm)
1149 static PyObject
*mmmeth_int(PyObject
*me
, PyObject
*arg
)
1153 mpmont
*mm
= MPMONT_PY(me
);
1155 if (!PyArg_ParseTuple(arg
, "O&:in", convmp
, &yy
))
1157 mp_div(0, &yy
, yy
, mm
->m
);
1158 z
= mp_pywrap(mpmont_mul(mm
, MP_NEW
, yy
, mm
->r2
));
1160 if (yy
) MP_DROP(yy
);
1164 static PyObject
*mmmeth_mul(PyObject
*me
, PyObject
*arg
)
1167 mp
*yy
= 0, *zz
= 0;
1169 if (!PyArg_ParseTuple(arg
, "O&O&:mul", convmp
, &yy
, convmp
, &zz
))
1171 rc
= mp_pywrap(mpmont_mul(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1173 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1177 static PyObject
*mmmeth_exp(PyObject
*me
, PyObject
*arg
)
1180 mp
*yy
= 0, *zz
= 0;
1182 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1185 if ((yy
= mp_modinv_checked(yy
, yy
, MPMONT_PY(me
)->m
)) == 0) goto end
;
1186 zz
= mp_neg(zz
, zz
);
1188 rc
= mp_pywrap(mpmont_exp(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1190 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1194 static PyObject
*mmmeth_expr(PyObject
*me
, PyObject
*arg
)
1197 mp
*yy
= 0, *zz
= 0;
1199 if (!PyArg_ParseTuple(arg
, "O&O&:expr", convmp
, &yy
, convmp
, &zz
))
1202 yy
= mpmont_reduce(MPMONT_PY(me
), yy
, yy
);
1203 if ((yy
= mp_modinv_checked(yy
, yy
, MPMONT_PY(me
)->m
)) == 0) goto end
;
1204 yy
= mpmont_mul(MPMONT_PY(me
), yy
, yy
, MPMONT_PY(me
)->r2
);
1205 zz
= mp_neg(zz
, zz
);
1207 rc
= mp_pywrap(mpmont_expr(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1209 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1213 static PyObject
*mm_mexpr_id(PyObject
*me
)
1214 { return mp_pywrap(MP_COPY(MPMONT_PY(me
)->r
)); }
1216 static int mm_mexpr_fill(void *p
, PyObject
*me
, PyObject
*x
, PyObject
*y
)
1218 mp
*xx
= 0, *yy
= 0;
1219 mp_expfactor
*f
= p
;
1220 mpmont
*mm
= MPMONT_PY(me
);
1222 if ((xx
= getmp(x
)) == 0 || (yy
= getmp(y
)) == 0)
1225 xx
= mpmont_reduce(mm
, xx
, xx
);
1226 if ((xx
= mp_modinv_checked(xx
, xx
, yy
)) == 0)
1228 xx
= mpmont_mul(mm
, xx
, xx
, mm
->r2
);
1229 yy
= mp_neg(yy
, yy
);
1236 mp_drop(xx
); mp_drop(yy
);
1240 static PyObject
*mm_mexpr(PyObject
*me
, void *v
, int n
)
1241 { return mp_pywrap(mpmont_mexpr(MPMONT_PY(me
), MP_NEW
, v
, n
)); }
1243 static void mp_mexp_drop(void *p
)
1245 mp_expfactor
*f
= p
;
1250 static PyObject
*mmmeth_mexpr(PyObject
*me
, PyObject
*arg
)
1252 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1253 mm_mexpr_id
, mm_mexpr_fill
, mm_mexpr
, mp_mexp_drop
);
1256 static PyObject
*mp_mexp_id(PyObject
*me
)
1257 { return mp_pywrap(MP_ONE
); }
1259 static int mp_mexp_fill(void *p
, PyObject
*me
, PyObject
*x
, PyObject
*y
)
1261 mp
*xx
= 0, *yy
= 0;
1262 mp_expfactor
*f
= p
;
1264 if ((xx
= getmp(x
)) == 0 || (yy
= getmp(y
)) == 0)
1267 if ((xx
= mp_modinv_checked(xx
, xx
, yy
)) == 0)
1269 yy
= mp_neg(yy
, yy
);
1276 mp_drop(xx
); mp_drop(yy
);
1280 static PyObject
*mm_mexp(PyObject
*me
, void *v
, int n
)
1281 { return mp_pywrap(mpmont_mexp(MPMONT_PY(me
), MP_NEW
, v
, n
)); }
1283 static PyObject
*mmmeth_mexp(PyObject
*me
, PyObject
*arg
)
1285 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1286 mp_mexp_id
, mp_mexp_fill
, mm_mexp
, mp_mexp_drop
);
1289 #define mmmeth_ext mmmeth_reduce
1290 static PyObject
*mmmeth_reduce(PyObject
*me
, PyObject
*arg
)
1295 if (!PyArg_ParseTuple(arg
, "O&", convmp
, &yy
)) goto end
;
1296 z
= mp_pywrap(mpmont_reduce(MPMONT_PY(me
), MP_NEW
, yy
));
1301 static void mpmont_pydealloc(PyObject
*me
)
1303 mpmont_destroy(MPMONT_PY(me
));
1307 static PyObject
*mpmont_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1309 mpmont_pyobj
*mm
= 0;
1310 static const char *const kwlist
[] = { "m", 0 };
1313 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convmp
, &xx
))
1315 if (!MP_POSP(xx
) || !MP_ODDP(xx
)) VALERR("m must be positive and odd");
1316 mm
= (mpmont_pyobj
*)ty
->tp_alloc(ty
, 0);
1317 mpmont_create(&mm
->mm
, xx
);
1319 if (xx
) MP_DROP(xx
);
1320 return ((PyObject
*)mm
);
1323 static PyObject
*mmget_m(PyObject
*me
, void *hunoz
)
1324 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->m
))); }
1326 static PyObject
*mmget_r(PyObject
*me
, void *hunoz
)
1327 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->r
))); }
1329 static PyObject
*mmget_r2(PyObject
*me
, void *hunoz
)
1330 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->r2
))); }
1332 static const PyGetSetDef mpmont_pygetset
[] = {
1333 #define GETSETNAME(op, name) mm##op##_##name
1334 GET (m
, "M.m -> modulus for reduction")
1335 GET (r
, "M.r -> multiplicative identity")
1336 GET (r2
, "M.r2 -> M.r^2, Montgomerization factor")
1341 static const PyMethodDef mpmont_pymethods
[] = {
1342 #define METHNAME(name) mmmeth_##name
1343 METH (int, "M.int(X) -> XR")
1344 METH (mul
, "M.mul(XR, YR) -> ZR where Z = X Y")
1345 METH (expr
, "M.expr(XR, N) -> ZR where Z = X^N mod M.m")
1346 METH (mexpr
, "M.mexpr([(XR0, N0), (XR1, N1), ...]) = ZR "
1347 "where Z = X0^N0 X1^N1 ... mod M.m\n"
1348 "\t(the list may be flattened if this more convenient.)")
1349 METH (reduce
, "M.reduce(XR) -> X")
1350 METH (ext
, "M.ext(XR) -> X")
1351 METH (exp
, "M.exp(X, N) -> X^N mod M.m")
1352 METH (mexp
, "M.mexp([(X0, N0), (X1, N1), ...]) = "
1353 "X0^N0 X1^N1 ... mod M.m\n"
1354 "\t(the list may be flattened if this more convenient.)")
1359 static const PyTypeObject mpmont_pytype_skel
= {
1360 PyVarObject_HEAD_INIT(0, 0) /* Header */
1361 "MPMont", /* @tp_name@ */
1362 sizeof(mpmont_pyobj
), /* @tp_basicsize@ */
1363 0, /* @tp_itemsize@ */
1365 mpmont_pydealloc
, /* @tp_dealloc@ */
1367 0, /* @tp_getattr@ */
1368 0, /* @tp_setattr@ */
1369 0, /* @tp_compare@ */
1371 0, /* @tp_as_number@ */
1372 0, /* @tp_as_sequence@ */
1373 0, /* @tp_as_mapping@ */
1377 0, /* @tp_getattro@ */
1378 0, /* @tp_setattro@ */
1379 0, /* @tp_as_buffer@ */
1380 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1381 Py_TPFLAGS_BASETYPE
,
1384 "MPMont(N): a Montgomery reduction context.",
1386 0, /* @tp_traverse@ */
1388 0, /* @tp_richcompare@ */
1389 0, /* @tp_weaklistoffset@ */
1391 0, /* @tp_iternext@ */
1392 PYMETHODS(mpmont
), /* @tp_methods@ */
1393 0, /* @tp_members@ */
1394 PYGETSET(mpmont
), /* @tp_getset@ */
1397 0, /* @tp_descr_get@ */
1398 0, /* @tp_descr_set@ */
1399 0, /* @tp_dictoffset@ */
1401 PyType_GenericAlloc
, /* @tp_alloc@ */
1402 mpmont_pynew
, /* @tp_new@ */
1407 /*----- Barrett reduction -------------------------------------------------*/
1409 static PyTypeObject
*mpbarrett_pytype
;
1411 typedef struct mpbarrett_pyobj
{
1416 #define MPBARRETT_PY(o) (&((mpbarrett_pyobj *)(o))->mb)
1418 static PyObject
*mbmeth_exp(PyObject
*me
, PyObject
*arg
)
1421 mp
*yy
= 0, *zz
= 0;
1423 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1426 if ((yy
= mp_modinv_checked(yy
, yy
, MPBARRETT_PY(me
)->m
)) == 0) goto end
;
1427 zz
= mp_neg(zz
, zz
);
1429 rc
= mp_pywrap(mpbarrett_exp(MPBARRETT_PY(me
), MP_NEW
, yy
, zz
));
1431 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1435 static PyObject
*mb_mexp(PyObject
*me
, void *v
, int n
)
1436 { return mp_pywrap(mpbarrett_mexp(MPBARRETT_PY(me
), MP_NEW
, v
, n
)); }
1438 static PyObject
*mbmeth_mexp(PyObject
*me
, PyObject
*arg
)
1440 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1441 mp_mexp_id
, mp_mexp_fill
, mb_mexp
, mp_mexp_drop
);
1444 static PyObject
*mbmeth_reduce(PyObject
*me
, PyObject
*arg
)
1449 if (!PyArg_ParseTuple(arg
, "O&:reduce", convmp
, &yy
))
1451 z
= mp_pywrap(mpbarrett_reduce(MPBARRETT_PY(me
), MP_NEW
, yy
));
1456 static void mpbarrett_pydealloc(PyObject
*me
)
1458 mpbarrett_destroy(MPBARRETT_PY(me
));
1462 static PyObject
*mpbarrett_pynew(PyTypeObject
*ty
,
1463 PyObject
*arg
, PyObject
*kw
)
1465 mpbarrett_pyobj
*mb
= 0;
1466 static const char *const kwlist
[] = { "m", 0 };
1469 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convmp
, &xx
))
1471 if (!MP_POSP(xx
)) VALERR("m must be positive");
1472 mb
= (mpbarrett_pyobj
*)ty
->tp_alloc(ty
, 0);
1473 mpbarrett_create(&mb
->mb
, xx
);
1475 if (xx
) MP_DROP(xx
);
1476 return ((PyObject
*)mb
);
1479 static PyObject
*mbget_m(PyObject
*me
, void *hunoz
)
1480 { return (mp_pywrap(MP_COPY(MPBARRETT_PY(me
)->m
))); }
1482 static const PyGetSetDef mpbarrett_pygetset
[] = {
1483 #define GETSETNAME(op, name) mb##op##_##name
1484 GET (m
, "B.m -> modulus for reduction")
1489 static const PyMethodDef mpbarrett_pymethods
[] = {
1490 #define METHNAME(name) mbmeth_##name
1491 METH (reduce
, "B.reduce(X) -> X mod B.m")
1492 METH (exp
, "B.exp(X, N) -> X^N mod B.m")
1493 METH (mexp
, "B.mexp([(X0, N0), (X1, N1), ...]) = "
1494 "X0^N0 X1^N1 ... mod B.m\n"
1495 "\t(the list may be flattened if this more convenient.)")
1500 static const PyTypeObject mpbarrett_pytype_skel
= {
1501 PyVarObject_HEAD_INIT(0, 0) /* Header */
1502 "MPBarrett", /* @tp_name@ */
1503 sizeof(mpbarrett_pyobj
), /* @tp_basicsize@ */
1504 0, /* @tp_itemsize@ */
1506 mpbarrett_pydealloc
, /* @tp_dealloc@ */
1508 0, /* @tp_getattr@ */
1509 0, /* @tp_setattr@ */
1510 0, /* @tp_compare@ */
1512 0, /* @tp_as_number@ */
1513 0, /* @tp_as_sequence@ */
1514 0, /* @tp_as_mapping@ */
1518 0, /* @tp_getattro@ */
1519 0, /* @tp_setattro@ */
1520 0, /* @tp_as_buffer@ */
1521 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1522 Py_TPFLAGS_BASETYPE
,
1525 "MPBarrett(N): a Barrett reduction context.",
1527 0, /* @tp_traverse@ */
1529 0, /* @tp_richcompare@ */
1530 0, /* @tp_weaklistoffset@ */
1532 0, /* @tp_iternext@ */
1533 PYMETHODS(mpbarrett
), /* @tp_methods@ */
1534 0, /* @tp_members@ */
1535 PYGETSET(mpbarrett
), /* @tp_getset@ */
1538 0, /* @tp_descr_get@ */
1539 0, /* @tp_descr_set@ */
1540 0, /* @tp_dictoffset@ */
1542 PyType_GenericAlloc
, /* @tp_alloc@ */
1543 mpbarrett_pynew
, /* @tp_new@ */
1548 /*----- Nice prime reduction ----------------------------------------------*/
1550 static PyTypeObject
*mpreduce_pytype
;
1552 typedef struct mpreduce_pyobj
{
1557 #define MPREDUCE_PY(o) (&((mpreduce_pyobj *)(o))->mr)
1559 static PyObject
*mrmeth_exp(PyObject
*me
, PyObject
*arg
)
1562 mp
*yy
= 0, *zz
= 0;
1564 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1567 if ((yy
= mp_modinv_checked(yy
, yy
, MPREDUCE_PY(me
)->p
)) == 0) goto end
;
1568 zz
= mp_neg(zz
, zz
);
1570 rc
= mp_pywrap(mpreduce_exp(MPREDUCE_PY(me
), MP_NEW
, yy
, zz
));
1572 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1576 static PyObject
*mrmeth_reduce(PyObject
*me
, PyObject
*arg
)
1581 if (!PyArg_ParseTuple(arg
, "O&:reduce", convmp
, &yy
)) goto end
;
1582 z
= mp_pywrap(mpreduce_do(MPREDUCE_PY(me
), MP_NEW
, yy
));
1587 static void mpreduce_pydealloc(PyObject
*me
)
1589 mpreduce_destroy(MPREDUCE_PY(me
));
1593 static PyObject
*mpreduce_pynew(PyTypeObject
*ty
,
1594 PyObject
*arg
, PyObject
*kw
)
1596 mpreduce_pyobj
*mr
= 0;
1598 static const char *const kwlist
[] = { "m", 0 };
1601 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convmp
, &xx
))
1603 if (!MP_POSP(xx
)) VALERR("m must be positive");
1604 if (mpreduce_create(&r
, xx
)) VALERR("bad modulus (must be 2^k - ...)");
1605 mr
= (mpreduce_pyobj
*)ty
->tp_alloc(ty
, 0);
1608 if (xx
) MP_DROP(xx
);
1609 return ((PyObject
*)mr
);
1612 static PyObject
*mrget_m(PyObject
*me
, void *hunoz
)
1613 { return (mp_pywrap(MP_COPY(MPREDUCE_PY(me
)->p
))); }
1615 static const PyGetSetDef mpreduce_pygetset
[] = {
1616 #define GETSETNAME(op, name) mr##op##_##name
1617 GET (m
, "R.m -> modulus for reduction")
1622 static const const PyMethodDef mpreduce_pymethods
[] = {
1623 #define METHNAME(name) mrmeth_##name
1624 METH (reduce
, "R.reduce(X) -> X mod B.m")
1625 METH (exp
, "R.exp(X, N) -> X^N mod B.m")
1630 static const PyTypeObject mpreduce_pytype_skel
= {
1631 PyVarObject_HEAD_INIT(0, 0) /* Header */
1632 "MPReduce", /* @tp_name@ */
1633 sizeof(mpreduce_pyobj
), /* @tp_basicsize@ */
1634 0, /* @tp_itemsize@ */
1636 mpreduce_pydealloc
, /* @tp_dealloc@ */
1638 0, /* @tp_getattr@ */
1639 0, /* @tp_setattr@ */
1640 0, /* @tp_compare@ */
1642 0, /* @tp_as_number@ */
1643 0, /* @tp_as_sequence@ */
1644 0, /* @tp_as_mapping@ */
1648 0, /* @tp_getattro@ */
1649 0, /* @tp_setattro@ */
1650 0, /* @tp_as_buffer@ */
1651 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1652 Py_TPFLAGS_BASETYPE
,
1655 "MPReduce(N): a reduction context for reduction modulo Solinas primes.",
1657 0, /* @tp_traverse@ */
1659 0, /* @tp_richcompare@ */
1660 0, /* @tp_weaklistoffset@ */
1662 0, /* @tp_iternext@ */
1663 PYMETHODS(mpreduce
), /* @tp_methods@ */
1664 0, /* @tp_members@ */
1665 PYGETSET(mpreduce
), /* @tp_getset@ */
1668 0, /* @tp_descr_get@ */
1669 0, /* @tp_descr_set@ */
1670 0, /* @tp_dictoffset@ */
1672 PyType_GenericAlloc
, /* @tp_alloc@ */
1673 mpreduce_pynew
, /* @tp_new@ */
1678 /*----- Chinese Remainder Theorem solution --------------------------------*/
1680 static PyTypeObject
*mpcrt_pytype
;
1682 typedef struct mpcrt_pyobj
{
1687 #define MPCRT_PY(o) (&((mpcrt_pyobj *)(o))->c)
1689 static PyObject
*mcmeth_solve(PyObject
*me
, PyObject
*arg
)
1691 mpcrt
*c
= MPCRT_PY(me
);
1692 PyObject
*q
= 0, *x
, *z
= 0;
1695 Py_ssize_t i
= 0, n
= c
->k
;
1698 if (PyTuple_GET_SIZE(arg
) == n
)
1700 else if (!PyArg_ParseTuple(arg
, "O:solve", &q
))
1703 if (!PySequence_Check(q
)) TYERR("want a sequence of residues");
1704 i
= PySequence_Size(q
); if (i
< 0) goto end
;
1705 if (i
!= n
) VALERR("residue count mismatch");
1706 v
= xmalloc(n
* sizeof(*v
));
1707 for (i
= 0; i
< n
; i
++) {
1708 if ((x
= PySequence_GetItem(q
, i
)) == 0) goto end
;
1709 xx
= getmp(x
); Py_DECREF(x
); if (!xx
) goto end
;
1712 z
= mp_pywrap(mpcrt_solve(c
, MP_NEW
, v
));
1716 for (i
= 0; i
< n
; i
++)
1725 static void mpcrt_pydealloc(PyObject
*me
)
1727 mpcrt
*c
= MPCRT_PY(me
);
1732 static PyObject
*mpcrt_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1735 Py_ssize_t n
, i
= 0, j
;
1736 static const char *const kwlist
[] = { "mv", 0 };
1737 PyObject
*q
= 0, *x
;
1738 mp
*xx
= MP_NEW
, *y
= MP_NEW
, *g
= MP_NEW
;
1742 if (PyTuple_GET_SIZE(arg
) > 1)
1744 else if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", KWLIST
, &q
))
1747 if (!PySequence_Check(q
)) TYERR("want a sequence of moduli");
1748 n
= PySequence_Size(q
); if (n
< 0) goto end
;
1749 if (!n
) VALERR("want at least one modulus");
1750 v
= xmalloc(n
* sizeof(*v
));
1751 for (i
= 0; i
< n
; i
++) {
1752 if ((x
= PySequence_GetItem(q
, i
)) == 0) goto end
;
1753 xx
= getmp(x
); Py_DECREF(x
); if (!xx
) goto end
;
1754 if (MP_CMP(xx
, <=, MP_ZERO
)) VALERR("moduli must be positive");
1755 v
[i
].m
= xx
; v
[i
].n
= 0; v
[i
].ni
= 0; v
[i
].nni
= 0; xx
= MP_NEW
;
1758 for (j
= 0; j
< i
; j
++) mpmul_add(&mm
, v
[j
].m
);
1759 xx
= mpmul_done(&mm
);
1760 for (j
= 0; j
< i
; j
++) {
1761 mp_div(&y
, 0, xx
, v
[j
].m
);
1762 mp_gcd(&g
, 0, 0, y
, v
[j
].m
);
1763 if (!MP_EQ(g
, MP_ONE
)) VALERR("moduli must be pairwise coprime");
1766 c
= (mpcrt_pyobj
*)ty
->tp_alloc(ty
, 0);
1767 mpcrt_create(&c
->c
, v
, n
, 0);
1769 mp_drop(xx
); mp_drop(y
); mp_drop(g
);
1770 return ((PyObject
*)c
);
1775 for (i
= 0; i
< n
; i
++)
1780 mp_drop(xx
); mp_drop(y
); mp_drop(g
);
1784 static PyObject
*mcget_product(PyObject
*me
, void *hunoz
)
1785 { return (mp_pywrap(MP_COPY(MPCRT_PY(me
)->mb
.m
))); }
1787 static PyObject
*mcget_moduli(PyObject
*me
, void *hunoz
)
1791 mpcrt
*c
= MPCRT_PY(me
);
1793 if ((q
= PyList_New(c
->k
)) == 0) return (0);
1794 for (i
= 0; i
< c
->k
; i
++)
1795 PyList_SET_ITEM(q
, i
, mp_pywrap(c
->v
[i
].m
));
1799 static const PyGetSetDef mpcrt_pygetset
[] = {
1800 #define GETSETNAME(op, name) mc##op##_##name
1801 GET (product
, "C.product -> product of moduli")
1802 GET (moduli
, "C.moduli -> list of individual moduli")
1807 static const PyMethodDef mpcrt_pymethods
[] = {
1808 #define METHNAME(name) mcmeth_##name
1809 METH (solve
, "C.solve([R0, R1]) -> X mod C.product")
1814 static const PyTypeObject mpcrt_pytype_skel
= {
1815 PyVarObject_HEAD_INIT(0, 0) /* Header */
1816 "MPCRT", /* @tp_name@ */
1817 sizeof(mpcrt_pyobj
), /* @tp_basicsize@ */
1818 0, /* @tp_itemsize@ */
1820 mpcrt_pydealloc
, /* @tp_dealloc@ */
1822 0, /* @tp_getattr@ */
1823 0, /* @tp_setattr@ */
1824 0, /* @tp_compare@ */
1826 0, /* @tp_as_number@ */
1827 0, /* @tp_as_sequence@ */
1828 0, /* @tp_as_mapping@ */
1832 0, /* @tp_getattro@ */
1833 0, /* @tp_setattro@ */
1834 0, /* @tp_as_buffer@ */
1835 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1836 Py_TPFLAGS_BASETYPE
,
1839 "MPCRT(SEQ): a context for solving Chinese Remainder Theorem problems.",
1841 0, /* @tp_traverse@ */
1843 0, /* @tp_richcompare@ */
1844 0, /* @tp_weaklistoffset@ */
1846 0, /* @tp_iternext@ */
1847 PYMETHODS(mpcrt
), /* @tp_methods@ */
1848 0, /* @tp_members@ */
1849 PYGETSET(mpcrt
), /* @tp_getset@ */
1852 0, /* @tp_descr_get@ */
1853 0, /* @tp_descr_set@ */
1854 0, /* @tp_dictoffset@ */
1856 PyType_GenericAlloc
, /* @tp_alloc@ */
1857 mpcrt_pynew
, /* @tp_new@ */
1862 /*----- Binary polynomials ------------------------------------------------*/
1864 static PyObject
*gf_pyrepr(PyObject
*o
)
1865 { return mp_topystring(MP_X(o
), 16, "GF(", "0x", "L)"); }
1867 static PyObject
*gf_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
1873 if (gfbinop(x
, y
, &xx
, &yy
)) RETURN_NOTIMPL
;
1875 case Py_EQ
: b
= MP_EQ(xx
, yy
); break;
1876 case Py_NE
: b
= !MP_EQ(xx
, yy
); break;
1881 case Py_LT
: b
= xl
< yl
; break;
1882 case Py_LE
: b
= xl
<= yl
; break;
1883 case Py_GT
: b
= xl
> yl
; break;
1884 case Py_GE
: b
= xl
>= yl
; break;
1889 MP_DROP(xx
); MP_DROP(yy
);
1890 return (getbool(b
));
1893 static PyObject
*gf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1899 static const char *const kwlist
[] = { "x", "radix", 0 };
1901 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|i:gf", KWLIST
, &x
, &radix
))
1903 if (GF_PYCHECK(x
)) RETURN_OBJ(x
);
1904 if (!good_radix_p(radix
, 1)) VALERR("radix out of range");
1905 if ((z
= mp_frompyobject(x
, radix
)) == 0) {
1906 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to gf",
1907 Py_TYPE(x
)->tp_name
);
1912 VALERR("gf cannot be negative");
1914 zz
= (mp_pyobj
*)ty
->tp_alloc(ty
, 0);
1917 return ((PyObject
*)zz
);
1920 static PyObject
*gf_pyexp(PyObject
*x
, PyObject
*y
, PyObject
*z
)
1922 mp
*xx
= 0, *yy
= 0, *zz
= 0;
1926 if ((xx
= tomp(x
)) == 0 || (yy
= tomp(y
)) == 0 ||
1927 (z
&& z
!= Py_None
&& (zz
= tomp(z
)) == 0)) {
1928 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
1931 if (!z
|| z
== Py_None
) {
1932 if (MP_NEGP(yy
)) VALERR("negative exponent");
1933 r
= gf_exp(MP_NEW
, xx
, yy
);
1936 if (MP_ZEROP(zz
)) ZDIVERR("zero modulus");
1938 if ((xx
= gf_modinv_checked(xx
, xx
, zz
)) == 0) goto end
;
1939 yy
= mp_neg(yy
, yy
);
1941 gfreduce_create(&gr
, zz
);
1942 r
= gfreduce_exp(&gr
, MP_NEW
, xx
, yy
);
1943 gfreduce_destroy(&gr
);
1947 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
1951 static PyObject
*gfmeth_sqr(PyObject
*me
)
1952 { return (gf_pywrap(gf_sqr(MP_NEW
, MP_X(me
)))); }
1954 static PyObject
*gfmeth_gcd(PyObject
*me
, PyObject
*arg
)
1957 mp
*yy
= 0, *zz
= MP_NEW
;
1959 if (!PyArg_ParseTuple(arg
, "O&:gcd", convgf
, &yy
)) goto end
;
1960 gf_gcd(&zz
, 0, 0, MP_X(me
), yy
);
1963 if (yy
) MP_DROP(yy
);
1967 static PyObject
*gfmeth_gcdx(PyObject
*me
, PyObject
*arg
)
1970 mp
*yy
= 0, *zz
= MP_NEW
, *uu
= MP_NEW
, *vv
= MP_NEW
;
1972 if (!PyArg_ParseTuple(arg
, "O&:gcdx", convgf
, &yy
))
1974 gf_gcd(&zz
, &uu
, &vv
, MP_X(me
), yy
);
1975 z
= Py_BuildValue("(NNN)",
1976 gf_pywrap(zz
), gf_pywrap(uu
), gf_pywrap(vv
));
1978 if (yy
) MP_DROP(yy
);
1982 static PyObject
*gfmeth_modinv(PyObject
*me
, PyObject
*arg
)
1985 mp
*yy
= 0, *zz
= MP_NEW
;
1987 if (!PyArg_ParseTuple(arg
, "O&:modinv", convgf
, &yy
) ||
1988 (zz
= gf_modinv_checked(MP_NEW
, yy
, MP_X(me
))) == 0)
1992 if (yy
) MP_DROP(yy
);
1996 static PyObject
*gfmeth_fromstring(PyObject
*me
,
1997 PyObject
*arg
, PyObject
*kw
)
2004 mptext_stringctx sc
;
2005 static const char *const kwlist
[] = { "x", "radix", 0 };
2007 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|i:fromstring", KWLIST
,
2010 if (!good_radix_p(r
, 1)) VALERR("bad radix");
2011 sc
.buf
= p
; sc
.lim
= p
+ len
;
2012 if ((zz
= mp_read(MP_NEW
, r
, &mptext_stringops
, &sc
)) == 0 ||
2014 if (zz
) MP_DROP(zz
);
2015 VALERR("bad binary polynomial");
2017 z
= Py_BuildValue("(Ns#)", gf_pywrap(zz
),
2018 sc
.buf
, (Py_ssize_t
)(sc
.lim
- sc
.buf
));
2023 static PyObject
*gfmeth_irreduciblep(PyObject
*me
)
2024 { return getbool(gf_irreduciblep(MP_X(me
))); }
2026 static PyObject
*gfget_degree(PyObject
*me
, void *hunoz
)
2027 { return (PyInt_FromLong(mp_bits(MP_X(me
)) - 1)); }
2029 static const PyGetSetDef gf_pygetset
[] = {
2030 #define GETSETNAME(op, name) gf##op##_##name
2031 GET (degree
, "X.degree -> polynomial degree of X")
2033 #define GETSETNAME(op, name) mp##op##_##name
2034 GET (nbits
, "X.nbits -> bit length of X")
2035 GET (noctets
, "X.noctets -> octet length of X")
2040 static const PyMethodDef gf_pymethods
[] = {
2041 #define METHNAME(func) gfmeth_##func
2042 METH (setbit
, "X.setbit(N) -> X with bit N set")
2043 METH (clearbit
, "X.clearbit(N) -> X with bit N clear")
2044 METH (testbit
, "X.testbit(N) -> true/false if bit N set/clear in X")
2045 NAMETH(sqr
, "X.sqr() -> X^2")
2046 METH (gcd
, "X.gcd(Y) -> gcd(X, Y)")
2047 METH (gcdx
, "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)")
2048 METH (modinv
, "X.modinv(Y) -> multiplicative inverse of Y mod X")
2049 NAMETH(irreduciblep
, "X.irreduciblep() -> true/false")
2050 KWSMTH(fromstring
, "fromstring(STR, [radix = 0]) -> (X, REST)\n"
2051 " Parse STR as a binary polynomial, according to RADIX. If RADIX is\n"
2052 " zero, read a prefix from STR to decide radix: allow `0b' for binary,\n"
2053 " `0' or `0o' for octal, `0x' for hex, or `R_' for other radix R.")
2054 SMTH (loadl
, "loadl(STR) -> X: read little-endian bytes")
2055 SMTH (loadb
, "loadb(STR) -> X: read big-endian bytes")
2056 SMTH (frombuf
, "frombuf(STR) -> (X, REST): read buffer format")
2058 #define METHNAME(func) mpmeth_##func
2059 KWMETH(tostring
, "X.tostring([radix = 10]) -> STR")
2060 KWMETH(storel
, "X.storel([len = -1]) -> little-endian bytes")
2061 KWMETH(storeb
, "X.storeb([len = -1]) -> big-endian bytes")
2062 KWMETH(storel2c
, "X.storel2c([len = -1]) -> "
2063 "little-endian bytes, two's complement")
2064 KWMETH(storeb2c
, "X.storeb2c([len = -1]) -> "
2065 "big-endian bytes, two's complement")
2066 NAMETH(tobuf
, "X.tobuf() -> buffer format")
2071 static const PyNumberMethods gf_pynumber
= {
2072 gf_pyadd
, /* @nb_add@ */
2073 gf_pysub
, /* @nb_subtract@ */
2074 gf_pymul
, /* @nb_multiply@ */
2075 0, /* @nb_divide@ */
2076 gf_pymod
, /* @nb_remainder@ */
2077 gf_pydivmod
, /* @nb_divmod@ */
2078 gf_pyexp
, /* @nb_power@ */
2079 mp_pyid
, /* @nb_negative@ */
2080 mp_pyid
, /* @nb_positive@ */
2081 mp_pyid
, /* @nb_absolute@ */
2082 mp_pynonzerop
, /* @nb_nonzero@ */
2083 0 /* doesn't make any sense */, /* @nb_invert@ */
2084 gf_pylsl
, /* @nb_lshift@ */
2085 gf_pylsr
, /* @nb_rshift@ */
2086 gf_pyand
, /* @nb_and@ */
2087 gf_pyxor
, /* @nb_xor@ */
2088 gf_pyor
, /* @nb_or@ */
2089 gf_pycoerce
, /* @nb_coerce@ */
2090 mp_pyint
, /* @nb_int@ */
2091 mp_pylong
, /* @nb_long@ */
2092 0 /* doesn't make any sense */, /* @nb_float@ */
2093 mp_pyoct
, /* @nb_oct@ */
2094 mp_pyhex
, /* @nb_hex@ */
2096 0, /* @nb_inplace_add@ */
2097 0, /* @nb_inplace_subtract@ */
2098 0, /* @nb_inplace_multiply@ */
2099 0, /* @nb_inplace_divide@ */
2100 0, /* @nb_inplace_remainder@ */
2101 0, /* @nb_inplace_power@ */
2102 0, /* @nb_inplace_lshift@ */
2103 0, /* @nb_inplace_rshift@ */
2104 0, /* @nb_inplace_and@ */
2105 0, /* @nb_inplace_xor@ */
2106 0, /* @nb_inplace_or@ */
2108 gf_pydiv
, /* @nb_floor_divide@ */
2109 0, /* @nb_true_divide@ */
2110 0, /* @nb_inplace_floor_divide@ */
2111 0, /* @nb_inplace_true_divide@ */
2114 static const PyTypeObject gf_pytype_skel
= {
2115 PyVarObject_HEAD_INIT(0, 0) /* Header */
2116 "GF", /* @tp_name@ */
2117 sizeof(mp_pyobj
), /* @tp_basicsize@ */
2118 0, /* @tp_itemsize@ */
2120 mp_pydealloc
, /* @tp_dealloc@ */
2122 0, /* @tp_getattr@ */
2123 0, /* @tp_setattr@ */
2124 0, /* @tp_compare@ */
2125 gf_pyrepr
, /* @tp_repr@ */
2126 PYNUMBER(gf
), /* @tp_as_number@ */
2127 0, /* @tp_as_sequence@ */
2128 0, /* @tp_as_mapping@ */
2129 mp_pyhash
, /* @tp_hash@ */
2131 mp_pyhex
, /* @tp_str@ */
2132 0, /* @tp_getattro@ */
2133 0, /* @tp_setattro@ */
2134 0, /* @tp_as_buffer@ */
2135 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2136 Py_TPFLAGS_CHECKTYPES
|
2137 Py_TPFLAGS_BASETYPE
,
2140 "Binary polynomials. Support almost all the standard arithmetic\n"
2143 "Constructor GF(X, [radix = R]) attempts to convert X to a `GF'. If\n"
2144 "X is a string, it's read in radix-R form, or we look for a prefix\n"
2145 "if R = 0. Other acceptable things are field elements, elliptic curve\n"
2146 "points, group elements, Python `int' and `long' objects, and anything\n"
2147 "with an integer conversion.\n"
2149 "The name is hopelessly wrong from a technical point of view, but\n"
2150 "but it's much easier to type than `p2' or `c2' or whatever.\n"
2154 " * Use `//' for Euclidean division: `/' gives exact rational division.",
2156 0, /* @tp_traverse@ */
2158 gf_pyrichcompare
, /* @tp_richcompare@ */
2159 0, /* @tp_weaklistoffset@ */
2161 0, /* @tp_iternext@ */
2162 PYMETHODS(gf
), /* @tp_methods@ */
2163 0, /* @tp_members@ */
2164 PYGETSET(gf
), /* @tp_getset@ */
2167 0, /* @tp_descr_get@ */
2168 0, /* @tp_descr_set@ */
2169 0, /* @tp_dictoffset@ */
2171 PyType_GenericAlloc
, /* @tp_alloc@ */
2172 gf_pynew
, /* @tp_new@ */
2177 /*----- Sparse poly reduction ---------------------------------------------*/
2179 static PyTypeObject
*gfreduce_pytype
;
2181 typedef struct gfreduce_pyobj
{
2186 #define GFREDUCE_PY(o) (&((gfreduce_pyobj *)(o))->mr)
2188 static PyObject
*grmeth_exp(PyObject
*me
, PyObject
*arg
)
2191 mp
*yy
= 0, *zz
= 0;
2193 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convgf
, &yy
, convgf
, &zz
))
2196 if ((yy
= gf_modinv_checked(yy
, yy
, GFREDUCE_PY(me
)->p
)) == 0) goto end
;
2197 zz
= mp_neg(zz
, zz
);
2199 rc
= gf_pywrap(gfreduce_exp(GFREDUCE_PY(me
), MP_NEW
, yy
, zz
));
2201 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
2205 static PyObject
*grmeth_trace(PyObject
*me
, PyObject
*arg
)
2210 if (!PyArg_ParseTuple(arg
, "O&:trace", convgf
, &xx
)) goto end
;
2211 rc
= PyInt_FromLong(gfreduce_trace(GFREDUCE_PY(me
), xx
));
2213 if (xx
) MP_DROP(xx
);
2217 static PyObject
*grmeth_halftrace(PyObject
*me
, PyObject
*arg
)
2222 if (!PyArg_ParseTuple(arg
, "O&:halftrace", convgf
, &xx
)) goto end
;
2223 rc
= gf_pywrap(gfreduce_halftrace(GFREDUCE_PY(me
), MP_NEW
, xx
));
2225 if (xx
) MP_DROP(xx
);
2229 static PyObject
*grmeth_sqrt(PyObject
*me
, PyObject
*arg
)
2234 if (!PyArg_ParseTuple(arg
, "O&:sqrt", convgf
, &xx
)) goto end
;
2235 if ((yy
= gfreduce_sqrt(GFREDUCE_PY(me
), MP_NEW
, xx
)) == 0)
2236 VALERR("no modular square root");
2239 if (xx
) MP_DROP(xx
);
2243 static PyObject
*grmeth_quadsolve(PyObject
*me
, PyObject
*arg
)
2248 if (!PyArg_ParseTuple(arg
, "O&:quadsolve", convgf
, &xx
)) goto end
;
2249 if ((yy
= gfreduce_quadsolve(GFREDUCE_PY(me
), MP_NEW
, xx
)) == 0)
2250 VALERR("no solution found");
2253 if (xx
) MP_DROP(xx
);
2257 static PyObject
*grmeth_reduce(PyObject
*me
, PyObject
*arg
)
2262 if (!PyArg_ParseTuple(arg
, "O&:reduce", convgf
, &yy
)) goto end
;
2263 z
= gf_pywrap(gfreduce_do(GFREDUCE_PY(me
), MP_NEW
, yy
));
2268 static void gfreduce_pydealloc(PyObject
*me
)
2270 gfreduce_destroy(GFREDUCE_PY(me
));
2274 static PyObject
*gfreduce_pynew(PyTypeObject
*ty
,
2275 PyObject
*arg
, PyObject
*kw
)
2277 gfreduce_pyobj
*mr
= 0;
2279 static const char *const kwlist
[] = { "m", 0 };
2282 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convgf
, &xx
))
2284 if (MP_ZEROP(xx
)) ZDIVERR("modulus is zero!");
2285 gfreduce_create(&r
, xx
);
2286 mr
= (gfreduce_pyobj
*)ty
->tp_alloc(ty
, 0);
2289 if (xx
) MP_DROP(xx
);
2290 return ((PyObject
*)mr
);
2293 static PyObject
*grget_m(PyObject
*me
, void *hunoz
)
2294 { return (gf_pywrap(MP_COPY(GFREDUCE_PY(me
)->p
))); }
2296 static const PyGetSetDef gfreduce_pygetset
[] = {
2297 #define GETSETNAME(op, name) gr##op##_##name
2298 GET (m
, "R.m -> reduction polynomial")
2303 static const PyMethodDef gfreduce_pymethods
[] = {
2304 #define METHNAME(name) grmeth_##name
2305 METH (reduce
, "R.reduce(X) -> X mod B.m")
2306 METH (trace
, "R.trace(X) -> Tr(X) = x + x^2 + ... + x^{2^{m - 1}}")
2307 METH (halftrace
, "R.halftrace(X) -> x + x^{2^2} + ... + x^{2^{m - 1}}")
2308 METH (sqrt
, "R.sqrt(X) -> Y where Y^2 = X mod R")
2309 METH (quadsolve
, "R.quadsolve(X) -> Y where Y^2 + Y = X mod R")
2310 METH (exp
, "R.exp(X, N) -> X^N mod B.m")
2315 static const PyTypeObject gfreduce_pytype_skel
= {
2316 PyVarObject_HEAD_INIT(0, 0) /* Header */
2317 "GFReduce", /* @tp_name@ */
2318 sizeof(gfreduce_pyobj
), /* @tp_basicsize@ */
2319 0, /* @tp_itemsize@ */
2321 gfreduce_pydealloc
, /* @tp_dealloc@ */
2323 0, /* @tp_getattr@ */
2324 0, /* @tp_setattr@ */
2325 0, /* @tp_compare@ */
2327 0, /* @tp_as_number@ */
2328 0, /* @tp_as_sequence@ */
2329 0, /* @tp_as_mapping@ */
2333 0, /* @tp_getattro@ */
2334 0, /* @tp_setattro@ */
2335 0, /* @tp_as_buffer@ */
2336 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2337 Py_TPFLAGS_BASETYPE
,
2340 "GFReduce(N): a context for reduction modulo sparse polynomials.",
2342 0, /* @tp_traverse@ */
2344 0, /* @tp_richcompare@ */
2345 0, /* @tp_weaklistoffset@ */
2347 0, /* @tp_iternext@ */
2348 PYMETHODS(gfreduce
), /* @tp_methods@ */
2349 0, /* @tp_members@ */
2350 PYGETSET(gfreduce
), /* @tp_getset@ */
2353 0, /* @tp_descr_get@ */
2354 0, /* @tp_descr_set@ */
2355 0, /* @tp_dictoffset@ */
2357 PyType_GenericAlloc
, /* @tp_alloc@ */
2358 gfreduce_pynew
, /* @tp_new@ */
2363 /*----- Normal/poly transformation ----------------------------------------*/
2365 static PyTypeObject
*gfn_pytype
;
2367 typedef struct gfn_pyobj
{
2373 #define GFN_P(o) (((gfn_pyobj *)(o))->p)
2374 #define GFN_PTON(o) (&((gfn_pyobj *)(o))->pton)
2375 #define GFN_NTOP(o) (&((gfn_pyobj *)(o))->ntop)
2377 static PyObject
*gfn_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2379 mp
*p
= 0, *beta
= 0;
2381 static const char *const kwlist
[] = { "p", "beta", 0 };
2383 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:new", KWLIST
,
2384 convgf
, &p
, convgf
, &beta
))
2386 gg
= PyObject_New(gfn_pyobj
, ty
);
2388 if (gfn_create(p
, beta
, &gg
->ntop
, &gg
->pton
)) {
2391 VALERR("can't invert transformation matrix");
2397 return ((PyObject
*)gg
);
2400 static PyObject
*gfnget_p(PyObject
*me
, void *hunoz
)
2401 { return (gf_pywrap(MP_COPY(GFN_P(me
)))); }
2403 static PyObject
*gfnget_beta(PyObject
*me
, void *hunoz
)
2405 gfn
*n
= GFN_NTOP(me
);
2406 mp
*x
= n
->r
[n
->n
- 1];
2407 return (gf_pywrap(MP_COPY(x
)));
2410 #define XFORMOP(name, NAME) \
2411 static PyObject *gfnmeth_##name(PyObject *me, PyObject *arg) \
2416 if (!PyArg_ParseTuple(arg, "O&:" #name, convgf, &xx)) goto end; \
2417 z = gfn_transform(GFN_##NAME(me), MP_NEW, xx); \
2420 if (!z) return (0); \
2421 return (gf_pywrap(z)); \
2427 static void gfn_pydealloc(PyObject
*me
)
2431 gfn_destroy(GFN_PTON(me
));
2432 gfn_destroy(GFN_NTOP(me
));
2437 static const PyGetSetDef gfn_pygetset
[] = {
2438 #define GETSETNAME(op, name) gfn##op##_##name
2439 GET (p
, "X.p -> polynomial basis, as polynomial")
2440 GET (beta
, "X.beta -> normal basis element, in poly form")
2445 static const PyMethodDef gfn_pymethods
[] = {
2446 #define METHNAME(name) gfnmeth_##name
2447 METH (pton
, "X.pton(A) -> normal-basis representation of A")
2448 METH (ntop
, "X.ntop(A) -> polynomial-basis representation of A")
2453 static const PyTypeObject gfn_pytype_skel
= {
2454 PyVarObject_HEAD_INIT(0, 0) /* Header */
2455 "GFN", /* @tp_name@ */
2456 sizeof(gfn_pyobj
), /* @tp_basicsize@ */
2457 0, /* @tp_itemsize@ */
2459 gfn_pydealloc
, /* @tp_dealloc@ */
2461 0, /* @tp_getattr@ */
2462 0, /* @tp_setattr@ */
2463 0, /* @tp_compare@ */
2465 0, /* @tp_as_number@ */
2466 0, /* @tp_as_sequence@ */
2467 0, /* @tp_as_mapping@ */
2471 0, /* @tp_getattro@ */
2472 0, /* @tp_setattro@ */
2473 0, /* @tp_as_buffer@ */
2474 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2475 Py_TPFLAGS_BASETYPE
,
2478 "GFN(P, BETA): an object for transforming elements of binary fields\n"
2479 " between polynomial and normal basis representations.",
2481 0, /* @tp_traverse@ */
2483 0, /* @tp_richcompare@ */
2484 0, /* @tp_weaklistoffset@ */
2486 0, /* @tp_iternext@ */
2487 PYMETHODS(gfn
), /* @tp_methods@ */
2488 0, /* @tp_members@ */
2489 PYGETSET(gfn
), /* @tp_getset@ */
2492 0, /* @tp_descr_get@ */
2493 0, /* @tp_descr_set@ */
2494 0, /* @tp_dictoffset@ */
2496 PyType_GenericAlloc
, /* @tp_alloc@ */
2497 gfn_pynew
, /* @tp_new@ */
2502 /*----- Glue --------------------------------------------------------------*/
2504 static const struct nameval consts
[] = {
2509 void mp_pyinit(void)
2513 INITTYPE(mpmul
, root
);
2514 INITTYPE(mpmont
, root
);
2515 INITTYPE(mpbarrett
, root
);
2516 INITTYPE(mpreduce
, root
);
2517 INITTYPE(mpcrt
, root
);
2518 INITTYPE(gfreduce
, root
);
2519 INITTYPE(gfn
, root
);
2522 void mp_pyinsert(PyObject
*mod
)
2524 INSERT("MP", mp_pytype
);
2525 INSERT("MPMul", mpmul_pytype
);
2526 INSERT("MPMont", mpmont_pytype
);
2527 INSERT("MPBarrett", mpbarrett_pytype
);
2528 INSERT("MPReduce", mpreduce_pytype
);
2529 INSERT("MPCRT", mpcrt_pytype
);
2530 INSERT("GF", gf_pytype
);
2531 INSERT("GFReduce", gfreduce_pytype
);
2532 INSERT("GFN", gfn_pytype
);
2533 setconstants(mod
, consts
);
2536 /*----- That's all, folks -------------------------------------------------*/