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