X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/4e02c19cca797d71f0479e9dea0bf07b61388dd5..08720fe03bb49ed4df3b0fbd99c2c8eec145fdcc:/mp.c diff --git a/mp.c b/mp.c index fff4aec..ab15d3f 100644 --- a/mp.c +++ b/mp.c @@ -211,6 +211,7 @@ mp *tomp(PyObject *o) return (MP_COPY(PFILT_F(o)->m)); else if (ECPT_PYCHECK(o)) { ec p = EC_INIT; + if (EC_ATINF(ECPT_P(o))) return (0); getecptout(&p, o); x = MP_COPY(p.x); EC_DESTROY(&p); @@ -1679,7 +1680,7 @@ static PyObject *mcmeth_solve(PyObject *me, PyObject *arg) PyObject *q = 0, *x, *z = 0; mp *xx; mp **v = 0; - int i = 0, n = c->k; + Py_ssize_t i = 0, n = c->k; Py_INCREF(me); if (PyTuple_Size(arg) == n) @@ -1688,7 +1689,8 @@ static PyObject *mcmeth_solve(PyObject *me, PyObject *arg) goto end; Py_INCREF(q); if (!PySequence_Check(q)) TYERR("want a sequence of residues"); - if (PySequence_Size(q) != n) VALERR("residue count mismatch"); + i = PySequence_Size(q); if (i < 0) goto end; + if (i != n) VALERR("residue count mismatch"); v = xmalloc(n * sizeof(*v)); for (i = 0; i < n; i++) { if ((x = PySequence_GetItem(q, i)) == 0) goto end; @@ -1718,10 +1720,11 @@ static void mpcrt_pydealloc(PyObject *me) static PyObject *mpcrt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { mpcrt_mod *v = 0; - int n, i = 0; + Py_ssize_t n, i = 0, j; char *kwlist[] = { "mv", 0 }; PyObject *q = 0, *x; - mp *xx = MP_NEW; + mp *xx = MP_NEW, *y = MP_NEW, *g = MP_NEW; + mpmul mm; mpcrt_pyobj *c = 0; if (PyTuple_Size(arg) > 1) @@ -1730,8 +1733,7 @@ static PyObject *mpcrt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) goto end; Py_INCREF(q); if (!PySequence_Check(q)) TYERR("want a sequence of moduli"); - n = PySequence_Size(q); - if (PyErr_Occurred()) goto end; + n = PySequence_Size(q); if (n < 0) goto end; if (!n) VALERR("want at least one modulus"); v = xmalloc(n * sizeof(*v)); for (i = 0; i < n; i++) { @@ -1740,10 +1742,19 @@ static PyObject *mpcrt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) if (MP_CMP(xx, <=, MP_ZERO)) VALERR("moduli must be positive"); v[i].m = xx; v[i].n = 0; v[i].ni = 0; v[i].nni = 0; xx = MP_NEW; } + mpmul_init(&mm); + for (j = 0; j < i; j++) mpmul_add(&mm, v[j].m); + xx = mpmul_done(&mm); + for (j = 0; j < i; j++) { + mp_div(&y, 0, xx, v[j].m); + mp_gcd(&g, 0, 0, y, v[j].m); + if (!MP_EQ(g, MP_ONE)) VALERR("moduli must be pairwise coprime"); + } + c = (mpcrt_pyobj *)ty->tp_alloc(ty, 0); mpcrt_create(&c->c, v, n, 0); Py_DECREF(q); - mp_drop(xx); + mp_drop(xx); mp_drop(y); mp_drop(g); return ((PyObject *)c); end: @@ -1754,7 +1765,7 @@ end: xfree(v); } Py_XDECREF(q); - mp_drop(xx); + mp_drop(xx); mp_drop(y); mp_drop(g); return (0); } @@ -2359,8 +2370,9 @@ static PyObject *gfn_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) convgf, &p, convgf, &beta)) goto end; gg = PyObject_New(gfn_pyobj, ty); + gg->p = 0; if (gfn_create(p, beta, &gg->ntop, &gg->pton)) { - FREEOBJ(gg); + Py_DECREF(gg); gg = 0; VALERR("can't invert transformation matrix"); } @@ -2392,7 +2404,7 @@ static PyObject *gfnget_beta(PyObject *me, void *hunoz) end: \ mp_drop(xx); \ if (!z) return (0); \ - return (mp_pywrap(z)); \ + return (gf_pywrap(z)); \ } XFORMOP(pton, PTON) XFORMOP(ntop, NTOP) @@ -2400,8 +2412,11 @@ XFORMOP(ntop, NTOP) static void gfn_pydealloc(PyObject *me) { - gfn_destroy(GFN_PTON(me)); - gfn_destroy(GFN_NTOP(me)); + if (GFN_P(me)) { + MP_DROP(GFN_P(me)); + gfn_destroy(GFN_PTON(me)); + gfn_destroy(GFN_NTOP(me)); + } FREEOBJ(me); }