@@@ py_buffer/freebin wip
[catacomb-python] / group.c
diff --git a/group.c b/group.c
index 7ae387a..77c3e9f 100644 (file)
--- a/group.c
+++ b/group.c
@@ -1,13 +1,11 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Abstract group inteface
  *
  * (c) 2004 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of the Python interface to Catacomb.
  *
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * Catacomb/Python is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with Catacomb/Python; if not, write to the Free Software Foundation,
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 /*----- DH and binary group infos -----------------------------------------*/
 
-PyObject *fginfo_pywrap(gprime_param *dp, PyTypeObject *ty)
+static PyTypeObject *fginfo_pytype, *dhinfo_pytype, *bindhinfo_pytype;
+
+typedef struct fginfo_pyobj {
+  PyObject_HEAD
+  gprime_param dp;
+} fginfo_pyobj;
+
+#define FGINFO_DP(fg) (&((fginfo_pyobj *)(fg))->dp)
+
+static PyObject *fginfo_pywrap(gprime_param *dp, PyTypeObject *ty)
 {
   fginfo_pyobj *z = PyObject_New(fginfo_pyobj, ty);
-  z->dp.p = MP_COPY(dp->p);
-  z->dp.q = MP_COPY(dp->q);
-  z->dp.g = MP_COPY(dp->g);
+  z->dp = *dp;
   return ((PyObject *)z);
 }
 
 static PyObject *fginfo_pynew(PyTypeObject *ty,
                              PyObject *arg, PyObject *kw)
 {
-  char *kwlist[] = { "p", "r", "g", 0 };
+  static const char *const kwlist[] = { "p", "r", "g", 0 };
   gprime_param dp = { 0 };
   fginfo_pyobj *z = 0;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&O&:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&O&:new", KWLIST,
                                   convmp, &dp.p,
                                   convmp, &dp.q,
-                                  &convmp, dp.g))
+                                  convmp, &dp.g))
     goto end;
   z = PyObject_New(fginfo_pyobj, ty);
   z->dp = dp;
@@ -64,22 +69,22 @@ end:
 }
 
 static PyObject *figet_r(PyObject *me, void *hunoz)
-  { return mp_pywrap(FGINFO_DP(me)->q); }
+  { return mp_pywrap(MP_COPY(FGINFO_DP(me)->q)); }
 
 static PyObject *diget_p(PyObject *me, void *hunoz)
-  { return mp_pywrap(FGINFO_DP(me)->p); }
+  { return mp_pywrap(MP_COPY(FGINFO_DP(me)->p)); }
 
 static PyObject *diget_g(PyObject *me, void *hunoz)
-  { return mp_pywrap(FGINFO_DP(me)->g); }
+  { return mp_pywrap(MP_COPY(FGINFO_DP(me)->g)); }
 
 static PyObject *biget_p(PyObject *me, void *hunoz)
-  { return gf_pywrap(FGINFO_DP(me)->p); }
+  { return gf_pywrap(MP_COPY(FGINFO_DP(me)->p)); }
 
 static PyObject *biget_m(PyObject *me, void *hunoz)
   { return PyInt_FromLong(mp_octets(FGINFO_DP(me)->p) - 1); }
 
 static PyObject *biget_g(PyObject *me, void *hunoz)
-  { return gf_pywrap(FGINFO_DP(me)->g); }
+  { return gf_pywrap(MP_COPY(FGINFO_DP(me)->g)); }
 
 static void fginfo_pydealloc(PyObject *me)
 {
@@ -89,61 +94,65 @@ static void fginfo_pydealloc(PyObject *me)
   FREEOBJ(me);
 }
 
-static PyObject *meth__DHInfo_generate(PyObject *me,
-                                      PyObject *arg, PyObject *kw)
+static PyObject *dimeth_generate(PyObject *me, PyObject *arg, PyObject *kw)
 {
   dh_param dp;
   unsigned ql = 0, pl;
   unsigned steps = 0;
   grand *r = &rand_global;
-  pgev evt = { 0 };
-  char *kwlist[] =
-    { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
+  struct excinfo exc = EXCINFO_INIT;
+  pypgev evt = { { 0 } };
+  static const char *const kwlist[] =
+    { "pbits", "qbits", "event", "rng", "nsteps", 0 };
   PyObject *rc = 0;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", kwlist,
-                                  &me, convuint, &pl, convuint, &ql,
+  evt.exc = &exc;
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&O&O&O&:generate", KWLIST,
+                                  convuint, &pl, convuint, &ql,
                                   convpgev, &evt, convgrand, &r,
                                   convuint, &steps))
     goto end;
-  if (dh_gen(&dp, ql, pl, steps, r, evt.proc, evt.ctx))
-    PGENERR;
+  if (dh_gen(&dp, ql, pl, steps, r, evt.ev.proc, evt.ev.ctx))
+    PGENERR(&exc);
   rc = fginfo_pywrap(&dp, dhinfo_pytype);
 end:
   droppgev(&evt);
   return (rc);
 }
 
-static PyObject *meth__DHInfo_genlimlee(PyObject *me,
-                                       PyObject *arg, PyObject *kw)
+static PyObject *dimeth_genlimlee(PyObject *me, PyObject *arg, PyObject *kw)
 {
   dh_param dp;
   unsigned ql, pl;
   unsigned steps = 0;
   grand *r = &rand_global;
-  pgev oe = { 0 }, ie = { 0 };
+  struct excinfo exc = EXCINFO_INIT;
+  pypgev oe = { { 0 } }, ie = { { 0 } };
   int subgroupp = 1;
   unsigned f = 0;
-  char *kwlist[] = { "class", "pbits", "qbits", "event", "ievent",
-                    "rng", "nsteps", "subgroupp", 0 };
+  static const char *const kwlist[] = {
+    "pbits", "qbits", "event", "ievent",
+    "rng", "nsteps", "subgroupp", 0
+  };
   size_t i, nf;
   mp **v = 0;
   PyObject *rc = 0, *vec = 0;
 
+  oe.exc = ie.exc = &exc;
   if (!PyArg_ParseTupleAndKeywords(arg, kw,
-                                  "OO&O&|O&O&O&O&O&:genlimlee", kwlist,
-                                  &me, convuint, &pl, convuint, &ql,
+                                  "O&O&|O&O&O&O&O&:genlimlee", KWLIST,
+                                  convuint, &pl, convuint, &ql,
                                   convpgev, &oe, convpgev, &ie,
                                   convgrand, &r, convuint, &steps,
                                   convbool, &subgroupp))
     goto end;
   if (subgroupp) f |= DH_SUBGROUP;
   if (dh_limlee(&dp, ql, pl, f, steps, r,
-               oe.proc, oe.ctx, ie.proc, ie.ctx, &nf, &v))
-    PGENERR;
+               oe.ev.proc, oe.ev.ctx, ie.ev.proc, ie.ev.ctx, &nf, &v))
+    PGENERR(&exc);
   vec = PyList_New(nf);
   for (i = 0; i < nf; i++)
