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