3 * Abstract group inteface
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- DH and binary group infos -----------------------------------------*/
33 PyObject *fginfo_pywrap(gprime_param *dp, PyTypeObject *ty)
35 fginfo_pyobj *z = PyObject_New(fginfo_pyobj, ty);
37 return ((PyObject *)z);
40 static PyObject *fginfo_pynew(PyTypeObject *ty,
41 PyObject *arg, PyObject *kw)
43 static const char *const kwlist[] = { "p", "r", "g", 0 };
44 gprime_param dp = { 0 };
47 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&O&:new", KWLIST,
52 z = PyObject_New(fginfo_pyobj, ty);
54 return ((PyObject *)z);
62 static PyObject *figet_r(PyObject *me, void *hunoz)
63 { return mp_pywrap(MP_COPY(FGINFO_DP(me)->q)); }
65 static PyObject *diget_p(PyObject *me, void *hunoz)
66 { return mp_pywrap(MP_COPY(FGINFO_DP(me)->p)); }
68 static PyObject *diget_g(PyObject *me, void *hunoz)
69 { return mp_pywrap(MP_COPY(FGINFO_DP(me)->g)); }
71 static PyObject *biget_p(PyObject *me, void *hunoz)
72 { return gf_pywrap(MP_COPY(FGINFO_DP(me)->p)); }
74 static PyObject *biget_m(PyObject *me, void *hunoz)
75 { return PyInt_FromLong(mp_octets(FGINFO_DP(me)->p) - 1); }
77 static PyObject *biget_g(PyObject *me, void *hunoz)
78 { return gf_pywrap(MP_COPY(FGINFO_DP(me)->g)); }
80 static void fginfo_pydealloc(PyObject *me)
82 mp_drop(FGINFO_DP(me)->p);
83 mp_drop(FGINFO_DP(me)->q);
84 mp_drop(FGINFO_DP(me)->g);
88 static PyObject *meth__DHInfo_generate(PyObject *me,
89 PyObject *arg, PyObject *kw)
94 grand *r = &rand_global;
96 static const char *const kwlist[] =
97 { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
100 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", KWLIST,
101 &me, convuint, &pl, convuint, &ql,
102 convpgev, &evt, convgrand, &r,
105 if (dh_gen(&dp, ql, pl, steps, r, evt.proc, evt.ctx))
107 rc = fginfo_pywrap(&dp, dhinfo_pytype);
113 static PyObject *meth__DHInfo_genlimlee(PyObject *me,
114 PyObject *arg, PyObject *kw)
119 grand *r = &rand_global;
120 pgev oe = { 0 }, ie = { 0 };
123 static const char *const kwlist[] = {
124 "class", "pbits", "qbits", "event", "ievent",
125 "rng", "nsteps", "subgroupp", 0
129 PyObject *rc = 0, *vec = 0;
131 if (!PyArg_ParseTupleAndKeywords(arg, kw,
132 "OO&O&|O&O&O&O&O&:genlimlee", KWLIST,
133 &me, convuint, &pl, convuint, &ql,
134 convpgev, &oe, convpgev, &ie,
135 convgrand, &r, convuint, &steps,
136 convbool, &subgroupp))
138 if (subgroupp) f |= DH_SUBGROUP;
139 if (dh_limlee(&dp, ql, pl, f, steps, r,
140 oe.proc, oe.ctx, ie.proc, ie.ctx, &nf, &v))
142 vec = PyList_New(nf);
143 for (i = 0; i < nf; i++)
144 PyList_SetItem(vec, i, mp_pywrap(v[i]));
146 rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype), vec);
148 droppgev(&oe); droppgev(&ie);
152 static PyObject *meth__DHInfo_genkcdsa(PyObject *me,
153 PyObject *arg, PyObject *kw)
158 grand *r = &rand_global;
160 static const char *const kwlist[] =
161 { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
165 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|O&O&O&:genkcdsa", KWLIST,
166 &me, convuint, &pl, convuint, &ql,
167 convpgev, &evt, convgrand, &r,
170 if (dh_kcdsagen(&dp, ql, pl, 0, steps, r, evt.proc, evt.ctx))
172 mp_div(&v, 0, dp.p, dp.q);
174 rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype),
181 static PyObject *meth__DHInfo_gendsa(PyObject *me,
182 PyObject *arg, PyObject *kw)
191 static const char *const kwlist[] =
192 { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 };
195 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&s#|O&O&:gendsa", KWLIST,
196 &me, convuint, &pl, convuint, &ql,
197 &k, &ksz, convpgev, &evt,
200 if (dsa_gen(&dp, ql, pl, steps, k, ksz, &ds, evt.proc, evt.ctx))
202 rc = Py_BuildValue("(NNl)", fginfo_pywrap(&dp, dhinfo_pytype),
203 bytestring_pywrap(ds.p, ds.sz), (long)ds.count);
210 static int npgroups = -1, nbingroups = -1;
212 static PyObject *namedgroups(const pentry *pp, int *ne)
219 for (i = 0; pp[i].name; i++) {
221 for (j = 0; j < i; j++) {
222 if (pp[i].data == pp[j].data) {
223 c = PyDict_GetItemString(d, (/*unconst*/ char *)pp[j].name);
228 c = PyInt_FromLong(i);
230 PyDict_SetItemString(d, (/*unconst*/ char *)p, c);
237 static PyObject *meth__groupn(PyObject *me, PyObject *arg,
238 PyTypeObject *ty, const pentry *pp, int ne)
244 if (!PyArg_ParseTuple(arg, "Oi:_groupn", &me, &i)) goto end;
245 if (i < 0 || i >= ne) VALERR("group index out of range");
246 dh_infofromdata(&gp, pp[i].data);
247 rc = fginfo_pywrap(&gp, ty);
252 static PyObject *meth__DHInfo__groupn(PyObject *me, PyObject *arg)
253 { return (meth__groupn(me, arg, dhinfo_pytype, ptab, npgroups)); }
255 static PyObject *meth__BinDHInfo__groupn(PyObject *me, PyObject *arg)
256 { return (meth__groupn(me, arg, bindhinfo_pytype, bintab, nbingroups)); }
258 static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty,
259 int (*parse)(qd_parse *, gprime_param *))
266 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
269 if (parse(&qd, &gp)) VALERR(qd.e);
270 rc = fginfo_pywrap(&gp, ty);
275 static PyObject *meth__DHInfo_parse(PyObject *me, PyObject *arg)
276 { return (meth__parse(me, arg, dhinfo_pytype, dh_parse)); }
278 static PyObject *meth__BinDHInfo_parse(PyObject *me, PyObject *arg)
279 { return (meth__parse(me, arg, bindhinfo_pytype, dhbin_parse)); }
281 static PyGetSetDef fginfo_pygetset[] = {
282 #define GETSETNAME(op, name) fi##op##_##name
283 GET (r, "I.r -> group order")
288 static PyGetSetDef dhinfo_pygetset[] = {
289 #define GETSETNAME(op, name) di##op##_##name
290 GET (p, "I.p -> prime")
291 GET (g, "I.g -> generator")
296 static PyGetSetDef bindhinfo_pygetset[] = {
297 #define GETSETNAME(op, name) bi##op##_##name
298 GET (p, "I.p -> irreducible polynomial")
299 GET (m, "I.m -> degree of polynomial")
300 GET (g, "I.g -> generator")
305 static PyTypeObject fginfo_pytype_skel = {
306 PyObject_HEAD_INIT(0) 0, /* Header */
307 "FGInfo", /* @tp_name@ */
308 sizeof(fginfo_pyobj), /* @tp_basicsize@ */
309 0, /* @tp_itemsize@ */
311 fginfo_pydealloc, /* @tp_dealloc@ */
313 0, /* @tp_getattr@ */
314 0, /* @tp_setattr@ */
315 0, /* @tp_compare@ */
317 0, /* @tp_as_number@ */
318 0, /* @tp_as_sequence@ */
319 0, /* @tp_as_mapping@ */
323 0, /* @tp_getattro@ */
324 0, /* @tp_setattro@ */
325 0, /* @tp_as_buffer@ */
326 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
330 "Abstract base class for field-group information objects.",
332 0, /* @tp_traverse@ */
334 0, /* @tp_richcompare@ */
335 0, /* @tp_weaklistoffset@ */
337 0, /* @tp_iternext@ */
338 0, /* @tp_methods@ */
339 0, /* @tp_members@ */
340 fginfo_pygetset, /* @tp_getset@ */
343 0, /* @tp_descr_get@ */
344 0, /* @tp_descr_set@ */
345 0, /* @tp_dictoffset@ */
347 PyType_GenericAlloc, /* @tp_alloc@ */
348 abstract_pynew, /* @tp_new@ */
353 static PyTypeObject dhinfo_pytype_skel = {
354 PyObject_HEAD_INIT(0) 0, /* Header */
355 "DHInfo", /* @tp_name@ */
356 sizeof(fginfo_pyobj), /* @tp_basicsize@ */
357 0, /* @tp_itemsize@ */
359 fginfo_pydealloc, /* @tp_dealloc@ */
361 0, /* @tp_getattr@ */
362 0, /* @tp_setattr@ */
363 0, /* @tp_compare@ */
365 0, /* @tp_as_number@ */
366 0, /* @tp_as_sequence@ */
367 0, /* @tp_as_mapping@ */
371 0, /* @tp_getattro@ */
372 0, /* @tp_setattro@ */
373 0, /* @tp_as_buffer@ */
374 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
378 "DHInfo(P, R, G): standard (integer) Diffie-Hellman group information.",
380 0, /* @tp_traverse@ */
382 0, /* @tp_richcompare@ */
383 0, /* @tp_weaklistoffset@ */
385 0, /* @tp_iternext@ */
386 0, /* @tp_methods@ */
387 0, /* @tp_members@ */
388 dhinfo_pygetset, /* @tp_getset@ */
391 0, /* @tp_descr_get@ */
392 0, /* @tp_descr_set@ */
393 0, /* @tp_dictoffset@ */
395 PyType_GenericAlloc, /* @tp_alloc@ */
396 fginfo_pynew, /* @tp_new@ */
401 static PyTypeObject bindhinfo_pytype_skel = {
402 PyObject_HEAD_INIT(0) 0, /* Header */
403 "BinDHInfo", /* @tp_name@ */
404 sizeof(fginfo_pyobj), /* @tp_basicsize@ */
405 0, /* @tp_itemsize@ */
407 fginfo_pydealloc, /* @tp_dealloc@ */
409 0, /* @tp_getattr@ */
410 0, /* @tp_setattr@ */
411 0, /* @tp_compare@ */
413 0, /* @tp_as_number@ */
414 0, /* @tp_as_sequence@ */
415 0, /* @tp_as_mapping@ */
419 0, /* @tp_getattro@ */
420 0, /* @tp_setattro@ */
421 0, /* @tp_as_buffer@ */
422 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
426 "BinDHInfo(P, R, G): binary-field Diffie-Hellman group information.",
428 0, /* @tp_traverse@ */
430 0, /* @tp_richcompare@ */
431 0, /* @tp_weaklistoffset@ */
433 0, /* @tp_iternext@ */
434 0, /* @tp_methods@ */
435 0, /* @tp_members@ */
436 bindhinfo_pygetset, /* @tp_getset@ */
439 0, /* @tp_descr_get@ */
440 0, /* @tp_descr_set@ */
441 0, /* @tp_dictoffset@ */
443 PyType_GenericAlloc, /* @tp_alloc@ */
444 fginfo_pynew, /* @tp_new@ */
449 /*----- General utilities -------------------------------------------------*/
451 PyTypeObject *ge_pytype, *group_pytype;
452 PyTypeObject *primegroup_pytype, *bingroup_pytype, *ecgroup_pytype;
454 group *group_copy(group *g)
456 if (strcmp(G_NAME(g), "prime") == 0) {
457 gctx_prime *gc = (gctx_prime *)g;
459 gp.g = G_TOINT(g, MP_NEW, g->g);
462 g = group_prime(&gp);
464 } else if (strcmp(G_NAME(g), "bin") == 0) {
465 gctx_bin *gc = (gctx_bin *)g;
467 gb.g = G_TOINT(g, MP_NEW, g->g);
470 g = group_binary(&gb);
472 } else if (strcmp(G_NAME(g), "ec") == 0) {
473 gctx_ec *gc = (gctx_ec *)g;
475 if ((ei.c = eccurve_copy(gc->ei.c)) == 0)
478 EC_COPY(&ei.g, &gc->ei.g);
479 ei.r = MP_COPY(gc->ei.r);
480 ei.h = MP_COPY(gc->ei.h);
487 PyObject *ge_pywrap(PyObject *gobj, ge *x)
489 ge_pyobj *z = PyObject_New(ge_pyobj, (PyTypeObject *)gobj);
491 z->g = GROUP_G(gobj);
493 return ((PyObject *)z);
496 static PyObject *ge_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
498 static const char *const kwlist[] = { "x", 0 };
507 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", KWLIST, &x)) goto end;
509 if (ECPT_PYCHECK(x)) {
511 if (G_FROMEC(g, xx, &p))
512 TYERR("can't convert from elliptic curve point");
514 } else if ((y = tomp(x)) != 0) {
515 if (G_FROMINT(g, xx, y))
516 TYERR("can't convert from integer");
518 } else if (PyString_Check(x)) {
519 sc.buf = PyString_AS_STRING(x);
520 sc.lim = sc.buf + PyString_GET_SIZE(x);
521 if (G_READ(g, xx, &mptext_stringops, &sc) || sc.buf < sc.lim)
522 VALERR("malformed group element string");
524 TYERR("can't convert to group element");
525 return (ge_pywrap((PyObject *)ty, xx));
529 if (xx) G_DESTROY(g, xx);
533 static PyObject *group_dopywrap(PyTypeObject *ty, group *g)
535 group_pyobj *gobj = newtype(ty, 0, g->ops->name);
537 gobj->ty.ht_type.tp_basicsize = sizeof(ge_pyobj);
538 gobj->ty.ht_type.tp_base = ge_pytype;
539 Py_INCREF(group_pytype);
540 gobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
541 Py_TPFLAGS_BASETYPE |
542 Py_TPFLAGS_CHECKTYPES |
543 Py_TPFLAGS_HEAPTYPE);
544 gobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
545 gobj->ty.ht_type.tp_free = 0;
546 gobj->ty.ht_type.tp_new = ge_pynew;
547 typeready(&gobj->ty.ht_type);
548 return ((PyObject *)gobj);
551 PyObject *group_pywrap(group *g)
555 if (strcmp(G_NAME(g), "prime") == 0) ty = primegroup_pytype;
556 else if (strcmp(G_NAME(g), "bin") == 0) ty = bingroup_pytype;
557 else if (strcmp(G_NAME(g), "ec") == 0) ty = ecgroup_pytype;
559 return (group_dopywrap(ty, g));
562 /*----- Group elements ----------------------------------------------------*/
564 #define BINOP(name) \
565 static PyObject *ge_py##name(PyObject *x, PyObject *y) \
569 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \
570 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \
574 g->ops->name(g, z, GE_X(x), GE_X(y)); \
575 return (ge_pywrap(GE_GOBJ(x), z)); \
582 static PyObject *gemeth_##name(PyObject *me, PyObject *arg) \
586 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
589 g->ops->name(g, z, GE_X(me)); \
590 return (ge_pywrap(GE_GOBJ(me), z)); \
596 static PyObject *ge_pyexp(PyObject *x, PyObject *n, PyObject *m)
601 if (m != Py_None || !GE_PYCHECK(x) || (nn = getmp(n)) == 0)
603 z = G_CREATE(GE_G(x));
604 G_EXP(GE_G(x), z, GE_X(x), nn);
606 return (ge_pywrap(GE_GOBJ(x), z));
609 static void ge_pydealloc(PyObject *me)
611 G_DESTROY(GE_G(me), GE_X(me));
612 Py_DECREF(GE_GOBJ(me));
616 static void group_pydealloc(PyObject *me)
618 G_DESTROYGROUP(GROUP_G(me));
619 PyType_Type.tp_dealloc(me);
622 static PyObject *gmexp_id(PyObject *me)
624 group *g = GROUP_G(me); ge *x = G_CREATE(g);
625 G_COPY(g, x, g->i); return (ge_pywrap(me, x));
628 static int gmexp_fill(void *pp, PyObject *me, PyObject *x, PyObject *m)
630 group_expfactor *f = pp;
632 if (!GE_PYCHECK(x) || GE_G(x) != GROUP_G(me) || (f->exp = getmp(m)) == 0)
638 static PyObject *ge_pyrichcompare(PyObject *x, PyObject *y, int op)
643 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) ||
644 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y))))
647 case Py_EQ: b = G_EQ(GE_G(x), GE_X(x), GE_X(y)); break;
648 case Py_NE: b = !G_EQ(GE_G(x), GE_X(x), GE_X(y)); break;
649 default: TYERR("group elements are unordered");
656 static PyObject *gemeth_check(PyObject *me, PyObject *arg)
658 if (!PyArg_ParseTuple(arg, ":check")) goto end;
659 if (group_check(GE_G(me), GE_X(me))) VALERR("bad group element");
665 static int ge_pynonzerop(PyObject *x)
666 { return (!G_IDENTP(GE_G(x), GE_X(x))); }
668 static PyObject *ge_pystr(PyObject *me)
673 group_writedstr(GE_G(me), GE_X(me), &d);
674 rc = PyString_FromStringAndSize(d.buf, d.len);
679 static PyObject *ge_pylong(PyObject *me)
684 if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
685 TYERR("can't convert to integer");
692 static PyObject *ge_pyint(PyObject *me)
698 if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
699 TYERR("can't convert to integer");
700 if (!mp_tolong_checked(x, &l, 0)) rc = PyInt_FromLong(l);
701 else rc = mp_topylong(x);
707 static PyObject *gemeth_toint(PyObject *me, PyObject *arg)
711 if (!PyArg_ParseTuple(arg, ":toint")) goto end;
712 if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
713 TYERR("can't convert to integer");
714 return (mp_pywrap(x));
719 static PyObject *gemeth_toec(PyObject *me, PyObject *arg, PyObject *kw)
721 static const char *const kwlist[] = { "curve", 0 };
722 PyTypeObject *cty = 0;
728 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:toec", KWLIST,
730 g = GROUP_G(GE_GOBJ(me));
732 if (!PyType_Check(cty) || !PyType_IsSubtype(cty, ecpt_pytype))
733 TYERR("want subtype of catacomb.ECPt");
734 Py_INCREF((PyObject *)cty);
735 } else if (strcmp(G_NAME(g), "ec") == 0) {
736 c = eccurve_copy(((gctx_ec *)g)->ei.c);
737 cty = (PyTypeObject *)eccurve_pywrap(0, c);
740 Py_INCREF((PyObject *)cty);
742 if (G_TOEC(GE_G(me), &p, GE_X(me))) {
743 Py_DECREF((PyObject *)cty);
744 TYERR("can't convert to ec point");
746 rc = ecpt_pywrapout(cty, &p);
747 Py_DECREF((PyObject *)cty);
752 static PyObject *gemeth_tobuf(PyObject *me, PyObject *arg)
758 if (!PyArg_ParseTuple(arg, ":tobuf")) return (0);
759 n = GE_G(me)->noctets + 4;
760 rc = bytestring_pywrap(0, n);
761 buf_init(&b, PyString_AS_STRING(rc), n);
762 G_TOBUF(GE_G(me), &b, GE_X(me));
764 _PyString_Resize(&rc, BLEN(&b));
768 static PyObject *gemeth_toraw(PyObject *me, PyObject *arg)
774 if (!PyArg_ParseTuple(arg, ":toraw")) return (0);
775 n = GE_G(me)->noctets;
776 rc = bytestring_pywrap(0, n);
777 buf_init(&b, PyString_AS_STRING(rc), n);
778 G_TORAW(GE_G(me), &b, GE_X(me));
780 _PyString_Resize(&rc, BLEN(&b));
784 static PyObject *gmexp_exp(PyObject *me, void *pp, int n)
786 ge *z = G_CREATE(GROUP_G(me));
787 G_MEXP(GROUP_G(me), z, pp, n);
788 return (ge_pywrap(me, z));
791 static void gmexp_drop(void *pp)
793 group_expfactor *f = pp;
797 static PyObject *gmeth_mexp(PyObject *me, PyObject *arg)
799 return (mexp_common(me, arg, sizeof(group_expfactor),
800 gmexp_id, gmexp_fill, gmexp_exp, gmexp_drop));
803 static PyObject *gmeth_checkgroup(PyObject *me, PyObject *arg, PyObject *kw)
805 static const char *const kwlist[] = { "rng", 0 };
806 grand *r = &rand_global;
809 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:checkgroup", KWLIST,
812 if ((p = G_CHECK(GROUP_G(me), r)) != 0)
819 static PyObject *group_pyrichcompare(PyObject *x, PyObject *y, int op)
821 int b = group_samep(GROUP_G(x), GROUP_G(y));
825 default: TYERR("can't order groups");
832 static PyObject *meth__GE_frombuf(PyObject *me, PyObject *arg)
840 if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &n))
845 if (G_FROMBUF(g, &b, x))
846 VALERR("invalid data");
847 return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
849 if (x) G_DESTROY(g, x);
853 static PyObject *meth__GE_fromraw(PyObject *me, PyObject *arg)
861 if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &n))
866 if (G_FROMRAW(g, &b, x))
867 VALERR("invalid data");
868 return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
870 if (x) G_DESTROY(g, x);
874 static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg)
882 if (!PyArg_ParseTuple(arg, "Os#:fromstring", &me, &p, &n))
888 if (G_READ(g, x, &mptext_stringops, &sc))
889 VALERR("bad group element string");
890 return (Py_BuildValue("(Ns#)", ge_pywrap(me, x),
891 sc.buf, (Py_ssize_t)(sc.lim - sc.buf)));
893 if (x) G_DESTROY(g, x);
897 static PyObject *meth__Group_parse(PyObject *me, PyObject *arg)
903 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
907 if ((g = group_parse(&qd)) == 0)
909 return (group_pywrap(g));
914 static PyObject *geget_group(PyObject *me, void *hunoz)
915 { RETURN_OBJ(GE_GOBJ(me)); }
917 static PyObject *gget_nbits(PyObject *me, void *hunoz)
918 { return (PyInt_FromLong(GROUP_G(me)->nbits)); }
920 static PyObject *gget_noctets(PyObject *me, void *hunoz)
921 { return (PyInt_FromLong(GROUP_G(me)->noctets)); }
923 static PyObject *gget_i(PyObject *me, void *hunoz)
925 group *g = GROUP_G(me); ge *x = G_CREATE(g);
926 G_COPY(g, x, g->i); return (ge_pywrap(me, x));
929 static PyObject *gget_g(PyObject *me, void *hunoz)
931 group *g = GROUP_G(me); ge *x = G_CREATE(g);
932 G_COPY(g, x, g->g); return (ge_pywrap(me, x));
935 static long ge_pyhash(PyObject *me)
938 size_t sz = GE_G(me)->noctets + 4;
939 uint32 h = 0xf672c776 + GE_G(me)->ops->ty;
940 octet *p = xmalloc(sz);
942 G_TOBUF(GE_G(me), &b, GE_X(me));
944 h = unihash_hash(&unihash_global, h, BBASE(&b), BLEN(&b));
946 return (h % LONG_MAX);
949 static PyObject *gget_r(PyObject *me, void *hunoz)
950 { return (mp_pywrap(MP_COPY(GROUP_G(me)->r))); }
952 static PyObject *gget_h(PyObject *me, void *hunoz)
953 { return (mp_pywrap(MP_COPY(GROUP_G(me)->h))); }
955 static PyGetSetDef ge_pygetset[] = {
956 #define GETSETNAME(op, name) ge##op##_##name
957 GET (group, "X.group -> group containing X")
962 static PyMethodDef ge_pymethods[] = {
963 #define METHNAME(name) gemeth_##name
964 METH (inv, "X.inv() -> inverse element of X")
965 METH (sqr, "X.sqr() -> X^2 = X * X")
966 METH (check, "X.check() -> check X really belongs to its group")
967 METH (toint, "X.toint() -> X converted to an integer")
969 X.toec(curve = ecpt) -> X converted to elliptic curve point")
970 METH (tobuf, "X.tobuf() -> X in buffer representation")
971 METH (toraw, "X.toraw() -> X in raw representation")
976 static PyNumberMethods ge_pynumber = {
978 0, /* @nb_subtract@ */
979 ge_pymul, /* @nb_multiply@ */
980 ge_pydiv, /* @nb_divide@ */
981 0, /* @nb_remainder@ */
983 ge_pyexp, /* @nb_power@ */
984 0, /* @nb_negative@ */
985 0, /* @nb_positive@ */
986 0, /* @nb_absolute@ */
987 ge_pynonzerop, /* @nb_nonzero@ */
995 ge_pyint, /* @nb_int@ */
996 ge_pylong, /* @nb_long@ */
997 0 /* meaningless */, /* @nb_float@ */
1001 0, /* @nb_inplace_add@ */
1002 0, /* @nb_inplace_subtract@ */
1003 0, /* @nb_inplace_multiply@ */
1004 0, /* @nb_inplace_divide@ */
1005 0, /* @nb_inplace_remainder@ */
1006 0, /* @nb_inplace_power@ */
1007 0, /* @nb_inplace_lshift@ */
1008 0, /* @nb_inplace_rshift@ */
1009 0, /* @nb_inplace_and@ */
1010 0, /* @nb_inplace_xor@ */
1011 0, /* @nb_inplace_or@ */
1013 0, /* @nb_floor_divide@ */
1014 ge_pydiv, /* @nb_true_divide@ */
1015 0, /* @nb_inplace_floor_divide@ */
1016 0, /* @nb_inplace_true_divide@ */
1019 static PyTypeObject ge_pytype_skel = {
1020 PyObject_HEAD_INIT(0) 0, /* Header */
1021 "GE", /* @tp_name@ */
1022 sizeof(ge_pyobj), /* @tp_basicsize@ */
1023 0, /* @tp_itemsize@ */
1025 ge_pydealloc, /* @tp_dealloc@ */
1027 0, /* @tp_getattr@ */
1028 0, /* @tp_setattr@ */
1029 0, /* @tp_compare@ */
1031 &ge_pynumber, /* @tp_as_number@ */
1032 0, /* @tp_as_sequence@ */
1033 0, /* @tp_as_mapping@ */
1034 ge_pyhash, /* @tp_hash@ */
1036 ge_pystr, /* @tp_str@ */
1037 0, /* @tp_getattro@ */
1038 0, /* @tp_setattro@ */
1039 0, /* @tp_as_buffer@ */
1040 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1041 Py_TPFLAGS_CHECKTYPES |
1042 Py_TPFLAGS_BASETYPE,
1045 "Group elements, abstract base class.",
1047 0, /* @tp_traverse@ */
1049 ge_pyrichcompare, /* @tp_richcompare@ */
1050 0, /* @tp_weaklistoffset@ */
1052 0, /* @tp_iternext@ */
1053 ge_pymethods, /* @tp_methods@ */
1054 0, /* @tp_members@ */
1055 ge_pygetset, /* @tp_getset@ */
1058 0, /* @tp_descr_get@ */
1059 0, /* @tp_descr_set@ */
1060 0, /* @tp_dictoffset@ */
1062 PyType_GenericAlloc, /* @tp_alloc@ */
1063 abstract_pynew, /* @tp_new@ */
1068 static PyGetSetDef group_pygetset[] = {
1069 #define GETSETNAME(op, name) g##op##_##name
1070 GET (noctets, "G.noctets -> size in octets of element")
1071 GET (nbits, "G.nbits -> size in bits of element")
1072 GET (i, "G.i -> group identity")
1073 GET (g, "G.g -> group generator")
1074 GET (r, "G.r -> group order")
1075 GET (h, "G.h -> group cofactor")
1080 static PyMethodDef group_pymethods[] = {
1081 #define METHNAME(name) gmeth_##name
1083 G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1084 KWMETH(checkgroup, "G.checkgroup(rand = random): check group is good")
1089 static PyTypeObject group_pytype_skel = {
1090 PyObject_HEAD_INIT(0) 0, /* Header */
1091 "Group", /* @tp_name@ */
1092 sizeof(group_pyobj), /* @tp_basicsize@ */
1093 0, /* @tp_itemsize@ */
1095 group_pydealloc, /* @tp_dealloc@ */
1097 0, /* @tp_getattr@ */
1098 0, /* @tp_setattr@ */
1099 0, /* @tp_compare@ */
1101 0, /* @tp_as_number@ */
1102 0, /* @tp_as_sequence@ */
1103 0, /* @tp_as_mapping@ */
1107 0, /* @tp_getattro@ */
1108 0, /* @tp_setattro@ */
1109 0, /* @tp_as_buffer@ */
1110 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1111 Py_TPFLAGS_BASETYPE,
1114 "Abstract base class for groups.",
1116 0, /* @tp_traverse@ */
1118 group_pyrichcompare, /* @tp_richcompare@ */
1119 0, /* @tp_weaklistoffset@ */
1121 0, /* @tp_iternext@ */
1122 group_pymethods, /* @tp_methods@ */
1123 0, /* @tp_members@ */
1124 group_pygetset, /* @tp_getset@ */
1127 0, /* @tp_descr_get@ */
1128 0, /* @tp_descr_set@ */
1129 0, /* @tp_dictoffset@ */
1131 PyType_GenericAlloc, /* @tp_alloc@ */
1132 abstract_pynew, /* @tp_new@ */
1137 static PyObject *pgget_info(PyObject *me, void *hunoz)
1140 gctx_prime *gg = (gctx_prime *)GROUP_G(me);
1141 dp.p = MP_COPY(gg->mm.m);
1142 dp.q = MP_COPY(gg->g.r);
1143 dp.g = mpmont_reduce(&gg->mm, MP_NEW, gg->gen.x);
1144 return (fginfo_pywrap(&dp, dhinfo_pytype));
1147 static PyGetSetDef primegroup_pygetset[] = {
1148 #define GETSETNAME(op, name) pg##op##_##name
1149 GET (info, "G.info -> information about the group")
1154 static PyObject *primegroup_pynew(PyTypeObject *ty,
1155 PyObject *arg, PyObject *kw)
1158 static const char *const kwlist[] = { "info", 0 };
1160 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
1163 return (group_dopywrap(ty, group_prime(FGINFO_DP(i))));
1166 static PyTypeObject primegroup_pytype_skel = {
1167 PyObject_HEAD_INIT(0) 0, /* Header */
1168 "PrimeGroup", /* @tp_name@ */
1169 sizeof(group_pyobj), /* @tp_basicsize@ */
1170 0, /* @tp_itemsize@ */
1172 group_pydealloc, /* @tp_dealloc@ */
1174 0, /* @tp_getattr@ */
1175 0, /* @tp_setattr@ */
1176 0, /* @tp_compare@ */
1178 0, /* @tp_as_number@ */
1179 0, /* @tp_as_sequence@ */
1180 0, /* @tp_as_mapping@ */
1184 0, /* @tp_getattro@ */
1185 0, /* @tp_setattro@ */
1186 0, /* @tp_as_buffer@ */
1187 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1188 Py_TPFLAGS_BASETYPE,
1191 "PrimeGroup(INFO): subgroups of prime fields.",
1193 0, /* @tp_traverse@ */
1195 0, /* @tp_richcompare@ */
1196 0, /* @tp_weaklistoffset@ */
1198 0, /* @tp_iternext@ */
1199 0, /* @tp_methods@ */
1200 0, /* @tp_members@ */
1201 primegroup_pygetset, /* @tp_getset@ */
1204 0, /* @tp_descr_get@ */
1205 0, /* @tp_descr_set@ */
1206 0, /* @tp_dictoffset@ */
1208 PyType_GenericAlloc, /* @tp_alloc@ */
1209 primegroup_pynew, /* @tp_new@ */
1214 static PyObject *bgget_info(PyObject *me, void *hunoz)
1217 gctx_bin *gg = (gctx_bin *)GROUP_G(me);
1218 dp.p = MP_COPY(gg->r.p);
1219 dp.q = MP_COPY(gg->g.r);
1220 dp.g = MP_COPY(gg->gen.x);
1221 return (fginfo_pywrap(&dp, bindhinfo_pytype));
1224 static PyGetSetDef bingroup_pygetset[] = {
1225 #define GETSETNAME(op, name) bg##op##_##name
1226 GET (info, "G.info -> information about the group")
1231 static PyObject *bingroup_pynew(PyTypeObject *ty,
1232 PyObject *arg, PyObject *kw)
1235 static const char *const kwlist[] = { "info", 0 };
1237 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
1238 bindhinfo_pytype, &i))
1240 return (group_dopywrap(ty, group_binary(FGINFO_DP(i))));
1243 static PyTypeObject bingroup_pytype_skel = {
1244 PyObject_HEAD_INIT(0) 0, /* Header */
1245 "BinGroup", /* @tp_name@ */
1246 sizeof(group_pyobj), /* @tp_basicsize@ */
1247 0, /* @tp_itemsize@ */
1249 group_pydealloc, /* @tp_dealloc@ */
1251 0, /* @tp_getattr@ */
1252 0, /* @tp_setattr@ */
1253 0, /* @tp_compare@ */
1255 0, /* @tp_as_number@ */
1256 0, /* @tp_as_sequence@ */
1257 0, /* @tp_as_mapping@ */
1261 0, /* @tp_getattro@ */
1262 0, /* @tp_setattro@ */
1263 0, /* @tp_as_buffer@ */
1264 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1265 Py_TPFLAGS_BASETYPE,
1268 "BinGroup(INFO): subgroups of binary fields.",
1270 0, /* @tp_traverse@ */
1272 0, /* @tp_richcompare@ */
1273 0, /* @tp_weaklistoffset@ */
1275 0, /* @tp_iternext@ */
1276 0, /* @tp_methods@ */
1277 0, /* @tp_members@ */
1278 bingroup_pygetset, /* @tp_getset@ */
1281 0, /* @tp_descr_get@ */
1282 0, /* @tp_descr_set@ */
1283 0, /* @tp_dictoffset@ */
1285 PyType_GenericAlloc, /* @tp_alloc@ */
1286 bingroup_pynew, /* @tp_new@ */
1291 static PyObject *egget_info(PyObject *me, void *hunoz)
1294 gctx_ec *gg = (gctx_ec *)GROUP_G(me);
1296 ecinfo_copy(&ei, &gg->ei);
1297 return (ecinfo_pywrap(&ei));
1300 static PyGetSetDef ecgroup_pygetset[] = {
1301 #define GETSETNAME(op, name) eg##op##_##name
1302 GET (info, "G.info -> information about the group")
1307 static PyObject *ecgroup_pynew(PyTypeObject *ty,
1308 PyObject *arg, PyObject *kw)
1312 static const char *const kwlist[] = { "info", 0 };
1314 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
1317 ecinfo_copy(&ei, ECINFO_EI(i));
1318 return (group_dopywrap(ty, group_ec(&ei)));
1321 static PyTypeObject ecgroup_pytype_skel = {
1322 PyObject_HEAD_INIT(0) 0, /* Header */
1323 "ECGroup", /* @tp_name@ */
1324 sizeof(group_pyobj), /* @tp_basicsize@ */
1325 0, /* @tp_itemsize@ */
1327 group_pydealloc, /* @tp_dealloc@ */
1329 0, /* @tp_getattr@ */
1330 0, /* @tp_setattr@ */
1331 0, /* @tp_compare@ */
1333 0, /* @tp_as_number@ */
1334 0, /* @tp_as_sequence@ */
1335 0, /* @tp_as_mapping@ */
1339 0, /* @tp_getattro@ */
1340 0, /* @tp_setattro@ */
1341 0, /* @tp_as_buffer@ */
1342 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1343 Py_TPFLAGS_BASETYPE,
1346 "ECGroup(INFO): elliptic curve groups.",
1348 0, /* @tp_traverse@ */
1350 0, /* @tp_richcompare@ */
1351 0, /* @tp_weaklistoffset@ */
1353 0, /* @tp_iternext@ */
1354 0, /* @tp_methods@ */
1355 0, /* @tp_members@ */
1356 ecgroup_pygetset, /* @tp_getset@ */
1359 0, /* @tp_descr_get@ */
1360 0, /* @tp_descr_set@ */
1361 0, /* @tp_dictoffset@ */
1363 PyType_GenericAlloc, /* @tp_alloc@ */
1364 ecgroup_pynew, /* @tp_new@ */
1369 /*----- Global stuff ------------------------------------------------------*/
1371 static PyMethodDef methods[] = {
1372 #define METHNAME(name) meth_##name
1373 METH (_GE_frombuf, "frombuf(BUF) -> X, REST")
1374 METH (_GE_fromraw, "fromraw(BUF) -> X, REST")
1375 METH (_GE_fromstring, "fromstring(STR) -> X, REST")
1376 METH (_Group_parse, "parse(STR) -> G, REST")
1377 METH (_DHInfo_parse, "parse(STR) -> D, REST")
1378 METH (_BinDHInfo_parse, "parse(STR) -> D, REST")
1379 METH (_DHInfo__groupn, 0)
1380 METH (_BinDHInfo__groupn, 0)
1381 KWMETH(_DHInfo_generate, "\
1382 generate(PBITS, [qbits = 0, event = pgen_nullev,\n\
1383 rng = rand, nsteps = 0]) -> D")
1384 KWMETH(_DHInfo_genlimlee, "\
1385 genlimlee(PBITS, QBITS, [event = pgen_nullev, ievent = pgen_nullev,\n\
1386 rng = rand, nsteps = 0, subgroupp = True]) -> (D, [Q, ...])")
1387 KWMETH(_DHInfo_gendsa, "\
1388 gendsa(PBITS, QBITS, SEED, [event = pgen_nullev, nsteps = 0])\n\
1389 -> (D, SEED, COUNT)")
1390 KWMETH(_DHInfo_genkcdsa, "\
1391 gendsa(PBITS, QBITS, [event = pgen_nullev, rng = rand, nsteps = 0])\n\
1397 void group_pyinit(void)
1399 INITTYPE(fginfo, root);
1400 INITTYPE(dhinfo, fginfo);
1401 INITTYPE(bindhinfo, dhinfo);
1403 INITTYPE(group, type);
1404 INITTYPE(primegroup, group);
1405 INITTYPE(bingroup, group);
1406 INITTYPE(ecgroup, group);
1407 addmethods(methods);
1410 void group_pyinsert(PyObject *mod)
1412 INSERT("FGInfo", fginfo_pytype);
1413 INSERT("DHInfo", dhinfo_pytype);
1414 INSERT("BinDHInfo", bindhinfo_pytype);
1415 INSERT("GE", ge_pytype);
1416 INSERT("Group", group_pytype);
1417 INSERT("PrimeGroup", primegroup_pytype);
1418 INSERT("BinGroup", bingroup_pytype);
1419 INSERT("ECGroup", ecgroup_pytype);
1420 INSERT("_pgroups", namedgroups(ptab, &npgroups));
1421 INSERT("_bingroups", namedgroups(bintab, &nbingroups));
1424 /*----- That's all, folks -------------------------------------------------*/