bytestring.c (bytestring_pyrepeat): Don't divide by zero.
[catacomb-python] / group.c
diff --git a/group.c b/group.c
index 001eb19..b5a1815 100644 (file)
--- a/group.c
+++ b/group.c
@@ -1,7 +1,5 @@
 /* -*-c-*-
  *
- * $Id$
- *
  * Abstract group inteface
  *
  * (c) 2004 Straylight/Edgeware
@@ -42,11 +40,11 @@ PyObject *fginfo_pywrap(gprime_param *dp, PyTypeObject *ty)
 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))
@@ -95,11 +93,11 @@ static PyObject *meth__DHInfo_generate(PyObject *me,
   unsigned steps = 0;
   grand *r = &rand_global;
   pgev evt = { 0 };
-  char *kwlist[] =
+  static const char *const kwlist[] =
     { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
   PyObject *rc = 0;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", KWLIST,
                                   &me, convuint, &pl, convuint, &ql,
                                   convpgev, &evt, convgrand, &r,
                                   convuint, &steps))
@@ -122,14 +120,16 @@ static PyObject *meth__DHInfo_genlimlee(PyObject *me,
   pgev 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[] = {
+    "class", "pbits", "qbits", "event", "ievent",
+    "rng", "nsteps", "subgroupp", 0
+  };
   size_t i, nf;
   mp **v = 0;
   PyObject *rc = 0, *vec = 0;
 
   if (!PyArg_ParseTupleAndKeywords(arg, kw,
-                                  "OO&O&|O&O&O&O&O&:genlimlee", kwlist,
+                                  "OO&O&|O&O&O&O&O&:genlimlee", KWLIST,
                                   &me, convuint, &pl, convuint, &ql,
                                   convpgev, &oe, convpgev, &ie,
                                   convgrand, &r, convuint, &steps,
@@ -157,12 +157,12 @@ static PyObject *meth__DHInfo_genkcdsa(PyObject *me,
   unsigned steps = 0;
   grand *r = &rand_global;
   pgev evt = { 0 };
-  char *kwlist[] = { "class", "pbits", "qbits",
-                    "event", "rng", "nsteps", 0 };
+  static const char *const kwlist[] =
+    { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
   mp *v = MP_NEW;
   PyObject *rc = 0;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|O&O&O&:genkcdsa", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|O&O&O&:genkcdsa", KWLIST,
                                   &me, convuint, &pl, convuint, &ql,
                                   convpgev, &evt, convgrand, &r,
                                   convuint, &steps))
@@ -186,13 +186,13 @@ static PyObject *meth__DHInfo_gendsa(PyObject *me,
   unsigned steps = 0;
   dsa_seed ds;
   char *k;
-  int ksz;
+  Py_ssize_t ksz;
   pgev evt = { 0 };
-  char *kwlist[] =
+  static const char *const kwlist[] =
     { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 };
   PyObject *rc = 0;
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&s#|O&O&:gendsa", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&s#|O&O&:gendsa", KWLIST,
                                   &me, convuint, &pl, convuint, &ql,
                                   &k, &ksz, convpgev, &evt,
                                   convuint, &steps))
@@ -227,7 +227,7 @@ static PyObject *namedgroups(const pentry *pp, int *ne)
     }
     c = PyInt_FromLong(i);
   found:
-    PyDict_SetItemString(d, (/*unconst*/ char *)pp[i].name, c);
+    PyDict_SetItemString(d, (/*unconst*/ char *)p, c);
     Py_DECREF(c);
   }
   *ne = i;
@@ -266,7 +266,7 @@ static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty,
   if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
   qd.p = p;
   qd.e = 0;
-  if (parse(&qd, &gp)) SYNERR(qd.e);
+  if (parse(&qd, &gp)) VALERR(qd.e);
   rc = fginfo_pywrap(&gp, ty);
 end:
   return (rc);
