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