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