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