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