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) + 4;
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, 0))
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);
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);
477 TYERR("can't convert to curve point");
481 if (c) EC_IN(c, p, p);
485 Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(z); Py_XDECREF(t);
490 static int ecptxl(ec_curve *c, ec *p, PyObject *x, PyObject *y, PyObject *z)
493 return (ecptxl_3(c, p, x, y, z));
495 return (ecptxl_2(c, p, x, y));
497 return (ecptxl_1(c, p, x));
500 static PyObject *ecptnc_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
502 PyObject *x = 0, *y = 0, *z = 0;
504 char *kwlist[] = { "x", "y", 0 };
506 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:new", kwlist, &x, &y) ||
507 ecptxl(0, &p, x, y, z))
509 return (ecpt_pywrapout(ty, &p));
515 static PyObject *ecpt_pyint(PyObject *me)
520 if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
522 if (!mp_tolong_checked(p.x, &l, 0)) rc = PyInt_FromLong(l);
523 else rc = mp_topylong(p.x);
529 static PyObject *ecpt_pylong(PyObject *me)
533 if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
535 rc = mp_topylong(p.x);
541 static PyObject *ecpt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
543 PyObject *x = 0, *y = 0, *z = 0;
545 char *kwlist[] = { "x", "y", "z", 0 };
547 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OOO:new", kwlist,
549 ecptxl(ECCURVE_C(ty), &p, x, y, z))
551 return (ecpt_pywrap((PyObject *)ty, &p));
557 static PyGetSetDef ecptnc_pygetset[] = {
558 #define GETSETNAME(op, name) epnc##op##_##name
559 GET (ix, "P.ix -> integer x coordinate of P")
560 GET (iy, "P.iy -> integer y coordinate of P")
561 GET (point, "P.point -> standalone curve point (no-op)")
566 static PyMethodDef ecptnc_pymethods[] = {
567 #define METHNAME(func) epmeth_##func
568 METH (tobuf, "X.tobuf() -> BIN")
573 static PyNumberMethods ecpt_pynumber = {
575 0, /* @nb_subtract@ */
576 0, /* @nb_multiply@ */
578 0, /* @nb_remainder@ */
581 0, /* @nb_negative@ */
582 0, /* @nb_positive@ */
583 0, /* @nb_absolute@ */
584 ecpt_pynonzerop, /* @nb_nonzero@ */
592 ecpt_pyint, /* @nb_int@ */
593 ecpt_pylong, /* @nb_long@ */
598 0, /* @nb_inplace_add@ */
599 0, /* @nb_inplace_subtract@ */
600 0, /* @nb_inplace_multiply@ */
601 0, /* @nb_inplace_divide@ */
602 0, /* @nb_inplace_remainder@ */
603 0, /* @nb_inplace_power@ */
604 0, /* @nb_inplace_lshift@ */
605 0, /* @nb_inplace_rshift@ */
606 0, /* @nb_inplace_and@ */
607 0, /* @nb_inplace_xor@ */
608 0, /* @nb_inplace_or@ */
610 0, /* @nb_floor_divide@ */
611 0, /* @nb_true_divide@ */
612 0, /* @nb_inplace_floor_divide@ */
613 0, /* @nb_inplace_true_divide@ */
616 static PyTypeObject ecpt_pytype_skel = {
617 PyObject_HEAD_INIT(0) 0, /* Header */
618 "ECPt", /* @tp_name@ */
619 sizeof(ecpt_pyobj), /* @tp_basicsize@ */
620 0, /* @tp_itemsize@ */
622 ecpt_pydealloc, /* @tp_dealloc@ */
624 0, /* @tp_getattr@ */
625 0, /* @tp_setattr@ */
626 0, /* @tp_compare@ */
628 &ecpt_pynumber, /* @tp_as_number@ */
629 0, /* @tp_as_sequence@ */
630 0, /* @tp_as_mapping@ */
631 ecpt_pyhash, /* @tp_hash@ */
634 0, /* @tp_getattro@ */
635 0, /* @tp_setattro@ */
636 0, /* @tp_as_buffer@ */
637 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
638 Py_TPFLAGS_CHECKTYPES |
642 "Elliptic curve points, not associated with any curve.",
644 0, /* @tp_traverse@ */
646 ecpt_pyrichcompare, /* @tp_richcompare@ */
647 0, /* @tp_weaklistoffset@ */
649 0, /* @tp_iternext@ */
650 ecptnc_pymethods, /* @tp_methods@ */
651 0, /* @tp_members@ */
652 ecptnc_pygetset, /* @tp_getset@ */
655 0, /* @tp_descr_get@ */
656 0, /* @tp_descr_set@ */
657 0, /* @tp_dictoffset@ */
659 PyType_GenericAlloc, /* @tp_alloc@ */
660 ecptnc_pynew, /* @tp_new@ */
665 static PyGetSetDef ecpt_pygetset[] = {
666 #define GETSETNAME(op, name) ep##op##_##name
667 GET (curve, "P.curve -> elliptic curve containing P")
668 GET (point, "P.point -> standalone curve point")
669 GET (x, "P.x -> Cartesian x coordinate of P")
670 GET (y, "P.y -> Cartesian y coordinate of P")
671 GET (_x, "P._x -> internal x coordinate of P")
672 GET (_y, "P._y -> internal y coordinate of P")
673 GET (_z, "P._z -> internal z coordinate of P, or None")
678 static PyMethodDef ecpt_pymethods[] = {
679 #define METHNAME(func) epmeth_##func
680 METH (toraw, "X.toraw() -> BIN")
681 KWMETH(ec2osp, "X.ec2osp([flags = EC_EXPLY]) -> BIN")
682 METH (dbl, "X.dbl() -> X + X")
683 METH (oncurvep, "X.oncurvep() -> BOOL")
688 static PyNumberMethods ecptcurve_pynumber = {
689 ecpt_pyadd, /* @nb_add@ */
690 ecpt_pysub, /* @nb_subtract@ */
691 ecpt_pymul, /* @nb_multiply@ */
693 0, /* @nb_remainder@ */
696 ecpt_pyneg, /* @nb_negative@ */
697 ecpt_pyid, /* @nb_positive@ */
698 0, /* @nb_absolute@ */
699 0, /* @nb_nonzero@ */
713 0, /* @nb_inplace_add@ */
714 0, /* @nb_inplace_subtract@ */
715 0, /* @nb_inplace_multiply@ */
716 0, /* @nb_inplace_divide@ */
717 0, /* @nb_inplace_remainder@ */
718 0, /* @nb_inplace_power@ */
719 0, /* @nb_inplace_lshift@ */
720 0, /* @nb_inplace_rshift@ */
721 0, /* @nb_inplace_and@ */
722 0, /* @nb_inplace_xor@ */
723 0, /* @nb_inplace_or@ */
725 0, /* @nb_floor_divide@ */
726 0, /* @nb_true_divide@ */
727 0, /* @nb_inplace_floor_divide@ */
728 0, /* @nb_inplace_true_divide@ */
731 static PyTypeObject ecptcurve_pytype_skel = {
732 PyObject_HEAD_INIT(0) 0, /* Header */
733 "ECPtCurve", /* @tp_name@ */
734 sizeof(ecpt_pyobj), /* @tp_basicsize@ */
735 0, /* @tp_itemsize@ */
737 ecpt_pydealloc, /* @tp_dealloc@ */
739 0, /* @tp_getattr@ */
740 0, /* @tp_setattr@ */
741 0, /* @tp_compare@ */
743 &ecptcurve_pynumber, /* @tp_as_number@ */
744 0, /* @tp_as_sequence@ */
745 0, /* @tp_as_mapping@ */
749 0, /* @tp_getattro@ */
750 0, /* @tp_setattro@ */
751 0, /* @tp_as_buffer@ */
752 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
753 Py_TPFLAGS_CHECKTYPES |
757 "Elliptic curve points; abstract base class for points on given curves.",
759 0, /* @tp_traverse@ */
761 0, /* @tp_richcompare@ */
762 0, /* @tp_weaklistoffset@ */
764 0, /* @tp_iternext@ */
765 ecpt_pymethods, /* @tp_methods@ */
766 0, /* @tp_members@ */
767 ecpt_pygetset, /* @tp_getset@ */
770 0, /* @tp_descr_get@ */
771 0, /* @tp_descr_set@ */
772 0, /* @tp_dictoffset@ */
774 PyType_GenericAlloc, /* @tp_alloc@ */
775 abstract_pynew, /* @tp_new@ */
780 /*----- Elliptic curves themselves ----------------------------------------*/
782 static PyObject *eccurve_pyrichcompare(PyObject *x, PyObject *y, int op)
786 assert(ECCURVE_PYCHECK(x));
787 if (!ECCURVE_PYCHECK(y)) RETURN_NOTIMPL;
788 b = ec_samep(ECCURVE_C(x), ECCURVE_C(y));
791 case Py_NE: b = !b; break;
792 default: TYERR("can't order elliptic curves");
799 static PyObject *ecmmul_id(PyObject *me)
800 { ec p = EC_INIT; return (ecpt_pywrap(me, &p)); }
802 static int ecmmul_fill(void *pp, PyObject *me, PyObject *x, PyObject *m)
804 ec_mulfactor *f = pp;
807 if (getecpt(ECCURVE_C(me), &f->base, x) ||
808 (f->exp = getmp(m)) == 0)
813 static PyObject *ecmmul_exp(PyObject *me, void *pp, int n)
816 ec_immul(ECCURVE_C(me), &p, pp, n);
817 return (ecpt_pywrap(me, &p));
820 static void ecmmul_drop(void *pp)
822 ec_mulfactor *f = pp;
823 EC_DESTROY(&f->base);
827 static PyObject *ecmeth_mmul(PyObject *me, PyObject *arg)
829 return (mexp_common(me, arg, sizeof(ec_mulfactor),
830 ecmmul_id, ecmmul_fill, ecmmul_exp, ecmmul_drop));
833 static PyObject *meth__ECPtCurve_fromraw(PyObject *me, PyObject *arg)
842 if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &len))
844 buf_init(&b, p, len);
846 if (ec_getraw(cc, &b, &pp))
849 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
854 static PyObject *meth__ECPtCurve_os2ecp(PyObject *me,
855 PyObject *arg, PyObject *kw)
862 unsigned f = EC_XONLY | EC_LSB | EC_SORT | EC_EXPLY;
864 char *kwlist[] = { "class", "buf", "flags", 0 };
866 if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|O&:os2ecp", kwlist,
867 &me, &p, &len, convuint, &f))
869 buf_init(&b, p, len);
871 if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point");
873 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
878 static PyObject *meth__ECPt_frombuf(PyObject *me, PyObject *arg)
886 if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end;
888 if (buf_getec(&b, &pp)) VALERR("malformed data");
889 rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp),
890 bytestring_pywrapbuf(&b));
895 static PyObject *meth__ECPt_parse(PyObject *me, PyObject *arg)
902 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
905 if (!ec_ptparse(&qd, &pp)) VALERR(qd.e);
906 rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p);
911 static void eccurve_pydealloc(PyObject *me)
913 ec_destroycurve(ECCURVE_C(me));
914 Py_DECREF(ECCURVE_FOBJ(me));
915 PyType_Type.tp_dealloc(me);
918 static PyObject *ecmeth_find(PyObject *me, PyObject *arg)
925 if (!PyArg_ParseTuple(arg, "O:find", &x)) goto end;
926 if (FIELD_PYCHECK(x) && FE_F(x) == ECCURVE_C(me)->f)
927 xx = MP_COPY(FE_X(x));
928 else if ((xx = getmp(x)) == 0)
931 xx = F_IN(ECCURVE_C(me)->f, xx, xx);
932 if (EC_FIND(ECCURVE_C(me), &p, xx) == 0)
933 VALERR("not on the curve");
934 rc = ecpt_pywrap(me, &p);
940 static PyObject *ecmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
942 char *kwlist[] = { "rng", 0 };
943 grand *r = &rand_global;
946 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", kwlist,
949 ec_rand(ECCURVE_C(me), &p, r);
950 EC_IN(ECCURVE_C(me), &p, &p);
951 return (ecpt_pywrap(me, &p));
954 static PyObject *eccurve_dopywrap(PyTypeObject *ty,
955 PyObject *fobj, ec_curve *c)
957 eccurve_pyobj *cobj = newtype(ty, 0, c->ops->name);
960 cobj->ty.ht_type.tp_basicsize = sizeof(ecpt_pyobj);
961 cobj->ty.ht_type.tp_base = ecptcurve_pytype;
962 Py_INCREF(ecptcurve_pytype);
963 cobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
964 Py_TPFLAGS_BASETYPE |
965 Py_TPFLAGS_CHECKTYPES |
966 Py_TPFLAGS_HEAPTYPE);
967 cobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
968 cobj->ty.ht_type.tp_free = 0;
969 cobj->ty.ht_type.tp_new = ecpt_pynew;
970 typeready(&cobj->ty.ht_type);
971 return ((PyObject *)cobj);
974 PyObject *eccurve_pywrap(PyObject *fobj, ec_curve *c)
979 fobj = field_pywrap(c->f);
982 assert(FIELD_F(fobj) == c->f);
983 if (strcmp(EC_NAME(c), "prime") == 0)
984 ty = ecprimecurve_pytype;
985 else if (strcmp(EC_NAME(c), "primeproj") == 0)
986 ty = ecprimeprojcurve_pytype;
987 else if (strcmp(EC_NAME(c), "bin") == 0)
988 ty = ecbincurve_pytype;
989 else if (strcmp(EC_NAME(c), "binproj") == 0)
990 ty = ecbinprojcurve_pytype;
993 return (eccurve_dopywrap(ty, fobj, c));
996 static PyObject *eccurve_pynew(PyTypeObject *ty,
997 ec_curve *(*make)(field *, mp *, mp *),
998 PyObject *arg, PyObject *kw)
1002 char *kwlist[] = { "field", "a", "b", 0 };
1003 mp *aa = 0, *bb = 0;
1005 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&O&", kwlist,
1006 field_pytype, &fobj,
1007 convmp, &aa, convmp, &bb))
1010 cobj = eccurve_dopywrap(ty, fobj, make(FIELD_F(fobj), aa, bb));
1012 if (aa) MP_DROP(aa);
1013 if (bb) MP_DROP(bb);
1017 static PyObject *meth__ECCurve_parse(PyObject *me, PyObject *arg)
1024 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
1028 if ((c = ec_curveparse(&qd)) == 0)
1030 rc = eccurve_pywrap(0, c);
1035 static PyObject *ecget_name(PyObject *me, void *hunoz)
1036 { return (PyString_FromString(EC_NAME(ECCURVE_C(me)))); }
1038 static PyObject *ecget_a(PyObject *me, void *hunoz)
1039 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->a))); }
1041 static PyObject *ecget_b(PyObject *me, void *hunoz)
1042 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->b))); }
1044 static PyObject *ecget_field(PyObject *me, void *hunoz)
1045 { RETURN_OBJ(ECCURVE_FOBJ(me)); }
1047 static PyObject *ecget_inf(PyObject *me, void *hunoz)
1048 { ec inf = EC_INIT; return (ecpt_pywrap(me, &inf)); }
1050 static PyGetSetDef eccurve_pygetset[] = {
1051 #define GETSETNAME(op, name) ec##op##_##name
1052 GET (name, "E.name -> name of this kind of curve")
1053 GET (a, "E.a -> first parameter of curve")
1054 GET (b, "E.b -> second parameter of curve")
1055 GET (field, "E.field -> finite field containing this curve")
1056 GET (inf, "E.inf -> point at infinity of this curve")
1061 static PyMethodDef eccurve_pymethods[] = {
1062 #define METHNAME(name) ecmeth_##name
1064 E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...")
1065 METH (find, "E.find(X) -> P")
1066 KWMETH(rand, "E.rand(rng = rand) ->P")
1071 static PyTypeObject eccurve_pytype_skel = {
1072 PyObject_HEAD_INIT(0) 0, /* Header */
1073 "ECCurve", /* @tp_name@ */
1074 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1075 0, /* @tp_itemsize@ */
1077 eccurve_pydealloc, /* @tp_dealloc@ */
1079 0, /* @tp_getattr@ */
1080 0, /* @tp_setattr@ */
1081 0, /* @tp_compare@ */
1083 0, /* @tp_as_number@ */
1084 0, /* @tp_as_sequence@ */
1085 0, /* @tp_as_mapping@ */
1089 0, /* @tp_getattro@ */
1090 0, /* @tp_setattro@ */
1091 0, /* @tp_as_buffer@ */
1092 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1093 Py_TPFLAGS_BASETYPE,
1096 "An elliptic curve. Abstract class.",
1098 0, /* @tp_traverse@ */
1100 eccurve_pyrichcompare, /* @tp_richcompare@ */
1101 0, /* @tp_weaklistoffset@ */
1103 0, /* @tp_iternext@ */
1104 eccurve_pymethods, /* @tp_methods@ */
1105 0, /* @tp_members@ */
1106 eccurve_pygetset, /* @tp_getset@ */
1109 0, /* @tp_descr_get@ */
1110 0, /* @tp_descr_set@ */
1111 0, /* @tp_dictoffset@ */
1113 PyType_GenericAlloc, /* @tp_alloc@ */
1114 abstract_pynew, /* @tp_new@ */
1119 static PyObject *ecprimecurve_pynew(PyTypeObject *ty,
1120 PyObject *arg, PyObject *kw)
1122 return (eccurve_pynew(ty, ec_prime, arg, kw));
1125 static PyTypeObject ecprimecurve_pytype_skel = {
1126 PyObject_HEAD_INIT(0) 0, /* Header */
1127 "ECPrimeCurve", /* @tp_name@ */
1128 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1129 0, /* @tp_itemsize@ */
1131 eccurve_pydealloc, /* @tp_dealloc@ */
1133 0, /* @tp_getattr@ */
1134 0, /* @tp_setattr@ */
1135 0, /* @tp_compare@ */
1137 0, /* @tp_as_number@ */
1138 0, /* @tp_as_sequence@ */
1139 0, /* @tp_as_mapping@ */
1143 0, /* @tp_getattro@ */
1144 0, /* @tp_setattro@ */
1145 0, /* @tp_as_buffer@ */
1146 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1147 Py_TPFLAGS_BASETYPE,
1150 "An elliptic curve over a prime field. Use ecprimeprojcurve.",
1152 0, /* @tp_traverse@ */
1154 eccurve_pyrichcompare, /* @tp_richcompare@ */
1155 0, /* @tp_weaklistoffset@ */
1157 0, /* @tp_iternext@ */
1158 0, /* @tp_methods@ */
1159 0, /* @tp_members@ */
1160 0, /* @tp_getset@ */
1163 0, /* @tp_descr_get@ */
1164 0, /* @tp_descr_set@ */
1165 0, /* @tp_dictoffset@ */
1167 PyType_GenericAlloc, /* @tp_alloc@ */
1168 ecprimecurve_pynew, /* @tp_new@ */
1173 static PyObject *ecprimeprojcurve_pynew(PyTypeObject *ty,
1174 PyObject *arg, PyObject *kw)
1176 return (eccurve_pynew(ty, ec_primeproj, arg, kw));
1179 static PyTypeObject ecprimeprojcurve_pytype_skel = {
1180 PyObject_HEAD_INIT(0) 0, /* Header */
1181 "ECPrimeProjCurve", /* @tp_name@ */
1182 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1183 0, /* @tp_itemsize@ */
1185 eccurve_pydealloc, /* @tp_dealloc@ */
1187 0, /* @tp_getattr@ */
1188 0, /* @tp_setattr@ */
1189 0, /* @tp_compare@ */
1191 0, /* @tp_as_number@ */
1192 0, /* @tp_as_sequence@ */
1193 0, /* @tp_as_mapping@ */
1197 0, /* @tp_getattro@ */
1198 0, /* @tp_setattro@ */
1199 0, /* @tp_as_buffer@ */
1200 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1201 Py_TPFLAGS_BASETYPE,
1204 "An elliptic curve over a prime field, using projective coordinates.",
1206 0, /* @tp_traverse@ */
1208 eccurve_pyrichcompare, /* @tp_richcompare@ */
1209 0, /* @tp_weaklistoffset@ */
1211 0, /* @tp_iternext@ */
1212 0, /* @tp_methods@ */
1213 0, /* @tp_members@ */
1214 0, /* @tp_getset@ */
1217 0, /* @tp_descr_get@ */
1218 0, /* @tp_descr_set@ */
1219 0, /* @tp_dictoffset@ */
1221 PyType_GenericAlloc, /* @tp_alloc@ */
1222 ecprimeprojcurve_pynew, /* @tp_new@ */
1227 static PyObject *ecbincurve_pynew(PyTypeObject *ty,
1228 PyObject *arg, PyObject *kw)
1230 return (eccurve_pynew(ty, ec_bin, arg, kw));
1233 static PyTypeObject ecbincurve_pytype_skel = {
1234 PyObject_HEAD_INIT(0) 0, /* Header */
1235 "ECBinCurve", /* @tp_name@ */
1236 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1237 0, /* @tp_itemsize@ */
1239 eccurve_pydealloc, /* @tp_dealloc@ */
1241 0, /* @tp_getattr@ */
1242 0, /* @tp_setattr@ */
1243 0, /* @tp_compare@ */
1245 0, /* @tp_as_number@ */
1246 0, /* @tp_as_sequence@ */
1247 0, /* @tp_as_mapping@ */
1251 0, /* @tp_getattro@ */
1252 0, /* @tp_setattro@ */
1253 0, /* @tp_as_buffer@ */
1254 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1255 Py_TPFLAGS_BASETYPE,
1258 "An elliptic curve over a binary field. Use ecbinprojcurve.",
1260 0, /* @tp_traverse@ */
1262 eccurve_pyrichcompare, /* @tp_richcompare@ */
1263 0, /* @tp_weaklistoffset@ */
1265 0, /* @tp_iternext@ */
1266 0, /* @tp_methods@ */
1267 0, /* @tp_members@ */
1268 0, /* @tp_getset@ */
1271 0, /* @tp_descr_get@ */
1272 0, /* @tp_descr_set@ */
1273 0, /* @tp_dictoffset@ */
1275 PyType_GenericAlloc, /* @tp_alloc@ */
1276 ecbincurve_pynew, /* @tp_new@ */
1281 static PyObject *ecbinprojcurve_pynew(PyTypeObject *ty,
1282 PyObject *arg, PyObject *kw)
1284 return (eccurve_pynew(ty, ec_binproj, arg, kw));
1287 static PyTypeObject ecbinprojcurve_pytype_skel = {
1288 PyObject_HEAD_INIT(0) 0, /* Header */
1289 "ECBinProjCurve", /* @tp_name@ */
1290 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1291 0, /* @tp_itemsize@ */
1293 eccurve_pydealloc, /* @tp_dealloc@ */
1295 0, /* @tp_getattr@ */
1296 0, /* @tp_setattr@ */
1297 0, /* @tp_compare@ */
1299 0, /* @tp_as_number@ */
1300 0, /* @tp_as_sequence@ */
1301 0, /* @tp_as_mapping@ */
1305 0, /* @tp_getattro@ */
1306 0, /* @tp_setattro@ */
1307 0, /* @tp_as_buffer@ */
1308 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1309 Py_TPFLAGS_BASETYPE,
1312 "An elliptic curve over a binary field, using projective coordinates.",
1314 0, /* @tp_traverse@ */
1316 eccurve_pyrichcompare, /* @tp_richcompare@ */
1317 0, /* @tp_weaklistoffset@ */
1319 0, /* @tp_iternext@ */
1320 0, /* @tp_methods@ */
1321 0, /* @tp_members@ */
1322 0, /* @tp_getset@ */
1325 0, /* @tp_descr_get@ */
1326 0, /* @tp_descr_set@ */
1327 0, /* @tp_dictoffset@ */
1329 PyType_GenericAlloc, /* @tp_alloc@ */
1330 ecbinprojcurve_pynew, /* @tp_new@ */
1335 /*----- Curve info --------------------------------------------------------*/
1337 static int ncurves = -1;
1339 void ecinfo_copy(ec_info *eic, const ec_info *ei)
1341 eic->c = eccurve_copy(ei->c);
1343 EC_COPY(&eic->g, &ei->g);
1344 eic->r = MP_COPY(ei->r);
1345 eic->h = MP_COPY(ei->h);
1348 PyObject *ecinfo_pywrap(ec_info *ei)
1352 o = PyObject_NEW(ecinfo_pyobj, ecinfo_pytype);
1354 o->cobj = eccurve_pywrap(0, o->ei.c);
1356 return ((PyObject *)o);
1359 static void ecinfo_pydealloc(PyObject *me)
1361 ec_info *ei = ECINFO_EI(me);
1365 Py_DECREF(ECINFO_COBJ(me));
1369 static PyObject *ecinfo_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1373 char *kwlist[] = { "curve", "G", "r", "h", 0 };
1374 ecinfo_pyobj *rc = 0;
1376 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!O&O&:new", kwlist,
1377 eccurve_pytype, &e, ecpt_pytype, &g,
1378 convmp, &ei.r, convmp, &ei.h))
1380 if (ECPT_C(g) != ECCURVE_C(e) && !ec_samep(ECPT_C(g), ECCURVE_C(e)))
1381 TYERR("point not from this curve");
1382 ei.c = ECCURVE_C(e);
1384 EC_OUT(ei.c, &ei.g, ECPT_P(g));
1385 rc = (ecinfo_pyobj *)ty->tp_alloc(ty, 0);
1388 Py_INCREF(rc->cobj);
1389 return ((PyObject *)rc);
1397 static PyObject *meth__ECInfo_parse(PyObject *me, PyObject *arg)
1404 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
1408 if (ec_infoparse(&qd, &ei))
1410 rc = Py_BuildValue("(Ns)", ecinfo_pywrap(&ei), qd.p);
1415 static PyObject *meth__ECInfo__curven(PyObject *me, PyObject *arg)
1421 if (!PyArg_ParseTuple(arg, "Oi:_curven", &me, &i)) goto end;
1422 if (i < 0 || i >= ncurves) VALERR("curve index out of range");
1423 ec_infofromdata(&ei, ectab[i].data);
1424 rc = ecinfo_pywrap(&ei);
1429 static PyObject *ecinfo_pyrichcompare(PyObject *x, PyObject *y, int op)
1431 int b = ec_sameinfop(ECINFO_EI(x), ECINFO_EI(y));
1435 default: TYERR("can't order elliptic curve infos");
1437 return (getbool(b));
1442 static PyObject *eimeth_check(PyObject *me, PyObject *arg, PyObject *kw)
1444 char *kwlist[] = { "rng", 0 };
1445 grand *r = &rand_global;
1448 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:check", kwlist,
1451 if ((p = ec_checkinfo(ECINFO_EI(me), r)) != 0)
1458 static PyObject *eiget_curve(PyObject *me, void *hunoz)
1459 { RETURN_OBJ(ECINFO_COBJ(me)); }
1461 static PyObject *eiget_G(PyObject *me, void *hunoz)
1463 ec_info *ei = ECINFO_EI(me);
1465 EC_IN(ei->c, &p, &ei->g);
1466 return (ecpt_pywrap(ECINFO_COBJ(me), &p));
1469 static PyObject *eiget_r(PyObject *me, void *hunoz)
1470 { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->r))); }
1472 static PyObject *eiget_h(PyObject *me, void *hunoz)
1473 { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->h))); }
1475 static PyGetSetDef ecinfo_pygetset[] = {
1476 #define GETSETNAME(op, name) ei##op##_##name
1477 GET (curve, "I.curve -> the elliptic curve")
1478 GET (G, "I.G -> generator point for the group")
1479 GET (r, "I.r -> order of the group (and hence of G")
1480 GET (h, "I.h -> cofactor of the group")
1485 static PyMethodDef ecinfo_pymethods[] = {
1486 #define METHNAME(name) eimeth_##name
1487 KWMETH(check, "I.check() -> None")
1492 static PyTypeObject ecinfo_pytype_skel = {
1493 PyObject_HEAD_INIT(0) 0, /* Header */
1494 "ECInfo", /* @tp_name@ */
1495 sizeof(ecinfo_pyobj), /* @tp_basicsize@ */
1496 0, /* @tp_itemsize@ */
1498 ecinfo_pydealloc, /* @tp_dealloc@ */
1500 0, /* @tp_getattr@ */
1501 0, /* @tp_setattr@ */
1502 0, /* @tp_compare@ */
1504 0, /* @tp_as_number@ */
1505 0, /* @tp_as_sequence@ */
1506 0, /* @tp_as_mapping@ */
1510 0, /* @tp_getattro@ */
1511 0, /* @tp_setattro@ */
1512 0, /* @tp_as_buffer@ */
1513 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1514 Py_TPFLAGS_BASETYPE,
1517 "Elliptic curve domain parameters.",
1519 0, /* @tp_traverse@ */
1521 ecinfo_pyrichcompare, /* @tp_richcompare@ */
1522 0, /* @tp_weaklistoffset@ */
1524 0, /* @tp_iternext@ */
1525 ecinfo_pymethods, /* @tp_methods@ */
1526 0, /* @tp_members@ */
1527 ecinfo_pygetset, /* @tp_getset@ */
1530 0, /* @tp_descr_get@ */
1531 0, /* @tp_descr_set@ */
1532 0, /* @tp_dictoffset@ */
1534 PyType_GenericAlloc, /* @tp_alloc@ */
1535 ecinfo_pynew, /* @tp_new@ */
1540 /*----- Setup -------------------------------------------------------------*/
1542 static PyMethodDef methods[] = {
1543 #define METHNAME(func) meth_##func
1544 METH (_ECPt_frombuf, "frombuf(E, STR) -> (P, REST)")
1545 METH (_ECPtCurve_fromraw, "fromraw(E, STR) -> (P, REST)")
1546 KWMETH(_ECPtCurve_os2ecp, "os2ecp(E, STR, [flags = ...]) -> (P, REST)")
1547 METH (_ECPt_parse, "parse(E, STR) -> (P, REST)")
1548 METH (_ECCurve_parse, "parse(STR) -> (E, REST)")
1549 METH (_ECInfo_parse, "parse(STR) -> (I, REST)")
1550 METH (_ECInfo__curven, "_curven(N) -> I")
1555 void ec_pyinit(void)
1557 INITTYPE(ecpt, root);
1558 INITTYPE(ecptcurve, ecpt);
1559 INITTYPE(eccurve, type);
1560 INITTYPE(ecprimecurve, eccurve);
1561 INITTYPE(ecprimeprojcurve, ecprimecurve);
1562 INITTYPE(ecbincurve, eccurve);
1563 INITTYPE(ecbinprojcurve, ecbincurve);
1564 INITTYPE(ecinfo, root);
1565 addmethods(methods);
1568 static PyObject *namedcurves(void)
1575 for (i = 0; ectab[i].name; i++) {
1577 for (j = 0; j < i; j++) {
1578 if (ectab[i].data == ectab[j].data) {
1579 c = PyDict_GetItemString(d, (/*unconst*/ char *)ectab[j].name);
1584 c = PyInt_FromLong(i);
1586 PyDict_SetItemString(d, (/*unconst*/ char *)p, c);
1593 void ec_pyinsert(PyObject *mod)
1595 INSERT("ECPt", ecpt_pytype);
1596 INSERT("ECPtCurve", ecptcurve_pytype);
1597 INSERT("ECCurve", eccurve_pytype);
1598 INSERT("ECPrimeCurve", ecprimecurve_pytype);
1599 INSERT("ECPrimeProjCurve", ecprimeprojcurve_pytype);
1600 INSERT("ECBinCurve", ecbincurve_pytype);
1601 INSERT("ECBinProjCurve", ecbinprojcurve_pytype);
1602 INSERT("ECInfo", ecinfo_pytype);
1603 INSERT("_eccurves", namedcurves());
1606 /*----- That's all, folks -------------------------------------------------*/