-    PyList_SetItem(vec, i, mp_pywrap(v[i]));
+    PyList_SET_ITEM(vec, i, mp_pywrap(v[i]));
   xfree(v);
   rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype), vec);
 end:
@@ -151,83 +160,65 @@ end:
   return (rc);
 }
 
-static PyObject *meth__DHInfo_gendsa(PyObject *me,
-                                    PyObject *arg, PyObject *kw)
+static PyObject *dimeth_genkcdsa(PyObject *me, PyObject *arg, PyObject *kw)
 {
-  dsa_param dp;
+  dh_param dp;
   unsigned ql, pl;
   unsigned steps = 0;
-  dsa_seed ds;
-  char *k;
-  int ksz;
-  pgev evt = { 0 };
-  char *kwlist[] =
-    { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 };
+  grand *r = &rand_global;
+  struct excinfo exc = EXCINFO_INIT;
+  pypgev evt = { { 0 } };
+  static const char *const kwlist[] =
+    { "pbits", "qbits", "event", "rng", "nsteps", 0 };
+  mp *v = MP_NEW;
   PyObject *rc = 0;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&s#|O&O&:generate", kwlist,
-                                  &me, convuint, &pl, convuint, &ql,
-                                  &k, &ksz, convpgev, &evt,
+  evt.exc = &exc;
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&O&O&:genkcdsa", KWLIST,
+                                  convuint, &pl, convuint, &ql,
+                                  convpgev, &evt, convgrand, &r,
                                   convuint, &steps))
     goto end;
-  if (dsa_gen(&dp, ql, pl, steps, k, ksz, &ds, evt.proc, evt.ctx))
-    PGENERR;
-  rc = Py_BuildValue("(NNl)", fginfo_pywrap(&dp, dhinfo_pytype),
-                    bytestring_pywrap(ds.p, ds.sz), (long)ds.count);
-  xfree(ds.p);
+  if (dh_kcdsagen(&dp, ql, pl, 0, steps, r, evt.ev.proc, evt.ev.ctx))
+    PGENERR(&exc);
+  mp_div(&v, 0, dp.p, dp.q);
+  v = mp_lsr(v, v, 1);
+  rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype),
+                    mp_pywrap(v));
 end:
   droppgev(&evt);
   return (rc);
 }
 
-static int npgroups = -1, nbingroups = -1;
-
-static PyObject *namedgroups(const pentry *pp, int *ne)
-{
-  int i, j;
-  const char *p;
-  PyObject *d, *c;
-
-  d = PyDict_New();
-  for (i = 0; pp[i].name; i++) {
-    p = pp[i].name;
-    for (j = 0; j < i; j++) {
-      if (pp[i].data == pp[j].data) {
-       c = PyDict_GetItemString(d, (/*unconst*/ char *)pp[j].name);
-       Py_INCREF(c);
-       goto found;
-      }
-    }
-    c = PyInt_FromLong(i);
-  found:
-    PyDict_SetItemString(d, (/*unconst*/ char *)pp[i].name, c);
-    Py_DECREF(c);
-  }
-  *ne = i;
-  return (d);  
-}
-
-static PyObject *meth__groupn(PyObject *me, PyObject *arg,
-                             PyTypeObject *ty, const pentry *pp, int ne)
+static PyObject *dimeth_gendsa(PyObject *me, PyObject *arg, PyObject *kw)
 {
-  int i;
-  gprime_param gp;
+  dsa_param dp;
+  unsigned ql, pl;
+  unsigned steps = 0;
+  dsa_seed ds;
+  struct bin k;
+  struct excinfo exc = EXCINFO_INIT;
+  pypgev evt = { { 0 } };
+  static const char *const kwlist[] =
+    { "pbits", "qbits", "seed", "event", "nsteps", 0 };
   PyObject *rc = 0;
 
-  if (!PyArg_ParseTuple(arg, "Oi:_groupn", &me, &i)) goto end;
-  if (i < 0 || i >= ne) VALERR("group index out of range");
-  dh_infofromdata(&gp, pp[i].data);
-  rc = fginfo_pywrap(&gp, ty);
+  evt.exc = &exc;
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&O&|O&O&:gendsa", KWLIST,
+                                  convuint, &pl, convuint, &ql,
+                                  convbin, &k,
+                                  convpgev, &evt, convuint, &steps))
+    goto end;
+  if (dsa_gen(&dp, ql, pl, steps, k.p, k.sz, &ds, evt.ev.proc, evt.ev.ctx))
+    PGENERR(&exc);
+  rc = Py_BuildValue("(NNl)", fginfo_pywrap(&dp, dhinfo_pytype),
+                    bytestring_pywrap(ds.p, ds.sz), (long)ds.count);
+  xfree(ds.p);
 end:
+  droppgev(&evt);
   return (rc);
 }
 
-static PyObject *meth__DHInfo__groupn(PyObject *me, PyObject *arg)
-  { return (meth__groupn(me, arg, dhinfo_pytype, ptab, npgroups)); }
-
-static PyObject *meth__BinDHInfo__groupn(PyObject *me, PyObject *arg)
-  { return (meth__groupn(me, arg, bindhinfo_pytype, bintab, nbingroups)); }
-
 static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty,
                             int (*parse)(qd_parse *, gprime_param *))
 {
@@ -236,48 +227,76 @@ static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty,
   gprime_param gp;
   PyObject *rc = 0;
 
-  if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
-  qd.p = p;
-  qd.e = 0;
-  if (parse(&qd, &gp)) SYNERR(qd.e);
+  if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end;
+  qd.p = p; qd.e = 0;
+  if (parse(&qd, &gp)) VALERR(qd.e);
   rc = fginfo_pywrap(&gp, ty);
 end:
-  return (rc);  
+  return (rc);
 }
 
