| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Elliptic curves |
| 4 | * |
| 5 | * (c) 2004 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with Catacomb/Python; if not, write to the Free Software Foundation, |
| 24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | /*----- Header files ------------------------------------------------------*/ |
| 28 | |
| 29 | #include "catacomb-python.h" |
| 30 | |
| 31 | /*----- Utility functions -------------------------------------------------*/ |
| 32 | |
| 33 | PyTypeObject *ecpt_pytype; |
| 34 | PyTypeObject *ecptcurve_pytype; |
| 35 | PyTypeObject *eccurve_pytype; |
| 36 | PyTypeObject *ecprimecurve_pytype; |
| 37 | PyTypeObject *ecprimeprojcurve_pytype; |
| 38 | PyTypeObject *ecbincurve_pytype; |
| 39 | PyTypeObject *ecbinprojcurve_pytype; |
| 40 | PyTypeObject *ecinfo_pytype; |
| 41 | |
| 42 | ec_curve *eccurve_copy(ec_curve *c) |
| 43 | { |
| 44 | field *f; |
| 45 | mp *a, *b; |
| 46 | |
| 47 | if ((f = field_copy(c->f)) == 0) |
| 48 | return (0); |
| 49 | a = F_OUT(f, MP_NEW, c->a); |
| 50 | b = F_OUT(f, MP_NEW, c->b); |
| 51 | if (STRCMP(EC_NAME(c), ==, "prime")) |
| 52 | c = ec_prime(f, a, b); |
| 53 | else if (STRCMP(EC_NAME(c), ==, "primeproj")) |
| 54 | c = ec_primeproj(f, a, b); |
| 55 | else if (STRCMP(EC_NAME(c), ==, "bin")) |
| 56 | c = ec_bin(f, a, b); |
| 57 | else if (STRCMP(EC_NAME(c), ==, "binproj")) |
| 58 | c = ec_binproj(f, a, b); |
| 59 | else |
| 60 | c = 0; |
| 61 | MP_DROP(a); |
| 62 | MP_DROP(b); |
| 63 | if (!c) F_DESTROY(f); |
| 64 | return (c); |
| 65 | } |
| 66 | |
| 67 | static PyObject *ecpt_dopywrap(PyObject *cobj, ec_curve *c, ec *p) |
| 68 | { |
| 69 | ecpt_pyobj *z = PyObject_New(ecpt_pyobj, (PyTypeObject *)cobj); |
| 70 | z->p = *p; |
| 71 | z->c = c; |
| 72 | Py_INCREF(cobj); |
| 73 | return ((PyObject *)z); |
| 74 | } |
| 75 | |
| 76 | PyObject *ecpt_pywrap(PyObject *cobj, ec *p) |
| 77 | { return (ecpt_dopywrap(cobj, ECCURVE_C(cobj), p)); } |
| 78 | |
| 79 | PyObject *ecpt_pywrapout(void *cobj, ec *p) |
| 80 | { |
| 81 | ec_curve *c; |
| 82 | |
| 83 | if (!PyType_IsSubtype(cobj, ecptcurve_pytype)) |
| 84 | c = 0; |
| 85 | else { |
| 86 | c = ECCURVE_C(cobj); |
| 87 | EC_IN(ECCURVE_C(cobj), p, p); |
| 88 | } |
| 89 | return (ecpt_dopywrap(cobj, c, p)); |
| 90 | } |
| 91 | |
| 92 | int toecpt(ec_curve *c, ec *d, PyObject *p) |
| 93 | { |
| 94 | if (ECPTCURVE_PYCHECK(p)) { |
| 95 | if (ECPT_C(p) != c && !ec_samep(ECPT_C(p), c)) |
| 96 | return (-1); |
| 97 | EC_COPY(d, ECPT_P(p)); |
| 98 | } else if (ECPT_PYCHECK(p)) |
| 99 | EC_IN(c, d, ECPT_P(p)); |
| 100 | else |
| 101 | return (-1); |
| 102 | return (0); |
| 103 | } |
| 104 | |
| 105 | int getecpt(ec_curve *c, ec *d, PyObject *p) |
| 106 | { |
| 107 | if (toecpt(c, d, p)) { |
| 108 | PyErr_Format(PyExc_TypeError, "can't convert %.100s to ecpt", |
| 109 | p->ob_type->tp_name); |
| 110 | return (-1); |
| 111 | } |
| 112 | return (0); |
| 113 | } |
| 114 | |
| 115 | void getecptout(ec *d, PyObject *p) |
| 116 | { |
| 117 | if (ECPTCURVE_PYCHECK(p)) |
| 118 | EC_OUT(ECPT_C(p), d, ECPT_P(p)); |
| 119 | else { |
| 120 | assert(ECPT_PYCHECK(p)); |
| 121 | EC_COPY(d, ECPT_P(p)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | int convecpt(PyObject *o, void *p) |
| 126 | { |
| 127 | if (!ECPT_PYCHECK(o)) |
| 128 | TYERR("want elliptic curve point"); |
| 129 | getecptout(p, o); |
| 130 | return (1); |
| 131 | end: |
| 132 | return (0); |
| 133 | } |
| 134 | |
| 135 | /*----- Curve points ------------------------------------------------------*/ |
| 136 | |
| 137 | static int ecbinop(PyObject *x, PyObject *y, |
| 138 | ec_curve **c, PyObject **cobj, ec *xx, ec *yy) |
| 139 | { |
| 140 | if (ECPTCURVE_PYCHECK(x)) *cobj = ECPT_COBJ(x); |
| 141 | else if (ECPTCURVE_PYCHECK(y)) *cobj = ECPT_COBJ(y); |
| 142 | else return (-1); |
| 143 | *c = ECCURVE_C(*cobj); |
| 144 | if (toecpt(*c, xx, x) || toecpt(*c, yy, y)) return (-1); |
| 145 | return (0); |
| 146 | } |
| 147 | |
| 148 | #define BINOP(name) \ |
| 149 | static PyObject *ecpt_py##name(PyObject *x, PyObject *y) { \ |
| 150 | ec xx = EC_INIT, yy = EC_INIT, zz = EC_INIT; \ |
| 151 | PyObject *cobj; \ |
| 152 | ec_curve *c; \ |
| 153 | if (ecbinop(x, y, &c, &cobj, &xx, &yy)) RETURN_NOTIMPL; \ |
| 154 | c->ops->name(c, &zz, &xx, &yy); \ |
| 155 | EC_DESTROY(&xx); EC_DESTROY(&yy); \ |
| 156 | return (ecpt_pywrap(ECPT_COBJ(x), &zz)); \ |
| 157 | } |
| 158 | BINOP(add) |
| 159 | BINOP(sub) |
| 160 | #undef BINOP |
| 161 | |
| 162 | #define UNOP(name) \ |
| 163 | static PyObject *ecpt_py##name(PyObject *x) { \ |
| 164 | ec zz = EC_INIT; \ |
| 165 | ec_curve *c = ECPT_C(x); \ |
| 166 | c->ops->name(c, &zz, ECPT_P(x)); \ |
| 167 | return (ecpt_pywrap(ECPT_COBJ(x), &zz)); \ |
| 168 | } |
| 169 | UNOP(neg) |
| 170 | #undef UNOP |
| 171 | |
| 172 | static PyObject *ecpt_pyid(PyObject *x) { RETURN_OBJ(x); } |
| 173 | |
| 174 | static int ecpt_pynonzerop(PyObject *x) { return (!EC_ATINF(ECPT_P(x))); } |
| 175 | |
| 176 | static void ecpt_pydealloc(PyObject *x) |
| 177 | { |
| 178 | EC_DESTROY(ECPT_P(x)); |
| 179 | Py_DECREF(ECPT_COBJ(x)); |
| 180 | FREEOBJ(x); |
| 181 | } |
| 182 | |
| 183 | static PyObject *ecpt_pymul(PyObject *x, PyObject *y) |
| 184 | { |
| 185 | mp *xx; |
| 186 | ec zz = EC_INIT; |
| 187 | |
| 188 | if (ECPT_PYCHECK(x)) { PyObject *t; t = x; x = y; y = t; } |
| 189 | if (!ECPT_PYCHECK(y) || (xx = tomp(x)) == 0) RETURN_NOTIMPL; |
| 190 | ec_imul(ECPT_C(y), &zz, ECPT_P(y), xx); |
| 191 | MP_DROP(xx); |
| 192 | return (ecpt_pywrap(ECPT_COBJ(y), &zz)); |
| 193 | } |
| 194 | |
| 195 | static long ecpt_pyhash(PyObject *me) |
| 196 | { |
| 197 | uint32 h; |
| 198 | ec p = EC_INIT; |
| 199 | |
| 200 | getecptout(&p, me); |
| 201 | if (EC_ATINF(&p)) h = 0x81d81a94; |
| 202 | else h = 0xe0fdd039 ^ (2*mphash(p.x)) ^ (3*mphash(p.y)); |
| 203 | EC_DESTROY(&p); |
| 204 | return (h%LONG_MAX); |
| 205 | } |
| 206 | |
| 207 | static PyObject *ecpt_pyrichcompare(PyObject *x, PyObject *y, int op) |
| 208 | { |
| 209 | ec p = EC_INIT, q = EC_INIT; |
| 210 | int b; |
| 211 | PyObject *rc = 0; |
| 212 | |
| 213 | if (!ECPT_PYCHECK(y)) RETURN_NOTIMPL; |
| 214 | getecptout(&p, x); |
| 215 | getecptout(&q, y); |
| 216 | switch (op) { |
| 217 | case Py_EQ: b = EC_EQ(&p, &q); break; |
| 218 | case Py_NE: b = !EC_EQ(&p, &q); break; |
| 219 | default: TYERR("elliptic curve points are unordered"); |
| 220 | } |
| 221 | rc = getbool(b); |
| 222 | end: |
| 223 | EC_DESTROY(&p); |
| 224 | EC_DESTROY(&q); |
| 225 | return (rc); |
| 226 | } |
| 227 | |
| 228 | static PyObject *epmeth_oncurvep(PyObject *me, PyObject *arg) |
| 229 | { |
| 230 | if (!PyArg_ParseTuple(arg, ":oncurvep")) return (0); |
| 231 | return (getbool(EC_ATINF(ECPT_P(me)) || |
| 232 | !EC_CHECK(ECPT_C(me), ECPT_P(me)))); |
| 233 | } |
| 234 | |
| 235 | static PyObject *epmeth_dbl(PyObject *me, PyObject *arg) |
| 236 | { |
| 237 | ec p = EC_INIT; |
| 238 | if (!PyArg_ParseTuple(arg, ":dbl")) return (0); |
| 239 | EC_DBL(ECPT_C(me), &p, ECPT_P(me)); |
| 240 | return (ecpt_pywrap(ECPT_COBJ(me), &p)); |
| 241 | } |
| 242 | |
| 243 | static PyObject *epmeth_tobuf(PyObject *me, PyObject *arg) |
| 244 | { |
| 245 | buf b; |
| 246 | ec p = EC_INIT; |
| 247 | PyObject *rc; |
| 248 | size_t n; |
| 249 | |
| 250 | if (!PyArg_ParseTuple(arg, ":tobuf")) return (0); |
| 251 | getecptout(&p, me); |
| 252 | if (EC_ATINF(&p)) |
| 253 | n = 2; |
| 254 | else |
| 255 | n = mp_octets(p.x) + mp_octets(p.y) + 6; |
| 256 | rc = bytestring_pywrap(0, n); |
| 257 | buf_init(&b, PyString_AS_STRING(rc), n); |
| 258 | buf_putec(&b, &p); |
| 259 | assert(BOK(&b)); |
| 260 | _PyString_Resize(&rc, BLEN(&b)); |
| 261 | EC_DESTROY(&p); |
| 262 | return (rc); |
| 263 | } |
| 264 | |
| 265 | static PyObject *epmeth_toraw(PyObject *me, PyObject *arg) |
| 266 | { |
| 267 | buf b; |
| 268 | PyObject *rc; |
| 269 | char *p; |
| 270 | ec_curve *c = ECPT_C(me); |
| 271 | ec pp = EC_INIT; |
| 272 | int len; |
| 273 | |
| 274 | if (!PyArg_ParseTuple(arg, ":toraw")) return (0); |
| 275 | len = c->f->noctets * 2 + 1; |
| 276 | rc = bytestring_pywrap(0, len); |
| 277 | p = PyString_AS_STRING(rc); |
| 278 | buf_init(&b, p, len); |
| 279 | EC_OUT(c, &pp, ECPT_P(me)); |
| 280 | ec_putraw(c, &b, &pp); |
| 281 | EC_DESTROY(&pp); |
| 282 | _PyString_Resize(&rc, BLEN(&b)); |
| 283 | return (rc); |
| 284 | } |
| 285 | |
| 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; |
| 293 | unsigned f = EC_EXPLY; |
| 294 | int len; |
| 295 | static const char *const kwlist[] = { "flags", 0 }; |
| 296 | |
| 297 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:ec2osp", KWLIST, |
| 298 | convuint, &f)) |
| 299 | return (0); |
| 300 | len = c->f->noctets * 2 + 1; |
| 301 | rc = bytestring_pywrap(0, len); |
| 302 | p = PyString_AS_STRING(rc); |
| 303 | buf_init(&b, p, len); |
| 304 | EC_OUT(c, &pp, ECPT_P(me)); |
| 305 | if (ec_ec2osp(c, f, &b, &pp)) { |
| 306 | Py_DECREF(rc); rc = 0; |
| 307 | VALERR("invalid flags"); |
| 308 | } |
| 309 | EC_DESTROY(&pp); |
| 310 | _PyString_Resize(&rc, BLEN(&b)); |
| 311 | end: |
| 312 | return (rc); |
| 313 | } |
| 314 | |
| 315 | static PyObject *epget_curve(PyObject *me, void *hunoz) |
| 316 | { RETURN_OBJ(ECPT_COBJ(me)); } |
| 317 | |
| 318 | static PyObject *epncget_ix(PyObject *me, void *hunoz) |
| 319 | { |
| 320 | ec p = EC_INIT; |
| 321 | PyObject *rc; |
| 322 | if (EC_ATINF(ECPT_P(me))) RETURN_NONE; |
| 323 | getecptout(&p, me); |
| 324 | rc = mp_pywrap(MP_COPY(p.x)); |
| 325 | EC_DESTROY(&p); |
| 326 | return (rc); |
| 327 | } |
| 328 | |
| 329 | static PyObject *epncget_iy(PyObject *me, void *hunoz) |
| 330 | { |
| 331 | ec p = EC_INIT; |
| 332 | PyObject *rc; |
| 333 | if (EC_ATINF(ECPT_P(me))) RETURN_NONE; |
| 334 | getecptout(&p, me); |
| 335 | rc = mp_pywrap(MP_COPY(p.y)); |
| 336 | EC_DESTROY(&p); |
| 337 | return (rc); |
| 338 | } |
| 339 | |
| 340 | static PyObject *epncget_point(PyObject *me, void *hunoz) |
| 341 | { RETURN_ME; } |
| 342 | |
| 343 | static PyObject *epget_point(PyObject *me, void *hunoz) |
| 344 | { |
| 345 | ec p = EC_INIT; |
| 346 | getecptout(&p, me); |
| 347 | return (ecpt_pywrapout(ecpt_pytype, &p)); |
| 348 | } |
| 349 | |
| 350 | static PyObject *epget_x(PyObject *me, void *hunoz) |
| 351 | { |
| 352 | ec_curve *c = ECPT_C(me); |
| 353 | ec *pp = ECPT_P(me); |
| 354 | PyObject *fobj = ECPT_FOBJ(me); |
| 355 | ec p = EC_INIT; |
| 356 | PyObject *rc; |
| 357 | |
| 358 | if (EC_ATINF(pp)) RETURN_NONE; |
| 359 | EC_OUT(c, &p, pp); |
| 360 | rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.x)); |
| 361 | EC_DESTROY(&p); |
| 362 | return (rc); |
| 363 | } |
| 364 | |
| 365 | static PyObject *epget_y(PyObject *me, void *hunoz) |
| 366 | { |
| 367 | ec_curve *c = ECPT_C(me); |
| 368 | ec *pp = ECPT_P(me); |
| 369 | PyObject *fobj = ECPT_FOBJ(me); |
| 370 | ec p = EC_INIT; |
| 371 | PyObject *rc; |
| 372 | |
| 373 | if (EC_ATINF(pp)) RETURN_NONE; |
| 374 | EC_OUT(c, &p, pp); |
| 375 | rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.y)); |
| 376 | EC_DESTROY(&p); |
| 377 | return (rc); |
| 378 | } |
| 379 | |
| 380 | static PyObject *epget__x(PyObject *me, void *hunoz) |
| 381 | { |
| 382 | if (EC_ATINF(ECPT_P(me))) RETURN_NONE; |
| 383 | return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->x))); |
| 384 | } |
| 385 | |
| 386 | static PyObject *epget__y(PyObject *me, void *hunoz) |
| 387 | { |
| 388 | if (EC_ATINF(ECPT_P(me))) RETURN_NONE; |
| 389 | return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->y))); |
| 390 | } |
| 391 | |
| 392 | static PyObject *epget__z(PyObject *me, void *hunoz) |
| 393 | { |
| 394 | if (EC_ATINF(ECPT_P(me)) || !ECPT_P(me)->z) RETURN_NONE; |
| 395 | return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->z))); |
| 396 | } |
| 397 | |
| 398 | static mp *coord_in(field *f, PyObject *x) |
| 399 | { |
| 400 | mp *xx; |
| 401 | if (FE_PYCHECK(x) && FE_F(x) == f) |
| 402 | return (MP_COPY(FE_X(x))); |
| 403 | else if ((xx = getmp(x)) == 0) |
| 404 | return (0); |
| 405 | else |
| 406 | return (F_IN(f, xx, xx)); |
| 407 | } |
| 408 | |
| 409 | static int ecptxl_3(ec_curve *c, ec *p, |
| 410 | PyObject *x, PyObject *y, PyObject *z) |
| 411 | { |
| 412 | int rc = -1; |
| 413 | |
| 414 | if (!x || !y || !z) TYERR("missing argument"); |
| 415 | if (!c) VALERR("internal form with no curve!"); |
| 416 | if ((p->x = coord_in(c->f, x)) == 0 || |
| 417 | (p->y = coord_in(c->f, y)) == 0 || |
| 418 | (z != Py_None && (p->z = coord_in(c->f, z)) == 0)) |
| 419 | goto end; |
| 420 | if (!p->z) p->z = MP_COPY(c->f->one); /* just in case */ |
| 421 | rc = 0; |
| 422 | end: |
| 423 | return (rc); |
| 424 | } |
| 425 | |
| 426 | static int ecptxl_2(ec_curve *c, ec *p, PyObject *x, PyObject *y) |
| 427 | { |
| 428 | int rc = -1; |
| 429 | |
| 430 | if (!x || !y) TYERR("missing argument"); |
| 431 | if ((p->x = getmp(x)) == 0 || |
| 432 | (p->y = getmp(y)) == 0) |
| 433 | goto end; |
| 434 | if (c) EC_IN(c, p, p); |
| 435 | rc = 0; |
| 436 | end: |
| 437 | return (rc); |
| 438 | } |
| 439 | |
| 440 | static int ecptxl_1(ec_curve *c, ec *p, PyObject *x) |
| 441 | { |
| 442 | int rc = -1; |
| 443 | PyObject *y = 0, *z = 0, *t = 0; |
| 444 | mp *xx = 0; |
| 445 | const void *q; |
| 446 | Py_ssize_t n; |
| 447 | qd_parse qd; |
| 448 | |
| 449 | Py_XINCREF(x); |
| 450 | if (!x || x == Py_None) |
| 451 | /*cool*/; |
| 452 | else if (ECPT_PYCHECK(x)) { |
| 453 | getecptout(p, x); |
| 454 | goto fix; |
| 455 | } else if (PyString_Check(x)) { |
| 456 | if (PyObject_AsReadBuffer(x, &q, &n)) |
| 457 | goto end; |
| 458 | qd.p = q; |
| 459 | qd.e = 0; |
| 460 | if (!ec_ptparse(&qd, p)) |
| 461 | VALERR(qd.e); |
| 462 | goto fix; |
| 463 | } else if (c && (xx = tomp(x)) != 0) { |
| 464 | xx = F_IN(c->f, xx, xx); |
| 465 | if (!EC_FIND(c, p, xx)) VALERR("not on the curve"); |
| 466 | } else if (PySequence_Check(x)) { |
| 467 | t = x; x = 0; |
| 468 | n = PySequence_Size(t); if (n < 0) goto end; |
| 469 | if (n != 2 && (n != 3 || !c)) |
| 470 | TYERR("want sequence of two or three items"); |
| 471 | if ((x = PySequence_GetItem(t, 0)) == 0 || |
| 472 | (y = PySequence_GetItem(t, 1)) == 0 || |
| 473 | (n == 3 && (z = PySequence_GetItem(t, 2)) == 0)) |
| 474 | goto end; |
| 475 | rc = (n == 2) ? ecptxl_2(c, p, x, y) : ecptxl_3(c, p, x, y, z); |
| 476 | goto end; |
| 477 | } else |
| 478 | TYERR("can't convert to curve point"); |
| 479 | goto ok; |
| 480 | |
| 481 | fix: |
| 482 | if (c) EC_IN(c, p, p); |
| 483 | ok: |
| 484 | rc = 0; |
| 485 | end: |
| 486 | Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(z); Py_XDECREF(t); |
| 487 | mp_drop(xx); |
| 488 | return (rc); |
| 489 | } |
| 490 | |
| 491 | static int ecptxl(ec_curve *c, ec *p, PyObject *x, PyObject *y, PyObject *z) |
| 492 | { |
| 493 | if (z) |
| 494 | return (ecptxl_3(c, p, x, y, z)); |
| 495 | else if (y) |
| 496 | return (ecptxl_2(c, p, x, y)); |
| 497 | else |
| 498 | return (ecptxl_1(c, p, x)); |
| 499 | } |
| 500 | |
| 501 | static PyObject *ecptnc_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 502 | { |
| 503 | PyObject *x = 0, *y = 0, *z = 0; |
| 504 | ec p = EC_INIT; |
| 505 | static const char *const kwlist[] = { "x", "y", 0 }; |
| 506 | |
| 507 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:new", KWLIST, &x, &y) || |
| 508 | ecptxl(0, &p, x, y, z)) |
| 509 | goto end; |
| 510 | return (ecpt_pywrapout(ty, &p)); |
| 511 | end: |
| 512 | mp_drop(p.x); mp_drop(p.y); mp_drop(p.z); |
| 513 | return (0); |
| 514 | } |
| 515 | |
| 516 | static PyObject *ecpt_pyint(PyObject *me) |
| 517 | { |
| 518 | ec p = EC_INIT; |
| 519 | long l; |
| 520 | PyObject *rc = 0; |
| 521 | if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity"); |
| 522 | getecptout(&p, me); |
| 523 | if (!mp_tolong_checked(p.x, &l, 0)) rc = PyInt_FromLong(l); |
| 524 | else rc = mp_topylong(p.x); |
| 525 | end: |
| 526 | EC_DESTROY(&p); |
| 527 | return (rc); |
| 528 | } |
| 529 | |
| 530 | static PyObject *ecpt_pylong(PyObject *me) |
| 531 | { |
| 532 | ec p = EC_INIT; |
| 533 | PyObject *rc = 0; |
| 534 | if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity"); |
| 535 | getecptout(&p, me); |
| 536 | rc = mp_topylong(p.x); |
| 537 | end: |
| 538 | EC_DESTROY(&p); |
| 539 | return (rc); |
| 540 | } |
| 541 | |
| 542 | static PyObject *ecpt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 543 | { |
| 544 | PyObject *x = 0, *y = 0, *z = 0; |
| 545 | ec p = EC_INIT; |
| 546 | static const char *const kwlist[] = { "x", "y", "z", 0 }; |
| 547 | |
| 548 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OOO:new", KWLIST, |
| 549 | &x, &y, &z) || |
| 550 | ecptxl(ECCURVE_C(ty), &p, x, y, z)) |
| 551 | goto end; |
| 552 | return (ecpt_pywrap((PyObject *)ty, &p)); |
| 553 | end: |
| 554 | mp_drop(p.x); mp_drop(p.y); mp_drop(p.z); |
| 555 | return (0); |
| 556 | } |
| 557 | |
| 558 | static PyGetSetDef ecptnc_pygetset[] = { |
| 559 | #define GETSETNAME(op, name) epnc##op##_##name |
| 560 | GET (ix, "P.ix -> integer x coordinate of P") |
| 561 | GET (iy, "P.iy -> integer y coordinate of P") |
| 562 | GET (point, "P.point -> standalone curve point (no-op)") |
| 563 | #undef GETSETNAME |
| 564 | { 0 } |
| 565 | }; |
| 566 | |
| 567 | static PyMethodDef ecptnc_pymethods[] = { |
| 568 | #define METHNAME(func) epmeth_##func |
| 569 | METH (tobuf, "X.tobuf() -> BIN") |
| 570 | #undef METHNAME |
| 571 | { 0 } |
| 572 | }; |
| 573 | |
| 574 | static PyNumberMethods ecpt_pynumber = { |
| 575 | 0, /* @nb_add@ */ |
| 576 | 0, /* @nb_subtract@ */ |
| 577 | 0, /* @nb_multiply@ */ |
| 578 | 0, /* @nb_divide@ */ |
| 579 | 0, /* @nb_remainder@ */ |
| 580 | 0, /* @nb_divmod@ */ |
| 581 | 0, /* @nb_power@ */ |
| 582 | 0, /* @nb_negative@ */ |
| 583 | 0, /* @nb_positive@ */ |
| 584 | 0, /* @nb_absolute@ */ |
| 585 | ecpt_pynonzerop, /* @nb_nonzero@ */ |
| 586 | 0, /* @nb_invert@ */ |
| 587 | 0, /* @nb_lshift@ */ |
| 588 | 0, /* @nb_rshift@ */ |
| 589 | 0, /* @nb_and@ */ |
| 590 | 0, /* @nb_xor@ */ |
| 591 | 0, /* @nb_or@ */ |
| 592 | 0, /* @nb_coerce@ */ |
| 593 | ecpt_pyint, /* @nb_int@ */ |
| 594 | ecpt_pylong, /* @nb_long@ */ |
| 595 | 0, /* @nb_float@ */ |
| 596 | 0, /* @nb_oct@ */ |
| 597 | 0, /* @nb_hex@ */ |
| 598 | |
| 599 | 0, /* @nb_inplace_add@ */ |
| 600 | 0, /* @nb_inplace_subtract@ */ |
| 601 | 0, /* @nb_inplace_multiply@ */ |
| 602 | 0, /* @nb_inplace_divide@ */ |
| 603 | 0, /* @nb_inplace_remainder@ */ |
| 604 | 0, /* @nb_inplace_power@ */ |
| 605 | 0, /* @nb_inplace_lshift@ */ |
| 606 | 0, /* @nb_inplace_rshift@ */ |
| 607 | 0, /* @nb_inplace_and@ */ |
| 608 | 0, /* @nb_inplace_xor@ */ |
| 609 | 0, /* @nb_inplace_or@ */ |
| 610 | |
| 611 | 0, /* @nb_floor_divide@ */ |
| 612 | 0, /* @nb_true_divide@ */ |
| 613 | 0, /* @nb_inplace_floor_divide@ */ |
| 614 | 0, /* @nb_inplace_true_divide@ */ |
| 615 | }; |
| 616 | |
| 617 | static PyTypeObject ecpt_pytype_skel = { |
| 618 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 619 | "ECPt", /* @tp_name@ */ |
| 620 | sizeof(ecpt_pyobj), /* @tp_basicsize@ */ |
| 621 | 0, /* @tp_itemsize@ */ |
| 622 | |
| 623 | ecpt_pydealloc, /* @tp_dealloc@ */ |
| 624 | 0, /* @tp_print@ */ |
| 625 | 0, /* @tp_getattr@ */ |
| 626 | 0, /* @tp_setattr@ */ |
| 627 | 0, /* @tp_compare@ */ |
| 628 | 0, /* @tp_repr@ */ |
| 629 | &ecpt_pynumber, /* @tp_as_number@ */ |
| 630 | 0, /* @tp_as_sequence@ */ |
| 631 | 0, /* @tp_as_mapping@ */ |
| 632 | ecpt_pyhash, /* @tp_hash@ */ |
| 633 | 0, /* @tp_call@ */ |
| 634 | 0, /* @tp_str@ */ |
| 635 | 0, /* @tp_getattro@ */ |
| 636 | 0, /* @tp_setattro@ */ |
| 637 | 0, /* @tp_as_buffer@ */ |
| 638 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 639 | Py_TPFLAGS_CHECKTYPES | |
| 640 | Py_TPFLAGS_BASETYPE, |
| 641 | |
| 642 | /* @tp_doc@ */ |
| 643 | "ECPt([X, [Y]]): elliptic curve points, not associated with any curve.\n\ |
| 644 | X alone may be None, an existing point, a string 'X, Y', an\n\ |
| 645 | x-coordinate, or a pair (X, Y); X and Y should be a coordinate pair.", |
| 646 | |
| 647 | 0, /* @tp_traverse@ */ |
| 648 | 0, /* @tp_clear@ */ |
| 649 | ecpt_pyrichcompare, /* @tp_richcompare@ */ |
| 650 | 0, /* @tp_weaklistoffset@ */ |
| 651 | 0, /* @tp_iter@ */ |
| 652 | 0, /* @tp_iternext@ */ |
| 653 | ecptnc_pymethods, /* @tp_methods@ */ |
| 654 | 0, /* @tp_members@ */ |
| 655 | ecptnc_pygetset, /* @tp_getset@ */ |
| 656 | 0, /* @tp_base@ */ |
| 657 | 0, /* @tp_dict@ */ |
| 658 | 0, /* @tp_descr_get@ */ |
| 659 | 0, /* @tp_descr_set@ */ |
| 660 | 0, /* @tp_dictoffset@ */ |
| 661 | 0, /* @tp_init@ */ |
| 662 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 663 | ecptnc_pynew, /* @tp_new@ */ |
| 664 | 0, /* @tp_free@ */ |
| 665 | 0 /* @tp_is_gc@ */ |
| 666 | }; |
| 667 | |
| 668 | static PyGetSetDef ecpt_pygetset[] = { |
| 669 | #define GETSETNAME(op, name) ep##op##_##name |
| 670 | GET (curve, "P.curve -> elliptic curve containing P") |
| 671 | GET (point, "P.point -> standalone curve point") |
| 672 | GET (x, "P.x -> Cartesian x coordinate of P") |
| 673 | GET (y, "P.y -> Cartesian y coordinate of P") |
| 674 | GET (_x, "P._x -> internal x coordinate of P") |
| 675 | GET (_y, "P._y -> internal y coordinate of P") |
| 676 | GET (_z, "P._z -> internal z coordinate of P, or None") |
| 677 | #undef GETSETNAME |
| 678 | { 0 } |
| 679 | }; |
| 680 | |
| 681 | static PyMethodDef ecpt_pymethods[] = { |
| 682 | #define METHNAME(func) epmeth_##func |
| 683 | METH (toraw, "X.toraw() -> BIN") |
| 684 | KWMETH(ec2osp, "X.ec2osp([flags = EC_EXPLY]) -> BIN") |
| 685 | METH (dbl, "X.dbl() -> X + X") |
| 686 | METH (oncurvep, "X.oncurvep() -> BOOL") |
| 687 | #undef METHNAME |
| 688 | { 0 } |
| 689 | }; |
| 690 | |
| 691 | static PyNumberMethods ecptcurve_pynumber = { |
| 692 | ecpt_pyadd, /* @nb_add@ */ |
| 693 | ecpt_pysub, /* @nb_subtract@ */ |
| 694 | ecpt_pymul, /* @nb_multiply@ */ |
| 695 | 0, /* @nb_divide@ */ |
| 696 | 0, /* @nb_remainder@ */ |
| 697 | 0, /* @nb_divmod@ */ |
| 698 | 0, /* @nb_power@ */ |
| 699 | ecpt_pyneg, /* @nb_negative@ */ |
| 700 | ecpt_pyid, /* @nb_positive@ */ |
| 701 | 0, /* @nb_absolute@ */ |
| 702 | 0, /* @nb_nonzero@ */ |
| 703 | 0, /* @nb_invert@ */ |
| 704 | 0, /* @nb_lshift@ */ |
| 705 | 0, /* @nb_rshift@ */ |
| 706 | 0, /* @nb_and@ */ |
| 707 | 0, /* @nb_xor@ */ |
| 708 | 0, /* @nb_or@ */ |
| 709 | 0, /* @nb_coerce@ */ |
| 710 | 0, /* @nb_int@ */ |
| 711 | 0, /* @nb_long@ */ |
| 712 | 0, /* @nb_float@ */ |
| 713 | 0, /* @nb_oct@ */ |
| 714 | 0, /* @nb_hex@ */ |
| 715 | |
| 716 | 0, /* @nb_inplace_add@ */ |
| 717 | 0, /* @nb_inplace_subtract@ */ |
| 718 | 0, /* @nb_inplace_multiply@ */ |
| 719 | 0, /* @nb_inplace_divide@ */ |
| 720 | 0, /* @nb_inplace_remainder@ */ |
| 721 | 0, /* @nb_inplace_power@ */ |
| 722 | 0, /* @nb_inplace_lshift@ */ |
| 723 | 0, /* @nb_inplace_rshift@ */ |
| 724 | 0, /* @nb_inplace_and@ */ |
| 725 | 0, /* @nb_inplace_xor@ */ |
| 726 | 0, /* @nb_inplace_or@ */ |
| 727 | |
| 728 | 0, /* @nb_floor_divide@ */ |
| 729 | 0, /* @nb_true_divide@ */ |
| 730 | 0, /* @nb_inplace_floor_divide@ */ |
| 731 | 0, /* @nb_inplace_true_divide@ */ |
| 732 | }; |
| 733 | |
| 734 | static PyTypeObject ecptcurve_pytype_skel = { |
| 735 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 736 | "ECPtCurve", /* @tp_name@ */ |
| 737 | sizeof(ecpt_pyobj), /* @tp_basicsize@ */ |
| 738 | 0, /* @tp_itemsize@ */ |
| 739 | |
| 740 | ecpt_pydealloc, /* @tp_dealloc@ */ |
| 741 | 0, /* @tp_print@ */ |
| 742 | 0, /* @tp_getattr@ */ |
| 743 | 0, /* @tp_setattr@ */ |
| 744 | 0, /* @tp_compare@ */ |
| 745 | 0, /* @tp_repr@ */ |
| 746 | &ecptcurve_pynumber, /* @tp_as_number@ */ |
| 747 | 0, /* @tp_as_sequence@ */ |
| 748 | 0, /* @tp_as_mapping@ */ |
| 749 | 0, /* @tp_hash@ */ |
| 750 | 0, /* @tp_call@ */ |
| 751 | 0, /* @tp_str@ */ |
| 752 | 0, /* @tp_getattro@ */ |
| 753 | 0, /* @tp_setattro@ */ |
| 754 | 0, /* @tp_as_buffer@ */ |
| 755 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 756 | Py_TPFLAGS_CHECKTYPES | |
| 757 | Py_TPFLAGS_BASETYPE, |
| 758 | |
| 759 | /* @tp_doc@ */ |
| 760 | "Elliptic curve points; abstract base class for points on given curves.", |
| 761 | |
| 762 | 0, /* @tp_traverse@ */ |
| 763 | 0, /* @tp_clear@ */ |
| 764 | 0, /* @tp_richcompare@ */ |
| 765 | 0, /* @tp_weaklistoffset@ */ |
| 766 | 0, /* @tp_iter@ */ |
| 767 | 0, /* @tp_iternext@ */ |
| 768 | ecpt_pymethods, /* @tp_methods@ */ |
| 769 | 0, /* @tp_members@ */ |
| 770 | ecpt_pygetset, /* @tp_getset@ */ |
| 771 | 0, /* @tp_base@ */ |
| 772 | 0, /* @tp_dict@ */ |
| 773 | 0, /* @tp_descr_get@ */ |
| 774 | 0, /* @tp_descr_set@ */ |
| 775 | 0, /* @tp_dictoffset@ */ |
| 776 | 0, /* @tp_init@ */ |
| 777 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 778 | abstract_pynew, /* @tp_new@ */ |
| 779 | 0, /* @tp_free@ */ |
| 780 | 0 /* @tp_is_gc@ */ |
| 781 | }; |
| 782 | |
| 783 | /*----- Elliptic curves themselves ----------------------------------------*/ |
| 784 | |
| 785 | static PyObject *eccurve_pyrichcompare(PyObject *x, PyObject *y, int op) |
| 786 | { |
| 787 | int b; |
| 788 | |
| 789 | assert(ECCURVE_PYCHECK(x)); |
| 790 | if (!ECCURVE_PYCHECK(y)) RETURN_NOTIMPL; |
| 791 | b = ec_samep(ECCURVE_C(x), ECCURVE_C(y)); |
| 792 | switch (op) { |
| 793 | case Py_EQ: break; |
| 794 | case Py_NE: b = !b; break; |
| 795 | default: TYERR("can't order elliptic curves"); |
| 796 | } |
| 797 | return (getbool(b)); |
| 798 | end: |
| 799 | return (0); |
| 800 | } |
| 801 | |
| 802 | static PyObject *ecmmul_id(PyObject *me) |
| 803 | { ec p = EC_INIT; return (ecpt_pywrap(me, &p)); } |
| 804 | |
| 805 | static int ecmmul_fill(void *pp, PyObject *me, PyObject *x, PyObject *m) |
| 806 | { |
| 807 | ec_mulfactor *f = pp; |
| 808 | |
| 809 | EC_CREATE(&f->base); |
| 810 | if (getecpt(ECCURVE_C(me), &f->base, x) || |
| 811 | (f->exp = getmp(m)) == 0) |
| 812 | return (-1); |
| 813 | return (0); |
| 814 | } |
| 815 | |
| 816 | static PyObject *ecmmul_exp(PyObject *me, void *pp, int n) |
| 817 | { |
| 818 | ec p = EC_INIT; |
| 819 | ec_immul(ECCURVE_C(me), &p, pp, n); |
| 820 | return (ecpt_pywrap(me, &p)); |
| 821 | } |
| 822 | |
| 823 | static void ecmmul_drop(void *pp) |
| 824 | { |
| 825 | ec_mulfactor *f = pp; |
| 826 | EC_DESTROY(&f->base); |
| 827 | MP_DROP(f->exp); |
| 828 | } |
| 829 | |
| 830 | static PyObject *ecmeth_mmul(PyObject *me, PyObject *arg) |
| 831 | { |
| 832 | return (mexp_common(me, arg, sizeof(ec_mulfactor), |
| 833 | ecmmul_id, ecmmul_fill, ecmmul_exp, ecmmul_drop)); |
| 834 | } |
| 835 | |
| 836 | static PyObject *meth__ECPtCurve_fromraw(PyObject *me, PyObject *arg) |
| 837 | { |
| 838 | char *p; |
| 839 | Py_ssize_t len; |
| 840 | buf b; |
| 841 | PyObject *rc = 0; |
| 842 | ec_curve *cc; |
| 843 | ec pp = EC_INIT; |
| 844 | |
| 845 | if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &len)) |
| 846 | return (0); |
| 847 | buf_init(&b, p, len); |
| 848 | cc = ECCURVE_C(me); |
| 849 | if (ec_getraw(cc, &b, &pp)) |
| 850 | VALERR("bad point"); |
| 851 | EC_IN(cc, &pp, &pp); |
| 852 | rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b)); |
| 853 | end: |
| 854 | return (rc); |
| 855 | } |
| 856 | |
| 857 | static PyObject *meth__ECPtCurve_os2ecp(PyObject *me, |
| 858 | PyObject *arg, PyObject *kw) |
| 859 | { |
| 860 | char *p; |
| 861 | Py_ssize_t len; |
| 862 | buf b; |
| 863 | PyObject *rc = 0; |
| 864 | ec_curve *cc; |
| 865 | unsigned f = EC_XONLY | EC_LSB | EC_SORT | EC_EXPLY; |
| 866 | ec pp = EC_INIT; |
| 867 | static const char *const kwlist[] = { "class", "buf", "flags", 0 }; |
| 868 | |
| 869 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|O&:os2ecp", KWLIST, |
| 870 | &me, &p, &len, convuint, &f)) |
| 871 | return (0); |
| 872 | buf_init(&b, p, len); |
| 873 | cc = ECCURVE_C(me); |
| 874 | if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point"); |
| 875 | EC_IN(cc, &pp, &pp); |
| 876 | rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b)); |
| 877 | end: |
| 878 | return (rc); |
| 879 | } |
| 880 | |
| 881 | static PyObject *meth__ECPt_frombuf(PyObject *me, PyObject *arg) |
| 882 | { |
| 883 | buf b; |
| 884 | char *p; |
| 885 | Py_ssize_t sz; |
| 886 | PyObject *rc = 0; |
| 887 | ec pp = EC_INIT; |
| 888 | |
| 889 | if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end; |
| 890 | buf_init(&b, p, sz); |
| 891 | if (buf_getec(&b, &pp)) VALERR("malformed data"); |
| 892 | rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp), |
| 893 | bytestring_pywrapbuf(&b)); |
| 894 | end: |
| 895 | return (rc); |
| 896 | } |
| 897 | |
| 898 | static PyObject *meth__ECPt_parse(PyObject *me, PyObject *arg) |
| 899 | { |
| 900 | char *p; |
| 901 | qd_parse qd; |
| 902 | PyObject *rc = 0; |
| 903 | ec pp = EC_INIT; |
| 904 | |
| 905 | if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end; |
| 906 | qd.p = p; |
| 907 | qd.e = 0; |
| 908 | if (!ec_ptparse(&qd, &pp)) VALERR(qd.e); |
| 909 | rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p); |
| 910 | end: |
| 911 | return (rc); |
| 912 | } |
| 913 | |
| 914 | static void eccurve_pydealloc(PyObject *me) |
| 915 | { |
| 916 | ec_destroycurve(ECCURVE_C(me)); |
| 917 | Py_DECREF(ECCURVE_FOBJ(me)); |
| 918 | PyType_Type.tp_dealloc(me); |
| 919 | } |
| 920 | |
| 921 | static PyObject *ecmeth_find(PyObject *me, PyObject *arg) |
| 922 | { |
| 923 | PyObject *x; |
| 924 | mp *xx = 0; |
| 925 | ec p = EC_INIT; |
| 926 | PyObject *rc = 0; |
| 927 | |
| 928 | if (!PyArg_ParseTuple(arg, "O:find", &x)) goto end; |
| 929 | if (FIELD_PYCHECK(x) && FE_F(x) == ECCURVE_C(me)->f) |
| 930 | xx = MP_COPY(FE_X(x)); |
| 931 | else if ((xx = getmp(x)) == 0) |
| 932 | goto end; |
| 933 | else |
| 934 | xx = F_IN(ECCURVE_C(me)->f, xx, xx); |
| 935 | if (EC_FIND(ECCURVE_C(me), &p, xx) == 0) |
| 936 | VALERR("not on the curve"); |
| 937 | rc = ecpt_pywrap(me, &p); |
| 938 | end: |
| 939 | if (xx) MP_DROP(xx); |
| 940 | return (rc); |
| 941 | } |
| 942 | |
| 943 | static PyObject *ecmeth_rand(PyObject *me, PyObject *arg, PyObject *kw) |
| 944 | { |
| 945 | static const char *const kwlist[] = { "rng", 0 }; |
| 946 | grand *r = &rand_global; |
| 947 | ec p = EC_INIT; |
| 948 | |
| 949 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", KWLIST, |
| 950 | convgrand, &r)) |
| 951 | return (0); |
| 952 | ec_rand(ECCURVE_C(me), &p, r); |
| 953 | EC_IN(ECCURVE_C(me), &p, &p); |
| 954 | return (ecpt_pywrap(me, &p)); |
| 955 | } |
| 956 | |
| 957 | static PyObject *eccurve_dopywrap(PyTypeObject *ty, |
| 958 | PyObject *fobj, ec_curve *c) |
| 959 | { |
| 960 | eccurve_pyobj *cobj = newtype(ty, 0, c->ops->name); |
| 961 | cobj->c = c; |
| 962 | cobj->fobj = fobj; |
| 963 | cobj->ty.ht_type.tp_basicsize = sizeof(ecpt_pyobj); |
| 964 | cobj->ty.ht_type.tp_base = ecptcurve_pytype; |
| 965 | Py_INCREF(ecptcurve_pytype); |
| 966 | cobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 967 | Py_TPFLAGS_BASETYPE | |
| 968 | Py_TPFLAGS_CHECKTYPES | |
| 969 | Py_TPFLAGS_HEAPTYPE); |
| 970 | cobj->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 971 | cobj->ty.ht_type.tp_free = 0; |
| 972 | cobj->ty.ht_type.tp_new = ecpt_pynew; |
| 973 | typeready(&cobj->ty.ht_type); |
| 974 | return ((PyObject *)cobj); |
| 975 | } |
| 976 | |
| 977 | PyObject *eccurve_pywrap(PyObject *fobj, ec_curve *c) |
| 978 | { |
| 979 | PyTypeObject *ty; |
| 980 | |
| 981 | if (!fobj) |
| 982 | fobj = field_pywrap(c->f); |
| 983 | else |
| 984 | Py_INCREF(fobj); |
| 985 | assert(FIELD_F(fobj) == c->f); |
| 986 | if (STRCMP(EC_NAME(c), ==, "prime")) |
| 987 | ty = ecprimecurve_pytype; |
| 988 | else if (STRCMP(EC_NAME(c), ==, "primeproj")) |
| 989 | ty = ecprimeprojcurve_pytype; |
| 990 | else if (STRCMP(EC_NAME(c), ==, "bin")) |
| 991 | ty = ecbincurve_pytype; |
| 992 | else if (STRCMP(EC_NAME(c), ==, "binproj")) |
| 993 | ty = ecbinprojcurve_pytype; |
| 994 | else |
| 995 | abort(); |
| 996 | return (eccurve_dopywrap(ty, fobj, c)); |
| 997 | } |
| 998 | |
| 999 | static PyObject *eccurve_pynew(PyTypeObject *ty, |
| 1000 | ec_curve *(*make)(field *, mp *, mp *), |
| 1001 | PyObject *arg, PyObject *kw) |
| 1002 | { |
| 1003 | PyObject *fobj; |
| 1004 | PyObject *cobj = 0; |
| 1005 | static const char *const kwlist[] = { "field", "a", "b", 0 }; |
| 1006 | mp *aa = 0, *bb = 0; |
| 1007 | |
| 1008 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&O&", KWLIST, |
| 1009 | field_pytype, &fobj, |
| 1010 | convmp, &aa, convmp, &bb)) |
| 1011 | goto end; |
| 1012 | Py_INCREF(fobj); |
| 1013 | cobj = eccurve_dopywrap(ty, fobj, make(FIELD_F(fobj), aa, bb)); |
| 1014 | end: |
| 1015 | if (aa) MP_DROP(aa); |
| 1016 | if (bb) MP_DROP(bb); |
| 1017 | return (cobj); |
| 1018 | } |
| 1019 | |
| 1020 | static PyObject *meth__ECCurve_parse(PyObject *me, PyObject *arg) |
| 1021 | { |
| 1022 | char *p; |
| 1023 | qd_parse qd; |
| 1024 | ec_curve *c; |
| 1025 | PyObject *rc = 0; |
| 1026 | |
| 1027 | if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) |
| 1028 | goto end; |
| 1029 | qd.p = p; |
| 1030 | qd.e = 0; |
| 1031 | if ((c = ec_curveparse(&qd)) == 0) |
| 1032 | VALERR(qd.e); |
| 1033 | rc = eccurve_pywrap(0, c); |
| 1034 | end: |
| 1035 | return (rc); |
| 1036 | } |
| 1037 | |
| 1038 | static PyObject *ecget_name(PyObject *me, void *hunoz) |
| 1039 | { return (PyString_FromString(EC_NAME(ECCURVE_C(me)))); } |
| 1040 | |
| 1041 | static PyObject *ecget_a(PyObject *me, void *hunoz) |
| 1042 | { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->a))); } |
| 1043 | |
| 1044 | static PyObject *ecget_b(PyObject *me, void *hunoz) |
| 1045 | { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->b))); } |
| 1046 | |
| 1047 | static PyObject *ecget_field(PyObject *me, void *hunoz) |
| 1048 | { RETURN_OBJ(ECCURVE_FOBJ(me)); } |
| 1049 | |
| 1050 | static PyObject *ecget_inf(PyObject *me, void *hunoz) |
| 1051 | { ec inf = EC_INIT; return (ecpt_pywrap(me, &inf)); } |
| 1052 | |
| 1053 | static PyGetSetDef eccurve_pygetset[] = { |
| 1054 | #define GETSETNAME(op, name) ec##op##_##name |
| 1055 | GET (name, "E.name -> name of this kind of curve") |
| 1056 | GET (a, "E.a -> first parameter of curve") |
| 1057 | GET (b, "E.b -> second parameter of curve") |
| 1058 | GET (field, "E.field -> finite field containing this curve") |
| 1059 | GET (inf, "E.inf -> point at infinity of this curve") |
| 1060 | #undef GETSETNAME |
| 1061 | { 0 } |
| 1062 | }; |
| 1063 | |
| 1064 | static PyMethodDef eccurve_pymethods[] = { |
| 1065 | #define METHNAME(name) ecmeth_##name |
| 1066 | METH (mmul, "\ |
| 1067 | E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...") |
| 1068 | METH (find, "E.find(X) -> P") |
| 1069 | KWMETH(rand, "E.rand([rng = rand]) -> P") |
| 1070 | #undef METHNAME |
| 1071 | { 0 } |
| 1072 | }; |
| 1073 | |
| 1074 | static PyTypeObject eccurve_pytype_skel = { |
| 1075 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1076 | "ECCurve", /* @tp_name@ */ |
| 1077 | sizeof(eccurve_pyobj), /* @tp_basicsize@ */ |
| 1078 | 0, /* @tp_itemsize@ */ |
| 1079 | |
| 1080 | eccurve_pydealloc, /* @tp_dealloc@ */ |
| 1081 | 0, /* @tp_print@ */ |
| 1082 | 0, /* @tp_getattr@ */ |
| 1083 | 0, /* @tp_setattr@ */ |
| 1084 | 0, /* @tp_compare@ */ |
| 1085 | 0, /* @tp_repr@ */ |
| 1086 | 0, /* @tp_as_number@ */ |
| 1087 | 0, /* @tp_as_sequence@ */ |
| 1088 | 0, /* @tp_as_mapping@ */ |
| 1089 | 0, /* @tp_hash@ */ |
| 1090 | 0, /* @tp_call@ */ |
| 1091 | 0, /* @tp_str@ */ |
| 1092 | 0, /* @tp_getattro@ */ |
| 1093 | 0, /* @tp_setattro@ */ |
| 1094 | 0, /* @tp_as_buffer@ */ |
| 1095 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1096 | Py_TPFLAGS_BASETYPE, |
| 1097 | |
| 1098 | /* @tp_doc@ */ |
| 1099 | "An elliptic curve. Abstract class.", |
| 1100 | |
| 1101 | 0, /* @tp_traverse@ */ |
| 1102 | 0, /* @tp_clear@ */ |
| 1103 | eccurve_pyrichcompare, /* @tp_richcompare@ */ |
| 1104 | 0, /* @tp_weaklistoffset@ */ |
| 1105 | 0, /* @tp_iter@ */ |
| 1106 | 0, /* @tp_iternext@ */ |
| 1107 | eccurve_pymethods, /* @tp_methods@ */ |
| 1108 | 0, /* @tp_members@ */ |
| 1109 | eccurve_pygetset, /* @tp_getset@ */ |
| 1110 | 0, /* @tp_base@ */ |
| 1111 | 0, /* @tp_dict@ */ |
| 1112 | 0, /* @tp_descr_get@ */ |
| 1113 | 0, /* @tp_descr_set@ */ |
| 1114 | 0, /* @tp_dictoffset@ */ |
| 1115 | 0, /* @tp_init@ */ |
| 1116 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1117 | abstract_pynew, /* @tp_new@ */ |
| 1118 | 0, /* @tp_free@ */ |
| 1119 | 0 /* @tp_is_gc@ */ |
| 1120 | }; |
| 1121 | |
| 1122 | static PyObject *ecprimecurve_pynew(PyTypeObject *ty, |
| 1123 | PyObject *arg, PyObject *kw) |
| 1124 | { |
| 1125 | return (eccurve_pynew(ty, ec_prime, arg, kw)); |
| 1126 | } |
| 1127 | |
| 1128 | static PyTypeObject ecprimecurve_pytype_skel = { |
| 1129 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1130 | "ECPrimeCurve", /* @tp_name@ */ |
| 1131 | sizeof(eccurve_pyobj), /* @tp_basicsize@ */ |
| 1132 | 0, /* @tp_itemsize@ */ |
| 1133 | |
| 1134 | eccurve_pydealloc, /* @tp_dealloc@ */ |
| 1135 | 0, /* @tp_print@ */ |
| 1136 | 0, /* @tp_getattr@ */ |
| 1137 | 0, /* @tp_setattr@ */ |
| 1138 | 0, /* @tp_compare@ */ |
| 1139 | 0, /* @tp_repr@ */ |
| 1140 | 0, /* @tp_as_number@ */ |
| 1141 | 0, /* @tp_as_sequence@ */ |
| 1142 | 0, /* @tp_as_mapping@ */ |
| 1143 | 0, /* @tp_hash@ */ |
| 1144 | 0, /* @tp_call@ */ |
| 1145 | 0, /* @tp_str@ */ |
| 1146 | 0, /* @tp_getattro@ */ |
| 1147 | 0, /* @tp_setattro@ */ |
| 1148 | 0, /* @tp_as_buffer@ */ |
| 1149 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1150 | Py_TPFLAGS_BASETYPE, |
| 1151 | |
| 1152 | /* @tp_doc@ */ |
| 1153 | "ECPrimeCurve(FIELD, A, B): an elliptic curve over a prime field.\n\ |
| 1154 | Use ECPrimeProjCurve instead.", |
| 1155 | |
| 1156 | 0, /* @tp_traverse@ */ |
| 1157 | 0, /* @tp_clear@ */ |
| 1158 | eccurve_pyrichcompare, /* @tp_richcompare@ */ |
| 1159 | 0, /* @tp_weaklistoffset@ */ |
| 1160 | 0, /* @tp_iter@ */ |
| 1161 | 0, /* @tp_iternext@ */ |
| 1162 | 0, /* @tp_methods@ */ |
| 1163 | 0, /* @tp_members@ */ |
| 1164 | 0, /* @tp_getset@ */ |
| 1165 | 0, /* @tp_base@ */ |
| 1166 | 0, /* @tp_dict@ */ |
| 1167 | 0, /* @tp_descr_get@ */ |
| 1168 | 0, /* @tp_descr_set@ */ |
| 1169 | 0, /* @tp_dictoffset@ */ |
| 1170 | 0, /* @tp_init@ */ |
| 1171 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1172 | ecprimecurve_pynew, /* @tp_new@ */ |
| 1173 | 0, /* @tp_free@ */ |
| 1174 | 0 /* @tp_is_gc@ */ |
| 1175 | }; |
| 1176 | |
| 1177 | static PyObject *ecprimeprojcurve_pynew(PyTypeObject *ty, |
| 1178 | PyObject *arg, PyObject *kw) |
| 1179 | { |
| 1180 | return (eccurve_pynew(ty, ec_primeproj, arg, kw)); |
| 1181 | } |
| 1182 | |
| 1183 | static PyTypeObject ecprimeprojcurve_pytype_skel = { |
| 1184 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1185 | "ECPrimeProjCurve", /* @tp_name@ */ |
| 1186 | sizeof(eccurve_pyobj), /* @tp_basicsize@ */ |
| 1187 | 0, /* @tp_itemsize@ */ |
| 1188 | |
| 1189 | eccurve_pydealloc, /* @tp_dealloc@ */ |
| 1190 | 0, /* @tp_print@ */ |
| 1191 | 0, /* @tp_getattr@ */ |
| 1192 | 0, /* @tp_setattr@ */ |
| 1193 | 0, /* @tp_compare@ */ |
| 1194 | 0, /* @tp_repr@ */ |
| 1195 | 0, /* @tp_as_number@ */ |
| 1196 | 0, /* @tp_as_sequence@ */ |
| 1197 | 0, /* @tp_as_mapping@ */ |
| 1198 | 0, /* @tp_hash@ */ |
| 1199 | 0, /* @tp_call@ */ |
| 1200 | 0, /* @tp_str@ */ |
| 1201 | 0, /* @tp_getattro@ */ |
| 1202 | 0, /* @tp_setattro@ */ |
| 1203 | 0, /* @tp_as_buffer@ */ |
| 1204 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1205 | Py_TPFLAGS_BASETYPE, |
| 1206 | |
| 1207 | /* @tp_doc@ */ |
| 1208 | "ECPrimeProjCurve(FIELD, A, B): an elliptic curve over a prime field\n\ |
| 1209 | using projective coordinates.", |
| 1210 | |
| 1211 | 0, /* @tp_traverse@ */ |
| 1212 | 0, /* @tp_clear@ */ |
| 1213 | eccurve_pyrichcompare, /* @tp_richcompare@ */ |
| 1214 | 0, /* @tp_weaklistoffset@ */ |
| 1215 | 0, /* @tp_iter@ */ |
| 1216 | 0, /* @tp_iternext@ */ |
| 1217 | 0, /* @tp_methods@ */ |
| 1218 | 0, /* @tp_members@ */ |
| 1219 | 0, /* @tp_getset@ */ |
| 1220 | 0, /* @tp_base@ */ |
| 1221 | 0, /* @tp_dict@ */ |
| 1222 | 0, /* @tp_descr_get@ */ |
| 1223 | 0, /* @tp_descr_set@ */ |
| 1224 | 0, /* @tp_dictoffset@ */ |
| 1225 | 0, /* @tp_init@ */ |
| 1226 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1227 | ecprimeprojcurve_pynew, /* @tp_new@ */ |
| 1228 | 0, /* @tp_free@ */ |
| 1229 | 0 /* @tp_is_gc@ */ |
| 1230 | }; |
| 1231 | |
| 1232 | static PyObject *ecbincurve_pynew(PyTypeObject *ty, |
| 1233 | PyObject *arg, PyObject *kw) |
| 1234 | { |
| 1235 | return (eccurve_pynew(ty, ec_bin, arg, kw)); |
| 1236 | } |
| 1237 | |
| 1238 | static PyTypeObject ecbincurve_pytype_skel = { |
| 1239 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1240 | "ECBinCurve", /* @tp_name@ */ |
| 1241 | sizeof(eccurve_pyobj), /* @tp_basicsize@ */ |
| 1242 | 0, /* @tp_itemsize@ */ |
| 1243 | |
| 1244 | eccurve_pydealloc, /* @tp_dealloc@ */ |
| 1245 | 0, /* @tp_print@ */ |
| 1246 | 0, /* @tp_getattr@ */ |
| 1247 | 0, /* @tp_setattr@ */ |
| 1248 | 0, /* @tp_compare@ */ |
| 1249 | 0, /* @tp_repr@ */ |
| 1250 | 0, /* @tp_as_number@ */ |
| 1251 | 0, /* @tp_as_sequence@ */ |
| 1252 | 0, /* @tp_as_mapping@ */ |
| 1253 | 0, /* @tp_hash@ */ |
| 1254 | 0, /* @tp_call@ */ |
| 1255 | 0, /* @tp_str@ */ |
| 1256 | 0, /* @tp_getattro@ */ |
| 1257 | 0, /* @tp_setattro@ */ |
| 1258 | 0, /* @tp_as_buffer@ */ |
| 1259 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1260 | Py_TPFLAGS_BASETYPE, |
| 1261 | |
| 1262 | /* @tp_doc@ */ |
| 1263 | "ECBinCurve(FIELD, A, B): an elliptic curve over a binary field.\n\ |
| 1264 | Use ECBinProjCurve instead.", |
| 1265 | |
| 1266 | 0, /* @tp_traverse@ */ |
| 1267 | 0, /* @tp_clear@ */ |
| 1268 | eccurve_pyrichcompare, /* @tp_richcompare@ */ |
| 1269 | 0, /* @tp_weaklistoffset@ */ |
| 1270 | 0, /* @tp_iter@ */ |
| 1271 | 0, /* @tp_iternext@ */ |
| 1272 | 0, /* @tp_methods@ */ |
| 1273 | 0, /* @tp_members@ */ |
| 1274 | 0, /* @tp_getset@ */ |
| 1275 | 0, /* @tp_base@ */ |
| 1276 | 0, /* @tp_dict@ */ |
| 1277 | 0, /* @tp_descr_get@ */ |
| 1278 | 0, /* @tp_descr_set@ */ |
| 1279 | 0, /* @tp_dictoffset@ */ |
| 1280 | 0, /* @tp_init@ */ |
| 1281 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1282 | ecbincurve_pynew, /* @tp_new@ */ |
| 1283 | 0, /* @tp_free@ */ |
| 1284 | 0 /* @tp_is_gc@ */ |
| 1285 | }; |
| 1286 | |
| 1287 | static PyObject *ecbinprojcurve_pynew(PyTypeObject *ty, |
| 1288 | PyObject *arg, PyObject *kw) |
| 1289 | { |
| 1290 | return (eccurve_pynew(ty, ec_binproj, arg, kw)); |
| 1291 | } |
| 1292 | |
| 1293 | static PyTypeObject ecbinprojcurve_pytype_skel = { |
| 1294 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1295 | "ECBinProjCurve", /* @tp_name@ */ |
| 1296 | sizeof(eccurve_pyobj), /* @tp_basicsize@ */ |
| 1297 | 0, /* @tp_itemsize@ */ |
| 1298 | |
| 1299 | eccurve_pydealloc, /* @tp_dealloc@ */ |
| 1300 | 0, /* @tp_print@ */ |
| 1301 | 0, /* @tp_getattr@ */ |
| 1302 | 0, /* @tp_setattr@ */ |
| 1303 | 0, /* @tp_compare@ */ |
| 1304 | 0, /* @tp_repr@ */ |
| 1305 | 0, /* @tp_as_number@ */ |
| 1306 | 0, /* @tp_as_sequence@ */ |
| 1307 | 0, /* @tp_as_mapping@ */ |
| 1308 | 0, /* @tp_hash@ */ |
| 1309 | 0, /* @tp_call@ */ |
| 1310 | 0, /* @tp_str@ */ |
| 1311 | 0, /* @tp_getattro@ */ |
| 1312 | 0, /* @tp_setattro@ */ |
| 1313 | 0, /* @tp_as_buffer@ */ |
| 1314 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1315 | Py_TPFLAGS_BASETYPE, |
| 1316 | |
| 1317 | /* @tp_doc@ */ |
| 1318 | "ECBinProjCurve(FIELD, A, B): an elliptic curve over a binary field,\n\ |
| 1319 | using projective coordinates.", |
| 1320 | |
| 1321 | 0, /* @tp_traverse@ */ |
| 1322 | 0, /* @tp_clear@ */ |
| 1323 | eccurve_pyrichcompare, /* @tp_richcompare@ */ |
| 1324 | 0, /* @tp_weaklistoffset@ */ |
| 1325 | 0, /* @tp_iter@ */ |
| 1326 | 0, /* @tp_iternext@ */ |
| 1327 | 0, /* @tp_methods@ */ |
| 1328 | 0, /* @tp_members@ */ |
| 1329 | 0, /* @tp_getset@ */ |
| 1330 | 0, /* @tp_base@ */ |
| 1331 | 0, /* @tp_dict@ */ |
| 1332 | 0, /* @tp_descr_get@ */ |
| 1333 | 0, /* @tp_descr_set@ */ |
| 1334 | 0, /* @tp_dictoffset@ */ |
| 1335 | 0, /* @tp_init@ */ |
| 1336 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1337 | ecbinprojcurve_pynew, /* @tp_new@ */ |
| 1338 | 0, /* @tp_free@ */ |
| 1339 | 0 /* @tp_is_gc@ */ |
| 1340 | }; |
| 1341 | |
| 1342 | /*----- Curve info --------------------------------------------------------*/ |
| 1343 | |
| 1344 | static int ncurves = -1; |
| 1345 | |
| 1346 | void ecinfo_copy(ec_info *eic, const ec_info *ei) |
| 1347 | { |
| 1348 | eic->c = eccurve_copy(ei->c); |
| 1349 | EC_CREATE(&eic->g); |
| 1350 | EC_COPY(&eic->g, &ei->g); |
| 1351 | eic->r = MP_COPY(ei->r); |
| 1352 | eic->h = MP_COPY(ei->h); |
| 1353 | } |
| 1354 | |
| 1355 | PyObject *ecinfo_pywrap(ec_info *ei) |
| 1356 | { |
| 1357 | ecinfo_pyobj *o; |
| 1358 | |
| 1359 | o = PyObject_NEW(ecinfo_pyobj, ecinfo_pytype); |
| 1360 | o->ei = *ei; |
| 1361 | o->cobj = eccurve_pywrap(0, o->ei.c); |
| 1362 | Py_INCREF(o->cobj); |
| 1363 | return ((PyObject *)o); |
| 1364 | } |
| 1365 | |
| 1366 | static void ecinfo_pydealloc(PyObject *me) |
| 1367 | { |
| 1368 | ec_info *ei = ECINFO_EI(me); |
| 1369 | EC_DESTROY(&ei->g); |
| 1370 | MP_DROP(ei->r); |
| 1371 | MP_DROP(ei->h); |
| 1372 | Py_DECREF(ECINFO_COBJ(me)); |
| 1373 | FREEOBJ(me); |
| 1374 | } |
| 1375 | |
| 1376 | static PyObject *ecinfo_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 1377 | { |
| 1378 | ec_info ei = { 0 }; |
| 1379 | PyObject *e, *g; |
| 1380 | static const char *const kwlist[] = { "curve", "G", "r", "h", 0 }; |
| 1381 | ecinfo_pyobj *rc = 0; |
| 1382 | |
| 1383 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!O&O&:new", KWLIST, |
| 1384 | eccurve_pytype, &e, ecpt_pytype, &g, |
| 1385 | convmp, &ei.r, convmp, &ei.h)) |
| 1386 | goto end; |
| 1387 | if (ECPT_C(g) != ECCURVE_C(e) && !ec_samep(ECPT_C(g), ECCURVE_C(e))) |
| 1388 | TYERR("point not from this curve"); |
| 1389 | ei.c = ECCURVE_C(e); |
| 1390 | EC_CREATE(&ei.g); |
| 1391 | EC_OUT(ei.c, &ei.g, ECPT_P(g)); |
| 1392 | rc = (ecinfo_pyobj *)ty->tp_alloc(ty, 0); |
| 1393 | rc->ei = ei; |
| 1394 | rc->cobj = e; |
| 1395 | Py_INCREF(rc->cobj); |
| 1396 | return ((PyObject *)rc); |
| 1397 | |
| 1398 | end: |
| 1399 | mp_drop(ei.r); |
| 1400 | mp_drop(ei.h); |
| 1401 | return (0); |
| 1402 | } |
| 1403 | |
| 1404 | static PyObject *meth__ECInfo_parse(PyObject *me, PyObject *arg) |
| 1405 | { |
| 1406 | char *p; |
| 1407 | qd_parse qd; |
| 1408 | ec_info ei; |
| 1409 | PyObject *rc = 0; |
| 1410 | |
| 1411 | if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) |
| 1412 | goto end; |
| 1413 | qd.p = p; |
| 1414 | qd.e = 0; |
| 1415 | if (ec_infoparse(&qd, &ei)) |
| 1416 | VALERR(qd.e); |
| 1417 | rc = Py_BuildValue("(Ns)", ecinfo_pywrap(&ei), qd.p); |
| 1418 | end: |
| 1419 | return (rc); |
| 1420 | } |
| 1421 | |
| 1422 | static PyObject *meth__ECInfo__curven(PyObject *me, PyObject *arg) |
| 1423 | { |
| 1424 | int i; |
| 1425 | ec_info ei; |
| 1426 | PyObject *rc = 0; |
| 1427 | |
| 1428 | if (!PyArg_ParseTuple(arg, "Oi:_curven", &me, &i)) goto end; |
| 1429 | if (i < 0 || i >= ncurves) VALERR("curve index out of range"); |
| 1430 | ec_infofromdata(&ei, ectab[i].data); |
| 1431 | rc = ecinfo_pywrap(&ei); |
| 1432 | end: |
| 1433 | return (rc); |
| 1434 | } |
| 1435 | |
| 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 | { |
| 1451 | static const char *const kwlist[] = { "rng", 0 }; |
| 1452 | grand *r = &rand_global; |
| 1453 | const char *p; |
| 1454 | |
| 1455 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:check", KWLIST, |
| 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 | |
| 1482 | static PyGetSetDef ecinfo_pygetset[] = { |
| 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 | |
| 1492 | static PyMethodDef ecinfo_pymethods[] = { |
| 1493 | #define METHNAME(name) eimeth_##name |
| 1494 | KWMETH(check, "I.check([rng = rand]) -> None") |
| 1495 | #undef METHNAME |
| 1496 | { 0 } |
| 1497 | }; |
| 1498 | |
| 1499 | static PyTypeObject ecinfo_pytype_skel = { |
| 1500 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1501 | "ECInfo", /* @tp_name@ */ |
| 1502 | sizeof(ecinfo_pyobj), /* @tp_basicsize@ */ |
| 1503 | 0, /* @tp_itemsize@ */ |
| 1504 | |
| 1505 | ecinfo_pydealloc, /* @tp_dealloc@ */ |
| 1506 | 0, /* @tp_print@ */ |
| 1507 | 0, /* @tp_getattr@ */ |
| 1508 | 0, /* @tp_setattr@ */ |
| 1509 | 0, /* @tp_compare@ */ |
| 1510 | 0, /* @tp_repr@ */ |
| 1511 | 0, /* @tp_as_number@ */ |
| 1512 | 0, /* @tp_as_sequence@ */ |
| 1513 | 0, /* @tp_as_mapping@ */ |
| 1514 | 0, /* @tp_hash@ */ |
| 1515 | 0, /* @tp_call@ */ |
| 1516 | 0, /* @tp_str@ */ |
| 1517 | 0, /* @tp_getattro@ */ |
| 1518 | 0, /* @tp_setattro@ */ |
| 1519 | 0, /* @tp_as_buffer@ */ |
| 1520 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1521 | Py_TPFLAGS_BASETYPE, |
| 1522 | |
| 1523 | /* @tp_doc@ */ |
| 1524 | "ECInfo(CURVE, G, R, H): elliptic curve domain parameters.", |
| 1525 | |
| 1526 | 0, /* @tp_traverse@ */ |
| 1527 | 0, /* @tp_clear@ */ |
| 1528 | ecinfo_pyrichcompare, /* @tp_richcompare@ */ |
| 1529 | 0, /* @tp_weaklistoffset@ */ |
| 1530 | 0, /* @tp_iter@ */ |
| 1531 | 0, /* @tp_iternext@ */ |
| 1532 | ecinfo_pymethods, /* @tp_methods@ */ |
| 1533 | 0, /* @tp_members@ */ |
| 1534 | ecinfo_pygetset, /* @tp_getset@ */ |
| 1535 | 0, /* @tp_base@ */ |
| 1536 | 0, /* @tp_dict@ */ |
| 1537 | 0, /* @tp_descr_get@ */ |
| 1538 | 0, /* @tp_descr_set@ */ |
| 1539 | 0, /* @tp_dictoffset@ */ |
| 1540 | 0, /* @tp_init@ */ |
| 1541 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1542 | ecinfo_pynew, /* @tp_new@ */ |
| 1543 | 0, /* @tp_free@ */ |
| 1544 | 0 /* @tp_is_gc@ */ |
| 1545 | }; |
| 1546 | |
| 1547 | /*----- Setup -------------------------------------------------------------*/ |
| 1548 | |
| 1549 | static PyMethodDef methods[] = { |
| 1550 | #define METHNAME(func) meth_##func |
| 1551 | METH (_ECPt_frombuf, "frombuf(E, STR) -> (P, REST)") |
| 1552 | METH (_ECPtCurve_fromraw, "fromraw(E, STR) -> (P, REST)") |
| 1553 | KWMETH(_ECPtCurve_os2ecp, "os2ecp(E, STR, [flags = ...]) -> (P, REST)") |
| 1554 | METH (_ECPt_parse, "parse(E, STR) -> (P, REST)") |
| 1555 | METH (_ECCurve_parse, "parse(STR) -> (E, REST)") |
| 1556 | METH (_ECInfo_parse, "parse(STR) -> (I, REST)") |
| 1557 | METH (_ECInfo__curven, "_curven(N) -> I") |
| 1558 | #undef METHNAME |
| 1559 | { 0 } |
| 1560 | }; |
| 1561 | |
| 1562 | void ec_pyinit(void) |
| 1563 | { |
| 1564 | INITTYPE(ecpt, root); |
| 1565 | INITTYPE(ecptcurve, ecpt); |
| 1566 | INITTYPE(eccurve, type); |
| 1567 | INITTYPE(ecprimecurve, eccurve); |
| 1568 | INITTYPE(ecprimeprojcurve, ecprimecurve); |
| 1569 | INITTYPE(ecbincurve, eccurve); |
| 1570 | INITTYPE(ecbinprojcurve, ecbincurve); |
| 1571 | INITTYPE(ecinfo, root); |
| 1572 | addmethods(methods); |
| 1573 | } |
| 1574 | |
| 1575 | static PyObject *namedcurves(void) |
| 1576 | { |
| 1577 | int i, j; |
| 1578 | const char *p; |
| 1579 | PyObject *d, *c; |
| 1580 | |
| 1581 | d = PyDict_New(); |
| 1582 | for (i = 0; ectab[i].name; i++) { |
| 1583 | p = ectab[i].name; |
| 1584 | for (j = 0; j < i; j++) { |
| 1585 | if (ectab[i].data == ectab[j].data) { |
| 1586 | c = PyDict_GetItemString(d, (/*unconst*/ char *)ectab[j].name); |
| 1587 | Py_INCREF(c); |
| 1588 | goto found; |
| 1589 | } |
| 1590 | } |
| 1591 | c = PyInt_FromLong(i); |
| 1592 | found: |
| 1593 | PyDict_SetItemString(d, (/*unconst*/ char *)p, c); |
| 1594 | Py_DECREF(c); |
| 1595 | } |
| 1596 | ncurves = i; |
| 1597 | return (d); |
| 1598 | } |
| 1599 | |
| 1600 | void ec_pyinsert(PyObject *mod) |
| 1601 | { |
| 1602 | INSERT("ECPt", ecpt_pytype); |
| 1603 | INSERT("ECPtCurve", ecptcurve_pytype); |
| 1604 | INSERT("ECCurve", eccurve_pytype); |
| 1605 | INSERT("ECPrimeCurve", ecprimecurve_pytype); |
| 1606 | INSERT("ECPrimeProjCurve", ecprimeprojcurve_pytype); |
| 1607 | INSERT("ECBinCurve", ecbincurve_pytype); |
| 1608 | INSERT("ECBinProjCurve", ecbinprojcurve_pytype); |
| 1609 | INSERT("ECInfo", ecinfo_pytype); |
| 1610 | INSERT("_eccurves", namedcurves()); |
| 1611 | } |
| 1612 | |
| 1613 | /*----- That's all, folks -------------------------------------------------*/ |