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);
191 return (ecpt_pywrap(ECPT_COBJ(y), &zz));
194 static long ecpt_pyhash(PyObject *me)
199 size_t sz = 2*ECPT_C(me)->f->noctets + 1;
200 octet *q = xmalloc(sz);
202 h = 0xe0fdd039 + ECPT_C(me)->f->ops->ty;
204 EC_OUT(ECPT_C(me), &p, ECPT_P(me));
205 ec_putraw(ECPT_C(me), &b, &p);
208 h = unihash_hash(&unihash_global, h, BBASE(&b), BLEN(&b));
209 return (h % LONG_MAX);
212 static PyObject *ecpt_pyrichcompare(PyObject *x, PyObject *y, int op)
216 ec p = EC_INIT, q = EC_INIT;
220 if (ecbinop(x, y, &c, &cobj, &p, &q)) RETURN_NOTIMPL;
224 case Py_EQ: b = EC_EQ(&p, &q); break;
225 case Py_NE: b = !EC_EQ(&p, &q); break;
226 default: TYERR("elliptic curve points are unordered");
235 static PyObject *epmeth_oncurvep(PyObject *me, PyObject *arg)
237 if (!PyArg_ParseTuple(arg, ":oncurvep")) return (0);
238 return (getbool(EC_ATINF(ECPT_P(me)) ||
239 !EC_CHECK(ECPT_C(me), ECPT_P(me))));
242 static PyObject *epmeth_dbl(PyObject *me, PyObject *arg)
245 if (!PyArg_ParseTuple(arg, ":dbl")) return (0);
246 EC_DBL(ECPT_C(me), &p, ECPT_P(me));
247 return (ecpt_pywrap(ECPT_COBJ(me), &p));
250 static PyObject *epmeth_tobuf(PyObject *me, PyObject *arg)
257 if (!PyArg_ParseTuple(arg, ":tobuf")) return (0);
262 n = mp_octets(p.x) + mp_octets(p.y) + 4;
263 rc = bytestring_pywrap(0, n);
264 buf_init(&b, PyString_AS_STRING(rc), n);
267 _PyString_Resize(&rc, BLEN(&b));
272 static PyObject *epmeth_toraw(PyObject *me, PyObject *arg)
277 ec_curve *c = ECPT_C(me);
281 if (!PyArg_ParseTuple(arg, ":toraw")) return (0);
282 len = c->f->noctets * 2 + 1;
283 rc = bytestring_pywrap(0, len);
284 p = PyString_AS_STRING(rc);
285 buf_init(&b, p, len);
286 EC_OUT(c, &pp, ECPT_P(me));
287 ec_putraw(c, &b, &pp);
289 _PyString_Resize(&rc, BLEN(&b));
293 static PyObject *epget_curve(PyObject *me, void *hunoz)
294 { RETURN_OBJ(ECPT_COBJ(me)); }
296 static PyObject *epncget_ix(PyObject *me, void *hunoz)
300 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
302 rc = mp_pywrap(MP_COPY(p.x));
307 static PyObject *epncget_iy(PyObject *me, void *hunoz)
311 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
313 rc = mp_pywrap(MP_COPY(p.y));
318 static PyObject *epncget_point(PyObject *me, void *hunoz)
321 static PyObject *epget_point(PyObject *me, void *hunoz)
325 return (ecpt_pywrapout(ecpt_pytype, &p));
328 static PyObject *epget_x(PyObject *me, void *hunoz)
330 ec_curve *c = ECPT_C(me);
332 PyObject *fobj = ECPT_FOBJ(me);
336 if (EC_ATINF(pp)) RETURN_NONE;
338 rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.x));
343 static PyObject *epget_y(PyObject *me, void *hunoz)
345 ec_curve *c = ECPT_C(me);
347 PyObject *fobj = ECPT_FOBJ(me);
351 if (EC_ATINF(pp)) RETURN_NONE;
353 rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.y));
358 static PyObject *epget__x(PyObject *me, void *hunoz)
360 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
361 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->x)));
364 static PyObject *epget__y(PyObject *me, void *hunoz)
366 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
367 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->y)));
370 static PyObject *epget__z(PyObject *me, void *hunoz)
372 if (EC_ATINF(ECPT_P(me)) || !ECPT_P(me)->z) RETURN_NONE;
373 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->z)));
376 static mp *coord_in(field *f, PyObject *x)
379 if (FE_PYCHECK(x) && FE_F(x) == f)
380 return (MP_COPY(FE_X(x)));
381 else if ((xx = getmp(x)) == 0)
384 return (F_IN(f, xx, xx));
387 static int ecptxl_3(ec_curve *c, ec *p,
388 PyObject *x, PyObject *y, PyObject *z)
392 if (!x || !y || !z) TYERR("missing argument");
393 if (!c) VALERR("internal form with no curve!");
394 if ((p->x == coord_in(c->f, x)) == 0 ||
395 (p->y == coord_in(c->f, y)) == 0 ||
396 (z != Py_None && (p->z = coord_in(c->f, z))) == 0)
398 if (!p->z) p->z = MP_COPY(c->f->one); /* just in case */
404 static int ecptxl_2(ec_curve *c, ec *p, PyObject *x, PyObject *y)
408 if (!x || !y) TYERR("missing argument");
409 if ((p->x = getmp(x)) == 0 ||
410 (p->y = getmp(y)) == 0)
412 if (c) EC_IN(c, p, p);
418 static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
421 PyObject *y = 0, *z = 0, *t = 0;
428 if (!x || x == Py_None)
430 else if (ECPT_PYCHECK(x)) {
433 } else if (PyString_Check(x)) {
434 if (PyObject_AsReadBuffer(x, &q, 0))
438 if (!ec_ptparse(&qd, p))
441 } else if (c && (xx = tomp(x)) != 0) {
442 xx = F_IN(c->f, xx, xx);
443 if (!EC_FIND(c, p, xx)) VALERR("not on the curve");
444 } else if (PySequence_Check(x)) {
446 n = PySequence_Size(t);
447 if (n != 2 && (n != 3 || !c))
448 TYERR("want sequence of two or three items");
449 if ((x = PySequence_GetItem(t, 0)) == 0 ||
450 (y = PySequence_GetItem(t, 1)) == 0 ||
451 (n == 3 && (z = PySequence_GetItem(t, 2)) == 0))
453 rc = (n == 2) ? ecptxl_2(c, p, x, y) : ecptxl_3(c, p, x, y, z);
455 TYERR("can't convert to curve point");
459 if (c) EC_IN(c, p, p);
463 Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(z); Py_XDECREF(t);
468 static int ecptxl(ec_curve *c, ec *p, PyObject *x, PyObject *y, PyObject *z)
471 return (ecptxl_3(c, p, x, y, z));
473 return (ecptxl_2(c, p, x, y));
475 return (ecptxl_1(c, p, x));
478 static PyObject *ecptnc_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
480 PyObject *x = 0, *y = 0, *z = 0;
482 char *kwlist[] = { "x", "y", 0 };
484 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:new", kwlist, &x, &y) ||
485 ecptxl(0, &p, x, y, z))
487 return (ecpt_pywrapout(ty, &p));
493 static PyObject *ecpt_pyint(PyObject *me)
498 if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
500 if (mp_tolong_checked(p.x, &l)) goto end;
501 rc = PyInt_FromLong(l);
507 static PyObject *ecpt_pylong(PyObject *me)
511 if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
513 rc = mp_topylong(p.x);
519 static PyObject *ecpt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
521 PyObject *x = 0, *y = 0, *z = 0;
523 char *kwlist[] = { "x", "y", "z", 0 };
525 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OOO:new", kwlist,
527 ecptxl(ECCURVE_C(ty), &p, x, y, z))
529 return (ecpt_pywrap((PyObject *)ty, &p));
535 static PyGetSetDef ecptnc_pygetset[] = {
536 #define GETSETNAME(op, name) epnc##op##_##name
537 GET (ix, "P.ix -> integer x coordinate of P")
538 GET (iy, "P.iy -> integer y coordinate of P")
539 GET (point, "P.point -> standalone curve point (no-op)")
544 static PyMethodDef ecptnc_pymethods[] = {
545 #define METHNAME(func) epmeth_##func
546 METH (tobuf, "X.tobuf() -> BIN")
551 static PyNumberMethods ecpt_pynumber = {
553 0, /* @nb_subtract@ */
554 0, /* @nb_multiply@ */
556 0, /* @nb_remainder@ */
559 0, /* @nb_negative@ */
560 0, /* @nb_positive@ */
561 0, /* @nb_absolute@ */
562 ecpt_pynonzerop, /* @nb_nonzero@ */
570 ecpt_pyint, /* @nb_int@ */
571 ecpt_pylong, /* @nb_long@ */
576 0, /* @nb_inplace_add@ */
577 0, /* @nb_inplace_subtract@ */
578 0, /* @nb_inplace_multiply@ */
579 0, /* @nb_inplace_divide@ */
580 0, /* @nb_inplace_remainder@ */
581 0, /* @nb_inplace_power@ */
582 0, /* @nb_inplace_lshift@ */
583 0, /* @nb_inplace_rshift@ */
584 0, /* @nb_inplace_and@ */
585 0, /* @nb_inplace_xor@ */
586 0, /* @nb_inplace_or@ */
588 0, /* @nb_floor_divide@ */
589 0, /* @nb_true_divide@ */
590 0, /* @nb_inplace_floor_divide@ */
591 0, /* @nb_inplace_true_divide@ */
594 static PyTypeObject ecpt_pytype_skel = {
595 PyObject_HEAD_INIT(0) 0, /* Header */
596 "ECPt", /* @tp_name@ */
597 sizeof(ecpt_pyobj), /* @tp_basicsize@ */
598 0, /* @tp_itemsize@ */
600 ecpt_pydealloc, /* @tp_dealloc@ */
602 0, /* @tp_getattr@ */
603 0, /* @tp_setattr@ */
604 0, /* @tp_compare@ */
606 &ecpt_pynumber, /* @tp_as_number@ */
607 0, /* @tp_as_sequence@ */
608 0, /* @tp_as_mapping@ */
609 ecpt_pyhash, /* @tp_hash@ */
612 0, /* @tp_getattro@ */
613 0, /* @tp_setattro@ */
614 0, /* @tp_as_buffer@ */
615 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
616 Py_TPFLAGS_CHECKTYPES |
620 "Elliptic curve points, not associated with any curve.",
622 0, /* @tp_traverse@ */
624 ecpt_pyrichcompare, /* @tp_richcompare@ */
625 0, /* @tp_weaklistoffset@ */
627 0, /* @tp_iternext@ */
628 ecptnc_pymethods, /* @tp_methods@ */
629 0, /* @tp_members@ */
630 ecptnc_pygetset, /* @tp_getset@ */
633 0, /* @tp_descr_get@ */
634 0, /* @tp_descr_set@ */
635 0, /* @tp_dictoffset@ */
637 PyType_GenericAlloc, /* @tp_alloc@ */
638 ecptnc_pynew, /* @tp_new@ */
643 static PyGetSetDef ecpt_pygetset[] = {
644 #define GETSETNAME(op, name) ep##op##_##name
645 GET (curve, "P.curve -> elliptic curve containing P")
646 GET (point, "P.point -> standalone curve point")
647 GET (x, "P.x -> Cartesian x coordinate of P")
648 GET (y, "P.y -> Cartesian y coordinate of P")
649 GET (_x, "P._x -> internal x coordinate of P")
650 GET (_y, "P._y -> internal y coordinate of P")
651 GET (_z, "P._z -> internal z coordinate of P, or None")
656 static PyMethodDef ecpt_pymethods[] = {
657 #define METHNAME(func) epmeth_##func
658 METH (toraw, "X.toraw() -> BIN")
659 METH (dbl, "X.dbl() -> X + X")
660 METH (oncurvep, "X.oncurvep() -> BOOL")
665 static PyNumberMethods ecptcurve_pynumber = {
666 ecpt_pyadd, /* @nb_add@ */
667 ecpt_pysub, /* @nb_subtract@ */
668 ecpt_pymul, /* @nb_multiply@ */
670 0, /* @nb_remainder@ */
673 ecpt_pyneg, /* @nb_negative@ */
674 ecpt_pyid, /* @nb_positive@ */
675 0, /* @nb_absolute@ */
676 0, /* @nb_nonzero@ */
690 0, /* @nb_inplace_add@ */
691 0, /* @nb_inplace_subtract@ */
692 0, /* @nb_inplace_multiply@ */
693 0, /* @nb_inplace_divide@ */
694 0, /* @nb_inplace_remainder@ */
695 0, /* @nb_inplace_power@ */
696 0, /* @nb_inplace_lshift@ */
697 0, /* @nb_inplace_rshift@ */
698 0, /* @nb_inplace_and@ */
699 0, /* @nb_inplace_xor@ */
700 0, /* @nb_inplace_or@ */
702 0, /* @nb_floor_divide@ */
703 0, /* @nb_true_divide@ */
704 0, /* @nb_inplace_floor_divide@ */
705 0, /* @nb_inplace_true_divide@ */
708 static PyTypeObject ecptcurve_pytype_skel = {
709 PyObject_HEAD_INIT(0) 0, /* Header */
710 "ECPtCurve", /* @tp_name@ */
711 sizeof(ecpt_pyobj), /* @tp_basicsize@ */
712 0, /* @tp_itemsize@ */
714 ecpt_pydealloc, /* @tp_dealloc@ */
716 0, /* @tp_getattr@ */
717 0, /* @tp_setattr@ */
718 0, /* @tp_compare@ */
720 &ecptcurve_pynumber, /* @tp_as_number@ */
721 0, /* @tp_as_sequence@ */
722 0, /* @tp_as_mapping@ */
726 0, /* @tp_getattro@ */
727 0, /* @tp_setattro@ */
728 0, /* @tp_as_buffer@ */
729 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
730 Py_TPFLAGS_CHECKTYPES |
734 "Elliptic curve points; abstract base class for points on given curves.",
736 0, /* @tp_traverse@ */
738 0, /* @tp_richcompare@ */
739 0, /* @tp_weaklistoffset@ */
741 0, /* @tp_iternext@ */
742 ecpt_pymethods, /* @tp_methods@ */
743 0, /* @tp_members@ */
744 ecpt_pygetset, /* @tp_getset@ */
747 0, /* @tp_descr_get@ */
748 0, /* @tp_descr_set@ */
749 0, /* @tp_dictoffset@ */
751 PyType_GenericAlloc, /* @tp_alloc@ */
752 abstract_pynew, /* @tp_new@ */
757 /*----- Elliptic curves themselves ----------------------------------------*/
759 static PyObject *eccurve_pyrichcompare(PyObject *x, PyObject *y, int op)
761 int b = ec_samep(ECCURVE_C(x), ECCURVE_C(y));
765 default: TYERR("can't order elliptic curves");
772 static PyObject *ecmmul_id(PyObject *me)
773 { ec p = EC_INIT; return (ecpt_pywrap(me, &p)); }
775 static int ecmmul_fill(void *pp, PyObject *me, PyObject *x, PyObject *m)
777 ec_mulfactor *f = pp;
780 if (getecpt(ECCURVE_C(me), &f->base, x) ||
781 (f->exp = getmp(m)) == 0)
786 static PyObject *ecmmul_exp(PyObject *me, void *pp, int n)
789 ec_immul(ECCURVE_C(me), &p, pp, n);
790 return (ecpt_pywrap(me, &p));
793 static void ecmmul_drop(void *pp)
795 ec_mulfactor *f = pp;
796 EC_DESTROY(&f->base);
800 static PyObject *ecmeth_mmul(PyObject *me, PyObject *arg)
802 return (mexp_common(me, arg, sizeof(ec_mulfactor),
803 ecmmul_id, ecmmul_fill, ecmmul_exp, ecmmul_drop));
806 static PyObject *meth__ECPtCurve_fromraw(PyObject *me, PyObject *arg)
815 if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &len))
817 buf_init(&b, p, len);
819 if (ec_getraw(cc, &b, &pp))
822 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
827 static PyObject *meth__ECPt_frombuf(PyObject *me, PyObject *arg)
835 if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end;
837 if (buf_getec(&b, &pp)) VALERR("malformed data");
838 rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp),
839 bytestring_pywrapbuf(&b));
844 static PyObject *meth__ECPt_parse(PyObject *me, PyObject *arg)
851 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
854 if (!ec_ptparse(&qd, &pp)) VALERR(qd.e);
855 rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p);
860 static void eccurve_pydealloc(PyObject *me)
862 ec_destroycurve(ECCURVE_C(me));
863 Py_DECREF(ECCURVE_FOBJ(me));
864 PyType_Type.tp_dealloc(me);
867 static PyObject *ecmeth_find(PyObject *me, PyObject *arg)
874 if (!PyArg_ParseTuple(arg, "O:find", &x)) goto end;
875 if (FIELD_PYCHECK(x) && FE_F(x) == ECCURVE_C(me)->f)
876 xx = MP_COPY(FE_X(x));
877 else if ((xx = getmp(x)) == 0)
880 xx = F_IN(ECCURVE_C(me)->f, xx, xx);
881 if (EC_FIND(ECCURVE_C(me), &p, xx) == 0)
882 VALERR("not on the curve");
883 rc = ecpt_pywrap(me, &p);
889 static PyObject *ecmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
891 char *kwlist[] = { "rng", 0 };
892 grand *r = &rand_global;
895 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", kwlist,
898 ec_rand(ECCURVE_C(me), &p, r);
899 EC_IN(ECCURVE_C(me), &p, &p);
900 return (ecpt_pywrap(me, &p));
903 static PyObject *eccurve_dopywrap(PyTypeObject *ty,
904 PyObject *fobj, ec_curve *c)
906 eccurve_pyobj *cobj = newtype(ty, 0, c->ops->name);
909 cobj->ty.ht_type.tp_basicsize = sizeof(ecpt_pyobj);
910 cobj->ty.ht_type.tp_base = ecptcurve_pytype;
911 Py_INCREF(ecptcurve_pytype);
912 cobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
913 Py_TPFLAGS_BASETYPE |
914 Py_TPFLAGS_CHECKTYPES |
915 Py_TPFLAGS_HEAPTYPE);
916 cobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
917 cobj->ty.ht_type.tp_free = 0;
918 cobj->ty.ht_type.tp_new = ecpt_pynew;
919 typeready(&cobj->ty.ht_type);
920 return ((PyObject *)cobj);
923 PyObject *eccurve_pywrap(PyObject *fobj, ec_curve *c)
928 fobj = field_pywrap(c->f);
931 assert(FIELD_F(fobj) == c->f);
932 if (strcmp(EC_NAME(c), "prime") == 0)
933 ty = ecprimecurve_pytype;
934 else if (strcmp(EC_NAME(c), "primeproj") == 0)
935 ty = ecprimeprojcurve_pytype;
936 else if (strcmp(EC_NAME(c), "bin") == 0)
937 ty = ecbincurve_pytype;
938 else if (strcmp(EC_NAME(c), "binproj") == 0)
939 ty = ecbinprojcurve_pytype;
942 return (eccurve_dopywrap(ty, fobj, c));
945 static PyObject *eccurve_pynew(PyTypeObject *ty,
946 ec_curve *(*make)(field *, mp *, mp *),
947 PyObject *arg, PyObject *kw)
951 char *kwlist[] = { "field", "a", "b", 0 };
954 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&O&", kwlist,
956 convmp, &aa, convmp, &bb))
959 cobj = eccurve_dopywrap(ty, fobj, make(FIELD_F(fobj), aa, bb));
966 static PyObject *meth__ECCurve_parse(PyObject *me, PyObject *arg)
973 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
977 if ((c = ec_curveparse(&qd)) == 0)
979 rc = eccurve_pywrap(0, c);
984 static PyObject *ecget_name(PyObject *me, void *hunoz)
985 { return (PyString_FromString(EC_NAME(ECCURVE_C(me)))); }
987 static PyObject *ecget_a(PyObject *me, void *hunoz)
988 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->a))); }
990 static PyObject *ecget_b(PyObject *me, void *hunoz)
991 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->b))); }
993 static PyObject *ecget_field(PyObject *me, void *hunoz)
994 { RETURN_OBJ(ECCURVE_FOBJ(me)); }
996 static PyObject *ecget_inf(PyObject *me, void *hunoz)
997 { ec inf = EC_INIT; return (ecpt_pywrap(me, &inf)); }
999 static PyGetSetDef eccurve_pygetset[] = {
1000 #define GETSETNAME(op, name) ec##op##_##name
1001 GET (name, "E.name -> name of this kind of curve")
1002 GET (a, "E.a -> first parameter of curve")
1003 GET (b, "E.b -> second parameter of curve")
1004 GET (field, "E.field -> finite field containing this curve")
1005 GET (inf, "E.inf -> point at infinity of this curve")
1010 static PyMethodDef eccurve_pymethods[] = {
1011 #define METHNAME(name) ecmeth_##name
1013 E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...")
1014 METH (find, "E.find(X) -> P")
1015 KWMETH(rand, "E.rand(rng = rand) ->P")
1020 static PyTypeObject eccurve_pytype_skel = {
1021 PyObject_HEAD_INIT(0) 0, /* Header */
1022 "ECCurve", /* @tp_name@ */
1023 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1024 0, /* @tp_itemsize@ */
1026 eccurve_pydealloc, /* @tp_dealloc@ */
1028 0, /* @tp_getattr@ */
1029 0, /* @tp_setattr@ */
1030 0, /* @tp_compare@ */
1032 0, /* @tp_as_number@ */
1033 0, /* @tp_as_sequence@ */
1034 0, /* @tp_as_mapping@ */
1038 0, /* @tp_getattro@ */
1039 0, /* @tp_setattro@ */
1040 0, /* @tp_as_buffer@ */
1041 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1042 Py_TPFLAGS_BASETYPE,
1045 "An elliptic curve. Abstract class.",
1047 0, /* @tp_traverse@ */
1049 eccurve_pyrichcompare, /* @tp_richcompare@ */
1050 0, /* @tp_weaklistoffset@ */
1052 0, /* @tp_iternext@ */
1053 eccurve_pymethods, /* @tp_methods@ */
1054 0, /* @tp_members@ */
1055 eccurve_pygetset, /* @tp_getset@ */
1058 0, /* @tp_descr_get@ */
1059 0, /* @tp_descr_set@ */
1060 0, /* @tp_dictoffset@ */
1062 PyType_GenericAlloc, /* @tp_alloc@ */
1063 abstract_pynew, /* @tp_new@ */
1068 static PyObject *ecprimecurve_pynew(PyTypeObject *ty,
1069 PyObject *arg, PyObject *kw)
1071 return (eccurve_pynew(ty, ec_prime, arg, kw));
1074 static PyTypeObject ecprimecurve_pytype_skel = {
1075 PyObject_HEAD_INIT(0) 0, /* Header */
1076 "ECPrimeCurve", /* @tp_name@ */
1077 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1078 0, /* @tp_itemsize@ */
1080 eccurve_pydealloc, /* @tp_dealloc@ */
1082 0, /* @tp_getattr@ */
1083 0, /* @tp_setattr@ */
1084 0, /* @tp_compare@ */
1086 0, /* @tp_as_number@ */
1087 0, /* @tp_as_sequence@ */
1088 0, /* @tp_as_mapping@ */
1092 0, /* @tp_getattro@ */
1093 0, /* @tp_setattro@ */
1094 0, /* @tp_as_buffer@ */
1095 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1096 Py_TPFLAGS_BASETYPE,
1099 "An elliptic curve over a prime field. Use ecprimeprojcurve.",
1101 0, /* @tp_traverse@ */
1103 eccurve_pyrichcompare, /* @tp_richcompare@ */
1104 0, /* @tp_weaklistoffset@ */
1106 0, /* @tp_iternext@ */
1107 0, /* @tp_methods@ */
1108 0, /* @tp_members@ */
1109 0, /* @tp_getset@ */
1112 0, /* @tp_descr_get@ */
1113 0, /* @tp_descr_set@ */
1114 0, /* @tp_dictoffset@ */
1116 PyType_GenericAlloc, /* @tp_alloc@ */
1117 ecprimecurve_pynew, /* @tp_new@ */
1122 static PyObject *ecprimeprojcurve_pynew(PyTypeObject *ty,
1123 PyObject *arg, PyObject *kw)
1125 return (eccurve_pynew(ty, ec_primeproj, arg, kw));
1128 static PyTypeObject ecprimeprojcurve_pytype_skel = {
1129 PyObject_HEAD_INIT(0) 0, /* Header */
1130 "ECPrimeProjCurve", /* @tp_name@ */
1131 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1132 0, /* @tp_itemsize@ */
1134 eccurve_pydealloc, /* @tp_dealloc@ */
1136 0, /* @tp_getattr@ */
1137 0, /* @tp_setattr@ */
1138 0, /* @tp_compare@ */
1140 0, /* @tp_as_number@ */
1141 0, /* @tp_as_sequence@ */
1142 0, /* @tp_as_mapping@ */
1146 0, /* @tp_getattro@ */
1147 0, /* @tp_setattro@ */
1148 0, /* @tp_as_buffer@ */
1149 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1150 Py_TPFLAGS_BASETYPE,
1153 "An elliptic curve over a prime field, using projective coordinates.",
1155 0, /* @tp_traverse@ */
1157 eccurve_pyrichcompare, /* @tp_richcompare@ */
1158 0, /* @tp_weaklistoffset@ */
1160 0, /* @tp_iternext@ */
1161 0, /* @tp_methods@ */
1162 0, /* @tp_members@ */
1163 0, /* @tp_getset@ */
1166 0, /* @tp_descr_get@ */
1167 0, /* @tp_descr_set@ */
1168 0, /* @tp_dictoffset@ */
1170 PyType_GenericAlloc, /* @tp_alloc@ */
1171 ecprimeprojcurve_pynew, /* @tp_new@ */
1176 static PyObject *ecbincurve_pynew(PyTypeObject *ty,
1177 PyObject *arg, PyObject *kw)
1179 return (eccurve_pynew(ty, ec_bin, arg, kw));
1182 static PyTypeObject ecbincurve_pytype_skel = {
1183 PyObject_HEAD_INIT(0) 0, /* Header */
1184 "ECBinCurve", /* @tp_name@ */
1185 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1186 0, /* @tp_itemsize@ */
1188 eccurve_pydealloc, /* @tp_dealloc@ */
1190 0, /* @tp_getattr@ */
1191 0, /* @tp_setattr@ */
1192 0, /* @tp_compare@ */
1194 0, /* @tp_as_number@ */
1195 0, /* @tp_as_sequence@ */
1196 0, /* @tp_as_mapping@ */
1200 0, /* @tp_getattro@ */
1201 0, /* @tp_setattro@ */
1202 0, /* @tp_as_buffer@ */
1203 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1204 Py_TPFLAGS_BASETYPE,
1207 "An elliptic curve over a binary field. Use ecbinprojcurve.",
1209 0, /* @tp_traverse@ */
1211 eccurve_pyrichcompare, /* @tp_richcompare@ */
1212 0, /* @tp_weaklistoffset@ */
1214 0, /* @tp_iternext@ */
1215 0, /* @tp_methods@ */
1216 0, /* @tp_members@ */
1217 0, /* @tp_getset@ */
1220 0, /* @tp_descr_get@ */
1221 0, /* @tp_descr_set@ */
1222 0, /* @tp_dictoffset@ */
1224 PyType_GenericAlloc, /* @tp_alloc@ */
1225 ecbincurve_pynew, /* @tp_new@ */
1230 static PyObject *ecbinprojcurve_pynew(PyTypeObject *ty,
1231 PyObject *arg, PyObject *kw)
1233 return (eccurve_pynew(ty, ec_binproj, arg, kw));
1236 static PyTypeObject ecbinprojcurve_pytype_skel = {
1237 PyObject_HEAD_INIT(0) 0, /* Header */
1238 "ECBinProjCurve", /* @tp_name@ */
1239 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1240 0, /* @tp_itemsize@ */
1242 eccurve_pydealloc, /* @tp_dealloc@ */
1244 0, /* @tp_getattr@ */
1245 0, /* @tp_setattr@ */
1246 0, /* @tp_compare@ */
1248 0, /* @tp_as_number@ */
1249 0, /* @tp_as_sequence@ */
1250 0, /* @tp_as_mapping@ */
1254 0, /* @tp_getattro@ */
1255 0, /* @tp_setattro@ */
1256 0, /* @tp_as_buffer@ */
1257 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1258 Py_TPFLAGS_BASETYPE,
1261 "An elliptic curve over a binary field, using projective coordinates.",
1263 0, /* @tp_traverse@ */
1265 eccurve_pyrichcompare, /* @tp_richcompare@ */
1266 0, /* @tp_weaklistoffset@ */
1268 0, /* @tp_iternext@ */
1269 0, /* @tp_methods@ */
1270 0, /* @tp_members@ */
1271 0, /* @tp_getset@ */
1274 0, /* @tp_descr_get@ */
1275 0, /* @tp_descr_set@ */
1276 0, /* @tp_dictoffset@ */
1278 PyType_GenericAlloc, /* @tp_alloc@ */
1279 ecbinprojcurve_pynew, /* @tp_new@ */
1284 /*----- Curve info --------------------------------------------------------*/
1286 static int ncurves = -1;
1288 void ecinfo_copy(ec_info *eic, const ec_info *ei)
1290 eic->c = eccurve_copy(ei->c);
1292 EC_COPY(&eic->g, &ei->g);
1293 eic->r = MP_COPY(ei->r);
1294 eic->h = MP_COPY(ei->h);
1297 PyObject *ecinfo_pywrap(ec_info *ei)
1301 o = PyObject_NEW(ecinfo_pyobj, ecinfo_pytype);
1303 o->cobj = eccurve_pywrap(0, o->ei.c);
1305 return ((PyObject *)o);
1308 static void ecinfo_pydealloc(PyObject *me)
1310 ec_info *ei = ECINFO_EI(me);
1314 Py_DECREF(ECINFO_COBJ(me));
1318 static PyObject *ecinfo_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1322 char *kwlist[] = { "curve", "G", "r", "h", 0 };
1323 ecinfo_pyobj *rc = 0;
1325 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!O&O&:new", kwlist,
1326 eccurve_pytype, &e, ecpt_pytype, &g,
1327 convmp, &ei.r, convmp, &ei.h))
1329 if (ECPT_C(g) != ECCURVE_C(e) && !ec_samep(ECPT_C(g), ECCURVE_C(e)))
1330 TYERR("point not from this curve");
1331 ei.c = ECCURVE_C(e);
1333 EC_OUT(ei.c, &ei.g, ECPT_P(g));
1334 rc = (ecinfo_pyobj *)ty->tp_alloc(ty, 0);
1337 Py_INCREF(rc->cobj);
1338 return ((PyObject *)rc);
1346 static PyObject *meth__ECInfo_parse(PyObject *me, PyObject *arg)
1353 if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
1357 if (ec_infoparse(&qd, &ei))
1359 rc = Py_BuildValue("(Ns)", ecinfo_pywrap(&ei), qd.p);
1364 static PyObject *meth__ECInfo__curven(PyObject *me, PyObject *arg)
1370 if (!PyArg_ParseTuple(arg, "Oi:_curven", &me, &i)) goto end;
1371 if (i < 0 || i >= ncurves) VALERR("curve index out of range");
1372 ec_infofromdata(&ei, ectab[i].data);
1373 rc = ecinfo_pywrap(&ei);
1378 static PyObject *ecinfo_pyrichcompare(PyObject *x, PyObject *y, int op)
1380 int b = ec_sameinfop(ECINFO_EI(x), ECINFO_EI(y));
1384 default: TYERR("can't order elliptic curve infos");
1386 return (getbool(b));
1391 static PyObject *eimeth_check(PyObject *me, PyObject *arg, PyObject *kw)
1393 char *kwlist[] = { "rng", 0 };
1394 grand *r = &rand_global;
1397 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:check", kwlist,
1400 if ((p = ec_checkinfo(ECINFO_EI(me), r)) != 0)
1407 static PyObject *eiget_curve(PyObject *me, void *hunoz)
1408 { RETURN_OBJ(ECINFO_COBJ(me)); }
1410 static PyObject *eiget_G(PyObject *me, void *hunoz)
1412 ec_info *ei = ECINFO_EI(me);
1414 EC_IN(ei->c, &p, &ei->g);
1415 return (ecpt_pywrap(ECINFO_COBJ(me), &p));
1418 static PyObject *eiget_r(PyObject *me, void *hunoz)
1419 { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->r))); }
1421 static PyObject *eiget_h(PyObject *me, void *hunoz)
1422 { return (mp_pywrap(MP_COPY(ECINFO_EI(me)->h))); }
1424 static PyGetSetDef ecinfo_pygetset[] = {
1425 #define GETSETNAME(op, name) ei##op##_##name
1426 GET (curve, "I.curve -> the elliptic curve")
1427 GET (G, "I.G -> generator point for the group")
1428 GET (r, "I.r -> order of the group (and hence of G")
1429 GET (h, "I.h -> cofactor of the group")
1434 static PyMethodDef ecinfo_pymethods[] = {
1435 #define METHNAME(name) eimeth_##name
1436 KWMETH(check, "I.check() -> None")
1441 static PyTypeObject ecinfo_pytype_skel = {
1442 PyObject_HEAD_INIT(0) 0, /* Header */
1443 "ECInfo", /* @tp_name@ */
1444 sizeof(ecinfo_pyobj), /* @tp_basicsize@ */
1445 0, /* @tp_itemsize@ */
1447 ecinfo_pydealloc, /* @tp_dealloc@ */
1449 0, /* @tp_getattr@ */
1450 0, /* @tp_setattr@ */
1451 0, /* @tp_compare@ */
1453 0, /* @tp_as_number@ */
1454 0, /* @tp_as_sequence@ */
1455 0, /* @tp_as_mapping@ */
1459 0, /* @tp_getattro@ */
1460 0, /* @tp_setattro@ */
1461 0, /* @tp_as_buffer@ */
1462 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1463 Py_TPFLAGS_BASETYPE,
1466 "Elliptic curve domain parameters.",
1468 0, /* @tp_traverse@ */
1470 ecinfo_pyrichcompare, /* @tp_richcompare@ */
1471 0, /* @tp_weaklistoffset@ */
1473 0, /* @tp_iternext@ */
1474 ecinfo_pymethods, /* @tp_methods@ */
1475 0, /* @tp_members@ */
1476 ecinfo_pygetset, /* @tp_getset@ */
1479 0, /* @tp_descr_get@ */
1480 0, /* @tp_descr_set@ */
1481 0, /* @tp_dictoffset@ */
1483 PyType_GenericAlloc, /* @tp_alloc@ */
1484 ecinfo_pynew, /* @tp_new@ */
1489 /*----- Setup -------------------------------------------------------------*/
1491 static PyMethodDef methods[] = {
1492 #define METHNAME(func) meth_##func
1493 METH (_ECPt_frombuf, "frombuf(E, STR) -> (P, REST)")
1494 METH (_ECPtCurve_fromraw, "fromraw(E, STR) -> (P, REST)")
1495 METH (_ECPt_parse, "parse(E, STR) -> (P, REST)")
1496 METH (_ECCurve_parse, "parse(STR) -> (E, REST)")
1497 METH (_ECInfo_parse, "parse(STR) -> (I, REST)")
1498 METH (_ECInfo__curven, "_curven(N) -> I")
1503 void ec_pyinit(void)
1505 INITTYPE(ecpt, root);
1506 INITTYPE(ecptcurve, ecpt);
1507 INITTYPE(eccurve, type);
1508 INITTYPE(ecprimecurve, eccurve);
1509 INITTYPE(ecprimeprojcurve, ecprimecurve);
1510 INITTYPE(ecbincurve, eccurve);
1511 INITTYPE(ecbinprojcurve, ecbincurve);
1512 INITTYPE(ecinfo, root);
1513 addmethods(methods);
1516 static PyObject *namedcurves(void)
1523 for (i = 0; ectab[i].name; i++) {
1525 for (j = 0; j < i; j++) {
1526 if (ectab[i].data == ectab[j].data) {
1527 c = PyDict_GetItemString(d, (/*unconst*/ char *)ectab[j].name);
1532 c = PyInt_FromLong(i);
1534 PyDict_SetItemString(d, (/*unconst*/ char *)p, c);
1541 void ec_pyinsert(PyObject *mod)
1543 INSERT("ECPt", ecpt_pytype);
1544 INSERT("ECPtCurve", ecptcurve_pytype);
1545 INSERT("ECCurve", eccurve_pytype);
1546 INSERT("ECPrimeCurve", ecprimecurve_pytype);
1547 INSERT("ECPrimeProjCurve", ecprimeprojcurve_pytype);
1548 INSERT("ECBinCurve", ecbincurve_pytype);
1549 INSERT("ECBinProjCurve", ecbinprojcurve_pytype);
1550 INSERT("ECInfo", ecinfo_pytype);
1551 INSERT("_eccurves", namedcurves());
1554 /*----- That's all, folks -------------------------------------------------*/