*.c: Use Python `METH_NOARGS' methods where applicable.
[catacomb-python] / algorithms.c
index 064fb03..4d4f5e2 100644 (file)
 /*----- Header files ------------------------------------------------------*/
 
 #include "catacomb-python.h"
+PUBLIC_SYMBOLS;
 #include "algorithms.h"
+PRIVATE_SYMBOLS;
 
 /*----- Key sizes ---------------------------------------------------------*/
 
-PyTypeObject *keysz_pytype;
-PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype;
-PyObject *sha_pyobj, *has160_pyobj;
+static PyTypeObject *keysz_pytype;
+static PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype;
+
+typedef struct keysz_pyobj {
+  PyObject_HEAD
+  int dfl;
+} keysz_pyobj;
+
+typedef struct keyszrange_pyobj {
+  PyObject_HEAD
+  int dfl;
+  int min, max, mod;
+} keyszrange_pyobj;
+
+typedef struct keyszset_pyobj {
+  PyObject_HEAD
+  int dfl;
+  PyObject *set;
+} keyszset_pyobj;
+
+#define KEYSZ_PYCHECK(o) PyObject_TypeCheck((o), keysz_pytype)
 
 #ifndef KSZ_OPMASK
 #  define KSZ_OPMASK 0x1f
@@ -173,9 +193,9 @@ static PyObject *ksget_min(PyObject *me, void *hunoz)
 {
   PyObject *set = ((keyszset_pyobj *)me)->set;
   int i, n, y, x = -1;
-  n = PyTuple_Size(set);
+  n = PyTuple_GET_SIZE(set);
   for (i = 0; i < n; i++) {
-    y = PyInt_AsLong(PyTuple_GetItem(set, i));
+    y = PyInt_AS_LONG(PyTuple_GET_ITEM(set, i));
     if (x == -1 || y < x) x = y;
   }
   return (PyInt_FromLong(x));
@@ -185,52 +205,94 @@ static PyObject *ksget_max(PyObject *me, void *hunoz)
 {
   PyObject *set = ((keyszset_pyobj *)me)->set;
   int i, n, y, x = -1;
-  n = PyTuple_Size(set);
+  n = PyTuple_GET_SIZE(set);
   for (i = 0; i < n; i++) {
-    y = PyInt_AsLong(PyTuple_GetItem(set, i));
+    y = PyInt_AS_LONG(PyTuple_GET_ITEM(set, i));
     if (y > x) x = y;
   }
   return (PyInt_FromLong(x));
 }
 
-static PyMemberDef keysz_pymembers[] = {
+static const PyMemberDef keysz_pymembers[] = {
 #define MEMBERSTRUCT keysz_pyobj
 #define default dfl /* ugh! */
-  MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size")
+  MEMBER(default, T_INT,  READONLY, "KSZ.default -> default key size")
 #undef default
 #undef MEMBERSTRUCT
   { 0 }
 };
 
-static PyGetSetDef keyszany_pygetset[] = {
+#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")
-  GET  (max,                   "KSZ.max -> largest allowed key size")
+  GET  (min,           "KSZ.min -> smallest allowed key size")
+  GET  (max,           "KSZ.max -> largest allowed key size")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyMemberDef keyszrange_pymembers[] = {
+static const PyMemberDef keyszrange_pymembers[] = {
 #define MEMBERSTRUCT keyszrange_pyobj
-  MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size")
-  MEMBER(max, T_INT, READONLY, "KSZ.max -> largest allowed key size")
-  MEMBER(mod, T_INT, READONLY,
-        "KSZ.mod -> key size must be a multiple of this")
+  MEMBER(min,  T_INT,    READONLY, "KSZ.min -> smallest allowed key size")
+  MEMBER(max,  T_INT,    READONLY, "KSZ.max -> largest allowed key size")
+  MEMBER(mod,  T_INT,    READONLY,
+                           "KSZ.mod -> key size must be a multiple of this")
 #undef MEMBERSTRUCT
   { 0 }
 };
 
-static PyGetSetDef keyszset_pygetset[] = {
+static const PyGetSetDef keyszset_pygetset[] = {
 #define GETSETNAME(op, name) ks##op##_##name
-  GET  (min,                   "KSZ.min -> smallest allowed key size")
-  GET  (max,                   "KSZ.max -> largest allowed key size")
+  GET  (min,           "KSZ.min -> smallest allowed key size")
+  GET  (max,           "KSZ.max -> largest allowed key size")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyMemberDef keyszset_pymembers[] = {
+static const PyMemberDef keyszset_pymembers[] = {
 #define MEMBERSTRUCT keyszset_pyobj
-  MEMBER(set, T_OBJECT, READONLY,      "KSZ.set -> allowed key sizes")
+  MEMBER(set,  T_OBJECT, READONLY, "KSZ.set -> allowed key sizes")
 #undef MEMBERSTRUCT
   { 0 }
 };
@@ -260,7 +322,7 @@ static PyTypeObject keysz_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Key size constraints.  Abstract.",
+  "Key size constraints.  Abstract.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -268,8 +330,8 @@ static PyTypeObject keysz_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  0,                                   /* @tp_methods@ */
-  keysz_pymembers,                     /* @tp_members@ */
+  PYMETHODS(keysz),                    /* @tp_methods@ */
+  PYMEMBERS(keysz),                    /* @tp_members@ */
   0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
@@ -308,8 +370,8 @@ static PyTypeObject keyszany_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"KeySZAny(DEFAULT)\n\
-  Key size constraints.  This object imposes no constraints on size.",
+  "KeySZAny(DEFAULT)\n"
+  "  Key size constraints.  This object imposes no constraints on size.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -319,7 +381,7 @@ static PyTypeObject keyszany_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  keyszany_pygetset,                   /* @tp_getset@ */
+  PYGETSET(keyszany),                  /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -357,9 +419,9 @@ static PyTypeObject keyszrange_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n\
-  Key size constraints.  Key size must be between MIN and MAX inclusive,\n\
-  and be a multiple of MOD.",
+  "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n"
+  "  Key size constraints: size must be between MIN and MAX inclusive, and\n"
+   be a multiple of MOD.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -368,7 +430,7 @@ static PyTypeObject keyszrange_pytype_skel = {
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
-  keyszrange_pymembers,                        /* @tp_members@ */
+  PYMEMBERS(keyszrange),               /* @tp_members@ */
   0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
@@ -407,8 +469,8 @@ static PyTypeObject keyszset_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"KeySZSet(DEFAULT, SEQ)\n\
-  Key size constraints.  Key size must be DEFAULT or one in SEQ.",
+  "KeySZSet(DEFAULT, SEQ)\n"
+  "  Key size constraints: size must be DEFAULT or an element of SEQ.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -417,8 +479,8 @@ static PyTypeObject keyszset_pytype_skel = {
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
-  keyszset_pymembers,                  /* @tp_members@ */
-  keyszset_pygetset,                   /* @tp_getset@ */
+  PYMEMBERS(keyszset),                 /* @tp_members@ */
+  PYGETSET(keyszset),                  /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -431,32 +493,29 @@ 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 ----------------------------------------------*/
 
-PyTypeObject *gccipher_pytype, *gcipher_pytype;
+static PyTypeObject *gccipher_pytype, *gcipher_pytype;
+
+typedef struct gccipher_pyobj {
+  PyHeapTypeObject ty;
+  gccipher *cc;
+} gccipher_pyobj;
+
+#define GCCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gccipher_pytype)
+#define GCCIPHER_CC(o) (((gccipher_pyobj *)(o))->cc)
+
+typedef struct gcipher_pyobj {
+  PyObject_HEAD
+  gcipher *c;
+} gcipher_pyobj;
+
+#define GCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gcipher_pytype)
+#define GCIPHER_C(o) (((gcipher_pyobj *)(o))->c)
 
 CONVFUNC(gccipher, gccipher *, GCCIPHER_CC)
-CONVFUNC(gcipher, gcipher *, GCIPHER_C)
 
-PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c)
+static PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c)
 {
   gcipher_pyobj *g;
   if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c));
@@ -581,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));
@@ -592,23 +650,23 @@ end:
   return (0);
 }
 
-static PyGetSetDef gccipher_pygetset[] = {
+static const PyGetSetDef gccipher_pygetset[] = {
 #define GETSETNAME(op, name) gcc##op##_##name
-  GET  (keysz,                 "CC.keysz -> acceptable key sizes")
-  GET  (blksz,                 "CC.blksz -> block size, or zero")
-  GET  (name,                  "CC.name -> name of this kind of cipher")
+  GET  (keysz,         "CC.keysz -> acceptable key sizes")
+  GET  (blksz,         "CC.blksz -> block size, or zero")
+  GET  (name,          "CC.name -> name of this kind of cipher")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyMethodDef gcipher_pymethods[] = {
+static const PyMethodDef gcipher_pymethods[] = {
 #define METHNAME(name) gcmeth_##name
-  METH (encrypt,               "C.encrypt(PT) -> CT")
-  METH (enczero,               "C.enczero(N) -> CT")
-  METH (decrypt,               "C.decrypt(CT) -> PT")
-  METH (deczero,               "C.deczero(N) -> PT")
-  METH (setiv,                 "C.setiv(IV)")
-  METH (bdry,                  "C.bdry()")
+  METH (encrypt,       "C.encrypt(PT) -> CT")
+  METH (enczero,       "C.enczero(N) -> CT")
+  METH (decrypt,       "C.decrypt(CT) -> PT")
+  METH (deczero,       "C.deczero(N) -> PT")
+  METH (setiv,         "C.setiv(IV)")
+  NAMETH(bdry,         "C.bdry()")
 #undef METHNAME
   { 0 }
 };
@@ -638,7 +696,7 @@ static PyTypeObject gccipher_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Symmetric cipher metaclass.",
+  "Symmetric cipher metaclass.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -648,7 +706,7 @@ static PyTypeObject gccipher_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  gccipher_pygetset,                   /* @tp_getset@ */
+  PYGETSET(gccipher),                  /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -686,7 +744,7 @@ static PyTypeObject gcipher_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Symmetric cipher, abstract base class.",
+  "Symmetric cipher, abstract base class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -694,7 +752,7 @@ static PyTypeObject gcipher_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  gcipher_pymethods,                   /* @tp_methods@ */
+  PYMETHODS(gcipher),                  /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
@@ -711,15 +769,111 @@ static PyTypeObject gcipher_pytype_skel = {
 
 /*----- Authenticated encryption ------------------------------------------*/
 
-PyTypeObject *gcaead_pytype, *gaeadkey_pytype;
-PyTypeObject *gcaeadaad_pytype, *gaeadaad_pytype;
-PyTypeObject *gcaeadenc_pytype, *gaeadenc_pytype;
-PyTypeObject *gcaeaddec_pytype, *gaeaddec_pytype;
+static PyTypeObject *gcaead_pytype, *gaeadkey_pytype;
+static PyTypeObject *gcaeadaad_pytype, *gaeadaad_pytype;
+static PyTypeObject *gcaeadenc_pytype, *gaeadenc_pytype;
+static PyTypeObject *gcaeaddec_pytype, *gaeaddec_pytype;
 
-CONVFUNC(gcaead, gcaead *, GCAEAD_AEC)
-CONVFUNC(gaeadkey, gaead_key *, GAEADKEY_K)
+typedef struct gcaead_pyobj {
+  PyHeapTypeObject ty;
+  gcaead *aec;
+  struct gcaeadaad_pyobj *aad;
+  struct gcaeadenc_pyobj *enc;
+  struct gcaeaddec_pyobj *dec;
+} gcaead_pyobj;
+
+#define GCAEAD_PYCHECK(o) PyObject_TypeCheck((o), gcaead_pytype)
+#define GCAEAD_AEC(o) (((gcaead_pyobj *)(o))->aec)
+#define GCAEAD_AAD(o) (((gcaead_pyobj *)(o))->aad)
+#define GCAEAD_ENC(o) (((gcaead_pyobj *)(o))->enc)
+#define GCAEAD_DEC(o) (((gcaead_pyobj *)(o))->dec)
+static PyObject *gcaead_pywrap(gcaead *);
+
+typedef struct gaeadkey_pyobj {
+  PyObject_HEAD
+  gaead_key *k;
+} gaeadkey_pyobj;
 
-PyObject *gaeadkey_pywrap(PyObject *cobj, gaead_key *k)
+#define GAEADKEY_PYCHECK(o) PyObject_TypeCheck((o), gaeadkey_pytype)
+#define GAEADKEY_K(o) (((gaeadkey_pyobj *)(o))->k)
+
+typedef struct gcaeadaad_pyobj {
+  PyHeapTypeObject ty;
+  gcaead_pyobj *key;
+} gcaeadaad_pyobj;
+
+#define GCAEADAAD_KEY(o) (((gcaeadaad_pyobj *)(o))->key)
+static PyObject *gaeadaad_pywrap(PyObject *, gaead_aad *, unsigned, size_t);
+
+typedef struct gaeadaad_pyobj {
+  PyObject_HEAD
+  gaead_aad *a;
+  unsigned f;
+#define AEADF_DEAD 32768u
+  size_t hsz, hlen;
+} gaeadaad_pyobj;
+
+#define GAEADAAD_PYCHECK(o) PyObject_TypeCheck((o), gaeadaad_pytype)
+#define GAEADAAD_A(o) (((gaeadaad_pyobj *)(o))->a)
+#define GAEADAAD_F(o) (((gaeadaad_pyobj *)(o))->f)
+#define GAEADAAD_HSZ(o) (((gaeadaad_pyobj *)(o))->hsz)
+#define GAEADAAD_HLEN(o) (((gaeadaad_pyobj *)(o))->hlen)
+
+typedef struct gcaeadenc_pyobj {
+  PyHeapTypeObject ty;
+  gcaead_pyobj *key;
+} gcaeadenc_pyobj;
+
+#define GCAEADENC_KEY(o) (((gcaeadenc_pyobj *)(o))->key)
+static PyObject *gaeadenc_pywrap(PyObject *, gaead_enc *, unsigned,
+                                size_t, size_t, size_t);
+
+typedef struct gaeadenc_pyobj {
+  PyObject_HEAD
+  gaead_enc *e;
+  gaeadaad_pyobj *aad;
+  unsigned f;
+  size_t hsz, msz, tsz;
+  size_t mlen;
+} gaeadenc_pyobj;
+
+#define GAEADENC_PYCHECK(o) PyObject_TypeCheck((o), gaeadenc_pytype)
+#define GAEADENC_AAD(o) (((gaeadenc_pyobj *)(o))->aad)
+#define GAEADENC_E(o) (((gaeadenc_pyobj *)(o))->e)
+#define GAEADENC_F(o) (((gaeadenc_pyobj *)(o))->f)
+#define GAEADENC_HSZ(o) (((gaeadenc_pyobj *)(o))->hsz)
+#define GAEADENC_MSZ(o) (((gaeadenc_pyobj *)(o))->msz)
+#define GAEADENC_TSZ(o) (((gaeadenc_pyobj *)(o))->tsz)
+#define GAEADENC_MLEN(o) (((gaeadenc_pyobj *)(o))->mlen)
+
+typedef struct gcaeaddec_pyobj {
+  PyHeapTypeObject ty;
+  gcaead_pyobj *key;
+} gcaeaddec_pyobj;
+
+#define GCAEADDEC_KEY(o) (((gcaeaddec_pyobj *)(o))->key)
+static PyObject *gaeaddec_pywrap(PyObject *, gaead_dec *, unsigned,
+                                size_t, size_t, size_t);
+
+typedef struct gaeaddec_pyobj {
+  PyObject_HEAD
+  gaead_dec *d;
+  gaeadaad_pyobj *aad;
+  unsigned f;
+  size_t hsz, csz, tsz;
+  size_t clen;
+} gaeaddec_pyobj;
+
+#define GAEADDEC_PYCHECK(o) PyObject_TypeCheck((o), gaeaddec_pytype)
+#define GAEADDEC_AAD(o) (((gaeaddec_pyobj *)(o))->aad)
+#define GAEADDEC_D(o) (((gaeaddec_pyobj *)(o))->d)
+#define GAEADDEC_F(o) (((gaeaddec_pyobj *)(o))->f)
+#define GAEADDEC_HSZ(o) (((gaeaddec_pyobj *)(o))->hsz)
+#define GAEADDEC_CSZ(o) (((gaeaddec_pyobj *)(o))->csz)
+#define GAEADDEC_TSZ(o) (((gaeaddec_pyobj *)(o))->tsz)
+#define GAEADDEC_CLEN(o) (((gaeaddec_pyobj *)(o))->clen)
+
+static PyObject *gaeadkey_pywrap(PyObject *cobj, gaead_key *k)
 {
   gaeadkey_pyobj *gk;
 
@@ -746,7 +900,7 @@ end:
   return (0);
 }
 
-PyObject *gcaead_pywrap(gcaead *aec)
+static PyObject *gcaead_pywrap(gcaead *aec)
 {
   gcaead_pyobj *gck;
   gcaeadaad_pyobj *gca;
@@ -807,26 +961,25 @@ static PyObject *gcaeget_ohd(PyObject *me, void *hunoz)
 static PyObject *gcaeget_flags(PyObject *me, void *hunoz)
   { return (PyInt_FromLong(GCAEAD_AEC(me)->f)); }
 
-static PyGetSetDef gcaead_pygetset[] = {
+static const PyGetSetDef gcaead_pygetset[] = {
 #define GETSETNAME(op, name) gcae##op##_##name
-  GET  (keysz,                 "AEC.keysz -> acceptable key sizes")
-  GET  (noncesz,               "AEC.noncesz -> acceptable nonce sizes")
-  GET  (tagsz,                 "AEC.tagsz -> acceptable tag sizes")
-  GET  (blksz,                 "AEC.blksz -> block size, or zero")
-  GET  (bufsz,                 "AEC.bufsz -> amount of data buffered internally")
-  GET  (ohd,                   "AEC.ohd -> maximum encryption overhead")
-  GET  (name,                  "AEC.name -> name of this kind of AEAD scheme")
-  GET  (flags,                 "AEC.flags -> mask of `AEADF_...' flags")
+  GET  (keysz,         "AEC.keysz -> acceptable key sizes")
+  GET  (noncesz,       "AEC.noncesz -> acceptable nonce sizes")
+  GET  (tagsz,         "AEC.tagsz -> acceptable tag sizes")
+  GET  (blksz,         "AEC.blksz -> block size, or zero")
+  GET  (bufsz,         "AEC.bufsz -> amount of data buffered internally")
+  GET  (ohd,           "AEC.ohd -> maximum encryption overhead")
+  GET  (name,          "AEC.name -> name of this kind of AEAD scheme")
+  GET  (flags,         "AEC.flags -> mask of `AEADF_...' flags")
 #undef GETSETNAME
   { 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),
