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