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 Py_TYPE(p
)->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;
523 if (!x
|| x
== Py_None
)
525 else if (ECPT_PYCHECK(x
)) {
528 } else if (TEXT_CHECK(x
)) {
529 qd
.p
= TEXT_PTR(x
); qd
.e
= 0;
530 qd_skipspc(&qd
); paren
= qd_delim(&qd
, '(');
531 if (!ec_ptparse(&qd
, p
)) VALERR(qd
.e
);
532 qd_skipspc(&qd
); if (paren
&& !qd_delim(&qd
, ')'))
533 { EC_DESTROY(p
); VALERR("missing `)'"); }
534 qd_skipspc(&qd
); if (!qd_eofp(&qd
)) VALERR("junk at eof");
536 } else if (c
&& (xx
= tomp(x
)) != 0) {
537 xx
= F_IN(c
->f
, xx
, xx
);
538 if (!EC_FIND(c
, p
, xx
)) VALERR("not on the curve");
539 } else if (PySequence_Check(x
)) {
541 n
= PySequence_Size(t
); if (n
< 0) goto end
;
542 if (n
!= 2 && n
!= 3)
543 TYERR("want sequence of two or three items");
544 if ((x
= PySequence_GetItem(t
, 0)) == 0 ||
545 (y
= PySequence_GetItem(t
, 1)) == 0 ||
546 (n
== 3 && (z
= PySequence_GetItem(t
, 2)) == 0))
548 rc
= (n
== 2) ?
ecptxl_2(c
, p
, x
, y
) : ecptxl_3(c
, p
, x
, y
, z
);
551 TYERR("can't convert to curve point");
555 if (c
) EC_IN(c
, p
, p
);
559 Py_XDECREF(x
); Py_XDECREF(y
); Py_XDECREF(z
); Py_XDECREF(t
);
564 static int ecptxl(ec_curve
*c
, ec
*p
, PyObject
*x
, PyObject
*y
, PyObject
*z
)
567 return (ecptxl_3(c
, p
, x
, y
, z
));
569 return (ecptxl_2(c
, p
, x
, y
));
571 return (ecptxl_1(c
, p
, x
));
574 static PyObject
*ecptnc_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
576 PyObject
*x
= 0, *y
= 0, *z
= 0;
578 static const char *const kwlist
[] = { "x", "y", 0 };
580 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|OO:new", KWLIST
, &x
, &y
) ||
581 ecptxl(0, &p
, x
, y
, z
))
583 return (ecpt_pywrapout(ty
, &p
));
585 mp_drop(p
.x
); mp_drop(p
.y
); mp_drop(p
.z
);
589 static PyObject
*ecpt_pyint(PyObject
*me
)
594 if (EC_ATINF(ECPT_P(me
))) VALERR("point at infinity");
596 if (!mp_tolong_checked(p
.x
, &l
, 0)) rc
= PyInt_FromLong(l
);
597 else rc
= mp_topylong(p
.x
);
604 static PyObject
*ecpt_pylong(PyObject
*me
)
608 if (EC_ATINF(ECPT_P(me
))) VALERR("point at infinity");
610 rc
= mp_topylong(p
.x
);
617 static PyObject
*ecpt_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
619 PyObject
*x
= 0, *y
= 0, *z
= 0;
621 static const char *const kwlist
[] = { "x", "y", "z", 0 };
623 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|OOO:new", KWLIST
,
625 ecptxl(ECCURVE_C(ty
), &p
, x
, y
, z
))
627 return (ecpt_pywrap((PyObject
*)ty
, &p
));
629 mp_drop(p
.x
); mp_drop(p
.y
); mp_drop(p
.z
);
633 static const PyGetSetDef ecptnc_pygetset
[] = {
634 #define GETSETNAME(op, name) epnc##op##_##name
635 GET (ix
, "P.ix -> integer x coordinate of P")
636 GET (iy
, "P.iy -> integer y coordinate of P")
637 GET (point
, "P.point -> standalone curve point (no-op)")
642 static const PyMethodDef ecptnc_pymethods
[] = {
643 #define METHNAME(func) epmeth_##func
644 NAMETH(tobuf
, "X.tobuf() -> BIN")
645 CMTH (frombuf
, "frombuf(STR) -> (P, REST)")
646 CMTH (parse
, "parse(STR) -> (P, REST)")
651 static const PyNumberMethods ecpt_pynumber
= {
653 0, /* @nb_subtract@ */
654 0, /* @nb_multiply@ */
658 0, /* @nb_remainder@ */
661 0, /* @nb_negative@ */
662 0, /* @nb_positive@ */
663 0, /* @nb_absolute@ */
664 ecpt_pynonzerop
, /* @nb_nonzero@ */
674 ecpt_pyint
, /* @nb_int@ */
675 PY23(ecpt_pylong
, 0), /* @nb_long@ */
682 0, /* @nb_inplace_add@ */
683 0, /* @nb_inplace_subtract@ */
684 0, /* @nb_inplace_multiply@ */
686 0, /* @nb_inplace_divide@ */
688 0, /* @nb_inplace_remainder@ */
689 0, /* @nb_inplace_power@ */
690 0, /* @nb_inplace_lshift@ */
691 0, /* @nb_inplace_rshift@ */
692 0, /* @nb_inplace_and@ */
693 0, /* @nb_inplace_xor@ */
694 0, /* @nb_inplace_or@ */
696 0, /* @nb_floor_divide@ */
697 0, /* @nb_true_divide@ */
698 0, /* @nb_inplace_floor_divide@ */
699 0, /* @nb_inplace_true_divide@ */
702 static const PyTypeObject ecpt_pytype_skel
= {
703 PyVarObject_HEAD_INIT(0, 0) /* Header */
704 "ECPt", /* @tp_name@ */
705 sizeof(ecpt_pyobj
), /* @tp_basicsize@ */
706 0, /* @tp_itemsize@ */
708 ecpt_pydealloc
, /* @tp_dealloc@ */
710 0, /* @tp_getattr@ */
711 0, /* @tp_setattr@ */
712 0, /* @tp_compare@ */
714 PYNUMBER(ecpt
), /* @tp_as_number@ */
715 0, /* @tp_as_sequence@ */
716 0, /* @tp_as_mapping@ */
717 ecpt_pyhash
, /* @tp_hash@ */
720 0, /* @tp_getattro@ */
721 0, /* @tp_setattro@ */
722 0, /* @tp_as_buffer@ */
723 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
724 Py_TPFLAGS_CHECKTYPES
|
728 "ECPt([X, [Y]]): elliptic curve points, not associated with any curve.\n"
729 " X alone may be None, an existing point, a string 'X, Y', an\n"
730 " x-coordinate, or a pair (X, Y); X and Y should be a coordinate pair.",
732 0, /* @tp_traverse@ */
734 ecpt_pyrichcompare
, /* @tp_richcompare@ */
735 0, /* @tp_weaklistoffset@ */
737 0, /* @tp_iternext@ */
738 PYMETHODS(ecptnc
), /* @tp_methods@ */
739 0, /* @tp_members@ */
740 PYGETSET(ecptnc
), /* @tp_getset@ */
743 0, /* @tp_descr_get@ */
744 0, /* @tp_descr_set@ */
745 0, /* @tp_dictoffset@ */
747 PyType_GenericAlloc
, /* @tp_alloc@ */
748 ecptnc_pynew
, /* @tp_new@ */
753 static const PyMemberDef ecpt_pymembers
[] = {
754 #define MEMBERSTRUCT ecpt_pyobj
755 MEMRNM(curve
, T_OBJECT
, PY23(ob_type
, ob_base
.ob_type
), READONLY
,
756 "P.curve -> elliptic curve containing P")
761 static const PyGetSetDef ecpt_pygetset
[] = {
762 #define GETSETNAME(op, name) ep##op##_##name
763 GET (point
, "P.point -> standalone curve point")
764 GET (x
, "P.x -> Cartesian x coordinate of P")
765 GET (y
, "P.y -> Cartesian y coordinate of P")
766 GET (_x
, "P._x -> internal x coordinate of P")
767 GET (_y
, "P._y -> internal y coordinate of P")
768 GET (_z
, "P._z -> internal z coordinate of P, or None")
773 static const PyMethodDef ecpt_pymethods
[] = {
774 #define METHNAME(func) epmeth_##func
775 NAMETH(toraw
, "X.toraw() -> BIN")
776 KWMETH(ec2osp
, "X.ec2osp([flags = EC_EXPLY]) -> BIN")
777 NAMETH(dbl
, "X.dbl() -> X + X")
778 NAMETH(oncurvep
, "X.oncurvep() -> BOOL")
779 CMTH (fromraw
, "fromraw(STR) -> (P, REST)")
780 KWCMTH(os2ecp
, "os2ecp(STR, [flags = ...]) -> (P, REST)")
785 static const PyNumberMethods ecptcurve_pynumber
= {
786 ecpt_pyadd
, /* @nb_add@ */
787 ecpt_pysub
, /* @nb_subtract@ */
788 ecpt_pymul
, /* @nb_multiply@ */
792 0, /* @nb_remainder@ */
795 ecpt_pyneg
, /* @nb_negative@ */
796 ecpt_pyid
, /* @nb_positive@ */
797 0, /* @nb_absolute@ */
798 0, /* @nb_nonzero@ */
816 0, /* @nb_inplace_add@ */
817 0, /* @nb_inplace_subtract@ */
818 0, /* @nb_inplace_multiply@ */
819 0, /* @nb_inplace_divide@ */
820 0, /* @nb_inplace_remainder@ */
821 0, /* @nb_inplace_power@ */
822 0, /* @nb_inplace_lshift@ */
823 0, /* @nb_inplace_rshift@ */
824 0, /* @nb_inplace_and@ */
825 0, /* @nb_inplace_xor@ */
826 0, /* @nb_inplace_or@ */
828 0, /* @nb_floor_divide@ */
829 0, /* @nb_true_divide@ */
830 0, /* @nb_inplace_floor_divide@ */
831 0, /* @nb_inplace_true_divide@ */
834 static const PyTypeObject ecptcurve_pytype_skel
= {
835 PyVarObject_HEAD_INIT(0, 0) /* Header */
836 "ECPtCurve", /* @tp_name@ */
837 sizeof(ecpt_pyobj
), /* @tp_basicsize@ */
838 0, /* @tp_itemsize@ */
840 ecpt_pydealloc
, /* @tp_dealloc@ */
842 0, /* @tp_getattr@ */
843 0, /* @tp_setattr@ */
844 0, /* @tp_compare@ */
846 PYNUMBER(ecptcurve
), /* @tp_as_number@ */
847 0, /* @tp_as_sequence@ */
848 0, /* @tp_as_mapping@ */
852 0, /* @tp_getattro@ */
853 0, /* @tp_setattro@ */
854 0, /* @tp_as_buffer@ */
855 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
856 Py_TPFLAGS_CHECKTYPES
|
860 "Elliptic curve points; abstract base class for points on given curves.",
862 0, /* @tp_traverse@ */
864 0, /* @tp_richcompare@ */
865 0, /* @tp_weaklistoffset@ */
867 0, /* @tp_iternext@ */
868 PYMETHODS(ecpt
), /* @tp_methods@ */
869 PYMEMBERS(ecpt
), /* @tp_members@ */
870 PYGETSET(ecpt
), /* @tp_getset@ */
873 0, /* @tp_descr_get@ */
874 0, /* @tp_descr_set@ */
875 0, /* @tp_dictoffset@ */
877 PyType_GenericAlloc
, /* @tp_alloc@ */
878 abstract_pynew
, /* @tp_new@ */
883 /*----- Elliptic curves themselves ----------------------------------------*/
885 static PyObject
*eccurve_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
889 assert(ECCURVE_PYCHECK(x
));
890 if (!ECCURVE_PYCHECK(y
)) RETURN_NOTIMPL
;
891 b
= ec_samep(ECCURVE_C(x
), ECCURVE_C(y
));
894 case Py_NE
: b
= !b
; break;
895 default: TYERR("can't order elliptic curves");
902 static PyObject
*ecmmul_id(PyObject
*me
)
903 { ec p
= EC_INIT
; return (ecpt_pywrap(me
, &p
)); }
905 static int ecmmul_fill(void *pp
, PyObject
*me
, PyObject
*x
, PyObject
*m
)
907 ec_mulfactor
*f
= pp
;
910 if (getecpt(ECCURVE_C(me
), &f
->base
, x
) ||
911 (f
->exp
= getmp(m
)) == 0)
916 static PyObject
*ecmmul_exp(PyObject
*me
, void *pp
, int n
)
919 ec_immul(ECCURVE_C(me
), &p
, pp
, n
);
920 return (ecpt_pywrap(me
, &p
));
923 static void ecmmul_drop(void *pp
)
925 ec_mulfactor
*f
= pp
;
926 EC_DESTROY(&f
->base
);
930 static PyObject
*ecmeth_mmul(PyObject
*me
, PyObject
*arg
)
932 return (mexp_common(me
, arg
, sizeof(ec_mulfactor
),
933 ecmmul_id
, ecmmul_fill
, ecmmul_exp
, ecmmul_drop
));
936 static void eccurve_pydealloc(PyObject
*me
)
938 ec_destroycurve(ECCURVE_C(me
));
939 Py_DECREF(ECCURVE_FOBJ(me
));
940 PyType_Type
.tp_dealloc(me
);
943 static PyObject
*ecmeth_find(PyObject
*me
, PyObject
*arg
)
950 if (!PyArg_ParseTuple(arg
, "O:find", &x
)) goto end
;
951 if (FIELD_PYCHECK(x
) && FE_F(x
) == ECCURVE_C(me
)->f
)
952 xx
= MP_COPY(FE_X(x
));
953 else if ((xx
= getmp(x
)) == 0)
956 xx
= F_IN(ECCURVE_C(me
)->f
, xx
, xx
);
957 if (EC_FIND(ECCURVE_C(me
), &p
, xx
) == 0)
958 VALERR("not on the curve");
959 rc
= ecpt_pywrap(me
, &p
);
965 static PyObject
*ecmeth_rand(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
967 static const char *const kwlist
[] = { "rng", 0 };
968 grand
*r
= &rand_global
;
971 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:rand", KWLIST
,
974 ec_rand(ECCURVE_C(me
), &p
, r
);
975 EC_IN(ECCURVE_C(me
), &p
, &p
);
976 return (ecpt_pywrap(me
, &p
));
979 static PyObject
*ecmeth_parse(PyObject
*me
, PyObject
*arg
)
986 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
988 if ((c
= ec_curveparse(&qd
)) == 0) VALERR(qd
.e
);
989 rc
= eccurve_pywrap(0, c
);
994 static PyObject
*eccurve_dopywrap(PyTypeObject
*ty
,
995 PyObject
*fobj
, ec_curve
*c
)
997 eccurve_pyobj
*cobj
= newtype(ty
, 0, c
->ops
->name
);
1000 cobj
->ty
.ht_type
.tp_basicsize
= sizeof(ecpt_pyobj
);
1001 cobj
->ty
.ht_type
.tp_base
= ecptcurve_pytype
;
1002 Py_INCREF(ecptcurve_pytype
);
1003 cobj
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1004 Py_TPFLAGS_BASETYPE
|
1005 Py_TPFLAGS_CHECKTYPES
|
1006 Py_TPFLAGS_HEAPTYPE
);
1007 cobj
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1008 cobj
->ty
.ht_type
.tp_free
= 0;
1009 cobj
->ty
.ht_type
.tp_new
= ecpt_pynew
;
1010 typeready(&cobj
->ty
.ht_type
);
1011 return ((PyObject
*)cobj
);
1014 PyObject
*eccurve_pywrap(PyObject
*fobj
, ec_curve
*c
)
1019 fobj
= field_pywrap(c
->f
);
1022 assert(FIELD_F(fobj
) == c
->f
);
1023 if (STRCMP(EC_NAME(c
), ==, "prime"))
1024 ty
= ecprimecurve_pytype
;
1025 else if (STRCMP(EC_NAME(c
), ==, "primeproj"))
1026 ty
= ecprimeprojcurve_pytype
;
1027 else if (STRCMP(EC_NAME(c
), ==, "bin"))
1028 ty
= ecbincurve_pytype
;
1029 else if (STRCMP(EC_NAME(c
), ==, "binproj"))
1030 ty
= ecbinprojcurve_pytype
;
1033 return (eccurve_dopywrap(ty
, fobj
, c
));
1036 static PyObject
*eccurve_pynew(PyTypeObject
*ty
,
1037 ec_curve
*(*make
)(field
*, mp
*, mp
*),
1038 PyObject
*arg
, PyObject
*kw
)
1042 static const char *const kwlist
[] = { "field", "a", "b", 0 };
1043 mp
*aa
= 0, *bb
= 0;
1045 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!O&O&", KWLIST
,
1046 field_pytype
, &fobj
,
1047 convmp
, &aa
, convmp
, &bb
))
1050 cobj
= eccurve_dopywrap(ty
, fobj
, make(FIELD_F(fobj
), aa
, bb
));
1052 if (aa
) MP_DROP(aa
);
1053 if (bb
) MP_DROP(bb
);
1057 static PyObject
*ecget_name(PyObject
*me
, void *hunoz
)
1058 { return (TEXT_FROMSTR(EC_NAME(ECCURVE_C(me
)))); }
1060 static PyObject
*ecget_a(PyObject
*me
, void *hunoz
)
1061 { return (fe_pywrap(ECCURVE_FOBJ(me
), MP_COPY(ECCURVE_C(me
)->a
))); }
1063 static PyObject
*ecget_b(PyObject
*me
, void *hunoz
)
1064 { return (fe_pywrap(ECCURVE_FOBJ(me
), MP_COPY(ECCURVE_C(me
)->b
))); }
1066 static PyObject
*ecget_field(PyObject
*me
, void *hunoz
)
1067 { RETURN_OBJ(ECCURVE_FOBJ(me
)); }
1069 static PyObject
*ecget_inf(PyObject
*me
, void *hunoz
)
1070 { ec inf
= EC_INIT
; return (ecpt_pywrap(me
, &inf
)); }
1072 static const PyGetSetDef eccurve_pygetset
[] = {
1073 #define GETSETNAME(op, name) ec##op##_##name
1074 GET (name
, "E.name -> name of this kind of curve")
1075 GET (a
, "E.a -> first parameter of curve")
1076 GET (b
, "E.b -> second parameter of curve")
1077 GET (field
, "E.field -> finite field containing this curve")
1078 GET (inf
, "E.inf -> point at infinity of this curve")
1083 static const PyMethodDef eccurve_pymethods
[] = {
1084 #define METHNAME(name) ecmeth_##name
1085 METH (mmul
, "E.mmul([(P0, N0), (P1, N1), ...]) = N0 P0 + N1 P1 + ...")
1086 METH (find
, "E.find(X) -> P")
1087 KWMETH(rand
, "E.rand([rng = rand]) -> P")
1088 SMTH (parse
, "parse(STR) -> (E, REST)")
1093 static const PyTypeObject eccurve_pytype_skel
= {
1094 PyVarObject_HEAD_INIT(0, 0) /* Header */
1095 "ECCurve", /* @tp_name@ */
1096 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1097 0, /* @tp_itemsize@ */
1099 eccurve_pydealloc
, /* @tp_dealloc@ */
1101 0, /* @tp_getattr@ */
1102 0, /* @tp_setattr@ */
1103 0, /* @tp_compare@ */
1105 0, /* @tp_as_number@ */
1106 0, /* @tp_as_sequence@ */
1107 0, /* @tp_as_mapping@ */
1111 0, /* @tp_getattro@ */
1112 0, /* @tp_setattro@ */
1113 0, /* @tp_as_buffer@ */
1114 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1115 Py_TPFLAGS_BASETYPE
,
1118 "An elliptic curve. Abstract class.",
1120 0, /* @tp_traverse@ */
1122 eccurve_pyrichcompare
, /* @tp_richcompare@ */
1123 0, /* @tp_weaklistoffset@ */
1125 0, /* @tp_iternext@ */
1126 PYMETHODS(eccurve
), /* @tp_methods@ */
1127 0, /* @tp_members@ */
1128 PYGETSET(eccurve
), /* @tp_getset@ */
1131 0, /* @tp_descr_get@ */
1132 0, /* @tp_descr_set@ */
1133 0, /* @tp_dictoffset@ */
1135 PyType_GenericAlloc
, /* @tp_alloc@ */
1136 abstract_pynew
, /* @tp_new@ */
1141 static PyObject
*ecprimecurve_pynew(PyTypeObject
*ty
,
1142 PyObject
*arg
, PyObject
*kw
)
1144 return (eccurve_pynew(ty
, ec_prime
, arg
, kw
));
1147 static const PyTypeObject ecprimecurve_pytype_skel
= {
1148 PyVarObject_HEAD_INIT(0, 0) /* Header */
1149 "ECPrimeCurve", /* @tp_name@ */
1150 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1151 0, /* @tp_itemsize@ */
1153 eccurve_pydealloc
, /* @tp_dealloc@ */
1155 0, /* @tp_getattr@ */
1156 0, /* @tp_setattr@ */
1157 0, /* @tp_compare@ */
1159 0, /* @tp_as_number@ */
1160 0, /* @tp_as_sequence@ */
1161 0, /* @tp_as_mapping@ */
1165 0, /* @tp_getattro@ */
1166 0, /* @tp_setattro@ */
1167 0, /* @tp_as_buffer@ */
1168 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1169 Py_TPFLAGS_BASETYPE
,
1172 "ECPrimeCurve(FIELD, A, B): an elliptic curve over a prime field.\n"
1173 " Use ECPrimeProjCurve instead.",
1175 0, /* @tp_traverse@ */
1177 0, /* @tp_richcompare@ */
1178 0, /* @tp_weaklistoffset@ */
1180 0, /* @tp_iternext@ */
1181 0, /* @tp_methods@ */
1182 0, /* @tp_members@ */
1183 0, /* @tp_getset@ */
1186 0, /* @tp_descr_get@ */
1187 0, /* @tp_descr_set@ */
1188 0, /* @tp_dictoffset@ */
1190 PyType_GenericAlloc
, /* @tp_alloc@ */
1191 ecprimecurve_pynew
, /* @tp_new@ */
1196 static PyObject
*ecprimeprojcurve_pynew(PyTypeObject
*ty
,
1197 PyObject
*arg
, PyObject
*kw
)
1199 return (eccurve_pynew(ty
, ec_primeproj
, arg
, kw
));
1202 static const PyTypeObject ecprimeprojcurve_pytype_skel
= {
1203 PyVarObject_HEAD_INIT(0, 0) /* Header */
1204 "ECPrimeProjCurve", /* @tp_name@ */
1205 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1206 0, /* @tp_itemsize@ */
1208 eccurve_pydealloc
, /* @tp_dealloc@ */
1210 0, /* @tp_getattr@ */
1211 0, /* @tp_setattr@ */
1212 0, /* @tp_compare@ */
1214 0, /* @tp_as_number@ */
1215 0, /* @tp_as_sequence@ */
1216 0, /* @tp_as_mapping@ */
1220 0, /* @tp_getattro@ */
1221 0, /* @tp_setattro@ */
1222 0, /* @tp_as_buffer@ */
1223 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1224 Py_TPFLAGS_BASETYPE
,
1227 "ECPrimeProjCurve(FIELD, A, B): an elliptic curve over a prime field\n"
1228 " using projective coordinates.",
1230 0, /* @tp_traverse@ */
1232 0, /* @tp_richcompare@ */
1233 0, /* @tp_weaklistoffset@ */
1235 0, /* @tp_iternext@ */
1236 0, /* @tp_methods@ */
1237 0, /* @tp_members@ */
1238 0, /* @tp_getset@ */
1241 0, /* @tp_descr_get@ */
1242 0, /* @tp_descr_set@ */
1243 0, /* @tp_dictoffset@ */
1245 PyType_GenericAlloc
, /* @tp_alloc@ */
1246 ecprimeprojcurve_pynew
, /* @tp_new@ */
1251 static PyObject
*ecbincurve_pynew(PyTypeObject
*ty
,
1252 PyObject
*arg
, PyObject
*kw
)
1254 return (eccurve_pynew(ty
, ec_bin
, arg
, kw
));
1257 static const PyTypeObject ecbincurve_pytype_skel
= {
1258 PyVarObject_HEAD_INIT(0, 0) /* Header */
1259 "ECBinCurve", /* @tp_name@ */
1260 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1261 0, /* @tp_itemsize@ */
1263 eccurve_pydealloc
, /* @tp_dealloc@ */
1265 0, /* @tp_getattr@ */
1266 0, /* @tp_setattr@ */
1267 0, /* @tp_compare@ */
1269 0, /* @tp_as_number@ */
1270 0, /* @tp_as_sequence@ */
1271 0, /* @tp_as_mapping@ */
1275 0, /* @tp_getattro@ */
1276 0, /* @tp_setattro@ */
1277 0, /* @tp_as_buffer@ */
1278 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1279 Py_TPFLAGS_BASETYPE
,
1282 "ECBinCurve(FIELD, A, B): an elliptic curve over a binary field.\n"
1283 " Use ECBinProjCurve instead.",
1285 0, /* @tp_traverse@ */
1287 0, /* @tp_richcompare@ */
1288 0, /* @tp_weaklistoffset@ */
1290 0, /* @tp_iternext@ */
1291 0, /* @tp_methods@ */
1292 0, /* @tp_members@ */
1293 0, /* @tp_getset@ */
1296 0, /* @tp_descr_get@ */
1297 0, /* @tp_descr_set@ */
1298 0, /* @tp_dictoffset@ */
1300 PyType_GenericAlloc
, /* @tp_alloc@ */
1301 ecbincurve_pynew
, /* @tp_new@ */
1306 static PyObject
*ecbinprojcurve_pynew(PyTypeObject
*ty
,
1307 PyObject
*arg
, PyObject
*kw
)
1309 return (eccurve_pynew(ty
, ec_binproj
, arg
, kw
));
1312 static const PyTypeObject ecbinprojcurve_pytype_skel
= {
1313 PyVarObject_HEAD_INIT(0, 0) /* Header */
1314 "ECBinProjCurve", /* @tp_name@ */
1315 sizeof(eccurve_pyobj
), /* @tp_basicsize@ */
1316 0, /* @tp_itemsize@ */
1318 eccurve_pydealloc
, /* @tp_dealloc@ */
1320 0, /* @tp_getattr@ */
1321 0, /* @tp_setattr@ */
1322 0, /* @tp_compare@ */
1324 0, /* @tp_as_number@ */
1325 0, /* @tp_as_sequence@ */
1326 0, /* @tp_as_mapping@ */
1330 0, /* @tp_getattro@ */
1331 0, /* @tp_setattro@ */
1332 0, /* @tp_as_buffer@ */
1333 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1334 Py_TPFLAGS_BASETYPE
,
1337 "ECBinProjCurve(FIELD, A, B): an elliptic curve over a binary field,\n"
1338 " using projective coordinates.",
1340 0, /* @tp_traverse@ */
1342 0, /* @tp_richcompare@ */
1343 0, /* @tp_weaklistoffset@ */
1345 0, /* @tp_iternext@ */
1346 0, /* @tp_methods@ */
1347 0, /* @tp_members@ */
1348 0, /* @tp_getset@ */
1351 0, /* @tp_descr_get@ */
1352 0, /* @tp_descr_set@ */
1353 0, /* @tp_dictoffset@ */
1355 PyType_GenericAlloc
, /* @tp_alloc@ */
1356 ecbinprojcurve_pynew
, /* @tp_new@ */
1361 /*----- Curve info --------------------------------------------------------*/
1363 void ecinfo_copy(ec_info
*eic
, const ec_info
*ei
)
1365 eic
->c
= eccurve_copy(ei
->c
);
1367 EC_COPY(&eic
->g
, &ei
->g
);
1368 eic
->r
= MP_COPY(ei
->r
);
1369 eic
->h
= MP_COPY(ei
->h
);
1372 PyObject
*ecinfo_pywrap(ec_info
*ei
)
1376 o
= PyObject_NEW(ecinfo_pyobj
, ecinfo_pytype
);
1378 o
->cobj
= eccurve_pywrap(0, o
->ei
.c
);
1380 return ((PyObject
*)o
);
1383 static void ecinfo_pydealloc(PyObject
*me
)
1385 ec_info
*ei
= ECINFO_EI(me
);
1389 Py_DECREF(ECINFO_COBJ(me
));
1393 static PyObject
*ecinfo_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1397 static const char *const kwlist
[] = { "curve", "G", "r", "h", 0 };
1398 ecinfo_pyobj
*rc
= 0;
1400 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O!O!O&O&:new", KWLIST
,
1401 eccurve_pytype
, &e
, ecpt_pytype
, &g
,
1402 convmp
, &ei
.r
, convmp
, &ei
.h
))
1404 if (ECPT_C(g
) != ECCURVE_C(e
) && !ec_samep(ECPT_C(g
), ECCURVE_C(e
)))
1405 TYERR("point not from this curve");
1406 ei
.c
= ECCURVE_C(e
);
1408 EC_OUT(ei
.c
, &ei
.g
, ECPT_P(g
));
1409 rc
= (ecinfo_pyobj
*)ty
->tp_alloc(ty
, 0);
1412 Py_INCREF(rc
->cobj
);
1413 return ((PyObject
*)rc
);
1421 static PyObject
*eimeth_parse(PyObject
*me
, PyObject
*arg
)
1428 if (!PyArg_ParseTuple(arg
, "s:parse", &p
)) goto end
;
1430 if (ec_infoparse(&qd
, &ei
)) VALERR(qd
.e
);
1431 rc
= Py_BuildValue("(Ns)", ecinfo_pywrap(&ei
), qd
.p
);
1436 static PyObject
*ecinfo_pyrichcompare(PyObject
*x
, PyObject
*y
, int op
)
1438 int b
= ec_sameinfop(ECINFO_EI(x
), ECINFO_EI(y
));
1442 default: TYERR("can't order elliptic curve infos");
1444 return (getbool(b
));
1449 static PyObject
*eimeth_check(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1451 static const char *const kwlist
[] = { "rng", 0 };
1452 grand
*r
= &rand_global
;
1455 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:check", KWLIST
,
1458 if ((p
= ec_checkinfo(ECINFO_EI(me
), r
)) != 0)
1465 static PyObject
*eiget_curve(PyObject
*me
, void *hunoz
)
1466 { RETURN_OBJ(ECINFO_COBJ(me
)); }
1468 static PyObject
*eiget_G(PyObject
*me
, void *hunoz
)
1470 ec_info
*ei
= ECINFO_EI(me
);
1472 EC_IN(ei
->c
, &p
, &ei
->g
);
1473 return (ecpt_pywrap(ECINFO_COBJ(me
), &p
));
1476 static PyObject
*eiget_r(PyObject
*me
, void *hunoz
)
1477 { return (mp_pywrap(MP_COPY(ECINFO_EI(me
)->r
))); }
1479 static PyObject
*eiget_h(PyObject
*me
, void *hunoz
)
1480 { return (mp_pywrap(MP_COPY(ECINFO_EI(me
)->h
))); }
1482 static const PyGetSetDef ecinfo_pygetset
[] = {
1483 #define GETSETNAME(op, name) ei##op##_##name
1484 GET (curve
, "I.curve -> the elliptic curve")
1485 GET (G
, "I.G -> generator point for the group")
1486 GET (r
, "I.r -> order of the group (and hence of G")
1487 GET (h
, "I.h -> cofactor of the group")
1492 static const PyMethodDef ecinfo_pymethods
[] = {
1493 #define METHNAME(name) eimeth_##name
1494 KWMETH(check
, "I.check([rng = rand]) -> None")
1495 SMTH (parse
, "parse(STR) -> (I, REST)")
1500 static const PyTypeObject ecinfo_pytype_skel
= {
1501 PyVarObject_HEAD_INIT(0, 0) /* Header */
1502 "ECInfo", /* @tp_name@ */
1503 sizeof(ecinfo_pyobj
), /* @tp_basicsize@ */
1504 0, /* @tp_itemsize@ */
1506 ecinfo_pydealloc
, /* @tp_dealloc@ */
1508 0, /* @tp_getattr@ */
1509 0, /* @tp_setattr@ */
1510 0, /* @tp_compare@ */
1512 0, /* @tp_as_number@ */
1513 0, /* @tp_as_sequence@ */
1514 0, /* @tp_as_mapping@ */
1518 0, /* @tp_getattro@ */
1519 0, /* @tp_setattro@ */
1520 0, /* @tp_as_buffer@ */
1521 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1522 Py_TPFLAGS_BASETYPE
,
1525 "ECInfo(CURVE, G, R, H): elliptic curve domain parameters.",
1527 0, /* @tp_traverse@ */
1529 ecinfo_pyrichcompare
, /* @tp_richcompare@ */
1530 0, /* @tp_weaklistoffset@ */
1532 0, /* @tp_iternext@ */
1533 PYMETHODS(ecinfo
), /* @tp_methods@ */
1534 0, /* @tp_members@ */
1535 PYGETSET(ecinfo
), /* @tp_getset@ */
1538 0, /* @tp_descr_get@ */
1539 0, /* @tp_descr_set@ */
1540 0, /* @tp_dictoffset@ */
1542 PyType_GenericAlloc
, /* @tp_alloc@ */
1543 ecinfo_pynew
, /* @tp_new@ */
1548 /*----- Setup -------------------------------------------------------------*/
1550 static const struct nameval consts
[] = {
1551 CONST(EC_XONLY
), CONST(EC_YBIT
), CONST(EC_LSB
),
1552 CONST(EC_CMPR
), CONST(EC_EXPLY
), CONST(EC_SORT
),
1556 void ec_pyinit(void)
1558 INITTYPE(ecpt
, root
);
1559 INITTYPE(ecptcurve
, ecpt
);
1560 INITTYPE(eccurve
, type
);
1561 INITTYPE(ecprimecurve
, eccurve
);
1562 INITTYPE(ecprimeprojcurve
, ecprimecurve
);
1563 INITTYPE(ecbincurve
, eccurve
);
1564 INITTYPE(ecbinprojcurve
, ecbincurve
);
1565 INITTYPE(ecinfo
, root
);
1568 static const char *ec_namefn(const void *p
)
1569 { const ecentry
*ec
= p
; return (ec
->name
); }
1571 static int ec_ixfn(const void *p
)
1573 const ecentry
*ec
= p
;
1576 for (i
= 0; ectab
[i
].name
; i
++)
1577 if (ectab
[i
].data
== ec
->data
) return (i
);
1581 static PyObject
*ec_valfn(int i
)
1585 ec_infofromdata(&ei
, ectab
[i
].data
);
1586 return (ecinfo_pywrap(&ei
));
1589 void ec_pyinsert(PyObject
*mod
)
1591 INSERT("ECPt", ecpt_pytype
);
1592 INSERT("ECPtCurve", ecptcurve_pytype
);
1593 INSERT("ECCurve", eccurve_pytype
);
1594 INSERT("ECPrimeCurve", ecprimecurve_pytype
);
1595 INSERT("ECPrimeProjCurve", ecprimeprojcurve_pytype
);
1596 INSERT("ECBinCurve", ecbincurve_pytype
);
1597 INSERT("ECBinProjCurve", ecbinprojcurve_pytype
);
1598 INSERT("ECInfo", ecinfo_pytype
);
1599 INSERT("eccurves", make_grouptab(ectab
, sizeof(*ectab
),
1600 ec_namefn
, ec_ixfn
, ec_valfn
));
1601 setconstants(mod
, consts
);
1604 /*----- That's all, folks -------------------------------------------------*/