X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/c306066969fa47d1f0a82c92b1c56a3fd6eb3b87..4e02c19cca797d71f0479e9dea0bf07b61388dd5:/mp.c diff --git a/mp.c b/mp.c index 2aa4cab..fff4aec 100644 --- a/mp.c +++ b/mp.c @@ -809,14 +809,14 @@ static PyMethodDef mp_pymethods[] = { METH (modsqrt, "X.modsqrt(Y) -> square root of Y mod X, if X prime") METH (leastcongruent, "X.leastcongruent(B, M) -> smallest Z >= B with Z == X (mod M)") - KWMETH(primep, "X.primep(rng = rand) -> true/false if X is prime") - KWMETH(tostring, "X.tostring(radix = 10) -> STR") - KWMETH(storel, "X.storel(len = -1) -> little-endian bytes") - KWMETH(storeb, "X.storeb(len = -1) -> big-endian bytes") + KWMETH(primep, "X.primep([rng = rand]) -> true/false if X is prime") + KWMETH(tostring, "X.tostring([radix = 10]) -> STR") + KWMETH(storel, "X.storel([len = -1]) -> little-endian bytes") + KWMETH(storeb, "X.storeb([len = -1]) -> big-endian bytes") KWMETH(storel2c, - "X.storel2c(len = -1) -> little-endian bytes, two's complement") + "X.storel2c([len = -1]) -> little-endian bytes, two's complement") KWMETH(storeb2c, - "X.storeb2c(len = -1) -> big-endian bytes, two's complement") + "X.storeb2c([len = -1]) -> big-endian bytes, two's complement") METH (tobuf, "X.tobuf() -> buffer format") #undef METHNAME { 0 } @@ -896,7 +896,7 @@ versatile. Support all the standard arithmetic operations, with\n\ implicit conversions from `PrimeFilter', and other objects which\n\ convert to `long'.\n\ \n\ -Constructor MP(X, radix = R) attempts to convert X to an `MP'. If\n\ +Constructor MP(X, [radix = R]) attempts to convert X to an `MP'. If\n\ X is a string, it's read in radix-R form, or we look for a prefix\n\ if R = 0. Other acceptable things are field elements, elliptic curve\n\ points, group elements, Python `int' and `long' objects, and anything\n\ @@ -1721,7 +1721,7 @@ static PyObject *mpcrt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) int n, i = 0; char *kwlist[] = { "mv", 0 }; PyObject *q = 0, *x; - mp *xx; + mp *xx = MP_NEW; mpcrt_pyobj *c = 0; if (PyTuple_Size(arg) > 1) @@ -1737,11 +1737,13 @@ static PyObject *mpcrt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) for (i = 0; i < n; i++) { if ((x = PySequence_GetItem(q, i)) == 0) goto end; xx = getmp(x); Py_DECREF(x); if (!xx) goto end; - v[i].m = xx; v[i].n = 0; v[i].ni = 0; v[i].nni = 0; + 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; } c = (mpcrt_pyobj *)ty->tp_alloc(ty, 0); mpcrt_create(&c->c, v, n, 0); Py_DECREF(q); + mp_drop(xx); return ((PyObject *)c); end: @@ -1752,6 +1754,7 @@ end: xfree(v); } Py_XDECREF(q); + mp_drop(xx); return (0); } @@ -2003,13 +2006,13 @@ static PyMethodDef gf_pymethods[] = { METH (irreduciblep, "X.irreduciblep() -> true/false") #undef METHNAME #define METHNAME(func) mpmeth_##func - KWMETH(tostring, "X.tostring(radix = 10) -> STR") - KWMETH(storel, "X.storel(len = -1) -> little-endian bytes") - KWMETH(storeb, "X.storeb(len = -1) -> big-endian bytes") + KWMETH(tostring, "X.tostring([radix = 10]) -> STR") + KWMETH(storel, "X.storel([len = -1]) -> little-endian bytes") + KWMETH(storeb, "X.storeb([len = -1]) -> big-endian bytes") KWMETH(storel2c, - "X.storel2c(len = -1) -> little-endian bytes, two's complement") + "X.storel2c([len = -1]) -> little-endian bytes, two's complement") KWMETH(storeb2c, - "X.storeb2c(len = -1) -> big-endian bytes, two's complement") + "X.storeb2c([len = -1]) -> big-endian bytes, two's complement") METH (tobuf, "X.tobuf() -> buffer format") #undef METHNAME { 0 } @@ -2087,7 +2090,7 @@ static PyTypeObject gf_pytype_skel = { "Binary polynomials. Support almost all the standard arithmetic\n\ operations.\n\ \n\ -Constructor GF(X, radix = R) attempts to convert X to a `GF'. If\n\ +Constructor GF(X, [radix = R]) attempts to convert X to a `GF'. If\n\ X is a string, it's read in radix-R form, or we look for a prefix\n\ if R = 0. Other acceptable things are field elements, elliptic curve\n\ points, group elements, Python `int' and `long' objects, and anything\n\ @@ -2472,13 +2475,13 @@ and normal basis representations.", static PyMethodDef methods[] = { #define METHNAME(func) meth_##func KWMETH(_MP_fromstring, "\ -fromstring(STR, radix = 0) -> (X, REST)\n\ +fromstring(STR, [radix = 0]) -> (X, REST)\n\ \n\ Parse STR as a large integer, according to radix. If radix is zero,\n\ read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\ or `R_' for other radix R.") KWMETH(_GF_fromstring, "\ -fromstring(STR, radix = 0) -> (X, REST)\n\ +fromstring(STR, [radix = 0]) -> (X, REST)\n\ \n\ Parse STR as a binary polynomial, according to radix. If radix is zero,\n\ read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\