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