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)
201 if (EC_ATINF(&p)) h = 0x81d81a94;
202 else h = 0xe0fdd039 ^ (2*mphash(p.x)) ^ (3*mphash(p.y));
207 static PyObject *ecpt_pyrichcompare(PyObject *x, PyObject *y, int op)
209 ec p = EC_INIT, q = EC_INIT;
213 if (!ECPT_PYCHECK(y)) RETURN_NOTIMPL;
217 case Py_EQ: b = EC_EQ(&p, &q); break;
218 case Py_NE: b = !EC_EQ(&p, &q); break;
219 default: TYERR("elliptic curve points are unordered");
228 static PyObject *epmeth_oncurvep(PyObject *me, PyObject *arg)
230 if (!PyArg_ParseTuple(arg, ":oncurvep")) return (0);
231 return (getbool(EC_ATINF(ECPT_P(me)) ||
232 !EC_CHECK(ECPT_C(me), ECPT_P(me))));
235 static PyObject *epmeth_dbl(PyObject *me, PyObject *arg)
238 if (!PyArg_ParseTuple(arg, ":dbl")) return (0);
239 EC_DBL(ECPT_C(me), &p, ECPT_P(me));
240 return (ecpt_pywrap(ECPT_COBJ(me), &p));
243 static PyObject *epmeth_tobuf(PyObject *me, PyObject *arg)
250 if (!PyArg_ParseTuple(arg, ":tobuf")) return (0);
255 n = mp_octets(p.x) + mp_octets(p.y) + 6;
256 rc = bytestring_pywrap(0, n);
257 buf_init(&b, PyString_AS_STRING(rc), n);
260 _PyString_Resize(&rc, BLEN(&b));
265 static PyObject *epmeth_toraw(PyObject *me, PyObject *arg)
270 ec_curve *c = ECPT_C(me);
274 if (!PyArg_ParseTuple(arg, ":toraw")) return (0);
275 len = c->f->noctets * 2 + 1;
276 rc = bytestring_pywrap(0, len);
277 p = PyString_AS_STRING(rc);
278 buf_init(&b, p, len);
279 EC_OUT(c, &pp, ECPT_P(me));
280 ec_putraw(c, &b, &pp);
282 _PyString_Resize(&rc, BLEN(&b));
286 static PyObject *epmeth_ec2osp(PyObject *me, PyObject *arg, PyObject *kw)
291 ec_curve *c = ECPT_C(me);
293 unsigned f = EC_EXPLY;
295 char *kwlist[] = { "flags", 0 };
297 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:ec2osp", kwlist,
300 len = c->f->noctets * 2 + 1;
301 rc = bytestring_pywrap(0, len);
302 p = PyString_AS_STRING(rc);
303 buf_init(&b, p, len);
304 EC_OUT(c, &pp, ECPT_P(me));
305 if (ec_ec2osp(c, f, &b, &pp)) {
306 Py_DECREF(rc); rc = 0;
307 VALERR("invalid flags");
310 _PyString_Resize(&rc, BLEN(&b));
315 static PyObject *epget_curve(PyObject *me, void *hunoz)
316 { RETURN_OBJ(ECPT_COBJ(me)); }
318 static PyObject *epncget_ix(PyObject *me, void *hunoz)
322 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
324 rc = mp_pywrap(MP_COPY(p.x));
329 static PyObject *epncget_iy(PyObject *me, void *hunoz)
333 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
335 rc = mp_pywrap(MP_COPY(p.y));
340 static PyObject *epncget_point(PyObject *me, void *hunoz)
343 static PyObject *epget_point(PyObject *me, void *hunoz)
347 return (ecpt_pywrapout(ecpt_pytype, &p));
350 static PyObject *epget_x(PyObject *me, void *hunoz)
352 ec_curve *c = ECPT_C(me);
354 PyObject *fobj = ECPT_FOBJ(me);
358 if (EC_ATINF(pp)) RETURN_NONE;
360 rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.x));
365 static PyObject *epget_y(PyObject *me, void *hunoz)
367 ec_curve *c = ECPT_C(me);
369 PyObject *fobj = ECPT_FOBJ(me);
373 if (EC_ATINF(pp)) RETURN_NONE;
375 rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.y));
380 static PyObject *epget__x(PyObject *me, void *hunoz)
382 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
383 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->x)));
386 static PyObject *epget__y(PyObject *me, void *hunoz)
388 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
389 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->y)));
392 static PyObject *epget__z(PyObject *me, void *hunoz)
394 if (EC_ATINF(ECPT_P(me)) || !ECPT_P(me)->z) RETURN_NONE;
395 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->z)));
398 static mp *coord_in(field *f, PyObject *x)
401 if (FE_PYCHECK(x) && FE_F(x) == f)
402 return (MP_COPY(FE_X(x)));
403 else if ((xx = getmp(x)) == 0)
406 return (F_IN(f, xx, xx));
409 static int ecptxl_3(ec_curve *c, ec *p,
410 PyObject *x, PyObject *y, PyObject *z)
414 if (!x || !y || !z) TYERR("missing argument");
415 if (!c) VALERR("internal form with no curve!");
416 if ((p->x = coord_in(c->f, x)) == 0 ||
417 (p->y = coord_in(c->f, y)) == 0 ||
418 (z != Py_None && (p->z = coord_in(c->f, z)) == 0))
420 if (!p->z) p->z = MP_COPY(c->f->one); /* just in case */
426 static int ecptxl_2(ec_curve *c, ec *p, PyObject *x, PyObject *y)
430 if (!x || !y) TYERR("missing argument");
431 if ((p->x = getmp(x)) == 0 ||
432 (p->y = getmp(y)) == 0)
434 if (c) EC_IN(c, p, p);
440 static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
443 PyObject *y = 0, *z = 0, *t = 0;
450 if (!x || x == Py_None)
452 else if (ECPT_PYCHECK(x)) {
455 } else if (PyString_Check(x)) {
456 if (PyObject_AsReadBuffer(x, &q, &n))
460 if (!ec_ptparse(&qd, p))
463 } else if (c && (xx = tomp(x)) != 0) {
464 xx = F_IN(c->f, xx, xx);
465 if (!EC_FIND(c, p, xx)) VALERR("not on the curve");
466 } else if (PySequence_Check(x)) {
468 n = PySequence_Size(t); if (n < 0) goto end;
469 if (n != 2 && (n != 3 || !c))
470 TYERR("want sequence of two or three items");
471 if ((x = PySequence_GetItem(t, 0)) == 0 ||
472 (y = PySequence_GetItem(t, 1)) == 0 ||
473 (n == 3 && (z = PySequence_GetItem(t, 2)) == 0))
475 rc = (n == 2) ? ecptxl_2(c, p, x, y) : ecptxl_3(c, p, x, y, z);
478 TYERR("can't convert to curve point");
482 if (c) EC_IN(c, p, p);
486 Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(z); Py_XDECREF(t);
491 static int ecptxl(ec_curve *c, ec *p, PyObject *x, PyObject *y, PyObject *z)
494 return (ecptxl_3(c, p, x, y, z));
496 return (ecptxl_2(c, p, x, y));
498 return (ecptxl_1(c, p, x));
501 static PyObject *ecptnc_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
503 PyObject *x = 0, *y = 0, *z = 0;
505 char *kwlist[] = { "x", "y", 0 };
507 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:new", kwlist, &x, &y) ||
508 ecptxl(0, &p, x, y, z))
510 return (ecpt_pywrapout(ty, &p));
512 mp_drop(p.x); mp_drop(p.y); mp_drop(p.z);
516 static PyObject *ecpt_pyint(PyObject *me)
521 if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
523 if (!mp_tolong_checked(p.x, &l, 0)) rc = PyInt_FromLong(l);
524 else rc = mp_topylong(p.x);
530 static PyObject *ecpt_pylong(PyObject *me)
534 if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
536 rc = mp_topylong(p.x);
542 static PyObject *ecpt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
544 PyObject *x = 0, *y = 0, *z = 0;
546 char *kwlist[] = { "x", "y", "z", 0 };
548 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OOO:new", kwlist,
550 ecptxl(ECCURVE_C(ty), &p, x, y, z))
552 return (ecpt_pywrap((PyObject *)ty, &p));
554 mp_drop(p.x); mp_drop(p.y); mp_drop(p.z);
558 static PyGetSetDef ecptnc_pygetset[] = {
559 #define GETSETNAME(op, name) epnc##op##_##name
560 GET (ix, "P.ix -> integer x coordinate of P")
561 GET (iy, "P.iy -> integer y coordinate of P")
562 GET (point, "P.point -> standalone curve point (no-op)")
567 static PyMethodDef ecptnc_pymethods[] = {
568 #define METHNAME(func) epmeth_##func
569 METH (tobuf, "X.tobuf() -> BIN")
574 static PyNumberMethods ecpt_pynumber = {
576 0, /* @nb_subtract@ */
577 0, /* @nb_multiply@ */
579 0, /* @nb_remainder@ */
582 0, /* @nb_negative@ */
583 0, /* @nb_positive@ */
584 0, /* @nb_absolute@ */
585 ecpt_pynonzerop, /* @nb_nonzero@ */
593 ecpt_pyint, /* @nb_int@ */
594 ecpt_pylong, /* @nb_long@ */
599 0, /* @nb_inplace_add@ */
600 0, /* @nb_inplace_subtract@ */
601 0, /* @nb_inplace_multiply@ */
602 0, /* @nb_inplace_divide@ */
603 0, /* @nb_inplace_remainder@ */
604 0, /* @nb_inplace_power@ */
605 0, /* @nb_inplace_lshift@ */
606 0, /* @nb_inplace_rshift@ */
607 0, /* @nb_inplace_and@ */
608 0, /* @nb_inplace_xor@ */
609 0, /* @nb_inplace_or@ */
611 0, /* @nb_floor_divide@ */
612 0, /* @nb_true_divide@ */
613 0, /* @nb_inplace_floor_divide@ */
614 0, /* @nb_inplace_true_divide@ */
617 static PyTypeObject ecpt_pytype_skel = {
618 PyObject_HEAD_INIT(0) 0, /* Header */
619 "ECPt", /* @tp_name@ */
620 sizeof(ecpt_pyobj), /* @tp_basicsize@ */
621 0, /* @tp_itemsize@ */
623 ecpt_pydealloc, /* @tp_dealloc@ */
625 0, /* @tp_getattr@ */
626 0, /* @tp_setattr@ */
627 0, /* @tp_compare@ */
629 &ecpt_pynumber, /* @tp_as_number@ */
630 0, /* @tp_as_sequence@ */
631 0, /* @tp_as_mapping@ */
632 ecpt_pyhash, /* @tp_hash@ */
635 0, /* @tp_getattro@ */
636 0, /* @tp_setattro@ */
637 0, /* @tp_as_buffer@ */
638 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
639 Py_TPFLAGS_CHECKTYPES |
643 "Elliptic curve points, not associated with any curve.",
645 0, /* @tp_traverse@ */
647 ecpt_pyrichcompare, /* @tp_richcompare@ */
648 0, /* @tp_weaklistoffset@ */
650 0, /* @tp_iternext@ */
651 ecptnc_pymethods, /* @tp_methods@ */
652 0, /* @tp_members@ */
653 ecptnc_pygetset, /* @tp_getset@ */
656 0, /* @tp_descr_get@ */
657 0, /* @tp_descr_set@ */
658 0, /* @tp_dictoffset@ */
660 PyType_GenericAlloc, /* @tp_alloc@ */
661 ecptnc_pynew, /* @tp_new@ */
666 static PyGetSetDef ecpt_pygetset[] = {
667 #define GETSETNAME(op, name) ep##op##_##name
668 GET (curve, "P.curve -> elliptic curve containing P")
669 GET (point, "P.point -> standalone curve point")
670 GET (x, "P.x -> Cartesian x coordinate of P")
671 GET (y, "P.y -> Cartesian y coordinate of P")
672 GET (_x, "P._x -> internal x coordinate of P")
673 GET (_y, "P._y -> internal y coordinate of P")
674 GET (_z, "P._z -> internal z coordinate of P, or None")
679 static PyMethodDef ecpt_pymethods[] = {
680 #define METHNAME(func) epmeth_##func
681 METH (toraw, "X.toraw() -> BIN")
682 KWMETH(ec2osp, "X.ec2osp([flags = EC_EXPLY]) -> BIN")
683 METH (dbl, "X.dbl() -> X + X")
684 METH (oncurvep, "X.oncurvep() -> BOOL")
689 static PyNumberMethods ecptcurve_pynumber = {
690 ecpt_pyadd, /* @nb_add@ */
691 ecpt_pysub, /* @nb_subtract@ */
692 ecpt_pymul, /* @nb_multiply@ */
694 0, /* @nb_remainder@ */
697 ecpt_pyneg, /* @nb_negative@ */
698 ecpt_pyid, /* @nb_positive@ */
699 0, /* @nb_absolute@ */
700 0, /* @nb_nonzero@ */
714 0, /* @nb_inplace_add@ */
715 0, /* @nb_inplace_subtract@ */
716 0, /* @nb_inplace_multiply@ */
717 0, /* @nb_inplace_divide@ */
718 0, /* @nb_inplace_remainder@ */
719 0, /* @nb_inplace_power@ */
720 0, /* @nb_inplace_lshift@ */
721 0, /* @nb_inplace_rshift@ */
722 0, /* @nb_inplace_and@ */
723 0, /* @nb_inplace_xor@ */
724 0, /* @nb_inplace_or@ */
726 0, /* @nb_floor_divide@ */
727 0, /* @nb_true_divide@ */
728 0, /* @nb_inplace_floor_divide@ */
729 0, /* @nb_inplace_true_divide@ */
732 static PyTypeObject ecptcurve_pytype_skel = {
733 PyObject_HEAD_INIT(0) 0, /* Header */
734 "ECPtCurve", /* @tp_name@ */
735 sizeof(ecpt_pyobj), /* @tp_basicsize@ */
736 0, /* @tp_itemsize@ */
738 ecpt_pydealloc, /* @tp_dealloc@ */
740 0, /* @tp_getattr@ */
741 0, /* @tp_setattr@ */
742 0, /* @tp_compare@ */
744 &ecptcurve_pynumber, /* @tp_as_number@ */
745 0, /* @tp_as_sequence@ */
746 0, /* @tp_as_mapping@ */
750 0, /* @tp_getattro@ */
751 0, /* @tp_setattro@ */
752 0, /* @tp_as_buffer@ */
753 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
754 Py_TPFLAGS_CHECKTYPES |
758 "Elliptic curve points; abstract base class for points on given curves.",
760 0, /* @tp_traverse@ */
762 0, /* @tp_richcompare@ */
763 0, /* @tp_weaklistoffset@ */
765 0, /* @tp_iternext@ */
766 ecpt_pymethods, /* @tp_methods@ */
767 0, /* @tp_members@ */
768 ecpt_pygetset, /* @tp_getset@ */
771 0, /* @tp_descr_get@ */
772 0, /* @tp_descr_set@ */
773 0, /* @tp_dictoffset@ */
775 PyType_GenericAlloc, /* @tp_alloc@ */
776 abstract_pynew, /* @tp_new@ */
781 /*----- Elliptic curves themselves ----------------------------------------*/
783 static PyObject *eccurve_pyrichcompare(PyObject *x, PyObject *y, int op)
787 assert(ECCURVE_PYCHECK(x));
788 if (!ECCURVE_PYCHECK(y)) RETURN_NOTIMPL;
789 b = ec_samep(ECCURVE_C(x), ECCURVE_C(y));
792 case Py_NE: b = !b; break;
793 default: TYERR("can't order elliptic curves");
800 static PyObject *ecmmul_id(PyObject *me)
801 { ec p = EC_INIT; return (ecpt_pywrap(me, &p)); }
803 static int ecmmul_fill(void *pp, PyObject *me, PyObject *x, PyObject *m)
805 ec_mulfactor *f = pp;
808 if (getecpt(ECCURVE_C(me), &f->base, x) ||
809 (f->exp = getmp(m)) == 0)
814 static PyObject *ecmmul_exp(PyObject *me, void *pp, int n)
817 ec_immul(ECCURVE_C(me), &p, pp, n);
818 return (ecpt_pywrap(me, &p));
821 static void ecmmul_drop(void *pp)
823 ec_mulfactor *f = pp;
824 EC_DESTROY(&f->base);
828 static PyObject *ecmeth_mmul(PyObject *me, PyObject *arg)
830 return (mexp_common(me, arg, sizeof(ec_mulfactor),
831 ecmmul_id, ecmmul_fill, ecmmul_exp, ecmmul_drop));
834 static PyObject *meth__ECPtCurve_fromraw(PyObject *me, PyObject *arg)
843 if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &len))
845 buf_init(&b, p, len);
847 if (ec_getraw(cc, &b, &pp))
850 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
855 static PyObject *meth__ECPtCurve_os2ecp(PyObject *me,
856 PyObject *arg, PyObject *kw)
863 unsigned f = EC_XONLY | EC_LSB | EC_SORT | EC_EXPLY;
865 char *kwlist[] = { "class", "buf", "flags", 0 };
867 if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|O&:os2ecp", kwlist,
868 &me, &p, &len, convuint, &f))
870 buf_init(&b, p, len);
872 if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point");
874 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
879 static PyObject *meth__ECPt_frombuf(PyObject *me, PyObject *arg)
887 if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end;
889 if (buf_getec(&b, &pp)) VALERR("malformed data");
890 rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp),
891 bytestring_pywrapbuf(&b));
896 static PyObject *meth__ECPt_parse(PyObject *me, PyObject *arg)
903 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
906 if (!ec_ptparse(&qd, &pp)) VALERR(qd.e);
907 rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p);
912 static void eccurve_pydealloc(PyObject *me)
914 ec_destroycurve(ECCURVE_C(me));
915 Py_DECREF(ECCURVE_FOBJ(me));
916 PyType_Type.tp_dealloc(me);
919 static PyObject *ecmeth_find(PyObject *me, PyObject *arg)
926 if (!PyArg_ParseTuple(arg, "O:find", &x)) goto end;
927 if (FIELD_PYCHECK(x) && FE_F(x) == ECCURVE_C(me)->f)
928 xx = MP_COPY(FE_X(x));
929 else if ((xx = getmp(x)) == 0)
932 xx = F_IN(ECCURVE_C(me)->f, xx, xx);
933 if (EC_FIND(ECCURVE_C(me), &p, xx) == 0)
934 VALERR("not on the curve");
935 rc = ecpt_pywrap(me, &p);
941 static PyObject *ecmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
943 char *kwlist[] = { "rng", 0 };
944 grand *r = &rand_global;
947 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", kwlist,
950 ec_rand(ECCURVE_C(me), &p, r);
951 EC_IN(ECCURVE_C(me), &p, &p);
952 return (ecpt_pywrap(me, &p));
955 static PyObject *eccurve_dopywrap(PyTypeObject *ty,
956 PyObject *fobj, ec_curve *c)
958 eccurve_pyobj *cobj = newtype(ty, 0, c->ops->name);
961 cobj->ty.ht_type.tp_basicsize = sizeof(ecpt_pyobj);
962 cobj->ty.ht_type.tp_base = ecptcurve_pytype;
963 Py_INCREF(ecptcurve_pytype);
964 cobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
965 Py_TPFLAGS_BASETYPE |
966 Py_TPFLAGS_CHECKTYPES |
967 Py_TPFLAGS_HEAPTYPE);
968 cobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
969 cobj->ty.ht_type.tp_free = 0;
970 cobj->ty.ht_type.tp_new = ecpt_pynew;
971 typeready(&cobj->ty.ht_type);
972 return ((PyObject *)cobj);
975 PyObject *eccurve_pywrap(PyObject *fobj, ec_curve *c)
980 fobj = field_pywrap(c->f);
983 assert(FIELD_F(fobj) == c->f);
984 if (strcmp(EC_NAME(c), "prime") == 0)
985 ty = ecprimecurve_pytype;
986 else if (strcmp(EC_NAME(c), "primeproj") == 0)
987 ty = ecprimeprojcurve_pytype;
988 else if (strcmp(EC_NAME(c), "bin") == 0)
989 ty = ecbincurve_pytype;
990 else if (strcmp(EC_NAME(c), "binproj") == 0)
991 ty = ecbinprojcurve_pytype;
994 return (eccurve_dopywrap(ty, fobj, c));
997 static PyObject *eccurve_pynew(PyTypeObject *ty,
998 ec_curve *(*make)(field *, mp *, mp *),
999 PyObject *arg, PyObject *kw)
1003 char *kwlist[] = { "field", "a", "b", 0 };
1004 mp *aa = 0, *bb = 0;
1006 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&O&", kwlist,
1007 field_pytype, &fobj,
1008 convmp, &aa, convmp, &bb))
1011 cobj = eccurve_dopywrap(ty, fobj, make(FIELD_F(fobj), aa, bb));
1013 if (aa) MP_DROP(aa);
1014 if (bb) MP_DROP(bb);
1018 static PyObject *meth__ECCurve_parse(PyObject *me, PyObject *arg)
1025 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
1029 if ((c = ec_curveparse(&qd)) == 0)
1031 rc = eccurve_pywrap(0, c);
1036 static PyObject *ecget_name(PyObject *me, void *hunoz)
1037 { return (PyString_FromString(EC_NAME(ECCURVE_C(me)))); }
1039 static PyObject *ecget_a(PyObject *me, void *hunoz)
1040 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->a))); }
1042 static PyObject *ecget_b(PyObject *me, void *hunoz)
1043 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->b))); }
1045 static PyObject *ecget_field(PyObject *me, void *hunoz)
1046 { RETURN_OBJ(ECCURVE_FOBJ(me)); }
1048 static PyObject *ecget_inf(PyObject *me, void *hunoz)
1049 { ec inf = EC_INIT; return (ecpt_pywrap(me, &inf)); }
1051 static PyGetSetDef eccurve_pygetset[] = {
1052 #define GETSETNAME(op, name) ec##op##_##name
1053 GET (name, "E.name -> name of this kind of curve")
1054 GET (a, "E.a -> first parameter of curve")
1055 GET (b, "E.b -> second parameter of curve")
1056 GET (field, "E.field -> finite field containing this curve")
1057 GET (inf, "E.inf -> point at infinity of this curve")
1062 static PyMethodDef eccurve_pymethods[] = {
1063 #define METHNAME(name) ecmeth_##name
1065 E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...")
1066 METH (find, "E.find(X) -> P")
1067 KWMETH(rand, "E.rand([rng = rand]) -> P")
1072 static PyTypeObject eccurve_pytype_skel = {
1073 PyObject_HEAD_INIT(0) 0, /* Header */
1074 "ECCurve", /* @tp_name@ */
1075 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1076 0, /* @tp_itemsize@ */
1078 eccurve_pydealloc, /* @tp_dealloc@ */
1080 0, /* @tp_getattr@ */
1081 0, /* @tp_setattr@ */
1082 0, /* @tp_compare@ */
1084 0, /* @tp_as_number@ */
1085 0, /* @tp_as_sequence@ */
1086 0, /* @tp_as_mapping@ */
1090 0, /* @tp_getattro@ */
1091 0, /* @tp_setattro@ */
1092 0, /* @tp_as_buffer@ */
1093 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1094 Py_TPFLAGS_BASETYPE,
1097 "An elliptic curve. Abstract class.",
1099 0, /* @tp_traverse@ */
1101 eccurve_pyrichcompare, /* @tp_richcompare@ */
1102 0, /* @tp_weaklistoffset@ */
1104 0, /* @tp_iternext@ */
1105 eccurve_pymethods, /* @tp_methods@ */
1106 0, /* @tp_members@ */
1107 eccurve_pygetset, /* @tp_getset@ */
1110 0, /* @tp_descr_get@ */
1111 0, /* @tp_descr_set@ */
1112 0, /* @tp_dictoffset@ */
1114 PyType_GenericAlloc, /* @tp_alloc@ */
1115 abstract_pynew, /* @tp_new@ */
1120 static PyObject *ecprimecurve_pynew(PyTypeObject *ty,
1121 PyObject *arg, PyObject *kw)
1123 return (eccurve_pynew(ty, ec_prime, arg, kw));
1126 static PyTypeObject ecprimecurve_pytype_skel = {
1127 PyObject_HEAD_INIT(0) 0, /* Header */
1128 "ECPrimeCurve", /* @tp_name@ */
1129 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1130 0, /* @tp_itemsize@ */
1132 eccurve_pydealloc, /* @tp_dealloc@ */
1134 0, /* @tp_getattr@ */
1135 0, /* @tp_setattr@ */
1136 0, /* @tp_compare@ */
1138 0, /* @tp_as_number@ */
1139 0, /* @tp_as_sequence@ */
1140 0, /* @tp_as_mapping@ */
1144 0, /* @tp_getattro@ */
1145 0, /* @tp_setattro@ */
1146 0, /* @tp_as_buffer@ */
1147 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1148 Py_TPFLAGS_BASETYPE,
1151 "An elliptic curve over a prime field. Use ecprimeprojcurve.",
1153 0, /* @tp_traverse@ */
1155 eccurve_pyrichcompare, /* @tp_richcompare@ */
1156 0, /* @tp_weaklistoffset@ */
1158 0, /* @tp_iternext@ */
1159 0, /* @tp_methods@ */
1160 0, /* @tp_members@ */
1161 0, /* @tp_getset@ */
1164 0, /* @tp_descr_get@ */
1165 0, /* @tp_descr_set@ */
1166 0, /* @tp_dictoffset@ */
1168 PyType_GenericAlloc, /* @tp_alloc@ */
1169 ecprimecurve_pynew, /* @tp_new@ */
1174 static PyObject *ecprimeprojcurve_pynew(PyTypeObject *ty,
1175 PyObject *arg, PyObject *kw)
1177 return (eccurve_pynew(ty, ec_primeproj, arg, kw));
1180 static PyTypeObject ecprimeprojcurve_pytype_skel = {
1181 PyObject_HEAD_INIT(0) 0, /* Header */
1182 "ECPrimeProjCurve", /* @tp_name@ */
1183 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1184 0, /* @tp_itemsize@ */
1186 eccurve_pydealloc, /* @tp_dealloc@ */
1188 0, /* @tp_getattr@ */
1189 0, /* @tp_setattr@ */
1190 0, /* @tp_compare@ */
1192 0, /* @tp_as_number@ */
1193 0, /* @tp_as_sequence@ */
1194 0, /* @tp_as_mapping@ */
1198 0, /* @tp_getattro@ */
1199 0, /* @tp_setattro@ */
1200 0, /* @tp_as_buffer@ */
1201 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1202 Py_TPFLAGS_BASETYPE,
1205 "An elliptic curve over a prime field, using projective coordinates.",
1207 0, /* @tp_traverse@ */
1209 eccurve_pyrichcompare, /* @tp_richcompare@ */
1210 0, /* @tp_weaklistoffset@ */
1212 0, /* @tp_iternext@ */
1213 0, /* @tp_methods@ */
1214 0, /* @tp_members@ */
1215 0, /* @tp_getset@ */
1218 0, /* @tp_descr_get@ */
1219 0, /* @tp_descr_set@ */
1220 0, /* @tp_dictoffset@ */
1222 PyType_GenericAlloc, /* @tp_alloc@ */
1223 ecprimeprojcurve_pynew, /* @tp_new@ */
1228 static PyObject *ecbincurve_pynew(PyTypeObject *ty,
1229 PyObject *arg, PyObject *kw)
1231 return (eccurve_pynew(ty, ec_bin, arg, kw));
1234 static PyTypeObject ecbincurve_pytype_skel = {
1235 PyObject_HEAD_INIT(0) 0, /* Header */
1236 "ECBinCurve", /* @tp_name@ */
1237 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1238 0, /* @tp_itemsize@ */
1240 eccurve_pydealloc, /* @tp_dealloc@ */
1242 0, /* @tp_getattr@ */
1243 0, /* @tp_setattr@ */
1244 0, /* @tp_compare@ */
1246 0, /* @tp_as_number@ */
1247 0, /* @tp_as_sequence@ */
1248 0, /* @tp_as_mapping@ */
1252 0, /* @tp_getattro@ */
1253 0, /* @tp_setattro@ */
1254 0, /* @tp_as_buffer@ */
1255 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1256 Py_TPFLAGS_BASETYPE,
1259 "An elliptic curve over a binary field. Use ecbinprojcurve.",
1261 0, /* @tp_traverse@ */
1263 eccurve_pyrichcompare, /* @tp_richcompare@ */
1264 0, /* @tp_weaklistoffset@ */
1266 0, /* @tp_iternext@ */
1267 0, /* @tp_methods@ */
1268 0, /* @tp_members@ */
1269 0, /* @tp_getset@ */
1272 0, /* @tp_descr_get@ */
1273 0, /* @tp_descr_set@ */
1274 0, /* @tp_dictoffset@ */
1276 PyType_GenericAlloc, /* @tp_alloc@ */
1277 ecbincurve_pynew, /* @tp_new@ */
1282 static PyObject *ecbinprojcurve_pynew(PyTypeObject *ty,
1283 PyObject *arg, PyObject *kw)
1285 return (eccurve_pynew(ty, ec_binproj, arg, kw));
1288 static PyTypeObject ecbinprojcurve_pytype_skel = {
1289 PyObject_HEAD_INIT(0) 0, /* Header */
1290 "ECBinProjCurve", /* @tp_name@ */
1291 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1292 0, /* @tp_itemsize@ */
1294 eccurve_pydealloc, /* @tp_dealloc@ */
1296 0, /* @tp_getattr@ */
1297 0, /* @tp_setattr@ */
1298 0, /* @tp_compare@ */
1300 0, /* @tp_as_number@ */
1301 0, /* @tp_as_sequence@ */
1302 0, /* @tp_as_mapping@ */
1306 0, /* @tp_getattro@ */
1307 0, /* @tp_setattro@ */
1308 0, /* @tp_as_buffer@ */
1309 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1310 Py_TPFLAGS_BASETYPE,
1313 "An elliptic curve over a binary field, using projective coordinates.",
1315 0, /* @tp_traverse@ */
1317 eccurve_pyrichcompare, /* @tp_richcompare@ */
1318 0, /* @tp_weaklistoffset@ */
1320 0, /* @tp_iternext@ */
1321 0, /* @tp_methods@ */
1322 0, /* @tp_members@ */
1323 0, /* @tp_getset@ */
1326 0, /* @tp_descr_get@ */
1327 0, /* @tp_descr_set@ */
1328 0, /* @tp_dictoffset@ */
1330 PyType_GenericAlloc, /* @tp_alloc@ */
1331 ecbinprojcurve_pynew, /* @tp_new@ */
1336 /*----- Curve info --------------------------------------------------------*/
1338 static int ncurves = -1;
1340 void ecinfo_copy(ec_info *eic, const ec_info *ei)
1342 eic->c = eccurve_copy(ei->c);
1344 EC_COPY(&eic->g, &ei->g);
1345 eic->r = MP_COPY(ei->r);
1346 eic->h = MP_COPY(ei->h);
1349 PyObject *ecinfo_pywrap(ec_info *ei)
1353 o = PyObject_NEW(ecinfo_pyobj, ecinfo_pytype);
1355 o->cobj = eccurve_pywrap(0, o->ei.c);
1357 return ((PyObject *)o);
1360 static void ecinfo_pydealloc(PyObject *me)
1362 ec_info *ei = ECINFO_EI(me);
1366 Py_DECREF(ECINFO_COBJ(me));
1370 static PyObject *ecinfo_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1374 char *kwlist[] = { "curve", "G", "r", "h", 0 };
1375 ecinfo_pyobj *rc = 0;
1377 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!O&O&:new", kwlist,
1378 eccurve_pytype, &e, ecpt_pytype, &g,
1379 convmp, &ei.r, convmp, &ei.h))
1381 if (ECPT_C(g) != ECCURVE_C(e) && !ec_samep(ECPT_C(g), ECCURVE_C(e)))
1382 TYERR("point not from this curve");
1383 ei.c = ECCURVE_C(e);
1385 EC_OUT(ei.c, &ei.g, ECPT_P(g));
1386 rc = (ecinfo_pyobj *)ty->tp_alloc(ty, 0);
1389 Py_INCREF(rc->cobj);
1390 return ((PyObject *)rc);
1398 static PyObject *meth__ECInfo_parse(PyObject *me, PyObject *arg)
1405 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
1409 if (ec_infoparse(&qd, &ei))
1411 rc = Py_BuildValue("(Ns)", ecinfo_pywrap(&ei), qd.p);
1416 static PyObject *meth__ECInfo__curven(PyObject *me, PyObject *arg)
1422 if (!PyArg_ParseTuple(arg, "Oi:_curven", &me, &i)) goto end;
1423 if (i < 0 || i >= ncurves) VALERR("curve index out of range");
1424 ec_infofromdata(&ei, ectab[i].data);
1425 rc = ecinfo_pywrap(&ei);
1430 static PyObject *ecinfo_pyrichcompare(PyObject *x, PyObject *y, int op)
1432 int b = ec_sameinfop(ECINFO_EI(x), ECINFO_EI(y));
1436 default: TYERR("can't order elliptic curve infos");
1438 return (getbool(b));
1443 static PyObject *eimeth_check(PyObject *me, PyObject *arg, PyObject *kw)
1445 char *kwlist[] = { "rng", 0 };
1446 grand *r = &rand_global;
1449 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:check", kwlist,
1452 if ((p = ec_checkinfo(ECINFO_EI(me), r)) != 0)
1459 static PyObject *eiget_curve(PyObject *me, void *hunoz)
1460 { RETURN_OBJ(ECINFO_COBJ(me)); }
1462 static PyObject *eiget_G(PyObject *me, void *hunoz)
1464 ec_info *ei = ECINFO_EI(me);
1466 EC_IN(ei->c, &p, &ei->g);
1467 return (ecpt_pywrap(ECINFO_COBJ(me), &p));
1470 static PyObject *eiget_r(PyObject *me, void *hunoz)
1471 { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->r))); }
1473 static PyObject *eiget_h(PyObject *me, void *hunoz)
1474 { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->h))); }
1476 static PyGetSetDef ecinfo_pygetset[] = {
1477 #define GETSETNAME(op, name) ei##op##_##name
1478 GET (curve, "I.curve -> the elliptic curve")
1479 GET (G, "I.G -> generator point for the group")
1480 GET (r, "I.r -> order of the group (and hence of G")
1481 GET (h, "I.h -> cofactor of the group")
1486 static PyMethodDef ecinfo_pymethods[] = {
1487 #define METHNAME(name) eimeth_##name
1488 KWMETH(check, "I.check([rng = rand]) -> None")
1493 static PyTypeObject ecinfo_pytype_skel = {
1494 PyObject_HEAD_INIT(0) 0, /* Header */
1495 "ECInfo", /* @tp_name@ */
1496 sizeof(ecinfo_pyobj), /* @tp_basicsize@ */
1497 0, /* @tp_itemsize@ */
1499 ecinfo_pydealloc, /* @tp_dealloc@ */
1501 0, /* @tp_getattr@ */
1502 0, /* @tp_setattr@ */
1503 0, /* @tp_compare@ */
1505 0, /* @tp_as_number@ */
1506 0, /* @tp_as_sequence@ */
1507 0, /* @tp_as_mapping@ */
1511 0, /* @tp_getattro@ */
1512 0, /* @tp_setattro@ */
1513 0, /* @tp_as_buffer@ */
1514 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1515 Py_TPFLAGS_BASETYPE,
1518 "Elliptic curve domain parameters.",
1520 0, /* @tp_traverse@ */
1522 ecinfo_pyrichcompare, /* @tp_richcompare@ */
1523 0, /* @tp_weaklistoffset@ */
1525 0, /* @tp_iternext@ */
1526 ecinfo_pymethods, /* @tp_methods@ */
1527 0, /* @tp_members@ */
1528 ecinfo_pygetset, /* @tp_getset@ */
1531 0, /* @tp_descr_get@ */
1532 0, /* @tp_descr_set@ */
1533 0, /* @tp_dictoffset@ */
1535 PyType_GenericAlloc, /* @tp_alloc@ */
1536 ecinfo_pynew, /* @tp_new@ */
1541 /*----- Setup -------------------------------------------------------------*/
1543 static PyMethodDef methods[] = {
1544 #define METHNAME(func) meth_##func
1545 METH (_ECPt_frombuf, "frombuf(E, STR) -> (P, REST)")
1546 METH (_ECPtCurve_fromraw, "fromraw(E, STR) -> (P, REST)")
1547 KWMETH(_ECPtCurve_os2ecp, "os2ecp(E, STR, [flags = ...]) -> (P, REST)")
1548 METH (_ECPt_parse, "parse(E, STR) -> (P, REST)")
1549 METH (_ECCurve_parse, "parse(STR) -> (E, REST)")
1550 METH (_ECInfo_parse, "parse(STR) -> (I, REST)")
1551 METH (_ECInfo__curven, "_curven(N) -> I")
1556 void ec_pyinit(void)
1558 INITTYPE(ecpt, root);
1559 INITTYPE(ecptcurve, ecpt);
1560 INITTYPE(eccurve, type);
1561 INITTYPE(ecprimecurve, eccurve);
1562 INITTYPE(ecprimeprojcurve, ecprimecurve);
1563 INITTYPE(ecbincurve, eccurve);
1564 INITTYPE(ecbinprojcurve, ecbincurve);
1565 INITTYPE(ecinfo, root);
1566 addmethods(methods);
1569 static PyObject *namedcurves(void)
1576 for (i = 0; ectab[i].name; i++) {
1578 for (j = 0; j < i; j++) {
1579 if (ectab[i].data == ectab[j].data) {
1580 c = PyDict_GetItemString(d, (/*unconst*/ char *)ectab[j].name);
1585 c = PyInt_FromLong(i);
1587 PyDict_SetItemString(d, (/*unconst*/ char *)p, c);
1594 void ec_pyinsert(PyObject *mod)
1596 INSERT("ECPt", ecpt_pytype);
1597 INSERT("ECPtCurve", ecptcurve_pytype);
1598 INSERT("ECCurve", eccurve_pytype);
1599 INSERT("ECPrimeCurve", ecprimecurve_pytype);
1600 INSERT("ECPrimeProjCurve", ecprimeprojcurve_pytype);
1601 INSERT("ECBinCurve", ecbincurve_pytype);
1602 INSERT("ECBinProjCurve", ecbinprojcurve_pytype);
1603 INSERT("ECInfo", ecinfo_pytype);
1604 INSERT("_eccurves", namedcurves());
1607 /*----- That's all, folks -------------------------------------------------*/