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 PyObject
*meth__parse(PyObject
*me
, PyObject
*arg
, PyTypeObject
*ty
,
224 int (*parse
)(qd_parse
*, gprime_param
*))
231 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
233 if (parse(&qd
, &gp
)) VALERR(qd
.e
);
234 rc
= fginfo_pywrap(&gp
, ty
);
239 static PyObject
*dimeth_parse(PyObject
*me
, PyObject
*arg
)
240 { return (meth__parse(me
, arg
, dhinfo_pytype
, dh_parse
)); }
242 static PyObject
*bimeth_parse(PyObject
*me
, PyObject
*arg
)
243 { return (meth__parse(me
, arg
, bindhinfo_pytype
, dhbin_parse
)); }
245 static const PyGetSetDef fginfo_pygetset
[] = {
246 #define GETSETNAME(op, name) fi##op##_##name
247 GET (r
, "I.r -> group order")
252 static const PyGetSetDef dhinfo_pygetset
[] = {
253 #define GETSETNAME(op, name) di##op##_##name
254 GET (p
, "I.p -> prime")
255 GET (g
, "I.g -> generator")
260 static const PyMethodDef dhinfo_pymethods
[] = {
261 #define METHNAME(name) dimeth_##name
262 SMTH (parse
, "parse(STR) -> D, REST")
264 "generate(PBITS, [qbits = 0], [event = pgen_nullev],\n"
265 " [rng = rand], [nsteps = 0]) -> D")
267 "genlimlee(PBITS, QBITS, [event = pgen_nullev], "
268 "[ievent = pgen_nullev],\n"
269 " [rng = rand], [nsteps = 0], [subgroupp = True]) "
272 "gendsa(PBITS, QBITS, SEED, [event = pgen_nullev], [nsteps = 0])\n"
273 " -> (D, SEED, COUNT)")
275 "gendsa(PBITS, QBITS, [event = pgen_nullev], "
276 "[rng = rand], [nsteps = 0])\n"
282 static const PyGetSetDef bindhinfo_pygetset
[] = {
283 #define GETSETNAME(op, name) bi##op##_##name
284 GET (p
, "I.p -> irreducible polynomial")
285 GET (m
, "I.m -> degree of polynomial")
286 GET (g
, "I.g -> generator")
291 static const PyMethodDef bindhinfo_pymethods
[] = {
292 #define METHNAME(name) bimeth_##name
293 SMTH (parse
, "parse(STR) -> D, REST")
298 static const PyTypeObject fginfo_pytype_skel
= {
299 PyVarObject_HEAD_INIT(0, 0) /* Header */
300 "FGInfo", /* @tp_name@ */
301 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
302 0, /* @tp_itemsize@ */
304 fginfo_pydealloc
, /* @tp_dealloc@ */
306 0, /* @tp_getattr@ */
307 0, /* @tp_setattr@ */
308 0, /* @tp_compare@ */
310 0, /* @tp_as_number@ */
311 0, /* @tp_as_sequence@ */
312 0, /* @tp_as_mapping@ */
316 0, /* @tp_getattro@ */
317 0, /* @tp_setattro@ */
318 0, /* @tp_as_buffer@ */
319 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
323 "Abstract base class for field-group information objects.",
325 0, /* @tp_traverse@ */
327 0, /* @tp_richcompare@ */
328 0, /* @tp_weaklistoffset@ */
330 0, /* @tp_iternext@ */
331 0, /* @tp_methods@ */
332 0, /* @tp_members@ */
333 PYGETSET(fginfo
), /* @tp_getset@ */
336 0, /* @tp_descr_get@ */
337 0, /* @tp_descr_set@ */
338 0, /* @tp_dictoffset@ */
340 PyType_GenericAlloc
, /* @tp_alloc@ */
341 abstract_pynew
, /* @tp_new@ */
346 static const PyTypeObject dhinfo_pytype_skel
= {
347 PyVarObject_HEAD_INIT(0, 0) /* Header */
348 "DHInfo", /* @tp_name@ */
349 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
350 0, /* @tp_itemsize@ */
352 fginfo_pydealloc
, /* @tp_dealloc@ */
354 0, /* @tp_getattr@ */
355 0, /* @tp_setattr@ */
356 0, /* @tp_compare@ */
358 0, /* @tp_as_number@ */
359 0, /* @tp_as_sequence@ */
360 0, /* @tp_as_mapping@ */
364 0, /* @tp_getattro@ */
365 0, /* @tp_setattro@ */
366 0, /* @tp_as_buffer@ */
367 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
371 "DHInfo(P, R, G): standard (integer) Diffie-Hellman group information.",
373 0, /* @tp_traverse@ */
375 0, /* @tp_richcompare@ */
376 0, /* @tp_weaklistoffset@ */
378 0, /* @tp_iternext@ */
379 PYMETHODS(dhinfo
), /* @tp_methods@ */
380 0, /* @tp_members@ */
381 PYGETSET(dhinfo
), /* @tp_getset@ */
384 0, /* @tp_descr_get@ */
385 0, /* @tp_descr_set@ */
386 0, /* @tp_dictoffset@ */
388 PyType_GenericAlloc
, /* @tp_alloc@ */
389 fginfo_pynew
, /* @tp_new@ */
394 static const PyTypeObject bindhinfo_pytype_skel
= {
395 PyVarObject_HEAD_INIT(0, 0) /* Header */
396 "BinDHInfo", /* @tp_name@ */
397 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
398 0, /* @tp_itemsize@ */
400 fginfo_pydealloc
, /* @tp_dealloc@ */
402 0, /* @tp_getattr@ */
403 0, /* @tp_setattr@ */
404 0, /* @tp_compare@ */
406 0, /* @tp_as_number@ */
407 0, /* @tp_as_sequence@ */
408 0, /* @tp_as_mapping@ */
412 0, /* @tp_getattro@ */
413 0, /* @tp_setattro@ */
414 0, /* @tp_as_buffer@ */
415 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
419 "BinDHInfo(P, R, G): binary-field Diffie-Hellman group information.",
421 0, /* @tp_traverse@ */
423 0, /* @tp_richcompare@ */
424 0, /* @tp_weaklistoffset@ */
426 0, /* @tp_iternext@ */
427 PYMETHODS(bindhinfo
), /* @tp_methods@ */
428 0, /* @tp_members@ */
429 PYGETSET(bindhinfo
), /* @tp_getset@ */
432 0, /* @tp_descr_get@ */
433 0, /* @tp_descr_set@ */
434 0, /* @tp_dictoffset@ */
436 PyType_GenericAlloc
, /* @tp_alloc@ */
437 fginfo_pynew
, /* @tp_new@ */
442 /*----- General utilities -------------------------------------------------*/
444 PyTypeObject
*ge_pytype
, *group_pytype
;
445 static PyTypeObject
*primegroup_pytype
, *bingroup_pytype
, *ecgroup_pytype
;
447 group
*group_copy(group
*g
)
449 if (STRCMP(G_NAME(g
), ==, "prime")) {
450 gctx_prime
*gc
= (gctx_prime
*)g
;
452 gp
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
455 g
= group_prime(&gp
);
457 } else if (STRCMP(G_NAME(g
), ==, "bin")) {
458 gctx_bin
*gc
= (gctx_bin
*)g
;
460 gb
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
463 g
= group_binary(&gb
);
465 } else if (STRCMP(G_NAME(g
), ==, "ec")) {
466 gctx_ec
*gc
= (gctx_ec
*)g
;
468 if ((ei
.c
= eccurve_copy(gc
->ei
.c
)) == 0)
471 EC_COPY(&ei
.g
, &gc
->ei
.g
);
472 ei
.r
= MP_COPY(gc
->ei
.r
);
473 ei
.h
= MP_COPY(gc
->ei
.h
);
480 PyObject
*ge_pywrap(PyObject
*gobj
, ge
*x
)
482 ge_pyobj
*z
= PyObject_New(ge_pyobj
, (PyTypeObject
*)gobj
);
484 z
->g
= GROUP_G(gobj
);
486 return ((PyObject
*)z
);
489 static PyObject
*ge_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
491 static const char *const kwlist
[] = { "x", 0 };
500 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O:new", KWLIST
, &x
)) goto end
;
502 if (ECPT_PYCHECK(x
)) {
504 if (G_FROMEC(g
, xx
, &p
))
505 TYERR("can't convert from elliptic curve point");
507 } else if ((y
= tomp(x
)) != 0) {
508 if (G_FROMINT(g
, xx
, y
))
509 TYERR("can't convert from integer");
511 } else if (PyString_Check(x
)) {
512 sc
.buf
= PyString_AS_STRING(x
);
513 sc
.lim
= sc
.buf
+ PyString_GET_SIZE(x
);
514 if (G_READ(g
, xx
, &mptext_stringops
, &sc
) || sc
.buf
< sc
.lim
)
515 VALERR("malformed group element string");
517 TYERR("can't convert to group element");
518 return (ge_pywrap((PyObject
*)ty
, xx
));
522 if (xx
) G_DESTROY(g
, xx
);
526 static PyObject
*group_dopywrap(PyTypeObject
*ty
, group
*g
)
528 group_pyobj
*gobj
= newtype(ty
, 0, g
->ops
->name
);
530 gobj
->ty
.ht_type
.tp_basicsize
= sizeof(ge_pyobj
);
531 gobj
->ty
.ht_type
.tp_base
= ge_pytype
;
532 Py_INCREF(group_pytype
);
533 gobj
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
534 Py_TPFLAGS_BASETYPE
|
535 Py_TPFLAGS_CHECKTYPES
|
536 Py_TPFLAGS_HEAPTYPE
);
537 gobj
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
538 gobj
->ty
.ht_type
.tp_free
= 0;
539 gobj
->ty
.ht_type
.tp_new
= ge_pynew
;
540 typeready(&gobj
->ty
.ht_type
);
541 return ((PyObject
*)gobj
);
544 PyObject
*group_pywrap(group
*g
)
548 if (STRCMP(G_NAME(g
), ==, "prime")) ty
= primegroup_pytype
;
549 else if (STRCMP(G_NAME(g
), ==, "bin")) ty
= bingroup_pytype
;
550 else if (STRCMP(G_NAME(g
), ==, "ec")) ty
= ecgroup_pytype
;
552 return (group_dopywrap(ty
, g
));
555 /*----- Group elements ----------------------------------------------------*/
557 #define BINOP(name) \
558 static PyObject *ge_py##name(PyObject *x, PyObject *y) \
562 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \
563 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \
567 g->ops->name(g, z, GE_X(x), GE_X(y)); \
568 return (ge_pywrap(GE_GOBJ(x), z)); \
575 static PyObject *gemeth_##name(PyObject *me) \
581 g->ops->name(g, z, GE_X(me)); \
582 return (ge_pywrap(GE_GOBJ(me), z)); \
588 static PyObject
*ge_pyexp(PyObject
*x
, PyObject
*n
, PyObject
*m
)
593 if (m
!= Py_None
|| !GE_PYCHECK(x
) || (nn
= getmp(n
)) == 0)
595 z
= G_CREATE(GE_G(x
));
596 G_EXP(GE_G(x
), z
, GE_X(x
), nn
);
598 return (ge_pywrap(GE_GOBJ(x
), z
));
601 static void ge_pydealloc(PyObject
*me
)
603 G_DESTROY(GE_G(me
), GE_X(me
));
604 Py_DECREF(GE_GOBJ(me
));
608 static void group_pydealloc(PyObject
*me
)
610 G_DESTROYGROUP(GROUP_G(me
));
611 PyType_Type
.tp_dealloc(me
);
614 static PyObject
*gmexp_id(PyObject
*me
)
616 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
617 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
620 static int gmexp_fill(void *pp
, PyObject
*me
, PyObject
*x
, PyObject
*m
)
622 group_expfactor
*f
= pp
;
624 if (!GE_PYCHECK(x
) || GE_G(x
) != GROUP_G(me
) || (f
->exp
= getmp(m
)) == 0)
630 static PyObject
*ge_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
635 if (!GE_PYCHECK(x
) || !GE_PYCHECK(y
) ||
636 (GE_G(x
) != GE_G(y
) && !group_samep(GE_G(x
), GE_G(y
))))
639 case Py_EQ
: b
= G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
640 case Py_NE
: b
= !G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
641 default: TYERR("group elements are unordered");
648 static PyObject
*gemeth_check(PyObject
*me
)
650 if (group_check(GE_G(me
), GE_X(me
))) VALERR("bad group element");
656 static int ge_pynonzerop(PyObject
*x
)
657 { return (!G_IDENTP(GE_G(x
), GE_X(x
))); }
659 static PyObject
*ge_pystr(PyObject
*me
)
664 group_writedstr(GE_G(me
), GE_X(me
), &d
);
665 rc
= PyString_FromStringAndSize(d
.buf
, d
.len
);
670 static PyObject
*ge_pylong(PyObject
*me
)
675 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
676 TYERR("can't convert to integer");
683 static PyObject
*ge_pyint(PyObject
*me
)
689 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
690 TYERR("can't convert to integer");
691 if (!mp_tolong_checked(x
, &l
, 0)) rc
= PyInt_FromLong(l
);
692 else rc
= mp_topylong(x
);
698 static PyObject
*gemeth_toint(PyObject
*me
)
702 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
703 TYERR("can't convert to integer");
704 return (mp_pywrap(x
));
709 static PyObject
*gemeth_toec(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
711 static const char *const kwlist
[] = { "curve", 0 };
712 PyTypeObject
*cty
= 0;
718 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:toec", KWLIST
,
720 g
= GROUP_G(GE_GOBJ(me
));
722 if (!PyType_Check(cty
) || !PyType_IsSubtype(cty
, ecpt_pytype
))
723 TYERR("want subtype of catacomb.ECPt");
724 Py_INCREF((PyObject
*)cty
);
725 } else if (STRCMP(G_NAME(g
), ==, "ec")) {
726 c
= eccurve_copy(((gctx_ec
*)g
)->ei
.c
);
727 cty
= (PyTypeObject
*)eccurve_pywrap(0, c
);
730 Py_INCREF((PyObject
*)cty
);
732 if (G_TOEC(GE_G(me
), &p
, GE_X(me
))) {
733 Py_DECREF((PyObject
*)cty
);
734 TYERR("can't convert to ec point");
736 rc
= ecpt_pywrapout(cty
, &p
);
737 Py_DECREF((PyObject
*)cty
);
742 static PyObject
*gemeth_tobuf(PyObject
*me
)
748 n
= GE_G(me
)->noctets
+ 4;
749 rc
= bytestring_pywrap(0, n
);
750 buf_init(&b
, PyString_AS_STRING(rc
), n
);
751 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
753 _PyString_Resize(&rc
, BLEN(&b
));
757 static PyObject
*gemeth_toraw(PyObject
*me
)
763 n
= GE_G(me
)->noctets
;
764 rc
= bytestring_pywrap(0, n
);
765 buf_init(&b
, PyString_AS_STRING(rc
), n
);
766 G_TORAW(GE_G(me
), &b
, GE_X(me
));
768 _PyString_Resize(&rc
, BLEN(&b
));
772 static PyObject
*gmexp_exp(PyObject
*me
, void *pp
, int n
)
774 ge
*z
= G_CREATE(GROUP_G(me
));
775 G_MEXP(GROUP_G(me
), z
, pp
, n
);
776 return (ge_pywrap(me
, z
));
779 static void gmexp_drop(void *pp
)
781 group_expfactor
*f
= pp
;
785 static PyObject
*gmeth_mexp(PyObject
*me
, PyObject
*arg
)
787 return (mexp_common(me
, arg
, sizeof(group_expfactor
),
788 gmexp_id
, gmexp_fill
, gmexp_exp
, gmexp_drop
));
791 static PyObject
*gmeth_checkgroup(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
793 static const char *const kwlist
[] = { "rng", 0 };
794 grand
*r
= &rand_global
;
797 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:checkgroup", KWLIST
,
800 if ((p
= G_CHECK(GROUP_G(me
), r
)) != 0)
807 static PyObject
*group_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
809 int b
= group_samep(GROUP_G(x
), GROUP_G(y
));
813 default: TYERR("can't order groups");
820 static PyObject
*gemeth_frombuf(PyObject
*me
, PyObject
*arg
)
828 if (!PyArg_ParseTuple(arg
, "s#:frombuf", &p
, &n
)) return (0);
832 if (G_FROMBUF(g
, &b
, x
)) VALERR("invalid data");
833 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
835 if (x
) G_DESTROY(g
, x
);
839 static PyObject
*gemeth_fromraw(PyObject
*me
, PyObject
*arg
)
847 if (!PyArg_ParseTuple(arg
, "s#:fromraw", &p
, &n
)) return (0);
851 if (G_FROMRAW(g
, &b
, x
)) VALERR("invalid data");
852 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
854 if (x
) G_DESTROY(g
, x
);
858 static PyObject
*gemeth_fromstring(PyObject
*me
, PyObject
*arg
)
866 if (!PyArg_ParseTuple(arg
, "s#:fromstring", &p
, &n
)) return (0);
871 if (G_READ(g
, x
, &mptext_stringops
, &sc
))
872 VALERR("bad group element string");
873 return (Py_BuildValue("(Ns#)", ge_pywrap(me
, x
),
874 sc
.buf
, (Py_ssize_t
)(sc
.lim
- sc
.buf
)));
876 if (x
) G_DESTROY(g
, x
);
880 static PyObject
*gmeth_parse(PyObject
*me
, PyObject
*arg
)
886 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
888 if ((g
= group_parse(&qd
)) == 0) VALERR(qd
.e
);
889 return (group_pywrap(g
));
894 static PyObject
*geget_group(PyObject
*me
, void *hunoz
)
895 { RETURN_OBJ(GE_GOBJ(me
)); }
897 static PyObject
*gget_nbits(PyObject
*me
, void *hunoz
)
898 { return (PyInt_FromLong(GROUP_G(me
)->nbits
)); }
900 static PyObject
*gget_noctets(PyObject
*me
, void *hunoz
)
901 { return (PyInt_FromLong(GROUP_G(me
)->noctets
)); }
903 static PyObject
*gget_i(PyObject
*me
, void *hunoz
)
905 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
906 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
909 static PyObject
*gget_g(PyObject
*me
, void *hunoz
)
911 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
912 G_COPY(g
, x
, g
->g
); return (ge_pywrap(me
, x
));
915 static long ge_pyhash(PyObject
*me
)
918 size_t sz
= GE_G(me
)->noctets
+ 4;
919 uint32 h
= 0xf672c776 + GE_G(me
)->ops
->ty
;
920 octet
*p
= xmalloc(sz
);
922 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
924 h
= unihash_hash(&unihash_global
, h
, BBASE(&b
), BLEN(&b
));
926 return (h
% LONG_MAX
);
929 static PyObject
*gget_r(PyObject
*me
, void *hunoz
)
930 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->r
))); }
932 static PyObject
*gget_h(PyObject
*me
, void *hunoz
)
933 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->h
))); }
935 static const PyGetSetDef ge_pygetset
[] = {
936 #define GETSETNAME(op, name) ge##op##_##name
937 GET (group
, "X.group -> group containing X")
942 static const PyMethodDef ge_pymethods
[] = {
943 #define METHNAME(name) gemeth_##name
944 NAMETH(inv
, "X.inv() -> inverse element of X")
945 NAMETH(sqr
, "X.sqr() -> X^2 = X * X")
946 NAMETH(check
, "X.check() -> check X really belongs to its group")
947 NAMETH(toint
, "X.toint() -> X converted to an integer")
948 KWMETH(toec
, "X.toec([curve = ECPt]) -> "
949 "X converted to elliptic curve point")
950 NAMETH(tobuf
, "X.tobuf() -> X in buffer representation")
951 NAMETH(toraw
, "X.toraw() -> X in raw representation")
952 CMTH (frombuf
, "frombuf(BUF) -> X, REST")
953 CMTH (fromraw
, "fromraw(BUF) -> X, REST")
954 CMTH (fromstring
, "fromstring(STR) -> X, REST")
959 static const PyNumberMethods ge_pynumber
= {
961 0, /* @nb_subtract@ */
962 ge_pymul
, /* @nb_multiply@ */
963 ge_pydiv
, /* @nb_divide@ */
964 0, /* @nb_remainder@ */
966 ge_pyexp
, /* @nb_power@ */
967 0, /* @nb_negative@ */
968 0, /* @nb_positive@ */
969 0, /* @nb_absolute@ */
970 ge_pynonzerop
, /* @nb_nonzero@ */
978 ge_pyint
, /* @nb_int@ */
979 ge_pylong
, /* @nb_long@ */
980 0 /* meaningless */, /* @nb_float@ */
984 0, /* @nb_inplace_add@ */
985 0, /* @nb_inplace_subtract@ */
986 0, /* @nb_inplace_multiply@ */
987 0, /* @nb_inplace_divide@ */
988 0, /* @nb_inplace_remainder@ */
989 0, /* @nb_inplace_power@ */
990 0, /* @nb_inplace_lshift@ */
991 0, /* @nb_inplace_rshift@ */
992 0, /* @nb_inplace_and@ */
993 0, /* @nb_inplace_xor@ */
994 0, /* @nb_inplace_or@ */
996 0, /* @nb_floor_divide@ */
997 ge_pydiv
, /* @nb_true_divide@ */
998 0, /* @nb_inplace_floor_divide@ */
999 0, /* @nb_inplace_true_divide@ */
1002 static const PyTypeObject ge_pytype_skel
= {
1003 PyVarObject_HEAD_INIT(0, 0) /* Header */
1004 "GE", /* @tp_name@ */
1005 sizeof(ge_pyobj
), /* @tp_basicsize@ */
1006 0, /* @tp_itemsize@ */
1008 ge_pydealloc
, /* @tp_dealloc@ */
1010 0, /* @tp_getattr@ */
1011 0, /* @tp_setattr@ */
1012 0, /* @tp_compare@ */
1014 PYNUMBER(ge
), /* @tp_as_number@ */
1015 0, /* @tp_as_sequence@ */
1016 0, /* @tp_as_mapping@ */
1017 ge_pyhash
, /* @tp_hash@ */
1019 ge_pystr
, /* @tp_str@ */
1020 0, /* @tp_getattro@ */
1021 0, /* @tp_setattro@ */
1022 0, /* @tp_as_buffer@ */
1023 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1024 Py_TPFLAGS_CHECKTYPES
|
1025 Py_TPFLAGS_BASETYPE
,
1028 "Group elements, abstract base class.",
1030 0, /* @tp_traverse@ */
1032 ge_pyrichcompare
, /* @tp_richcompare@ */
1033 0, /* @tp_weaklistoffset@ */
1035 0, /* @tp_iternext@ */
1036 PYMETHODS(ge
), /* @tp_methods@ */
1037 0, /* @tp_members@ */
1038 PYGETSET(ge
), /* @tp_getset@ */
1041 0, /* @tp_descr_get@ */
1042 0, /* @tp_descr_set@ */
1043 0, /* @tp_dictoffset@ */
1045 PyType_GenericAlloc
, /* @tp_alloc@ */
1046 abstract_pynew
, /* @tp_new@ */
1051 static const PyGetSetDef group_pygetset
[] = {
1052 #define GETSETNAME(op, name) g##op##_##name
1053 GET (noctets
, "G.noctets -> size in octets of element")
1054 GET (nbits
, "G.nbits -> size in bits of element")
1055 GET (i
, "G.i -> group identity")
1056 GET (g
, "G.g -> group generator")
1057 GET (r
, "G.r -> group order")
1058 GET (h
, "G.h -> group cofactor")
1063 static const PyMethodDef group_pymethods
[] = {
1064 #define METHNAME(name) gmeth_##name
1065 METH (mexp
, "G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1066 KWMETH(checkgroup
, "G.checkgroup([rng = rand]): check group is good")
1067 SMTH (parse
, "parse(STR) -> G, REST")
1072 static const PyTypeObject group_pytype_skel
= {
1073 PyVarObject_HEAD_INIT(0, 0) /* Header */
1074 "Group", /* @tp_name@ */
1075 sizeof(group_pyobj
), /* @tp_basicsize@ */
1076 0, /* @tp_itemsize@ */
1078 group_pydealloc
, /* @tp_dealloc@ */
1080 0, /* @tp_getattr@ */
1081 0, /* @tp_setattr@ */
1082 0, /* @tp_compare@ */
1084 0, /* @tp_as_number@ */
1085 0, /* @tp_as_sequence@ */
1086 0, /* @tp_as_mapping@ */
1090 0, /* @tp_getattro@ */
1091 0, /* @tp_setattro@ */
1092 0, /* @tp_as_buffer@ */
1093 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1094 Py_TPFLAGS_BASETYPE
,
1097 "Abstract base class for groups.",
1099 0, /* @tp_traverse@ */
1101 group_pyrichcompare
, /* @tp_richcompare@ */
1102 0, /* @tp_weaklistoffset@ */
1104 0, /* @tp_iternext@ */
1105 PYMETHODS(group
), /* @tp_methods@ */
1106 0, /* @tp_members@ */
1107 PYGETSET(group
), /* @tp_getset@ */
1110 0, /* @tp_descr_get@ */
1111 0, /* @tp_descr_set@ */
1112 0, /* @tp_dictoffset@ */
1114 PyType_GenericAlloc
, /* @tp_alloc@ */
1115 abstract_pynew
, /* @tp_new@ */
1120 static PyObject
*pgget_info(PyObject
*me
, void *hunoz
)
1123 gctx_prime
*gg
= (gctx_prime
*)GROUP_G(me
);
1124 dp
.p
= MP_COPY(gg
->mm
.m
);
1125 dp
.q
= MP_COPY(gg
->g
.r
);
1126 dp
.g
= mpmont_reduce(&gg
->mm
, MP_NEW
, gg
->gen
.x
);
1127 return (fginfo_pywrap(&dp
, dhinfo_pytype
));
1130 static const PyGetSetDef primegroup_pygetset
[] = {
1131 #define GETSETNAME(op, name) pg##op##_##name
1132 GET (info
, "G.info -> information about the group")
1137 static PyObject
*primegroup_pynew(PyTypeObject
*ty
,
1138 PyObject
*arg
, PyObject
*kw
)
1141 static const char *const kwlist
[] = { "info", 0 };
1143 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1146 return (group_dopywrap(ty
, group_prime(FGINFO_DP(i
))));
1149 static const PyTypeObject primegroup_pytype_skel
= {
1150 PyVarObject_HEAD_INIT(0, 0) /* Header */
1151 "PrimeGroup", /* @tp_name@ */
1152 sizeof(group_pyobj
), /* @tp_basicsize@ */
1153 0, /* @tp_itemsize@ */
1155 group_pydealloc
, /* @tp_dealloc@ */
1157 0, /* @tp_getattr@ */
1158 0, /* @tp_setattr@ */
1159 0, /* @tp_compare@ */
1161 0, /* @tp_as_number@ */
1162 0, /* @tp_as_sequence@ */
1163 0, /* @tp_as_mapping@ */
1167 0, /* @tp_getattro@ */
1168 0, /* @tp_setattro@ */
1169 0, /* @tp_as_buffer@ */
1170 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1171 Py_TPFLAGS_BASETYPE
,
1174 "PrimeGroup(INFO): subgroups of prime fields.",
1176 0, /* @tp_traverse@ */
1178 0, /* @tp_richcompare@ */
1179 0, /* @tp_weaklistoffset@ */
1181 0, /* @tp_iternext@ */
1182 0, /* @tp_methods@ */
1183 0, /* @tp_members@ */
1184 PYGETSET(primegroup
), /* @tp_getset@ */
1187 0, /* @tp_descr_get@ */
1188 0, /* @tp_descr_set@ */
1189 0, /* @tp_dictoffset@ */
1191 PyType_GenericAlloc
, /* @tp_alloc@ */
1192 primegroup_pynew
, /* @tp_new@ */
1197 static PyObject
*bgget_info(PyObject
*me
, void *hunoz
)
1200 gctx_bin
*gg
= (gctx_bin
*)GROUP_G(me
);
1201 dp
.p
= MP_COPY(gg
->r
.p
);
1202 dp
.q
= MP_COPY(gg
->g
.r
);
1203 dp
.g
= MP_COPY(gg
->gen
.x
);
1204 return (fginfo_pywrap(&dp
, bindhinfo_pytype
));
1207 static const PyGetSetDef bingroup_pygetset
[] = {
1208 #define GETSETNAME(op, name) bg##op##_##name
1209 GET (info
, "G.info -> information about the group")
1214 static PyObject
*bingroup_pynew(PyTypeObject
*ty
,
1215 PyObject
*arg
, PyObject
*kw
)
1218 static const char *const kwlist
[] = { "info", 0 };
1220 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1221 bindhinfo_pytype
, &i
))
1223 return (group_dopywrap(ty
, group_binary(FGINFO_DP(i
))));
1226 static const PyTypeObject bingroup_pytype_skel
= {
1227 PyVarObject_HEAD_INIT(0, 0) /* Header */
1228 "BinGroup", /* @tp_name@ */
1229 sizeof(group_pyobj
), /* @tp_basicsize@ */
1230 0, /* @tp_itemsize@ */
1232 group_pydealloc
, /* @tp_dealloc@ */
1234 0, /* @tp_getattr@ */
1235 0, /* @tp_setattr@ */
1236 0, /* @tp_compare@ */
1238 0, /* @tp_as_number@ */
1239 0, /* @tp_as_sequence@ */
1240 0, /* @tp_as_mapping@ */
1244 0, /* @tp_getattro@ */
1245 0, /* @tp_setattro@ */
1246 0, /* @tp_as_buffer@ */
1247 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1248 Py_TPFLAGS_BASETYPE
,
1251 "BinGroup(INFO): subgroups of binary fields.",
1253 0, /* @tp_traverse@ */
1255 0, /* @tp_richcompare@ */
1256 0, /* @tp_weaklistoffset@ */
1258 0, /* @tp_iternext@ */
1259 0, /* @tp_methods@ */
1260 0, /* @tp_members@ */
1261 PYGETSET(bingroup
), /* @tp_getset@ */
1264 0, /* @tp_descr_get@ */
1265 0, /* @tp_descr_set@ */
1266 0, /* @tp_dictoffset@ */
1268 PyType_GenericAlloc
, /* @tp_alloc@ */
1269 bingroup_pynew
, /* @tp_new@ */
1274 static PyObject
*egget_info(PyObject
*me
, void *hunoz
)
1277 gctx_ec
*gg
= (gctx_ec
*)GROUP_G(me
);
1279 ecinfo_copy(&ei
, &gg
->ei
);
1280 return (ecinfo_pywrap(&ei
));
1283 static const PyGetSetDef ecgroup_pygetset
[] = {
1284 #define GETSETNAME(op, name) eg##op##_##name
1285 GET (info
, "G.info -> information about the group")
1290 static PyObject
*ecgroup_pynew(PyTypeObject
*ty
,
1291 PyObject
*arg
, PyObject
*kw
)
1295 static const char *const kwlist
[] = { "info", 0 };
1297 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1300 ecinfo_copy(&ei
, ECINFO_EI(i
));
1301 return (group_dopywrap(ty
, group_ec(&ei
)));
1304 static const PyTypeObject ecgroup_pytype_skel
= {
1305 PyVarObject_HEAD_INIT(0, 0) /* Header */
1306 "ECGroup", /* @tp_name@ */
1307 sizeof(group_pyobj
), /* @tp_basicsize@ */
1308 0, /* @tp_itemsize@ */
1310 group_pydealloc
, /* @tp_dealloc@ */
1312 0, /* @tp_getattr@ */
1313 0, /* @tp_setattr@ */
1314 0, /* @tp_compare@ */
1316 0, /* @tp_as_number@ */
1317 0, /* @tp_as_sequence@ */
1318 0, /* @tp_as_mapping@ */
1322 0, /* @tp_getattro@ */
1323 0, /* @tp_setattro@ */
1324 0, /* @tp_as_buffer@ */
1325 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1326 Py_TPFLAGS_BASETYPE
,
1329 "ECGroup(INFO): elliptic curve groups.",
1331 0, /* @tp_traverse@ */
1333 0, /* @tp_richcompare@ */
1334 0, /* @tp_weaklistoffset@ */
1336 0, /* @tp_iternext@ */
1337 0, /* @tp_methods@ */
1338 0, /* @tp_members@ */
1339 PYGETSET(ecgroup
), /* @tp_getset@ */
1342 0, /* @tp_descr_get@ */
1343 0, /* @tp_descr_set@ */
1344 0, /* @tp_dictoffset@ */
1346 PyType_GenericAlloc
, /* @tp_alloc@ */
1347 ecgroup_pynew
, /* @tp_new@ */
1352 /*----- Global stuff ------------------------------------------------------*/
1354 void group_pyinit(void)
1356 INITTYPE(fginfo
, root
);
1357 INITTYPE(dhinfo
, fginfo
);
1358 INITTYPE(bindhinfo
, dhinfo
);
1360 INITTYPE(group
, type
);
1361 INITTYPE(primegroup
, group
);
1362 INITTYPE(bingroup
, group
);
1363 INITTYPE(ecgroup
, group
);
1366 static const char *grp_namefn(const void *p
)
1367 { const pentry
*pt
= p
; return (pt
->name
); }
1369 static int grp_ixfn(const pentry
*tab
, const pentry
*pt
)
1373 for (i
= 0; tab
[i
].name
; i
++)
1374 if (tab
[i
].data
== pt
->data
) return (i
);
1377 static int pgrp_ixfn(const void *p
) { return (grp_ixfn(ptab
, p
)); }
1378 static int bgrp_ixfn(const void *p
) { return (grp_ixfn(bintab
, p
)); }
1380 static PyObject
*grp_valfn(const pentry
*tab
, PyTypeObject
*ty
, int i
)
1384 dh_infofromdata(&gp
, tab
[i
].data
);
1385 return (fginfo_pywrap(&gp
, ty
));
1387 static PyObject
*pgrp_valfn(int i
)
1388 { return (grp_valfn(ptab
, dhinfo_pytype
, i
)); }
1389 static PyObject
*bgrp_valfn(int i
)
1390 { return (grp_valfn(bintab
, bindhinfo_pytype
, i
)); }
1392 void group_pyinsert(PyObject
*mod
)
1394 INSERT("FGInfo", fginfo_pytype
);
1395 INSERT("DHInfo", dhinfo_pytype
);
1396 INSERT("BinDHInfo", bindhinfo_pytype
);
1397 INSERT("GE", ge_pytype
);
1398 INSERT("Group", group_pytype
);
1399 INSERT("PrimeGroup", primegroup_pytype
);
1400 INSERT("BinGroup", bingroup_pytype
);
1401 INSERT("ECGroup", ecgroup_pytype
);
1402 INSERT("primegroups", make_grouptab(ptab
, sizeof(*ptab
),
1403 grp_namefn
, pgrp_ixfn
, pgrp_valfn
));
1404 INSERT("bingroups", make_grouptab(bintab
, sizeof(*bintab
),
1405 grp_namefn
, bgrp_ixfn
, bgrp_valfn
));
1408 /*----- That's all, folks -------------------------------------------------*/