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