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