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