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