chiark / gitweb /
algorithms.c: Fix docstring for `GCHash.hashsz'.
[catacomb-python] / algorithms.c
index ea5243471478707cdfa8b7e8cd6545cd50d6326c..06fdc3271200f7a99f36fe2786c024dc28bd2e6a 100644 (file)
@@ -1,6 +1,4 @@
 /* -*-c-*-
- *
- * $Id$
  *
  * Symmetric cryptography
  *
@@ -232,7 +230,7 @@ static PyMemberDef keyszset_pymembers[] = {
 
 static PyTypeObject keysz_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeySZ",                    /* @tp_name@ */
+  "KeySZ",                             /* @tp_name@ */
   sizeof(keysz_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -280,7 +278,7 @@ static PyTypeObject keysz_pytype_skel = {
 
 static PyTypeObject keyszany_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeySZAny",                 /* @tp_name@ */
+  "KeySZAny",                          /* @tp_name@ */
   sizeof(keysz_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -328,7 +326,7 @@ static PyTypeObject keyszany_pytype_skel = {
 
 static PyTypeObject keyszrange_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeySZRange",               /* @tp_name@ */
+  "KeySZRange",                                /* @tp_name@ */
   sizeof(keyszrange_pyobj),            /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -377,7 +375,7 @@ sizes, and requires the key length to be a multiple of some value.",
 
 static PyTypeObject keyszset_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.KeySZSet",                 /* @tp_name@ */
+  "KeySZSet",                          /* @tp_name@ */
   sizeof(keyszset_pyobj),              /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -424,6 +422,24 @@ few listed sizes.",
   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;
@@ -471,7 +487,7 @@ PyObject *gccipher_pywrap(gccipher *cc)
   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   g->ty.ht_type.tp_free = 0;
   g->ty.ht_type.tp_new = gcipher_pynew;
-  PyType_Ready(&g->ty.ht_type);
+  typeready(&g->ty.ht_type);
   return ((PyObject *)g);
 }
 
@@ -591,7 +607,7 @@ static PyMethodDef gcipher_pymethods[] = {
 
 static PyTypeObject gccipher_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.GCCipher",                 /* @tp_name@ */
+  "GCCipher",                          /* @tp_name@ */
   sizeof(gccipher_pyobj),              /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -639,7 +655,7 @@ static PyTypeObject gccipher_pytype_skel = {
 
 static PyTypeObject gcipher_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.GCipher",                  /* @tp_name@ */
+  "GCipher",                           /* @tp_name@ */
   sizeof(gcipher_pyobj),               /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -715,7 +731,7 @@ PyObject *gchash_pywrap(gchash *ch)
   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   g->ty.ht_type.tp_free = 0;
   g->ty.ht_type.tp_new = ghash_pynew;
-  PyType_Ready(&g->ty.ht_type);
+  typeready(&g->ty.ht_type);
   return ((PyObject *)g);
 }
 
@@ -771,7 +787,7 @@ static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
 static PyGetSetDef gchash_pygetset[] = {
 #define GETSETNAME(op, name) gch##op##_##name
   GET  (bufsz,                 "CH.bufsz -> hash buffer size, or zero")
-  GET  (hashsz,                "CH.blksz -> hash output size")
+  GET  (hashsz,                "CH.hashsz -> hash output size")
   GET  (name,                  "CH.name -> name of this kind of hash")
 #undef GETSETNAME
   { 0 }
@@ -826,7 +842,7 @@ static PyMethodDef ghash_pymethods[] = {
 
 static PyTypeObject gchash_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.GCHash",                   /* @tp_name@ */
+  "GCHash",                            /* @tp_name@ */
   sizeof(gchash_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -874,7 +890,7 @@ static PyTypeObject gchash_pytype_skel = {
 
 static PyTypeObject ghash_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.GHash",                    /* @tp_name@ */
+  "GHash",                             /* @tp_name@ */
   sizeof(ghash_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -970,7 +986,7 @@ PyObject *gcmac_pywrap(gcmac *cm)
   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   g->ty.ht_type.tp_free = 0;
   g->ty.ht_type.tp_new = gmac_pynew;
-  PyType_Ready(&g->ty.ht_type);
+  typeready(&g->ty.ht_type);
   return ((PyObject *)g);
 }
 
@@ -980,6 +996,7 @@ PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f)
   if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
   else Py_INCREF(cobj);
   g = newtype((PyTypeObject *)cobj, 0, 0);
+  g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
   g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name);
   g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name);
   g->ty.ht_type.tp_base = gmhash_pytype;
@@ -990,7 +1007,7 @@ PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f)
   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   g->ty.ht_type.tp_free = 0;
   g->ty.ht_type.tp_new = gmhash_pynew;
-  PyType_Ready(&g->ty.ht_type);
+  typeready(&g->ty.ht_type);
   g->m = m;
   g->f = f;
   return ((PyObject *)g);
@@ -1024,7 +1041,7 @@ static PyGetSetDef gcmac_pygetset[] = {
 
 static PyTypeObject gcmac_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.GCMAC",                    /* @tp_name@ */
+  "GCMAC",                             /* @tp_name@ */
   sizeof(gchash_pyobj),                        /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1072,7 +1089,7 @@ static PyTypeObject gcmac_pytype_skel = {
 
 static PyTypeObject gmac_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.GMAC",                     /* @tp_name@ */
+  "GMAC",                              /* @tp_name@ */
   sizeof(gmac_pyobj),                  /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1120,7 +1137,7 @@ static PyTypeObject gmac_pytype_skel = {
 
 static PyTypeObject gmhash_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.GMACHash",                 /* @tp_name@ */
+  "GMACHash",                          /* @tp_name@ */
   sizeof(ghash_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1259,7 +1276,7 @@ static PyObject *gcprp_pywrap(const prpinfo *prp)
   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
   g->ty.ht_type.tp_free = 0;
   g->ty.ht_type.tp_new = gprp_pynew;
-  PyType_Ready(&g->ty.ht_type);
+  typeready(&g->ty.ht_type);
   return ((PyObject *)g);
 }
 
@@ -1317,7 +1334,7 @@ static PyMethodDef gprp_pymethods[] = {
 
 static PyTypeObject gcprp_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.GCPRP",                    /* @tp_name@ */
+  "GCPRP",                             /* @tp_name@ */
   sizeof(gcprp_pyobj),                 /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1365,7 +1382,7 @@ static PyTypeObject gcprp_pytype_skel = {
 
 static PyTypeObject gprp_pytype_skel = {
   PyObject_HEAD_INIT(0) 0,             /* Header */
-  "catacomb.GPRP",                     /* @tp_name@ */
+  "GPRP",                              /* @tp_name@ */
   sizeof(gprp_pyobj),                  /* @tp_basicsize@ */
   0,                                   /* @tp_itemsize@ */
 
@@ -1413,6 +1430,28 @@ static PyTypeObject gprp_pytype_skel = {
 
 /*----- Main code ---------------------------------------------------------*/
 
+static 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")
+#undef METHNAME
+  { 0 }
+};
+
 void algorithms_pyinit(void)
 {
   INITTYPE(keysz, root);
@@ -1428,6 +1467,7 @@ void algorithms_pyinit(void)
   INITTYPE(gmhash, ghash);
   INITTYPE(gcprp, type);
   INITTYPE(gprp, root);
+  addmethods(methods);
 }
 
 GEN(gcciphers, cipher)