5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 /*----- Utility functions -------------------------------------------------*/
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;
42 ec_curve *eccurve_copy(ec_curve *c)
47 if ((f = field_copy(c->f)) == 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)
57 else if (strcmp(EC_NAME(c), "binproj") == 0)
58 c = ec_binproj(f, a, b);
67 static PyObject *ecpt_dopywrap(PyObject *cobj, ec_curve *c, ec *p)
69 ecpt_pyobj *z = PyObject_New(ecpt_pyobj, (PyTypeObject *)cobj);
73 return ((PyObject *)z);
76 PyObject *ecpt_pywrap(PyObject *cobj, ec *p)
77 { return (ecpt_dopywrap(cobj, ECCURVE_C(cobj), p)); }
79 PyObject *ecpt_pywrapout(void *cobj, ec *p)
83 if (!PyType_IsSubtype(cobj, ecptcurve_pytype))
87 EC_IN(ECCURVE_C(cobj), p, p);
89 return (ecpt_dopywrap(cobj, c, p));
92 int toecpt(ec_curve *c, ec *d, PyObject *p)
94 if (ECPTCURVE_PYCHECK(p)) {
95 if (ECPT_C(p) != c && !ec_samep(ECPT_C(p), c))
97 EC_COPY(d, ECPT_P(p));
98 } else if (ECPT_PYCHECK(p))
99 EC_IN(c, d, ECPT_P(p));
105 int getecpt(ec_curve *c, ec *d, PyObject *p)
107 if (toecpt(c, d, p)) {
108 PyErr_Format(PyExc_TypeError, "can't convert %.100s to ecpt",
109 p->ob_type->tp_name);
115 void getecptout(ec *d, PyObject *p)
117 if (ECPTCURVE_PYCHECK(p))
118 EC_OUT(ECPT_C(p), d, ECPT_P(p));
120 assert(ECPT_PYCHECK(p));
121 EC_COPY(d, ECPT_P(p));
125 int convecpt(PyObject *o, void *p)
127 if (!ECPT_PYCHECK(o))
128 TYERR("want elliptic curve point");
135 /*----- Curve points ------------------------------------------------------*/
137 static int ecbinop(PyObject *x, PyObject *y,
138 ec_curve **c, PyObject **cobj, ec *xx, ec *yy)
140 if (ECPTCURVE_PYCHECK(x)) *cobj = ECPT_COBJ(x);
141 else if (ECPTCURVE_PYCHECK(y)) *cobj = ECPT_COBJ(y);
143 *c = ECCURVE_C(*cobj);
144 if (toecpt(*c, xx, x) || toecpt(*c, yy, y)) return (-1);
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; \
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)); \
163 static PyObject *ecpt_py##name(PyObject *x) { \
165 ec_curve *c = ECPT_C(x); \
166 c->ops->name(c, &zz, ECPT_P(x)); \
167 return (ecpt_pywrap(ECPT_COBJ(x), &zz)); \
172 static PyObject *ecpt_pyid(PyObject *x) { RETURN_OBJ(x); }
174 static int ecpt_pynonzerop(PyObject *x) { return (!EC_ATINF(ECPT_P(x))); }
176 static void ecpt_pydealloc(PyObject *x)
178 EC_DESTROY(ECPT_P(x));
179 Py_DECREF(ECPT_COBJ(x));
183 static PyObject *ecpt_pymul(PyObject *x, PyObject *y)
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);
192 return (ecpt_pywrap(ECPT_COBJ(y), &zz));
195 static long ecpt_pyhash(PyObject *me)
200 size_t sz = 2*ECPT_C(me)->f->noctets + 1;
201 octet *q = xmalloc(sz);
203 h = 0xe0fdd039 + ECPT_C(me)->f->ops->ty;
205 EC_OUT(ECPT_C(me), &p, ECPT_P(me));
206 ec_putraw(ECPT_C(me), &b, &p);
208 h = unihash_hash(&unihash_global, h, BBASE(&b), BLEN(&b));
210 return (h % LONG_MAX);
213 static PyObject *ecpt_pyrichcompare(PyObject *x, PyObject *y, int op)
217 ec p = EC_INIT, q = EC_INIT;
221 if (ecbinop(x, y, &c, &cobj, &p, &q)) RETURN_NOTIMPL;
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");
236 static PyObject *epmeth_oncurvep(PyObject *me, PyObject *arg)
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))));
243 static PyObject *epmeth_dbl(PyObject *me, PyObject *arg)
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));
251 static PyObject *epmeth_tobuf(PyObject *me, PyObject *arg)
258 if (!PyArg_ParseTuple(arg, ":tobuf")) return (0);
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);
268 _PyString_Resize(&rc, BLEN(&b));
273 static PyObject *epmeth_toraw(PyObject *me, PyObject *arg)
278 ec_curve *c = ECPT_C(me);
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);
290 _PyString_Resize(&rc, BLEN(&b));
294 static PyObject *epmeth_ec2osp(PyObject *me, PyObject *arg, PyObject *kw)
299 ec_curve *c = ECPT_C(me);
301 unsigned f = EC_EXPLY;
303 char *kwlist[] = { "flags", 0 };
305 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:ec2osp", kwlist,
308 len = c->f->noctets * 2 + 1;
309 rc = bytestring_pywrap(0, len);
310 p = PyString_AS_STRING(rc);
311 buf_init(&b, p, len);
312 EC_OUT(c, &pp, ECPT_P(me));
313 if (ec_ec2osp(c, f, &b, &pp)) {
314 Py_DECREF(rc); rc = 0;
315 VALERR("invalid flags");
318 _PyString_Resize(&rc, BLEN(&b));
323 static PyObject *epget_curve(PyObject *me, void *hunoz)
324 { RETURN_OBJ(ECPT_COBJ(me)); }
326 static PyObject *epncget_ix(PyObject *me, void *hunoz)
330 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
332 rc = mp_pywrap(MP_COPY(p.x));
337 static PyObject *epncget_iy(PyObject *me, void *hunoz)
341 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
343 rc = mp_pywrap(MP_COPY(p.y));
348 static PyObject *epncget_point(PyObject *me, void *hunoz)
351 static PyObject *epget_point(PyObject *me, void *hunoz)
355 return (ecpt_pywrapout(ecpt_pytype, &p));
358 static PyObject *epget_x(PyObject *me, void *hunoz)
360 ec_curve *c = ECPT_C(me);
362 PyObject *fobj = ECPT_FOBJ(me);
366 if (EC_ATINF(pp)) RETURN_NONE;
368 rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.x));
373 static PyObject *epget_y(PyObject *me, void *hunoz)
375 ec_curve *c = ECPT_C(me);
377 PyObject *fobj = ECPT_FOBJ(me);
381 if (EC_ATINF(pp)) RETURN_NONE;
383 rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.y));
388 static PyObject *epget__x(PyObject *me, void *hunoz)
390 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
391 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->x)));
394 static PyObject *epget__y(PyObject *me, void *hunoz)
396 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
397 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->y)));
400 static PyObject *epget__z(PyObject *me, void *hunoz)
402 if (EC_ATINF(ECPT_P(me)) || !ECPT_P(me)->z) RETURN_NONE;
403 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->z)));
406 static mp *coord_in(field *f, PyObject *x)
409 if (FE_PYCHECK(x) && FE_F(x) == f)
410 return (MP_COPY(FE_X(x)));
411 else if ((xx = getmp(x)) == 0)
414 return (F_IN(f, xx, xx));
417 static int ecptxl_3(ec_curve *c, ec *p,
418 PyObject *x, PyObject *y, PyObject *z)
422 if (!x || !y || !z) TYERR("missing argument");
423 if (!c) VALERR("internal form with no curve!");
424 if ((p->x = coord_in(c->f, x)) == 0 ||
425 (p->y = coord_in(c->f, y)) == 0 ||
426 (z != Py_None && (p->z = coord_in(c->f, z)) == 0))
428 if (!p->z) p->z = MP_COPY(c->f->one); /* just in case */
434 static int ecptxl_2(ec_curve *c, ec *p, PyObject *x, PyObject *y)
438 if (!x || !y) TYERR("missing argument");
439 if ((p->x = getmp(x)) == 0 ||
440 (p->y = getmp(y)) == 0)
442 if (c) EC_IN(c, p, p);
448 static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
451 PyObject *y = 0, *z = 0, *t = 0;
458 if (!x || x == Py_None)
460 else if (ECPT_PYCHECK(x)) {
463 } else if (PyString_Check(x)) {
464 if (PyObject_AsReadBuffer(x, &q, 0))
468 if (!ec_ptparse(&qd, p))
471 } else if (c && (xx = tomp(x)) != 0) {
472 xx = F_IN(c->f, xx, xx);
473 if (!EC_FIND(c, p, xx)) VALERR("not on the curve");
474 } else if (PySequence_Check(x)) {
476 n = PySequence_Size(t);
477 if (n != 2 && (n != 3 || !c))
478 TYERR("want sequence of two or three items");
479 if ((x = PySequence_GetItem(t, 0)) == 0 ||
480 (y = PySequence_GetItem(t, 1)) == 0 ||
481 (n == 3 && (z = PySequence_GetItem(t, 2)) == 0))
483 rc = (n == 2) ? ecptxl_2(c, p, x, y) : ecptxl_3(c, p, x, y, z);
485 TYERR("can't convert to curve point");
489 if (c) EC_IN(c, p, p);
493 Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(z); Py_XDECREF(t);
498 static int ecptxl(ec_curve *c, ec *p, PyObject *x, PyObject *y, PyObject *z)
501 return (ecptxl_3(c, p, x, y, z));
503 return (ecptxl_2(c, p, x, y));
505 return (ecptxl_1(c, p, x));
508 static PyObject *ecptnc_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
510 PyObject *x = 0, *y = 0, *z = 0;
512 char *kwlist[] = { "x", "y", 0 };
514 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:new", kwlist, &x, &y) ||
515 ecptxl(0, &p, x, y, z))
517 return (ecpt_pywrapout(ty, &p));
523 static PyObject *ecpt_pyint(PyObject *me)
528 if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
530 if (!mp_tolong_checked(p.x, &l, 0)) rc = PyInt_FromLong(l);
531 else rc = mp_topylong(p.x);
537 static PyObject *ecpt_pylong(PyObject *me)
541 if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
543 rc = mp_topylong(p.x);
549 static PyObject *ecpt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
551 PyObject *x = 0, *y = 0, *z = 0;
553 char *kwlist[] = { "x", "y", "z", 0 };
555 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OOO:new", kwlist,
557 ecptxl(ECCURVE_C(ty), &p, x, y, z))
559 return (ecpt_pywrap((PyObject *)ty, &p));
565 static PyGetSetDef ecptnc_pygetset[] = {
566 #define GETSETNAME(op, name) epnc##op##_##name
567 GET (ix, "P.ix -> integer x coordinate of P")
568 GET (iy, "P.iy -> integer y coordinate of P")
569 GET (point, "P.point -> standalone curve point (no-op)")
574 static PyMethodDef ecptnc_pymethods[] = {
575 #define METHNAME(func) epmeth_##func
576 METH (tobuf, "X.tobuf() -> BIN")
581 static PyNumberMethods ecpt_pynumber = {
583 0, /* @nb_subtract@ */
584 0, /* @nb_multiply@ */
586 0, /* @nb_remainder@ */
589 0, /* @nb_negative@ */
590 0, /* @nb_positive@ */
591 0, /* @nb_absolute@ */
592 ecpt_pynonzerop, /* @nb_nonzero@ */
600 ecpt_pyint, /* @nb_int@ */
601 ecpt_pylong, /* @nb_long@ */
606 0, /* @nb_inplace_add@ */
607 0, /* @nb_inplace_subtract@ */
608 0, /* @nb_inplace_multiply@ */
609 0, /* @nb_inplace_divide@ */
610 0, /* @nb_inplace_remainder@ */
611 0, /* @nb_inplace_power@ */
612 0, /* @nb_inplace_lshift@ */
613 0, /* @nb_inplace_rshift@ */
614 0, /* @nb_inplace_and@ */
615 0, /* @nb_inplace_xor@ */
616 0, /* @nb_inplace_or@ */
618 0, /* @nb_floor_divide@ */
619 0, /* @nb_true_divide@ */
620 0, /* @nb_inplace_floor_divide@ */
621 0, /* @nb_inplace_true_divide@ */
624 static PyTypeObject ecpt_pytype_skel = {
625 PyObject_HEAD_INIT(0) 0, /* Header */
626 "ECPt", /* @tp_name@ */
627 sizeof(ecpt_pyobj), /* @tp_basicsize@ */
628 0, /* @tp_itemsize@ */
630 ecpt_pydealloc, /* @tp_dealloc@ */
632 0, /* @tp_getattr@ */
633 0, /* @tp_setattr@ */
634 0, /* @tp_compare@ */
636 &ecpt_pynumber, /* @tp_as_number@ */
637 0, /* @tp_as_sequence@ */
638 0, /* @tp_as_mapping@ */
639 ecpt_pyhash, /* @tp_hash@ */
642 0, /* @tp_getattro@ */
643 0, /* @tp_setattro@ */
644 0, /* @tp_as_buffer@ */
645 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
646 Py_TPFLAGS_CHECKTYPES |
650 "Elliptic curve points, not associated with any curve.",
652 0, /* @tp_traverse@ */
654 ecpt_pyrichcompare, /* @tp_richcompare@ */
655 0, /* @tp_weaklistoffset@ */
657 0, /* @tp_iternext@ */
658 ecptnc_pymethods, /* @tp_methods@ */
659 0, /* @tp_members@ */
660 ecptnc_pygetset, /* @tp_getset@ */
663 0, /* @tp_descr_get@ */
664 0, /* @tp_descr_set@ */
665 0, /* @tp_dictoffset@ */
667 PyType_GenericAlloc, /* @tp_alloc@ */
668 ecptnc_pynew, /* @tp_new@ */
673 static PyGetSetDef ecpt_pygetset[] = {
674 #define GETSETNAME(op, name) ep##op##_##name
675 GET (curve, "P.curve -> elliptic curve containing P")
676 GET (point, "P.point -> standalone curve point")
677 GET (x, "P.x -> Cartesian x coordinate of P")
678 GET (y, "P.y -> Cartesian y coordinate of P")
679 GET (_x, "P._x -> internal x coordinate of P")
680 GET (_y, "P._y -> internal y coordinate of P")
681 GET (_z, "P._z -> internal z coordinate of P, or None")
686 static PyMethodDef ecpt_pymethods[] = {
687 #define METHNAME(func) epmeth_##func
688 METH (toraw, "X.toraw() -> BIN")
689 KWMETH(ec2osp, "X.ec2osp([flags = EC_EXPLY]) -> BIN")
690 METH (dbl, "X.dbl() -> X + X")
691 METH (oncurvep, "X.oncurvep() -> BOOL")
696 static PyNumberMethods ecptcurve_pynumber = {
697 ecpt_pyadd, /* @nb_add@ */
698 ecpt_pysub, /* @nb_subtract@ */
699 ecpt_pymul, /* @nb_multiply@ */
701 0, /* @nb_remainder@ */
704 ecpt_pyneg, /* @nb_negative@ */
705 ecpt_pyid, /* @nb_positive@ */
706 0, /* @nb_absolute@ */
707 0, /* @nb_nonzero@ */
721 0, /* @nb_inplace_add@ */
722 0, /* @nb_inplace_subtract@ */
723 0, /* @nb_inplace_multiply@ */
724 0, /* @nb_inplace_divide@ */
725 0, /* @nb_inplace_remainder@ */
726 0, /* @nb_inplace_power@ */
727 0, /* @nb_inplace_lshift@ */
728 0, /* @nb_inplace_rshift@ */
729 0, /* @nb_inplace_and@ */
730 0, /* @nb_inplace_xor@ */
731 0, /* @nb_inplace_or@ */
733 0, /* @nb_floor_divide@ */
734 0, /* @nb_true_divide@ */
735 0, /* @nb_inplace_floor_divide@ */
736 0, /* @nb_inplace_true_divide@ */
739 static PyTypeObject ecptcurve_pytype_skel = {
740 PyObject_HEAD_INIT(0) 0, /* Header */
741 "ECPtCurve", /* @tp_name@ */
742 sizeof(ecpt_pyobj), /* @tp_basicsize@ */
743 0, /* @tp_itemsize@ */
745 ecpt_pydealloc, /* @tp_dealloc@ */
747 0, /* @tp_getattr@ */
748 0, /* @tp_setattr@ */
749 0, /* @tp_compare@ */
751 &ecptcurve_pynumber, /* @tp_as_number@ */
752 0, /* @tp_as_sequence@ */
753 0, /* @tp_as_mapping@ */
757 0, /* @tp_getattro@ */
758 0, /* @tp_setattro@ */
759 0, /* @tp_as_buffer@ */
760 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
761 Py_TPFLAGS_CHECKTYPES |
765 "Elliptic curve points; abstract base class for points on given curves.",
767 0, /* @tp_traverse@ */
769 0, /* @tp_richcompare@ */
770 0, /* @tp_weaklistoffset@ */
772 0, /* @tp_iternext@ */
773 ecpt_pymethods, /* @tp_methods@ */
774 0, /* @tp_members@ */
775 ecpt_pygetset, /* @tp_getset@ */
778 0, /* @tp_descr_get@ */
779 0, /* @tp_descr_set@ */
780 0, /* @tp_dictoffset@ */
782 PyType_GenericAlloc, /* @tp_alloc@ */
783 abstract_pynew, /* @tp_new@ */
788 /*----- Elliptic curves themselves ----------------------------------------*/
790 static PyObject *eccurve_pyrichcompare(PyObject *x, PyObject *y, int op)
794 assert(ECCURVE_PYCHECK(x));
795 if (!ECCURVE_PYCHECK(y)) RETURN_NOTIMPL;
796 b = ec_samep(ECCURVE_C(x), ECCURVE_C(y));
799 case Py_NE: b = !b; break;
800 default: TYERR("can't order elliptic curves");
807 static PyObject *ecmmul_id(PyObject *me)
808 { ec p = EC_INIT; return (ecpt_pywrap(me, &p)); }
810 static int ecmmul_fill(void *pp, PyObject *me, PyObject *x, PyObject *m)
812 ec_mulfactor *f = pp;
815 if (getecpt(ECCURVE_C(me), &f->base, x) ||
816 (f->exp = getmp(m)) == 0)
821 static PyObject *ecmmul_exp(PyObject *me, void *pp, int n)
824 ec_immul(ECCURVE_C(me), &p, pp, n);
825 return (ecpt_pywrap(me, &p));
828 static void ecmmul_drop(void *pp)
830 ec_mulfactor *f = pp;
831 EC_DESTROY(&f->base);
835 static PyObject *ecmeth_mmul(PyObject *me, PyObject *arg)
837 return (mexp_common(me, arg, sizeof(ec_mulfactor),
838 ecmmul_id, ecmmul_fill, ecmmul_exp, ecmmul_drop));
841 static PyObject *meth__ECPtCurve_fromraw(PyObject *me, PyObject *arg)
850 if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &len))
852 buf_init(&b, p, len);
854 if (ec_getraw(cc, &b, &pp))
857 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
862 static PyObject *meth__ECPtCurve_os2ecp(PyObject *me,
863 PyObject *arg, PyObject *kw)
870 unsigned f = EC_XONLY | EC_LSB | EC_SORT | EC_EXPLY;
872 char *kwlist[] = { "class", "buf", "flags", 0 };
874 if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|O&:os2ecp", kwlist,
875 &me, &p, &len, convuint, &f))
877 buf_init(&b, p, len);
879 if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point");
881 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
886 static PyObject *meth__ECPt_frombuf(PyObject *me, PyObject *arg)
894 if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end;
896 if (buf_getec(&b, &pp)) VALERR("malformed data");
897 rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp),
898 bytestring_pywrapbuf(&b));
903 static PyObject *meth__ECPt_parse(PyObject *me, PyObject *arg)
910 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
913 if (!ec_ptparse(&qd, &pp)) VALERR(qd.e);
914 rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p);
919 static void eccurve_pydealloc(PyObject *me)
921 ec_destroycurve(ECCURVE_C(me));
922 Py_DECREF(ECCURVE_FOBJ(me));
923 PyType_Type.tp_dealloc(me);
926 static PyObject *ecmeth_find(PyObject *me, PyObject *arg)
933 if (!PyArg_ParseTuple(arg, "O:find", &x)) goto end;
934 if (FIELD_PYCHECK(x) && FE_F(x) == ECCURVE_C(me)->f)
935 xx = MP_COPY(FE_X(x));
936 else if ((xx = getmp(x)) == 0)
939 xx = F_IN(ECCURVE_C(me)->f, xx, xx);
940 if (EC_FIND(ECCURVE_C(me), &p, xx) == 0)
941 VALERR("not on the curve");
942 rc = ecpt_pywrap(me, &p);
948 static PyObject *ecmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
950 char *kwlist[] = { "rng", 0 };
951 grand *r = &rand_global;
954 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", kwlist,
957 ec_rand(ECCURVE_C(me), &p, r);
958 EC_IN(ECCURVE_C(me), &p, &p);
959 return (ecpt_pywrap(me, &p));
962 static PyObject *eccurve_dopywrap(PyTypeObject *ty,
963 PyObject *fobj, ec_curve *c)
965 eccurve_pyobj *cobj = newtype(ty, 0, c->ops->name);
968 cobj->ty.ht_type.tp_basicsize = sizeof(ecpt_pyobj);
969 cobj->ty.ht_type.tp_base = ecptcurve_pytype;
970 Py_INCREF(ecptcurve_pytype);
971 cobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
972 Py_TPFLAGS_BASETYPE |
973 Py_TPFLAGS_CHECKTYPES |
974 Py_TPFLAGS_HEAPTYPE);
975 cobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
976 cobj->ty.ht_type.tp_free = 0;
977 cobj->ty.ht_type.tp_new = ecpt_pynew;
978 typeready(&cobj->ty.ht_type);
979 return ((PyObject *)cobj);
982 PyObject *eccurve_pywrap(PyObject *fobj, ec_curve *c)
987 fobj = field_pywrap(c->f);
990 assert(FIELD_F(fobj) == c->f);
991 if (strcmp(EC_NAME(c), "prime") == 0)
992 ty = ecprimecurve_pytype;
993 else if (strcmp(EC_NAME(c), "primeproj") == 0)
994 ty = ecprimeprojcurve_pytype;
995 else if (strcmp(EC_NAME(c), "bin") == 0)
996 ty = ecbincurve_pytype;
997 else if (strcmp(EC_NAME(c), "binproj") == 0)
998 ty = ecbinprojcurve_pytype;
1001 return (eccurve_dopywrap(ty, fobj, c));
1004 static PyObject *eccurve_pynew(PyTypeObject *ty,
1005 ec_curve *(*make)(field *, mp *, mp *),
1006 PyObject *arg, PyObject *kw)
1010 char *kwlist[] = { "field", "a", "b", 0 };
1011 mp *aa = 0, *bb = 0;
1013 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&O&", kwlist,
1014 field_pytype, &fobj,
1015 convmp, &aa, convmp, &bb))
1018 cobj = eccurve_dopywrap(ty, fobj, make(FIELD_F(fobj), aa, bb));
1020 if (aa) MP_DROP(aa);
1021 if (bb) MP_DROP(bb);
1025 static PyObject *meth__ECCurve_parse(PyObject *me, PyObject *arg)
1032 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
1036 if ((c = ec_curveparse(&qd)) == 0)
1038 rc = eccurve_pywrap(0, c);
1043 static PyObject *ecget_name(PyObject *me, void *hunoz)
1044 { return (PyString_FromString(EC_NAME(ECCURVE_C(me)))); }
1046 static PyObject *ecget_a(PyObject *me, void *hunoz)
1047 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->a))); }
1049 static PyObject *ecget_b(PyObject *me, void *hunoz)
1050 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->b))); }
1052 static PyObject *ecget_field(PyObject *me, void *hunoz)
1053 { RETURN_OBJ(ECCURVE_FOBJ(me)); }
1055 static PyObject *ecget_inf(PyObject *me, void *hunoz)
1056 { ec inf = EC_INIT; return (ecpt_pywrap(me, &inf)); }
1058 static PyGetSetDef eccurve_pygetset[] = {
1059 #define GETSETNAME(op, name) ec##op##_##name
1060 GET (name, "E.name -> name of this kind of curve")
1061 GET (a, "E.a -> first parameter of curve")
1062 GET (b, "E.b -> second parameter of curve")
1063 GET (field, "E.field -> finite field containing this curve")
1064 GET (inf, "E.inf -> point at infinity of this curve")
1069 static PyMethodDef eccurve_pymethods[] = {
1070 #define METHNAME(name) ecmeth_##name
1072 E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...")
1073 METH (find, "E.find(X) -> P")
1074 KWMETH(rand, "E.rand(rng = rand) ->P")
1079 static PyTypeObject eccurve_pytype_skel = {
1080 PyObject_HEAD_INIT(0) 0, /* Header */
1081 "ECCurve", /* @tp_name@ */
1082 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1083 0, /* @tp_itemsize@ */
1085 eccurve_pydealloc, /* @tp_dealloc@ */
1087 0, /* @tp_getattr@ */
1088 0, /* @tp_setattr@ */
1089 0, /* @tp_compare@ */
1091 0, /* @tp_as_number@ */
1092 0, /* @tp_as_sequence@ */
1093 0, /* @tp_as_mapping@ */
1097 0, /* @tp_getattro@ */
1098 0, /* @tp_setattro@ */
1099 0, /* @tp_as_buffer@ */
1100 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1101 Py_TPFLAGS_BASETYPE,
1104 "An elliptic curve. Abstract class.",
1106 0, /* @tp_traverse@ */
1108 eccurve_pyrichcompare, /* @tp_richcompare@ */
1109 0, /* @tp_weaklistoffset@ */
1111 0, /* @tp_iternext@ */
1112 eccurve_pymethods, /* @tp_methods@ */
1113 0, /* @tp_members@ */
1114 eccurve_pygetset, /* @tp_getset@ */
1117 0, /* @tp_descr_get@ */
1118 0, /* @tp_descr_set@ */
1119 0, /* @tp_dictoffset@ */
1121 PyType_GenericAlloc, /* @tp_alloc@ */
1122 abstract_pynew, /* @tp_new@ */
1127 static PyObject *ecprimecurve_pynew(PyTypeObject *ty,
1128 PyObject *arg, PyObject *kw)
1130 return (eccurve_pynew(ty, ec_prime, arg, kw));
1133 static PyTypeObject ecprimecurve_pytype_skel = {
1134 PyObject_HEAD_INIT(0) 0, /* Header */
1135 "ECPrimeCurve", /* @tp_name@ */
1136 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1137 0, /* @tp_itemsize@ */
1139 eccurve_pydealloc, /* @tp_dealloc@ */
1141 0, /* @tp_getattr@ */
1142 0, /* @tp_setattr@ */
1143 0, /* @tp_compare@ */
1145 0, /* @tp_as_number@ */
1146 0, /* @tp_as_sequence@ */
1147 0, /* @tp_as_mapping@ */
1151 0, /* @tp_getattro@ */
1152 0, /* @tp_setattro@ */
1153 0, /* @tp_as_buffer@ */
1154 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1155 Py_TPFLAGS_BASETYPE,
1158 "An elliptic curve over a prime field. Use ecprimeprojcurve.",
1160 0, /* @tp_traverse@ */
1162 eccurve_pyrichcompare, /* @tp_richcompare@ */
1163 0, /* @tp_weaklistoffset@ */
1165 0, /* @tp_iternext@ */
1166 0, /* @tp_methods@ */
1167 0, /* @tp_members@ */
1168 0, /* @tp_getset@ */
1171 0, /* @tp_descr_get@ */
1172 0, /* @tp_descr_set@ */
1173 0, /* @tp_dictoffset@ */
1175 PyType_GenericAlloc, /* @tp_alloc@ */
1176 ecprimecurve_pynew, /* @tp_new@ */
1181 static PyObject *ecprimeprojcurve_pynew(PyTypeObject *ty,
1182 PyObject *arg, PyObject *kw)
1184 return (eccurve_pynew(ty, ec_primeproj, arg, kw));
1187 static PyTypeObject ecprimeprojcurve_pytype_skel = {
1188 PyObject_HEAD_INIT(0) 0, /* Header */
1189 "ECPrimeProjCurve", /* @tp_name@ */
1190 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1191 0, /* @tp_itemsize@ */
1193 eccurve_pydealloc, /* @tp_dealloc@ */
1195 0, /* @tp_getattr@ */
1196 0, /* @tp_setattr@ */
1197 0, /* @tp_compare@ */
1199 0, /* @tp_as_number@ */
1200 0, /* @tp_as_sequence@ */
1201 0, /* @tp_as_mapping@ */
1205 0, /* @tp_getattro@ */
1206 0, /* @tp_setattro@ */
1207 0, /* @tp_as_buffer@ */
1208 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1209 Py_TPFLAGS_BASETYPE,
1212 "An elliptic curve over a prime field, using projective coordinates.",
1214 0, /* @tp_traverse@ */
1216 eccurve_pyrichcompare, /* @tp_richcompare@ */
1217 0, /* @tp_weaklistoffset@ */
1219 0, /* @tp_iternext@ */
1220 0, /* @tp_methods@ */
1221 0, /* @tp_members@ */
1222 0, /* @tp_getset@ */
1225 0, /* @tp_descr_get@ */
1226 0, /* @tp_descr_set@ */
1227 0, /* @tp_dictoffset@ */
1229 PyType_GenericAlloc, /* @tp_alloc@ */
1230 ecprimeprojcurve_pynew, /* @tp_new@ */
1235 static PyObject *ecbincurve_pynew(PyTypeObject *ty,
1236 PyObject *arg, PyObject *kw)
1238 return (eccurve_pynew(ty, ec_bin, arg, kw));
1241 static PyTypeObject ecbincurve_pytype_skel = {
1242 PyObject_HEAD_INIT(0) 0, /* Header */
1243 "ECBinCurve", /* @tp_name@ */
1244 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1245 0, /* @tp_itemsize@ */
1247 eccurve_pydealloc, /* @tp_dealloc@ */
1249 0, /* @tp_getattr@ */
1250 0, /* @tp_setattr@ */
1251 0, /* @tp_compare@ */
1253 0, /* @tp_as_number@ */
1254 0, /* @tp_as_sequence@ */
1255 0, /* @tp_as_mapping@ */
1259 0, /* @tp_getattro@ */
1260 0, /* @tp_setattro@ */
1261 0, /* @tp_as_buffer@ */
1262 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1263 Py_TPFLAGS_BASETYPE,
1266 "An elliptic curve over a binary field. Use ecbinprojcurve.",
1268 0, /* @tp_traverse@ */
1270 eccurve_pyrichcompare, /* @tp_richcompare@ */
1271 0, /* @tp_weaklistoffset@ */
1273 0, /* @tp_iternext@ */
1274 0, /* @tp_methods@ */
1275 0, /* @tp_members@ */
1276 0, /* @tp_getset@ */
1279 0, /* @tp_descr_get@ */
1280 0, /* @tp_descr_set@ */
1281 0, /* @tp_dictoffset@ */
1283 PyType_GenericAlloc, /* @tp_alloc@ */
1284 ecbincurve_pynew, /* @tp_new@ */
1289 static PyObject *ecbinprojcurve_pynew(PyTypeObject *ty,
1290 PyObject *arg, PyObject *kw)
1292 return (eccurve_pynew(ty, ec_binproj, arg, kw));
1295 static PyTypeObject ecbinprojcurve_pytype_skel = {
1296 PyObject_HEAD_INIT(0) 0, /* Header */
1297 "ECBinProjCurve", /* @tp_name@ */
1298 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1299 0, /* @tp_itemsize@ */
1301 eccurve_pydealloc, /* @tp_dealloc@ */
1303 0, /* @tp_getattr@ */
1304 0, /* @tp_setattr@ */
1305 0, /* @tp_compare@ */
1307 0, /* @tp_as_number@ */
1308 0, /* @tp_as_sequence@ */
1309 0, /* @tp_as_mapping@ */
1313 0, /* @tp_getattro@ */
1314 0, /* @tp_setattro@ */
1315 0, /* @tp_as_buffer@ */
1316 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1317 Py_TPFLAGS_BASETYPE,
1320 "An elliptic curve over a binary field, using projective coordinates.",
1322 0, /* @tp_traverse@ */
1324 eccurve_pyrichcompare, /* @tp_richcompare@ */
1325 0, /* @tp_weaklistoffset@ */
1327 0, /* @tp_iternext@ */
1328 0, /* @tp_methods@ */
1329 0, /* @tp_members@ */
1330 0, /* @tp_getset@ */
1333 0, /* @tp_descr_get@ */
1334 0, /* @tp_descr_set@ */
1335 0, /* @tp_dictoffset@ */
1337 PyType_GenericAlloc, /* @tp_alloc@ */
1338 ecbinprojcurve_pynew, /* @tp_new@ */
1343 /*----- Curve info --------------------------------------------------------*/
1345 static int ncurves = -1;
1347 void ecinfo_copy(ec_info *eic, const ec_info *ei)
1349 eic->c = eccurve_copy(ei->c);
1351 EC_COPY(&eic->g, &ei->g);
1352 eic->r = MP_COPY(ei->r);
1353 eic->h = MP_COPY(ei->h);
1356 PyObject *ecinfo_pywrap(ec_info *ei)
1360 o = PyObject_NEW(ecinfo_pyobj, ecinfo_pytype);
1362 o->cobj = eccurve_pywrap(0, o->ei.c);
1364 return ((PyObject *)o);
1367 static void ecinfo_pydealloc(PyObject *me)
1369 ec_info *ei = ECINFO_EI(me);
1373 Py_DECREF(ECINFO_COBJ(me));
1377 static PyObject *ecinfo_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1381 char *kwlist[] = { "curve", "G", "r", "h", 0 };
1382 ecinfo_pyobj *rc = 0;
1384 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!O&O&:new", kwlist,
1385 eccurve_pytype, &e, ecpt_pytype, &g,
1386 convmp, &ei.r, convmp, &ei.h))
1388 if (ECPT_C(g) != ECCURVE_C(e) && !ec_samep(ECPT_C(g), ECCURVE_C(e)))
1389 TYERR("point not from this curve");
1390 ei.c = ECCURVE_C(e);
1392 EC_OUT(ei.c, &ei.g, ECPT_P(g));
1393 rc = (ecinfo_pyobj *)ty->tp_alloc(ty, 0);
1396 Py_INCREF(rc->cobj);
1397 return ((PyObject *)rc);
1405 static PyObject *meth__ECInfo_parse(PyObject *me, PyObject *arg)
1412 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
1416 if (ec_infoparse(&qd, &ei))
1418 rc = Py_BuildValue("(Ns)", ecinfo_pywrap(&ei), qd.p);
1423 static PyObject *meth__ECInfo__curven(PyObject *me, PyObject *arg)
1429 if (!PyArg_ParseTuple(arg, "Oi:_curven", &me, &i)) goto end;
1430 if (i < 0 || i >= ncurves) VALERR("curve index out of range");
1431 ec_infofromdata(&ei, ectab[i].data);
1432 rc = ecinfo_pywrap(&ei);
1437 static PyObject *ecinfo_pyrichcompare(PyObject *x, PyObject *y, int op)
1439 int b = ec_sameinfop(ECINFO_EI(x), ECINFO_EI(y));
1443 default: TYERR("can't order elliptic curve infos");
1445 return (getbool(b));
1450 static PyObject *eimeth_check(PyObject *me, PyObject *arg, PyObject *kw)
1452 char *kwlist[] = { "rng", 0 };
1453 grand *r = &rand_global;
1456 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:check", kwlist,
1459 if ((p = ec_checkinfo(ECINFO_EI(me), r)) != 0)
1466 static PyObject *eiget_curve(PyObject *me, void *hunoz)
1467 { RETURN_OBJ(ECINFO_COBJ(me)); }
1469 static PyObject *eiget_G(PyObject *me, void *hunoz)
1471 ec_info *ei = ECINFO_EI(me);
1473 EC_IN(ei->c, &p, &ei->g);
1474 return (ecpt_pywrap(ECINFO_COBJ(me), &p));
1477 static PyObject *eiget_r(PyObject *me, void *hunoz)
1478 { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->r))); }
1480 static PyObject *eiget_h(PyObject *me, void *hunoz)
1481 { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->h))); }
1483 static PyGetSetDef ecinfo_pygetset[] = {
1484 #define GETSETNAME(op, name) ei##op##_##name
1485 GET (curve, "I.curve -> the elliptic curve")
1486 GET (G, "I.G -> generator point for the group")
1487 GET (r, "I.r -> order of the group (and hence of G")
1488 GET (h, "I.h -> cofactor of the group")
1493 static PyMethodDef ecinfo_pymethods[] = {
1494 #define METHNAME(name) eimeth_##name
1495 KWMETH(check, "I.check() -> None")
1500 static PyTypeObject ecinfo_pytype_skel = {
1501 PyObject_HEAD_INIT(0) 0, /* Header */
1502 "ECInfo", /* @tp_name@ */
1503 sizeof(ecinfo_pyobj), /* @tp_basicsize@ */
1504 0, /* @tp_itemsize@ */
1506 ecinfo_pydealloc, /* @tp_dealloc@ */
1508 0, /* @tp_getattr@ */
1509 0, /* @tp_setattr@ */
1510 0, /* @tp_compare@ */
1512 0, /* @tp_as_number@ */
1513 0, /* @tp_as_sequence@ */
1514 0, /* @tp_as_mapping@ */
1518 0, /* @tp_getattro@ */
1519 0, /* @tp_setattro@ */
1520 0, /* @tp_as_buffer@ */
1521 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1522 Py_TPFLAGS_BASETYPE,
1525 "Elliptic curve domain parameters.",
1527 0, /* @tp_traverse@ */
1529 ecinfo_pyrichcompare, /* @tp_richcompare@ */
1530 0, /* @tp_weaklistoffset@ */
1532 0, /* @tp_iternext@ */
1533 ecinfo_pymethods, /* @tp_methods@ */
1534 0, /* @tp_members@ */
1535 ecinfo_pygetset, /* @tp_getset@ */
1538 0, /* @tp_descr_get@ */
1539 0, /* @tp_descr_set@ */
1540 0, /* @tp_dictoffset@ */
1542 PyType_GenericAlloc, /* @tp_alloc@ */
1543 ecinfo_pynew, /* @tp_new@ */
1548 /*----- Setup -------------------------------------------------------------*/
1550 static PyMethodDef methods[] = {
1551 #define METHNAME(func) meth_##func
1552 METH (_ECPt_frombuf, "frombuf(E, STR) -> (P, REST)")
1553 METH (_ECPtCurve_fromraw, "fromraw(E, STR) -> (P, REST)")
1554 KWMETH(_ECPtCurve_os2ecp, "os2ecp(E, STR, [flags = ...]) -> (P, REST)")
1555 METH (_ECPt_parse, "parse(E, STR) -> (P, REST)")
1556 METH (_ECCurve_parse, "parse(STR) -> (E, REST)")
1557 METH (_ECInfo_parse, "parse(STR) -> (I, REST)")
1558 METH (_ECInfo__curven, "_curven(N) -> I")
1563 void ec_pyinit(void)
1565 INITTYPE(ecpt, root);
1566 INITTYPE(ecptcurve, ecpt);
1567 INITTYPE(eccurve, type);
1568 INITTYPE(ecprimecurve, eccurve);
1569 INITTYPE(ecprimeprojcurve, ecprimecurve);
1570 INITTYPE(ecbincurve, eccurve);
1571 INITTYPE(ecbinprojcurve, ecbincurve);
1572 INITTYPE(ecinfo, root);
1573 addmethods(methods);
1576 static PyObject *namedcurves(void)
1583 for (i = 0; ectab[i].name; i++) {
1585 for (j = 0; j < i; j++) {
1586 if (ectab[i].data == ectab[j].data) {
1587 c = PyDict_GetItemString(d, (/*unconst*/ char *)ectab[j].name);
1592 c = PyInt_FromLong(i);
1594 PyDict_SetItemString(d, (/*unconst*/ char *)p, c);
1601 void ec_pyinsert(PyObject *mod)
1603 INSERT("ECPt", ecpt_pytype);
1604 INSERT("ECPtCurve", ecptcurve_pytype);
1605 INSERT("ECCurve", eccurve_pytype);
1606 INSERT("ECPrimeCurve", ecprimecurve_pytype);
1607 INSERT("ECPrimeProjCurve", ecprimeprojcurve_pytype);
1608 INSERT("ECBinCurve", ecbincurve_pytype);
1609 INSERT("ECBinProjCurve", ecbinprojcurve_pytype);
1610 INSERT("ECInfo", ecinfo_pytype);
1611 INSERT("_eccurves", namedcurves());
1614 /*----- That's all, folks -------------------------------------------------*/