Commit | Line | Data |
---|---|---|
d7ab1bab | 1 | /* -*-c-*- |
2 | * | |
3 | * $Id$ | |
4 | * | |
5 | * Abstract group inteface | |
6 | * | |
7 | * (c) 2004 Straylight/Edgeware | |
8 | */ | |
9 | ||
10 | /*----- Licensing notice --------------------------------------------------* | |
11 | * | |
12 | * This file is part of the Python interface to Catacomb. | |
13 | * | |
14 | * Catacomb/Python is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or | |
17 | * (at your option) any later version. | |
18 | * | |
19 | * Catacomb/Python is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with Catacomb/Python; if not, write to the Free Software Foundation, | |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
27 | */ | |
28 | ||
29 | /*----- Header files ------------------------------------------------------*/ | |
30 | ||
31 | #include "catacomb-python.h" | |
32 | ||
33 | /*----- DH and binary group infos -----------------------------------------*/ | |
34 | ||
35 | PyObject *fginfo_pywrap(gprime_param *dp, PyTypeObject *ty) | |
36 | { | |
37 | fginfo_pyobj *z = PyObject_New(fginfo_pyobj, ty); | |
2a28a112 | 38 | z->dp = *dp; |
d7ab1bab | 39 | return ((PyObject *)z); |
40 | } | |
41 | ||
42 | static PyObject *fginfo_pynew(PyTypeObject *ty, | |
43 | PyObject *arg, PyObject *kw) | |
44 | { | |
45 | char *kwlist[] = { "p", "r", "g", 0 }; | |
46 | gprime_param dp = { 0 }; | |
47 | fginfo_pyobj *z = 0; | |
48 | ||
49 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&O&:new", kwlist, | |
50 | convmp, &dp.p, | |
51 | convmp, &dp.q, | |
5f959e50 | 52 | convmp, &dp.g)) |
d7ab1bab | 53 | goto end; |
54 | z = PyObject_New(fginfo_pyobj, ty); | |
55 | z->dp = dp; | |
56 | return ((PyObject *)z); | |
57 | end: | |
58 | mp_drop(dp.p); | |
59 | mp_drop(dp.q); | |
60 | mp_drop(dp.g); | |
61 | return (0); | |
62 | } | |
63 | ||
64 | static PyObject *figet_r(PyObject *me, void *hunoz) | |
2a28a112 | 65 | { return mp_pywrap(MP_COPY(FGINFO_DP(me)->q)); } |
d7ab1bab | 66 | |
67 | static PyObject *diget_p(PyObject *me, void *hunoz) | |
2a28a112 | 68 | { return mp_pywrap(MP_COPY(FGINFO_DP(me)->p)); } |
d7ab1bab | 69 | |
70 | static PyObject *diget_g(PyObject *me, void *hunoz) | |
2a28a112 | 71 | { return mp_pywrap(MP_COPY(FGINFO_DP(me)->g)); } |
d7ab1bab | 72 | |
73 | static PyObject *biget_p(PyObject *me, void *hunoz) | |
2a28a112 | 74 | { return gf_pywrap(MP_COPY(FGINFO_DP(me)->p)); } |
d7ab1bab | 75 | |
76 | static PyObject *biget_m(PyObject *me, void *hunoz) | |
77 | { return PyInt_FromLong(mp_octets(FGINFO_DP(me)->p) - 1); } | |
78 | ||
79 | static PyObject *biget_g(PyObject *me, void *hunoz) | |
2a28a112 | 80 | { return gf_pywrap(MP_COPY(FGINFO_DP(me)->g)); } |
d7ab1bab | 81 | |
82 | static void fginfo_pydealloc(PyObject *me) | |
83 | { | |
84 | mp_drop(FGINFO_DP(me)->p); | |
85 | mp_drop(FGINFO_DP(me)->q); | |
86 | mp_drop(FGINFO_DP(me)->g); | |
3aa33042 | 87 | FREEOBJ(me); |
d7ab1bab | 88 | } |
89 | ||
90 | static PyObject *meth__DHInfo_generate(PyObject *me, | |
91 | PyObject *arg, PyObject *kw) | |
92 | { | |
93 | dh_param dp; | |
94 | unsigned ql = 0, pl; | |
95 | unsigned steps = 0; | |
96 | grand *r = &rand_global; | |
97 | pgev evt = { 0 }; | |
98 | char *kwlist[] = | |
99 | { "class", "pbits", "qbits", "event", "rng", "nsteps", 0 }; | |
100 | PyObject *rc = 0; | |
101 | ||
102 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", kwlist, | |
103 | &me, convuint, &pl, convuint, &ql, | |
104 | convpgev, &evt, convgrand, &r, | |
105 | convuint, &steps)) | |
106 | goto end; | |
107 | if (dh_gen(&dp, ql, pl, steps, r, evt.proc, evt.ctx)) | |
108 | PGENERR; | |
109 | rc = fginfo_pywrap(&dp, dhinfo_pytype); | |
110 | end: | |
111 | droppgev(&evt); | |
112 | return (rc); | |
113 | } | |
114 | ||
115 | static PyObject *meth__DHInfo_genlimlee(PyObject *me, | |
116 | PyObject *arg, PyObject *kw) | |
117 | { | |
118 | dh_param dp; | |
119 | unsigned ql, pl; | |
120 | unsigned steps = 0; | |
121 | grand *r = &rand_global; | |
122 | pgev oe = { 0 }, ie = { 0 }; | |
123 | int subgroupp = 1; | |
124 | unsigned f = 0; | |
125 | char *kwlist[] = { "class", "pbits", "qbits", "event", "ievent", | |
126 | "rng", "nsteps", "subgroupp", 0 }; | |
127 | size_t i, nf; | |
128 | mp **v = 0; | |
129 | PyObject *rc = 0, *vec = 0; | |
130 | ||
131 | if (!PyArg_ParseTupleAndKeywords(arg, kw, | |
132 | "OO&O&|O&O&O&O&O&:genlimlee", kwlist, | |
133 | &me, convuint, &pl, convuint, &ql, | |
134 | convpgev, &oe, convpgev, &ie, | |
135 | convgrand, &r, convuint, &steps, | |
136 | convbool, &subgroupp)) | |
137 | goto end; | |
138 | if (subgroupp) f |= DH_SUBGROUP; | |
139 | if (dh_limlee(&dp, ql, pl, f, steps, r, | |
140 | oe.proc, oe.ctx, ie.proc, ie.ctx, &nf, &v)) | |
141 | PGENERR; | |
142 | vec = PyList_New(nf); | |
143 | for (i = 0; i < nf; i++) | |
144 | PyList_SetItem(vec, i, mp_pywrap(v[i])); | |
145 | xfree(v); | |
146 | rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype), vec); | |
147 | end: | |
148 | droppgev(&oe); droppgev(&ie); | |
149 | return (rc); | |
150 | } | |
151 | ||
2a28a112 MW |
152 | static PyObject *meth__DHInfo_genkcdsa(PyObject *me, |
153 | PyObject *arg, PyObject *kw) | |
154 | { | |
155 | dh_param dp; | |
156 | unsigned ql, pl; | |
157 | unsigned steps = 0; | |
158 | grand *r = &rand_global; | |
159 | pgev evt = { 0 }; | |
160 | char *kwlist[] = { "class", "pbits", "qbits", | |
161 | "event", "rng", "nsteps", 0 }; | |
162 | mp *v = MP_NEW; | |
163 | PyObject *rc = 0; | |
164 | ||
165 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&|O&O&O&:genkcdsa", kwlist, | |
166 | &me, convuint, &pl, convuint, &ql, | |
167 | convpgev, &evt, convgrand, &r, | |
168 | convuint, &steps)) | |
169 | goto end; | |
170 | if (dh_kcdsagen(&dp, ql, pl, 0, steps, r, evt.proc, evt.ctx)) | |
171 | PGENERR; | |
172 | mp_div(&v, 0, dp.p, dp.q); | |
173 | v = mp_lsr(v, v, 1); | |
174 | rc = Py_BuildValue("(NN)", fginfo_pywrap(&dp, dhinfo_pytype), | |
175 | mp_pywrap(v)); | |
176 | end: | |
177 | droppgev(&evt); | |
178 | return (rc); | |
179 | } | |
180 | ||
d7ab1bab | 181 | static PyObject *meth__DHInfo_gendsa(PyObject *me, |
182 | PyObject *arg, PyObject *kw) | |
183 | { | |
184 | dsa_param dp; | |
185 | unsigned ql, pl; | |
186 | unsigned steps = 0; | |
187 | dsa_seed ds; | |
188 | char *k; | |
189 | int ksz; | |
190 | pgev evt = { 0 }; | |
191 | char *kwlist[] = | |
192 | { "class", "pbits", "qbits", "seed", "event", "nsteps", 0 }; | |
193 | PyObject *rc = 0; | |
194 | ||
f368b46e | 195 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&O&s#|O&O&:gendsa", kwlist, |
d7ab1bab | 196 | &me, convuint, &pl, convuint, &ql, |
197 | &k, &ksz, convpgev, &evt, | |
198 | convuint, &steps)) | |
199 | goto end; | |
200 | if (dsa_gen(&dp, ql, pl, steps, k, ksz, &ds, evt.proc, evt.ctx)) | |
201 | PGENERR; | |
202 | rc = Py_BuildValue("(NNl)", fginfo_pywrap(&dp, dhinfo_pytype), | |
203 | bytestring_pywrap(ds.p, ds.sz), (long)ds.count); | |
204 | xfree(ds.p); | |
205 | end: | |
206 | droppgev(&evt); | |
207 | return (rc); | |
208 | } | |
209 | ||
210 | static int npgroups = -1, nbingroups = -1; | |
211 | ||
212 | static PyObject *namedgroups(const pentry *pp, int *ne) | |
213 | { | |
214 | int i, j; | |
215 | const char *p; | |
216 | PyObject *d, *c; | |
217 | ||
218 | d = PyDict_New(); | |
219 | for (i = 0; pp[i].name; i++) { | |
220 | p = pp[i].name; | |
221 | for (j = 0; j < i; j++) { | |
222 | if (pp[i].data == pp[j].data) { | |
223 | c = PyDict_GetItemString(d, (/*unconst*/ char *)pp[j].name); | |
224 | Py_INCREF(c); | |
225 | goto found; | |
226 | } | |
227 | } | |
228 | c = PyInt_FromLong(i); | |
229 | found: | |
230 | PyDict_SetItemString(d, (/*unconst*/ char *)pp[i].name, c); | |
231 | Py_DECREF(c); | |
232 | } | |
233 | *ne = i; | |
234 | return (d); | |
235 | } | |
236 | ||
237 | static PyObject *meth__groupn(PyObject *me, PyObject *arg, | |
238 | PyTypeObject *ty, const pentry *pp, int ne) | |
239 | { | |
240 | int i; | |
241 | gprime_param gp; | |
242 | PyObject *rc = 0; | |
243 | ||
244 | if (!PyArg_ParseTuple(arg, "Oi:_groupn", &me, &i)) goto end; | |
245 | if (i < 0 || i >= ne) VALERR("group index out of range"); | |
246 | dh_infofromdata(&gp, pp[i].data); | |
247 | rc = fginfo_pywrap(&gp, ty); | |
248 | end: | |
249 | return (rc); | |
250 | } | |
251 | ||
252 | static PyObject *meth__DHInfo__groupn(PyObject *me, PyObject *arg) | |
253 | { return (meth__groupn(me, arg, dhinfo_pytype, ptab, npgroups)); } | |
254 | ||
255 | static PyObject *meth__BinDHInfo__groupn(PyObject *me, PyObject *arg) | |
256 | { return (meth__groupn(me, arg, bindhinfo_pytype, bintab, nbingroups)); } | |
257 | ||
258 | static PyObject *meth__parse(PyObject *me, PyObject *arg, PyTypeObject *ty, | |
259 | int (*parse)(qd_parse *, gprime_param *)) | |
260 | { | |
261 | qd_parse qd; | |
262 | char *p; | |
263 | gprime_param gp; | |
264 | PyObject *rc = 0; | |
265 | ||
266 | if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; | |
267 | qd.p = p; | |
268 | qd.e = 0; | |
269 | if (parse(&qd, &gp)) SYNERR(qd.e); | |
270 | rc = fginfo_pywrap(&gp, ty); | |
271 | end: | |
272 | return (rc); | |
273 | } | |
274 | ||
275 | static PyObject *meth__DHInfo_parse(PyObject *me, PyObject *arg) | |
276 | { return (meth__parse(me, arg, dhinfo_pytype, dh_parse)); } | |
277 | ||
278 | static PyObject *meth__BinDHInfo_parse(PyObject *me, PyObject *arg) | |
279 | { return (meth__parse(me, arg, bindhinfo_pytype, dhbin_parse)); } | |
280 | ||
281 | static PyGetSetDef fginfo_pygetset[] = { | |
282 | #define GETSETNAME(op, name) fi##op##_##name | |
283 | GET (r, "I.r -> group order") | |
284 | #undef GETSETNAME | |
285 | { 0 } | |
286 | }; | |
287 | ||
288 | static PyGetSetDef dhinfo_pygetset[] = { | |
289 | #define GETSETNAME(op, name) di##op##_##name | |
290 | GET (p, "I.p -> prime") | |
291 | GET (g, "I.g -> generator") | |
292 | #undef GETSETNAME | |
293 | { 0 } | |
294 | }; | |
295 | ||
296 | static PyGetSetDef bindhinfo_pygetset[] = { | |
297 | #define GETSETNAME(op, name) bi##op##_##name | |
298 | GET (p, "I.p -> irreducible polynomial") | |
299 | GET (m, "I.m -> degree of polynomial") | |
300 | GET (g, "I.g -> generator") | |
301 | #undef GETSETNAME | |
302 | { 0 } | |
303 | }; | |
304 | ||
305 | static PyTypeObject fginfo_pytype_skel = { | |
6d4db0bf | 306 | PyObject_HEAD_INIT(0) 0, /* Header */ |
d7ab1bab | 307 | "catacomb.FGInfo", /* @tp_name@ */ |
308 | sizeof(fginfo_pyobj), /* @tp_basicsize@ */ | |
309 | 0, /* @tp_itemsize@ */ | |
310 | ||
311 | fginfo_pydealloc, /* @tp_dealloc@ */ | |
312 | 0, /* @tp_print@ */ | |
313 | 0, /* @tp_getattr@ */ | |
314 | 0, /* @tp_setattr@ */ | |
315 | 0, /* @tp_compare@ */ | |
316 | 0, /* @tp_repr@ */ | |
317 | 0, /* @tp_as_number@ */ | |
318 | 0, /* @tp_as_sequence@ */ | |
319 | 0, /* @tp_as_mapping@ */ | |
320 | 0, /* @tp_hash@ */ | |
321 | 0, /* @tp_call@ */ | |
322 | 0, /* @tp_str@ */ | |
323 | 0, /* @tp_getattro@ */ | |
324 | 0, /* @tp_setattro@ */ | |
325 | 0, /* @tp_as_buffer@ */ | |
326 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
327 | Py_TPFLAGS_BASETYPE, | |
328 | ||
329 | /* @tp_doc@ */ | |
330 | "Abstract base class for field-group information objects.", | |
331 | ||
332 | 0, /* @tp_traverse@ */ | |
333 | 0, /* @tp_clear@ */ | |
334 | 0, /* @tp_richcompare@ */ | |
335 | 0, /* @tp_weaklistoffset@ */ | |
336 | 0, /* @tp_iter@ */ | |
963a6148 | 337 | 0, /* @tp_iternext@ */ |
d7ab1bab | 338 | 0, /* @tp_methods@ */ |
339 | 0, /* @tp_members@ */ | |
340 | fginfo_pygetset, /* @tp_getset@ */ | |
341 | 0, /* @tp_base@ */ | |
342 | 0, /* @tp_dict@ */ | |
343 | 0, /* @tp_descr_get@ */ | |
344 | 0, /* @tp_descr_set@ */ | |
345 | 0, /* @tp_dictoffset@ */ | |
346 | 0, /* @tp_init@ */ | |
347 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
348 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 349 | 0, /* @tp_free@ */ |
d7ab1bab | 350 | 0 /* @tp_is_gc@ */ |
351 | }; | |
352 | ||
353 | static PyTypeObject dhinfo_pytype_skel = { | |
6d4db0bf | 354 | PyObject_HEAD_INIT(0) 0, /* Header */ |
d7ab1bab | 355 | "catacomb.DHInfo", /* @tp_name@ */ |
356 | sizeof(fginfo_pyobj), /* @tp_basicsize@ */ | |
357 | 0, /* @tp_itemsize@ */ | |
358 | ||
359 | fginfo_pydealloc, /* @tp_dealloc@ */ | |
360 | 0, /* @tp_print@ */ | |
361 | 0, /* @tp_getattr@ */ | |
362 | 0, /* @tp_setattr@ */ | |
363 | 0, /* @tp_compare@ */ | |
364 | 0, /* @tp_repr@ */ | |
365 | 0, /* @tp_as_number@ */ | |
366 | 0, /* @tp_as_sequence@ */ | |
367 | 0, /* @tp_as_mapping@ */ | |
368 | 0, /* @tp_hash@ */ | |
369 | 0, /* @tp_call@ */ | |
370 | 0, /* @tp_str@ */ | |
371 | 0, /* @tp_getattro@ */ | |
372 | 0, /* @tp_setattro@ */ | |
373 | 0, /* @tp_as_buffer@ */ | |
374 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
375 | Py_TPFLAGS_BASETYPE, | |
376 | ||
377 | /* @tp_doc@ */ | |
378 | "Standard (integer) Diffie-Hellman group information.", | |
379 | ||
380 | 0, /* @tp_traverse@ */ | |
381 | 0, /* @tp_clear@ */ | |
382 | 0, /* @tp_richcompare@ */ | |
383 | 0, /* @tp_weaklistoffset@ */ | |
384 | 0, /* @tp_iter@ */ | |
963a6148 | 385 | 0, /* @tp_iternext@ */ |
d7ab1bab | 386 | 0, /* @tp_methods@ */ |
387 | 0, /* @tp_members@ */ | |
388 | dhinfo_pygetset, /* @tp_getset@ */ | |
389 | 0, /* @tp_base@ */ | |
390 | 0, /* @tp_dict@ */ | |
391 | 0, /* @tp_descr_get@ */ | |
392 | 0, /* @tp_descr_set@ */ | |
393 | 0, /* @tp_dictoffset@ */ | |
394 | 0, /* @tp_init@ */ | |
395 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
396 | fginfo_pynew, /* @tp_new@ */ | |
3aa33042 | 397 | 0, /* @tp_free@ */ |
d7ab1bab | 398 | 0 /* @tp_is_gc@ */ |
399 | }; | |
400 | ||
401 | static PyTypeObject bindhinfo_pytype_skel = { | |
6d4db0bf | 402 | PyObject_HEAD_INIT(0) 0, /* Header */ |
d7ab1bab | 403 | "catacomb.BinDHInfo", /* @tp_name@ */ |
404 | sizeof(fginfo_pyobj), /* @tp_basicsize@ */ | |
405 | 0, /* @tp_itemsize@ */ | |
406 | ||
407 | fginfo_pydealloc, /* @tp_dealloc@ */ | |
408 | 0, /* @tp_print@ */ | |
409 | 0, /* @tp_getattr@ */ | |
410 | 0, /* @tp_setattr@ */ | |
411 | 0, /* @tp_compare@ */ | |
412 | 0, /* @tp_repr@ */ | |
413 | 0, /* @tp_as_number@ */ | |
414 | 0, /* @tp_as_sequence@ */ | |
415 | 0, /* @tp_as_mapping@ */ | |
416 | 0, /* @tp_hash@ */ | |
417 | 0, /* @tp_call@ */ | |
418 | 0, /* @tp_str@ */ | |
419 | 0, /* @tp_getattro@ */ | |
420 | 0, /* @tp_setattro@ */ | |
421 | 0, /* @tp_as_buffer@ */ | |
422 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
423 | Py_TPFLAGS_BASETYPE, | |
424 | ||
425 | /* @tp_doc@ */ | |
426 | "Binary-field Diffie-Hellman group information.", | |
427 | ||
428 | 0, /* @tp_traverse@ */ | |
429 | 0, /* @tp_clear@ */ | |
430 | 0, /* @tp_richcompare@ */ | |
431 | 0, /* @tp_weaklistoffset@ */ | |
432 | 0, /* @tp_iter@ */ | |
963a6148 | 433 | 0, /* @tp_iternext@ */ |
d7ab1bab | 434 | 0, /* @tp_methods@ */ |
435 | 0, /* @tp_members@ */ | |
436 | bindhinfo_pygetset, /* @tp_getset@ */ | |
437 | 0, /* @tp_base@ */ | |
438 | 0, /* @tp_dict@ */ | |
439 | 0, /* @tp_descr_get@ */ | |
440 | 0, /* @tp_descr_set@ */ | |
441 | 0, /* @tp_dictoffset@ */ | |
442 | 0, /* @tp_init@ */ | |
443 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
444 | fginfo_pynew, /* @tp_new@ */ | |
3aa33042 | 445 | 0, /* @tp_free@ */ |
d7ab1bab | 446 | 0 /* @tp_is_gc@ */ |
447 | }; | |
448 | ||
449 | /*----- General utilities -------------------------------------------------*/ | |
450 | ||
451 | PyTypeObject *ge_pytype, *group_pytype; | |
452 | PyTypeObject *primegroup_pytype, *bingroup_pytype, *ecgroup_pytype; | |
453 | ||
454 | group *group_copy(group *g) | |
455 | { | |
456 | if (strcmp(G_NAME(g), "prime") == 0) { | |
457 | gctx_prime *gc = (gctx_prime *)g; | |
458 | gprime_param gp; | |
459 | gp.g = G_TOINT(g, MP_NEW, g->g); | |
460 | gp.p = gc->mm.m; | |
461 | gp.q = gc->g.r; | |
462 | g = group_prime(&gp); | |
463 | MP_DROP(gp.g); | |
464 | } else if (strcmp(G_NAME(g), "bin") == 0) { | |
465 | gctx_bin *gc = (gctx_bin *)g; | |
466 | gbin_param gb; | |
467 | gb.g = G_TOINT(g, MP_NEW, g->g); | |
468 | gb.p = gc->r.p; | |
469 | gb.q = gc->g.r; | |
470 | g = group_binary(&gb); | |
471 | MP_DROP(gb.g); | |
472 | } else if (strcmp(G_NAME(g), "ec") == 0) { | |
473 | gctx_ec *gc = (gctx_ec *)g; | |
474 | ec_info ei; | |
475 | if ((ei.c = eccurve_copy(gc->ei.c)) == 0) | |
476 | return (0); | |
477 | EC_CREATE(&ei.g); | |
478 | EC_COPY(&ei.g, &gc->ei.g); | |
479 | ei.r = MP_COPY(gc->ei.r); | |
480 | ei.h = MP_COPY(gc->ei.h); | |
481 | g = group_ec(&ei); | |
482 | } else | |
483 | g = 0; | |
484 | return (g); | |
485 | } | |
486 | ||
487 | PyObject *ge_pywrap(PyObject *gobj, ge *x) | |
488 | { | |
489 | ge_pyobj *z = PyObject_New(ge_pyobj, (PyTypeObject *)gobj); | |
490 | z->x = x; | |
491 | z->g = GROUP_G(gobj); | |
492 | Py_INCREF(gobj); | |
493 | return ((PyObject *)z); | |
494 | } | |
495 | ||
496 | static PyObject *ge_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
497 | { | |
498 | char *kwlist[] = { "x", 0 }; | |
499 | PyObject *x; | |
500 | group *g; | |
501 | ec p = EC_INIT; | |
502 | mp *y = 0; | |
503 | ge *xx = 0; | |
504 | mptext_stringctx sc; | |
505 | ||
506 | g = GROUP_G(ty); | |
507 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O:new", kwlist, &x)) goto end; | |
508 | xx = G_CREATE(g); | |
509 | if (ECPT_PYCHECK(x)) { | |
510 | getecptout(&p, x); | |
511 | if (G_FROMEC(g, xx, &p)) | |
512 | TYERR("can't convert from elliptic curve point"); | |
513 | EC_DESTROY(&p); | |
514 | } else if ((y = tomp(x)) != 0) { | |
515 | if (G_FROMINT(g, xx, y)) | |
516 | TYERR("can't convert from integer"); | |
517 | MP_DROP(y); | |
518 | } else if (PyString_Check(x)) { | |
519 | sc.buf = PyString_AS_STRING(x); | |
520 | sc.lim = sc.buf + PyString_GET_SIZE(x); | |
521 | if (G_READ(g, xx, &mptext_stringops, &sc) || sc.buf < sc.lim) | |
522 | SYNERR("malformed group element string"); | |
523 | } else | |
524 | TYERR("can't convert to group element"); | |
525 | return (ge_pywrap((PyObject *)ty, xx)); | |
526 | end: | |
527 | mp_drop(y); | |
528 | EC_DESTROY(&p); | |
529 | if (xx) G_DESTROY(g, xx); | |
530 | return (0); | |
531 | } | |
532 | ||
533 | static PyObject *group_dopywrap(PyTypeObject *ty, group *g) | |
534 | { | |
df9f8366 | 535 | group_pyobj *gobj = newtype(ty, 0, g->ops->name); |
d7ab1bab | 536 | gobj->g = g; |
df9f8366 | 537 | gobj->ty.type.tp_basicsize = sizeof(ge_pyobj); |
538 | gobj->ty.type.tp_base = ge_pytype; | |
d7ab1bab | 539 | Py_INCREF(group_pytype); |
df9f8366 | 540 | gobj->ty.type.tp_flags = (Py_TPFLAGS_DEFAULT | |
541 | Py_TPFLAGS_BASETYPE | | |
542 | Py_TPFLAGS_CHECKTYPES | | |
543 | Py_TPFLAGS_HEAPTYPE); | |
544 | gobj->ty.type.tp_alloc = PyType_GenericAlloc; | |
545 | gobj->ty.type.tp_free = 0; | |
546 | gobj->ty.type.tp_new = ge_pynew; | |
547 | PyType_Ready(&gobj->ty.type); | |
d7ab1bab | 548 | return ((PyObject *)gobj); |
549 | } | |
550 | ||
551 | PyObject *group_pywrap(group *g) | |
552 | { | |
553 | PyTypeObject *ty; | |
554 | ||
555 | if (strcmp(G_NAME(g), "prime") == 0) ty = primegroup_pytype; | |
556 | else if (strcmp(G_NAME(g), "bin") == 0) ty = bingroup_pytype; | |
557 | else if (strcmp(G_NAME(g), "ec") == 0) ty = ecgroup_pytype; | |
558 | else abort(); | |
559 | return (group_dopywrap(ty, g)); | |
560 | } | |
561 | ||
562 | /*----- Group elements ----------------------------------------------------*/ | |
563 | ||
564 | #define BINOP(name) \ | |
565 | static PyObject *ge_py##name(PyObject *x, PyObject *y) \ | |
566 | { \ | |
567 | ge *z; \ | |
568 | group *g; \ | |
569 | if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || \ | |
570 | (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) \ | |
571 | RETURN_NOTIMPL; \ | |
572 | g = GE_G(x); \ | |
573 | z = G_CREATE(g); \ | |
574 | g->ops->name(g, z, GE_X(x), GE_X(y)); \ | |
575 | return (ge_pywrap(GE_GOBJ(x), z)); \ | |
576 | } | |
577 | BINOP(mul) | |
578 | BINOP(div) | |
579 | #undef BINOP | |
580 | ||
581 | #define UNOP(name) \ | |
582 | static PyObject *gemeth_##name(PyObject *me, PyObject *arg) \ | |
583 | { \ | |
584 | group *g; \ | |
585 | ge *z; \ | |
586 | if (!PyArg_ParseTuple(arg, ":" #name)) return (0); \ | |
587 | g = GE_G(me); \ | |
588 | z = G_CREATE(g); \ | |
589 | g->ops->name(g, z, GE_X(me)); \ | |
590 | return (ge_pywrap(GE_GOBJ(me), z)); \ | |
591 | } | |
592 | UNOP(sqr) | |
593 | UNOP(inv) | |
594 | #undef UNOP | |
595 | ||
596 | static PyObject *ge_pyexp(PyObject *x, PyObject *n, PyObject *m) | |
597 | { | |
598 | mp *nn; | |
599 | ge *z; | |
600 | ||
601 | if (m != Py_None || !GE_PYCHECK(x) || (nn = getmp(n)) == 0) | |
602 | RETURN_NOTIMPL; | |
603 | z = G_CREATE(GE_G(x)); | |
604 | G_EXP(GE_G(x), z, GE_X(x), nn); | |
605 | MP_DROP(nn); | |
606 | return (ge_pywrap(GE_GOBJ(x), z)); | |
607 | } | |
608 | ||
609 | static void ge_pydealloc(PyObject *me) | |
610 | { | |
611 | G_DESTROY(GE_G(me), GE_X(me)); | |
612 | Py_DECREF(GE_GOBJ(me)); | |
3aa33042 | 613 | FREEOBJ(me); |
d7ab1bab | 614 | } |
615 | ||
616 | static void group_pydealloc(PyObject *me) | |
617 | { | |
618 | G_DESTROYGROUP(GROUP_G(me)); | |
619 | PyType_Type.tp_dealloc(me); | |
620 | } | |
621 | ||
622 | static PyObject *gmexp_id(PyObject *me) | |
623 | { | |
624 | group *g = GROUP_G(me); ge *x = G_CREATE(g); | |
625 | G_COPY(g, x, g->i); return (ge_pywrap(me, x)); | |
626 | } | |
627 | ||
628 | static int gmexp_fill(void *pp, PyObject *me, PyObject *x, PyObject *m) | |
629 | { | |
630 | group_expfactor *f = pp; | |
631 | ||
632 | if (!GE_PYCHECK(x) || GE_G(x) != GROUP_G(me) || (f->exp = getmp(m)) == 0) | |
633 | return (-1); | |
634 | f->base = GE_X(x); | |
635 | return (0); | |
636 | } | |
637 | ||
638 | static PyObject *ge_pyrichcompare(PyObject *x, PyObject *y, int op) | |
639 | { | |
640 | int b; | |
641 | PyObject *rc = 0; | |
642 | ||
643 | if (!GE_PYCHECK(x) || !GE_PYCHECK(y) || | |
644 | (GE_G(x) != GE_G(y) && !group_samep(GE_G(x), GE_G(y)))) | |
645 | RETURN_NOTIMPL; | |
646 | switch (op) { | |
647 | case Py_EQ: b = G_EQ(GE_G(x), GE_X(x), GE_X(y)); break; | |
648 | case Py_NE: b = !G_EQ(GE_G(x), GE_X(x), GE_X(y)); break; | |
649 | default: TYERR("group elements are unordered"); | |
650 | } | |
651 | rc = getbool(b); | |
652 | end: | |
653 | return (rc); | |
654 | } | |
655 | ||
656 | static PyObject *gemeth_check(PyObject *me, PyObject *arg) | |
657 | { | |
658 | if (!PyArg_ParseTuple(arg, ":check")) goto end; | |
659 | if (group_check(GE_G(me), GE_X(me))) VALERR("bad group element"); | |
660 | RETURN_OBJ(me); | |
661 | end: | |
662 | return (0); | |
663 | } | |
664 | ||
665 | static int ge_pynonzerop(PyObject *x) | |
666 | { return (!G_IDENTP(GE_G(x), GE_X(x))); } | |
667 | ||
668 | static PyObject *ge_pystr(PyObject *me) | |
669 | { | |
670 | dstr d = DSTR_INIT; | |
671 | PyObject *rc; | |
672 | ||
673 | group_writedstr(GE_G(me), GE_X(me), &d); | |
674 | rc = PyString_FromStringAndSize(d.buf, d.len); | |
675 | DDESTROY(&d); | |
676 | return (rc); | |
677 | } | |
678 | ||
679 | static PyObject *ge_pylong(PyObject *me) | |
680 | { | |
681 | mp *x = 0; | |
682 | PyObject *rc = 0; | |
683 | ||
684 | if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0) | |
685 | TYERR("can't convert to integer"); | |
f368b46e | 686 | rc = mp_topylong(x); |
d7ab1bab | 687 | end: |
688 | mp_drop(x); | |
689 | return (rc); | |
690 | } | |
691 | ||
692 | static PyObject *ge_pyint(PyObject *me) | |
693 | { | |
694 | mp *x = 0; | |
695 | PyObject *rc = 0; | |
696 | long l; | |
697 | ||
698 | if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0) | |
699 | TYERR("can't convert to integer"); | |
700 | if (mp_tolong_checked(x, &l)) goto end; | |
701 | rc = PyInt_FromLong(l); | |
702 | end: | |
703 | mp_drop(x); | |
704 | return (rc); | |
705 | } | |
706 | ||
707 | static PyObject *gemeth_toint(PyObject *me, PyObject *arg) | |
708 | { | |
709 | mp *x; | |
710 | ||
711 | if (!PyArg_ParseTuple(arg, ":toint")) goto end; | |
712 | if ((x = G_TOINT(GE_G(me), MP_NEW, GE_X(me))) == 0) | |
713 | TYERR("can't convert to integer"); | |
714 | return (mp_pywrap(x)); | |
715 | end: | |
716 | return (0); | |
717 | } | |
718 | ||
719 | static PyObject *gemeth_toec(PyObject *me, PyObject *arg, PyObject *kw) | |
720 | { | |
721 | char *kwlist[] = { "curve", 0 }; | |
722 | PyTypeObject *cty = ecpt_pytype; | |
723 | ec p = EC_INIT; | |
724 | ||
725 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O:toec", kwlist, | |
726 | &cty)) goto end; | |
727 | if (!PyType_Check(cty) || !PyType_IsSubtype(cty, ecpt_pytype)) | |
728 | TYERR("want subtype of catacomb.ECPt"); | |
729 | if (G_TOEC(GE_G(me), &p, GE_X(me))) | |
730 | TYERR("can't convert to ec point"); | |
731 | return (ecpt_pywrapout(cty, &p)); | |
732 | end: | |
733 | return (0); | |
734 | } | |
735 | ||
736 | static PyObject *gemeth_tobuf(PyObject *me, PyObject *arg) | |
737 | { | |
738 | buf b; | |
739 | PyObject *rc; | |
740 | size_t n; | |
741 | ||
742 | if (!PyArg_ParseTuple(arg, ":tobuf")) return (0); | |
743 | n = GE_G(me)->noctets + 4; | |
744 | rc = bytestring_pywrap(0, n); | |
745 | buf_init(&b, PyString_AS_STRING(rc), n); | |
746 | G_TOBUF(GE_G(me), &b, GE_X(me)); | |
747 | assert(BOK(&b)); | |
748 | _PyString_Resize(&rc, BLEN(&b)); | |
749 | return (rc); | |
750 | } | |
751 | ||
752 | static PyObject *gemeth_toraw(PyObject *me, PyObject *arg) | |
753 | { | |
754 | buf b; | |
755 | PyObject *rc; | |
756 | size_t n; | |
757 | ||
758 | if (!PyArg_ParseTuple(arg, ":toraw")) return (0); | |
759 | n = GE_G(me)->noctets; | |
760 | rc = bytestring_pywrap(0, n); | |
761 | buf_init(&b, PyString_AS_STRING(rc), n); | |
762 | G_TORAW(GE_G(me), &b, GE_X(me)); | |
763 | assert(BOK(&b)); | |
764 | _PyString_Resize(&rc, BLEN(&b)); | |
765 | return (rc); | |
766 | } | |
767 | ||
768 | static PyObject *gmexp_exp(PyObject *me, void *pp, int n) | |
769 | { | |
770 | ge *z = G_CREATE(GROUP_G(me)); | |
771 | G_MEXP(GROUP_G(me), z, pp, n); | |
772 | return (ge_pywrap(me, z)); | |
773 | } | |
774 | ||
775 | static void gmexp_drop(void *pp) | |
776 | { | |
777 | group_expfactor *f = pp; | |
778 | MP_DROP(f->exp); | |
779 | } | |
780 | ||
781 | static PyObject *gmeth_mexp(PyObject *me, PyObject *arg) | |
782 | { | |
783 | return (mexp_common(me, arg, sizeof(group_expfactor), | |
784 | gmexp_id, gmexp_fill, gmexp_exp, gmexp_drop)); | |
785 | } | |
786 | ||
787 | static PyObject *gmeth_check(PyObject *me, PyObject *arg, PyObject *kw) | |
788 | { | |
789 | char *kwlist[] = { "rng", 0 }; | |
790 | grand *r = &rand_global; | |
791 | const char *p; | |
792 | ||
793 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:check", kwlist, | |
794 | convgrand, &r)) | |
795 | goto end; | |
796 | if ((p = G_CHECK(GROUP_G(me), r)) != 0) | |
797 | VALERR(p); | |
798 | RETURN_OBJ(me); | |
799 | end: | |
800 | return (0); | |
801 | } | |
802 | ||
803 | static PyObject *group_pyrichcompare(PyObject *x, PyObject *y, int op) | |
804 | { | |
805 | int b = group_samep(GROUP_G(x), GROUP_G(y)); | |
806 | switch (op) { | |
807 | case Py_EQ: break; | |
808 | case Py_NE: b = !b; | |
809 | default: TYERR("can't order groups"); | |
810 | } | |
811 | return (getbool(b)); | |
812 | end: | |
813 | return (0); | |
814 | } | |
815 | ||
816 | static PyObject *meth__GE_frombuf(PyObject *me, PyObject *arg) | |
817 | { | |
818 | buf b; | |
819 | char *p; | |
820 | int n; | |
821 | group *g; | |
822 | ge *x = 0; | |
823 | ||
824 | if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &n)) | |
825 | return (0); | |
826 | g = GROUP_G(me); | |
827 | buf_init(&b, p, n); | |
828 | x = G_CREATE(g); | |
829 | if (G_FROMBUF(g, &b, x)) | |
830 | VALERR("invalid data"); | |
831 | return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b))); | |
832 | end: | |
833 | if (x) G_DESTROY(g, x); | |
834 | return (0); | |
835 | } | |
836 | ||
837 | static PyObject *meth__GE_fromraw(PyObject *me, PyObject *arg) | |
838 | { | |
839 | buf b; | |
840 | char *p; | |
841 | int n; | |
842 | group *g; | |
843 | ge *x = 0; | |
844 | ||
845 | if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &n)) | |
846 | return (0); | |
847 | g = GROUP_G(me); | |
848 | buf_init(&b, p, n); | |
849 | x = G_CREATE(g); | |
850 | if (G_FROMRAW(g, &b, x)) | |
851 | VALERR("invalid data"); | |
852 | return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b))); | |
853 | end: | |
854 | if (x) G_DESTROY(g, x); | |
855 | return (0); | |
856 | } | |
857 | ||
858 | static PyObject *meth__GE_fromstring(PyObject *me, PyObject *arg) | |
859 | { | |
860 | mptext_stringctx sc; | |
861 | char *p; | |
862 | int n; | |
863 | group *g; | |
864 | ge *x = 0; | |
865 | ||
866 | if (!PyArg_ParseTuple(arg, "Os#:fromstring", &me, &p, &n)) | |
867 | return (0); | |
868 | sc.buf = p; | |
869 | sc.lim = sc.buf + n; | |
870 | g = GROUP_G(me); | |
871 | x = G_CREATE(g); | |
872 | if (G_READ(g, x, &mptext_stringops, &sc)) | |
873 | SYNERR("bad group element string"); | |
874 | return (Py_BuildValue("(Ns#)", ge_pywrap(me, x), | |
875 | sc.buf, (int)(sc.lim - sc.buf))); | |
876 | end: | |
877 | if (x) G_DESTROY(g, x); | |
878 | return (0); | |
879 | } | |
880 | ||
881 | static PyObject *meth__Group_parse(PyObject *me, PyObject *arg) | |
882 | { | |
883 | char *p; | |
884 | qd_parse qd; | |
885 | group *g; | |
886 | ||
887 | if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) | |
888 | goto end; | |
889 | qd.p = p; | |
890 | qd.e = 0; | |
891 | if ((g = group_parse(&qd)) == 0) | |
892 | SYNERR(qd.e); | |
893 | return (group_pywrap(g)); | |
894 | end: | |
895 | return (0); | |
896 | } | |
897 | ||
898 | static PyObject *geget_group(PyObject *me, void *hunoz) | |
899 | { RETURN_OBJ(GE_GOBJ(me)); } | |
900 | ||
901 | static PyObject *gget_nbits(PyObject *me, void *hunoz) | |
902 | { return (PyInt_FromLong(GROUP_G(me)->nbits)); } | |
903 | ||
904 | static PyObject *gget_noctets(PyObject *me, void *hunoz) | |
905 | { return (PyInt_FromLong(GROUP_G(me)->noctets)); } | |
906 | ||
907 | static PyObject *gget_i(PyObject *me, void *hunoz) | |
908 | { | |
909 | group *g = GROUP_G(me); ge *x = G_CREATE(g); | |
910 | G_COPY(g, x, g->i); return (ge_pywrap(me, x)); | |
911 | } | |
912 | ||
913 | static PyObject *gget_g(PyObject *me, void *hunoz) | |
914 | { | |
915 | group *g = GROUP_G(me); ge *x = G_CREATE(g); | |
916 | G_COPY(g, x, g->g); return (ge_pywrap(me, x)); | |
917 | } | |
918 | ||
919 | static PyObject *gget_r(PyObject *me, void *hunoz) | |
920 | { return (mp_pywrap(MP_COPY(GROUP_G(me)->r))); } | |
921 | ||
922 | static PyObject *gget_h(PyObject *me, void *hunoz) | |
923 | { return (mp_pywrap(MP_COPY(GROUP_G(me)->h))); } | |
924 | ||
925 | static PyGetSetDef ge_pygetset[] = { | |
926 | #define GETSETNAME(op, name) ge##op##_##name | |
927 | GET (group, "X.group -> group containing X") | |
928 | #undef GETSETNAME | |
929 | { 0 } | |
930 | }; | |
931 | ||
932 | static PyMethodDef ge_pymethods[] = { | |
933 | #define METHNAME(name) gemeth_##name | |
934 | METH (inv, "X.inv() -> inverse element of X") | |
935 | METH (sqr, "X.sqr() -> X^2 = X * X") | |
936 | METH (check, "X.check() -> check X really belongs to its group") | |
937 | METH (toint, "X.toint() -> X converted to an integer") | |
938 | KWMETH(toec, "\ | |
939 | X.toec(curve = ecpt) -> X converted to elliptic curve point") | |
940 | METH (tobuf, "X.tobuf() -> X in buffer representation") | |
941 | METH (toraw, "X.toraw() -> X in raw representation") | |
942 | #undef METHNAME | |
943 | { 0 } | |
944 | }; | |
945 | ||
946 | static PyNumberMethods ge_pynumber = { | |
947 | 0, /* @nb_add@ */ | |
948 | 0, /* @nb_subtract@ */ | |
949 | ge_pymul, /* @nb_multiply@ */ | |
950 | ge_pydiv, /* @nb_divide@ */ | |
951 | 0, /* @nb_remainder@ */ | |
952 | 0, /* @nb_divmod@ */ | |
953 | ge_pyexp, /* @nb_power@ */ | |
954 | 0, /* @nb_negative@ */ | |
955 | 0, /* @nb_positive@ */ | |
956 | 0, /* @nb_absolute@ */ | |
957 | ge_pynonzerop, /* @nb_nonzero@ */ | |
958 | 0, /* @nb_invert@ */ | |
959 | 0, /* @nb_lshift@ */ | |
960 | 0, /* @nb_rshift@ */ | |
961 | 0, /* @nb_and@ */ | |
962 | 0, /* @nb_xor@ */ | |
963 | 0, /* @nb_or@ */ | |
964 | 0, /* @nb_coerce@ */ | |
965 | ge_pyint, /* @nb_int@ */ | |
966 | ge_pylong, /* @nb_long@ */ | |
967 | 0 /* meaningless */, /* @nb_float@ */ | |
968 | 0, /* @nb_oct@ */ | |
969 | 0, /* @nb_hex@ */ | |
970 | ||
971 | 0, /* @nb_inplace_add@ */ | |
972 | 0, /* @nb_inplace_subtract@ */ | |
973 | 0, /* @nb_inplace_multiply@ */ | |
974 | 0, /* @nb_inplace_divide@ */ | |
975 | 0, /* @nb_inplace_remainder@ */ | |
976 | 0, /* @nb_inplace_power@ */ | |
977 | 0, /* @nb_inplace_lshift@ */ | |
978 | 0, /* @nb_inplace_rshift@ */ | |
979 | 0, /* @nb_inplace_and@ */ | |
980 | 0, /* @nb_inplace_xor@ */ | |
981 | 0, /* @nb_inplace_or@ */ | |
982 | ||
983 | 0, /* @nb_floor_divide@ */ | |
984 | ge_pydiv, /* @nb_true_divide@ */ | |
985 | 0, /* @nb_inplace_floor_divide@ */ | |
986 | 0, /* @nb_inplace_true_divide@ */ | |
987 | }; | |
988 | ||
989 | static PyTypeObject ge_pytype_skel = { | |
6d4db0bf | 990 | PyObject_HEAD_INIT(0) 0, /* Header */ |
d7ab1bab | 991 | "catacomb.GE", /* @tp_name@ */ |
992 | sizeof(ge_pyobj), /* @tp_basicsize@ */ | |
993 | 0, /* @tp_itemsize@ */ | |
994 | ||
995 | ge_pydealloc, /* @tp_dealloc@ */ | |
996 | 0, /* @tp_print@ */ | |
997 | 0, /* @tp_getattr@ */ | |
998 | 0, /* @tp_setattr@ */ | |
999 | 0, /* @tp_compare@ */ | |
1000 | 0, /* @tp_repr@ */ | |
1001 | &ge_pynumber, /* @tp_as_number@ */ | |
1002 | 0, /* @tp_as_sequence@ */ | |
1003 | 0, /* @tp_as_mapping@ */ | |
1004 | 0, /* @tp_hash@ */ | |
1005 | 0, /* @tp_call@ */ | |
1006 | ge_pystr, /* @tp_str@ */ | |
1007 | 0, /* @tp_getattro@ */ | |
1008 | 0, /* @tp_setattro@ */ | |
1009 | 0, /* @tp_as_buffer@ */ | |
1010 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1011 | Py_TPFLAGS_CHECKTYPES | | |
1012 | Py_TPFLAGS_BASETYPE, | |
1013 | ||
1014 | /* @tp_doc@ */ | |
1015 | "Group elements, abstract base class.", | |
1016 | ||
1017 | 0, /* @tp_traverse@ */ | |
1018 | 0, /* @tp_clear@ */ | |
1019 | ge_pyrichcompare, /* @tp_richcompare@ */ | |
1020 | 0, /* @tp_weaklistoffset@ */ | |
1021 | 0, /* @tp_iter@ */ | |
963a6148 | 1022 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1023 | ge_pymethods, /* @tp_methods@ */ |
1024 | 0, /* @tp_members@ */ | |
1025 | ge_pygetset, /* @tp_getset@ */ | |
1026 | 0, /* @tp_base@ */ | |
1027 | 0, /* @tp_dict@ */ | |
1028 | 0, /* @tp_descr_get@ */ | |
1029 | 0, /* @tp_descr_set@ */ | |
1030 | 0, /* @tp_dictoffset@ */ | |
1031 | 0, /* @tp_init@ */ | |
1032 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1033 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 1034 | 0, /* @tp_free@ */ |
d7ab1bab | 1035 | 0 /* @tp_is_gc@ */ |
1036 | }; | |
1037 | ||
1038 | static PyGetSetDef group_pygetset[] = { | |
1039 | #define GETSETNAME(op, name) g##op##_##name | |
1040 | GET (noctets, "G.noctets -> size in octets of element") | |
1041 | GET (nbits, "G.nbits -> size in bits of element") | |
1042 | GET (i, "G.i -> group identity") | |
1043 | GET (g, "G.g -> group generator") | |
1044 | GET (r, "G.r -> group order") | |
1045 | GET (h, "G.h -> group cofactor") | |
1046 | #undef GETSETNAME | |
1047 | { 0 } | |
1048 | }; | |
1049 | ||
1050 | static PyMethodDef group_pymethods[] = { | |
1051 | #define METHNAME(name) gmeth_##name | |
1052 | METH (mexp, "\ | |
1053 | G.mexp([(X0, N0), (X1, N1), ...]) -> X0^N0 X1^N1 ...") | |
1054 | KWMETH(check, "G.check(rand = random): check group is good") | |
1055 | #undef METHNAME | |
1056 | { 0 } | |
1057 | }; | |
1058 | ||
1059 | static PyTypeObject group_pytype_skel = { | |
6d4db0bf | 1060 | PyObject_HEAD_INIT(0) 0, /* Header */ |
d7ab1bab | 1061 | "catacomb.Group", /* @tp_name@ */ |
1062 | sizeof(group_pyobj), /* @tp_basicsize@ */ | |
1063 | 0, /* @tp_itemsize@ */ | |
1064 | ||
1065 | group_pydealloc, /* @tp_dealloc@ */ | |
1066 | 0, /* @tp_print@ */ | |
1067 | 0, /* @tp_getattr@ */ | |
1068 | 0, /* @tp_setattr@ */ | |
1069 | 0, /* @tp_compare@ */ | |
1070 | 0, /* @tp_repr@ */ | |
1071 | 0, /* @tp_as_number@ */ | |
1072 | 0, /* @tp_as_sequence@ */ | |
1073 | 0, /* @tp_as_mapping@ */ | |
1074 | 0, /* @tp_hash@ */ | |
1075 | 0, /* @tp_call@ */ | |
1076 | 0, /* @tp_str@ */ | |
1077 | 0, /* @tp_getattro@ */ | |
1078 | 0, /* @tp_setattro@ */ | |
1079 | 0, /* @tp_as_buffer@ */ | |
1080 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1081 | Py_TPFLAGS_BASETYPE, | |
1082 | ||
1083 | /* @tp_doc@ */ | |
1084 | "Abstract base class for groups.", | |
1085 | ||
1086 | 0, /* @tp_traverse@ */ | |
1087 | 0, /* @tp_clear@ */ | |
1088 | group_pyrichcompare, /* @tp_richcompare@ */ | |
1089 | 0, /* @tp_weaklistoffset@ */ | |
1090 | 0, /* @tp_iter@ */ | |
963a6148 | 1091 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1092 | group_pymethods, /* @tp_methods@ */ |
1093 | 0, /* @tp_members@ */ | |
1094 | group_pygetset, /* @tp_getset@ */ | |
1095 | 0, /* @tp_base@ */ | |
1096 | 0, /* @tp_dict@ */ | |
1097 | 0, /* @tp_descr_get@ */ | |
1098 | 0, /* @tp_descr_set@ */ | |
1099 | 0, /* @tp_dictoffset@ */ | |
1100 | 0, /* @tp_init@ */ | |
1101 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1102 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 1103 | 0, /* @tp_free@ */ |
d7ab1bab | 1104 | 0 /* @tp_is_gc@ */ |
1105 | }; | |
1106 | ||
1107 | static PyObject *pgget_info(PyObject *me, void *hunoz) | |
1108 | { | |
1109 | gprime_param dp; | |
1110 | gctx_prime *gg = (gctx_prime *)GROUP_G(me); | |
1111 | dp.p = MP_COPY(gg->mm.m); | |
1112 | dp.q = MP_COPY(gg->g.r); | |
1113 | dp.g = mpmont_reduce(&gg->mm, MP_NEW, gg->gen); | |
1114 | return (fginfo_pywrap(&dp, dhinfo_pytype)); | |
1115 | } | |
1116 | ||
1117 | static PyGetSetDef primegroup_pygetset[] = { | |
1118 | #define GETSETNAME(op, name) pg##op##_##name | |
1119 | GET (info, "G.info -> information about the group") | |
1120 | #undef GETSETNAME | |
1121 | { 0 } | |
1122 | }; | |
1123 | ||
1124 | static PyObject *primegroup_pynew(PyTypeObject *ty, | |
1125 | PyObject *arg, PyObject *kw) | |
1126 | { | |
1127 | PyObject *i; | |
1128 | char *kwlist[] = { "info", 0 }; | |
1129 | ||
1130 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", kwlist, | |
1131 | dhinfo_pytype, &i)) | |
1132 | return (0); | |
1133 | return (group_dopywrap(ty, group_prime(FGINFO_DP(i)))); | |
1134 | } | |
1135 | ||
1136 | static PyTypeObject primegroup_pytype_skel = { | |
6d4db0bf | 1137 | PyObject_HEAD_INIT(0) 0, /* Header */ |
d7ab1bab | 1138 | "catacomb.PrimeGroup", /* @tp_name@ */ |
1139 | sizeof(group_pyobj), /* @tp_basicsize@ */ | |
1140 | 0, /* @tp_itemsize@ */ | |
1141 | ||
1142 | group_pydealloc, /* @tp_dealloc@ */ | |
1143 | 0, /* @tp_print@ */ | |
1144 | 0, /* @tp_getattr@ */ | |
1145 | 0, /* @tp_setattr@ */ | |
1146 | 0, /* @tp_compare@ */ | |
1147 | 0, /* @tp_repr@ */ | |
1148 | 0, /* @tp_as_number@ */ | |
1149 | 0, /* @tp_as_sequence@ */ | |
1150 | 0, /* @tp_as_mapping@ */ | |
1151 | 0, /* @tp_hash@ */ | |
1152 | 0, /* @tp_call@ */ | |
1153 | 0, /* @tp_str@ */ | |
1154 | 0, /* @tp_getattro@ */ | |
1155 | 0, /* @tp_setattro@ */ | |
1156 | 0, /* @tp_as_buffer@ */ | |
1157 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1158 | Py_TPFLAGS_BASETYPE, | |
1159 | ||
1160 | /* @tp_doc@ */ | |
1161 | "Subgroups of prime fields.", | |
1162 | ||
1163 | 0, /* @tp_traverse@ */ | |
1164 | 0, /* @tp_clear@ */ | |
1165 | 0, /* @tp_richcompare@ */ | |
1166 | 0, /* @tp_weaklistoffset@ */ | |
1167 | 0, /* @tp_iter@ */ | |
963a6148 | 1168 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1169 | 0, /* @tp_methods@ */ |
1170 | 0, /* @tp_members@ */ | |
1171 | primegroup_pygetset, /* @tp_getset@ */ | |
1172 | 0, /* @tp_base@ */ | |
1173 | 0, /* @tp_dict@ */ | |
1174 | 0, /* @tp_descr_get@ */ | |
1175 | 0, /* @tp_descr_set@ */ | |
1176 | 0, /* @tp_dictoffset@ */ | |
1177 | 0, /* @tp_init@ */ | |
1178 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1179 | primegroup_pynew, /* @tp_new@ */ | |
3aa33042 | 1180 | 0, /* @tp_free@ */ |
d7ab1bab | 1181 | 0 /* @tp_is_gc@ */ |
1182 | }; | |
1183 | ||
1184 | static PyObject *bgget_info(PyObject *me, void *hunoz) | |
1185 | { | |
1186 | gbin_param dp; | |
1187 | gctx_bin *gg = (gctx_bin *)GROUP_G(me); | |
1188 | dp.p = MP_COPY(gg->r.p); | |
1189 | dp.q = MP_COPY(gg->g.r); | |
1190 | dp.g = MP_COPY(gg->gen); | |
1191 | return (fginfo_pywrap(&dp, bindhinfo_pytype)); | |
1192 | } | |
1193 | ||
1194 | static PyGetSetDef bingroup_pygetset[] = { | |
1195 | #define GETSETNAME(op, name) bg##op##_##name | |
1196 | GET (info, "G.info -> information about the group") | |
1197 | #undef GETSETNAME | |
1198 | { 0 } | |
1199 | }; | |
1200 | ||
1201 | static PyObject *bingroup_pynew(PyTypeObject *ty, | |
1202 | PyObject *arg, PyObject *kw) | |
1203 | { | |
1204 | PyObject *i; | |
1205 | char *kwlist[] = { "info", 0 }; | |
1206 | ||
1207 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", kwlist, | |
1208 | bindhinfo_pytype, &i)) | |
1209 | return (0); | |
1210 | return (group_dopywrap(ty, group_binary(FGINFO_DP(i)))); | |
1211 | } | |
1212 | ||
1213 | static PyTypeObject bingroup_pytype_skel = { | |
6d4db0bf | 1214 | PyObject_HEAD_INIT(0) 0, /* Header */ |
d7ab1bab | 1215 | "catacomb.BinGroup", /* @tp_name@ */ |
1216 | sizeof(group_pyobj), /* @tp_basicsize@ */ | |
1217 | 0, /* @tp_itemsize@ */ | |
1218 | ||
1219 | group_pydealloc, /* @tp_dealloc@ */ | |
1220 | 0, /* @tp_print@ */ | |
1221 | 0, /* @tp_getattr@ */ | |
1222 | 0, /* @tp_setattr@ */ | |
1223 | 0, /* @tp_compare@ */ | |
1224 | 0, /* @tp_repr@ */ | |
1225 | 0, /* @tp_as_number@ */ | |
1226 | 0, /* @tp_as_sequence@ */ | |
1227 | 0, /* @tp_as_mapping@ */ | |
1228 | 0, /* @tp_hash@ */ | |
1229 | 0, /* @tp_call@ */ | |
1230 | 0, /* @tp_str@ */ | |
1231 | 0, /* @tp_getattro@ */ | |
1232 | 0, /* @tp_setattro@ */ | |
1233 | 0, /* @tp_as_buffer@ */ | |
1234 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1235 | Py_TPFLAGS_BASETYPE, | |
1236 | ||
1237 | /* @tp_doc@ */ | |
1238 | "Subgroups of binary fields.", | |
1239 | ||
1240 | 0, /* @tp_traverse@ */ | |
1241 | 0, /* @tp_clear@ */ | |
1242 | 0, /* @tp_richcompare@ */ | |
1243 | 0, /* @tp_weaklistoffset@ */ | |
1244 | 0, /* @tp_iter@ */ | |
963a6148 | 1245 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1246 | 0, /* @tp_methods@ */ |
1247 | 0, /* @tp_members@ */ | |
1248 | bingroup_pygetset, /* @tp_getset@ */ | |
1249 | 0, /* @tp_base@ */ | |
1250 | 0, /* @tp_dict@ */ | |
1251 | 0, /* @tp_descr_get@ */ | |
1252 | 0, /* @tp_descr_set@ */ | |
1253 | 0, /* @tp_dictoffset@ */ | |
1254 | 0, /* @tp_init@ */ | |
1255 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1256 | bingroup_pynew, /* @tp_new@ */ | |
3aa33042 | 1257 | 0, /* @tp_free@ */ |
d7ab1bab | 1258 | 0 /* @tp_is_gc@ */ |
1259 | }; | |
1260 | ||
1261 | static PyObject *egget_info(PyObject *me, void *hunoz) | |
1262 | { | |
1263 | ec_info ei; | |
1264 | gctx_ec *gg = (gctx_ec *)GROUP_G(me); | |
1265 | ||
1266 | ecinfo_copy(&ei, &gg->ei); | |
1267 | return (ecinfo_pywrap(&ei)); | |
1268 | } | |
1269 | ||
1270 | static PyGetSetDef ecgroup_pygetset[] = { | |
1271 | #define GETSETNAME(op, name) eg##op##_##name | |
1272 | GET (info, "G.info -> information about the group") | |
1273 | #undef GETSETNAME | |
1274 | { 0 } | |
1275 | }; | |
1276 | ||
1277 | static PyObject *ecgroup_pynew(PyTypeObject *ty, | |
1278 | PyObject *arg, PyObject *kw) | |
1279 | { | |
1280 | PyObject *i; | |
1281 | ec_info ei; | |
1282 | char *kwlist[] = { "info", 0 }; | |
1283 | ||
1284 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!:new", kwlist, | |
1285 | ecinfo_pytype, &i)) | |
1286 | return (0); | |
1287 | ecinfo_copy(&ei, ECINFO_EI(i)); | |
1288 | return (group_dopywrap(ty, group_ec(&ei))); | |
1289 | } | |
1290 | ||
1291 | static PyTypeObject ecgroup_pytype_skel = { | |
6d4db0bf | 1292 | PyObject_HEAD_INIT(0) 0, /* Header */ |
d7ab1bab | 1293 | "catacomb.ECGroup", /* @tp_name@ */ |
1294 | sizeof(group_pyobj), /* @tp_basicsize@ */ | |
1295 | 0, /* @tp_itemsize@ */ | |
1296 | ||
1297 | group_pydealloc, /* @tp_dealloc@ */ | |
1298 | 0, /* @tp_print@ */ | |
1299 | 0, /* @tp_getattr@ */ | |
1300 | 0, /* @tp_setattr@ */ | |
1301 | 0, /* @tp_compare@ */ | |
1302 | 0, /* @tp_repr@ */ | |
1303 | 0, /* @tp_as_number@ */ | |
1304 | 0, /* @tp_as_sequence@ */ | |
1305 | 0, /* @tp_as_mapping@ */ | |
1306 | 0, /* @tp_hash@ */ | |
1307 | 0, /* @tp_call@ */ | |
1308 | 0, /* @tp_str@ */ | |
1309 | 0, /* @tp_getattro@ */ | |
1310 | 0, /* @tp_setattro@ */ | |
1311 | 0, /* @tp_as_buffer@ */ | |
1312 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1313 | Py_TPFLAGS_BASETYPE, | |
1314 | ||
1315 | /* @tp_doc@ */ | |
1316 | "Elliptic curve groups.", | |
1317 | ||
1318 | 0, /* @tp_traverse@ */ | |
1319 | 0, /* @tp_clear@ */ | |
1320 | 0, /* @tp_richcompare@ */ | |
1321 | 0, /* @tp_weaklistoffset@ */ | |
1322 | 0, /* @tp_iter@ */ | |
963a6148 | 1323 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1324 | 0, /* @tp_methods@ */ |
1325 | 0, /* @tp_members@ */ | |
1326 | ecgroup_pygetset, /* @tp_getset@ */ | |
1327 | 0, /* @tp_base@ */ | |
1328 | 0, /* @tp_dict@ */ | |
1329 | 0, /* @tp_descr_get@ */ | |
1330 | 0, /* @tp_descr_set@ */ | |
1331 | 0, /* @tp_dictoffset@ */ | |
1332 | 0, /* @tp_init@ */ | |
1333 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1334 | ecgroup_pynew, /* @tp_new@ */ | |
3aa33042 | 1335 | 0, /* @tp_free@ */ |
d7ab1bab | 1336 | 0 /* @tp_is_gc@ */ |
1337 | }; | |
1338 | ||
1339 | /*----- Global stuff ------------------------------------------------------*/ | |
1340 | ||
1341 | static PyMethodDef methods[] = { | |
1342 | #define METHNAME(name) meth_##name | |
1343 | METH (_GE_frombuf, "frombuf(BUF) -> X, REST") | |
1344 | METH (_GE_fromraw, "fromraw(BUF) -> X, REST") | |
1345 | METH (_GE_fromstring, "fromstring(STR) -> X, REST") | |
1346 | METH (_Group_parse, "parse(STR) -> G, REST") | |
1347 | METH (_DHInfo_parse, "parse(STR) -> D, REST") | |
1348 | METH (_BinDHInfo_parse, "parse(STR) -> D, REST") | |
1349 | METH (_DHInfo__groupn, 0) | |
1350 | METH (_BinDHInfo__groupn, 0) | |
1351 | KWMETH(_DHInfo_generate, "\ | |
5d00a630 | 1352 | generate(PBITS, [qbits = 0, event = pgen_nullev,\n\ |
d7ab1bab | 1353 | rng = rand, nsteps = 0]) -> D") |
1354 | KWMETH(_DHInfo_genlimlee, "\ | |
1355 | genlimlee(PBITS, QBITS, [event = pgen_nullev, ievent = pgen_nullev,\n\ | |
1356 | rng = rand, nsteps = 0, subgroupp = True]) -> (D, [Q, ...])") | |
1357 | KWMETH(_DHInfo_gendsa, "\ | |
1358 | gendsa(PBITS, QBITS, SEED, [event = pgen_nullev, nsteps = 0])\n\ | |
1359 | -> (D, SEED, COUNT)") | |
2a28a112 MW |
1360 | KWMETH(_DHInfo_genkcdsa, "\ |
1361 | gendsa(PBITS, QBITS, [event = pgen_nullev, rng = rand, nsteps = 0])\n\ | |
1362 | -> (D, V)") | |
d7ab1bab | 1363 | #undef METHNAME |
1364 | { 0 } | |
1365 | }; | |
1366 | ||
1367 | void group_pyinit(void) | |
1368 | { | |
1369 | INITTYPE(fginfo, root); | |
1370 | INITTYPE(dhinfo, fginfo); | |
1371 | INITTYPE(bindhinfo, dhinfo); | |
1372 | INITTYPE(ge, root); | |
1373 | INITTYPE(group, type); | |
1374 | INITTYPE(primegroup, group); | |
1375 | INITTYPE(bingroup, group); | |
1376 | INITTYPE(ecgroup, group); | |
1377 | addmethods(methods); | |
1378 | } | |
1379 | ||
1380 | void group_pyinsert(PyObject *mod) | |
1381 | { | |
1382 | INSERT("FGInfo", fginfo_pytype); | |
1383 | INSERT("DHInfo", dhinfo_pytype); | |
1384 | INSERT("BinDHInfo", bindhinfo_pytype); | |
1385 | INSERT("GE", ge_pytype); | |
1386 | INSERT("Group", group_pytype); | |
1387 | INSERT("PrimeGroup", primegroup_pytype); | |
1388 | INSERT("BinGroup", bingroup_pytype); | |
1389 | INSERT("ECGroup", ecgroup_pytype); | |
1390 | INSERT("_pgroups", namedgroups(ptab, &npgroups)); | |
1391 | INSERT("_bingroups", namedgroups(bintab, &nbingroups)); | |
1392 | } | |
1393 | ||
1394 | /*----- That's all, folks -------------------------------------------------*/ |