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