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