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