*.c: Use Python `METH_NOARGS' methods where applicable.
[catacomb-python] / algorithms.c
index af98018..4d4f5e2 100644 (file)
@@ -222,6 +222,48 @@ static const PyMemberDef keysz_pymembers[] = {
   { 0 }
 };
 
+#define KSZCONVOP(op)                                                  \
+  static PyObject *kszmeth_##op(PyObject *me, PyObject *arg)           \
+  {                                                                    \
+    double x, y;                                                       \
+    if (!PyArg_ParseTuple(arg, "d:" #op, &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 PyMethodDef keysz_pymethods[] = {
+#define METHNAME(name) kszmeth_##name
+  SMTH (fromdl,        "fromdl(N) -> M: "
+                   "convert integer discrete log field size to work factor")
+  SMTH (fromschnorr, "fromschnorr(N) -> M: "
+                               "convert Schnorr group order to work factor")
+  SMTH (fromif,        "fromif(N) -> M: "
+                "convert integer factorization problem size to work factor")
+  SMTH (fromec,        "fromec(N) -> M: "
+                        "convert elliptic curve group order to work factor")
+  SMTH (todl,          "todl(N) -> M: "
+                   "convert work factor to integer discrete log field size")
+  SMTH (toschnorr,     "toschnorr(N) -> M: "
+                               "convert work factor to Schnorr group order")
+  SMTH (toif,          "toif(N) -> M: "
+                "convert work factor to integer factorization problem size")
+  SMTH (toec,          "toec(N) -> M: "
+                        "convert work factor to elliptic curve group order")
+  SMTH (toec,          "toec(N) -> M: "
+                        "convert work factor to elliptic curve group order")
+#undef METHNAME
+  { 0 }
+};
+
 static const PyGetSetDef keyszany_pygetset[] = {
 #define GETSETNAME(op, name) ka##op##_##name
   GET  (min,           "KSZ.min -> smallest allowed key size")
@@ -288,7 +330,7 @@ static PyTypeObject keysz_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  0,                                   /* @tp_methods@ */
+  PYMETHODS(keysz),                    /* @tp_methods@ */
   PYMEMBERS(keysz),                    /* @tp_members@ */
   0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
@@ -451,24 +493,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;
@@ -616,9 +640,8 @@ end:
   return (0);
 }
 
-static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
+static PyObject *gcmeth_bdry(PyObject *me)
 {
-  if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
   if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported");
   if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
   GC_BDRY(GCIPHER_C(me));
@@ -643,7 +666,7 @@ static const PyMethodDef gcipher_pymethods[] = {
   METH (decrypt,       "C.decrypt(CT) -> PT")
   METH (deczero,       "C.deczero(N) -> PT")
   METH (setiv,         "C.setiv(IV)")
-  METH (bdry,          "C.bdry()")
+  NAMETH(bdry,         "C.bdry()")
 #undef METHNAME
   { 0 }
 };
@@ -952,12 +975,11 @@ static const PyGetSetDef gcaead_pygetset[] = {
   { 0 }
 };
 
-static PyObject *gaekmeth_aad(PyObject *me, PyObject *arg)
+static PyObject *gaekmeth_aad(PyObject *me)
 {
   const gaead_key *k = GAEADKEY_K(me);
   PyObject *rc = 0;
 
-  if (!PyArg_ParseTuple(arg, ":aad")) return (0);
   if (k->ops->c->f&AEADF_AADNDEP)
     VALERR("aad must be associated with enc/dec op");
   rc = gaeadaad_pywrap((PyObject *)GCAEAD_AAD(me->ob_type),
@@ -1044,7 +1066,7 @@ end:
 
 static const PyMethodDef gaeadkey_pymethods[] = {
 #define METHNAME(name) gaekmeth_##name
-  METH (aad,           "KEY.aad() -> AAD")
+  NAMETH(aad,          "KEY.aad() -> AAD")
   KWMETH(enc,          "KEY.enc(NONCE, [hsz], [msz], [tsz]) -> ENC")
   KWMETH(dec,          "KEY.dec(NONCE, [hsz], [csz], [tsz]) -> DEC")
 #undef METHNAME
@@ -1108,11 +1130,10 @@ static const PyGetSetDef gaeadaad_pygetset[] = {
   { 0 }
 };
 
-static PyObject *gaeameth_copy(PyObject *me, PyObject *arg)
+static PyObject *gaeameth_copy(PyObject *me)
 {
   PyObject *rc = 0;
 
-  if (!PyArg_ParseTuple(arg, ":copy")) goto end;
   if (gaea_check(me)) goto end;
   if (GAEADAAD_F(me)&AEADF_AADNDEP)
     VALERR("can't duplicate nonce-dependent aad");
@@ -1183,7 +1204,7 @@ static PyObject *gaeameth_hashstrz(PyObject *me, PyObject *arg)
 
 static const PyMethodDef gaeadaad_pymethods[] = {
 #define METHNAME(name) gaeameth_##name
-  METH (copy,          "AAD.copy() -> AAD'")
+  NAMETH(copy,         "AAD.copy() -> AAD'")
   METH (hash,          "AAD.hash(H)")
 #define METHU_(n, W, w) METH(hashu##w, "AAD.hashu" #w "(WORD)")
   DOUINTCONV(METHU_)
@@ -1247,12 +1268,11 @@ static const PyGetSetDef gaeadenc_pygetset[] = {
   { 0 }
 };
 
-static PyObject *gaeemeth_aad(PyObject *me, PyObject *arg)
+static PyObject *gaeemeth_aad(PyObject *me)
 {
   gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
   PyObject *rc = 0;
 
-  if (!PyArg_ParseTuple(arg, ":aad")) return (0);
   if (!(ge->f&AEADF_AADNDEP))
     rc = gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad,
                         GAEAD_AAD(ge->e), 0, 0);
@@ -1364,7 +1384,7 @@ end:
 
 static const PyMethodDef gaeadenc_pymethods[] = {
 #define METHNAME(name) gaeemeth_##name
-  METH (aad,           "ENC.aad() -> AAD")
+  NAMETH(aad,          "ENC.aad() -> AAD")
   KWMETH(reinit,       "ENC.reinit(NONCE, [hsz], [msz], [tsz])")
   METH (encrypt,       "ENC.encrypt(MSG) -> CT")
   KWMETH(done,         "ENC.done([tsz], [aad]) -> CT, TAG")
@@ -1422,11 +1442,10 @@ static const PyGetSetDef gaeaddec_pygetset[] = {
   { 0 }
 };
 
-static PyObject *gaedmeth_aad(PyObject *me, PyObject *arg)
+static PyObject *gaedmeth_aad(PyObject *me)
 {
   gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
 
-  if (!PyArg_ParseTuple(arg, ":aad")) return (0);
   if (!(gd->f&AEADF_AADNDEP))
     return (gaeadaad_pywrap((PyObject *)GCAEADDEC_KEY(gd->ob_type)->aad,
                            GAEAD_AAD(gd->d), 0, 0));
@@ -1527,7 +1546,7 @@ end:
 
 static const PyMethodDef gaeaddec_pymethods[] = {
 #define METHNAME(name) gaedmeth_##name
-  METH (aad,           "DEC.aad() -> AAD")
+  NAMETH(aad,          "DEC.aad() -> AAD")
   KWMETH(reinit,       "DEC.reinit(NONCE, [hsz], [csz], [tsz])")
   METH (decrypt,       "DEC.decrypt(CT) -> MSG")
   KWMETH(done,         "DEC.done(TAG, [aad]) -> MSG | None")
@@ -1989,11 +2008,8 @@ static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
 static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
   { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
 
-static PyObject *ghmeth_copy(PyObject *me, PyObject *arg)
-{
-  if (!PyArg_ParseTuple(arg, ":copy")) return (0);
-  return (ghash_pywrap((PyObject *)me->ob_type, GH_COPY(GHASH_H(me))));
-}
+static PyObject *ghmeth_copy(PyObject *me)
+  { return (ghash_pywrap((PyObject *)me->ob_type, GH_COPY(GHASH_H(me)))); }
 
 static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
 {
@@ -2036,11 +2052,10 @@ static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
   RETURN_ME;
 }
 
-static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
+static PyObject *ghmeth_done(PyObject *me)
 {
   ghash *g;
   PyObject *rc;
-  if (!PyArg_ParseTuple(arg, ":done")) return (0);
   g = GH_COPY(GHASH_H(me));
   rc = bytestring_pywrap(0, g->ops->c->hashsz);
   GH_DONE(g, PyString_AS_STRING(rc));
@@ -2059,7 +2074,7 @@ static const PyGetSetDef gchash_pygetset[] = {
 
 static const PyMethodDef ghash_pymethods[] = {
 #define METHNAME(name) ghmeth_##name
-  METH (copy,          "H.copy() -> HH")
+  NAMETH(copy,         "H.copy() -> HH")
   METH (hash,          "H.hash(M)")
 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
   DOUINTCONV(METHU_)
@@ -2068,7 +2083,7 @@ static const PyMethodDef ghash_pymethods[] = {
   DOUINTCONV(METHBUF_)
 #undef METHBUF_
   METH (hashstrz,      "H.hashstrz(STRING)")
-  METH (done,          "H.done() -> HASH")
+  NAMETH(done,         "H.done() -> HASH")
 #undef METHNAME
   { 0 }
 };
@@ -2518,10 +2533,9 @@ static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz)
 static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz)
   { return (PyInt_FromLong(POLY1305_TAGSZ)); }
 
-static PyObject *polymeth_copy(PyObject *me, PyObject *arg)
+static PyObject *polymeth_copy(PyObject *me)
 {
   poly1305hash_pyobj *ph;
-  if (!PyArg_ParseTuple(arg, ":copy")) return (0);
   ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type);
   poly1305_copy(&ph->ctx, P1305_CTX(me));
   Py_INCREF(me->ob_type);
@@ -2572,19 +2586,11 @@ static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg)
   RETURN_ME;
 }
 
-static PyObject *polymeth_flush(PyObject *me, PyObject *arg)
-{
-  if (!PyArg_ParseTuple(arg, ":flush")) return (0);
-  poly1305_flush(P1305_CTX(me));
-  RETURN_ME;
-}
+static PyObject *polymeth_flush(PyObject *me)
+  { poly1305_flush(P1305_CTX(me)); RETURN_ME; }
 
-static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg)
-{
-  if (!PyArg_ParseTuple(arg, ":flushzero")) return (0);
-  poly1305_flushzero(P1305_CTX(me));
-  RETURN_ME;
-}
+static PyObject *polymeth_flushzero(PyObject *me)
+  { poly1305_flushzero(P1305_CTX(me)); RETURN_ME; }
 
 static PyObject *polymeth_concat(PyObject *me, PyObject *arg)
 {
@@ -2602,10 +2608,9 @@ end:
   return (0);
 }
 
-static PyObject *polymeth_done(PyObject *me, PyObject *arg)
+static PyObject *polymeth_done(PyObject *me)
 {
   PyObject *rc;
-  if (!PyArg_ParseTuple(arg, ":done")) return (0);
   if (!(P1305_F(me) & f_mask)) VALERR("no mask");
   rc = bytestring_pywrap(0, POLY1305_TAGSZ);
   poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc));
@@ -2626,7 +2631,7 @@ static const PyGetSetDef poly1305cls_pygetset[] = {
 
 static const PyMethodDef poly1305hash_pymethods[] = {
 #define METHNAME(name) polymeth_##name
-  METH (copy,          "P.copy() -> PP")
+  NAMETH(copy,         "P.copy() -> PP")
   METH (hash,          "P.hash(M)")
 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
   DOUINTCONV(METHU_)
@@ -2635,10 +2640,10 @@ static const PyMethodDef poly1305hash_pymethods[] = {
   DOUINTCONV(METHBUF_)
 #undef METHBUF_
   METH (hashstrz,      "P.hashstrz(STRING)")
-  METH (flush,         "P.flush()")
-  METH (flushzero,     "P.flushzero()")
+  NAMETH(flush,                "P.flush()")
+  NAMETH(flushzero,    "P.flushzero()")
   METH (concat,        "P.concat(PREFIX, SUFFIX)")
-  METH (done,          "P.done() -> TAG")
+  NAMETH(done,         "P.done() -> TAG")
 #undef METHNAME
   { 0 }
 };
@@ -2842,13 +2847,11 @@ end:
   return ((PyObject *)rc);
 }
 
-static PyObject *kxvikmeth_copy(PyObject *me, PyObject *arg)
+static PyObject *kxvikmeth_copy(PyObject *me)
 {
   kxvik_pyobj *k = (kxvik_pyobj *)me, *rc = 0;
-  if (!PyArg_ParseTuple(arg, ":copy")) goto end;
   rc = (kxvik_pyobj *)k->ob_type->tp_alloc(k->ob_type, 0);
   rc->s = k->s; rc->n = k->n;
-end:
   return ((PyObject *)rc);
 }
 
@@ -2897,10 +2900,9 @@ end:
   return (rc);
 }
 
-static PyObject *kxvikmeth_step(PyObject *me, PyObject *arg)
+static PyObject *kxvikmeth_step(PyObject *me)
 {
   kxvik_pyobj *k = (kxvik_pyobj *)me;
-  if (!PyArg_ParseTuple(arg, ":step")) return (0);
   keccak1600_p(&k->s, &k->s, k->n);
   RETURN_ME;
 }
@@ -2934,10 +2936,10 @@ static const PyGetSetDef kxvik_pygetset[] = {
 
 static const PyMethodDef kxvik_pymethods[] = {
 #define METHNAME(func) kxvikmeth_##func
-  METH (copy,          "KECCAK.copy() -> KECCAK'")
+  NAMETH(copy,         "KECCAK.copy() -> KECCAK'")
   METH (mix,           "KECCAK.mix(DATA)")
   METH (extract,       "KECCAK.extract(NOCTETS)")
-  METH (step,          "KECCAK.step()")
+  NAMETH(step,         "KECCAK.step()")
 #undef METHNAME
   { 0 }
 };
@@ -3086,9 +3088,8 @@ static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg)
   RETURN_ME;
 }
 
-static PyObject *shakemeth_xof(PyObject *me, PyObject *arg)
+static PyObject *shakemeth_xof(PyObject *me)
 {
-  if (!PyArg_ParseTuple(arg, ":xof")) goto end;
   if (shake_check(me, 0)) goto end;
   shake_xof(SHAKE_H(me));
   SHAKE_ST(me) = 1;
@@ -3110,15 +3111,13 @@ end:
   return (rc);
 }
 
-static PyObject *shakemeth_copy(PyObject *me, PyObject *arg)
+static PyObject *shakemeth_copy(PyObject *me)
 {
   shake_pyobj *rc = 0;
 
-  if (!PyArg_ParseTuple(arg, ":copy")) goto end;
   rc = PyObject_NEW(shake_pyobj, me->ob_type);
   rc->h = *SHAKE_H(me);
   rc->st = SHAKE_ST(me);
-end:
   return ((PyObject *)rc);
 }
 
@@ -3172,7 +3171,7 @@ static const PyGetSetDef shake_pygetset[] = {
 
 static const PyMethodDef shake_pymethods[] = {
 #define METHNAME(func) shakemeth_##func
-  METH (copy,          "S.copy() -> SS")
+  NAMETH(copy,         "S.copy() -> SS")
   METH (hash,          "S.hash(M)")
 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
   DOUINTCONV(METHU_)
@@ -3181,7 +3180,7 @@ static const PyMethodDef shake_pymethods[] = {
   DOUINTCONV(METHBUF_)
 #undef METHBUF_
   METH (hashstrz,      "S.hashstrz(STRING)")
-  METH (xof,           "S.xof()")
+  NAMETH(xof,          "S.xof()")
   METH (done,          "S.done(LEN) -> H")
   METH (get,           "S.get(LEN) -> H")
   METH (mask,          "S.mask(M) -> C")
@@ -3582,24 +3581,6 @@ static PyTypeObject gprp_pytype_skel = {
 
 static const PyMethodDef methods[] = {
 #define METHNAME(func) meth_##func
-  METH (_KeySZ_fromdl, "fromdl(N) -> M: "
-                   "convert integer discrete log field size to work factor")
-  METH (_KeySZ_fromschnorr, "fromschnorr(N) -> M: "
-                               "convert Schnorr group order to work factor")
-  METH (_KeySZ_fromif, "fromif(N) -> M: "
-                "convert integer factorization problem size to work factor")
-  METH (_KeySZ_fromec, "fromec(N) -> M: "
-                        "convert elliptic curve group order to work factor")
-  METH (_KeySZ_todl,   "todl(N) -> M: "
-                   "convert work factor to integer discrete log field size")
-  METH (_KeySZ_toschnorr, "toschnorr(N) -> M: "
-                               "convert work factor to Schnorr group order")
-  METH (_KeySZ_toif,   "toif(N) -> M: "
-                "convert work factor to integer factorization problem size")
-  METH (_KeySZ_toec,   "toec(N) -> M: "
-                        "convert work factor to elliptic curve group order")
-  METH (_KeySZ_toec,   "toec(N) -> M: "
-                        "convert work factor to elliptic curve group order")
 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf,                 \
         "" #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
   METH_HDANCE(hsalsa20, "HSalsa20")