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