Commit | Line | Data |
---|---|---|
d7ab1bab | 1 | /* -*-c-*- |
d7ab1bab | 2 | * |
3 | * Elliptic curves | |
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 | /*----- Utility functions -------------------------------------------------*/ | |
32 | ||
33 | PyTypeObject *ecpt_pytype; | |
34 | PyTypeObject *ecptcurve_pytype; | |
35 | PyTypeObject *eccurve_pytype; | |
36 | PyTypeObject *ecprimecurve_pytype; | |
37 | PyTypeObject *ecprimeprojcurve_pytype; | |
38 | PyTypeObject *ecbincurve_pytype; | |
39 | PyTypeObject *ecbinprojcurve_pytype; | |
40 | PyTypeObject *ecinfo_pytype; | |
41 | ||
42 | ec_curve *eccurve_copy(ec_curve *c) | |
43 | { | |
44 | field *f; | |
45 | mp *a, *b; | |
46 | ||
47 | if ((f = field_copy(c->f)) == 0) | |
48 | return (0); | |
49 | a = F_OUT(f, MP_NEW, c->a); | |
50 | b = F_OUT(f, MP_NEW, c->b); | |
51 | if (strcmp(EC_NAME(c), "prime") == 0) | |
52 | c = ec_prime(f, a, b); | |
53 | else if (strcmp(EC_NAME(c), "primeproj") == 0) | |
54 | c = ec_primeproj(f, a, b); | |
55 | else if (strcmp(EC_NAME(c), "bin") == 0) | |
56 | c = ec_bin(f, a, b); | |
57 | else if (strcmp(EC_NAME(c), "binproj") == 0) | |
58 | c = ec_binproj(f, a, b); | |
59 | else | |
60 | c = 0; | |
61 | MP_DROP(a); | |
62 | MP_DROP(b); | |
63 | if (!c) F_DESTROY(f); | |
64 | return (c); | |
65 | } | |
66 | ||
67 | static PyObject *ecpt_dopywrap(PyObject *cobj, ec_curve *c, ec *p) | |
68 | { | |
69 | ecpt_pyobj *z = PyObject_New(ecpt_pyobj, (PyTypeObject *)cobj); | |
70 | z->p = *p; | |
71 | z->c = c; | |
72 | Py_INCREF(cobj); | |
73 | return ((PyObject *)z); | |
74 | } | |
75 | ||
76 | PyObject *ecpt_pywrap(PyObject *cobj, ec *p) | |
77 | { return (ecpt_dopywrap(cobj, ECCURVE_C(cobj), p)); } | |
78 | ||
79 | PyObject *ecpt_pywrapout(void *cobj, ec *p) | |
80 | { | |
81 | ec_curve *c; | |
82 | ||
83 | if (!PyType_IsSubtype(cobj, ecptcurve_pytype)) | |
84 | c = 0; | |
85 | else { | |
86 | c = ECCURVE_C(cobj); | |
87 | EC_IN(ECCURVE_C(cobj), p, p); | |
88 | } | |
89 | return (ecpt_dopywrap(cobj, c, p)); | |
90 | } | |
91 | ||
92 | int toecpt(ec_curve *c, ec *d, PyObject *p) | |
93 | { | |
94 | if (ECPTCURVE_PYCHECK(p)) { | |
95 | if (ECPT_C(p) != c && !ec_samep(ECPT_C(p), c)) | |
96 | return (-1); | |
97 | EC_COPY(d, ECPT_P(p)); | |
98 | } else if (ECPT_PYCHECK(p)) | |
99 | EC_IN(c, d, ECPT_P(p)); | |
100 | else | |
101 | return (-1); | |
102 | return (0); | |
103 | } | |
104 | ||
105 | int getecpt(ec_curve *c, ec *d, PyObject *p) | |
106 | { | |
107 | if (toecpt(c, d, p)) { | |
108 | PyErr_Format(PyExc_TypeError, "can't convert %.100s to ecpt", | |
109 | p->ob_type->tp_name); | |
110 | return (-1); | |
111 | } | |
112 | return (0); | |
113 | } | |
114 | ||
115 | void getecptout(ec *d, PyObject *p) | |
116 | { | |
117 | if (ECPTCURVE_PYCHECK(p)) | |
118 | EC_OUT(ECPT_C(p), d, ECPT_P(p)); | |
119 | else { | |
120 | assert(ECPT_PYCHECK(p)); | |
121 | EC_COPY(d, ECPT_P(p)); | |
122 | } | |
123 | } | |
124 | ||
125 | int convecpt(PyObject *o, void *p) | |
126 | { | |
127 | if (!ECPT_PYCHECK(o)) | |
128 | TYERR("want elliptic curve point"); | |
129 | getecptout(p, o); | |
130 | return (1); | |
131 | end: | |
132 | return (0); | |
133 | } | |
134 | ||
135 | /*----- Curve points ------------------------------------------------------*/ | |
136 | ||
137 | static int ecbinop(PyObject *x, PyObject *y, | |
138 | ec_curve **c, PyObject **cobj, ec *xx, ec *yy) | |
139 | { | |
140 | if (ECPTCURVE_PYCHECK(x)) *cobj = ECPT_COBJ(x); | |
141 | else if (ECPTCURVE_PYCHECK(y)) *cobj = ECPT_COBJ(y); | |
142 | else return (-1); | |
143 | *c = ECCURVE_C(*cobj); | |
144 | if (toecpt(*c, xx, x) || toecpt(*c, yy, y)) return (-1); | |
145 | return (0); | |
146 | } | |
147 | ||
148 | #define BINOP(name) \ | |
149 | static PyObject *ecpt_py##name(PyObject *x, PyObject *y) { \ | |
150 | ec xx = EC_INIT, yy = EC_INIT, zz = EC_INIT; \ | |
151 | PyObject *cobj; \ | |
152 | ec_curve *c; \ | |
153 | if (ecbinop(x, y, &c, &cobj, &xx, &yy)) RETURN_NOTIMPL; \ | |
154 | c->ops->name(c, &zz, &xx, &yy); \ | |
155 | EC_DESTROY(&xx); EC_DESTROY(&yy); \ | |
156 | return (ecpt_pywrap(ECPT_COBJ(x), &zz)); \ | |
157 | } | |
158 | BINOP(add) | |
159 | BINOP(sub) | |
160 | #undef BINOP | |
161 | ||
162 | #define UNOP(name) \ | |
163 | static PyObject *ecpt_py##name(PyObject *x) { \ | |
164 | ec zz = EC_INIT; \ | |
165 | ec_curve *c = ECPT_C(x); \ | |
166 | c->ops->name(c, &zz, ECPT_P(x)); \ | |
167 | return (ecpt_pywrap(ECPT_COBJ(x), &zz)); \ | |
168 | } | |
169 | UNOP(neg) | |
170 | #undef UNOP | |
171 | ||
172 | static PyObject *ecpt_pyid(PyObject *x) { RETURN_OBJ(x); } | |
173 | ||
174 | static int ecpt_pynonzerop(PyObject *x) { return (!EC_ATINF(ECPT_P(x))); } | |
175 | ||
176 | static void ecpt_pydealloc(PyObject *x) | |
177 | { | |
178 | EC_DESTROY(ECPT_P(x)); | |
179 | Py_DECREF(ECPT_COBJ(x)); | |
3aa33042 | 180 | FREEOBJ(x); |
d7ab1bab | 181 | } |
182 | ||
183 | static PyObject *ecpt_pymul(PyObject *x, PyObject *y) | |
184 | { | |
185 | mp *xx; | |
186 | ec zz = EC_INIT; | |
187 | ||
188 | if (ECPT_PYCHECK(x)) { PyObject *t; t = x; x = y; y = t; } | |
189 | if (!ECPT_PYCHECK(y) || (xx = tomp(x)) == 0) RETURN_NOTIMPL; | |
190 | ec_imul(ECPT_C(y), &zz, ECPT_P(y), xx); | |
191 | return (ecpt_pywrap(ECPT_COBJ(y), &zz)); | |
192 | } | |
193 | ||
194 | static long ecpt_pyhash(PyObject *me) | |
195 | { | |
6d481bc6 MW |
196 | uint32 h; |
197 | buf b; | |
d7ab1bab | 198 | ec p = EC_INIT; |
6d481bc6 MW |
199 | size_t sz = 2*ECPT_C(me)->f->noctets + 1; |
200 | octet *q = xmalloc(sz); | |
d7ab1bab | 201 | |
6d481bc6 MW |
202 | h = 0xe0fdd039 + ECPT_C(me)->f->ops->ty; |
203 | buf_init(&b, q, sz); | |
d7ab1bab | 204 | EC_OUT(ECPT_C(me), &p, ECPT_P(me)); |
6d481bc6 | 205 | ec_putraw(ECPT_C(me), &b, &p); |
d7ab1bab | 206 | EC_DESTROY(&p); |
6d481bc6 MW |
207 | xfree(q); |
208 | h = unihash_hash(&unihash_global, h, BBASE(&b), BLEN(&b)); | |
209 | return (h % LONG_MAX); | |
d7ab1bab | 210 | } |
211 | ||
212 | static PyObject *ecpt_pyrichcompare(PyObject *x, PyObject *y, int op) | |
213 | { | |
214 | ec_curve *c; | |
215 | PyObject *cobj; | |
216 | ec p = EC_INIT, q = EC_INIT; | |
217 | int b; | |
218 | PyObject *rc = 0; | |
219 | ||
220 | if (ecbinop(x, y, &c, &cobj, &p, &q)) RETURN_NOTIMPL; | |
221 | EC_OUT(c, &p, &p); | |
222 | EC_OUT(c, &q, &q); | |
223 | switch (op) { | |
224 | case Py_EQ: b = EC_EQ(&p, &q); break; | |
225 | case Py_NE: b = !EC_EQ(&p, &q); break; | |
226 | default: TYERR("elliptic curve points are unordered"); | |
227 | } | |
228 | rc = getbool(b); | |
229 | end: | |
230 | EC_DESTROY(&p); | |
231 | EC_DESTROY(&q); | |
232 | return (rc); | |
233 | } | |
234 | ||
235 | static PyObject *epmeth_oncurvep(PyObject *me, PyObject *arg) | |
236 | { | |
237 | if (!PyArg_ParseTuple(arg, ":oncurvep")) return (0); | |
49915b4a MW |
238 | return (getbool(EC_ATINF(ECPT_P(me)) || |
239 | !EC_CHECK(ECPT_C(me), ECPT_P(me)))); | |
d7ab1bab | 240 | } |
241 | ||
242 | static PyObject *epmeth_dbl(PyObject *me, PyObject *arg) | |
243 | { | |
244 | ec p = EC_INIT; | |
245 | if (!PyArg_ParseTuple(arg, ":dbl")) return (0); | |
246 | EC_DBL(ECPT_C(me), &p, ECPT_P(me)); | |
247 | return (ecpt_pywrap(ECPT_COBJ(me), &p)); | |
248 | } | |
249 | ||
250 | static PyObject *epmeth_tobuf(PyObject *me, PyObject *arg) | |
251 | { | |
252 | buf b; | |
253 | ec p = EC_INIT; | |
254 | PyObject *rc; | |
255 | size_t n; | |
256 | ||
257 | if (!PyArg_ParseTuple(arg, ":tobuf")) return (0); | |
258 | getecptout(&p, me); | |
259 | if (EC_ATINF(&p)) | |
260 | n = 2; | |
261 | else | |
262 | n = mp_octets(p.x) + mp_octets(p.y) + 4; | |
263 | rc = bytestring_pywrap(0, n); | |
264 | buf_init(&b, PyString_AS_STRING(rc), n); | |
265 | buf_putec(&b, &p); | |
266 | assert(BOK(&b)); | |
267 | _PyString_Resize(&rc, BLEN(&b)); | |
268 | EC_DESTROY(&p); | |
269 | return (rc); | |
270 | } | |
271 | ||
272 | static PyObject *epmeth_toraw(PyObject *me, PyObject *arg) | |
273 | { | |
274 | buf b; | |
275 | PyObject *rc; | |
276 | char *p; | |
277 | ec_curve *c = ECPT_C(me); | |
278 | ec pp = EC_INIT; | |
279 | int len; | |
280 | ||
281 | if (!PyArg_ParseTuple(arg, ":toraw")) return (0); | |
282 | len = c->f->noctets * 2 + 1; | |
283 | rc = bytestring_pywrap(0, len); | |
284 | p = PyString_AS_STRING(rc); | |
285 | buf_init(&b, p, len); | |
286 | EC_OUT(c, &pp, ECPT_P(me)); | |
287 | ec_putraw(c, &b, &pp); | |
288 | EC_DESTROY(&pp); | |
289 | _PyString_Resize(&rc, BLEN(&b)); | |
290 | return (rc); | |
291 | } | |
292 | ||
293 | static PyObject *epget_curve(PyObject *me, void *hunoz) | |
294 | { RETURN_OBJ(ECPT_COBJ(me)); } | |
295 | ||
296 | static PyObject *epncget_ix(PyObject *me, void *hunoz) | |
297 | { | |
298 | ec p = EC_INIT; | |
299 | PyObject *rc; | |
300 | if (EC_ATINF(ECPT_P(me))) RETURN_NONE; | |
301 | getecptout(&p, me); | |
302 | rc = mp_pywrap(MP_COPY(p.x)); | |
303 | EC_DESTROY(&p); | |
304 | return (rc); | |
305 | } | |
306 | ||
307 | static PyObject *epncget_iy(PyObject *me, void *hunoz) | |
308 | { | |
309 | ec p = EC_INIT; | |
310 | PyObject *rc; | |
311 | if (EC_ATINF(ECPT_P(me))) RETURN_NONE; | |
312 | getecptout(&p, me); | |
313 | rc = mp_pywrap(MP_COPY(p.y)); | |
314 | EC_DESTROY(&p); | |
315 | return (rc); | |
316 | } | |
317 | ||
318 | static PyObject *epncget_point(PyObject *me, void *hunoz) | |
319 | { RETURN_ME; } | |
320 | ||
321 | static PyObject *epget_point(PyObject *me, void *hunoz) | |
322 | { | |
323 | ec p = EC_INIT; | |
324 | getecptout(&p, me); | |
325 | return (ecpt_pywrapout(ecpt_pytype, &p)); | |
326 | } | |
327 | ||
328 | static PyObject *epget_x(PyObject *me, void *hunoz) | |
329 | { | |
330 | ec_curve *c = ECPT_C(me); | |
331 | ec *pp = ECPT_P(me); | |
332 | PyObject *fobj = ECPT_FOBJ(me); | |
333 | ec p = EC_INIT; | |
334 | PyObject *rc; | |
335 | ||
336 | if (EC_ATINF(pp)) RETURN_NONE; | |
337 | EC_OUT(c, &p, pp); | |
338 | rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.x)); | |
339 | EC_DESTROY(&p); | |
340 | return (rc); | |
341 | } | |
342 | ||
343 | static PyObject *epget_y(PyObject *me, void *hunoz) | |
344 | { | |
345 | ec_curve *c = ECPT_C(me); | |
346 | ec *pp = ECPT_P(me); | |
347 | PyObject *fobj = ECPT_FOBJ(me); | |
348 | ec p = EC_INIT; | |
349 | PyObject *rc; | |
350 | ||
351 | if (EC_ATINF(pp)) RETURN_NONE; | |
352 | EC_OUT(c, &p, pp); | |
353 | rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.y)); | |
354 | EC_DESTROY(&p); | |
355 | return (rc); | |
356 | } | |
357 | ||
358 | static PyObject *epget__x(PyObject *me, void *hunoz) | |
359 | { | |
360 | if (EC_ATINF(ECPT_P(me))) RETURN_NONE; | |
361 | return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->x))); | |
362 | } | |
363 | ||
364 | static PyObject *epget__y(PyObject *me, void *hunoz) | |
365 | { | |
366 | if (EC_ATINF(ECPT_P(me))) RETURN_NONE; | |
367 | return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->y))); | |
368 | } | |
369 | ||
370 | static PyObject *epget__z(PyObject *me, void *hunoz) | |
371 | { | |
372 | if (EC_ATINF(ECPT_P(me)) || !ECPT_P(me)->z) RETURN_NONE; | |
373 | return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->z))); | |
374 | } | |
375 | ||
376 | static mp *coord_in(field *f, PyObject *x) | |
377 | { | |
378 | mp *xx; | |
379 | if (FE_PYCHECK(x) && FE_F(x) == f) | |
380 | return (MP_COPY(FE_X(x))); | |
381 | else if ((xx = getmp(x)) == 0) | |
382 | return (0); | |
383 | else | |
384 | return (F_IN(f, xx, xx)); | |
385 | } | |
386 | ||
387 | static int ecptxl_3(ec_curve *c, ec *p, | |
388 | PyObject *x, PyObject *y, PyObject *z) | |
389 | { | |
390 | int rc = -1; | |
391 | ||
392 | if (!x || !y || !z) TYERR("missing argument"); | |
393 | if (!c) VALERR("internal form with no curve!"); | |
394 | if ((p->x == coord_in(c->f, x)) == 0 || | |
395 | (p->y == coord_in(c->f, y)) == 0 || | |
396 | (z != Py_None && (p->z = coord_in(c->f, z))) == 0) | |
397 | goto end; | |
398 | if (!p->z) p->z = MP_COPY(c->f->one); /* just in case */ | |
399 | rc = 0; | |
400 | end: | |
401 | return (rc); | |
402 | } | |
403 | ||
404 | static int ecptxl_2(ec_curve *c, ec *p, PyObject *x, PyObject *y) | |
405 | { | |
406 | int rc = -1; | |
407 | ||
408 | if (!x || !y) TYERR("missing argument"); | |
409 | if ((p->x = getmp(x)) == 0 || | |
410 | (p->y = getmp(y)) == 0) | |
411 | goto end; | |
412 | if (c) EC_IN(c, p, p); | |
413 | rc = 0; | |
414 | end: | |
415 | return (rc); | |
416 | } | |
417 | ||
418 | static int ecptxl_1(ec_curve *c, ec *p, PyObject *x) | |
419 | { | |
420 | int rc = -1; | |
421 | PyObject *y = 0, *z = 0, *t = 0; | |
422 | mp *xx = 0; | |
423 | const void *q; | |
424 | int n; | |
425 | qd_parse qd; | |
426 | ||
427 | Py_XINCREF(x); | |
428 | if (!x || x == Py_None) | |
429 | /*cool*/; | |
430 | else if (ECPT_PYCHECK(x)) { | |
431 | getecptout(p, x); | |
432 | goto fix; | |
433 | } else if (PyString_Check(x)) { | |
434 | if (PyObject_AsReadBuffer(x, &q, 0)) | |
435 | goto end; | |
436 | qd.p = q; | |
437 | qd.e = 0; | |
438 | if (!ec_ptparse(&qd, p)) | |
80f7cd89 | 439 | VALERR(qd.e); |
d7ab1bab | 440 | goto fix; |
441 | } else if (c && (xx = tomp(x)) != 0) { | |
442 | xx = F_IN(c->f, xx, xx); | |
443 | if (!EC_FIND(c, p, xx)) VALERR("not on the curve"); | |
444 | } else if (PySequence_Check(x)) { | |
445 | t = x; x = 0; | |
446 | n = PySequence_Size(t); | |
447 | if (n != 2 && (n != 3 || !c)) | |
448 | TYERR("want sequence of two or three items"); | |
449 | if ((x = PySequence_GetItem(t, 0)) == 0 || | |
450 | (y = PySequence_GetItem(t, 1)) == 0 || | |
451 | (n == 3 && (z = PySequence_GetItem(t, 2)) == 0)) | |
452 | goto end; | |
453 | rc = (n == 2) ? ecptxl_2(c, p, x, y) : ecptxl_3(c, p, x, y, z); | |
454 | } else | |
455 | TYERR("can't convert to curve point"); | |
456 | goto ok; | |
457 | ||
458 | fix: | |
459 | if (c) EC_IN(c, p, p); | |
460 | ok: | |
461 | rc = 0; | |
462 | end: | |
463 | Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(z); Py_XDECREF(t); | |
464 | mp_drop(xx); | |
465 | return (rc); | |
466 | } | |
467 | ||
468 | static int ecptxl(ec_curve *c, ec *p, PyObject *x, PyObject *y, PyObject *z) | |
469 | { | |
470 | if (z) | |
471 | return (ecptxl_3(c, p, x, y, z)); | |
472 | else if (y) | |
473 | return (ecptxl_2(c, p, x, y)); | |
474 | else | |
475 | return (ecptxl_1(c, p, x)); | |
476 | } | |
477 | ||
d7ab1bab | 478 | static PyObject *ecptnc_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
479 | { | |
480 | PyObject *x = 0, *y = 0, *z = 0; | |
481 | ec p = EC_INIT; | |
482 | char *kwlist[] = { "x", "y", 0 }; | |
483 | ||
484 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:new", kwlist, &x, &y) || | |
485 | ecptxl(0, &p, x, y, z)) | |
486 | goto end; | |
487 | return (ecpt_pywrapout(ty, &p)); | |
488 | end: | |
489 | EC_DESTROY(&p); | |
490 | return (0); | |
491 | } | |
492 | ||
493 | static PyObject *ecpt_pyint(PyObject *me) | |
494 | { | |
495 | ec p = EC_INIT; | |
496 | long l; | |
497 | PyObject *rc = 0; | |
498 | if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity"); | |
499 | getecptout(&p, me); | |
500 | if (mp_tolong_checked(p.x, &l)) goto end; | |
501 | rc = PyInt_FromLong(l); | |
502 | end: | |
503 | EC_DESTROY(&p); | |
504 | return (rc); | |
505 | } | |
506 | ||
507 | static PyObject *ecpt_pylong(PyObject *me) | |
508 | { | |
509 | ec p = EC_INIT; | |
510 | PyObject *rc = 0; | |
511 | if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity"); | |
512 | getecptout(&p, me); | |
f368b46e | 513 | rc = mp_topylong(p.x); |
d7ab1bab | 514 | end: |
515 | EC_DESTROY(&p); | |
516 | return (rc); | |
517 | } | |
518 | ||
519 | static PyObject *ecpt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
520 | { | |
521 | PyObject *x = 0, *y = 0, *z = 0; | |
522 | ec p = EC_INIT; | |
523 | char *kwlist[] = { "x", "y", "z", 0 }; | |
524 | ||
525 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OOO:new", kwlist, | |
526 | &x, &y, &z) || | |
527 | ecptxl(ECCURVE_C(ty), &p, x, y, z)) | |
528 | goto end; | |
529 | return (ecpt_pywrap((PyObject *)ty, &p)); | |
530 | end: | |
531 | EC_DESTROY(&p); | |
532 | return (0); | |
533 | } | |
534 | ||
535 | static PyGetSetDef ecptnc_pygetset[] = { | |
536 | #define GETSETNAME(op, name) epnc##op##_##name | |
537 | GET (ix, "P.ix -> integer x coordinate of P") | |
538 | GET (iy, "P.iy -> integer y coordinate of P") | |
539 | GET (point, "P.point -> standalone curve point (no-op)") | |
540 | #undef GETSETNAME | |
541 | { 0 } | |
542 | }; | |
543 | ||
544 | static PyMethodDef ecptnc_pymethods[] = { | |
545 | #define METHNAME(func) epmeth_##func | |
546 | METH (tobuf, "X.tobuf() -> BIN") | |
547 | #undef METHNAME | |
548 | { 0 } | |
549 | }; | |
550 | ||
551 | static PyNumberMethods ecpt_pynumber = { | |
552 | 0, /* @nb_add@ */ | |
553 | 0, /* @nb_subtract@ */ | |
554 | 0, /* @nb_multiply@ */ | |
555 | 0, /* @nb_divide@ */ | |
556 | 0, /* @nb_remainder@ */ | |
557 | 0, /* @nb_divmod@ */ | |
558 | 0, /* @nb_power@ */ | |
559 | 0, /* @nb_negative@ */ | |
560 | 0, /* @nb_positive@ */ | |
561 | 0, /* @nb_absolute@ */ | |
562 | ecpt_pynonzerop, /* @nb_nonzero@ */ | |
563 | 0, /* @nb_invert@ */ | |
564 | 0, /* @nb_lshift@ */ | |
565 | 0, /* @nb_rshift@ */ | |
566 | 0, /* @nb_and@ */ | |
567 | 0, /* @nb_xor@ */ | |
568 | 0, /* @nb_or@ */ | |
569 | 0, /* @nb_coerce@ */ | |
570 | ecpt_pyint, /* @nb_int@ */ | |
571 | ecpt_pylong, /* @nb_long@ */ | |
572 | 0, /* @nb_float@ */ | |
573 | 0, /* @nb_oct@ */ | |
574 | 0, /* @nb_hex@ */ | |
575 | ||
576 | 0, /* @nb_inplace_add@ */ | |
577 | 0, /* @nb_inplace_subtract@ */ | |
578 | 0, /* @nb_inplace_multiply@ */ | |
579 | 0, /* @nb_inplace_divide@ */ | |
580 | 0, /* @nb_inplace_remainder@ */ | |
581 | 0, /* @nb_inplace_power@ */ | |
582 | 0, /* @nb_inplace_lshift@ */ | |
583 | 0, /* @nb_inplace_rshift@ */ | |
584 | 0, /* @nb_inplace_and@ */ | |
585 | 0, /* @nb_inplace_xor@ */ | |
586 | 0, /* @nb_inplace_or@ */ | |
587 | ||
588 | 0, /* @nb_floor_divide@ */ | |
589 | 0, /* @nb_true_divide@ */ | |
590 | 0, /* @nb_inplace_floor_divide@ */ | |
591 | 0, /* @nb_inplace_true_divide@ */ | |
592 | }; | |
593 | ||
594 | static PyTypeObject ecpt_pytype_skel = { | |
6d4db0bf | 595 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 596 | "ECPt", /* @tp_name@ */ |
d7ab1bab | 597 | sizeof(ecpt_pyobj), /* @tp_basicsize@ */ |
598 | 0, /* @tp_itemsize@ */ | |
599 | ||
600 | ecpt_pydealloc, /* @tp_dealloc@ */ | |
601 | 0, /* @tp_print@ */ | |
602 | 0, /* @tp_getattr@ */ | |
603 | 0, /* @tp_setattr@ */ | |
604 | 0, /* @tp_compare@ */ | |
605 | 0, /* @tp_repr@ */ | |
606 | &ecpt_pynumber, /* @tp_as_number@ */ | |
607 | 0, /* @tp_as_sequence@ */ | |
608 | 0, /* @tp_as_mapping@ */ | |
609 | ecpt_pyhash, /* @tp_hash@ */ | |
610 | 0, /* @tp_call@ */ | |
611 | 0, /* @tp_str@ */ | |
612 | 0, /* @tp_getattro@ */ | |
613 | 0, /* @tp_setattro@ */ | |
614 | 0, /* @tp_as_buffer@ */ | |
615 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
616 | Py_TPFLAGS_CHECKTYPES | | |
617 | Py_TPFLAGS_BASETYPE, | |
618 | ||
619 | /* @tp_doc@ */ | |
620 | "Elliptic curve points, not associated with any curve.", | |
621 | ||
622 | 0, /* @tp_traverse@ */ | |
623 | 0, /* @tp_clear@ */ | |
624 | ecpt_pyrichcompare, /* @tp_richcompare@ */ | |
625 | 0, /* @tp_weaklistoffset@ */ | |
626 | 0, /* @tp_iter@ */ | |
963a6148 | 627 | 0, /* @tp_iternext@ */ |
d7ab1bab | 628 | ecptnc_pymethods, /* @tp_methods@ */ |
629 | 0, /* @tp_members@ */ | |
630 | ecptnc_pygetset, /* @tp_getset@ */ | |
631 | 0, /* @tp_base@ */ | |
632 | 0, /* @tp_dict@ */ | |
633 | 0, /* @tp_descr_get@ */ | |
634 | 0, /* @tp_descr_set@ */ | |
635 | 0, /* @tp_dictoffset@ */ | |
636 | 0, /* @tp_init@ */ | |
637 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
638 | ecptnc_pynew, /* @tp_new@ */ | |
3aa33042 | 639 | 0, /* @tp_free@ */ |
d7ab1bab | 640 | 0 /* @tp_is_gc@ */ |
641 | }; | |
642 | ||
643 | static PyGetSetDef ecpt_pygetset[] = { | |
644 | #define GETSETNAME(op, name) ep##op##_##name | |
645 | GET (curve, "P.curve -> elliptic curve containing P") | |
646 | GET (point, "P.point -> standalone curve point") | |
647 | GET (x, "P.x -> Cartesian x coordinate of P") | |
648 | GET (y, "P.y -> Cartesian y coordinate of P") | |
649 | GET (_x, "P._x -> internal x coordinate of P") | |
650 | GET (_y, "P._y -> internal y coordinate of P") | |
651 | GET (_z, "P._z -> internal z coordinate of P, or None") | |
652 | #undef GETSETNAME | |
653 | { 0 } | |
654 | }; | |
655 | ||
656 | static PyMethodDef ecpt_pymethods[] = { | |
657 | #define METHNAME(func) epmeth_##func | |
658 | METH (toraw, "X.toraw() -> BIN") | |
659 | METH (dbl, "X.dbl() -> X + X") | |
3aa33042 | 660 | METH (oncurvep, "X.oncurvep() -> BOOL") |
d7ab1bab | 661 | #undef METHNAME |
662 | { 0 } | |
663 | }; | |
664 | ||
665 | static PyNumberMethods ecptcurve_pynumber = { | |
666 | ecpt_pyadd, /* @nb_add@ */ | |
667 | ecpt_pysub, /* @nb_subtract@ */ | |
668 | ecpt_pymul, /* @nb_multiply@ */ | |
669 | 0, /* @nb_divide@ */ | |
670 | 0, /* @nb_remainder@ */ | |
671 | 0, /* @nb_divmod@ */ | |
672 | 0, /* @nb_power@ */ | |
673 | ecpt_pyneg, /* @nb_negative@ */ | |
674 | ecpt_pyid, /* @nb_positive@ */ | |
675 | 0, /* @nb_absolute@ */ | |
676 | 0, /* @nb_nonzero@ */ | |
677 | 0, /* @nb_invert@ */ | |
678 | 0, /* @nb_lshift@ */ | |
679 | 0, /* @nb_rshift@ */ | |
680 | 0, /* @nb_and@ */ | |
681 | 0, /* @nb_xor@ */ | |
682 | 0, /* @nb_or@ */ | |
683 | 0, /* @nb_coerce@ */ | |
684 | 0, /* @nb_int@ */ | |
685 | 0, /* @nb_long@ */ | |
686 | 0, /* @nb_float@ */ | |
687 | 0, /* @nb_oct@ */ | |
688 | 0, /* @nb_hex@ */ | |
689 | ||
690 | 0, /* @nb_inplace_add@ */ | |
691 | 0, /* @nb_inplace_subtract@ */ | |
692 | 0, /* @nb_inplace_multiply@ */ | |
693 | 0, /* @nb_inplace_divide@ */ | |
694 | 0, /* @nb_inplace_remainder@ */ | |
695 | 0, /* @nb_inplace_power@ */ | |
696 | 0, /* @nb_inplace_lshift@ */ | |
697 | 0, /* @nb_inplace_rshift@ */ | |
698 | 0, /* @nb_inplace_and@ */ | |
699 | 0, /* @nb_inplace_xor@ */ | |
700 | 0, /* @nb_inplace_or@ */ | |
701 | ||
702 | 0, /* @nb_floor_divide@ */ | |
703 | 0, /* @nb_true_divide@ */ | |
704 | 0, /* @nb_inplace_floor_divide@ */ | |
705 | 0, /* @nb_inplace_true_divide@ */ | |
706 | }; | |
707 | ||
708 | static PyTypeObject ecptcurve_pytype_skel = { | |
6d4db0bf | 709 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 710 | "ECPtCurve", /* @tp_name@ */ |
d7ab1bab | 711 | sizeof(ecpt_pyobj), /* @tp_basicsize@ */ |
712 | 0, /* @tp_itemsize@ */ | |
713 | ||
714 | ecpt_pydealloc, /* @tp_dealloc@ */ | |
715 | 0, /* @tp_print@ */ | |
716 | 0, /* @tp_getattr@ */ | |
717 | 0, /* @tp_setattr@ */ | |
718 | 0, /* @tp_compare@ */ | |
719 | 0, /* @tp_repr@ */ | |
720 | &ecptcurve_pynumber, /* @tp_as_number@ */ | |
721 | 0, /* @tp_as_sequence@ */ | |
722 | 0, /* @tp_as_mapping@ */ | |
723 | 0, /* @tp_hash@ */ | |
724 | 0, /* @tp_call@ */ | |
725 | 0, /* @tp_str@ */ | |
726 | 0, /* @tp_getattro@ */ | |
727 | 0, /* @tp_setattro@ */ | |
728 | 0, /* @tp_as_buffer@ */ | |
729 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
730 | Py_TPFLAGS_CHECKTYPES | | |
731 | Py_TPFLAGS_BASETYPE, | |
732 | ||
733 | /* @tp_doc@ */ | |
734 | "Elliptic curve points; abstract base class for points on given curves.", | |
735 | ||
736 | 0, /* @tp_traverse@ */ | |
737 | 0, /* @tp_clear@ */ | |
738 | 0, /* @tp_richcompare@ */ | |
739 | 0, /* @tp_weaklistoffset@ */ | |
740 | 0, /* @tp_iter@ */ | |
963a6148 | 741 | 0, /* @tp_iternext@ */ |
d7ab1bab | 742 | ecpt_pymethods, /* @tp_methods@ */ |
743 | 0, /* @tp_members@ */ | |
744 | ecpt_pygetset, /* @tp_getset@ */ | |
745 | 0, /* @tp_base@ */ | |
746 | 0, /* @tp_dict@ */ | |
747 | 0, /* @tp_descr_get@ */ | |
748 | 0, /* @tp_descr_set@ */ | |
749 | 0, /* @tp_dictoffset@ */ | |
750 | 0, /* @tp_init@ */ | |
751 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
752 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 753 | 0, /* @tp_free@ */ |
d7ab1bab | 754 | 0 /* @tp_is_gc@ */ |
755 | }; | |
756 | ||
757 | /*----- Elliptic curves themselves ----------------------------------------*/ | |
758 | ||
759 | static PyObject *eccurve_pyrichcompare(PyObject *x, PyObject *y, int op) | |
760 | { | |
761 | int b = ec_samep(ECCURVE_C(x), ECCURVE_C(y)); | |
762 | switch (op) { | |
763 | case Py_EQ: break; | |
764 | case Py_NE: b = !b; | |
765 | default: TYERR("can't order elliptic curves"); | |
766 | } | |
767 | return (getbool(b)); | |
768 | end: | |
769 | return (0); | |
770 | } | |
771 | ||
772 | static PyObject *ecmmul_id(PyObject *me) | |
773 | { ec p = EC_INIT; return (ecpt_pywrap(me, &p)); } | |
774 | ||
775 | static int ecmmul_fill(void *pp, PyObject *me, PyObject *x, PyObject *m) | |
776 | { | |
777 | ec_mulfactor *f = pp; | |
778 | ||
50bff227 | 779 | EC_CREATE(&f->base); |
d7ab1bab | 780 | if (getecpt(ECCURVE_C(me), &f->base, x) || |
781 | (f->exp = getmp(m)) == 0) | |
782 | return (-1); | |
d7ab1bab | 783 | return (0); |
784 | } | |
785 | ||
786 | static PyObject *ecmmul_exp(PyObject *me, void *pp, int n) | |
787 | { | |
788 | ec p = EC_INIT; | |
789 | ec_immul(ECCURVE_C(me), &p, pp, n); | |
790 | return (ecpt_pywrap(me, &p)); | |
791 | } | |
792 | ||
793 | static void ecmmul_drop(void *pp) | |
794 | { | |
795 | ec_mulfactor *f = pp; | |
796 | EC_DESTROY(&f->base); | |
797 | MP_DROP(f->exp); | |
798 | } | |
799 | ||
800 | static PyObject *ecmeth_mmul(PyObject *me, PyObject *arg) | |
801 | { | |
802 | return (mexp_common(me, arg, sizeof(ec_mulfactor), | |
803 | ecmmul_id, ecmmul_fill, ecmmul_exp, ecmmul_drop)); | |
804 | } | |
805 | ||
806 | static PyObject *meth__ECPtCurve_fromraw(PyObject *me, PyObject *arg) | |
807 | { | |
808 | char *p; | |
809 | int len; | |
810 | buf b; | |
811 | PyObject *rc = 0; | |
812 | ec_curve *cc; | |
813 | ec pp = EC_INIT; | |
814 | ||
815 | if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &len)) | |
816 | return (0); | |
817 | buf_init(&b, p, len); | |
818 | cc = ECCURVE_C(me); | |
819 | if (ec_getraw(cc, &b, &pp)) | |
80f7cd89 | 820 | VALERR("bad point"); |
d7ab1bab | 821 | EC_IN(cc, &pp, &pp); |
822 | rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b)); | |
823 | end: | |
824 | return (rc); | |
825 | } | |
826 | ||
827 | static PyObject *meth__ECPt_frombuf(PyObject *me, PyObject *arg) | |
828 | { | |
829 | buf b; | |
830 | char *p; | |
831 | int sz; | |
832 | PyObject *rc = 0; | |
833 | ec pp = EC_INIT; | |
834 | ||
835 | if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end; | |
836 | buf_init(&b, p, sz); | |
837 | if (buf_getec(&b, &pp)) VALERR("malformed data"); | |
838 | rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp), | |
839 | bytestring_pywrapbuf(&b)); | |
840 | end: | |
841 | return (rc); | |
842 | } | |
843 | ||
844 | static PyObject *meth__ECPt_parse(PyObject *me, PyObject *arg) | |
845 | { | |
846 | char *p; | |
847 | qd_parse qd; | |
848 | PyObject *rc = 0; | |
849 | ec pp = EC_INIT; | |
850 | ||
851 | if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; | |
852 | qd.p = p; | |
853 | qd.e = 0; | |
80f7cd89 | 854 | if (!ec_ptparse(&qd, &pp)) VALERR(qd.e); |
d7ab1bab | 855 | rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p); |
856 | end: | |
857 | return (rc); | |
858 | } | |
859 | ||
860 | static void eccurve_pydealloc(PyObject *me) | |
861 | { | |
862 | ec_destroycurve(ECCURVE_C(me)); | |
863 | Py_DECREF(ECCURVE_FOBJ(me)); | |
864 | PyType_Type.tp_dealloc(me); | |
865 | } | |
866 | ||
867 | static PyObject *ecmeth_find(PyObject *me, PyObject *arg) | |
868 | { | |
869 | PyObject *x; | |
870 | mp *xx = 0; | |
871 | ec p = EC_INIT; | |
872 | PyObject *rc = 0; | |
873 | ||
874 | if (!PyArg_ParseTuple(arg, "O:find", &x)) goto end; | |
875 | if (FIELD_PYCHECK(x) && FE_F(x) == ECCURVE_C(me)->f) | |
876 | xx = MP_COPY(FE_X(x)); | |
877 | else if ((xx = getmp(x)) == 0) | |
878 | goto end; | |
879 | else | |
880 | xx = F_IN(ECCURVE_C(me)->f, xx, xx); | |
881 | if (EC_FIND(ECCURVE_C(me), &p, xx) == 0) | |
882 | VALERR("not on the curve"); | |
883 | rc = ecpt_pywrap(me, &p); | |
884 | end: | |
885 | if (xx) MP_DROP(xx); | |
886 | return (rc); | |
887 | } | |
888 | ||
889 | static PyObject *ecmeth_rand(PyObject *me, PyObject *arg, PyObject *kw) | |
890 | { | |
891 | char *kwlist[] = { "rng", 0 }; | |
892 | grand *r = &rand_global; | |
893 | ec p = EC_INIT; | |
894 | ||
895 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", kwlist, | |
896 | convgrand, &r)) | |
897 | return (0); | |
898 | ec_rand(ECCURVE_C(me), &p, r); | |
899 | EC_IN(ECCURVE_C(me), &p, &p); | |
900 | return (ecpt_pywrap(me, &p)); | |
901 | } | |
902 | ||
903 | static PyObject *eccurve_dopywrap(PyTypeObject *ty, | |
904 | PyObject *fobj, ec_curve *c) | |
905 | { | |
df9f8366 | 906 | eccurve_pyobj *cobj = newtype(ty, 0, c->ops->name); |
d7ab1bab | 907 | cobj->c = c; |
908 | cobj->fobj = fobj; | |
24b3d57b MW |
909 | cobj->ty.ht_type.tp_basicsize = sizeof(ecpt_pyobj); |
910 | cobj->ty.ht_type.tp_base = ecptcurve_pytype; | |
d7ab1bab | 911 | Py_INCREF(ecptcurve_pytype); |
24b3d57b MW |
912 | cobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
913 | Py_TPFLAGS_BASETYPE | | |
914 | Py_TPFLAGS_CHECKTYPES | | |
915 | Py_TPFLAGS_HEAPTYPE); | |
916 | cobj->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
917 | cobj->ty.ht_type.tp_free = 0; | |
918 | cobj->ty.ht_type.tp_new = ecpt_pynew; | |
dc075750 | 919 | typeready(&cobj->ty.ht_type); |
d7ab1bab | 920 | return ((PyObject *)cobj); |
921 | } | |
922 | ||
923 | PyObject *eccurve_pywrap(PyObject *fobj, ec_curve *c) | |
924 | { | |
925 | PyTypeObject *ty; | |
926 | ||
927 | if (!fobj) | |
928 | fobj = field_pywrap(c->f); | |
929 | else | |
930 | Py_INCREF(fobj); | |
931 | assert(FIELD_F(fobj) == c->f); | |
932 | if (strcmp(EC_NAME(c), "prime") == 0) | |
933 | ty = ecprimecurve_pytype; | |
934 | else if (strcmp(EC_NAME(c), "primeproj") == 0) | |
935 | ty = ecprimeprojcurve_pytype; | |
936 | else if (strcmp(EC_NAME(c), "bin") == 0) | |
937 | ty = ecbincurve_pytype; | |
938 | else if (strcmp(EC_NAME(c), "binproj") == 0) | |
939 | ty = ecbinprojcurve_pytype; | |
940 | else | |
941 | abort(); | |
942 | return (eccurve_dopywrap(ty, fobj, c)); | |
943 | } | |
944 | ||
945 | static PyObject *eccurve_pynew(PyTypeObject *ty, | |
946 | ec_curve *(*make)(field *, mp *, mp *), | |
947 | PyObject *arg, PyObject *kw) | |
948 | { | |
949 | PyObject *fobj; | |
950 | PyObject *cobj = 0; | |
951 | char *kwlist[] = { "field", "a", "b", 0 }; | |
952 | mp *aa = 0, *bb = 0; | |
953 | ||
5f959e50 | 954 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&O&", kwlist, |
d7ab1bab | 955 | field_pytype, &fobj, |
956 | convmp, &aa, convmp, &bb)) | |
957 | goto end; | |
958 | Py_INCREF(fobj); | |
959 | cobj = eccurve_dopywrap(ty, fobj, make(FIELD_F(fobj), aa, bb)); | |
960 | end: | |
961 | if (aa) MP_DROP(aa); | |
962 | if (bb) MP_DROP(bb); | |
963 | return (cobj); | |
964 | } | |
965 | ||
966 | static PyObject *meth__ECCurve_parse(PyObject *me, PyObject *arg) | |
967 | { | |
968 | char *p; | |
969 | qd_parse qd; | |
970 | ec_curve *c; | |
971 | PyObject *rc = 0; | |
972 | ||
46e6ad89 | 973 | if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) |
d7ab1bab | 974 | goto end; |
975 | qd.p = p; | |
976 | qd.e = 0; | |
977 | if ((c = ec_curveparse(&qd)) == 0) | |
80f7cd89 | 978 | VALERR(qd.e); |
d7ab1bab | 979 | rc = eccurve_pywrap(0, c); |
980 | end: | |
981 | return (rc); | |
982 | } | |
983 | ||
984 | static PyObject *ecget_name(PyObject *me, void *hunoz) | |
985 | { return (PyString_FromString(EC_NAME(ECCURVE_C(me)))); } | |
986 | ||
987 | static PyObject *ecget_a(PyObject *me, void *hunoz) | |
988 | { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->a))); } | |
989 | ||
b2687a0a | 990 | static PyObject *ecget_b(PyObject *me, void *hunoz) |
d7ab1bab | 991 | { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->b))); } |
992 | ||
993 | static PyObject *ecget_field(PyObject *me, void *hunoz) | |
994 | { RETURN_OBJ(ECCURVE_FOBJ(me)); } | |
995 | ||
996 | static PyObject *ecget_inf(PyObject *me, void *hunoz) | |
997 | { ec inf = EC_INIT; return (ecpt_pywrap(me, &inf)); } | |
998 | ||
999 | static PyGetSetDef eccurve_pygetset[] = { | |
1000 | #define GETSETNAME(op, name) ec##op##_##name | |
1001 | GET (name, "E.name -> name of this kind of curve") | |
1002 | GET (a, "E.a -> first parameter of curve") | |
1003 | GET (b, "E.b -> second parameter of curve") | |
1004 | GET (field, "E.field -> finite field containing this curve") | |
1005 | GET (inf, "E.inf -> point at infinity of this curve") | |
1006 | #undef GETSETNAME | |
1007 | { 0 } | |
b2687a0a | 1008 | }; |
d7ab1bab | 1009 | |
1010 | static PyMethodDef eccurve_pymethods[] = { | |
1011 | #define METHNAME(name) ecmeth_##name | |
1012 | METH (mmul, "\ | |
1013 | E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...") | |
1014 | METH (find, "E.find(X) -> P") | |
1015 | KWMETH(rand, "E.rand(rng = rand) ->P") | |
1016 | #undef METHNAME | |
1017 | { 0 } | |
1018 | }; | |
1019 | ||
1020 | static PyTypeObject eccurve_pytype_skel = { | |
6d4db0bf | 1021 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1022 | "ECCurve", /* @tp_name@ */ |
d7ab1bab | 1023 | sizeof(eccurve_pyobj), /* @tp_basicsize@ */ |
1024 | 0, /* @tp_itemsize@ */ | |
1025 | ||
1026 | eccurve_pydealloc, /* @tp_dealloc@ */ | |
1027 | 0, /* @tp_print@ */ | |
1028 | 0, /* @tp_getattr@ */ | |
1029 | 0, /* @tp_setattr@ */ | |
1030 | 0, /* @tp_compare@ */ | |
1031 | 0, /* @tp_repr@ */ | |
1032 | 0, /* @tp_as_number@ */ | |
1033 | 0, /* @tp_as_sequence@ */ | |
1034 | 0, /* @tp_as_mapping@ */ | |
1035 | 0, /* @tp_hash@ */ | |
1036 | 0, /* @tp_call@ */ | |
1037 | 0, /* @tp_str@ */ | |
1038 | 0, /* @tp_getattro@ */ | |
1039 | 0, /* @tp_setattro@ */ | |
1040 | 0, /* @tp_as_buffer@ */ | |
1041 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1042 | Py_TPFLAGS_BASETYPE, | |
1043 | ||
1044 | /* @tp_doc@ */ | |
1045 | "An elliptic curve. Abstract class.", | |
1046 | ||
1047 | 0, /* @tp_traverse@ */ | |
1048 | 0, /* @tp_clear@ */ | |
1049 | eccurve_pyrichcompare, /* @tp_richcompare@ */ | |
1050 | 0, /* @tp_weaklistoffset@ */ | |
1051 | 0, /* @tp_iter@ */ | |
963a6148 | 1052 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1053 | eccurve_pymethods, /* @tp_methods@ */ |
1054 | 0, /* @tp_members@ */ | |
1055 | eccurve_pygetset, /* @tp_getset@ */ | |
1056 | 0, /* @tp_base@ */ | |
1057 | 0, /* @tp_dict@ */ | |
1058 | 0, /* @tp_descr_get@ */ | |
1059 | 0, /* @tp_descr_set@ */ | |
1060 | 0, /* @tp_dictoffset@ */ | |
1061 | 0, /* @tp_init@ */ | |
1062 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1063 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 1064 | 0, /* @tp_free@ */ |
d7ab1bab | 1065 | 0 /* @tp_is_gc@ */ |
1066 | }; | |
1067 | ||
1068 | static PyObject *ecprimecurve_pynew(PyTypeObject *ty, | |
1069 | PyObject *arg, PyObject *kw) | |
1070 | { | |
1071 | return (eccurve_pynew(ty, ec_prime, arg, kw)); | |
1072 | } | |
1073 | ||
1074 | static PyTypeObject ecprimecurve_pytype_skel = { | |
6d4db0bf | 1075 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1076 | "ECPrimeCurve", /* @tp_name@ */ |
d7ab1bab | 1077 | sizeof(eccurve_pyobj), /* @tp_basicsize@ */ |
1078 | 0, /* @tp_itemsize@ */ | |
1079 | ||
1080 | eccurve_pydealloc, /* @tp_dealloc@ */ | |
1081 | 0, /* @tp_print@ */ | |
1082 | 0, /* @tp_getattr@ */ | |
1083 | 0, /* @tp_setattr@ */ | |
1084 | 0, /* @tp_compare@ */ | |
1085 | 0, /* @tp_repr@ */ | |
1086 | 0, /* @tp_as_number@ */ | |
1087 | 0, /* @tp_as_sequence@ */ | |
1088 | 0, /* @tp_as_mapping@ */ | |
1089 | 0, /* @tp_hash@ */ | |
1090 | 0, /* @tp_call@ */ | |
1091 | 0, /* @tp_str@ */ | |
1092 | 0, /* @tp_getattro@ */ | |
1093 | 0, /* @tp_setattro@ */ | |
1094 | 0, /* @tp_as_buffer@ */ | |
1095 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1096 | Py_TPFLAGS_BASETYPE, | |
1097 | ||
1098 | /* @tp_doc@ */ | |
1099 | "An elliptic curve over a prime field. Use ecprimeprojcurve.", | |
1100 | ||
1101 | 0, /* @tp_traverse@ */ | |
1102 | 0, /* @tp_clear@ */ | |
1103 | eccurve_pyrichcompare, /* @tp_richcompare@ */ | |
1104 | 0, /* @tp_weaklistoffset@ */ | |
1105 | 0, /* @tp_iter@ */ | |
963a6148 | 1106 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1107 | 0, /* @tp_methods@ */ |
1108 | 0, /* @tp_members@ */ | |
1109 | 0, /* @tp_getset@ */ | |
1110 | 0, /* @tp_base@ */ | |
1111 | 0, /* @tp_dict@ */ | |
1112 | 0, /* @tp_descr_get@ */ | |
1113 | 0, /* @tp_descr_set@ */ | |
1114 | 0, /* @tp_dictoffset@ */ | |
1115 | 0, /* @tp_init@ */ | |
1116 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1117 | ecprimecurve_pynew, /* @tp_new@ */ | |
3aa33042 | 1118 | 0, /* @tp_free@ */ |
d7ab1bab | 1119 | 0 /* @tp_is_gc@ */ |
1120 | }; | |
1121 | ||
1122 | static PyObject *ecprimeprojcurve_pynew(PyTypeObject *ty, | |
1123 | PyObject *arg, PyObject *kw) | |
1124 | { | |
1125 | return (eccurve_pynew(ty, ec_primeproj, arg, kw)); | |
1126 | } | |
1127 | ||
1128 | static PyTypeObject ecprimeprojcurve_pytype_skel = { | |
6d4db0bf | 1129 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1130 | "ECPrimeProjCurve", /* @tp_name@ */ |
d7ab1bab | 1131 | sizeof(eccurve_pyobj), /* @tp_basicsize@ */ |
1132 | 0, /* @tp_itemsize@ */ | |
1133 | ||
1134 | eccurve_pydealloc, /* @tp_dealloc@ */ | |
1135 | 0, /* @tp_print@ */ | |
1136 | 0, /* @tp_getattr@ */ | |
1137 | 0, /* @tp_setattr@ */ | |
1138 | 0, /* @tp_compare@ */ | |
1139 | 0, /* @tp_repr@ */ | |
1140 | 0, /* @tp_as_number@ */ | |
1141 | 0, /* @tp_as_sequence@ */ | |
1142 | 0, /* @tp_as_mapping@ */ | |
1143 | 0, /* @tp_hash@ */ | |
1144 | 0, /* @tp_call@ */ | |
1145 | 0, /* @tp_str@ */ | |
1146 | 0, /* @tp_getattro@ */ | |
1147 | 0, /* @tp_setattro@ */ | |
1148 | 0, /* @tp_as_buffer@ */ | |
1149 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1150 | Py_TPFLAGS_BASETYPE, | |
1151 | ||
1152 | /* @tp_doc@ */ | |
1153 | "An elliptic curve over a prime field, using projective coordinates.", | |
1154 | ||
1155 | 0, /* @tp_traverse@ */ | |
1156 | 0, /* @tp_clear@ */ | |
1157 | eccurve_pyrichcompare, /* @tp_richcompare@ */ | |
1158 | 0, /* @tp_weaklistoffset@ */ | |
1159 | 0, /* @tp_iter@ */ | |
963a6148 | 1160 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1161 | 0, /* @tp_methods@ */ |
1162 | 0, /* @tp_members@ */ | |
1163 | 0, /* @tp_getset@ */ | |
1164 | 0, /* @tp_base@ */ | |
1165 | 0, /* @tp_dict@ */ | |
1166 | 0, /* @tp_descr_get@ */ | |
1167 | 0, /* @tp_descr_set@ */ | |
1168 | 0, /* @tp_dictoffset@ */ | |
1169 | 0, /* @tp_init@ */ | |
1170 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1171 | ecprimeprojcurve_pynew, /* @tp_new@ */ | |
3aa33042 | 1172 | 0, /* @tp_free@ */ |
d7ab1bab | 1173 | 0 /* @tp_is_gc@ */ |
1174 | }; | |
1175 | ||
1176 | static PyObject *ecbincurve_pynew(PyTypeObject *ty, | |
1177 | PyObject *arg, PyObject *kw) | |
1178 | { | |
1179 | return (eccurve_pynew(ty, ec_bin, arg, kw)); | |
1180 | } | |
1181 | ||
1182 | static PyTypeObject ecbincurve_pytype_skel = { | |
6d4db0bf | 1183 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1184 | "ECBinCurve", /* @tp_name@ */ |
d7ab1bab | 1185 | sizeof(eccurve_pyobj), /* @tp_basicsize@ */ |
1186 | 0, /* @tp_itemsize@ */ | |
1187 | ||
1188 | eccurve_pydealloc, /* @tp_dealloc@ */ | |
1189 | 0, /* @tp_print@ */ | |
1190 | 0, /* @tp_getattr@ */ | |
1191 | 0, /* @tp_setattr@ */ | |
1192 | 0, /* @tp_compare@ */ | |
1193 | 0, /* @tp_repr@ */ | |
1194 | 0, /* @tp_as_number@ */ | |
1195 | 0, /* @tp_as_sequence@ */ | |
1196 | 0, /* @tp_as_mapping@ */ | |
1197 | 0, /* @tp_hash@ */ | |
1198 | 0, /* @tp_call@ */ | |
1199 | 0, /* @tp_str@ */ | |
1200 | 0, /* @tp_getattro@ */ | |
1201 | 0, /* @tp_setattro@ */ | |
1202 | 0, /* @tp_as_buffer@ */ | |
1203 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1204 | Py_TPFLAGS_BASETYPE, | |
1205 | ||
1206 | /* @tp_doc@ */ | |
1207 | "An elliptic curve over a binary field. Use ecbinprojcurve.", | |
1208 | ||
1209 | 0, /* @tp_traverse@ */ | |
1210 | 0, /* @tp_clear@ */ | |
1211 | eccurve_pyrichcompare, /* @tp_richcompare@ */ | |
1212 | 0, /* @tp_weaklistoffset@ */ | |
1213 | 0, /* @tp_iter@ */ | |
963a6148 | 1214 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1215 | 0, /* @tp_methods@ */ |
1216 | 0, /* @tp_members@ */ | |
1217 | 0, /* @tp_getset@ */ | |
1218 | 0, /* @tp_base@ */ | |
1219 | 0, /* @tp_dict@ */ | |
1220 | 0, /* @tp_descr_get@ */ | |
1221 | 0, /* @tp_descr_set@ */ | |
1222 | 0, /* @tp_dictoffset@ */ | |
1223 | 0, /* @tp_init@ */ | |
1224 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1225 | ecbincurve_pynew, /* @tp_new@ */ | |
3aa33042 | 1226 | 0, /* @tp_free@ */ |
d7ab1bab | 1227 | 0 /* @tp_is_gc@ */ |
1228 | }; | |
1229 | ||
1230 | static PyObject *ecbinprojcurve_pynew(PyTypeObject *ty, | |
1231 | PyObject *arg, PyObject *kw) | |
1232 | { | |
1233 | return (eccurve_pynew(ty, ec_binproj, arg, kw)); | |
1234 | } | |
1235 | ||
1236 | static PyTypeObject ecbinprojcurve_pytype_skel = { | |
6d4db0bf | 1237 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1238 | "ECBinProjCurve", /* @tp_name@ */ |
d7ab1bab | 1239 | sizeof(eccurve_pyobj), /* @tp_basicsize@ */ |
1240 | 0, /* @tp_itemsize@ */ | |
1241 | ||
1242 | eccurve_pydealloc, /* @tp_dealloc@ */ | |
1243 | 0, /* @tp_print@ */ | |
1244 | 0, /* @tp_getattr@ */ | |
1245 | 0, /* @tp_setattr@ */ | |
1246 | 0, /* @tp_compare@ */ | |
1247 | 0, /* @tp_repr@ */ | |
1248 | 0, /* @tp_as_number@ */ | |
1249 | 0, /* @tp_as_sequence@ */ | |
1250 | 0, /* @tp_as_mapping@ */ | |
1251 | 0, /* @tp_hash@ */ | |
1252 | 0, /* @tp_call@ */ | |
1253 | 0, /* @tp_str@ */ | |
1254 | 0, /* @tp_getattro@ */ | |
1255 | 0, /* @tp_setattro@ */ | |
1256 | 0, /* @tp_as_buffer@ */ | |
1257 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1258 | Py_TPFLAGS_BASETYPE, | |
1259 | ||
1260 | /* @tp_doc@ */ | |
1261 | "An elliptic curve over a binary field, using projective coordinates.", | |
1262 | ||
1263 | 0, /* @tp_traverse@ */ | |
1264 | 0, /* @tp_clear@ */ | |
1265 | eccurve_pyrichcompare, /* @tp_richcompare@ */ | |
1266 | 0, /* @tp_weaklistoffset@ */ | |
1267 | 0, /* @tp_iter@ */ | |
963a6148 | 1268 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1269 | 0, /* @tp_methods@ */ |
1270 | 0, /* @tp_members@ */ | |
1271 | 0, /* @tp_getset@ */ | |
1272 | 0, /* @tp_base@ */ | |
1273 | 0, /* @tp_dict@ */ | |
1274 | 0, /* @tp_descr_get@ */ | |
1275 | 0, /* @tp_descr_set@ */ | |
1276 | 0, /* @tp_dictoffset@ */ | |
1277 | 0, /* @tp_init@ */ | |
1278 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1279 | ecbinprojcurve_pynew, /* @tp_new@ */ | |
3aa33042 | 1280 | 0, /* @tp_free@ */ |
d7ab1bab | 1281 | 0 /* @tp_is_gc@ */ |
1282 | }; | |
1283 | ||
1284 | /*----- Curve info --------------------------------------------------------*/ | |
1285 | ||
1286 | static int ncurves = -1; | |
1287 | ||
1288 | void ecinfo_copy(ec_info *eic, const ec_info *ei) | |
1289 | { | |
1290 | eic->c = eccurve_copy(ei->c); | |
1291 | EC_CREATE(&eic->g); | |
1292 | EC_COPY(&eic->g, &ei->g); | |
1293 | eic->r = MP_COPY(ei->r); | |
1294 | eic->h = MP_COPY(ei->h); | |
1295 | } | |
1296 | ||
1297 | PyObject *ecinfo_pywrap(ec_info *ei) | |
1298 | { | |
1299 | ecinfo_pyobj *o; | |
1300 | ||
1301 | o = PyObject_NEW(ecinfo_pyobj, ecinfo_pytype); | |
1302 | o->ei = *ei; | |
1303 | o->cobj = eccurve_pywrap(0, o->ei.c); | |
1304 | Py_INCREF(o->cobj); | |
1305 | return ((PyObject *)o); | |
1306 | } | |
1307 | ||
1308 | static void ecinfo_pydealloc(PyObject *me) | |
1309 | { | |
1310 | ec_info *ei = ECINFO_EI(me); | |
1311 | EC_DESTROY(&ei->g); | |
1312 | MP_DROP(ei->r); | |
1313 | MP_DROP(ei->h); | |
1314 | Py_DECREF(ECINFO_COBJ(me)); | |
3aa33042 | 1315 | FREEOBJ(me); |
d7ab1bab | 1316 | } |
1317 | ||
1318 | static PyObject *ecinfo_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
1319 | { | |
1320 | ec_info ei = { 0 }; | |
1321 | PyObject *e, *g; | |
1322 | char *kwlist[] = { "curve", "G", "r", "h", 0 }; | |
1323 | ecinfo_pyobj *rc = 0; | |
1324 | ||
1325 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!O&O&:new", kwlist, | |
1326 | eccurve_pytype, &e, ecpt_pytype, &g, | |
1327 | convmp, &ei.r, convmp, &ei.h)) | |
1328 | goto end; | |
1329 | if (ECPT_C(g) != ECCURVE_C(e) && !ec_samep(ECPT_C(g), ECCURVE_C(e))) | |
1330 | TYERR("point not from this curve"); | |
1331 | ei.c = ECCURVE_C(e); | |
1332 | EC_CREATE(&ei.g); | |
30720615 | 1333 | EC_OUT(ei.c, &ei.g, ECPT_P(g)); |
d7ab1bab | 1334 | rc = (ecinfo_pyobj *)ty->tp_alloc(ty, 0); |
1335 | rc->ei = ei; | |
1336 | rc->cobj = e; | |
1337 | Py_INCREF(rc->cobj); | |
1338 | return ((PyObject *)rc); | |
1339 | ||
1340 | end: | |
1341 | mp_drop(ei.r); | |
1342 | mp_drop(ei.h); | |
1343 | return (0); | |
1344 | } | |
1345 | ||
1346 | static PyObject *meth__ECInfo_parse(PyObject *me, PyObject *arg) | |
1347 | { | |
1348 | char *p; | |
1349 | qd_parse qd; | |
1350 | ec_info ei; | |
1351 | PyObject *rc = 0; | |
1352 | ||
1353 | if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) | |
1354 | goto end; | |
1355 | qd.p = p; | |
1356 | qd.e = 0; | |
1357 | if (ec_infoparse(&qd, &ei)) | |
80f7cd89 | 1358 | VALERR(qd.e); |
d7ab1bab | 1359 | rc = Py_BuildValue("(Ns)", ecinfo_pywrap(&ei), qd.p); |
1360 | end: | |
1361 | return (rc); | |
1362 | } | |
1363 | ||
1364 | static PyObject *meth__ECInfo__curven(PyObject *me, PyObject *arg) | |
1365 | { | |
1366 | int i; | |
1367 | ec_info ei; | |
1368 | PyObject *rc = 0; | |
1369 | ||
1370 | if (!PyArg_ParseTuple(arg, "Oi:_curven", &me, &i)) goto end; | |
1371 | if (i < 0 || i >= ncurves) VALERR("curve index out of range"); | |
1372 | ec_infofromdata(&ei, ectab[i].data); | |
1373 | rc = ecinfo_pywrap(&ei); | |
1374 | end: | |
1375 | return (rc); | |
1376 | } | |
1377 | ||
1378 | static PyObject *ecinfo_pyrichcompare(PyObject *x, PyObject *y, int op) | |
1379 | { | |
1380 | int b = ec_sameinfop(ECINFO_EI(x), ECINFO_EI(y)); | |
1381 | switch (op) { | |
1382 | case Py_EQ: break; | |
1383 | case Py_NE: b = !b; | |
1384 | default: TYERR("can't order elliptic curve infos"); | |
1385 | } | |
1386 | return (getbool(b)); | |
1387 | end: | |
1388 | return (0); | |
1389 | } | |
1390 | ||
1391 | static PyObject *eimeth_check(PyObject *me, PyObject *arg, PyObject *kw) | |
1392 | { | |
1393 | char *kwlist[] = { "rng", 0 }; | |
1394 | grand *r = &rand_global; | |
1395 | const char *p; | |
1396 | ||
1397 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:check", kwlist, | |
1398 | convgrand, &r)) | |
1399 | goto end; | |
1400 | if ((p = ec_checkinfo(ECINFO_EI(me), r)) != 0) | |
1401 | VALERR(p); | |
1402 | RETURN_ME; | |
1403 | end: | |
1404 | return (0); | |
1405 | } | |
1406 | ||
1407 | static PyObject *eiget_curve(PyObject *me, void *hunoz) | |
1408 | { RETURN_OBJ(ECINFO_COBJ(me)); } | |
1409 | ||
1410 | static PyObject *eiget_G(PyObject *me, void *hunoz) | |
1411 | { | |
1412 | ec_info *ei = ECINFO_EI(me); | |
1413 | ec p = EC_INIT; | |
1414 | EC_IN(ei->c, &p, &ei->g); | |
1415 | return (ecpt_pywrap(ECINFO_COBJ(me), &p)); | |
1416 | } | |
1417 | ||
1418 | static PyObject *eiget_r(PyObject *me, void *hunoz) | |
1419 | { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->r))); } | |
1420 | ||
1421 | static PyObject *eiget_h(PyObject *me, void *hunoz) | |
1422 | { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->h))); } | |
1423 | ||
1424 | static PyGetSetDef ecinfo_pygetset[] = { | |
1425 | #define GETSETNAME(op, name) ei##op##_##name | |
1426 | GET (curve, "I.curve -> the elliptic curve") | |
1427 | GET (G, "I.G -> generator point for the group") | |
1428 | GET (r, "I.r -> order of the group (and hence of G") | |
1429 | GET (h, "I.h -> cofactor of the group") | |
1430 | #undef GETSETNAME | |
1431 | { 0 } | |
1432 | }; | |
1433 | ||
1434 | static PyMethodDef ecinfo_pymethods[] = { | |
1435 | #define METHNAME(name) eimeth_##name | |
1436 | KWMETH(check, "I.check() -> None") | |
1437 | #undef METHNAME | |
1438 | { 0 } | |
1439 | }; | |
1440 | ||
1441 | static PyTypeObject ecinfo_pytype_skel = { | |
6d4db0bf | 1442 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1443 | "ECInfo", /* @tp_name@ */ |
d7ab1bab | 1444 | sizeof(ecinfo_pyobj), /* @tp_basicsize@ */ |
1445 | 0, /* @tp_itemsize@ */ | |
1446 | ||
1447 | ecinfo_pydealloc, /* @tp_dealloc@ */ | |
1448 | 0, /* @tp_print@ */ | |
1449 | 0, /* @tp_getattr@ */ | |
1450 | 0, /* @tp_setattr@ */ | |
1451 | 0, /* @tp_compare@ */ | |
1452 | 0, /* @tp_repr@ */ | |
1453 | 0, /* @tp_as_number@ */ | |
1454 | 0, /* @tp_as_sequence@ */ | |
1455 | 0, /* @tp_as_mapping@ */ | |
1456 | 0, /* @tp_hash@ */ | |
1457 | 0, /* @tp_call@ */ | |
1458 | 0, /* @tp_str@ */ | |
1459 | 0, /* @tp_getattro@ */ | |
1460 | 0, /* @tp_setattro@ */ | |
1461 | 0, /* @tp_as_buffer@ */ | |
1462 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1463 | Py_TPFLAGS_BASETYPE, | |
1464 | ||
1465 | /* @tp_doc@ */ | |
1466 | "Elliptic curve domain parameters.", | |
1467 | ||
1468 | 0, /* @tp_traverse@ */ | |
1469 | 0, /* @tp_clear@ */ | |
1470 | ecinfo_pyrichcompare, /* @tp_richcompare@ */ | |
1471 | 0, /* @tp_weaklistoffset@ */ | |
1472 | 0, /* @tp_iter@ */ | |
963a6148 | 1473 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1474 | ecinfo_pymethods, /* @tp_methods@ */ |
1475 | 0, /* @tp_members@ */ | |
1476 | ecinfo_pygetset, /* @tp_getset@ */ | |
1477 | 0, /* @tp_base@ */ | |
1478 | 0, /* @tp_dict@ */ | |
1479 | 0, /* @tp_descr_get@ */ | |
1480 | 0, /* @tp_descr_set@ */ | |
1481 | 0, /* @tp_dictoffset@ */ | |
1482 | 0, /* @tp_init@ */ | |
1483 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1484 | ecinfo_pynew, /* @tp_new@ */ | |
3aa33042 | 1485 | 0, /* @tp_free@ */ |
d7ab1bab | 1486 | 0 /* @tp_is_gc@ */ |
1487 | }; | |
1488 | ||
1489 | /*----- Setup -------------------------------------------------------------*/ | |
1490 | ||
1491 | static PyMethodDef methods[] = { | |
1492 | #define METHNAME(func) meth_##func | |
b2687a0a MW |
1493 | METH (_ECPt_frombuf, "frombuf(E, STR) -> (P, REST)") |
1494 | METH (_ECPtCurve_fromraw, "fromraw(E, STR) -> (P, REST)") | |
1495 | METH (_ECPt_parse, "parse(E, STR) -> (P, REST)") | |
1496 | METH (_ECCurve_parse, "parse(STR) -> (E, REST)") | |
1497 | METH (_ECInfo_parse, "parse(STR) -> (I, REST)") | |
1498 | METH (_ECInfo__curven, "_curven(N) -> I") | |
d7ab1bab | 1499 | #undef METHNAME |
1500 | { 0 } | |
1501 | }; | |
1502 | ||
1503 | void ec_pyinit(void) | |
1504 | { | |
1505 | INITTYPE(ecpt, root); | |
1506 | INITTYPE(ecptcurve, ecpt); | |
1507 | INITTYPE(eccurve, type); | |
1508 | INITTYPE(ecprimecurve, eccurve); | |
1509 | INITTYPE(ecprimeprojcurve, ecprimecurve); | |
1510 | INITTYPE(ecbincurve, eccurve); | |
1511 | INITTYPE(ecbinprojcurve, ecbincurve); | |
1512 | INITTYPE(ecinfo, root); | |
1513 | addmethods(methods); | |
1514 | } | |
1515 | ||
1516 | static PyObject *namedcurves(void) | |
1517 | { | |
1518 | int i, j; | |
1519 | const char *p; | |
1520 | PyObject *d, *c; | |
1521 | ||
1522 | d = PyDict_New(); | |
1523 | for (i = 0; ectab[i].name; i++) { | |
1524 | p = ectab[i].name; | |
1525 | for (j = 0; j < i; j++) { | |
1526 | if (ectab[i].data == ectab[j].data) { | |
1527 | c = PyDict_GetItemString(d, (/*unconst*/ char *)ectab[j].name); | |
1528 | Py_INCREF(c); | |
1529 | goto found; | |
1530 | } | |
1531 | } | |
1532 | c = PyInt_FromLong(i); | |
1533 | found: | |
98963817 | 1534 | PyDict_SetItemString(d, (/*unconst*/ char *)p, c); |
d7ab1bab | 1535 | Py_DECREF(c); |
1536 | } | |
1537 | ncurves = i; | |
1538 | return (d); | |
1539 | } | |
1540 | ||
1541 | void ec_pyinsert(PyObject *mod) | |
1542 | { | |
1543 | INSERT("ECPt", ecpt_pytype); | |
1544 | INSERT("ECPtCurve", ecptcurve_pytype); | |
1545 | INSERT("ECCurve", eccurve_pytype); | |
1546 | INSERT("ECPrimeCurve", ecprimecurve_pytype); | |
1547 | INSERT("ECPrimeProjCurve", ecprimeprojcurve_pytype); | |
1548 | INSERT("ECBinCurve", ecbincurve_pytype); | |
1549 | INSERT("ECBinProjCurve", ecbinprojcurve_pytype); | |
1550 | INSERT("ECInfo", ecinfo_pytype); | |
1551 | INSERT("_eccurves", namedcurves()); | |
1552 | } | |
1553 | ||
1554 | /*----- That's all, folks -------------------------------------------------*/ |