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