ec.c, field.c, group.c: Allow exponents and scalars from prime fields.
[catacomb-python] / ec.c
diff --git a/ec.c b/ec.c
index 858aeaa..9535240 100644 (file)
--- a/ec.c
+++ b/ec.c
@@ -186,7 +186,11 @@ static PyObject *ecpt_pymul(PyObject *x, PyObject *y)
   ec zz = EC_INIT;
 
   if (ECPT_PYCHECK(x)) { PyObject *t; t = x; x = y; y = t; }
-  if (!ECPT_PYCHECK(y) || (xx = tomp(x)) == 0) RETURN_NOTIMPL;
+  if (!ECPT_PYCHECK(y)) RETURN_NOTIMPL;
+  if (FE_PYCHECK(x) && FE_F(x)->ops->ty == FTY_PRIME)
+    xx = F_OUT(FE_F(x), MP_NEW, FE_X(x));
+  else if ((xx = implicitmp(x)) == 0)
+    RETURN_NOTIMPL;
   ec_imul(ECPT_C(y), &zz, ECPT_P(y), xx);
   MP_DROP(xx);
   return (ecpt_pywrap(ECPT_COBJ(y), &zz));
@@ -251,10 +255,10 @@ static PyObject *epmeth_tobuf(PyObject *me)
   else
     n = mp_octets(p.x) + mp_octets(p.y) + 6;
   rc = bytestring_pywrap(0, n);
-  buf_init(&b, PyString_AS_STRING(rc), n);
+  buf_init(&b, BIN_PTR(rc), n);
   buf_putec(&b, &p);
   assert(BOK(&b));
-  _PyString_Resize(&rc, BLEN(&b));
+  BIN_SETLEN(rc, BLEN(&b));
   EC_DESTROY(&p);
   return (rc);
 }
@@ -270,12 +274,12 @@ static PyObject *epmeth_toraw(PyObject *me)
 
   len = c->f->noctets * 2 + 1;
   rc = bytestring_pywrap(0, len);
-  p = PyString_AS_STRING(rc);
+  p = BIN_PTR(rc);
   buf_init(&b, p, len);
   EC_OUT(c, &pp, ECPT_P(me));
   ec_putraw(c, &b, &pp);
   EC_DESTROY(&pp);
-  _PyString_Resize(&rc, BLEN(&b));
+  BIN_SETLEN(rc, BLEN(&b));
   return (rc);
 }
 
@@ -295,7 +299,7 @@ static PyObject *epmeth_ec2osp(PyObject *me, PyObject *arg, PyObject *kw)
     return (0);
   len = c->f->noctets * 2 + 1;
   rc = bytestring_pywrap(0, len);
-  p = PyString_AS_STRING(rc);
+  p = BIN_PTR(rc);
   buf_init(&b, p, len);
   EC_OUT(c, &pp, ECPT_P(me));
   if (ec_ec2osp(c, f, &b, &pp)) {
@@ -303,21 +307,20 @@ static PyObject *epmeth_ec2osp(PyObject *me, PyObject *arg, PyObject *kw)
     VALERR("invalid flags");
   }
   EC_DESTROY(&pp);
-  _PyString_Resize(&rc, BLEN(&b));
+  BIN_SETLEN(rc, BLEN(&b));
 end:
   return (rc);
 }
 
 static PyObject *epmeth_frombuf(PyObject *me, PyObject *arg)
 {
+  struct bin in;
   buf b;
-  char *p;
-  Py_ssize_t sz;
   PyObject *rc = 0;
   ec pp = EC_INIT;
 
-  if (!PyArg_ParseTuple(arg, "s#:frombuf", &p, &sz)) goto end;
-  buf_init(&b, p, sz);
+  if (!PyArg_ParseTuple(arg, "O&:frombuf", convbin, &in)) goto end;
+  buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
   if (buf_getec(&b, &pp)) VALERR("malformed data");
   rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp),
                     bytestring_pywrapbuf(&b));
