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