Commit | Line | Data |
---|---|---|
d7ab1bab | 1 | /* -*-c-*- |
2 | * | |
d7ab1bab | 3 | * Multiprecision arithmetic |
4 | * | |
5 | * (c) 2004 Straylight/Edgeware | |
6 | */ | |
7 | ||
b2687a0a | 8 | /*----- Licensing notice --------------------------------------------------* |
d7ab1bab | 9 | * |
10 | * This file is part of the Python interface to Catacomb. | |
11 | * | |
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. | |
b2687a0a | 16 | * |
d7ab1bab | 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. | |
b2687a0a | 21 | * |
d7ab1bab | 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. | |
25 | */ | |
26 | ||
27 | /*----- Header files ------------------------------------------------------*/ | |
28 | ||
29 | #include "catacomb-python.h" | |
30 | ||
31 | /*----- General utilities -------------------------------------------------*/ | |
32 | ||
33 | PyTypeObject *mp_pytype = 0; | |
34 | PyTypeObject *gf_pytype = 0; | |
35 | ||
31a298bb MW |
36 | #ifndef PyLong_SHIFT |
37 | # define PyLong_SHIFT SHIFT | |
38 | #endif | |
39 | ||
40 | #ifndef PyLong_MASK | |
41 | # define PyLong_MASK MASK | |
42 | #endif | |
43 | ||
44 | STATIC_ASSERT(MPW_BITS >= PyLong_SHIFT, | |
ff1993c7 MW |
45 | "Catacomb's limbs are now narrower than than Python's!"); |
46 | ||
f368b46e | 47 | mp *mp_frompylong(PyObject *obj) |
d7ab1bab | 48 | { |
49 | unsigned long bits; | |
f368b46e | 50 | PyLongObject *l = (PyLongObject *)obj; |
d7ab1bab | 51 | int sz; |
52 | size_t w; | |
53 | mpd r = 0; | |
54 | int b = 0; | |
55 | int i; | |
56 | mp *x; | |
57 | mpw *p; | |
58 | ||
f984b31a | 59 | sz = Py_SIZE(l); |
d7ab1bab | 60 | if (sz < 0) sz = -sz; |
31a298bb | 61 | bits = (unsigned long)sz * PyLong_SHIFT; |
d7ab1bab | 62 | w = (bits + MPW_BITS - 1)/MPW_BITS; |
f984b31a | 63 | x = mp_new(w, Py_SIZE(l) < 0 ? MP_NEG : 0); |
d7ab1bab | 64 | p = x->v; |
65 | for (i = 0; i < sz; i++) { | |
66 | r |= (mpd)l->ob_digit[i] << b; | |
31a298bb | 67 | b += PyLong_SHIFT; |
d7ab1bab | 68 | while (b >= MPW_BITS) { |
69 | *p++ = MPW(r); | |
70 | r >>= MPW_BITS; | |
71 | b -= MPW_BITS; | |
72 | } | |
73 | } | |
74 | while (r) { | |
75 | *p++ = MPW(r); | |
76 | r >>= MPW_BITS; | |
77 | } | |
78 | x->vl = p; | |
79 | MP_SHRINK(x); | |
80 | return (x); | |
81 | } | |
82 | ||
f368b46e | 83 | PyObject *mp_topylong(mp *x) |
d7ab1bab | 84 | { |
85 | unsigned long bits = mp_bits(x); | |
31a298bb | 86 | int sz = (bits + PyLong_SHIFT - 1)/PyLong_SHIFT; |
d7ab1bab | 87 | PyLongObject *l = _PyLong_New(sz); |
88 | mpd r = 0; | |
89 | int b = 0; | |
90 | mpw *p = x->v; | |
91 | int i = 0; | |
92 | ||
d7ab1bab | 93 | while (i < sz && p < x->vl) { |
f368b46e | 94 | r |= (mpd)*p++ << b; |
d7ab1bab | 95 | b += MPW_BITS; |
31a298bb MW |
96 | while (i < sz && b >= PyLong_SHIFT) { |
97 | l->ob_digit[i++] = r & PyLong_MASK; | |
98 | r >>= PyLong_SHIFT; | |
99 | b -= PyLong_SHIFT; | |
d7ab1bab | 100 | } |
101 | } | |
102 | while (i < sz && r) { | |
31a298bb MW |
103 | l->ob_digit[i++] = r & PyLong_MASK; |
104 | r >>= PyLong_SHIFT; | |
d7ab1bab | 105 | } |
f984b31a | 106 | Py_SIZE(l) = (x->f & MP_NEG) ? -sz : sz; |
f368b46e | 107 | return ((PyObject *)l); |
d7ab1bab | 108 | } |
109 | ||
110 | mp *mp_frompyobject(PyObject *o, int radix) | |
111 | { | |
112 | mp *x; | |
113 | ||
cfb291f0 | 114 | if (TEXT_CHECK(o)) { |
d7ab1bab | 115 | mptext_stringctx sc; |
116 | mp *x; | |
cfb291f0 MW |
117 | size_t sz; |
118 | TEXT_PTRLEN(o, sc.buf, sz); sc.lim = sc.buf + sz; | |
d7ab1bab | 119 | x = mp_read(MP_NEW, radix, &mptext_stringops, &sc); |
120 | if (!x) return (0); | |
121 | if (sc.buf < sc.lim) { MP_DROP(x); return (0); } | |
122 | return (x); | |
123 | } | |
f368b46e MW |
124 | if ((x = tomp(o)) != 0) |
125 | return (x); | |
d7ab1bab | 126 | return (0); |
127 | } | |
128 | ||
129 | PyObject *mp_topystring(mp *x, int radix, const char *xpre, | |
130 | const char *pre, const char *post) | |
131 | { | |
132 | int len = mptext_len(x, radix) + 1; | |
133 | mptext_stringctx sc; | |
134 | PyObject *o; | |
135 | size_t xprelen = xpre ? strlen(xpre) : 0; | |
136 | size_t prelen = pre ? strlen(pre) : 0; | |
137 | size_t postlen = post ? strlen(post) : 0; | |
138 | char *p; | |
139 | MP_COPY(x); | |
cfb291f0 | 140 | TEXT_PREPAREWRITE(o, p, len + 1 + xprelen + prelen + postlen); |
d7ab1bab | 141 | sc.buf = p; |
142 | if (xpre) { memcpy(sc.buf, xpre, xprelen); sc.buf += xprelen; } | |
143 | if (MP_NEGP(x)) { *sc.buf++ = '-'; x = mp_neg(x, x); } | |
144 | if (pre) { memcpy(sc.buf, pre, prelen); sc.buf += prelen; } | |
145 | sc.lim = sc.buf + len; | |
146 | mp_write(x, radix, &mptext_stringops, &sc); | |
147 | if (post) { memcpy(sc.buf, post, postlen); sc.buf += postlen; } | |
148 | MP_DROP(x); | |
cfb291f0 | 149 | TEXT_DONEWRITE(o, sc.buf - p); |
d7ab1bab | 150 | return (o); |
151 | } | |
152 | ||
f368b46e MW |
153 | static int good_radix_p(int r, int readp) |
154 | { | |
155 | return ((r >= -255 && r <= -2) || | |
156 | (readp && r == 0) || | |
157 | (r >= 2 && r <= 62)); | |
158 | } | |
159 | ||
d7ab1bab | 160 | PyObject *mp_pywrap(mp *x) |
161 | { | |
162 | mp_pyobj *z = PyObject_New(mp_pyobj, mp_pytype); | |
163 | z->x = x; | |
164 | return ((PyObject *)z); | |
165 | } | |
166 | ||
167 | PyObject *gf_pywrap(mp *x) | |
168 | { | |
169 | mp_pyobj *z = PyObject_New(mp_pyobj, gf_pytype); | |
170 | z->x = x; | |
171 | return ((PyObject *)z); | |
172 | } | |
173 | ||
bc243788 | 174 | int mp_tolong_checked(mp *x, long *l, int must) |
d7ab1bab | 175 | { |
176 | static mp *longmin = 0, *longmax = 0; | |
177 | int rc = -1; | |
178 | ||
179 | if (!longmax) { | |
180 | longmin = mp_fromlong(MP_NEW, LONG_MIN); | |
181 | longmax = mp_fromlong(MP_NEW, LONG_MAX); | |
182 | } | |
bc243788 MW |
183 | if (MP_CMP(x, <, longmin) || MP_CMP(x, >, longmax)) { |
184 | if (must) VALERR("mp out of range for int"); | |
185 | else goto end; | |
186 | } | |
d7ab1bab | 187 | *l = mp_tolong(x); |
188 | rc = 0; | |
189 | end: | |
190 | return (rc); | |
191 | } | |
192 | ||
193 | /*----- Python interface --------------------------------------------------*/ | |
194 | ||
195 | static void mp_pydealloc(PyObject *o) | |
196 | { | |
197 | MP_DROP(MP_X(o)); | |
3aa33042 | 198 | FREEOBJ(o); |
d7ab1bab | 199 | } |
200 | ||
201 | static PyObject *mp_pyrepr(PyObject *o) | |
202 | { return mp_topystring(MP_X(o), 10, "MP(", 0, "L)"); } | |
203 | ||
204 | static PyObject *mp_pystr(PyObject *o) | |
205 | { return mp_topystring(MP_X(o), 10, 0, 0, 0); } | |
206 | ||
207 | mp *tomp(PyObject *o) | |
208 | { | |
209 | PyObject *l; | |
210 | mp *x; | |
211 | ||
1810cc51 | 212 | if (!o || PyFloat_Check(o)) |
d7ab1bab | 213 | return (0); |
214 | else if (MP_PYCHECK(o) || GF_PYCHECK(o)) | |
215 | return (MP_COPY(MP_X(o))); | |
216 | else if (FE_PYCHECK(o)) | |
217 | return (F_OUT(FE_F(o), MP_NEW, FE_X(o))); | |
218 | else if (PFILT_PYCHECK(o)) | |
219 | return (MP_COPY(PFILT_F(o)->m)); | |
220 | else if (ECPT_PYCHECK(o)) { | |
221 | ec p = EC_INIT; | |
46c13d77 | 222 | if (EC_ATINF(ECPT_P(o))) return (0); |
d7ab1bab | 223 | getecptout(&p, o); |
224 | x = MP_COPY(p.x); | |
225 | EC_DESTROY(&p); | |
226 | return (x); | |
227 | } else if (GE_PYCHECK(o)) { | |
228 | if ((x = G_TOINT(GE_G(o), MP_NEW, GE_X(o))) == 0) | |
229 | return (0); | |
230 | return (x); | |
231 | } else if (PyInt_Check(o)) | |
232 | return (mp_fromlong(MP_NEW, PyInt_AS_LONG(o))); | |
233 | else if ((l = PyNumber_Long(o)) != 0) { | |
f368b46e | 234 | x = mp_frompylong(l); |
d7ab1bab | 235 | Py_DECREF(l); |
236 | return (x); | |
237 | } else { | |
238 | PyErr_Clear(); | |
239 | return (0); | |
240 | } | |
241 | } | |
242 | ||
243 | mp *getmp(PyObject *o) | |
244 | { | |
245 | mp *x = 0; | |
246 | if (!o) return (0); | |
247 | if ((x = tomp(o)) == 0) { | |
248 | PyErr_Format(PyExc_TypeError, "can't convert %.100s to mp", | |
f984b31a | 249 | Py_TYPE(o)->tp_name); |
d7ab1bab | 250 | } |
251 | return (x); | |
252 | } | |
253 | ||
254 | int convmp(PyObject *o, void *p) | |
255 | { | |
256 | mp *x; | |
257 | if ((x = getmp(o)) == 0) return (0); | |
258 | *(mp **)p = x; | |
259 | return (1); | |
260 | } | |
261 | ||
262 | mp *getgf(PyObject *o) | |
263 | { | |
264 | mp *x = 0; | |
265 | if (!o) return (0); | |
266 | if ((x = tomp(o)) == 0) { | |
267 | PyErr_Format(PyExc_TypeError, "can't convert %.100s to gf", | |
f984b31a | 268 | Py_TYPE(o)->tp_name); |
d7ab1bab | 269 | } |
270 | return (x); | |
271 | } | |
272 | ||
273 | int convgf(PyObject *o, void *p) | |
274 | { | |
275 | mp *x; | |
276 | if ((x = getgf(o)) == 0) return (0); | |
277 | *(mp **)p = x; | |
278 | return (1); | |
279 | } | |
280 | ||
bf8910bd MW |
281 | static mp *implicitmp(PyObject *o) |
282 | { | |
283 | if (!o || | |
284 | GF_PYCHECK(o) || | |
285 | ECPT_PYCHECK(o) || | |
286 | FE_PYCHECK(o) || | |
287 | GE_PYCHECK(o)) | |
288 | return (0); | |
289 | return (tomp(o)); | |
290 | } | |
291 | ||
292 | static mp *implicitgf(PyObject *o) | |
293 | { | |
294 | if (!o || | |
295 | MP_PYCHECK(o) || | |
296 | ECPT_PYCHECK(o) || | |
297 | FE_PYCHECK(o) || | |
298 | GE_PYCHECK(o)) | |
299 | return (0); | |
300 | return (tomp(o)); | |
301 | } | |
302 | ||
d7ab1bab | 303 | static int mpbinop(PyObject *x, PyObject *y, mp **xx, mp **yy) |
304 | { | |
bf8910bd MW |
305 | if ((*xx = implicitmp(x)) == 0) |
306 | return (-1); | |
307 | if ((*yy = implicitmp(y)) == 0) { | |
308 | MP_DROP(*xx); | |
309 | return (-1); | |
310 | } | |
311 | return (0); | |
312 | } | |
313 | ||
314 | static int gfbinop(PyObject *x, PyObject *y, mp **xx, mp **yy) | |
315 | { | |
316 | if ((*xx = implicitgf(x)) == 0) | |
d7ab1bab | 317 | return (-1); |
bf8910bd | 318 | if ((*yy = implicitgf(y)) == 0) { |
d7ab1bab | 319 | MP_DROP(*xx); |
320 | return (-1); | |
321 | } | |
322 | return (0); | |
323 | } | |
324 | ||
1810cc51 MW |
325 | #define FPBINOP(name, pyop) \ |
326 | static PyObject *mp_py##name(PyObject *x, PyObject *y) { \ | |
327 | mp *xx, *yy, *zz; \ | |
328 | PyObject *l, *rc; \ | |
329 | if (PyFloat_Check(x)) { \ | |
330 | l = mp_topylong(MP_X(y)); rc = PyNumber_##pyop(x, l); \ | |
331 | Py_DECREF(l); return (rc); \ | |
332 | } else if (PyFloat_Check(y)) { \ | |
333 | l = mp_topylong(MP_X(x)); rc = PyNumber_##pyop(l, y); \ | |
334 | Py_DECREF(l); return (rc); \ | |
335 | } \ | |
336 | if (mpbinop(x, y, &xx, &yy)) RETURN_NOTIMPL; \ | |
337 | zz = mp_##name(MP_NEW, xx, yy); \ | |
338 | MP_DROP(xx); MP_DROP(yy); \ | |
339 | return (mp_pywrap(zz)); \ | |
340 | } | |
341 | FPBINOP(add, Add) | |
342 | FPBINOP(sub, Subtract) | |
343 | FPBINOP(mul, Multiply) | |
344 | ||
d7ab1bab | 345 | #define gf_and mp_and |
346 | #define gf_or mp_or | |
347 | #define gf_xor mp_xor | |
348 | #define BINOP(pre, name) \ | |
349 | static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \ | |
350 | mp *xx, *yy, *zz; \ | |
bf8910bd | 351 | if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \ |
d7ab1bab | 352 | zz = pre##_##name(MP_NEW, xx, yy); \ |
353 | MP_DROP(xx); MP_DROP(yy); \ | |
354 | return (pre##_pywrap(zz)); \ | |
355 | } | |
d7ab1bab | 356 | BINOP(mp, and2c) |
357 | BINOP(mp, or2c) | |
358 | BINOP(mp, xor2c) | |
359 | BINOP(gf, add) | |
360 | BINOP(gf, sub) | |
361 | BINOP(gf, mul) | |
362 | BINOP(gf, and) | |
363 | BINOP(gf, or) | |
364 | BINOP(gf, xor) | |
365 | #undef BINOP | |
366 | ||
367 | static mp *mp_abs(mp *d, mp *x) | |
368 | { | |
369 | if (MP_NEGP(x)) | |
370 | return (mp_neg(d, x)); | |
371 | MP_COPY(x); | |
372 | if (d) MP_DROP(d); | |
373 | return (x); | |
374 | } | |
375 | ||
376 | #define UNOP(pre, name) \ | |
377 | static PyObject *pre##_py##name(PyObject *x) \ | |
378 | { return mp_pywrap(pre##_##name(MP_NEW, MP_X(x))); } | |
379 | UNOP(mp, neg) | |
380 | UNOP(mp, abs) | |
381 | UNOP(mp, not2c) | |
382 | #undef UNOP | |
383 | ||
384 | static PyObject *mp_pyid(PyObject *x) { RETURN_OBJ(x); } | |
385 | ||
386 | #define gf_lsr mp_lsr | |
387 | #define SHIFTOP(pre, name, rname) \ | |
388 | static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \ | |
389 | mp *xx, *yy; \ | |
390 | PyObject *z = 0; \ | |
391 | long n; \ | |
bf8910bd | 392 | if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \ |
bc243788 | 393 | if (mp_tolong_checked(yy, &n, 1)) goto end; \ |
d7ab1bab | 394 | if (n < 0) \ |
395 | z = pre##_pywrap(mp_##rname(MP_NEW, xx, -n)); \ | |
396 | else \ | |
397 | z = pre##_pywrap(mp_##name(MP_NEW, xx, n)); \ | |
398 | end: \ | |
399 | MP_DROP(xx); MP_DROP(yy); \ | |
400 | return (z); \ | |
401 | } | |
402 | SHIFTOP(mp, lsl2c, lsr2c) | |
403 | SHIFTOP(mp, lsr2c, lsl2c) | |
404 | SHIFTOP(gf, lsl, lsr) | |
405 | SHIFTOP(gf, lsr, lsl) | |
406 | #undef SHIFTOP | |
407 | ||
408 | #define DIVOP(pre, name, qq, rr, gather) \ | |
409 | static PyObject *pre##_py##name(PyObject *x, PyObject *y) { \ | |
410 | mp *xx, *yy; \ | |
411 | PyObject *z = 0; \ | |
412 | INIT_##qq(q) INIT_##rr(r) \ | |
bf8910bd | 413 | if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL; \ |
d7ab1bab | 414 | if (MP_ZEROP(yy)) \ |
415 | ZDIVERR("division by zero"); \ | |
416 | pre##_div(ARG_##qq(q), ARG_##rr(r), xx, yy); \ | |
417 | z = gather; \ | |
418 | end: \ | |
419 | MP_DROP(xx); MP_DROP(yy); \ | |
420 | return (z); \ | |
421 | } | |
422 | #define INIT_YES(p) mp *p = MP_NEW; | |
423 | #define INIT_NO(p) | |
424 | #define ARG_YES(p) &p | |
425 | #define ARG_NO(p) 0 | |
426 | DIVOP(mp, divmod, YES, YES, | |
427 | Py_BuildValue("(NN)", mp_pywrap(q), mp_pywrap(r))) | |
428 | DIVOP(mp, div, YES, NO, mp_pywrap(q)) | |
429 | DIVOP(mp, mod, NO, YES, mp_pywrap(r)) | |
430 | DIVOP(gf, divmod, YES, YES, | |
431 | Py_BuildValue("(NN)", gf_pywrap(q), gf_pywrap(r))) | |
432 | DIVOP(gf, div, YES, NO, gf_pywrap(q)) | |
433 | DIVOP(gf, mod, NO, YES, gf_pywrap(r)) | |
434 | #undef INIT_YES | |
435 | #undef INIT_NO | |
436 | #undef ARG_YES | |
437 | #undef ARG_NO | |
438 | #undef DIVOP | |
439 | ||
440 | static mp *mp_modinv_checked(mp *d, mp *x, mp *p) | |
441 | { | |
442 | mp *g = MP_NEW; | |
443 | mp_gcd(&g, 0, &d, p, x); | |
444 | if (!MP_EQ(g, MP_ONE)) { | |
445 | MP_DROP(g); MP_DROP(d); | |
446 | PyErr_SetString(PyExc_ZeroDivisionError, "no modular inverse"); | |
447 | return (0); | |
448 | } | |
449 | MP_DROP(g); return (d); | |
450 | } | |
451 | ||
452 | static PyObject *mp_pyexp(PyObject *x, PyObject *y, PyObject *z) | |
453 | { | |
454 | mp *xx = 0, *yy = 0, *zz = 0; | |
455 | mp *r = 0; | |
456 | PyObject *rc = 0; | |
457 | ||
bf8910bd | 458 | if ((xx = implicitmp(x)) == 0 || (yy = implicitmp(y)) == 0 || |
d7ab1bab | 459 | (z && z != Py_None && (zz = tomp(z)) == 0)) { |
460 | mp_drop(xx); mp_drop(yy); mp_drop(zz); | |
461 | RETURN_NOTIMPL; | |
462 | } | |
463 | if (!z || z == Py_None) { | |
464 | if (MP_NEGP(yy)) VALERR("negative exponent"); | |
465 | r = mp_exp(MP_NEW, xx, yy); | |
466 | } else { | |
467 | if (!MP_POSP(zz)) VALERR("modulus must be positive"); | |
468 | if (MP_NEGP(yy)) { | |
469 | if ((xx = mp_modinv_checked(xx, xx, zz)) == 0) goto end; | |
470 | yy = mp_neg(yy, yy); | |
471 | } | |
472 | if (MP_ODDP(zz)) { | |
473 | mpmont mm; | |
474 | mpmont_create(&mm, zz); | |
475 | r = mpmont_exp(&mm, MP_NEW, xx, yy); | |
476 | mpmont_destroy(&mm); | |
477 | } else { | |
478 | mpbarrett mb; | |
479 | mpbarrett_create(&mb, zz); | |
480 | r = mpbarrett_exp(&mb, MP_NEW, xx, yy); | |
481 | mpbarrett_destroy(&mb); | |
482 | } | |
483 | } | |
484 | rc = mp_pywrap(r); | |
485 | end: | |
486 | mp_drop(xx); mp_drop(yy); mp_drop(zz); | |
487 | return (rc); | |
488 | } | |
489 | ||
490 | static mp *gf_modinv_checked(mp *d, mp *x, mp *p) | |
491 | { | |
492 | mp *g = MP_NEW; | |
493 | gf_gcd(&g, 0, &d, p, x); | |
494 | if (!MP_EQ(g, MP_ONE)) { | |
495 | MP_DROP(g); MP_DROP(d); | |
496 | PyErr_SetString(PyExc_ZeroDivisionError, "no modular inverse"); | |
497 | return (0); | |
498 | } | |
499 | MP_DROP(g); return (d); | |
500 | } | |
501 | ||
502 | #define BASEOP(name, radix, pre) \ | |
503 | static PyObject *mp_py##name(PyObject *x) \ | |
504 | { return mp_topystring(MP_X(x), radix, 0, pre, 0); } | |
505 | BASEOP(oct, 8, "0"); | |
506 | BASEOP(hex, 16, "0x"); | |
507 | #undef BASEOP | |
508 | ||
509 | static int mp_pynonzerop(PyObject *x) { return !MP_ZEROP(MP_X(x)); } | |
510 | ||
511 | static PyObject *mp_pyint(PyObject *x) | |
512 | { | |
513 | long l; | |
bc243788 MW |
514 | if (!mp_tolong_checked(MP_X(x), &l, 0)) return (PyInt_FromLong(l)); |
515 | else return mp_topylong(MP_X(x)); | |
d7ab1bab | 516 | } |
517 | static PyObject *mp_pylong(PyObject *x) | |
f368b46e | 518 | { return (mp_topylong(MP_X(x))); } |
d7ab1bab | 519 | static PyObject *mp_pyfloat(PyObject *x) |
520 | { | |
f368b46e | 521 | PyObject *l = mp_topylong(MP_X(x)); |
d7ab1bab | 522 | double f = PyLong_AsDouble(l); |
523 | Py_DECREF(l); | |
524 | return (PyFloat_FromDouble(f)); | |
525 | } | |
526 | ||
b2687a0a | 527 | #define COERCE(pre, PRE) \ |
d7ab1bab | 528 | static int pre##_pycoerce(PyObject **x, PyObject **y) \ |
529 | { \ | |
530 | mp *z; \ | |
531 | \ | |
532 | if (PRE##_PYCHECK(*y)) { \ | |
533 | Py_INCREF(*x); Py_INCREF(*y); \ | |
534 | return (0); \ | |
535 | } \ | |
536 | if ((z = tomp(*y)) != 0) { \ | |
537 | Py_INCREF(*x); \ | |
538 | *y = pre##_pywrap(z); \ | |
539 | return (0); \ | |
540 | } \ | |
541 | return (1); \ | |
542 | } | |
543 | COERCE(mp, MP) | |
544 | COERCE(gf, GF) | |
545 | #undef COERCE | |
546 | ||
1810cc51 MW |
547 | static PyObject *mp_pyrichcompare(PyObject *x, PyObject *y, int op) |
548 | { | |
549 | mp *xx, *yy; | |
550 | PyObject *l, *rc; | |
551 | if (PyFloat_Check(y)) { | |
552 | l = mp_topylong(MP_X(x)); rc = PyObject_RichCompare(l, y, op); | |
553 | Py_DECREF(l); return (rc); | |
554 | } | |
555 | if (mpbinop(x, y, &xx, &yy)) RETURN_NOTIMPL; | |
556 | rc = enrich_compare(op, mp_cmp(xx, yy)); | |
557 | MP_DROP(xx); MP_DROP(yy); | |
558 | return (rc); | |
559 | } | |
560 | ||
d7ab1bab | 561 | static int mp_pycompare(PyObject *x, PyObject *y) |
562 | { return mp_cmp(MP_X(x), MP_X(y)); } | |
563 | ||
564 | static PyObject *mp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
565 | { | |
566 | PyObject *x; | |
567 | mp *z; | |
568 | mp_pyobj *zz = 0; | |
569 | int radix = 0; | |
827f89d7 | 570 | static const char *const kwlist[] = { "x", "radix", 0 }; |
d7ab1bab | 571 | |
827f89d7 | 572 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|i:new", KWLIST, &x, &radix)) |
d7ab1bab | 573 | goto end; |
574 | if (MP_PYCHECK(x)) RETURN_OBJ(x); | |
f368b46e | 575 | if (!good_radix_p(radix, 1)) VALERR("bad radix"); |
d7ab1bab | 576 | if ((z = mp_frompyobject(x, radix)) == 0) { |
577 | PyErr_Format(PyExc_TypeError, "can't convert %.100s to mp", | |
f984b31a | 578 | Py_TYPE(x)->tp_name); |
d7ab1bab | 579 | goto end; |
580 | } | |
581 | zz = (mp_pyobj *)ty->tp_alloc(ty, 0); | |
582 | zz->x = z; | |
583 | end: | |
584 | return ((PyObject *)zz); | |
585 | } | |
586 | ||
620169ae | 587 | Py_hash_t mphash(mp *x) |
d7ab1bab | 588 | { |
be6df1c3 | 589 | PyObject *l = mp_topylong(x); |
620169ae | 590 | Py_hash_t h = PyObject_Hash(l); |
f368b46e | 591 | Py_DECREF(l); return (h); |
d7ab1bab | 592 | } |
593 | ||
620169ae | 594 | static Py_hash_t mp_pyhash(PyObject *me) { return (mphash(MP_X(me))); } |
be6df1c3 | 595 | |
d7ab1bab | 596 | static PyObject *mpmeth_jacobi(PyObject *me, PyObject *arg) |
597 | { | |
598 | mp *y = 0; | |
599 | PyObject *z = 0; | |
600 | ||
601 | if (!PyArg_ParseTuple(arg, "O&:jacobi", convmp, &y)) goto end; | |
602 | z = PyInt_FromLong(mp_jacobi(y, MP_X(me))); | |
603 | end: | |
604 | if (y) MP_DROP(y); | |
605 | return (z); | |
606 | } | |
607 | ||
608 | #define BITOP(pre, name, c) \ | |
609 | static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \ | |
610 | { \ | |
a1be89ad MW |
611 | unsigned long i; \ |
612 | if (!PyArg_ParseTuple(arg, "O&:" #name, convulong, &i)) return (0); \ | |
d7ab1bab | 613 | return (pre##_pywrap(mp_##name##c(MP_NEW, MP_X(me), i))); \ |
614 | } | |
615 | BITOP(mp, setbit, 2c); | |
616 | BITOP(mp, clearbit, 2c); | |
b2687a0a | 617 | BITOP(gf, setbit, ); |
d7ab1bab | 618 | BITOP(gf, clearbit, ); |
619 | #undef BITOP | |
620 | ||
621 | static PyObject *mpmeth_testbit(PyObject *me, PyObject *arg) | |
622 | { | |
a1be89ad MW |
623 | unsigned long i; |
624 | if (!PyArg_ParseTuple(arg, "O&:testbit", convulong, &i)) return (0); | |
d7ab1bab | 625 | return (getbool(mp_testbit2c(MP_X(me), i))); |
626 | } | |
627 | ||
628 | static PyObject *gfmeth_testbit(PyObject *me, PyObject *arg) | |
629 | { | |
a1be89ad MW |
630 | unsigned long i; |
631 | if (!PyArg_ParseTuple(arg, "O&:testbit", convulong, &i)) return (0); | |
d7ab1bab | 632 | return (getbool(mp_testbit(MP_X(me), i))); |
633 | } | |
634 | ||
7a75bb76 | 635 | static PyObject *mpmeth_odd(PyObject *me) |
d7ab1bab | 636 | { |
637 | mp *t; | |
638 | size_t s; | |
639 | ||
d7ab1bab | 640 | t = mp_odd(MP_NEW, MP_X(me), &s); |
641 | return (Py_BuildValue("(lN)", (long)s, mp_pywrap(t))); | |
642 | } | |
643 | ||
7a75bb76 MW |
644 | static PyObject *mpmeth_sqr(PyObject *me) |
645 | { return (mp_pywrap(mp_sqr(MP_NEW, MP_X(me)))); } | |
d7ab1bab | 646 | |
7a75bb76 | 647 | static PyObject *mpmeth_sqrt(PyObject *me) |
d7ab1bab | 648 | { |
d7ab1bab | 649 | if (MP_NEGP(MP_X(me))) VALERR("negative root"); |
650 | return (mp_pywrap(mp_sqrt(MP_NEW, MP_X(me)))); | |
651 | end: | |
652 | return (0); | |
653 | } | |
654 | ||
655 | static PyObject *mpmeth_gcd(PyObject *me, PyObject *arg) | |
656 | { | |
657 | mp *y = 0, *zz = MP_NEW; | |
658 | PyObject *z = 0; | |
659 | ||
660 | if (!PyArg_ParseTuple(arg, "O&:gcd", convmp, &y)) goto end; | |
661 | mp_gcd(&zz, 0, 0, MP_X(me), y); | |
662 | z = mp_pywrap(zz); | |
663 | end: | |
664 | if (y) MP_DROP(y); | |
665 | return (z); | |
666 | } | |
667 | ||
668 | static PyObject *mpmeth_gcdx(PyObject *me, PyObject *arg) | |
669 | { | |
670 | PyObject *z = 0; | |
671 | mp *yy = 0, *zz = MP_NEW, *uu = MP_NEW, *vv = MP_NEW; | |
672 | ||
673 | if (!PyArg_ParseTuple(arg, "O&:gcdx", convmp, &yy)) goto end; | |
674 | mp_gcd(&zz, &uu, &vv, MP_X(me), yy); | |
675 | z = Py_BuildValue("(NNN)", | |
676 | mp_pywrap(zz), mp_pywrap(uu), mp_pywrap(vv)); | |
677 | end: | |
678 | if (yy) MP_DROP(yy); | |
679 | return (z); | |
680 | } | |
681 | ||
682 | static PyObject *mpmeth_modinv(PyObject *me, PyObject *arg) | |
683 | { | |
684 | PyObject *z = 0; | |
685 | mp *yy = 0, *zz = MP_NEW; | |
686 | ||
687 | if (!PyArg_ParseTuple(arg, "O&:modinv", convmp, &yy) || | |
688 | (zz = mp_modinv_checked(MP_NEW, yy, MP_X(me))) == 0) | |
689 | goto end; | |
690 | z = mp_pywrap(zz); | |
691 | end: | |
692 | if (yy) MP_DROP(yy); | |
693 | return (z); | |
694 | } | |
695 | ||
696 | static PyObject *mpmeth_tostring(PyObject *me, PyObject *arg, PyObject *kw) | |
697 | { | |
698 | int radix = 10; | |
827f89d7 MW |
699 | static const char *const kwlist[] = { "radix", 0 }; |
700 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|i:tostring", KWLIST, &radix)) | |
d7ab1bab | 701 | goto end; |
f368b46e | 702 | if (!good_radix_p(radix, 0)) VALERR("bad radix"); |
d7ab1bab | 703 | return (mp_topystring(MP_X(me), radix, 0, 0, 0)); |
704 | end: | |
705 | return (0); | |
706 | } | |
707 | ||
708 | static PyObject *mpmeth_modsqrt(PyObject *me, PyObject *arg) | |
709 | { | |
710 | PyObject *z = 0; | |
711 | mp *yy = 0, *zz = MP_NEW; | |
712 | ||
713 | if (!PyArg_ParseTuple(arg, "O&:modsqrt", convmp, &yy)) goto end; | |
714 | if ((zz = mp_modsqrt(MP_NEW, yy, MP_X(me))) == 0) | |
715 | VALERR("no modular square root"); | |
716 | z = mp_pywrap(zz); | |
717 | end: | |
718 | if (yy) MP_DROP(yy); | |
719 | return (z); | |
720 | } | |
721 | ||
d0d94587 MW |
722 | static PyObject *mpmeth_leastcongruent(PyObject *me, PyObject *arg) |
723 | { | |
724 | mp *z, *b, *m; | |
725 | PyObject *rc = 0; | |
726 | ||
727 | if (!PyArg_ParseTuple(arg, "O&O&:leastcongruent", convmp, &b, convmp, &m)) | |
728 | goto end; | |
729 | z = mp_leastcongruent(MP_NEW, b, MP_X(me), m); | |
730 | rc = mp_pywrap(z); | |
731 | end: | |
732 | return (rc); | |
733 | } | |
734 | ||
d7ab1bab | 735 | #define STOREOP(name, c) \ |
736 | static PyObject *mpmeth_##name(PyObject *me, \ | |
737 | PyObject *arg, PyObject *kw) \ | |
738 | { \ | |
739 | long len = -1; \ | |
f927b002 | 740 | static const char *const kwlist[] = { "len", 0 }; \ |
d7ab1bab | 741 | PyObject *rc = 0; \ |
742 | \ | |
743 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|l:" #name, \ | |
827f89d7 | 744 | KWLIST, &len)) \ |
d7ab1bab | 745 | goto end; \ |
746 | if (len < 0) { \ | |
747 | len = mp_octets##c(MP_X(me)); \ | |
748 | if (!len) len = 1; \ | |
749 | } \ | |
750 | rc = bytestring_pywrap(0, len); \ | |
cfb291f0 | 751 | mp_##name(MP_X(me), BIN_PTR(rc), len); \ |
d7ab1bab | 752 | end: \ |
753 | return (rc); \ | |
754 | } | |
755 | STOREOP(storel, ) | |
756 | STOREOP(storeb, ) | |
757 | STOREOP(storel2c, 2c) | |
758 | STOREOP(storeb2c, 2c) | |
759 | #undef STOREOP | |
760 | ||
b16009b0 MW |
761 | #define BUFOP(ty) \ |
762 | static PyObject *ty##meth_frombuf(PyObject *me, PyObject *arg) \ | |
d7ab1bab | 763 | { \ |
764 | buf b; \ | |
be17c8c2 | 765 | struct bin in; \ |
d7ab1bab | 766 | PyObject *rc = 0; \ |
767 | mp *x; \ | |
768 | \ | |
be17c8c2 MW |
769 | if (!PyArg_ParseTuple(arg, "O&:frombuf", convbin, &in)) goto end; \ |
770 | buf_init(&b, (/*unconst*/ void *)in.p, in.sz); \ | |
d7ab1bab | 771 | if ((x = buf_getmp(&b)) == 0) VALERR("malformed data"); \ |
772 | rc = Py_BuildValue("(NN)", ty##_pywrap(x), \ | |
773 | bytestring_pywrapbuf(&b)); \ | |
774 | end: \ | |
775 | return (rc); \ | |
776 | } | |
b16009b0 MW |
777 | BUFOP(mp) |
778 | BUFOP(gf) | |
d7ab1bab | 779 | #undef BUFOP |
780 | ||
7a75bb76 | 781 | static PyObject *mpmeth_tobuf(PyObject *me) |
d7ab1bab | 782 | { |
783 | buf b; | |
784 | PyObject *rc; | |
785 | mp *x; | |
786 | size_t n; | |
787 | ||
d7ab1bab | 788 | x = MP_X(me); |
789 | n = mp_octets(x) + 3; | |
790 | rc = bytestring_pywrap(0, n); | |
cfb291f0 | 791 | buf_init(&b, BIN_PTR(rc), n); |
d7ab1bab | 792 | buf_putmp(&b, x); |
793 | assert(BOK(&b)); | |
cfb291f0 | 794 | BIN_SETLEN(rc, BLEN(&b)); |
d7ab1bab | 795 | return (rc); |
796 | } | |
797 | ||
798 | static PyObject *mpmeth_primep(PyObject *me, PyObject *arg, PyObject *kw) | |
799 | { | |
800 | grand *r = &rand_global; | |
827f89d7 | 801 | static const char *const kwlist[] = { "rng", 0 }; |
d7ab1bab | 802 | PyObject *rc = 0; |
803 | ||
827f89d7 | 804 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&", KWLIST, convgrand, &r)) |
d7ab1bab | 805 | goto end; |
806 | rc = getbool(pgen_primep(MP_X(me), r)); | |
807 | end: | |
808 | return (rc); | |
809 | } | |
810 | ||
b16009b0 MW |
811 | static PyObject *mpmeth_fromstring(PyObject *me, |
812 | PyObject *arg, PyObject *kw) | |
27cbcf9d MW |
813 | { |
814 | int r = 0; | |
815 | char *p; | |
816 | Py_ssize_t len; | |
817 | PyObject *z = 0; | |
818 | mp *zz; | |
819 | mptext_stringctx sc; | |
b16009b0 | 820 | static const char *const kwlist[] = { "x", "radix", 0 }; |
27cbcf9d | 821 | |
b16009b0 MW |
822 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|i:fromstring", KWLIST, |
823 | &p, &len, &r)) | |
27cbcf9d MW |
824 | goto end; |
825 | if (!good_radix_p(r, 1)) VALERR("bad radix"); | |
826 | sc.buf = p; sc.lim = p + len; | |
827 | if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0) | |
828 | VALERR("bad integer"); | |
829 | z = Py_BuildValue("(Ns#)", mp_pywrap(zz), | |
830 | sc.buf, (Py_ssize_t)(sc.lim - sc.buf)); | |
831 | end: | |
832 | return (z); | |
833 | } | |
834 | ||
b16009b0 | 835 | static PyObject *mpmeth_factorial(PyObject *me, PyObject *arg) |
27cbcf9d MW |
836 | { |
837 | unsigned long i; | |
838 | mp *x; | |
b16009b0 | 839 | if (!PyArg_ParseTuple(arg, "O&:factorial", convulong, &i)) return (0); |
27cbcf9d MW |
840 | x = mp_factorial(i); |
841 | return mp_pywrap(x); | |
842 | } | |
843 | ||
b16009b0 | 844 | static PyObject *mpmeth_fibonacci(PyObject *me, PyObject *arg) |
27cbcf9d MW |
845 | { |
846 | long i; | |
847 | mp *x; | |
b16009b0 | 848 | if (!PyArg_ParseTuple(arg, "l:fibonacci", &i)) return (0); |
27cbcf9d MW |
849 | x = mp_fibonacci(i); |
850 | return mp_pywrap(x); | |
851 | } | |
852 | ||
b16009b0 MW |
853 | #define LOADOP(pre, name) \ |
854 | static PyObject *pre##meth_##name(PyObject *me, PyObject *arg) \ | |
27cbcf9d | 855 | { \ |
be17c8c2 MW |
856 | struct bin in; \ |
857 | if (!PyArg_ParseTuple(arg, "O&:" #name, convbin, &in)) return (0); \ | |
858 | return (pre##_pywrap(mp_##name(MP_NEW, in.p, in.sz))); \ | |
27cbcf9d | 859 | } |
b16009b0 MW |
860 | LOADOP(mp, loadl) |
861 | LOADOP(mp, loadb) | |
862 | LOADOP(mp, loadl2c) | |
863 | LOADOP(mp, loadb2c) | |
864 | LOADOP(gf, loadl) | |
865 | LOADOP(gf, loadb) | |
27cbcf9d MW |
866 | #undef LOADOP |
867 | ||
d7ab1bab | 868 | static PyObject *mpget_nbits(PyObject *me, void *hunoz) |
869 | { return (PyInt_FromLong(mp_bits(MP_X(me)))); } | |
870 | ||
871 | static PyObject *mpget_noctets(PyObject *me, void *hunoz) | |
872 | { return (PyInt_FromLong(mp_octets(MP_X(me)))); } | |
873 | ||
874 | static PyObject *mpget_noctets2c(PyObject *me, void *hunoz) | |
875 | { return (PyInt_FromLong(mp_octets2c(MP_X(me)))); } | |
876 | ||
637b9140 | 877 | static const PyGetSetDef mp_pygetset[] = { |
d7ab1bab | 878 | #define GETSETNAME(op, func) mp##op##_##func |
879 | GET (nbits, "X.nbits -> bit length of X") | |
880 | GET (noctets, "X.noctets -> octet length of X") | |
881 | GET (noctets2c, "X.noctets2c -> two's complement octet length of X") | |
882 | #undef GETSETNAME | |
883 | { 0 } | |
884 | }; | |
885 | ||
637b9140 | 886 | static const PyMethodDef mp_pymethods[] = { |
d7ab1bab | 887 | #define METHNAME(func) mpmeth_##func |
c3060669 | 888 | METH (jacobi, "X.jacobi(Y) -> Jacobi symbol (Y|X) (NB inversion!)") |
d7ab1bab | 889 | METH (setbit, "X.setbit(N) -> X with bit N set") |
890 | METH (clearbit, "X.clearbit(N) -> X with bit N clear") | |
891 | METH (testbit, "X.testbit(N) -> true/false if bit N set/clear in X") | |
7a75bb76 MW |
892 | NAMETH(odd, "X.odd() -> S, T where X = 2^S T with T odd") |
893 | NAMETH(sqr, "X.sqr() -> X^2") | |
894 | NAMETH(sqrt, "X.sqrt() -> largest integer <= sqrt(X)") | |
d7ab1bab | 895 | METH (gcd, "X.gcd(Y) -> gcd(X, Y)") |
ef783f91 MW |
896 | METH (gcdx, "X.gcdx(Y) -> (gcd(X, Y), U, V) " |
897 | "with X U + Y V = gcd(X, Y)") | |
d7ab1bab | 898 | METH (modinv, "X.modinv(Y) -> multiplicative inverse of Y mod X") |
899 | METH (modsqrt, "X.modsqrt(Y) -> square root of Y mod X, if X prime") | |
ef783f91 MW |
900 | METH (leastcongruent, "X.leastcongruent(B, M) -> " |
901 | "smallest Z >= B with Z == X (mod M)") | |
f3f76093 | 902 | KWMETH(primep, "X.primep([rng = rand]) -> X is prime?") |
1df8d5fe MW |
903 | KWMETH(tostring, "X.tostring([radix = 10]) -> STR") |
904 | KWMETH(storel, "X.storel([len = -1]) -> little-endian bytes") | |
905 | KWMETH(storeb, "X.storeb([len = -1]) -> big-endian bytes") | |
ef783f91 MW |
906 | KWMETH(storel2c, "X.storel2c([len = -1]) -> " |
907 | "little-endian bytes, two's complement") | |
908 | KWMETH(storeb2c, "X.storeb2c([len = -1]) -> " | |
909 | "big-endian bytes, two's complement") | |
7a75bb76 | 910 | NAMETH(tobuf, "X.tobuf() -> buffer format") |
b16009b0 MW |
911 | KWSMTH(fromstring, "fromstring(STR, [radix = 0]) -> (X, REST)\n" |
912 | " Parse STR as a large integer, according to RADIX. If RADIX is\n" | |
913 | " zero, read a prefix from STR to decide radix: allow `0b' for binary,\n" | |
914 | " `0' or `0o' for octal, `0x' for hex, or `R_' for other radix R.") | |
915 | SMTH (factorial, "factorial(I) -> I!: compute factorial") | |
916 | SMTH (fibonacci, "fibonacci(I) -> F(I): compute Fibonacci number") | |
917 | SMTH (loadl, "loadl(STR) -> X: read little-endian bytes") | |
918 | SMTH (loadb, "loadb(STR) -> X: read big-endian bytes") | |
919 | SMTH (loadl2c, "loadl2c(STR) -> X: " | |
920 | "read little-endian bytes, two's complement") | |
921 | SMTH (loadb2c, "loadb2c(STR) -> X: " | |
922 | "read big-endian bytes, two's complement") | |
923 | SMTH (frombuf, "frombuf(STR) -> (X, REST): read buffer format") | |
d7ab1bab | 924 | #undef METHNAME |
925 | { 0 } | |
926 | }; | |
927 | ||
637b9140 | 928 | static const PyNumberMethods mp_pynumber = { |
d7ab1bab | 929 | mp_pyadd, /* @nb_add@ */ |
930 | mp_pysub, /* @nb_subtract@ */ | |
931 | mp_pymul, /* @nb_multiply@ */ | |
932 | 0, /* @nb_divide@ */ | |
933 | mp_pymod, /* @nb_remainder@ */ | |
934 | mp_pydivmod, /* @nb_divmod@ */ | |
935 | mp_pyexp, /* @nb_power@ */ | |
936 | mp_pyneg, /* @nb_negative@ */ | |
937 | mp_pyid, /* @nb_positive@ */ | |
938 | mp_pyabs, /* @nb_absolute@ */ | |
939 | mp_pynonzerop, /* @nb_nonzero@ */ | |
940 | mp_pynot2c, /* @nb_invert@ */ | |
941 | mp_pylsl2c, /* @nb_lshift@ */ | |
942 | mp_pylsr2c, /* @nb_rshift@ */ | |
943 | mp_pyand2c, /* @nb_and@ */ | |
944 | mp_pyxor2c, /* @nb_xor@ */ | |
945 | mp_pyor2c, /* @nb_or@ */ | |
946 | mp_pycoerce, /* @nb_coerce@ */ | |
947 | mp_pyint, /* @nb_int@ */ | |
948 | mp_pylong, /* @nb_long@ */ | |
949 | mp_pyfloat, /* @nb_float@ */ | |
950 | mp_pyoct, /* @nb_oct@ */ | |
951 | mp_pyhex, /* @nb_hex@ */ | |
952 | ||
953 | 0, /* @nb_inplace_add@ */ | |
954 | 0, /* @nb_inplace_subtract@ */ | |
955 | 0, /* @nb_inplace_multiply@ */ | |
956 | 0, /* @nb_inplace_divide@ */ | |
957 | 0, /* @nb_inplace_remainder@ */ | |
958 | 0, /* @nb_inplace_power@ */ | |
959 | 0, /* @nb_inplace_lshift@ */ | |
960 | 0, /* @nb_inplace_rshift@ */ | |
961 | 0, /* @nb_inplace_and@ */ | |
962 | 0, /* @nb_inplace_xor@ */ | |
963 | 0, /* @nb_inplace_or@ */ | |
964 | ||
965 | mp_pydiv, /* @nb_floor_divide@ */ | |
966 | 0, /* @nb_true_divide@ */ | |
967 | 0, /* @nb_inplace_floor_divide@ */ | |
968 | 0, /* @nb_inplace_true_divide@ */ | |
969 | }; | |
970 | ||
ddd4720b | 971 | static const PyTypeObject mp_pytype_skel = { |
4648f560 | 972 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 973 | "MP", /* @tp_name@ */ |
d7ab1bab | 974 | sizeof(mp_pyobj), /* @tp_basicsize@ */ |
975 | 0, /* @tp_itemsize@ */ | |
976 | ||
977 | mp_pydealloc, /* @tp_dealloc@ */ | |
978 | 0, /* @tp_print@ */ | |
979 | 0, /* @tp_getattr@ */ | |
980 | 0, /* @tp_setattr@ */ | |
981 | mp_pycompare, /* @tp_compare@ */ | |
982 | mp_pyrepr, /* @tp_repr@ */ | |
637b9140 | 983 | PYNUMBER(mp), /* @tp_as_number@ */ |
d7ab1bab | 984 | 0, /* @tp_as_sequence@ */ |
985 | 0, /* @tp_as_mapping@ */ | |
986 | mp_pyhash, /* @tp_hash@ */ | |
987 | 0, /* @tp_call@ */ | |
988 | mp_pystr, /* @tp_str@ */ | |
989 | 0, /* @tp_getattro@ */ | |
990 | 0, /* @tp_setattro@ */ | |
991 | 0, /* @tp_as_buffer@ */ | |
992 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
993 | Py_TPFLAGS_CHECKTYPES | | |
994 | Py_TPFLAGS_BASETYPE, | |
995 | ||
996 | /* @tp_doc@ */ | |
ef783f91 MW |
997 | "Multiprecision integers, similar to `long' but more efficient and\n" |
998 | "versatile. Support all the standard arithmetic operations, with\n" | |
999 | "implicit conversions from `PrimeFilter', and other objects which\n" | |
1000 | "convert to `long'.\n" | |
1001 | "\n" | |
1002 | "Constructor MP(X, [radix = R]) attempts to convert X to an `MP'. If\n" | |
1003 | "X is a string, it's read in radix-R form, or we look for a prefix\n" | |
1004 | "if R = 0. Other acceptable things are field elements, elliptic curve\n" | |
1005 | "points, group elements, Python `int' and `long' objects, and anything\n" | |
1006 | "with an integer conversion.\n" | |
1007 | "\n" | |
1008 | "Notes:\n" | |
1009 | "\n" | |
1010 | " * Use `//' for integer division: `/' gives exact rational division.", | |
d7ab1bab | 1011 | |
1012 | 0, /* @tp_traverse@ */ | |
1013 | 0, /* @tp_clear@ */ | |
1810cc51 | 1014 | mp_pyrichcompare, /* @tp_richcompare@ */ |
d7ab1bab | 1015 | 0, /* @tp_weaklistoffset@ */ |
1016 | 0, /* @tp_iter@ */ | |
963a6148 | 1017 | 0, /* @tp_iternext@ */ |
637b9140 | 1018 | PYMETHODS(mp), /* @tp_methods@ */ |
d7ab1bab | 1019 | 0, /* @tp_members@ */ |
637b9140 | 1020 | PYGETSET(mp), /* @tp_getset@ */ |
d7ab1bab | 1021 | 0, /* @tp_base@ */ |
1022 | 0, /* @tp_dict@ */ | |
1023 | 0, /* @tp_descr_get@ */ | |
1024 | 0, /* @tp_descr_set@ */ | |
1025 | 0, /* @tp_dictoffset@ */ | |
1026 | 0, /* @tp_init@ */ | |
1027 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1028 | mp_pynew, /* @tp_new@ */ | |
3aa33042 | 1029 | 0, /* @tp_free@ */ |
d7ab1bab | 1030 | 0 /* @tp_is_gc@ */ |
1031 | }; | |
1032 | ||
32507e35 MW |
1033 | /*----- Products of small integers ----------------------------------------*/ |
1034 | ||
ddd4720b MW |
1035 | static PyTypeObject *mpmul_pytype; |
1036 | ||
32507e35 MW |
1037 | typedef struct mpmul_pyobj { |
1038 | PyObject_HEAD | |
1039 | int livep; | |
1040 | mpmul mm; | |
1041 | } mpmul_pyobj; | |
1042 | ||
1043 | #define MPMUL_LIVEP(o) (((mpmul_pyobj *)(o))->livep) | |
1044 | #define MPMUL_PY(o) (&((mpmul_pyobj *)(o))->mm) | |
1045 | ||
1046 | static void mpmul_pydealloc(PyObject *me) | |
1047 | { | |
1048 | if (MPMUL_LIVEP(me)) | |
1049 | mp_drop(mpmul_done(MPMUL_PY(me))); | |
1050 | FREEOBJ(me); | |
1051 | } | |
1052 | ||
1053 | static PyObject *mmmeth_factor(PyObject *me, PyObject *arg) | |
d7ab1bab | 1054 | { |
d7ab1bab | 1055 | PyObject *q, *i; |
1056 | mp *x; | |
1057 | ||
32507e35 | 1058 | if (!MPMUL_LIVEP(me)) VALERR("MPMul object invalid"); |
8dd6ebb3 | 1059 | if (PyTuple_GET_SIZE(arg) != 1) |
d7ab1bab | 1060 | i = PyObject_GetIter(arg); |
32507e35 | 1061 | else { |
8dd6ebb3 | 1062 | if ((q = PyTuple_GET_ITEM(arg, 0)) == 0) goto end; |
d7ab1bab | 1063 | if ((i = PyObject_GetIter(q)) == 0) { |
1064 | PyErr_Clear(); /* that's ok */ | |
1065 | i = PyObject_GetIter(arg); | |
1066 | } | |
1067 | } | |
32507e35 | 1068 | if (!i) goto end; |
d7ab1bab | 1069 | while ((q = PyIter_Next(i)) != 0) { |
1070 | x = getmp(q); Py_DECREF(q); if (!x) { | |
d7ab1bab | 1071 | Py_DECREF(i); |
32507e35 | 1072 | goto end; |
d7ab1bab | 1073 | } |
32507e35 | 1074 | mpmul_add(MPMUL_PY(me), x); |
d7ab1bab | 1075 | MP_DROP(x); |
1076 | } | |
d7ab1bab | 1077 | Py_DECREF(i); |
32507e35 MW |
1078 | RETURN_ME; |
1079 | end: | |
1080 | return (0); | |
1081 | } | |
1082 | ||
7a75bb76 | 1083 | static PyObject *mmmeth_done(PyObject *me) |
32507e35 MW |
1084 | { |
1085 | mp *x; | |
1086 | ||
32507e35 MW |
1087 | if (!MPMUL_LIVEP(me)) VALERR("MPMul object invalid"); |
1088 | x = mpmul_done(MPMUL_PY(me)); | |
1089 | MPMUL_LIVEP(me) = 0; | |
d7ab1bab | 1090 | return (mp_pywrap(x)); |
32507e35 MW |
1091 | end: |
1092 | return (0); | |
d7ab1bab | 1093 | } |
1094 | ||
32507e35 MW |
1095 | static PyObject *mpmul_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
1096 | { | |
1097 | mpmul_pyobj *mm; | |
1098 | ||
1099 | if (kw) TYERR("keyword arguments not allowed here"); | |
1100 | mm = (mpmul_pyobj *)ty->tp_alloc(ty, 0); | |
1101 | mpmul_init(&mm->mm); | |
1102 | mm->livep = 1; | |
1103 | if (mmmeth_factor((PyObject *)mm, arg) == 0) { | |
1104 | Py_DECREF(mm); | |
1105 | goto end; | |
d7ab1bab | 1106 | } |
32507e35 MW |
1107 | return ((PyObject *)mm); |
1108 | end: | |
1109 | return (0); | |
1110 | } | |
1111 | ||
1112 | static PyObject *mmget_livep(PyObject *me, void *hunoz) | |
1113 | { return (getbool(MPMUL_LIVEP(me))); } | |
1114 | ||
637b9140 | 1115 | static const PyGetSetDef mpmul_pygetset[] = { |
32507e35 | 1116 | #define GETSETNAME(op, name) mm##op##_##name |
ef783f91 | 1117 | GET (livep, "MM.livep -> flag: object still valid?") |
32507e35 MW |
1118 | #undef GETSETNAME |
1119 | { 0 } | |
1120 | }; | |
1121 | ||
637b9140 | 1122 | static const PyMethodDef mpmul_pymethods[] = { |
32507e35 | 1123 | #define METHNAME(name) mmmeth_##name |
ef783f91 | 1124 | METH (factor, "MM.factor(ITERABLE) or MM.factor(I, ...)") |
7a75bb76 | 1125 | NAMETH(done, "MM.done() -> PRODUCT") |
32507e35 MW |
1126 | #undef METHNAME |
1127 | { 0 } | |
1128 | }; | |
1129 | ||
ddd4720b | 1130 | static const PyTypeObject mpmul_pytype_skel = { |
4648f560 | 1131 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 1132 | "MPMul", /* @tp_name@ */ |
32507e35 MW |
1133 | sizeof(mpmul_pyobj), /* @tp_basicsize@ */ |
1134 | 0, /* @tp_itemsize@ */ | |
1135 | ||
1136 | mpmul_pydealloc, /* @tp_dealloc@ */ | |
1137 | 0, /* @tp_print@ */ | |
1138 | 0, /* @tp_getattr@ */ | |
1139 | 0, /* @tp_setattr@ */ | |
1140 | 0, /* @tp_compare@ */ | |
1141 | 0, /* @tp_repr@ */ | |
1142 | 0, /* @tp_as_number@ */ | |
1143 | 0, /* @tp_as_sequence@ */ | |
1144 | 0, /* @tp_as_mapping@ */ | |
1145 | 0, /* @tp_hash@ */ | |
1146 | 0, /* @tp_call@ */ | |
1147 | 0, /* @tp_str@ */ | |
1148 | 0, /* @tp_getattro@ */ | |
1149 | 0, /* @tp_setattro@ */ | |
1150 | 0, /* @tp_as_buffer@ */ | |
1151 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1152 | Py_TPFLAGS_BASETYPE, | |
1153 | ||
1154 | /* @tp_doc@ */ | |
ef783f91 | 1155 | "MPMul(N_0, N_1, ....): an object for multiplying many small integers.", |
32507e35 MW |
1156 | |
1157 | 0, /* @tp_traverse@ */ | |
1158 | 0, /* @tp_clear@ */ | |
1159 | 0, /* @tp_richcompare@ */ | |
1160 | 0, /* @tp_weaklistoffset@ */ | |
1161 | 0, /* @tp_iter@ */ | |
1162 | 0, /* @tp_iternext@ */ | |
637b9140 | 1163 | PYMETHODS(mpmul), /* @tp_methods@ */ |
32507e35 | 1164 | 0, /* @tp_members@ */ |
637b9140 | 1165 | PYGETSET(mpmul), /* @tp_getset@ */ |
32507e35 MW |
1166 | 0, /* @tp_base@ */ |
1167 | 0, /* @tp_dict@ */ | |
1168 | 0, /* @tp_descr_get@ */ | |
1169 | 0, /* @tp_descr_set@ */ | |
1170 | 0, /* @tp_dictoffset@ */ | |
1171 | 0, /* @tp_init@ */ | |
1172 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1173 | mpmul_pynew, /* @tp_new@ */ | |
1174 | 0, /* @tp_free@ */ | |
1175 | 0 /* @tp_is_gc@ */ | |
1176 | }; | |
d7ab1bab | 1177 | |
1178 | /*----- Montgomery reduction ----------------------------------------------*/ | |
1179 | ||
ddd4720b MW |
1180 | static PyTypeObject *mpmont_pytype; |
1181 | ||
d7ab1bab | 1182 | typedef struct mpmont_pyobj { |
1183 | PyObject_HEAD | |
1184 | mpmont mm; | |
1185 | } mpmont_pyobj; | |
1186 | ||
1187 | #define MPMONT_PY(o) (&((mpmont_pyobj *)(o))->mm) | |
1188 | ||
1189 | static PyObject *mmmeth_int(PyObject *me, PyObject *arg) | |
1190 | { | |
1191 | PyObject *z = 0; | |
1192 | mp *yy = 0; | |
1193 | mpmont *mm = MPMONT_PY(me); | |
1194 | ||
1195 | if (!PyArg_ParseTuple(arg, "O&:in", convmp, &yy)) | |
1196 | goto end; | |
1197 | mp_div(0, &yy, yy, mm->m); | |
1198 | z = mp_pywrap(mpmont_mul(mm, MP_NEW, yy, mm->r2)); | |
1199 | end: | |
1200 | if (yy) MP_DROP(yy); | |
1201 | return (z); | |
1202 | } | |
1203 | ||
1204 | static PyObject *mmmeth_mul(PyObject *me, PyObject *arg) | |
1205 | { | |
1206 | PyObject *rc = 0; | |
1207 | mp *yy = 0, *zz = 0; | |
1208 | ||
1209 | if (!PyArg_ParseTuple(arg, "O&O&:mul", convmp, &yy, convmp, &zz)) | |
1210 | goto end; | |
1211 | rc = mp_pywrap(mpmont_mul(MPMONT_PY(me), MP_NEW, yy, zz)); | |
1212 | end: | |
1213 | if (yy) MP_DROP(yy); if (zz) MP_DROP(zz); | |
1214 | return (rc); | |
1215 | } | |
1216 | ||
1217 | static PyObject *mmmeth_exp(PyObject *me, PyObject *arg) | |
1218 | { | |
1219 | PyObject *rc = 0; | |
1220 | mp *yy = 0, *zz = 0; | |
1221 | ||
1222 | if (!PyArg_ParseTuple(arg, "O&O&:exp", convmp, &yy, convmp, &zz)) | |
1223 | goto end; | |
1224 | if (MP_NEGP(zz)) { | |
1225 | if ((yy = mp_modinv_checked(yy, yy, MPMONT_PY(me)->m)) == 0) goto end; | |
1226 | zz = mp_neg(zz, zz); | |
1227 | } | |
1228 | rc = mp_pywrap(mpmont_exp(MPMONT_PY(me), MP_NEW, yy, zz)); | |
1229 | end: | |
1230 | if (yy) MP_DROP(yy); if (zz) MP_DROP(zz); | |
1231 | return (rc); | |
1232 | } | |
1233 | ||
1234 | static PyObject *mmmeth_expr(PyObject *me, PyObject *arg) | |
1235 | { | |
1236 | PyObject *rc = 0; | |
1237 | mp *yy = 0, *zz = 0; | |
1238 | ||
1239 | if (!PyArg_ParseTuple(arg, "O&O&:expr", convmp, &yy, convmp, &zz)) | |
1240 | goto end; | |
1241 | if (MP_NEGP(zz)) { | |
1242 | yy = mpmont_reduce(MPMONT_PY(me), yy, yy); | |
1243 | if ((yy = mp_modinv_checked(yy, yy, MPMONT_PY(me)->m)) == 0) goto end; | |
1244 | yy = mpmont_mul(MPMONT_PY(me), yy, yy, MPMONT_PY(me)->r2); | |
1245 | zz = mp_neg(zz, zz); | |
1246 | } | |
1247 | rc = mp_pywrap(mpmont_expr(MPMONT_PY(me), MP_NEW, yy, zz)); | |
1248 | end: | |
1249 | if (yy) MP_DROP(yy); if (zz) MP_DROP(zz); | |
1250 | return (rc); | |
1251 | } | |
1252 | ||
1253 | static PyObject *mm_mexpr_id(PyObject *me) | |
1254 | { return mp_pywrap(MP_COPY(MPMONT_PY(me)->r)); } | |
1255 | ||
1256 | static int mm_mexpr_fill(void *p, PyObject *me, PyObject *x, PyObject *y) | |
1257 | { | |
1258 | mp *xx = 0, *yy = 0; | |
1259 | mp_expfactor *f = p; | |
1260 | mpmont *mm = MPMONT_PY(me); | |
1261 | ||
1262 | if ((xx = getmp(x)) == 0 || (yy = getmp(y)) == 0) | |
1263 | goto fail; | |
1264 | if (MP_NEGP(yy)) { | |
1265 | xx = mpmont_reduce(mm, xx, xx); | |
1266 | if ((xx = mp_modinv_checked(xx, xx, yy)) == 0) | |
1267 | goto fail; | |
1268 | xx = mpmont_mul(mm, xx, xx, mm->r2); | |
1269 | yy = mp_neg(yy, yy); | |
1270 | } | |
1271 | f->base = xx; | |
1272 | f->exp = yy; | |
1273 | return (0); | |
1274 | ||
1275 | fail: | |
1276 | mp_drop(xx); mp_drop(yy); | |
1277 | return (-1); | |
1278 | } | |
1279 | ||
1280 | static PyObject *mm_mexpr(PyObject *me, void *v, int n) | |
1281 | { return mp_pywrap(mpmont_mexpr(MPMONT_PY(me), MP_NEW, v, n)); } | |
b2687a0a | 1282 | |
d7ab1bab | 1283 | static void mp_mexp_drop(void *p) |
1284 | { | |
1285 | mp_expfactor *f = p; | |
1286 | mp_drop(f->base); | |
1287 | mp_drop(f->exp); | |
1288 | } | |
1289 | ||
1290 | static PyObject *mmmeth_mexpr(PyObject *me, PyObject *arg) | |
1291 | { | |
1292 | return mexp_common(me, arg, sizeof(mp_expfactor), | |
1293 | mm_mexpr_id, mm_mexpr_fill, mm_mexpr, mp_mexp_drop); | |
1294 | } | |
1295 | ||
1296 | static PyObject *mp_mexp_id(PyObject *me) | |
1297 | { return mp_pywrap(MP_ONE); } | |
1298 | ||
1299 | static int mp_mexp_fill(void *p, PyObject *me, PyObject *x, PyObject *y) | |
1300 | { | |
1301 | mp *xx = 0, *yy = 0; | |
1302 | mp_expfactor *f = p; | |
1303 | ||
1304 | if ((xx = getmp(x)) == 0 || (yy = getmp(y)) == 0) | |
1305 | goto fail; | |
1306 | if (MP_NEGP(yy)) { | |
1307 | if ((xx = mp_modinv_checked(xx, xx, yy)) == 0) | |
1308 | goto fail; | |
1309 | yy = mp_neg(yy, yy); | |
1310 | } | |
1311 | f->base = xx; | |
1312 | f->exp = yy; | |
1313 | return (0); | |
1314 | ||
1315 | fail: | |
1316 | mp_drop(xx); mp_drop(yy); | |
1317 | return (-1); | |
1318 | } | |
1319 | ||
1320 | static PyObject *mm_mexp(PyObject *me, void *v, int n) | |
1321 | { return mp_pywrap(mpmont_mexp(MPMONT_PY(me), MP_NEW, v, n)); } | |
b2687a0a | 1322 | |
d7ab1bab | 1323 | static PyObject *mmmeth_mexp(PyObject *me, PyObject *arg) |
1324 | { | |
1325 | return mexp_common(me, arg, sizeof(mp_expfactor), | |
1326 | mp_mexp_id, mp_mexp_fill, mm_mexp, mp_mexp_drop); | |
1327 | } | |
1328 | ||
1329 | #define mmmeth_ext mmmeth_reduce | |
1330 | static PyObject *mmmeth_reduce(PyObject *me, PyObject *arg) | |
1331 | { | |
1332 | PyObject *z = 0; | |
1333 | mp *yy = 0; | |
1334 | ||
1335 | if (!PyArg_ParseTuple(arg, "O&", convmp, &yy)) goto end; | |
1336 | z = mp_pywrap(mpmont_reduce(MPMONT_PY(me), MP_NEW, yy)); | |
1337 | end: | |
1338 | return (z); | |
1339 | } | |
1340 | ||
1341 | static void mpmont_pydealloc(PyObject *me) | |
1342 | { | |
1343 | mpmont_destroy(MPMONT_PY(me)); | |
3aa33042 | 1344 | FREEOBJ(me); |
d7ab1bab | 1345 | } |
1346 | ||
1347 | static PyObject *mpmont_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
1348 | { | |
1349 | mpmont_pyobj *mm = 0; | |
827f89d7 | 1350 | static const char *const kwlist[] = { "m", 0 }; |
d7ab1bab | 1351 | mp *xx = 0; |
1352 | ||
827f89d7 | 1353 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convmp, &xx)) |
d7ab1bab | 1354 | goto end; |
1355 | if (!MP_POSP(xx) || !MP_ODDP(xx)) VALERR("m must be positive and odd"); | |
1356 | mm = (mpmont_pyobj *)ty->tp_alloc(ty, 0); | |
1357 | mpmont_create(&mm->mm, xx); | |
1358 | end: | |
1359 | if (xx) MP_DROP(xx); | |
1360 | return ((PyObject *)mm); | |
1361 | } | |
1362 | ||
1363 | static PyObject *mmget_m(PyObject *me, void *hunoz) | |
1364 | { return (mp_pywrap(MP_COPY(MPMONT_PY(me)->m))); } | |
1365 | ||
1366 | static PyObject *mmget_r(PyObject *me, void *hunoz) | |
1367 | { return (mp_pywrap(MP_COPY(MPMONT_PY(me)->r))); } | |
1368 | ||
1369 | static PyObject *mmget_r2(PyObject *me, void *hunoz) | |
1370 | { return (mp_pywrap(MP_COPY(MPMONT_PY(me)->r2))); } | |
1371 | ||
637b9140 | 1372 | static const PyGetSetDef mpmont_pygetset[] = { |
d7ab1bab | 1373 | #define GETSETNAME(op, name) mm##op##_##name |
1374 | GET (m, "M.m -> modulus for reduction") | |
1375 | GET (r, "M.r -> multiplicative identity") | |
1376 | GET (r2, "M.r2 -> M.r^2, Montgomerization factor") | |
1377 | #undef GETSETNAME | |
1378 | { 0 } | |
1379 | }; | |
1380 | ||
637b9140 | 1381 | static const PyMethodDef mpmont_pymethods[] = { |
d7ab1bab | 1382 | #define METHNAME(name) mmmeth_##name |
986bcc44 | 1383 | METH (int, "M.int(X) -> XR") |
d7ab1bab | 1384 | METH (mul, "M.mul(XR, YR) -> ZR where Z = X Y") |
1385 | METH (expr, "M.expr(XR, N) -> ZR where Z = X^N mod M.m") | |
ef783f91 MW |
1386 | METH (mexpr, "M.mexpr([(XR0, N0), (XR1, N1), ...]) = ZR " |
1387 | "where Z = X0^N0 X1^N1 ... mod M.m\n" | |
1388 | "\t(the list may be flattened if this more convenient.)") | |
d7ab1bab | 1389 | METH (reduce, "M.reduce(XR) -> X") |
1390 | METH (ext, "M.ext(XR) -> X") | |
1391 | METH (exp, "M.exp(X, N) -> X^N mod M.m") | |
ef783f91 MW |
1392 | METH (mexp, "M.mexp([(X0, N0), (X1, N1), ...]) = " |
1393 | "X0^N0 X1^N1 ... mod M.m\n" | |
1394 | "\t(the list may be flattened if this more convenient.)") | |
d7ab1bab | 1395 | #undef METHNAME |
1396 | { 0 } | |
1397 | }; | |
1398 | ||
ddd4720b | 1399 | static const PyTypeObject mpmont_pytype_skel = { |
4648f560 | 1400 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 1401 | "MPMont", /* @tp_name@ */ |
d7ab1bab | 1402 | sizeof(mpmont_pyobj), /* @tp_basicsize@ */ |
1403 | 0, /* @tp_itemsize@ */ | |
1404 | ||
1405 | mpmont_pydealloc, /* @tp_dealloc@ */ | |
1406 | 0, /* @tp_print@ */ | |
1407 | 0, /* @tp_getattr@ */ | |
1408 | 0, /* @tp_setattr@ */ | |
1409 | 0, /* @tp_compare@ */ | |
1410 | 0, /* @tp_repr@ */ | |
1411 | 0, /* @tp_as_number@ */ | |
1412 | 0, /* @tp_as_sequence@ */ | |
1413 | 0, /* @tp_as_mapping@ */ | |
1414 | 0, /* @tp_hash@ */ | |
1415 | 0, /* @tp_call@ */ | |
1416 | 0, /* @tp_str@ */ | |
1417 | 0, /* @tp_getattro@ */ | |
1418 | 0, /* @tp_setattro@ */ | |
1419 | 0, /* @tp_as_buffer@ */ | |
1420 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1421 | Py_TPFLAGS_BASETYPE, | |
1422 | ||
1423 | /* @tp_doc@ */ | |
ef783f91 | 1424 | "MPMont(N): a Montgomery reduction context.", |
d7ab1bab | 1425 | |
1426 | 0, /* @tp_traverse@ */ | |
1427 | 0, /* @tp_clear@ */ | |
1428 | 0, /* @tp_richcompare@ */ | |
1429 | 0, /* @tp_weaklistoffset@ */ | |
1430 | 0, /* @tp_iter@ */ | |
963a6148 | 1431 | 0, /* @tp_iternext@ */ |
637b9140 | 1432 | PYMETHODS(mpmont), /* @tp_methods@ */ |
d7ab1bab | 1433 | 0, /* @tp_members@ */ |
637b9140 | 1434 | PYGETSET(mpmont), /* @tp_getset@ */ |
d7ab1bab | 1435 | 0, /* @tp_base@ */ |
1436 | 0, /* @tp_dict@ */ | |
1437 | 0, /* @tp_descr_get@ */ | |
1438 | 0, /* @tp_descr_set@ */ | |
1439 | 0, /* @tp_dictoffset@ */ | |
1440 | 0, /* @tp_init@ */ | |
1441 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1442 | mpmont_pynew, /* @tp_new@ */ | |
3aa33042 | 1443 | 0, /* @tp_free@ */ |
d7ab1bab | 1444 | 0 /* @tp_is_gc@ */ |
1445 | }; | |
1446 | ||
1447 | /*----- Barrett reduction -------------------------------------------------*/ | |
1448 | ||
ddd4720b MW |
1449 | static PyTypeObject *mpbarrett_pytype; |
1450 | ||
d7ab1bab | 1451 | typedef struct mpbarrett_pyobj { |
1452 | PyObject_HEAD | |
1453 | mpbarrett mb; | |
1454 | } mpbarrett_pyobj; | |
1455 | ||
1456 | #define MPBARRETT_PY(o) (&((mpbarrett_pyobj *)(o))->mb) | |
1457 | ||
1458 | static PyObject *mbmeth_exp(PyObject *me, PyObject *arg) | |
1459 | { | |
1460 | PyObject *rc = 0; | |
1461 | mp *yy = 0, *zz = 0; | |
1462 | ||
1463 | if (!PyArg_ParseTuple(arg, "O&O&:exp", convmp, &yy, convmp, &zz)) | |
1464 | goto end; | |
1465 | if (MP_NEGP(zz)) { | |
1466 | if ((yy = mp_modinv_checked(yy, yy, MPBARRETT_PY(me)->m)) == 0) goto end; | |
1467 | zz = mp_neg(zz, zz); | |
1468 | } | |
1469 | rc = mp_pywrap(mpbarrett_exp(MPBARRETT_PY(me), MP_NEW, yy, zz)); | |
1470 | end: | |
1471 | if (yy) MP_DROP(yy); if (zz) MP_DROP(zz); | |
1472 | return (rc); | |
1473 | } | |
1474 | ||
1475 | static PyObject *mb_mexp(PyObject *me, void *v, int n) | |
1476 | { return mp_pywrap(mpbarrett_mexp(MPBARRETT_PY(me), MP_NEW, v, n)); } | |
b2687a0a | 1477 | |
d7ab1bab | 1478 | static PyObject *mbmeth_mexp(PyObject *me, PyObject *arg) |
1479 | { | |
1480 | return mexp_common(me, arg, sizeof(mp_expfactor), | |
1481 | mp_mexp_id, mp_mexp_fill, mb_mexp, mp_mexp_drop); | |
1482 | } | |
1483 | ||
1484 | static PyObject *mbmeth_reduce(PyObject *me, PyObject *arg) | |
1485 | { | |
1486 | PyObject *z = 0; | |
1487 | mp *yy = 0; | |
1488 | ||
1489 | if (!PyArg_ParseTuple(arg, "O&:reduce", convmp, &yy)) | |
1490 | goto end; | |
1491 | z = mp_pywrap(mpbarrett_reduce(MPBARRETT_PY(me), MP_NEW, yy)); | |
1492 | end: | |
1493 | return (z); | |
1494 | } | |
1495 | ||
1496 | static void mpbarrett_pydealloc(PyObject *me) | |
1497 | { | |
1498 | mpbarrett_destroy(MPBARRETT_PY(me)); | |
3aa33042 | 1499 | FREEOBJ(me); |
d7ab1bab | 1500 | } |
1501 | ||
1502 | static PyObject *mpbarrett_pynew(PyTypeObject *ty, | |
1503 | PyObject *arg, PyObject *kw) | |
1504 | { | |
1505 | mpbarrett_pyobj *mb = 0; | |
827f89d7 | 1506 | static const char *const kwlist[] = { "m", 0 }; |
d7ab1bab | 1507 | mp *xx = 0; |
1508 | ||
827f89d7 | 1509 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convmp, &xx)) |
d7ab1bab | 1510 | goto end; |
1511 | if (!MP_POSP(xx)) VALERR("m must be positive"); | |
1512 | mb = (mpbarrett_pyobj *)ty->tp_alloc(ty, 0); | |
1513 | mpbarrett_create(&mb->mb, xx); | |
1514 | end: | |
1515 | if (xx) MP_DROP(xx); | |
1516 | return ((PyObject *)mb); | |
1517 | } | |
1518 | ||
1519 | static PyObject *mbget_m(PyObject *me, void *hunoz) | |
1520 | { return (mp_pywrap(MP_COPY(MPBARRETT_PY(me)->m))); } | |
1521 | ||
637b9140 | 1522 | static const PyGetSetDef mpbarrett_pygetset[] = { |
d7ab1bab | 1523 | #define GETSETNAME(op, name) mb##op##_##name |
1524 | GET (m, "B.m -> modulus for reduction") | |
1525 | #undef GETSETNAME | |
1526 | { 0 } | |
1527 | }; | |
1528 | ||
637b9140 | 1529 | static const PyMethodDef mpbarrett_pymethods[] = { |
d7ab1bab | 1530 | #define METHNAME(name) mbmeth_##name |
1531 | METH (reduce, "B.reduce(X) -> X mod B.m") | |
1532 | METH (exp, "B.exp(X, N) -> X^N mod B.m") | |
ef783f91 MW |
1533 | METH (mexp, "B.mexp([(X0, N0), (X1, N1), ...]) = " |
1534 | "X0^N0 X1^N1 ... mod B.m\n" | |
1535 | "\t(the list may be flattened if this more convenient.)") | |
d7ab1bab | 1536 | #undef METHNAME |
1537 | { 0 } | |
1538 | }; | |
1539 | ||
ddd4720b | 1540 | static const PyTypeObject mpbarrett_pytype_skel = { |
4648f560 | 1541 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 1542 | "MPBarrett", /* @tp_name@ */ |
d7ab1bab | 1543 | sizeof(mpbarrett_pyobj), /* @tp_basicsize@ */ |
1544 | 0, /* @tp_itemsize@ */ | |
1545 | ||
1546 | mpbarrett_pydealloc, /* @tp_dealloc@ */ | |
1547 | 0, /* @tp_print@ */ | |
1548 | 0, /* @tp_getattr@ */ | |
1549 | 0, /* @tp_setattr@ */ | |
1550 | 0, /* @tp_compare@ */ | |
1551 | 0, /* @tp_repr@ */ | |
1552 | 0, /* @tp_as_number@ */ | |
1553 | 0, /* @tp_as_sequence@ */ | |
1554 | 0, /* @tp_as_mapping@ */ | |
1555 | 0, /* @tp_hash@ */ | |
1556 | 0, /* @tp_call@ */ | |
1557 | 0, /* @tp_str@ */ | |
1558 | 0, /* @tp_getattro@ */ | |
1559 | 0, /* @tp_setattro@ */ | |
1560 | 0, /* @tp_as_buffer@ */ | |
1561 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1562 | Py_TPFLAGS_BASETYPE, | |
1563 | ||
1564 | /* @tp_doc@ */ | |
ef783f91 | 1565 | "MPBarrett(N): a Barrett reduction context.", |
d7ab1bab | 1566 | |
1567 | 0, /* @tp_traverse@ */ | |
1568 | 0, /* @tp_clear@ */ | |
1569 | 0, /* @tp_richcompare@ */ | |
1570 | 0, /* @tp_weaklistoffset@ */ | |
1571 | 0, /* @tp_iter@ */ | |
963a6148 | 1572 | 0, /* @tp_iternext@ */ |
637b9140 | 1573 | PYMETHODS(mpbarrett), /* @tp_methods@ */ |
d7ab1bab | 1574 | 0, /* @tp_members@ */ |
637b9140 | 1575 | PYGETSET(mpbarrett), /* @tp_getset@ */ |
d7ab1bab | 1576 | 0, /* @tp_base@ */ |
1577 | 0, /* @tp_dict@ */ | |
1578 | 0, /* @tp_descr_get@ */ | |
1579 | 0, /* @tp_descr_set@ */ | |
1580 | 0, /* @tp_dictoffset@ */ | |
1581 | 0, /* @tp_init@ */ | |
1582 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1583 | mpbarrett_pynew, /* @tp_new@ */ | |
3aa33042 | 1584 | 0, /* @tp_free@ */ |
d7ab1bab | 1585 | 0 /* @tp_is_gc@ */ |
1586 | }; | |
1587 | ||
1588 | /*----- Nice prime reduction ----------------------------------------------*/ | |
1589 | ||
ddd4720b MW |
1590 | static PyTypeObject *mpreduce_pytype; |
1591 | ||
d7ab1bab | 1592 | typedef struct mpreduce_pyobj { |
1593 | PyObject_HEAD | |
1594 | mpreduce mr; | |
1595 | } mpreduce_pyobj; | |
1596 | ||
1597 | #define MPREDUCE_PY(o) (&((mpreduce_pyobj *)(o))->mr) | |
1598 | ||
1599 | static PyObject *mrmeth_exp(PyObject *me, PyObject *arg) | |
1600 | { | |
1601 | PyObject *rc = 0; | |
1602 | mp *yy = 0, *zz = 0; | |
1603 | ||
1604 | if (!PyArg_ParseTuple(arg, "O&O&:exp", convmp, &yy, convmp, &zz)) | |
1605 | goto end; | |
1606 | if (MP_NEGP(zz)) { | |
1607 | if ((yy = mp_modinv_checked(yy, yy, MPREDUCE_PY(me)->p)) == 0) goto end; | |
1608 | zz = mp_neg(zz, zz); | |
1609 | } | |
1610 | rc = mp_pywrap(mpreduce_exp(MPREDUCE_PY(me), MP_NEW, yy, zz)); | |
1611 | end: | |
1612 | if (yy) MP_DROP(yy); if (zz) MP_DROP(zz); | |
1613 | return (rc); | |
1614 | } | |
1615 | ||
1616 | static PyObject *mrmeth_reduce(PyObject *me, PyObject *arg) | |
1617 | { | |
1618 | PyObject *z = 0; | |
1619 | mp *yy = 0; | |
1620 | ||
1621 | if (!PyArg_ParseTuple(arg, "O&:reduce", convmp, &yy)) goto end; | |
1622 | z = mp_pywrap(mpreduce_do(MPREDUCE_PY(me), MP_NEW, yy)); | |
1623 | end: | |
1624 | return (z); | |
1625 | } | |
1626 | ||
1627 | static void mpreduce_pydealloc(PyObject *me) | |
1628 | { | |
1629 | mpreduce_destroy(MPREDUCE_PY(me)); | |
3aa33042 | 1630 | FREEOBJ(me); |
d7ab1bab | 1631 | } |
1632 | ||
1633 | static PyObject *mpreduce_pynew(PyTypeObject *ty, | |
1634 | PyObject *arg, PyObject *kw) | |
1635 | { | |
1636 | mpreduce_pyobj *mr = 0; | |
1637 | mpreduce r; | |
827f89d7 | 1638 | static const char *const kwlist[] = { "m", 0 }; |
d7ab1bab | 1639 | mp *xx = 0; |
1640 | ||
827f89d7 | 1641 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convmp, &xx)) |
d7ab1bab | 1642 | goto end; |
1643 | if (!MP_POSP(xx)) VALERR("m must be positive"); | |
1644 | if (mpreduce_create(&r, xx)) VALERR("bad modulus (must be 2^k - ...)"); | |
1645 | mr = (mpreduce_pyobj *)ty->tp_alloc(ty, 0); | |
1646 | mr->mr = r; | |
1647 | end: | |
1648 | if (xx) MP_DROP(xx); | |
1649 | return ((PyObject *)mr); | |
1650 | } | |
1651 | ||
1652 | static PyObject *mrget_m(PyObject *me, void *hunoz) | |
1653 | { return (mp_pywrap(MP_COPY(MPREDUCE_PY(me)->p))); } | |
1654 | ||
637b9140 | 1655 | static const PyGetSetDef mpreduce_pygetset[] = { |
d7ab1bab | 1656 | #define GETSETNAME(op, name) mr##op##_##name |
1657 | GET (m, "R.m -> modulus for reduction") | |
1658 | #undef GETSETNAME | |
1659 | { 0 } | |
1660 | }; | |
1661 | ||
637b9140 | 1662 | static const const PyMethodDef mpreduce_pymethods[] = { |
d7ab1bab | 1663 | #define METHNAME(name) mrmeth_##name |
1664 | METH (reduce, "R.reduce(X) -> X mod B.m") | |
1665 | METH (exp, "R.exp(X, N) -> X^N mod B.m") | |
1666 | #undef METHNAME | |
1667 | { 0 } | |
1668 | }; | |
1669 | ||
ddd4720b | 1670 | static const PyTypeObject mpreduce_pytype_skel = { |
4648f560 | 1671 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 1672 | "MPReduce", /* @tp_name@ */ |
d7ab1bab | 1673 | sizeof(mpreduce_pyobj), /* @tp_basicsize@ */ |
1674 | 0, /* @tp_itemsize@ */ | |
1675 | ||
1676 | mpreduce_pydealloc, /* @tp_dealloc@ */ | |
1677 | 0, /* @tp_print@ */ | |
1678 | 0, /* @tp_getattr@ */ | |
1679 | 0, /* @tp_setattr@ */ | |
1680 | 0, /* @tp_compare@ */ | |
1681 | 0, /* @tp_repr@ */ | |
1682 | 0, /* @tp_as_number@ */ | |
1683 | 0, /* @tp_as_sequence@ */ | |
1684 | 0, /* @tp_as_mapping@ */ | |
1685 | 0, /* @tp_hash@ */ | |
1686 | 0, /* @tp_call@ */ | |
1687 | 0, /* @tp_str@ */ | |
1688 | 0, /* @tp_getattro@ */ | |
1689 | 0, /* @tp_setattro@ */ | |
1690 | 0, /* @tp_as_buffer@ */ | |
1691 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1692 | Py_TPFLAGS_BASETYPE, | |
1693 | ||
1694 | /* @tp_doc@ */ | |
ef783f91 | 1695 | "MPReduce(N): a reduction context for reduction modulo Solinas primes.", |
d7ab1bab | 1696 | |
1697 | 0, /* @tp_traverse@ */ | |
1698 | 0, /* @tp_clear@ */ | |
1699 | 0, /* @tp_richcompare@ */ | |
1700 | 0, /* @tp_weaklistoffset@ */ | |
1701 | 0, /* @tp_iter@ */ | |
963a6148 | 1702 | 0, /* @tp_iternext@ */ |
637b9140 | 1703 | PYMETHODS(mpreduce), /* @tp_methods@ */ |
d7ab1bab | 1704 | 0, /* @tp_members@ */ |
637b9140 | 1705 | PYGETSET(mpreduce), /* @tp_getset@ */ |
d7ab1bab | 1706 | 0, /* @tp_base@ */ |
1707 | 0, /* @tp_dict@ */ | |
1708 | 0, /* @tp_descr_get@ */ | |
1709 | 0, /* @tp_descr_set@ */ | |
1710 | 0, /* @tp_dictoffset@ */ | |
1711 | 0, /* @tp_init@ */ | |
1712 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1713 | mpreduce_pynew, /* @tp_new@ */ | |
3aa33042 | 1714 | 0, /* @tp_free@ */ |
d7ab1bab | 1715 | 0 /* @tp_is_gc@ */ |
1716 | }; | |
1717 | ||
1718 | /*----- Chinese Remainder Theorem solution --------------------------------*/ | |
1719 | ||
ddd4720b MW |
1720 | static PyTypeObject *mpcrt_pytype; |
1721 | ||
d7ab1bab | 1722 | typedef struct mpcrt_pyobj { |
1723 | PyObject_HEAD | |
1724 | mpcrt c; | |
1725 | } mpcrt_pyobj; | |
1726 | ||
1727 | #define MPCRT_PY(o) (&((mpcrt_pyobj *)(o))->c) | |
1728 | ||
1729 | static PyObject *mcmeth_solve(PyObject *me, PyObject *arg) | |
1730 | { | |
1731 | mpcrt *c = MPCRT_PY(me); | |
1732 | PyObject *q = 0, *x, *z = 0; | |
1733 | mp *xx; | |
1734 | mp **v = 0; | |
77a0d53f | 1735 | Py_ssize_t i = 0, n = c->k; |
d7ab1bab | 1736 | |
1737 | Py_INCREF(me); | |
8dd6ebb3 | 1738 | if (PyTuple_GET_SIZE(arg) == n) |
d7ab1bab | 1739 | q = arg; |
1740 | else if (!PyArg_ParseTuple(arg, "O:solve", &q)) | |
1741 | goto end; | |
1742 | Py_INCREF(q); | |
1743 | if (!PySequence_Check(q)) TYERR("want a sequence of residues"); | |
b17bbe3b MW |
1744 | i = PySequence_Size(q); if (i < 0) goto end; |
1745 | if (i != n) VALERR("residue count mismatch"); | |
d7ab1bab | 1746 | v = xmalloc(n * sizeof(*v)); |
1747 | for (i = 0; i < n; i++) { | |
1748 | if ((x = PySequence_GetItem(q, i)) == 0) goto end; | |
1749 | xx = getmp(x); Py_DECREF(x); if (!xx) goto end; | |
1750 | v[i] = xx; | |
1751 | } | |
1752 | z = mp_pywrap(mpcrt_solve(c, MP_NEW, v)); | |
1753 | end: | |
1754 | if (v) { | |
1755 | n = i; | |
1756 | for (i = 0; i < n; i++) | |
1757 | MP_DROP(v[i]); | |
1758 | xfree(v); | |
1759 | } | |
1760 | Py_DECREF(me); | |
1761 | Py_XDECREF(q); | |
1762 | return (z); | |
1763 | } | |
1764 | ||
1765 | static void mpcrt_pydealloc(PyObject *me) | |
1766 | { | |
1767 | mpcrt *c = MPCRT_PY(me); | |
1768 | mpcrt_destroy(c); | |
1769 | xfree(c->v); | |
1770 | } | |
1771 | ||
1772 | static PyObject *mpcrt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
1773 | { | |
1774 | mpcrt_mod *v = 0; | |
77a0d53f | 1775 | Py_ssize_t n, i = 0, j; |
827f89d7 | 1776 | static const char *const kwlist[] = { "mv", 0 }; |
d7ab1bab | 1777 | PyObject *q = 0, *x; |
d2b7e92c MW |
1778 | mp *xx = MP_NEW, *y = MP_NEW, *g = MP_NEW; |
1779 | mpmul mm; | |
d7ab1bab | 1780 | mpcrt_pyobj *c = 0; |
1781 | ||
8dd6ebb3 | 1782 | if (PyTuple_GET_SIZE(arg) > 1) |
d7ab1bab | 1783 | q = arg; |
827f89d7 | 1784 | else if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", KWLIST, &q)) |
d7ab1bab | 1785 | goto end; |
1786 | Py_INCREF(q); | |
1787 | if (!PySequence_Check(q)) TYERR("want a sequence of moduli"); | |
b17bbe3b | 1788 | n = PySequence_Size(q); if (n < 0) goto end; |
d7ab1bab | 1789 | if (!n) VALERR("want at least one modulus"); |
1790 | v = xmalloc(n * sizeof(*v)); | |
1791 | for (i = 0; i < n; i++) { | |
1792 | if ((x = PySequence_GetItem(q, i)) == 0) goto end; | |
1793 | xx = getmp(x); Py_DECREF(x); if (!xx) goto end; | |
684cd535 | 1794 | if (MP_CMP(xx, <=, MP_ZERO)) VALERR("moduli must be positive"); |
4e02c19c | 1795 | v[i].m = xx; v[i].n = 0; v[i].ni = 0; v[i].nni = 0; xx = MP_NEW; |
d7ab1bab | 1796 | } |
d2b7e92c MW |
1797 | mpmul_init(&mm); |
1798 | for (j = 0; j < i; j++) mpmul_add(&mm, v[j].m); | |
1799 | xx = mpmul_done(&mm); | |
1800 | for (j = 0; j < i; j++) { | |
1801 | mp_div(&y, 0, xx, v[j].m); | |
1802 | mp_gcd(&g, 0, 0, y, v[j].m); | |
1803 | if (!MP_EQ(g, MP_ONE)) VALERR("moduli must be pairwise coprime"); | |
1804 | } | |
1805 | ||
d7ab1bab | 1806 | c = (mpcrt_pyobj *)ty->tp_alloc(ty, 0); |
1807 | mpcrt_create(&c->c, v, n, 0); | |
1808 | Py_DECREF(q); | |
d2b7e92c | 1809 | mp_drop(xx); mp_drop(y); mp_drop(g); |
d7ab1bab | 1810 | return ((PyObject *)c); |
1811 | ||
1812 | end: | |
1813 | if (v) { | |
1814 | n = i; | |
1815 | for (i = 0; i < n; i++) | |
1816 | MP_DROP(v[i].m); | |
1817 | xfree(v); | |
1818 | } | |
1819 | Py_XDECREF(q); | |
d2b7e92c | 1820 | mp_drop(xx); mp_drop(y); mp_drop(g); |
d7ab1bab | 1821 | return (0); |
1822 | } | |
1823 | ||
1824 | static PyObject *mcget_product(PyObject *me, void *hunoz) | |
1825 | { return (mp_pywrap(MP_COPY(MPCRT_PY(me)->mb.m))); } | |
1826 | ||
1827 | static PyObject *mcget_moduli(PyObject *me, void *hunoz) | |
1828 | { | |
1829 | int i; | |
1830 | PyObject *q; | |
1831 | mpcrt *c = MPCRT_PY(me); | |
1832 | ||
1833 | if ((q = PyList_New(c->k)) == 0) return (0); | |
1834 | for (i = 0; i < c->k; i++) | |
8dd6ebb3 | 1835 | PyList_SET_ITEM(q, i, mp_pywrap(c->v[i].m)); |
d7ab1bab | 1836 | return (q); |
1837 | } | |
1838 | ||
637b9140 | 1839 | static const PyGetSetDef mpcrt_pygetset[] = { |
d7ab1bab | 1840 | #define GETSETNAME(op, name) mc##op##_##name |
1841 | GET (product, "C.product -> product of moduli") | |
1842 | GET (moduli, "C.moduli -> list of individual moduli") | |
1843 | #undef GETSETNAME | |
1844 | { 0 } | |
1845 | }; | |
1846 | ||
637b9140 | 1847 | static const PyMethodDef mpcrt_pymethods[] = { |
d7ab1bab | 1848 | #define METHNAME(name) mcmeth_##name |
1849 | METH (solve, "C.solve([R0, R1]) -> X mod C.product") | |
1850 | #undef METHNAME | |
1851 | { 0 } | |
1852 | }; | |
1853 | ||
ddd4720b | 1854 | static const PyTypeObject mpcrt_pytype_skel = { |
4648f560 | 1855 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 1856 | "MPCRT", /* @tp_name@ */ |
d7ab1bab | 1857 | sizeof(mpcrt_pyobj), /* @tp_basicsize@ */ |
1858 | 0, /* @tp_itemsize@ */ | |
1859 | ||
1860 | mpcrt_pydealloc, /* @tp_dealloc@ */ | |
1861 | 0, /* @tp_print@ */ | |
1862 | 0, /* @tp_getattr@ */ | |
1863 | 0, /* @tp_setattr@ */ | |
1864 | 0, /* @tp_compare@ */ | |
1865 | 0, /* @tp_repr@ */ | |
1866 | 0, /* @tp_as_number@ */ | |
1867 | 0, /* @tp_as_sequence@ */ | |
1868 | 0, /* @tp_as_mapping@ */ | |
1869 | 0, /* @tp_hash@ */ | |
1870 | 0, /* @tp_call@ */ | |
1871 | 0, /* @tp_str@ */ | |
1872 | 0, /* @tp_getattro@ */ | |
1873 | 0, /* @tp_setattro@ */ | |
1874 | 0, /* @tp_as_buffer@ */ | |
1875 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1876 | Py_TPFLAGS_BASETYPE, | |
1877 | ||
1878 | /* @tp_doc@ */ | |
ef783f91 | 1879 | "MPCRT(SEQ): a context for solving Chinese Remainder Theorem problems.", |
d7ab1bab | 1880 | |
1881 | 0, /* @tp_traverse@ */ | |
1882 | 0, /* @tp_clear@ */ | |
1883 | 0, /* @tp_richcompare@ */ | |
1884 | 0, /* @tp_weaklistoffset@ */ | |
1885 | 0, /* @tp_iter@ */ | |
963a6148 | 1886 | 0, /* @tp_iternext@ */ |
637b9140 | 1887 | PYMETHODS(mpcrt), /* @tp_methods@ */ |
d7ab1bab | 1888 | 0, /* @tp_members@ */ |
637b9140 | 1889 | PYGETSET(mpcrt), /* @tp_getset@ */ |
d7ab1bab | 1890 | 0, /* @tp_base@ */ |
1891 | 0, /* @tp_dict@ */ | |
1892 | 0, /* @tp_descr_get@ */ | |
1893 | 0, /* @tp_descr_set@ */ | |
1894 | 0, /* @tp_dictoffset@ */ | |
1895 | 0, /* @tp_init@ */ | |
1896 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1897 | mpcrt_pynew, /* @tp_new@ */ | |
3aa33042 | 1898 | 0, /* @tp_free@ */ |
d7ab1bab | 1899 | 0 /* @tp_is_gc@ */ |
1900 | }; | |
1901 | ||
1902 | /*----- Binary polynomials ------------------------------------------------*/ | |
1903 | ||
1904 | static PyObject *gf_pyrepr(PyObject *o) | |
1905 | { return mp_topystring(MP_X(o), 16, "GF(", "0x", "L)"); } | |
1906 | ||
1907 | static PyObject *gf_pyrichcompare(PyObject *x, PyObject *y, int op) | |
1908 | { | |
1909 | mp *xx, *yy; | |
1910 | int xl, yl; | |
1911 | int b; | |
1912 | ||
bf8910bd | 1913 | if (gfbinop(x, y, &xx, &yy)) RETURN_NOTIMPL; |
d7ab1bab | 1914 | switch (op) { |
1915 | case Py_EQ: b = MP_EQ(xx, yy); break; | |
1916 | case Py_NE: b = !MP_EQ(xx, yy); break; | |
1917 | default: | |
1918 | xl = mp_bits(xx); | |
1919 | yl = mp_bits(yy); | |
1920 | switch (op) { | |
1921 | case Py_LT: b = xl < yl; break; | |
1922 | case Py_LE: b = xl <= yl; break; | |
1923 | case Py_GT: b = xl > yl; break; | |
1924 | case Py_GE: b = xl >= yl; break; | |
1925 | default: abort(); | |
1926 | } | |
1927 | break; | |
1928 | } | |
1929 | MP_DROP(xx); MP_DROP(yy); | |
1930 | return (getbool(b)); | |
1931 | } | |
1932 | ||
1933 | static PyObject *gf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
1934 | { | |
1935 | PyObject *x; | |
1936 | mp *z; | |
1937 | mp_pyobj *zz = 0; | |
1938 | int radix = 0; | |
827f89d7 | 1939 | static const char *const kwlist[] = { "x", "radix", 0 }; |
d7ab1bab | 1940 | |
827f89d7 | 1941 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|i:gf", KWLIST, &x, &radix)) |
d7ab1bab | 1942 | goto end; |
1943 | if (GF_PYCHECK(x)) RETURN_OBJ(x); | |
f368b46e | 1944 | if (!good_radix_p(radix, 1)) VALERR("radix out of range"); |
d7ab1bab | 1945 | if ((z = mp_frompyobject(x, radix)) == 0) { |
1946 | PyErr_Format(PyExc_TypeError, "can't convert %.100s to gf", | |
f984b31a | 1947 | Py_TYPE(x)->tp_name); |
d7ab1bab | 1948 | goto end; |
1949 | } | |
1950 | if (MP_NEGP(z)) { | |
1951 | MP_DROP(z); | |
1952 | VALERR("gf cannot be negative"); | |
1953 | } | |
1954 | zz = (mp_pyobj *)ty->tp_alloc(ty, 0); | |
1955 | zz->x = z; | |
1956 | end: | |
1957 | return ((PyObject *)zz); | |
1958 | } | |
1959 | ||
d7ab1bab | 1960 | static PyObject *gf_pyexp(PyObject *x, PyObject *y, PyObject *z) |
1961 | { | |
1962 | mp *xx = 0, *yy = 0, *zz = 0; | |
1963 | mp *r = 0; | |
1964 | PyObject *rc = 0; | |
1965 | ||
1966 | if ((xx = tomp(x)) == 0 || (yy = tomp(y)) == 0 || | |
1967 | (z && z != Py_None && (zz = tomp(z)) == 0)) { | |
1968 | mp_drop(xx); mp_drop(yy); mp_drop(zz); | |
1969 | RETURN_NOTIMPL; | |
1970 | } | |
1971 | if (!z || z == Py_None) { | |
1972 | if (MP_NEGP(yy)) VALERR("negative exponent"); | |
1973 | r = gf_exp(MP_NEW, xx, yy); | |
1974 | } else { | |
1975 | gfreduce gr; | |
1976 | if (MP_ZEROP(zz)) ZDIVERR("zero modulus"); | |
1977 | if (MP_NEGP(yy)) { | |
1978 | if ((xx = gf_modinv_checked(xx, xx, zz)) == 0) goto end; | |
1979 | yy = mp_neg(yy, yy); | |
1980 | } | |
1981 | gfreduce_create(&gr, zz); | |
1982 | r = gfreduce_exp(&gr, MP_NEW, xx, yy); | |
1983 | gfreduce_destroy(&gr); | |
1984 | } | |
1985 | rc = gf_pywrap(r); | |
1986 | end: | |
1987 | mp_drop(xx); mp_drop(yy); mp_drop(zz); | |
1988 | return (rc); | |
1989 | } | |
1990 | ||
7a75bb76 MW |
1991 | static PyObject *gfmeth_sqr(PyObject *me) |
1992 | { return (gf_pywrap(gf_sqr(MP_NEW, MP_X(me)))); } | |
d7ab1bab | 1993 | |
1994 | static PyObject *gfmeth_gcd(PyObject *me, PyObject *arg) | |
1995 | { | |
1996 | PyObject *z = 0; | |
1997 | mp *yy = 0, *zz = MP_NEW; | |
1998 | ||
1999 | if (!PyArg_ParseTuple(arg, "O&:gcd", convgf, &yy)) goto end; | |
2000 | gf_gcd(&zz, 0, 0, MP_X(me), yy); | |
2001 | z = gf_pywrap(zz); | |
2002 | end: | |
2003 | if (yy) MP_DROP(yy); | |
2004 | return (z); | |
2005 | } | |
2006 | ||
2007 | static PyObject *gfmeth_gcdx(PyObject *me, PyObject *arg) | |
2008 | { | |
2009 | PyObject *z = 0; | |
2010 | mp *yy = 0, *zz = MP_NEW, *uu = MP_NEW, *vv = MP_NEW; | |
2011 | ||
2012 | if (!PyArg_ParseTuple(arg, "O&:gcdx", convgf, &yy)) | |
2013 | goto end; | |
2014 | gf_gcd(&zz, &uu, &vv, MP_X(me), yy); | |
2015 | z = Py_BuildValue("(NNN)", | |
2016 | gf_pywrap(zz), gf_pywrap(uu), gf_pywrap(vv)); | |
2017 | end: | |
2018 | if (yy) MP_DROP(yy); | |
2019 | return (z); | |
2020 | } | |
2021 | ||
2022 | static PyObject *gfmeth_modinv(PyObject *me, PyObject *arg) | |
2023 | { | |
2024 | PyObject *z = 0; | |
2025 | mp *yy = 0, *zz = MP_NEW; | |
2026 | ||
2027 | if (!PyArg_ParseTuple(arg, "O&:modinv", convgf, &yy) || | |
2028 | (zz = gf_modinv_checked(MP_NEW, yy, MP_X(me))) == 0) | |
2029 | goto end; | |
2030 | z = gf_pywrap(zz); | |
2031 | end: | |
2032 | if (yy) MP_DROP(yy); | |
2033 | return (z); | |
2034 | } | |
2035 | ||
b16009b0 MW |
2036 | static PyObject *gfmeth_fromstring(PyObject *me, |
2037 | PyObject *arg, PyObject *kw) | |
27cbcf9d MW |
2038 | { |
2039 | int r = 0; | |
2040 | char *p; | |
2041 | Py_ssize_t len; | |
2042 | PyObject *z = 0; | |
2043 | mp *zz; | |
2044 | mptext_stringctx sc; | |
b16009b0 | 2045 | static const char *const kwlist[] = { "x", "radix", 0 }; |
27cbcf9d | 2046 | |
b16009b0 MW |
2047 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|i:fromstring", KWLIST, |
2048 | &p, &len, &r)) | |
27cbcf9d MW |
2049 | goto end; |
2050 | if (!good_radix_p(r, 1)) VALERR("bad radix"); | |
2051 | sc.buf = p; sc.lim = p + len; | |
2052 | if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0 || | |
2053 | MP_NEGP(zz)) { | |
2054 | if (zz) MP_DROP(zz); | |
2055 | VALERR("bad binary polynomial"); | |
2056 | } | |
2057 | z = Py_BuildValue("(Ns#)", gf_pywrap(zz), | |
2058 | sc.buf, (Py_ssize_t)(sc.lim - sc.buf)); | |
2059 | end: | |
2060 | return (z); | |
2061 | } | |
2062 | ||
7a75bb76 MW |
2063 | static PyObject *gfmeth_irreduciblep(PyObject *me) |
2064 | { return getbool(gf_irreduciblep(MP_X(me))); } | |
d7ab1bab | 2065 | |
2066 | static PyObject *gfget_degree(PyObject *me, void *hunoz) | |
2067 | { return (PyInt_FromLong(mp_bits(MP_X(me)) - 1)); } | |
2068 | ||
637b9140 | 2069 | static const PyGetSetDef gf_pygetset[] = { |
d7ab1bab | 2070 | #define GETSETNAME(op, name) gf##op##_##name |
2071 | GET (degree, "X.degree -> polynomial degree of X") | |
2072 | #undef GETSETNAME | |
2073 | #define GETSETNAME(op, name) mp##op##_##name | |
2074 | GET (nbits, "X.nbits -> bit length of X") | |
2075 | GET (noctets, "X.noctets -> octet length of X") | |
2076 | #undef GETSETNAME | |
2077 | { 0 } | |
2078 | }; | |
2079 | ||
637b9140 | 2080 | static const PyMethodDef gf_pymethods[] = { |
d7ab1bab | 2081 | #define METHNAME(func) gfmeth_##func |
2082 | METH (setbit, "X.setbit(N) -> X with bit N set") | |
2083 | METH (clearbit, "X.clearbit(N) -> X with bit N clear") | |
2084 | METH (testbit, "X.testbit(N) -> true/false if bit N set/clear in X") | |
7a75bb76 | 2085 | NAMETH(sqr, "X.sqr() -> X^2") |
d7ab1bab | 2086 | METH (gcd, "X.gcd(Y) -> gcd(X, Y)") |
ef783f91 | 2087 | METH (gcdx, "X.gcdx(Y) -> (gcd(X, Y), U, V) with X U + Y V = gcd(X, Y)") |
d7ab1bab | 2088 | METH (modinv, "X.modinv(Y) -> multiplicative inverse of Y mod X") |
7a75bb76 | 2089 | NAMETH(irreduciblep, "X.irreduciblep() -> true/false") |
b16009b0 MW |
2090 | KWSMTH(fromstring, "fromstring(STR, [radix = 0]) -> (X, REST)\n" |
2091 | " Parse STR as a binary polynomial, according to RADIX. If RADIX is\n" | |
2092 | " zero, read a prefix from STR to decide radix: allow `0b' for binary,\n" | |
2093 | " `0' or `0o' for octal, `0x' for hex, or `R_' for other radix R.") | |
2094 | SMTH (loadl, "loadl(STR) -> X: read little-endian bytes") | |
2095 | SMTH (loadb, "loadb(STR) -> X: read big-endian bytes") | |
2096 | SMTH (frombuf, "frombuf(STR) -> (X, REST): read buffer format") | |
d7ab1bab | 2097 | #undef METHNAME |
2098 | #define METHNAME(func) mpmeth_##func | |
1df8d5fe MW |
2099 | KWMETH(tostring, "X.tostring([radix = 10]) -> STR") |
2100 | KWMETH(storel, "X.storel([len = -1]) -> little-endian bytes") | |
2101 | KWMETH(storeb, "X.storeb([len = -1]) -> big-endian bytes") | |
ef783f91 MW |
2102 | KWMETH(storel2c, "X.storel2c([len = -1]) -> " |
2103 | "little-endian bytes, two's complement") | |
2104 | KWMETH(storeb2c, "X.storeb2c([len = -1]) -> " | |
2105 | "big-endian bytes, two's complement") | |
7a75bb76 | 2106 | NAMETH(tobuf, "X.tobuf() -> buffer format") |
d7ab1bab | 2107 | #undef METHNAME |
2108 | { 0 } | |
2109 | }; | |
2110 | ||
637b9140 | 2111 | static const PyNumberMethods gf_pynumber = { |
d7ab1bab | 2112 | gf_pyadd, /* @nb_add@ */ |
2113 | gf_pysub, /* @nb_subtract@ */ | |
2114 | gf_pymul, /* @nb_multiply@ */ | |
2115 | 0, /* @nb_divide@ */ | |
2116 | gf_pymod, /* @nb_remainder@ */ | |
2117 | gf_pydivmod, /* @nb_divmod@ */ | |
2118 | gf_pyexp, /* @nb_power@ */ | |
2119 | mp_pyid, /* @nb_negative@ */ | |
2120 | mp_pyid, /* @nb_positive@ */ | |
2121 | mp_pyid, /* @nb_absolute@ */ | |
2122 | mp_pynonzerop, /* @nb_nonzero@ */ | |
2123 | 0 /* doesn't make any sense */, /* @nb_invert@ */ | |
2124 | gf_pylsl, /* @nb_lshift@ */ | |
2125 | gf_pylsr, /* @nb_rshift@ */ | |
2126 | gf_pyand, /* @nb_and@ */ | |
2127 | gf_pyxor, /* @nb_xor@ */ | |
2128 | gf_pyor, /* @nb_or@ */ | |
2129 | gf_pycoerce, /* @nb_coerce@ */ | |
2130 | mp_pyint, /* @nb_int@ */ | |
2131 | mp_pylong, /* @nb_long@ */ | |
2132 | 0 /* doesn't make any sense */, /* @nb_float@ */ | |
2133 | mp_pyoct, /* @nb_oct@ */ | |
2134 | mp_pyhex, /* @nb_hex@ */ | |
2135 | ||
2136 | 0, /* @nb_inplace_add@ */ | |
2137 | 0, /* @nb_inplace_subtract@ */ | |
2138 | 0, /* @nb_inplace_multiply@ */ | |
2139 | 0, /* @nb_inplace_divide@ */ | |
2140 | 0, /* @nb_inplace_remainder@ */ | |
2141 | 0, /* @nb_inplace_power@ */ | |
2142 | 0, /* @nb_inplace_lshift@ */ | |
2143 | 0, /* @nb_inplace_rshift@ */ | |
2144 | 0, /* @nb_inplace_and@ */ | |
2145 | 0, /* @nb_inplace_xor@ */ | |
2146 | 0, /* @nb_inplace_or@ */ | |
2147 | ||
2148 | gf_pydiv, /* @nb_floor_divide@ */ | |
2149 | 0, /* @nb_true_divide@ */ | |
2150 | 0, /* @nb_inplace_floor_divide@ */ | |
2151 | 0, /* @nb_inplace_true_divide@ */ | |
2152 | }; | |
2153 | ||
ddd4720b | 2154 | static const PyTypeObject gf_pytype_skel = { |
4648f560 | 2155 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 2156 | "GF", /* @tp_name@ */ |
d7ab1bab | 2157 | sizeof(mp_pyobj), /* @tp_basicsize@ */ |
2158 | 0, /* @tp_itemsize@ */ | |
2159 | ||
2160 | mp_pydealloc, /* @tp_dealloc@ */ | |
2161 | 0, /* @tp_print@ */ | |
2162 | 0, /* @tp_getattr@ */ | |
2163 | 0, /* @tp_setattr@ */ | |
2164 | 0, /* @tp_compare@ */ | |
2165 | gf_pyrepr, /* @tp_repr@ */ | |
637b9140 | 2166 | PYNUMBER(gf), /* @tp_as_number@ */ |
d7ab1bab | 2167 | 0, /* @tp_as_sequence@ */ |
2168 | 0, /* @tp_as_mapping@ */ | |
ab723d73 | 2169 | mp_pyhash, /* @tp_hash@ */ |
d7ab1bab | 2170 | 0, /* @tp_call@ */ |
2171 | mp_pyhex, /* @tp_str@ */ | |
2172 | 0, /* @tp_getattro@ */ | |
2173 | 0, /* @tp_setattro@ */ | |
2174 | 0, /* @tp_as_buffer@ */ | |
2175 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2176 | Py_TPFLAGS_CHECKTYPES | | |
2177 | Py_TPFLAGS_BASETYPE, | |
2178 | ||
2179 | /* @tp_doc@ */ | |
ef783f91 MW |
2180 | "Binary polynomials. Support almost all the standard arithmetic\n" |
2181 | "operations.\n" | |
2182 | "\n" | |
2183 | "Constructor GF(X, [radix = R]) attempts to convert X to a `GF'. If\n" | |
2184 | "X is a string, it's read in radix-R form, or we look for a prefix\n" | |
2185 | "if R = 0. Other acceptable things are field elements, elliptic curve\n" | |
2186 | "points, group elements, Python `int' and `long' objects, and anything\n" | |
2187 | "with an integer conversion.\n" | |
2188 | "\n" | |
2189 | "The name is hopelessly wrong from a technical point of view, but\n" | |
2190 | "but it's much easier to type than `p2' or `c2' or whatever.\n" | |
2191 | "\n" | |
2192 | "Notes:\n" | |
2193 | "\n" | |
2194 | " * Use `//' for Euclidean division: `/' gives exact rational division.", | |
d7ab1bab | 2195 | |
2196 | 0, /* @tp_traverse@ */ | |
2197 | 0, /* @tp_clear@ */ | |
2198 | gf_pyrichcompare, /* @tp_richcompare@ */ | |
2199 | 0, /* @tp_weaklistoffset@ */ | |
2200 | 0, /* @tp_iter@ */ | |
963a6148 | 2201 | 0, /* @tp_iternext@ */ |
637b9140 | 2202 | PYMETHODS(gf), /* @tp_methods@ */ |
d7ab1bab | 2203 | 0, /* @tp_members@ */ |
637b9140 | 2204 | PYGETSET(gf), /* @tp_getset@ */ |
d7ab1bab | 2205 | 0, /* @tp_base@ */ |
2206 | 0, /* @tp_dict@ */ | |
2207 | 0, /* @tp_descr_get@ */ | |
2208 | 0, /* @tp_descr_set@ */ | |
2209 | 0, /* @tp_dictoffset@ */ | |
2210 | 0, /* @tp_init@ */ | |
2211 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2212 | gf_pynew, /* @tp_new@ */ | |
3aa33042 | 2213 | 0, /* @tp_free@ */ |
d7ab1bab | 2214 | 0 /* @tp_is_gc@ */ |
2215 | }; | |
2216 | ||
d7ab1bab | 2217 | /*----- Sparse poly reduction ---------------------------------------------*/ |
2218 | ||
ddd4720b MW |
2219 | static PyTypeObject *gfreduce_pytype; |
2220 | ||
d7ab1bab | 2221 | typedef struct gfreduce_pyobj { |
2222 | PyObject_HEAD | |
2223 | gfreduce mr; | |
2224 | } gfreduce_pyobj; | |
2225 | ||
2226 | #define GFREDUCE_PY(o) (&((gfreduce_pyobj *)(o))->mr) | |
2227 | ||
2228 | static PyObject *grmeth_exp(PyObject *me, PyObject *arg) | |
2229 | { | |
2230 | PyObject *rc = 0; | |
2231 | mp *yy = 0, *zz = 0; | |
2232 | ||
2233 | if (!PyArg_ParseTuple(arg, "O&O&:exp", convgf, &yy, convgf, &zz)) | |
2234 | goto end; | |
2235 | if (MP_NEGP(zz)) { | |
2236 | if ((yy = gf_modinv_checked(yy, yy, GFREDUCE_PY(me)->p)) == 0) goto end; | |
2237 | zz = mp_neg(zz, zz); | |
2238 | } | |
2239 | rc = gf_pywrap(gfreduce_exp(GFREDUCE_PY(me), MP_NEW, yy, zz)); | |
2240 | end: | |
2241 | if (yy) MP_DROP(yy); if (zz) MP_DROP(zz); | |
2242 | return (rc); | |
2243 | } | |
2244 | ||
fbc145f3 MW |
2245 | static PyObject *grmeth_trace(PyObject *me, PyObject *arg) |
2246 | { | |
2247 | PyObject *rc = 0; | |
2248 | mp *xx = 0; | |
2249 | ||
2250 | if (!PyArg_ParseTuple(arg, "O&:trace", convgf, &xx)) goto end; | |
2251 | rc = PyInt_FromLong(gfreduce_trace(GFREDUCE_PY(me), xx)); | |
2252 | end: | |
2253 | if (xx) MP_DROP(xx); | |
2254 | return (rc); | |
2255 | } | |
2256 | ||
2257 | static PyObject *grmeth_halftrace(PyObject *me, PyObject *arg) | |
2258 | { | |
2259 | PyObject *rc = 0; | |
2260 | mp *xx = 0; | |
2261 | ||
2262 | if (!PyArg_ParseTuple(arg, "O&:halftrace", convgf, &xx)) goto end; | |
2263 | rc = gf_pywrap(gfreduce_halftrace(GFREDUCE_PY(me), MP_NEW, xx)); | |
2264 | end: | |
2265 | if (xx) MP_DROP(xx); | |
2266 | return (rc); | |
2267 | } | |
2268 | ||
2269 | static PyObject *grmeth_sqrt(PyObject *me, PyObject *arg) | |
2270 | { | |
2271 | PyObject *rc = 0; | |
2272 | mp *xx = 0, *yy; | |
2273 | ||
2274 | if (!PyArg_ParseTuple(arg, "O&:sqrt", convgf, &xx)) goto end; | |
2275 | if ((yy = gfreduce_sqrt(GFREDUCE_PY(me), MP_NEW, xx)) == 0) | |
2276 | VALERR("no modular square root"); | |
2277 | rc = gf_pywrap(yy); | |
2278 | end: | |
2279 | if (xx) MP_DROP(xx); | |
2280 | return (rc); | |
2281 | } | |
2282 | ||
2283 | static PyObject *grmeth_quadsolve(PyObject *me, PyObject *arg) | |
2284 | { | |
2285 | PyObject *rc = 0; | |
2286 | mp *xx = 0, *yy; | |
2287 | ||
2288 | if (!PyArg_ParseTuple(arg, "O&:quadsolve", convgf, &xx)) goto end; | |
2289 | if ((yy = gfreduce_quadsolve(GFREDUCE_PY(me), MP_NEW, xx)) == 0) | |
2290 | VALERR("no solution found"); | |
2291 | rc = gf_pywrap(yy); | |
2292 | end: | |
2293 | if (xx) MP_DROP(xx); | |
2294 | return (rc); | |
2295 | } | |
2296 | ||
d7ab1bab | 2297 | static PyObject *grmeth_reduce(PyObject *me, PyObject *arg) |
2298 | { | |
2299 | PyObject *z = 0; | |
2300 | mp *yy = 0; | |
2301 | ||
2302 | if (!PyArg_ParseTuple(arg, "O&:reduce", convgf, &yy)) goto end; | |
2303 | z = gf_pywrap(gfreduce_do(GFREDUCE_PY(me), MP_NEW, yy)); | |
2304 | end: | |
2305 | return (z); | |
2306 | } | |
2307 | ||
2308 | static void gfreduce_pydealloc(PyObject *me) | |
2309 | { | |
2310 | gfreduce_destroy(GFREDUCE_PY(me)); | |
3aa33042 | 2311 | FREEOBJ(me); |
d7ab1bab | 2312 | } |
2313 | ||
2314 | static PyObject *gfreduce_pynew(PyTypeObject *ty, | |
2315 | PyObject *arg, PyObject *kw) | |
2316 | { | |
2317 | gfreduce_pyobj *mr = 0; | |
2318 | gfreduce r; | |
827f89d7 | 2319 | static const char *const kwlist[] = { "m", 0 }; |
d7ab1bab | 2320 | mp *xx = 0; |
2321 | ||
827f89d7 | 2322 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convgf, &xx)) |
d7ab1bab | 2323 | goto end; |
2324 | if (MP_ZEROP(xx)) ZDIVERR("modulus is zero!"); | |
2325 | gfreduce_create(&r, xx); | |
2326 | mr = (gfreduce_pyobj *)ty->tp_alloc(ty, 0); | |
2327 | mr->mr = r; | |
2328 | end: | |
2329 | if (xx) MP_DROP(xx); | |
2330 | return ((PyObject *)mr); | |
2331 | } | |
2332 | ||
2333 | static PyObject *grget_m(PyObject *me, void *hunoz) | |
2334 | { return (gf_pywrap(MP_COPY(GFREDUCE_PY(me)->p))); } | |
2335 | ||
637b9140 | 2336 | static const PyGetSetDef gfreduce_pygetset[] = { |
d7ab1bab | 2337 | #define GETSETNAME(op, name) gr##op##_##name |
2338 | GET (m, "R.m -> reduction polynomial") | |
2339 | #undef GETSETNAME | |
2340 | { 0 } | |
2341 | }; | |
2342 | ||
637b9140 | 2343 | static const PyMethodDef gfreduce_pymethods[] = { |
d7ab1bab | 2344 | #define METHNAME(name) grmeth_##name |
2345 | METH (reduce, "R.reduce(X) -> X mod B.m") | |
fbc145f3 | 2346 | METH (trace, "R.trace(X) -> Tr(X) = x + x^2 + ... + x^{2^{m - 1}}") |
ef783f91 | 2347 | METH (halftrace, "R.halftrace(X) -> x + x^{2^2} + ... + x^{2^{m - 1}}") |
fbc145f3 MW |
2348 | METH (sqrt, "R.sqrt(X) -> Y where Y^2 = X mod R") |
2349 | METH (quadsolve, "R.quadsolve(X) -> Y where Y^2 + Y = X mod R") | |
d7ab1bab | 2350 | METH (exp, "R.exp(X, N) -> X^N mod B.m") |
2351 | #undef METHNAME | |
2352 | { 0 } | |
2353 | }; | |
2354 | ||
ddd4720b | 2355 | static const PyTypeObject gfreduce_pytype_skel = { |
4648f560 | 2356 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 2357 | "GFReduce", /* @tp_name@ */ |
d7ab1bab | 2358 | sizeof(gfreduce_pyobj), /* @tp_basicsize@ */ |
2359 | 0, /* @tp_itemsize@ */ | |
2360 | ||
2361 | gfreduce_pydealloc, /* @tp_dealloc@ */ | |
2362 | 0, /* @tp_print@ */ | |
2363 | 0, /* @tp_getattr@ */ | |
2364 | 0, /* @tp_setattr@ */ | |
2365 | 0, /* @tp_compare@ */ | |
2366 | 0, /* @tp_repr@ */ | |
2367 | 0, /* @tp_as_number@ */ | |
2368 | 0, /* @tp_as_sequence@ */ | |
2369 | 0, /* @tp_as_mapping@ */ | |
2370 | 0, /* @tp_hash@ */ | |
2371 | 0, /* @tp_call@ */ | |
2372 | 0, /* @tp_str@ */ | |
2373 | 0, /* @tp_getattro@ */ | |
2374 | 0, /* @tp_setattro@ */ | |
2375 | 0, /* @tp_as_buffer@ */ | |
2376 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2377 | Py_TPFLAGS_BASETYPE, | |
2378 | ||
2379 | /* @tp_doc@ */ | |
ef783f91 | 2380 | "GFReduce(N): a context for reduction modulo sparse polynomials.", |
d7ab1bab | 2381 | |
2382 | 0, /* @tp_traverse@ */ | |
2383 | 0, /* @tp_clear@ */ | |
2384 | 0, /* @tp_richcompare@ */ | |
2385 | 0, /* @tp_weaklistoffset@ */ | |
2386 | 0, /* @tp_iter@ */ | |
963a6148 | 2387 | 0, /* @tp_iternext@ */ |
637b9140 | 2388 | PYMETHODS(gfreduce), /* @tp_methods@ */ |
d7ab1bab | 2389 | 0, /* @tp_members@ */ |
637b9140 | 2390 | PYGETSET(gfreduce), /* @tp_getset@ */ |
d7ab1bab | 2391 | 0, /* @tp_base@ */ |
2392 | 0, /* @tp_dict@ */ | |
2393 | 0, /* @tp_descr_get@ */ | |
2394 | 0, /* @tp_descr_set@ */ | |
2395 | 0, /* @tp_dictoffset@ */ | |
2396 | 0, /* @tp_init@ */ | |
2397 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2398 | gfreduce_pynew, /* @tp_new@ */ | |
3aa33042 | 2399 | 0, /* @tp_free@ */ |
d7ab1bab | 2400 | 0 /* @tp_is_gc@ */ |
2401 | }; | |
2402 | ||
2403 | /*----- Normal/poly transformation ----------------------------------------*/ | |
2404 | ||
ddd4720b MW |
2405 | static PyTypeObject *gfn_pytype; |
2406 | ||
d7ab1bab | 2407 | typedef struct gfn_pyobj { |
2408 | PyObject_HEAD | |
2409 | mp *p; | |
2410 | gfn ntop, pton; | |
2411 | } gfn_pyobj; | |
2412 | ||
d7ab1bab | 2413 | #define GFN_P(o) (((gfn_pyobj *)(o))->p) |
2414 | #define GFN_PTON(o) (&((gfn_pyobj *)(o))->pton) | |
2415 | #define GFN_NTOP(o) (&((gfn_pyobj *)(o))->ntop) | |
2416 | ||
2417 | static PyObject *gfn_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
2418 | { | |
2419 | mp *p = 0, *beta = 0; | |
2420 | gfn_pyobj *gg = 0; | |
827f89d7 | 2421 | static const char *const kwlist[] = { "p", "beta", 0 }; |
d7ab1bab | 2422 | |
827f89d7 | 2423 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", KWLIST, |
d7ab1bab | 2424 | convgf, &p, convgf, &beta)) |
2425 | goto end; | |
2426 | gg = PyObject_New(gfn_pyobj, ty); | |
79ac6c22 | 2427 | gg->p = 0; |
d7ab1bab | 2428 | if (gfn_create(p, beta, &gg->ntop, &gg->pton)) { |
79ac6c22 | 2429 | Py_DECREF(gg); |
d7ab1bab | 2430 | gg = 0; |
2431 | VALERR("can't invert transformation matrix"); | |
2432 | } | |
2433 | gg->p = MP_COPY(p); | |
2434 | end: | |
2435 | mp_drop(p); | |
2436 | mp_drop(beta); | |
2437 | return ((PyObject *)gg); | |
2438 | } | |
2439 | ||
2440 | static PyObject *gfnget_p(PyObject *me, void *hunoz) | |
2441 | { return (gf_pywrap(MP_COPY(GFN_P(me)))); } | |
2442 | ||
2443 | static PyObject *gfnget_beta(PyObject *me, void *hunoz) | |
2444 | { | |
2445 | gfn *n = GFN_NTOP(me); | |
2446 | mp *x = n->r[n->n - 1]; | |
2447 | return (gf_pywrap(MP_COPY(x))); | |
2448 | } | |
2449 | ||
2450 | #define XFORMOP(name, NAME) \ | |
2451 | static PyObject *gfnmeth_##name(PyObject *me, PyObject *arg) \ | |
2452 | { \ | |
2453 | mp *xx = 0; \ | |
2454 | mp *z = 0; \ | |
2455 | \ | |
2456 | if (!PyArg_ParseTuple(arg, "O&:" #name, convgf, &xx)) goto end; \ | |
2457 | z = gfn_transform(GFN_##NAME(me), MP_NEW, xx); \ | |
2458 | end: \ | |
2459 | mp_drop(xx); \ | |
2460 | if (!z) return (0); \ | |
62ce807d | 2461 | return (gf_pywrap(z)); \ |
d7ab1bab | 2462 | } |
2463 | XFORMOP(pton, PTON) | |
2464 | XFORMOP(ntop, NTOP) | |
2465 | #undef XFORMOP | |
2466 | ||
2467 | static void gfn_pydealloc(PyObject *me) | |
2468 | { | |
79ac6c22 | 2469 | if (GFN_P(me)) { |
b76bb841 | 2470 | MP_DROP(GFN_P(me)); |
79ac6c22 MW |
2471 | gfn_destroy(GFN_PTON(me)); |
2472 | gfn_destroy(GFN_NTOP(me)); | |
2473 | } | |
3aa33042 | 2474 | FREEOBJ(me); |
d7ab1bab | 2475 | } |
2476 | ||
637b9140 | 2477 | static const PyGetSetDef gfn_pygetset[] = { |
d7ab1bab | 2478 | #define GETSETNAME(op, name) gfn##op##_##name |
2479 | GET (p, "X.p -> polynomial basis, as polynomial") | |
2480 | GET (beta, "X.beta -> normal basis element, in poly form") | |
2481 | #undef GETSETNAME | |
2482 | { 0 } | |
2483 | }; | |
2484 | ||
637b9140 | 2485 | static const PyMethodDef gfn_pymethods[] = { |
d7ab1bab | 2486 | #define METHNAME(name) gfnmeth_##name |
2487 | METH (pton, "X.pton(A) -> normal-basis representation of A") | |
2488 | METH (ntop, "X.ntop(A) -> polynomial-basis representation of A") | |
2489 | #undef METHNAME | |
2490 | { 0 } | |
2491 | }; | |
2492 | ||
ddd4720b | 2493 | static const PyTypeObject gfn_pytype_skel = { |
4648f560 | 2494 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 2495 | "GFN", /* @tp_name@ */ |
d7ab1bab | 2496 | sizeof(gfn_pyobj), /* @tp_basicsize@ */ |
2497 | 0, /* @tp_itemsize@ */ | |
2498 | ||
2499 | gfn_pydealloc, /* @tp_dealloc@ */ | |
2500 | 0, /* @tp_print@ */ | |
2501 | 0, /* @tp_getattr@ */ | |
2502 | 0, /* @tp_setattr@ */ | |
2503 | 0, /* @tp_compare@ */ | |
2504 | 0, /* @tp_repr@ */ | |
2505 | 0, /* @tp_as_number@ */ | |
2506 | 0, /* @tp_as_sequence@ */ | |
2507 | 0, /* @tp_as_mapping@ */ | |
2508 | 0, /* @tp_hash@ */ | |
2509 | 0, /* @tp_call@ */ | |
2510 | 0, /* @tp_str@ */ | |
2511 | 0, /* @tp_getattro@ */ | |
2512 | 0, /* @tp_setattro@ */ | |
2513 | 0, /* @tp_as_buffer@ */ | |
2514 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2515 | Py_TPFLAGS_BASETYPE, | |
2516 | ||
2517 | /* @tp_doc@ */ | |
ef783f91 MW |
2518 | "GFN(P, BETA): an object for transforming elements of binary fields\n" |
2519 | " between polynomial and normal basis representations.", | |
d7ab1bab | 2520 | |
2521 | 0, /* @tp_traverse@ */ | |
2522 | 0, /* @tp_clear@ */ | |
2523 | 0, /* @tp_richcompare@ */ | |
2524 | 0, /* @tp_weaklistoffset@ */ | |
2525 | 0, /* @tp_iter@ */ | |
963a6148 | 2526 | 0, /* @tp_iternext@ */ |
637b9140 | 2527 | PYMETHODS(gfn), /* @tp_methods@ */ |
d7ab1bab | 2528 | 0, /* @tp_members@ */ |
637b9140 | 2529 | PYGETSET(gfn), /* @tp_getset@ */ |
d7ab1bab | 2530 | 0, /* @tp_base@ */ |
2531 | 0, /* @tp_dict@ */ | |
2532 | 0, /* @tp_descr_get@ */ | |
2533 | 0, /* @tp_descr_set@ */ | |
2534 | 0, /* @tp_dictoffset@ */ | |
2535 | 0, /* @tp_init@ */ | |
2536 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2537 | gfn_pynew, /* @tp_new@ */ | |
3aa33042 | 2538 | 0, /* @tp_free@ */ |
d7ab1bab | 2539 | 0 /* @tp_is_gc@ */ |
2540 | }; | |
2541 | ||
2542 | /*----- Glue --------------------------------------------------------------*/ | |
2543 | ||
cc36f2d8 MW |
2544 | static const struct nameval consts[] = { |
2545 | CONST(MPW_MAX), | |
2546 | { 0 } | |
2547 | }; | |
2548 | ||
d7ab1bab | 2549 | void mp_pyinit(void) |
2550 | { | |
2551 | INITTYPE(mp, root); | |
2552 | INITTYPE(gf, root); | |
32507e35 | 2553 | INITTYPE(mpmul, root); |
d7ab1bab | 2554 | INITTYPE(mpmont, root); |
2555 | INITTYPE(mpbarrett, root); | |
2556 | INITTYPE(mpreduce, root); | |
2557 | INITTYPE(mpcrt, root); | |
2558 | INITTYPE(gfreduce, root); | |
2559 | INITTYPE(gfn, root); | |
d7ab1bab | 2560 | } |
2561 | ||
2562 | void mp_pyinsert(PyObject *mod) | |
2563 | { | |
2564 | INSERT("MP", mp_pytype); | |
32507e35 | 2565 | INSERT("MPMul", mpmul_pytype); |
d7ab1bab | 2566 | INSERT("MPMont", mpmont_pytype); |
2567 | INSERT("MPBarrett", mpbarrett_pytype); | |
2568 | INSERT("MPReduce", mpreduce_pytype); | |
2569 | INSERT("MPCRT", mpcrt_pytype); | |
2570 | INSERT("GF", gf_pytype); | |
2571 | INSERT("GFReduce", gfreduce_pytype); | |
2572 | INSERT("GFN", gfn_pytype); | |
cc36f2d8 | 2573 | setconstants(mod, consts); |
d7ab1bab | 2574 | } |
2575 | ||
2576 | /*----- That's all, folks -------------------------------------------------*/ |