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 static const char *const 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 } };
97 static const char *const kwlist
[] =
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 static const char *const kwlist
[] = {
127 "class", "pbits", "qbits", "event", "ievent",
128 "rng", "nsteps", "subgroupp", 0
132 PyObject
*rc
= 0, *vec
= 0;
134 oe
.exc
= ie
.exc
= &exc
;
135 if (!PyArg_ParseTupleAndKeywords(arg
, kw
,
136 "OO&O&|O&O&O&O&O&:genlimlee", KWLIST
,
137 &me
, convuint
, &pl
, convuint
, &ql
,
138 convpgev
, &oe
, convpgev
, &ie
,
139 convgrand
, &r
, convuint
, &steps
,
140 convbool
, &subgroupp
))
142 if (subgroupp
) f
|= DH_SUBGROUP
;
143 if (dh_limlee(&dp
, ql
, pl
, f
, steps
, r
,
144 oe
.ev
.proc
, oe
.ev
.ctx
, ie
.ev
.proc
, ie
.ev
.ctx
, &nf
, &v
))
146 vec
= PyList_New(nf
);
147 for (i
= 0; i
< nf
; i
++)
148 PyList_SetItem(vec
, i
, mp_pywrap(v
[i
]));
150 rc
= Py_BuildValue("(NN)", fginfo_pywrap(&dp
, dhinfo_pytype
), vec
);
152 droppgev(&oe
); droppgev(&ie
);
156 static PyObject
*meth__DHInfo_genkcdsa(PyObject
*me
,
157 PyObject
*arg
, PyObject
*kw
)
162 grand
*r
= &rand_global
;
163 struct excinfo exc
= EXCINFO_INIT
;
164 pypgev evt
= { { 0 } };
165 static const char *const kwlist
[] =
166 { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 };
171 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&O&|O&O&O&:genkcdsa", KWLIST
,
172 &me
, convuint
, &pl
, convuint
, &ql
,
173 convpgev
, &evt
, convgrand
, &r
,
176 if (dh_kcdsagen(&dp
, ql
, pl
, 0, steps
, r
, evt
.ev
.proc
, evt
.ev
.ctx
))
178 mp_div(&v
, 0, dp
.p
, dp
.q
);
180 rc
= Py_BuildValue("(NN)", fginfo_pywrap(&dp
, dhinfo_pytype
),
187 static PyObject
*meth__DHInfo_gendsa(PyObject
*me
,
188 PyObject
*arg
, PyObject
*kw
)
196 struct excinfo exc
= EXCINFO_INIT
;
197 pypgev evt
= { { 0 } };
198 static const char *const kwlist
[] =
199 { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 };
203 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&O&s#|O&O&:gendsa", KWLIST
,
204 &me
, convuint
, &pl
, convuint
, &ql
,
205 &k
, &ksz
, convpgev
, &evt
,
208 if (dsa_gen(&dp
, ql
, pl
, steps
, k
, ksz
, &ds
, evt
.ev
.proc
, evt
.ev
.ctx
))
210 rc
= Py_BuildValue("(NNl)", fginfo_pywrap(&dp
, dhinfo_pytype
),
211 bytestring_pywrap(ds
.p
, ds
.sz
), (long)ds
.count
);
218 static int npgroups
= -1, nbingroups
= -1;
220 static PyObject
*namedgroups(const pentry
*pp
, int *ne
)
227 for (i
= 0; pp
[i
].name
; i
++) {
229 for (j
= 0; j
< i
; j
++) {
230 if (pp
[i
].data
== pp
[j
].data
) {
231 c
= PyDict_GetItemString(d
, (/*unconst*/ char *)pp
[j
].name
);
236 c
= PyInt_FromLong(i
);
238 PyDict_SetItemString(d
, (/*unconst*/ char *)p
, c
);
245 static PyObject
*meth__groupn(PyObject
*me
, PyObject
*arg
,
246 PyTypeObject
*ty
, const pentry
*pp
, int ne
)
252 if (!PyArg_ParseTuple(arg
, "Oi:_groupn", &me
, &i
)) goto end
;
253 if (i
< 0 || i
>= ne
) VALERR("group index out of range");
254 dh_infofromdata(&gp
, pp
[i
].data
);
255 rc
= fginfo_pywrap(&gp
, ty
);
260 static PyObject
*meth__DHInfo__groupn(PyObject
*me
, PyObject
*arg
)
261 { return (meth__groupn(me
, arg
, dhinfo_pytype
, ptab
, npgroups
)); }
263 static PyObject
*meth__BinDHInfo__groupn(PyObject
*me
, PyObject
*arg
)
264 { return (meth__groupn(me
, arg
, bindhinfo_pytype
, bintab
, nbingroups
)); }
266 static PyObject
*meth__parse(PyObject
*me
, PyObject
*arg
, PyTypeObject
*ty
,
267 int (*parse
)(qd_parse
*, gprime_param
*))
274 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
)) goto end
;
277 if (parse(&qd
, &gp
)) VALERR(qd
.e
);
278 rc
= fginfo_pywrap(&gp
, ty
);
283 static PyObject
*meth__DHInfo_parse(PyObject
*me
, PyObject
*arg
)
284 { return (meth__parse(me
, arg
, dhinfo_pytype
, dh_parse
)); }
286 static PyObject
*meth__BinDHInfo_parse(PyObject
*me
, PyObject
*arg
)
287 { return (meth__parse(me
, arg
, bindhinfo_pytype
, dhbin_parse
)); }
289 static PyGetSetDef fginfo_pygetset
[] = {
290 #define GETSETNAME(op, name) fi##op##_##name
291 GET (r
, "I.r -> group order")
296 static PyGetSetDef dhinfo_pygetset
[] = {
297 #define GETSETNAME(op, name) di##op##_##name
298 GET (p
, "I.p -> prime")
299 GET (g
, "I.g -> generator")
304 static PyGetSetDef bindhinfo_pygetset
[] = {
305 #define GETSETNAME(op, name) bi##op##_##name
306 GET (p
, "I.p -> irreducible polynomial")
307 GET (m
, "I.m -> degree of polynomial")
308 GET (g
, "I.g -> generator")
313 static PyTypeObject fginfo_pytype_skel
= {
314 PyObject_HEAD_INIT(0) 0, /* Header */
315 "FGInfo", /* @tp_name@ */
316 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
317 0, /* @tp_itemsize@ */
319 fginfo_pydealloc
, /* @tp_dealloc@ */
321 0, /* @tp_getattr@ */
322 0, /* @tp_setattr@ */
323 0, /* @tp_compare@ */
325 0, /* @tp_as_number@ */
326 0, /* @tp_as_sequence@ */
327 0, /* @tp_as_mapping@ */
331 0, /* @tp_getattro@ */
332 0, /* @tp_setattro@ */
333 0, /* @tp_as_buffer@ */
334 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
338 "Abstract base class for field-group information objects.",
340 0, /* @tp_traverse@ */
342 0, /* @tp_richcompare@ */
343 0, /* @tp_weaklistoffset@ */
345 0, /* @tp_iternext@ */
346 0, /* @tp_methods@ */
347 0, /* @tp_members@ */
348 fginfo_pygetset
, /* @tp_getset@ */
351 0, /* @tp_descr_get@ */
352 0, /* @tp_descr_set@ */
353 0, /* @tp_dictoffset@ */
355 PyType_GenericAlloc
, /* @tp_alloc@ */
356 abstract_pynew
, /* @tp_new@ */
361 static PyTypeObject dhinfo_pytype_skel
= {
362 PyObject_HEAD_INIT(0) 0, /* Header */
363 "DHInfo", /* @tp_name@ */
364 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
365 0, /* @tp_itemsize@ */
367 fginfo_pydealloc
, /* @tp_dealloc@ */
369 0, /* @tp_getattr@ */
370 0, /* @tp_setattr@ */
371 0, /* @tp_compare@ */
373 0, /* @tp_as_number@ */
374 0, /* @tp_as_sequence@ */
375 0, /* @tp_as_mapping@ */
379 0, /* @tp_getattro@ */
380 0, /* @tp_setattro@ */
381 0, /* @tp_as_buffer@ */
382 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
386 "DHInfo(P, R, G): standard (integer) Diffie-Hellman group information.",
388 0, /* @tp_traverse@ */
390 0, /* @tp_richcompare@ */
391 0, /* @tp_weaklistoffset@ */
393 0, /* @tp_iternext@ */
394 0, /* @tp_methods@ */
395 0, /* @tp_members@ */
396 dhinfo_pygetset
, /* @tp_getset@ */
399 0, /* @tp_descr_get@ */
400 0, /* @tp_descr_set@ */
401 0, /* @tp_dictoffset@ */
403 PyType_GenericAlloc
, /* @tp_alloc@ */
404 fginfo_pynew
, /* @tp_new@ */
409 static PyTypeObject bindhinfo_pytype_skel
= {
410 PyObject_HEAD_INIT(0) 0, /* Header */
411 "BinDHInfo", /* @tp_name@ */
412 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
413 0, /* @tp_itemsize@ */
415 fginfo_pydealloc
, /* @tp_dealloc@ */
417 0, /* @tp_getattr@ */
418 0, /* @tp_setattr@ */
419 0, /* @tp_compare@ */
421 0, /* @tp_as_number@ */
422 0, /* @tp_as_sequence@ */
423 0, /* @tp_as_mapping@ */
427 0, /* @tp_getattro@ */
428 0, /* @tp_setattro@ */
429 0, /* @tp_as_buffer@ */
430 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
434 "BinDHInfo(P, R, G): binary-field Diffie-Hellman group information.",
436 0, /* @tp_traverse@ */
438 0, /* @tp_richcompare@ */
439 0, /* @tp_weaklistoffset@ */
441 0, /* @tp_iternext@ */
442 0, /* @tp_methods@ */
443 0, /* @tp_members@ */
444 bindhinfo_pygetset
, /* @tp_getset@ */
447 0, /* @tp_descr_get@ */
448 0, /* @tp_descr_set@ */
449 0, /* @tp_dictoffset@ */
451 PyType_GenericAlloc
, /* @tp_alloc@ */
452 fginfo_pynew
, /* @tp_new@ */
457 /*----- General utilities -------------------------------------------------*/
459 PyTypeObject
*ge_pytype
, *group_pytype
;
460 PyTypeObject
*primegroup_pytype
, *bingroup_pytype
, *ecgroup_pytype
;
462 group
*group_copy(group
*g
)
464 if (STRCMP(G_NAME(g
), ==, "prime")) {
465 gctx_prime
*gc
= (gctx_prime
*)g
;
467 gp
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
470 g
= group_prime(&gp
);
472 } else if (STRCMP(G_NAME(g
), ==, "bin")) {
473 gctx_bin
*gc
= (gctx_bin
*)g
;
475 gb
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
478 g
= group_binary(&gb
);
480 } else if (STRCMP(G_NAME(g
), ==, "ec")) {
481 gctx_ec
*gc
= (gctx_ec
*)g
;
483 if ((ei
.c
= eccurve_copy(gc
->ei
.c
)) == 0)
486 EC_COPY(&ei
.g
, &gc
->ei
.g
);
487 ei
.r
= MP_COPY(gc
->ei
.r
);
488 ei
.h
= MP_COPY(gc
->ei
.h
);
495 PyObject
*ge_pywrap(PyObject
*gobj
, ge
*x
)
497 ge_pyobj
*z
= PyObject_New(ge_pyobj
, (PyTypeObject
*)gobj
);
499 z
->g
= GROUP_G(gobj
);
501 return ((PyObject
*)z
);
504 static PyObject
*ge_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
506 static const char *const kwlist
[] = { "x", 0 };
515 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", KWLIST
, &x
)) goto end
;
517 if (ECPT_PYCHECK(x
)) {
519 if (G_FROMEC(g
, xx
, &p
))
520 TYERR("can't convert from elliptic curve point");
522 } else if ((y
= tomp(x
)) != 0) {
523 if (G_FROMINT(g
, xx
, y
))
524 TYERR("can't convert from integer");
526 } else if (PyString_Check(x
)) {
527 sc
.buf
= PyString_AS_STRING(x
);
528 sc
.lim
= sc
.buf
+ PyString_GET_SIZE(x
);
529 if (G_READ(g
, xx
, &mptext_stringops
, &sc
) || sc
.buf
< sc
.lim
)
530 VALERR("malformed group element string");
532 TYERR("can't convert to group element");
533 return (ge_pywrap((PyObject
*)ty
, xx
));
537 if (xx
) G_DESTROY(g
, xx
);
541 static PyObject
*group_dopywrap(PyTypeObject
*ty
, group
*g
)
543 group_pyobj
*gobj
= newtype(ty
, 0, g
->ops
->name
);
545 gobj
->ty
.ht_type
.tp_basicsize
= sizeof(ge_pyobj
);
546 gobj
->ty
.ht_type
.tp_base
= ge_pytype
;
547 Py_INCREF(group_pytype
);
548 gobj
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
549 Py_TPFLAGS_BASETYPE
|
550 Py_TPFLAGS_CHECKTYPES
|
551 Py_TPFLAGS_HEAPTYPE
);
552 gobj
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
553 gobj
->ty
.ht_type
.tp_free
= 0;
554 gobj
->ty
.ht_type
.tp_new
= ge_pynew
;
555 typeready(&gobj
->ty
.ht_type
);
556 return ((PyObject
*)gobj
);
559 PyObject
*group_pywrap(group
*g
)
563 if (STRCMP(G_NAME(g
), ==, "prime")) ty
= primegroup_pytype
;
564 else if (STRCMP(G_NAME(g
), ==, "bin")) ty
= bingroup_pytype
;
565 else if (STRCMP(G_NAME(g
), ==, "ec")) ty
= ecgroup_pytype
;
567 return (group_dopywrap(ty
, g
));
570 /*----- Group elements ----------------------------------------------------*/
572 #define BINOP(name) \
573 static PyObject *ge_py##name(PyObject *x, PyObject *y) \
577 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \
578 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \
582 g->ops->name(g, z, GE_X(x), GE_X(y)); \
583 return (ge_pywrap(GE_GOBJ(x), z)); \
590 static PyObject *gemeth_##name(PyObject *me, PyObject *arg) \
594 if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \
597 g->ops->name(g, z, GE_X(me)); \
598 return (ge_pywrap(GE_GOBJ(me), z)); \
604 static PyObject
*ge_pyexp(PyObject
*x
, PyObject
*n
, PyObject
*m
)
609 if (m
!= Py_None
|| !GE_PYCHECK(x
) || (nn
= getmp(n
)) == 0)
611 z
= G_CREATE(GE_G(x
));
612 G_EXP(GE_G(x
), z
, GE_X(x
), nn
);
614 return (ge_pywrap(GE_GOBJ(x
), z
));
617 static void ge_pydealloc(PyObject
*me
)
619 G_DESTROY(GE_G(me
), GE_X(me
));
620 Py_DECREF(GE_GOBJ(me
));
624 static void group_pydealloc(PyObject
*me
)
626 G_DESTROYGROUP(GROUP_G(me
));
627 PyType_Type
.tp_dealloc(me
);
630 static PyObject
*gmexp_id(PyObject
*me
)
632 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
633 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
636 static int gmexp_fill(void *pp
, PyObject
*me
, PyObject
*x
, PyObject
*m
)
638 group_expfactor
*f
= pp
;
640 if (!GE_PYCHECK(x
) || GE_G(x
) != GROUP_G(me
) || (f
->exp
= getmp(m
)) == 0)
646 static PyObject
*ge_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
651 if (!GE_PYCHECK(x
) || !GE_PYCHECK(y
) ||
652 (GE_G(x
) != GE_G(y
) && !group_samep(GE_G(x
), GE_G(y
))))
655 case Py_EQ
: b
= G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
656 case Py_NE
: b
= !G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
657 default: TYERR("group elements are unordered");
664 static PyObject
*gemeth_check(PyObject
*me
, PyObject
*arg
)
666 if (!PyArg_ParseTuple(arg
, ":check")) goto end
;
667 if (group_check(GE_G(me
), GE_X(me
))) VALERR("bad group element");
673 static int ge_pynonzerop(PyObject
*x
)
674 { return (!G_IDENTP(GE_G(x
), GE_X(x
))); }
676 static PyObject
*ge_pystr(PyObject
*me
)
681 group_writedstr(GE_G(me
), GE_X(me
), &d
);
682 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
687 static PyObject
*ge_pylong(PyObject
*me
)
692 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
693 TYERR("can't convert to integer");
700 static PyObject
*ge_pyint(PyObject
*me
)
706 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
707 TYERR("can't convert to integer");
708 if (!mp_tolong_checked(x
, &l
, 0)) rc
= PyInt_FromLong(l
);
709 else rc
= mp_topylong(x
);
715 static PyObject
*gemeth_toint(PyObject
*me
, PyObject
*arg
)
719 if (!PyArg_ParseTuple(arg
, ":toint")) goto end
;
720 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
721 TYERR("can't convert to integer");
722 return (mp_pywrap(x
));
727 static PyObject
*gemeth_toec(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
729 static const char *const kwlist
[] = { "curve", 0 };
730 PyTypeObject
*cty
= 0;
736 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:toec", KWLIST
,
738 g
= GROUP_G(GE_GOBJ(me
));
740 if (!PyType_Check(cty
) || !PyType_IsSubtype(cty
, ecpt_pytype
))
741 TYERR("want subtype of catacomb.ECPt");
742 Py_INCREF((PyObject
*)cty
);
743 } else if (STRCMP(G_NAME(g
), ==, "ec")) {
744 c
= eccurve_copy(((gctx_ec
*)g
)->ei
.c
);
745 cty
= (PyTypeObject
*)eccurve_pywrap(0, c
);
748 Py_INCREF((PyObject
*)cty
);
750 if (G_TOEC(GE_G(me
), &p
, GE_X(me
))) {
751 Py_DECREF((PyObject
*)cty
);
752 TYERR("can't convert to ec point");
754 rc
= ecpt_pywrapout(cty
, &p
);
755 Py_DECREF((PyObject
*)cty
);
760 static PyObject
*gemeth_tobuf(PyObject
*me
, PyObject
*arg
)
766 if (!PyArg_ParseTuple(arg
, ":tobuf")) return (0);
767 n
= GE_G(me
)->noctets
+ 4;
768 rc
= bytestring_pywrap(0, n
);
769 buf_init(&b
, PyString_AS_STRING(rc
), n
);
770 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
772 _PyString_Resize(&rc
, BLEN(&b
));
776 static PyObject
*gemeth_toraw(PyObject
*me
, PyObject
*arg
)
782 if (!PyArg_ParseTuple(arg
, ":toraw")) return (0);
783 n
= GE_G(me
)->noctets
;
784 rc
= bytestring_pywrap(0, n
);
785 buf_init(&b
, PyString_AS_STRING(rc
), n
);
786 G_TORAW(GE_G(me
), &b
, GE_X(me
));
788 _PyString_Resize(&rc
, BLEN(&b
));
792 static PyObject
*gmexp_exp(PyObject
*me
, void *pp
, int n
)
794 ge
*z
= G_CREATE(GROUP_G(me
));
795 G_MEXP(GROUP_G(me
), z
, pp
, n
);
796 return (ge_pywrap(me
, z
));
799 static void gmexp_drop(void *pp
)
801 group_expfactor
*f
= pp
;
805 static PyObject
*gmeth_mexp(PyObject
*me
, PyObject
*arg
)
807 return (mexp_common(me
, arg
, sizeof(group_expfactor
),
808 gmexp_id
, gmexp_fill
, gmexp_exp
, gmexp_drop
));
811 static PyObject
*gmeth_checkgroup(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
813 static const char *const kwlist
[] = { "rng", 0 };
814 grand
*r
= &rand_global
;
817 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:checkgroup", KWLIST
,
820 if ((p
= G_CHECK(GROUP_G(me
), r
)) != 0)
827 static PyObject
*group_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
829 int b
= group_samep(GROUP_G(x
), GROUP_G(y
));
833 default: TYERR("can't order groups");
840 static PyObject
*meth__GE_frombuf(PyObject
*me
, PyObject
*arg
)
848 if (!PyArg_ParseTuple(arg
, "Os#:frombuf", &me
, &p
, &n
))
853 if (G_FROMBUF(g
, &b
, x
))
854 VALERR("invalid data");
855 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
857 if (x
) G_DESTROY(g
, x
);
861 static PyObject
*meth__GE_fromraw(PyObject
*me
, PyObject
*arg
)
869 if (!PyArg_ParseTuple(arg
, "Os#:fromraw", &me
, &p
, &n
))
874 if (G_FROMRAW(g
, &b
, x
))
875 VALERR("invalid data");
876 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
878 if (x
) G_DESTROY(g
, x
);
882 static PyObject
*meth__GE_fromstring(PyObject
*me
, PyObject
*arg
)
890 if (!PyArg_ParseTuple(arg
, "Os#:fromstring", &me
, &p
, &n
))
896 if (G_READ(g
, x
, &mptext_stringops
, &sc
))
897 VALERR("bad group element string");
898 return (Py_BuildValue("(Ns#)", ge_pywrap(me
, x
),
899 sc
.buf
, (Py_ssize_t
)(sc
.lim
- sc
.buf
)));
901 if (x
) G_DESTROY(g
, x
);
905 static PyObject
*meth__Group_parse(PyObject
*me
, PyObject
*arg
)
911 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
))
915 if ((g
= group_parse(&qd
)) == 0)
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 PyGetSetDef ge_pygetset
[] = {
964 #define GETSETNAME(op, name) ge##op##_##name
965 GET (group
, "X.group -> group containing X")
970 static 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")
977 X.toec([curve = ECPt]) -> 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 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 &ge_pynumber
, /* @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 ge_pymethods
, /* @tp_methods@ */
1062 0, /* @tp_members@ */
1063 ge_pygetset
, /* @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 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 PyMethodDef group_pymethods
[] = {
1089 #define METHNAME(name) gmeth_##name
1091 G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1092 KWMETH(checkgroup
, "G.checkgroup([rng = rand]): check group is good")
1097 static PyTypeObject group_pytype_skel
= {
1098 PyObject_HEAD_INIT(0) 0, /* Header */
1099 "Group", /* @tp_name@ */
1100 sizeof(group_pyobj
), /* @tp_basicsize@ */
1101 0, /* @tp_itemsize@ */
1103 group_pydealloc
, /* @tp_dealloc@ */
1105 0, /* @tp_getattr@ */
1106 0, /* @tp_setattr@ */
1107 0, /* @tp_compare@ */
1109 0, /* @tp_as_number@ */
1110 0, /* @tp_as_sequence@ */
1111 0, /* @tp_as_mapping@ */
1115 0, /* @tp_getattro@ */
1116 0, /* @tp_setattro@ */
1117 0, /* @tp_as_buffer@ */
1118 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1119 Py_TPFLAGS_BASETYPE
,
1122 "Abstract base class for groups.",
1124 0, /* @tp_traverse@ */
1126 group_pyrichcompare
, /* @tp_richcompare@ */
1127 0, /* @tp_weaklistoffset@ */
1129 0, /* @tp_iternext@ */
1130 group_pymethods
, /* @tp_methods@ */
1131 0, /* @tp_members@ */
1132 group_pygetset
, /* @tp_getset@ */
1135 0, /* @tp_descr_get@ */
1136 0, /* @tp_descr_set@ */
1137 0, /* @tp_dictoffset@ */
1139 PyType_GenericAlloc
, /* @tp_alloc@ */
1140 abstract_pynew
, /* @tp_new@ */
1145 static PyObject
*pgget_info(PyObject
*me
, void *hunoz
)
1148 gctx_prime
*gg
= (gctx_prime
*)GROUP_G(me
);
1149 dp
.p
= MP_COPY(gg
->mm
.m
);
1150 dp
.q
= MP_COPY(gg
->g
.r
);
1151 dp
.g
= mpmont_reduce(&gg
->mm
, MP_NEW
, gg
->gen
.x
);
1152 return (fginfo_pywrap(&dp
, dhinfo_pytype
));
1155 static PyGetSetDef primegroup_pygetset
[] = {
1156 #define GETSETNAME(op, name) pg##op##_##name
1157 GET (info
, "G.info -> information about the group")
1162 static PyObject
*primegroup_pynew(PyTypeObject
*ty
,
1163 PyObject
*arg
, PyObject
*kw
)
1166 static const char *const kwlist
[] = { "info", 0 };
1168 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1171 return (group_dopywrap(ty
, group_prime(FGINFO_DP(i
))));
1174 static PyTypeObject primegroup_pytype_skel
= {
1175 PyObject_HEAD_INIT(0) 0, /* Header */
1176 "PrimeGroup", /* @tp_name@ */
1177 sizeof(group_pyobj
), /* @tp_basicsize@ */
1178 0, /* @tp_itemsize@ */
1180 group_pydealloc
, /* @tp_dealloc@ */
1182 0, /* @tp_getattr@ */
1183 0, /* @tp_setattr@ */
1184 0, /* @tp_compare@ */
1186 0, /* @tp_as_number@ */
1187 0, /* @tp_as_sequence@ */
1188 0, /* @tp_as_mapping@ */
1192 0, /* @tp_getattro@ */
1193 0, /* @tp_setattro@ */
1194 0, /* @tp_as_buffer@ */
1195 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1196 Py_TPFLAGS_BASETYPE
,
1199 "PrimeGroup(INFO): subgroups of prime fields.",
1201 0, /* @tp_traverse@ */
1203 0, /* @tp_richcompare@ */
1204 0, /* @tp_weaklistoffset@ */
1206 0, /* @tp_iternext@ */
1207 0, /* @tp_methods@ */
1208 0, /* @tp_members@ */
1209 primegroup_pygetset
, /* @tp_getset@ */
1212 0, /* @tp_descr_get@ */
1213 0, /* @tp_descr_set@ */
1214 0, /* @tp_dictoffset@ */
1216 PyType_GenericAlloc
, /* @tp_alloc@ */
1217 primegroup_pynew
, /* @tp_new@ */
1222 static PyObject
*bgget_info(PyObject
*me
, void *hunoz
)
1225 gctx_bin
*gg
= (gctx_bin
*)GROUP_G(me
);
1226 dp
.p
= MP_COPY(gg
->r
.p
);
1227 dp
.q
= MP_COPY(gg
->g
.r
);
1228 dp
.g
= MP_COPY(gg
->gen
.x
);
1229 return (fginfo_pywrap(&dp
, bindhinfo_pytype
));
1232 static PyGetSetDef bingroup_pygetset
[] = {
1233 #define GETSETNAME(op, name) bg##op##_##name
1234 GET (info
, "G.info -> information about the group")
1239 static PyObject
*bingroup_pynew(PyTypeObject
*ty
,
1240 PyObject
*arg
, PyObject
*kw
)
1243 static const char *const kwlist
[] = { "info", 0 };
1245 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1246 bindhinfo_pytype
, &i
))
1248 return (group_dopywrap(ty
, group_binary(FGINFO_DP(i
))));
1251 static PyTypeObject bingroup_pytype_skel
= {
1252 PyObject_HEAD_INIT(0) 0, /* Header */
1253 "BinGroup", /* @tp_name@ */
1254 sizeof(group_pyobj
), /* @tp_basicsize@ */
1255 0, /* @tp_itemsize@ */
1257 group_pydealloc
, /* @tp_dealloc@ */
1259 0, /* @tp_getattr@ */
1260 0, /* @tp_setattr@ */
1261 0, /* @tp_compare@ */
1263 0, /* @tp_as_number@ */
1264 0, /* @tp_as_sequence@ */
1265 0, /* @tp_as_mapping@ */
1269 0, /* @tp_getattro@ */
1270 0, /* @tp_setattro@ */
1271 0, /* @tp_as_buffer@ */
1272 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1273 Py_TPFLAGS_BASETYPE
,
1276 "BinGroup(INFO): subgroups of binary fields.",
1278 0, /* @tp_traverse@ */
1280 0, /* @tp_richcompare@ */
1281 0, /* @tp_weaklistoffset@ */
1283 0, /* @tp_iternext@ */
1284 0, /* @tp_methods@ */
1285 0, /* @tp_members@ */
1286 bingroup_pygetset
, /* @tp_getset@ */
1289 0, /* @tp_descr_get@ */
1290 0, /* @tp_descr_set@ */
1291 0, /* @tp_dictoffset@ */
1293 PyType_GenericAlloc
, /* @tp_alloc@ */
1294 bingroup_pynew
, /* @tp_new@ */
1299 static PyObject
*egget_info(PyObject
*me
, void *hunoz
)
1302 gctx_ec
*gg
= (gctx_ec
*)GROUP_G(me
);
1304 ecinfo_copy(&ei
, &gg
->ei
);
1305 return (ecinfo_pywrap(&ei
));
1308 static PyGetSetDef ecgroup_pygetset
[] = {
1309 #define GETSETNAME(op, name) eg##op##_##name
1310 GET (info
, "G.info -> information about the group")
1315 static PyObject
*ecgroup_pynew(PyTypeObject
*ty
,
1316 PyObject
*arg
, PyObject
*kw
)
1320 static const char *const kwlist
[] = { "info", 0 };
1322 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1325 ecinfo_copy(&ei
, ECINFO_EI(i
));
1326 return (group_dopywrap(ty
, group_ec(&ei
)));
1329 static PyTypeObject ecgroup_pytype_skel
= {
1330 PyObject_HEAD_INIT(0) 0, /* Header */
1331 "ECGroup", /* @tp_name@ */
1332 sizeof(group_pyobj
), /* @tp_basicsize@ */
1333 0, /* @tp_itemsize@ */
1335 group_pydealloc
, /* @tp_dealloc@ */
1337 0, /* @tp_getattr@ */
1338 0, /* @tp_setattr@ */
1339 0, /* @tp_compare@ */
1341 0, /* @tp_as_number@ */
1342 0, /* @tp_as_sequence@ */
1343 0, /* @tp_as_mapping@ */
1347 0, /* @tp_getattro@ */
1348 0, /* @tp_setattro@ */
1349 0, /* @tp_as_buffer@ */
1350 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1351 Py_TPFLAGS_BASETYPE
,
1354 "ECGroup(INFO): elliptic curve groups.",
1356 0, /* @tp_traverse@ */
1358 0, /* @tp_richcompare@ */
1359 0, /* @tp_weaklistoffset@ */
1361 0, /* @tp_iternext@ */
1362 0, /* @tp_methods@ */
1363 0, /* @tp_members@ */
1364 ecgroup_pygetset
, /* @tp_getset@ */
1367 0, /* @tp_descr_get@ */
1368 0, /* @tp_descr_set@ */
1369 0, /* @tp_dictoffset@ */
1371 PyType_GenericAlloc
, /* @tp_alloc@ */
1372 ecgroup_pynew
, /* @tp_new@ */
1377 /*----- Global stuff ------------------------------------------------------*/
1379 static PyMethodDef methods
[] = {
1380 #define METHNAME(name) meth_##name
1381 METH (_GE_frombuf
, "frombuf(BUF) -> X, REST")
1382 METH (_GE_fromraw
, "fromraw(BUF) -> X, REST")
1383 METH (_GE_fromstring
, "fromstring(STR) -> X, REST")
1384 METH (_Group_parse
, "parse(STR) -> G, REST")
1385 METH (_DHInfo_parse
, "parse(STR) -> D, REST")
1386 METH (_BinDHInfo_parse
, "parse(STR) -> D, REST")
1387 METH (_DHInfo__groupn
, 0)
1388 METH (_BinDHInfo__groupn
, 0)
1389 KWMETH(_DHInfo_generate
, "\
1390 generate(PBITS, [qbits = 0], [event = pgen_nullev],\n\
1391 [rng = rand], [nsteps = 0]) -> D")
1392 KWMETH(_DHInfo_genlimlee
, "\
1393 genlimlee(PBITS, QBITS, [event = pgen_nullev], [ievent = pgen_nullev],\n\
1394 [rng = rand], [nsteps = 0], [subgroupp = True]) -> (D, [Q, ...])")
1395 KWMETH(_DHInfo_gendsa
, "\
1396 gendsa(PBITS, QBITS, SEED, [event = pgen_nullev], [nsteps = 0])\n\
1397 -> (D, SEED, COUNT)")
1398 KWMETH(_DHInfo_genkcdsa
, "\
1399 gendsa(PBITS, QBITS, [event = pgen_nullev], [rng = rand], [nsteps = 0])\n\
1405 void group_pyinit(void)
1407 INITTYPE(fginfo
, root
);
1408 INITTYPE(dhinfo
, fginfo
);
1409 INITTYPE(bindhinfo
, dhinfo
);
1411 INITTYPE(group
, type
);
1412 INITTYPE(primegroup
, group
);
1413 INITTYPE(bingroup
, group
);
1414 INITTYPE(ecgroup
, group
);
1415 addmethods(methods
);
1418 void group_pyinsert(PyObject
*mod
)
1420 INSERT("FGInfo", fginfo_pytype
);
1421 INSERT("DHInfo", dhinfo_pytype
);
1422 INSERT("BinDHInfo", bindhinfo_pytype
);
1423 INSERT("GE", ge_pytype
);
1424 INSERT("Group", group_pytype
);
1425 INSERT("PrimeGroup", primegroup_pytype
);
1426 INSERT("BinGroup", bingroup_pytype
);
1427 INSERT("ECGroup", ecgroup_pytype
);
1428 INSERT("_pgroups", namedgroups(ptab
, &npgroups
));
1429 INSERT("_bingroups", namedgroups(bintab
, &nbingroups
));
1432 /*----- That's all, folks -------------------------------------------------*/