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
)
339 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
341 qd_skipspc(&qd
); paren
= qd_delim(&qd
, '(');
342 if (!ec_ptparse(&qd
, &pp
)) VALERR(qd
.e
);
343 qd_skipspc(&qd
); if (paren
&& !qd_delim(&qd
, ')'))
344 { EC_DESTROY(&pp
); VALERR("missing `)'"); }
345 rc
= Py_BuildValue("(Ns)", ecpt_pywrapout(me
, &pp
), qd
.p
);
350 static PyObject
*epmeth_fromraw(PyObject
*me
, PyObject
*arg
)
358 if (!PyArg_ParseTuple(arg
, "O&:fromraw", convbin
, &in
)) return (0);
359 buf_init(&b
, (/*unconst*/ void *)in
.p
, in
.sz
);
361 if (ec_getraw(cc
, &b
, &pp
))
364 rc
= Py_BuildValue("(NN)", ecpt_pywrap(me
, &pp
), bytestring_pywrapbuf(&b
));
369 static PyObject
*epmeth_os2ecp(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
375 unsigned f
= EC_XONLY
| EC_LSB
| EC_SORT
| EC_EXPLY
;
377 static const char *const kwlist
[] = { "buf", "flags", 0 };
379 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:os2ecp", KWLIST
,
380 convbin
, &in
, convuint
, &f
))
382 buf_init(&b
, (/*unconst*/ void *)in
.p
, in
.sz
);
384 if (ec_os2ecp(cc
, f
, &b
, &pp
)) VALERR("bad point");
386 rc
= Py_BuildValue("(NN)", ecpt_pywrap(me
, &pp
), bytestring_pywrapbuf(&b
));
391 static PyObject
*epncget_ix(PyObject
*me
, void *hunoz
)
395 if (EC_ATINF(ECPT_P(me
))) RETURN_NONE
;
397 rc
= mp_pywrap(MP_COPY(p
.x
));
402 static PyObject
*epncget_iy(PyObject
*me
, void *hunoz
)
406 if (EC_ATINF(ECPT_P(me
))) RETURN_NONE
;
408 rc
= mp_pywrap(MP_COPY(p
.y
));
413 static PyObject
*epncget_point(PyObject
*me
, void *hunoz
)
416 static PyObject
*epget_point(PyObject
*me
, void *hunoz
)
420 return (ecpt_pywrapout(ecpt_pytype
, &p
));
423 static PyObject
*epget_x(PyObject
*me
, void *hunoz
)
425 ec_curve
*c
= ECPT_C(me
);
427 PyObject
*fobj
= ECPT_FOBJ(me
);
431 if (EC_ATINF(pp
)) RETURN_NONE
;
433 rc
= fe_pywrap(fobj
, F_IN(c
->f
, MP_NEW
, p
.x
));
438 static PyObject
*epget_y(PyObject
*me
, void *hunoz
)
440 ec_curve
*c
= ECPT_C(me
);
442 PyObject
*fobj
= ECPT_FOBJ(me
);
446 if (EC_ATINF(pp
)) RETURN_NONE
;
448 rc
= fe_pywrap(fobj
, F_IN(c
->f
, MP_NEW
, p
.y
));
453 static PyObject
*epget__x(PyObject
*me
, void *hunoz
)
455 if (EC_ATINF(ECPT_P(me
))) RETURN_NONE
;
456 return (fe_pywrap(ECPT_FOBJ(me
), MP_COPY(ECPT_P(me
)->x
)));
459 static PyObject
*epget__y(PyObject
*me
, void *hunoz
)
461 if (EC_ATINF(ECPT_P(me
))) RETURN_NONE
;
462 return (fe_pywrap(ECPT_FOBJ(me
), MP_COPY(ECPT_P(me
)->y
)));
465 static PyObject
*epget__z(PyObject
*me
, void *hunoz
)
467 if (EC_ATINF(ECPT_P(me
)) || !ECPT_P(me
)->z
) RETURN_NONE
;
468 return (fe_pywrap(ECPT_FOBJ(me
), MP_COPY(ECPT_P(me
)->z
)));
471 static mp
*coord_in(field
*f
, PyObject
*x
)
474 if (FE_PYCHECK(x
) && (FE_F(x
) == f
|| field_samep(FE_F(x
), f
)))
475 return (MP_COPY(FE_X(x
)));
476 else if ((xx
= getmp(x
)) == 0)
479 return (F_IN(f
, xx
, xx
));
482 static int ecptxl_3(ec_curve
*c
, ec
*p
,
483 PyObject
*x
, PyObject
*y
, PyObject
*z
)
487 if (!x
|| !y
|| !z
) TYERR("missing argument");
488 if (!c
) VALERR("internal form with no curve!");
489 if ((p
->x
= coord_in(c
->f
, x
)) == 0 ||
490 (p
->y
= coord_in(c
->f
, y
)) == 0 ||
491 (z
!= Py_None
&& (p
->z
= coord_in(c
->f
, z
)) == 0))
493 if (!p
->z
) p
->z
= MP_COPY(c
->f
->one
); /* just in case */
499 static int ecptxl_2(ec_curve
*c
, ec
*p
, PyObject
*x
, PyObject
*y
)
503 if (!x
|| !y
) TYERR("missing argument");
504 if ((p
->x
= getmp(x
)) == 0 ||
505 (p
->y
= getmp(y
)) == 0)
507 if (c
) EC_IN(c
, p
, p
);
513 static int ecptxl_1(ec_curve
*c
, ec
*p
, PyObject
*x
)
516 PyObject
*y
= 0, *z
= 0, *t
= 0, *u
= 0;
522 if (!x
|| x
== Py_None
)
524 else if (ECPT_PYCHECK(x
)) {
527 } else if (TEXT_CHECK(x
)) {
528 qd
.p
= TEXT_PTR(x
); qd
.e
= 0;
529 qd_skipspc(&qd
); paren
= qd_delim(&qd
, '(');
530 if (!ec_ptparse(&qd
, p
)) VALERR(qd
.e
);
531 qd_skipspc(&qd
); if (paren
&& !qd_delim(&qd
, ')'))
532 { EC_DESTROY(p
); VALERR("missing `)'"); }
533 qd_skipspc(&qd
); if (!qd_eofp(&qd
)) VALERR("junk at eof");
535 } else if (c
&& (xx
= tomp(x
)) != 0) {
536 xx
= F_IN(c
->f
, xx
, xx
);
537 if (!EC_FIND(c
, p
, xx
)) VALERR("not on the curve");
538 } else if ((t
= PyObject_GetIter(x
)) != 0) {
540 x
= PyIter_Next(t
); if (!x
) goto enditer
;
541 y
= PyIter_Next(t
); if (!y
) goto enditer
;
542 z
= PyIter_Next(t
); if (!z
&& PyErr_Occurred()) goto end
;
546 else if (PyErr_Occurred()) goto end
;
548 rc
= !z ?
ecptxl_2(c
, p
, x
, y
) : ecptxl_3(c
, p
, x
, y
, z
);
552 TYERR("can't convert to curve point");
557 if (PyErr_Occurred()) goto end
;
558 TYERR("expected sequence of 2 or 3 items");
560 if (c
) EC_IN(c
, p
, p
);
564 Py_XDECREF(x
); Py_XDECREF(y
); Py_XDECREF(z
); Py_XDECREF(t
); Py_XDECREF(u
);
569 static int ecptxl(ec_curve
*c
, ec
*p
, PyObject
*x
, PyObject
*y
, PyObject
*z
)
572 return (ecptxl_3(c
, p
, x
, y
, z
));
574 return (ecptxl_2(c
, p
, x
, y
));
576 return (ecptxl_1(c
, p
, x
));
579 static PyObject
*ecptnc_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
581 PyObject
*x
= 0, *y
= 0, *z
= 0;
583 static const char *const kwlist
[] = { "x", "y", 0 };
585 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|OO:new", KWLIST
, &x
, &y
) ||
586 ecptxl(0, &p
, x
, y
, z
))
588 return (ecpt_pywrapout(ty
, &p
));
590 mp_drop(p
.x
); mp_drop(p
.y
); mp_drop(p
.z
);
594 static PyObject
*ecpt_pyint(PyObject
*me
)
599 if (EC_ATINF(ECPT_P(me
))) VALERR("point at infinity");
601 if (!mp_tolong_checked(p
.x
, &l
, 0)) rc
= PyInt_FromLong(l
);
602 else rc
= mp_topylong(p
.x
);
609 static PyObject
*ecpt_pylong(PyObject
*me
)
613 if (EC_ATINF(ECPT_P(me
))) VALERR("point at infinity");
615 rc
= mp_topylong(p
.x
);
622 static PyObject
*ecpt_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
624 PyObject
*x
= 0, *y
= 0, *z
= 0;
626 static const char *const kwlist
[] = { "x", "y", "z", 0 };
628 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|OOO:new", KWLIST
,
630 ecptxl(ECCURVE_C(ty
), &p
, x
, y
, z
))
632 return (ecpt_pywrap((PyObject
*)ty
, &p
));
634 mp_drop(p
.x
); mp_drop(p
.y
); mp_drop(p
.z
);
638 static const PyGetSetDef ecptnc_pygetset
[] = {
639 #define GETSETNAME(op, name) epnc##op##_##name
640 GET (ix
, "P.ix -> integer x coordinate of P")
641 GET (iy
, "P.iy -> integer y coordinate of P")
642 GET (point
, "P.point -> standalone curve point (no-op)")
647 static const PyMethodDef ecptnc_pymethods
[] = {
648 #define METHNAME(func) epmeth_##func
649 NAMETH(tobuf
, "X.tobuf() -> BIN")
650 CMTH (frombuf
, "frombuf(STR) -> (P, REST)")
651 CMTH (parse
, "parse(STR) -> (P, REST)")
656 static const PyNumberMethods ecpt_pynumber
= {
658 0, /* @nb_subtract@ */
659 0, /* @nb_multiply@ */
663 0, /* @nb_remainder@ */
666 0, /* @nb_negative@ */
667 0, /* @nb_positive@ */
668 0, /* @nb_absolute@ */
669 ecpt_pynonzerop
, /* @nb_nonzero@ */
679 ecpt_pyint
, /* @nb_int@ */
680 PY23(ecpt_pylong
, 0), /* @nb_long@ */
687 0, /* @nb_inplace_add@ */
688 0, /* @nb_inplace_subtract@ */
689 0, /* @nb_inplace_multiply@ */
691 0, /* @nb_inplace_divide@ */
693 0, /* @nb_inplace_remainder@ */
694 0, /* @nb_inplace_power@ */
695 0, /* @nb_inplace_lshift@ */
696 0, /* @nb_inplace_rshift@ */
697 0, /* @nb_inplace_and@ */
698 0, /* @nb_inplace_xor@ */
699 0, /* @nb_inplace_or@ */
701 0, /* @nb_floor_divide@ */
702 0, /* @nb_true_divide@ */
703 0, /* @nb_inplace_floor_divide@ */
704 0, /* @nb_inplace_true_divide@ */
707 static const PyTypeObject ecpt_pytype_skel
= {
708 PyVarObject_HEAD_INIT(0, 0) /* Header */
709 "ECPt", /* @tp_name@ */
710 sizeof(ecpt_pyobj
), /* @tp_basicsize@ */
711 0, /* @tp_itemsize@ */
713 ecpt_pydealloc
, /* @tp_dealloc@ */
715 0, /* @tp_getattr@ */
716 0, /* @tp_setattr@ */
717 0, /* @tp_compare@ */
719 PYNUMBER(ecpt
), /* @tp_as_number@ */
720 0, /* @tp_as_sequence@ */
721 0, /* @tp_as_mapping@ */
722 ecpt_pyhash
, /* @tp_hash@ */
725 0, /* @tp_getattro@ */
726 0, /* @tp_setattro@ */
727 0, /* @tp_as_buffer@ */
728 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
729 Py_TPFLAGS_CHECKTYPES
|
733 "ECPt([X, [Y]]): elliptic curve points, not associated with any curve.\n"
734 " X alone may be None, an existing point, a string 'X, Y', an\n"
735 " x-coordinate, or a pair (X, Y); X and Y should be a coordinate pair.",
737 0, /* @tp_traverse@ */
739 ecpt_pyrichcompare
, /* @tp_richcompare@ */
740 0, /* @tp_weaklistoffset@ */
742 0, /* @tp_iternext@ */
743 PYMETHODS(ecptnc
), /* @tp_methods@ */
744 0, /* @tp_members@ */
745 PYGETSET(ecptnc
), /* @tp_getset@ */
748 0, /* @tp_descr_get@ */
749 0, /* @tp_descr_set@ */
750 0, /* @tp_dictoffset@ */
752 PyType_GenericAlloc
, /* @tp_alloc@ */
753 ecptnc_pynew
, /* @tp_new@ */
758 static const PyMemberDef ecpt_pymembers
[] = {
759 #define MEMBERSTRUCT ecpt_pyobj
760 MEMRNM(curve
, T_OBJECT
, PY23(ob_type
, ob_base
.ob_type
), READONLY
,
761 "P.curve -> elliptic curve containing P")
766 static const PyGetSetDef ecpt_pygetset
[] = {
767 #define GETSETNAME(op, name) ep##op##_##name
768 GET (point
, "P.point -> standalone curve point")
769 GET (x
, "P.x -> Cartesian x coordinate of P")
770 GET (y
, "P.y -> Cartesian y coordinate of P")
771 GET (_x
, "P._x -> internal x coordinate of P")
772 GET (_y
, "P._y -> internal y coordinate of P")
773 GET (_z
, "P._z -> internal z coordinate of P, or None")
778 static const PyMethodDef ecpt_pymethods
[] = {
779 #define METHNAME(func) epmeth_##func
780 NAMETH(toraw
, "X.toraw() -> BIN")
781 KWMETH(ec2osp
, "X.ec2osp([flags = EC_EXPLY]) -> BIN")
782 NAMETH(dbl
, "X.dbl() -> X + X")
783 NAMETH(oncurvep
, "X.oncurvep() -> BOOL")
784 CMTH (fromraw
, "fromraw(STR) -> (P, REST)")
785 KWCMTH(os2ecp
, "os2ecp(STR, [flags = ...]) -> (P, REST)")
790 static const PyNumberMethods ecptcurve_pynumber
= {
791 ecpt_pyadd
, /* @nb_add@ */
792 ecpt_pysub
, /* @nb_subtract@ */
793 ecpt_pymul
, /* @nb_multiply@ */
797 0, /* @nb_remainder@ */
800 ecpt_pyneg
, /* @nb_negative@ */
801 ecpt_pyid
, /* @nb_positive@ */
802 0, /* @nb_absolute@ */
803 0, /* @nb_nonzero@ */
821 0, /* @nb_inplace_add@ */
822 0, /* @nb_inplace_subtract@ */
823 0, /* @nb_inplace_multiply@ */
824 0, /* @nb_inplace_divide@ */
825 0, /* @nb_inplace_remainder@ */
826 0, /* @nb_inplace_power@ */
827 0, /* @nb_inplace_lshift@ */
828 0, /* @nb_inplace_rshift@ */
829 0, /* @nb_inplace_and@ */
830 0, /* @nb_inplace_xor@ */
831 0, /* @nb_inplace_or@ */
833 0, /* @nb_floor_divide@ */
834 0, /* @nb_true_divide@ */
835 0, /* @nb_inplace_floor_divide@ */
836 0, /* @nb_inplace_true_divide@ */
839 static const PyTypeObject ecptcurve_pytype_skel
= {
840 PyVarObject_HEAD_INIT(0, 0) /* Header */
841 "ECPtCurve", /* @tp_name@ */
842 sizeof(ecpt_pyobj
), /* @tp_basicsize@ */
843 0, /* @tp_itemsize@ */
845 ecpt_pydealloc
, /* @tp_dealloc@ */
847 0, /* @tp_getattr@ */
848 0, /* @tp_setattr@ */
849 0, /* @tp_compare@ */
851 PYNUMBER(ecptcurve
), /* @tp_as_number@ */
852 0, /* @tp_as_sequence@ */
853 0, /* @tp_as_mapping@ */
857 0, /* @tp_getattro@ */
858 0, /* @tp_setattro@ */
859 0, /* @tp_as_buffer@ */
860 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
861 Py_TPFLAGS_CHECKTYPES
|
865 "Elliptic curve points; abstract base class for points on given curves.",
867 0, /* @tp_traverse@ */
869 0, /* @tp_richcompare@ */
870 0, /* @tp_weaklistoffset@ */
872 0, /* @tp_iternext@ */
873 PYMETHODS(ecpt
), /* @tp_methods@ */
874 PYMEMBERS(ecpt
), /* @tp_members@ */
875 PYGETSET(ecpt
), /* @tp_getset@ */
878 0, /* @tp_descr_get@ */
879 0, /* @tp_descr_set@ */
880 0, /* @tp_dictoffset@ */
882 PyType_GenericAlloc
, /* @tp_alloc@ */
883 abstract_pynew
, /* @tp_new@ */
888 /*----- Elliptic curves themselves ----------------------------------------*/
890 static PyObject
*eccurve_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
894 assert(ECCURVE_PYCHECK(x
));
895 if (!ECCURVE_PYCHECK(y
)) RETURN_NOTIMPL
;
896 b
= ec_samep(ECCURVE_C(x
), ECCURVE_C(y
));
899 case Py_NE
: b
= !b
; break;
900 default: TYERR("can't order elliptic curves");
907 static PyObject
*ecmmul_id(PyObject
*me
)
908 { ec p
= EC_INIT
; return (ecpt_pywrap(me
, &p
)); }
910 static int ecmmul_fill(void *pp
, PyObject
*me
, PyObject
*x
, PyObject
*m
)
912 ec_mulfactor
*f
= pp
;
915 if (getecpt(ECCURVE_C(me
), &f
->base
, x
) ||
916 (f
->exp
= getmp(m
)) == 0)
921 static PyObject
*ecmmul_exp(PyObject
*me
, void *pp
, size_t n
)
924 ec_immul(ECCURVE_C(me
), &p
, pp
, n
);
925 return (ecpt_pywrap(me
, &p
));
928 static void ecmmul_drop(void *pp
)
930 ec_mulfactor
*f
= pp
;
931 EC_DESTROY(&f
->base
);
935 static PyObject
*ecmeth_mmul(PyObject
*me
, PyObject
*arg
)
937 return (mexp_common(me
, arg
, sizeof(ec_mulfactor
),
938 ecmmul_id
, ecmmul_fill
, ecmmul_exp
, ecmmul_drop
));
941 static void eccurve_pydealloc(PyObject
*me
)
943 ec_destroycurve(ECCURVE_C(me
));
944 Py_DECREF(ECCURVE_FOBJ(me
));
945 PyType_Type
.tp_dealloc(me
);
948 static PyObject
*ecmeth_find(PyObject
*me
, PyObject
*arg
)
955 if (!PyArg_ParseTuple(arg
, "O:find", &x
)) goto end
;
956 if (FIELD_PYCHECK(x
) && FE_F(x
) == ECCURVE_C(me
)->f
)
957 xx
= MP_COPY(FE_X(x
));
958 else if ((xx
= getmp(x
)) == 0)
961 xx
= F_IN(ECCURVE_C(me
)->f
, xx
, xx
);
962 if (EC_FIND(ECCURVE_C(me
), &p
, xx
) == 0)
963 VALERR("not on the curve");
964 rc
= ecpt_pywrap(me
, &p
);
970 static PyObject
*ecmeth_rand(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
972 static const char *const kwlist
[] = { "rng", 0 };
973 grand
*r
= &rand_global
;
976 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:rand", KWLIST
,
979 ec_rand(ECCURVE_C(me
), &p
, r
);
980 EC_IN(ECCURVE_C(me
), &p
, &p
);
981 return (ecpt_pywrap(me
, &p
));
984 static PyObject
*ecmeth_parse(PyObject
*me
, PyObject
*arg
)
991 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
993 if ((c
= ec_curveparse(&qd
)) == 0) VALERR(qd
.e
);
994 rc
= Py_BuildValue("(Ns)", eccurve_pywrap(0, c
), qd
.p
);
999 static PyObject
*eccurve_dopywrap(PyTypeObject
*ty
,
1000 PyObject
*fobj
, ec_curve
*c
)
1002 eccurve_pyobj
*cobj
= newtype(ty
, 0, c
->ops
->name
);
1005 cobj
->ty
.ht_type
.tp_basicsize
= sizeof(ecpt_pyobj
);
1006 cobj
->ty
.ht_type
.tp_base
= ecptcurve_pytype
;
1007 Py_INCREF(ecptcurve_pytype
);
1008 cobj
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1009 Py_TPFLAGS_BASETYPE
|
1010 Py_TPFLAGS_CHECKTYPES
|
1011 Py_TPFLAGS_HEAPTYPE
);
1012 cobj
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1013 cobj
->ty
.ht_type
.tp_free
= 0;
1014 cobj
->ty
.ht_type
.tp_new
= ecpt_pynew
;
1015 typeready(&cobj
->ty
.ht_type
);
1016 return ((PyObject
*)cobj
);
1019 PyObject
*eccurve_pywrap(PyObject
*fobj
, ec_curve
*c
)
1024 fobj
= field_pywrap(c
->f
);
1027 assert(FIELD_F(fobj
) == c
->f
);
1028 if (STRCMP(EC_NAME(c
), ==, "prime"))
1029 ty
= ecprimecurve_pytype
;
1030 else if (STRCMP(EC_NAME(c
), ==, "primeproj"))
1031 ty
= ecprimeprojcurve_pytype
;
1032 else if (STRCMP(EC_NAME(c
), ==, "bin"))
1033 ty
= ecbincurve_pytype
;
1034 else if (STRCMP(EC_NAME(c
), ==, "binproj"))
1035 ty
= ecbinprojcurve_pytype
;
1038 return (eccurve_dopywrap(ty
, fobj
, c
));
1041 static PyObject
*eccurve_pynew(PyTypeObject
*ty
,
1042 ec_curve
*(*make
)(field
*, mp
*, mp
*),
1043 PyObject
*arg
, PyObject
*kw
)
1047 static const char *const kwlist
[] = { "field", "a", "b", 0 };
1048 mp
*aa
= 0, *bb
= 0;
1050 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!O&O&", KWLIST
,
1051 field_pytype
, &fobj
,
1052 convmp
, &aa
, convmp
, &bb
))
1055 cobj
= eccurve_dopywrap(ty
, fobj
, make(FIELD_F(fobj
), aa
, bb
));
1057 if (aa
) MP_DROP(aa
);
1058 if (bb
) MP_DROP(bb
);
1062 static PyObject
*ecget_name(PyObject
*me
, void *hunoz
)
1063 { return (TEXT_FROMSTR(EC_NAME(ECCURVE_C(me
)))); }
1065 static PyObject
*ecget_a(PyObject
*me
, void *hunoz
)
1066 { return (fe_pywrap(ECCURVE_FOBJ(me
), MP_COPY(ECCURVE_C(me
)->a
))); }
1068 static PyObject
*ecget_b(PyObject
*me
, void *hunoz
)
1069 { return (fe_pywrap(ECCURVE_FOBJ(me
), MP_COPY(ECCURVE_C(me
)->b
))); }
1071 static PyObject
*ecget_field(PyObject
*me
, void *hunoz
)
1072 { RETURN_OBJ(ECCURVE_FOBJ(me
)); }
1074 static PyObject
*ecget_inf(PyObject
*me
, void *hunoz
)
1075 { ec inf
= EC_INIT
; return (ecpt_pywrap(me
, &inf
)); }
1077 static const PyGetSetDef eccurve_pygetset
[] = {
1078 #define GETSETNAME(op, name) ec##op##_##name
1079 GET (name
, "E.name -> name of this kind of curve")
1080 GET (a
, "E.a -> first parameter of curve")
1081 GET (b
, "E.b -> second parameter of curve")
1082 GET (field
, "E.field -> finite field containing this curve")
1083 GET (inf
, "E.inf -> point at infinity of this curve")
1088 static const PyMethodDef eccurve_pymethods
[] = {
1089 #define METHNAME(name) ecmeth_##name
1090 METH (mmul
, "E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...")
1091 METH (find
, "E.find(X) -> P")
1092 KWMETH(rand
, "E.rand([rng = rand]) -> P")
1093 SMTH (parse
, "parse(STR) -> (E, REST)")
1098 static const PyTypeObject eccurve_pytype_skel
= {
1099 PyVarObject_HEAD_INIT(0, 0) /* Header */
1100 "ECCurve", /* @tp_name@ */
1101 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1102 0, /* @tp_itemsize@ */
1104 eccurve_pydealloc
, /* @tp_dealloc@ */
1106 0, /* @tp_getattr@ */
1107 0, /* @tp_setattr@ */
1108 0, /* @tp_compare@ */
1110 0, /* @tp_as_number@ */
1111 0, /* @tp_as_sequence@ */
1112 0, /* @tp_as_mapping@ */
1116 0, /* @tp_getattro@ */
1117 0, /* @tp_setattro@ */
1118 0, /* @tp_as_buffer@ */
1119 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1120 Py_TPFLAGS_BASETYPE
,
1123 "An elliptic curve. Abstract class.",
1125 0, /* @tp_traverse@ */
1127 eccurve_pyrichcompare
, /* @tp_richcompare@ */
1128 0, /* @tp_weaklistoffset@ */
1130 0, /* @tp_iternext@ */
1131 PYMETHODS(eccurve
), /* @tp_methods@ */
1132 0, /* @tp_members@ */
1133 PYGETSET(eccurve
), /* @tp_getset@ */
1136 0, /* @tp_descr_get@ */
1137 0, /* @tp_descr_set@ */
1138 0, /* @tp_dictoffset@ */
1140 PyType_GenericAlloc
, /* @tp_alloc@ */
1141 abstract_pynew
, /* @tp_new@ */
1146 static PyObject
*ecprimecurve_pynew(PyTypeObject
*ty
,
1147 PyObject
*arg
, PyObject
*kw
)
1149 return (eccurve_pynew(ty
, ec_prime
, arg
, kw
));
1152 static const PyTypeObject ecprimecurve_pytype_skel
= {
1153 PyVarObject_HEAD_INIT(0, 0) /* Header */
1154 "ECPrimeCurve", /* @tp_name@ */
1155 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1156 0, /* @tp_itemsize@ */
1158 eccurve_pydealloc
, /* @tp_dealloc@ */
1160 0, /* @tp_getattr@ */
1161 0, /* @tp_setattr@ */
1162 0, /* @tp_compare@ */
1164 0, /* @tp_as_number@ */
1165 0, /* @tp_as_sequence@ */
1166 0, /* @tp_as_mapping@ */
1170 0, /* @tp_getattro@ */
1171 0, /* @tp_setattro@ */
1172 0, /* @tp_as_buffer@ */
1173 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1174 Py_TPFLAGS_BASETYPE
,
1177 "ECPrimeCurve(FIELD, A, B): an elliptic curve over a prime field.\n"
1178 " Use ECPrimeProjCurve instead.",
1180 0, /* @tp_traverse@ */
1182 0, /* @tp_richcompare@ */
1183 0, /* @tp_weaklistoffset@ */
1185 0, /* @tp_iternext@ */
1186 0, /* @tp_methods@ */
1187 0, /* @tp_members@ */
1188 0, /* @tp_getset@ */
1191 0, /* @tp_descr_get@ */
1192 0, /* @tp_descr_set@ */
1193 0, /* @tp_dictoffset@ */
1195 PyType_GenericAlloc
, /* @tp_alloc@ */
1196 ecprimecurve_pynew
, /* @tp_new@ */
1201 static PyObject
*ecprimeprojcurve_pynew(PyTypeObject
*ty
,
1202 PyObject
*arg
, PyObject
*kw
)
1204 return (eccurve_pynew(ty
, ec_primeproj
, arg
, kw
));
1207 static const PyTypeObject ecprimeprojcurve_pytype_skel
= {
1208 PyVarObject_HEAD_INIT(0, 0) /* Header */
1209 "ECPrimeProjCurve", /* @tp_name@ */
1210 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1211 0, /* @tp_itemsize@ */
1213 eccurve_pydealloc
, /* @tp_dealloc@ */
1215 0, /* @tp_getattr@ */
1216 0, /* @tp_setattr@ */
1217 0, /* @tp_compare@ */
1219 0, /* @tp_as_number@ */
1220 0, /* @tp_as_sequence@ */
1221 0, /* @tp_as_mapping@ */
1225 0, /* @tp_getattro@ */
1226 0, /* @tp_setattro@ */
1227 0, /* @tp_as_buffer@ */
1228 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1229 Py_TPFLAGS_BASETYPE
,
1232 "ECPrimeProjCurve(FIELD, A, B): an elliptic curve over a prime field\n"
1233 " using projective coordinates.",
1235 0, /* @tp_traverse@ */
1237 0, /* @tp_richcompare@ */
1238 0, /* @tp_weaklistoffset@ */
1240 0, /* @tp_iternext@ */
1241 0, /* @tp_methods@ */
1242 0, /* @tp_members@ */
1243 0, /* @tp_getset@ */
1246 0, /* @tp_descr_get@ */
1247 0, /* @tp_descr_set@ */
1248 0, /* @tp_dictoffset@ */
1250 PyType_GenericAlloc
, /* @tp_alloc@ */
1251 ecprimeprojcurve_pynew
, /* @tp_new@ */
1256 static PyObject
*ecbincurve_pynew(PyTypeObject
*ty
,
1257 PyObject
*arg
, PyObject
*kw
)
1259 return (eccurve_pynew(ty
, ec_bin
, arg
, kw
));
1262 static const PyTypeObject ecbincurve_pytype_skel
= {
1263 PyVarObject_HEAD_INIT(0, 0) /* Header */
1264 "ECBinCurve", /* @tp_name@ */
1265 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1266 0, /* @tp_itemsize@ */
1268 eccurve_pydealloc
, /* @tp_dealloc@ */
1270 0, /* @tp_getattr@ */
1271 0, /* @tp_setattr@ */
1272 0, /* @tp_compare@ */
1274 0, /* @tp_as_number@ */
1275 0, /* @tp_as_sequence@ */
1276 0, /* @tp_as_mapping@ */
1280 0, /* @tp_getattro@ */
1281 0, /* @tp_setattro@ */
1282 0, /* @tp_as_buffer@ */
1283 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1284 Py_TPFLAGS_BASETYPE
,
1287 "ECBinCurve(FIELD, A, B): an elliptic curve over a binary field.\n"
1288 " Use ECBinProjCurve instead.",
1290 0, /* @tp_traverse@ */
1292 0, /* @tp_richcompare@ */
1293 0, /* @tp_weaklistoffset@ */
1295 0, /* @tp_iternext@ */
1296 0, /* @tp_methods@ */
1297 0, /* @tp_members@ */
1298 0, /* @tp_getset@ */
1301 0, /* @tp_descr_get@ */
1302 0, /* @tp_descr_set@ */
1303 0, /* @tp_dictoffset@ */
1305 PyType_GenericAlloc
, /* @tp_alloc@ */
1306 ecbincurve_pynew
, /* @tp_new@ */
1311 static PyObject
*ecbinprojcurve_pynew(PyTypeObject
*ty
,
1312 PyObject
*arg
, PyObject
*kw
)
1314 return (eccurve_pynew(ty
, ec_binproj
, arg
, kw
));
1317 static const PyTypeObject ecbinprojcurve_pytype_skel
= {
1318 PyVarObject_HEAD_INIT(0, 0) /* Header */
1319 "ECBinProjCurve", /* @tp_name@ */
1320 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1321 0, /* @tp_itemsize@ */
1323 eccurve_pydealloc
, /* @tp_dealloc@ */
1325 0, /* @tp_getattr@ */
1326 0, /* @tp_setattr@ */
1327 0, /* @tp_compare@ */
1329 0, /* @tp_as_number@ */
1330 0, /* @tp_as_sequence@ */
1331 0, /* @tp_as_mapping@ */
1335 0, /* @tp_getattro@ */
1336 0, /* @tp_setattro@ */
1337 0, /* @tp_as_buffer@ */
1338 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1339 Py_TPFLAGS_BASETYPE
,
1342 "ECBinProjCurve(FIELD, A, B): an elliptic curve over a binary field,\n"
1343 " using projective coordinates.",
1345 0, /* @tp_traverse@ */
1347 0, /* @tp_richcompare@ */
1348 0, /* @tp_weaklistoffset@ */
1350 0, /* @tp_iternext@ */
1351 0, /* @tp_methods@ */
1352 0, /* @tp_members@ */
1353 0, /* @tp_getset@ */
1356 0, /* @tp_descr_get@ */
1357 0, /* @tp_descr_set@ */
1358 0, /* @tp_dictoffset@ */
1360 PyType_GenericAlloc
, /* @tp_alloc@ */
1361 ecbinprojcurve_pynew
, /* @tp_new@ */
1366 /*----- Curve info --------------------------------------------------------*/
1368 void ecinfo_copy(ec_info
*eic
, const ec_info
*ei
)
1370 eic
->c
= eccurve_copy(ei
->c
);
1372 EC_COPY(&eic
->g
, &ei
->g
);
1373 eic
->r
= MP_COPY(ei
->r
);
1374 eic
->h
= MP_COPY(ei
->h
);
1377 PyObject
*ecinfo_pywrap(ec_info
*ei
)
1381 o
= PyObject_NEW(ecinfo_pyobj
, ecinfo_pytype
);
1383 o
->cobj
= eccurve_pywrap(0, o
->ei
.c
);
1385 return ((PyObject
*)o
);
1388 static void ecinfo_pydealloc(PyObject
*me
)
1390 ec_info
*ei
= ECINFO_EI(me
);
1394 Py_DECREF(ECINFO_COBJ(me
));
1398 static PyObject
*ecinfo_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1402 static const char *const kwlist
[] = { "curve", "G", "r", "h", 0 };
1403 ecinfo_pyobj
*rc
= 0;
1405 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!O!O&O&:new", KWLIST
,
1406 eccurve_pytype
, &e
, ecpt_pytype
, &g
,
1407 convmp
, &ei
.r
, convmp
, &ei
.h
))
1409 if (ECPT_C(g
) != ECCURVE_C(e
) && !ec_samep(ECPT_C(g
), ECCURVE_C(e
)))
1410 TYERR("point not from this curve");
1411 ei
.c
= ECCURVE_C(e
);
1413 EC_OUT(ei
.c
, &ei
.g
, ECPT_P(g
));
1414 rc
= (ecinfo_pyobj
*)ty
->tp_alloc(ty
, 0);
1417 Py_INCREF(rc
->cobj
);
1418 return ((PyObject
*)rc
);
1426 static PyObject
*eimeth_parse(PyObject
*me
, PyObject
*arg
)
1433 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
1435 if (ec_infoparse(&qd
, &ei
)) VALERR(qd
.e
);
1436 rc
= Py_BuildValue("(Ns)", ecinfo_pywrap(&ei
), qd
.p
);
1441 static PyObject
*ecinfo_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
1443 int b
= ec_sameinfop(ECINFO_EI(x
), ECINFO_EI(y
));
1447 default: TYERR("can't order elliptic curve infos");
1449 return (getbool(b
));
1454 static PyObject
*eimeth_check(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1456 static const char *const kwlist
[] = { "rng", 0 };
1457 grand
*r
= &rand_global
;
1460 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:check", KWLIST
,
1463 if ((p
= ec_checkinfo(ECINFO_EI(me
), r
)) != 0)
1470 static PyObject
*eiget_curve(PyObject
*me
, void *hunoz
)
1471 { RETURN_OBJ(ECINFO_COBJ(me
)); }
1473 static PyObject
*eiget_G(PyObject
*me
, void *hunoz
)
1475 ec_info
*ei
= ECINFO_EI(me
);
1477 EC_IN(ei
->c
, &p
, &ei
->g
);
1478 return (ecpt_pywrap(ECINFO_COBJ(me
), &p
));
1481 static PyObject
*eiget_r(PyObject
*me
, void *hunoz
)
1482 { return (mp_pywrap(MP_COPY(ECINFO_EI(me
)->r
))); }
1484 static PyObject
*eiget_h(PyObject
*me
, void *hunoz
)
1485 { return (mp_pywrap(MP_COPY(ECINFO_EI(me
)->h
))); }
1487 static const PyGetSetDef ecinfo_pygetset
[] = {
1488 #define GETSETNAME(op, name) ei##op##_##name
1489 GET (curve
, "I.curve -> the elliptic curve")
1490 GET (G
, "I.G -> generator point for the group")
1491 GET (r
, "I.r -> order of the group (and hence of G")
1492 GET (h
, "I.h -> cofactor of the group")
1497 static const PyMethodDef ecinfo_pymethods
[] = {
1498 #define METHNAME(name) eimeth_##name
1499 KWMETH(check
, "I.check([rng = rand]) -> None")
1500 SMTH (parse
, "parse(STR) -> (I, REST)")
1505 static const PyTypeObject ecinfo_pytype_skel
= {
1506 PyVarObject_HEAD_INIT(0, 0) /* Header */
1507 "ECInfo", /* @tp_name@ */
1508 sizeof(ecinfo_pyobj
), /* @tp_basicsize@ */
1509 0, /* @tp_itemsize@ */
1511 ecinfo_pydealloc
, /* @tp_dealloc@ */
1513 0, /* @tp_getattr@ */
1514 0, /* @tp_setattr@ */
1515 0, /* @tp_compare@ */
1517 0, /* @tp_as_number@ */
1518 0, /* @tp_as_sequence@ */
1519 0, /* @tp_as_mapping@ */
1523 0, /* @tp_getattro@ */
1524 0, /* @tp_setattro@ */
1525 0, /* @tp_as_buffer@ */
1526 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1527 Py_TPFLAGS_BASETYPE
,
1530 "ECInfo(CURVE, G, R, H): elliptic curve domain parameters.",
1532 0, /* @tp_traverse@ */
1534 ecinfo_pyrichcompare
, /* @tp_richcompare@ */
1535 0, /* @tp_weaklistoffset@ */
1537 0, /* @tp_iternext@ */
1538 PYMETHODS(ecinfo
), /* @tp_methods@ */
1539 0, /* @tp_members@ */
1540 PYGETSET(ecinfo
), /* @tp_getset@ */
1543 0, /* @tp_descr_get@ */
1544 0, /* @tp_descr_set@ */
1545 0, /* @tp_dictoffset@ */
1547 PyType_GenericAlloc
, /* @tp_alloc@ */
1548 ecinfo_pynew
, /* @tp_new@ */
1553 /*----- Setup -------------------------------------------------------------*/
1555 static const struct nameval consts
[] = {
1556 CONST(EC_XONLY
), CONST(EC_YBIT
), CONST(EC_LSB
),
1557 CONST(EC_CMPR
), CONST(EC_EXPLY
), CONST(EC_SORT
),
1561 void ec_pyinit(void)
1563 INITTYPE(ecpt
, root
);
1564 INITTYPE(ecptcurve
, ecpt
);
1565 INITTYPE(eccurve
, type
);
1566 INITTYPE(ecprimecurve
, eccurve
);
1567 INITTYPE(ecprimeprojcurve
, ecprimecurve
);
1568 INITTYPE(ecbincurve
, eccurve
);
1569 INITTYPE(ecbinprojcurve
, ecbincurve
);
1570 INITTYPE(ecinfo
, root
);
1573 static const char *ec_namefn(const void *p
)
1574 { const ecentry
*ec
= p
; return (ec
->name
); }
1576 static int ec_ixfn(const void *p
)
1578 const ecentry
*ec
= p
;
1581 for (i
= 0; ectab
[i
].name
; i
++)
1582 if (ectab
[i
].data
== ec
->data
) return (i
);
1586 static PyObject
*ec_valfn(int i
)
1590 ec_infofromdata(&ei
, ectab
[i
].data
);
1591 return (ecinfo_pywrap(&ei
));
1594 void ec_pyinsert(PyObject
*mod
)
1596 INSERT("ECPt", ecpt_pytype
);
1597 INSERT("ECPtCurve", ecptcurve_pytype
);
1598 INSERT("ECCurve", eccurve_pytype
);
1599 INSERT("ECPrimeCurve", ecprimecurve_pytype
);
1600 INSERT("ECPrimeProjCurve", ecprimeprojcurve_pytype
);
1601 INSERT("ECBinCurve", ecbincurve_pytype
);
1602 INSERT("ECBinProjCurve", ecbinprojcurve_pytype
);
1603 INSERT("ECInfo", ecinfo_pytype
);
1604 INSERT("eccurves", make_grouptab(ectab
, sizeof(*ectab
),
1605 ec_namefn
, ec_ixfn
, ec_valfn
));
1606 setconstants(mod
, consts
);
1609 /*----- That's all, folks -------------------------------------------------*/