3 * Multiprecision arithmetic
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
12 * Catacomb/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Catacomb/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Catacomb/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- General utilities -------------------------------------------------*/
33 PyTypeObject
*mp_pytype
= 0;
34 PyTypeObject
*gf_pytype
= 0;
36 mp
*mp_frompylong(PyObject
*obj
)
39 PyLongObject
*l
= (PyLongObject
*)obj
;
50 assert(MPW_BITS
>= SHIFT
);
51 bits
= (unsigned long)sz
* SHIFT
;
52 w
= (bits
+ MPW_BITS
- 1)/MPW_BITS
;
53 x
= mp_new(w
, l
->ob_size
< 0 ? MP_NEG
: 0);
55 for (i
= 0; i
< sz
; i
++) {
56 r
|= (mpd
)l
->ob_digit
[i
] << b
;
58 while (b
>= MPW_BITS
) {
73 PyObject
*mp_topylong(mp
*x
)
75 unsigned long bits
= mp_bits(x
);
76 int sz
= (bits
+ SHIFT
- 1)/SHIFT
;
77 PyLongObject
*l
= _PyLong_New(sz
);
83 assert(MPW_BITS
>= SHIFT
);
84 while (i
< sz
&& p
< x
->vl
) {
87 while (i
< sz
&& b
>= SHIFT
) {
88 l
->ob_digit
[i
++] = r
& MASK
;
94 l
->ob_digit
[i
++] = r
& MASK
;
97 l
->ob_size
= (x
->f
& MP_NEG
) ?
-sz
: sz
;
98 return ((PyObject
*)l
);
101 mp
*mp_frompyobject(PyObject
*o
, int radix
)
105 if (PyString_Check(o
)) {
108 sc
.buf
= PyString_AS_STRING(o
);
109 sc
.lim
= sc
.buf
+ PyString_GET_SIZE(o
);
110 x
= mp_read(MP_NEW
, radix
, &mptext_stringops
, &sc
);
112 if (sc
.buf
< sc
.lim
) { MP_DROP(x
); return (0); }
115 if ((x
= tomp(o
)) != 0)
120 PyObject
*mp_topystring(mp
*x
, int radix
, const char *xpre
,
121 const char *pre
, const char *post
)
123 int len
= mptext_len(x
, radix
) + 1;
126 size_t xprelen
= xpre ?
strlen(xpre
) : 0;
127 size_t prelen
= pre ?
strlen(pre
) : 0;
128 size_t postlen
= post ?
strlen(post
) : 0;
131 o
= PyString_FromStringAndSize(0, len
+ 1 + xprelen
+ prelen
+ postlen
);
132 p
= PyString_AS_STRING(o
);
134 if (xpre
) { memcpy(sc
.buf
, xpre
, xprelen
); sc
.buf
+= xprelen
; }
135 if (MP_NEGP(x
)) { *sc
.buf
++ = '-'; x
= mp_neg(x
, x
); }
136 if (pre
) { memcpy(sc
.buf
, pre
, prelen
); sc
.buf
+= prelen
; }
137 sc
.lim
= sc
.buf
+ len
;
138 mp_write(x
, radix
, &mptext_stringops
, &sc
);
139 if (post
) { memcpy(sc
.buf
, post
, postlen
); sc
.buf
+= postlen
; }
141 _PyString_Resize(&o
, sc
.buf
- p
);
145 static int good_radix_p(int r
, int readp
)
147 return ((r
>= -255 && r
<= -2) ||
149 (r
>= 2 && r
<= 62));
152 PyObject
*mp_pywrap(mp
*x
)
154 mp_pyobj
*z
= PyObject_New(mp_pyobj
, mp_pytype
);
156 return ((PyObject
*)z
);
159 PyObject
*gf_pywrap(mp
*x
)
161 mp_pyobj
*z
= PyObject_New(mp_pyobj
, gf_pytype
);
163 return ((PyObject
*)z
);
166 int mp_tolong_checked(mp
*x
, long *l
, int must
)
168 static mp
*longmin
= 0, *longmax
= 0;
172 longmin
= mp_fromlong(MP_NEW
, LONG_MIN
);
173 longmax
= mp_fromlong(MP_NEW
, LONG_MAX
);
175 if (MP_CMP(x
, <, longmin
) || MP_CMP(x
, >, longmax
)) {
176 if (must
) VALERR("mp out of range for int");
185 /*----- Python interface --------------------------------------------------*/
187 static void mp_pydealloc(PyObject
*o
)
193 static PyObject
*mp_pyrepr(PyObject
*o
)
194 { return mp_topystring(MP_X(o
), 10, "MP(", 0, "L)"); }
196 static PyObject
*mp_pystr(PyObject
*o
)
197 { return mp_topystring(MP_X(o
), 10, 0, 0, 0); }
199 mp
*tomp(PyObject
*o
)
206 else if (MP_PYCHECK(o
) || GF_PYCHECK(o
))
207 return (MP_COPY(MP_X(o
)));
208 else if (FE_PYCHECK(o
))
209 return (F_OUT(FE_F(o
), MP_NEW
, FE_X(o
)));
210 else if (PFILT_PYCHECK(o
))
211 return (MP_COPY(PFILT_F(o
)->m
));
212 else if (ECPT_PYCHECK(o
)) {
214 if (EC_ATINF(ECPT_P(o
))) return (0);
219 } else if (GE_PYCHECK(o
)) {
220 if ((x
= G_TOINT(GE_G(o
), MP_NEW
, GE_X(o
))) == 0)
223 } else if (PyInt_Check(o
))
224 return (mp_fromlong(MP_NEW
, PyInt_AS_LONG(o
)));
225 else if ((l
= PyNumber_Long(o
)) != 0) {
226 x
= mp_frompylong(l
);
235 mp
*getmp(PyObject
*o
)
239 if ((x
= tomp(o
)) == 0) {
240 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to mp",
241 o
->ob_type
->tp_name
);
246 int convmp(PyObject
*o
, void *p
)
249 if ((x
= getmp(o
)) == 0) return (0);
254 mp
*getgf(PyObject
*o
)
258 if ((x
= tomp(o
)) == 0) {
259 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to gf",
260 o
->ob_type
->tp_name
);
265 int convgf(PyObject
*o
, void *p
)
268 if ((x
= getgf(o
)) == 0) return (0);
273 static mp
*implicitmp(PyObject
*o
)
284 static mp
*implicitgf(PyObject
*o
)
295 static int mpbinop(PyObject
*x
, PyObject
*y
, mp
**xx
, mp
**yy
)
297 if ((*xx
= implicitmp(x
)) == 0)
299 if ((*yy
= implicitmp(y
)) == 0) {
306 static int gfbinop(PyObject
*x
, PyObject
*y
, mp
**xx
, mp
**yy
)
308 if ((*xx
= implicitgf(x
)) == 0)
310 if ((*yy
= implicitgf(y
)) == 0) {
317 #define gf_and mp_and
319 #define gf_xor mp_xor
320 #define BINOP(pre, name) \
321 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
323 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
324 zz = pre##_##name(MP_NEW, xx, yy); \
325 MP_DROP(xx); MP_DROP(yy); \
326 return (pre##_pywrap(zz)); \
342 static mp
*mp_abs(mp
*d
, mp
*x
)
345 return (mp_neg(d
, x
));
351 #define UNOP(pre, name) \
352 static PyObject *pre##_py##name(PyObject *x) \
353 { return mp_pywrap(pre##_##name(MP_NEW, MP_X(x))); }
359 static PyObject
*mp_pyid(PyObject
*x
) { RETURN_OBJ(x
); }
361 #define gf_lsr mp_lsr
362 #define SHIFTOP(pre, name, rname) \
363 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
367 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
368 if (mp_tolong_checked(yy, &n, 1)) goto end; \
370 z = pre##_pywrap(mp_##rname(MP_NEW, xx, -n)); \
372 z = pre##_pywrap(mp_##name(MP_NEW, xx, n)); \
374 MP_DROP(xx); MP_DROP(yy); \
377 SHIFTOP(mp
, lsl2c
, lsr2c
)
378 SHIFTOP(mp
, lsr2c
, lsl2c
)
379 SHIFTOP(gf
, lsl
, lsr
)
380 SHIFTOP(gf
, lsr
, lsl
)
383 #define DIVOP(pre, name, qq, rr, gather) \
384 static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \
387 INIT_##qq(q) INIT_##rr(r) \
388 if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \
390 ZDIVERR("division by zero"); \
391 pre##_div(ARG_##qq(q), ARG_##rr(r), xx, yy); \
394 MP_DROP(xx); MP_DROP(yy); \
397 #define INIT_YES(p) mp *p = MP_NEW;
399 #define ARG_YES(p) &p
401 DIVOP(mp
, divmod
, YES
, YES
,
402 Py_BuildValue("(NN)", mp_pywrap(q
), mp_pywrap(r
)))
403 DIVOP(mp
, div
, YES
, NO
, mp_pywrap(q
))
404 DIVOP(mp
, mod
, NO
, YES
, mp_pywrap(r
))
405 DIVOP(gf
, divmod
, YES
, YES
,
406 Py_BuildValue("(NN)", gf_pywrap(q
), gf_pywrap(r
)))
407 DIVOP(gf
, div
, YES
, NO
, gf_pywrap(q
))
408 DIVOP(gf
, mod
, NO
, YES
, gf_pywrap(r
))
415 static mp
*mp_modinv_checked(mp
*d
, mp
*x
, mp
*p
)
418 mp_gcd(&g
, 0, &d
, p
, x
);
419 if (!MP_EQ(g
, MP_ONE
)) {
420 MP_DROP(g
); MP_DROP(d
);
421 PyErr_SetString(PyExc_ZeroDivisionError
, "no modular inverse");
424 MP_DROP(g
); return (d
);
427 static PyObject
*mp_pyexp(PyObject
*x
, PyObject
*y
, PyObject
*z
)
429 mp
*xx
= 0, *yy
= 0, *zz
= 0;
433 if ((xx
= implicitmp(x
)) == 0 || (yy
= implicitmp(y
)) == 0 ||
434 (z
&& z
!= Py_None
&& (zz
= tomp(z
)) == 0)) {
435 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
438 if (!z
|| z
== Py_None
) {
439 if (MP_NEGP(yy
)) VALERR("negative exponent");
440 r
= mp_exp(MP_NEW
, xx
, yy
);
442 if (!MP_POSP(zz
)) VALERR("modulus must be positive");
444 if ((xx
= mp_modinv_checked(xx
, xx
, zz
)) == 0) goto end
;
449 mpmont_create(&mm
, zz
);
450 r
= mpmont_exp(&mm
, MP_NEW
, xx
, yy
);
454 mpbarrett_create(&mb
, zz
);
455 r
= mpbarrett_exp(&mb
, MP_NEW
, xx
, yy
);
456 mpbarrett_destroy(&mb
);
461 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
465 static mp
*gf_modinv_checked(mp
*d
, mp
*x
, mp
*p
)
468 gf_gcd(&g
, 0, &d
, p
, x
);
469 if (!MP_EQ(g
, MP_ONE
)) {
470 MP_DROP(g
); MP_DROP(d
);
471 PyErr_SetString(PyExc_ZeroDivisionError
, "no modular inverse");
474 MP_DROP(g
); return (d
);
477 #define BASEOP(name, radix, pre) \
478 static PyObject *mp_py##name(PyObject *x) \
479 { return mp_topystring(MP_X(x), radix, 0, pre, 0); }
481 BASEOP(hex
, 16, "0x");
484 static int mp_pynonzerop(PyObject
*x
) { return !MP_ZEROP(MP_X(x
)); }
486 static PyObject
*mp_pyint(PyObject
*x
)
489 if (!mp_tolong_checked(MP_X(x
), &l
, 0)) return (PyInt_FromLong(l
));
490 else return mp_topylong(MP_X(x
));
492 static PyObject
*mp_pylong(PyObject
*x
)
493 { return (mp_topylong(MP_X(x
))); }
494 static PyObject
*mp_pyfloat(PyObject
*x
)
496 PyObject
*l
= mp_topylong(MP_X(x
));
497 double f
= PyLong_AsDouble(l
);
499 return (PyFloat_FromDouble(f
));
502 #define COERCE(pre, PRE) \
503 static int pre##_pycoerce(PyObject **x, PyObject **y) \
507 if (PRE##_PYCHECK(*y)) { \
508 Py_INCREF(*x); Py_INCREF(*y); \
511 if ((z = tomp(*y)) != 0) { \
513 *y = pre##_pywrap(z); \
522 static int mp_pycompare(PyObject
*x
, PyObject
*y
)
523 { return mp_cmp(MP_X(x
), MP_X(y
)); }
525 static PyObject
*mp_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
531 static const char *const kwlist
[] = { "x", "radix", 0 };
533 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|i:new", KWLIST
, &x
, &radix
))
535 if (MP_PYCHECK(x
)) RETURN_OBJ(x
);
536 if (!good_radix_p(radix
, 1)) VALERR("bad radix");
537 if ((z
= mp_frompyobject(x
, radix
)) == 0) {
538 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to mp",
539 x
->ob_type
->tp_name
);
542 zz
= (mp_pyobj
*)ty
->tp_alloc(ty
, 0);
545 return ((PyObject
*)zz
);
550 PyObject
*l
= mp_topylong(x
);
551 long h
= PyObject_Hash(l
);
552 Py_DECREF(l
); return (h
);
555 static long mp_pyhash(PyObject
*me
) { return (mphash(MP_X(me
))); }
557 static PyObject
*mpmeth_jacobi(PyObject
*me
, PyObject
*arg
)
562 if (!PyArg_ParseTuple(arg
, "O&:jacobi", convmp
, &y
)) goto end
;
563 z
= PyInt_FromLong(mp_jacobi(y
, MP_X(me
)));
569 #define BITOP(pre, name, c) \
570 static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \
573 if (!PyArg_ParseTuple(arg, "O&:" #name, convulong, &i)) return (0); \
574 return (pre##_pywrap(mp_##name##c(MP_NEW, MP_X(me), i))); \
576 BITOP(mp
, setbit
, 2c
);
577 BITOP(mp
, clearbit
, 2c
);
579 BITOP(gf
, clearbit
, );
582 static PyObject
*mpmeth_testbit(PyObject
*me
, PyObject
*arg
)
585 if (!PyArg_ParseTuple(arg
, "O&:testbit", convulong
, &i
)) return (0);
586 return (getbool(mp_testbit2c(MP_X(me
), i
)));
589 static PyObject
*gfmeth_testbit(PyObject
*me
, PyObject
*arg
)
592 if (!PyArg_ParseTuple(arg
, "O&:testbit", convulong
, &i
)) return (0);
593 return (getbool(mp_testbit(MP_X(me
), i
)));
596 static PyObject
*mpmeth_odd(PyObject
*me
)
601 t
= mp_odd(MP_NEW
, MP_X(me
), &s
);
602 return (Py_BuildValue("(lN)", (long)s
, mp_pywrap(t
)));
605 static PyObject
*mpmeth_sqr(PyObject
*me
)
606 { return (mp_pywrap(mp_sqr(MP_NEW
, MP_X(me
)))); }
608 static PyObject
*mpmeth_sqrt(PyObject
*me
)
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 static const char *const 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 static PyObject
*mpmeth_leastcongruent(PyObject
*me
, PyObject
*arg
)
688 if (!PyArg_ParseTuple(arg
, "O&O&:leastcongruent", convmp
, &b
, convmp
, &m
))
690 z
= mp_leastcongruent(MP_NEW
, b
, MP_X(me
), m
);
696 #define STOREOP(name, c) \
697 static PyObject *mpmeth_##name(PyObject *me, \
698 PyObject *arg, PyObject *kw) \
701 static const char *const kwlist[] = { "len", 0 }; \
704 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|l:" #name, \
708 len = mp_octets##c(MP_X(me)); \
711 rc = bytestring_pywrap(0, len); \
712 mp_##name(MP_X(me), PyString_AS_STRING(rc), len); \
718 STOREOP(storel2c
, 2c
)
719 STOREOP(storeb2c
, 2c
)
723 static PyObject *ty##meth_frombuf(PyObject *me, PyObject *arg) \
731 if (!PyArg_ParseTuple(arg, "s#:frombuf", &p, &sz)) goto end; \
732 buf_init(&b, p, sz); \
733 if ((x = buf_getmp(&b)) == 0) VALERR("malformed data"); \
734 rc = Py_BuildValue("(NN)", ty##_pywrap(x), \
735 bytestring_pywrapbuf(&b)); \
743 static PyObject
*mpmeth_tobuf(PyObject
*me
)
751 n
= mp_octets(x
) + 3;
752 rc
= bytestring_pywrap(0, n
);
753 buf_init(&b
, PyString_AS_STRING(rc
), n
);
756 _PyString_Resize(&rc
, BLEN(&b
));
760 static PyObject
*mpmeth_primep(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
762 grand
*r
= &rand_global
;
763 static const char *const kwlist
[] = { "rng", 0 };
766 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&", KWLIST
, convgrand
, &r
))
768 rc
= getbool(pgen_primep(MP_X(me
), r
));
773 static PyObject
*mpmeth_fromstring(PyObject
*me
,
774 PyObject
*arg
, PyObject
*kw
)
782 static const char *const kwlist
[] = { "x", "radix", 0 };
784 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|i:fromstring", KWLIST
,
787 if (!good_radix_p(r
, 1)) VALERR("bad radix");
788 sc
.buf
= p
; sc
.lim
= p
+ len
;
789 if ((zz
= mp_read(MP_NEW
, r
, &mptext_stringops
, &sc
)) == 0)
790 VALERR("bad integer");
791 z
= Py_BuildValue("(Ns#)", mp_pywrap(zz
),
792 sc
.buf
, (Py_ssize_t
)(sc
.lim
- sc
.buf
));
797 static PyObject
*mpmeth_factorial(PyObject
*me
, PyObject
*arg
)
801 if (!PyArg_ParseTuple(arg
, "O&:factorial", convulong
, &i
)) return (0);
806 static PyObject
*mpmeth_fibonacci(PyObject
*me
, PyObject
*arg
)
810 if (!PyArg_ParseTuple(arg
, "l:fibonacci", &i
)) return (0);
815 #define LOADOP(pre, name) \
816 static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \
820 if (!PyArg_ParseTuple(arg, "s#:" #name, &p, &len)) return (0); \
821 return (pre##_pywrap(mp_##name(MP_NEW, p, len))); \
831 static PyObject
*mpget_nbits(PyObject
*me
, void *hunoz
)
832 { return (PyInt_FromLong(mp_bits(MP_X(me
)))); }
834 static PyObject
*mpget_noctets(PyObject
*me
, void *hunoz
)
835 { return (PyInt_FromLong(mp_octets(MP_X(me
)))); }
837 static PyObject
*mpget_noctets2c(PyObject
*me
, void *hunoz
)
838 { return (PyInt_FromLong(mp_octets2c(MP_X(me
)))); }
840 static const PyGetSetDef mp_pygetset
[] = {
841 #define GETSETNAME(op, func) mp##op##_##func
842 GET (nbits
, "X.nbits -> bit length of X")
843 GET (noctets
, "X.noctets -> octet length of X")
844 GET (noctets2c
, "X.noctets2c -> two's complement octet length of X")
849 static const PyMethodDef mp_pymethods
[] = {
850 #define METHNAME(func) mpmeth_##func
851 METH (jacobi
, "X.jacobi(Y) -> Jacobi symbol (Y|X) (NB inversion!)")
852 METH (setbit
, "X.setbit(N) -> X with bit N set")
853 METH (clearbit
, "X.clearbit(N) -> X with bit N clear")
854 METH (testbit
, "X.testbit(N) -> true/false if bit N set/clear in X")
855 NAMETH(odd
, "X.odd() -> S, T where X = 2^S T with T odd")
856 NAMETH(sqr
, "X.sqr() -> X^2")
857 NAMETH(sqrt
, "X.sqrt() -> largest integer <= sqrt(X)")
858 METH (gcd
, "X.gcd(Y) -> gcd(X, Y)")
859 METH (gcdx
, "X.gcdx(Y) -> (gcd(X, Y), U, V) "
860 "with X U + Y V = gcd(X, Y)")
861 METH (modinv
, "X.modinv(Y) -> multiplicative inverse of Y mod X")
862 METH (modsqrt
, "X.modsqrt(Y) -> square root of Y mod X, if X prime")
863 METH (leastcongruent
, "X.leastcongruent(B, M) -> "
864 "smallest Z >= B with Z == X (mod M)")
865 KWMETH(primep
, "X.primep([rng = rand]) -> X is prime?")
866 KWMETH(tostring
, "X.tostring([radix = 10]) -> STR")
867 KWMETH(storel
, "X.storel([len = -1]) -> little-endian bytes")
868 KWMETH(storeb
, "X.storeb([len = -1]) -> big-endian bytes")
869 KWMETH(storel2c
, "X.storel2c([len = -1]) -> "
870 "little-endian bytes, two's complement")
871 KWMETH(storeb2c
, "X.storeb2c([len = -1]) -> "
872 "big-endian bytes, two's complement")
873 NAMETH(tobuf
, "X.tobuf() -> buffer format")
874 KWSMTH(fromstring
, "fromstring(STR, [radix = 0]) -> (X, REST)\n"
875 " Parse STR as a large integer, according to RADIX. If RADIX is\n"
876 " zero, read a prefix from STR to decide radix: allow `0b' for binary,\n"
877 " `0' or `0o' for octal, `0x' for hex, or `R_' for other radix R.")
878 SMTH (factorial
, "factorial(I) -> I!: compute factorial")
879 SMTH (fibonacci
, "fibonacci(I) -> F(I): compute Fibonacci number")
880 SMTH (loadl
, "loadl(STR) -> X: read little-endian bytes")
881 SMTH (loadb
, "loadb(STR) -> X: read big-endian bytes")
882 SMTH (loadl2c
, "loadl2c(STR) -> X: "
883 "read little-endian bytes, two's complement")
884 SMTH (loadb2c
, "loadb2c(STR) -> X: "
885 "read big-endian bytes, two's complement")
886 SMTH (frombuf
, "frombuf(STR) -> (X, REST): read buffer format")
891 static const PyNumberMethods mp_pynumber
= {
892 mp_pyadd
, /* @nb_add@ */
893 mp_pysub
, /* @nb_subtract@ */
894 mp_pymul
, /* @nb_multiply@ */
896 mp_pymod
, /* @nb_remainder@ */
897 mp_pydivmod
, /* @nb_divmod@ */
898 mp_pyexp
, /* @nb_power@ */
899 mp_pyneg
, /* @nb_negative@ */
900 mp_pyid
, /* @nb_positive@ */
901 mp_pyabs
, /* @nb_absolute@ */
902 mp_pynonzerop
, /* @nb_nonzero@ */
903 mp_pynot2c
, /* @nb_invert@ */
904 mp_pylsl2c
, /* @nb_lshift@ */
905 mp_pylsr2c
, /* @nb_rshift@ */
906 mp_pyand2c
, /* @nb_and@ */
907 mp_pyxor2c
, /* @nb_xor@ */
908 mp_pyor2c
, /* @nb_or@ */
909 mp_pycoerce
, /* @nb_coerce@ */
910 mp_pyint
, /* @nb_int@ */
911 mp_pylong
, /* @nb_long@ */
912 mp_pyfloat
, /* @nb_float@ */
913 mp_pyoct
, /* @nb_oct@ */
914 mp_pyhex
, /* @nb_hex@ */
916 0, /* @nb_inplace_add@ */
917 0, /* @nb_inplace_subtract@ */
918 0, /* @nb_inplace_multiply@ */
919 0, /* @nb_inplace_divide@ */
920 0, /* @nb_inplace_remainder@ */
921 0, /* @nb_inplace_power@ */
922 0, /* @nb_inplace_lshift@ */
923 0, /* @nb_inplace_rshift@ */
924 0, /* @nb_inplace_and@ */
925 0, /* @nb_inplace_xor@ */
926 0, /* @nb_inplace_or@ */
928 mp_pydiv
, /* @nb_floor_divide@ */
929 0, /* @nb_true_divide@ */
930 0, /* @nb_inplace_floor_divide@ */
931 0, /* @nb_inplace_true_divide@ */
934 static PyTypeObject mp_pytype_skel
= {
935 PyObject_HEAD_INIT(0) 0, /* Header */
936 "MP", /* @tp_name@ */
937 sizeof(mp_pyobj
), /* @tp_basicsize@ */
938 0, /* @tp_itemsize@ */
940 mp_pydealloc
, /* @tp_dealloc@ */
942 0, /* @tp_getattr@ */
943 0, /* @tp_setattr@ */
944 mp_pycompare
, /* @tp_compare@ */
945 mp_pyrepr
, /* @tp_repr@ */
946 PYNUMBER(mp
), /* @tp_as_number@ */
947 0, /* @tp_as_sequence@ */
948 0, /* @tp_as_mapping@ */
949 mp_pyhash
, /* @tp_hash@ */
951 mp_pystr
, /* @tp_str@ */
952 0, /* @tp_getattro@ */
953 0, /* @tp_setattro@ */
954 0, /* @tp_as_buffer@ */
955 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
956 Py_TPFLAGS_CHECKTYPES
|
960 "Multiprecision integers, similar to `long' but more efficient and\n"
961 "versatile. Support all the standard arithmetic operations, with\n"
962 "implicit conversions from `PrimeFilter', and other objects which\n"
963 "convert to `long'.\n"
965 "Constructor MP(X, [radix = R]) attempts to convert X to an `MP'. If\n"
966 "X is a string, it's read in radix-R form, or we look for a prefix\n"
967 "if R = 0. Other acceptable things are field elements, elliptic curve\n"
968 "points, group elements, Python `int' and `long' objects, and anything\n"
969 "with an integer conversion.\n"
973 " * Use `//' for integer division: `/' gives exact rational division.",
975 0, /* @tp_traverse@ */
977 0, /* @tp_richcompare@ */
978 0, /* @tp_weaklistoffset@ */
980 0, /* @tp_iternext@ */
981 PYMETHODS(mp
), /* @tp_methods@ */
982 0, /* @tp_members@ */
983 PYGETSET(mp
), /* @tp_getset@ */
986 0, /* @tp_descr_get@ */
987 0, /* @tp_descr_set@ */
988 0, /* @tp_dictoffset@ */
990 PyType_GenericAlloc
, /* @tp_alloc@ */
991 mp_pynew
, /* @tp_new@ */
996 /*----- Products of small integers ----------------------------------------*/
998 typedef struct mpmul_pyobj
{
1004 #define MPMUL_LIVEP(o) (((mpmul_pyobj *)(o))->livep)
1005 #define MPMUL_PY(o) (&((mpmul_pyobj *)(o))->mm)
1007 static void mpmul_pydealloc(PyObject
*me
)
1009 if (MPMUL_LIVEP(me
))
1010 mp_drop(mpmul_done(MPMUL_PY(me
)));
1014 static PyObject
*mmmeth_factor(PyObject
*me
, PyObject
*arg
)
1019 if (!MPMUL_LIVEP(me
)) VALERR("MPMul object invalid");
1020 if (PyTuple_GET_SIZE(arg
) != 1)
1021 i
= PyObject_GetIter(arg
);
1023 if ((q
= PyTuple_GET_ITEM(arg
, 0)) == 0) goto end
;
1024 if ((i
= PyObject_GetIter(q
)) == 0) {
1025 PyErr_Clear(); /* that's ok */
1026 i
= PyObject_GetIter(arg
);
1030 while ((q
= PyIter_Next(i
)) != 0) {
1031 x
= getmp(q
); Py_DECREF(q
); if (!x
) {
1035 mpmul_add(MPMUL_PY(me
), x
);
1044 static PyObject
*mmmeth_done(PyObject
*me
)
1048 if (!MPMUL_LIVEP(me
)) VALERR("MPMul object invalid");
1049 x
= mpmul_done(MPMUL_PY(me
));
1050 MPMUL_LIVEP(me
) = 0;
1051 return (mp_pywrap(x
));
1056 static PyObject
*mpmul_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1060 if (kw
) TYERR("keyword arguments not allowed here");
1061 mm
= (mpmul_pyobj
*)ty
->tp_alloc(ty
, 0);
1062 mpmul_init(&mm
->mm
);
1064 if (mmmeth_factor((PyObject
*)mm
, arg
) == 0) {
1068 return ((PyObject
*)mm
);
1073 static PyObject
*mmget_livep(PyObject
*me
, void *hunoz
)
1074 { return (getbool(MPMUL_LIVEP(me
))); }
1076 static const PyGetSetDef mpmul_pygetset
[] = {
1077 #define GETSETNAME(op, name) mm##op##_##name
1078 GET (livep
, "MM.livep -> flag: object still valid?")
1083 static const PyMethodDef mpmul_pymethods
[] = {
1084 #define METHNAME(name) mmmeth_##name
1085 METH (factor
, "MM.factor(ITERABLE) or MM.factor(I, ...)")
1086 NAMETH(done
, "MM.done() -> PRODUCT")
1091 static PyTypeObject
*mpmul_pytype
, mpmul_pytype_skel
= {
1092 PyObject_HEAD_INIT(0) 0, /* Header */
1093 "MPMul", /* @tp_name@ */
1094 sizeof(mpmul_pyobj
), /* @tp_basicsize@ */
1095 0, /* @tp_itemsize@ */
1097 mpmul_pydealloc
, /* @tp_dealloc@ */
1099 0, /* @tp_getattr@ */
1100 0, /* @tp_setattr@ */
1101 0, /* @tp_compare@ */
1103 0, /* @tp_as_number@ */
1104 0, /* @tp_as_sequence@ */
1105 0, /* @tp_as_mapping@ */
1109 0, /* @tp_getattro@ */
1110 0, /* @tp_setattro@ */
1111 0, /* @tp_as_buffer@ */
1112 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1113 Py_TPFLAGS_BASETYPE
,
1116 "MPMul(N_0, N_1, ....): an object for multiplying many small integers.",
1118 0, /* @tp_traverse@ */
1120 0, /* @tp_richcompare@ */
1121 0, /* @tp_weaklistoffset@ */
1123 0, /* @tp_iternext@ */
1124 PYMETHODS(mpmul
), /* @tp_methods@ */
1125 0, /* @tp_members@ */
1126 PYGETSET(mpmul
), /* @tp_getset@ */
1129 0, /* @tp_descr_get@ */
1130 0, /* @tp_descr_set@ */
1131 0, /* @tp_dictoffset@ */
1133 PyType_GenericAlloc
, /* @tp_alloc@ */
1134 mpmul_pynew
, /* @tp_new@ */
1139 /*----- Montgomery reduction ----------------------------------------------*/
1141 typedef struct mpmont_pyobj
{
1146 #define MPMONT_PY(o) (&((mpmont_pyobj *)(o))->mm)
1148 static PyObject
*mmmeth_int(PyObject
*me
, PyObject
*arg
)
1152 mpmont
*mm
= MPMONT_PY(me
);
1154 if (!PyArg_ParseTuple(arg
, "O&:in", convmp
, &yy
))
1156 mp_div(0, &yy
, yy
, mm
->m
);
1157 z
= mp_pywrap(mpmont_mul(mm
, MP_NEW
, yy
, mm
->r2
));
1159 if (yy
) MP_DROP(yy
);
1163 static PyObject
*mmmeth_mul(PyObject
*me
, PyObject
*arg
)
1166 mp
*yy
= 0, *zz
= 0;
1168 if (!PyArg_ParseTuple(arg
, "O&O&:mul", convmp
, &yy
, convmp
, &zz
))
1170 rc
= mp_pywrap(mpmont_mul(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1172 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1176 static PyObject
*mmmeth_exp(PyObject
*me
, PyObject
*arg
)
1179 mp
*yy
= 0, *zz
= 0;
1181 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1184 if ((yy
= mp_modinv_checked(yy
, yy
, MPMONT_PY(me
)->m
)) == 0) goto end
;
1185 zz
= mp_neg(zz
, zz
);
1187 rc
= mp_pywrap(mpmont_exp(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1189 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1193 static PyObject
*mmmeth_expr(PyObject
*me
, PyObject
*arg
)
1196 mp
*yy
= 0, *zz
= 0;
1198 if (!PyArg_ParseTuple(arg
, "O&O&:expr", convmp
, &yy
, convmp
, &zz
))
1201 yy
= mpmont_reduce(MPMONT_PY(me
), yy
, yy
);
1202 if ((yy
= mp_modinv_checked(yy
, yy
, MPMONT_PY(me
)->m
)) == 0) goto end
;
1203 yy
= mpmont_mul(MPMONT_PY(me
), yy
, yy
, MPMONT_PY(me
)->r2
);
1204 zz
= mp_neg(zz
, zz
);
1206 rc
= mp_pywrap(mpmont_expr(MPMONT_PY(me
), MP_NEW
, yy
, zz
));
1208 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1212 static PyObject
*mm_mexpr_id(PyObject
*me
)
1213 { return mp_pywrap(MP_COPY(MPMONT_PY(me
)->r
)); }
1215 static int mm_mexpr_fill(void *p
, PyObject
*me
, PyObject
*x
, PyObject
*y
)
1217 mp
*xx
= 0, *yy
= 0;
1218 mp_expfactor
*f
= p
;
1219 mpmont
*mm
= MPMONT_PY(me
);
1221 if ((xx
= getmp(x
)) == 0 || (yy
= getmp(y
)) == 0)
1224 xx
= mpmont_reduce(mm
, xx
, xx
);
1225 if ((xx
= mp_modinv_checked(xx
, xx
, yy
)) == 0)
1227 xx
= mpmont_mul(mm
, xx
, xx
, mm
->r2
);
1228 yy
= mp_neg(yy
, yy
);
1235 mp_drop(xx
); mp_drop(yy
);
1239 static PyObject
*mm_mexpr(PyObject
*me
, void *v
, int n
)
1240 { return mp_pywrap(mpmont_mexpr(MPMONT_PY(me
), MP_NEW
, v
, n
)); }
1242 static void mp_mexp_drop(void *p
)
1244 mp_expfactor
*f
= p
;
1249 static PyObject
*mmmeth_mexpr(PyObject
*me
, PyObject
*arg
)
1251 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1252 mm_mexpr_id
, mm_mexpr_fill
, mm_mexpr
, mp_mexp_drop
);
1255 static PyObject
*mp_mexp_id(PyObject
*me
)
1256 { return mp_pywrap(MP_ONE
); }
1258 static int mp_mexp_fill(void *p
, PyObject
*me
, PyObject
*x
, PyObject
*y
)
1260 mp
*xx
= 0, *yy
= 0;
1261 mp_expfactor
*f
= p
;
1263 if ((xx
= getmp(x
)) == 0 || (yy
= getmp(y
)) == 0)
1266 if ((xx
= mp_modinv_checked(xx
, xx
, yy
)) == 0)
1268 yy
= mp_neg(yy
, yy
);
1275 mp_drop(xx
); mp_drop(yy
);
1279 static PyObject
*mm_mexp(PyObject
*me
, void *v
, int n
)
1280 { return mp_pywrap(mpmont_mexp(MPMONT_PY(me
), MP_NEW
, v
, n
)); }
1282 static PyObject
*mmmeth_mexp(PyObject
*me
, PyObject
*arg
)
1284 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1285 mp_mexp_id
, mp_mexp_fill
, mm_mexp
, mp_mexp_drop
);
1288 #define mmmeth_ext mmmeth_reduce
1289 static PyObject
*mmmeth_reduce(PyObject
*me
, PyObject
*arg
)
1294 if (!PyArg_ParseTuple(arg
, "O&", convmp
, &yy
)) goto end
;
1295 z
= mp_pywrap(mpmont_reduce(MPMONT_PY(me
), MP_NEW
, yy
));
1300 static void mpmont_pydealloc(PyObject
*me
)
1302 mpmont_destroy(MPMONT_PY(me
));
1306 static PyObject
*mpmont_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1308 mpmont_pyobj
*mm
= 0;
1309 static const char *const kwlist
[] = { "m", 0 };
1312 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convmp
, &xx
))
1314 if (!MP_POSP(xx
) || !MP_ODDP(xx
)) VALERR("m must be positive and odd");
1315 mm
= (mpmont_pyobj
*)ty
->tp_alloc(ty
, 0);
1316 mpmont_create(&mm
->mm
, xx
);
1318 if (xx
) MP_DROP(xx
);
1319 return ((PyObject
*)mm
);
1322 static PyObject
*mmget_m(PyObject
*me
, void *hunoz
)
1323 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->m
))); }
1325 static PyObject
*mmget_r(PyObject
*me
, void *hunoz
)
1326 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->r
))); }
1328 static PyObject
*mmget_r2(PyObject
*me
, void *hunoz
)
1329 { return (mp_pywrap(MP_COPY(MPMONT_PY(me
)->r2
))); }
1331 static const PyGetSetDef mpmont_pygetset
[] = {
1332 #define GETSETNAME(op, name) mm##op##_##name
1333 GET (m
, "M.m -> modulus for reduction")
1334 GET (r
, "M.r -> multiplicative identity")
1335 GET (r2
, "M.r2 -> M.r^2, Montgomerization factor")
1340 static const PyMethodDef mpmont_pymethods
[] = {
1341 #define METHNAME(name) mmmeth_##name
1342 METH (int, "M.int(X) -> XR")
1343 METH (mul
, "M.mul(XR, YR) -> ZR where Z = X Y")
1344 METH (expr
, "M.expr(XR, N) -> ZR where Z = X^N mod M.m")
1345 METH (mexpr
, "M.mexpr([(XR0, N0), (XR1, N1), ...]) = ZR "
1346 "where Z = X0^N0 X1^N1 ... mod M.m\n"
1347 "\t(the list may be flattened if this more convenient.)")
1348 METH (reduce
, "M.reduce(XR) -> X")
1349 METH (ext
, "M.ext(XR) -> X")
1350 METH (exp
, "M.exp(X, N) -> X^N mod M.m")
1351 METH (mexp
, "M.mexp([(X0, N0), (X1, N1), ...]) = "
1352 "X0^N0 X1^N1 ... mod M.m\n"
1353 "\t(the list may be flattened if this more convenient.)")
1358 static PyTypeObject
*mpmont_pytype
, mpmont_pytype_skel
= {
1359 PyObject_HEAD_INIT(0) 0, /* Header */
1360 "MPMont", /* @tp_name@ */
1361 sizeof(mpmont_pyobj
), /* @tp_basicsize@ */
1362 0, /* @tp_itemsize@ */
1364 mpmont_pydealloc
, /* @tp_dealloc@ */
1366 0, /* @tp_getattr@ */
1367 0, /* @tp_setattr@ */
1368 0, /* @tp_compare@ */
1370 0, /* @tp_as_number@ */
1371 0, /* @tp_as_sequence@ */
1372 0, /* @tp_as_mapping@ */
1376 0, /* @tp_getattro@ */
1377 0, /* @tp_setattro@ */
1378 0, /* @tp_as_buffer@ */
1379 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1380 Py_TPFLAGS_BASETYPE
,
1383 "MPMont(N): a Montgomery reduction context.",
1385 0, /* @tp_traverse@ */
1387 0, /* @tp_richcompare@ */
1388 0, /* @tp_weaklistoffset@ */
1390 0, /* @tp_iternext@ */
1391 PYMETHODS(mpmont
), /* @tp_methods@ */
1392 0, /* @tp_members@ */
1393 PYGETSET(mpmont
), /* @tp_getset@ */
1396 0, /* @tp_descr_get@ */
1397 0, /* @tp_descr_set@ */
1398 0, /* @tp_dictoffset@ */
1400 PyType_GenericAlloc
, /* @tp_alloc@ */
1401 mpmont_pynew
, /* @tp_new@ */
1406 /*----- Barrett reduction -------------------------------------------------*/
1408 typedef struct mpbarrett_pyobj
{
1413 #define MPBARRETT_PY(o) (&((mpbarrett_pyobj *)(o))->mb)
1415 static PyObject
*mbmeth_exp(PyObject
*me
, PyObject
*arg
)
1418 mp
*yy
= 0, *zz
= 0;
1420 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1423 if ((yy
= mp_modinv_checked(yy
, yy
, MPBARRETT_PY(me
)->m
)) == 0) goto end
;
1424 zz
= mp_neg(zz
, zz
);
1426 rc
= mp_pywrap(mpbarrett_exp(MPBARRETT_PY(me
), MP_NEW
, yy
, zz
));
1428 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1432 static PyObject
*mb_mexp(PyObject
*me
, void *v
, int n
)
1433 { return mp_pywrap(mpbarrett_mexp(MPBARRETT_PY(me
), MP_NEW
, v
, n
)); }
1435 static PyObject
*mbmeth_mexp(PyObject
*me
, PyObject
*arg
)
1437 return mexp_common(me
, arg
, sizeof(mp_expfactor
),
1438 mp_mexp_id
, mp_mexp_fill
, mb_mexp
, mp_mexp_drop
);
1441 static PyObject
*mbmeth_reduce(PyObject
*me
, PyObject
*arg
)
1446 if (!PyArg_ParseTuple(arg
, "O&:reduce", convmp
, &yy
))
1448 z
= mp_pywrap(mpbarrett_reduce(MPBARRETT_PY(me
), MP_NEW
, yy
));
1453 static void mpbarrett_pydealloc(PyObject
*me
)
1455 mpbarrett_destroy(MPBARRETT_PY(me
));
1459 static PyObject
*mpbarrett_pynew(PyTypeObject
*ty
,
1460 PyObject
*arg
, PyObject
*kw
)
1462 mpbarrett_pyobj
*mb
= 0;
1463 static const char *const kwlist
[] = { "m", 0 };
1466 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convmp
, &xx
))
1468 if (!MP_POSP(xx
)) VALERR("m must be positive");
1469 mb
= (mpbarrett_pyobj
*)ty
->tp_alloc(ty
, 0);
1470 mpbarrett_create(&mb
->mb
, xx
);
1472 if (xx
) MP_DROP(xx
);
1473 return ((PyObject
*)mb
);
1476 static PyObject
*mbget_m(PyObject
*me
, void *hunoz
)
1477 { return (mp_pywrap(MP_COPY(MPBARRETT_PY(me
)->m
))); }
1479 static const PyGetSetDef mpbarrett_pygetset
[] = {
1480 #define GETSETNAME(op, name) mb##op##_##name
1481 GET (m
, "B.m -> modulus for reduction")
1486 static const PyMethodDef mpbarrett_pymethods
[] = {
1487 #define METHNAME(name) mbmeth_##name
1488 METH (reduce
, "B.reduce(X) -> X mod B.m")
1489 METH (exp
, "B.exp(X, N) -> X^N mod B.m")
1490 METH (mexp
, "B.mexp([(X0, N0), (X1, N1), ...]) = "
1491 "X0^N0 X1^N1 ... mod B.m\n"
1492 "\t(the list may be flattened if this more convenient.)")
1497 static PyTypeObject
*mpbarrett_pytype
, mpbarrett_pytype_skel
= {
1498 PyObject_HEAD_INIT(0) 0, /* Header */
1499 "MPBarrett", /* @tp_name@ */
1500 sizeof(mpbarrett_pyobj
), /* @tp_basicsize@ */
1501 0, /* @tp_itemsize@ */
1503 mpbarrett_pydealloc
, /* @tp_dealloc@ */
1505 0, /* @tp_getattr@ */
1506 0, /* @tp_setattr@ */
1507 0, /* @tp_compare@ */
1509 0, /* @tp_as_number@ */
1510 0, /* @tp_as_sequence@ */
1511 0, /* @tp_as_mapping@ */
1515 0, /* @tp_getattro@ */
1516 0, /* @tp_setattro@ */
1517 0, /* @tp_as_buffer@ */
1518 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1519 Py_TPFLAGS_BASETYPE
,
1522 "MPBarrett(N): a Barrett reduction context.",
1524 0, /* @tp_traverse@ */
1526 0, /* @tp_richcompare@ */
1527 0, /* @tp_weaklistoffset@ */
1529 0, /* @tp_iternext@ */
1530 PYMETHODS(mpbarrett
), /* @tp_methods@ */
1531 0, /* @tp_members@ */
1532 PYGETSET(mpbarrett
), /* @tp_getset@ */
1535 0, /* @tp_descr_get@ */
1536 0, /* @tp_descr_set@ */
1537 0, /* @tp_dictoffset@ */
1539 PyType_GenericAlloc
, /* @tp_alloc@ */
1540 mpbarrett_pynew
, /* @tp_new@ */
1545 /*----- Nice prime reduction ----------------------------------------------*/
1547 typedef struct mpreduce_pyobj
{
1552 #define MPREDUCE_PY(o) (&((mpreduce_pyobj *)(o))->mr)
1554 static PyObject
*mrmeth_exp(PyObject
*me
, PyObject
*arg
)
1557 mp
*yy
= 0, *zz
= 0;
1559 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convmp
, &yy
, convmp
, &zz
))
1562 if ((yy
= mp_modinv_checked(yy
, yy
, MPREDUCE_PY(me
)->p
)) == 0) goto end
;
1563 zz
= mp_neg(zz
, zz
);
1565 rc
= mp_pywrap(mpreduce_exp(MPREDUCE_PY(me
), MP_NEW
, yy
, zz
));
1567 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
1571 static PyObject
*mrmeth_reduce(PyObject
*me
, PyObject
*arg
)
1576 if (!PyArg_ParseTuple(arg
, "O&:reduce", convmp
, &yy
)) goto end
;
1577 z
= mp_pywrap(mpreduce_do(MPREDUCE_PY(me
), MP_NEW
, yy
));
1582 static void mpreduce_pydealloc(PyObject
*me
)
1584 mpreduce_destroy(MPREDUCE_PY(me
));
1588 static PyObject
*mpreduce_pynew(PyTypeObject
*ty
,
1589 PyObject
*arg
, PyObject
*kw
)
1591 mpreduce_pyobj
*mr
= 0;
1593 static const char *const kwlist
[] = { "m", 0 };
1596 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convmp
, &xx
))
1598 if (!MP_POSP(xx
)) VALERR("m must be positive");
1599 if (mpreduce_create(&r
, xx
)) VALERR("bad modulus (must be 2^k - ...)");
1600 mr
= (mpreduce_pyobj
*)ty
->tp_alloc(ty
, 0);
1603 if (xx
) MP_DROP(xx
);
1604 return ((PyObject
*)mr
);
1607 static PyObject
*mrget_m(PyObject
*me
, void *hunoz
)
1608 { return (mp_pywrap(MP_COPY(MPREDUCE_PY(me
)->p
))); }
1610 static const PyGetSetDef mpreduce_pygetset
[] = {
1611 #define GETSETNAME(op, name) mr##op##_##name
1612 GET (m
, "R.m -> modulus for reduction")
1617 static const const PyMethodDef mpreduce_pymethods
[] = {
1618 #define METHNAME(name) mrmeth_##name
1619 METH (reduce
, "R.reduce(X) -> X mod B.m")
1620 METH (exp
, "R.exp(X, N) -> X^N mod B.m")
1625 static PyTypeObject
*mpreduce_pytype
, mpreduce_pytype_skel
= {
1626 PyObject_HEAD_INIT(0) 0, /* Header */
1627 "MPReduce", /* @tp_name@ */
1628 sizeof(mpreduce_pyobj
), /* @tp_basicsize@ */
1629 0, /* @tp_itemsize@ */
1631 mpreduce_pydealloc
, /* @tp_dealloc@ */
1633 0, /* @tp_getattr@ */
1634 0, /* @tp_setattr@ */
1635 0, /* @tp_compare@ */
1637 0, /* @tp_as_number@ */
1638 0, /* @tp_as_sequence@ */
1639 0, /* @tp_as_mapping@ */
1643 0, /* @tp_getattro@ */
1644 0, /* @tp_setattro@ */
1645 0, /* @tp_as_buffer@ */
1646 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1647 Py_TPFLAGS_BASETYPE
,
1650 "MPReduce(N): a reduction context for reduction modulo Solinas primes.",
1652 0, /* @tp_traverse@ */
1654 0, /* @tp_richcompare@ */
1655 0, /* @tp_weaklistoffset@ */
1657 0, /* @tp_iternext@ */
1658 PYMETHODS(mpreduce
), /* @tp_methods@ */
1659 0, /* @tp_members@ */
1660 PYGETSET(mpreduce
), /* @tp_getset@ */
1663 0, /* @tp_descr_get@ */
1664 0, /* @tp_descr_set@ */
1665 0, /* @tp_dictoffset@ */
1667 PyType_GenericAlloc
, /* @tp_alloc@ */
1668 mpreduce_pynew
, /* @tp_new@ */
1673 /*----- Chinese Remainder Theorem solution --------------------------------*/
1675 typedef struct mpcrt_pyobj
{
1680 #define MPCRT_PY(o) (&((mpcrt_pyobj *)(o))->c)
1682 static PyObject
*mcmeth_solve(PyObject
*me
, PyObject
*arg
)
1684 mpcrt
*c
= MPCRT_PY(me
);
1685 PyObject
*q
= 0, *x
, *z
= 0;
1688 Py_ssize_t i
= 0, n
= c
->k
;
1691 if (PyTuple_GET_SIZE(arg
) == n
)
1693 else if (!PyArg_ParseTuple(arg
, "O:solve", &q
))
1696 if (!PySequence_Check(q
)) TYERR("want a sequence of residues");
1697 i
= PySequence_Size(q
); if (i
< 0) goto end
;
1698 if (i
!= n
) VALERR("residue count mismatch");
1699 v
= xmalloc(n
* sizeof(*v
));
1700 for (i
= 0; i
< n
; i
++) {
1701 if ((x
= PySequence_GetItem(q
, i
)) == 0) goto end
;
1702 xx
= getmp(x
); Py_DECREF(x
); if (!xx
) goto end
;
1705 z
= mp_pywrap(mpcrt_solve(c
, MP_NEW
, v
));
1709 for (i
= 0; i
< n
; i
++)
1718 static void mpcrt_pydealloc(PyObject
*me
)
1720 mpcrt
*c
= MPCRT_PY(me
);
1725 static PyObject
*mpcrt_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1728 Py_ssize_t n
, i
= 0, j
;
1729 static const char *const kwlist
[] = { "mv", 0 };
1730 PyObject
*q
= 0, *x
;
1731 mp
*xx
= MP_NEW
, *y
= MP_NEW
, *g
= MP_NEW
;
1735 if (PyTuple_GET_SIZE(arg
) > 1)
1737 else if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", KWLIST
, &q
))
1740 if (!PySequence_Check(q
)) TYERR("want a sequence of moduli");
1741 n
= PySequence_Size(q
); if (n
< 0) goto end
;
1742 if (!n
) VALERR("want at least one modulus");
1743 v
= xmalloc(n
* sizeof(*v
));
1744 for (i
= 0; i
< n
; i
++) {
1745 if ((x
= PySequence_GetItem(q
, i
)) == 0) goto end
;
1746 xx
= getmp(x
); Py_DECREF(x
); if (!xx
) goto end
;
1747 if (MP_CMP(xx
, <=, MP_ZERO
)) VALERR("moduli must be positive");
1748 v
[i
].m
= xx
; v
[i
].n
= 0; v
[i
].ni
= 0; v
[i
].nni
= 0; xx
= MP_NEW
;
1751 for (j
= 0; j
< i
; j
++) mpmul_add(&mm
, v
[j
].m
);
1752 xx
= mpmul_done(&mm
);
1753 for (j
= 0; j
< i
; j
++) {
1754 mp_div(&y
, 0, xx
, v
[j
].m
);
1755 mp_gcd(&g
, 0, 0, y
, v
[j
].m
);
1756 if (!MP_EQ(g
, MP_ONE
)) VALERR("moduli must be pairwise coprime");
1759 c
= (mpcrt_pyobj
*)ty
->tp_alloc(ty
, 0);
1760 mpcrt_create(&c
->c
, v
, n
, 0);
1762 mp_drop(xx
); mp_drop(y
); mp_drop(g
);
1763 return ((PyObject
*)c
);
1768 for (i
= 0; i
< n
; i
++)
1773 mp_drop(xx
); mp_drop(y
); mp_drop(g
);
1777 static PyObject
*mcget_product(PyObject
*me
, void *hunoz
)
1778 { return (mp_pywrap(MP_COPY(MPCRT_PY(me
)->mb
.m
))); }
1780 static PyObject
*mcget_moduli(PyObject
*me
, void *hunoz
)
1784 mpcrt
*c
= MPCRT_PY(me
);
1786 if ((q
= PyList_New(c
->k
)) == 0) return (0);
1787 for (i
= 0; i
< c
->k
; i
++)
1788 PyList_SET_ITEM(q
, i
, mp_pywrap(c
->v
[i
].m
));
1792 static const PyGetSetDef mpcrt_pygetset
[] = {
1793 #define GETSETNAME(op, name) mc##op##_##name
1794 GET (product
, "C.product -> product of moduli")
1795 GET (moduli
, "C.moduli -> list of individual moduli")
1800 static const PyMethodDef mpcrt_pymethods
[] = {
1801 #define METHNAME(name) mcmeth_##name
1802 METH (solve
, "C.solve([R0, R1]) -> X mod C.product")
1807 static PyTypeObject
*mpcrt_pytype
, mpcrt_pytype_skel
= {
1808 PyObject_HEAD_INIT(0) 0, /* Header */
1809 "MPCRT", /* @tp_name@ */
1810 sizeof(mpcrt_pyobj
), /* @tp_basicsize@ */
1811 0, /* @tp_itemsize@ */
1813 mpcrt_pydealloc
, /* @tp_dealloc@ */
1815 0, /* @tp_getattr@ */
1816 0, /* @tp_setattr@ */
1817 0, /* @tp_compare@ */
1819 0, /* @tp_as_number@ */
1820 0, /* @tp_as_sequence@ */
1821 0, /* @tp_as_mapping@ */
1825 0, /* @tp_getattro@ */
1826 0, /* @tp_setattro@ */
1827 0, /* @tp_as_buffer@ */
1828 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1829 Py_TPFLAGS_BASETYPE
,
1832 "MPCRT(SEQ): a context for solving Chinese Remainder Theorem problems.",
1834 0, /* @tp_traverse@ */
1836 0, /* @tp_richcompare@ */
1837 0, /* @tp_weaklistoffset@ */
1839 0, /* @tp_iternext@ */
1840 PYMETHODS(mpcrt
), /* @tp_methods@ */
1841 0, /* @tp_members@ */
1842 PYGETSET(mpcrt
), /* @tp_getset@ */
1845 0, /* @tp_descr_get@ */
1846 0, /* @tp_descr_set@ */
1847 0, /* @tp_dictoffset@ */
1849 PyType_GenericAlloc
, /* @tp_alloc@ */
1850 mpcrt_pynew
, /* @tp_new@ */
1855 /*----- Binary polynomials ------------------------------------------------*/
1857 static PyObject
*gf_pyrepr(PyObject
*o
)
1858 { return mp_topystring(MP_X(o
), 16, "GF(", "0x", "L)"); }
1860 static PyObject
*gf_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
1866 if (gfbinop(x
, y
, &xx
, &yy
)) RETURN_NOTIMPL
;
1868 case Py_EQ
: b
= MP_EQ(xx
, yy
); break;
1869 case Py_NE
: b
= !MP_EQ(xx
, yy
); break;
1874 case Py_LT
: b
= xl
< yl
; break;
1875 case Py_LE
: b
= xl
<= yl
; break;
1876 case Py_GT
: b
= xl
> yl
; break;
1877 case Py_GE
: b
= xl
>= yl
; break;
1882 MP_DROP(xx
); MP_DROP(yy
);
1883 return (getbool(b
));
1886 static PyObject
*gf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1892 static const char *const kwlist
[] = { "x", "radix", 0 };
1894 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O|i:gf", KWLIST
, &x
, &radix
))
1896 if (GF_PYCHECK(x
)) RETURN_OBJ(x
);
1897 if (!good_radix_p(radix
, 1)) VALERR("radix out of range");
1898 if ((z
= mp_frompyobject(x
, radix
)) == 0) {
1899 PyErr_Format(PyExc_TypeError
, "can't convert %.100s to gf",
1900 x
->ob_type
->tp_name
);
1905 VALERR("gf cannot be negative");
1907 zz
= (mp_pyobj
*)ty
->tp_alloc(ty
, 0);
1910 return ((PyObject
*)zz
);
1913 static PyObject
*gf_pyexp(PyObject
*x
, PyObject
*y
, PyObject
*z
)
1915 mp
*xx
= 0, *yy
= 0, *zz
= 0;
1919 if ((xx
= tomp(x
)) == 0 || (yy
= tomp(y
)) == 0 ||
1920 (z
&& z
!= Py_None
&& (zz
= tomp(z
)) == 0)) {
1921 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
1924 if (!z
|| z
== Py_None
) {
1925 if (MP_NEGP(yy
)) VALERR("negative exponent");
1926 r
= gf_exp(MP_NEW
, xx
, yy
);
1929 if (MP_ZEROP(zz
)) ZDIVERR("zero modulus");
1931 if ((xx
= gf_modinv_checked(xx
, xx
, zz
)) == 0) goto end
;
1932 yy
= mp_neg(yy
, yy
);
1934 gfreduce_create(&gr
, zz
);
1935 r
= gfreduce_exp(&gr
, MP_NEW
, xx
, yy
);
1936 gfreduce_destroy(&gr
);
1940 mp_drop(xx
); mp_drop(yy
); mp_drop(zz
);
1944 static PyObject
*gfmeth_sqr(PyObject
*me
)
1945 { return (gf_pywrap(gf_sqr(MP_NEW
, MP_X(me
)))); }
1947 static PyObject
*gfmeth_gcd(PyObject
*me
, PyObject
*arg
)
1950 mp
*yy
= 0, *zz
= MP_NEW
;
1952 if (!PyArg_ParseTuple(arg
, "O&:gcd", convgf
, &yy
)) goto end
;
1953 gf_gcd(&zz
, 0, 0, MP_X(me
), yy
);
1956 if (yy
) MP_DROP(yy
);
1960 static PyObject
*gfmeth_gcdx(PyObject
*me
, PyObject
*arg
)
1963 mp
*yy
= 0, *zz
= MP_NEW
, *uu
= MP_NEW
, *vv
= MP_NEW
;
1965 if (!PyArg_ParseTuple(arg
, "O&:gcdx", convgf
, &yy
))
1967 gf_gcd(&zz
, &uu
, &vv
, MP_X(me
), yy
);
1968 z
= Py_BuildValue("(NNN)",
1969 gf_pywrap(zz
), gf_pywrap(uu
), gf_pywrap(vv
));
1971 if (yy
) MP_DROP(yy
);
1975 static PyObject
*gfmeth_modinv(PyObject
*me
, PyObject
*arg
)
1978 mp
*yy
= 0, *zz
= MP_NEW
;
1980 if (!PyArg_ParseTuple(arg
, "O&:modinv", convgf
, &yy
) ||
1981 (zz
= gf_modinv_checked(MP_NEW
, yy
, MP_X(me
))) == 0)
1985 if (yy
) MP_DROP(yy
);
1989 static PyObject
*gfmeth_fromstring(PyObject
*me
,
1990 PyObject
*arg
, PyObject
*kw
)
1997 mptext_stringctx sc
;
1998 static const char *const kwlist
[] = { "x", "radix", 0 };
2000 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|i:fromstring", KWLIST
,
2003 if (!good_radix_p(r
, 1)) VALERR("bad radix");
2004 sc
.buf
= p
; sc
.lim
= p
+ len
;
2005 if ((zz
= mp_read(MP_NEW
, r
, &mptext_stringops
, &sc
)) == 0 ||
2007 if (zz
) MP_DROP(zz
);
2008 VALERR("bad binary polynomial");
2010 z
= Py_BuildValue("(Ns#)", gf_pywrap(zz
),
2011 sc
.buf
, (Py_ssize_t
)(sc
.lim
- sc
.buf
));
2016 static PyObject
*gfmeth_irreduciblep(PyObject
*me
)
2017 { return getbool(gf_irreduciblep(MP_X(me
))); }
2019 static PyObject
*gfget_degree(PyObject
*me
, void *hunoz
)
2020 { return (PyInt_FromLong(mp_bits(MP_X(me
)) - 1)); }
2022 static const PyGetSetDef gf_pygetset
[] = {
2023 #define GETSETNAME(op, name) gf##op##_##name
2024 GET (degree
, "X.degree -> polynomial degree of X")
2026 #define GETSETNAME(op, name) mp##op##_##name
2027 GET (nbits
, "X.nbits -> bit length of X")
2028 GET (noctets
, "X.noctets -> octet length of X")
2033 static const PyMethodDef gf_pymethods
[] = {
2034 #define METHNAME(func) gfmeth_##func
2035 METH (setbit
, "X.setbit(N) -> X with bit N set")
2036 METH (clearbit
, "X.clearbit(N) -> X with bit N clear")
2037 METH (testbit
, "X.testbit(N) -> true/false if bit N set/clear in X")
2038 NAMETH(sqr
, "X.sqr() -> X^2")
2039 METH (gcd
, "X.gcd(Y) -> gcd(X, Y)")
2040 METH (gcdx
, "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)")
2041 METH (modinv
, "X.modinv(Y) -> multiplicative inverse of Y mod X")
2042 NAMETH(irreduciblep
, "X.irreduciblep() -> true/false")
2043 KWSMTH(fromstring
, "fromstring(STR, [radix = 0]) -> (X, REST)\n"
2044 " Parse STR as a binary polynomial, according to RADIX. If RADIX is\n"
2045 " zero, read a prefix from STR to decide radix: allow `0b' for binary,\n"
2046 " `0' or `0o' for octal, `0x' for hex, or `R_' for other radix R.")
2047 SMTH (loadl
, "loadl(STR) -> X: read little-endian bytes")
2048 SMTH (loadb
, "loadb(STR) -> X: read big-endian bytes")
2049 SMTH (frombuf
, "frombuf(STR) -> (X, REST): read buffer format")
2051 #define METHNAME(func) mpmeth_##func
2052 KWMETH(tostring
, "X.tostring([radix = 10]) -> STR")
2053 KWMETH(storel
, "X.storel([len = -1]) -> little-endian bytes")
2054 KWMETH(storeb
, "X.storeb([len = -1]) -> big-endian bytes")
2055 KWMETH(storel2c
, "X.storel2c([len = -1]) -> "
2056 "little-endian bytes, two's complement")
2057 KWMETH(storeb2c
, "X.storeb2c([len = -1]) -> "
2058 "big-endian bytes, two's complement")
2059 NAMETH(tobuf
, "X.tobuf() -> buffer format")
2064 static const PyNumberMethods gf_pynumber
= {
2065 gf_pyadd
, /* @nb_add@ */
2066 gf_pysub
, /* @nb_subtract@ */
2067 gf_pymul
, /* @nb_multiply@ */
2068 0, /* @nb_divide@ */
2069 gf_pymod
, /* @nb_remainder@ */
2070 gf_pydivmod
, /* @nb_divmod@ */
2071 gf_pyexp
, /* @nb_power@ */
2072 mp_pyid
, /* @nb_negative@ */
2073 mp_pyid
, /* @nb_positive@ */
2074 mp_pyid
, /* @nb_absolute@ */
2075 mp_pynonzerop
, /* @nb_nonzero@ */
2076 0 /* doesn't make any sense */, /* @nb_invert@ */
2077 gf_pylsl
, /* @nb_lshift@ */
2078 gf_pylsr
, /* @nb_rshift@ */
2079 gf_pyand
, /* @nb_and@ */
2080 gf_pyxor
, /* @nb_xor@ */
2081 gf_pyor
, /* @nb_or@ */
2082 gf_pycoerce
, /* @nb_coerce@ */
2083 mp_pyint
, /* @nb_int@ */
2084 mp_pylong
, /* @nb_long@ */
2085 0 /* doesn't make any sense */, /* @nb_float@ */
2086 mp_pyoct
, /* @nb_oct@ */
2087 mp_pyhex
, /* @nb_hex@ */
2089 0, /* @nb_inplace_add@ */
2090 0, /* @nb_inplace_subtract@ */
2091 0, /* @nb_inplace_multiply@ */
2092 0, /* @nb_inplace_divide@ */
2093 0, /* @nb_inplace_remainder@ */
2094 0, /* @nb_inplace_power@ */
2095 0, /* @nb_inplace_lshift@ */
2096 0, /* @nb_inplace_rshift@ */
2097 0, /* @nb_inplace_and@ */
2098 0, /* @nb_inplace_xor@ */
2099 0, /* @nb_inplace_or@ */
2101 gf_pydiv
, /* @nb_floor_divide@ */
2102 0, /* @nb_true_divide@ */
2103 0, /* @nb_inplace_floor_divide@ */
2104 0, /* @nb_inplace_true_divide@ */
2107 static PyTypeObject gf_pytype_skel
= {
2108 PyObject_HEAD_INIT(0) 0, /* Header */
2109 "GF", /* @tp_name@ */
2110 sizeof(mp_pyobj
), /* @tp_basicsize@ */
2111 0, /* @tp_itemsize@ */
2113 mp_pydealloc
, /* @tp_dealloc@ */
2115 0, /* @tp_getattr@ */
2116 0, /* @tp_setattr@ */
2117 0, /* @tp_compare@ */
2118 gf_pyrepr
, /* @tp_repr@ */
2119 PYNUMBER(gf
), /* @tp_as_number@ */
2120 0, /* @tp_as_sequence@ */
2121 0, /* @tp_as_mapping@ */
2122 mp_pyhash
, /* @tp_hash@ */
2124 mp_pyhex
, /* @tp_str@ */
2125 0, /* @tp_getattro@ */
2126 0, /* @tp_setattro@ */
2127 0, /* @tp_as_buffer@ */
2128 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2129 Py_TPFLAGS_CHECKTYPES
|
2130 Py_TPFLAGS_BASETYPE
,
2133 "Binary polynomials. Support almost all the standard arithmetic\n"
2136 "Constructor GF(X, [radix = R]) attempts to convert X to a `GF'. If\n"
2137 "X is a string, it's read in radix-R form, or we look for a prefix\n"
2138 "if R = 0. Other acceptable things are field elements, elliptic curve\n"
2139 "points, group elements, Python `int' and `long' objects, and anything\n"
2140 "with an integer conversion.\n"
2142 "The name is hopelessly wrong from a technical point of view, but\n"
2143 "but it's much easier to type than `p2' or `c2' or whatever.\n"
2147 " * Use `//' for Euclidean division: `/' gives exact rational division.",
2149 0, /* @tp_traverse@ */
2151 gf_pyrichcompare
, /* @tp_richcompare@ */
2152 0, /* @tp_weaklistoffset@ */
2154 0, /* @tp_iternext@ */
2155 PYMETHODS(gf
), /* @tp_methods@ */
2156 0, /* @tp_members@ */
2157 PYGETSET(gf
), /* @tp_getset@ */
2160 0, /* @tp_descr_get@ */
2161 0, /* @tp_descr_set@ */
2162 0, /* @tp_dictoffset@ */
2164 PyType_GenericAlloc
, /* @tp_alloc@ */
2165 gf_pynew
, /* @tp_new@ */
2170 /*----- Sparse poly reduction ---------------------------------------------*/
2172 typedef struct gfreduce_pyobj
{
2177 #define GFREDUCE_PY(o) (&((gfreduce_pyobj *)(o))->mr)
2179 static PyObject
*grmeth_exp(PyObject
*me
, PyObject
*arg
)
2182 mp
*yy
= 0, *zz
= 0;
2184 if (!PyArg_ParseTuple(arg
, "O&O&:exp", convgf
, &yy
, convgf
, &zz
))
2187 if ((yy
= gf_modinv_checked(yy
, yy
, GFREDUCE_PY(me
)->p
)) == 0) goto end
;
2188 zz
= mp_neg(zz
, zz
);
2190 rc
= gf_pywrap(gfreduce_exp(GFREDUCE_PY(me
), MP_NEW
, yy
, zz
));
2192 if (yy
) MP_DROP(yy
); if (zz
) MP_DROP(zz
);
2196 static PyObject
*grmeth_trace(PyObject
*me
, PyObject
*arg
)
2201 if (!PyArg_ParseTuple(arg
, "O&:trace", convgf
, &xx
)) goto end
;
2202 rc
= PyInt_FromLong(gfreduce_trace(GFREDUCE_PY(me
), xx
));
2204 if (xx
) MP_DROP(xx
);
2208 static PyObject
*grmeth_halftrace(PyObject
*me
, PyObject
*arg
)
2213 if (!PyArg_ParseTuple(arg
, "O&:halftrace", convgf
, &xx
)) goto end
;
2214 rc
= gf_pywrap(gfreduce_halftrace(GFREDUCE_PY(me
), MP_NEW
, xx
));
2216 if (xx
) MP_DROP(xx
);
2220 static PyObject
*grmeth_sqrt(PyObject
*me
, PyObject
*arg
)
2225 if (!PyArg_ParseTuple(arg
, "O&:sqrt", convgf
, &xx
)) goto end
;
2226 if ((yy
= gfreduce_sqrt(GFREDUCE_PY(me
), MP_NEW
, xx
)) == 0)
2227 VALERR("no modular square root");
2230 if (xx
) MP_DROP(xx
);
2234 static PyObject
*grmeth_quadsolve(PyObject
*me
, PyObject
*arg
)
2239 if (!PyArg_ParseTuple(arg
, "O&:quadsolve", convgf
, &xx
)) goto end
;
2240 if ((yy
= gfreduce_quadsolve(GFREDUCE_PY(me
), MP_NEW
, xx
)) == 0)
2241 VALERR("no solution found");
2244 if (xx
) MP_DROP(xx
);
2248 static PyObject
*grmeth_reduce(PyObject
*me
, PyObject
*arg
)
2253 if (!PyArg_ParseTuple(arg
, "O&:reduce", convgf
, &yy
)) goto end
;
2254 z
= gf_pywrap(gfreduce_do(GFREDUCE_PY(me
), MP_NEW
, yy
));
2259 static void gfreduce_pydealloc(PyObject
*me
)
2261 gfreduce_destroy(GFREDUCE_PY(me
));
2265 static PyObject
*gfreduce_pynew(PyTypeObject
*ty
,
2266 PyObject
*arg
, PyObject
*kw
)
2268 gfreduce_pyobj
*mr
= 0;
2270 static const char *const kwlist
[] = { "m", 0 };
2273 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&:new", KWLIST
, convgf
, &xx
))
2275 if (MP_ZEROP(xx
)) ZDIVERR("modulus is zero!");
2276 gfreduce_create(&r
, xx
);
2277 mr
= (gfreduce_pyobj
*)ty
->tp_alloc(ty
, 0);
2280 if (xx
) MP_DROP(xx
);
2281 return ((PyObject
*)mr
);
2284 static PyObject
*grget_m(PyObject
*me
, void *hunoz
)
2285 { return (gf_pywrap(MP_COPY(GFREDUCE_PY(me
)->p
))); }
2287 static const PyGetSetDef gfreduce_pygetset
[] = {
2288 #define GETSETNAME(op, name) gr##op##_##name
2289 GET (m
, "R.m -> reduction polynomial")
2294 static const PyMethodDef gfreduce_pymethods
[] = {
2295 #define METHNAME(name) grmeth_##name
2296 METH (reduce
, "R.reduce(X) -> X mod B.m")
2297 METH (trace
, "R.trace(X) -> Tr(X) = x + x^2 + ... + x^{2^{m - 1}}")
2298 METH (halftrace
, "R.halftrace(X) -> x + x^{2^2} + ... + x^{2^{m - 1}}")
2299 METH (sqrt
, "R.sqrt(X) -> Y where Y^2 = X mod R")
2300 METH (quadsolve
, "R.quadsolve(X) -> Y where Y^2 + Y = X mod R")
2301 METH (exp
, "R.exp(X, N) -> X^N mod B.m")
2306 static PyTypeObject
*gfreduce_pytype
, gfreduce_pytype_skel
= {
2307 PyObject_HEAD_INIT(0) 0, /* Header */
2308 "GFReduce", /* @tp_name@ */
2309 sizeof(gfreduce_pyobj
), /* @tp_basicsize@ */
2310 0, /* @tp_itemsize@ */
2312 gfreduce_pydealloc
, /* @tp_dealloc@ */
2314 0, /* @tp_getattr@ */
2315 0, /* @tp_setattr@ */
2316 0, /* @tp_compare@ */
2318 0, /* @tp_as_number@ */
2319 0, /* @tp_as_sequence@ */
2320 0, /* @tp_as_mapping@ */
2324 0, /* @tp_getattro@ */
2325 0, /* @tp_setattro@ */
2326 0, /* @tp_as_buffer@ */
2327 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2328 Py_TPFLAGS_BASETYPE
,
2331 "GFReduce(N): a context for reduction modulo sparse polynomials.",
2333 0, /* @tp_traverse@ */
2335 0, /* @tp_richcompare@ */
2336 0, /* @tp_weaklistoffset@ */
2338 0, /* @tp_iternext@ */
2339 PYMETHODS(gfreduce
), /* @tp_methods@ */
2340 0, /* @tp_members@ */
2341 PYGETSET(gfreduce
), /* @tp_getset@ */
2344 0, /* @tp_descr_get@ */
2345 0, /* @tp_descr_set@ */
2346 0, /* @tp_dictoffset@ */
2348 PyType_GenericAlloc
, /* @tp_alloc@ */
2349 gfreduce_pynew
, /* @tp_new@ */
2354 /*----- Normal/poly transformation ----------------------------------------*/
2356 typedef struct gfn_pyobj
{
2362 static PyTypeObject
*gfn_pytype
, gfn_pytype_skel
;
2364 #define GFN_P(o) (((gfn_pyobj *)(o))->p)
2365 #define GFN_PTON(o) (&((gfn_pyobj *)(o))->pton)
2366 #define GFN_NTOP(o) (&((gfn_pyobj *)(o))->ntop)
2368 static PyObject
*gfn_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2370 mp
*p
= 0, *beta
= 0;
2372 static const char *const kwlist
[] = { "p", "beta", 0 };
2374 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&:new", KWLIST
,
2375 convgf
, &p
, convgf
, &beta
))
2377 gg
= PyObject_New(gfn_pyobj
, ty
);
2379 if (gfn_create(p
, beta
, &gg
->ntop
, &gg
->pton
)) {
2382 VALERR("can't invert transformation matrix");
2388 return ((PyObject
*)gg
);
2391 static PyObject
*gfnget_p(PyObject
*me
, void *hunoz
)
2392 { return (gf_pywrap(MP_COPY(GFN_P(me
)))); }
2394 static PyObject
*gfnget_beta(PyObject
*me
, void *hunoz
)
2396 gfn
*n
= GFN_NTOP(me
);
2397 mp
*x
= n
->r
[n
->n
- 1];
2398 return (gf_pywrap(MP_COPY(x
)));
2401 #define XFORMOP(name, NAME) \
2402 static PyObject *gfnmeth_##name(PyObject *me, PyObject *arg) \
2407 if (!PyArg_ParseTuple(arg, "O&:" #name, convgf, &xx)) goto end; \
2408 z = gfn_transform(GFN_##NAME(me), MP_NEW, xx); \
2411 if (!z) return (0); \
2412 return (gf_pywrap(z)); \
2418 static void gfn_pydealloc(PyObject
*me
)
2422 gfn_destroy(GFN_PTON(me
));
2423 gfn_destroy(GFN_NTOP(me
));
2428 static const PyGetSetDef gfn_pygetset
[] = {
2429 #define GETSETNAME(op, name) gfn##op##_##name
2430 GET (p
, "X.p -> polynomial basis, as polynomial")
2431 GET (beta
, "X.beta -> normal basis element, in poly form")
2436 static const PyMethodDef gfn_pymethods
[] = {
2437 #define METHNAME(name) gfnmeth_##name
2438 METH (pton
, "X.pton(A) -> normal-basis representation of A")
2439 METH (ntop
, "X.ntop(A) -> polynomial-basis representation of A")
2444 static PyTypeObject gfn_pytype_skel
= {
2445 PyObject_HEAD_INIT(0) 0, /* Header */
2446 "GFN", /* @tp_name@ */
2447 sizeof(gfn_pyobj
), /* @tp_basicsize@ */
2448 0, /* @tp_itemsize@ */
2450 gfn_pydealloc
, /* @tp_dealloc@ */
2452 0, /* @tp_getattr@ */
2453 0, /* @tp_setattr@ */
2454 0, /* @tp_compare@ */
2456 0, /* @tp_as_number@ */
2457 0, /* @tp_as_sequence@ */
2458 0, /* @tp_as_mapping@ */
2462 0, /* @tp_getattro@ */
2463 0, /* @tp_setattro@ */
2464 0, /* @tp_as_buffer@ */
2465 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2466 Py_TPFLAGS_BASETYPE
,
2469 "GFN(P, BETA): an object for transforming elements of binary fields\n"
2470 " between polynomial and normal basis representations.",
2472 0, /* @tp_traverse@ */
2474 0, /* @tp_richcompare@ */
2475 0, /* @tp_weaklistoffset@ */
2477 0, /* @tp_iternext@ */
2478 PYMETHODS(gfn
), /* @tp_methods@ */
2479 0, /* @tp_members@ */
2480 PYGETSET(gfn
), /* @tp_getset@ */
2483 0, /* @tp_descr_get@ */
2484 0, /* @tp_descr_set@ */
2485 0, /* @tp_dictoffset@ */
2487 PyType_GenericAlloc
, /* @tp_alloc@ */
2488 gfn_pynew
, /* @tp_new@ */
2493 /*----- Glue --------------------------------------------------------------*/
2495 void mp_pyinit(void)
2499 INITTYPE(mpmul
, root
);
2500 INITTYPE(mpmont
, root
);
2501 INITTYPE(mpbarrett
, root
);
2502 INITTYPE(mpreduce
, root
);
2503 INITTYPE(mpcrt
, root
);
2504 INITTYPE(gfreduce
, root
);
2505 INITTYPE(gfn
, root
);
2508 void mp_pyinsert(PyObject
*mod
)
2510 INSERT("MP", mp_pytype
);
2511 INSERT("MPMul", mpmul_pytype
);
2512 INSERT("MPMont", mpmont_pytype
);
2513 INSERT("MPBarrett", mpbarrett_pytype
);
2514 INSERT("MPReduce", mpreduce_pytype
);
2515 INSERT("MPCRT", mpcrt_pytype
);
2516 INSERT("GF", gf_pytype
);
2517 INSERT("GFReduce", gfreduce_pytype
);
2518 INSERT("GFN", gfn_pytype
);
2521 /*----- That's all, folks -------------------------------------------------*/