buffer.c: Implement `ec2osp' and `os2ecp' as operations on buffers.
[catacomb-python] / buffer.c
index c616403..d0df068 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -184,6 +184,22 @@ end:
   return (rc);
 }
 
+static PyObject *rbmeth_os2ecp(PyObject *me, PyObject *arg, PyObject *kw)
+{
+  PyObject *cobj;
+  ec pt = EC_INIT;
+  unsigned f = EC_XONLY | EC_LSB | EC_SORT | EC_EXPLY;
+  PyObject *rc = 0;
+  static const char *const kwlist[] = { "curve", "flags", 0 };
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O&:os2ecp", KWLIST,
+                                  eccurve_pytype, &cobj, convuint, &f))
+    goto end;
+  if (ec_os2ecp(ECCURVE_C(cobj), f, BUF_B(me), &pt)) VALERR("bad point");
+  rc = ecpt_pywrapout(cobj, &pt);
+end:
+  return (rc);
+}
+
 static PyObject *rbmeth_getge(PyObject *me, PyObject *arg)
 {
   PyObject *gobj;
@@ -257,6 +273,7 @@ static const PyMethodDef rbuf_pymethods[] = {
   NAMETH(getgf,                "RBUF.getgf() -> X")
   KWMETH(getecpt,      "RBUF.getecpt([curve = None]) -> P")
   METH (getecptraw,    "RBUF.getecptraw(CURVE) -> P")
+  KWMETH(os2ecp,       "RBUF.os2ecp(CURVE, [flags = ...]) -> P")
   METH (getge,         "RBUF.getge(GROUP) -> X")
   METH (getgeraw,      "RBUF.getgeraw(GROUP) -> X")
 #undef METHNAME
@@ -477,6 +494,25 @@ static PyObject *wbmeth_putecptraw(PyObject *me, PyObject *arg)
   RETURN_ME;
 }
 
+static PyObject *wbmeth_ec2osp(PyObject *me, PyObject *arg, PyObject *kw)
+{
+  PyTypeObject *ptobj;
+  ec_curve *cc;
+  ec pt = EC_INIT;
+  unsigned f = EC_EXPLY;
+  static const char *const kwlist[] = { "point", "flags", 0 };
+  if (!PyArg_ParseTupleAndKeywords(arg, kw, "O!|O&:os2ecp", KWLIST,
+                                  ecptcurve_pytype, &ptobj, convuint, &f))
+    goto end;
+  cc = ECPT_C(ptobj);
+  EC_OUT(cc, &pt, ECPT_P(ptobj));
+  if (ensurebuf(me, 2*cc->f->noctets + 1)) return (0);
+  if (ec_ec2osp(cc, f, BUF_B(me), &pt)) VALERR("invalid flags");
+  RETURN_ME;
+end:
+  return (0);
+}
+
 static PyObject *wbmeth_putge(PyObject *me, PyObject *arg)
 {
   PyObject *geobj;
@@ -523,6 +559,7 @@ static const PyMethodDef wbuf_pymethods[] = {
   METH (putgf,         "WBUF.putgf(X)")
   METH (putecpt,       "WBUF.putecpt(P)")
   METH (putecptraw,    "WBUF.putecptraw(P)")
+  KWMETH(ec2osp,       "WBUF.ec2osp(P, [flags = EC_EXPLY])")
   METH (putge,         "WBUF.putge(X)")
   METH (putgeraw,      "WBUF.putgeraw(X)")
 #undef METHNAME