chiark / gitweb /
Merge branch '1.3.x'
[catacomb-python] / group.c
1 /* -*-c-*-
2  *
3  * Abstract group inteface
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the Python interface to Catacomb.
11  *
12  * Catacomb/Python is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * Catacomb/Python is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Catacomb/Python; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "catacomb-python.h"
30
31 /*----- DH and binary group infos -----------------------------------------*/
32
33 PyObject *fginfo_pywrap(gprime_param *dp, PyTypeObject *ty)
34 {
35   fginfo_pyobj *z = PyObject_New(fginfo_pyobj, ty);
36   z->dp = *dp;
37   return ((PyObject *)z);
38 }
39
40 static PyObject *fginfo_pynew(PyTypeObject *ty,
41                               PyObject *arg, PyObject *kw)
42 {
43   static const char *const kwlist[] = { "p", "r", "g", 0 };
44   gprime_param dp = { 0 };
45   fginfo_pyobj *z = 0;
46
47   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&O&:new", KWLIST,
48                                    convmp, &dp.p,
49                                    convmp, &dp.q,
50                                    convmp, &dp.g))
51     goto end;
52   z = PyObject_New(fginfo_pyobj, ty);
53   z->dp = dp;
54   return ((PyObject *)z);
55 end:
56   mp_drop(dp.p);
57   mp_drop(dp.q);
58   mp_drop(dp.g);
59   return (0);
60 }
61
62 static PyObject *figet_r(PyObject *me, void *hunoz)
63   { return mp_pywrap(MP_COPY(FGINFO_DP(me)->q)); }
64
65 static PyObject *diget_p(PyObject *me, void *hunoz)
66   { return mp_pywrap(MP_COPY(FGINFO_DP(me)->p)); }
67
68 static PyObject *diget_g(PyObject *me, void *hunoz)
69   { return mp_pywrap(MP_COPY(FGINFO_DP(me)->g)); }
70
71 static PyObject *biget_p(PyObject *me, void *hunoz)
72   { return gf_pywrap(MP_COPY(FGINFO_DP(me)->p)); }
73
74 static PyObject *biget_m(PyObject *me, void *hunoz)
75   { return PyInt_FromLong(mp_octets(FGINFO_DP(me)->p) - 1); }
76
77 static PyObject *biget_g(PyObject *me, void *hunoz)
78   { return gf_pywrap(MP_COPY(FGINFO_DP(me)->g)); }
79
80 static void fginfo_pydealloc(PyObject *me)
81 {
82   mp_drop(FGINFO_DP(me)->p);
83   mp_drop(FGINFO_DP(me)->q);
84   mp_drop(FGINFO_DP(me)->g);
85   FREEOBJ(me);
86 }
87
88 static PyObject *meth__DHInfo_generate(PyObject *me,
89                                        PyObject *arg, PyObject *kw)
90 {
91   dh_param dp;
92   unsigned ql = 0, pl;
93   unsigned steps = 0;
94   grand *r = &rand_global;
95   struct excinfo exc = EXCINFO_INIT;
96   pypgev evt = { { 0 } };
97   static const char *const kwlist[] =
98     { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
99   PyObject *rc = 0;
100
101   evt.exc = &exc;
102   if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", KWLIST,
103                                    &me, convuint, &pl, convuint, &ql,
104                                    convpgev, &evt, convgrand, &r,
105                                    convuint, &steps))
106     goto end;
107   if (dh_gen(&dp, ql, pl, steps, r, evt.ev.proc, evt.ev.ctx))
108     PGENERR(&exc);
109   rc = fginfo_pywrap(&dp, dhinfo_pytype);
110 end:
111   droppgev(&evt);
112   return (rc);
113 }
114
115 static PyObject *meth__DHInfo_genlimlee(PyObject *me,
116                                         PyObject *arg, PyObject *kw)
117 {
118   dh_param dp;
119   unsigned ql, pl;
120   unsigned steps = 0;
121   grand *r = &rand_global;
122   struct excinfo exc = EXCINFO_INIT;
123   pypgev oe = { { 0 } }, ie = { { 0 } };
124   int subgroupp = 1;
125   unsigned f = 0;
126   static const char *const kwlist[] = {
127     "class", "pbits", "qbits", "event", "ievent",
128     "rng", "nsteps", "subgroupp", 0
129   };
130   size_t i, nf;
131   mp **v = 0;
132   PyObject *rc = 0, *vec = 0;
133
134   oe.exc = ie.exc = &exc;
135   if (!PyArg_ParseTupleAndKeywords(arg, kw,
136                                    "OO&O&|O&O&O&O&O&:genlimlee", KWLIST,
137                                    &me, convuint, &pl, convuint, &ql,
138                                    convpgev, &oe, convpgev, &ie,
139                                    convgrand, &r, convuint, &steps,
140                                    convbool, &subgroupp))
141     goto end;
142   if (subgroupp) f |= DH_SUBGROUP;
143   if (dh_limlee(&dp, ql, pl, f, steps, r,
144                 oe.ev.proc, oe.ev.ctx, ie.ev.proc, ie.ev.ctx, &nf, &v))
145     PGENERR(&exc);
146   vec = PyList_New(nf);
147   for (i = 0; i < nf; i++)
148     PyList_SetItem(vec, i, mp_pywrap(v[i]));
149   xfree(v);
150   rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype), vec);
151 end:
152   droppgev(&oe); droppgev(&ie);
153   return (rc);
154 }
155
156 static PyObject *meth__DHInfo_genkcdsa(PyObject *me,
157                                        PyObject *arg, PyObject *kw)
158 {
159   dh_param dp;
160   unsigned ql, pl;
161   unsigned steps = 0;
162   grand *r = &rand_global;
163   struct excinfo exc = EXCINFO_INIT;
164   pypgev evt = { { 0 } };
165   static const char *const kwlist[] =
166     { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
167   mp *v = MP_NEW;
168   PyObject *rc = 0;
169
170   evt.exc = &exc;
171   if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|O&O&O&:genkcdsa", KWLIST,
172                                    &me, convuint, &pl, convuint, &ql,
173                                    convpgev, &evt, convgrand, &r,
174                                    convuint, &steps))
175     goto end;
176   if (dh_kcdsagen(&dp, ql, pl, 0, steps, r, evt.ev.proc, evt.ev.ctx))
177     PGENERR(&exc);
178   mp_div(&v, 0, dp.p, dp.q);
179   v = mp_lsr(v, v, 1);
180   rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype),
181                      mp_pywrap(v));
182 end:
183   droppgev(&evt);
184   return (rc);
185 }
186
187 static PyObject *meth__DHInfo_gendsa(PyObject *me,
188                                      PyObject *arg, PyObject *kw)
189 {
190   dsa_param dp;
191   unsigned ql, pl;
192   unsigned steps = 0;
193   dsa_seed ds;
194   char *k;
195   Py_ssize_t ksz;
196   struct excinfo exc = EXCINFO_INIT;
197   pypgev evt = { { 0 } };
198   static const char *const kwlist[] =
199     { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 };
200   PyObject *rc = 0;
201
202   evt.exc = &exc;
203   if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&s#|O&O&:gendsa", KWLIST,
204                                    &me, convuint, &pl, convuint, &ql,
205                                    &k, &ksz, convpgev, &evt,
206                                    convuint, &steps))
207     goto end;
208   if (dsa_gen(&dp, ql, pl, steps, k, ksz, &ds, evt.ev.proc, evt.ev.ctx))
209     PGENERR(&exc);
210   rc = Py_BuildValue("(NNl)", fginfo_pywrap(&dp, dhinfo_pytype),
211                      bytestring_pywrap(ds.p, ds.sz), (long)ds.count);
212   xfree(ds.p);
213 end:
214   droppgev(&evt);
215   return (rc);
216 }
217
218 static int npgroups = -1, nbingroups = -1;
219
220 static PyObject *namedgroups(const pentry *pp, int *ne)
221 {
222   int i, j;
223   const char *p;
224   PyObject *d, *c;
225
226   d = PyDict_New();
227   for (i = 0; pp[i].name; i++) {
228     p = pp[i].name;
229     for (j = 0; j < i; j++) {
230       if (pp[i].data == pp[j].data) {
231         c = PyDict_GetItemString(d, (/*unconst*/ char *)pp[j].name);
232         Py_INCREF(c);
233         goto found;
234       }
235     }
236     c = PyInt_FromLong(i);
237   found:
238     PyDict_SetItemString(d, (/*unconst*/ char *)p, c);
239     Py_DECREF(c);
240   }
241   *ne = i;
242   return (d);
243 }
244
245 static PyObject *meth__groupn(PyObject *me, PyObject *arg,
246                               PyTypeObject *ty, const pentry *pp, int ne)
247 {
248   int i;
249   gprime_param gp;
250   PyObject *rc = 0;
251
252   if (!PyArg_ParseTuple(arg, "Oi:_groupn", &me, &i)) goto end;
253   if (i < 0 || i >= ne) VALERR("group index out of range");
254   dh_infofromdata(&gp, pp[i].data);
255   rc = fginfo_pywrap(&gp, ty);
256 end:
257   return (rc);
258 }
259
260 static PyObject *meth__DHInfo__groupn(PyObject *me, PyObject *arg)
261   { return (meth__groupn(me, arg, dhinfo_pytype, ptab, npgroups)); }
262
263 static PyObject *meth__BinDHInfo__groupn(PyObject *me, PyObject *arg)
264   { return (meth__groupn(me, arg, bindhinfo_pytype, bintab, nbingroups)); }
265
266 static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty,
267                              int (*parse)(qd_parse *, gprime_param *))
268 {
269   qd_parse qd;
270   char *p;
271   gprime_param gp;
272   PyObject *rc = 0;
273
274   if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
275   qd.p = p;
276   qd.e = 0;
277   if (parse(&qd, &gp)) VALERR(qd.e);
278   rc = fginfo_pywrap(&gp, ty);
279 end:
280   return (rc);
281 }
282
283 static PyObject *meth__DHInfo_parse(PyObject *me, PyObject *arg)
284   { return (meth__parse(me, arg, dhinfo_pytype, dh_parse)); }
285
286 static PyObject *meth__BinDHInfo_parse(PyObject *me, PyObject *arg)
287   { return (meth__parse(me, arg, bindhinfo_pytype, dhbin_parse)); }
288
289 static PyGetSetDef fginfo_pygetset[] = {
290 #define GETSETNAME(op, name) fi##op##_##name
291   GET   (r,                     "I.r -> group order")
292 #undef GETSETNAME
293   { 0 }
294 };
295
296 static PyGetSetDef dhinfo_pygetset[] = {
297 #define GETSETNAME(op, name) di##op##_##name
298   GET   (p,                     "I.p -> prime")
299   GET   (g,                     "I.g -> generator")
300 #undef GETSETNAME
301   { 0 }
302 };
303
304 static PyGetSetDef bindhinfo_pygetset[] = {
305 #define GETSETNAME(op, name) bi##op##_##name
306   GET   (p,                     "I.p -> irreducible polynomial")
307   GET   (m,                     "I.m -> degree of polynomial")
308   GET   (g,                     "I.g -> generator")
309 #undef GETSETNAME
310   { 0 }
311 };
312
313 static PyTypeObject fginfo_pytype_skel = {
314   PyObject_HEAD_INIT(0) 0,              /* Header */
315   "FGInfo",                             /* @tp_name@ */
316   sizeof(fginfo_pyobj),                 /* @tp_basicsize@ */
317   0,                                    /* @tp_itemsize@ */
318
319   fginfo_pydealloc,                     /* @tp_dealloc@ */
320   0,                                    /* @tp_print@ */
321   0,                                    /* @tp_getattr@ */
322   0,                                    /* @tp_setattr@ */
323   0,                                    /* @tp_compare@ */
324   0,                                    /* @tp_repr@ */
325   0,                                    /* @tp_as_number@ */
326   0,                                    /* @tp_as_sequence@ */
327   0,                                    /* @tp_as_mapping@ */
328   0,                                    /* @tp_hash@ */
329   0,                                    /* @tp_call@ */
330   0,                                    /* @tp_str@ */
331   0,                                    /* @tp_getattro@ */
332   0,                                    /* @tp_setattro@ */
333   0,                                    /* @tp_as_buffer@ */
334   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
335     Py_TPFLAGS_BASETYPE,
336
337   /* @tp_doc@ */
338 "Abstract base class for field-group information objects.",
339
340   0,                                    /* @tp_traverse@ */
341   0,                                    /* @tp_clear@ */
342   0,                                    /* @tp_richcompare@ */
343   0,                                    /* @tp_weaklistoffset@ */
344   0,                                    /* @tp_iter@ */
345   0,                                    /* @tp_iternext@ */
346   0,                                    /* @tp_methods@ */
347   0,                                    /* @tp_members@ */
348   fginfo_pygetset,                      /* @tp_getset@ */
349   0,                                    /* @tp_base@ */
350   0,                                    /* @tp_dict@ */
351   0,                                    /* @tp_descr_get@ */
352   0,                                    /* @tp_descr_set@ */
353   0,                                    /* @tp_dictoffset@ */
354   0,                                    /* @tp_init@ */
355   PyType_GenericAlloc,                  /* @tp_alloc@ */
356   abstract_pynew,                       /* @tp_new@ */
357   0,                                    /* @tp_free@ */
358   0                                     /* @tp_is_gc@ */
359 };
360
361 static PyTypeObject dhinfo_pytype_skel = {
362   PyObject_HEAD_INIT(0) 0,              /* Header */
363   "DHInfo",                             /* @tp_name@ */
364   sizeof(fginfo_pyobj),                 /* @tp_basicsize@ */
365   0,                                    /* @tp_itemsize@ */
366
367   fginfo_pydealloc,                     /* @tp_dealloc@ */
368   0,                                    /* @tp_print@ */
369   0,                                    /* @tp_getattr@ */
370   0,                                    /* @tp_setattr@ */
371   0,                                    /* @tp_compare@ */
372   0,                                    /* @tp_repr@ */
373   0,                                    /* @tp_as_number@ */
374   0,                                    /* @tp_as_sequence@ */
375   0,                                    /* @tp_as_mapping@ */
376   0,                                    /* @tp_hash@ */
377   0,                                    /* @tp_call@ */
378   0,                                    /* @tp_str@ */
379   0,                                    /* @tp_getattro@ */
380   0,                                    /* @tp_setattro@ */
381   0,                                    /* @tp_as_buffer@ */
382   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
383     Py_TPFLAGS_BASETYPE,
384
385   /* @tp_doc@ */
386 "DHInfo(P, R, G): standard (integer) Diffie-Hellman group information.",
387
388   0,                                    /* @tp_traverse@ */
389   0,                                    /* @tp_clear@ */
390   0,                                    /* @tp_richcompare@ */
391   0,                                    /* @tp_weaklistoffset@ */
392   0,                                    /* @tp_iter@ */
393   0,                                    /* @tp_iternext@ */
394   0,                                    /* @tp_methods@ */
395   0,                                    /* @tp_members@ */
396   dhinfo_pygetset,                      /* @tp_getset@ */
397   0,                                    /* @tp_base@ */
398   0,                                    /* @tp_dict@ */
399   0,                                    /* @tp_descr_get@ */
400   0,                                    /* @tp_descr_set@ */
401   0,                                    /* @tp_dictoffset@ */
402   0,                                    /* @tp_init@ */
403   PyType_GenericAlloc,                  /* @tp_alloc@ */
404   fginfo_pynew,                         /* @tp_new@ */
405   0,                                    /* @tp_free@ */
406   0                                     /* @tp_is_gc@ */
407 };
408
409 static PyTypeObject bindhinfo_pytype_skel = {
410   PyObject_HEAD_INIT(0) 0,              /* Header */
411   "BinDHInfo",                          /* @tp_name@ */
412   sizeof(fginfo_pyobj),                 /* @tp_basicsize@ */
413   0,                                    /* @tp_itemsize@ */
414
415   fginfo_pydealloc,                     /* @tp_dealloc@ */
416   0,                                    /* @tp_print@ */
417   0,                                    /* @tp_getattr@ */
418   0,                                    /* @tp_setattr@ */
419   0,                                    /* @tp_compare@ */
420   0,                                    /* @tp_repr@ */
421   0,                                    /* @tp_as_number@ */
422   0,                                    /* @tp_as_sequence@ */
423   0,                                    /* @tp_as_mapping@ */
424   0,                                    /* @tp_hash@ */
425   0,                                    /* @tp_call@ */
426   0,                                    /* @tp_str@ */
427   0,                                    /* @tp_getattro@ */
428   0,                                    /* @tp_setattro@ */
429   0,                                    /* @tp_as_buffer@ */
430   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
431     Py_TPFLAGS_BASETYPE,
432
433   /* @tp_doc@ */
434 "BinDHInfo(P, R, G): binary-field Diffie-Hellman group information.",
435
436   0,                                    /* @tp_traverse@ */
437   0,                                    /* @tp_clear@ */
438   0,                                    /* @tp_richcompare@ */
439   0,                                    /* @tp_weaklistoffset@ */
440   0,                                    /* @tp_iter@ */
441   0,                                    /* @tp_iternext@ */
442   0,                                    /* @tp_methods@ */
443   0,                                    /* @tp_members@ */
444   bindhinfo_pygetset,                   /* @tp_getset@ */
445   0,                                    /* @tp_base@ */
446   0,                                    /* @tp_dict@ */
447   0,                                    /* @tp_descr_get@ */
448   0,                                    /* @tp_descr_set@ */
449   0,                                    /* @tp_dictoffset@ */
450   0,                                    /* @tp_init@ */
451   PyType_GenericAlloc,                  /* @tp_alloc@ */
452   fginfo_pynew,                         /* @tp_new@ */
453   0,                                    /* @tp_free@ */
454   0                                     /* @tp_is_gc@ */
455 };
456
457 /*----- General utilities -------------------------------------------------*/
458
459 PyTypeObject *ge_pytype, *group_pytype;
460 PyTypeObject *primegroup_pytype, *bingroup_pytype, *ecgroup_pytype;
461
462 group *group_copy(group *g)
463 {
464   if (STRCMP(G_NAME(g), ==, "prime")) {
465     gctx_prime *gc = (gctx_prime *)g;
466     gprime_param gp;
467     gp.g = G_TOINT(g, MP_NEW, g->g);
468     gp.p = gc->mm.m;
469     gp.q = gc->g.r;
470     g = group_prime(&gp);
471     MP_DROP(gp.g);
472   } else if (STRCMP(G_NAME(g), ==, "bin")) {
473     gctx_bin *gc = (gctx_bin *)g;
474     gbin_param gb;
475     gb.g = G_TOINT(g, MP_NEW, g->g);
476     gb.p = gc->r.p;
477     gb.q = gc->g.r;
478     g = group_binary(&gb);
479     MP_DROP(gb.g);
480   } else if (STRCMP(G_NAME(g), ==, "ec")) {
481     gctx_ec *gc = (gctx_ec *)g;
482     ec_info ei;
483     if ((ei.c = eccurve_copy(gc->ei.c)) == 0)
484       return (0);
485     EC_CREATE(&ei.g);
486     EC_COPY(&ei.g, &gc->ei.g);
487     ei.r = MP_COPY(gc->ei.r);
488     ei.h = MP_COPY(gc->ei.h);
489     g = group_ec(&ei);
490   } else
491     g = 0;
492   return (g);
493 }
494
495 PyObject *ge_pywrap(PyObject *gobj, ge *x)
496 {
497   ge_pyobj *z = PyObject_New(ge_pyobj, (PyTypeObject *)gobj);
498   z->x = x;
499   z->g = GROUP_G(gobj);
500   Py_INCREF(gobj);
501   return ((PyObject *)z);
502 }
503
504 static PyObject *ge_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
505 {
506   static const char *const kwlist[] = { "x", 0 };
507   PyObject *x;
508   group *g;
509   ec p = EC_INIT;
510   mp *y = 0;
511   ge *xx = 0;
512   mptext_stringctx sc;
513
514   g = GROUP_G(ty);
515   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", KWLIST, &x)) goto end;
516   xx = G_CREATE(g);
517   if (ECPT_PYCHECK(x)) {
518     getecptout(&p, x);
519     if (G_FROMEC(g, xx, &p))
520       TYERR("can't convert from elliptic curve point");
521     EC_DESTROY(&p);
522   } else if ((y = tomp(x)) != 0) {
523     if (G_FROMINT(g, xx, y))
524       TYERR("can't convert from integer");
525     MP_DROP(y);
526   } else if (PyString_Check(x)) {
527     sc.buf = PyString_AS_STRING(x);
528     sc.lim = sc.buf + PyString_GET_SIZE(x);
529     if (G_READ(g, xx, &mptext_stringops, &sc) || sc.buf < sc.lim)
530       VALERR("malformed group element string");
531   } else
532     TYERR("can't convert to group element");
533   return (ge_pywrap((PyObject *)ty, xx));
534 end:
535   mp_drop(y);
536   EC_DESTROY(&p);
537   if (xx) G_DESTROY(g, xx);
538   return (0);
539 }
540
541 static PyObject *group_dopywrap(PyTypeObject *ty, group *g)
542 {
543   group_pyobj *gobj = newtype(ty, 0, g->ops->name);
544   gobj->g = g;
545   gobj->ty.ht_type.tp_basicsize = sizeof(ge_pyobj);
546   gobj->ty.ht_type.tp_base = ge_pytype;
547   Py_INCREF(group_pytype);
548   gobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
549                                Py_TPFLAGS_BASETYPE |
550                                Py_TPFLAGS_CHECKTYPES |
551                                Py_TPFLAGS_HEAPTYPE);
552   gobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
553   gobj->ty.ht_type.tp_free = 0;
554   gobj->ty.ht_type.tp_new = ge_pynew;
555   typeready(&gobj->ty.ht_type);
556   return ((PyObject *)gobj);
557 }
558
559 PyObject *group_pywrap(group *g)
560 {
561   PyTypeObject *ty;
562
563   if (STRCMP(G_NAME(g), ==, "prime")) ty = primegroup_pytype;
564   else if (STRCMP(G_NAME(g), ==, "bin")) ty = bingroup_pytype;
565   else if (STRCMP(G_NAME(g), ==, "ec")) ty = ecgroup_pytype;
566   else abort();
567   return (group_dopywrap(ty, g));
568 }
569
570 /*----- Group elements ----------------------------------------------------*/
571
572 #define BINOP(name)                                                     \
573   static PyObject *ge_py##name(PyObject *x, PyObject *y)                \
574   {                                                                     \
575     ge *z;                                                              \
576     group *g;                                                           \
577     if (!GE_PYCHECK(x) || !GE_PYCHECK(y) ||                             \
578         (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y))))         \
579       RETURN_NOTIMPL;                                                   \
580     g = GE_G(x);                                                        \
581     z = G_CREATE(g);                                                    \
582     g->ops->name(g, z, GE_X(x), GE_X(y));                               \
583     return (ge_pywrap(GE_GOBJ(x), z));                                  \
584   }
585 BINOP(mul)
586 BINOP(div)
587 #undef BINOP
588
589 #define UNOP(name)                                                      \
590   static PyObject *gemeth_##name(PyObject *me, PyObject *arg)           \
591   {                                                                     \
592     group *g;                                                           \
593     ge *z;                                                              \
594     if (!PyArg_ParseTuple(arg, ":" #name)) return (0);                  \
595     g = GE_G(me);                                                       \
596     z = G_CREATE(g);                                                    \
597     g->ops->name(g, z, GE_X(me));                                       \
598     return (ge_pywrap(GE_GOBJ(me), z));                                 \
599   }
600 UNOP(sqr)
601 UNOP(inv)
602 #undef UNOP
603
604 static PyObject *ge_pyexp(PyObject *x, PyObject *n, PyObject *m)
605 {
606   mp *nn;
607   ge *z;
608
609   if (m != Py_None || !GE_PYCHECK(x) || (nn = getmp(n)) == 0)
610     RETURN_NOTIMPL;
611   z = G_CREATE(GE_G(x));
612   G_EXP(GE_G(x), z, GE_X(x), nn);
613   MP_DROP(nn);
614   return (ge_pywrap(GE_GOBJ(x), z));
615 }
616
617 static void ge_pydealloc(PyObject *me)
618 {
619   G_DESTROY(GE_G(me), GE_X(me));
620   Py_DECREF(GE_GOBJ(me));
621   FREEOBJ(me);
622 }
623
624 static void group_pydealloc(PyObject *me)
625 {
626   G_DESTROYGROUP(GROUP_G(me));
627   PyType_Type.tp_dealloc(me);
628 }
629
630 static PyObject *gmexp_id(PyObject *me)
631 {
632   group *g = GROUP_G(me); ge *x = G_CREATE(g);
633   G_COPY(g, x, g->i); return (ge_pywrap(me, x));
634 }
635
636 static int gmexp_fill(void *pp, PyObject  *me, PyObject *x, PyObject *m)
637 {
638   group_expfactor *f = pp;
639
640   if (!GE_PYCHECK(x) || GE_G(x) != GROUP_G(me) || (f->exp = getmp(m)) == 0)
641     return (-1);
642   f->base = GE_X(x);
643   return (0);
644 }
645
646 static PyObject *ge_pyrichcompare(PyObject *x, PyObject *y, int op)
647 {
648   int b;
649   PyObject *rc = 0;
650
651   if (!GE_PYCHECK(x) || !GE_PYCHECK(y) ||
652       (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y))))
653     RETURN_NOTIMPL;
654   switch (op) {
655     case Py_EQ: b = G_EQ(GE_G(x), GE_X(x), GE_X(y)); break;
656     case Py_NE: b = !G_EQ(GE_G(x), GE_X(x), GE_X(y)); break;
657     default: TYERR("group elements are unordered");
658   }
659   rc = getbool(b);
660 end:
661   return (rc);
662 }
663
664 static PyObject *gemeth_check(PyObject *me, PyObject *arg)
665 {
666   if (!PyArg_ParseTuple(arg, ":check")) goto end;
667   if (group_check(GE_G(me), GE_X(me))) VALERR("bad group element");
668   RETURN_OBJ(me);
669 end:
670   return (0);
671 }
672
673 static int ge_pynonzerop(PyObject *x)
674   { return (!G_IDENTP(GE_G(x), GE_X(x))); }
675
676 static PyObject *ge_pystr(PyObject *me)
677 {
678   dstr d = DSTR_INIT;
679   PyObject *rc;
680
681   group_writedstr(GE_G(me), GE_X(me), &d);
682   rc = PyString_FromStringAndSize(d.buf, d.len);
683   DDESTROY(&d);
684   return (rc);
685 }
686
687 static PyObject *ge_pylong(PyObject *me)
688 {
689   mp *x = 0;
690   PyObject *rc = 0;
691
692   if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
693     TYERR("can't convert to integer");
694   rc = mp_topylong(x);
695 end:
696   mp_drop(x);
697   return (rc);
698 }
699
700 static PyObject *ge_pyint(PyObject *me)
701 {
702   mp *x = 0;
703   PyObject *rc = 0;
704   long l;
705
706   if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
707     TYERR("can't convert to integer");
708   if (!mp_tolong_checked(x, &l, 0)) rc = PyInt_FromLong(l);
709   else rc = mp_topylong(x);
710 end:
711   mp_drop(x);
712   return (rc);
713 }
714
715 static PyObject *gemeth_toint(PyObject *me, PyObject *arg)
716 {
717   mp *x;
718
719   if (!PyArg_ParseTuple(arg, ":toint")) goto end;
720   if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
721     TYERR("can't convert to integer");
722   return (mp_pywrap(x));
723 end:
724   return (0);
725 }
726
727 static PyObject *gemeth_toec(PyObject *me, PyObject *arg, PyObject *kw)
728 {
729   static const char *const kwlist[] = { "curve", 0 };
730   PyTypeObject *cty = 0;
731   PyObject *rc = 0;
732   group *g;
733   ec_curve *c;
734   ec p = EC_INIT;
735
736   if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:toec", KWLIST,
737                                    &cty)) goto end;
738   g = GROUP_G(GE_GOBJ(me));
739   if (cty) {
740     if (!PyType_Check(cty) || !PyType_IsSubtype(cty, ecpt_pytype))
741       TYERR("want subtype of catacomb.ECPt");
742     Py_INCREF((PyObject *)cty);
743   } else if (STRCMP(G_NAME(g), ==, "ec")) {
744     c = eccurve_copy(((gctx_ec *)g)->ei.c);
745     cty = (PyTypeObject *)eccurve_pywrap(0, c);
746   } else  {
747     cty = ecpt_pytype;
748     Py_INCREF((PyObject *)cty);
749   }
750   if (G_TOEC(GE_G(me), &p, GE_X(me))) {
751     Py_DECREF((PyObject *)cty);
752     TYERR("can't convert to ec point");
753   }
754   rc = ecpt_pywrapout(cty, &p);
755   Py_DECREF((PyObject *)cty);
756 end:
757   return (rc);
758 }
759
760 static PyObject *gemeth_tobuf(PyObject *me, PyObject *arg)
761 {
762   buf b;
763   PyObject *rc;
764   size_t n;
765
766   if (!PyArg_ParseTuple(arg, ":tobuf")) return (0);
767   n = GE_G(me)->noctets + 4;
768   rc = bytestring_pywrap(0, n);
769   buf_init(&b, PyString_AS_STRING(rc), n);
770   G_TOBUF(GE_G(me), &b, GE_X(me));
771   assert(BOK(&b));
772   _PyString_Resize(&rc, BLEN(&b));
773   return (rc);
774 }
775
776 static PyObject *gemeth_toraw(PyObject *me, PyObject *arg)
777 {
778   buf b;
779   PyObject *rc;
780   size_t n;
781
782   if (!PyArg_ParseTuple(arg, ":toraw")) return (0);
783   n = GE_G(me)->noctets;
784   rc = bytestring_pywrap(0, n);
785   buf_init(&b, PyString_AS_STRING(rc), n);
786   G_TORAW(GE_G(me), &b, GE_X(me));
787   assert(BOK(&b));
788   _PyString_Resize(&rc, BLEN(&b));
789   return (rc);
790 }
791
792 static PyObject *gmexp_exp(PyObject *me, void *pp, int n)
793 {
794   ge *z = G_CREATE(GROUP_G(me));
795   G_MEXP(GROUP_G(me), z, pp, n);
796   return (ge_pywrap(me, z));
797 }
798
799 static void gmexp_drop(void *pp)
800 {
801   group_expfactor *f = pp;
802   MP_DROP(f->exp);
803 }
804
805 static PyObject *gmeth_mexp(PyObject *me, PyObject *arg)
806 {
807   return (mexp_common(me, arg, sizeof(group_expfactor),
808                       gmexp_id, gmexp_fill, gmexp_exp, gmexp_drop));
809 }
810
811 static PyObject *gmeth_checkgroup(PyObject *me, PyObject *arg, PyObject *kw)
812 {
813   static const char *const kwlist[] = { "rng", 0 };
814   grand *r = &rand_global;
815   const char *p;
816
817   if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:checkgroup", KWLIST,
818                                    convgrand, &r))
819     goto end;
820   if ((p = G_CHECK(GROUP_G(me), r)) != 0)
821     VALERR(p);
822   RETURN_OBJ(me);
823 end:
824   return (0);
825 }
826
827 static PyObject *group_pyrichcompare(PyObject *x, PyObject *y, int op)
828 {
829   int b = group_samep(GROUP_G(x), GROUP_G(y));
830   switch (op) {
831     case Py_EQ: break;
832     case Py_NE: b = !b;
833     default: TYERR("can't order groups");
834   }
835   return (getbool(b));
836 end:
837   return (0);
838 }
839
840 static PyObject *meth__GE_frombuf(PyObject *me, PyObject *arg)
841 {
842   buf b;
843   char *p;
844   Py_ssize_t n;
845   group *g;
846   ge *x = 0;
847
848   if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &n))
849     return (0);
850   g = GROUP_G(me);
851   buf_init(&b, p, n);
852   x = G_CREATE(g);
853   if (G_FROMBUF(g, &b, x))
854     VALERR("invalid data");
855   return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
856 end:
857   if (x) G_DESTROY(g, x);
858   return (0);
859 }
860
861 static PyObject *meth__GE_fromraw(PyObject *me, PyObject *arg)
862 {
863   buf b;
864   char *p;
865   Py_ssize_t n;
866   group *g;
867   ge *x = 0;
868
869   if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &n))
870     return (0);
871   g = GROUP_G(me);
872   buf_init(&b, p, n);
873   x = G_CREATE(g);
874   if (G_FROMRAW(g, &b, x))
875     VALERR("invalid data");
876   return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
877 end:
878   if (x) G_DESTROY(g, x);
879   return (0);
880 }
881
882 static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg)
883 {
884   mptext_stringctx sc;
885   char *p;
886   Py_ssize_t n;
887   group *g;
888   ge *x = 0;
889
890   if (!PyArg_ParseTuple(arg, "Os#:fromstring", &me, &p, &n))
891     return (0);
892   sc.buf = p;
893   sc.lim = sc.buf + n;
894   g = GROUP_G(me);
895   x = G_CREATE(g);
896   if (G_READ(g, x, &mptext_stringops, &sc))
897     VALERR("bad group element string");
898   return (Py_BuildValue("(Ns#)", ge_pywrap(me, x),
899                         sc.buf, (Py_ssize_t)(sc.lim - sc.buf)));
900 end:
901   if (x) G_DESTROY(g, x);
902   return (0);
903 }
904
905 static PyObject *meth__Group_parse(PyObject *me, PyObject *arg)
906 {
907   char *p;
908   qd_parse qd;
909   group *g;
910
911   if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
912     goto end;
913   qd.p = p;
914   qd.e = 0;
915   if ((g = group_parse(&qd)) == 0)
916     VALERR(qd.e);
917   return (group_pywrap(g));
918 end:
919   return (0);
920 }
921
922 static PyObject *geget_group(PyObject *me, void *hunoz)
923   { RETURN_OBJ(GE_GOBJ(me)); }
924
925 static PyObject *gget_nbits(PyObject *me, void *hunoz)
926   { return (PyInt_FromLong(GROUP_G(me)->nbits)); }
927
928 static PyObject *gget_noctets(PyObject *me, void *hunoz)
929   { return (PyInt_FromLong(GROUP_G(me)->noctets)); }
930
931 static PyObject *gget_i(PyObject *me, void *hunoz)
932 {
933   group *g = GROUP_G(me); ge *x = G_CREATE(g);
934   G_COPY(g, x, g->i); return (ge_pywrap(me, x));
935 }
936
937 static PyObject *gget_g(PyObject *me, void *hunoz)
938 {
939   group *g = GROUP_G(me); ge *x = G_CREATE(g);
940   G_COPY(g, x, g->g); return (ge_pywrap(me, x));
941 }
942
943 static long ge_pyhash(PyObject *me)
944 {
945   buf b;
946   size_t sz = GE_G(me)->noctets + 4;
947   uint32 h = 0xf672c776 + GE_G(me)->ops->ty;
948   octet *p = xmalloc(sz);
949   buf_init(&b, p, sz);
950   G_TOBUF(GE_G(me), &b, GE_X(me));
951   assert(BOK(&b));
952   h = unihash_hash(&unihash_global, h, BBASE(&b), BLEN(&b));
953   xfree(p);
954   return (h % LONG_MAX);
955 }
956
957 static PyObject *gget_r(PyObject *me, void *hunoz)
958   { return (mp_pywrap(MP_COPY(GROUP_G(me)->r))); }
959
960 static PyObject *gget_h(PyObject *me, void *hunoz)
961   { return (mp_pywrap(MP_COPY(GROUP_G(me)->h))); }
962
963 static PyGetSetDef ge_pygetset[] = {
964 #define GETSETNAME(op, name) ge##op##_##name
965   GET   (group,         "X.group -> group containing X")
966 #undef GETSETNAME
967   { 0 }
968 };
969
970 static PyMethodDef ge_pymethods[] = {
971 #define METHNAME(name) gemeth_##name
972   METH  (inv,           "X.inv() -> inverse element of X")
973   METH  (sqr,           "X.sqr() -> X^2 = X * X")
974   METH  (check,         "X.check() -> check X really belongs to its group")
975   METH  (toint,         "X.toint() -> X converted to an integer")
976   KWMETH(toec,          "\
977 X.toec([curve = ECPt]) -> X converted to elliptic curve point")
978   METH  (tobuf,         "X.tobuf() -> X in buffer representation")
979   METH  (toraw,         "X.toraw() -> X in raw representation")
980 #undef METHNAME
981   { 0 }
982 };
983
984 static PyNumberMethods ge_pynumber = {
985   0,                                    /* @nb_add@ */
986   0,                                    /* @nb_subtract@ */
987   ge_pymul,                             /* @nb_multiply@ */
988   ge_pydiv,                             /* @nb_divide@ */
989   0,                                    /* @nb_remainder@ */
990   0,                                    /* @nb_divmod@ */
991   ge_pyexp,                             /* @nb_power@ */
992   0,                                    /* @nb_negative@ */
993   0,                                    /* @nb_positive@ */
994   0,                                    /* @nb_absolute@ */
995   ge_pynonzerop,                        /* @nb_nonzero@ */
996   0,                                    /* @nb_invert@ */
997   0,                                    /* @nb_lshift@ */
998   0,                                    /* @nb_rshift@ */
999   0,                                    /* @nb_and@ */
1000   0,                                    /* @nb_xor@ */
1001   0,                                    /* @nb_or@ */
1002   0,                                    /* @nb_coerce@ */
1003   ge_pyint,                             /* @nb_int@ */
1004   ge_pylong,                            /* @nb_long@ */
1005   0 /* meaningless */,                  /* @nb_float@ */
1006   0,                                    /* @nb_oct@ */
1007   0,                                    /* @nb_hex@ */
1008
1009   0,                                    /* @nb_inplace_add@ */
1010   0,                                    /* @nb_inplace_subtract@ */
1011   0,                                    /* @nb_inplace_multiply@ */
1012   0,                                    /* @nb_inplace_divide@ */
1013   0,                                    /* @nb_inplace_remainder@ */
1014   0,                                    /* @nb_inplace_power@ */
1015   0,                                    /* @nb_inplace_lshift@ */
1016   0,                                    /* @nb_inplace_rshift@ */
1017   0,                                    /* @nb_inplace_and@ */
1018   0,                                    /* @nb_inplace_xor@ */
1019   0,                                    /* @nb_inplace_or@ */
1020
1021   0,                                    /* @nb_floor_divide@ */
1022   ge_pydiv,                             /* @nb_true_divide@ */
1023   0,                                    /* @nb_inplace_floor_divide@ */
1024   0,                                    /* @nb_inplace_true_divide@ */
1025 };
1026
1027 static PyTypeObject ge_pytype_skel = {
1028   PyObject_HEAD_INIT(0) 0,              /* Header */
1029   "GE",                                 /* @tp_name@ */
1030   sizeof(ge_pyobj),                     /* @tp_basicsize@ */
1031   0,                                    /* @tp_itemsize@ */
1032
1033   ge_pydealloc,                         /* @tp_dealloc@ */
1034   0,                                    /* @tp_print@ */
1035   0,                                    /* @tp_getattr@ */
1036   0,                                    /* @tp_setattr@ */
1037   0,                                    /* @tp_compare@ */
1038   0,                                    /* @tp_repr@ */
1039   &ge_pynumber,                         /* @tp_as_number@ */
1040   0,                                    /* @tp_as_sequence@ */
1041   0,                                    /* @tp_as_mapping@ */
1042   ge_pyhash,                            /* @tp_hash@ */
1043   0,                                    /* @tp_call@ */
1044   ge_pystr,                             /* @tp_str@ */
1045   0,                                    /* @tp_getattro@ */
1046   0,                                    /* @tp_setattro@ */
1047   0,                                    /* @tp_as_buffer@ */
1048   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
1049     Py_TPFLAGS_CHECKTYPES |
1050     Py_TPFLAGS_BASETYPE,
1051
1052   /* @tp_doc@ */
1053 "Group elements, abstract base class.",
1054
1055   0,                                    /* @tp_traverse@ */
1056   0,                                    /* @tp_clear@ */
1057   ge_pyrichcompare,                     /* @tp_richcompare@ */
1058   0,                                    /* @tp_weaklistoffset@ */
1059   0,                                    /* @tp_iter@ */
1060   0,                                    /* @tp_iternext@ */
1061   ge_pymethods,                         /* @tp_methods@ */
1062   0,                                    /* @tp_members@ */
1063   ge_pygetset,                          /* @tp_getset@ */
1064   0,                                    /* @tp_base@ */
1065   0,                                    /* @tp_dict@ */
1066   0,                                    /* @tp_descr_get@ */
1067   0,                                    /* @tp_descr_set@ */
1068   0,                                    /* @tp_dictoffset@ */
1069   0,                                    /* @tp_init@ */
1070   PyType_GenericAlloc,                  /* @tp_alloc@ */
1071   abstract_pynew,                       /* @tp_new@ */
1072   0,                                    /* @tp_free@ */
1073   0                                     /* @tp_is_gc@ */
1074 };
1075
1076 static PyGetSetDef group_pygetset[] = {
1077 #define GETSETNAME(op, name) g##op##_##name
1078   GET   (noctets,       "G.noctets -> size in octets of element")
1079   GET   (nbits,         "G.nbits -> size in bits of element")
1080   GET   (i,             "G.i -> group identity")
1081   GET   (g,             "G.g -> group generator")
1082   GET   (r,             "G.r -> group order")
1083   GET   (h,             "G.h -> group cofactor")
1084 #undef GETSETNAME
1085   { 0 }
1086 };
1087
1088 static PyMethodDef group_pymethods[] = {
1089 #define METHNAME(name) gmeth_##name
1090   METH  (mexp,          "\
1091 G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1092   KWMETH(checkgroup,    "G.checkgroup([rng = rand]): check group is good")
1093 #undef METHNAME
1094   { 0 }
1095 };
1096
1097 static PyTypeObject group_pytype_skel = {
1098   PyObject_HEAD_INIT(0) 0,              /* Header */
1099   "Group",                              /* @tp_name@ */
1100   sizeof(group_pyobj),                  /* @tp_basicsize@ */
1101   0,                                    /* @tp_itemsize@ */
1102
1103   group_pydealloc,                      /* @tp_dealloc@ */
1104   0,                                    /* @tp_print@ */
1105   0,                                    /* @tp_getattr@ */
1106   0,                                    /* @tp_setattr@ */
1107   0,                                    /* @tp_compare@ */
1108   0,                                    /* @tp_repr@ */
1109   0,                                    /* @tp_as_number@ */
1110   0,                                    /* @tp_as_sequence@ */
1111   0,                                    /* @tp_as_mapping@ */
1112   0,                                    /* @tp_hash@ */
1113   0,                                    /* @tp_call@ */
1114   0,                                    /* @tp_str@ */
1115   0,                                    /* @tp_getattro@ */
1116   0,                                    /* @tp_setattro@ */
1117   0,                                    /* @tp_as_buffer@ */
1118   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
1119     Py_TPFLAGS_BASETYPE,
1120
1121   /* @tp_doc@ */
1122 "Abstract base class for groups.",
1123
1124   0,                                    /* @tp_traverse@ */
1125   0,                                    /* @tp_clear@ */
1126   group_pyrichcompare,                  /* @tp_richcompare@ */
1127   0,                                    /* @tp_weaklistoffset@ */
1128   0,                                    /* @tp_iter@ */
1129   0,                                    /* @tp_iternext@ */
1130   group_pymethods,                      /* @tp_methods@ */
1131   0,                                    /* @tp_members@ */
1132   group_pygetset,                       /* @tp_getset@ */
1133   0,                                    /* @tp_base@ */
1134   0,                                    /* @tp_dict@ */
1135   0,                                    /* @tp_descr_get@ */
1136   0,                                    /* @tp_descr_set@ */
1137   0,                                    /* @tp_dictoffset@ */
1138   0,                                    /* @tp_init@ */
1139   PyType_GenericAlloc,                  /* @tp_alloc@ */
1140   abstract_pynew,                       /* @tp_new@ */
1141   0,                                    /* @tp_free@ */
1142   0                                     /* @tp_is_gc@ */
1143 };
1144
1145 static PyObject *pgget_info(PyObject *me, void *hunoz)
1146 {
1147   gprime_param dp;
1148   gctx_prime *gg = (gctx_prime *)GROUP_G(me);
1149   dp.p = MP_COPY(gg->mm.m);
1150   dp.q = MP_COPY(gg->g.r);
1151   dp.g = mpmont_reduce(&gg->mm, MP_NEW, gg->gen.x);
1152   return (fginfo_pywrap(&dp, dhinfo_pytype));
1153 }
1154
1155 static PyGetSetDef primegroup_pygetset[] = {
1156 #define GETSETNAME(op, name) pg##op##_##name
1157   GET   (info,          "G.info -> information about the group")
1158 #undef GETSETNAME
1159   { 0 }
1160 };
1161
1162 static PyObject *primegroup_pynew(PyTypeObject *ty,
1163                                   PyObject *arg, PyObject *kw)
1164 {
1165   PyObject *i;
1166   static const char *const kwlist[] = { "info", 0 };
1167
1168   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
1169                                    dhinfo_pytype, &i))
1170     return (0);
1171   return (group_dopywrap(ty, group_prime(FGINFO_DP(i))));
1172 }
1173
1174 static PyTypeObject primegroup_pytype_skel = {
1175   PyObject_HEAD_INIT(0) 0,              /* Header */
1176   "PrimeGroup",                         /* @tp_name@ */
1177   sizeof(group_pyobj),                  /* @tp_basicsize@ */
1178   0,                                    /* @tp_itemsize@ */
1179
1180   group_pydealloc,                      /* @tp_dealloc@ */
1181   0,                                    /* @tp_print@ */
1182   0,                                    /* @tp_getattr@ */
1183   0,                                    /* @tp_setattr@ */
1184   0,                                    /* @tp_compare@ */
1185   0,                                    /* @tp_repr@ */
1186   0,                                    /* @tp_as_number@ */
1187   0,                                    /* @tp_as_sequence@ */
1188   0,                                    /* @tp_as_mapping@ */
1189   0,                                    /* @tp_hash@ */
1190   0,                                    /* @tp_call@ */
1191   0,                                    /* @tp_str@ */
1192   0,                                    /* @tp_getattro@ */
1193   0,                                    /* @tp_setattro@ */
1194   0,                                    /* @tp_as_buffer@ */
1195   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
1196     Py_TPFLAGS_BASETYPE,
1197
1198   /* @tp_doc@ */
1199 "PrimeGroup(INFO): subgroups of prime fields.",
1200
1201   0,                                    /* @tp_traverse@ */
1202   0,                                    /* @tp_clear@ */
1203   0,                                    /* @tp_richcompare@ */
1204   0,                                    /* @tp_weaklistoffset@ */
1205   0,                                    /* @tp_iter@ */
1206   0,                                    /* @tp_iternext@ */
1207   0,                                    /* @tp_methods@ */
1208   0,                                    /* @tp_members@ */
1209   primegroup_pygetset,                  /* @tp_getset@ */
1210   0,                                    /* @tp_base@ */
1211   0,                                    /* @tp_dict@ */
1212   0,                                    /* @tp_descr_get@ */
1213   0,                                    /* @tp_descr_set@ */
1214   0,                                    /* @tp_dictoffset@ */
1215   0,                                    /* @tp_init@ */
1216   PyType_GenericAlloc,                  /* @tp_alloc@ */
1217   primegroup_pynew,                     /* @tp_new@ */
1218   0,                                    /* @tp_free@ */
1219   0                                     /* @tp_is_gc@ */
1220 };
1221
1222 static PyObject *bgget_info(PyObject *me, void *hunoz)
1223 {
1224   gbin_param dp;
1225   gctx_bin *gg = (gctx_bin *)GROUP_G(me);
1226   dp.p = MP_COPY(gg->r.p);
1227   dp.q = MP_COPY(gg->g.r);
1228   dp.g = MP_COPY(gg->gen.x);
1229   return (fginfo_pywrap(&dp, bindhinfo_pytype));
1230 }
1231
1232 static PyGetSetDef bingroup_pygetset[] = {
1233 #define GETSETNAME(op, name) bg##op##_##name
1234   GET   (info,          "G.info -> information about the group")
1235 #undef GETSETNAME
1236   { 0 }
1237 };
1238
1239 static PyObject *bingroup_pynew(PyTypeObject *ty,
1240                                 PyObject *arg, PyObject *kw)
1241 {
1242   PyObject *i;
1243   static const char *const kwlist[] = { "info", 0 };
1244
1245   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
1246                                    bindhinfo_pytype, &i))
1247     return (0);
1248   return (group_dopywrap(ty, group_binary(FGINFO_DP(i))));
1249 }
1250
1251 static PyTypeObject bingroup_pytype_skel = {
1252   PyObject_HEAD_INIT(0) 0,              /* Header */
1253   "BinGroup",                           /* @tp_name@ */
1254   sizeof(group_pyobj),                  /* @tp_basicsize@ */
1255   0,                                    /* @tp_itemsize@ */
1256
1257   group_pydealloc,                      /* @tp_dealloc@ */
1258   0,                                    /* @tp_print@ */
1259   0,                                    /* @tp_getattr@ */
1260   0,                                    /* @tp_setattr@ */
1261   0,                                    /* @tp_compare@ */
1262   0,                                    /* @tp_repr@ */
1263   0,                                    /* @tp_as_number@ */
1264   0,                                    /* @tp_as_sequence@ */
1265   0,                                    /* @tp_as_mapping@ */
1266   0,                                    /* @tp_hash@ */
1267   0,                                    /* @tp_call@ */
1268   0,                                    /* @tp_str@ */
1269   0,                                    /* @tp_getattro@ */
1270   0,                                    /* @tp_setattro@ */
1271   0,                                    /* @tp_as_buffer@ */
1272   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
1273     Py_TPFLAGS_BASETYPE,
1274
1275   /* @tp_doc@ */
1276 "BinGroup(INFO): subgroups of binary fields.",
1277
1278   0,                                    /* @tp_traverse@ */
1279   0,                                    /* @tp_clear@ */
1280   0,                                    /* @tp_richcompare@ */
1281   0,                                    /* @tp_weaklistoffset@ */
1282   0,                                    /* @tp_iter@ */
1283   0,                                    /* @tp_iternext@ */
1284   0,                                    /* @tp_methods@ */
1285   0,                                    /* @tp_members@ */
1286   bingroup_pygetset,                    /* @tp_getset@ */
1287   0,                                    /* @tp_base@ */
1288   0,                                    /* @tp_dict@ */
1289   0,                                    /* @tp_descr_get@ */
1290   0,                                    /* @tp_descr_set@ */
1291   0,                                    /* @tp_dictoffset@ */
1292   0,                                    /* @tp_init@ */
1293   PyType_GenericAlloc,                  /* @tp_alloc@ */
1294   bingroup_pynew,                       /* @tp_new@ */
1295   0,                                    /* @tp_free@ */
1296   0                                     /* @tp_is_gc@ */
1297 };
1298
1299 static PyObject *egget_info(PyObject *me, void *hunoz)
1300 {
1301   ec_info ei;
1302   gctx_ec *gg = (gctx_ec *)GROUP_G(me);
1303
1304   ecinfo_copy(&ei, &gg->ei);
1305   return (ecinfo_pywrap(&ei));
1306 }
1307
1308 static PyGetSetDef ecgroup_pygetset[] = {
1309 #define GETSETNAME(op, name) eg##op##_##name
1310   GET   (info,          "G.info -> information about the group")
1311 #undef GETSETNAME
1312   { 0 }
1313 };
1314
1315 static PyObject *ecgroup_pynew(PyTypeObject *ty,
1316                                PyObject *arg, PyObject *kw)
1317 {
1318   PyObject *i;
1319   ec_info ei;
1320   static const char *const kwlist[] = { "info", 0 };
1321
1322   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
1323                                    ecinfo_pytype, &i))
1324     return (0);
1325   ecinfo_copy(&ei, ECINFO_EI(i));
1326   return (group_dopywrap(ty, group_ec(&ei)));
1327 }
1328
1329 static PyTypeObject ecgroup_pytype_skel = {
1330   PyObject_HEAD_INIT(0) 0,              /* Header */
1331   "ECGroup",                            /* @tp_name@ */
1332   sizeof(group_pyobj),                  /* @tp_basicsize@ */
1333   0,                                    /* @tp_itemsize@ */
1334
1335   group_pydealloc,                      /* @tp_dealloc@ */
1336   0,                                    /* @tp_print@ */
1337   0,                                    /* @tp_getattr@ */
1338   0,                                    /* @tp_setattr@ */
1339   0,                                    /* @tp_compare@ */
1340   0,                                    /* @tp_repr@ */
1341   0,                                    /* @tp_as_number@ */
1342   0,                                    /* @tp_as_sequence@ */
1343   0,                                    /* @tp_as_mapping@ */
1344   0,                                    /* @tp_hash@ */
1345   0,                                    /* @tp_call@ */
1346   0,                                    /* @tp_str@ */
1347   0,                                    /* @tp_getattro@ */
1348   0,                                    /* @tp_setattro@ */
1349   0,                                    /* @tp_as_buffer@ */
1350   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
1351     Py_TPFLAGS_BASETYPE,
1352
1353   /* @tp_doc@ */
1354 "ECGroup(INFO): elliptic curve groups.",
1355
1356   0,                                    /* @tp_traverse@ */
1357   0,                                    /* @tp_clear@ */
1358   0,                                    /* @tp_richcompare@ */
1359   0,                                    /* @tp_weaklistoffset@ */
1360   0,                                    /* @tp_iter@ */
1361   0,                                    /* @tp_iternext@ */
1362   0,                                    /* @tp_methods@ */
1363   0,                                    /* @tp_members@ */
1364   ecgroup_pygetset,                     /* @tp_getset@ */
1365   0,                                    /* @tp_base@ */
1366   0,                                    /* @tp_dict@ */
1367   0,                                    /* @tp_descr_get@ */
1368   0,                                    /* @tp_descr_set@ */
1369   0,                                    /* @tp_dictoffset@ */
1370   0,                                    /* @tp_init@ */
1371   PyType_GenericAlloc,                  /* @tp_alloc@ */
1372   ecgroup_pynew,                        /* @tp_new@ */
1373   0,                                    /* @tp_free@ */
1374   0                                     /* @tp_is_gc@ */
1375 };
1376
1377 /*----- Global stuff ------------------------------------------------------*/
1378
1379 static PyMethodDef methods[] = {
1380 #define METHNAME(name) meth_##name
1381   METH  (_GE_frombuf,           "frombuf(BUF) -> X, REST")
1382   METH  (_GE_fromraw,           "fromraw(BUF) -> X, REST")
1383   METH  (_GE_fromstring,        "fromstring(STR) -> X, REST")
1384   METH  (_Group_parse,          "parse(STR) -> G, REST")
1385   METH  (_DHInfo_parse,         "parse(STR) -> D, REST")
1386   METH  (_BinDHInfo_parse,      "parse(STR) -> D, REST")
1387   METH  (_DHInfo__groupn,       0)
1388   METH  (_BinDHInfo__groupn,    0)
1389   KWMETH(_DHInfo_generate,      "\
1390 generate(PBITS, [qbits = 0], [event = pgen_nullev],\n\
1391          [rng = rand], [nsteps = 0]) -> D")
1392   KWMETH(_DHInfo_genlimlee,     "\
1393 genlimlee(PBITS, QBITS, [event = pgen_nullev], [ievent = pgen_nullev],\n\
1394           [rng = rand], [nsteps = 0], [subgroupp = True]) -> (D, [Q, ...])")
1395   KWMETH(_DHInfo_gendsa,        "\
1396 gendsa(PBITS, QBITS, SEED, [event = pgen_nullev], [nsteps = 0])\n\
1397   -> (D, SEED, COUNT)")
1398   KWMETH(_DHInfo_genkcdsa,      "\
1399 gendsa(PBITS, QBITS, [event = pgen_nullev], [rng = rand], [nsteps = 0])\n\
1400   -> (D, V)")
1401 #undef METHNAME
1402   { 0 }
1403 };
1404
1405 void group_pyinit(void)
1406 {
1407   INITTYPE(fginfo, root);
1408   INITTYPE(dhinfo, fginfo);
1409   INITTYPE(bindhinfo, dhinfo);
1410   INITTYPE(ge, root);
1411   INITTYPE(group, type);
1412   INITTYPE(primegroup, group);
1413   INITTYPE(bingroup, group);
1414   INITTYPE(ecgroup, group);
1415   addmethods(methods);
1416 }
1417
1418 void group_pyinsert(PyObject *mod)
1419 {
1420   INSERT("FGInfo", fginfo_pytype);
1421   INSERT("DHInfo", dhinfo_pytype);
1422   INSERT("BinDHInfo", bindhinfo_pytype);
1423   INSERT("GE", ge_pytype);
1424   INSERT("Group", group_pytype);
1425   INSERT("PrimeGroup", primegroup_pytype);
1426   INSERT("BinGroup", bingroup_pytype);
1427   INSERT("ECGroup", ecgroup_pytype);
1428   INSERT("_pgroups", namedgroups(ptab, &npgroups));
1429   INSERT("_bingroups", namedgroups(bintab, &nbingroups));
1430 }
1431
1432 /*----- That's all, folks -------------------------------------------------*/