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"))
52 c = ec_prime(f, a, b);
53 else if (STRCMP(EC_NAME(c), ==, "primeproj"))
54 c = ec_primeproj(f, a, b);
55 else if (STRCMP(EC_NAME(c), ==, "bin"))
57 else if (STRCMP(EC_NAME(c), ==, "binproj"))
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)) RETURN_NOTIMPL;
190 if (FE_PYCHECK(x) && FE_F(x)->ops->ty == FTY_PRIME)
191 xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
192 else if ((xx = implicitmp(x)) == 0)
194 ec_imul(ECPT_C(y), &zz, ECPT_P(y), xx);
196 return (ecpt_pywrap(ECPT_COBJ(y), &zz));
199 static Py_hash_t ecpt_pyhash(PyObject *me)
205 if (EC_ATINF(&p)) h = 0x81d81a94;
206 else h = 0xe0fdd039 ^ (2*mphash(p.x)) ^ (3*mphash(p.y));
211 static PyObject *ecpt_pyrichcompare(PyObject *x, PyObject *y, int op)
213 ec p = EC_INIT, q = EC_INIT;
217 if (!ECPT_PYCHECK(y)) RETURN_NOTIMPL;
221 case Py_EQ: b = EC_EQ(&p, &q); break;
222 case Py_NE: b = !EC_EQ(&p, &q); break;
223 default: TYERR("elliptic curve points are unordered");
232 static PyObject *epmeth_oncurvep(PyObject *me)
234 return (getbool(EC_ATINF(ECPT_P(me)) ||
235 !EC_CHECK(ECPT_C(me), ECPT_P(me))));
238 static PyObject *epmeth_dbl(PyObject *me)
241 EC_DBL(ECPT_C(me), &p, ECPT_P(me));
242 return (ecpt_pywrap(ECPT_COBJ(me), &p));
245 static PyObject *epmeth_tobuf(PyObject *me)
256 n = mp_octets(p.x) + mp_octets(p.y) + 6;
257 rc = bytestring_pywrap(0, n);
258 buf_init(&b, BIN_PTR(rc), n);
261 BIN_SETLEN(rc, BLEN(&b));
266 static PyObject *epmeth_toraw(PyObject *me)
271 ec_curve *c = ECPT_C(me);
275 len = c->f->noctets * 2 + 1;
276 rc = bytestring_pywrap(0, len);
278 buf_init(&b, p, len);
279 EC_OUT(c, &pp, ECPT_P(me));
280 ec_putraw(c, &b, &pp);
282 BIN_SETLEN(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 static const char *const 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);
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 BIN_SETLEN(rc, BLEN(&b));
315 static PyObject *epmeth_frombuf(PyObject *me, PyObject *arg)
322 if (!PyArg_ParseTuple(arg, "O&:frombuf", convbin, &in)) goto end;
323 buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
324 if (buf_getec(&b, &pp)) VALERR("malformed data");
325 rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp),
326 bytestring_pywrapbuf(&b));
331 static PyObject *epmeth_parse(PyObject *me, PyObject *arg)
338 if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end;
340 if (!ec_ptparse(&qd, &pp)) VALERR(qd.e);
341 rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p);
346 static PyObject *epmeth_fromraw(PyObject *me, PyObject *arg)
354 if (!PyArg_ParseTuple(arg, "O&:fromraw", convbin, &in)) return (0);
355 buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
357 if (ec_getraw(cc, &b, &pp))
360 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
365 static PyObject *epmeth_os2ecp(PyObject *me, PyObject *arg, PyObject *kw)
371 unsigned f = EC_XONLY | EC_LSB | EC_SORT | EC_EXPLY;
373 static const char *const kwlist[] = { "buf", "flags", 0 };
375 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:os2ecp", KWLIST,
376 convbin, &in, convuint, &f))
378 buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
380 if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point");
382 rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
387 static PyObject *epncget_ix(PyObject *me, void *hunoz)
391 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
393 rc = mp_pywrap(MP_COPY(p.x));
398 static PyObject *epncget_iy(PyObject *me, void *hunoz)
402 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
404 rc = mp_pywrap(MP_COPY(p.y));
409 static PyObject *epncget_point(PyObject *me, void *hunoz)
412 static PyObject *epget_point(PyObject *me, void *hunoz)
416 return (ecpt_pywrapout(ecpt_pytype, &p));
419 static PyObject *epget_x(PyObject *me, void *hunoz)
421 ec_curve *c = ECPT_C(me);
423 PyObject *fobj = ECPT_FOBJ(me);
427 if (EC_ATINF(pp)) RETURN_NONE;
429 rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.x));
434 static PyObject *epget_y(PyObject *me, void *hunoz)
436 ec_curve *c = ECPT_C(me);
438 PyObject *fobj = ECPT_FOBJ(me);
442 if (EC_ATINF(pp)) RETURN_NONE;
444 rc = fe_pywrap(fobj, F_IN(c->f, MP_NEW, p.y));
449 static PyObject *epget__x(PyObject *me, void *hunoz)
451 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
452 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->x)));
455 static PyObject *epget__y(PyObject *me, void *hunoz)
457 if (EC_ATINF(ECPT_P(me))) RETURN_NONE;
458 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->y)));
461 static PyObject *epget__z(PyObject *me, void *hunoz)
463 if (EC_ATINF(ECPT_P(me)) || !ECPT_P(me)->z) RETURN_NONE;
464 return (fe_pywrap(ECPT_FOBJ(me), MP_COPY(ECPT_P(me)->z)));
467 static mp *coord_in(field *f, PyObject *x)
470 if (FE_PYCHECK(x) && (FE_F(x) == f || field_samep(FE_F(x), f)))
471 return (MP_COPY(FE_X(x)));
472 else if ((xx = getmp(x)) == 0)
475 return (F_IN(f, xx, xx));
478 static int ecptxl_3(ec_curve *c, ec *p,
479 PyObject *x, PyObject *y, PyObject *z)
483 if (!x || !y || !z) TYERR("missing argument");
484 if (!c) VALERR("internal form with no curve!");
485 if ((p->x = coord_in(c->f, x)) == 0 ||
486 (p->y = coord_in(c->f, y)) == 0 ||
487 (z != Py_None && (p->z = coord_in(c->f, z)) == 0))
489 if (!p->z) p->z = MP_COPY(c->f->one); /* just in case */
495 static int ecptxl_2(ec_curve *c, ec *p, PyObject *x, PyObject *y)
499 if (!x || !y) TYERR("missing argument");
500 if ((p->x = getmp(x)) == 0 ||
501 (p->y = getmp(y)) == 0)
503 if (c) EC_IN(c, p, p);
509 static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
512 PyObject *y = 0, *z = 0, *t = 0;
518 if (!x || x == Py_None)
520 else if (ECPT_PYCHECK(x)) {
523 } else if (TEXT_CHECK(x)) {
526 if (!ec_ptparse(&qd, p))
528 qd_skipspc(&qd); if (!qd_eofp(&qd)) VALERR("junk at eof");
530 } else if (c && (xx = tomp(x)) != 0) {
531 xx = F_IN(c->f, xx, xx);
532 if (!EC_FIND(c, p, xx)) VALERR("not on the curve");
533 } else if (PySequence_Check(x)) {
535 n = PySequence_Size(t); if (n < 0) goto end;
536 if (n != 2 && (n != 3 || !c))
537 TYERR("want sequence of two or three items");
538 if ((x = PySequence_GetItem(t, 0)) == 0 ||
539 (y = PySequence_GetItem(t, 1)) == 0 ||
540 (n == 3 && (z = PySequence_GetItem(t, 2)) == 0))
542 rc = (n == 2) ? ecptxl_2(c, p, x, y) : ecptxl_3(c, p, x, y, z);
545 TYERR("can't convert to curve point");
549 if (c) EC_IN(c, p, p);
553 Py_XDECREF(x); Py_XDECREF(y); Py_XDECREF(z); Py_XDECREF(t);
558 static int ecptxl(ec_curve *c, ec *p, PyObject *x, PyObject *y, PyObject *z)
561 return (ecptxl_3(c, p, x, y, z));
563 return (ecptxl_2(c, p, x, y));
565 return (ecptxl_1(c, p, x));
568 static PyObject *ecptnc_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
570 PyObject *x = 0, *y = 0, *z = 0;
572 static const char *const kwlist[] = { "x", "y", 0 };
574 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:new", KWLIST, &x, &y) ||
575 ecptxl(0, &p, x, y, z))
577 return (ecpt_pywrapout(ty, &p));
579 mp_drop(p.x); mp_drop(p.y); mp_drop(p.z);
583 static PyObject *ecpt_pyint(PyObject *me)
588 if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
590 if (!mp_tolong_checked(p.x, &l, 0)) rc = PyInt_FromLong(l);
591 else rc = mp_topylong(p.x);
598 static PyObject *ecpt_pylong(PyObject *me)
602 if (EC_ATINF(ECPT_P(me))) VALERR("point at infinity");
604 rc = mp_topylong(p.x);
611 static PyObject *ecpt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
613 PyObject *x = 0, *y = 0, *z = 0;
615 static const char *const kwlist[] = { "x", "y", "z", 0 };
617 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OOO:new", KWLIST,
619 ecptxl(ECCURVE_C(ty), &p, x, y, z))
621 return (ecpt_pywrap((PyObject *)ty, &p));
623 mp_drop(p.x); mp_drop(p.y); mp_drop(p.z);
627 static const PyGetSetDef ecptnc_pygetset[] = {
628 #define GETSETNAME(op, name) epnc##op##_##name
629 GET (ix, "P.ix -> integer x coordinate of P")
630 GET (iy, "P.iy -> integer y coordinate of P")
631 GET (point, "P.point -> standalone curve point (no-op)")
636 static const PyMethodDef ecptnc_pymethods[] = {
637 #define METHNAME(func) epmeth_##func
638 NAMETH(tobuf, "X.tobuf() -> BIN")
639 CMTH (frombuf, "frombuf(STR) -> (P, REST)")
640 CMTH (parse, "parse(STR) -> (P, REST)")
645 static const PyNumberMethods ecpt_pynumber = {
647 0, /* @nb_subtract@ */
648 0, /* @nb_multiply@ */
652 0, /* @nb_remainder@ */
655 0, /* @nb_negative@ */
656 0, /* @nb_positive@ */
657 0, /* @nb_absolute@ */
658 ecpt_pynonzerop, /* @nb_nonzero@ */
668 ecpt_pyint, /* @nb_int@ */
669 PY23(ecpt_pylong, 0), /* @nb_long@ */
676 0, /* @nb_inplace_add@ */
677 0, /* @nb_inplace_subtract@ */
678 0, /* @nb_inplace_multiply@ */
680 0, /* @nb_inplace_divide@ */
682 0, /* @nb_inplace_remainder@ */
683 0, /* @nb_inplace_power@ */
684 0, /* @nb_inplace_lshift@ */
685 0, /* @nb_inplace_rshift@ */
686 0, /* @nb_inplace_and@ */
687 0, /* @nb_inplace_xor@ */
688 0, /* @nb_inplace_or@ */
690 0, /* @nb_floor_divide@ */
691 0, /* @nb_true_divide@ */
692 0, /* @nb_inplace_floor_divide@ */
693 0, /* @nb_inplace_true_divide@ */
696 static const PyTypeObject ecpt_pytype_skel = {
697 PyVarObject_HEAD_INIT(0, 0) /* Header */
698 "ECPt", /* @tp_name@ */
699 sizeof(ecpt_pyobj), /* @tp_basicsize@ */
700 0, /* @tp_itemsize@ */
702 ecpt_pydealloc, /* @tp_dealloc@ */
704 0, /* @tp_getattr@ */
705 0, /* @tp_setattr@ */
706 0, /* @tp_compare@ */
708 PYNUMBER(ecpt), /* @tp_as_number@ */
709 0, /* @tp_as_sequence@ */
710 0, /* @tp_as_mapping@ */
711 ecpt_pyhash, /* @tp_hash@ */
714 0, /* @tp_getattro@ */
715 0, /* @tp_setattro@ */
716 0, /* @tp_as_buffer@ */
717 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
718 Py_TPFLAGS_CHECKTYPES |
722 "ECPt([X, [Y]]): elliptic curve points, not associated with any curve.\n"
723 " X alone may be None, an existing point, a string 'X, Y', an\n"
724 " x-coordinate, or a pair (X, Y); X and Y should be a coordinate pair.",
726 0, /* @tp_traverse@ */
728 ecpt_pyrichcompare, /* @tp_richcompare@ */
729 0, /* @tp_weaklistoffset@ */
731 0, /* @tp_iternext@ */
732 PYMETHODS(ecptnc), /* @tp_methods@ */
733 0, /* @tp_members@ */
734 PYGETSET(ecptnc), /* @tp_getset@ */
737 0, /* @tp_descr_get@ */
738 0, /* @tp_descr_set@ */
739 0, /* @tp_dictoffset@ */
741 PyType_GenericAlloc, /* @tp_alloc@ */
742 ecptnc_pynew, /* @tp_new@ */
747 static const PyMemberDef ecpt_pymembers[] = {
748 #define MEMBERSTRUCT ecpt_pyobj
749 MEMRNM(curve, T_OBJECT, PY23(ob_type, ob_base.ob_type), READONLY,
750 "P.curve -> elliptic curve containing P")
755 static const PyGetSetDef ecpt_pygetset[] = {
756 #define GETSETNAME(op, name) ep##op##_##name
757 GET (point, "P.point -> standalone curve point")
758 GET (x, "P.x -> Cartesian x coordinate of P")
759 GET (y, "P.y -> Cartesian y coordinate of P")
760 GET (_x, "P._x -> internal x coordinate of P")
761 GET (_y, "P._y -> internal y coordinate of P")
762 GET (_z, "P._z -> internal z coordinate of P, or None")
767 static const PyMethodDef ecpt_pymethods[] = {
768 #define METHNAME(func) epmeth_##func
769 NAMETH(toraw, "X.toraw() -> BIN")
770 KWMETH(ec2osp, "X.ec2osp([flags = EC_EXPLY]) -> BIN")
771 NAMETH(dbl, "X.dbl() -> X + X")
772 NAMETH(oncurvep, "X.oncurvep() -> BOOL")
773 CMTH (fromraw, "fromraw(STR) -> (P, REST)")
774 KWCMTH(os2ecp, "os2ecp(STR, [flags = ...]) -> (P, REST)")
779 static const PyNumberMethods ecptcurve_pynumber = {
780 ecpt_pyadd, /* @nb_add@ */
781 ecpt_pysub, /* @nb_subtract@ */
782 ecpt_pymul, /* @nb_multiply@ */
786 0, /* @nb_remainder@ */
789 ecpt_pyneg, /* @nb_negative@ */
790 ecpt_pyid, /* @nb_positive@ */
791 0, /* @nb_absolute@ */
792 0, /* @nb_nonzero@ */
810 0, /* @nb_inplace_add@ */
811 0, /* @nb_inplace_subtract@ */
812 0, /* @nb_inplace_multiply@ */
813 0, /* @nb_inplace_divide@ */
814 0, /* @nb_inplace_remainder@ */
815 0, /* @nb_inplace_power@ */
816 0, /* @nb_inplace_lshift@ */
817 0, /* @nb_inplace_rshift@ */
818 0, /* @nb_inplace_and@ */
819 0, /* @nb_inplace_xor@ */
820 0, /* @nb_inplace_or@ */
822 0, /* @nb_floor_divide@ */
823 0, /* @nb_true_divide@ */
824 0, /* @nb_inplace_floor_divide@ */
825 0, /* @nb_inplace_true_divide@ */
828 static const PyTypeObject ecptcurve_pytype_skel = {
829 PyVarObject_HEAD_INIT(0, 0) /* Header */
830 "ECPtCurve", /* @tp_name@ */
831 sizeof(ecpt_pyobj), /* @tp_basicsize@ */
832 0, /* @tp_itemsize@ */
834 ecpt_pydealloc, /* @tp_dealloc@ */
836 0, /* @tp_getattr@ */
837 0, /* @tp_setattr@ */
838 0, /* @tp_compare@ */
840 PYNUMBER(ecptcurve), /* @tp_as_number@ */
841 0, /* @tp_as_sequence@ */
842 0, /* @tp_as_mapping@ */
846 0, /* @tp_getattro@ */
847 0, /* @tp_setattro@ */
848 0, /* @tp_as_buffer@ */
849 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
850 Py_TPFLAGS_CHECKTYPES |
854 "Elliptic curve points; abstract base class for points on given curves.",
856 0, /* @tp_traverse@ */
858 0, /* @tp_richcompare@ */
859 0, /* @tp_weaklistoffset@ */
861 0, /* @tp_iternext@ */
862 PYMETHODS(ecpt), /* @tp_methods@ */
863 PYMEMBERS(ecpt), /* @tp_members@ */
864 PYGETSET(ecpt), /* @tp_getset@ */
867 0, /* @tp_descr_get@ */
868 0, /* @tp_descr_set@ */
869 0, /* @tp_dictoffset@ */
871 PyType_GenericAlloc, /* @tp_alloc@ */
872 abstract_pynew, /* @tp_new@ */
877 /*----- Elliptic curves themselves ----------------------------------------*/
879 static PyObject *eccurve_pyrichcompare(PyObject *x, PyObject *y, int op)
883 assert(ECCURVE_PYCHECK(x));
884 if (!ECCURVE_PYCHECK(y)) RETURN_NOTIMPL;
885 b = ec_samep(ECCURVE_C(x), ECCURVE_C(y));
888 case Py_NE: b = !b; break;
889 default: TYERR("can't order elliptic curves");
896 static PyObject *ecmmul_id(PyObject *me)
897 { ec p = EC_INIT; return (ecpt_pywrap(me, &p)); }
899 static int ecmmul_fill(void *pp, PyObject *me, PyObject *x, PyObject *m)
901 ec_mulfactor *f = pp;
904 if (getecpt(ECCURVE_C(me), &f->base, x) ||
905 (f->exp = getmp(m)) == 0)
910 static PyObject *ecmmul_exp(PyObject *me, void *pp, int n)
913 ec_immul(ECCURVE_C(me), &p, pp, n);
914 return (ecpt_pywrap(me, &p));
917 static void ecmmul_drop(void *pp)
919 ec_mulfactor *f = pp;
920 EC_DESTROY(&f->base);
924 static PyObject *ecmeth_mmul(PyObject *me, PyObject *arg)
926 return (mexp_common(me, arg, sizeof(ec_mulfactor),
927 ecmmul_id, ecmmul_fill, ecmmul_exp, ecmmul_drop));
930 static void eccurve_pydealloc(PyObject *me)
932 ec_destroycurve(ECCURVE_C(me));
933 Py_DECREF(ECCURVE_FOBJ(me));
934 PyType_Type.tp_dealloc(me);
937 static PyObject *ecmeth_find(PyObject *me, PyObject *arg)
944 if (!PyArg_ParseTuple(arg, "O:find", &x)) goto end;
945 if (FIELD_PYCHECK(x) && FE_F(x) == ECCURVE_C(me)->f)
946 xx = MP_COPY(FE_X(x));
947 else if ((xx = getmp(x)) == 0)
950 xx = F_IN(ECCURVE_C(me)->f, xx, xx);
951 if (EC_FIND(ECCURVE_C(me), &p, xx) == 0)
952 VALERR("not on the curve");
953 rc = ecpt_pywrap(me, &p);
959 static PyObject *ecmeth_rand(PyObject *me, PyObject *arg, PyObject *kw)
961 static const char *const kwlist[] = { "rng", 0 };
962 grand *r = &rand_global;
965 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:rand", KWLIST,
968 ec_rand(ECCURVE_C(me), &p, r);
969 EC_IN(ECCURVE_C(me), &p, &p);
970 return (ecpt_pywrap(me, &p));
973 static PyObject *ecmeth_parse(PyObject *me, PyObject *arg)
980 if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end;
982 if ((c = ec_curveparse(&qd)) == 0) VALERR(qd.e);
983 rc = eccurve_pywrap(0, c);
988 static PyObject *eccurve_dopywrap(PyTypeObject *ty,
989 PyObject *fobj, ec_curve *c)
991 eccurve_pyobj *cobj = newtype(ty, 0, c->ops->name);
994 cobj->ty.ht_type.tp_basicsize = sizeof(ecpt_pyobj);
995 cobj->ty.ht_type.tp_base = ecptcurve_pytype;
996 Py_INCREF(ecptcurve_pytype);
997 cobj->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
998 Py_TPFLAGS_BASETYPE |
999 Py_TPFLAGS_CHECKTYPES |
1000 Py_TPFLAGS_HEAPTYPE);
1001 cobj->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1002 cobj->ty.ht_type.tp_free = 0;
1003 cobj->ty.ht_type.tp_new = ecpt_pynew;
1004 typeready(&cobj->ty.ht_type);
1005 return ((PyObject *)cobj);
1008 PyObject *eccurve_pywrap(PyObject *fobj, ec_curve *c)
1013 fobj = field_pywrap(c->f);
1016 assert(FIELD_F(fobj) == c->f);
1017 if (STRCMP(EC_NAME(c), ==, "prime"))
1018 ty = ecprimecurve_pytype;
1019 else if (STRCMP(EC_NAME(c), ==, "primeproj"))
1020 ty = ecprimeprojcurve_pytype;
1021 else if (STRCMP(EC_NAME(c), ==, "bin"))
1022 ty = ecbincurve_pytype;
1023 else if (STRCMP(EC_NAME(c), ==, "binproj"))
1024 ty = ecbinprojcurve_pytype;
1027 return (eccurve_dopywrap(ty, fobj, c));
1030 static PyObject *eccurve_pynew(PyTypeObject *ty,
1031 ec_curve *(*make)(field *, mp *, mp *),
1032 PyObject *arg, PyObject *kw)
1036 static const char *const kwlist[] = { "field", "a", "b", 0 };
1037 mp *aa = 0, *bb = 0;
1039 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O&O&", KWLIST,
1040 field_pytype, &fobj,
1041 convmp, &aa, convmp, &bb))
1044 cobj = eccurve_dopywrap(ty, fobj, make(FIELD_F(fobj), aa, bb));
1046 if (aa) MP_DROP(aa);
1047 if (bb) MP_DROP(bb);
1051 static PyObject *ecget_name(PyObject *me, void *hunoz)
1052 { return (TEXT_FROMSTR(EC_NAME(ECCURVE_C(me)))); }
1054 static PyObject *ecget_a(PyObject *me, void *hunoz)
1055 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->a))); }
1057 static PyObject *ecget_b(PyObject *me, void *hunoz)
1058 { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->b))); }
1060 static PyObject *ecget_field(PyObject *me, void *hunoz)
1061 { RETURN_OBJ(ECCURVE_FOBJ(me)); }
1063 static PyObject *ecget_inf(PyObject *me, void *hunoz)
1064 { ec inf = EC_INIT; return (ecpt_pywrap(me, &inf)); }
1066 static const PyGetSetDef eccurve_pygetset[] = {
1067 #define GETSETNAME(op, name) ec##op##_##name
1068 GET (name, "E.name -> name of this kind of curve")
1069 GET (a, "E.a -> first parameter of curve")
1070 GET (b, "E.b -> second parameter of curve")
1071 GET (field, "E.field -> finite field containing this curve")
1072 GET (inf, "E.inf -> point at infinity of this curve")
1077 static const PyMethodDef eccurve_pymethods[] = {
1078 #define METHNAME(name) ecmeth_##name
1079 METH (mmul, "E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...")
1080 METH (find, "E.find(X) -> P")
1081 KWMETH(rand, "E.rand([rng = rand]) -> P")
1082 SMTH (parse, "parse(STR) -> (E, REST)")
1087 static const PyTypeObject eccurve_pytype_skel = {
1088 PyVarObject_HEAD_INIT(0, 0) /* Header */
1089 "ECCurve", /* @tp_name@ */
1090 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1091 0, /* @tp_itemsize@ */
1093 eccurve_pydealloc, /* @tp_dealloc@ */
1095 0, /* @tp_getattr@ */
1096 0, /* @tp_setattr@ */
1097 0, /* @tp_compare@ */
1099 0, /* @tp_as_number@ */
1100 0, /* @tp_as_sequence@ */
1101 0, /* @tp_as_mapping@ */
1105 0, /* @tp_getattro@ */
1106 0, /* @tp_setattro@ */
1107 0, /* @tp_as_buffer@ */
1108 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1109 Py_TPFLAGS_BASETYPE,
1112 "An elliptic curve. Abstract class.",
1114 0, /* @tp_traverse@ */
1116 eccurve_pyrichcompare, /* @tp_richcompare@ */
1117 0, /* @tp_weaklistoffset@ */
1119 0, /* @tp_iternext@ */
1120 PYMETHODS(eccurve), /* @tp_methods@ */
1121 0, /* @tp_members@ */
1122 PYGETSET(eccurve), /* @tp_getset@ */
1125 0, /* @tp_descr_get@ */
1126 0, /* @tp_descr_set@ */
1127 0, /* @tp_dictoffset@ */
1129 PyType_GenericAlloc, /* @tp_alloc@ */
1130 abstract_pynew, /* @tp_new@ */
1135 static PyObject *ecprimecurve_pynew(PyTypeObject *ty,
1136 PyObject *arg, PyObject *kw)
1138 return (eccurve_pynew(ty, ec_prime, arg, kw));
1141 static const PyTypeObject ecprimecurve_pytype_skel = {
1142 PyVarObject_HEAD_INIT(0, 0) /* Header */
1143 "ECPrimeCurve", /* @tp_name@ */
1144 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1145 0, /* @tp_itemsize@ */
1147 eccurve_pydealloc, /* @tp_dealloc@ */
1149 0, /* @tp_getattr@ */
1150 0, /* @tp_setattr@ */
1151 0, /* @tp_compare@ */
1153 0, /* @tp_as_number@ */
1154 0, /* @tp_as_sequence@ */
1155 0, /* @tp_as_mapping@ */
1159 0, /* @tp_getattro@ */
1160 0, /* @tp_setattro@ */
1161 0, /* @tp_as_buffer@ */
1162 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1163 Py_TPFLAGS_BASETYPE,
1166 "ECPrimeCurve(FIELD, A, B): an elliptic curve over a prime field.\n"
1167 " Use ECPrimeProjCurve instead.",
1169 0, /* @tp_traverse@ */
1171 0, /* @tp_richcompare@ */
1172 0, /* @tp_weaklistoffset@ */
1174 0, /* @tp_iternext@ */
1175 0, /* @tp_methods@ */
1176 0, /* @tp_members@ */
1177 0, /* @tp_getset@ */
1180 0, /* @tp_descr_get@ */
1181 0, /* @tp_descr_set@ */
1182 0, /* @tp_dictoffset@ */
1184 PyType_GenericAlloc, /* @tp_alloc@ */
1185 ecprimecurve_pynew, /* @tp_new@ */
1190 static PyObject *ecprimeprojcurve_pynew(PyTypeObject *ty,
1191 PyObject *arg, PyObject *kw)
1193 return (eccurve_pynew(ty, ec_primeproj, arg, kw));
1196 static const PyTypeObject ecprimeprojcurve_pytype_skel = {
1197 PyVarObject_HEAD_INIT(0, 0) /* Header */
1198 "ECPrimeProjCurve", /* @tp_name@ */
1199 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1200 0, /* @tp_itemsize@ */
1202 eccurve_pydealloc, /* @tp_dealloc@ */
1204 0, /* @tp_getattr@ */
1205 0, /* @tp_setattr@ */
1206 0, /* @tp_compare@ */
1208 0, /* @tp_as_number@ */
1209 0, /* @tp_as_sequence@ */
1210 0, /* @tp_as_mapping@ */
1214 0, /* @tp_getattro@ */
1215 0, /* @tp_setattro@ */
1216 0, /* @tp_as_buffer@ */
1217 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1218 Py_TPFLAGS_BASETYPE,
1221 "ECPrimeProjCurve(FIELD, A, B): an elliptic curve over a prime field\n"
1222 " using projective coordinates.",
1224 0, /* @tp_traverse@ */
1226 0, /* @tp_richcompare@ */
1227 0, /* @tp_weaklistoffset@ */
1229 0, /* @tp_iternext@ */
1230 0, /* @tp_methods@ */
1231 0, /* @tp_members@ */
1232 0, /* @tp_getset@ */
1235 0, /* @tp_descr_get@ */
1236 0, /* @tp_descr_set@ */
1237 0, /* @tp_dictoffset@ */
1239 PyType_GenericAlloc, /* @tp_alloc@ */
1240 ecprimeprojcurve_pynew, /* @tp_new@ */
1245 static PyObject *ecbincurve_pynew(PyTypeObject *ty,
1246 PyObject *arg, PyObject *kw)
1248 return (eccurve_pynew(ty, ec_bin, arg, kw));
1251 static const PyTypeObject ecbincurve_pytype_skel = {
1252 PyVarObject_HEAD_INIT(0, 0) /* Header */
1253 "ECBinCurve", /* @tp_name@ */
1254 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1255 0, /* @tp_itemsize@ */
1257 eccurve_pydealloc, /* @tp_dealloc@ */
1259 0, /* @tp_getattr@ */
1260 0, /* @tp_setattr@ */
1261 0, /* @tp_compare@ */
1263 0, /* @tp_as_number@ */
1264 0, /* @tp_as_sequence@ */
1265 0, /* @tp_as_mapping@ */
1269 0, /* @tp_getattro@ */
1270 0, /* @tp_setattro@ */
1271 0, /* @tp_as_buffer@ */
1272 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1273 Py_TPFLAGS_BASETYPE,
1276 "ECBinCurve(FIELD, A, B): an elliptic curve over a binary field.\n"
1277 " Use ECBinProjCurve instead.",
1279 0, /* @tp_traverse@ */
1281 0, /* @tp_richcompare@ */
1282 0, /* @tp_weaklistoffset@ */
1284 0, /* @tp_iternext@ */
1285 0, /* @tp_methods@ */
1286 0, /* @tp_members@ */
1287 0, /* @tp_getset@ */
1290 0, /* @tp_descr_get@ */
1291 0, /* @tp_descr_set@ */
1292 0, /* @tp_dictoffset@ */
1294 PyType_GenericAlloc, /* @tp_alloc@ */
1295 ecbincurve_pynew, /* @tp_new@ */
1300 static PyObject *ecbinprojcurve_pynew(PyTypeObject *ty,
1301 PyObject *arg, PyObject *kw)
1303 return (eccurve_pynew(ty, ec_binproj, arg, kw));
1306 static const PyTypeObject ecbinprojcurve_pytype_skel = {
1307 PyVarObject_HEAD_INIT(0, 0) /* Header */
1308 "ECBinProjCurve", /* @tp_name@ */
1309 sizeof(eccurve_pyobj), /* @tp_basicsize@ */
1310 0, /* @tp_itemsize@ */
1312 eccurve_pydealloc, /* @tp_dealloc@ */
1314 0, /* @tp_getattr@ */
1315 0, /* @tp_setattr@ */
1316 0, /* @tp_compare@ */
1318 0, /* @tp_as_number@ */
1319 0, /* @tp_as_sequence@ */
1320 0, /* @tp_as_mapping@ */
1324 0, /* @tp_getattro@ */
1325 0, /* @tp_setattro@ */
1326 0, /* @tp_as_buffer@ */
1327 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1328 Py_TPFLAGS_BASETYPE,
1331 "ECBinProjCurve(FIELD, A, B): an elliptic curve over a binary field,\n"
1332 " using projective coordinates.",
1334 0, /* @tp_traverse@ */
1336 0, /* @tp_richcompare@ */
1337 0, /* @tp_weaklistoffset@ */
1339 0, /* @tp_iternext@ */
1340 0, /* @tp_methods@ */
1341 0, /* @tp_members@ */
1342 0, /* @tp_getset@ */
1345 0, /* @tp_descr_get@ */
1346 0, /* @tp_descr_set@ */
1347 0, /* @tp_dictoffset@ */
1349 PyType_GenericAlloc, /* @tp_alloc@ */
1350 ecbinprojcurve_pynew, /* @tp_new@ */
1355 /*----- Curve info --------------------------------------------------------*/
1357 void ecinfo_copy(ec_info *eic, const ec_info *ei)
1359 eic->c = eccurve_copy(ei->c);
1361 EC_COPY(&eic->g, &ei->g);
1362 eic->r = MP_COPY(ei->r);
1363 eic->h = MP_COPY(ei->h);
1366 PyObject *ecinfo_pywrap(ec_info *ei)
1370 o = PyObject_NEW(ecinfo_pyobj, ecinfo_pytype);
1372 o->cobj = eccurve_pywrap(0, o->ei.c);
1374 return ((PyObject *)o);
1377 static void ecinfo_pydealloc(PyObject *me)
1379 ec_info *ei = ECINFO_EI(me);
1383 Py_DECREF(ECINFO_COBJ(me));
1387 static PyObject *ecinfo_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1391 static const char *const kwlist[] = { "curve", "G", "r", "h", 0 };
1392 ecinfo_pyobj *rc = 0;
1394 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!O!O&O&:new", KWLIST,
1395 eccurve_pytype, &e, ecpt_pytype, &g,
1396 convmp, &ei.r, convmp, &ei.h))
1398 if (ECPT_C(g) != ECCURVE_C(e) && !ec_samep(ECPT_C(g), ECCURVE_C(e)))
1399 TYERR("point not from this curve");
1400 ei.c = ECCURVE_C(e);
1402 EC_OUT(ei.c, &ei.g, ECPT_P(g));
1403 rc = (ecinfo_pyobj *)ty->tp_alloc(ty, 0);
1406 Py_INCREF(rc->cobj);
1407 return ((PyObject *)rc);
1415 static PyObject *eimeth_parse(PyObject *me, PyObject *arg)
1422 if (!PyArg_ParseTuple(arg, "s:parse", &p)) goto end;
1424 if (ec_infoparse(&qd, &ei)) VALERR(qd.e);
1425 rc = Py_BuildValue("(Ns)", ecinfo_pywrap(&ei), qd.p);
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 static const char *const 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 const 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 const PyMethodDef ecinfo_pymethods[] = {
1487 #define METHNAME(name) eimeth_##name
1488 KWMETH(check, "I.check([rng = rand]) -> None")
1489 SMTH (parse, "parse(STR) -> (I, REST)")
1494 static const PyTypeObject ecinfo_pytype_skel = {
1495 PyVarObject_HEAD_INIT(0, 0) /* Header */
1496 "ECInfo", /* @tp_name@ */
1497 sizeof(ecinfo_pyobj), /* @tp_basicsize@ */
1498 0, /* @tp_itemsize@ */
1500 ecinfo_pydealloc, /* @tp_dealloc@ */
1502 0, /* @tp_getattr@ */
1503 0, /* @tp_setattr@ */
1504 0, /* @tp_compare@ */
1506 0, /* @tp_as_number@ */
1507 0, /* @tp_as_sequence@ */
1508 0, /* @tp_as_mapping@ */
1512 0, /* @tp_getattro@ */
1513 0, /* @tp_setattro@ */
1514 0, /* @tp_as_buffer@ */
1515 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1516 Py_TPFLAGS_BASETYPE,
1519 "ECInfo(CURVE, G, R, H): elliptic curve domain parameters.",
1521 0, /* @tp_traverse@ */
1523 ecinfo_pyrichcompare, /* @tp_richcompare@ */
1524 0, /* @tp_weaklistoffset@ */
1526 0, /* @tp_iternext@ */
1527 PYMETHODS(ecinfo), /* @tp_methods@ */
1528 0, /* @tp_members@ */
1529 PYGETSET(ecinfo), /* @tp_getset@ */
1532 0, /* @tp_descr_get@ */
1533 0, /* @tp_descr_set@ */
1534 0, /* @tp_dictoffset@ */
1536 PyType_GenericAlloc, /* @tp_alloc@ */
1537 ecinfo_pynew, /* @tp_new@ */
1542 /*----- Setup -------------------------------------------------------------*/
1544 static const struct nameval consts[] = {
1545 CONST(EC_XONLY), CONST(EC_YBIT), CONST(EC_LSB),
1546 CONST(EC_CMPR), CONST(EC_EXPLY), CONST(EC_SORT),
1550 void ec_pyinit(void)
1552 INITTYPE(ecpt, root);
1553 INITTYPE(ecptcurve, ecpt);
1554 INITTYPE(eccurve, type);
1555 INITTYPE(ecprimecurve, eccurve);
1556 INITTYPE(ecprimeprojcurve, ecprimecurve);
1557 INITTYPE(ecbincurve, eccurve);
1558 INITTYPE(ecbinprojcurve, ecbincurve);
1559 INITTYPE(ecinfo, root);
1562 static const char *ec_namefn(const void *p)
1563 { const ecentry *ec = p; return (ec->name); }
1565 static int ec_ixfn(const void *p)
1567 const ecentry *ec = p;
1570 for (i = 0; ectab[i].name; i++)
1571 if (ectab[i].data == ec->data) return (i);
1575 static PyObject *ec_valfn(int i)
1579 ec_infofromdata(&ei, ectab[i].data);
1580 return (ecinfo_pywrap(&ei));
1583 void ec_pyinsert(PyObject *mod)
1585 INSERT("ECPt", ecpt_pytype);
1586 INSERT("ECPtCurve", ecptcurve_pytype);
1587 INSERT("ECCurve", eccurve_pytype);
1588 INSERT("ECPrimeCurve", ecprimecurve_pytype);
1589 INSERT("ECPrimeProjCurve", ecprimeprojcurve_pytype);
1590 INSERT("ECBinCurve", ecbincurve_pytype);
1591 INSERT("ECBinProjCurve", ecbinprojcurve_pytype);
1592 INSERT("ECInfo", ecinfo_pytype);
1593 INSERT("eccurves", make_grouptab(ectab, sizeof(*ectab),
1594 ec_namefn, ec_ixfn, ec_valfn));
1595 setconstants(mod, consts);
1598 /*----- That's all, folks -------------------------------------------------*/