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