-static PyObject *meth__DHInfo_parse(PyObject *me, PyObject *arg)
+static PyObject *dimeth_parse(PyObject *me, PyObject *arg)
   { return (meth__parse(me, arg, dhinfo_pytype, dh_parse)); }
 
-static PyObject *meth__BinDHInfo_parse(PyObject *me, PyObject *arg)
+static PyObject *bimeth_parse(PyObject *me, PyObject *arg)
   { return (meth__parse(me, arg, bindhinfo_pytype, dhbin_parse)); }
 
-static PyGetSetDef fginfo_pygetset[] = {
+static const PyGetSetDef fginfo_pygetset[] = {
 #define GETSETNAME(op, name) fi##op##_##name
-  GET  (r,                     "I.r -> group order")
+  GET  (r,             "I.r -> group order")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyGetSetDef dhinfo_pygetset[] = {
+static const PyGetSetDef dhinfo_pygetset[] = {
 #define GETSETNAME(op, name) di##op##_##name
-  GET  (p,                     "I.p -> prime")
-  GET  (g,                     "I.g -> generator")
+  GET  (p,             "I.p -> prime")
+  GET  (g,             "I.g -> generator")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyGetSetDef bindhinfo_pygetset[] = {
+static const PyMethodDef dhinfo_pymethods[] = {
+#define METHNAME(name) dimeth_##name
+  SMTH (parse,         "parse(STR) -> D, REST")
+  KWSMTH(generate,
+       "generate(PBITS, [qbits = 0], [event = pgen_nullev],\n"
+       "        [rng = rand], [nsteps = 0]) -> D")
+  KWSMTH(genlimlee,
+       "genlimlee(PBITS, QBITS, [event = pgen_nullev], "
+                                                 "[ievent = pgen_nullev],\n"
+       "         [rng = rand], [nsteps = 0], [subgroupp = True]) "
+                                                         "-> (D, [Q, ...])")
+  KWSMTH(gendsa,
+       "gendsa(PBITS, QBITS, SEED, [event = pgen_nullev], [nsteps = 0])\n"
+       "  -> (D, SEED, COUNT)")
+  KWSMTH(genkcdsa,
+       "gendsa(PBITS, QBITS, [event = pgen_nullev], "
+                                             "[rng = rand], [nsteps = 0])\n"
+       "  -> (D, V)")
+#undef METHNAME
+  { 0 }
+};
+
+static const PyGetSetDef bindhinfo_pygetset[] = {
 #define GETSETNAME(op, name) bi##op##_##name
-  GET  (p,                     "I.p -> irreducible polynomial")
-  GET  (m,                     "I.m -> degree of polynomial")
-  GET  (g,                     "I.g -> generator")
+  GET  (p,             "I.p -> irreducible polynomial")
+  GET  (m,             "I.m -> degree of polynomial")
+  GET  (g,             "I.g -> generator")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyTypeObject fginfo_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
-  "catacomb.FGInfo",                   /* @tp_name@ */
+static const PyMethodDef bindhinfo_pymethods[] = {
+#define METHNAME(name) bimeth_##name
+  SMTH (parse,         "parse(STR) -> D, REST")
+#undef METHNAME
+  { 0 }
+};
+
+static const PyTypeObject fginfo_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "FGInfo",                            /* @tp_name@ */
   sizeof(fginfo_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -307,10 +326,10 @@ static PyTypeObject fginfo_pytype_skel = {
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  fginfo_pygetset,                     /* @tp_getset@ */
+  PYGETSET(fginfo),                    /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -323,9 +342,9 @@ static PyTypeObject fginfo_pytype_skel = {
   0                                    /* @tp_is_gc@ */
 };
 
-static PyTypeObject dhinfo_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
-  "catacomb.DHInfo",                   /* @tp_name@ */
+static const PyTypeObject dhinfo_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "DHInfo",                            /* @tp_name@ */
   sizeof(fginfo_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -348,17 +367,17 @@ static PyTypeObject dhinfo_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-  "Standard (integer) Diffie-Hellman group information.",
+  "DHInfo(P, R, G): standard (integer) Diffie-Hellman group information.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
-  0,                                   /* @tp_methods@ */
+  0,                                   /* @tp_iternext@ */
+  PYMETHODS(dhinfo),                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  dhinfo_pygetset,                     /* @tp_getset@ */
+  PYGETSET(dhinfo),                    /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -371,9 +390,9 @@ static PyTypeObject dhinfo_pytype_skel = {
   0                                    /* @tp_is_gc@ */
 };
 
-static PyTypeObject bindhinfo_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
-  "catacomb.BinDHInfo",                        /* @tp_name@ */
+static const PyTypeObject bindhinfo_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "BinDHInfo",                         /* @tp_name@ */
   sizeof(fginfo_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -396,17 +415,17 @@ static PyTypeObject bindhinfo_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-  "Binary-field Diffie-Hellman group information.",
+  "BinDHInfo(P, R, G): binary-field Diffie-Hellman group information.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
-  0,                                   /* @tp_methods@ */
+  0,                                   /* @tp_iternext@ */
+  PYMETHODS(bindhinfo),                        /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  bindhinfo_pygetset,                  /* @tp_getset@ */
+  PYGETSET(bindhinfo),                 /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -422,11 +441,11 @@ static PyTypeObject bindhinfo_pytype_skel = {
 /*----- General utilities -------------------------------------------------*/
 
 PyTypeObject *ge_pytype, *group_pytype;
-PyTypeObject *primegroup_pytype, *bingroup_pytype, *ecgroup_pytype;
+static PyTypeObject *primegroup_pytype, *bingroup_pytype, *ecgroup_pytype;
 
 group *group_copy(group *g)
 {
-  if (strcmp(G_NAME(g), "prime") == 0) {
+  if (STRCMP(G_NAME(g), ==, "prime")) {
     gctx_prime *gc = (gctx_prime *)g;
     gprime_param gp;
     gp.g = G_TOINT(g, MP_NEW, g->g);
@@ -434,15 +453,15 @@ group *group_copy(group *g)
     gp.q = gc->g.r;
     g = group_prime(&gp);
     MP_DROP(gp.g);
-  } else if (strcmp(G_NAME(g), "bin") == 0) {
+  } else if (STRCMP(G_NAME(g), ==, "bin")) {
     gctx_bin *gc = (gctx_bin *)g;
     gbin_param gb;
     gb.g = G_TOINT(g, MP_NEW, g->g);
     gb.p = gc->r.p;
     gb.q = gc->g.r;
     g = group_binary(&gb);
-    MP_DROP(gb.g);    
-  } else if (strcmp(G_NAME(g), "ec") == 0) {
+    MP_DROP(gb.g);
+  } else if (STRCMP(G_NAME(g), ==, "ec")) {
     gctx_ec *gc = (gctx_ec *)g;
     ec_info ei;
     if ((ei.c = eccurve_copy(gc->ei.c)) == 0)
@@ -468,16 +487,17 @@ PyObject *ge_pywrap(PyObject *gobj, ge *x)
 
 static PyObject *ge_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
 {
-  char *kwlist[] = { "x", 0 };
+  static const char *const kwlist[] = { "x", 0 };
   PyObject *x;
   group *g;
   ec p = EC_INIT;
   mp *y = 0;
   ge *xx = 0;
+  size_t n;
   mptext_stringctx sc;
 
   g = GROUP_G(ty);
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &x)) goto end;
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", KWLIST, &x)) goto end;
   xx = G_CREATE(g);
   if (ECPT_PYCHECK(x)) {
     getecptout(&p, x);
@@ -488,11 +508,10 @@ static PyObject *ge_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
     if (G_FROMINT(g, xx, y))
       TYERR("can't convert from integer");
     MP_DROP(y);
-  } else if (PyString_Check(x)) {
-    sc.buf = PyString_AS_STRING(x);
-    sc.lim = sc.buf + PyString_GET_SIZE(x);
+  } else if (TEXT_CHECK(x)) {
+    TEXT_PTRLEN(x, sc.buf, n); sc.lim = sc.buf + n;
     if (G_READ(g, xx, &mptext_stringops, &sc) || sc.buf < sc.lim)
-      SYNERR("malformed group element string");
+      VALERR("malformed group element string");
   } else
     TYERR("can't convert to group element");
   return (ge_pywrap((PyObject *)ty, xx));
@@ -505,20 +524,19 @@ end:
 
 static PyObject *group_dopywrap(PyTypeObject *ty, group *g)
 {
-  group_pyobj *gobj = newtype(ty, 0);
+  group_pyobj *gobj = newtype(ty, 0, g->ops->name);
   gobj->g = g;
-  gobj->ty.tp_name = (/*unconst*/ char *)g->ops->name;
-  gobj->ty.tp_basicsize = sizeof(ge_pyobj);
-  gobj->ty.tp_base = ge_pytype;
+  gobj->ty.ht_type.tp_basicsize = sizeof(ge_pyobj);
+  gobj->ty.ht_type.tp_base = ge_pytype;
   Py_INCREF(group_pytype);
-  gobj->ty.tp_flags = (Py_TPFLAGS_DEFAULT |
-                      Py_TPFLAGS_BASETYPE |
-                      Py_TPFLAGS_CHECKTYPES |
-                      Py_TPFLAGS_HEAPTYPE);
-  gobj->ty.tp_alloc = PyType_GenericAlloc;
-  gobj->ty.tp_free = 0;
-  gobj->ty.tp_new = ge_pynew;
-  PyType_Ready(&gobj->ty);
+  gobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
+                              Py_TPFLAGS_BASETYPE |
+                              Py_TPFLAGS_CHECKTYPES |
+                              Py_TPFLAGS_HEAPTYPE);
+  gobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
+  gobj->ty.ht_type.tp_free = 0;
+  gobj->ty.ht_type.tp_new = ge_pynew;
+  typeready(&gobj->ty.ht_type);
   return ((PyObject *)gobj);
 }
 
@@ -526,9 +544,9 @@ PyObject *group_pywrap(group *g)
 {
   PyTypeObject *ty;
 
-  if (strcmp(G_NAME(g), "prime") == 0) ty = primegroup_pytype;
-  else if (strcmp(G_NAME(g), "bin") == 0) ty = bingroup_pytype;
-  else if (strcmp(G_NAME(g), "ec") == 0) ty = ecgroup_pytype;
+  if (STRCMP(G_NAME(g), ==, "prime")) ty = primegroup_pytype;
+  else if (STRCMP(G_NAME(g), ==, "bin")) ty = bingroup_pytype;
+  else if (STRCMP(G_NAME(g), ==, "ec")) ty = ecgroup_pytype;
   else abort();
   return (group_dopywrap(ty, g));
 }
@@ -553,11 +571,10 @@ BINOP(div)
 #undef BINOP
 
 #define UNOP(name)                                                     \
-  static PyObject *gemeth_##name(PyObject *me, PyObject *arg)          \
+  static PyObject *gemeth_##name(PyObject *me)                         \
   {                                                                    \
     group *g;                                                          \
     ge *z;                                                             \
-    if (!PyArg_ParseTuple(arg, ":" #name)) return (0);                 \
     g = GE_G(me);                                                      \
     z = G_CREATE(g);                                                   \
     g->ops->name(g, z, GE_X(me));                                      \
@@ -572,7 +589,10 @@ static PyObject *ge_pyexp(PyObject *x, PyObject *n, PyObject *m)
   mp *nn;
   ge *z;
 
-  if (m != Py_None || !GE_PYCHECK(x) || (nn = getmp(n)) == 0)
+  if (m != Py_None || !GE_PYCHECK(x)) RETURN_NOTIMPL;
+  if (FE_PYCHECK(n) && FE_F(n)->ops->ty == FTY_PRIME)
+    nn = F_OUT(FE_F(n), MP_NEW, FE_X(n));
+  else if ((nn = implicitmp(n)) == 0)
     RETURN_NOTIMPL;
   z = G_CREATE(GE_G(x));
   G_EXP(GE_G(x), z, GE_X(x), nn);
@@ -627,9 +647,8 @@ end:
   return (rc);
 }
 
-static PyObject *gemeth_check(PyObject *me, PyObject *arg)
+static PyObject *gemeth_check(PyObject *me)
 {
-  if (!PyArg_ParseTuple(arg, ":check")) goto end;
   if (group_check(GE_G(me), GE_X(me))) VALERR("bad group element");
   RETURN_OBJ(me);
 end:
@@ -645,11 +664,12 @@ static PyObject *ge_pystr(PyObject *me)
   PyObject *rc;
 
   group_writedstr(GE_G(me), GE_X(me), &d);
-  rc = PyString_FromStringAndSize(d.buf, d.len);
+  rc = TEXT_FROMSTRLEN(d.buf, d.len);
   DDESTROY(&d);
   return (rc);
 }
 
+#ifdef PY2
 static PyObject *ge_pylong(PyObject *me)
 {
   mp *x = 0;
@@ -657,11 +677,12 @@ static PyObject *ge_pylong(PyObject *me)
 
   if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
     TYERR("can't convert to integer");
-  rc = (PyObject *)mp_topylong(x);
+  rc = mp_topylong(x);
 end:
   mp_drop(x);
   return (rc);
 }
+#endif
 
 static PyObject *ge_pyint(PyObject *me)
 {
@@ -671,18 +692,17 @@ static PyObject *ge_pyint(PyObject *me)
 
   if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
     TYERR("can't convert to integer");
-  if (mp_tolong_checked(x, &l)) goto end;
-  rc = PyInt_FromLong(l);
+  if (!mp_tolong_checked(x, &l, 0)) rc = PyInt_FromLong(l);
+  else rc = mp_topylong(x);
 end:
   mp_drop(x);
   return (rc);
 }
 
-static PyObject *gemeth_toint(PyObject *me, PyObject *arg)
+static PyObject *gemeth_toint(PyObject *me)
 {
   mp *x;
 
-  if (!PyArg_ParseTuple(arg, ":toint")) goto end;
   if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
     TYERR("can't convert to integer");
   return (mp_pywrap(x));
@@ -692,54 +712,68 @@ end:
 
 static PyObject *gemeth_toec(PyObject *me, PyObject *arg, PyObject *kw)
 {
-  char *kwlist[] = { "curve", 0 };
-  PyTypeObject *cty = ecpt_pytype;
+  static const char *const kwlist[] = { "curve", 0 };
+  PyTypeObject *cty = 0;
+  PyObject *rc = 0;
+  group *g;
+  ec_curve *c;
   ec p = EC_INIT;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:toec", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:toec", KWLIST,
                                   &cty)) goto end;
-  if (!PyType_Check(cty) || !PyType_IsSubtype(cty, ecpt_pytype))
-    TYERR("want subtype of catacomb.ECPt");
-  if (G_TOEC(GE_G(me), &p, GE_X(me)))
+  g = GROUP_G(GE_GOBJ(me));
+  if (cty) {
+    if (!PyType_Check(cty) || !PyType_IsSubtype(cty, ecpt_pytype))
+      TYERR("want subtype of catacomb.ECPt");
+    Py_INCREF((PyObject *)cty);
+  } else if (STRCMP(G_NAME(g), ==, "ec")) {
+    c = eccurve_copy(((gctx_ec *)g)->ei.c);
+    cty = (PyTypeObject *)eccurve_pywrap(0, c);
+  } else  {
+    cty = ecpt_pytype;
+    Py_INCREF((PyObject *)cty);
+  }
+  if (G_TOEC(GE_G(me), &p, GE_X(me))) {
+    Py_DECREF((PyObject *)cty);
     TYERR("can't convert to ec point");
-  return (ecpt_pywrapout(cty, &p));
+  }
+  rc = ecpt_pywrapout(cty, &p);
+  Py_DECREF((PyObject *)cty);
 end:
-  return (0);
+  return (rc);
 }
 
-static PyObject *gemeth_tobuf(PyObject *me, PyObject *arg)
+static PyObject *gemeth_tobuf(PyObject *me)
 {
   buf b;
   PyObject *rc;
   size_t n;
 
-  if (!PyArg_ParseTuple(arg, ":tobuf")) return (0);
   n = GE_G(me)->noctets + 4;
   rc = bytestring_pywrap(0, n);
-  buf_init(&b, PyString_AS_STRING(rc), n);
+  buf_init(&b, BIN_PTR(rc), n);
   G_TOBUF(GE_G(me), &b, GE_X(me));
   assert(BOK(&b));
-  _PyString_Resize(&rc, BLEN(&b));
+  BIN_SETLEN(rc, BLEN(&b));
   return (rc);
 }
 
-static PyObject *gemeth_toraw(PyObject *me, PyObject *arg)
+static PyObject *gemeth_toraw(PyObject *me)
 {
   buf b;
   PyObject *rc;
   size_t n;
 
-  if (!PyArg_ParseTuple(arg, ":toraw")) return (0);
   n = GE_G(me)->noctets;
   rc = bytestring_pywrap(0, n);
-  buf_init(&b, PyString_AS_STRING(rc), n);
+  buf_init(&b, BIN_PTR(rc), n);
   G_TORAW(GE_G(me), &b, GE_X(me));
   assert(BOK(&b));
-  _PyString_Resize(&rc, BLEN(&b));
+  BIN_SETLEN(rc, BLEN(&b));
   return (rc);
 }
 
-static PyObject *gmexp_exp(PyObject *me, void *pp, int n)
+static PyObject *gmexp_exp(PyObject *me, void *pp, size_t n)
 {
   ge *z = G_CREATE(GROUP_G(me));
   G_MEXP(GROUP_G(me), z, pp, n);
@@ -758,13 +792,13 @@ static PyObject *gmeth_mexp(PyObject *me, PyObject *arg)
                      gmexp_id, gmexp_fill, gmexp_exp, gmexp_drop));
 }
 
-static PyObject *gmeth_check(PyObject *me, PyObject *arg, PyObject *kw)
+static PyObject *gmeth_checkgroup(PyObject *me, PyObject *arg, PyObject *kw)
 {
-  char *kwlist[] = { "rng", 0 };
+  static const char *const kwlist[] = { "rng", 0 };
   grand *r = &rand_global;
   const char *p;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:check", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:checkgroup", KWLIST,
                                   convgrand, &r))
     goto end;
   if ((p = G_CHECK(GROUP_G(me), r)) != 0)
@@ -787,83 +821,73 @@ end:
   return (0);
 }
 
-static PyObject *meth__GE_frombuf(PyObject *me, PyObject *arg)
+static PyObject *gemeth_frombuf(PyObject *me, PyObject *arg)
 {
   buf b;
-  char *p;
-  int n;
+  struct bin in;
   group *g;
   ge *x = 0;
 
-  if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &n))
-    return (0);
+  if (!PyArg_ParseTuple(arg, "O&:frombuf", convbin, &in)) return (0);
   g = GROUP_G(me);
-  buf_init(&b, p, n);
+  buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
   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);
   return (0);
 }
 
-static PyObject *meth__GE_fromraw(PyObject *me, PyObject *arg)
+static PyObject *gemeth_fromraw(PyObject *me, PyObject *arg)
 {
   buf b;
-  char *p;
-  int n;
+  struct bin in;
   group *g;
   ge *x = 0;
 
-  if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &n))
-    return (0);
+  if (!PyArg_ParseTuple(arg, "O&:fromraw", convbin, &in)) return (0);
   g = GROUP_G(me);
-  buf_init(&b, p, n);
+  buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
   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);
   return (0);
 }
 
-static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg)
+static PyObject *gemeth_fromstring(PyObject *me, PyObject *arg)
 {
   mptext_stringctx sc;
   char *p;
-  int n;
+  Py_ssize_t n;
   group *g;
   ge *x = 0;
 
-  if (!PyArg_ParseTuple(arg, "Os#:fromstring", &me, &p, &n))
-    return (0);
+  if (!PyArg_ParseTuple(arg, "s#:fromstring", &p, &n)) return (0);
   sc.buf = p;
   sc.lim = sc.buf + n;
   g = GROUP_G(me);
   x = G_CREATE(g);
   if (G_READ(g, x, &mptext_stringops, &sc))
-    SYNERR("bad group element string");
+    VALERR("bad group element string");
   return (Py_BuildValue("(Ns#)", ge_pywrap(me, x),
-                       sc.buf, (int)(sc.lim - sc.buf)));
+                       sc.buf, (Py_ssize_t)(sc.lim - sc.buf)));
 end:
   if (x) G_DESTROY(g, x);
   return (0);
 }
 
