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