ec.c (ecptxl_1): Preparatory reformatting.
[catacomb-python] / ec.c
1 /* -*-c-*-
2 *
3 * Elliptic curves
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Python interface to Catacomb.
11 *
12 * Catacomb/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * Catacomb/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Catacomb/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "catacomb-python.h"
30
31 /*----- Utility functions -------------------------------------------------*/
32
33 PyTypeObject *ecpt_pytype;
34 PyTypeObject *ecptcurve_pytype;
35 PyTypeObject *eccurve_pytype;
36 PyTypeObject *ecprimecurve_pytype;
37 PyTypeObject *ecprimeprojcurve_pytype;
38 PyTypeObject *ecbincurve_pytype;
39 PyTypeObject *ecbinprojcurve_pytype;
40 PyTypeObject *ecinfo_pytype;
41
42 ec_curve *eccurve_copy(ec_curve *c)
43 {
44 field *f;
45 mp *a, *b;
46
47 if ((f = field_copy(c->f)) == 0)
48 return (0);
49 a = F_OUT(f, MP_NEW, c->a);
50 b = F_OUT(f, MP_NEW, c->b);
51 if (STRCMP(EC_NAME(c), ==, "prime"))
52 c = ec_prime(f, a, b);
53 else if (STRCMP(EC_NAME(c), ==, "primeproj"))
54 c = ec_primeproj(f, a, b);
55 else if (STRCMP(EC_NAME(c), ==, "bin"))
56 c = ec_bin(f, a, b);
57 else if (STRCMP(EC_NAME(c), ==, "binproj"))
58 c = ec_binproj(f, a, b);
59 else
60 c = 0;
61 MP_DROP(a);
62 MP_DROP(b);
63 if (!c) F_DESTROY(f);
64 return (c);
65 }
66
67 static PyObject *ecpt_dopywrap(PyObject *cobj, ec_curve *c, ec *p)
68 {
69 ecpt_pyobj *z = PyObject_New(ecpt_pyobj, (PyTypeObject *)cobj);
70 z->p = *p;
71 z->c = c;
72 Py_INCREF(cobj);
73 return ((PyObject *)z);
74 }
75
76 PyObject *ecpt_pywrap(PyObject *cobj, ec *p)
77 { return (ecpt_dopywrap(cobj, ECCURVE_C(cobj), p)); }
78
79 PyObject *ecpt_pywrapout(void *cobj, ec *p)
80 {
81 ec_curve *c;
82
83 if (!PyType_IsSubtype(cobj, ecptcurve_pytype))
84 c = 0;
85 else {
86 c = ECCURVE_C(cobj);
87 EC_IN(ECCURVE_C(cobj), p, p);
88 }
89 return (ecpt_dopywrap(cobj, c, p));
90 }
91
92 int toecpt(ec_curve *c, ec *d, PyObject *p)
93 {
94 if (ECPTCURVE_PYCHECK(p)) {
95 if (ECPT_C(p) != c && !ec_samep(ECPT_C(p), c))
96 return (-1);
97 EC_COPY(d, ECPT_P(p));
98 } else if (ECPT_PYCHECK(p))
99 EC_IN(c, d, ECPT_P(p));
100 else
101 return (-1);
102 return (0);
103 }
104
105 int getecpt(ec_curve *c, ec *d, PyObject *p)
106 {
107 if (toecpt(c, d, p)) {
108 PyErr_Format(PyExc_TypeError, "can't convert %.100s to ecpt",
109 Py_TYPE(p)->tp_name);
110 return (-1);
111 }
112 return (0);
113 }
114
115 void getecptout(ec *d, PyObject *p)
116 {
117 if (ECPTCURVE_PYCHECK(p))
118 EC_OUT(ECPT_C(p), d, ECPT_P(p));
119 else {
120 assert(ECPT_PYCHECK(p));
121 EC_COPY(d, ECPT_P(p));
122 }
123 }
124
125 int convecpt(PyObject *o, void *p)
126 {
127 if (!ECPT_PYCHECK(o))
128 TYERR("want elliptic curve point");
129 getecptout(p, o);
130 return (1);
131 end:
132 return (0);
133 }
134
135 /*----- Curve points ------------------------------------------------------*/
136
137 static int ecbinop(PyObject *x, PyObject *y,
138 ec_curve **c, PyObject **cobj, ec *xx, ec *yy)
139 {
140 if (ECPTCURVE_PYCHECK(x)) *cobj = ECPT_COBJ(x);
141 else if (ECPTCURVE_PYCHECK(y)) *cobj = ECPT_COBJ(y);
142 else return (-1);
143 *c = ECCURVE_C(*cobj);
144 if (toecpt(*c, xx, x) || toecpt(*c, yy, y)) return (-1);
145 return (0);
146 }
147
148 #define BINOP(name) \
149 static PyObject *ecpt_py##name(PyObject *x, PyObject *y) { \
150 ec xx = EC_INIT, yy = EC_INIT, zz = EC_INIT; \
151 PyObject *cobj; \
152 ec_curve *c; \
153 if (ecbinop(x, y, &c, &cobj, &xx, &yy)) RETURN_NOTIMPL; \
154 c->ops->name(c, &zz, &xx, &yy); \
155 EC_DESTROY(&xx); EC_DESTROY(&yy); \
156 return (ecpt_pywrap(ECPT_COBJ(x), &zz)); \
157 }
158 BINOP(add)
159 BINOP(sub)
160 #undef BINOP
161
162 #define UNOP(name) \
163 static PyObject *ecpt_py##name(PyObject *x) { \
164 ec zz = EC_INIT; \
165 ec_curve *c = ECPT_C(x); \
166 c->ops->name(c, &zz, ECPT_P(x)); \
167 return (ecpt_pywrap(ECPT_COBJ(x), &zz)); \
168 }
169 UNOP(neg)
170 #undef UNOP
171
172 static PyObject *ecpt_pyid(PyObject *x) { RETURN_OBJ(x); }
173
174 static int ecpt_pynonzerop(PyObject *x) { return (!EC_ATINF(ECPT_P(x))); }
175
176 static void ecpt_pydealloc(PyObject *x)
177 {
178 EC_DESTROY(ECPT_P(x));
179 Py_DECREF(ECPT_COBJ(x));
180 FREEOBJ(x);
181 }
182
183 static PyObject *ecpt_pymul(PyObject *x, PyObject *y)
184 {
185 mp *xx;
186 ec zz = EC_INIT;
187
188 if (ECPT_PYCHECK(x)) { PyObject *t; t = x; x = y; y = t; }
189 if (!ECPT_PYCHECK(y)) 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;
194 ec_imul(ECPT_C(y), &zz, ECPT_P(y), xx);
195 MP_DROP(xx);
196 return (ecpt_pywrap(ECPT_COBJ(y), &zz));
197 }
198
199 static Py_hash_t ecpt_pyhash(PyObject *me)
200 {
201 uint32 h;
202 ec p = EC_INIT;
203
204 getecptout(&p, me);
205 if (EC_ATINF(&p)) h = 0x81d81a94;
206 else h = 0xe0fdd039 ^ (2*mphash(p.x)) ^ (3*mphash(p.y));
207 EC_DESTROY(&p);
208 return (h%LONG_MAX);
209 }
210
211 static PyObject *ecpt_pyrichcompare(PyObject *x, PyObject *y, int op)
212 {
213 ec p = EC_INIT, q = EC_INIT;
214 int b;
215 PyObject *rc = 0;
216
217 if (!ECPT_PYCHECK(y)) RETURN_NOTIMPL;
218 getecptout(&p, x);
219 getecptout(&q, y);
220 switch (op) {
221 case Py_EQ: b = EC_EQ(&p, &q); break;
222 case Py_NE: b = !EC_EQ(&p, &q); break;
223 default: TYERR("elliptic curve points are unordered");
224 }
225 rc = getbool(b);
226 end:
227 EC_DESTROY(&p);
228 EC_DESTROY(&q);
229 return (rc);
230 }
231
232 static PyObject *epmeth_oncurvep(PyObject *me)
233 {
234 return (getbool(EC_ATINF(ECPT_P(me)) ||
235 !EC_CHECK(ECPT_C(me), ECPT_P(me))));
236 }
237
238 static PyObject *epmeth_dbl(PyObject *me)
239 {
240 ec p = EC_INIT;
241 EC_DBL(ECPT_C(me), &p, ECPT_P(me));
242 return (ecpt_pywrap(ECPT_COBJ(me), &p));
243 }
244
245 static PyObject *epmeth_tobuf(PyObject *me)
246 {
247 buf b;
248 ec p = EC_INIT;
249 PyObject *rc;
250 size_t n;
251
252 getecptout(&p, me);
253 if (EC_ATINF(&p))
254 n = 2;
255 else
256 n = mp_octets(p.x) + mp_octets(p.y) + 6;
257 rc = bytestring_pywrap(0, n);
258 buf_init(&b, BIN_PTR(rc), n);
259 buf_putec(&b, &p);
260 assert(BOK(&b));
261 BIN_SETLEN(rc, BLEN(&b));
262 EC_DESTROY(&p);
263 return (rc);
264 }
265
266 static PyObject *epmeth_toraw(PyObject *me)
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
275 len = c->f->noctets * 2 + 1;
276 rc = bytestring_pywrap(0, len);
277 p = BIN_PTR(rc);
278 buf_init(&b, p, len);
279 EC_OUT(c, &pp, ECPT_P(me));
280 ec_putraw(c, &b, &pp);
281 EC_DESTROY(&pp);
282 BIN_SETLEN(rc, BLEN(&b));
283 return (rc);
284 }
285
286 static PyObject *epmeth_ec2osp(PyObject *me, PyObject *arg, PyObject *kw)
287 {
288 buf b;
289 PyObject *rc;
290 char *p;
291 ec_curve *c = ECPT_C(me);
292 ec pp = EC_INIT;
293 unsigned f = EC_EXPLY;
294 int len;
295 static const char *const kwlist[] = { "flags", 0 };
296
297 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:ec2osp", KWLIST,
298 convuint, &f))
299 return (0);
300 len = c->f->noctets * 2 + 1;
301 rc = bytestring_pywrap(0, len);
302 p = BIN_PTR(rc);
303 buf_init(&b, p, len);
304 EC_OUT(c, &pp, ECPT_P(me));
305 if (ec_ec2osp(c, f, &b, &pp)) {
306 Py_DECREF(rc); rc = 0;
307 VALERR("invalid flags");
308 }
309 EC_DESTROY(&pp);
310 BIN_SETLEN(rc, BLEN(&b));
311 end:
312 return (rc);
313 }
314
315 static PyObject *epmeth_frombuf(PyObject *me, PyObject *arg)
316 {
317 struct bin in;
318 buf b;
319 PyObject *rc = 0;
320 ec pp = EC_INIT;
321
322 if (!PyArg_ParseTuple(arg, "O&:frombuf", convbin, &in)) goto end;
323 buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
324 if (buf_getec(&b, &pp)) VALERR("malformed data");
325 rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp),
326 bytestring_pywrapbuf(&b));
327 end:
328 return (rc);
329 }
330
331 static PyObject *epmeth_parse(PyObject *me, PyObject *arg)
332 {
333 char *p;
334 qd_parse qd;
335 PyObject *rc = 0;
336 ec pp = EC_INIT;
337
338 if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end;
339 qd.p = p; qd.e = 0;
340 if (!ec_ptparse(&qd, &pp)) VALERR(qd.e);
341 rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p);
342 end:
343 return (rc);
344 }
345
346 static PyObject *epmeth_fromraw(PyObject *me, PyObject *arg)
347 {
348 struct bin in;
349 buf b;
350 PyObject *rc = 0;
351 ec_curve *cc;
352 ec pp = EC_INIT;
353
354 if (!PyArg_ParseTuple(arg, "O&:fromraw", convbin, &in)) return (0);
355 buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
356 cc = ECCURVE_C(me);
357 if (ec_getraw(cc, &b, &pp))
358 VALERR("bad point");
359 EC_IN(cc, &pp, &pp);
360 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
361 end:
362 return (rc);
363 }
364
365 static PyObject *epmeth_os2ecp(PyObject *me, PyObject *arg, PyObject *kw)
366 {
367 struct bin in;
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;
373 static const char *const kwlist[] = { "buf", "flags", 0 };
374
375 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:os2ecp", KWLIST,
376 convbin, &in, convuint, &f))
377 return (0);
378 buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
379 cc = ECCURVE_C(me);
380 if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point");
381 EC_IN(cc, &pp, &pp);
382 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
383 end:
384 return (rc);
385 }
386
387 static PyObject *epncget_ix(PyObject *me, void *hunoz)
388 {
389 ec p = EC_INIT;
390 PyObject *rc;
391 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
392 getecptout(&p, me);
393 rc = mp_pywrap(MP_COPY(p.x));
394 EC_DESTROY(&p);
395 return (rc);
396 }
397
398 static PyObject *epncget_iy(PyObject *me, void *hunoz)
399 {
400 ec p = EC_INIT;
401 PyObject *rc;
402 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
403 getecptout(&p, me);
404 rc = mp_pywrap(MP_COPY(p.y));
405 EC_DESTROY(&p);
406 return (rc);
407 }
408
409 static PyObject *epncget_point(PyObject *me, void *hunoz)
410 { RETURN_ME; }
411
412 static PyObject *epget_point(PyObject *me, void *hunoz)
413 {
414 ec p = EC_INIT;
415 getecptout(&p, me);
416 return (ecpt_pywrapout(ecpt_pytype, &p));
417 }
418
419 static PyObject *epget_x(PyObject *me, void *hunoz)
420 {
421 ec_curve *c = ECPT_C(me);
422 ec *pp = ECPT_P(me);
423 PyObject *fobj = ECPT_FOBJ(me);
424 ec p = EC_INIT;
425 PyObject *rc;
426
427 if (EC_ATINF(pp)) RETURN_NONE;
428 EC_OUT(c, &p, pp);
429 rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.x));
430 EC_DESTROY(&p);
431 return (rc);
432 }
433
434 static PyObject *epget_y(PyObject *me, void *hunoz)
435 {
436 ec_curve *c = ECPT_C(me);
437 ec *pp = ECPT_P(me);
438 PyObject *fobj = ECPT_FOBJ(me);
439 ec p = EC_INIT;
440 PyObject *rc;
441
442 if (EC_ATINF(pp)) RETURN_NONE;
443 EC_OUT(c, &p, pp);
444 rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.y));
445 EC_DESTROY(&p);
446 return (rc);
447 }
448
449 static PyObject *epget__x(PyObject *me, void *hunoz)
450 {
451 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
452 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->x)));
453 }
454
455 static PyObject *epget__y(PyObject *me, void *hunoz)
456 {
457 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
458 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->y)));
459 }
460
461 static PyObject *epget__z(PyObject *me, void *hunoz)
462 {
463 if (EC_ATINF(ECPT_P(me)) || !ECPT_P(me)->z) RETURN_NONE;
464 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->z)));
465 }
466
467 static mp *coord_in(field *f, PyObject *x)
468 {
469 mp *xx;
470 if (FE_PYCHECK(x) && (FE_F(x) == f || field_samep(FE_F(x), f)))
471 return (MP_COPY(FE_X(x)));
472 else if ((xx = getmp(x)) == 0)
473 return (0);
474 else
475 return (F_IN(f, xx, xx));
476 }
477
478 static int ecptxl_3(ec_curve *c, ec *p,
479 PyObject *x, PyObject *y, PyObject *z)
480 {
481 int rc = -1;
482
483 if (!x || !y || !z) TYERR("missing argument");
484 if (!c) VALERR("internal form with no curve!");
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))
488 goto end;
489 if (!p->z) p->z = MP_COPY(c->f->one); /* just in case */
490 rc = 0;
491 end:
492 return (rc);
493 }
494
495 static int ecptxl_2(ec_curve *c, ec *p, PyObject *x, PyObject *y)
496 {
497 int rc = -1;
498
499 if (!x || !y) TYERR("missing argument");
500 if ((p->x = getmp(x)) == 0 ||
501 (p->y = getmp(y)) == 0)
502 goto end;
503 if (c) EC_IN(c, p, p);
504 rc = 0;
505 end:
506 return (rc);
507 }
508
509 static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
510 {
511 int rc = -1;
512 PyObject *y = 0, *z = 0, *t = 0;
513 mp *xx = 0;
514 Py_ssize_t n;
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;
523 } else if (TEXT_CHECK(x)) {
524 qd.p = TEXT_PTR(x); qd.e = 0;
525 if (!ec_ptparse(&qd, p)) VALERR(qd.e);
526 qd_skipspc(&qd); if (!qd_eofp(&qd)) VALERR("junk at eof");
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;
533 n = PySequence_Size(t); if (n < 0) goto end;
534 if (n != 2 && n != 3)
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);
541 goto end;
542 } else
543 TYERR("can't convert to curve point");
544 goto ok;
545
546 fix:
547 if (c) EC_IN(c, p, p);
548 ok:
549 rc = 0;
550 end:
551 Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(z); Py_XDECREF(t);
552 mp_drop(xx);
553 return (rc);
554 }
555
556 static 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
566 static PyObject *ecptnc_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
567 {
568 PyObject *x = 0, *y = 0, *z = 0;
569 ec p = EC_INIT;
570 static const char *const kwlist[] = { "x", "y", 0 };
571
572 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:new", KWLIST, &x, &y) ||
573 ecptxl(0, &p, x, y, z))
574 goto end;
575 return (ecpt_pywrapout(ty, &p));
576 end:
577 mp_drop(p.x); mp_drop(p.y); mp_drop(p.z);
578 return (0);
579 }
580
581 static 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);
588 if (!mp_tolong_checked(p.x, &l, 0)) rc = PyInt_FromLong(l);
589 else rc = mp_topylong(p.x);
590 end:
591 EC_DESTROY(&p);
592 return (rc);
593 }
594
595 #ifdef PY2
596 static 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);
602 rc = mp_topylong(p.x);
603 end:
604 EC_DESTROY(&p);
605 return (rc);
606 }
607 #endif
608
609 static PyObject *ecpt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
610 {
611 PyObject *x = 0, *y = 0, *z = 0;
612 ec p = EC_INIT;
613 static const char *const kwlist[] = { "x", "y", "z", 0 };
614
615 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OOO:new", KWLIST,
616 &x, &y, &z) ||
617 ecptxl(ECCURVE_C(ty), &p, x, y, z))
618 goto end;
619 return (ecpt_pywrap((PyObject *)ty, &p));
620 end:
621 mp_drop(p.x); mp_drop(p.y); mp_drop(p.z);
622 return (0);
623 }
624
625 static const PyGetSetDef ecptnc_pygetset[] = {
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
634 static const PyMethodDef ecptnc_pymethods[] = {
635 #define METHNAME(func) epmeth_##func
636 NAMETH(tobuf, "X.tobuf() -> BIN")
637 CMTH (frombuf, "frombuf(STR) -> (P, REST)")
638 CMTH (parse, "parse(STR) -> (P, REST)")
639 #undef METHNAME
640 { 0 }
641 };
642
643 static const PyNumberMethods ecpt_pynumber = {
644 0, /* @nb_add@ */
645 0, /* @nb_subtract@ */
646 0, /* @nb_multiply@ */
647 #ifdef PY2
648 0, /* @nb_divide@ */
649 #endif
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@ */
663 #ifdef PY2
664 0, /* @nb_coerce@ */
665 #endif
666 ecpt_pyint, /* @nb_int@ */
667 PY23(ecpt_pylong, 0), /* @nb_long@ */
668 0, /* @nb_float@ */
669 #ifdef PY2
670 0, /* @nb_oct@ */
671 0, /* @nb_hex@ */
672 #endif
673
674 0, /* @nb_inplace_add@ */
675 0, /* @nb_inplace_subtract@ */
676 0, /* @nb_inplace_multiply@ */
677 #ifdef PY2
678 0, /* @nb_inplace_divide@ */
679 #endif
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
694 static const PyTypeObject ecpt_pytype_skel = {
695 PyVarObject_HEAD_INIT(0, 0) /* Header */
696 "ECPt", /* @tp_name@ */
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@ */
706 PYNUMBER(ecpt), /* @tp_as_number@ */
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@ */
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.",
723
724 0, /* @tp_traverse@ */
725 0, /* @tp_clear@ */
726 ecpt_pyrichcompare, /* @tp_richcompare@ */
727 0, /* @tp_weaklistoffset@ */
728 0, /* @tp_iter@ */
729 0, /* @tp_iternext@ */
730 PYMETHODS(ecptnc), /* @tp_methods@ */
731 0, /* @tp_members@ */
732 PYGETSET(ecptnc), /* @tp_getset@ */
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@ */
741 0, /* @tp_free@ */
742 0 /* @tp_is_gc@ */
743 };
744
745 static const PyMemberDef ecpt_pymembers[] = {
746 #define MEMBERSTRUCT ecpt_pyobj
747 MEMRNM(curve, T_OBJECT, PY23(ob_type, ob_base.ob_type), READONLY,
748 "P.curve -> elliptic curve containing P")
749 #undef MEMBERSTRUCT
750 { 0 }
751 };
752
753 static const PyGetSetDef ecpt_pygetset[] = {
754 #define GETSETNAME(op, name) ep##op##_##name
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
765 static const PyMethodDef ecpt_pymethods[] = {
766 #define METHNAME(func) epmeth_##func
767 NAMETH(toraw, "X.toraw() -> BIN")
768 KWMETH(ec2osp, "X.ec2osp([flags = EC_EXPLY]) -> BIN")
769 NAMETH(dbl, "X.dbl() -> X + X")
770 NAMETH(oncurvep, "X.oncurvep() -> BOOL")
771 CMTH (fromraw, "fromraw(STR) -> (P, REST)")
772 KWCMTH(os2ecp, "os2ecp(STR, [flags = ...]) -> (P, REST)")
773 #undef METHNAME
774 { 0 }
775 };
776
777 static const PyNumberMethods ecptcurve_pynumber = {
778 ecpt_pyadd, /* @nb_add@ */
779 ecpt_pysub, /* @nb_subtract@ */
780 ecpt_pymul, /* @nb_multiply@ */
781 #ifdef PY2
782 0, /* @nb_divide@ */
783 #endif
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@ */
797 #ifdef PY2
798 0, /* @nb_coerce@ */
799 #endif
800 0, /* @nb_int@ */
801 0, /* @nb_long@ */
802 0, /* @nb_float@ */
803 #ifdef PY2
804 0, /* @nb_oct@ */
805 0, /* @nb_hex@ */
806 #endif
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
826 static const PyTypeObject ecptcurve_pytype_skel = {
827 PyVarObject_HEAD_INIT(0, 0) /* Header */
828 "ECPtCurve", /* @tp_name@ */
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@ */
838 PYNUMBER(ecptcurve), /* @tp_as_number@ */
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@ */
852 "Elliptic curve points; abstract base class for points on given curves.",
853
854 0, /* @tp_traverse@ */
855 0, /* @tp_clear@ */
856 0, /* @tp_richcompare@ */
857 0, /* @tp_weaklistoffset@ */
858 0, /* @tp_iter@ */
859 0, /* @tp_iternext@ */
860 PYMETHODS(ecpt), /* @tp_methods@ */
861 PYMEMBERS(ecpt), /* @tp_members@ */
862 PYGETSET(ecpt), /* @tp_getset@ */
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@ */
871 0, /* @tp_free@ */
872 0 /* @tp_is_gc@ */
873 };
874
875 /*----- Elliptic curves themselves ----------------------------------------*/
876
877 static PyObject *eccurve_pyrichcompare(PyObject *x, PyObject *y, int op)
878 {
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));
884 switch (op) {
885 case Py_EQ: break;
886 case Py_NE: b = !b; break;
887 default: TYERR("can't order elliptic curves");
888 }
889 return (getbool(b));
890 end:
891 return (0);
892 }
893
894 static PyObject *ecmmul_id(PyObject *me)
895 { ec p = EC_INIT; return (ecpt_pywrap(me, &p)); }
896
897 static int ecmmul_fill(void *pp, PyObject *me, PyObject *x, PyObject *m)
898 {
899 ec_mulfactor *f = pp;
900
901 EC_CREATE(&f->base);
902 if (getecpt(ECCURVE_C(me), &f->base, x) ||
903 (f->exp = getmp(m)) == 0)
904 return (-1);
905 return (0);
906 }
907
908 static 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
915 static void ecmmul_drop(void *pp)
916 {
917 ec_mulfactor *f = pp;
918 EC_DESTROY(&f->base);
919 MP_DROP(f->exp);
920 }
921
922 static 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
928 static 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
935 static 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);
952 end:
953 if (xx) MP_DROP(xx);
954 return (rc);
955 }
956
957 static PyObject *ecmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
958 {
959 static const char *const kwlist[] = { "rng", 0 };
960 grand *r = &rand_global;
961 ec p = EC_INIT;
962
963 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", KWLIST,
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
971 static PyObject *ecmeth_parse(PyObject *me, PyObject *arg)
972 {
973 char *p;
974 qd_parse qd;
975 ec_curve *c;
976 PyObject *rc = 0;
977
978 if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end;
979 qd.p = p; qd.e = 0;
980 if ((c = ec_curveparse(&qd)) == 0) VALERR(qd.e);
981 rc = eccurve_pywrap(0, c);
982 end:
983 return (rc);
984 }
985
986 static PyObject *eccurve_dopywrap(PyTypeObject *ty,
987 PyObject *fobj, ec_curve *c)
988 {
989 eccurve_pyobj *cobj = newtype(ty, 0, c->ops->name);
990 cobj->c = c;
991 cobj->fobj = fobj;
992 cobj->ty.ht_type.tp_basicsize = sizeof(ecpt_pyobj);
993 cobj->ty.ht_type.tp_base = ecptcurve_pytype;
994 Py_INCREF(ecptcurve_pytype);
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;
1002 typeready(&cobj->ty.ht_type);
1003 return ((PyObject *)cobj);
1004 }
1005
1006 PyObject *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);
1015 if (STRCMP(EC_NAME(c), ==, "prime"))
1016 ty = ecprimecurve_pytype;
1017 else if (STRCMP(EC_NAME(c), ==, "primeproj"))
1018 ty = ecprimeprojcurve_pytype;
1019 else if (STRCMP(EC_NAME(c), ==, "bin"))
1020 ty = ecbincurve_pytype;
1021 else if (STRCMP(EC_NAME(c), ==, "binproj"))
1022 ty = ecbinprojcurve_pytype;
1023 else
1024 abort();
1025 return (eccurve_dopywrap(ty, fobj, c));
1026 }
1027
1028 static 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;
1034 static const char *const kwlist[] = { "field", "a", "b", 0 };
1035 mp *aa = 0, *bb = 0;
1036
1037 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&O&", KWLIST,
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));
1043 end:
1044 if (aa) MP_DROP(aa);
1045 if (bb) MP_DROP(bb);
1046 return (cobj);
1047 }
1048
1049 static PyObject *ecget_name(PyObject *me, void *hunoz)
1050 { return (TEXT_FROMSTR(EC_NAME(ECCURVE_C(me)))); }
1051
1052 static PyObject *ecget_a(PyObject *me, void *hunoz)
1053 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->a))); }
1054
1055 static PyObject *ecget_b(PyObject *me, void *hunoz)
1056 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->b))); }
1057
1058 static PyObject *ecget_field(PyObject *me, void *hunoz)
1059 { RETURN_OBJ(ECCURVE_FOBJ(me)); }
1060
1061 static PyObject *ecget_inf(PyObject *me, void *hunoz)
1062 { ec inf = EC_INIT; return (ecpt_pywrap(me, &inf)); }
1063
1064 static const PyGetSetDef eccurve_pygetset[] = {
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 }
1073 };
1074
1075 static const PyMethodDef eccurve_pymethods[] = {
1076 #define METHNAME(name) ecmeth_##name
1077 METH (mmul, "E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...")
1078 METH (find, "E.find(X) -> P")
1079 KWMETH(rand, "E.rand([rng = rand]) -> P")
1080 SMTH (parse, "parse(STR) -> (E, REST)")
1081 #undef METHNAME
1082 { 0 }
1083 };
1084
1085 static const PyTypeObject eccurve_pytype_skel = {
1086 PyVarObject_HEAD_INIT(0, 0) /* Header */
1087 "ECCurve", /* @tp_name@ */
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@ */
1110 "An elliptic curve. Abstract class.",
1111
1112 0, /* @tp_traverse@ */
1113 0, /* @tp_clear@ */
1114 eccurve_pyrichcompare, /* @tp_richcompare@ */
1115 0, /* @tp_weaklistoffset@ */
1116 0, /* @tp_iter@ */
1117 0, /* @tp_iternext@ */
1118 PYMETHODS(eccurve), /* @tp_methods@ */
1119 0, /* @tp_members@ */
1120 PYGETSET(eccurve), /* @tp_getset@ */
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@ */
1129 0, /* @tp_free@ */
1130 0 /* @tp_is_gc@ */
1131 };
1132
1133 static PyObject *ecprimecurve_pynew(PyTypeObject *ty,
1134 PyObject *arg, PyObject *kw)
1135 {
1136 return (eccurve_pynew(ty, ec_prime, arg, kw));
1137 }
1138
1139 static const PyTypeObject ecprimecurve_pytype_skel = {
1140 PyVarObject_HEAD_INIT(0, 0) /* Header */
1141 "ECPrimeCurve", /* @tp_name@ */
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@ */
1164 "ECPrimeCurve(FIELD, A, B): an elliptic curve over a prime field.\n"
1165 " Use ECPrimeProjCurve instead.",
1166
1167 0, /* @tp_traverse@ */
1168 0, /* @tp_clear@ */
1169 0, /* @tp_richcompare@ */
1170 0, /* @tp_weaklistoffset@ */
1171 0, /* @tp_iter@ */
1172 0, /* @tp_iternext@ */
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@ */
1184 0, /* @tp_free@ */
1185 0 /* @tp_is_gc@ */
1186 };
1187
1188 static PyObject *ecprimeprojcurve_pynew(PyTypeObject *ty,
1189 PyObject *arg, PyObject *kw)
1190 {
1191 return (eccurve_pynew(ty, ec_primeproj, arg, kw));
1192 }
1193
1194 static const PyTypeObject ecprimeprojcurve_pytype_skel = {
1195 PyVarObject_HEAD_INIT(0, 0) /* Header */
1196 "ECPrimeProjCurve", /* @tp_name@ */
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@ */
1219 "ECPrimeProjCurve(FIELD, A, B): an elliptic curve over a prime field\n"
1220 " using projective coordinates.",
1221
1222 0, /* @tp_traverse@ */
1223 0, /* @tp_clear@ */
1224 0, /* @tp_richcompare@ */
1225 0, /* @tp_weaklistoffset@ */
1226 0, /* @tp_iter@ */
1227 0, /* @tp_iternext@ */
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@ */
1239 0, /* @tp_free@ */
1240 0 /* @tp_is_gc@ */
1241 };
1242
1243 static PyObject *ecbincurve_pynew(PyTypeObject *ty,
1244 PyObject *arg, PyObject *kw)
1245 {
1246 return (eccurve_pynew(ty, ec_bin, arg, kw));
1247 }
1248
1249 static const PyTypeObject ecbincurve_pytype_skel = {
1250 PyVarObject_HEAD_INIT(0, 0) /* Header */
1251 "ECBinCurve", /* @tp_name@ */
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@ */
1274 "ECBinCurve(FIELD, A, B): an elliptic curve over a binary field.\n"
1275 " Use ECBinProjCurve instead.",
1276
1277 0, /* @tp_traverse@ */
1278 0, /* @tp_clear@ */
1279 0, /* @tp_richcompare@ */
1280 0, /* @tp_weaklistoffset@ */
1281 0, /* @tp_iter@ */
1282 0, /* @tp_iternext@ */
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@ */
1294 0, /* @tp_free@ */
1295 0 /* @tp_is_gc@ */
1296 };
1297
1298 static PyObject *ecbinprojcurve_pynew(PyTypeObject *ty,
1299 PyObject *arg, PyObject *kw)
1300 {
1301 return (eccurve_pynew(ty, ec_binproj, arg, kw));
1302 }
1303
1304 static const PyTypeObject ecbinprojcurve_pytype_skel = {
1305 PyVarObject_HEAD_INIT(0, 0) /* Header */
1306 "ECBinProjCurve", /* @tp_name@ */
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@ */
1329 "ECBinProjCurve(FIELD, A, B): an elliptic curve over a binary field,\n"
1330 " using projective coordinates.",
1331
1332 0, /* @tp_traverse@ */
1333 0, /* @tp_clear@ */
1334 0, /* @tp_richcompare@ */
1335 0, /* @tp_weaklistoffset@ */
1336 0, /* @tp_iter@ */
1337 0, /* @tp_iternext@ */
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@ */
1349 0, /* @tp_free@ */
1350 0 /* @tp_is_gc@ */
1351 };
1352
1353 /*----- Curve info --------------------------------------------------------*/
1354
1355 void 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
1364 PyObject *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
1375 static 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));
1382 FREEOBJ(me);
1383 }
1384
1385 static PyObject *ecinfo_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1386 {
1387 ec_info ei = { 0 };
1388 PyObject *e, *g;
1389 static const char *const kwlist[] = { "curve", "G", "r", "h", 0 };
1390 ecinfo_pyobj *rc = 0;
1391
1392 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!O&O&:new", KWLIST,
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);
1400 EC_OUT(ei.c, &ei.g, ECPT_P(g));
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
1407 end:
1408 mp_drop(ei.r);
1409 mp_drop(ei.h);
1410 return (0);
1411 }
1412
1413 static PyObject *eimeth_parse(PyObject *me, PyObject *arg)
1414 {
1415 char *p;
1416 qd_parse qd;
1417 ec_info ei;
1418 PyObject *rc = 0;
1419
1420 if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end;
1421 qd.p = p; qd.e = 0;
1422 if (ec_infoparse(&qd, &ei)) VALERR(qd.e);
1423 rc = Py_BuildValue("(Ns)", ecinfo_pywrap(&ei), qd.p);
1424 end:
1425 return (rc);
1426 }
1427
1428 static 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));
1437 end:
1438 return (0);
1439 }
1440
1441 static PyObject *eimeth_check(PyObject *me, PyObject *arg, PyObject *kw)
1442 {
1443 static const char *const kwlist[] = { "rng", 0 };
1444 grand *r = &rand_global;
1445 const char *p;
1446
1447 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:check", KWLIST,
1448 convgrand, &r))
1449 goto end;
1450 if ((p = ec_checkinfo(ECINFO_EI(me), r)) != 0)
1451 VALERR(p);
1452 RETURN_ME;
1453 end:
1454 return (0);
1455 }
1456
1457 static PyObject *eiget_curve(PyObject *me, void *hunoz)
1458 { RETURN_OBJ(ECINFO_COBJ(me)); }
1459
1460 static 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
1468 static PyObject *eiget_r(PyObject *me, void *hunoz)
1469 { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->r))); }
1470
1471 static PyObject *eiget_h(PyObject *me, void *hunoz)
1472 { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->h))); }
1473
1474 static const PyGetSetDef ecinfo_pygetset[] = {
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
1484 static const PyMethodDef ecinfo_pymethods[] = {
1485 #define METHNAME(name) eimeth_##name
1486 KWMETH(check, "I.check([rng = rand]) -> None")
1487 SMTH (parse, "parse(STR) -> (I, REST)")
1488 #undef METHNAME
1489 { 0 }
1490 };
1491
1492 static const PyTypeObject ecinfo_pytype_skel = {
1493 PyVarObject_HEAD_INIT(0, 0) /* Header */
1494 "ECInfo", /* @tp_name@ */
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@ */
1517 "ECInfo(CURVE, G, R, H): elliptic curve domain parameters.",
1518
1519 0, /* @tp_traverse@ */
1520 0, /* @tp_clear@ */
1521 ecinfo_pyrichcompare, /* @tp_richcompare@ */
1522 0, /* @tp_weaklistoffset@ */
1523 0, /* @tp_iter@ */
1524 0, /* @tp_iternext@ */
1525 PYMETHODS(ecinfo), /* @tp_methods@ */
1526 0, /* @tp_members@ */
1527 PYGETSET(ecinfo), /* @tp_getset@ */
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@ */
1536 0, /* @tp_free@ */
1537 0 /* @tp_is_gc@ */
1538 };
1539
1540 /*----- Setup -------------------------------------------------------------*/
1541
1542 static 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
1548 void 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);
1558 }
1559
1560 static const char *ec_namefn(const void *p)
1561 { const ecentry *ec = p; return (ec->name); }
1562
1563 static int ec_ixfn(const void *p)
1564 {
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
1573 static PyObject *ec_valfn(int i)
1574 {
1575 ec_info ei;
1576
1577 ec_infofromdata(&ei, ectab[i].data);
1578 return (ecinfo_pywrap(&ei));
1579 }
1580
1581 void 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);
1591 INSERT("eccurves", make_grouptab(ectab, sizeof(*ectab),
1592 ec_namefn, ec_ixfn, ec_valfn));
1593 setconstants(mod, consts);
1594 }
1595
1596 /*----- That's all, folks -------------------------------------------------*/