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 char *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;
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 char *kwlist[] = { "class", "pbits", "qbits", "event", "ievent",
124 "rng", "nsteps", "subgroupp", 0 };
127 PyObject *rc = 0, *vec = 0;
129 if (!PyArg_ParseTupleAndKeywords(arg, kw,
130 "OO&O&|O&O&O&O&O&:genlimlee", kwlist,
131 &me, convuint, &pl, convuint, &ql,
132 convpgev, &oe, convpgev, &ie,
133 convgrand, &r, convuint, &steps,
134 convbool, &subgroupp))
136 if (subgroupp) f |= DH_SUBGROUP;
137 if (dh_limlee(&dp, ql, pl, f, steps, r,
138 oe.proc, oe.ctx, ie.proc, ie.ctx, &nf, &v))
140 vec = PyList_New(nf);
141 for (i = 0; i < nf; i++)
142 PyList_SetItem(vec, i, mp_pywrap(v[i]));
144 rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype), vec);
146 droppgev(&oe); droppgev(&ie);
150 static PyObject *meth__DHInfo_genkcdsa(PyObject *me,
151 PyObject *arg, PyObject *kw)
156 grand *r = &rand_global;
158 char *kwlist[] = { "class", "pbits", "qbits",
159 "event", "rng", "nsteps", 0 };
163 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|O&O&O&:genkcdsa", kwlist,
164 &me, convuint, &pl, convuint, &ql,
165 convpgev, &evt, convgrand, &r,
168 if (dh_kcdsagen(&dp, ql, pl, 0, steps, r, evt.proc, evt.ctx))
170 mp_div(&v, 0, dp.p, dp.q);
172 rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype),
179 static PyObject *meth__DHInfo_gendsa(PyObject *me,
180 PyObject *arg, PyObject *kw)
190 { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 };
193 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&s#|O&O&:gendsa", kwlist,
194 &me, convuint, &pl, convuint, &ql,
195 &k, &ksz, convpgev, &evt,
198 if (dsa_gen(&dp, ql, pl, steps, k, ksz, &ds, evt.proc, evt.ctx))
200 rc = Py_BuildValue("(NNl)", fginfo_pywrap(&dp, dhinfo_pytype),
201 bytestring_pywrap(ds.p, ds.sz), (long)ds.count);
208 static int npgroups = -1, nbingroups = -1;
210 static PyObject *namedgroups(const pentry *pp, int *ne)
217 for (i = 0; pp[i].name; i++) {
219 for (j = 0; j < i; j++) {
220 if (pp[i].data == pp[j].data) {
221 c = PyDict_GetItemString(d, (/*unconst*/ char *)pp[j].name);
226 c = PyInt_FromLong(i);
228 PyDict_SetItemString(d, (/*unconst*/ char *)p, c);
235 static PyObject *meth__groupn(PyObject *me, PyObject *arg,
236 PyTypeObject *ty, const pentry *pp, int ne)
242 if (!PyArg_ParseTuple(arg, "Oi:_groupn", &me, &i)) goto end;
243 if (i < 0 || i >= ne) VALERR("group index out of range");
244 dh_infofromdata(&gp, pp[i].data);
245 rc = fginfo_pywrap(&gp, ty);
250 static PyObject *meth__DHInfo__groupn(PyObject *me, PyObject *arg)
251 { return (meth__groupn(me, arg, dhinfo_pytype, ptab, npgroups)); }
253 static PyObject *meth__BinDHInfo__groupn(PyObject *me, PyObject *arg)
254 { return (meth__groupn(me, arg, bindhinfo_pytype, bintab, nbingroups)); }
256 static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty,
257 int (*parse)(qd_parse *, gprime_param *))
264 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
267 if (parse(&qd, &gp)) VALERR(qd.e);
268 rc = fginfo_pywrap(&gp, ty);
273 static PyObject *meth__DHInfo_parse(PyObject *me, PyObject *arg)
274 { return (meth__parse(me, arg, dhinfo_pytype, dh_parse)); }
276 static PyObject *meth__BinDHInfo_parse(PyObject *me, PyObject *arg)
277 { return (meth__parse(me, arg, bindhinfo_pytype, dhbin_parse)); }
279 static PyGetSetDef fginfo_pygetset[] = {
280 #define GETSETNAME(op, name) fi##op##_##name
281 GET (r, "I.r -> group order")
286 static PyGetSetDef dhinfo_pygetset[] = {
287 #define GETSETNAME(op, name) di##op##_##name
288 GET (p, "I.p -> prime")
289 GET (g, "I.g -> generator")
294 static PyGetSetDef bindhinfo_pygetset[] = {
295 #define GETSETNAME(op, name) bi##op##_##name
296 GET (p, "I.p -> irreducible polynomial")
297 GET (m, "I.m -> degree of polynomial")
298 GET (g, "I.g -> generator")
303 static PyTypeObject fginfo_pytype_skel = {
304 PyObject_HEAD_INIT(0) 0, /* Header */
305 "FGInfo", /* @tp_name@ */
306 sizeof(fginfo_pyobj), /* @tp_basicsize@ */
307 0, /* @tp_itemsize@ */
309 fginfo_pydealloc, /* @tp_dealloc@ */
311 0, /* @tp_getattr@ */
312 0, /* @tp_setattr@ */
313 0, /* @tp_compare@ */
315 0, /* @tp_as_number@ */
316 0, /* @tp_as_sequence@ */
317 0, /* @tp_as_mapping@ */
321 0, /* @tp_getattro@ */
322 0, /* @tp_setattro@ */
323 0, /* @tp_as_buffer@ */
324 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
328 "Abstract base class for field-group information objects.",
330 0, /* @tp_traverse@ */
332 0, /* @tp_richcompare@ */
333 0, /* @tp_weaklistoffset@ */
335 0, /* @tp_iternext@ */
336 0, /* @tp_methods@ */
337 0, /* @tp_members@ */
338 fginfo_pygetset, /* @tp_getset@ */
341 0, /* @tp_descr_get@ */
342 0, /* @tp_descr_set@ */
343 0, /* @tp_dictoffset@ */
345 PyType_GenericAlloc, /* @tp_alloc@ */
346 abstract_pynew, /* @tp_new@ */
351 static PyTypeObject dhinfo_pytype_skel = {
352 PyObject_HEAD_INIT(0) 0, /* Header */
353 "DHInfo", /* @tp_name@ */
354 sizeof(fginfo_pyobj), /* @tp_basicsize@ */
355 0, /* @tp_itemsize@ */
357 fginfo_pydealloc, /* @tp_dealloc@ */
359 0, /* @tp_getattr@ */
360 0, /* @tp_setattr@ */
361 0, /* @tp_compare@ */
363 0, /* @tp_as_number@ */
364 0, /* @tp_as_sequence@ */
365 0, /* @tp_as_mapping@ */
369 0, /* @tp_getattro@ */
370 0, /* @tp_setattro@ */
371 0, /* @tp_as_buffer@ */
372 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
376 "Standard (integer) Diffie-Hellman group information.",
378 0, /* @tp_traverse@ */
380 0, /* @tp_richcompare@ */
381 0, /* @tp_weaklistoffset@ */
383 0, /* @tp_iternext@ */
384 0, /* @tp_methods@ */
385 0, /* @tp_members@ */
386 dhinfo_pygetset, /* @tp_getset@ */
389 0, /* @tp_descr_get@ */
390 0, /* @tp_descr_set@ */
391 0, /* @tp_dictoffset@ */
393 PyType_GenericAlloc, /* @tp_alloc@ */
394 fginfo_pynew, /* @tp_new@ */
399 static PyTypeObject bindhinfo_pytype_skel = {
400 PyObject_HEAD_INIT(0) 0, /* Header */
401 "BinDHInfo", /* @tp_name@ */
402 sizeof(fginfo_pyobj), /* @tp_basicsize@ */
403 0, /* @tp_itemsize@ */
405 fginfo_pydealloc, /* @tp_dealloc@ */
407 0, /* @tp_getattr@ */
408 0, /* @tp_setattr@ */
409 0, /* @tp_compare@ */
411 0, /* @tp_as_number@ */
412 0, /* @tp_as_sequence@ */
413 0, /* @tp_as_mapping@ */
417 0, /* @tp_getattro@ */
418 0, /* @tp_setattro@ */
419 0, /* @tp_as_buffer@ */
420 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
424 "Binary-field Diffie-Hellman group information.",
426 0, /* @tp_traverse@ */
428 0, /* @tp_richcompare@ */
429 0, /* @tp_weaklistoffset@ */
431 0, /* @tp_iternext@ */
432 0, /* @tp_methods@ */
433 0, /* @tp_members@ */
434 bindhinfo_pygetset, /* @tp_getset@ */
437 0, /* @tp_descr_get@ */
438 0, /* @tp_descr_set@ */
439 0, /* @tp_dictoffset@ */
441 PyType_GenericAlloc, /* @tp_alloc@ */
442 fginfo_pynew, /* @tp_new@ */
447 /*----- General utilities -------------------------------------------------*/
449 PyTypeObject *ge_pytype, *group_pytype;
450 PyTypeObject *primegroup_pytype, *bingroup_pytype, *ecgroup_pytype;
452 group *group_copy(group *g)
454 if (strcmp(G_NAME(g), "prime") == 0) {
455 gctx_prime *gc = (gctx_prime *)g;
457 gp.g = G_TOINT(g, MP_NEW, g->g);
460 g = group_prime(&gp);
462 } else if (strcmp(G_NAME(g), "bin") == 0) {
463 gctx_bin *gc = (gctx_bin *)g;
465 gb.g = G_TOINT(g, MP_NEW, g->g);
468 g = group_binary(&gb);
470 } else if (strcmp(G_NAME(g), "ec") == 0) {
471 gctx_ec *gc = (gctx_ec *)g;
473 if ((ei.c = eccurve_copy(gc->ei.c)) == 0)
476 EC_COPY(&ei.g, &gc->ei.g);
477 ei.r = MP_COPY(gc->ei.r);
478 ei.h = MP_COPY(gc->ei.h);
485 PyObject *ge_pywrap(PyObject *gobj, ge *x)
487 ge_pyobj *z = PyObject_New(ge_pyobj, (PyTypeObject *)gobj);
489 z->g = GROUP_G(gobj);
491 return ((PyObject *)z);
494 static PyObject *ge_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
496 char *kwlist[] = { "x", 0 };
505 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &x)) goto end;
507 if (ECPT_PYCHECK(x)) {
509 if (G_FROMEC(g, xx, &p))
510 TYERR("can't convert from elliptic curve point");
512 } else if ((y = tomp(x)) != 0) {
513 if (G_FROMINT(g, xx, y))
514 TYERR("can't convert from integer");
516 } else if (PyString_Check(x)) {
517 sc.buf = PyString_AS_STRING(x);
518 sc.lim = sc.buf + PyString_GET_SIZE(x);
519 if (G_READ(g, xx, &mptext_stringops, &sc) || sc.buf < sc.lim)
520 VALERR("malformed group element string");
522 TYERR("can't convert to group element");
523 return (ge_pywrap((PyObject *)ty, xx));
527 if (xx) G_DESTROY(g, xx);
531 static PyObject *group_dopywrap(PyTypeObject *ty, group *g)
533 group_pyobj *gobj = newtype(ty, 0, g->ops->name);
535 gobj->ty.ht_type.tp_basicsize = sizeof(ge_pyobj);
536 gobj->ty.ht_type.tp_base = ge_pytype;
537 Py_INCREF(group_pytype);
538 gobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
539 Py_TPFLAGS_BASETYPE |
540 Py_TPFLAGS_CHECKTYPES |
541 Py_TPFLAGS_HEAPTYPE);
542 gobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
543 gobj->ty.ht_type.tp_free = 0;
544 gobj->ty.ht_type.tp_new = ge_pynew;
545 typeready(&gobj->ty.ht_type);
546 return ((PyObject *)gobj);
549 PyObject *group_pywrap(group *g)
553 if (strcmp(G_NAME(g), "prime") == 0) ty = primegroup_pytype;
554 else if (strcmp(G_NAME(g), "bin") == 0) ty = bingroup_pytype;
555 else if (strcmp(G_NAME(g), "ec") == 0) ty = ecgroup_pytype;
557 return (group_dopywrap(ty, g));
560 /*----- Group elements ----------------------------------------------------*/
562 #define BINOP(name) \
563 static PyObject *ge_py##name(PyObject *x, PyObject *y) \
567 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \
568 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \
572 g->ops->name(g, z, GE_X(x), GE_X(y)); \
573 return (ge_pywrap(GE_GOBJ(x), z)); \
580 static PyObject *gemeth_##name(PyObject *me, PyObject *arg) \
584 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
587 g->ops->name(g, z, GE_X(me)); \
588 return (ge_pywrap(GE_GOBJ(me), z)); \
594 static PyObject *ge_pyexp(PyObject *x, PyObject *n, PyObject *m)
599 if (m != Py_None || !GE_PYCHECK(x) || (nn = getmp(n)) == 0)
601 z = G_CREATE(GE_G(x));
602 G_EXP(GE_G(x), z, GE_X(x), nn);
604 return (ge_pywrap(GE_GOBJ(x), z));
607 static void ge_pydealloc(PyObject *me)
609 G_DESTROY(GE_G(me), GE_X(me));
610 Py_DECREF(GE_GOBJ(me));
614 static void group_pydealloc(PyObject *me)
616 G_DESTROYGROUP(GROUP_G(me));
617 PyType_Type.tp_dealloc(me);
620 static PyObject *gmexp_id(PyObject *me)
622 group *g = GROUP_G(me); ge *x = G_CREATE(g);
623 G_COPY(g, x, g->i); return (ge_pywrap(me, x));
626 static int gmexp_fill(void *pp, PyObject *me, PyObject *x, PyObject *m)
628 group_expfactor *f = pp;
630 if (!GE_PYCHECK(x) || GE_G(x) != GROUP_G(me) || (f->exp = getmp(m)) == 0)
636 static PyObject *ge_pyrichcompare(PyObject *x, PyObject *y, int op)
641 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) ||
642 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y))))
645 case Py_EQ: b = G_EQ(GE_G(x), GE_X(x), GE_X(y)); break;
646 case Py_NE: b = !G_EQ(GE_G(x), GE_X(x), GE_X(y)); break;
647 default: TYERR("group elements are unordered");
654 static PyObject *gemeth_check(PyObject *me, PyObject *arg)
656 if (!PyArg_ParseTuple(arg, ":check")) goto end;
657 if (group_check(GE_G(me), GE_X(me))) VALERR("bad group element");
663 static int ge_pynonzerop(PyObject *x)
664 { return (!G_IDENTP(GE_G(x), GE_X(x))); }
666 static PyObject *ge_pystr(PyObject *me)
671 group_writedstr(GE_G(me), GE_X(me), &d);
672 rc = PyString_FromStringAndSize(d.buf, d.len);
677 static PyObject *ge_pylong(PyObject *me)
682 if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
683 TYERR("can't convert to integer");
690 static PyObject *ge_pyint(PyObject *me)
696 if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
697 TYERR("can't convert to integer");
698 if (!mp_tolong_checked(x, &l, 0)) rc = PyInt_FromLong(l);
699 else rc = mp_topylong(x);
705 static PyObject *gemeth_toint(PyObject *me, PyObject *arg)
709 if (!PyArg_ParseTuple(arg, ":toint")) goto end;
710 if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
711 TYERR("can't convert to integer");
712 return (mp_pywrap(x));
717 static PyObject *gemeth_toec(PyObject *me, PyObject *arg, PyObject *kw)
719 char *kwlist[] = { "curve", 0 };
720 PyTypeObject *cty = 0;
726 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:toec", kwlist,
728 g = GROUP_G(GE_GOBJ(me));
730 if (!PyType_Check(cty) || !PyType_IsSubtype(cty, ecpt_pytype))
731 TYERR("want subtype of catacomb.ECPt");
732 Py_INCREF((PyObject *)cty);
733 } else if (strcmp(G_NAME(g), "ec") == 0) {
734 c = eccurve_copy(((gctx_ec *)g)->ei.c);
735 cty = (PyTypeObject *)eccurve_pywrap(0, c);
738 Py_INCREF((PyObject *)cty);
740 if (G_TOEC(GE_G(me), &p, GE_X(me))) {
741 Py_DECREF((PyObject *)cty);
742 TYERR("can't convert to ec point");
744 rc = ecpt_pywrapout(cty, &p);
745 Py_DECREF((PyObject *)cty);
750 static PyObject *gemeth_tobuf(PyObject *me, PyObject *arg)
756 if (!PyArg_ParseTuple(arg, ":tobuf")) return (0);
757 n = GE_G(me)->noctets + 4;
758 rc = bytestring_pywrap(0, n);
759 buf_init(&b, PyString_AS_STRING(rc), n);
760 G_TOBUF(GE_G(me), &b, GE_X(me));
762 _PyString_Resize(&rc, BLEN(&b));
766 static PyObject *gemeth_toraw(PyObject *me, PyObject *arg)
772 if (!PyArg_ParseTuple(arg, ":toraw")) return (0);
773 n = GE_G(me)->noctets;
774 rc = bytestring_pywrap(0, n);
775 buf_init(&b, PyString_AS_STRING(rc), n);
776 G_TORAW(GE_G(me), &b, GE_X(me));
778 _PyString_Resize(&rc, BLEN(&b));
782 static PyObject *gmexp_exp(PyObject *me, void *pp, int n)
784 ge *z = G_CREATE(GROUP_G(me));
785 G_MEXP(GROUP_G(me), z, pp, n);
786 return (ge_pywrap(me, z));
789 static void gmexp_drop(void *pp)
791 group_expfactor *f = pp;
795 static PyObject *gmeth_mexp(PyObject *me, PyObject *arg)
797 return (mexp_common(me, arg, sizeof(group_expfactor),
798 gmexp_id, gmexp_fill, gmexp_exp, gmexp_drop));
801 static PyObject *gmeth_checkgroup(PyObject *me, PyObject *arg, PyObject *kw)
803 char *kwlist[] = { "rng", 0 };
804 grand *r = &rand_global;
807 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:checkgroup", kwlist,
810 if ((p = G_CHECK(GROUP_G(me), r)) != 0)
817 static PyObject *group_pyrichcompare(PyObject *x, PyObject *y, int op)
819 int b = group_samep(GROUP_G(x), GROUP_G(y));
823 default: TYERR("can't order groups");
830 static PyObject *meth__GE_frombuf(PyObject *me, PyObject *arg)
838 if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &n))
843 if (G_FROMBUF(g, &b, x))
844 VALERR("invalid data");
845 return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
847 if (x) G_DESTROY(g, x);
851 static PyObject *meth__GE_fromraw(PyObject *me, PyObject *arg)
859 if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &n))
864 if (G_FROMRAW(g, &b, x))
865 VALERR("invalid data");
866 return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
868 if (x) G_DESTROY(g, x);
872 static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg)
880 if (!PyArg_ParseTuple(arg, "Os#:fromstring", &me, &p, &n))
886 if (G_READ(g, x, &mptext_stringops, &sc))
887 VALERR("bad group element string");
888 return (Py_BuildValue("(Ns#)", ge_pywrap(me, x),
889 sc.buf, (Py_ssize_t)(sc.lim - sc.buf)));
891 if (x) G_DESTROY(g, x);
895 static PyObject *meth__Group_parse(PyObject *me, PyObject *arg)
901 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
905 if ((g = group_parse(&qd)) == 0)
907 return (group_pywrap(g));
912 static PyObject *geget_group(PyObject *me, void *hunoz)
913 { RETURN_OBJ(GE_GOBJ(me)); }
915 static PyObject *gget_nbits(PyObject *me, void *hunoz)
916 { return (PyInt_FromLong(GROUP_G(me)->nbits)); }
918 static PyObject *gget_noctets(PyObject *me, void *hunoz)
919 { return (PyInt_FromLong(GROUP_G(me)->noctets)); }
921 static PyObject *gget_i(PyObject *me, void *hunoz)
923 group *g = GROUP_G(me); ge *x = G_CREATE(g);
924 G_COPY(g, x, g->i); return (ge_pywrap(me, x));
927 static PyObject *gget_g(PyObject *me, void *hunoz)
929 group *g = GROUP_G(me); ge *x = G_CREATE(g);
930 G_COPY(g, x, g->g); return (ge_pywrap(me, x));
933 static long ge_pyhash(PyObject *me)
936 size_t sz = GE_G(me)->noctets + 4;
937 uint32 h = 0xf672c776 + GE_G(me)->ops->ty;
938 octet *p = xmalloc(sz);
940 G_TOBUF(GE_G(me), &b, GE_X(me));
942 h = unihash_hash(&unihash_global, h, BBASE(&b), BLEN(&b));
944 return (h % LONG_MAX);
947 static PyObject *gget_r(PyObject *me, void *hunoz)
948 { return (mp_pywrap(MP_COPY(GROUP_G(me)->r))); }
950 static PyObject *gget_h(PyObject *me, void *hunoz)
951 { return (mp_pywrap(MP_COPY(GROUP_G(me)->h))); }
953 static PyGetSetDef ge_pygetset[] = {
954 #define GETSETNAME(op, name) ge##op##_##name
955 GET (group, "X.group -> group containing X")
960 static PyMethodDef ge_pymethods[] = {
961 #define METHNAME(name) gemeth_##name
962 METH (inv, "X.inv() -> inverse element of X")
963 METH (sqr, "X.sqr() -> X^2 = X * X")
964 METH (check, "X.check() -> check X really belongs to its group")
965 METH (toint, "X.toint() -> X converted to an integer")
967 X.toec(curve = ecpt) -> X converted to elliptic curve point")
968 METH (tobuf, "X.tobuf() -> X in buffer representation")
969 METH (toraw, "X.toraw() -> X in raw representation")
974 static PyNumberMethods ge_pynumber = {
976 0, /* @nb_subtract@ */
977 ge_pymul, /* @nb_multiply@ */
978 ge_pydiv, /* @nb_divide@ */
979 0, /* @nb_remainder@ */
981 ge_pyexp, /* @nb_power@ */
982 0, /* @nb_negative@ */
983 0, /* @nb_positive@ */
984 0, /* @nb_absolute@ */
985 ge_pynonzerop, /* @nb_nonzero@ */
993 ge_pyint, /* @nb_int@ */
994 ge_pylong, /* @nb_long@ */
995 0 /* meaningless */, /* @nb_float@ */
999 0, /* @nb_inplace_add@ */
1000 0, /* @nb_inplace_subtract@ */
1001 0, /* @nb_inplace_multiply@ */
1002 0, /* @nb_inplace_divide@ */
1003 0, /* @nb_inplace_remainder@ */
1004 0, /* @nb_inplace_power@ */
1005 0, /* @nb_inplace_lshift@ */
1006 0, /* @nb_inplace_rshift@ */
1007 0, /* @nb_inplace_and@ */
1008 0, /* @nb_inplace_xor@ */
1009 0, /* @nb_inplace_or@ */
1011 0, /* @nb_floor_divide@ */
1012 ge_pydiv, /* @nb_true_divide@ */
1013 0, /* @nb_inplace_floor_divide@ */
1014 0, /* @nb_inplace_true_divide@ */
1017 static PyTypeObject ge_pytype_skel = {
1018 PyObject_HEAD_INIT(0) 0, /* Header */
1019 "GE", /* @tp_name@ */
1020 sizeof(ge_pyobj), /* @tp_basicsize@ */
1021 0, /* @tp_itemsize@ */
1023 ge_pydealloc, /* @tp_dealloc@ */
1025 0, /* @tp_getattr@ */
1026 0, /* @tp_setattr@ */
1027 0, /* @tp_compare@ */
1029 &ge_pynumber, /* @tp_as_number@ */
1030 0, /* @tp_as_sequence@ */
1031 0, /* @tp_as_mapping@ */
1032 ge_pyhash, /* @tp_hash@ */
1034 ge_pystr, /* @tp_str@ */
1035 0, /* @tp_getattro@ */
1036 0, /* @tp_setattro@ */
1037 0, /* @tp_as_buffer@ */
1038 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1039 Py_TPFLAGS_CHECKTYPES |
1040 Py_TPFLAGS_BASETYPE,
1043 "Group elements, abstract base class.",
1045 0, /* @tp_traverse@ */
1047 ge_pyrichcompare, /* @tp_richcompare@ */
1048 0, /* @tp_weaklistoffset@ */
1050 0, /* @tp_iternext@ */
1051 ge_pymethods, /* @tp_methods@ */
1052 0, /* @tp_members@ */
1053 ge_pygetset, /* @tp_getset@ */
1056 0, /* @tp_descr_get@ */
1057 0, /* @tp_descr_set@ */
1058 0, /* @tp_dictoffset@ */
1060 PyType_GenericAlloc, /* @tp_alloc@ */
1061 abstract_pynew, /* @tp_new@ */
1066 static PyGetSetDef group_pygetset[] = {
1067 #define GETSETNAME(op, name) g##op##_##name
1068 GET (noctets, "G.noctets -> size in octets of element")
1069 GET (nbits, "G.nbits -> size in bits of element")
1070 GET (i, "G.i -> group identity")
1071 GET (g, "G.g -> group generator")
1072 GET (r, "G.r -> group order")
1073 GET (h, "G.h -> group cofactor")
1078 static PyMethodDef group_pymethods[] = {
1079 #define METHNAME(name) gmeth_##name
1081 G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1082 KWMETH(checkgroup, "G.checkgroup(rand = random): check group is good")
1087 static PyTypeObject group_pytype_skel = {
1088 PyObject_HEAD_INIT(0) 0, /* Header */
1089 "Group", /* @tp_name@ */
1090 sizeof(group_pyobj), /* @tp_basicsize@ */
1091 0, /* @tp_itemsize@ */
1093 group_pydealloc, /* @tp_dealloc@ */
1095 0, /* @tp_getattr@ */
1096 0, /* @tp_setattr@ */
1097 0, /* @tp_compare@ */
1099 0, /* @tp_as_number@ */
1100 0, /* @tp_as_sequence@ */
1101 0, /* @tp_as_mapping@ */
1105 0, /* @tp_getattro@ */
1106 0, /* @tp_setattro@ */
1107 0, /* @tp_as_buffer@ */
1108 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1109 Py_TPFLAGS_BASETYPE,
1112 "Abstract base class for groups.",
1114 0, /* @tp_traverse@ */
1116 group_pyrichcompare, /* @tp_richcompare@ */
1117 0, /* @tp_weaklistoffset@ */
1119 0, /* @tp_iternext@ */
1120 group_pymethods, /* @tp_methods@ */
1121 0, /* @tp_members@ */
1122 group_pygetset, /* @tp_getset@ */
1125 0, /* @tp_descr_get@ */
1126 0, /* @tp_descr_set@ */
1127 0, /* @tp_dictoffset@ */
1129 PyType_GenericAlloc, /* @tp_alloc@ */
1130 abstract_pynew, /* @tp_new@ */
1135 static PyObject *pgget_info(PyObject *me, void *hunoz)
1138 gctx_prime *gg = (gctx_prime *)GROUP_G(me);
1139 dp.p = MP_COPY(gg->mm.m);
1140 dp.q = MP_COPY(gg->g.r);
1141 dp.g = mpmont_reduce(&gg->mm, MP_NEW, gg->gen.x);
1142 return (fginfo_pywrap(&dp, dhinfo_pytype));
1145 static PyGetSetDef primegroup_pygetset[] = {
1146 #define GETSETNAME(op, name) pg##op##_##name
1147 GET (info, "G.info -> information about the group")
1152 static PyObject *primegroup_pynew(PyTypeObject *ty,
1153 PyObject *arg, PyObject *kw)
1156 char *kwlist[] = { "info", 0 };
1158 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", kwlist,
1161 return (group_dopywrap(ty, group_prime(FGINFO_DP(i))));
1164 static PyTypeObject primegroup_pytype_skel = {
1165 PyObject_HEAD_INIT(0) 0, /* Header */
1166 "PrimeGroup", /* @tp_name@ */
1167 sizeof(group_pyobj), /* @tp_basicsize@ */
1168 0, /* @tp_itemsize@ */
1170 group_pydealloc, /* @tp_dealloc@ */
1172 0, /* @tp_getattr@ */
1173 0, /* @tp_setattr@ */
1174 0, /* @tp_compare@ */
1176 0, /* @tp_as_number@ */
1177 0, /* @tp_as_sequence@ */
1178 0, /* @tp_as_mapping@ */
1182 0, /* @tp_getattro@ */
1183 0, /* @tp_setattro@ */
1184 0, /* @tp_as_buffer@ */
1185 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1186 Py_TPFLAGS_BASETYPE,
1189 "Subgroups of prime fields.",
1191 0, /* @tp_traverse@ */
1193 0, /* @tp_richcompare@ */
1194 0, /* @tp_weaklistoffset@ */
1196 0, /* @tp_iternext@ */
1197 0, /* @tp_methods@ */
1198 0, /* @tp_members@ */
1199 primegroup_pygetset, /* @tp_getset@ */
1202 0, /* @tp_descr_get@ */
1203 0, /* @tp_descr_set@ */
1204 0, /* @tp_dictoffset@ */
1206 PyType_GenericAlloc, /* @tp_alloc@ */
1207 primegroup_pynew, /* @tp_new@ */
1212 static PyObject *bgget_info(PyObject *me, void *hunoz)
1215 gctx_bin *gg = (gctx_bin *)GROUP_G(me);
1216 dp.p = MP_COPY(gg->r.p);
1217 dp.q = MP_COPY(gg->g.r);
1218 dp.g = MP_COPY(gg->gen.x);
1219 return (fginfo_pywrap(&dp, bindhinfo_pytype));
1222 static PyGetSetDef bingroup_pygetset[] = {
1223 #define GETSETNAME(op, name) bg##op##_##name
1224 GET (info, "G.info -> information about the group")
1229 static PyObject *bingroup_pynew(PyTypeObject *ty,
1230 PyObject *arg, PyObject *kw)
1233 char *kwlist[] = { "info", 0 };
1235 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", kwlist,
1236 bindhinfo_pytype, &i))
1238 return (group_dopywrap(ty, group_binary(FGINFO_DP(i))));
1241 static PyTypeObject bingroup_pytype_skel = {
1242 PyObject_HEAD_INIT(0) 0, /* Header */
1243 "BinGroup", /* @tp_name@ */
1244 sizeof(group_pyobj), /* @tp_basicsize@ */
1245 0, /* @tp_itemsize@ */
1247 group_pydealloc, /* @tp_dealloc@ */
1249 0, /* @tp_getattr@ */
1250 0, /* @tp_setattr@ */
1251 0, /* @tp_compare@ */
1253 0, /* @tp_as_number@ */
1254 0, /* @tp_as_sequence@ */
1255 0, /* @tp_as_mapping@ */
1259 0, /* @tp_getattro@ */
1260 0, /* @tp_setattro@ */
1261 0, /* @tp_as_buffer@ */
1262 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1263 Py_TPFLAGS_BASETYPE,
1266 "Subgroups of binary fields.",
1268 0, /* @tp_traverse@ */
1270 0, /* @tp_richcompare@ */
1271 0, /* @tp_weaklistoffset@ */
1273 0, /* @tp_iternext@ */
1274 0, /* @tp_methods@ */
1275 0, /* @tp_members@ */
1276 bingroup_pygetset, /* @tp_getset@ */
1279 0, /* @tp_descr_get@ */
1280 0, /* @tp_descr_set@ */
1281 0, /* @tp_dictoffset@ */
1283 PyType_GenericAlloc, /* @tp_alloc@ */
1284 bingroup_pynew, /* @tp_new@ */
1289 static PyObject *egget_info(PyObject *me, void *hunoz)
1292 gctx_ec *gg = (gctx_ec *)GROUP_G(me);
1294 ecinfo_copy(&ei, &gg->ei);
1295 return (ecinfo_pywrap(&ei));
1298 static PyGetSetDef ecgroup_pygetset[] = {
1299 #define GETSETNAME(op, name) eg##op##_##name
1300 GET (info, "G.info -> information about the group")
1305 static PyObject *ecgroup_pynew(PyTypeObject *ty,
1306 PyObject *arg, PyObject *kw)
1310 char *kwlist[] = { "info", 0 };
1312 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", kwlist,
1315 ecinfo_copy(&ei, ECINFO_EI(i));
1316 return (group_dopywrap(ty, group_ec(&ei)));
1319 static PyTypeObject ecgroup_pytype_skel = {
1320 PyObject_HEAD_INIT(0) 0, /* Header */
1321 "ECGroup", /* @tp_name@ */
1322 sizeof(group_pyobj), /* @tp_basicsize@ */
1323 0, /* @tp_itemsize@ */
1325 group_pydealloc, /* @tp_dealloc@ */
1327 0, /* @tp_getattr@ */
1328 0, /* @tp_setattr@ */
1329 0, /* @tp_compare@ */
1331 0, /* @tp_as_number@ */
1332 0, /* @tp_as_sequence@ */
1333 0, /* @tp_as_mapping@ */
1337 0, /* @tp_getattro@ */
1338 0, /* @tp_setattro@ */
1339 0, /* @tp_as_buffer@ */
1340 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1341 Py_TPFLAGS_BASETYPE,
1344 "Elliptic curve groups.",
1346 0, /* @tp_traverse@ */
1348 0, /* @tp_richcompare@ */
1349 0, /* @tp_weaklistoffset@ */
1351 0, /* @tp_iternext@ */
1352 0, /* @tp_methods@ */
1353 0, /* @tp_members@ */
1354 ecgroup_pygetset, /* @tp_getset@ */
1357 0, /* @tp_descr_get@ */
1358 0, /* @tp_descr_set@ */
1359 0, /* @tp_dictoffset@ */
1361 PyType_GenericAlloc, /* @tp_alloc@ */
1362 ecgroup_pynew, /* @tp_new@ */
1367 /*----- Global stuff ------------------------------------------------------*/
1369 static PyMethodDef methods[] = {
1370 #define METHNAME(name) meth_##name
1371 METH (_GE_frombuf, "frombuf(BUF) -> X, REST")
1372 METH (_GE_fromraw, "fromraw(BUF) -> X, REST")
1373 METH (_GE_fromstring, "fromstring(STR) -> X, REST")
1374 METH (_Group_parse, "parse(STR) -> G, REST")
1375 METH (_DHInfo_parse, "parse(STR) -> D, REST")
1376 METH (_BinDHInfo_parse, "parse(STR) -> D, REST")
1377 METH (_DHInfo__groupn, 0)
1378 METH (_BinDHInfo__groupn, 0)
1379 KWMETH(_DHInfo_generate, "\
1380 generate(PBITS, [qbits = 0, event = pgen_nullev,\n\
1381 rng = rand, nsteps = 0]) -> D")
1382 KWMETH(_DHInfo_genlimlee, "\
1383 genlimlee(PBITS, QBITS, [event = pgen_nullev, ievent = pgen_nullev,\n\
1384 rng = rand, nsteps = 0, subgroupp = True]) -> (D, [Q, ...])")
1385 KWMETH(_DHInfo_gendsa, "\
1386 gendsa(PBITS, QBITS, SEED, [event = pgen_nullev, nsteps = 0])\n\
1387 -> (D, SEED, COUNT)")
1388 KWMETH(_DHInfo_genkcdsa, "\
1389 gendsa(PBITS, QBITS, [event = pgen_nullev, rng = rand, nsteps = 0])\n\
1395 void group_pyinit(void)
1397 INITTYPE(fginfo, root);
1398 INITTYPE(dhinfo, fginfo);
1399 INITTYPE(bindhinfo, dhinfo);
1401 INITTYPE(group, type);
1402 INITTYPE(primegroup, group);
1403 INITTYPE(bingroup, group);
1404 INITTYPE(ecgroup, group);
1405 addmethods(methods);
1408 void group_pyinsert(PyObject *mod)
1410 INSERT("FGInfo", fginfo_pytype);
1411 INSERT("DHInfo", dhinfo_pytype);
1412 INSERT("BinDHInfo", bindhinfo_pytype);
1413 INSERT("GE", ge_pytype);
1414 INSERT("Group", group_pytype);
1415 INSERT("PrimeGroup", primegroup_pytype);
1416 INSERT("BinGroup", bingroup_pytype);
1417 INSERT("ECGroup", ecgroup_pytype);
1418 INSERT("_pgroups", namedgroups(ptab, &npgroups));
1419 INSERT("_bingroups", namedgroups(bintab, &nbingroups));
1422 /*----- That's all, folks -------------------------------------------------*/