-static PyObject *meth__Group_parse(PyObject *me, PyObject *arg)
+static PyObject *gmeth_parse(PyObject *me, PyObject *arg)
 {
   char *p;
   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)
-    SYNERR(qd.e);
+  if (!PyArg_ParseTuple(arg, "s:parse", &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);
@@ -890,38 +914,57 @@ static PyObject *gget_g(PyObject *me, void *hunoz)
   G_COPY(g, x, g->g); return (ge_pywrap(me, x));
 }
 
+static Py_hash_t ge_pyhash(PyObject *me)
+{
+  buf b;
+  size_t sz = GE_G(me)->noctets + 4;
+  uint32 h = 0xf672c776 + GE_G(me)->ops->ty;
+  octet *p = xmalloc(sz);
+  buf_init(&b, p, sz);
+  G_TOBUF(GE_G(me), &b, GE_X(me));
+  assert(BOK(&b));
+  h = unihash_hash(&unihash_global, h, BBASE(&b), BLEN(&b));
+  xfree(p);
+  return (h % LONG_MAX);
+}
+
 static PyObject *gget_r(PyObject *me, void *hunoz)
   { return (mp_pywrap(MP_COPY(GROUP_G(me)->r))); }
 
 static PyObject *gget_h(PyObject *me, void *hunoz)
   { return (mp_pywrap(MP_COPY(GROUP_G(me)->h))); }
 