@@ -342,15 +345,14 @@ end:
 
 static PyObject *epmeth_fromraw(PyObject *me, PyObject *arg)
 {
-  char *p;
-  Py_ssize_t len;
+  struct bin in;
   buf b;
   PyObject *rc = 0;
   ec_curve *cc;
   ec pp = EC_INIT;
 
-  if (!PyArg_ParseTuple(arg, "s#:fromraw", &me, &p, &len)) return (0);
-  buf_init(&b, p, len);
+  if (!PyArg_ParseTuple(arg, "O&:fromraw", convbin, &in)) return (0);
+  buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
   cc = ECCURVE_C(me);
   if (ec_getraw(cc, &b, &pp))
     VALERR("bad point");
@@ -362,8 +364,7 @@ end:
 
 static PyObject *epmeth_os2ecp(PyObject *me, PyObject *arg, PyObject *kw)
 {
-  char *p;
-  Py_ssize_t len;
+  struct bin in;
   buf b;
   PyObject *rc = 0;
   ec_curve *cc;
@@ -371,10 +372,10 @@ static PyObject *epmeth_os2ecp(PyObject *me, PyObject *arg, PyObject *kw)
   ec pp = EC_INIT;
   static const char *const kwlist[] = { "buf", "flags", 0 };
 
-  if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O&:os2ecp", KWLIST,
-                                  &p, &len, convuint, &f))
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:os2ecp", KWLIST,
+                                  convbin, &in, convuint, &f))
     return (0);
-  buf_init(&b, p, len);
+  buf_init(&b, (/*unconst*/ void *)in.p, in.sz);
   cc = ECCURVE_C(me);
   if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point");
   EC_IN(cc, &pp, &pp);
@@ -510,7 +511,6 @@ static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
   int rc = -1;
   PyObject *y = 0, *z = 0, *t = 0;
   mp *xx = 0;
-  const void *q;
   Py_ssize_t n;
   qd_parse qd;
 
@@ -520,10 +520,8 @@ static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
   else if (ECPT_PYCHECK(x)) {
     getecptout(p, x);
     goto fix;
-  } else if (PyString_Check(x)) {
-    if (PyObject_AsReadBuffer(x, &q, &n))
-      goto end;
-    qd.p = q;
+  } else if (TEXT_CHECK(x)) {
+    qd.p = TEXT_PTR(x);
     qd.e = 0;
     if (!ec_ptparse(&qd, p))
       VALERR(qd.e);
@@ -595,6 +593,7 @@ end:
   return (rc);
 }
 
+#ifdef PY2
 static PyObject *ecpt_pylong(PyObject *me)
 {
   ec p = EC_INIT;
@@ -606,6 +605,7 @@ end:
   EC_DESTROY(&p);
   return (rc);
 }
