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