5 * Abstract group inteface
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Python interface to Catacomb.
14 * Catacomb/Python is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Catacomb/Python is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Catacomb/Python; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
31 #include "catacomb-python.h"
33 /*----- DH and binary group infos -----------------------------------------*/
35 PyObject
*fginfo_pywrap(gprime_param
*dp
, PyTypeObject
*ty
)
37 fginfo_pyobj
*z
= PyObject_New(fginfo_pyobj
, ty
);
39 return ((PyObject
*)z
);
42 static PyObject
*fginfo_pynew(PyTypeObject
*ty
,
43 PyObject
*arg
, PyObject
*kw
)
45 char *kwlist
[] = { "p", "r", "g", 0 };
46 gprime_param dp
= { 0 };
49 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&O&:new", kwlist
,
54 z
= PyObject_New(fginfo_pyobj
, ty
);
56 return ((PyObject
*)z
);
64 static PyObject
*figet_r(PyObject
*me
, void *hunoz
)
65 { return mp_pywrap(MP_COPY(FGINFO_DP(me
)->q
)); }
67 static PyObject
*diget_p(PyObject
*me
, void *hunoz
)
68 { return mp_pywrap(MP_COPY(FGINFO_DP(me
)->p
)); }
70 static PyObject
*diget_g(PyObject
*me
, void *hunoz
)
71 { return mp_pywrap(MP_COPY(FGINFO_DP(me
)->g
)); }
73 static PyObject
*biget_p(PyObject
*me
, void *hunoz
)
74 { return gf_pywrap(MP_COPY(FGINFO_DP(me
)->p
)); }
76 static PyObject
*biget_m(PyObject
*me
, void *hunoz
)
77 { return PyInt_FromLong(mp_octets(FGINFO_DP(me
)->p
) - 1); }
79 static PyObject
*biget_g(PyObject
*me
, void *hunoz
)
80 { return gf_pywrap(MP_COPY(FGINFO_DP(me
)->g
)); }
82 static void fginfo_pydealloc(PyObject
*me
)
84 mp_drop(FGINFO_DP(me
)->p
);
85 mp_drop(FGINFO_DP(me
)->q
);
86 mp_drop(FGINFO_DP(me
)->g
);
90 static PyObject
*meth__DHInfo_generate(PyObject
*me
,
91 PyObject
*arg
, PyObject
*kw
)
96 grand
*r
= &rand_global
;
99 { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
102 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&|O&O&O&O&:generate", kwlist
,
103 &me
, convuint
, &pl
, convuint
, &ql
,
104 convpgev
, &evt
, convgrand
, &r
,
107 if (dh_gen(&dp
, ql
, pl
, steps
, r
, evt
.proc
, evt
.ctx
))
109 rc
= fginfo_pywrap(&dp
, dhinfo_pytype
);
115 static PyObject
*meth__DHInfo_genlimlee(PyObject
*me
,
116 PyObject
*arg
, PyObject
*kw
)
121 grand
*r
= &rand_global
;
122 pgev oe
= { 0 }, ie
= { 0 };
125 char *kwlist
[] = { "class", "pbits", "qbits", "event", "ievent",
126 "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 char *kwlist
[] = { "class", "pbits", "qbits",
161 "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
)
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 *)pp
[i
].name
, 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
)) SYNERR(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 "catacomb.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 "catacomb.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 "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 "catacomb.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 "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 char *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 SYNERR("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
.type
.tp_basicsize
= sizeof(ge_pyobj
);
538 gobj
->ty
.type
.tp_base
= ge_pytype
;
539 Py_INCREF(group_pytype
);
540 gobj
->ty
.type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
541 Py_TPFLAGS_BASETYPE
|
542 Py_TPFLAGS_CHECKTYPES
|
543 Py_TPFLAGS_HEAPTYPE
);
544 gobj
->ty
.type
.tp_alloc
= PyType_GenericAlloc
;
545 gobj
->ty
.type
.tp_free
= 0;
546 gobj
->ty
.type
.tp_new
= ge_pynew
;
547 PyType_Ready(&gobj
->ty
.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
)) goto end
;
701 rc
= PyInt_FromLong(l
);
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 char *kwlist
[] = { "curve", 0 };
722 PyTypeObject
*cty
= ecpt_pytype
;
725 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:toec", kwlist
,
727 if (!PyType_Check(cty
) || !PyType_IsSubtype(cty
, ecpt_pytype
))
728 TYERR("want subtype of catacomb.ECPt");
729 if (G_TOEC(GE_G(me
), &p
, GE_X(me
)))
730 TYERR("can't convert to ec point");
731 return (ecpt_pywrapout(cty
, &p
));
736 static PyObject
*gemeth_tobuf(PyObject
*me
, PyObject
*arg
)
742 if (!PyArg_ParseTuple(arg
, ":tobuf")) return (0);
743 n
= GE_G(me
)->noctets
+ 4;
744 rc
= bytestring_pywrap(0, n
);
745 buf_init(&b
, PyString_AS_STRING(rc
), n
);
746 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
748 _PyString_Resize(&rc
, BLEN(&b
));
752 static PyObject
*gemeth_toraw(PyObject
*me
, PyObject
*arg
)
758 if (!PyArg_ParseTuple(arg
, ":toraw")) return (0);
759 n
= GE_G(me
)->noctets
;
760 rc
= bytestring_pywrap(0, n
);
761 buf_init(&b
, PyString_AS_STRING(rc
), n
);
762 G_TORAW(GE_G(me
), &b
, GE_X(me
));
764 _PyString_Resize(&rc
, BLEN(&b
));
768 static PyObject
*gmexp_exp(PyObject
*me
, void *pp
, int n
)
770 ge
*z
= G_CREATE(GROUP_G(me
));
771 G_MEXP(GROUP_G(me
), z
, pp
, n
);
772 return (ge_pywrap(me
, z
));
775 static void gmexp_drop(void *pp
)
777 group_expfactor
*f
= pp
;
781 static PyObject
*gmeth_mexp(PyObject
*me
, PyObject
*arg
)
783 return (mexp_common(me
, arg
, sizeof(group_expfactor
),
784 gmexp_id
, gmexp_fill
, gmexp_exp
, gmexp_drop
));
787 static PyObject
*gmeth_check(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
789 char *kwlist
[] = { "rng", 0 };
790 grand
*r
= &rand_global
;
793 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:check", kwlist
,
796 if ((p
= G_CHECK(GROUP_G(me
), r
)) != 0)
803 static PyObject
*group_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
805 int b
= group_samep(GROUP_G(x
), GROUP_G(y
));
809 default: TYERR("can't order groups");
816 static PyObject
*meth__GE_frombuf(PyObject
*me
, PyObject
*arg
)
824 if (!PyArg_ParseTuple(arg
, "Os#:frombuf", &me
, &p
, &n
))
829 if (G_FROMBUF(g
, &b
, x
))
830 VALERR("invalid data");
831 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
833 if (x
) G_DESTROY(g
, x
);
837 static PyObject
*meth__GE_fromraw(PyObject
*me
, PyObject
*arg
)
845 if (!PyArg_ParseTuple(arg
, "Os#:fromraw", &me
, &p
, &n
))
850 if (G_FROMRAW(g
, &b
, x
))
851 VALERR("invalid data");
852 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
854 if (x
) G_DESTROY(g
, x
);
858 static PyObject
*meth__GE_fromstring(PyObject
*me
, PyObject
*arg
)
866 if (!PyArg_ParseTuple(arg
, "Os#:fromstring", &me
, &p
, &n
))
872 if (G_READ(g
, x
, &mptext_stringops
, &sc
))
873 SYNERR("bad group element string");
874 return (Py_BuildValue("(Ns#)", ge_pywrap(me
, x
),
875 sc
.buf
, (int)(sc
.lim
- sc
.buf
)));
877 if (x
) G_DESTROY(g
, x
);
881 static PyObject
*meth__Group_parse(PyObject
*me
, PyObject
*arg
)
887 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
))
891 if ((g
= group_parse(&qd
)) == 0)
893 return (group_pywrap(g
));
898 static PyObject
*geget_group(PyObject
*me
, void *hunoz
)
899 { RETURN_OBJ(GE_GOBJ(me
)); }
901 static PyObject
*gget_nbits(PyObject
*me
, void *hunoz
)
902 { return (PyInt_FromLong(GROUP_G(me
)->nbits
)); }
904 static PyObject
*gget_noctets(PyObject
*me
, void *hunoz
)
905 { return (PyInt_FromLong(GROUP_G(me
)->noctets
)); }
907 static PyObject
*gget_i(PyObject
*me
, void *hunoz
)
909 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
910 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
913 static PyObject
*gget_g(PyObject
*me
, void *hunoz
)
915 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
916 G_COPY(g
, x
, g
->g
); return (ge_pywrap(me
, x
));
919 static PyObject
*gget_r(PyObject
*me
, void *hunoz
)
920 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->r
))); }
922 static PyObject
*gget_h(PyObject
*me
, void *hunoz
)
923 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->h
))); }
925 static PyGetSetDef ge_pygetset
[] = {
926 #define GETSETNAME(op, name) ge##op##_##name
927 GET (group
, "X.group -> group containing X")
932 static PyMethodDef ge_pymethods
[] = {
933 #define METHNAME(name) gemeth_##name
934 METH (inv
, "X.inv() -> inverse element of X")
935 METH (sqr
, "X.sqr() -> X^2 = X * X")
936 METH (check
, "X.check() -> check X really belongs to its group")
937 METH (toint
, "X.toint() -> X converted to an integer")
939 X.toec(curve = ecpt) -> X converted to elliptic curve point")
940 METH (tobuf
, "X.tobuf() -> X in buffer representation")
941 METH (toraw
, "X.toraw() -> X in raw representation")
946 static PyNumberMethods ge_pynumber
= {
948 0, /* @nb_subtract@ */
949 ge_pymul
, /* @nb_multiply@ */
950 ge_pydiv
, /* @nb_divide@ */
951 0, /* @nb_remainder@ */
953 ge_pyexp
, /* @nb_power@ */
954 0, /* @nb_negative@ */
955 0, /* @nb_positive@ */
956 0, /* @nb_absolute@ */
957 ge_pynonzerop
, /* @nb_nonzero@ */
965 ge_pyint
, /* @nb_int@ */
966 ge_pylong
, /* @nb_long@ */
967 0 /* meaningless */, /* @nb_float@ */
971 0, /* @nb_inplace_add@ */
972 0, /* @nb_inplace_subtract@ */
973 0, /* @nb_inplace_multiply@ */
974 0, /* @nb_inplace_divide@ */
975 0, /* @nb_inplace_remainder@ */
976 0, /* @nb_inplace_power@ */
977 0, /* @nb_inplace_lshift@ */
978 0, /* @nb_inplace_rshift@ */
979 0, /* @nb_inplace_and@ */
980 0, /* @nb_inplace_xor@ */
981 0, /* @nb_inplace_or@ */
983 0, /* @nb_floor_divide@ */
984 ge_pydiv
, /* @nb_true_divide@ */
985 0, /* @nb_inplace_floor_divide@ */
986 0, /* @nb_inplace_true_divide@ */
989 static PyTypeObject ge_pytype_skel
= {
990 PyObject_HEAD_INIT(0) 0, /* Header */
991 "catacomb.GE", /* @tp_name@ */
992 sizeof(ge_pyobj
), /* @tp_basicsize@ */
993 0, /* @tp_itemsize@ */
995 ge_pydealloc
, /* @tp_dealloc@ */
997 0, /* @tp_getattr@ */
998 0, /* @tp_setattr@ */
999 0, /* @tp_compare@ */
1001 &ge_pynumber
, /* @tp_as_number@ */
1002 0, /* @tp_as_sequence@ */
1003 0, /* @tp_as_mapping@ */
1006 ge_pystr
, /* @tp_str@ */
1007 0, /* @tp_getattro@ */
1008 0, /* @tp_setattro@ */
1009 0, /* @tp_as_buffer@ */
1010 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1011 Py_TPFLAGS_CHECKTYPES
|
1012 Py_TPFLAGS_BASETYPE
,
1015 "Group elements, abstract base class.",
1017 0, /* @tp_traverse@ */
1019 ge_pyrichcompare
, /* @tp_richcompare@ */
1020 0, /* @tp_weaklistoffset@ */
1022 0, /* @tp_iternext@ */
1023 ge_pymethods
, /* @tp_methods@ */
1024 0, /* @tp_members@ */
1025 ge_pygetset
, /* @tp_getset@ */
1028 0, /* @tp_descr_get@ */
1029 0, /* @tp_descr_set@ */
1030 0, /* @tp_dictoffset@ */
1032 PyType_GenericAlloc
, /* @tp_alloc@ */
1033 abstract_pynew
, /* @tp_new@ */
1038 static PyGetSetDef group_pygetset
[] = {
1039 #define GETSETNAME(op, name) g##op##_##name
1040 GET (noctets
, "G.noctets -> size in octets of element")
1041 GET (nbits
, "G.nbits -> size in bits of element")
1042 GET (i
, "G.i -> group identity")
1043 GET (g
, "G.g -> group generator")
1044 GET (r
, "G.r -> group order")
1045 GET (h
, "G.h -> group cofactor")
1050 static PyMethodDef group_pymethods
[] = {
1051 #define METHNAME(name) gmeth_##name
1053 G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1054 KWMETH(check
, "G.check(rand = random): check group is good")
1059 static PyTypeObject group_pytype_skel
= {
1060 PyObject_HEAD_INIT(0) 0, /* Header */
1061 "catacomb.Group", /* @tp_name@ */
1062 sizeof(group_pyobj
), /* @tp_basicsize@ */
1063 0, /* @tp_itemsize@ */
1065 group_pydealloc
, /* @tp_dealloc@ */
1067 0, /* @tp_getattr@ */
1068 0, /* @tp_setattr@ */
1069 0, /* @tp_compare@ */
1071 0, /* @tp_as_number@ */
1072 0, /* @tp_as_sequence@ */
1073 0, /* @tp_as_mapping@ */
1077 0, /* @tp_getattro@ */
1078 0, /* @tp_setattro@ */
1079 0, /* @tp_as_buffer@ */
1080 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1081 Py_TPFLAGS_BASETYPE
,
1084 "Abstract base class for groups.",
1086 0, /* @tp_traverse@ */
1088 group_pyrichcompare
, /* @tp_richcompare@ */
1089 0, /* @tp_weaklistoffset@ */
1091 0, /* @tp_iternext@ */
1092 group_pymethods
, /* @tp_methods@ */
1093 0, /* @tp_members@ */
1094 group_pygetset
, /* @tp_getset@ */
1097 0, /* @tp_descr_get@ */
1098 0, /* @tp_descr_set@ */
1099 0, /* @tp_dictoffset@ */
1101 PyType_GenericAlloc
, /* @tp_alloc@ */
1102 abstract_pynew
, /* @tp_new@ */
1107 static PyObject
*pgget_info(PyObject
*me
, void *hunoz
)
1110 gctx_prime
*gg
= (gctx_prime
*)GROUP_G(me
);
1111 dp
.p
= MP_COPY(gg
->mm
.m
);
1112 dp
.q
= MP_COPY(gg
->g
.r
);
1113 dp
.g
= mpmont_reduce(&gg
->mm
, MP_NEW
, gg
->gen
);
1114 return (fginfo_pywrap(&dp
, dhinfo_pytype
));
1117 static PyGetSetDef primegroup_pygetset
[] = {
1118 #define GETSETNAME(op, name) pg##op##_##name
1119 GET (info
, "G.info -> information about the group")
1124 static PyObject
*primegroup_pynew(PyTypeObject
*ty
,
1125 PyObject
*arg
, PyObject
*kw
)
1128 char *kwlist
[] = { "info", 0 };
1130 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", kwlist
,
1133 return (group_dopywrap(ty
, group_prime(FGINFO_DP(i
))));
1136 static PyTypeObject primegroup_pytype_skel
= {
1137 PyObject_HEAD_INIT(0) 0, /* Header */
1138 "catacomb.PrimeGroup", /* @tp_name@ */
1139 sizeof(group_pyobj
), /* @tp_basicsize@ */
1140 0, /* @tp_itemsize@ */
1142 group_pydealloc
, /* @tp_dealloc@ */
1144 0, /* @tp_getattr@ */
1145 0, /* @tp_setattr@ */
1146 0, /* @tp_compare@ */
1148 0, /* @tp_as_number@ */
1149 0, /* @tp_as_sequence@ */
1150 0, /* @tp_as_mapping@ */
1154 0, /* @tp_getattro@ */
1155 0, /* @tp_setattro@ */
1156 0, /* @tp_as_buffer@ */
1157 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1158 Py_TPFLAGS_BASETYPE
,
1161 "Subgroups of prime fields.",
1163 0, /* @tp_traverse@ */
1165 0, /* @tp_richcompare@ */
1166 0, /* @tp_weaklistoffset@ */
1168 0, /* @tp_iternext@ */
1169 0, /* @tp_methods@ */
1170 0, /* @tp_members@ */
1171 primegroup_pygetset
, /* @tp_getset@ */
1174 0, /* @tp_descr_get@ */
1175 0, /* @tp_descr_set@ */
1176 0, /* @tp_dictoffset@ */
1178 PyType_GenericAlloc
, /* @tp_alloc@ */
1179 primegroup_pynew
, /* @tp_new@ */
1184 static PyObject
*bgget_info(PyObject
*me
, void *hunoz
)
1187 gctx_bin
*gg
= (gctx_bin
*)GROUP_G(me
);
1188 dp
.p
= MP_COPY(gg
->r
.p
);
1189 dp
.q
= MP_COPY(gg
->g
.r
);
1190 dp
.g
= MP_COPY(gg
->gen
);
1191 return (fginfo_pywrap(&dp
, bindhinfo_pytype
));
1194 static PyGetSetDef bingroup_pygetset
[] = {
1195 #define GETSETNAME(op, name) bg##op##_##name
1196 GET (info
, "G.info -> information about the group")
1201 static PyObject
*bingroup_pynew(PyTypeObject
*ty
,
1202 PyObject
*arg
, PyObject
*kw
)
1205 char *kwlist
[] = { "info", 0 };
1207 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", kwlist
,
1208 bindhinfo_pytype
, &i
))
1210 return (group_dopywrap(ty
, group_binary(FGINFO_DP(i
))));
1213 static PyTypeObject bingroup_pytype_skel
= {
1214 PyObject_HEAD_INIT(0) 0, /* Header */
1215 "catacomb.BinGroup", /* @tp_name@ */
1216 sizeof(group_pyobj
), /* @tp_basicsize@ */
1217 0, /* @tp_itemsize@ */
1219 group_pydealloc
, /* @tp_dealloc@ */
1221 0, /* @tp_getattr@ */
1222 0, /* @tp_setattr@ */
1223 0, /* @tp_compare@ */
1225 0, /* @tp_as_number@ */
1226 0, /* @tp_as_sequence@ */
1227 0, /* @tp_as_mapping@ */
1231 0, /* @tp_getattro@ */
1232 0, /* @tp_setattro@ */
1233 0, /* @tp_as_buffer@ */
1234 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1235 Py_TPFLAGS_BASETYPE
,
1238 "Subgroups of binary fields.",
1240 0, /* @tp_traverse@ */
1242 0, /* @tp_richcompare@ */
1243 0, /* @tp_weaklistoffset@ */
1245 0, /* @tp_iternext@ */
1246 0, /* @tp_methods@ */
1247 0, /* @tp_members@ */
1248 bingroup_pygetset
, /* @tp_getset@ */
1251 0, /* @tp_descr_get@ */
1252 0, /* @tp_descr_set@ */
1253 0, /* @tp_dictoffset@ */
1255 PyType_GenericAlloc
, /* @tp_alloc@ */
1256 bingroup_pynew
, /* @tp_new@ */
1261 static PyObject
*egget_info(PyObject
*me
, void *hunoz
)
1264 gctx_ec
*gg
= (gctx_ec
*)GROUP_G(me
);
1266 ecinfo_copy(&ei
, &gg
->ei
);
1267 return (ecinfo_pywrap(&ei
));
1270 static PyGetSetDef ecgroup_pygetset
[] = {
1271 #define GETSETNAME(op, name) eg##op##_##name
1272 GET (info
, "G.info -> information about the group")
1277 static PyObject
*ecgroup_pynew(PyTypeObject
*ty
,
1278 PyObject
*arg
, PyObject
*kw
)
1282 char *kwlist
[] = { "info", 0 };
1284 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", kwlist
,
1287 ecinfo_copy(&ei
, ECINFO_EI(i
));
1288 return (group_dopywrap(ty
, group_ec(&ei
)));
1291 static PyTypeObject ecgroup_pytype_skel
= {
1292 PyObject_HEAD_INIT(0) 0, /* Header */
1293 "catacomb.ECGroup", /* @tp_name@ */
1294 sizeof(group_pyobj
), /* @tp_basicsize@ */
1295 0, /* @tp_itemsize@ */
1297 group_pydealloc
, /* @tp_dealloc@ */
1299 0, /* @tp_getattr@ */
1300 0, /* @tp_setattr@ */
1301 0, /* @tp_compare@ */
1303 0, /* @tp_as_number@ */
1304 0, /* @tp_as_sequence@ */
1305 0, /* @tp_as_mapping@ */
1309 0, /* @tp_getattro@ */
1310 0, /* @tp_setattro@ */
1311 0, /* @tp_as_buffer@ */
1312 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1313 Py_TPFLAGS_BASETYPE
,
1316 "Elliptic curve groups.",
1318 0, /* @tp_traverse@ */
1320 0, /* @tp_richcompare@ */
1321 0, /* @tp_weaklistoffset@ */
1323 0, /* @tp_iternext@ */
1324 0, /* @tp_methods@ */
1325 0, /* @tp_members@ */
1326 ecgroup_pygetset
, /* @tp_getset@ */
1329 0, /* @tp_descr_get@ */
1330 0, /* @tp_descr_set@ */
1331 0, /* @tp_dictoffset@ */
1333 PyType_GenericAlloc
, /* @tp_alloc@ */
1334 ecgroup_pynew
, /* @tp_new@ */
1339 /*----- Global stuff ------------------------------------------------------*/
1341 static PyMethodDef methods
[] = {
1342 #define METHNAME(name) meth_##name
1343 METH (_GE_frombuf
, "frombuf(BUF) -> X, REST")
1344 METH (_GE_fromraw
, "fromraw(BUF) -> X, REST")
1345 METH (_GE_fromstring
, "fromstring(STR) -> X, REST")
1346 METH (_Group_parse
, "parse(STR) -> G, REST")
1347 METH (_DHInfo_parse
, "parse(STR) -> D, REST")
1348 METH (_BinDHInfo_parse
, "parse(STR) -> D, REST")
1349 METH (_DHInfo__groupn
, 0)
1350 METH (_BinDHInfo__groupn
, 0)
1351 KWMETH(_DHInfo_generate
, "\
1352 generate(PBITS, [qbits = 0, event = pgen_nullev,\n\
1353 rng = rand, nsteps = 0]) -> D")
1354 KWMETH(_DHInfo_genlimlee
, "\
1355 genlimlee(PBITS, QBITS, [event = pgen_nullev, ievent = pgen_nullev,\n\
1356 rng = rand, nsteps = 0, subgroupp = True]) -> (D, [Q, ...])")
1357 KWMETH(_DHInfo_gendsa
, "\
1358 gendsa(PBITS, QBITS, SEED, [event = pgen_nullev, nsteps = 0])\n\
1359 -> (D, SEED, COUNT)")
1360 KWMETH(_DHInfo_genkcdsa
, "\
1361 gendsa(PBITS, QBITS, [event = pgen_nullev, rng = rand, nsteps = 0])\n\
1367 void group_pyinit(void)
1369 INITTYPE(fginfo
, root
);
1370 INITTYPE(dhinfo
, fginfo
);
1371 INITTYPE(bindhinfo
, dhinfo
);
1373 INITTYPE(group
, type
);
1374 INITTYPE(primegroup
, group
);
1375 INITTYPE(bingroup
, group
);
1376 INITTYPE(ecgroup
, group
);
1377 addmethods(methods
);
1380 void group_pyinsert(PyObject
*mod
)
1382 INSERT("FGInfo", fginfo_pytype
);
1383 INSERT("DHInfo", dhinfo_pytype
);
1384 INSERT("BinDHInfo", bindhinfo_pytype
);
1385 INSERT("GE", ge_pytype
);
1386 INSERT("Group", group_pytype
);
1387 INSERT("PrimeGroup", primegroup_pytype
);
1388 INSERT("BinGroup", bingroup_pytype
);
1389 INSERT("ECGroup", ecgroup_pytype
);
1390 INSERT("_pgroups", namedgroups(ptab
, &npgroups
));
1391 INSERT("_bingroups", namedgroups(bintab
, &nbingroups
));
1394 /*----- That's all, folks -------------------------------------------------*/