+#endif
 
 static PyObject *ecpt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
 {
@@ -645,7 +645,9 @@ static const PyNumberMethods ecpt_pynumber = {
   0,                                   /* @nb_add@ */
   0,                                   /* @nb_subtract@ */
   0,                                   /* @nb_multiply@ */
+#ifdef PY2
   0,                                   /* @nb_divide@ */
+#endif
   0,                                   /* @nb_remainder@ */
   0,                                   /* @nb_divmod@ */
   0,                                   /* @nb_power@ */
@@ -659,17 +661,23 @@ static const PyNumberMethods ecpt_pynumber = {
   0,                                   /* @nb_and@ */
   0,                                   /* @nb_xor@ */
   0,                                   /* @nb_or@ */
+#ifdef PY2
   0,                                   /* @nb_coerce@ */
+#endif
   ecpt_pyint,                          /* @nb_int@ */
-  ecpt_pylong,                         /* @nb_long@ */
+  PY23(ecpt_pylong, 0),                        /* @nb_long@ */
   0,                                   /* @nb_float@ */
+#ifdef PY2
   0,                                   /* @nb_oct@ */
   0,                                   /* @nb_hex@ */
+#endif
 
   0,                                   /* @nb_inplace_add@ */
   0,                                   /* @nb_inplace_subtract@ */
   0,                                   /* @nb_inplace_multiply@ */
+#ifdef PY2
   0,                                   /* @nb_inplace_divide@ */
+#endif
   0,                                   /* @nb_inplace_remainder@ */
   0,                                   /* @nb_inplace_power@ */
   0,                                   /* @nb_inplace_lshift@ */
@@ -737,7 +745,7 @@ static const PyTypeObject ecpt_pytype_skel = {
 
 static const PyMemberDef ecpt_pymembers[] = {
 #define MEMBERSTRUCT ecpt_pyobj
-  MEMRNM(curve, T_OBJECT, ob_type, READONLY,
+  MEMRNM(curve, T_OBJECT, PY23(ob_type, ob_base.ob_type), READONLY,
                                    "P.curve -> elliptic curve containing P")
 #undef MEMBERSTRUCT
   { 0 }
@@ -771,7 +779,9 @@ static const PyNumberMethods ecptcurve_pynumber = {
   ecpt_pyadd,                          /* @nb_add@ */
   ecpt_pysub,                          /* @nb_subtract@ */
   ecpt_pymul,                          /* @nb_multiply@ */
+#ifdef PY2
   0,                                   /* @nb_divide@ */
+#endif
   0,                                   /* @nb_remainder@ */
   0,                                   /* @nb_divmod@ */
   0,                                   /* @nb_power@ */
@@ -785,12 +795,16 @@ static const PyNumberMethods ecptcurve_pynumber = {
   0,                                   /* @nb_and@ */
   0,                                   /* @nb_xor@ */
   0,                                   /* @nb_or@ */
+#ifdef PY2
   0,                                   /* @nb_coerce@ */
+#endif
   0,                                   /* @nb_int@ */
   0,                                   /* @nb_long@ */
   0,                                   /* @nb_float@ */
+#ifdef PY2
   0,                                   /* @nb_oct@ */
   0,                                   /* @nb_hex@ */
+#endif
 
   0,                                   /* @nb_inplace_add@ */
   0,                                   /* @nb_inplace_subtract@ */
@@ -1034,7 +1048,7 @@ end:
 }
 
 static PyObject *ecget_name(PyObject *me, void *hunoz)
-  { return (PyString_FromString(EC_NAME(ECCURVE_C(me)))); }
+  { return (TEXT_FROMSTR(EC_NAME(ECCURVE_C(me)))); }
 
 static PyObject *ecget_a(PyObject *me, void *hunoz)
   { return (fe_pywrap(ECCURVE_FOBJ(me), MP_COPY(ECCURVE_C(me)->a))); }
@@ -1153,7 +1167,7 @@ static const PyTypeObject ecprimecurve_pytype_skel = {
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
-  eccurve_pyrichcompare,               /* @tp_richcompare@ */
+  0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
@@ -1208,7 +1222,7 @@ static const PyTypeObject ecprimeprojcurve_pytype_skel = {
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
-  eccurve_pyrichcompare,               /* @tp_richcompare@ */
+  0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
@@ -1263,7 +1277,7 @@ static const PyTypeObject ecbincurve_pytype_skel = {
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
-  eccurve_pyrichcompare,               /* @tp_richcompare@ */
+  0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */
@@ -1318,7 +1332,7 @@ static const PyTypeObject ecbinprojcurve_pytype_skel = {
 
   0,                                   /* @tp_traverse@ */
   0,                                   /* @tp_clear@ */
-  eccurve_pyrichcompare,               /* @tp_richcompare@ */
+  0,                                   /* @tp_richcompare@ */
   0,                                   /* @tp_weaklistoffset@ */
   0,                                   /* @tp_iter@ */
   0,                                   /* @tp_iternext@ */