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
*epmeth_ec2osp(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
298 ec_curve
*c
= ECPT_C(me
);
302 char *kwlist
[] = { "flags", 0 };
304 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|i:ectosp", kwlist
, &f
))
306 len
= c
->f
->noctets
* 2 + 1;
307 rc
= bytestring_pywrap(0, len
);
308 p
= PyString_AS_STRING(rc
);
309 buf_init(&b
, p
, len
);
310 EC_OUT(c
, &pp
, ECPT_P(me
));
311 if (ec_ec2osp(c
, f
, &b
, &pp
)) {
312 Py_DECREF(rc
); rc
= 0;
313 VALERR("invalid flags");
316 _PyString_Resize(&rc
, BLEN(&b
));
321 static PyObject
*epget_curve(PyObject
*me
, void *hunoz
)
322 { RETURN_OBJ(ECPT_COBJ(me
)); }
324 static PyObject
*epncget_ix(PyObject
*me
, void *hunoz
)
328 if (EC_ATINF(ECPT_P(me
))) RETURN_NONE
;
330 rc
= mp_pywrap(MP_COPY(p
.x
));
335 static PyObject
*epncget_iy(PyObject
*me
, void *hunoz
)
339 if (EC_ATINF(ECPT_P(me
))) RETURN_NONE
;
341 rc
= mp_pywrap(MP_COPY(p
.y
));
346 static PyObject
*epncget_point(PyObject
*me
, void *hunoz
)
349 static PyObject
*epget_point(PyObject
*me
, void *hunoz
)
353 return (ecpt_pywrapout(ecpt_pytype
, &p
));
356 static PyObject
*epget_x(PyObject
*me
, void *hunoz
)
358 ec_curve
*c
= ECPT_C(me
);
360 PyObject
*fobj
= ECPT_FOBJ(me
);
364 if (EC_ATINF(pp
)) RETURN_NONE
;
366 rc
= fe_pywrap(fobj
, F_IN(c
->f
, MP_NEW
, p
.x
));
371 static PyObject
*epget_y(PyObject
*me
, void *hunoz
)
373 ec_curve
*c
= ECPT_C(me
);
375 PyObject
*fobj
= ECPT_FOBJ(me
);
379 if (EC_ATINF(pp
)) RETURN_NONE
;
381 rc
= fe_pywrap(fobj
, F_IN(c
->f
, MP_NEW
, p
.y
));
386 static PyObject
*epget__x(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
)->x
)));
392 static PyObject
*epget__y(PyObject
*me
, void *hunoz
)
394 if (EC_ATINF(ECPT_P(me
))) RETURN_NONE
;
395 return (fe_pywrap(ECPT_FOBJ(me
), MP_COPY(ECPT_P(me
)->y
)));
398 static PyObject
*epget__z(PyObject
*me
, void *hunoz
)
400 if (EC_ATINF(ECPT_P(me
)) || !ECPT_P(me
)->z
) RETURN_NONE
;
401 return (fe_pywrap(ECPT_FOBJ(me
), MP_COPY(ECPT_P(me
)->z
)));
404 static mp
*coord_in(field
*f
, PyObject
*x
)
407 if (FE_PYCHECK(x
) && FE_F(x
) == f
)
408 return (MP_COPY(FE_X(x
)));
409 else if ((xx
= getmp(x
)) == 0)
412 return (F_IN(f
, xx
, xx
));
415 static int ecptxl_3(ec_curve
*c
, ec
*p
,
416 PyObject
*x
, PyObject
*y
, PyObject
*z
)
420 if (!x
|| !y
|| !z
) TYERR("missing argument");
421 if (!c
) VALERR("internal form with no curve!");
422 if ((p
->x
== coord_in(c
->f
, x
)) == 0 ||
423 (p
->y
== coord_in(c
->f
, y
)) == 0 ||
424 (z
!= Py_None
&& (p
->z
= coord_in(c
->f
, z
))) == 0)
426 if (!p
->z
) p
->z
= MP_COPY(c
->f
->one
); /* just in case */
432 static int ecptxl_2(ec_curve
*c
, ec
*p
, PyObject
*x
, PyObject
*y
)
436 if (!x
|| !y
) TYERR("missing argument");
437 if ((p
->x
= getmp(x
)) == 0 ||
438 (p
->y
= getmp(y
)) == 0)
440 if (c
) EC_IN(c
, p
, p
);
446 static int ecptxl_1(ec_curve
*c
, ec
*p
, PyObject
*x
)
449 PyObject
*y
= 0, *z
= 0, *t
= 0;
456 if (!x
|| x
== Py_None
)
458 else if (ECPT_PYCHECK(x
)) {
461 } else if (PyString_Check(x
)) {
462 if (PyObject_AsReadBuffer(x
, &q
, 0))
466 if (!ec_ptparse(&qd
, p
))
469 } else if (c
&& (xx
= tomp(x
)) != 0) {
470 xx
= F_IN(c
->f
, xx
, xx
);
471 if (!EC_FIND(c
, p
, xx
)) VALERR("not on the curve");
472 } else if (PySequence_Check(x
)) {
474 n
= PySequence_Size(t
);
475 if (n
!= 2 && (n
!= 3 || !c
))
476 TYERR("want sequence of two or three items");
477 if ((x
= PySequence_GetItem(t
, 0)) == 0 ||
478 (y
= PySequence_GetItem(t
, 1)) == 0 ||
479 (n
== 3 && (z
= PySequence_GetItem(t
, 2)) == 0))
481 rc
= (n
== 2) ?
ecptxl_2(c
, p
, x
, y
) : ecptxl_3(c
, p
, x
, y
, z
);
483 TYERR("can't convert to curve point");
487 if (c
) EC_IN(c
, p
, p
);
491 Py_XDECREF(x
); Py_XDECREF(y
); Py_XDECREF(z
); Py_XDECREF(t
);
496 static int ecptxl(ec_curve
*c
, ec
*p
, PyObject
*x
, PyObject
*y
, PyObject
*z
)
499 return (ecptxl_3(c
, p
, x
, y
, z
));
501 return (ecptxl_2(c
, p
, x
, y
));
503 return (ecptxl_1(c
, p
, x
));
506 static PyObject
*ecptnc_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
508 PyObject
*x
= 0, *y
= 0, *z
= 0;
510 char *kwlist
[] = { "x", "y", 0 };
512 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|OO:new", kwlist
, &x
, &y
) ||
513 ecptxl(0, &p
, x
, y
, z
))
515 return (ecpt_pywrapout(ty
, &p
));
521 static PyObject
*ecpt_pyint(PyObject
*me
)
526 if (EC_ATINF(ECPT_P(me
))) VALERR("point at infinity");
528 if (!mp_tolong_checked(p
.x
, &l
, 0)) rc
= PyInt_FromLong(l
);
529 else rc
= mp_topylong(p
.x
);
535 static PyObject
*ecpt_pylong(PyObject
*me
)
539 if (EC_ATINF(ECPT_P(me
))) VALERR("point at infinity");
541 rc
= mp_topylong(p
.x
);
547 static PyObject
*ecpt_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
549 PyObject
*x
= 0, *y
= 0, *z
= 0;
551 char *kwlist
[] = { "x", "y", "z", 0 };
553 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|OOO:new", kwlist
,
555 ecptxl(ECCURVE_C(ty
), &p
, x
, y
, z
))
557 return (ecpt_pywrap((PyObject
*)ty
, &p
));
563 static PyGetSetDef ecptnc_pygetset
[] = {
564 #define GETSETNAME(op, name) epnc##op##_##name
565 GET (ix
, "P.ix -> integer x coordinate of P")
566 GET (iy
, "P.iy -> integer y coordinate of P")
567 GET (point
, "P.point -> standalone curve point (no-op)")
572 static PyMethodDef ecptnc_pymethods
[] = {
573 #define METHNAME(func) epmeth_##func
574 METH (tobuf
, "X.tobuf() -> BIN")
579 static PyNumberMethods ecpt_pynumber
= {
581 0, /* @nb_subtract@ */
582 0, /* @nb_multiply@ */
584 0, /* @nb_remainder@ */
587 0, /* @nb_negative@ */
588 0, /* @nb_positive@ */
589 0, /* @nb_absolute@ */
590 ecpt_pynonzerop
, /* @nb_nonzero@ */
598 ecpt_pyint
, /* @nb_int@ */
599 ecpt_pylong
, /* @nb_long@ */
604 0, /* @nb_inplace_add@ */
605 0, /* @nb_inplace_subtract@ */
606 0, /* @nb_inplace_multiply@ */
607 0, /* @nb_inplace_divide@ */
608 0, /* @nb_inplace_remainder@ */
609 0, /* @nb_inplace_power@ */
610 0, /* @nb_inplace_lshift@ */
611 0, /* @nb_inplace_rshift@ */
612 0, /* @nb_inplace_and@ */
613 0, /* @nb_inplace_xor@ */
614 0, /* @nb_inplace_or@ */
616 0, /* @nb_floor_divide@ */
617 0, /* @nb_true_divide@ */
618 0, /* @nb_inplace_floor_divide@ */
619 0, /* @nb_inplace_true_divide@ */
622 static PyTypeObject ecpt_pytype_skel
= {
623 PyObject_HEAD_INIT(0) 0, /* Header */
624 "ECPt", /* @tp_name@ */
625 sizeof(ecpt_pyobj
), /* @tp_basicsize@ */
626 0, /* @tp_itemsize@ */
628 ecpt_pydealloc
, /* @tp_dealloc@ */
630 0, /* @tp_getattr@ */
631 0, /* @tp_setattr@ */
632 0, /* @tp_compare@ */
634 &ecpt_pynumber
, /* @tp_as_number@ */
635 0, /* @tp_as_sequence@ */
636 0, /* @tp_as_mapping@ */
637 ecpt_pyhash
, /* @tp_hash@ */
640 0, /* @tp_getattro@ */
641 0, /* @tp_setattro@ */
642 0, /* @tp_as_buffer@ */
643 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
644 Py_TPFLAGS_CHECKTYPES
|
648 "Elliptic curve points, not associated with any curve.",
650 0, /* @tp_traverse@ */
652 ecpt_pyrichcompare
, /* @tp_richcompare@ */
653 0, /* @tp_weaklistoffset@ */
655 0, /* @tp_iternext@ */
656 ecptnc_pymethods
, /* @tp_methods@ */
657 0, /* @tp_members@ */
658 ecptnc_pygetset
, /* @tp_getset@ */
661 0, /* @tp_descr_get@ */
662 0, /* @tp_descr_set@ */
663 0, /* @tp_dictoffset@ */
665 PyType_GenericAlloc
, /* @tp_alloc@ */
666 ecptnc_pynew
, /* @tp_new@ */
671 static PyGetSetDef ecpt_pygetset
[] = {
672 #define GETSETNAME(op, name) ep##op##_##name
673 GET (curve
, "P.curve -> elliptic curve containing P")
674 GET (point
, "P.point -> standalone curve point")
675 GET (x
, "P.x -> Cartesian x coordinate of P")
676 GET (y
, "P.y -> Cartesian y coordinate of P")
677 GET (_x
, "P._x -> internal x coordinate of P")
678 GET (_y
, "P._y -> internal y coordinate of P")
679 GET (_z
, "P._z -> internal z coordinate of P, or None")
684 static PyMethodDef ecpt_pymethods
[] = {
685 #define METHNAME(func) epmeth_##func
686 METH (toraw
, "X.toraw() -> BIN")
687 KWMETH(ec2osp
, "X.ec2osp([flags = EC_EXPLY]) -> BIN")
688 METH (dbl
, "X.dbl() -> X + X")
689 METH (oncurvep
, "X.oncurvep() -> BOOL")
694 static PyNumberMethods ecptcurve_pynumber
= {
695 ecpt_pyadd
, /* @nb_add@ */
696 ecpt_pysub
, /* @nb_subtract@ */
697 ecpt_pymul
, /* @nb_multiply@ */
699 0, /* @nb_remainder@ */
702 ecpt_pyneg
, /* @nb_negative@ */
703 ecpt_pyid
, /* @nb_positive@ */
704 0, /* @nb_absolute@ */
705 0, /* @nb_nonzero@ */
719 0, /* @nb_inplace_add@ */
720 0, /* @nb_inplace_subtract@ */
721 0, /* @nb_inplace_multiply@ */
722 0, /* @nb_inplace_divide@ */
723 0, /* @nb_inplace_remainder@ */
724 0, /* @nb_inplace_power@ */
725 0, /* @nb_inplace_lshift@ */
726 0, /* @nb_inplace_rshift@ */
727 0, /* @nb_inplace_and@ */
728 0, /* @nb_inplace_xor@ */
729 0, /* @nb_inplace_or@ */
731 0, /* @nb_floor_divide@ */
732 0, /* @nb_true_divide@ */
733 0, /* @nb_inplace_floor_divide@ */
734 0, /* @nb_inplace_true_divide@ */
737 static PyTypeObject ecptcurve_pytype_skel
= {
738 PyObject_HEAD_INIT(0) 0, /* Header */
739 "ECPtCurve", /* @tp_name@ */
740 sizeof(ecpt_pyobj
), /* @tp_basicsize@ */
741 0, /* @tp_itemsize@ */
743 ecpt_pydealloc
, /* @tp_dealloc@ */
745 0, /* @tp_getattr@ */
746 0, /* @tp_setattr@ */
747 0, /* @tp_compare@ */
749 &ecptcurve_pynumber
, /* @tp_as_number@ */
750 0, /* @tp_as_sequence@ */
751 0, /* @tp_as_mapping@ */
755 0, /* @tp_getattro@ */
756 0, /* @tp_setattro@ */
757 0, /* @tp_as_buffer@ */
758 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
759 Py_TPFLAGS_CHECKTYPES
|
763 "Elliptic curve points; abstract base class for points on given curves.",
765 0, /* @tp_traverse@ */
767 0, /* @tp_richcompare@ */
768 0, /* @tp_weaklistoffset@ */
770 0, /* @tp_iternext@ */
771 ecpt_pymethods
, /* @tp_methods@ */
772 0, /* @tp_members@ */
773 ecpt_pygetset
, /* @tp_getset@ */
776 0, /* @tp_descr_get@ */
777 0, /* @tp_descr_set@ */
778 0, /* @tp_dictoffset@ */
780 PyType_GenericAlloc
, /* @tp_alloc@ */
781 abstract_pynew
, /* @tp_new@ */
786 /*----- Elliptic curves themselves ----------------------------------------*/
788 static PyObject
*eccurve_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
790 int b
= ec_samep(ECCURVE_C(x
), ECCURVE_C(y
));
794 default: TYERR("can't order elliptic curves");
801 static PyObject
*ecmmul_id(PyObject
*me
)
802 { ec p
= EC_INIT
; return (ecpt_pywrap(me
, &p
)); }
804 static int ecmmul_fill(void *pp
, PyObject
*me
, PyObject
*x
, PyObject
*m
)
806 ec_mulfactor
*f
= pp
;
809 if (getecpt(ECCURVE_C(me
), &f
->base
, x
) ||
810 (f
->exp
= getmp(m
)) == 0)
815 static PyObject
*ecmmul_exp(PyObject
*me
, void *pp
, int n
)
818 ec_immul(ECCURVE_C(me
), &p
, pp
, n
);
819 return (ecpt_pywrap(me
, &p
));
822 static void ecmmul_drop(void *pp
)
824 ec_mulfactor
*f
= pp
;
825 EC_DESTROY(&f
->base
);
829 static PyObject
*ecmeth_mmul(PyObject
*me
, PyObject
*arg
)
831 return (mexp_common(me
, arg
, sizeof(ec_mulfactor
),
832 ecmmul_id
, ecmmul_fill
, ecmmul_exp
, ecmmul_drop
));
835 static PyObject
*meth__ECPtCurve_fromraw(PyObject
*me
, PyObject
*arg
)
844 if (!PyArg_ParseTuple(arg
, "Os#:fromraw", &me
, &p
, &len
))
846 buf_init(&b
, p
, len
);
848 if (ec_getraw(cc
, &b
, &pp
))
851 rc
= Py_BuildValue("(NN)", ecpt_pywrap(me
, &pp
), bytestring_pywrapbuf(&b
));
856 static PyObject
*meth__ECPtCurve_os2ecp(PyObject
*me
,
857 PyObject
*arg
, PyObject
*kw
)
864 int f
= EC_XONLY
| EC_LSB
| EC_SORT
| EC_EXPLY
;
866 char *kwlist
[] = { "buf", "flags", 0 };
868 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "Os#|f:os2ecp", kwlist
,
871 buf_init(&b
, p
, len
);
873 if (ec_os2ecp(cc
, f
, &b
, &pp
)) VALERR("bad point");
875 rc
= Py_BuildValue("(NN)", ecpt_pywrap(me
, &pp
), bytestring_pywrapbuf(&b
));
880 static PyObject
*meth__ECPt_frombuf(PyObject
*me
, PyObject
*arg
)
888 if (!PyArg_ParseTuple(arg
, "Os#:frombuf", &me
, &p
, &sz
)) goto end
;
890 if (buf_getec(&b
, &pp
)) VALERR("malformed data");
891 rc
= Py_BuildValue("(NN)", ecpt_pywrapout(me
, &pp
),
892 bytestring_pywrapbuf(&b
));
897 static PyObject
*meth__ECPt_parse(PyObject
*me
, PyObject
*arg
)
904 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
)) goto end
;
907 if (!ec_ptparse(&qd
, &pp
)) VALERR(qd
.e
);
908 rc
= Py_BuildValue("(Ns)", ecpt_pywrapout(me
, &pp
), qd
.p
);
913 static void eccurve_pydealloc(PyObject
*me
)
915 ec_destroycurve(ECCURVE_C(me
));
916 Py_DECREF(ECCURVE_FOBJ(me
));
917 PyType_Type
.tp_dealloc(me
);
920 static PyObject
*ecmeth_find(PyObject
*me
, PyObject
*arg
)
927 if (!PyArg_ParseTuple(arg
, "O:find", &x
)) goto end
;
928 if (FIELD_PYCHECK(x
) && FE_F(x
) == ECCURVE_C(me
)->f
)
929 xx
= MP_COPY(FE_X(x
));
930 else if ((xx
= getmp(x
)) == 0)
933 xx
= F_IN(ECCURVE_C(me
)->f
, xx
, xx
);
934 if (EC_FIND(ECCURVE_C(me
), &p
, xx
) == 0)
935 VALERR("not on the curve");
936 rc
= ecpt_pywrap(me
, &p
);
942 static PyObject
*ecmeth_rand(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
944 char *kwlist
[] = { "rng", 0 };
945 grand
*r
= &rand_global
;
948 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:rand", kwlist
,
951 ec_rand(ECCURVE_C(me
), &p
, r
);
952 EC_IN(ECCURVE_C(me
), &p
, &p
);
953 return (ecpt_pywrap(me
, &p
));
956 static PyObject
*eccurve_dopywrap(PyTypeObject
*ty
,
957 PyObject
*fobj
, ec_curve
*c
)
959 eccurve_pyobj
*cobj
= newtype(ty
, 0, c
->ops
->name
);
962 cobj
->ty
.ht_type
.tp_basicsize
= sizeof(ecpt_pyobj
);
963 cobj
->ty
.ht_type
.tp_base
= ecptcurve_pytype
;
964 Py_INCREF(ecptcurve_pytype
);
965 cobj
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
966 Py_TPFLAGS_BASETYPE
|
967 Py_TPFLAGS_CHECKTYPES
|
968 Py_TPFLAGS_HEAPTYPE
);
969 cobj
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
970 cobj
->ty
.ht_type
.tp_free
= 0;
971 cobj
->ty
.ht_type
.tp_new
= ecpt_pynew
;
972 typeready(&cobj
->ty
.ht_type
);
973 return ((PyObject
*)cobj
);
976 PyObject
*eccurve_pywrap(PyObject
*fobj
, ec_curve
*c
)
981 fobj
= field_pywrap(c
->f
);
984 assert(FIELD_F(fobj
) == c
->f
);
985 if (strcmp(EC_NAME(c
), "prime") == 0)
986 ty
= ecprimecurve_pytype
;
987 else if (strcmp(EC_NAME(c
), "primeproj") == 0)
988 ty
= ecprimeprojcurve_pytype
;
989 else if (strcmp(EC_NAME(c
), "bin") == 0)
990 ty
= ecbincurve_pytype
;
991 else if (strcmp(EC_NAME(c
), "binproj") == 0)
992 ty
= ecbinprojcurve_pytype
;
995 return (eccurve_dopywrap(ty
, fobj
, c
));
998 static PyObject
*eccurve_pynew(PyTypeObject
*ty
,
999 ec_curve
*(*make
)(field
*, mp
*, mp
*),
1000 PyObject
*arg
, PyObject
*kw
)
1004 char *kwlist
[] = { "field", "a", "b", 0 };
1005 mp
*aa
= 0, *bb
= 0;
1007 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!O&O&", kwlist
,
1008 field_pytype
, &fobj
,
1009 convmp
, &aa
, convmp
, &bb
))
1012 cobj
= eccurve_dopywrap(ty
, fobj
, make(FIELD_F(fobj
), aa
, bb
));
1014 if (aa
) MP_DROP(aa
);
1015 if (bb
) MP_DROP(bb
);
1019 static PyObject
*meth__ECCurve_parse(PyObject
*me
, PyObject
*arg
)
1026 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
))
1030 if ((c
= ec_curveparse(&qd
)) == 0)
1032 rc
= eccurve_pywrap(0, c
);
1037 static PyObject
*ecget_name(PyObject
*me
, void *hunoz
)
1038 { return (PyString_FromString(EC_NAME(ECCURVE_C(me
)))); }
1040 static PyObject
*ecget_a(PyObject
*me
, void *hunoz
)
1041 { return (fe_pywrap(ECCURVE_FOBJ(me
), MP_COPY(ECCURVE_C(me
)->a
))); }
1043 static PyObject
*ecget_b(PyObject
*me
, void *hunoz
)
1044 { return (fe_pywrap(ECCURVE_FOBJ(me
), MP_COPY(ECCURVE_C(me
)->b
))); }
1046 static PyObject
*ecget_field(PyObject
*me
, void *hunoz
)
1047 { RETURN_OBJ(ECCURVE_FOBJ(me
)); }
1049 static PyObject
*ecget_inf(PyObject
*me
, void *hunoz
)
1050 { ec inf
= EC_INIT
; return (ecpt_pywrap(me
, &inf
)); }
1052 static PyGetSetDef eccurve_pygetset
[] = {
1053 #define GETSETNAME(op, name) ec##op##_##name
1054 GET (name
, "E.name -> name of this kind of curve")
1055 GET (a
, "E.a -> first parameter of curve")
1056 GET (b
, "E.b -> second parameter of curve")
1057 GET (field
, "E.field -> finite field containing this curve")
1058 GET (inf
, "E.inf -> point at infinity of this curve")
1063 static PyMethodDef eccurve_pymethods
[] = {
1064 #define METHNAME(name) ecmeth_##name
1066 E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...")
1067 METH (find
, "E.find(X) -> P")
1068 KWMETH(rand
, "E.rand(rng = rand) ->P")
1073 static PyTypeObject eccurve_pytype_skel
= {
1074 PyObject_HEAD_INIT(0) 0, /* Header */
1075 "ECCurve", /* @tp_name@ */
1076 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1077 0, /* @tp_itemsize@ */
1079 eccurve_pydealloc
, /* @tp_dealloc@ */
1081 0, /* @tp_getattr@ */
1082 0, /* @tp_setattr@ */
1083 0, /* @tp_compare@ */
1085 0, /* @tp_as_number@ */
1086 0, /* @tp_as_sequence@ */
1087 0, /* @tp_as_mapping@ */
1091 0, /* @tp_getattro@ */
1092 0, /* @tp_setattro@ */
1093 0, /* @tp_as_buffer@ */
1094 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1095 Py_TPFLAGS_BASETYPE
,
1098 "An elliptic curve. Abstract class.",
1100 0, /* @tp_traverse@ */
1102 eccurve_pyrichcompare
, /* @tp_richcompare@ */
1103 0, /* @tp_weaklistoffset@ */
1105 0, /* @tp_iternext@ */
1106 eccurve_pymethods
, /* @tp_methods@ */
1107 0, /* @tp_members@ */
1108 eccurve_pygetset
, /* @tp_getset@ */
1111 0, /* @tp_descr_get@ */
1112 0, /* @tp_descr_set@ */
1113 0, /* @tp_dictoffset@ */
1115 PyType_GenericAlloc
, /* @tp_alloc@ */
1116 abstract_pynew
, /* @tp_new@ */
1121 static PyObject
*ecprimecurve_pynew(PyTypeObject
*ty
,
1122 PyObject
*arg
, PyObject
*kw
)
1124 return (eccurve_pynew(ty
, ec_prime
, arg
, kw
));
1127 static PyTypeObject ecprimecurve_pytype_skel
= {
1128 PyObject_HEAD_INIT(0) 0, /* Header */
1129 "ECPrimeCurve", /* @tp_name@ */
1130 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1131 0, /* @tp_itemsize@ */
1133 eccurve_pydealloc
, /* @tp_dealloc@ */
1135 0, /* @tp_getattr@ */
1136 0, /* @tp_setattr@ */
1137 0, /* @tp_compare@ */
1139 0, /* @tp_as_number@ */
1140 0, /* @tp_as_sequence@ */
1141 0, /* @tp_as_mapping@ */
1145 0, /* @tp_getattro@ */
1146 0, /* @tp_setattro@ */
1147 0, /* @tp_as_buffer@ */
1148 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1149 Py_TPFLAGS_BASETYPE
,
1152 "An elliptic curve over a prime field. Use ecprimeprojcurve.",
1154 0, /* @tp_traverse@ */
1156 eccurve_pyrichcompare
, /* @tp_richcompare@ */
1157 0, /* @tp_weaklistoffset@ */
1159 0, /* @tp_iternext@ */
1160 0, /* @tp_methods@ */
1161 0, /* @tp_members@ */
1162 0, /* @tp_getset@ */
1165 0, /* @tp_descr_get@ */
1166 0, /* @tp_descr_set@ */
1167 0, /* @tp_dictoffset@ */
1169 PyType_GenericAlloc
, /* @tp_alloc@ */
1170 ecprimecurve_pynew
, /* @tp_new@ */
1175 static PyObject
*ecprimeprojcurve_pynew(PyTypeObject
*ty
,
1176 PyObject
*arg
, PyObject
*kw
)
1178 return (eccurve_pynew(ty
, ec_primeproj
, arg
, kw
));
1181 static PyTypeObject ecprimeprojcurve_pytype_skel
= {
1182 PyObject_HEAD_INIT(0) 0, /* Header */
1183 "ECPrimeProjCurve", /* @tp_name@ */
1184 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1185 0, /* @tp_itemsize@ */
1187 eccurve_pydealloc
, /* @tp_dealloc@ */
1189 0, /* @tp_getattr@ */
1190 0, /* @tp_setattr@ */
1191 0, /* @tp_compare@ */
1193 0, /* @tp_as_number@ */
1194 0, /* @tp_as_sequence@ */
1195 0, /* @tp_as_mapping@ */
1199 0, /* @tp_getattro@ */
1200 0, /* @tp_setattro@ */
1201 0, /* @tp_as_buffer@ */
1202 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1203 Py_TPFLAGS_BASETYPE
,
1206 "An elliptic curve over a prime field, using projective coordinates.",
1208 0, /* @tp_traverse@ */
1210 eccurve_pyrichcompare
, /* @tp_richcompare@ */
1211 0, /* @tp_weaklistoffset@ */
1213 0, /* @tp_iternext@ */
1214 0, /* @tp_methods@ */
1215 0, /* @tp_members@ */
1216 0, /* @tp_getset@ */
1219 0, /* @tp_descr_get@ */
1220 0, /* @tp_descr_set@ */
1221 0, /* @tp_dictoffset@ */
1223 PyType_GenericAlloc
, /* @tp_alloc@ */
1224 ecprimeprojcurve_pynew
, /* @tp_new@ */
1229 static PyObject
*ecbincurve_pynew(PyTypeObject
*ty
,
1230 PyObject
*arg
, PyObject
*kw
)
1232 return (eccurve_pynew(ty
, ec_bin
, arg
, kw
));
1235 static PyTypeObject ecbincurve_pytype_skel
= {
1236 PyObject_HEAD_INIT(0) 0, /* Header */
1237 "ECBinCurve", /* @tp_name@ */
1238 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1239 0, /* @tp_itemsize@ */
1241 eccurve_pydealloc
, /* @tp_dealloc@ */
1243 0, /* @tp_getattr@ */
1244 0, /* @tp_setattr@ */
1245 0, /* @tp_compare@ */
1247 0, /* @tp_as_number@ */
1248 0, /* @tp_as_sequence@ */
1249 0, /* @tp_as_mapping@ */
1253 0, /* @tp_getattro@ */
1254 0, /* @tp_setattro@ */
1255 0, /* @tp_as_buffer@ */
1256 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1257 Py_TPFLAGS_BASETYPE
,
1260 "An elliptic curve over a binary field. Use ecbinprojcurve.",
1262 0, /* @tp_traverse@ */
1264 eccurve_pyrichcompare
, /* @tp_richcompare@ */
1265 0, /* @tp_weaklistoffset@ */
1267 0, /* @tp_iternext@ */
1268 0, /* @tp_methods@ */
1269 0, /* @tp_members@ */
1270 0, /* @tp_getset@ */
1273 0, /* @tp_descr_get@ */
1274 0, /* @tp_descr_set@ */
1275 0, /* @tp_dictoffset@ */
1277 PyType_GenericAlloc
, /* @tp_alloc@ */
1278 ecbincurve_pynew
, /* @tp_new@ */
1283 static PyObject
*ecbinprojcurve_pynew(PyTypeObject
*ty
,
1284 PyObject
*arg
, PyObject
*kw
)
1286 return (eccurve_pynew(ty
, ec_binproj
, arg
, kw
));
1289 static PyTypeObject ecbinprojcurve_pytype_skel
= {
1290 PyObject_HEAD_INIT(0) 0, /* Header */
1291 "ECBinProjCurve", /* @tp_name@ */
1292 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1293 0, /* @tp_itemsize@ */
1295 eccurve_pydealloc
, /* @tp_dealloc@ */
1297 0, /* @tp_getattr@ */
1298 0, /* @tp_setattr@ */
1299 0, /* @tp_compare@ */
1301 0, /* @tp_as_number@ */
1302 0, /* @tp_as_sequence@ */
1303 0, /* @tp_as_mapping@ */
1307 0, /* @tp_getattro@ */
1308 0, /* @tp_setattro@ */
1309 0, /* @tp_as_buffer@ */
1310 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1311 Py_TPFLAGS_BASETYPE
,
1314 "An elliptic curve over a binary field, using projective coordinates.",
1316 0, /* @tp_traverse@ */
1318 eccurve_pyrichcompare
, /* @tp_richcompare@ */
1319 0, /* @tp_weaklistoffset@ */
1321 0, /* @tp_iternext@ */
1322 0, /* @tp_methods@ */
1323 0, /* @tp_members@ */
1324 0, /* @tp_getset@ */
1327 0, /* @tp_descr_get@ */
1328 0, /* @tp_descr_set@ */
1329 0, /* @tp_dictoffset@ */
1331 PyType_GenericAlloc
, /* @tp_alloc@ */
1332 ecbinprojcurve_pynew
, /* @tp_new@ */
1337 /*----- Curve info --------------------------------------------------------*/
1339 static int ncurves
= -1;
1341 void ecinfo_copy(ec_info
*eic
, const ec_info
*ei
)
1343 eic
->c
= eccurve_copy(ei
->c
);
1345 EC_COPY(&eic
->g
, &ei
->g
);
1346 eic
->r
= MP_COPY(ei
->r
);
1347 eic
->h
= MP_COPY(ei
->h
);
1350 PyObject
*ecinfo_pywrap(ec_info
*ei
)
1354 o
= PyObject_NEW(ecinfo_pyobj
, ecinfo_pytype
);
1356 o
->cobj
= eccurve_pywrap(0, o
->ei
.c
);
1358 return ((PyObject
*)o
);
1361 static void ecinfo_pydealloc(PyObject
*me
)
1363 ec_info
*ei
= ECINFO_EI(me
);
1367 Py_DECREF(ECINFO_COBJ(me
));
1371 static PyObject
*ecinfo_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1375 char *kwlist
[] = { "curve", "G", "r", "h", 0 };
1376 ecinfo_pyobj
*rc
= 0;
1378 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!O!O&O&:new", kwlist
,
1379 eccurve_pytype
, &e
, ecpt_pytype
, &g
,
1380 convmp
, &ei
.r
, convmp
, &ei
.h
))
1382 if (ECPT_C(g
) != ECCURVE_C(e
) && !ec_samep(ECPT_C(g
), ECCURVE_C(e
)))
1383 TYERR("point not from this curve");
1384 ei
.c
= ECCURVE_C(e
);
1386 EC_OUT(ei
.c
, &ei
.g
, ECPT_P(g
));
1387 rc
= (ecinfo_pyobj
*)ty
->tp_alloc(ty
, 0);
1390 Py_INCREF(rc
->cobj
);
1391 return ((PyObject
*)rc
);
1399 static PyObject
*meth__ECInfo_parse(PyObject
*me
, PyObject
*arg
)
1406 if (!PyArg_ParseTuple(arg
, "Os:parse", &me
, &p
))
1410 if (ec_infoparse(&qd
, &ei
))
1412 rc
= Py_BuildValue("(Ns)", ecinfo_pywrap(&ei
), qd
.p
);
1417 static PyObject
*meth__ECInfo__curven(PyObject
*me
, PyObject
*arg
)
1423 if (!PyArg_ParseTuple(arg
, "Oi:_curven", &me
, &i
)) goto end
;
1424 if (i
< 0 || i
>= ncurves
) VALERR("curve index out of range");
1425 ec_infofromdata(&ei
, ectab
[i
].data
);
1426 rc
= ecinfo_pywrap(&ei
);
1431 static PyObject
*ecinfo_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
1433 int b
= ec_sameinfop(ECINFO_EI(x
), ECINFO_EI(y
));
1437 default: TYERR("can't order elliptic curve infos");
1439 return (getbool(b
));
1444 static PyObject
*eimeth_check(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1446 char *kwlist
[] = { "rng", 0 };
1447 grand
*r
= &rand_global
;
1450 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:check", kwlist
,
1453 if ((p
= ec_checkinfo(ECINFO_EI(me
), r
)) != 0)
1460 static PyObject
*eiget_curve(PyObject
*me
, void *hunoz
)
1461 { RETURN_OBJ(ECINFO_COBJ(me
)); }
1463 static PyObject
*eiget_G(PyObject
*me
, void *hunoz
)
1465 ec_info
*ei
= ECINFO_EI(me
);
1467 EC_IN(ei
->c
, &p
, &ei
->g
);
1468 return (ecpt_pywrap(ECINFO_COBJ(me
), &p
));
1471 static PyObject
*eiget_r(PyObject
*me
, void *hunoz
)
1472 { return (mp_pywrap(MP_COPY(ECINFO_EI(me
)->r
))); }
1474 static PyObject
*eiget_h(PyObject
*me
, void *hunoz
)
1475 { return (mp_pywrap(MP_COPY(ECINFO_EI(me
)->h
))); }
1477 static PyGetSetDef ecinfo_pygetset
[] = {
1478 #define GETSETNAME(op, name) ei##op##_##name
1479 GET (curve
, "I.curve -> the elliptic curve")
1480 GET (G
, "I.G -> generator point for the group")
1481 GET (r
, "I.r -> order of the group (and hence of G")
1482 GET (h
, "I.h -> cofactor of the group")
1487 static PyMethodDef ecinfo_pymethods
[] = {
1488 #define METHNAME(name) eimeth_##name
1489 KWMETH(check
, "I.check() -> None")
1494 static PyTypeObject ecinfo_pytype_skel
= {
1495 PyObject_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 "Elliptic curve domain parameters.",
1521 0, /* @tp_traverse@ */
1523 ecinfo_pyrichcompare
, /* @tp_richcompare@ */
1524 0, /* @tp_weaklistoffset@ */
1526 0, /* @tp_iternext@ */
1527 ecinfo_pymethods
, /* @tp_methods@ */
1528 0, /* @tp_members@ */
1529 ecinfo_pygetset
, /* @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 PyMethodDef methods
[] = {
1545 #define METHNAME(func) meth_##func
1546 METH (_ECPt_frombuf
, "frombuf(E, STR) -> (P, REST)")
1547 METH (_ECPtCurve_fromraw
, "fromraw(E, STR) -> (P, REST)")
1548 KWMETH(_ECPtCurve_os2ecp
, "os2ecp(E, STR, [flags = ...]) -> (P, REST)")
1549 METH (_ECPt_parse
, "parse(E, STR) -> (P, REST)")
1550 METH (_ECCurve_parse
, "parse(STR) -> (E, REST)")
1551 METH (_ECInfo_parse
, "parse(STR) -> (I, REST)")
1552 METH (_ECInfo__curven
, "_curven(N) -> I")
1557 void ec_pyinit(void)
1559 INITTYPE(ecpt
, root
);
1560 INITTYPE(ecptcurve
, ecpt
);
1561 INITTYPE(eccurve
, type
);
1562 INITTYPE(ecprimecurve
, eccurve
);
1563 INITTYPE(ecprimeprojcurve
, ecprimecurve
);
1564 INITTYPE(ecbincurve
, eccurve
);
1565 INITTYPE(ecbinprojcurve
, ecbincurve
);
1566 INITTYPE(ecinfo
, root
);
1567 addmethods(methods
);
1570 static PyObject
*namedcurves(void)
1577 for (i
= 0; ectab
[i
].name
; i
++) {
1579 for (j
= 0; j
< i
; j
++) {
1580 if (ectab
[i
].data
== ectab
[j
].data
) {
1581 c
= PyDict_GetItemString(d
, (/*unconst*/ char *)ectab
[j
].name
);
1586 c
= PyInt_FromLong(i
);
1588 PyDict_SetItemString(d
, (/*unconst*/ char *)p
, c
);
1595 void ec_pyinsert(PyObject
*mod
)
1597 INSERT("ECPt", ecpt_pytype
);
1598 INSERT("ECPtCurve", ecptcurve_pytype
);
1599 INSERT("ECCurve", eccurve_pytype
);
1600 INSERT("ECPrimeCurve", ecprimecurve_pytype
);
1601 INSERT("ECPrimeProjCurve", ecprimeprojcurve_pytype
);
1602 INSERT("ECBinCurve", ecbincurve_pytype
);
1603 INSERT("ECBinProjCurve", ecbinprojcurve_pytype
);
1604 INSERT("ECInfo", ecinfo_pytype
);
1605 INSERT("_eccurves", namedcurves());
1608 /*----- That's all, folks -------------------------------------------------*/