-static PyGetSetDef ge_pygetset[] = {
+static const PyGetSetDef ge_pygetset[] = {
 #define GETSETNAME(op, name) ge##op##_##name
   GET  (group,         "X.group -> group containing X")
 #undef GETSETNAME
   { 0 }
 };
 
-static PyMethodDef ge_pymethods[] = {
+static const PyMethodDef ge_pymethods[] = {
 #define METHNAME(name) gemeth_##name
-  METH (inv,           "X.inv() -> inverse element of X")
-  METH (sqr,           "X.sqr() -> X^2 = X * X")
-  METH (check,         "X.check() -> check X really belongs to its group")
-  METH (toint,         "X.toint() -> X converted to an integer")
-  KWMETH(toec,         "\
-X.toec(curve = ecpt) -> X converted to elliptic curve point")
-  METH (tobuf,         "X.tobuf() -> X in buffer representation")
-  METH (toraw,         "X.toraw() -> X in raw representation")
+  NAMETH(inv,          "X.inv() -> inverse element of X")
+  NAMETH(sqr,          "X.sqr() -> X^2 = X * X")
+  NAMETH(check,                "X.check() -> check X really belongs to its group")
+  NAMETH(toint,                "X.toint() -> X converted to an integer")
+  KWMETH(toec,         "X.toec([curve = ECPt]) -> "
+                                      "X converted to elliptic curve point")
+  NAMETH(tobuf,                "X.tobuf() -> X in buffer representation")
+  NAMETH(toraw,                "X.toraw() -> X in raw representation")
+  CMTH (frombuf,       "frombuf(BUF) -> X, REST")
+  CMTH (fromraw,       "fromraw(BUF) -> X, REST")
+  CMTH (fromstring,    "fromstring(STR) -> X, REST")
 #undef METHNAME
   { 0 }
 };
 
-static PyNumberMethods ge_pynumber = {
+static const PyNumberMethods ge_pynumber = {
   0,                                   /* @nb_add@ */
   0,                                   /* @nb_subtract@ */
   ge_pymul,                            /* @nb_multiply@ */
+#ifdef PY2
   ge_pydiv,                            /* @nb_divide@ */
+#endif
   0,                                   /* @nb_remainder@ */
   0,                                   /* @nb_divmod@ */
   ge_pyexp,                            /* @nb_power@ */
@@ -935,17 +978,23 @@ static PyNumberMethods ge_pynumber = {
   0,                                   /* @nb_and@ */
   0,                                   /* @nb_xor@ */
   0,                                   /* @nb_or@ */
+#ifdef PY2
   0,                                   /* @nb_coerce@ */
+#endif
   ge_pyint,                            /* @nb_int@ */
-  ge_pylong,                           /* @nb_long@ */
+  PY23(ge_pylong, 0),                  /* @nb_long@ */
   0 /* meaningless */,                 /* @nb_float@ */
+#ifdef PY2
   0,                                   /* @nb_oct@ */
   0,                                   /* @nb_hex@ */
+#endif
 
   0,                                   /* @nb_inplace_add@ */
   0,                                   /* @nb_inplace_subtract@ */
   0,                                   /* @nb_inplace_multiply@ */
+#ifdef PY2
   0,                                   /* @nb_inplace_divide@ */
+#endif
   0,                                   /* @nb_inplace_remainder@ */
   0,                                   /* @nb_inplace_power@ */
   0,                                   /* @nb_inplace_lshift@ */
@@ -960,9 +1009,9 @@ static PyNumberMethods ge_pynumber = {
   0,                                   /* @nb_inplace_true_divide@ */
 };
 
-static PyTypeObject ge_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
-  "catacomb.GE",                       /* @tp_name@ */
+static const PyTypeObject ge_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "GE",                                        /* @tp_name@ */
   sizeof(ge_pyobj),                    /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -972,10 +1021,10 @@ static PyTypeObject ge_pytype_skel = {
   0,                                   /* @tp_setattr@ */
   0,                                   /* @tp_compare@ */
   0,                                   /* @tp_repr@ */
-  &ge_pynumber,                                /* @tp_as_number@ */
+  PYNUMBER(ge),                                /* @tp_as_number@ */
   0,                                   /* @tp_as_sequence@ */
   0,                                   /* @tp_as_mapping@ */
-  0,                                   /* @tp_hash@ */
+  ge_pyhash,                           /* @tp_hash@ */
   0,                                   /* @tp_call@ */
   ge_pystr,                            /* @tp_str@ */
   0,                                   /* @tp_getattro@ */
@@ -986,17 +1035,17 @@ static PyTypeObject ge_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Group elements, abstract base class.",
+  "Group elements, abstract base class.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
   ge_pyrichcompare,                    /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
-  ge_pymethods,                                /* @tp_methods@ */
+  0,                                   /* @tp_iternext@ */
+  PYMETHODS(ge),                       /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  ge_pygetset,                         /* @tp_getset@ */
+  PYGETSET(ge),                                /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -1009,7 +1058,7 @@ static PyTypeObject ge_pytype_skel = {
   0                                    /* @tp_is_gc@ */
 };
 
-static PyGetSetDef group_pygetset[] = {
+static const PyGetSetDef group_pygetset[] = {
 #define GETSETNAME(op, name) g##op##_##name
   GET  (noctets,       "G.noctets -> size in octets of element")
   GET  (nbits,         "G.nbits -> size in bits of element")
@@ -1021,18 +1070,18 @@ static PyGetSetDef group_pygetset[] = {
   { 0 }
 };
 
-static PyMethodDef group_pymethods[] = {
+static const PyMethodDef group_pymethods[] = {
 #define METHNAME(name) gmeth_##name
-  METH (mexp,          "\
-G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
-  KWMETH(check,                "G.check(rand = random): check group is good")
+  METH (mexp,        "G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
+  KWMETH(checkgroup,   "G.checkgroup([rng = rand]): check group is good")
+  SMTH (parse,         "parse(STR) -> G, REST")
 #undef METHNAME
   { 0 }
 };
 
-static PyTypeObject group_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
-  "catacomb.Group",                    /* @tp_name@ */
+static const PyTypeObject group_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "Group",                             /* @tp_name@ */
   sizeof(group_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1055,17 +1104,17 @@ static PyTypeObject group_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Abstract base class for groups.",
+  "Abstract base class for groups.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
   group_pyrichcompare,                 /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
-  group_pymethods,                     /* @tp_methods@ */
+  0,                                   /* @tp_iternext@ */
+  PYMETHODS(group),                    /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  group_pygetset,                      /* @tp_getset@ */
+  PYGETSET(group),                     /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -1084,11 +1133,11 @@ static PyObject *pgget_info(PyObject *me, void *hunoz)
   gctx_prime *gg = (gctx_prime *)GROUP_G(me);
   dp.p = MP_COPY(gg->mm.m);
   dp.q = MP_COPY(gg->g.r);
-  dp.g = mpmont_reduce(&gg->mm, MP_NEW, gg->gen);
+  dp.g = mpmont_reduce(&gg->mm, MP_NEW, gg->gen.x);
   return (fginfo_pywrap(&dp, dhinfo_pytype));
 }
 
-static PyGetSetDef primegroup_pygetset[] = {
+static const PyGetSetDef primegroup_pygetset[] = {
 #define GETSETNAME(op, name) pg##op##_##name
   GET  (info,          "G.info -> information about the group")
 #undef GETSETNAME
@@ -1099,17 +1148,17 @@ static PyObject *primegroup_pynew(PyTypeObject *ty,
                                  PyObject *arg, PyObject *kw)
 {
   PyObject *i;
-  char *kwlist[] = { "info", 0 };
+  static const char *const kwlist[] = { "info", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
                                   dhinfo_pytype, &i))
     return (0);
   return (group_dopywrap(ty, group_prime(FGINFO_DP(i))));
 }
 
-static PyTypeObject primegroup_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
-  "catacomb.PrimeGroup",               /* @tp_name@ */
+static const PyTypeObject primegroup_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "PrimeGroup",                                /* @tp_name@ */
   sizeof(group_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1132,17 +1181,17 @@ static PyTypeObject primegroup_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Subgroups of prime fields.",
+  "PrimeGroup(INFO): subgroups of prime fields.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  primegroup_pygetset,                 /* @tp_getset@ */
+  PYGETSET(primegroup),                        /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -1161,11 +1210,11 @@ static PyObject *bgget_info(PyObject *me, void *hunoz)
   gctx_bin *gg = (gctx_bin *)GROUP_G(me);
   dp.p = MP_COPY(gg->r.p);
   dp.q = MP_COPY(gg->g.r);
-  dp.g = MP_COPY(gg->gen);
+  dp.g = MP_COPY(gg->gen.x);
   return (fginfo_pywrap(&dp, bindhinfo_pytype));
 }
 
-static PyGetSetDef bingroup_pygetset[] = {
+static const PyGetSetDef bingroup_pygetset[] = {
 #define GETSETNAME(op, name) bg##op##_##name
   GET  (info,          "G.info -> information about the group")
 #undef GETSETNAME
@@ -1176,17 +1225,17 @@ static PyObject *bingroup_pynew(PyTypeObject *ty,
                                PyObject *arg, PyObject *kw)
 {
   PyObject *i;
-  char *kwlist[] = { "info", 0 };
+  static const char *const kwlist[] = { "info", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
                                   bindhinfo_pytype, &i))
     return (0);
   return (group_dopywrap(ty, group_binary(FGINFO_DP(i))));
 }
 
-static PyTypeObject bingroup_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
-  "catacomb.BinGroup",                 /* @tp_name@ */
+static const PyTypeObject bingroup_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "BinGroup",                          /* @tp_name@ */
   sizeof(group_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1209,17 +1258,17 @@ static PyTypeObject bingroup_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Subgroups of binary fields.",
+  "BinGroup(INFO): subgroups of binary fields.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  bingroup_pygetset,                   /* @tp_getset@ */
+  PYGETSET(bingroup),                  /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -1241,7 +1290,7 @@ static PyObject *egget_info(PyObject *me, void *hunoz)
   return (ecinfo_pywrap(&ei));
 }
 
-static PyGetSetDef ecgroup_pygetset[] = {
+static const PyGetSetDef ecgroup_pygetset[] = {
 #define GETSETNAME(op, name) eg##op##_##name
   GET  (info,          "G.info -> information about the group")
 #undef GETSETNAME
@@ -1253,18 +1302,18 @@ static PyObject *ecgroup_pynew(PyTypeObject *ty,
 {
   PyObject *i;
   ec_info ei;
-  char *kwlist[] = { "info", 0 };
+  static const char *const kwlist[] = { "info", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
                                   ecinfo_pytype, &i))
     return (0);
   ecinfo_copy(&ei, ECINFO_EI(i));
   return (group_dopywrap(ty, group_ec(&ei)));
 }
 
-static PyTypeObject ecgroup_pytype_skel = {
-  PyObject_HEAD_INIT(&PyType_Type) 0,  /* Header */
-  "catacomb.ECGroup",                  /* @tp_name@ */
+static const PyTypeObject ecgroup_pytype_skel = {
+  PyVarObject_HEAD_INIT(0, 0)          /* Header */
+  "ECGroup",                           /* @tp_name@ */
   sizeof(group_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1287,17 +1336,17 @@ static PyTypeObject ecgroup_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Elliptic curve groups.",
+  "ECGroup(INFO): elliptic curve groups.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
   0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
-  0,                                   /* @tp_iternexr@ */
+  0,                                   /* @tp_iternext@ */
   0,                                   /* @tp_methods@ */
   0,                                   /* @tp_members@ */
-  ecgroup_pygetset,                    /* @tp_getset@ */
+  PYGETSET(ecgroup),                   /* @tp_getset@ */
   0,                                   /* @tp_base@ */
   0,                                   /* @tp_dict@ */
   0,                                   /* @tp_descr_get@ */
@@ -1312,29 +1361,6 @@ static PyTypeObject ecgroup_pytype_skel = {
 
 /*----- Global stuff ------------------------------------------------------*/
 
-static PyMethodDef methods[] = {
-#define METHNAME(name) meth_##name
-  METH (_GE_frombuf,           "frombuf(BUF) -> X, REST")
-  METH (_GE_fromraw,           "fromraw(BUF) -> X, REST")
-  METH (_GE_fromstring,        "fromstring(STR) -> X, REST")
-  METH (_Group_parse,          "parse(STR) -> G, REST")
-  METH (_DHInfo_parse,         "parse(STR) -> D, REST")
-  METH (_BinDHInfo_parse,      "parse(STR) -> D, REST")
-  METH (_DHInfo__groupn,       0)
-  METH (_BinDHInfo__groupn,    0)
-  KWMETH(_DHInfo_generate,     "\
-generate(PBITS, [qbits = 0, event = pgen_nullev,\n\
-         rng = rand, nsteps = 0]) -> D")
-  KWMETH(_DHInfo_genlimlee,    "\
-genlimlee(PBITS, QBITS, [event = pgen_nullev, ievent = pgen_nullev,\n\
-          rng = rand, nsteps = 0, subgroupp = True]) -> (D, [Q, ...])")
-  KWMETH(_DHInfo_gendsa,       "\
-gendsa(PBITS, QBITS, SEED, [event = pgen_nullev, nsteps = 0])\n\
-  -> (D, SEED, COUNT)")
-#undef METHNAME
-  { 0 }
-};
-
 void group_pyinit(void)
 {
   INITTYPE(fginfo, root);
@@ -1345,9 +1371,34 @@ void group_pyinit(void)
   INITTYPE(primegroup, group);
   INITTYPE(bingroup, group);
   INITTYPE(ecgroup, group);
-  addmethods(methods);
 }
 
+static const char *grp_namefn(const void *p)
+  { const pentry *pt = p; return (pt->name); }
+
+static int grp_ixfn(const pentry *tab, const pentry *pt)
+{
+  int i;
+
+  for (i = 0; tab[i].name; i++)
+    if (tab[i].data == pt->data) return (i);
+  return (-1);
+}
+static int pgrp_ixfn(const void *p) { return (grp_ixfn(ptab, p)); }
+static int bgrp_ixfn(const void *p) { return (grp_ixfn(bintab, p)); }
+
+static PyObject *grp_valfn(const pentry *tab, PyTypeObject *ty, int i)
+{
+  gprime_param gp;
+
+  dh_infofromdata(&gp, tab[i].data);
+  return (fginfo_pywrap(&gp, ty));
+}
+static PyObject *pgrp_valfn(int i)
+  { return (grp_valfn(ptab, dhinfo_pytype, i)); }
+static PyObject *bgrp_valfn(int i)
+  { return (grp_valfn(bintab, bindhinfo_pytype, i)); }
+
 void group_pyinsert(PyObject *mod)
 {
   INSERT("FGInfo", fginfo_pytype);
@@ -1358,8 +1409,10 @@ void group_pyinsert(PyObject *mod)
   INSERT("PrimeGroup", primegroup_pytype);
   INSERT("BinGroup", bingroup_pytype);
   INSERT("ECGroup", ecgroup_pytype);
-  INSERT("_pgroups", namedgroups(ptab, &npgroups));
-  INSERT("_bingroups", namedgroups(bintab, &nbingroups));
+  INSERT("primegroups", make_grouptab(ptab, sizeof(*ptab),
+                                     grp_namefn, pgrp_ixfn, pgrp_valfn));
+  INSERT("bingroups", make_grouptab(bintab, sizeof(*bintab),
+                                   grp_namefn, bgrp_ixfn, bgrp_valfn));
 }
 
 /*----- That's all, folks -------------------------------------------------*/