From 27cbcf9d360a7b25e54c4e268db5889246015440 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Mon, 18 Nov 2019 19:53:29 +0000 Subject: [PATCH] *.c: Rearrange and reformat the class methods. This is preparation for using Python's built-in facilities for marking static and and class methods (which weren't around when this project started out, which is why things are the rather strange way that they are). --- algorithms.c | 36 +++++------ ec.c | 197 ++++++++++++++++++++++++++++------------------------------- field.c | 33 +++++----- group.c | 27 +++----- key.c | 18 ++---- mp.c | 171 ++++++++++++++++++++++++++------------------------- 6 files changed, 228 insertions(+), 254 deletions(-) diff --git a/algorithms.c b/algorithms.c index af98018..651ee52 100644 --- a/algorithms.c +++ b/algorithms.c @@ -222,6 +222,24 @@ static const PyMemberDef keysz_pymembers[] = { { 0 } }; +#define KSZCONVOP(op) \ + static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \ + { \ + double x, y; \ + if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \ + y = keysz_##op(x); \ + return (PyFloat_FromDouble(y)); \ + } +KSZCONVOP(fromdl) +KSZCONVOP(fromschnorr) +KSZCONVOP(fromif) +KSZCONVOP(fromec) +KSZCONVOP(todl) +KSZCONVOP(toschnorr) +KSZCONVOP(toif) +KSZCONVOP(toec) +#undef KSZCONVOP + static const PyGetSetDef keyszany_pygetset[] = { #define GETSETNAME(op, name) ka##op##_##name GET (min, "KSZ.min -> smallest allowed key size") @@ -451,24 +469,6 @@ static PyTypeObject keyszset_pytype_skel = { 0 /* @tp_is_gc@ */ }; -#define KSZCONVOP(op) \ - static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \ - { \ - double x, y; \ - if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \ - y = keysz_##op(x); \ - return (PyFloat_FromDouble(y)); \ - } -KSZCONVOP(fromdl) -KSZCONVOP(fromschnorr) -KSZCONVOP(fromif) -KSZCONVOP(fromec) -KSZCONVOP(todl) -KSZCONVOP(toschnorr) -KSZCONVOP(toif) -KSZCONVOP(toec) -#undef KSZCONVOP - /*----- Symmetric encryption ----------------------------------------------*/ static PyTypeObject *gccipher_pytype, *gcipher_pytype; diff --git a/ec.c b/ec.c index 0568a82..7e89fa8 100644 --- a/ec.c +++ b/ec.c @@ -315,6 +315,83 @@ end: static PyObject *epget_curve(PyObject *me, void *hunoz) { RETURN_OBJ(ECPT_COBJ(me)); } +static PyObject *meth__ECPt_frombuf(PyObject *me, PyObject *arg) +{ + buf b; + char *p; + Py_ssize_t sz; + PyObject *rc = 0; + ec pp = EC_INIT; + + if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end; + buf_init(&b, p, sz); + if (buf_getec(&b, &pp)) VALERR("malformed data"); + rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp), + bytestring_pywrapbuf(&b)); +end: + return (rc); +} + +static PyObject *meth__ECPt_parse(PyObject *me, PyObject *arg) +{ + char *p; + qd_parse qd; + PyObject *rc = 0; + ec pp = EC_INIT; + + if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; + qd.p = p; qd.e = 0; + if (!ec_ptparse(&qd, &pp)) VALERR(qd.e); + rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p); +end: + return (rc); +} + +static PyObject *meth__ECPtCurve_fromraw(PyObject *me, PyObject *arg) +{ + char *p; + Py_ssize_t len; + buf b; + PyObject *rc = 0; + ec_curve *cc; + ec pp = EC_INIT; + + if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &len)) + return (0); + buf_init(&b, p, len); + cc = ECCURVE_C(me); + if (ec_getraw(cc, &b, &pp)) + VALERR("bad point"); + EC_IN(cc, &pp, &pp); + rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b)); +end: + return (rc); +} + +static PyObject *meth__ECPtCurve_os2ecp(PyObject *me, + PyObject *arg, PyObject *kw) +{ + char *p; + Py_ssize_t len; + buf b; + PyObject *rc = 0; + ec_curve *cc; + unsigned f = EC_XONLY | EC_LSB | EC_SORT | EC_EXPLY; + ec pp = EC_INIT; + static const char *const kwlist[] = { "class", "buf", "flags", 0 }; + + if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|O&:os2ecp", KWLIST, + &me, &p, &len, convuint, &f)) + return (0); + buf_init(&b, p, len); + cc = ECCURVE_C(me); + if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point"); + EC_IN(cc, &pp, &pp); + rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b)); +end: + return (rc); +} + static PyObject *epncget_ix(PyObject *me, void *hunoz) { ec p = EC_INIT; @@ -833,84 +910,6 @@ static PyObject *ecmeth_mmul(PyObject *me, PyObject *arg) ecmmul_id, ecmmul_fill, ecmmul_exp, ecmmul_drop)); } -static PyObject *meth__ECPtCurve_fromraw(PyObject *me, PyObject *arg) -{ - char *p; - Py_ssize_t len; - buf b; - PyObject *rc = 0; - ec_curve *cc; - ec pp = EC_INIT; - - if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &len)) - return (0); - buf_init(&b, p, len); - cc = ECCURVE_C(me); - if (ec_getraw(cc, &b, &pp)) - VALERR("bad point"); - EC_IN(cc, &pp, &pp); - rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b)); -end: - return (rc); -} - -static PyObject *meth__ECPtCurve_os2ecp(PyObject *me, - PyObject *arg, PyObject *kw) -{ - char *p; - Py_ssize_t len; - buf b; - PyObject *rc = 0; - ec_curve *cc; - unsigned f = EC_XONLY | EC_LSB | EC_SORT | EC_EXPLY; - ec pp = EC_INIT; - static const char *const kwlist[] = { "class", "buf", "flags", 0 }; - - if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|O&:os2ecp", KWLIST, - &me, &p, &len, convuint, &f)) - return (0); - buf_init(&b, p, len); - cc = ECCURVE_C(me); - if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point"); - EC_IN(cc, &pp, &pp); - rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b)); -end: - return (rc); -} - -static PyObject *meth__ECPt_frombuf(PyObject *me, PyObject *arg) -{ - buf b; - char *p; - Py_ssize_t sz; - PyObject *rc = 0; - ec pp = EC_INIT; - - if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end; - buf_init(&b, p, sz); - if (buf_getec(&b, &pp)) VALERR("malformed data"); - rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp), - bytestring_pywrapbuf(&b)); -end: - return (rc); -} - -static PyObject *meth__ECPt_parse(PyObject *me, PyObject *arg) -{ - char *p; - qd_parse qd; - PyObject *rc = 0; - ec pp = EC_INIT; - - if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; - qd.p = p; - qd.e = 0; - if (!ec_ptparse(&qd, &pp)) VALERR(qd.e); - rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p); -end: - return (rc); -} - static void eccurve_pydealloc(PyObject *me) { ec_destroycurve(ECCURVE_C(me)); @@ -954,6 +953,21 @@ static PyObject *ecmeth_rand(PyObject *me, PyObject *arg, PyObject *kw) return (ecpt_pywrap(me, &p)); } +static PyObject *meth__ECCurve_parse(PyObject *me, PyObject *arg) +{ + char *p; + qd_parse qd; + ec_curve *c; + PyObject *rc = 0; + + if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; + qd.p = p; qd.e = 0; + if ((c = ec_curveparse(&qd)) == 0) VALERR(qd.e); + rc = eccurve_pywrap(0, c); +end: + return (rc); +} + static PyObject *eccurve_dopywrap(PyTypeObject *ty, PyObject *fobj, ec_curve *c) { @@ -1017,24 +1031,6 @@ end: return (cobj); } -static PyObject *meth__ECCurve_parse(PyObject *me, PyObject *arg) -{ - char *p; - qd_parse qd; - ec_curve *c; - PyObject *rc = 0; - - if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) - goto end; - qd.p = p; - qd.e = 0; - if ((c = ec_curveparse(&qd)) == 0) - VALERR(qd.e); - rc = eccurve_pywrap(0, c); -end: - return (rc); -} - static PyObject *ecget_name(PyObject *me, void *hunoz) { return (PyString_FromString(EC_NAME(ECCURVE_C(me)))); } @@ -1407,12 +1403,9 @@ static PyObject *meth__ECInfo_parse(PyObject *me, PyObject *arg) ec_info ei; PyObject *rc = 0; - if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) - goto end; - qd.p = p; - qd.e = 0; - if (ec_infoparse(&qd, &ei)) - VALERR(qd.e); + if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; + qd.p = p; qd.e = 0; + if (ec_infoparse(&qd, &ei)) VALERR(qd.e); rc = Py_BuildValue("(Ns)", ecinfo_pywrap(&ei), qd.p); end: return (rc); diff --git a/field.c b/field.c index 8091843..7fc1f9d 100644 --- a/field.c +++ b/field.c @@ -472,6 +472,21 @@ static PyObject *fmeth__adopt(PyObject *me, PyObject *arg) return (fe_pywrap(me, xx)); } +static PyObject *meth__Field_parse(PyObject *me, PyObject *arg) +{ + field *f; + char *p; + PyObject *rc = 0; + qd_parse qd; + + if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; + qd.p = p; qd.e = 0; + if ((f = field_parse(&qd)) == 0) VALERR(qd.e); + rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p); +end: + return (rc); +} + static void field_pydealloc(PyObject *me) { F_DESTROY(FIELD_F(me)); @@ -924,24 +939,6 @@ static PyTypeObject binnormfield_pytype_skel = { /*----- Setup -------------------------------------------------------------*/ -static PyObject *meth__Field_parse(PyObject *me, PyObject *arg) -{ - field *f; - char *p; - PyObject *rc = 0; - qd_parse qd; - - if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) - goto end; - qd.p = p; - qd.e = 0; - if ((f = field_parse(&qd)) == 0) - VALERR(qd.e); - rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p); -end: - return (rc); -} - static const PyMethodDef methods[] = { #define METHNAME(func) meth_##func METH (_Field_parse, "parse(STR) -> F, REST") diff --git a/group.c b/group.c index bdf75b1..727e0b0 100644 --- a/group.c +++ b/group.c @@ -281,8 +281,7 @@ static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty, PyObject *rc = 0; if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; - qd.p = p; - qd.e = 0; + qd.p = p; qd.e = 0; if (parse(&qd, &gp)) VALERR(qd.e); rc = fginfo_pywrap(&gp, ty); end: @@ -854,13 +853,11 @@ static PyObject *meth__GE_frombuf(PyObject *me, PyObject *arg) group *g; ge *x = 0; - if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &n)) - return (0); + if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &n)) return (0); g = GROUP_G(me); buf_init(&b, p, n); x = G_CREATE(g); - if (G_FROMBUF(g, &b, x)) - VALERR("invalid data"); + if (G_FROMBUF(g, &b, x)) VALERR("invalid data"); return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b))); end: if (x) G_DESTROY(g, x); @@ -875,13 +872,11 @@ static PyObject *meth__GE_fromraw(PyObject *me, PyObject *arg) group *g; ge *x = 0; - if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &n)) - return (0); + if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &n)) return (0); g = GROUP_G(me); buf_init(&b, p, n); x = G_CREATE(g); - if (G_FROMRAW(g, &b, x)) - VALERR("invalid data"); + if (G_FROMRAW(g, &b, x)) VALERR("invalid data"); return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b))); end: if (x) G_DESTROY(g, x); @@ -896,8 +891,7 @@ static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg) group *g; ge *x = 0; - if (!PyArg_ParseTuple(arg, "Os#:fromstring", &me, &p, &n)) - return (0); + if (!PyArg_ParseTuple(arg, "Os#:fromstring", &me, &p, &n)) return (0); sc.buf = p; sc.lim = sc.buf + n; g = GROUP_G(me); @@ -917,12 +911,9 @@ static PyObject *meth__Group_parse(PyObject *me, PyObject *arg) qd_parse qd; group *g; - if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) - goto end; - qd.p = p; - qd.e = 0; - if ((g = group_parse(&qd)) == 0) - VALERR(qd.e); + if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; + qd.p = p; qd.e = 0; + if ((g = group_parse(&qd)) == 0) VALERR(qd.e); return (group_pywrap(g)); end: return (0); diff --git a/key.c b/key.c index b6a8c6a..a2ffb0a 100644 --- a/key.c +++ b/key.c @@ -283,10 +283,8 @@ static PyObject *meth__KeyData_readflags(PyObject *me, PyObject *arg) PyObject *rc = 0; int err; - if (!PyArg_ParseTuple(arg, "Os:key_readflags", &me, &p)) - goto end; - if ((err = key_readflags(p, &end, &f, &m)) != 0) - KEYERR(err); + if (!PyArg_ParseTuple(arg, "Os:key_readflags", &me, &p)) goto end; + if ((err = key_readflags(p, &end, &f, &m)) != 0) KEYERR(err); rc = Py_BuildValue("(NNs)", getulong(f), getulong(m), end); end: return (rc); @@ -435,10 +433,8 @@ static PyObject *meth__KeyData_read(PyObject *me, PyObject *arg) key_data *kd; PyObject *rc = 0; - if (!PyArg_ParseTuple(arg, "Os:read", &me, &p)) - goto end; - if ((kd = key_read(p, &end)) == 0) - KEYERR(KERR_MALFORMED); + if (!PyArg_ParseTuple(arg, "Os:read", &me, &p)) goto end; + if ((kd = key_read(p, &end)) == 0) KEYERR(KERR_MALFORMED); rc = Py_BuildValue("(Ns)", keydata_pywrap(kd), end); end: return (rc); @@ -451,10 +447,8 @@ static PyObject *meth__KeyData_decode(PyObject *me, PyObject *arg) key_data *kd; PyObject *rc = 0; - if (!PyArg_ParseTuple(arg, "Os#:decode", &me, &p, &n)) - goto end; - if ((kd = key_decode(p, n)) == 0) - KEYERR(KERR_MALFORMED); + if (!PyArg_ParseTuple(arg, "Os#:decode", &me, &p, &n)) goto end; + if ((kd = key_decode(p, n)) == 0) KEYERR(KERR_MALFORMED); rc = keydata_pywrap(kd); end: return (rc); diff --git a/mp.c b/mp.c index 7759e29..b4e65cb 100644 --- a/mp.c +++ b/mp.c @@ -776,6 +776,64 @@ end: return (rc); } +static PyObject *meth__MP_fromstring(PyObject *me, + PyObject *arg, PyObject *kw) +{ + int r = 0; + char *p; + Py_ssize_t len; + PyObject *z = 0; + mp *zz; + mptext_stringctx sc; + static const char *const kwlist[] = { "class", "x", "radix", 0 }; + + if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|i:fromstring", + KWLIST, &me, &p, &len, &r)) + goto end; + if (!good_radix_p(r, 1)) VALERR("bad radix"); + sc.buf = p; sc.lim = p + len; + if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0) + VALERR("bad integer"); + z = Py_BuildValue("(Ns#)", mp_pywrap(zz), + sc.buf, (Py_ssize_t)(sc.lim - sc.buf)); +end: + return (z); +} + +static PyObject *meth__MP_factorial(PyObject *me, PyObject *arg) +{ + unsigned long i; + mp *x; + if (!PyArg_ParseTuple(arg, "OO&:factorial", &me, convulong, &i)) return (0); + x = mp_factorial(i); + return mp_pywrap(x); +} + +static PyObject *meth__MP_fibonacci(PyObject *me, PyObject *arg) +{ + long i; + mp *x; + if (!PyArg_ParseTuple(arg, "Ol:fibonacci", &me, &i)) return (0); + x = mp_fibonacci(i); + return mp_pywrap(x); +} + +#define LOADOP(pre, py, name) \ + static PyObject *meth__##py##_##name(PyObject *me, PyObject *arg) \ + { \ + char *p; \ + Py_ssize_t len; \ + if (!PyArg_ParseTuple(arg, "Os#:" #name, &me, &p, &len)) return (0); \ + return (pre##_pywrap(mp_##name(MP_NEW, p, len))); \ + } +LOADOP(mp, MP, loadl) +LOADOP(mp, MP, loadb) +LOADOP(mp, MP, loadl2c) +LOADOP(mp, MP, loadb2c) +LOADOP(gf, GF, loadl) +LOADOP(gf, GF, loadb) +#undef LOADOP + static PyObject *mpget_nbits(PyObject *me, void *hunoz) { return (PyInt_FromLong(mp_bits(MP_X(me)))); } @@ -928,65 +986,6 @@ static PyTypeObject mp_pytype_skel = { 0 /* @tp_is_gc@ */ }; -static PyObject *meth__MP_fromstring(PyObject *me, - PyObject *arg, PyObject *kw) -{ - int r = 0; - char *p; - Py_ssize_t len; - PyObject *z = 0; - mp *zz; - mptext_stringctx sc; - static const char *const kwlist[] = { "class", "x", "radix", 0 }; - - if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|i:fromstring", - KWLIST, &me, &p, &len, &r)) - goto end; - if (!good_radix_p(r, 1)) VALERR("bad radix"); - sc.buf = p; sc.lim = p + len; - if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0) - VALERR("bad integer"); - z = Py_BuildValue("(Ns#)", mp_pywrap(zz), - sc.buf, (Py_ssize_t)(sc.lim - sc.buf)); -end: - return (z); -} - -static PyObject *meth__MP_factorial(PyObject *me, PyObject *arg) -{ - unsigned long i; - mp *x; - if (!PyArg_ParseTuple(arg, "OO&:factorial", &me, convulong, &i)) - return (0); - x = mp_factorial(i); - return mp_pywrap(x); -} - -static PyObject *meth__MP_fibonacci(PyObject *me, PyObject *arg) -{ - long i; - mp *x; - if (!PyArg_ParseTuple(arg, "Ol:fibonacci", &me, &i)) return (0); - x = mp_fibonacci(i); - return mp_pywrap(x); -} - -#define LOADOP(pre, py, name) \ - static PyObject *meth__##py##_##name(PyObject *me, PyObject *arg) \ - { \ - char *p; \ - Py_ssize_t len; \ - if (!PyArg_ParseTuple(arg, "Os#:" #name, &me, &p, &len)) return (0); \ - return (pre##_pywrap(mp_##name(MP_NEW, p, len))); \ - } -LOADOP(mp, MP, loadl) -LOADOP(mp, MP, loadb) -LOADOP(mp, MP, loadl2c) -LOADOP(mp, MP, loadb2c) -LOADOP(gf, GF, loadl) -LOADOP(gf, GF, loadb) -#undef LOADOP - /*----- Products of small integers ----------------------------------------*/ typedef struct mpmul_pyobj { @@ -1984,6 +1983,33 @@ end: return (z); } +static PyObject *meth__GF_fromstring(PyObject *me, + PyObject *arg, PyObject *kw) +{ + int r = 0; + char *p; + Py_ssize_t len; + PyObject *z = 0; + mp *zz; + mptext_stringctx sc; + static const char *const kwlist[] = { "class", "x", "radix", 0 }; + + if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|i:fromstring", + KWLIST, &me, &p, &len, &r)) + goto end; + if (!good_radix_p(r, 1)) VALERR("bad radix"); + sc.buf = p; sc.lim = p + len; + if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0 || + MP_NEGP(zz)) { + if (zz) MP_DROP(zz); + VALERR("bad binary polynomial"); + } + z = Py_BuildValue("(Ns#)", gf_pywrap(zz), + sc.buf, (Py_ssize_t)(sc.lim - sc.buf)); +end: + return (z); +} + static PyObject *gfmeth_irreduciblep(PyObject *me, PyObject *arg) { if (!PyArg_ParseTuple(arg, ":irreduciblep")) return (0); @@ -2134,33 +2160,6 @@ static PyTypeObject gf_pytype_skel = { 0 /* @tp_is_gc@ */ }; -static PyObject *meth__GF_fromstring(PyObject *me, - PyObject *arg, PyObject *kw) -{ - int r = 0; - char *p; - Py_ssize_t len; - PyObject *z = 0; - mp *zz; - mptext_stringctx sc; - static const char *const kwlist[] = { "class", "x", "radix", 0 }; - - if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|i:fromstring", - KWLIST, &me, &p, &len, &r)) - goto end; - if (!good_radix_p(r, 1)) VALERR("bad radix"); - sc.buf = p; sc.lim = p + len; - if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0 || - MP_NEGP(zz)) { - if (zz) MP_DROP(zz); - VALERR("bad binary polynomial"); - } - z = Py_BuildValue("(Ns#)", gf_pywrap(zz), - sc.buf, (Py_ssize_t)(sc.lim - sc.buf)); -end: - return (z); -} - /*----- Sparse poly reduction ---------------------------------------------*/ typedef struct gfreduce_pyobj { -- 2.11.0