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
)
200 struct excinfo exc
= EXCINFO_INIT
;
201 pypgev evt
= { { 0 } };
202 static const char *const kwlist
[] =
203 { "pbits", "qbits", "seed", "event", "nsteps", 0 };
207 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&O&O&|O&O&:gendsa", KWLIST
,
208 convuint
, &pl
, convuint
, &ql
,
210 convpgev
, &evt
, convuint
, &steps
))
212 if (dsa_gen(&dp
, ql
, pl
, steps
, k
.p
, k
.sz
, &ds
, evt
.ev
.proc
, evt
.ev
.ctx
))
214 rc
= Py_BuildValue("(NNl)", fginfo_pywrap(&dp
, dhinfo_pytype
),
215 bytestring_pywrap(ds
.p
, ds
.sz
), (long)ds
.count
);
222 static PyObject
*meth__parse(PyObject
*me
, PyObject
*arg
, PyTypeObject
*ty
,
223 int (*parse
)(qd_parse
*, gprime_param
*))
230 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
232 if (parse(&qd
, &gp
)) VALERR(qd
.e
);
233 rc
= fginfo_pywrap(&gp
, ty
);
238 static PyObject
*dimeth_parse(PyObject
*me
, PyObject
*arg
)
239 { return (meth__parse(me
, arg
, dhinfo_pytype
, dh_parse
)); }
241 static PyObject
*bimeth_parse(PyObject
*me
, PyObject
*arg
)
242 { return (meth__parse(me
, arg
, bindhinfo_pytype
, dhbin_parse
)); }
244 static const PyGetSetDef fginfo_pygetset
[] = {
245 #define GETSETNAME(op, name) fi##op##_##name
246 GET (r
, "I.r -> group order")
251 static const PyGetSetDef dhinfo_pygetset
[] = {
252 #define GETSETNAME(op, name) di##op##_##name
253 GET (p
, "I.p -> prime")
254 GET (g
, "I.g -> generator")
259 static const PyMethodDef dhinfo_pymethods
[] = {
260 #define METHNAME(name) dimeth_##name
261 SMTH (parse
, "parse(STR) -> D, REST")
263 "generate(PBITS, [qbits = 0], [event = pgen_nullev],\n"
264 " [rng = rand], [nsteps = 0]) -> D")
266 "genlimlee(PBITS, QBITS, [event = pgen_nullev], "
267 "[ievent = pgen_nullev],\n"
268 " [rng = rand], [nsteps = 0], [subgroupp = True]) "
271 "gendsa(PBITS, QBITS, SEED, [event = pgen_nullev], [nsteps = 0])\n"
272 " -> (D, SEED, COUNT)")
274 "gendsa(PBITS, QBITS, [event = pgen_nullev], "
275 "[rng = rand], [nsteps = 0])\n"
281 static const PyGetSetDef bindhinfo_pygetset
[] = {
282 #define GETSETNAME(op, name) bi##op##_##name
283 GET (p
, "I.p -> irreducible polynomial")
284 GET (m
, "I.m -> degree of polynomial")
285 GET (g
, "I.g -> generator")
290 static const PyMethodDef bindhinfo_pymethods
[] = {
291 #define METHNAME(name) bimeth_##name
292 SMTH (parse
, "parse(STR) -> D, REST")
297 static const PyTypeObject fginfo_pytype_skel
= {
298 PyVarObject_HEAD_INIT(0, 0) /* Header */
299 "FGInfo", /* @tp_name@ */
300 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
301 0, /* @tp_itemsize@ */
303 fginfo_pydealloc
, /* @tp_dealloc@ */
305 0, /* @tp_getattr@ */
306 0, /* @tp_setattr@ */
307 0, /* @tp_compare@ */
309 0, /* @tp_as_number@ */
310 0, /* @tp_as_sequence@ */
311 0, /* @tp_as_mapping@ */
315 0, /* @tp_getattro@ */
316 0, /* @tp_setattro@ */
317 0, /* @tp_as_buffer@ */
318 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
322 "Abstract base class for field-group information objects.",
324 0, /* @tp_traverse@ */
326 0, /* @tp_richcompare@ */
327 0, /* @tp_weaklistoffset@ */
329 0, /* @tp_iternext@ */
330 0, /* @tp_methods@ */
331 0, /* @tp_members@ */
332 PYGETSET(fginfo
), /* @tp_getset@ */
335 0, /* @tp_descr_get@ */
336 0, /* @tp_descr_set@ */
337 0, /* @tp_dictoffset@ */
339 PyType_GenericAlloc
, /* @tp_alloc@ */
340 abstract_pynew
, /* @tp_new@ */
345 static const PyTypeObject dhinfo_pytype_skel
= {
346 PyVarObject_HEAD_INIT(0, 0) /* Header */
347 "DHInfo", /* @tp_name@ */
348 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
349 0, /* @tp_itemsize@ */
351 fginfo_pydealloc
, /* @tp_dealloc@ */
353 0, /* @tp_getattr@ */
354 0, /* @tp_setattr@ */
355 0, /* @tp_compare@ */
357 0, /* @tp_as_number@ */
358 0, /* @tp_as_sequence@ */
359 0, /* @tp_as_mapping@ */
363 0, /* @tp_getattro@ */
364 0, /* @tp_setattro@ */
365 0, /* @tp_as_buffer@ */
366 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
370 "DHInfo(P, R, G): standard (integer) Diffie-Hellman group information.",
372 0, /* @tp_traverse@ */
374 0, /* @tp_richcompare@ */
375 0, /* @tp_weaklistoffset@ */
377 0, /* @tp_iternext@ */
378 PYMETHODS(dhinfo
), /* @tp_methods@ */
379 0, /* @tp_members@ */
380 PYGETSET(dhinfo
), /* @tp_getset@ */
383 0, /* @tp_descr_get@ */
384 0, /* @tp_descr_set@ */
385 0, /* @tp_dictoffset@ */
387 PyType_GenericAlloc
, /* @tp_alloc@ */
388 fginfo_pynew
, /* @tp_new@ */
393 static const PyTypeObject bindhinfo_pytype_skel
= {
394 PyVarObject_HEAD_INIT(0, 0) /* Header */
395 "BinDHInfo", /* @tp_name@ */
396 sizeof(fginfo_pyobj
), /* @tp_basicsize@ */
397 0, /* @tp_itemsize@ */
399 fginfo_pydealloc
, /* @tp_dealloc@ */
401 0, /* @tp_getattr@ */
402 0, /* @tp_setattr@ */
403 0, /* @tp_compare@ */
405 0, /* @tp_as_number@ */
406 0, /* @tp_as_sequence@ */
407 0, /* @tp_as_mapping@ */
411 0, /* @tp_getattro@ */
412 0, /* @tp_setattro@ */
413 0, /* @tp_as_buffer@ */
414 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
418 "BinDHInfo(P, R, G): binary-field Diffie-Hellman group information.",
420 0, /* @tp_traverse@ */
422 0, /* @tp_richcompare@ */
423 0, /* @tp_weaklistoffset@ */
425 0, /* @tp_iternext@ */
426 PYMETHODS(bindhinfo
), /* @tp_methods@ */
427 0, /* @tp_members@ */
428 PYGETSET(bindhinfo
), /* @tp_getset@ */
431 0, /* @tp_descr_get@ */
432 0, /* @tp_descr_set@ */
433 0, /* @tp_dictoffset@ */
435 PyType_GenericAlloc
, /* @tp_alloc@ */
436 fginfo_pynew
, /* @tp_new@ */
441 /*----- General utilities -------------------------------------------------*/
443 PyTypeObject
*ge_pytype
, *group_pytype
;
444 static PyTypeObject
*primegroup_pytype
, *bingroup_pytype
, *ecgroup_pytype
;
446 group
*group_copy(group
*g
)
448 if (STRCMP(G_NAME(g
), ==, "prime")) {
449 gctx_prime
*gc
= (gctx_prime
*)g
;
451 gp
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
454 g
= group_prime(&gp
);
456 } else if (STRCMP(G_NAME(g
), ==, "bin")) {
457 gctx_bin
*gc
= (gctx_bin
*)g
;
459 gb
.g
= G_TOINT(g
, MP_NEW
, g
->g
);
462 g
= group_binary(&gb
);
464 } else if (STRCMP(G_NAME(g
), ==, "ec")) {
465 gctx_ec
*gc
= (gctx_ec
*)g
;
467 if ((ei
.c
= eccurve_copy(gc
->ei
.c
)) == 0)
470 EC_COPY(&ei
.g
, &gc
->ei
.g
);
471 ei
.r
= MP_COPY(gc
->ei
.r
);
472 ei
.h
= MP_COPY(gc
->ei
.h
);
479 PyObject
*ge_pywrap(PyObject
*gobj
, ge
*x
)
481 ge_pyobj
*z
= PyObject_New(ge_pyobj
, (PyTypeObject
*)gobj
);
483 z
->g
= GROUP_G(gobj
);
485 return ((PyObject
*)z
);
488 static PyObject
*ge_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
490 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 (TEXT_CHECK(x
)) {
512 TEXT_PTRLEN(x
, sc
.buf
, n
); sc
.lim
= sc
.buf
+ n
;
513 if (G_READ(g
, xx
, &mptext_stringops
, &sc
) || sc
.buf
< sc
.lim
)
514 VALERR("malformed group element string");
516 TYERR("can't convert to group element");
517 return (ge_pywrap((PyObject
*)ty
, xx
));
521 if (xx
) G_DESTROY(g
, xx
);
525 static PyObject
*group_dopywrap(PyTypeObject
*ty
, group
*g
)
527 group_pyobj
*gobj
= newtype(ty
, 0, g
->ops
->name
);
529 gobj
->ty
.ht_type
.tp_basicsize
= sizeof(ge_pyobj
);
530 gobj
->ty
.ht_type
.tp_base
= ge_pytype
;
531 Py_INCREF(group_pytype
);
532 gobj
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
533 Py_TPFLAGS_BASETYPE
|
534 Py_TPFLAGS_CHECKTYPES
|
535 Py_TPFLAGS_HEAPTYPE
);
536 gobj
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
537 gobj
->ty
.ht_type
.tp_free
= 0;
538 gobj
->ty
.ht_type
.tp_new
= ge_pynew
;
539 typeready(&gobj
->ty
.ht_type
);
540 return ((PyObject
*)gobj
);
543 PyObject
*group_pywrap(group
*g
)
547 if (STRCMP(G_NAME(g
), ==, "prime")) ty
= primegroup_pytype
;
548 else if (STRCMP(G_NAME(g
), ==, "bin")) ty
= bingroup_pytype
;
549 else if (STRCMP(G_NAME(g
), ==, "ec")) ty
= ecgroup_pytype
;
551 return (group_dopywrap(ty
, g
));
554 /*----- Group elements ----------------------------------------------------*/
556 #define BINOP(name) \
557 static PyObject *ge_py##name(PyObject *x, PyObject *y) \
561 if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \
562 (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \
566 g->ops->name(g, z, GE_X(x), GE_X(y)); \
567 return (ge_pywrap(GE_GOBJ(x), z)); \
574 static PyObject *gemeth_##name(PyObject *me) \
580 g->ops->name(g, z, GE_X(me)); \
581 return (ge_pywrap(GE_GOBJ(me), z)); \
587 static PyObject
*ge_pyexp(PyObject
*x
, PyObject
*n
, PyObject
*m
)
592 if (m
!= Py_None
|| !GE_PYCHECK(x
)) RETURN_NOTIMPL
;
593 if (FE_PYCHECK(n
) && FE_F(n
)->ops
->ty
== FTY_PRIME
)
594 nn
= F_OUT(FE_F(n
), MP_NEW
, FE_X(n
));
595 else if ((nn
= implicitmp(n
)) == 0)
597 z
= G_CREATE(GE_G(x
));
598 G_EXP(GE_G(x
), z
, GE_X(x
), nn
);
600 return (ge_pywrap(GE_GOBJ(x
), z
));
603 static void ge_pydealloc(PyObject
*me
)
605 G_DESTROY(GE_G(me
), GE_X(me
));
606 Py_DECREF(GE_GOBJ(me
));
610 static void group_pydealloc(PyObject
*me
)
612 G_DESTROYGROUP(GROUP_G(me
));
613 PyType_Type
.tp_dealloc(me
);
616 static PyObject
*gmexp_id(PyObject
*me
)
618 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
619 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
622 static int gmexp_fill(void *pp
, PyObject
*me
, PyObject
*x
, PyObject
*m
)
624 group_expfactor
*f
= pp
;
626 if (!GE_PYCHECK(x
) || GE_G(x
) != GROUP_G(me
) || (f
->exp
= getmp(m
)) == 0)
632 static PyObject
*ge_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
637 if (!GE_PYCHECK(x
) || !GE_PYCHECK(y
) ||
638 (GE_G(x
) != GE_G(y
) && !group_samep(GE_G(x
), GE_G(y
))))
641 case Py_EQ
: b
= G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
642 case Py_NE
: b
= !G_EQ(GE_G(x
), GE_X(x
), GE_X(y
)); break;
643 default: TYERR("group elements are unordered");
650 static PyObject
*gemeth_check(PyObject
*me
)
652 if (group_check(GE_G(me
), GE_X(me
))) VALERR("bad group element");
658 static int ge_pynonzerop(PyObject
*x
)
659 { return (!G_IDENTP(GE_G(x
), GE_X(x
))); }
661 static PyObject
*ge_pystr(PyObject
*me
)
666 group_writedstr(GE_G(me
), GE_X(me
), &d
);
667 rc
= TEXT_FROMSTRLEN(d
.buf
, d
.len
);
673 static PyObject
*ge_pylong(PyObject
*me
)
678 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
679 TYERR("can't convert to integer");
687 static PyObject
*ge_pyint(PyObject
*me
)
693 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
694 TYERR("can't convert to integer");
695 if (!mp_tolong_checked(x
, &l
, 0)) rc
= PyInt_FromLong(l
);
696 else rc
= mp_topylong(x
);
702 static PyObject
*gemeth_toint(PyObject
*me
)
706 if ((x
= G_TOINT(GE_G(me
), MP_NEW
, GE_X(me
))) == 0)
707 TYERR("can't convert to integer");
708 return (mp_pywrap(x
));
713 static PyObject
*gemeth_toec(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
715 static const char *const kwlist
[] = { "curve", 0 };
716 PyTypeObject
*cty
= 0;
722 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O:toec", KWLIST
,
724 g
= GROUP_G(GE_GOBJ(me
));
726 if (!PyType_Check(cty
) || !PyType_IsSubtype(cty
, ecpt_pytype
))
727 TYERR("want subtype of catacomb.ECPt");
728 Py_INCREF((PyObject
*)cty
);
729 } else if (STRCMP(G_NAME(g
), ==, "ec")) {
730 c
= eccurve_copy(((gctx_ec
*)g
)->ei
.c
);
731 cty
= (PyTypeObject
*)eccurve_pywrap(0, c
);
734 Py_INCREF((PyObject
*)cty
);
736 if (G_TOEC(GE_G(me
), &p
, GE_X(me
))) {
737 Py_DECREF((PyObject
*)cty
);
738 TYERR("can't convert to ec point");
740 rc
= ecpt_pywrapout(cty
, &p
);
741 Py_DECREF((PyObject
*)cty
);
746 static PyObject
*gemeth_tobuf(PyObject
*me
)
752 n
= GE_G(me
)->noctets
+ 4;
753 rc
= bytestring_pywrap(0, n
);
754 buf_init(&b
, BIN_PTR(rc
), n
);
755 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
757 BIN_SETLEN(rc
, BLEN(&b
));
761 static PyObject
*gemeth_toraw(PyObject
*me
)
767 n
= GE_G(me
)->noctets
;
768 rc
= bytestring_pywrap(0, n
);
769 buf_init(&b
, BIN_PTR(rc
), n
);
770 G_TORAW(GE_G(me
), &b
, GE_X(me
));
772 BIN_SETLEN(rc
, BLEN(&b
));
776 static PyObject
*gmexp_exp(PyObject
*me
, void *pp
, size_t n
)
778 ge
*z
= G_CREATE(GROUP_G(me
));
779 G_MEXP(GROUP_G(me
), z
, pp
, n
);
780 return (ge_pywrap(me
, z
));
783 static void gmexp_drop(void *pp
)
785 group_expfactor
*f
= pp
;
789 static PyObject
*gmeth_mexp(PyObject
*me
, PyObject
*arg
)
791 return (mexp_common(me
, arg
, sizeof(group_expfactor
),
792 gmexp_id
, gmexp_fill
, gmexp_exp
, gmexp_drop
));
795 static PyObject
*gmeth_checkgroup(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
797 static const char *const kwlist
[] = { "rng", 0 };
798 grand
*r
= &rand_global
;
801 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:checkgroup", KWLIST
,
804 if ((p
= G_CHECK(GROUP_G(me
), r
)) != 0)
811 static PyObject
*group_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
813 int b
= group_samep(GROUP_G(x
), GROUP_G(y
));
817 default: TYERR("can't order groups");
824 static PyObject
*gemeth_frombuf(PyObject
*me
, PyObject
*arg
)
831 if (!PyArg_ParseTuple(arg
, "O&:frombuf", convbin
, &in
)) return (0);
833 buf_init(&b
, (/*unconst*/ void *)in
.p
, in
.sz
);
835 if (G_FROMBUF(g
, &b
, x
)) VALERR("invalid data");
836 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
838 if (x
) G_DESTROY(g
, x
);
842 static PyObject
*gemeth_fromraw(PyObject
*me
, PyObject
*arg
)
849 if (!PyArg_ParseTuple(arg
, "O&:fromraw", convbin
, &in
)) return (0);
851 buf_init(&b
, (/*unconst*/ void *)in
.p
, in
.sz
);
853 if (G_FROMRAW(g
, &b
, x
)) VALERR("invalid data");
854 return (Py_BuildValue("(NN)", ge_pywrap(me
, x
), bytestring_pywrapbuf(&b
)));
856 if (x
) G_DESTROY(g
, x
);
860 static PyObject
*gemeth_fromstring(PyObject
*me
, PyObject
*arg
)
868 if (!PyArg_ParseTuple(arg
, "s#:fromstring", &p
, &n
)) return (0);
873 if (G_READ(g
, x
, &mptext_stringops
, &sc
))
874 VALERR("bad group element string");
875 return (Py_BuildValue("(Ns#)", ge_pywrap(me
, x
),
876 sc
.buf
, (Py_ssize_t
)(sc
.lim
- sc
.buf
)));
878 if (x
) G_DESTROY(g
, x
);
882 static PyObject
*gmeth_parse(PyObject
*me
, PyObject
*arg
)
888 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
890 if ((g
= group_parse(&qd
)) == 0) VALERR(qd
.e
);
891 return (group_pywrap(g
));
896 static PyObject
*geget_group(PyObject
*me
, void *hunoz
)
897 { RETURN_OBJ(GE_GOBJ(me
)); }
899 static PyObject
*gget_nbits(PyObject
*me
, void *hunoz
)
900 { return (PyInt_FromLong(GROUP_G(me
)->nbits
)); }
902 static PyObject
*gget_noctets(PyObject
*me
, void *hunoz
)
903 { return (PyInt_FromLong(GROUP_G(me
)->noctets
)); }
905 static PyObject
*gget_i(PyObject
*me
, void *hunoz
)
907 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
908 G_COPY(g
, x
, g
->i
); return (ge_pywrap(me
, x
));
911 static PyObject
*gget_g(PyObject
*me
, void *hunoz
)
913 group
*g
= GROUP_G(me
); ge
*x
= G_CREATE(g
);
914 G_COPY(g
, x
, g
->g
); return (ge_pywrap(me
, x
));
917 static Py_hash_t
ge_pyhash(PyObject
*me
)
920 size_t sz
= GE_G(me
)->noctets
+ 4;
921 uint32 h
= 0xf672c776 + GE_G(me
)->ops
->ty
;
922 octet
*p
= xmalloc(sz
);
924 G_TOBUF(GE_G(me
), &b
, GE_X(me
));
926 h
= unihash_hash(&unihash_global
, h
, BBASE(&b
), BLEN(&b
));
928 return (h
% LONG_MAX
);
931 static PyObject
*gget_r(PyObject
*me
, void *hunoz
)
932 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->r
))); }
934 static PyObject
*gget_h(PyObject
*me
, void *hunoz
)
935 { return (mp_pywrap(MP_COPY(GROUP_G(me
)->h
))); }
937 static const PyGetSetDef ge_pygetset
[] = {
938 #define GETSETNAME(op, name) ge##op##_##name
939 GET (group
, "X.group -> group containing X")
944 static const PyMethodDef ge_pymethods
[] = {
945 #define METHNAME(name) gemeth_##name
946 NAMETH(inv
, "X.inv() -> inverse element of X")
947 NAMETH(sqr
, "X.sqr() -> X^2 = X * X")
948 NAMETH(check
, "X.check() -> check X really belongs to its group")
949 NAMETH(toint
, "X.toint() -> X converted to an integer")
950 KWMETH(toec
, "X.toec([curve = ECPt]) -> "
951 "X converted to elliptic curve point")
952 NAMETH(tobuf
, "X.tobuf() -> X in buffer representation")
953 NAMETH(toraw
, "X.toraw() -> X in raw representation")
954 CMTH (frombuf
, "frombuf(BUF) -> X, REST")
955 CMTH (fromraw
, "fromraw(BUF) -> X, REST")
956 CMTH (fromstring
, "fromstring(STR) -> X, REST")
961 static const PyNumberMethods ge_pynumber
= {
963 0, /* @nb_subtract@ */
964 ge_pymul
, /* @nb_multiply@ */
966 ge_pydiv
, /* @nb_divide@ */
968 0, /* @nb_remainder@ */
970 ge_pyexp
, /* @nb_power@ */
971 0, /* @nb_negative@ */
972 0, /* @nb_positive@ */
973 0, /* @nb_absolute@ */
974 ge_pynonzerop
, /* @nb_nonzero@ */
984 ge_pyint
, /* @nb_int@ */
985 PY23(ge_pylong
, 0), /* @nb_long@ */
986 0 /* meaningless */, /* @nb_float@ */
992 0, /* @nb_inplace_add@ */
993 0, /* @nb_inplace_subtract@ */
994 0, /* @nb_inplace_multiply@ */
996 0, /* @nb_inplace_divide@ */
998 0, /* @nb_inplace_remainder@ */
999 0, /* @nb_inplace_power@ */
1000 0, /* @nb_inplace_lshift@ */
1001 0, /* @nb_inplace_rshift@ */
1002 0, /* @nb_inplace_and@ */
1003 0, /* @nb_inplace_xor@ */
1004 0, /* @nb_inplace_or@ */
1006 0, /* @nb_floor_divide@ */
1007 ge_pydiv
, /* @nb_true_divide@ */
1008 0, /* @nb_inplace_floor_divide@ */
1009 0, /* @nb_inplace_true_divide@ */
1012 static const PyTypeObject ge_pytype_skel
= {
1013 PyVarObject_HEAD_INIT(0, 0) /* Header */
1014 "GE", /* @tp_name@ */
1015 sizeof(ge_pyobj
), /* @tp_basicsize@ */
1016 0, /* @tp_itemsize@ */
1018 ge_pydealloc
, /* @tp_dealloc@ */
1020 0, /* @tp_getattr@ */
1021 0, /* @tp_setattr@ */
1022 0, /* @tp_compare@ */
1024 PYNUMBER(ge
), /* @tp_as_number@ */
1025 0, /* @tp_as_sequence@ */
1026 0, /* @tp_as_mapping@ */
1027 ge_pyhash
, /* @tp_hash@ */
1029 ge_pystr
, /* @tp_str@ */
1030 0, /* @tp_getattro@ */
1031 0, /* @tp_setattro@ */
1032 0, /* @tp_as_buffer@ */
1033 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1034 Py_TPFLAGS_CHECKTYPES
|
1035 Py_TPFLAGS_BASETYPE
,
1038 "Group elements, abstract base class.",
1040 0, /* @tp_traverse@ */
1042 ge_pyrichcompare
, /* @tp_richcompare@ */
1043 0, /* @tp_weaklistoffset@ */
1045 0, /* @tp_iternext@ */
1046 PYMETHODS(ge
), /* @tp_methods@ */
1047 0, /* @tp_members@ */
1048 PYGETSET(ge
), /* @tp_getset@ */
1051 0, /* @tp_descr_get@ */
1052 0, /* @tp_descr_set@ */
1053 0, /* @tp_dictoffset@ */
1055 PyType_GenericAlloc
, /* @tp_alloc@ */
1056 abstract_pynew
, /* @tp_new@ */
1061 static const PyGetSetDef group_pygetset
[] = {
1062 #define GETSETNAME(op, name) g##op##_##name
1063 GET (noctets
, "G.noctets -> size in octets of element")
1064 GET (nbits
, "G.nbits -> size in bits of element")
1065 GET (i
, "G.i -> group identity")
1066 GET (g
, "G.g -> group generator")
1067 GET (r
, "G.r -> group order")
1068 GET (h
, "G.h -> group cofactor")
1073 static const PyMethodDef group_pymethods
[] = {
1074 #define METHNAME(name) gmeth_##name
1075 METH (mexp
, "G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...")
1076 KWMETH(checkgroup
, "G.checkgroup([rng = rand]): check group is good")
1077 SMTH (parse
, "parse(STR) -> G, REST")
1082 static const PyTypeObject group_pytype_skel
= {
1083 PyVarObject_HEAD_INIT(0, 0) /* Header */
1084 "Group", /* @tp_name@ */
1085 sizeof(group_pyobj
), /* @tp_basicsize@ */
1086 0, /* @tp_itemsize@ */
1088 group_pydealloc
, /* @tp_dealloc@ */
1090 0, /* @tp_getattr@ */
1091 0, /* @tp_setattr@ */
1092 0, /* @tp_compare@ */
1094 0, /* @tp_as_number@ */
1095 0, /* @tp_as_sequence@ */
1096 0, /* @tp_as_mapping@ */
1100 0, /* @tp_getattro@ */
1101 0, /* @tp_setattro@ */
1102 0, /* @tp_as_buffer@ */
1103 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1104 Py_TPFLAGS_BASETYPE
,
1107 "Abstract base class for groups.",
1109 0, /* @tp_traverse@ */
1111 group_pyrichcompare
, /* @tp_richcompare@ */
1112 0, /* @tp_weaklistoffset@ */
1114 0, /* @tp_iternext@ */
1115 PYMETHODS(group
), /* @tp_methods@ */
1116 0, /* @tp_members@ */
1117 PYGETSET(group
), /* @tp_getset@ */
1120 0, /* @tp_descr_get@ */
1121 0, /* @tp_descr_set@ */
1122 0, /* @tp_dictoffset@ */
1124 PyType_GenericAlloc
, /* @tp_alloc@ */
1125 abstract_pynew
, /* @tp_new@ */
1130 static PyObject
*pgget_info(PyObject
*me
, void *hunoz
)
1133 gctx_prime
*gg
= (gctx_prime
*)GROUP_G(me
);
1134 dp
.p
= MP_COPY(gg
->mm
.m
);
1135 dp
.q
= MP_COPY(gg
->g
.r
);
1136 dp
.g
= mpmont_reduce(&gg
->mm
, MP_NEW
, gg
->gen
.x
);
1137 return (fginfo_pywrap(&dp
, dhinfo_pytype
));
1140 static const PyGetSetDef primegroup_pygetset
[] = {
1141 #define GETSETNAME(op, name) pg##op##_##name
1142 GET (info
, "G.info -> information about the group")
1147 static PyObject
*primegroup_pynew(PyTypeObject
*ty
,
1148 PyObject
*arg
, PyObject
*kw
)
1151 static const char *const kwlist
[] = { "info", 0 };
1153 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1156 return (group_dopywrap(ty
, group_prime(FGINFO_DP(i
))));
1159 static const PyTypeObject primegroup_pytype_skel
= {
1160 PyVarObject_HEAD_INIT(0, 0) /* Header */
1161 "PrimeGroup", /* @tp_name@ */
1162 sizeof(group_pyobj
), /* @tp_basicsize@ */
1163 0, /* @tp_itemsize@ */
1165 group_pydealloc
, /* @tp_dealloc@ */
1167 0, /* @tp_getattr@ */
1168 0, /* @tp_setattr@ */
1169 0, /* @tp_compare@ */
1171 0, /* @tp_as_number@ */
1172 0, /* @tp_as_sequence@ */
1173 0, /* @tp_as_mapping@ */
1177 0, /* @tp_getattro@ */
1178 0, /* @tp_setattro@ */
1179 0, /* @tp_as_buffer@ */
1180 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1181 Py_TPFLAGS_BASETYPE
,
1184 "PrimeGroup(INFO): subgroups of prime fields.",
1186 0, /* @tp_traverse@ */
1188 0, /* @tp_richcompare@ */
1189 0, /* @tp_weaklistoffset@ */
1191 0, /* @tp_iternext@ */
1192 0, /* @tp_methods@ */
1193 0, /* @tp_members@ */
1194 PYGETSET(primegroup
), /* @tp_getset@ */
1197 0, /* @tp_descr_get@ */
1198 0, /* @tp_descr_set@ */
1199 0, /* @tp_dictoffset@ */
1201 PyType_GenericAlloc
, /* @tp_alloc@ */
1202 primegroup_pynew
, /* @tp_new@ */
1207 static PyObject
*bgget_info(PyObject
*me
, void *hunoz
)
1210 gctx_bin
*gg
= (gctx_bin
*)GROUP_G(me
);
1211 dp
.p
= MP_COPY(gg
->r
.p
);
1212 dp
.q
= MP_COPY(gg
->g
.r
);
1213 dp
.g
= MP_COPY(gg
->gen
.x
);
1214 return (fginfo_pywrap(&dp
, bindhinfo_pytype
));
1217 static const PyGetSetDef bingroup_pygetset
[] = {
1218 #define GETSETNAME(op, name) bg##op##_##name
1219 GET (info
, "G.info -> information about the group")
1224 static PyObject
*bingroup_pynew(PyTypeObject
*ty
,
1225 PyObject
*arg
, PyObject
*kw
)
1228 static const char *const kwlist
[] = { "info", 0 };
1230 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1231 bindhinfo_pytype
, &i
))
1233 return (group_dopywrap(ty
, group_binary(FGINFO_DP(i
))));
1236 static const PyTypeObject bingroup_pytype_skel
= {
1237 PyVarObject_HEAD_INIT(0, 0) /* Header */
1238 "BinGroup", /* @tp_name@ */
1239 sizeof(group_pyobj
), /* @tp_basicsize@ */
1240 0, /* @tp_itemsize@ */
1242 group_pydealloc
, /* @tp_dealloc@ */
1244 0, /* @tp_getattr@ */
1245 0, /* @tp_setattr@ */
1246 0, /* @tp_compare@ */
1248 0, /* @tp_as_number@ */
1249 0, /* @tp_as_sequence@ */
1250 0, /* @tp_as_mapping@ */
1254 0, /* @tp_getattro@ */
1255 0, /* @tp_setattro@ */
1256 0, /* @tp_as_buffer@ */
1257 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1258 Py_TPFLAGS_BASETYPE
,
1261 "BinGroup(INFO): subgroups of binary fields.",
1263 0, /* @tp_traverse@ */
1265 0, /* @tp_richcompare@ */
1266 0, /* @tp_weaklistoffset@ */
1268 0, /* @tp_iternext@ */
1269 0, /* @tp_methods@ */
1270 0, /* @tp_members@ */
1271 PYGETSET(bingroup
), /* @tp_getset@ */
1274 0, /* @tp_descr_get@ */
1275 0, /* @tp_descr_set@ */
1276 0, /* @tp_dictoffset@ */
1278 PyType_GenericAlloc
, /* @tp_alloc@ */
1279 bingroup_pynew
, /* @tp_new@ */
1284 static PyObject
*egget_info(PyObject
*me
, void *hunoz
)
1287 gctx_ec
*gg
= (gctx_ec
*)GROUP_G(me
);
1289 ecinfo_copy(&ei
, &gg
->ei
);
1290 return (ecinfo_pywrap(&ei
));
1293 static const PyGetSetDef ecgroup_pygetset
[] = {
1294 #define GETSETNAME(op, name) eg##op##_##name
1295 GET (info
, "G.info -> information about the group")
1300 static PyObject
*ecgroup_pynew(PyTypeObject
*ty
,
1301 PyObject
*arg
, PyObject
*kw
)
1305 static const char *const kwlist
[] = { "info", 0 };
1307 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!:new", KWLIST
,
1310 ecinfo_copy(&ei
, ECINFO_EI(i
));
1311 return (group_dopywrap(ty
, group_ec(&ei
)));
1314 static const PyTypeObject ecgroup_pytype_skel
= {
1315 PyVarObject_HEAD_INIT(0, 0) /* Header */
1316 "ECGroup", /* @tp_name@ */
1317 sizeof(group_pyobj
), /* @tp_basicsize@ */
1318 0, /* @tp_itemsize@ */
1320 group_pydealloc
, /* @tp_dealloc@ */
1322 0, /* @tp_getattr@ */
1323 0, /* @tp_setattr@ */
1324 0, /* @tp_compare@ */
1326 0, /* @tp_as_number@ */
1327 0, /* @tp_as_sequence@ */
1328 0, /* @tp_as_mapping@ */
1332 0, /* @tp_getattro@ */
1333 0, /* @tp_setattro@ */
1334 0, /* @tp_as_buffer@ */
1335 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1336 Py_TPFLAGS_BASETYPE
,
1339 "ECGroup(INFO): elliptic curve groups.",
1341 0, /* @tp_traverse@ */
1343 0, /* @tp_richcompare@ */
1344 0, /* @tp_weaklistoffset@ */
1346 0, /* @tp_iternext@ */
1347 0, /* @tp_methods@ */
1348 0, /* @tp_members@ */
1349 PYGETSET(ecgroup
), /* @tp_getset@ */
1352 0, /* @tp_descr_get@ */
1353 0, /* @tp_descr_set@ */
1354 0, /* @tp_dictoffset@ */
1356 PyType_GenericAlloc
, /* @tp_alloc@ */
1357 ecgroup_pynew
, /* @tp_new@ */
1362 /*----- Global stuff ------------------------------------------------------*/
1364 void group_pyinit(void)
1366 INITTYPE(fginfo
, root
);
1367 INITTYPE(dhinfo
, fginfo
);
1368 INITTYPE(bindhinfo
, dhinfo
);
1370 INITTYPE(group
, type
);
1371 INITTYPE(primegroup
, group
);
1372 INITTYPE(bingroup
, group
);
1373 INITTYPE(ecgroup
, group
);
1376 static const char *grp_namefn(const void *p
)
1377 { const pentry
*pt
= p
; return (pt
->name
); }
1379 static int grp_ixfn(const pentry
*tab
, const pentry
*pt
)
1383 for (i
= 0; tab
[i
].name
; i
++)
1384 if (tab
[i
].data
== pt
->data
) return (i
);
1387 static int pgrp_ixfn(const void *p
) { return (grp_ixfn(ptab
, p
)); }
1388 static int bgrp_ixfn(const void *p
) { return (grp_ixfn(bintab
, p
)); }
1390 static PyObject
*grp_valfn(const pentry
*tab
, PyTypeObject
*ty
, int i
)
1394 dh_infofromdata(&gp
, tab
[i
].data
);
1395 return (fginfo_pywrap(&gp
, ty
));
1397 static PyObject
*pgrp_valfn(int i
)
1398 { return (grp_valfn(ptab
, dhinfo_pytype
, i
)); }
1399 static PyObject
*bgrp_valfn(int i
)
1400 { return (grp_valfn(bintab
, bindhinfo_pytype
, i
)); }
1402 void group_pyinsert(PyObject
*mod
)
1404 INSERT("FGInfo", fginfo_pytype
);
1405 INSERT("DHInfo", dhinfo_pytype
);
1406 INSERT("BinDHInfo", bindhinfo_pytype
);
1407 INSERT("GE", ge_pytype
);
1408 INSERT("Group", group_pytype
);
1409 INSERT("PrimeGroup", primegroup_pytype
);
1410 INSERT("BinGroup", bingroup_pytype
);
1411 INSERT("ECGroup", ecgroup_pytype
);
1412 INSERT("primegroups", make_grouptab(ptab
, sizeof(*ptab
),
1413 grp_namefn
, pgrp_ixfn
, pgrp_valfn
));
1414 INSERT("bingroups", make_grouptab(bintab
, sizeof(*bintab
),
1415 grp_namefn
, bgrp_ixfn
, bgrp_valfn
));
1418 /*----- That's all, folks -------------------------------------------------*/