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