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
*dimeth_generate(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
102 grand
*r
= &rand_global
;
103 struct excinfo exc
= EXCINFO_INIT
;
104 pypgev evt
= { { 0 } };
105 static const char *const kwlist
[] =
106 { "pbits", "qbits", "event", "rng", "nsteps", 0 };
110 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&O&O&O&:generate", KWLIST
,
111 convuint
, &pl
, convuint
, &ql
,
112 convpgev
, &evt
, convgrand
, &r
,
115 if (dh_gen(&dp
, ql
, pl
, steps
, r
, evt
.ev
.proc
, evt
.ev
.ctx
))
117 rc
= fginfo_pywrap(&dp
, dhinfo_pytype
);
123 static PyObject
*dimeth_genlimlee(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
128 grand
*r
= &rand_global
;
129 struct excinfo exc
= EXCINFO_INIT
;
130 pypgev oe
= { { 0 } }, ie
= { { 0 } };
133 static const char *const kwlist
[] = {
134 "pbits", "qbits", "event", "ievent",
135 "rng", "nsteps", "subgroupp", 0
139 PyObject
*rc
= 0, *vec
= 0;
141 oe
.exc
= ie
.exc
= &exc
;
142 if (!PyArg_ParseTupleAndKeywords(arg
, kw
,
143 "O&O&|O&O&O&O&O&:genlimlee", KWLIST
,
144 convuint
, &pl
, convuint
, &ql
,
145 convpgev
, &oe
, convpgev
, &ie
,
146 convgrand
, &r
, convuint
, &steps
,
147 convbool
, &subgroupp
))
149 if (subgroupp
) f
|= DH_SUBGROUP
;
150 if (dh_limlee(&dp
, ql
, pl
, f
, steps
, r
,
151 oe
.ev
.proc
, oe
.ev
.ctx
, ie
.ev
.proc
, ie
.ev
.ctx
, &nf
, &v
))
153 vec
= PyList_New(nf
);
154 for (i
= 0; i
< nf
; i
++)
155 PyList_SET_ITEM(vec
, i
, mp_pywrap(v
[i
]));
157 rc
= Py_BuildValue("(NN)", fginfo_pywrap(&dp
, dhinfo_pytype
), vec
);
159 droppgev(&oe
); droppgev(&ie
);
163 static PyObject
*dimeth_genkcdsa(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
168 grand
*r
= &rand_global
;
169 struct excinfo exc
= EXCINFO_INIT
;
170 pypgev evt
= { { 0 } };
171 static const char *const kwlist
[] =
172 { "pbits", "qbits", "event", "rng", "nsteps", 0 };
177 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&|O&O&O&:genkcdsa", KWLIST
,
178 convuint
, &pl
, convuint
, &ql
,
179 convpgev
, &evt
, convgrand
, &r
,
182 if (dh_kcdsagen(&dp
, ql
, pl
, 0, steps
, r
, evt
.ev
.proc
, evt
.ev
.ctx
))
184 mp_div(&v
, 0, dp
.p
, dp
.q
);
186 rc
= Py_BuildValue("(NN)", fginfo_pywrap(&dp
, dhinfo_pytype
),
193 static PyObject
*dimeth_gendsa(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
201 struct excinfo exc
= EXCINFO_INIT
;
202 pypgev evt
= { { 0 } };
203 static const char *const kwlist
[] =
204 { "pbits", "qbits", "seed", "event", "nsteps", 0 };
208 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&s#|O&O&:gendsa", KWLIST
,
209 convuint
, &pl
, convuint
, &ql
,
210 &k
, &ksz
, convpgev
, &evt
,
213 if (dsa_gen(&dp
, ql
, pl
, steps
, k
, ksz
, &ds
, evt
.ev
.proc
, evt
.ev
.ctx
))
215 rc
= Py_BuildValue("(NNl)", fginfo_pywrap(&dp
, dhinfo_pytype
),
216 bytestring_pywrap(ds
.p
, ds
.sz
), (long)ds
.count
);
223 static int npgroups
= -1, nbingroups
= -1;
225 static PyObject
*namedgroups(const pentry
*pp
, int *ne
)
232 for (i
= 0; pp
[i
].name
; i
++) {
234 for (j
= 0; j
< i
; j
++) {
235 if (pp
[i
].data
== pp
[j
].data
) {
236 c
= PyDict_GetItemString(d
, (/*unconst*/ char *)pp
[j
].name
);
241 c
= PyInt_FromLong(i
);
243 PyDict_SetItemString(d
, (/*unconst*/ char *)p
, c
);
250 static PyObject
*meth__groupn(PyObject
*me
, PyObject
*arg
,
251 PyTypeObject
*ty
, const pentry
*pp
, int ne
)
257 if (!PyArg_ParseTuple(arg
, "i:_groupn", &i
)) goto end
;
258 if (i
< 0 || i
>= ne
) VALERR("group index out of range");
259 dh_infofromdata(&gp
, pp
[i
].data
);
260 rc
= fginfo_pywrap(&gp
, ty
);
265 static PyObject
*dimeth__groupn(PyObject
*me
, PyObject
*arg
)
266 { return (meth__groupn(me
, arg
, dhinfo_pytype
, ptab
, npgroups
)); }
268 static PyObject
*bimeth__groupn(PyObject
*me
, PyObject
*arg
)
269 { return (meth__groupn(me
, arg
, bindhinfo_pytype
, bintab
, nbingroups
)); }
271 static PyObject
*meth__parse(PyObject
*me
, PyObject
*arg
, PyTypeObject
*ty
,
272 int (*parse
)(qd_parse
*, gprime_param
*))
279 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
281 if (parse(&qd
, &gp
)) VALERR(qd
.e
);
282 rc
= fginfo_pywrap(&gp
, ty
);
287 static PyObject
*dimeth_parse(PyObject
*me
, PyObject
*arg
)
288 { return (meth__parse(me
, arg
, dhinfo_pytype
, dh_parse
)); }
290 static PyObject
*bimeth_parse(PyObject
*me
, PyObject
*arg
)
291 { return (meth__parse(me
, arg
, bindhinfo_pytype
, dhbin_parse
)); }
293 static const PyGetSetDef fginfo_pygetset
[] = {
294 #define GETSETNAME(op, name) fi##op##_##name
295 GET (r
, "I.r -> group order")
300 static const PyGetSetDef dhinfo_pygetset
[] = {
301 #define GETSETNAME(op, name) di##op##_##name
302 GET (p
, "I.p -> prime")
303 GET (g
, "I.g -> generator")
308 static const PyMethodDef dhinfo_pymethods
[] = {
309 #define METHNAME(name) dimeth_##name
310 SMTH (parse
, "parse(STR) -> D, REST")
313 "generate(PBITS, [qbits = 0], [event = pgen_nullev],\n"
314 " [rng = rand], [nsteps = 0]) -> D")
316 "genlimlee(PBITS, QBITS, [event = pgen_nullev], "
317 "[ievent = pgen_nullev],\n"
318 " [rng = rand], [nsteps = 0], [subgroupp = True]) "
321 "gendsa(PBITS, QBITS, SEED, [event = pgen_nullev], [nsteps = 0])\n"
322 " -> (D, SEED, COUNT)")
324 "gendsa(PBITS, QBITS, [event = pgen_nullev], "
325 "[rng = rand], [nsteps = 0])\n"
331 static const PyGetSetDef bindhinfo_pygetset
[] = {
332 #define GETSETNAME(op, name) bi##op##_##name
333 GET (p
, "I.p -> irreducible polynomial")
334 GET (m
, "I.m -> degree of polynomial")
335 GET (g
, "I.g -> generator")
340 static const PyMethodDef bindhinfo_pymethods
[] = {
341 #define METHNAME(name) bimeth_##name
342 SMTH (parse
, "parse(STR) -> D, REST")
348 static const PyTypeObject fginfo_pytype_skel
= {
349 PyObject_HEAD_INIT(0) 0, /* Header */
350 "FGInfo", /* @tp_name@ */
351 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
352 0, /* @tp_itemsize@ */
354 fginfo_pydealloc
, /* @tp_dealloc@ */
356 0, /* @tp_getattr@ */
357 0, /* @tp_setattr@ */
358 0, /* @tp_compare@ */
360 0, /* @tp_as_number@ */
361 0, /* @tp_as_sequence@ */
362 0, /* @tp_as_mapping@ */
366 0, /* @tp_getattro@ */
367 0, /* @tp_setattro@ */
368 0, /* @tp_as_buffer@ */
369 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
373 "Abstract base class for field-group information objects.",
375 0, /* @tp_traverse@ */
377 0, /* @tp_richcompare@ */
378 0, /* @tp_weaklistoffset@ */
380 0, /* @tp_iternext@ */
381 0, /* @tp_methods@ */
382 0, /* @tp_members@ */
383 PYGETSET(fginfo
), /* @tp_getset@ */
386 0, /* @tp_descr_get@ */
387 0, /* @tp_descr_set@ */
388 0, /* @tp_dictoffset@ */
390 PyType_GenericAlloc
, /* @tp_alloc@ */
391 abstract_pynew
, /* @tp_new@ */
396 static const PyTypeObject dhinfo_pytype_skel
= {
397 PyObject_HEAD_INIT(0) 0, /* Header */
398 "DHInfo", /* @tp_name@ */
399 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
400 0, /* @tp_itemsize@ */
402 fginfo_pydealloc
, /* @tp_dealloc@ */
404 0, /* @tp_getattr@ */
405 0, /* @tp_setattr@ */
406 0, /* @tp_compare@ */
408 0, /* @tp_as_number@ */
409 0, /* @tp_as_sequence@ */
410 0, /* @tp_as_mapping@ */
414 0, /* @tp_getattro@ */
415 0, /* @tp_setattro@ */
416 0, /* @tp_as_buffer@ */
417 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
421 "DHInfo(P, R, G): standard (integer) Diffie-Hellman group information.",
423 0, /* @tp_traverse@ */
425 0, /* @tp_richcompare@ */
426 0, /* @tp_weaklistoffset@ */
428 0, /* @tp_iternext@ */
429 PYMETHODS(dhinfo
), /* @tp_methods@ */
430 0, /* @tp_members@ */
431 PYGETSET(dhinfo
), /* @tp_getset@ */
434 0, /* @tp_descr_get@ */
435 0, /* @tp_descr_set@ */
436 0, /* @tp_dictoffset@ */
438 PyType_GenericAlloc
, /* @tp_alloc@ */
439 fginfo_pynew
, /* @tp_new@ */
444 static const PyTypeObject bindhinfo_pytype_skel
= {
445 PyObject_HEAD_INIT(0) 0, /* Header */
446 "BinDHInfo", /* @tp_name@ */
447 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
448 0, /* @tp_itemsize@ */
450 fginfo_pydealloc
, /* @tp_dealloc@ */
452 0, /* @tp_getattr@ */
453 0, /* @tp_setattr@ */
454 0, /* @tp_compare@ */
456 0, /* @tp_as_number@ */
457 0, /* @tp_as_sequence@ */
458 0, /* @tp_as_mapping@ */
462 0, /* @tp_getattro@ */
463 0, /* @tp_setattro@ */
464 0, /* @tp_as_buffer@ */
465 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
469 "BinDHInfo(P, R, G): binary-field Diffie-Hellman group information.",
471 0, /* @tp_traverse@ */
473 0, /* @tp_richcompare@ */
474 0, /* @tp_weaklistoffset@ */
476 0, /* @tp_iternext@ */
477 PYMETHODS(bindhinfo
), /* @tp_methods@ */
478 0, /* @tp_members@ */
479 PYGETSET(bindhinfo
), /* @tp_getset@ */
482 0, /* @tp_descr_get@ */
483 0, /* @tp_descr_set@ */
484 0, /* @tp_dictoffset@ */
486 PyType_GenericAlloc
, /* @tp_alloc@ */
487 fginfo_pynew
, /* @tp_new@ */
492 /*----- General utilities -------------------------------------------------*/
494 PyTypeObject
*ge_pytype
, *group_pytype
;
495 static PyTypeObject
*primegroup_pytype
, *bingroup_pytype
, *ecgroup_pytype
;
497 group
*group_copy(group
*g
)
499 if (STRCMP(G_NAME(g
), ==, "prime")) {
500 gctx_prime
*gc
= (gctx_prime
*)g
;
502 gp
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
505 g
= group_prime(&gp
);
507 } else if (STRCMP(G_NAME(g
), ==, "bin")) {
508 gctx_bin
*gc
= (gctx_bin
*)g
;
510 gb
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
513 g
= group_binary(&gb
);
515 } else if (STRCMP(G_NAME(g
), ==, "ec")) {
516 gctx_ec
*gc
= (gctx_ec
*)g
;
518 if ((ei
.c
= eccurve_copy(gc
->ei
.c
)) == 0)
521 EC_COPY(&ei
.g
, &gc
->ei
.g
);
522 ei
.r
= MP_COPY(gc
->ei
.r
);
523 ei
.h
= MP_COPY(gc
->ei
.h
);
530 PyObject
*ge_pywrap(PyObject
*gobj
, ge
*x
)
532 ge_pyobj
*z
= PyObject_New(ge_pyobj
, (PyTypeObject
*)gobj
);
534 z
->g
= GROUP_G(gobj
);
536 return ((PyObject
*)z
);
539 static PyObject
*ge_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
541 static const char *const kwlist
[] = { "x", 0 };
550 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", KWLIST
, &x
)) goto end
;
552 if (ECPT_PYCHECK(x
)) {
554 if (G_FROMEC(g
, xx
, &p
))
555 TYERR("can't convert from elliptic curve point");
557 } else if ((y
= tomp(x
)) != 0) {
558 if (G_FROMINT(g
, xx
, y
))
559 TYERR("can't convert from integer");
561 } else if (PyString_Check(x
)) {
562 sc
.buf
= PyString_AS_STRING(x
);
563 sc
.lim
= sc
.buf
+ PyString_GET_SIZE(x
);
564 if (G_READ(g
, xx
, &mptext_stringops
, &sc
) || sc
.buf
< sc
.lim
)
565 VALERR("malformed group element string");
567 TYERR("can't convert to group element");
568 return (ge_pywrap((PyObject
*)ty
, xx
));
572 if (xx
) G_DESTROY(g
, xx
);
576 static PyObject
*group_dopywrap(PyTypeObject
*ty
, group
*g
)
578 group_pyobj
*gobj
= newtype(ty
, 0, g
->ops
->name
);
580 gobj
->ty
.ht_type
.tp_basicsize
= sizeof(ge_pyobj
);
581 gobj
->ty
.ht_type
.tp_base
= ge_pytype
;
582 Py_INCREF(group_pytype
);
583 gobj
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
584 Py_TPFLAGS_BASETYPE
|
585 Py_TPFLAGS_CHECKTYPES
|
586 Py_TPFLAGS_HEAPTYPE
);
587 gobj
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
588 gobj
->ty
.ht_type
.tp_free
= 0;
589 gobj
->ty
.ht_type
.tp_new
= ge_pynew
;
590 typeready(&gobj
->ty
.ht_type
);
591 return ((PyObject
*)gobj
);
594 PyObject
*group_pywrap(group
*g
)
598 if (STRCMP(G_NAME(g
), ==, "prime")) ty
= primegroup_pytype
;
599 else if (STRCMP(G_NAME(g
), ==, "bin")) ty
= bingroup_pytype
;
600 else if (STRCMP(G_NAME(g
), ==, "ec")) ty
= ecgroup_pytype
;
602 return (group_dopywrap(ty
, g
));
605 /*----- Group elements ----------------------------------------------------*/
607 #define BINOP(name) \
608 static PyObject *ge_py##name(PyObject *x, PyObject *y) \
612 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \
613 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \
617 g->ops->name(g, z, GE_X(x), GE_X(y)); \
618 return (ge_pywrap(GE_GOBJ(x), z)); \
625 static PyObject *gemeth_##name(PyObject *me) \
631 g->ops->name(g, z, GE_X(me)); \
632 return (ge_pywrap(GE_GOBJ(me), z)); \
638 static PyObject
*ge_pyexp(PyObject
*x
, PyObject
*n
, PyObject
*m
)
643 if (m
!= Py_None
|| !GE_PYCHECK(x
) || (nn
= getmp(n
)) == 0)
645 z
= G_CREATE(GE_G(x
));
646 G_EXP(GE_G(x
), z
, GE_X(x
), nn
);
648 return (ge_pywrap(GE_GOBJ(x
), z
));
651 static void ge_pydealloc(PyObject
*me
)
653 G_DESTROY(GE_G(me
), GE_X(me
));
654 Py_DECREF(GE_GOBJ(me
));
658 static void group_pydealloc(PyObject
*me
)
660 G_DESTROYGROUP(GROUP_G(me
));
661 PyType_Type
.tp_dealloc(me
);
664 static PyObject
*gmexp_id(PyObject
*me
)
666 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
667 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
670 static int gmexp_fill(void *pp
, PyObject
*me
, PyObject
*x
, PyObject
*m
)
672 group_expfactor
*f
= pp
;
674 if (!GE_PYCHECK(x
) || GE_G(x
) != GROUP_G(me
) || (f
->exp
= getmp(m
)) == 0)
680 static PyObject
*ge_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
685 if (!GE_PYCHECK(x
) || !GE_PYCHECK(y
) ||
686 (GE_G(x
) != GE_G(y
) && !group_samep(GE_G(x
), GE_G(y
))))
689 case Py_EQ
: b
= G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
690 case Py_NE
: b
= !G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
691 default: TYERR("group elements are unordered");
698 static PyObject
*gemeth_check(PyObject
*me
)
700 if (group_check(GE_G(me
), GE_X(me
))) VALERR("bad group element");
706 static int ge_pynonzerop(PyObject
*x
)
707 { return (!G_IDENTP(GE_G(x
), GE_X(x
))); }
709 static PyObject
*ge_pystr(PyObject
*me
)
714 group_writedstr(GE_G(me
), GE_X(me
), &d
);
715 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
720 static PyObject
*ge_pylong(PyObject
*me
)
725 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
726 TYERR("can't convert to integer");
733 static PyObject
*ge_pyint(PyObject
*me
)
739 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
740 TYERR("can't convert to integer");
741 if (!mp_tolong_checked(x
, &l
, 0)) rc
= PyInt_FromLong(l
);
742 else rc
= mp_topylong(x
);
748 static PyObject
*gemeth_toint(PyObject
*me
)
752 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
753 TYERR("can't convert to integer");
754 return (mp_pywrap(x
));
759 static PyObject
*gemeth_toec(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
761 static const char *const kwlist
[] = { "curve", 0 };
762 PyTypeObject
*cty
= 0;
768 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:toec", KWLIST
,
770 g
= GROUP_G(GE_GOBJ(me
));
772 if (!PyType_Check(cty
) || !PyType_IsSubtype(cty
, ecpt_pytype
))
773 TYERR("want subtype of catacomb.ECPt");
774 Py_INCREF((PyObject
*)cty
);
775 } else if (STRCMP(G_NAME(g
), ==, "ec")) {
776 c
= eccurve_copy(((gctx_ec
*)g
)->ei
.c
);
777 cty
= (PyTypeObject
*)eccurve_pywrap(0, c
);
780 Py_INCREF((PyObject
*)cty
);
782 if (G_TOEC(GE_G(me
), &p
, GE_X(me
))) {
783 Py_DECREF((PyObject
*)cty
);
784 TYERR("can't convert to ec point");
786 rc
= ecpt_pywrapout(cty
, &p
);
787 Py_DECREF((PyObject
*)cty
);
792 static PyObject
*gemeth_tobuf(PyObject
*me
)
798 n
= GE_G(me
)->noctets
+ 4;
799 rc
= bytestring_pywrap(0, n
);
800 buf_init(&b
, PyString_AS_STRING(rc
), n
);
801 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
803 _PyString_Resize(&rc
, BLEN(&b
));
807 static PyObject
*gemeth_toraw(PyObject
*me
)
813 n
= GE_G(me
)->noctets
;
814 rc
= bytestring_pywrap(0, n
);
815 buf_init(&b
, PyString_AS_STRING(rc
), n
);
816 G_TORAW(GE_G(me
), &b
, GE_X(me
));
818 _PyString_Resize(&rc
, BLEN(&b
));
822 static PyObject
*gmexp_exp(PyObject
*me
, void *pp
, int n
)
824 ge
*z
= G_CREATE(GROUP_G(me
));
825 G_MEXP(GROUP_G(me
), z
, pp
, n
);
826 return (ge_pywrap(me
, z
));
829 static void gmexp_drop(void *pp
)
831 group_expfactor
*f
= pp
;
835 static PyObject
*gmeth_mexp(PyObject
*me
, PyObject
*arg
)
837 return (mexp_common(me
, arg
, sizeof(group_expfactor
),
838 gmexp_id
, gmexp_fill
, gmexp_exp
, gmexp_drop
));
841 static PyObject
*gmeth_checkgroup(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
843 static const char *const kwlist
[] = { "rng", 0 };
844 grand
*r
= &rand_global
;
847 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:checkgroup", KWLIST
,
850 if ((p
= G_CHECK(GROUP_G(me
), r
)) != 0)
857 static PyObject
*group_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
859 int b
= group_samep(GROUP_G(x
), GROUP_G(y
));
863 default: TYERR("can't order groups");
870 static PyObject
*gemeth_frombuf(PyObject
*me
, PyObject
*arg
)
878 if (!PyArg_ParseTuple(arg
, "s#:frombuf", &p
, &n
)) return (0);
882 if (G_FROMBUF(g
, &b
, x
)) VALERR("invalid data");
883 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
885 if (x
) G_DESTROY(g
, x
);
889 static PyObject
*gemeth_fromraw(PyObject
*me
, PyObject
*arg
)
897 if (!PyArg_ParseTuple(arg
, "s#:fromraw", &p
, &n
)) return (0);
901 if (G_FROMRAW(g
, &b
, x
)) VALERR("invalid data");
902 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
904 if (x
) G_DESTROY(g
, x
);
908 static PyObject
*gemeth_fromstring(PyObject
*me
, PyObject
*arg
)
916 if (!PyArg_ParseTuple(arg
, "s#:fromstring", &p
, &n
)) return (0);
921 if (G_READ(g
, x
, &mptext_stringops
, &sc
))
922 VALERR("bad group element string");
923 return (Py_BuildValue("(Ns#)", ge_pywrap(me
, x
),
924 sc
.buf
, (Py_ssize_t
)(sc
.lim
- sc
.buf
)));
926 if (x
) G_DESTROY(g
, x
);
930 static PyObject
*gmeth_parse(PyObject
*me
, PyObject
*arg
)
936 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
938 if ((g
= group_parse(&qd
)) == 0) VALERR(qd
.e
);
939 return (group_pywrap(g
));
944 static PyObject
*geget_group(PyObject
*me
, void *hunoz
)
945 { RETURN_OBJ(GE_GOBJ(me
)); }
947 static PyObject
*gget_nbits(PyObject
*me
, void *hunoz
)
948 { return (PyInt_FromLong(GROUP_G(me
)->nbits
)); }
950 static PyObject
*gget_noctets(PyObject
*me
, void *hunoz
)
951 { return (PyInt_FromLong(GROUP_G(me
)->noctets
)); }
953 static PyObject
*gget_i(PyObject
*me
, void *hunoz
)
955 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
956 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
959 static PyObject
*gget_g(PyObject
*me
, void *hunoz
)
961 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
962 G_COPY(g
, x
, g
->g
); return (ge_pywrap(me
, x
));
965 static long ge_pyhash(PyObject
*me
)
968 size_t sz
= GE_G(me
)->noctets
+ 4;
969 uint32 h
= 0xf672c776 + GE_G(me
)->ops
->ty
;
970 octet
*p
= xmalloc(sz
);
972 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
974 h
= unihash_hash(&unihash_global
, h
, BBASE(&b
), BLEN(&b
));
976 return (h
% LONG_MAX
);
979 static PyObject
*gget_r(PyObject
*me
, void *hunoz
)
980 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->r
))); }
982 static PyObject
*gget_h(PyObject
*me
, void *hunoz
)
983 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->h
))); }
985 static const PyGetSetDef ge_pygetset
[] = {
986 #define GETSETNAME(op, name) ge##op##_##name
987 GET (group
, "X.group -> group containing X")
992 static const PyMethodDef ge_pymethods
[] = {
993 #define METHNAME(name) gemeth_##name
994 NAMETH(inv
, "X.inv() -> inverse element of X")
995 NAMETH(sqr
, "X.sqr() -> X^2 = X * X")
996 NAMETH(check
, "X.check() -> check X really belongs to its group")
997 NAMETH(toint
, "X.toint() -> X converted to an integer")
998 KWMETH(toec
, "X.toec([curve = ECPt]) -> "
999 "X converted to elliptic curve point")
1000 NAMETH(tobuf
, "X.tobuf() -> X in buffer representation")
1001 NAMETH(toraw
, "X.toraw() -> X in raw representation")
1002 CMTH (frombuf
, "frombuf(BUF) -> X, REST")
1003 CMTH (fromraw
, "fromraw(BUF) -> X, REST")
1004 CMTH (fromstring
, "fromstring(STR) -> X, REST")
1009 static const PyNumberMethods ge_pynumber
= {
1011 0, /* @nb_subtract@ */
1012 ge_pymul
, /* @nb_multiply@ */
1013 ge_pydiv
, /* @nb_divide@ */
1014 0, /* @nb_remainder@ */
1015 0, /* @nb_divmod@ */
1016 ge_pyexp
, /* @nb_power@ */
1017 0, /* @nb_negative@ */
1018 0, /* @nb_positive@ */
1019 0, /* @nb_absolute@ */
1020 ge_pynonzerop
, /* @nb_nonzero@ */
1021 0, /* @nb_invert@ */
1022 0, /* @nb_lshift@ */
1023 0, /* @nb_rshift@ */
1027 0, /* @nb_coerce@ */
1028 ge_pyint
, /* @nb_int@ */
1029 ge_pylong
, /* @nb_long@ */
1030 0 /* meaningless */, /* @nb_float@ */
1034 0, /* @nb_inplace_add@ */
1035 0, /* @nb_inplace_subtract@ */
1036 0, /* @nb_inplace_multiply@ */
1037 0, /* @nb_inplace_divide@ */
1038 0, /* @nb_inplace_remainder@ */
1039 0, /* @nb_inplace_power@ */
1040 0, /* @nb_inplace_lshift@ */
1041 0, /* @nb_inplace_rshift@ */
1042 0, /* @nb_inplace_and@ */
1043 0, /* @nb_inplace_xor@ */
1044 0, /* @nb_inplace_or@ */
1046 0, /* @nb_floor_divide@ */
1047 ge_pydiv
, /* @nb_true_divide@ */
1048 0, /* @nb_inplace_floor_divide@ */
1049 0, /* @nb_inplace_true_divide@ */
1052 static const PyTypeObject ge_pytype_skel
= {
1053 PyObject_HEAD_INIT(0) 0, /* Header */
1054 "GE", /* @tp_name@ */
1055 sizeof(ge_pyobj
), /* @tp_basicsize@ */
1056 0, /* @tp_itemsize@ */
1058 ge_pydealloc
, /* @tp_dealloc@ */
1060 0, /* @tp_getattr@ */
1061 0, /* @tp_setattr@ */
1062 0, /* @tp_compare@ */
1064 PYNUMBER(ge
), /* @tp_as_number@ */
1065 0, /* @tp_as_sequence@ */
1066 0, /* @tp_as_mapping@ */
1067 ge_pyhash
, /* @tp_hash@ */
1069 ge_pystr
, /* @tp_str@ */
1070 0, /* @tp_getattro@ */
1071 0, /* @tp_setattro@ */
1072 0, /* @tp_as_buffer@ */
1073 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1074 Py_TPFLAGS_CHECKTYPES
|
1075 Py_TPFLAGS_BASETYPE
,
1078 "Group elements, abstract base class.",
1080 0, /* @tp_traverse@ */
1082 ge_pyrichcompare
, /* @tp_richcompare@ */
1083 0, /* @tp_weaklistoffset@ */
1085 0, /* @tp_iternext@ */
1086 PYMETHODS(ge
), /* @tp_methods@ */
1087 0, /* @tp_members@ */
1088 PYGETSET(ge
), /* @tp_getset@ */
1091 0, /* @tp_descr_get@ */
1092 0, /* @tp_descr_set@ */
1093 0, /* @tp_dictoffset@ */
1095 PyType_GenericAlloc
, /* @tp_alloc@ */
1096 abstract_pynew
, /* @tp_new@ */
1101 static const PyGetSetDef group_pygetset
[] = {
1102 #define GETSETNAME(op, name) g##op##_##name
1103 GET (noctets
, "G.noctets -> size in octets of element")
1104 GET (nbits
, "G.nbits -> size in bits of element")
1105 GET (i
, "G.i -> group identity")
1106 GET (g
, "G.g -> group generator")
1107 GET (r
, "G.r -> group order")
1108 GET (h
, "G.h -> group cofactor")
1113 static const PyMethodDef group_pymethods
[] = {
1114 #define METHNAME(name) gmeth_##name
1115 METH (mexp
, "G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1116 KWMETH(checkgroup
, "G.checkgroup([rng = rand]): check group is good")
1117 SMTH (parse
, "parse(STR) -> G, REST")
1122 static const PyTypeObject group_pytype_skel
= {
1123 PyObject_HEAD_INIT(0) 0, /* Header */
1124 "Group", /* @tp_name@ */
1125 sizeof(group_pyobj
), /* @tp_basicsize@ */
1126 0, /* @tp_itemsize@ */
1128 group_pydealloc
, /* @tp_dealloc@ */
1130 0, /* @tp_getattr@ */
1131 0, /* @tp_setattr@ */
1132 0, /* @tp_compare@ */
1134 0, /* @tp_as_number@ */
1135 0, /* @tp_as_sequence@ */
1136 0, /* @tp_as_mapping@ */
1140 0, /* @tp_getattro@ */
1141 0, /* @tp_setattro@ */
1142 0, /* @tp_as_buffer@ */
1143 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1144 Py_TPFLAGS_BASETYPE
,
1147 "Abstract base class for groups.",
1149 0, /* @tp_traverse@ */
1151 group_pyrichcompare
, /* @tp_richcompare@ */
1152 0, /* @tp_weaklistoffset@ */
1154 0, /* @tp_iternext@ */
1155 PYMETHODS(group
), /* @tp_methods@ */
1156 0, /* @tp_members@ */
1157 PYGETSET(group
), /* @tp_getset@ */
1160 0, /* @tp_descr_get@ */
1161 0, /* @tp_descr_set@ */
1162 0, /* @tp_dictoffset@ */
1164 PyType_GenericAlloc
, /* @tp_alloc@ */
1165 abstract_pynew
, /* @tp_new@ */
1170 static PyObject
*pgget_info(PyObject
*me
, void *hunoz
)
1173 gctx_prime
*gg
= (gctx_prime
*)GROUP_G(me
);
1174 dp
.p
= MP_COPY(gg
->mm
.m
);
1175 dp
.q
= MP_COPY(gg
->g
.r
);
1176 dp
.g
= mpmont_reduce(&gg
->mm
, MP_NEW
, gg
->gen
.x
);
1177 return (fginfo_pywrap(&dp
, dhinfo_pytype
));
1180 static const PyGetSetDef primegroup_pygetset
[] = {
1181 #define GETSETNAME(op, name) pg##op##_##name
1182 GET (info
, "G.info -> information about the group")
1187 static PyObject
*primegroup_pynew(PyTypeObject
*ty
,
1188 PyObject
*arg
, PyObject
*kw
)
1191 static const char *const kwlist
[] = { "info", 0 };
1193 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1196 return (group_dopywrap(ty
, group_prime(FGINFO_DP(i
))));
1199 static const PyTypeObject primegroup_pytype_skel
= {
1200 PyObject_HEAD_INIT(0) 0, /* Header */
1201 "PrimeGroup", /* @tp_name@ */
1202 sizeof(group_pyobj
), /* @tp_basicsize@ */
1203 0, /* @tp_itemsize@ */
1205 group_pydealloc
, /* @tp_dealloc@ */
1207 0, /* @tp_getattr@ */
1208 0, /* @tp_setattr@ */
1209 0, /* @tp_compare@ */
1211 0, /* @tp_as_number@ */
1212 0, /* @tp_as_sequence@ */
1213 0, /* @tp_as_mapping@ */
1217 0, /* @tp_getattro@ */
1218 0, /* @tp_setattro@ */
1219 0, /* @tp_as_buffer@ */
1220 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1221 Py_TPFLAGS_BASETYPE
,
1224 "PrimeGroup(INFO): subgroups of prime fields.",
1226 0, /* @tp_traverse@ */
1228 0, /* @tp_richcompare@ */
1229 0, /* @tp_weaklistoffset@ */
1231 0, /* @tp_iternext@ */
1232 0, /* @tp_methods@ */
1233 0, /* @tp_members@ */
1234 PYGETSET(primegroup
), /* @tp_getset@ */
1237 0, /* @tp_descr_get@ */
1238 0, /* @tp_descr_set@ */
1239 0, /* @tp_dictoffset@ */
1241 PyType_GenericAlloc
, /* @tp_alloc@ */
1242 primegroup_pynew
, /* @tp_new@ */
1247 static PyObject
*bgget_info(PyObject
*me
, void *hunoz
)
1250 gctx_bin
*gg
= (gctx_bin
*)GROUP_G(me
);
1251 dp
.p
= MP_COPY(gg
->r
.p
);
1252 dp
.q
= MP_COPY(gg
->g
.r
);
1253 dp
.g
= MP_COPY(gg
->gen
.x
);
1254 return (fginfo_pywrap(&dp
, bindhinfo_pytype
));
1257 static const PyGetSetDef bingroup_pygetset
[] = {
1258 #define GETSETNAME(op, name) bg##op##_##name
1259 GET (info
, "G.info -> information about the group")
1264 static PyObject
*bingroup_pynew(PyTypeObject
*ty
,
1265 PyObject
*arg
, PyObject
*kw
)
1268 static const char *const kwlist
[] = { "info", 0 };
1270 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1271 bindhinfo_pytype
, &i
))
1273 return (group_dopywrap(ty
, group_binary(FGINFO_DP(i
))));
1276 static const PyTypeObject bingroup_pytype_skel
= {
1277 PyObject_HEAD_INIT(0) 0, /* Header */
1278 "BinGroup", /* @tp_name@ */
1279 sizeof(group_pyobj
), /* @tp_basicsize@ */
1280 0, /* @tp_itemsize@ */
1282 group_pydealloc
, /* @tp_dealloc@ */
1284 0, /* @tp_getattr@ */
1285 0, /* @tp_setattr@ */
1286 0, /* @tp_compare@ */
1288 0, /* @tp_as_number@ */
1289 0, /* @tp_as_sequence@ */
1290 0, /* @tp_as_mapping@ */
1294 0, /* @tp_getattro@ */
1295 0, /* @tp_setattro@ */
1296 0, /* @tp_as_buffer@ */
1297 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1298 Py_TPFLAGS_BASETYPE
,
1301 "BinGroup(INFO): subgroups of binary fields.",
1303 0, /* @tp_traverse@ */
1305 0, /* @tp_richcompare@ */
1306 0, /* @tp_weaklistoffset@ */
1308 0, /* @tp_iternext@ */
1309 0, /* @tp_methods@ */
1310 0, /* @tp_members@ */
1311 PYGETSET(bingroup
), /* @tp_getset@ */
1314 0, /* @tp_descr_get@ */
1315 0, /* @tp_descr_set@ */
1316 0, /* @tp_dictoffset@ */
1318 PyType_GenericAlloc
, /* @tp_alloc@ */
1319 bingroup_pynew
, /* @tp_new@ */
1324 static PyObject
*egget_info(PyObject
*me
, void *hunoz
)
1327 gctx_ec
*gg
= (gctx_ec
*)GROUP_G(me
);
1329 ecinfo_copy(&ei
, &gg
->ei
);
1330 return (ecinfo_pywrap(&ei
));
1333 static const PyGetSetDef ecgroup_pygetset
[] = {
1334 #define GETSETNAME(op, name) eg##op##_##name
1335 GET (info
, "G.info -> information about the group")
1340 static PyObject
*ecgroup_pynew(PyTypeObject
*ty
,
1341 PyObject
*arg
, PyObject
*kw
)
1345 static const char *const kwlist
[] = { "info", 0 };
1347 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1350 ecinfo_copy(&ei
, ECINFO_EI(i
));
1351 return (group_dopywrap(ty
, group_ec(&ei
)));
1354 static const PyTypeObject ecgroup_pytype_skel
= {
1355 PyObject_HEAD_INIT(0) 0, /* Header */
1356 "ECGroup", /* @tp_name@ */
1357 sizeof(group_pyobj
), /* @tp_basicsize@ */
1358 0, /* @tp_itemsize@ */
1360 group_pydealloc
, /* @tp_dealloc@ */
1362 0, /* @tp_getattr@ */
1363 0, /* @tp_setattr@ */
1364 0, /* @tp_compare@ */
1366 0, /* @tp_as_number@ */
1367 0, /* @tp_as_sequence@ */
1368 0, /* @tp_as_mapping@ */
1372 0, /* @tp_getattro@ */
1373 0, /* @tp_setattro@ */
1374 0, /* @tp_as_buffer@ */
1375 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1376 Py_TPFLAGS_BASETYPE
,
1379 "ECGroup(INFO): elliptic curve groups.",
1381 0, /* @tp_traverse@ */
1383 0, /* @tp_richcompare@ */
1384 0, /* @tp_weaklistoffset@ */
1386 0, /* @tp_iternext@ */
1387 0, /* @tp_methods@ */
1388 0, /* @tp_members@ */
1389 PYGETSET(ecgroup
), /* @tp_getset@ */
1392 0, /* @tp_descr_get@ */
1393 0, /* @tp_descr_set@ */
1394 0, /* @tp_dictoffset@ */
1396 PyType_GenericAlloc
, /* @tp_alloc@ */
1397 ecgroup_pynew
, /* @tp_new@ */
1402 /*----- Global stuff ------------------------------------------------------*/
1404 void group_pyinit(void)
1406 INITTYPE(fginfo
, root
);
1407 INITTYPE(dhinfo
, fginfo
);
1408 INITTYPE(bindhinfo
, dhinfo
);
1410 INITTYPE(group
, type
);
1411 INITTYPE(primegroup
, group
);
1412 INITTYPE(bingroup
, group
);
1413 INITTYPE(ecgroup
, group
);
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 -------------------------------------------------*/