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