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 static PyTypeObject *fginfo_pytype, *dhinfo_pytype, *bindhinfo_pytype;
35 typedef struct fginfo_pyobj {
40 #define FGINFO_DP(fg) (&((fginfo_pyobj *)(fg))->dp)
42 static PyObject *fginfo_pywrap(gprime_param *dp, PyTypeObject *ty)
44 fginfo_pyobj *z = PyObject_New(fginfo_pyobj, ty);
46 return ((PyObject *)z);
49 static PyObject *fginfo_pynew(PyTypeObject *ty,
50 PyObject *arg, PyObject *kw)
52 static const char *const kwlist[] = { "p", "r", "g", 0 };
53 gprime_param dp = { 0 };
56 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&O&:new", KWLIST,
61 z = PyObject_New(fginfo_pyobj, ty);
63 return ((PyObject *)z);
71 static PyObject *figet_r(PyObject *me, void *hunoz)
72 { return mp_pywrap(MP_COPY(FGINFO_DP(me)->q)); }
74 static PyObject *diget_p(PyObject *me, void *hunoz)
75 { return mp_pywrap(MP_COPY(FGINFO_DP(me)->p)); }
77 static PyObject *diget_g(PyObject *me, void *hunoz)
78 { return mp_pywrap(MP_COPY(FGINFO_DP(me)->g)); }
80 static PyObject *biget_p(PyObject *me, void *hunoz)
81 { return gf_pywrap(MP_COPY(FGINFO_DP(me)->p)); }
83 static PyObject *biget_m(PyObject *me, void *hunoz)
84 { return PyInt_FromLong(mp_octets(FGINFO_DP(me)->p) - 1); }
86 static PyObject *biget_g(PyObject *me, void *hunoz)
87 { return gf_pywrap(MP_COPY(FGINFO_DP(me)->g)); }
89 static void fginfo_pydealloc(PyObject *me)
91 mp_drop(FGINFO_DP(me)->p);
92 mp_drop(FGINFO_DP(me)->q);
93 mp_drop(FGINFO_DP(me)->g);
97 static PyObject *meth__DHInfo_generate(PyObject *me,
98 PyObject *arg, PyObject *kw)
103 grand *r = &rand_global;
104 struct excinfo exc = EXCINFO_INIT;
105 pypgev evt = { { 0 } };
106 static const char *const kwlist[] =
107 { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
111 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", KWLIST,
112 &me, convuint, &pl, convuint, &ql,
113 convpgev, &evt, convgrand, &r,
116 if (dh_gen(&dp, ql, pl, steps, r, evt.ev.proc, evt.ev.ctx))
118 rc = fginfo_pywrap(&dp, dhinfo_pytype);
124 static PyObject *meth__DHInfo_genlimlee(PyObject *me,
125 PyObject *arg, PyObject *kw)
130 grand *r = &rand_global;
131 struct excinfo exc = EXCINFO_INIT;
132 pypgev oe = { { 0 } }, ie = { { 0 } };
135 static const char *const kwlist[] = {
136 "class", "pbits", "qbits", "event", "ievent",
137 "rng", "nsteps", "subgroupp", 0
141 PyObject *rc = 0, *vec = 0;
143 oe.exc = ie.exc = &exc;
144 if (!PyArg_ParseTupleAndKeywords(arg, kw,
145 "OO&O&|O&O&O&O&O&:genlimlee", KWLIST,
146 &me, convuint, &pl, convuint, &ql,
147 convpgev, &oe, convpgev, &ie,
148 convgrand, &r, convuint, &steps,
149 convbool, &subgroupp))
151 if (subgroupp) f |= DH_SUBGROUP;
152 if (dh_limlee(&dp, ql, pl, f, steps, r,
153 oe.ev.proc, oe.ev.ctx, ie.ev.proc, ie.ev.ctx, &nf, &v))
155 vec = PyList_New(nf);
156 for (i = 0; i < nf; i++)
157 PyList_SET_ITEM(vec, i, mp_pywrap(v[i]));
159 rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype), vec);
161 droppgev(&oe); droppgev(&ie);
165 static PyObject *meth__DHInfo_genkcdsa(PyObject *me,
166 PyObject *arg, PyObject *kw)
171 grand *r = &rand_global;
172 struct excinfo exc = EXCINFO_INIT;
173 pypgev evt = { { 0 } };
174 static const char *const kwlist[] =
175 { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
180 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|O&O&O&:genkcdsa", KWLIST,
181 &me, convuint, &pl, convuint, &ql,
182 convpgev, &evt, convgrand, &r,
185 if (dh_kcdsagen(&dp, ql, pl, 0, steps, r, evt.ev.proc, evt.ev.ctx))
187 mp_div(&v, 0, dp.p, dp.q);
189 rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype),
196 static PyObject *meth__DHInfo_gendsa(PyObject *me,
197 PyObject *arg, PyObject *kw)
205 struct excinfo exc = EXCINFO_INIT;
206 pypgev evt = { { 0 } };
207 static const char *const kwlist[] =
208 { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 };
212 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&s#|O&O&:gendsa", KWLIST,
213 &me, convuint, &pl, convuint, &ql,
214 &k, &ksz, convpgev, &evt,
217 if (dsa_gen(&dp, ql, pl, steps, k, ksz, &ds, evt.ev.proc, evt.ev.ctx))
219 rc = Py_BuildValue("(NNl)", fginfo_pywrap(&dp, dhinfo_pytype),
220 bytestring_pywrap(ds.p, ds.sz), (long)ds.count);
227 static int npgroups = -1, nbingroups = -1;
229 static PyObject *namedgroups(const pentry *pp, int *ne)
236 for (i = 0; pp[i].name; i++) {
238 for (j = 0; j < i; j++) {
239 if (pp[i].data == pp[j].data) {
240 c = PyDict_GetItemString(d, (/*unconst*/ char *)pp[j].name);
245 c = PyInt_FromLong(i);
247 PyDict_SetItemString(d, (/*unconst*/ char *)p, c);
254 static PyObject *meth__groupn(PyObject *me, PyObject *arg,
255 PyTypeObject *ty, const pentry *pp, int ne)
261 if (!PyArg_ParseTuple(arg, "Oi:_groupn", &me, &i)) goto end;
262 if (i < 0 || i >= ne) VALERR("group index out of range");
263 dh_infofromdata(&gp, pp[i].data);
264 rc = fginfo_pywrap(&gp, ty);
269 static PyObject *meth__DHInfo__groupn(PyObject *me, PyObject *arg)
270 { return (meth__groupn(me, arg, dhinfo_pytype, ptab, npgroups)); }
272 static PyObject *meth__BinDHInfo__groupn(PyObject *me, PyObject *arg)
273 { return (meth__groupn(me, arg, bindhinfo_pytype, bintab, nbingroups)); }
275 static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty,
276 int (*parse)(qd_parse *, gprime_param *))
283 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
286 if (parse(&qd, &gp)) VALERR(qd.e);
287 rc = fginfo_pywrap(&gp, ty);
292 static PyObject *meth__DHInfo_parse(PyObject *me, PyObject *arg)
293 { return (meth__parse(me, arg, dhinfo_pytype, dh_parse)); }
295 static PyObject *meth__BinDHInfo_parse(PyObject *me, PyObject *arg)
296 { return (meth__parse(me, arg, bindhinfo_pytype, dhbin_parse)); }
298 static PyGetSetDef fginfo_pygetset[] = {
299 #define GETSETNAME(op, name) fi##op##_##name
300 GET (r, "I.r -> group order")
305 static PyGetSetDef dhinfo_pygetset[] = {
306 #define GETSETNAME(op, name) di##op##_##name
307 GET (p, "I.p -> prime")
308 GET (g, "I.g -> generator")
313 static PyGetSetDef bindhinfo_pygetset[] = {
314 #define GETSETNAME(op, name) bi##op##_##name
315 GET (p, "I.p -> irreducible polynomial")
316 GET (m, "I.m -> degree of polynomial")
317 GET (g, "I.g -> generator")
322 static PyTypeObject fginfo_pytype_skel = {
323 PyObject_HEAD_INIT(0) 0, /* Header */
324 "FGInfo", /* @tp_name@ */
325 sizeof(fginfo_pyobj), /* @tp_basicsize@ */
326 0, /* @tp_itemsize@ */
328 fginfo_pydealloc, /* @tp_dealloc@ */
330 0, /* @tp_getattr@ */
331 0, /* @tp_setattr@ */
332 0, /* @tp_compare@ */
334 0, /* @tp_as_number@ */
335 0, /* @tp_as_sequence@ */
336 0, /* @tp_as_mapping@ */
340 0, /* @tp_getattro@ */
341 0, /* @tp_setattro@ */
342 0, /* @tp_as_buffer@ */
343 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
347 "Abstract base class for field-group information objects.",
349 0, /* @tp_traverse@ */
351 0, /* @tp_richcompare@ */
352 0, /* @tp_weaklistoffset@ */
354 0, /* @tp_iternext@ */
355 0, /* @tp_methods@ */
356 0, /* @tp_members@ */
357 fginfo_pygetset, /* @tp_getset@ */
360 0, /* @tp_descr_get@ */
361 0, /* @tp_descr_set@ */
362 0, /* @tp_dictoffset@ */
364 PyType_GenericAlloc, /* @tp_alloc@ */
365 abstract_pynew, /* @tp_new@ */
370 static PyTypeObject dhinfo_pytype_skel = {
371 PyObject_HEAD_INIT(0) 0, /* Header */
372 "DHInfo", /* @tp_name@ */
373 sizeof(fginfo_pyobj), /* @tp_basicsize@ */
374 0, /* @tp_itemsize@ */
376 fginfo_pydealloc, /* @tp_dealloc@ */
378 0, /* @tp_getattr@ */
379 0, /* @tp_setattr@ */
380 0, /* @tp_compare@ */
382 0, /* @tp_as_number@ */
383 0, /* @tp_as_sequence@ */
384 0, /* @tp_as_mapping@ */
388 0, /* @tp_getattro@ */
389 0, /* @tp_setattro@ */
390 0, /* @tp_as_buffer@ */
391 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
395 "DHInfo(P, R, G): standard (integer) Diffie-Hellman group information.",
397 0, /* @tp_traverse@ */
399 0, /* @tp_richcompare@ */
400 0, /* @tp_weaklistoffset@ */
402 0, /* @tp_iternext@ */
403 0, /* @tp_methods@ */
404 0, /* @tp_members@ */
405 dhinfo_pygetset, /* @tp_getset@ */
408 0, /* @tp_descr_get@ */
409 0, /* @tp_descr_set@ */
410 0, /* @tp_dictoffset@ */
412 PyType_GenericAlloc, /* @tp_alloc@ */
413 fginfo_pynew, /* @tp_new@ */
418 static PyTypeObject bindhinfo_pytype_skel = {
419 PyObject_HEAD_INIT(0) 0, /* Header */
420 "BinDHInfo", /* @tp_name@ */
421 sizeof(fginfo_pyobj), /* @tp_basicsize@ */
422 0, /* @tp_itemsize@ */
424 fginfo_pydealloc, /* @tp_dealloc@ */
426 0, /* @tp_getattr@ */
427 0, /* @tp_setattr@ */
428 0, /* @tp_compare@ */
430 0, /* @tp_as_number@ */
431 0, /* @tp_as_sequence@ */
432 0, /* @tp_as_mapping@ */
436 0, /* @tp_getattro@ */
437 0, /* @tp_setattro@ */
438 0, /* @tp_as_buffer@ */
439 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
443 "BinDHInfo(P, R, G): binary-field Diffie-Hellman group information.",
445 0, /* @tp_traverse@ */
447 0, /* @tp_richcompare@ */
448 0, /* @tp_weaklistoffset@ */
450 0, /* @tp_iternext@ */
451 0, /* @tp_methods@ */
452 0, /* @tp_members@ */
453 bindhinfo_pygetset, /* @tp_getset@ */
456 0, /* @tp_descr_get@ */
457 0, /* @tp_descr_set@ */
458 0, /* @tp_dictoffset@ */
460 PyType_GenericAlloc, /* @tp_alloc@ */
461 fginfo_pynew, /* @tp_new@ */
466 /*----- General utilities -------------------------------------------------*/
468 PyTypeObject *ge_pytype, *group_pytype;
469 static PyTypeObject *primegroup_pytype, *bingroup_pytype, *ecgroup_pytype;
471 group *group_copy(group *g)
473 if (STRCMP(G_NAME(g), ==, "prime")) {
474 gctx_prime *gc = (gctx_prime *)g;
476 gp.g = G_TOINT(g, MP_NEW, g->g);
479 g = group_prime(&gp);
481 } else if (STRCMP(G_NAME(g), ==, "bin")) {
482 gctx_bin *gc = (gctx_bin *)g;
484 gb.g = G_TOINT(g, MP_NEW, g->g);
487 g = group_binary(&gb);
489 } else if (STRCMP(G_NAME(g), ==, "ec")) {
490 gctx_ec *gc = (gctx_ec *)g;
492 if ((ei.c = eccurve_copy(gc->ei.c)) == 0)
495 EC_COPY(&ei.g, &gc->ei.g);
496 ei.r = MP_COPY(gc->ei.r);
497 ei.h = MP_COPY(gc->ei.h);
504 PyObject *ge_pywrap(PyObject *gobj, ge *x)
506 ge_pyobj *z = PyObject_New(ge_pyobj, (PyTypeObject *)gobj);
508 z->g = GROUP_G(gobj);
510 return ((PyObject *)z);
513 static PyObject *ge_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
515 static const char *const kwlist[] = { "x", 0 };
524 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", KWLIST, &x)) goto end;
526 if (ECPT_PYCHECK(x)) {
528 if (G_FROMEC(g, xx, &p))
529 TYERR("can't convert from elliptic curve point");
531 } else if ((y = tomp(x)) != 0) {
532 if (G_FROMINT(g, xx, y))
533 TYERR("can't convert from integer");
535 } else if (PyString_Check(x)) {
536 sc.buf = PyString_AS_STRING(x);
537 sc.lim = sc.buf + PyString_GET_SIZE(x);
538 if (G_READ(g, xx, &mptext_stringops, &sc) || sc.buf < sc.lim)
539 VALERR("malformed group element string");
541 TYERR("can't convert to group element");
542 return (ge_pywrap((PyObject *)ty, xx));
546 if (xx) G_DESTROY(g, xx);
550 static PyObject *group_dopywrap(PyTypeObject *ty, group *g)
552 group_pyobj *gobj = newtype(ty, 0, g->ops->name);
554 gobj->ty.ht_type.tp_basicsize = sizeof(ge_pyobj);
555 gobj->ty.ht_type.tp_base = ge_pytype;
556 Py_INCREF(group_pytype);
557 gobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
558 Py_TPFLAGS_BASETYPE |
559 Py_TPFLAGS_CHECKTYPES |
560 Py_TPFLAGS_HEAPTYPE);
561 gobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
562 gobj->ty.ht_type.tp_free = 0;
563 gobj->ty.ht_type.tp_new = ge_pynew;
564 typeready(&gobj->ty.ht_type);
565 return ((PyObject *)gobj);
568 PyObject *group_pywrap(group *g)
572 if (STRCMP(G_NAME(g), ==, "prime")) ty = primegroup_pytype;
573 else if (STRCMP(G_NAME(g), ==, "bin")) ty = bingroup_pytype;
574 else if (STRCMP(G_NAME(g), ==, "ec")) ty = ecgroup_pytype;
576 return (group_dopywrap(ty, g));
579 /*----- Group elements ----------------------------------------------------*/
581 #define BINOP(name) \
582 static PyObject *ge_py##name(PyObject *x, PyObject *y) \
586 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \
587 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \
591 g->ops->name(g, z, GE_X(x), GE_X(y)); \
592 return (ge_pywrap(GE_GOBJ(x), z)); \
599 static PyObject *gemeth_##name(PyObject *me, PyObject *arg) \
603 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
606 g->ops->name(g, z, GE_X(me)); \
607 return (ge_pywrap(GE_GOBJ(me), z)); \
613 static PyObject *ge_pyexp(PyObject *x, PyObject *n, PyObject *m)
618 if (m != Py_None || !GE_PYCHECK(x) || (nn = getmp(n)) == 0)
620 z = G_CREATE(GE_G(x));
621 G_EXP(GE_G(x), z, GE_X(x), nn);
623 return (ge_pywrap(GE_GOBJ(x), z));
626 static void ge_pydealloc(PyObject *me)
628 G_DESTROY(GE_G(me), GE_X(me));
629 Py_DECREF(GE_GOBJ(me));
633 static void group_pydealloc(PyObject *me)
635 G_DESTROYGROUP(GROUP_G(me));
636 PyType_Type.tp_dealloc(me);
639 static PyObject *gmexp_id(PyObject *me)
641 group *g = GROUP_G(me); ge *x = G_CREATE(g);
642 G_COPY(g, x, g->i); return (ge_pywrap(me, x));
645 static int gmexp_fill(void *pp, PyObject *me, PyObject *x, PyObject *m)
647 group_expfactor *f = pp;
649 if (!GE_PYCHECK(x) || GE_G(x) != GROUP_G(me) || (f->exp = getmp(m)) == 0)
655 static PyObject *ge_pyrichcompare(PyObject *x, PyObject *y, int op)
660 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) ||
661 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y))))
664 case Py_EQ: b = G_EQ(GE_G(x), GE_X(x), GE_X(y)); break;
665 case Py_NE: b = !G_EQ(GE_G(x), GE_X(x), GE_X(y)); break;
666 default: TYERR("group elements are unordered");
673 static PyObject *gemeth_check(PyObject *me, PyObject *arg)
675 if (!PyArg_ParseTuple(arg, ":check")) goto end;
676 if (group_check(GE_G(me), GE_X(me))) VALERR("bad group element");
682 static int ge_pynonzerop(PyObject *x)
683 { return (!G_IDENTP(GE_G(x), GE_X(x))); }
685 static PyObject *ge_pystr(PyObject *me)
690 group_writedstr(GE_G(me), GE_X(me), &d);
691 rc = PyString_FromStringAndSize(d.buf, d.len);
696 static PyObject *ge_pylong(PyObject *me)
701 if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
702 TYERR("can't convert to integer");
709 static PyObject *ge_pyint(PyObject *me)
715 if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
716 TYERR("can't convert to integer");
717 if (!mp_tolong_checked(x, &l, 0)) rc = PyInt_FromLong(l);
718 else rc = mp_topylong(x);
724 static PyObject *gemeth_toint(PyObject *me, PyObject *arg)
728 if (!PyArg_ParseTuple(arg, ":toint")) goto end;
729 if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0)
730 TYERR("can't convert to integer");
731 return (mp_pywrap(x));
736 static PyObject *gemeth_toec(PyObject *me, PyObject *arg, PyObject *kw)
738 static const char *const kwlist[] = { "curve", 0 };
739 PyTypeObject *cty = 0;
745 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:toec", KWLIST,
747 g = GROUP_G(GE_GOBJ(me));
749 if (!PyType_Check(cty) || !PyType_IsSubtype(cty, ecpt_pytype))
750 TYERR("want subtype of catacomb.ECPt");
751 Py_INCREF((PyObject *)cty);
752 } else if (STRCMP(G_NAME(g), ==, "ec")) {
753 c = eccurve_copy(((gctx_ec *)g)->ei.c);
754 cty = (PyTypeObject *)eccurve_pywrap(0, c);
757 Py_INCREF((PyObject *)cty);
759 if (G_TOEC(GE_G(me), &p, GE_X(me))) {
760 Py_DECREF((PyObject *)cty);
761 TYERR("can't convert to ec point");
763 rc = ecpt_pywrapout(cty, &p);
764 Py_DECREF((PyObject *)cty);
769 static PyObject *gemeth_tobuf(PyObject *me, PyObject *arg)
775 if (!PyArg_ParseTuple(arg, ":tobuf")) return (0);
776 n = GE_G(me)->noctets + 4;
777 rc = bytestring_pywrap(0, n);
778 buf_init(&b, PyString_AS_STRING(rc), n);
779 G_TOBUF(GE_G(me), &b, GE_X(me));
781 _PyString_Resize(&rc, BLEN(&b));
785 static PyObject *gemeth_toraw(PyObject *me, PyObject *arg)
791 if (!PyArg_ParseTuple(arg, ":toraw")) return (0);
792 n = GE_G(me)->noctets;
793 rc = bytestring_pywrap(0, n);
794 buf_init(&b, PyString_AS_STRING(rc), n);
795 G_TORAW(GE_G(me), &b, GE_X(me));
797 _PyString_Resize(&rc, BLEN(&b));
801 static PyObject *gmexp_exp(PyObject *me, void *pp, int n)
803 ge *z = G_CREATE(GROUP_G(me));
804 G_MEXP(GROUP_G(me), z, pp, n);
805 return (ge_pywrap(me, z));
808 static void gmexp_drop(void *pp)
810 group_expfactor *f = pp;
814 static PyObject *gmeth_mexp(PyObject *me, PyObject *arg)
816 return (mexp_common(me, arg, sizeof(group_expfactor),
817 gmexp_id, gmexp_fill, gmexp_exp, gmexp_drop));
820 static PyObject *gmeth_checkgroup(PyObject *me, PyObject *arg, PyObject *kw)
822 static const char *const kwlist[] = { "rng", 0 };
823 grand *r = &rand_global;
826 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:checkgroup", KWLIST,
829 if ((p = G_CHECK(GROUP_G(me), r)) != 0)
836 static PyObject *group_pyrichcompare(PyObject *x, PyObject *y, int op)
838 int b = group_samep(GROUP_G(x), GROUP_G(y));
842 default: TYERR("can't order groups");
849 static PyObject *meth__GE_frombuf(PyObject *me, PyObject *arg)
857 if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &n))
862 if (G_FROMBUF(g, &b, x))
863 VALERR("invalid data");
864 return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
866 if (x) G_DESTROY(g, x);
870 static PyObject *meth__GE_fromraw(PyObject *me, PyObject *arg)
878 if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &n))
883 if (G_FROMRAW(g, &b, x))
884 VALERR("invalid data");
885 return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
887 if (x) G_DESTROY(g, x);
891 static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg)
899 if (!PyArg_ParseTuple(arg, "Os#:fromstring", &me, &p, &n))
905 if (G_READ(g, x, &mptext_stringops, &sc))
906 VALERR("bad group element string");
907 return (Py_BuildValue("(Ns#)", ge_pywrap(me, x),
908 sc.buf, (Py_ssize_t)(sc.lim - sc.buf)));
910 if (x) G_DESTROY(g, x);
914 static PyObject *meth__Group_parse(PyObject *me, PyObject *arg)
920 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
924 if ((g = group_parse(&qd)) == 0)
926 return (group_pywrap(g));
931 static PyObject *geget_group(PyObject *me, void *hunoz)
932 { RETURN_OBJ(GE_GOBJ(me)); }
934 static PyObject *gget_nbits(PyObject *me, void *hunoz)
935 { return (PyInt_FromLong(GROUP_G(me)->nbits)); }
937 static PyObject *gget_noctets(PyObject *me, void *hunoz)
938 { return (PyInt_FromLong(GROUP_G(me)->noctets)); }
940 static PyObject *gget_i(PyObject *me, void *hunoz)
942 group *g = GROUP_G(me); ge *x = G_CREATE(g);
943 G_COPY(g, x, g->i); return (ge_pywrap(me, x));
946 static PyObject *gget_g(PyObject *me, void *hunoz)
948 group *g = GROUP_G(me); ge *x = G_CREATE(g);
949 G_COPY(g, x, g->g); return (ge_pywrap(me, x));
952 static long ge_pyhash(PyObject *me)
955 size_t sz = GE_G(me)->noctets + 4;
956 uint32 h = 0xf672c776 + GE_G(me)->ops->ty;
957 octet *p = xmalloc(sz);
959 G_TOBUF(GE_G(me), &b, GE_X(me));
961 h = unihash_hash(&unihash_global, h, BBASE(&b), BLEN(&b));
963 return (h % LONG_MAX);
966 static PyObject *gget_r(PyObject *me, void *hunoz)
967 { return (mp_pywrap(MP_COPY(GROUP_G(me)->r))); }
969 static PyObject *gget_h(PyObject *me, void *hunoz)
970 { return (mp_pywrap(MP_COPY(GROUP_G(me)->h))); }
972 static PyGetSetDef ge_pygetset[] = {
973 #define GETSETNAME(op, name) ge##op##_##name
974 GET (group, "X.group -> group containing X")
979 static PyMethodDef ge_pymethods[] = {
980 #define METHNAME(name) gemeth_##name
981 METH (inv, "X.inv() -> inverse element of X")
982 METH (sqr, "X.sqr() -> X^2 = X * X")
983 METH (check, "X.check() -> check X really belongs to its group")
984 METH (toint, "X.toint() -> X converted to an integer")
985 KWMETH(toec, "X.toec([curve = ECPt]) -> "
986 "X converted to elliptic curve point")
987 METH (tobuf, "X.tobuf() -> X in buffer representation")
988 METH (toraw, "X.toraw() -> X in raw representation")
993 static PyNumberMethods ge_pynumber = {
995 0, /* @nb_subtract@ */
996 ge_pymul, /* @nb_multiply@ */
997 ge_pydiv, /* @nb_divide@ */
998 0, /* @nb_remainder@ */
1000 ge_pyexp, /* @nb_power@ */
1001 0, /* @nb_negative@ */
1002 0, /* @nb_positive@ */
1003 0, /* @nb_absolute@ */
1004 ge_pynonzerop, /* @nb_nonzero@ */
1005 0, /* @nb_invert@ */
1006 0, /* @nb_lshift@ */
1007 0, /* @nb_rshift@ */
1011 0, /* @nb_coerce@ */
1012 ge_pyint, /* @nb_int@ */
1013 ge_pylong, /* @nb_long@ */
1014 0 /* meaningless */, /* @nb_float@ */
1018 0, /* @nb_inplace_add@ */
1019 0, /* @nb_inplace_subtract@ */
1020 0, /* @nb_inplace_multiply@ */
1021 0, /* @nb_inplace_divide@ */
1022 0, /* @nb_inplace_remainder@ */
1023 0, /* @nb_inplace_power@ */
1024 0, /* @nb_inplace_lshift@ */
1025 0, /* @nb_inplace_rshift@ */
1026 0, /* @nb_inplace_and@ */
1027 0, /* @nb_inplace_xor@ */
1028 0, /* @nb_inplace_or@ */
1030 0, /* @nb_floor_divide@ */
1031 ge_pydiv, /* @nb_true_divide@ */
1032 0, /* @nb_inplace_floor_divide@ */
1033 0, /* @nb_inplace_true_divide@ */
1036 static PyTypeObject ge_pytype_skel = {
1037 PyObject_HEAD_INIT(0) 0, /* Header */
1038 "GE", /* @tp_name@ */
1039 sizeof(ge_pyobj), /* @tp_basicsize@ */
1040 0, /* @tp_itemsize@ */
1042 ge_pydealloc, /* @tp_dealloc@ */
1044 0, /* @tp_getattr@ */
1045 0, /* @tp_setattr@ */
1046 0, /* @tp_compare@ */
1048 &ge_pynumber, /* @tp_as_number@ */
1049 0, /* @tp_as_sequence@ */
1050 0, /* @tp_as_mapping@ */
1051 ge_pyhash, /* @tp_hash@ */
1053 ge_pystr, /* @tp_str@ */
1054 0, /* @tp_getattro@ */
1055 0, /* @tp_setattro@ */
1056 0, /* @tp_as_buffer@ */
1057 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1058 Py_TPFLAGS_CHECKTYPES |
1059 Py_TPFLAGS_BASETYPE,
1062 "Group elements, abstract base class.",
1064 0, /* @tp_traverse@ */
1066 ge_pyrichcompare, /* @tp_richcompare@ */
1067 0, /* @tp_weaklistoffset@ */
1069 0, /* @tp_iternext@ */
1070 ge_pymethods, /* @tp_methods@ */
1071 0, /* @tp_members@ */
1072 ge_pygetset, /* @tp_getset@ */
1075 0, /* @tp_descr_get@ */
1076 0, /* @tp_descr_set@ */
1077 0, /* @tp_dictoffset@ */
1079 PyType_GenericAlloc, /* @tp_alloc@ */
1080 abstract_pynew, /* @tp_new@ */
1085 static PyGetSetDef group_pygetset[] = {
1086 #define GETSETNAME(op, name) g##op##_##name
1087 GET (noctets, "G.noctets -> size in octets of element")
1088 GET (nbits, "G.nbits -> size in bits of element")
1089 GET (i, "G.i -> group identity")
1090 GET (g, "G.g -> group generator")
1091 GET (r, "G.r -> group order")
1092 GET (h, "G.h -> group cofactor")
1097 static PyMethodDef group_pymethods[] = {
1098 #define METHNAME(name) gmeth_##name
1099 METH (mexp, "G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1100 KWMETH(checkgroup, "G.checkgroup([rng = rand]): check group is good")
1105 static PyTypeObject group_pytype_skel = {
1106 PyObject_HEAD_INIT(0) 0, /* Header */
1107 "Group", /* @tp_name@ */
1108 sizeof(group_pyobj), /* @tp_basicsize@ */
1109 0, /* @tp_itemsize@ */
1111 group_pydealloc, /* @tp_dealloc@ */
1113 0, /* @tp_getattr@ */
1114 0, /* @tp_setattr@ */
1115 0, /* @tp_compare@ */
1117 0, /* @tp_as_number@ */
1118 0, /* @tp_as_sequence@ */
1119 0, /* @tp_as_mapping@ */
1123 0, /* @tp_getattro@ */
1124 0, /* @tp_setattro@ */
1125 0, /* @tp_as_buffer@ */
1126 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1127 Py_TPFLAGS_BASETYPE,
1130 "Abstract base class for groups.",
1132 0, /* @tp_traverse@ */
1134 group_pyrichcompare, /* @tp_richcompare@ */
1135 0, /* @tp_weaklistoffset@ */
1137 0, /* @tp_iternext@ */
1138 group_pymethods, /* @tp_methods@ */
1139 0, /* @tp_members@ */
1140 group_pygetset, /* @tp_getset@ */
1143 0, /* @tp_descr_get@ */
1144 0, /* @tp_descr_set@ */
1145 0, /* @tp_dictoffset@ */
1147 PyType_GenericAlloc, /* @tp_alloc@ */
1148 abstract_pynew, /* @tp_new@ */
1153 static PyObject *pgget_info(PyObject *me, void *hunoz)
1156 gctx_prime *gg = (gctx_prime *)GROUP_G(me);
1157 dp.p = MP_COPY(gg->mm.m);
1158 dp.q = MP_COPY(gg->g.r);
1159 dp.g = mpmont_reduce(&gg->mm, MP_NEW, gg->gen.x);
1160 return (fginfo_pywrap(&dp, dhinfo_pytype));
1163 static PyGetSetDef primegroup_pygetset[] = {
1164 #define GETSETNAME(op, name) pg##op##_##name
1165 GET (info, "G.info -> information about the group")
1170 static PyObject *primegroup_pynew(PyTypeObject *ty,
1171 PyObject *arg, PyObject *kw)
1174 static const char *const kwlist[] = { "info", 0 };
1176 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
1179 return (group_dopywrap(ty, group_prime(FGINFO_DP(i))));
1182 static PyTypeObject primegroup_pytype_skel = {
1183 PyObject_HEAD_INIT(0) 0, /* Header */
1184 "PrimeGroup", /* @tp_name@ */
1185 sizeof(group_pyobj), /* @tp_basicsize@ */
1186 0, /* @tp_itemsize@ */
1188 group_pydealloc, /* @tp_dealloc@ */
1190 0, /* @tp_getattr@ */
1191 0, /* @tp_setattr@ */
1192 0, /* @tp_compare@ */
1194 0, /* @tp_as_number@ */
1195 0, /* @tp_as_sequence@ */
1196 0, /* @tp_as_mapping@ */
1200 0, /* @tp_getattro@ */
1201 0, /* @tp_setattro@ */
1202 0, /* @tp_as_buffer@ */
1203 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1204 Py_TPFLAGS_BASETYPE,
1207 "PrimeGroup(INFO): subgroups of prime fields.",
1209 0, /* @tp_traverse@ */
1211 0, /* @tp_richcompare@ */
1212 0, /* @tp_weaklistoffset@ */
1214 0, /* @tp_iternext@ */
1215 0, /* @tp_methods@ */
1216 0, /* @tp_members@ */
1217 primegroup_pygetset, /* @tp_getset@ */
1220 0, /* @tp_descr_get@ */
1221 0, /* @tp_descr_set@ */
1222 0, /* @tp_dictoffset@ */
1224 PyType_GenericAlloc, /* @tp_alloc@ */
1225 primegroup_pynew, /* @tp_new@ */
1230 static PyObject *bgget_info(PyObject *me, void *hunoz)
1233 gctx_bin *gg = (gctx_bin *)GROUP_G(me);
1234 dp.p = MP_COPY(gg->r.p);
1235 dp.q = MP_COPY(gg->g.r);
1236 dp.g = MP_COPY(gg->gen.x);
1237 return (fginfo_pywrap(&dp, bindhinfo_pytype));
1240 static PyGetSetDef bingroup_pygetset[] = {
1241 #define GETSETNAME(op, name) bg##op##_##name
1242 GET (info, "G.info -> information about the group")
1247 static PyObject *bingroup_pynew(PyTypeObject *ty,
1248 PyObject *arg, PyObject *kw)
1251 static const char *const kwlist[] = { "info", 0 };
1253 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
1254 bindhinfo_pytype, &i))
1256 return (group_dopywrap(ty, group_binary(FGINFO_DP(i))));
1259 static PyTypeObject bingroup_pytype_skel = {
1260 PyObject_HEAD_INIT(0) 0, /* Header */
1261 "BinGroup", /* @tp_name@ */
1262 sizeof(group_pyobj), /* @tp_basicsize@ */
1263 0, /* @tp_itemsize@ */
1265 group_pydealloc, /* @tp_dealloc@ */
1267 0, /* @tp_getattr@ */
1268 0, /* @tp_setattr@ */
1269 0, /* @tp_compare@ */
1271 0, /* @tp_as_number@ */
1272 0, /* @tp_as_sequence@ */
1273 0, /* @tp_as_mapping@ */
1277 0, /* @tp_getattro@ */
1278 0, /* @tp_setattro@ */
1279 0, /* @tp_as_buffer@ */
1280 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1281 Py_TPFLAGS_BASETYPE,
1284 "BinGroup(INFO): subgroups of binary fields.",
1286 0, /* @tp_traverse@ */
1288 0, /* @tp_richcompare@ */
1289 0, /* @tp_weaklistoffset@ */
1291 0, /* @tp_iternext@ */
1292 0, /* @tp_methods@ */
1293 0, /* @tp_members@ */
1294 bingroup_pygetset, /* @tp_getset@ */
1297 0, /* @tp_descr_get@ */
1298 0, /* @tp_descr_set@ */
1299 0, /* @tp_dictoffset@ */
1301 PyType_GenericAlloc, /* @tp_alloc@ */
1302 bingroup_pynew, /* @tp_new@ */
1307 static PyObject *egget_info(PyObject *me, void *hunoz)
1310 gctx_ec *gg = (gctx_ec *)GROUP_G(me);
1312 ecinfo_copy(&ei, &gg->ei);
1313 return (ecinfo_pywrap(&ei));
1316 static PyGetSetDef ecgroup_pygetset[] = {
1317 #define GETSETNAME(op, name) eg##op##_##name
1318 GET (info, "G.info -> information about the group")
1323 static PyObject *ecgroup_pynew(PyTypeObject *ty,
1324 PyObject *arg, PyObject *kw)
1328 static const char *const kwlist[] = { "info", 0 };
1330 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", KWLIST,
1333 ecinfo_copy(&ei, ECINFO_EI(i));
1334 return (group_dopywrap(ty, group_ec(&ei)));
1337 static PyTypeObject ecgroup_pytype_skel = {
1338 PyObject_HEAD_INIT(0) 0, /* Header */
1339 "ECGroup", /* @tp_name@ */
1340 sizeof(group_pyobj), /* @tp_basicsize@ */
1341 0, /* @tp_itemsize@ */
1343 group_pydealloc, /* @tp_dealloc@ */
1345 0, /* @tp_getattr@ */
1346 0, /* @tp_setattr@ */
1347 0, /* @tp_compare@ */
1349 0, /* @tp_as_number@ */
1350 0, /* @tp_as_sequence@ */
1351 0, /* @tp_as_mapping@ */
1355 0, /* @tp_getattro@ */
1356 0, /* @tp_setattro@ */
1357 0, /* @tp_as_buffer@ */
1358 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1359 Py_TPFLAGS_BASETYPE,
1362 "ECGroup(INFO): elliptic curve groups.",
1364 0, /* @tp_traverse@ */
1366 0, /* @tp_richcompare@ */
1367 0, /* @tp_weaklistoffset@ */
1369 0, /* @tp_iternext@ */
1370 0, /* @tp_methods@ */
1371 0, /* @tp_members@ */
1372 ecgroup_pygetset, /* @tp_getset@ */
1375 0, /* @tp_descr_get@ */
1376 0, /* @tp_descr_set@ */
1377 0, /* @tp_dictoffset@ */
1379 PyType_GenericAlloc, /* @tp_alloc@ */
1380 ecgroup_pynew, /* @tp_new@ */
1385 /*----- Global stuff ------------------------------------------------------*/
1387 static PyMethodDef methods[] = {
1388 #define METHNAME(name) meth_##name
1389 METH (_GE_frombuf, "frombuf(BUF) -> X, REST")
1390 METH (_GE_fromraw, "fromraw(BUF) -> X, REST")
1391 METH (_GE_fromstring, "fromstring(STR) -> X, REST")
1392 METH (_Group_parse, "parse(STR) -> G, REST")
1393 METH (_DHInfo_parse, "parse(STR) -> D, REST")
1394 METH (_BinDHInfo_parse, "parse(STR) -> D, REST")
1395 METH (_DHInfo__groupn, 0)
1396 METH (_BinDHInfo__groupn, 0)
1397 KWMETH(_DHInfo_generate,
1398 "generate(PBITS, [qbits = 0, event = pgen_nullev,\n"
1399 " rng = rand, nsteps = 0]) -> D")
1400 KWMETH(_DHInfo_genlimlee,
1401 "genlimlee(PBITS, QBITS, [event = pgen_nullev], "
1402 "[ievent = pgen_nullev],\n"
1403 " [rng = rand], [nsteps = 0], [subgroupp = True]) "
1405 KWMETH(_DHInfo_gendsa,
1406 "gendsa(PBITS, QBITS, SEED, [event = pgen_nullev], [nsteps = 0])\n"
1407 " -> (D, SEED, COUNT)")
1408 KWMETH(_DHInfo_genkcdsa,
1409 "gendsa(PBITS, QBITS, [event = pgen_nullev], "
1410 "[rng = rand], [nsteps = 0])\n"
1416 void group_pyinit(void)
1418 INITTYPE(fginfo, root);
1419 INITTYPE(dhinfo, fginfo);
1420 INITTYPE(bindhinfo, dhinfo);
1422 INITTYPE(group, type);
1423 INITTYPE(primegroup, group);
1424 INITTYPE(bingroup, group);
1425 INITTYPE(ecgroup, group);
1426 addmethods(methods);
1429 void group_pyinsert(PyObject *mod)
1431 INSERT("FGInfo", fginfo_pytype);
1432 INSERT("DHInfo", dhinfo_pytype);
1433 INSERT("BinDHInfo", bindhinfo_pytype);
1434 INSERT("GE", ge_pytype);
1435 INSERT("Group", group_pytype);
1436 INSERT("PrimeGroup", primegroup_pytype);
1437 INSERT("BinGroup", bingroup_pytype);
1438 INSERT("ECGroup", ecgroup_pytype);
1439 INSERT("_pgroups", namedgroups(ptab, &npgroups));
1440 INSERT("_bingroups", namedgroups(bintab, &nbingroups));
1443 /*----- That's all, folks -------------------------------------------------*/