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