@@ -911,17 +1064,17 @@ end:
   return (rc);
 }
 
-static PyMethodDef gaeadkey_pymethods[] = {
+static const PyMethodDef gaeadkey_pymethods[] = {
 #define METHNAME(name) gaekmeth_##name
-  METH  (aad,                  "KEY.aad() -> AAD")
-  KWMETH(enc,                  "KEY.enc(NONCE, [hsz], [msz], [tsz]) -> ENC")
-  KWMETH(dec,                  "KEY.dec(NONCE, [hsz], [csz], [tsz]) -> DEC")
+  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
   { 0 }
 };
 
-PyObject *gaeadaad_pywrap(PyObject *cobj, gaead_aad *a,
-                         unsigned f, size_t hsz)
+static PyObject *gaeadaad_pywrap(PyObject *cobj, gaead_aad *a,
+                                unsigned f, size_t hsz)
 {
   gaeadaad_pyobj *ga;
 
@@ -969,19 +1122,18 @@ static PyObject *gaeaget_hsz(PyObject *me, void *hunoz)
 static PyObject *gaeaget_hlen(PyObject *me, void *hunoz)
   { return (gaea_check(me) ? 0 : getulong(GAEADAAD_HLEN(me))); }
 
-static PyGetSetDef gaeadaad_pygetset[] = {
+static const PyGetSetDef gaeadaad_pygetset[] = {
 #define GETSETNAME(op, name) gaea##op##_##name
-  GET  (hsz,                   "AAD.hsz -> precommitted header length or `None'")
-  GET  (hlen,                  "AAD.hlen -> header length so far")
+  GET  (hsz,           "AAD.hsz -> precommitted header length or `None'")
+  GET  (hlen,          "AAD.hlen -> header length so far")
 #undef GETSETNAME
   { 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");
@@ -1050,23 +1202,23 @@ static PyObject *gaeameth_hashstrz(PyObject *me, PyObject *arg)
   RETURN_ME;
 }
 
-static PyMethodDef gaeadaad_pymethods[] = {
+static const PyMethodDef gaeadaad_pymethods[] = {
 #define METHNAME(name) gaeameth_##name
-  METH  (copy,                 "AAD.copy() -> AAD'")
-  METH  (hash,                 "AAD.hash(H)")
+  NAMETH(copy,         "AAD.copy() -> AAD'")
+  METH (hash,          "AAD.hash(H)")
 #define METHU_(n, W, w) METH(hashu##w, "AAD.hashu" #w "(WORD)")
   DOUINTCONV(METHU_)
 #undef METHU_
 #define METHBUF_(n, W, w) METH(hashbuf##w, "AAD.hashbuf" #w "(BYTES)")
   DOUINTCONV(METHBUF_)
 #undef METHBUF_
-  METH  (hashstrz,             "AAD.hashstrz(STRING)")
+  METH (hashstrz,      "AAD.hashstrz(STRING)")
 #undef METHNAME
   { 0 }
 };
 
-PyObject *gaeadenc_pywrap(PyObject *cobj, gaead_enc *e, unsigned f,
-                         size_t hsz, size_t msz, size_t tsz)
+static PyObject *gaeadenc_pywrap(PyObject *cobj, gaead_enc *e, unsigned f,
+                                size_t hsz, size_t msz, size_t tsz)
 {
   gaeadenc_pyobj *ge;
 
@@ -1106,22 +1258,21 @@ static PyObject *gaeeget_tsz(PyObject *me, void *hunoz)
 static PyObject *gaeeget_mlen(PyObject *me, void *hunoz)
   { return getulong(GAEADENC_MLEN(me)); }
 
-static PyGetSetDef gaeadenc_pygetset[] = {
+static const PyGetSetDef gaeadenc_pygetset[] = {
 #define GETSETNAME(op, name) gaee##op##_##name
-  GET  (hsz,                   "ENC.hsz -> precommitted header length or `None'")
-  GET  (msz,                   "ENC.msz -> precommitted message length or `None'")
-  GET  (tsz,                   "ENC.tsz -> precommitted tag length or `None'")
-  GET  (mlen,                  "ENC.mlen -> message length so far")
+  GET  (hsz,           "ENC.hsz -> precommitted header length or `None'")
+  GET  (msz,           "ENC.msz -> precommitted message length or `None'")
+  GET  (tsz,           "ENC.tsz -> precommitted tag length or `None'")
+  GET  (mlen,          "ENC.mlen -> message length so far")
 #undef GETSETNAME
   { 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);
@@ -1231,18 +1382,18 @@ end:
   return (rc);
 }
 
-static PyMethodDef gaeadenc_pymethods[] = {
+static const PyMethodDef gaeadenc_pymethods[] = {
 #define METHNAME(name) gaeemeth_##name
-  METH  (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")
+  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")
 #undef METHNAME
   { 0 }
 };
 
-PyObject *gaeaddec_pywrap(PyObject *cobj, gaead_dec *d, unsigned f,
-                         size_t hsz, size_t csz, size_t tsz)
+static PyObject *gaeaddec_pywrap(PyObject *cobj, gaead_dec *d, unsigned f,
+                                size_t hsz, size_t csz, size_t tsz)
 {
   gaeaddec_pyobj *gd;
   assert(cobj); Py_INCREF(cobj);
@@ -1281,21 +1432,20 @@ static PyObject *gaedget_tsz(PyObject *me, void *hunoz)
 static PyObject *gaedget_clen(PyObject *me, void *hunoz)
   { return getulong(GAEADDEC_CLEN(me)); }
 
-static PyGetSetDef gaeaddec_pygetset[] = {
+static const PyGetSetDef gaeaddec_pygetset[] = {
 #define GETSETNAME(op, name) gaed##op##_##name
-  GET  (hsz,                   "DEC.hsz -> precommitted header length or `None'")
-  GET  (csz,                   "DEC.csz -> precommitted ciphertext length or `None'")
-  GET  (tsz,                   "DEC.tsz -> precommitted tag length or `None'")
-  GET  (clen,                  "DEC.clen -> ciphertext length so far")
+  GET  (hsz,           "DEC.hsz -> precommitted header length or `None'")
+  GET  (csz,          "DEC.csz -> precommitted ciphertext length or `None'")
+  GET  (tsz,           "DEC.tsz -> precommitted tag length or `None'")
+  GET  (clen,          "DEC.clen -> ciphertext length so far")
 #undef GETSETNAME
   { 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));
@@ -1394,12 +1544,12 @@ end:
   return (rc);
 }
 
-static PyMethodDef gaeaddec_pymethods[] = {
+static const PyMethodDef gaeaddec_pymethods[] = {
 #define METHNAME(name) gaedmeth_##name
-  METH  (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")
+  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")
 #undef METHNAME
   { 0 }
 };
@@ -1429,7 +1579,7 @@ static PyTypeObject gcaead_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Authenticated encryption (key) metaclass.",
+  "Authenticated encryption (key) metaclass.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1439,7 +1589,7 @@ static PyTypeObject gcaead_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  gcaead_pygetset,                     /* @tp_getset@ */
+  PYGETSET(gcaead),                    /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -1477,7 +1627,7 @@ static PyTypeObject gaeadkey_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Authenticated encryption key.",
+  "Authenticated encryption key.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1485,7 +1635,7 @@ static PyTypeObject gaeadkey_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  gaeadkey_pymethods,                  /* @tp_methods@ */
+  PYMETHODS(gaeadkey),                 /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
@@ -1525,7 +1675,7 @@ static PyTypeObject gcaeadaad_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Authenticated encryption additional-data hash metaclass.",
+  "Authenticated encryption additional-data hash metaclass.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1573,7 +1723,7 @@ static PyTypeObject gaeadaad_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Authenticated encryption AAD hash.",
+  "Authenticated encryption AAD hash.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1581,9 +1731,9 @@ static PyTypeObject gaeadaad_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  gaeadaad_pymethods,                  /* @tp_methods@ */
+  PYMETHODS(gaeadaad),                 /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  gaeadaad_pygetset,                   /* @tp_getset@ */
+  PYGETSET(gaeadaad),                  /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -1621,7 +1771,7 @@ static PyTypeObject gcaeadenc_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Authenticated encryption operation metaclass.",
+  "Authenticated encryption operation metaclass.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1669,7 +1819,7 @@ static PyTypeObject gaeadenc_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Authenticated encryption operation.",
+  "Authenticated encryption operation.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1677,9 +1827,9 @@ static PyTypeObject gaeadenc_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  gaeadenc_pymethods,                  /* @tp_methods@ */
+  PYMETHODS(gaeadenc),                 /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  gaeadenc_pygetset,                   /* @tp_getset@ */
+  PYGETSET(gaeadenc),                  /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -1717,7 +1867,7 @@ static PyTypeObject gcaeaddec_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Authenticated decryption operation metaclass.",
+  "Authenticated decryption operation metaclass.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1765,7 +1915,7 @@ static PyTypeObject gaeaddec_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Authenticated decryption operation.",
+  "Authenticated decryption operation.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1773,9 +1923,9 @@ static PyTypeObject gaeaddec_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  gaeaddec_pymethods,                  /* @tp_methods@ */
+  PYMETHODS(gaeaddec),                 /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  gaeaddec_pygetset,                   /* @tp_getset@ */
+  PYGETSET(gaeaddec),                  /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -1790,7 +1940,17 @@ static PyTypeObject gaeaddec_pytype_skel = {
 
 /*----- Hash functions ----------------------------------------------------*/
 
-PyTypeObject *gchash_pytype, *ghash_pytype;
+PyTypeObject *gchash_pytype;
+static PyTypeObject *ghash_pytype;
+PyObject *sha_pyobj, *has160_pyobj;
+
+typedef struct ghash_pyobj {
+  PyObject_HEAD
+  ghash *h;
+} ghash_pyobj;
+
+#define GHASH_PYCHECK(o) PyObject_TypeCheck((o), ghash_pytype)
+#define GHASH_H(o) (((ghash_pyobj *)(o))->h)
 
 CONVFUNC(gchash, gchash *, GCHASH_CH)
 CONVFUNC(ghash, ghash *, GHASH_H)
@@ -1805,7 +1965,7 @@ end:
   return (0);
 }
 
-PyObject *gchash_pywrap(gchash *ch)
+static PyObject *gchash_pywrap(gchash *ch)
 {
   gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
   g->ch = ch;
@@ -1848,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)
 {
@@ -1895,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));
@@ -1907,27 +2063,27 @@ static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
   return (rc);
 }
 
-static PyGetSetDef gchash_pygetset[] = {
+static const PyGetSetDef gchash_pygetset[] = {
 #define GETSETNAME(op, name) gch##op##_##name
-  GET  (bufsz,                 "CH.bufsz -> hash buffer size, or zero")
-  GET  (hashsz,                "CH.hashsz -> hash output size")
-  GET  (name,                  "CH.name -> name of this kind of hash")
+  GET  (bufsz,         "CH.bufsz -> hash buffer size, or zero")
+  GET  (hashsz,        "CH.hashsz -> hash output size")
+  GET  (name,          "CH.name -> name of this kind of hash")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyMethodDef ghash_pymethods[] = {
+static const PyMethodDef ghash_pymethods[] = {
 #define METHNAME(name) ghmeth_##name
-  METH (copy,                  "H.copy() -> HH")
-  METH (hash,                  "H.hash(M)")
+  NAMETH(copy,         "H.copy() -> HH")
+  METH (hash,          "H.hash(M)")
 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
   DOUINTCONV(METHU_)
 #undef METHU_
 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
   DOUINTCONV(METHBUF_)
 #undef METHBUF_
-  METH (hashstrz,              "H.hashstrz(STRING)")
-  METH (done,                  "H.done() -> HASH")
+  METH (hashstrz,      "H.hashstrz(STRING)")
+  NAMETH(done,         "H.done() -> HASH")
 #undef METHNAME
   { 0 }
 };
@@ -1957,7 +2113,7 @@ static PyTypeObject gchash_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Hash function metaclass.",
+  "Hash function metaclass.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -1967,7 +2123,7 @@ static PyTypeObject gchash_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  gchash_pygetset,                     /* @tp_getset@ */
+  PYGETSET(gchash),                    /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -2005,7 +2161,7 @@ static PyTypeObject ghash_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Hash function, abstract base class.",
+  "Hash function, abstract base class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -2013,7 +2169,7 @@ static PyTypeObject ghash_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  ghash_pymethods,                     /* @tp_methods@ */
+  PYMETHODS(ghash),                    /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
@@ -2030,11 +2186,30 @@ static PyTypeObject ghash_pytype_skel = {
 
 /*----- Message authentication --------------------------------------------*/
 
-PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
+static PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
+
+typedef struct gcmac_pyobj {
+  PyHeapTypeObject ty;
+  gcmac *cm;
+} gcmac_pyobj;
 
+#define GCMAC_PYCHECK(o) PyObject_TypeCheck((o), gcmac_pytype)
+#define GCMAC_CM(o) (((gcmac_pyobj *)(o))->cm)
+#define GCMAC_F(o) (((gcmac_pyobj *)(o))->f)
 CONVFUNC(gcmac, gcmac *, GCMAC_CM)
-CONVFUNC(gmac, gmac *, GMAC_M)
-CONVFUNC(gmhash, ghash *, GHASH_H)
+static PyObject *gmac_pywrap(PyObject *, gmac *);
+
+typedef struct gmac_pyobj {
+  PyHeapTypeObject ty;
+  gmac *m;
+} gmac_pyobj;
+
+extern PyTypeObject *gmac_pytype;
+#define GMAC_PYCHECK(o) PyObject_TypeCheck((o), gmac_pytype)
+#define GMAC_M(o) (((gmac_pyobj *)(o))->m)
+#define GMAC_F(o) (((gmac_pyobj *)(o))->f)
+extern PyObject *gmac_pywrap(PyObject *, gmac *);
+extern int convgmac(PyObject *, void *);
 
 static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
 {
@@ -2063,7 +2238,7 @@ static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
   return ((PyObject *)g);
 }
 
-PyObject *gcmac_pywrap(gcmac *cm)
+static PyObject *gcmac_pywrap(gcmac *cm)
 {
   gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
   g->cm = cm;
@@ -2080,7 +2255,7 @@ PyObject *gcmac_pywrap(gcmac *cm)
   return ((PyObject *)g);
 }
 
-PyObject *gmac_pywrap(PyObject *cobj, gmac *m)
+static PyObject *gmac_pywrap(PyObject *cobj, gmac *m)
 {
   gmac_pyobj *g;
   if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
@@ -2118,11 +2293,11 @@ static PyObject *gcmget_keysz(PyObject *me, void *hunoz)
 static PyObject *gcmget_tagsz(PyObject *me, void *hunoz)
   { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); }
 
-static PyGetSetDef gcmac_pygetset[] = {
+static const PyGetSetDef gcmac_pygetset[] = {
 #define GETSETNAME(op, name) gcm##op##_##name
-  GET  (keysz,                 "CM.keysz -> acceptable key sizes")
-  GET  (tagsz,                 "CM.tagsz -> MAC output size")
-  GET  (name,                  "CM.name -> name of this kind of MAC")
+  GET  (keysz,         "CM.keysz -> acceptable key sizes")
+  GET  (tagsz,         "CM.tagsz -> MAC output size")
+  GET  (name,          "CM.name -> name of this kind of MAC")
 #undef GETSETNAME
   { 0 }
 };
@@ -2152,7 +2327,7 @@ static PyTypeObject gcmac_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Message authentication code metametaclass.",
+  "Message authentication code metametaclass.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -2162,7 +2337,7 @@ static PyTypeObject gcmac_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  gcmac_pygetset,                      /* @tp_getset@ */
+  PYGETSET(gcmac),                     /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -2200,7 +2375,7 @@ static PyTypeObject gmac_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Message authentication code metaclass, abstract base class.",
+  "Message authentication code metaclass, abstract base class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -2248,7 +2423,7 @@ static PyTypeObject gmhash_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Message authentication code, abstract base class.",
+  "Message authentication code, abstract base class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -2358,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);
@@ -2412,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)
 {
@@ -2442,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));
@@ -2454,31 +2619,31 @@ end:
   return (0);
 }
 
-static PyGetSetDef poly1305cls_pygetset[] = {
+static const PyGetSetDef poly1305cls_pygetset[] = {
 #define GETSETNAME(op, name) poly1305cls##op##_##name
-  GET  (keysz,                 "PC.keysz -> acceptable key sizes")
-  GET  (masksz,                "PC.masksz -> mask size")
-  GET  (tagsz,                 "PC.tagsz -> MAC output size")
-  GET  (name,                  "PC.name -> name of this kind of MAC")
+  GET  (keysz,         "PC.keysz -> acceptable key sizes")
+  GET  (masksz,        "PC.masksz -> mask size")
+  GET  (tagsz,         "PC.tagsz -> MAC output size")
+  GET  (name,          "PC.name -> name of this kind of MAC")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyMethodDef poly1305hash_pymethods[] = {
+static const PyMethodDef poly1305hash_pymethods[] = {
 #define METHNAME(name) polymeth_##name
-  METH  (copy,                 "P.copy() -> PP")
-  METH (hash,                  "P.hash(M)")
+  NAMETH(copy,         "P.copy() -> PP")
+  METH (hash,          "P.hash(M)")
 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
   DOUINTCONV(METHU_)
 #undef METHU_
 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
   DOUINTCONV(METHBUF_)
 #undef METHBUF_
-  METH (hashstrz,              "P.hashstrz(STRING)")
-  METH  (flush,                        "P.flush()")
-  METH  (flushzero,            "P.flushzero()")
-  METH  (concat,               "P.concat(PREFIX, SUFFIX)")
-  METH (done,                  "P.done() -> TAG")
+  METH (hashstrz,      "P.hashstrz(STRING)")
+  NAMETH(flush,                "P.flush()")
+  NAMETH(flushzero,    "P.flushzero()")
+  METH (concat,        "P.concat(PREFIX, SUFFIX)")
+  NAMETH(done,         "P.done() -> TAG")
 #undef METHNAME
   { 0 }
 };
@@ -2508,7 +2673,7 @@ static PyTypeObject poly1305cls_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Poly1305 metametaclass.  Best not to ask.",
+  "Poly1305 metametaclass.  Best not to ask.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -2518,7 +2683,7 @@ static PyTypeObject poly1305cls_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  poly1305cls_pygetset,                        /* @tp_getset@ */
+  PYGETSET(poly1305cls),               /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -2556,7 +2721,7 @@ static PyTypeObject poly1305key_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"poly1305(K): Poly1305 key.",
+  "poly1305(K): Poly1305 key.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -2604,7 +2769,7 @@ static PyTypeObject poly1305hash_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Poly1305 MAC context base class.",
+  "Poly1305 MAC context base class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -2612,7 +2777,7 @@ static PyTypeObject poly1305hash_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  poly1305hash_pymethods,              /* @tp_methods@ */
+  PYMETHODS(poly1305hash),             /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
@@ -2667,8 +2832,7 @@ typedef struct kxvik_pyobj {
   unsigned n;
 } kxvik_pyobj;
 
-static PyObject *kxvik_pynew(PyTypeObject *ty,
-                                 PyObject *arg, PyObject *kw)
+static PyObject *kxvik_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
 {
   unsigned n = 24;
   kxvik_pyobj *rc = 0;
@@ -2683,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);
 }
 
@@ -2738,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;
 }
@@ -2766,19 +2927,19 @@ end:
   return (rc);
 }
 
-static PyGetSetDef kxvik_pygetset[] = {
+static const PyGetSetDef kxvik_pygetset[] = {
 #define GETSETNAME(op, name) kxvik##op##_##name
-  GETSET(nround,               "KECCAK.nround -> number of rounds")
+  GETSET(nround,       "KECCAK.nround -> number of rounds")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyMethodDef kxvik_pymethods[] = {
+static const PyMethodDef kxvik_pymethods[] = {
 #define METHNAME(func) kxvikmeth_##func
-  METH (copy,                  "KECCAK.copy() -> KECCAK'")
-  METH (mix,                   "KECCAK.mix(DATA)")
-  METH (extract,               "KECCAK.extract(NOCTETS)")
-  METH (step,                  "KECCAK.step()")
+  NAMETH(copy,         "KECCAK.copy() -> KECCAK'")
+  METH (mix,           "KECCAK.mix(DATA)")
+  METH (extract,       "KECCAK.extract(NOCTETS)")
+  NAMETH(step,         "KECCAK.step()")
 #undef METHNAME
   { 0 }
 };
@@ -2808,7 +2969,7 @@ static PyTypeObject kxvik_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Keccak1600([nround = 24]): Keccak-p[1600, n] state.",
+  "Keccak1600([nround = 24]): Keccak-p[1600, n] state.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -2816,9 +2977,9 @@ static PyTypeObject kxvik_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  kxvik_pymethods,                     /* @tp_methods@ */
+  PYMETHODS(kxvik),                    /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  kxvik_pygetset,                      /* @tp_getset@ */
+  PYGETSET(kxvik),                     /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -2927,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;
@@ -2951,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);
 }
 
@@ -3002,30 +3160,30 @@ static PyObject *shakeget_state(PyObject *me, void *hunoz)
                              st == 1 ? "squeeze" : "dead"));
 }
 
-static PyGetSetDef shake_pygetset[] = {
+static const PyGetSetDef shake_pygetset[] = {
 #define GETSETNAME(op, name) shake##op##_##name
-  GET  (rate,                  "S.rate -> rate, in bytes")
-  GET  (buffered,              "S.buffered -> amount currently buffered")
-  GET  (state,                 "S.state -> `absorb', `squeeze', `dead'")
+  GET  (rate,          "S.rate -> rate, in bytes")
+  GET  (buffered,      "S.buffered -> amount currently buffered")
+  GET  (state,         "S.state -> `absorb', `squeeze', `dead'")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyMethodDef shake_pymethods[] = {
+static const PyMethodDef shake_pymethods[] = {
 #define METHNAME(func) shakemeth_##func
-  METH  (copy,                 "S.copy() -> SS")
-  METH (hash,                  "S.hash(M)")
+  NAMETH(copy,         "S.copy() -> SS")
+  METH (hash,          "S.hash(M)")
 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
   DOUINTCONV(METHU_)
 #undef METHU_
 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
   DOUINTCONV(METHBUF_)
 #undef METHBUF_
-  METH (hashstrz,              "S.hashstrz(STRING)")
-  METH (xof,                   "S.xof()")
-  METH (done,                  "S.done(LEN) ->H")
-  METH (get,                   "S.get(LEN) -> H")
-  METH (mask,                  "S.mask(M) -> C")
+  METH (hashstrz,      "S.hashstrz(STRING)")
+  NAMETH(xof,          "S.xof()")
+  METH (done,          "S.done(LEN) -> H")
+  METH (get,           "S.get(LEN) -> H")
+  METH (mask,          "S.mask(M) -> C")
 #undef METHNAME
   { 0 }
 };
@@ -3055,7 +3213,7 @@ static PyTypeObject shake_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"SHAKE/cSHAKE base class.",
+  "SHAKE/cSHAKE/KMAC base class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -3063,9 +3221,9 @@ static PyTypeObject shake_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  shake_pymethods,                     /* @tp_methods@ */
+  PYMETHODS(shake),                    /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  shake_pygetset,                      /* @tp_getset@ */
+  PYGETSET(shake),                     /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -3103,7 +3261,7 @@ static PyTypeObject shake128_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.",
+  "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -3151,7 +3309,7 @@ static PyTypeObject shake256_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.",
+  "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -3306,19 +3464,19 @@ end:
   return (rc);
 }
 
-static PyGetSetDef gcprp_pygetset[] = {
+static const PyGetSetDef gcprp_pygetset[] = {
 #define GETSETNAME(op, name) gcp##op##_##name
-  GET  (keysz,                 "CP.keysz -> acceptable key sizes")
-  GET  (blksz,                 "CP.blksz -> block size")
-  GET  (name,                  "CP.name -> name of this kind of PRP")
+  GET  (keysz,         "CP.keysz -> acceptable key sizes")
+  GET  (blksz,         "CP.blksz -> block size")
+  GET  (name,          "CP.name -> name of this kind of PRP")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyMethodDef gprp_pymethods[] = {
+static const PyMethodDef gprp_pymethods[] = {
 #define METHNAME(name) gpmeth_##name
-  METH (encrypt,               "P.encrypt(PT) -> CT")
-  METH (decrypt,               "P.decrypt(CT) -> PT")
+  METH (encrypt,       "P.encrypt(PT) -> CT")
+  METH (decrypt,       "P.decrypt(CT) -> PT")
 #undef METHNAME
   { 0 }
 };
@@ -3348,7 +3506,7 @@ static PyTypeObject gcprp_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Pseudorandom permutation metaclass.",
+  "Pseudorandom permutation metaclass.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -3358,7 +3516,7 @@ static PyTypeObject gcprp_pytype_skel = {
   0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  gcprp_pygetset,                      /* @tp_getset@ */
+  PYGETSET(gcprp),                     /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -3396,7 +3554,7 @@ static PyTypeObject gprp_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Pseudorandom permutation, abstract base class.",
+  "Pseudorandom permutation, abstract base class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -3404,7 +3562,7 @@ static PyTypeObject gprp_pytype_skel = {
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
-  gprp_pymethods,                      /* @tp_methods@ */
+  PYMETHODS(gprp),                     /* @tp_methods@ */
   0,                                   /* @tp_members@ */
   0,                                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
@@ -3421,28 +3579,10 @@ static PyTypeObject gprp_pytype_skel = {
 
 /*----- Main code ---------------------------------------------------------*/
 
-static PyMethodDef methods[] = {
+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")
+#define METH_HDANCE(hdance, HDance) METH(hdance##_prf,                 \
+        "" #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
   METH_HDANCE(hsalsa20, "HSalsa20")
   METH_HDANCE(hsalsa2012, "HSalsa20/12")
   METH_HDANCE(hsalsa208, "HSalsa20/8")