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
);
38 z
->dp
.p
= MP_COPY(dp
->p
);
39 z
->dp
.q
= MP_COPY(dp
->q
);
40 z
->dp
.g
= MP_COPY(dp
->g
);
41 return ((PyObject
*)z
);
44 static PyObject
*fginfo_pynew(PyTypeObject
*ty
,
45 PyObject
*arg
, PyObject
*kw
)
47 char *kwlist
[] = { "p", "r", "g", 0 };
48 gprime_param dp
= { 0 };
51 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&O&:new", kwlist
,
56 z
= PyObject_New(fginfo_pyobj
, ty
);
58 return ((PyObject
*)z
);
66 static PyObject
*figet_r(PyObject
*me
, void *hunoz
)
67 { return mp_pywrap(FGINFO_DP(me
)->q
); }
69 static PyObject
*diget_p(PyObject
*me
, void *hunoz
)
70 { return mp_pywrap(FGINFO_DP(me
)->p
); }
72 static PyObject
*diget_g(PyObject
*me
, void *hunoz
)
73 { return mp_pywrap(FGINFO_DP(me
)->g
); }
75 static PyObject
*biget_p(PyObject
*me
, void *hunoz
)
76 { return gf_pywrap(FGINFO_DP(me
)->p
); }
78 static PyObject
*biget_m(PyObject
*me
, void *hunoz
)
79 { return PyInt_FromLong(mp_octets(FGINFO_DP(me
)->p
) - 1); }
81 static PyObject
*biget_g(PyObject
*me
, void *hunoz
)
82 { return gf_pywrap(FGINFO_DP(me
)->g
); }
84 static void fginfo_pydealloc(PyObject
*me
)
86 mp_drop(FGINFO_DP(me
)->p
);
87 mp_drop(FGINFO_DP(me
)->q
);
88 mp_drop(FGINFO_DP(me
)->g
);
92 static PyObject
*meth__DHInfo_generate(PyObject
*me
,
93 PyObject
*arg
, PyObject
*kw
)
98 grand
*r
= &rand_global
;
101 { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
104 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&|O&O&O&O&:generate", kwlist
,
105 &me
, convuint
, &pl
, convuint
, &ql
,
106 convpgev
, &evt
, convgrand
, &r
,
109 if (dh_gen(&dp
, ql
, pl
, steps
, r
, evt
.proc
, evt
.ctx
))
111 rc
= fginfo_pywrap(&dp
, dhinfo_pytype
);
117 static PyObject
*meth__DHInfo_genlimlee(PyObject
*me
,
118 PyObject
*arg
, PyObject
*kw
)
123 grand
*r
= &rand_global
;
124 pgev oe
= { 0 }, ie
= { 0 };
127 char *kwlist
[] = { "class", "pbits", "qbits", "event", "ievent",
128 "rng", "nsteps", "subgroupp", 0 };
131 PyObject
*rc
= 0, *vec
= 0;
133 if (!PyArg_ParseTupleAndKeywords(arg
, kw
,
134 "OO&O&|O&O&O&O&O&:genlimlee", kwlist
,
135 &me
, convuint
, &pl
, convuint
, &ql
,
136 convpgev
, &oe
, convpgev
, &ie
,
137 convgrand
, &r
, convuint
, &steps
,
138 convbool
, &subgroupp
))
140 if (subgroupp
) f
|= DH_SUBGROUP
;
141 if (dh_limlee(&dp
, ql
, pl
, f
, steps
, r
,
142 oe
.proc
, oe
.ctx
, ie
.proc
, ie
.ctx
, &nf
, &v
))
144 vec
= PyList_New(nf
);
145 for (i
= 0; i
< nf
; i
++)
146 PyList_SetItem(vec
, i
, mp_pywrap(v
[i
]));
148 rc
= Py_BuildValue("(NN)", fginfo_pywrap(&dp
, dhinfo_pytype
), vec
);
150 droppgev(&oe
); droppgev(&ie
);
154 static PyObject
*meth__DHInfo_gendsa(PyObject
*me
,
155 PyObject
*arg
, PyObject
*kw
)
165 { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 };
168 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&O&s#|O&O&:generate", kwlist
,
169 &me
, convuint
, &pl
, convuint
, &ql
,
170 &k
, &ksz
, convpgev
, &evt
,
173 if (dsa_gen(&dp
, ql
, pl
, steps
, k
, ksz
, &ds
, evt
.proc
, evt
.ctx
))
175 rc
= Py_BuildValue("(NNl)", fginfo_pywrap(&dp
, dhinfo_pytype
),
176 bytestring_pywrap(ds
.p
, ds
.sz
), (long)ds
.count
);
183 static int npgroups
= -1, nbingroups
= -1;
185 static PyObject
*namedgroups(const pentry
*pp
, int *ne
)
192 for (i
= 0; pp
[i
].name
; i
++) {
194 for (j
= 0; j
< i
; j
++) {
195 if (pp
[i
].data
== pp
[j
].data
) {
196 c
= PyDict_GetItemString(d
, (/*unconst*/ char *)pp
[j
].name
);
201 c
= PyInt_FromLong(i
);
203 PyDict_SetItemString(d
, (/*unconst*/ char *)pp
[i
].name
, c
);
210 static PyObject
*meth__groupn(PyObject
*me
, PyObject
*arg
,
211 PyTypeObject
*ty
, const pentry
*pp
, int ne
)
217 if (!PyArg_ParseTuple(arg
, "Oi:_groupn", &me
, &i
)) goto end
;
218 if (i
< 0 || i
>= ne
) VALERR("group index out of range");
219 dh_infofromdata(&gp
, pp
[i
].data
);
220 rc
= fginfo_pywrap(&gp
, ty
);
225 static PyObject
*meth__DHInfo__groupn(PyObject
*me
, PyObject
*arg
)
226 { return (meth__groupn(me
, arg
, dhinfo_pytype
, ptab
, npgroups
)); }
228 static PyObject
*meth__BinDHInfo__groupn(PyObject
*me
, PyObject
*arg
)
229 { return (meth__groupn(me
, arg
, bindhinfo_pytype
, bintab
, nbingroups
)); }
231 static PyObject
*meth__parse(PyObject
*me
, PyObject
*arg
, PyTypeObject
*ty
,
232 int (*parse
)(qd_parse
*, gprime_param
*))
239 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
)) goto end
;
242 if (parse(&qd
, &gp
)) SYNERR(qd
.e
);
243 rc
= fginfo_pywrap(&gp
, ty
);
248 static PyObject
*meth__DHInfo_parse(PyObject
*me
, PyObject
*arg
)
249 { return (meth__parse(me
, arg
, dhinfo_pytype
, dh_parse
)); }
251 static PyObject
*meth__BinDHInfo_parse(PyObject
*me
, PyObject
*arg
)
252 { return (meth__parse(me
, arg
, bindhinfo_pytype
, dhbin_parse
)); }
254 static PyGetSetDef fginfo_pygetset
[] = {
255 #define GETSETNAME(op, name) fi##op##_##name
256 GET (r
, "I.r -> group order")
261 static PyGetSetDef dhinfo_pygetset
[] = {
262 #define GETSETNAME(op, name) di##op##_##name
263 GET (p
, "I.p -> prime")
264 GET (g
, "I.g -> generator")
269 static PyGetSetDef bindhinfo_pygetset
[] = {
270 #define GETSETNAME(op, name) bi##op##_##name
271 GET (p
, "I.p -> irreducible polynomial")
272 GET (m
, "I.m -> degree of polynomial")
273 GET (g
, "I.g -> generator")
278 static PyTypeObject fginfo_pytype_skel
= {
279 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
280 "catacomb.FGInfo", /* @tp_name@ */
281 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
282 0, /* @tp_itemsize@ */
284 fginfo_pydealloc
, /* @tp_dealloc@ */
286 0, /* @tp_getattr@ */
287 0, /* @tp_setattr@ */
288 0, /* @tp_compare@ */
290 0, /* @tp_as_number@ */
291 0, /* @tp_as_sequence@ */
292 0, /* @tp_as_mapping@ */
296 0, /* @tp_getattro@ */
297 0, /* @tp_setattro@ */
298 0, /* @tp_as_buffer@ */
299 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
303 "Abstract base class for field-group information objects.",
305 0, /* @tp_traverse@ */
307 0, /* @tp_richcompare@ */
308 0, /* @tp_weaklistoffset@ */
310 0, /* @tp_iternexr@ */
311 0, /* @tp_methods@ */
312 0, /* @tp_members@ */
313 fginfo_pygetset
, /* @tp_getset@ */
316 0, /* @tp_descr_get@ */
317 0, /* @tp_descr_set@ */
318 0, /* @tp_dictoffset@ */
320 PyType_GenericAlloc
, /* @tp_alloc@ */
321 abstract_pynew
, /* @tp_new@ */
322 _PyObject_Del
, /* @tp_free@ */
326 static PyTypeObject dhinfo_pytype_skel
= {
327 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
328 "catacomb.DHInfo", /* @tp_name@ */
329 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
330 0, /* @tp_itemsize@ */
332 fginfo_pydealloc
, /* @tp_dealloc@ */
334 0, /* @tp_getattr@ */
335 0, /* @tp_setattr@ */
336 0, /* @tp_compare@ */
338 0, /* @tp_as_number@ */
339 0, /* @tp_as_sequence@ */
340 0, /* @tp_as_mapping@ */
344 0, /* @tp_getattro@ */
345 0, /* @tp_setattro@ */
346 0, /* @tp_as_buffer@ */
347 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
351 "Standard (integer) Diffie-Hellman group information.",
353 0, /* @tp_traverse@ */
355 0, /* @tp_richcompare@ */
356 0, /* @tp_weaklistoffset@ */
358 0, /* @tp_iternexr@ */
359 0, /* @tp_methods@ */
360 0, /* @tp_members@ */
361 dhinfo_pygetset
, /* @tp_getset@ */
364 0, /* @tp_descr_get@ */
365 0, /* @tp_descr_set@ */
366 0, /* @tp_dictoffset@ */
368 PyType_GenericAlloc
, /* @tp_alloc@ */
369 fginfo_pynew
, /* @tp_new@ */
370 _PyObject_Del
, /* @tp_free@ */
374 static PyTypeObject bindhinfo_pytype_skel
= {
375 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
376 "catacomb.BinDHInfo", /* @tp_name@ */
377 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
378 0, /* @tp_itemsize@ */
380 fginfo_pydealloc
, /* @tp_dealloc@ */
382 0, /* @tp_getattr@ */
383 0, /* @tp_setattr@ */
384 0, /* @tp_compare@ */
386 0, /* @tp_as_number@ */
387 0, /* @tp_as_sequence@ */
388 0, /* @tp_as_mapping@ */
392 0, /* @tp_getattro@ */
393 0, /* @tp_setattro@ */
394 0, /* @tp_as_buffer@ */
395 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
399 "Binary-field Diffie-Hellman group information.",
401 0, /* @tp_traverse@ */
403 0, /* @tp_richcompare@ */
404 0, /* @tp_weaklistoffset@ */
406 0, /* @tp_iternexr@ */
407 0, /* @tp_methods@ */
408 0, /* @tp_members@ */
409 bindhinfo_pygetset
, /* @tp_getset@ */
412 0, /* @tp_descr_get@ */
413 0, /* @tp_descr_set@ */
414 0, /* @tp_dictoffset@ */
416 PyType_GenericAlloc
, /* @tp_alloc@ */
417 fginfo_pynew
, /* @tp_new@ */
418 _PyObject_Del
, /* @tp_free@ */
422 /*----- General utilities -------------------------------------------------*/
424 PyTypeObject
*ge_pytype
, *group_pytype
;
425 PyTypeObject
*primegroup_pytype
, *bingroup_pytype
, *ecgroup_pytype
;
427 group
*group_copy(group
*g
)
429 if (strcmp(G_NAME(g
), "prime") == 0) {
430 gctx_prime
*gc
= (gctx_prime
*)g
;
432 gp
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
435 g
= group_prime(&gp
);
437 } else if (strcmp(G_NAME(g
), "bin") == 0) {
438 gctx_bin
*gc
= (gctx_bin
*)g
;
440 gb
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
443 g
= group_binary(&gb
);
445 } else if (strcmp(G_NAME(g
), "ec") == 0) {
446 gctx_ec
*gc
= (gctx_ec
*)g
;
448 if ((ei
.c
= eccurve_copy(gc
->ei
.c
)) == 0)
451 EC_COPY(&ei
.g
, &gc
->ei
.g
);
452 ei
.r
= MP_COPY(gc
->ei
.r
);
453 ei
.h
= MP_COPY(gc
->ei
.h
);
460 PyObject
*ge_pywrap(PyObject
*gobj
, ge
*x
)
462 ge_pyobj
*z
= PyObject_New(ge_pyobj
, (PyTypeObject
*)gobj
);
464 z
->g
= GROUP_G(gobj
);
466 return ((PyObject
*)z
);
469 static PyObject
*ge_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
471 char *kwlist
[] = { "x", 0 };
480 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", kwlist
, &x
)) goto end
;
482 if (ECPT_PYCHECK(x
)) {
484 if (G_FROMEC(g
, xx
, &p
))
485 TYERR("can't convert from elliptic curve point");
487 } else if ((y
= tomp(x
)) != 0) {
488 if (G_FROMINT(g
, xx
, y
))
489 TYERR("can't convert from integer");
491 } else if (PyString_Check(x
)) {
492 sc
.buf
= PyString_AS_STRING(x
);
493 sc
.lim
= sc
.buf
+ PyString_GET_SIZE(x
);
494 if (G_READ(g
, xx
, &mptext_stringops
, &sc
) || sc
.buf
< sc
.lim
)
495 SYNERR("malformed group element string");
497 TYERR("can't convert to group element");
498 return (ge_pywrap((PyObject
*)ty
, xx
));
502 if (xx
) G_DESTROY(g
, xx
);
506 static PyObject
*group_dopywrap(PyTypeObject
*ty
, group
*g
)
508 group_pyobj
*gobj
= newtype(ty
, 0);
510 gobj
->ty
.tp_name
= (/*unconst*/ char *)g
->ops
->name
;
511 gobj
->ty
.tp_basicsize
= sizeof(ge_pyobj
);
512 gobj
->ty
.tp_base
= ge_pytype
;
513 Py_INCREF(group_pytype
);
514 gobj
->ty
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
515 Py_TPFLAGS_BASETYPE
|
516 Py_TPFLAGS_CHECKTYPES
|
517 Py_TPFLAGS_HEAPTYPE
);
518 gobj
->ty
.tp_alloc
= PyType_GenericAlloc
;
519 gobj
->ty
.tp_free
=_PyObject_Del
;
520 gobj
->ty
.tp_new
= ge_pynew
;
521 PyType_Ready(&gobj
->ty
);
522 return ((PyObject
*)gobj
);
525 PyObject
*group_pywrap(group
*g
)
529 if (strcmp(G_NAME(g
), "prime") == 0) ty
= primegroup_pytype
;
530 else if (strcmp(G_NAME(g
), "bin") == 0) ty
= bingroup_pytype
;
531 else if (strcmp(G_NAME(g
), "ec") == 0) ty
= ecgroup_pytype
;
533 return (group_dopywrap(ty
, g
));
536 /*----- Group elements ----------------------------------------------------*/
538 #define BINOP(name) \
539 static PyObject *ge_py##name(PyObject *x, PyObject *y) \
543 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \
544 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \
548 g->ops->name(g, z, GE_X(x), GE_X(y)); \
549 return (ge_pywrap(GE_GOBJ(x), z)); \
556 static PyObject *gemeth_##name(PyObject *me, PyObject *arg) \
560 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
563 g->ops->name(g, z, GE_X(me)); \
564 return (ge_pywrap(GE_GOBJ(me), z)); \
570 static PyObject
*ge_pyexp(PyObject
*x
, PyObject
*n
, PyObject
*m
)
575 if (m
!= Py_None
|| !GE_PYCHECK(x
) || (nn
= getmp(n
)) == 0)
577 z
= G_CREATE(GE_G(x
));
578 G_EXP(GE_G(x
), z
, GE_X(x
), nn
);
580 return (ge_pywrap(GE_GOBJ(x
), z
));
583 static void ge_pydealloc(PyObject
*me
)
585 G_DESTROY(GE_G(me
), GE_X(me
));
586 Py_DECREF(GE_GOBJ(me
));
590 static void group_pydealloc(PyObject
*me
)
592 G_DESTROYGROUP(GROUP_G(me
));
593 PyType_Type
.tp_dealloc(me
);
596 static PyObject
*gmexp_id(PyObject
*me
)
598 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
599 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
602 static int gmexp_fill(void *pp
, PyObject
*me
, PyObject
*x
, PyObject
*m
)
604 group_expfactor
*f
= pp
;
606 if (!GE_PYCHECK(x
) || GE_G(x
) != GROUP_G(me
) || (f
->exp
= getmp(m
)) == 0)
612 static PyObject
*ge_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
617 if (!GE_PYCHECK(x
) || !GE_PYCHECK(y
) ||
618 (GE_G(x
) != GE_G(y
) && !group_samep(GE_G(x
), GE_G(y
))))
621 case Py_EQ
: b
= G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
622 case Py_NE
: b
= !G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
623 default: TYERR("group elements are unordered");
630 static PyObject
*gemeth_check(PyObject
*me
, PyObject
*arg
)
632 if (!PyArg_ParseTuple(arg
, ":check")) goto end
;
633 if (group_check(GE_G(me
), GE_X(me
))) VALERR("bad group element");
639 static int ge_pynonzerop(PyObject
*x
)
640 { return (!G_IDENTP(GE_G(x
), GE_X(x
))); }
642 static PyObject
*ge_pystr(PyObject
*me
)
647 group_writedstr(GE_G(me
), GE_X(me
), &d
);
648 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
653 static PyObject
*ge_pylong(PyObject
*me
)
658 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
659 TYERR("can't convert to integer");
660 rc
= (PyObject
*)mp_topylong(x
);
666 static PyObject
*ge_pyint(PyObject
*me
)
672 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
673 TYERR("can't convert to integer");
674 if (mp_tolong_checked(x
, &l
)) goto end
;
675 rc
= PyInt_FromLong(l
);
681 static PyObject
*gemeth_toint(PyObject
*me
, PyObject
*arg
)
685 if (!PyArg_ParseTuple(arg
, ":toint")) goto end
;
686 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
687 TYERR("can't convert to integer");
688 return (mp_pywrap(x
));
693 static PyObject
*gemeth_toec(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
695 char *kwlist
[] = { "curve", 0 };
696 PyTypeObject
*cty
= ecpt_pytype
;
699 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:toec", kwlist
,
701 if (!PyType_Check(cty
) || !PyType_IsSubtype(cty
, ecpt_pytype
))
702 TYERR("want subtype of catacomb.ECPt");
703 if (G_TOEC(GE_G(me
), &p
, GE_X(me
)))
704 TYERR("can't convert to ec point");
705 return (ecpt_pywrapout(cty
, &p
));
710 static PyObject
*gemeth_tobuf(PyObject
*me
, PyObject
*arg
)
716 if (!PyArg_ParseTuple(arg
, ":tobuf")) return (0);
717 n
= GE_G(me
)->noctets
+ 4;
718 rc
= bytestring_pywrap(0, n
);
719 buf_init(&b
, PyString_AS_STRING(rc
), n
);
720 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
722 _PyString_Resize(&rc
, BLEN(&b
));
726 static PyObject
*gemeth_toraw(PyObject
*me
, PyObject
*arg
)
732 if (!PyArg_ParseTuple(arg
, ":toraw")) return (0);
733 n
= GE_G(me
)->noctets
;
734 rc
= bytestring_pywrap(0, n
);
735 buf_init(&b
, PyString_AS_STRING(rc
), n
);
736 G_TORAW(GE_G(me
), &b
, GE_X(me
));
738 _PyString_Resize(&rc
, BLEN(&b
));
742 static PyObject
*gmexp_exp(PyObject
*me
, void *pp
, int n
)
744 ge
*z
= G_CREATE(GROUP_G(me
));
745 G_MEXP(GROUP_G(me
), z
, pp
, n
);
746 return (ge_pywrap(me
, z
));
749 static void gmexp_drop(void *pp
)
751 group_expfactor
*f
= pp
;
755 static PyObject
*gmeth_mexp(PyObject
*me
, PyObject
*arg
)
757 return (mexp_common(me
, arg
, sizeof(group_expfactor
),
758 gmexp_id
, gmexp_fill
, gmexp_exp
, gmexp_drop
));
761 static PyObject
*gmeth_check(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
763 char *kwlist
[] = { "rng", 0 };
764 grand
*r
= &rand_global
;
767 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:check", kwlist
,
770 if ((p
= G_CHECK(GROUP_G(me
), r
)) != 0)
777 static PyObject
*group_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
779 int b
= group_samep(GROUP_G(x
), GROUP_G(y
));
783 default: TYERR("can't order groups");
790 static PyObject
*meth__GE_frombuf(PyObject
*me
, PyObject
*arg
)
798 if (!PyArg_ParseTuple(arg
, "Os#:frombuf", &me
, &p
, &n
))
803 if (G_FROMBUF(g
, &b
, x
))
804 VALERR("invalid data");
805 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
807 if (x
) G_DESTROY(g
, x
);
811 static PyObject
*meth__GE_fromraw(PyObject
*me
, PyObject
*arg
)
819 if (!PyArg_ParseTuple(arg
, "Os#:fromraw", &me
, &p
, &n
))
824 if (G_FROMRAW(g
, &b
, x
))
825 VALERR("invalid data");
826 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
828 if (x
) G_DESTROY(g
, x
);
832 static PyObject
*meth__GE_fromstring(PyObject
*me
, PyObject
*arg
)
840 if (!PyArg_ParseTuple(arg
, "Os#:fromstring", &me
, &p
, &n
))
846 if (G_READ(g
, x
, &mptext_stringops
, &sc
))
847 SYNERR("bad group element string");
848 return (Py_BuildValue("(Ns#)", ge_pywrap(me
, x
),
849 sc
.buf
, (int)(sc
.lim
- sc
.buf
)));
851 if (x
) G_DESTROY(g
, x
);
855 static PyObject
*meth__Group_parse(PyObject
*me
, PyObject
*arg
)
861 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
))
865 if ((g
= group_parse(&qd
)) == 0)
867 return (group_pywrap(g
));
872 static PyObject
*geget_group(PyObject
*me
, void *hunoz
)
873 { RETURN_OBJ(GE_GOBJ(me
)); }
875 static PyObject
*gget_nbits(PyObject
*me
, void *hunoz
)
876 { return (PyInt_FromLong(GROUP_G(me
)->nbits
)); }
878 static PyObject
*gget_noctets(PyObject
*me
, void *hunoz
)
879 { return (PyInt_FromLong(GROUP_G(me
)->noctets
)); }
881 static PyObject
*gget_i(PyObject
*me
, void *hunoz
)
883 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
884 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
887 static PyObject
*gget_g(PyObject
*me
, void *hunoz
)
889 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
890 G_COPY(g
, x
, g
->g
); return (ge_pywrap(me
, x
));
893 static PyObject
*gget_r(PyObject
*me
, void *hunoz
)
894 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->r
))); }
896 static PyObject
*gget_h(PyObject
*me
, void *hunoz
)
897 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->h
))); }
899 static PyGetSetDef ge_pygetset
[] = {
900 #define GETSETNAME(op, name) ge##op##_##name
901 GET (group
, "X.group -> group containing X")
906 static PyMethodDef ge_pymethods
[] = {
907 #define METHNAME(name) gemeth_##name
908 METH (inv
, "X.inv() -> inverse element of X")
909 METH (sqr
, "X.sqr() -> X^2 = X * X")
910 METH (check
, "X.check() -> check X really belongs to its group")
911 METH (toint
, "X.toint() -> X converted to an integer")
913 X.toec(curve = ecpt) -> X converted to elliptic curve point")
914 METH (tobuf
, "X.tobuf() -> X in buffer representation")
915 METH (toraw
, "X.toraw() -> X in raw representation")
920 static PyNumberMethods ge_pynumber
= {
922 0, /* @nb_subtract@ */
923 ge_pymul
, /* @nb_multiply@ */
924 ge_pydiv
, /* @nb_divide@ */
925 0, /* @nb_remainder@ */
927 ge_pyexp
, /* @nb_power@ */
928 0, /* @nb_negative@ */
929 0, /* @nb_positive@ */
930 0, /* @nb_absolute@ */
931 ge_pynonzerop
, /* @nb_nonzero@ */
939 ge_pyint
, /* @nb_int@ */
940 ge_pylong
, /* @nb_long@ */
941 0 /* meaningless */, /* @nb_float@ */
945 0, /* @nb_inplace_add@ */
946 0, /* @nb_inplace_subtract@ */
947 0, /* @nb_inplace_multiply@ */
948 0, /* @nb_inplace_divide@ */
949 0, /* @nb_inplace_remainder@ */
950 0, /* @nb_inplace_power@ */
951 0, /* @nb_inplace_lshift@ */
952 0, /* @nb_inplace_rshift@ */
953 0, /* @nb_inplace_and@ */
954 0, /* @nb_inplace_xor@ */
955 0, /* @nb_inplace_or@ */
957 0, /* @nb_floor_divide@ */
958 ge_pydiv
, /* @nb_true_divide@ */
959 0, /* @nb_inplace_floor_divide@ */
960 0, /* @nb_inplace_true_divide@ */
963 static PyTypeObject ge_pytype_skel
= {
964 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
965 "catacomb.GE", /* @tp_name@ */
966 sizeof(ge_pyobj
), /* @tp_basicsize@ */
967 0, /* @tp_itemsize@ */
969 ge_pydealloc
, /* @tp_dealloc@ */
971 0, /* @tp_getattr@ */
972 0, /* @tp_setattr@ */
973 0, /* @tp_compare@ */
975 &ge_pynumber
, /* @tp_as_number@ */
976 0, /* @tp_as_sequence@ */
977 0, /* @tp_as_mapping@ */
980 ge_pystr
, /* @tp_str@ */
981 0, /* @tp_getattro@ */
982 0, /* @tp_setattro@ */
983 0, /* @tp_as_buffer@ */
984 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
985 Py_TPFLAGS_CHECKTYPES
|
989 "Group elements, abstract base class.",
991 0, /* @tp_traverse@ */
993 ge_pyrichcompare
, /* @tp_richcompare@ */
994 0, /* @tp_weaklistoffset@ */
996 0, /* @tp_iternexr@ */
997 ge_pymethods
, /* @tp_methods@ */
998 0, /* @tp_members@ */
999 ge_pygetset
, /* @tp_getset@ */
1002 0, /* @tp_descr_get@ */
1003 0, /* @tp_descr_set@ */
1004 0, /* @tp_dictoffset@ */
1006 PyType_GenericAlloc
, /* @tp_alloc@ */
1007 abstract_pynew
, /* @tp_new@ */
1008 _PyObject_Del
, /* @tp_free@ */
1012 static PyGetSetDef group_pygetset
[] = {
1013 #define GETSETNAME(op, name) g##op##_##name
1014 GET (noctets
, "G.noctets -> size in octets of element")
1015 GET (nbits
, "G.nbits -> size in bits of element")
1016 GET (i
, "G.i -> group identity")
1017 GET (g
, "G.g -> group generator")
1018 GET (r
, "G.r -> group order")
1019 GET (h
, "G.h -> group cofactor")
1024 static PyMethodDef group_pymethods
[] = {
1025 #define METHNAME(name) gmeth_##name
1027 G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1028 KWMETH(check
, "G.check(rand = random): check group is good")
1033 static PyTypeObject group_pytype_skel
= {
1034 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
1035 "catacomb.Group", /* @tp_name@ */
1036 sizeof(group_pyobj
), /* @tp_basicsize@ */
1037 0, /* @tp_itemsize@ */
1039 group_pydealloc
, /* @tp_dealloc@ */
1041 0, /* @tp_getattr@ */
1042 0, /* @tp_setattr@ */
1043 0, /* @tp_compare@ */
1045 0, /* @tp_as_number@ */
1046 0, /* @tp_as_sequence@ */
1047 0, /* @tp_as_mapping@ */
1051 0, /* @tp_getattro@ */
1052 0, /* @tp_setattro@ */
1053 0, /* @tp_as_buffer@ */
1054 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1055 Py_TPFLAGS_BASETYPE
,
1058 "Abstract base class for groups.",
1060 0, /* @tp_traverse@ */
1062 group_pyrichcompare
, /* @tp_richcompare@ */
1063 0, /* @tp_weaklistoffset@ */
1065 0, /* @tp_iternexr@ */
1066 group_pymethods
, /* @tp_methods@ */
1067 0, /* @tp_members@ */
1068 group_pygetset
, /* @tp_getset@ */
1071 0, /* @tp_descr_get@ */
1072 0, /* @tp_descr_set@ */
1073 0, /* @tp_dictoffset@ */
1075 PyType_GenericAlloc
, /* @tp_alloc@ */
1076 abstract_pynew
, /* @tp_new@ */
1077 _PyObject_Del
, /* @tp_free@ */
1081 static PyObject
*pgget_info(PyObject
*me
, void *hunoz
)
1084 gctx_prime
*gg
= (gctx_prime
*)GROUP_G(me
);
1085 dp
.p
= MP_COPY(gg
->mm
.m
);
1086 dp
.q
= MP_COPY(gg
->g
.r
);
1087 dp
.g
= mpmont_reduce(&gg
->mm
, MP_NEW
, gg
->gen
);
1088 return (fginfo_pywrap(&dp
, dhinfo_pytype
));
1091 static PyGetSetDef primegroup_pygetset
[] = {
1092 #define GETSETNAME(op, name) pg##op##_##name
1093 GET (info
, "G.info -> information about the group")
1098 static PyObject
*primegroup_pynew(PyTypeObject
*ty
,
1099 PyObject
*arg
, PyObject
*kw
)
1102 char *kwlist
[] = { "info", 0 };
1104 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", kwlist
,
1107 return (group_dopywrap(ty
, group_prime(FGINFO_DP(i
))));
1110 static PyTypeObject primegroup_pytype_skel
= {
1111 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
1112 "catacomb.PrimeGroup", /* @tp_name@ */
1113 sizeof(group_pyobj
), /* @tp_basicsize@ */
1114 0, /* @tp_itemsize@ */
1116 group_pydealloc
, /* @tp_dealloc@ */
1118 0, /* @tp_getattr@ */
1119 0, /* @tp_setattr@ */
1120 0, /* @tp_compare@ */
1122 0, /* @tp_as_number@ */
1123 0, /* @tp_as_sequence@ */
1124 0, /* @tp_as_mapping@ */
1128 0, /* @tp_getattro@ */
1129 0, /* @tp_setattro@ */
1130 0, /* @tp_as_buffer@ */
1131 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1132 Py_TPFLAGS_BASETYPE
,
1135 "Subgroups of prime fields.",
1137 0, /* @tp_traverse@ */
1139 0, /* @tp_richcompare@ */
1140 0, /* @tp_weaklistoffset@ */
1142 0, /* @tp_iternexr@ */
1143 0, /* @tp_methods@ */
1144 0, /* @tp_members@ */
1145 primegroup_pygetset
, /* @tp_getset@ */
1148 0, /* @tp_descr_get@ */
1149 0, /* @tp_descr_set@ */
1150 0, /* @tp_dictoffset@ */
1152 PyType_GenericAlloc
, /* @tp_alloc@ */
1153 primegroup_pynew
, /* @tp_new@ */
1154 _PyObject_Del
, /* @tp_free@ */
1158 static PyObject
*bgget_info(PyObject
*me
, void *hunoz
)
1161 gctx_bin
*gg
= (gctx_bin
*)GROUP_G(me
);
1162 dp
.p
= MP_COPY(gg
->r
.p
);
1163 dp
.q
= MP_COPY(gg
->g
.r
);
1164 dp
.g
= MP_COPY(gg
->gen
);
1165 return (fginfo_pywrap(&dp
, bindhinfo_pytype
));
1168 static PyGetSetDef bingroup_pygetset
[] = {
1169 #define GETSETNAME(op, name) bg##op##_##name
1170 GET (info
, "G.info -> information about the group")
1175 static PyObject
*bingroup_pynew(PyTypeObject
*ty
,
1176 PyObject
*arg
, PyObject
*kw
)
1179 char *kwlist
[] = { "info", 0 };
1181 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", kwlist
,
1182 bindhinfo_pytype
, &i
))
1184 return (group_dopywrap(ty
, group_binary(FGINFO_DP(i
))));
1187 static PyTypeObject bingroup_pytype_skel
= {
1188 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
1189 "catacomb.BinGroup", /* @tp_name@ */
1190 sizeof(group_pyobj
), /* @tp_basicsize@ */
1191 0, /* @tp_itemsize@ */
1193 group_pydealloc
, /* @tp_dealloc@ */
1195 0, /* @tp_getattr@ */
1196 0, /* @tp_setattr@ */
1197 0, /* @tp_compare@ */
1199 0, /* @tp_as_number@ */
1200 0, /* @tp_as_sequence@ */
1201 0, /* @tp_as_mapping@ */
1205 0, /* @tp_getattro@ */
1206 0, /* @tp_setattro@ */
1207 0, /* @tp_as_buffer@ */
1208 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1209 Py_TPFLAGS_BASETYPE
,
1212 "Subgroups of binary fields.",
1214 0, /* @tp_traverse@ */
1216 0, /* @tp_richcompare@ */
1217 0, /* @tp_weaklistoffset@ */
1219 0, /* @tp_iternexr@ */
1220 0, /* @tp_methods@ */
1221 0, /* @tp_members@ */
1222 bingroup_pygetset
, /* @tp_getset@ */
1225 0, /* @tp_descr_get@ */
1226 0, /* @tp_descr_set@ */
1227 0, /* @tp_dictoffset@ */
1229 PyType_GenericAlloc
, /* @tp_alloc@ */
1230 bingroup_pynew
, /* @tp_new@ */
1231 _PyObject_Del
, /* @tp_free@ */
1235 static PyObject
*egget_info(PyObject
*me
, void *hunoz
)
1238 gctx_ec
*gg
= (gctx_ec
*)GROUP_G(me
);
1240 ecinfo_copy(&ei
, &gg
->ei
);
1241 return (ecinfo_pywrap(&ei
));
1244 static PyGetSetDef ecgroup_pygetset
[] = {
1245 #define GETSETNAME(op, name) eg##op##_##name
1246 GET (info
, "G.info -> information about the group")
1251 static PyObject
*ecgroup_pynew(PyTypeObject
*ty
,
1252 PyObject
*arg
, PyObject
*kw
)
1256 char *kwlist
[] = { "info", 0 };
1258 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", kwlist
,
1261 ecinfo_copy(&ei
, ECINFO_EI(i
));
1262 return (group_dopywrap(ty
, group_ec(&ei
)));
1265 static PyTypeObject ecgroup_pytype_skel
= {
1266 PyObject_HEAD_INIT(&PyType_Type
) 0, /* Header */
1267 "catacomb.ECGroup", /* @tp_name@ */
1268 sizeof(group_pyobj
), /* @tp_basicsize@ */
1269 0, /* @tp_itemsize@ */
1271 group_pydealloc
, /* @tp_dealloc@ */
1273 0, /* @tp_getattr@ */
1274 0, /* @tp_setattr@ */
1275 0, /* @tp_compare@ */
1277 0, /* @tp_as_number@ */
1278 0, /* @tp_as_sequence@ */
1279 0, /* @tp_as_mapping@ */
1283 0, /* @tp_getattro@ */
1284 0, /* @tp_setattro@ */
1285 0, /* @tp_as_buffer@ */
1286 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1287 Py_TPFLAGS_BASETYPE
,
1290 "Elliptic curve groups.",
1292 0, /* @tp_traverse@ */
1294 0, /* @tp_richcompare@ */
1295 0, /* @tp_weaklistoffset@ */
1297 0, /* @tp_iternexr@ */
1298 0, /* @tp_methods@ */
1299 0, /* @tp_members@ */
1300 ecgroup_pygetset
, /* @tp_getset@ */
1303 0, /* @tp_descr_get@ */
1304 0, /* @tp_descr_set@ */
1305 0, /* @tp_dictoffset@ */
1307 PyType_GenericAlloc
, /* @tp_alloc@ */
1308 ecgroup_pynew
, /* @tp_new@ */
1309 _PyObject_Del
, /* @tp_free@ */
1313 /*----- Global stuff ------------------------------------------------------*/
1315 static PyMethodDef methods
[] = {
1316 #define METHNAME(name) meth_##name
1317 METH (_GE_frombuf
, "frombuf(BUF) -> X, REST")
1318 METH (_GE_fromraw
, "fromraw(BUF) -> X, REST")
1319 METH (_GE_fromstring
, "fromstring(STR) -> X, REST")
1320 METH (_Group_parse
, "parse(STR) -> G, REST")
1321 METH (_DHInfo_parse
, "parse(STR) -> D, REST")
1322 METH (_BinDHInfo_parse
, "parse(STR) -> D, REST")
1323 METH (_DHInfo__groupn
, 0)
1324 METH (_BinDHInfo__groupn
, 0)
1325 KWMETH(_DHInfo_generate
, "\
1326 generate(PBITS, [qbits = 0, event = pgen_nullev,
1327 rng = rand, nsteps = 0]) -> D")
1328 KWMETH(_DHInfo_genlimlee
, "\
1329 genlimlee(PBITS, QBITS, [event = pgen_nullev, ievent = pgen_nullev,\n\
1330 rng = rand, nsteps = 0, subgroupp = True]) -> (D, [Q, ...])")
1331 KWMETH(_DHInfo_gendsa
, "\
1332 gendsa(PBITS, QBITS, SEED, [event = pgen_nullev, nsteps = 0])\n\
1333 -> (D, SEED, COUNT)")
1338 void group_pyinit(void)
1340 INITTYPE(fginfo
, root
);
1341 INITTYPE(dhinfo
, fginfo
);
1342 INITTYPE(bindhinfo
, dhinfo
);
1344 INITTYPE(group
, type
);
1345 INITTYPE(primegroup
, group
);
1346 INITTYPE(bingroup
, group
);
1347 INITTYPE(ecgroup
, group
);
1348 addmethods(methods
);
1351 void group_pyinsert(PyObject
*mod
)
1353 INSERT("FGInfo", fginfo_pytype
);
1354 INSERT("DHInfo", dhinfo_pytype
);
1355 INSERT("BinDHInfo", bindhinfo_pytype
);
1356 INSERT("GE", ge_pytype
);
1357 INSERT("Group", group_pytype
);
1358 INSERT("PrimeGroup", primegroup_pytype
);
1359 INSERT("BinGroup", bingroup_pytype
);
1360 INSERT("ECGroup", ecgroup_pytype
);
1361 INSERT("_pgroups", namedgroups(ptab
, &npgroups
));
1362 INSERT("_bingroups", namedgroups(bintab
, &nbingroups
));
1365 /*----- That's all, folks -------------------------------------------------*/