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