@@ -304,7 +304,7 @@ static PyGetSetDef bindhinfo_pygetset[] = {
 
 static PyTypeObject fginfo_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.FGInfo",                   /* @tp_name@ */
+  "FGInfo",                            /* @tp_name@ */
   sizeof(fginfo_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -327,7 +327,7 @@ static PyTypeObject fginfo_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-  "Abstract base class for field-group information objects.",
+"Abstract base class for field-group information objects.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
@@ -352,7 +352,7 @@ static PyTypeObject fginfo_pytype_skel = {
 
 static PyTypeObject dhinfo_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.DHInfo",                   /* @tp_name@ */
+  "DHInfo",                            /* @tp_name@ */
   sizeof(fginfo_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -375,7 +375,7 @@ 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@ */
@@ -400,7 +400,7 @@ static PyTypeObject dhinfo_pytype_skel = {
 
 static PyTypeObject bindhinfo_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.BinDHInfo",                        /* @tp_name@ */
+  "BinDHInfo",                         /* @tp_name@ */
   sizeof(fginfo_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -423,7 +423,7 @@ 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@ */
@@ -495,7 +495,7 @@ 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;
@@ -504,7 +504,7 @@ static PyObject *ge_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
   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);
@@ -519,7 +519,7 @@ static PyObject *ge_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
     sc.buf = PyString_AS_STRING(x);
     sc.lim = sc.buf + PyString_GET_SIZE(x);
     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));
@@ -544,7 +544,7 @@ static PyObject *group_dopywrap(PyTypeObject *ty, group *g)
   gobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   gobj->ty.ht_type.tp_free = 0;
   gobj->ty.ht_type.tp_new = ge_pynew;
-  PyType_Ready(&gobj->ty.ht_type);
+  typeready(&gobj->ty.ht_type);
   return ((PyObject *)gobj);
 }
 
@@ -697,8 +697,8 @@ 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);
@@ -718,19 +718,35 @@ 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") == 0) {
+    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)
@@ -786,11 +802,11 @@ static PyObject *gmeth_mexp(PyObject *me, PyObject *arg)
 
 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&:checkgroup", kwlist,
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:checkgroup", KWLIST,
                                   convgrand, &r))
     goto end;
   if ((p = G_CHECK(GROUP_G(me), r)) != 0)
@@ -817,7 +833,7 @@ static PyObject *meth__GE_frombuf(PyObject *me, PyObject *arg)
 {
   buf b;
   char *p;
-  int n;
+  Py_ssize_t n;
   group *g;
   ge *x = 0;
 
@@ -838,7 +854,7 @@ static PyObject *meth__GE_fromraw(PyObject *me, PyObject *arg)
 {
   buf b;
   char *p;
-  int n;
+  Py_ssize_t n;
   group *g;
   ge *x = 0;
 
@@ -859,7 +875,7 @@ static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg)
 {
   mptext_stringctx sc;
   char *p;
-  int n;
+  Py_ssize_t n;
   group *g;
   ge *x = 0;
 
@@ -870,9 +886,9 @@ static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg)
   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);
@@ -889,7 +905,7 @@ static PyObject *meth__Group_parse(PyObject *me, PyObject *arg)
   qd.p = p;
   qd.e = 0;
   if ((g = group_parse(&qd)) == 0)
-    SYNERR(qd.e);
+    VALERR(qd.e);
   return (group_pywrap(g));
 end:
   return (0);
@@ -916,6 +932,20 @@ static PyObject *gget_g(PyObject *me, void *hunoz)
   G_COPY(g, x, g->g); return (ge_pywrap(me, x));
 }
 
+static long 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))); }
 
@@ -988,7 +1018,7 @@ static PyNumberMethods ge_pynumber = {
 
 static PyTypeObject ge_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.GE",                       /* @tp_name@ */
+  "GE",                                        /* @tp_name@ */
   sizeof(ge_pyobj),                    /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1001,7 +1031,7 @@ static PyTypeObject ge_pytype_skel = {
   &ge_pynumber,                                /* @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@ */
@@ -1058,7 +1088,7 @@ G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
 
 static PyTypeObject group_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.Group",                    /* @tp_name@ */
+  "Group",                             /* @tp_name@ */
   sizeof(group_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1110,7 +1140,7 @@ 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));
 }
 
@@ -1125,9 +1155,9 @@ 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))));
@@ -1135,7 +1165,7 @@ static PyObject *primegroup_pynew(PyTypeObject *ty,
 
 static PyTypeObject primegroup_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.PrimeGroup",               /* @tp_name@ */
+  "PrimeGroup",                                /* @tp_name@ */
   sizeof(group_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1158,7 +1188,7 @@ 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@ */
@@ -1187,7 +1217,7 @@ 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));
 }
 
@@ -1202,9 +1232,9 @@ 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))));
@@ -1212,7 +1242,7 @@ static PyObject *bingroup_pynew(PyTypeObject *ty,
 
 static PyTypeObject bingroup_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.BinGroup",                 /* @tp_name@ */
+  "BinGroup",                          /* @tp_name@ */
   sizeof(group_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1235,7 +1265,7 @@ 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@ */
@@ -1279,9 +1309,9 @@ 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));
@@ -1290,7 +1320,7 @@ static PyObject *ecgroup_pynew(PyTypeObject *ty,
 
 static PyTypeObject ecgroup_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.ECGroup",                  /* @tp_name@ */
+  "ECGroup",                           /* @tp_name@ */
   sizeof(group_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1313,7 +1343,7 @@ static PyTypeObject ecgroup_pytype_skel = {
     Py_TPFLAGS_BASETYPE,
 
   /* @tp_doc@ */
-"Elliptic curve groups.",
+"ECGroup(INFO): elliptic curve groups.",
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */