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