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
;
285 if (parse(&qd
, &gp
)) VALERR(qd
.e
);
286 rc
= fginfo_pywrap(&gp
, ty
);
291 static PyObject
*meth__DHInfo_parse(PyObject
*me
, PyObject
*arg
)
292 { return (meth__parse(me
, arg
, dhinfo_pytype
, dh_parse
)); }
294 static PyObject
*meth__BinDHInfo_parse(PyObject
*me
, PyObject
*arg
)
295 { return (meth__parse(me
, arg
, bindhinfo_pytype
, dhbin_parse
)); }
297 static const PyGetSetDef fginfo_pygetset
[] = {
298 #define GETSETNAME(op, name) fi##op##_##name
299 GET (r
, "I.r -> group order")
304 static const PyGetSetDef dhinfo_pygetset
[] = {
305 #define GETSETNAME(op, name) di##op##_##name
306 GET (p
, "I.p -> prime")
307 GET (g
, "I.g -> generator")
312 static const PyGetSetDef bindhinfo_pygetset
[] = {
313 #define GETSETNAME(op, name) bi##op##_##name
314 GET (p
, "I.p -> irreducible polynomial")
315 GET (m
, "I.m -> degree of polynomial")
316 GET (g
, "I.g -> generator")
321 static PyTypeObject fginfo_pytype_skel
= {
322 PyObject_HEAD_INIT(0) 0, /* Header */
323 "FGInfo", /* @tp_name@ */
324 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
325 0, /* @tp_itemsize@ */
327 fginfo_pydealloc
, /* @tp_dealloc@ */
329 0, /* @tp_getattr@ */
330 0, /* @tp_setattr@ */
331 0, /* @tp_compare@ */
333 0, /* @tp_as_number@ */
334 0, /* @tp_as_sequence@ */
335 0, /* @tp_as_mapping@ */
339 0, /* @tp_getattro@ */
340 0, /* @tp_setattro@ */
341 0, /* @tp_as_buffer@ */
342 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
346 "Abstract base class for field-group information objects.",
348 0, /* @tp_traverse@ */
350 0, /* @tp_richcompare@ */
351 0, /* @tp_weaklistoffset@ */
353 0, /* @tp_iternext@ */
354 0, /* @tp_methods@ */
355 0, /* @tp_members@ */
356 PYGETSET(fginfo
), /* @tp_getset@ */
359 0, /* @tp_descr_get@ */
360 0, /* @tp_descr_set@ */
361 0, /* @tp_dictoffset@ */
363 PyType_GenericAlloc
, /* @tp_alloc@ */
364 abstract_pynew
, /* @tp_new@ */
369 static PyTypeObject dhinfo_pytype_skel
= {
370 PyObject_HEAD_INIT(0) 0, /* Header */
371 "DHInfo", /* @tp_name@ */
372 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
373 0, /* @tp_itemsize@ */
375 fginfo_pydealloc
, /* @tp_dealloc@ */
377 0, /* @tp_getattr@ */
378 0, /* @tp_setattr@ */
379 0, /* @tp_compare@ */
381 0, /* @tp_as_number@ */
382 0, /* @tp_as_sequence@ */
383 0, /* @tp_as_mapping@ */
387 0, /* @tp_getattro@ */
388 0, /* @tp_setattro@ */
389 0, /* @tp_as_buffer@ */
390 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
394 "DHInfo(P, R, G): standard (integer) Diffie-Hellman group information.",
396 0, /* @tp_traverse@ */
398 0, /* @tp_richcompare@ */
399 0, /* @tp_weaklistoffset@ */
401 0, /* @tp_iternext@ */
402 0, /* @tp_methods@ */
403 0, /* @tp_members@ */
404 PYGETSET(dhinfo
), /* @tp_getset@ */
407 0, /* @tp_descr_get@ */
408 0, /* @tp_descr_set@ */
409 0, /* @tp_dictoffset@ */
411 PyType_GenericAlloc
, /* @tp_alloc@ */
412 fginfo_pynew
, /* @tp_new@ */
417 static PyTypeObject bindhinfo_pytype_skel
= {
418 PyObject_HEAD_INIT(0) 0, /* Header */
419 "BinDHInfo", /* @tp_name@ */
420 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
421 0, /* @tp_itemsize@ */
423 fginfo_pydealloc
, /* @tp_dealloc@ */
425 0, /* @tp_getattr@ */
426 0, /* @tp_setattr@ */
427 0, /* @tp_compare@ */
429 0, /* @tp_as_number@ */
430 0, /* @tp_as_sequence@ */
431 0, /* @tp_as_mapping@ */
435 0, /* @tp_getattro@ */
436 0, /* @tp_setattro@ */
437 0, /* @tp_as_buffer@ */
438 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
442 "BinDHInfo(P, R, G): binary-field Diffie-Hellman group information.",
444 0, /* @tp_traverse@ */
446 0, /* @tp_richcompare@ */
447 0, /* @tp_weaklistoffset@ */
449 0, /* @tp_iternext@ */
450 0, /* @tp_methods@ */
451 0, /* @tp_members@ */
452 PYGETSET(bindhinfo
), /* @tp_getset@ */
455 0, /* @tp_descr_get@ */
456 0, /* @tp_descr_set@ */
457 0, /* @tp_dictoffset@ */
459 PyType_GenericAlloc
, /* @tp_alloc@ */
460 fginfo_pynew
, /* @tp_new@ */
465 /*----- General utilities -------------------------------------------------*/
467 PyTypeObject
*ge_pytype
, *group_pytype
;
468 static PyTypeObject
*primegroup_pytype
, *bingroup_pytype
, *ecgroup_pytype
;
470 group
*group_copy(group
*g
)
472 if (STRCMP(G_NAME(g
), ==, "prime")) {
473 gctx_prime
*gc
= (gctx_prime
*)g
;
475 gp
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
478 g
= group_prime(&gp
);
480 } else if (STRCMP(G_NAME(g
), ==, "bin")) {
481 gctx_bin
*gc
= (gctx_bin
*)g
;
483 gb
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
486 g
= group_binary(&gb
);
488 } else if (STRCMP(G_NAME(g
), ==, "ec")) {
489 gctx_ec
*gc
= (gctx_ec
*)g
;
491 if ((ei
.c
= eccurve_copy(gc
->ei
.c
)) == 0)
494 EC_COPY(&ei
.g
, &gc
->ei
.g
);
495 ei
.r
= MP_COPY(gc
->ei
.r
);
496 ei
.h
= MP_COPY(gc
->ei
.h
);
503 PyObject
*ge_pywrap(PyObject
*gobj
, ge
*x
)
505 ge_pyobj
*z
= PyObject_New(ge_pyobj
, (PyTypeObject
*)gobj
);
507 z
->g
= GROUP_G(gobj
);
509 return ((PyObject
*)z
);
512 static PyObject
*ge_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
514 static const char *const kwlist
[] = { "x", 0 };
523 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", KWLIST
, &x
)) goto end
;
525 if (ECPT_PYCHECK(x
)) {
527 if (G_FROMEC(g
, xx
, &p
))
528 TYERR("can't convert from elliptic curve point");
530 } else if ((y
= tomp(x
)) != 0) {
531 if (G_FROMINT(g
, xx
, y
))
532 TYERR("can't convert from integer");
534 } else if (PyString_Check(x
)) {
535 sc
.buf
= PyString_AS_STRING(x
);
536 sc
.lim
= sc
.buf
+ PyString_GET_SIZE(x
);
537 if (G_READ(g
, xx
, &mptext_stringops
, &sc
) || sc
.buf
< sc
.lim
)
538 VALERR("malformed group element string");
540 TYERR("can't convert to group element");
541 return (ge_pywrap((PyObject
*)ty
, xx
));
545 if (xx
) G_DESTROY(g
, xx
);
549 static PyObject
*group_dopywrap(PyTypeObject
*ty
, group
*g
)
551 group_pyobj
*gobj
= newtype(ty
, 0, g
->ops
->name
);
553 gobj
->ty
.ht_type
.tp_basicsize
= sizeof(ge_pyobj
);
554 gobj
->ty
.ht_type
.tp_base
= ge_pytype
;
555 Py_INCREF(group_pytype
);
556 gobj
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
557 Py_TPFLAGS_BASETYPE
|
558 Py_TPFLAGS_CHECKTYPES
|
559 Py_TPFLAGS_HEAPTYPE
);
560 gobj
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
561 gobj
->ty
.ht_type
.tp_free
= 0;
562 gobj
->ty
.ht_type
.tp_new
= ge_pynew
;
563 typeready(&gobj
->ty
.ht_type
);
564 return ((PyObject
*)gobj
);
567 PyObject
*group_pywrap(group
*g
)
571 if (STRCMP(G_NAME(g
), ==, "prime")) ty
= primegroup_pytype
;
572 else if (STRCMP(G_NAME(g
), ==, "bin")) ty
= bingroup_pytype
;
573 else if (STRCMP(G_NAME(g
), ==, "ec")) ty
= ecgroup_pytype
;
575 return (group_dopywrap(ty
, g
));
578 /*----- Group elements ----------------------------------------------------*/
580 #define BINOP(name) \
581 static PyObject *ge_py##name(PyObject *x, PyObject *y) \
585 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \
586 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \
590 g->ops->name(g, z, GE_X(x), GE_X(y)); \
591 return (ge_pywrap(GE_GOBJ(x), z)); \
598 static PyObject *gemeth_##name(PyObject *me, PyObject *arg) \
602 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
605 g->ops->name(g, z, GE_X(me)); \
606 return (ge_pywrap(GE_GOBJ(me), z)); \
612 static PyObject
*ge_pyexp(PyObject
*x
, PyObject
*n
, PyObject
*m
)
617 if (m
!= Py_None
|| !GE_PYCHECK(x
) || (nn
= getmp(n
)) == 0)
619 z
= G_CREATE(GE_G(x
));
620 G_EXP(GE_G(x
), z
, GE_X(x
), nn
);
622 return (ge_pywrap(GE_GOBJ(x
), z
));
625 static void ge_pydealloc(PyObject
*me
)
627 G_DESTROY(GE_G(me
), GE_X(me
));
628 Py_DECREF(GE_GOBJ(me
));
632 static void group_pydealloc(PyObject
*me
)
634 G_DESTROYGROUP(GROUP_G(me
));
635 PyType_Type
.tp_dealloc(me
);
638 static PyObject
*gmexp_id(PyObject
*me
)
640 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
641 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
644 static int gmexp_fill(void *pp
, PyObject
*me
, PyObject
*x
, PyObject
*m
)
646 group_expfactor
*f
= pp
;
648 if (!GE_PYCHECK(x
) || GE_G(x
) != GROUP_G(me
) || (f
->exp
= getmp(m
)) == 0)
654 static PyObject
*ge_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
659 if (!GE_PYCHECK(x
) || !GE_PYCHECK(y
) ||
660 (GE_G(x
) != GE_G(y
) && !group_samep(GE_G(x
), GE_G(y
))))
663 case Py_EQ
: b
= G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
664 case Py_NE
: b
= !G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
665 default: TYERR("group elements are unordered");
672 static PyObject
*gemeth_check(PyObject
*me
, PyObject
*arg
)
674 if (!PyArg_ParseTuple(arg
, ":check")) goto end
;
675 if (group_check(GE_G(me
), GE_X(me
))) VALERR("bad group element");
681 static int ge_pynonzerop(PyObject
*x
)
682 { return (!G_IDENTP(GE_G(x
), GE_X(x
))); }
684 static PyObject
*ge_pystr(PyObject
*me
)
689 group_writedstr(GE_G(me
), GE_X(me
), &d
);
690 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
695 static PyObject
*ge_pylong(PyObject
*me
)
700 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
701 TYERR("can't convert to integer");
708 static PyObject
*ge_pyint(PyObject
*me
)
714 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
715 TYERR("can't convert to integer");
716 if (!mp_tolong_checked(x
, &l
, 0)) rc
= PyInt_FromLong(l
);
717 else rc
= mp_topylong(x
);
723 static PyObject
*gemeth_toint(PyObject
*me
, PyObject
*arg
)
727 if (!PyArg_ParseTuple(arg
, ":toint")) goto end
;
728 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
729 TYERR("can't convert to integer");
730 return (mp_pywrap(x
));
735 static PyObject
*gemeth_toec(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
737 static const char *const kwlist
[] = { "curve", 0 };
738 PyTypeObject
*cty
= 0;
744 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:toec", KWLIST
,
746 g
= GROUP_G(GE_GOBJ(me
));
748 if (!PyType_Check(cty
) || !PyType_IsSubtype(cty
, ecpt_pytype
))
749 TYERR("want subtype of catacomb.ECPt");
750 Py_INCREF((PyObject
*)cty
);
751 } else if (STRCMP(G_NAME(g
), ==, "ec")) {
752 c
= eccurve_copy(((gctx_ec
*)g
)->ei
.c
);
753 cty
= (PyTypeObject
*)eccurve_pywrap(0, c
);
756 Py_INCREF((PyObject
*)cty
);
758 if (G_TOEC(GE_G(me
), &p
, GE_X(me
))) {
759 Py_DECREF((PyObject
*)cty
);
760 TYERR("can't convert to ec point");
762 rc
= ecpt_pywrapout(cty
, &p
);
763 Py_DECREF((PyObject
*)cty
);
768 static PyObject
*gemeth_tobuf(PyObject
*me
, PyObject
*arg
)
774 if (!PyArg_ParseTuple(arg
, ":tobuf")) return (0);
775 n
= GE_G(me
)->noctets
+ 4;
776 rc
= bytestring_pywrap(0, n
);
777 buf_init(&b
, PyString_AS_STRING(rc
), n
);
778 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
780 _PyString_Resize(&rc
, BLEN(&b
));
784 static PyObject
*gemeth_toraw(PyObject
*me
, PyObject
*arg
)
790 if (!PyArg_ParseTuple(arg
, ":toraw")) return (0);
791 n
= GE_G(me
)->noctets
;
792 rc
= bytestring_pywrap(0, n
);
793 buf_init(&b
, PyString_AS_STRING(rc
), n
);
794 G_TORAW(GE_G(me
), &b
, GE_X(me
));
796 _PyString_Resize(&rc
, BLEN(&b
));
800 static PyObject
*gmexp_exp(PyObject
*me
, void *pp
, int n
)
802 ge
*z
= G_CREATE(GROUP_G(me
));
803 G_MEXP(GROUP_G(me
), z
, pp
, n
);
804 return (ge_pywrap(me
, z
));
807 static void gmexp_drop(void *pp
)
809 group_expfactor
*f
= pp
;
813 static PyObject
*gmeth_mexp(PyObject
*me
, PyObject
*arg
)
815 return (mexp_common(me
, arg
, sizeof(group_expfactor
),
816 gmexp_id
, gmexp_fill
, gmexp_exp
, gmexp_drop
));
819 static PyObject
*gmeth_checkgroup(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
821 static const char *const kwlist
[] = { "rng", 0 };
822 grand
*r
= &rand_global
;
825 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:checkgroup", KWLIST
,
828 if ((p
= G_CHECK(GROUP_G(me
), r
)) != 0)
835 static PyObject
*group_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
837 int b
= group_samep(GROUP_G(x
), GROUP_G(y
));
841 default: TYERR("can't order groups");
848 static PyObject
*meth__GE_frombuf(PyObject
*me
, PyObject
*arg
)
856 if (!PyArg_ParseTuple(arg
, "Os#:frombuf", &me
, &p
, &n
)) return (0);
860 if (G_FROMBUF(g
, &b
, x
)) VALERR("invalid data");
861 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
863 if (x
) G_DESTROY(g
, x
);
867 static PyObject
*meth__GE_fromraw(PyObject
*me
, PyObject
*arg
)
875 if (!PyArg_ParseTuple(arg
, "Os#:fromraw", &me
, &p
, &n
)) return (0);
879 if (G_FROMRAW(g
, &b
, x
)) VALERR("invalid data");
880 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
882 if (x
) G_DESTROY(g
, x
);
886 static PyObject
*meth__GE_fromstring(PyObject
*me
, PyObject
*arg
)
894 if (!PyArg_ParseTuple(arg
, "Os#:fromstring", &me
, &p
, &n
)) return (0);
899 if (G_READ(g
, x
, &mptext_stringops
, &sc
))
900 VALERR("bad group element string");
901 return (Py_BuildValue("(Ns#)", ge_pywrap(me
, x
),
902 sc
.buf
, (Py_ssize_t
)(sc
.lim
- sc
.buf
)));
904 if (x
) G_DESTROY(g
, x
);
908 static PyObject
*meth__Group_parse(PyObject
*me
, PyObject
*arg
)
914 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
)) goto end
;
916 if ((g
= group_parse(&qd
)) == 0) VALERR(qd
.e
);
917 return (group_pywrap(g
));
922 static PyObject
*geget_group(PyObject
*me
, void *hunoz
)
923 { RETURN_OBJ(GE_GOBJ(me
)); }
925 static PyObject
*gget_nbits(PyObject
*me
, void *hunoz
)
926 { return (PyInt_FromLong(GROUP_G(me
)->nbits
)); }
928 static PyObject
*gget_noctets(PyObject
*me
, void *hunoz
)
929 { return (PyInt_FromLong(GROUP_G(me
)->noctets
)); }
931 static PyObject
*gget_i(PyObject
*me
, void *hunoz
)
933 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
934 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
937 static PyObject
*gget_g(PyObject
*me
, void *hunoz
)
939 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
940 G_COPY(g
, x
, g
->g
); return (ge_pywrap(me
, x
));
943 static long ge_pyhash(PyObject
*me
)
946 size_t sz
= GE_G(me
)->noctets
+ 4;
947 uint32 h
= 0xf672c776 + GE_G(me
)->ops
->ty
;
948 octet
*p
= xmalloc(sz
);
950 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
952 h
= unihash_hash(&unihash_global
, h
, BBASE(&b
), BLEN(&b
));
954 return (h
% LONG_MAX
);
957 static PyObject
*gget_r(PyObject
*me
, void *hunoz
)
958 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->r
))); }
960 static PyObject
*gget_h(PyObject
*me
, void *hunoz
)
961 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->h
))); }
963 static const PyGetSetDef ge_pygetset
[] = {
964 #define GETSETNAME(op, name) ge##op##_##name
965 GET (group
, "X.group -> group containing X")
970 static const PyMethodDef ge_pymethods
[] = {
971 #define METHNAME(name) gemeth_##name
972 METH (inv
, "X.inv() -> inverse element of X")
973 METH (sqr
, "X.sqr() -> X^2 = X * X")
974 METH (check
, "X.check() -> check X really belongs to its group")
975 METH (toint
, "X.toint() -> X converted to an integer")
976 KWMETH(toec
, "X.toec([curve = ECPt]) -> "
977 "X converted to elliptic curve point")
978 METH (tobuf
, "X.tobuf() -> X in buffer representation")
979 METH (toraw
, "X.toraw() -> X in raw representation")
984 static const PyNumberMethods ge_pynumber
= {
986 0, /* @nb_subtract@ */
987 ge_pymul
, /* @nb_multiply@ */
988 ge_pydiv
, /* @nb_divide@ */
989 0, /* @nb_remainder@ */
991 ge_pyexp
, /* @nb_power@ */
992 0, /* @nb_negative@ */
993 0, /* @nb_positive@ */
994 0, /* @nb_absolute@ */
995 ge_pynonzerop
, /* @nb_nonzero@ */
1002 0, /* @nb_coerce@ */
1003 ge_pyint
, /* @nb_int@ */
1004 ge_pylong
, /* @nb_long@ */
1005 0 /* meaningless */, /* @nb_float@ */
1009 0, /* @nb_inplace_add@ */
1010 0, /* @nb_inplace_subtract@ */
1011 0, /* @nb_inplace_multiply@ */
1012 0, /* @nb_inplace_divide@ */
1013 0, /* @nb_inplace_remainder@ */
1014 0, /* @nb_inplace_power@ */
1015 0, /* @nb_inplace_lshift@ */
1016 0, /* @nb_inplace_rshift@ */
1017 0, /* @nb_inplace_and@ */
1018 0, /* @nb_inplace_xor@ */
1019 0, /* @nb_inplace_or@ */
1021 0, /* @nb_floor_divide@ */
1022 ge_pydiv
, /* @nb_true_divide@ */
1023 0, /* @nb_inplace_floor_divide@ */
1024 0, /* @nb_inplace_true_divide@ */
1027 static PyTypeObject ge_pytype_skel
= {
1028 PyObject_HEAD_INIT(0) 0, /* Header */
1029 "GE", /* @tp_name@ */
1030 sizeof(ge_pyobj
), /* @tp_basicsize@ */
1031 0, /* @tp_itemsize@ */
1033 ge_pydealloc
, /* @tp_dealloc@ */
1035 0, /* @tp_getattr@ */
1036 0, /* @tp_setattr@ */
1037 0, /* @tp_compare@ */
1039 PYNUMBER(ge
), /* @tp_as_number@ */
1040 0, /* @tp_as_sequence@ */
1041 0, /* @tp_as_mapping@ */
1042 ge_pyhash
, /* @tp_hash@ */
1044 ge_pystr
, /* @tp_str@ */
1045 0, /* @tp_getattro@ */
1046 0, /* @tp_setattro@ */
1047 0, /* @tp_as_buffer@ */
1048 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1049 Py_TPFLAGS_CHECKTYPES
|
1050 Py_TPFLAGS_BASETYPE
,
1053 "Group elements, abstract base class.",
1055 0, /* @tp_traverse@ */
1057 ge_pyrichcompare
, /* @tp_richcompare@ */
1058 0, /* @tp_weaklistoffset@ */
1060 0, /* @tp_iternext@ */
1061 PYMETHODS(ge
), /* @tp_methods@ */
1062 0, /* @tp_members@ */
1063 PYGETSET(ge
), /* @tp_getset@ */
1066 0, /* @tp_descr_get@ */
1067 0, /* @tp_descr_set@ */
1068 0, /* @tp_dictoffset@ */
1070 PyType_GenericAlloc
, /* @tp_alloc@ */
1071 abstract_pynew
, /* @tp_new@ */
1076 static const PyGetSetDef group_pygetset
[] = {
1077 #define GETSETNAME(op, name) g##op##_##name
1078 GET (noctets
, "G.noctets -> size in octets of element")
1079 GET (nbits
, "G.nbits -> size in bits of element")
1080 GET (i
, "G.i -> group identity")
1081 GET (g
, "G.g -> group generator")
1082 GET (r
, "G.r -> group order")
1083 GET (h
, "G.h -> group cofactor")
1088 static const PyMethodDef group_pymethods
[] = {
1089 #define METHNAME(name) gmeth_##name
1090 METH (mexp
, "G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1091 KWMETH(checkgroup
, "G.checkgroup([rng = rand]): check group is good")
1096 static PyTypeObject group_pytype_skel
= {
1097 PyObject_HEAD_INIT(0) 0, /* Header */
1098 "Group", /* @tp_name@ */
1099 sizeof(group_pyobj
), /* @tp_basicsize@ */
1100 0, /* @tp_itemsize@ */
1102 group_pydealloc
, /* @tp_dealloc@ */
1104 0, /* @tp_getattr@ */
1105 0, /* @tp_setattr@ */
1106 0, /* @tp_compare@ */
1108 0, /* @tp_as_number@ */
1109 0, /* @tp_as_sequence@ */
1110 0, /* @tp_as_mapping@ */
1114 0, /* @tp_getattro@ */
1115 0, /* @tp_setattro@ */
1116 0, /* @tp_as_buffer@ */
1117 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1118 Py_TPFLAGS_BASETYPE
,
1121 "Abstract base class for groups.",
1123 0, /* @tp_traverse@ */
1125 group_pyrichcompare
, /* @tp_richcompare@ */
1126 0, /* @tp_weaklistoffset@ */
1128 0, /* @tp_iternext@ */
1129 PYMETHODS(group
), /* @tp_methods@ */
1130 0, /* @tp_members@ */
1131 PYGETSET(group
), /* @tp_getset@ */
1134 0, /* @tp_descr_get@ */
1135 0, /* @tp_descr_set@ */
1136 0, /* @tp_dictoffset@ */
1138 PyType_GenericAlloc
, /* @tp_alloc@ */
1139 abstract_pynew
, /* @tp_new@ */
1144 static PyObject
*pgget_info(PyObject
*me
, void *hunoz
)
1147 gctx_prime
*gg
= (gctx_prime
*)GROUP_G(me
);
1148 dp
.p
= MP_COPY(gg
->mm
.m
);
1149 dp
.q
= MP_COPY(gg
->g
.r
);
1150 dp
.g
= mpmont_reduce(&gg
->mm
, MP_NEW
, gg
->gen
.x
);
1151 return (fginfo_pywrap(&dp
, dhinfo_pytype
));
1154 static const PyGetSetDef primegroup_pygetset
[] = {
1155 #define GETSETNAME(op, name) pg##op##_##name
1156 GET (info
, "G.info -> information about the group")
1161 static PyObject
*primegroup_pynew(PyTypeObject
*ty
,
1162 PyObject
*arg
, PyObject
*kw
)
1165 static const char *const kwlist
[] = { "info", 0 };
1167 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1170 return (group_dopywrap(ty
, group_prime(FGINFO_DP(i
))));
1173 static PyTypeObject primegroup_pytype_skel
= {
1174 PyObject_HEAD_INIT(0) 0, /* Header */
1175 "PrimeGroup", /* @tp_name@ */
1176 sizeof(group_pyobj
), /* @tp_basicsize@ */
1177 0, /* @tp_itemsize@ */
1179 group_pydealloc
, /* @tp_dealloc@ */
1181 0, /* @tp_getattr@ */
1182 0, /* @tp_setattr@ */
1183 0, /* @tp_compare@ */
1185 0, /* @tp_as_number@ */
1186 0, /* @tp_as_sequence@ */
1187 0, /* @tp_as_mapping@ */
1191 0, /* @tp_getattro@ */
1192 0, /* @tp_setattro@ */
1193 0, /* @tp_as_buffer@ */
1194 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1195 Py_TPFLAGS_BASETYPE
,
1198 "PrimeGroup(INFO): subgroups of prime fields.",
1200 0, /* @tp_traverse@ */
1202 0, /* @tp_richcompare@ */
1203 0, /* @tp_weaklistoffset@ */
1205 0, /* @tp_iternext@ */
1206 0, /* @tp_methods@ */
1207 0, /* @tp_members@ */
1208 PYGETSET(primegroup
), /* @tp_getset@ */
1211 0, /* @tp_descr_get@ */
1212 0, /* @tp_descr_set@ */
1213 0, /* @tp_dictoffset@ */
1215 PyType_GenericAlloc
, /* @tp_alloc@ */
1216 primegroup_pynew
, /* @tp_new@ */
1221 static PyObject
*bgget_info(PyObject
*me
, void *hunoz
)
1224 gctx_bin
*gg
= (gctx_bin
*)GROUP_G(me
);
1225 dp
.p
= MP_COPY(gg
->r
.p
);
1226 dp
.q
= MP_COPY(gg
->g
.r
);
1227 dp
.g
= MP_COPY(gg
->gen
.x
);
1228 return (fginfo_pywrap(&dp
, bindhinfo_pytype
));
1231 static const PyGetSetDef bingroup_pygetset
[] = {
1232 #define GETSETNAME(op, name) bg##op##_##name
1233 GET (info
, "G.info -> information about the group")
1238 static PyObject
*bingroup_pynew(PyTypeObject
*ty
,
1239 PyObject
*arg
, PyObject
*kw
)
1242 static const char *const kwlist
[] = { "info", 0 };
1244 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1245 bindhinfo_pytype
, &i
))
1247 return (group_dopywrap(ty
, group_binary(FGINFO_DP(i
))));
1250 static PyTypeObject bingroup_pytype_skel
= {
1251 PyObject_HEAD_INIT(0) 0, /* Header */
1252 "BinGroup", /* @tp_name@ */
1253 sizeof(group_pyobj
), /* @tp_basicsize@ */
1254 0, /* @tp_itemsize@ */
1256 group_pydealloc
, /* @tp_dealloc@ */
1258 0, /* @tp_getattr@ */
1259 0, /* @tp_setattr@ */
1260 0, /* @tp_compare@ */
1262 0, /* @tp_as_number@ */
1263 0, /* @tp_as_sequence@ */
1264 0, /* @tp_as_mapping@ */
1268 0, /* @tp_getattro@ */
1269 0, /* @tp_setattro@ */
1270 0, /* @tp_as_buffer@ */
1271 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1272 Py_TPFLAGS_BASETYPE
,
1275 "BinGroup(INFO): subgroups of binary fields.",
1277 0, /* @tp_traverse@ */
1279 0, /* @tp_richcompare@ */
1280 0, /* @tp_weaklistoffset@ */
1282 0, /* @tp_iternext@ */
1283 0, /* @tp_methods@ */
1284 0, /* @tp_members@ */
1285 PYGETSET(bingroup
), /* @tp_getset@ */
1288 0, /* @tp_descr_get@ */
1289 0, /* @tp_descr_set@ */
1290 0, /* @tp_dictoffset@ */
1292 PyType_GenericAlloc
, /* @tp_alloc@ */
1293 bingroup_pynew
, /* @tp_new@ */
1298 static PyObject
*egget_info(PyObject
*me
, void *hunoz
)
1301 gctx_ec
*gg
= (gctx_ec
*)GROUP_G(me
);
1303 ecinfo_copy(&ei
, &gg
->ei
);
1304 return (ecinfo_pywrap(&ei
));
1307 static const PyGetSetDef ecgroup_pygetset
[] = {
1308 #define GETSETNAME(op, name) eg##op##_##name
1309 GET (info
, "G.info -> information about the group")
1314 static PyObject
*ecgroup_pynew(PyTypeObject
*ty
,
1315 PyObject
*arg
, PyObject
*kw
)
1319 static const char *const kwlist
[] = { "info", 0 };
1321 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1324 ecinfo_copy(&ei
, ECINFO_EI(i
));
1325 return (group_dopywrap(ty
, group_ec(&ei
)));
1328 static PyTypeObject ecgroup_pytype_skel
= {
1329 PyObject_HEAD_INIT(0) 0, /* Header */
1330 "ECGroup", /* @tp_name@ */
1331 sizeof(group_pyobj
), /* @tp_basicsize@ */
1332 0, /* @tp_itemsize@ */
1334 group_pydealloc
, /* @tp_dealloc@ */
1336 0, /* @tp_getattr@ */
1337 0, /* @tp_setattr@ */
1338 0, /* @tp_compare@ */
1340 0, /* @tp_as_number@ */
1341 0, /* @tp_as_sequence@ */
1342 0, /* @tp_as_mapping@ */
1346 0, /* @tp_getattro@ */
1347 0, /* @tp_setattro@ */
1348 0, /* @tp_as_buffer@ */
1349 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1350 Py_TPFLAGS_BASETYPE
,
1353 "ECGroup(INFO): elliptic curve groups.",
1355 0, /* @tp_traverse@ */
1357 0, /* @tp_richcompare@ */
1358 0, /* @tp_weaklistoffset@ */
1360 0, /* @tp_iternext@ */
1361 0, /* @tp_methods@ */
1362 0, /* @tp_members@ */
1363 PYGETSET(ecgroup
), /* @tp_getset@ */
1366 0, /* @tp_descr_get@ */
1367 0, /* @tp_descr_set@ */
1368 0, /* @tp_dictoffset@ */
1370 PyType_GenericAlloc
, /* @tp_alloc@ */
1371 ecgroup_pynew
, /* @tp_new@ */
1376 /*----- Global stuff ------------------------------------------------------*/
1378 static const PyMethodDef methods
[] = {
1379 #define METHNAME(name) meth_##name
1380 METH (_GE_frombuf
, "frombuf(BUF) -> X, REST")
1381 METH (_GE_fromraw
, "fromraw(BUF) -> X, REST")
1382 METH (_GE_fromstring
, "fromstring(STR) -> X, REST")
1383 METH (_Group_parse
, "parse(STR) -> G, REST")
1384 METH (_DHInfo_parse
, "parse(STR) -> D, REST")
1385 METH (_BinDHInfo_parse
, "parse(STR) -> D, REST")
1386 METH (_DHInfo__groupn
, 0)
1387 METH (_BinDHInfo__groupn
, 0)
1388 KWMETH(_DHInfo_generate
,
1389 "generate(PBITS, [qbits = 0, event = pgen_nullev,\n"
1390 " rng = rand, nsteps = 0]) -> D")
1391 KWMETH(_DHInfo_genlimlee
,
1392 "genlimlee(PBITS, QBITS, [event = pgen_nullev], "
1393 "[ievent = pgen_nullev],\n"
1394 " [rng = rand], [nsteps = 0], [subgroupp = True]) "
1396 KWMETH(_DHInfo_gendsa
,
1397 "gendsa(PBITS, QBITS, SEED, [event = pgen_nullev], [nsteps = 0])\n"
1398 " -> (D, SEED, COUNT)")
1399 KWMETH(_DHInfo_genkcdsa
,
1400 "gendsa(PBITS, QBITS, [event = pgen_nullev], "
1401 "[rng = rand], [nsteps = 0])\n"
1407 void group_pyinit(void)
1409 INITTYPE(fginfo
, root
);
1410 INITTYPE(dhinfo
, fginfo
);
1411 INITTYPE(bindhinfo
, dhinfo
);
1413 INITTYPE(group
, type
);
1414 INITTYPE(primegroup
, group
);
1415 INITTYPE(bingroup
, group
);
1416 INITTYPE(ecgroup
, group
);
1417 addmethods(methods
);
1420 void group_pyinsert(PyObject
*mod
)
1422 INSERT("FGInfo", fginfo_pytype
);
1423 INSERT("DHInfo", dhinfo_pytype
);
1424 INSERT("BinDHInfo", bindhinfo_pytype
);
1425 INSERT("GE", ge_pytype
);
1426 INSERT("Group", group_pytype
);
1427 INSERT("PrimeGroup", primegroup_pytype
);
1428 INSERT("BinGroup", bingroup_pytype
);
1429 INSERT("ECGroup", ecgroup_pytype
);
1430 INSERT("_pgroups", namedgroups(ptab
, &npgroups
));
1431 INSERT("_bingroups", namedgroups(bintab
, &nbingroups
));
1434 /*----- That's all, folks -------------------------------------------------*/