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
;
95 struct excinfo exc
= EXCINFO_INIT
;
96 pypgev evt
= { { 0 } };
98 { "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
.ev
.proc
, evt
.ev
.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 struct excinfo exc
= EXCINFO_INIT
;
123 pypgev oe
= { { 0 } }, ie
= { { 0 } };
126 char *kwlist
[] = { "class", "pbits", "qbits", "event", "ievent",
127 "rng", "nsteps", "subgroupp", 0 };
130 PyObject
*rc
= 0, *vec
= 0;
132 oe
.exc
= ie
.exc
= &exc
;
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
.ev
.proc
, oe
.ev
.ctx
, ie
.ev
.proc
, ie
.ev
.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_genkcdsa(PyObject
*me
,
155 PyObject
*arg
, PyObject
*kw
)
160 grand
*r
= &rand_global
;
161 struct excinfo exc
= EXCINFO_INIT
;
162 pypgev evt
= { { 0 } };
163 char *kwlist
[] = { "class", "pbits", "qbits",
164 "event", "rng", "nsteps", 0 };
169 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&O&|O&O&O&:genkcdsa", kwlist
,
170 &me
, convuint
, &pl
, convuint
, &ql
,
171 convpgev
, &evt
, convgrand
, &r
,
174 if (dh_kcdsagen(&dp
, ql
, pl
, 0, steps
, r
, evt
.ev
.proc
, evt
.ev
.ctx
))
176 mp_div(&v
, 0, dp
.p
, dp
.q
);
178 rc
= Py_BuildValue("(NN)", fginfo_pywrap(&dp
, dhinfo_pytype
),
185 static PyObject
*meth__DHInfo_gendsa(PyObject
*me
,
186 PyObject
*arg
, PyObject
*kw
)
194 struct excinfo exc
= EXCINFO_INIT
;
195 pypgev evt
= { { 0 } };
197 { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 };
201 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&O&s#|O&O&:gendsa", kwlist
,
202 &me
, convuint
, &pl
, convuint
, &ql
,
203 &k
, &ksz
, convpgev
, &evt
,
206 if (dsa_gen(&dp
, ql
, pl
, steps
, k
, ksz
, &ds
, evt
.ev
.proc
, evt
.ev
.ctx
))
208 rc
= Py_BuildValue("(NNl)", fginfo_pywrap(&dp
, dhinfo_pytype
),
209 bytestring_pywrap(ds
.p
, ds
.sz
), (long)ds
.count
);
216 static int npgroups
= -1, nbingroups
= -1;
218 static PyObject
*namedgroups(const pentry
*pp
, int *ne
)
225 for (i
= 0; pp
[i
].name
; i
++) {
227 for (j
= 0; j
< i
; j
++) {
228 if (pp
[i
].data
== pp
[j
].data
) {
229 c
= PyDict_GetItemString(d
, (/*unconst*/ char *)pp
[j
].name
);
234 c
= PyInt_FromLong(i
);
236 PyDict_SetItemString(d
, (/*unconst*/ char *)p
, c
);
243 static PyObject
*meth__groupn(PyObject
*me
, PyObject
*arg
,
244 PyTypeObject
*ty
, const pentry
*pp
, int ne
)
250 if (!PyArg_ParseTuple(arg
, "Oi:_groupn", &me
, &i
)) goto end
;
251 if (i
< 0 || i
>= ne
) VALERR("group index out of range");
252 dh_infofromdata(&gp
, pp
[i
].data
);
253 rc
= fginfo_pywrap(&gp
, ty
);
258 static PyObject
*meth__DHInfo__groupn(PyObject
*me
, PyObject
*arg
)
259 { return (meth__groupn(me
, arg
, dhinfo_pytype
, ptab
, npgroups
)); }
261 static PyObject
*meth__BinDHInfo__groupn(PyObject
*me
, PyObject
*arg
)
262 { return (meth__groupn(me
, arg
, bindhinfo_pytype
, bintab
, nbingroups
)); }
264 static PyObject
*meth__parse(PyObject
*me
, PyObject
*arg
, PyTypeObject
*ty
,
265 int (*parse
)(qd_parse
*, gprime_param
*))
272 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
)) goto end
;
275 if (parse(&qd
, &gp
)) VALERR(qd
.e
);
276 rc
= fginfo_pywrap(&gp
, ty
);
281 static PyObject
*meth__DHInfo_parse(PyObject
*me
, PyObject
*arg
)
282 { return (meth__parse(me
, arg
, dhinfo_pytype
, dh_parse
)); }
284 static PyObject
*meth__BinDHInfo_parse(PyObject
*me
, PyObject
*arg
)
285 { return (meth__parse(me
, arg
, bindhinfo_pytype
, dhbin_parse
)); }
287 static PyGetSetDef fginfo_pygetset
[] = {
288 #define GETSETNAME(op, name) fi##op##_##name
289 GET (r
, "I.r -> group order")
294 static PyGetSetDef dhinfo_pygetset
[] = {
295 #define GETSETNAME(op, name) di##op##_##name
296 GET (p
, "I.p -> prime")
297 GET (g
, "I.g -> generator")
302 static PyGetSetDef bindhinfo_pygetset
[] = {
303 #define GETSETNAME(op, name) bi##op##_##name
304 GET (p
, "I.p -> irreducible polynomial")
305 GET (m
, "I.m -> degree of polynomial")
306 GET (g
, "I.g -> generator")
311 static PyTypeObject fginfo_pytype_skel
= {
312 PyObject_HEAD_INIT(0) 0, /* Header */
313 "FGInfo", /* @tp_name@ */
314 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
315 0, /* @tp_itemsize@ */
317 fginfo_pydealloc
, /* @tp_dealloc@ */
319 0, /* @tp_getattr@ */
320 0, /* @tp_setattr@ */
321 0, /* @tp_compare@ */
323 0, /* @tp_as_number@ */
324 0, /* @tp_as_sequence@ */
325 0, /* @tp_as_mapping@ */
329 0, /* @tp_getattro@ */
330 0, /* @tp_setattro@ */
331 0, /* @tp_as_buffer@ */
332 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
336 "Abstract base class for field-group information objects.",
338 0, /* @tp_traverse@ */
340 0, /* @tp_richcompare@ */
341 0, /* @tp_weaklistoffset@ */
343 0, /* @tp_iternext@ */
344 0, /* @tp_methods@ */
345 0, /* @tp_members@ */
346 fginfo_pygetset
, /* @tp_getset@ */
349 0, /* @tp_descr_get@ */
350 0, /* @tp_descr_set@ */
351 0, /* @tp_dictoffset@ */
353 PyType_GenericAlloc
, /* @tp_alloc@ */
354 abstract_pynew
, /* @tp_new@ */
359 static PyTypeObject dhinfo_pytype_skel
= {
360 PyObject_HEAD_INIT(0) 0, /* Header */
361 "DHInfo", /* @tp_name@ */
362 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
363 0, /* @tp_itemsize@ */
365 fginfo_pydealloc
, /* @tp_dealloc@ */
367 0, /* @tp_getattr@ */
368 0, /* @tp_setattr@ */
369 0, /* @tp_compare@ */
371 0, /* @tp_as_number@ */
372 0, /* @tp_as_sequence@ */
373 0, /* @tp_as_mapping@ */
377 0, /* @tp_getattro@ */
378 0, /* @tp_setattro@ */
379 0, /* @tp_as_buffer@ */
380 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
384 "Standard (integer) Diffie-Hellman group information.",
386 0, /* @tp_traverse@ */
388 0, /* @tp_richcompare@ */
389 0, /* @tp_weaklistoffset@ */
391 0, /* @tp_iternext@ */
392 0, /* @tp_methods@ */
393 0, /* @tp_members@ */
394 dhinfo_pygetset
, /* @tp_getset@ */
397 0, /* @tp_descr_get@ */
398 0, /* @tp_descr_set@ */
399 0, /* @tp_dictoffset@ */
401 PyType_GenericAlloc
, /* @tp_alloc@ */
402 fginfo_pynew
, /* @tp_new@ */
407 static PyTypeObject bindhinfo_pytype_skel
= {
408 PyObject_HEAD_INIT(0) 0, /* Header */
409 "BinDHInfo", /* @tp_name@ */
410 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
411 0, /* @tp_itemsize@ */
413 fginfo_pydealloc
, /* @tp_dealloc@ */
415 0, /* @tp_getattr@ */
416 0, /* @tp_setattr@ */
417 0, /* @tp_compare@ */
419 0, /* @tp_as_number@ */
420 0, /* @tp_as_sequence@ */
421 0, /* @tp_as_mapping@ */
425 0, /* @tp_getattro@ */
426 0, /* @tp_setattro@ */
427 0, /* @tp_as_buffer@ */
428 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
432 "Binary-field Diffie-Hellman group information.",
434 0, /* @tp_traverse@ */
436 0, /* @tp_richcompare@ */
437 0, /* @tp_weaklistoffset@ */
439 0, /* @tp_iternext@ */
440 0, /* @tp_methods@ */
441 0, /* @tp_members@ */
442 bindhinfo_pygetset
, /* @tp_getset@ */
445 0, /* @tp_descr_get@ */
446 0, /* @tp_descr_set@ */
447 0, /* @tp_dictoffset@ */
449 PyType_GenericAlloc
, /* @tp_alloc@ */
450 fginfo_pynew
, /* @tp_new@ */
455 /*----- General utilities -------------------------------------------------*/
457 PyTypeObject
*ge_pytype
, *group_pytype
;
458 PyTypeObject
*primegroup_pytype
, *bingroup_pytype
, *ecgroup_pytype
;
460 group
*group_copy(group
*g
)
462 if (strcmp(G_NAME(g
), "prime") == 0) {
463 gctx_prime
*gc
= (gctx_prime
*)g
;
465 gp
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
468 g
= group_prime(&gp
);
470 } else if (strcmp(G_NAME(g
), "bin") == 0) {
471 gctx_bin
*gc
= (gctx_bin
*)g
;
473 gb
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
476 g
= group_binary(&gb
);
478 } else if (strcmp(G_NAME(g
), "ec") == 0) {
479 gctx_ec
*gc
= (gctx_ec
*)g
;
481 if ((ei
.c
= eccurve_copy(gc
->ei
.c
)) == 0)
484 EC_COPY(&ei
.g
, &gc
->ei
.g
);
485 ei
.r
= MP_COPY(gc
->ei
.r
);
486 ei
.h
= MP_COPY(gc
->ei
.h
);
493 PyObject
*ge_pywrap(PyObject
*gobj
, ge
*x
)
495 ge_pyobj
*z
= PyObject_New(ge_pyobj
, (PyTypeObject
*)gobj
);
497 z
->g
= GROUP_G(gobj
);
499 return ((PyObject
*)z
);
502 static PyObject
*ge_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
504 char *kwlist
[] = { "x", 0 };
513 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", kwlist
, &x
)) goto end
;
515 if (ECPT_PYCHECK(x
)) {
517 if (G_FROMEC(g
, xx
, &p
))
518 TYERR("can't convert from elliptic curve point");
520 } else if ((y
= tomp(x
)) != 0) {
521 if (G_FROMINT(g
, xx
, y
))
522 TYERR("can't convert from integer");
524 } else if (PyString_Check(x
)) {
525 sc
.buf
= PyString_AS_STRING(x
);
526 sc
.lim
= sc
.buf
+ PyString_GET_SIZE(x
);
527 if (G_READ(g
, xx
, &mptext_stringops
, &sc
) || sc
.buf
< sc
.lim
)
528 VALERR("malformed group element string");
530 TYERR("can't convert to group element");
531 return (ge_pywrap((PyObject
*)ty
, xx
));
535 if (xx
) G_DESTROY(g
, xx
);
539 static PyObject
*group_dopywrap(PyTypeObject
*ty
, group
*g
)
541 group_pyobj
*gobj
= newtype(ty
, 0, g
->ops
->name
);
543 gobj
->ty
.ht_type
.tp_basicsize
= sizeof(ge_pyobj
);
544 gobj
->ty
.ht_type
.tp_base
= ge_pytype
;
545 Py_INCREF(group_pytype
);
546 gobj
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
547 Py_TPFLAGS_BASETYPE
|
548 Py_TPFLAGS_CHECKTYPES
|
549 Py_TPFLAGS_HEAPTYPE
);
550 gobj
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
551 gobj
->ty
.ht_type
.tp_free
= 0;
552 gobj
->ty
.ht_type
.tp_new
= ge_pynew
;
553 typeready(&gobj
->ty
.ht_type
);
554 return ((PyObject
*)gobj
);
557 PyObject
*group_pywrap(group
*g
)
561 if (strcmp(G_NAME(g
), "prime") == 0) ty
= primegroup_pytype
;
562 else if (strcmp(G_NAME(g
), "bin") == 0) ty
= bingroup_pytype
;
563 else if (strcmp(G_NAME(g
), "ec") == 0) ty
= ecgroup_pytype
;
565 return (group_dopywrap(ty
, g
));
568 /*----- Group elements ----------------------------------------------------*/
570 #define BINOP(name) \
571 static PyObject *ge_py##name(PyObject *x, PyObject *y) \
575 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \
576 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \
580 g->ops->name(g, z, GE_X(x), GE_X(y)); \
581 return (ge_pywrap(GE_GOBJ(x), z)); \
588 static PyObject *gemeth_##name(PyObject *me, PyObject *arg) \
592 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
595 g->ops->name(g, z, GE_X(me)); \
596 return (ge_pywrap(GE_GOBJ(me), z)); \
602 static PyObject
*ge_pyexp(PyObject
*x
, PyObject
*n
, PyObject
*m
)
607 if (m
!= Py_None
|| !GE_PYCHECK(x
) || (nn
= getmp(n
)) == 0)
609 z
= G_CREATE(GE_G(x
));
610 G_EXP(GE_G(x
), z
, GE_X(x
), nn
);
612 return (ge_pywrap(GE_GOBJ(x
), z
));
615 static void ge_pydealloc(PyObject
*me
)
617 G_DESTROY(GE_G(me
), GE_X(me
));
618 Py_DECREF(GE_GOBJ(me
));
622 static void group_pydealloc(PyObject
*me
)
624 G_DESTROYGROUP(GROUP_G(me
));
625 PyType_Type
.tp_dealloc(me
);
628 static PyObject
*gmexp_id(PyObject
*me
)
630 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
631 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
634 static int gmexp_fill(void *pp
, PyObject
*me
, PyObject
*x
, PyObject
*m
)
636 group_expfactor
*f
= pp
;
638 if (!GE_PYCHECK(x
) || GE_G(x
) != GROUP_G(me
) || (f
->exp
= getmp(m
)) == 0)
644 static PyObject
*ge_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
649 if (!GE_PYCHECK(x
) || !GE_PYCHECK(y
) ||
650 (GE_G(x
) != GE_G(y
) && !group_samep(GE_G(x
), GE_G(y
))))
653 case Py_EQ
: b
= G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
654 case Py_NE
: b
= !G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
655 default: TYERR("group elements are unordered");
662 static PyObject
*gemeth_check(PyObject
*me
, PyObject
*arg
)
664 if (!PyArg_ParseTuple(arg
, ":check")) goto end
;
665 if (group_check(GE_G(me
), GE_X(me
))) VALERR("bad group element");
671 static int ge_pynonzerop(PyObject
*x
)
672 { return (!G_IDENTP(GE_G(x
), GE_X(x
))); }
674 static PyObject
*ge_pystr(PyObject
*me
)
679 group_writedstr(GE_G(me
), GE_X(me
), &d
);
680 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
685 static PyObject
*ge_pylong(PyObject
*me
)
690 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
691 TYERR("can't convert to integer");
698 static PyObject
*ge_pyint(PyObject
*me
)
704 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
705 TYERR("can't convert to integer");
706 if (!mp_tolong_checked(x
, &l
, 0)) rc
= PyInt_FromLong(l
);
707 else rc
= mp_topylong(x
);
713 static PyObject
*gemeth_toint(PyObject
*me
, PyObject
*arg
)
717 if (!PyArg_ParseTuple(arg
, ":toint")) goto end
;
718 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
719 TYERR("can't convert to integer");
720 return (mp_pywrap(x
));
725 static PyObject
*gemeth_toec(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
727 char *kwlist
[] = { "curve", 0 };
728 PyTypeObject
*cty
= 0;
734 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:toec", kwlist
,
736 g
= GROUP_G(GE_GOBJ(me
));
738 if (!PyType_Check(cty
) || !PyType_IsSubtype(cty
, ecpt_pytype
))
739 TYERR("want subtype of catacomb.ECPt");
740 Py_INCREF((PyObject
*)cty
);
741 } else if (strcmp(G_NAME(g
), "ec") == 0) {
742 c
= eccurve_copy(((gctx_ec
*)g
)->ei
.c
);
743 cty
= (PyTypeObject
*)eccurve_pywrap(0, c
);
746 Py_INCREF((PyObject
*)cty
);
748 if (G_TOEC(GE_G(me
), &p
, GE_X(me
))) {
749 Py_DECREF((PyObject
*)cty
);
750 TYERR("can't convert to ec point");
752 rc
= ecpt_pywrapout(cty
, &p
);
753 Py_DECREF((PyObject
*)cty
);
758 static PyObject
*gemeth_tobuf(PyObject
*me
, PyObject
*arg
)
764 if (!PyArg_ParseTuple(arg
, ":tobuf")) return (0);
765 n
= GE_G(me
)->noctets
+ 4;
766 rc
= bytestring_pywrap(0, n
);
767 buf_init(&b
, PyString_AS_STRING(rc
), n
);
768 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
770 _PyString_Resize(&rc
, BLEN(&b
));
774 static PyObject
*gemeth_toraw(PyObject
*me
, PyObject
*arg
)
780 if (!PyArg_ParseTuple(arg
, ":toraw")) return (0);
781 n
= GE_G(me
)->noctets
;
782 rc
= bytestring_pywrap(0, n
);
783 buf_init(&b
, PyString_AS_STRING(rc
), n
);
784 G_TORAW(GE_G(me
), &b
, GE_X(me
));
786 _PyString_Resize(&rc
, BLEN(&b
));
790 static PyObject
*gmexp_exp(PyObject
*me
, void *pp
, int n
)
792 ge
*z
= G_CREATE(GROUP_G(me
));
793 G_MEXP(GROUP_G(me
), z
, pp
, n
);
794 return (ge_pywrap(me
, z
));
797 static void gmexp_drop(void *pp
)
799 group_expfactor
*f
= pp
;
803 static PyObject
*gmeth_mexp(PyObject
*me
, PyObject
*arg
)
805 return (mexp_common(me
, arg
, sizeof(group_expfactor
),
806 gmexp_id
, gmexp_fill
, gmexp_exp
, gmexp_drop
));
809 static PyObject
*gmeth_checkgroup(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
811 char *kwlist
[] = { "rng", 0 };
812 grand
*r
= &rand_global
;
815 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:checkgroup", kwlist
,
818 if ((p
= G_CHECK(GROUP_G(me
), r
)) != 0)
825 static PyObject
*group_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
827 int b
= group_samep(GROUP_G(x
), GROUP_G(y
));
831 default: TYERR("can't order groups");
838 static PyObject
*meth__GE_frombuf(PyObject
*me
, PyObject
*arg
)
846 if (!PyArg_ParseTuple(arg
, "Os#:frombuf", &me
, &p
, &n
))
851 if (G_FROMBUF(g
, &b
, x
))
852 VALERR("invalid data");
853 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
855 if (x
) G_DESTROY(g
, x
);
859 static PyObject
*meth__GE_fromraw(PyObject
*me
, PyObject
*arg
)
867 if (!PyArg_ParseTuple(arg
, "Os#:fromraw", &me
, &p
, &n
))
872 if (G_FROMRAW(g
, &b
, x
))
873 VALERR("invalid data");
874 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
876 if (x
) G_DESTROY(g
, x
);
880 static PyObject
*meth__GE_fromstring(PyObject
*me
, PyObject
*arg
)
888 if (!PyArg_ParseTuple(arg
, "Os#:fromstring", &me
, &p
, &n
))
894 if (G_READ(g
, x
, &mptext_stringops
, &sc
))
895 VALERR("bad group element string");
896 return (Py_BuildValue("(Ns#)", ge_pywrap(me
, x
),
897 sc
.buf
, (Py_ssize_t
)(sc
.lim
- sc
.buf
)));
899 if (x
) G_DESTROY(g
, x
);
903 static PyObject
*meth__Group_parse(PyObject
*me
, PyObject
*arg
)
909 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
))
913 if ((g
= group_parse(&qd
)) == 0)
915 return (group_pywrap(g
));
920 static PyObject
*geget_group(PyObject
*me
, void *hunoz
)
921 { RETURN_OBJ(GE_GOBJ(me
)); }
923 static PyObject
*gget_nbits(PyObject
*me
, void *hunoz
)
924 { return (PyInt_FromLong(GROUP_G(me
)->nbits
)); }
926 static PyObject
*gget_noctets(PyObject
*me
, void *hunoz
)
927 { return (PyInt_FromLong(GROUP_G(me
)->noctets
)); }
929 static PyObject
*gget_i(PyObject
*me
, void *hunoz
)
931 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
932 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
935 static PyObject
*gget_g(PyObject
*me
, void *hunoz
)
937 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
938 G_COPY(g
, x
, g
->g
); return (ge_pywrap(me
, x
));
941 static long ge_pyhash(PyObject
*me
)
944 size_t sz
= GE_G(me
)->noctets
+ 4;
945 uint32 h
= 0xf672c776 + GE_G(me
)->ops
->ty
;
946 octet
*p
= xmalloc(sz
);
948 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
950 h
= unihash_hash(&unihash_global
, h
, BBASE(&b
), BLEN(&b
));
952 return (h
% LONG_MAX
);
955 static PyObject
*gget_r(PyObject
*me
, void *hunoz
)
956 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->r
))); }
958 static PyObject
*gget_h(PyObject
*me
, void *hunoz
)
959 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->h
))); }
961 static PyGetSetDef ge_pygetset
[] = {
962 #define GETSETNAME(op, name) ge##op##_##name
963 GET (group
, "X.group -> group containing X")
968 static PyMethodDef ge_pymethods
[] = {
969 #define METHNAME(name) gemeth_##name
970 METH (inv
, "X.inv() -> inverse element of X")
971 METH (sqr
, "X.sqr() -> X^2 = X * X")
972 METH (check
, "X.check() -> check X really belongs to its group")
973 METH (toint
, "X.toint() -> X converted to an integer")
975 X.toec([curve = ECPt]) -> X converted to elliptic curve point")
976 METH (tobuf
, "X.tobuf() -> X in buffer representation")
977 METH (toraw
, "X.toraw() -> X in raw representation")
982 static PyNumberMethods ge_pynumber
= {
984 0, /* @nb_subtract@ */
985 ge_pymul
, /* @nb_multiply@ */
986 ge_pydiv
, /* @nb_divide@ */
987 0, /* @nb_remainder@ */
989 ge_pyexp
, /* @nb_power@ */
990 0, /* @nb_negative@ */
991 0, /* @nb_positive@ */
992 0, /* @nb_absolute@ */
993 ge_pynonzerop
, /* @nb_nonzero@ */
1000 0, /* @nb_coerce@ */
1001 ge_pyint
, /* @nb_int@ */
1002 ge_pylong
, /* @nb_long@ */
1003 0 /* meaningless */, /* @nb_float@ */
1007 0, /* @nb_inplace_add@ */
1008 0, /* @nb_inplace_subtract@ */
1009 0, /* @nb_inplace_multiply@ */
1010 0, /* @nb_inplace_divide@ */
1011 0, /* @nb_inplace_remainder@ */
1012 0, /* @nb_inplace_power@ */
1013 0, /* @nb_inplace_lshift@ */
1014 0, /* @nb_inplace_rshift@ */
1015 0, /* @nb_inplace_and@ */
1016 0, /* @nb_inplace_xor@ */
1017 0, /* @nb_inplace_or@ */
1019 0, /* @nb_floor_divide@ */
1020 ge_pydiv
, /* @nb_true_divide@ */
1021 0, /* @nb_inplace_floor_divide@ */
1022 0, /* @nb_inplace_true_divide@ */
1025 static PyTypeObject ge_pytype_skel
= {
1026 PyObject_HEAD_INIT(0) 0, /* Header */
1027 "GE", /* @tp_name@ */
1028 sizeof(ge_pyobj
), /* @tp_basicsize@ */
1029 0, /* @tp_itemsize@ */
1031 ge_pydealloc
, /* @tp_dealloc@ */
1033 0, /* @tp_getattr@ */
1034 0, /* @tp_setattr@ */
1035 0, /* @tp_compare@ */
1037 &ge_pynumber
, /* @tp_as_number@ */
1038 0, /* @tp_as_sequence@ */
1039 0, /* @tp_as_mapping@ */
1040 ge_pyhash
, /* @tp_hash@ */
1042 ge_pystr
, /* @tp_str@ */
1043 0, /* @tp_getattro@ */
1044 0, /* @tp_setattro@ */
1045 0, /* @tp_as_buffer@ */
1046 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1047 Py_TPFLAGS_CHECKTYPES
|
1048 Py_TPFLAGS_BASETYPE
,
1051 "Group elements, abstract base class.",
1053 0, /* @tp_traverse@ */
1055 ge_pyrichcompare
, /* @tp_richcompare@ */
1056 0, /* @tp_weaklistoffset@ */
1058 0, /* @tp_iternext@ */
1059 ge_pymethods
, /* @tp_methods@ */
1060 0, /* @tp_members@ */
1061 ge_pygetset
, /* @tp_getset@ */
1064 0, /* @tp_descr_get@ */
1065 0, /* @tp_descr_set@ */
1066 0, /* @tp_dictoffset@ */
1068 PyType_GenericAlloc
, /* @tp_alloc@ */
1069 abstract_pynew
, /* @tp_new@ */
1074 static PyGetSetDef group_pygetset
[] = {
1075 #define GETSETNAME(op, name) g##op##_##name
1076 GET (noctets
, "G.noctets -> size in octets of element")
1077 GET (nbits
, "G.nbits -> size in bits of element")
1078 GET (i
, "G.i -> group identity")
1079 GET (g
, "G.g -> group generator")
1080 GET (r
, "G.r -> group order")
1081 GET (h
, "G.h -> group cofactor")
1086 static PyMethodDef group_pymethods
[] = {
1087 #define METHNAME(name) gmeth_##name
1089 G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1090 KWMETH(checkgroup
, "G.checkgroup([rng = rand]): check group is good")
1095 static PyTypeObject group_pytype_skel
= {
1096 PyObject_HEAD_INIT(0) 0, /* Header */
1097 "Group", /* @tp_name@ */
1098 sizeof(group_pyobj
), /* @tp_basicsize@ */
1099 0, /* @tp_itemsize@ */
1101 group_pydealloc
, /* @tp_dealloc@ */
1103 0, /* @tp_getattr@ */
1104 0, /* @tp_setattr@ */
1105 0, /* @tp_compare@ */
1107 0, /* @tp_as_number@ */
1108 0, /* @tp_as_sequence@ */
1109 0, /* @tp_as_mapping@ */
1113 0, /* @tp_getattro@ */
1114 0, /* @tp_setattro@ */
1115 0, /* @tp_as_buffer@ */
1116 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1117 Py_TPFLAGS_BASETYPE
,
1120 "Abstract base class for groups.",
1122 0, /* @tp_traverse@ */
1124 group_pyrichcompare
, /* @tp_richcompare@ */
1125 0, /* @tp_weaklistoffset@ */
1127 0, /* @tp_iternext@ */
1128 group_pymethods
, /* @tp_methods@ */
1129 0, /* @tp_members@ */
1130 group_pygetset
, /* @tp_getset@ */
1133 0, /* @tp_descr_get@ */
1134 0, /* @tp_descr_set@ */
1135 0, /* @tp_dictoffset@ */
1137 PyType_GenericAlloc
, /* @tp_alloc@ */
1138 abstract_pynew
, /* @tp_new@ */
1143 static PyObject
*pgget_info(PyObject
*me
, void *hunoz
)
1146 gctx_prime
*gg
= (gctx_prime
*)GROUP_G(me
);
1147 dp
.p
= MP_COPY(gg
->mm
.m
);
1148 dp
.q
= MP_COPY(gg
->g
.r
);
1149 dp
.g
= mpmont_reduce(&gg
->mm
, MP_NEW
, gg
->gen
.x
);
1150 return (fginfo_pywrap(&dp
, dhinfo_pytype
));
1153 static PyGetSetDef primegroup_pygetset
[] = {
1154 #define GETSETNAME(op, name) pg##op##_##name
1155 GET (info
, "G.info -> information about the group")
1160 static PyObject
*primegroup_pynew(PyTypeObject
*ty
,
1161 PyObject
*arg
, PyObject
*kw
)
1164 char *kwlist
[] = { "info", 0 };
1166 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", kwlist
,
1169 return (group_dopywrap(ty
, group_prime(FGINFO_DP(i
))));
1172 static PyTypeObject primegroup_pytype_skel
= {
1173 PyObject_HEAD_INIT(0) 0, /* Header */
1174 "PrimeGroup", /* @tp_name@ */
1175 sizeof(group_pyobj
), /* @tp_basicsize@ */
1176 0, /* @tp_itemsize@ */
1178 group_pydealloc
, /* @tp_dealloc@ */
1180 0, /* @tp_getattr@ */
1181 0, /* @tp_setattr@ */
1182 0, /* @tp_compare@ */
1184 0, /* @tp_as_number@ */
1185 0, /* @tp_as_sequence@ */
1186 0, /* @tp_as_mapping@ */
1190 0, /* @tp_getattro@ */
1191 0, /* @tp_setattro@ */
1192 0, /* @tp_as_buffer@ */
1193 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1194 Py_TPFLAGS_BASETYPE
,
1197 "Subgroups of prime fields.",
1199 0, /* @tp_traverse@ */
1201 0, /* @tp_richcompare@ */
1202 0, /* @tp_weaklistoffset@ */
1204 0, /* @tp_iternext@ */
1205 0, /* @tp_methods@ */
1206 0, /* @tp_members@ */
1207 primegroup_pygetset
, /* @tp_getset@ */
1210 0, /* @tp_descr_get@ */
1211 0, /* @tp_descr_set@ */
1212 0, /* @tp_dictoffset@ */
1214 PyType_GenericAlloc
, /* @tp_alloc@ */
1215 primegroup_pynew
, /* @tp_new@ */
1220 static PyObject
*bgget_info(PyObject
*me
, void *hunoz
)
1223 gctx_bin
*gg
= (gctx_bin
*)GROUP_G(me
);
1224 dp
.p
= MP_COPY(gg
->r
.p
);
1225 dp
.q
= MP_COPY(gg
->g
.r
);
1226 dp
.g
= MP_COPY(gg
->gen
.x
);
1227 return (fginfo_pywrap(&dp
, bindhinfo_pytype
));
1230 static PyGetSetDef bingroup_pygetset
[] = {
1231 #define GETSETNAME(op, name) bg##op##_##name
1232 GET (info
, "G.info -> information about the group")
1237 static PyObject
*bingroup_pynew(PyTypeObject
*ty
,
1238 PyObject
*arg
, PyObject
*kw
)
1241 char *kwlist
[] = { "info", 0 };
1243 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", kwlist
,
1244 bindhinfo_pytype
, &i
))
1246 return (group_dopywrap(ty
, group_binary(FGINFO_DP(i
))));
1249 static PyTypeObject bingroup_pytype_skel
= {
1250 PyObject_HEAD_INIT(0) 0, /* Header */
1251 "BinGroup", /* @tp_name@ */
1252 sizeof(group_pyobj
), /* @tp_basicsize@ */
1253 0, /* @tp_itemsize@ */
1255 group_pydealloc
, /* @tp_dealloc@ */
1257 0, /* @tp_getattr@ */
1258 0, /* @tp_setattr@ */
1259 0, /* @tp_compare@ */
1261 0, /* @tp_as_number@ */
1262 0, /* @tp_as_sequence@ */
1263 0, /* @tp_as_mapping@ */
1267 0, /* @tp_getattro@ */
1268 0, /* @tp_setattro@ */
1269 0, /* @tp_as_buffer@ */
1270 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1271 Py_TPFLAGS_BASETYPE
,
1274 "Subgroups of binary fields.",
1276 0, /* @tp_traverse@ */
1278 0, /* @tp_richcompare@ */
1279 0, /* @tp_weaklistoffset@ */
1281 0, /* @tp_iternext@ */
1282 0, /* @tp_methods@ */
1283 0, /* @tp_members@ */
1284 bingroup_pygetset
, /* @tp_getset@ */
1287 0, /* @tp_descr_get@ */
1288 0, /* @tp_descr_set@ */
1289 0, /* @tp_dictoffset@ */
1291 PyType_GenericAlloc
, /* @tp_alloc@ */
1292 bingroup_pynew
, /* @tp_new@ */
1297 static PyObject
*egget_info(PyObject
*me
, void *hunoz
)
1300 gctx_ec
*gg
= (gctx_ec
*)GROUP_G(me
);
1302 ecinfo_copy(&ei
, &gg
->ei
);
1303 return (ecinfo_pywrap(&ei
));
1306 static PyGetSetDef ecgroup_pygetset
[] = {
1307 #define GETSETNAME(op, name) eg##op##_##name
1308 GET (info
, "G.info -> information about the group")
1313 static PyObject
*ecgroup_pynew(PyTypeObject
*ty
,
1314 PyObject
*arg
, PyObject
*kw
)
1318 char *kwlist
[] = { "info", 0 };
1320 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", kwlist
,
1323 ecinfo_copy(&ei
, ECINFO_EI(i
));
1324 return (group_dopywrap(ty
, group_ec(&ei
)));
1327 static PyTypeObject ecgroup_pytype_skel
= {
1328 PyObject_HEAD_INIT(0) 0, /* Header */
1329 "ECGroup", /* @tp_name@ */
1330 sizeof(group_pyobj
), /* @tp_basicsize@ */
1331 0, /* @tp_itemsize@ */
1333 group_pydealloc
, /* @tp_dealloc@ */
1335 0, /* @tp_getattr@ */
1336 0, /* @tp_setattr@ */
1337 0, /* @tp_compare@ */
1339 0, /* @tp_as_number@ */
1340 0, /* @tp_as_sequence@ */
1341 0, /* @tp_as_mapping@ */
1345 0, /* @tp_getattro@ */
1346 0, /* @tp_setattro@ */
1347 0, /* @tp_as_buffer@ */
1348 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1349 Py_TPFLAGS_BASETYPE
,
1352 "Elliptic curve groups.",
1354 0, /* @tp_traverse@ */
1356 0, /* @tp_richcompare@ */
1357 0, /* @tp_weaklistoffset@ */
1359 0, /* @tp_iternext@ */
1360 0, /* @tp_methods@ */
1361 0, /* @tp_members@ */
1362 ecgroup_pygetset
, /* @tp_getset@ */
1365 0, /* @tp_descr_get@ */
1366 0, /* @tp_descr_set@ */
1367 0, /* @tp_dictoffset@ */
1369 PyType_GenericAlloc
, /* @tp_alloc@ */
1370 ecgroup_pynew
, /* @tp_new@ */
1375 /*----- Global stuff ------------------------------------------------------*/
1377 static PyMethodDef methods
[] = {
1378 #define METHNAME(name) meth_##name
1379 METH (_GE_frombuf
, "frombuf(BUF) -> X, REST")
1380 METH (_GE_fromraw
, "fromraw(BUF) -> X, REST")
1381 METH (_GE_fromstring
, "fromstring(STR) -> X, REST")
1382 METH (_Group_parse
, "parse(STR) -> G, REST")
1383 METH (_DHInfo_parse
, "parse(STR) -> D, REST")
1384 METH (_BinDHInfo_parse
, "parse(STR) -> D, REST")
1385 METH (_DHInfo__groupn
, 0)
1386 METH (_BinDHInfo__groupn
, 0)
1387 KWMETH(_DHInfo_generate
, "\
1388 generate(PBITS, [qbits = 0], [event = pgen_nullev],\n\
1389 [rng = rand], [nsteps = 0]) -> D")
1390 KWMETH(_DHInfo_genlimlee
, "\
1391 genlimlee(PBITS, QBITS, [event = pgen_nullev], [ievent = pgen_nullev],\n\
1392 [rng = rand], [nsteps = 0], [subgroupp = True]) -> (D, [Q, ...])")
1393 KWMETH(_DHInfo_gendsa
, "\
1394 gendsa(PBITS, QBITS, SEED, [event = pgen_nullev], [nsteps = 0])\n\
1395 -> (D, SEED, COUNT)")
1396 KWMETH(_DHInfo_genkcdsa
, "\
1397 gendsa(PBITS, QBITS, [event = pgen_nullev], [rng = rand], [nsteps = 0])\n\
1403 void group_pyinit(void)
1405 INITTYPE(fginfo
, root
);
1406 INITTYPE(dhinfo
, fginfo
);
1407 INITTYPE(bindhinfo
, dhinfo
);
1409 INITTYPE(group
, type
);
1410 INITTYPE(primegroup
, group
);
1411 INITTYPE(bingroup
, group
);
1412 INITTYPE(ecgroup
, group
);
1413 addmethods(methods
);
1416 void group_pyinsert(PyObject
*mod
)
1418 INSERT("FGInfo", fginfo_pytype
);
1419 INSERT("DHInfo", dhinfo_pytype
);
1420 INSERT("BinDHInfo", bindhinfo_pytype
);
1421 INSERT("GE", ge_pytype
);
1422 INSERT("Group", group_pytype
);
1423 INSERT("PrimeGroup", primegroup_pytype
);
1424 INSERT("BinGroup", bingroup_pytype
);
1425 INSERT("ECGroup", ecgroup_pytype
);
1426 INSERT("_pgroups", namedgroups(ptab
, &npgroups
));
1427 INSERT("_bingroups", namedgroups(bintab
, &nbingroups
));
1430 /*----- That's all, folks -------------------------------------------------*/