{ 0 }
};
+#define KSZCONVOP(op) \
+ static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
+ { \
+ double x, y; \
+ if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
+ y = keysz_##op(x); \
+ return (PyFloat_FromDouble(y)); \
+ }
+KSZCONVOP(fromdl)
+KSZCONVOP(fromschnorr)
+KSZCONVOP(fromif)
+KSZCONVOP(fromec)
+KSZCONVOP(todl)
+KSZCONVOP(toschnorr)
+KSZCONVOP(toif)
+KSZCONVOP(toec)
+#undef KSZCONVOP
+
static const PyGetSetDef keyszany_pygetset[] = {
#define GETSETNAME(op, name) ka##op##_##name
GET (min, "KSZ.min -> smallest allowed key size")
0 /* @tp_is_gc@ */
};
-#define KSZCONVOP(op) \
- static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
- { \
- double x, y; \
- if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
- y = keysz_##op(x); \
- return (PyFloat_FromDouble(y)); \
- }
-KSZCONVOP(fromdl)
-KSZCONVOP(fromschnorr)
-KSZCONVOP(fromif)
-KSZCONVOP(fromec)
-KSZCONVOP(todl)
-KSZCONVOP(toschnorr)
-KSZCONVOP(toif)
-KSZCONVOP(toec)
-#undef KSZCONVOP
-
/*----- Symmetric encryption ----------------------------------------------*/
static PyTypeObject *gccipher_pytype, *gcipher_pytype;
static PyObject *epget_curve(PyObject *me, void *hunoz)
{ RETURN_OBJ(ECPT_COBJ(me)); }
+static PyObject *meth__ECPt_frombuf(PyObject *me, PyObject *arg)
+{
+ buf b;
+ char *p;
+ Py_ssize_t sz;
+ PyObject *rc = 0;
+ ec pp = EC_INIT;
+
+ if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end;
+ buf_init(&b, p, sz);
+ if (buf_getec(&b, &pp)) VALERR("malformed data");
+ rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp),
+ bytestring_pywrapbuf(&b));
+end:
+ return (rc);
+}
+
+static PyObject *meth__ECPt_parse(PyObject *me, PyObject *arg)
+{
+ char *p;
+ qd_parse qd;
+ PyObject *rc = 0;
+ ec pp = EC_INIT;
+
+ if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
+ qd.p = p; qd.e = 0;
+ if (!ec_ptparse(&qd, &pp)) VALERR(qd.e);
+ rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p);
+end:
+ return (rc);
+}
+
+static PyObject *meth__ECPtCurve_fromraw(PyObject *me, PyObject *arg)
+{
+ char *p;
+ Py_ssize_t len;
+ buf b;
+ PyObject *rc = 0;
+ ec_curve *cc;
+ ec pp = EC_INIT;
+
+ if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &len))
+ return (0);
+ buf_init(&b, p, len);
+ cc = ECCURVE_C(me);
+ if (ec_getraw(cc, &b, &pp))
+ VALERR("bad point");
+ EC_IN(cc, &pp, &pp);
+ rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
+end:
+ return (rc);
+}
+
+static PyObject *meth__ECPtCurve_os2ecp(PyObject *me,
+ PyObject *arg, PyObject *kw)
+{
+ char *p;
+ Py_ssize_t len;
+ buf b;
+ PyObject *rc = 0;
+ ec_curve *cc;
+ unsigned f = EC_XONLY | EC_LSB | EC_SORT | EC_EXPLY;
+ ec pp = EC_INIT;
+ static const char *const kwlist[] = { "class", "buf", "flags", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|O&:os2ecp", KWLIST,
+ &me, &p, &len, convuint, &f))
+ return (0);
+ buf_init(&b, p, len);
+ cc = ECCURVE_C(me);
+ if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point");
+ EC_IN(cc, &pp, &pp);
+ rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
+end:
+ return (rc);
+}
+
static PyObject *epncget_ix(PyObject *me, void *hunoz)
{
ec p = EC_INIT;
ecmmul_id, ecmmul_fill, ecmmul_exp, ecmmul_drop));
}
-static PyObject *meth__ECPtCurve_fromraw(PyObject *me, PyObject *arg)
-{
- char *p;
- Py_ssize_t len;
- buf b;
- PyObject *rc = 0;
- ec_curve *cc;
- ec pp = EC_INIT;
-
- if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &len))
- return (0);
- buf_init(&b, p, len);
- cc = ECCURVE_C(me);
- if (ec_getraw(cc, &b, &pp))
- VALERR("bad point");
- EC_IN(cc, &pp, &pp);
- rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
-end:
- return (rc);
-}
-
-static PyObject *meth__ECPtCurve_os2ecp(PyObject *me,
- PyObject *arg, PyObject *kw)
-{
- char *p;
- Py_ssize_t len;
- buf b;
- PyObject *rc = 0;
- ec_curve *cc;
- unsigned f = EC_XONLY | EC_LSB | EC_SORT | EC_EXPLY;
- ec pp = EC_INIT;
- static const char *const kwlist[] = { "class", "buf", "flags", 0 };
-
- if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|O&:os2ecp", KWLIST,
- &me, &p, &len, convuint, &f))
- return (0);
- buf_init(&b, p, len);
- cc = ECCURVE_C(me);
- if (ec_os2ecp(cc, f, &b, &pp)) VALERR("bad point");
- EC_IN(cc, &pp, &pp);
- rc = Py_BuildValue("(NN)", ecpt_pywrap(me, &pp), bytestring_pywrapbuf(&b));
-end:
- return (rc);
-}
-
-static PyObject *meth__ECPt_frombuf(PyObject *me, PyObject *arg)
-{
- buf b;
- char *p;
- Py_ssize_t sz;
- PyObject *rc = 0;
- ec pp = EC_INIT;
-
- if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &sz)) goto end;
- buf_init(&b, p, sz);
- if (buf_getec(&b, &pp)) VALERR("malformed data");
- rc = Py_BuildValue("(NN)", ecpt_pywrapout(me, &pp),
- bytestring_pywrapbuf(&b));
-end:
- return (rc);
-}
-
-static PyObject *meth__ECPt_parse(PyObject *me, PyObject *arg)
-{
- char *p;
- qd_parse qd;
- PyObject *rc = 0;
- ec pp = EC_INIT;
-
- if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
- qd.p = p;
- qd.e = 0;
- if (!ec_ptparse(&qd, &pp)) VALERR(qd.e);
- rc = Py_BuildValue("(Ns)", ecpt_pywrapout(me, &pp), qd.p);
-end:
- return (rc);
-}
-
static void eccurve_pydealloc(PyObject *me)
{
ec_destroycurve(ECCURVE_C(me));
return (ecpt_pywrap(me, &p));
}
+static PyObject *meth__ECCurve_parse(PyObject *me, PyObject *arg)
+{
+ char *p;
+ qd_parse qd;
+ ec_curve *c;
+ PyObject *rc = 0;
+
+ if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
+ qd.p = p; qd.e = 0;
+ if ((c = ec_curveparse(&qd)) == 0) VALERR(qd.e);
+ rc = eccurve_pywrap(0, c);
+end:
+ return (rc);
+}
+
static PyObject *eccurve_dopywrap(PyTypeObject *ty,
PyObject *fobj, ec_curve *c)
{
return (cobj);
}
-static PyObject *meth__ECCurve_parse(PyObject *me, PyObject *arg)
-{
- char *p;
- qd_parse qd;
- ec_curve *c;
- PyObject *rc = 0;
-
- if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
- goto end;
- qd.p = p;
- qd.e = 0;
- if ((c = ec_curveparse(&qd)) == 0)
- VALERR(qd.e);
- rc = eccurve_pywrap(0, c);
-end:
- return (rc);
-}
-
static PyObject *ecget_name(PyObject *me, void *hunoz)
{ return (PyString_FromString(EC_NAME(ECCURVE_C(me)))); }
ec_info ei;
PyObject *rc = 0;
- if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
- goto end;
- qd.p = p;
- qd.e = 0;
- if (ec_infoparse(&qd, &ei))
- VALERR(qd.e);
+ if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
+ qd.p = p; qd.e = 0;
+ if (ec_infoparse(&qd, &ei)) VALERR(qd.e);
rc = Py_BuildValue("(Ns)", ecinfo_pywrap(&ei), qd.p);
end:
return (rc);
return (fe_pywrap(me, xx));
}
+static PyObject *meth__Field_parse(PyObject *me, PyObject *arg)
+{
+ field *f;
+ char *p;
+ PyObject *rc = 0;
+ qd_parse qd;
+
+ if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
+ qd.p = p; qd.e = 0;
+ if ((f = field_parse(&qd)) == 0) VALERR(qd.e);
+ rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
+end:
+ return (rc);
+}
+
static void field_pydealloc(PyObject *me)
{
F_DESTROY(FIELD_F(me));
/*----- Setup -------------------------------------------------------------*/
-static PyObject *meth__Field_parse(PyObject *me, PyObject *arg)
-{
- field *f;
- char *p;
- PyObject *rc = 0;
- qd_parse qd;
-
- if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
- goto end;
- qd.p = p;
- qd.e = 0;
- if ((f = field_parse(&qd)) == 0)
- VALERR(qd.e);
- rc = Py_BuildValue("(Ns)", field_pywrap(f), qd.p);
-end:
- return (rc);
-}
-
static const PyMethodDef methods[] = {
#define METHNAME(func) meth_##func
METH (_Field_parse, "parse(STR) -> F, REST")
PyObject *rc = 0;
if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
- qd.p = p;
- qd.e = 0;
+ qd.p = p; qd.e = 0;
if (parse(&qd, &gp)) VALERR(qd.e);
rc = fginfo_pywrap(&gp, ty);
end:
group *g;
ge *x = 0;
- if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &n))
- return (0);
+ if (!PyArg_ParseTuple(arg, "Os#:frombuf", &me, &p, &n)) return (0);
g = GROUP_G(me);
buf_init(&b, p, n);
x = G_CREATE(g);
- if (G_FROMBUF(g, &b, x))
- VALERR("invalid data");
+ if (G_FROMBUF(g, &b, x)) VALERR("invalid data");
return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
end:
if (x) G_DESTROY(g, x);
group *g;
ge *x = 0;
- if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &n))
- return (0);
+ if (!PyArg_ParseTuple(arg, "Os#:fromraw", &me, &p, &n)) return (0);
g = GROUP_G(me);
buf_init(&b, p, n);
x = G_CREATE(g);
- if (G_FROMRAW(g, &b, x))
- VALERR("invalid data");
+ if (G_FROMRAW(g, &b, x)) VALERR("invalid data");
return (Py_BuildValue("(NN)", ge_pywrap(me, x), bytestring_pywrapbuf(&b)));
end:
if (x) G_DESTROY(g, x);
group *g;
ge *x = 0;
- if (!PyArg_ParseTuple(arg, "Os#:fromstring", &me, &p, &n))
- return (0);
+ if (!PyArg_ParseTuple(arg, "Os#:fromstring", &me, &p, &n)) return (0);
sc.buf = p;
sc.lim = sc.buf + n;
g = GROUP_G(me);
qd_parse qd;
group *g;
- if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p))
- goto end;
- qd.p = p;
- qd.e = 0;
- if ((g = group_parse(&qd)) == 0)
- VALERR(qd.e);
+ if (!PyArg_ParseTuple(arg, "Os:parse", &me, &p)) goto end;
+ qd.p = p; qd.e = 0;
+ if ((g = group_parse(&qd)) == 0) VALERR(qd.e);
return (group_pywrap(g));
end:
return (0);
PyObject *rc = 0;
int err;
- if (!PyArg_ParseTuple(arg, "Os:key_readflags", &me, &p))
- goto end;
- if ((err = key_readflags(p, &end, &f, &m)) != 0)
- KEYERR(err);
+ if (!PyArg_ParseTuple(arg, "Os:key_readflags", &me, &p)) goto end;
+ if ((err = key_readflags(p, &end, &f, &m)) != 0) KEYERR(err);
rc = Py_BuildValue("(NNs)", getulong(f), getulong(m), end);
end:
return (rc);
key_data *kd;
PyObject *rc = 0;
- if (!PyArg_ParseTuple(arg, "Os:read", &me, &p))
- goto end;
- if ((kd = key_read(p, &end)) == 0)
- KEYERR(KERR_MALFORMED);
+ if (!PyArg_ParseTuple(arg, "Os:read", &me, &p)) goto end;
+ if ((kd = key_read(p, &end)) == 0) KEYERR(KERR_MALFORMED);
rc = Py_BuildValue("(Ns)", keydata_pywrap(kd), end);
end:
return (rc);
key_data *kd;
PyObject *rc = 0;
- if (!PyArg_ParseTuple(arg, "Os#:decode", &me, &p, &n))
- goto end;
- if ((kd = key_decode(p, n)) == 0)
- KEYERR(KERR_MALFORMED);
+ if (!PyArg_ParseTuple(arg, "Os#:decode", &me, &p, &n)) goto end;
+ if ((kd = key_decode(p, n)) == 0) KEYERR(KERR_MALFORMED);
rc = keydata_pywrap(kd);
end:
return (rc);
return (rc);
}
+static PyObject *meth__MP_fromstring(PyObject *me,
+ PyObject *arg, PyObject *kw)
+{
+ int r = 0;
+ char *p;
+ Py_ssize_t len;
+ PyObject *z = 0;
+ mp *zz;
+ mptext_stringctx sc;
+ static const char *const kwlist[] = { "class", "x", "radix", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|i:fromstring",
+ KWLIST, &me, &p, &len, &r))
+ goto end;
+ if (!good_radix_p(r, 1)) VALERR("bad radix");
+ sc.buf = p; sc.lim = p + len;
+ if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0)
+ VALERR("bad integer");
+ z = Py_BuildValue("(Ns#)", mp_pywrap(zz),
+ sc.buf, (Py_ssize_t)(sc.lim - sc.buf));
+end:
+ return (z);
+}
+
+static PyObject *meth__MP_factorial(PyObject *me, PyObject *arg)
+{
+ unsigned long i;
+ mp *x;
+ if (!PyArg_ParseTuple(arg, "OO&:factorial", &me, convulong, &i)) return (0);
+ x = mp_factorial(i);
+ return mp_pywrap(x);
+}
+
+static PyObject *meth__MP_fibonacci(PyObject *me, PyObject *arg)
+{
+ long i;
+ mp *x;
+ if (!PyArg_ParseTuple(arg, "Ol:fibonacci", &me, &i)) return (0);
+ x = mp_fibonacci(i);
+ return mp_pywrap(x);
+}
+
+#define LOADOP(pre, py, name) \
+ static PyObject *meth__##py##_##name(PyObject *me, PyObject *arg) \
+ { \
+ char *p; \
+ Py_ssize_t len; \
+ if (!PyArg_ParseTuple(arg, "Os#:" #name, &me, &p, &len)) return (0); \
+ return (pre##_pywrap(mp_##name(MP_NEW, p, len))); \
+ }
+LOADOP(mp, MP, loadl)
+LOADOP(mp, MP, loadb)
+LOADOP(mp, MP, loadl2c)
+LOADOP(mp, MP, loadb2c)
+LOADOP(gf, GF, loadl)
+LOADOP(gf, GF, loadb)
+#undef LOADOP
+
static PyObject *mpget_nbits(PyObject *me, void *hunoz)
{ return (PyInt_FromLong(mp_bits(MP_X(me)))); }
0 /* @tp_is_gc@ */
};
-static PyObject *meth__MP_fromstring(PyObject *me,
- PyObject *arg, PyObject *kw)
-{
- int r = 0;
- char *p;
- Py_ssize_t len;
- PyObject *z = 0;
- mp *zz;
- mptext_stringctx sc;
- static const char *const kwlist[] = { "class", "x", "radix", 0 };
-
- if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|i:fromstring",
- KWLIST, &me, &p, &len, &r))
- goto end;
- if (!good_radix_p(r, 1)) VALERR("bad radix");
- sc.buf = p; sc.lim = p + len;
- if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0)
- VALERR("bad integer");
- z = Py_BuildValue("(Ns#)", mp_pywrap(zz),
- sc.buf, (Py_ssize_t)(sc.lim - sc.buf));
-end:
- return (z);
-}
-
-static PyObject *meth__MP_factorial(PyObject *me, PyObject *arg)
-{
- unsigned long i;
- mp *x;
- if (!PyArg_ParseTuple(arg, "OO&:factorial", &me, convulong, &i))
- return (0);
- x = mp_factorial(i);
- return mp_pywrap(x);
-}
-
-static PyObject *meth__MP_fibonacci(PyObject *me, PyObject *arg)
-{
- long i;
- mp *x;
- if (!PyArg_ParseTuple(arg, "Ol:fibonacci", &me, &i)) return (0);
- x = mp_fibonacci(i);
- return mp_pywrap(x);
-}
-
-#define LOADOP(pre, py, name) \
- static PyObject *meth__##py##_##name(PyObject *me, PyObject *arg) \
- { \
- char *p; \
- Py_ssize_t len; \
- if (!PyArg_ParseTuple(arg, "Os#:" #name, &me, &p, &len)) return (0); \
- return (pre##_pywrap(mp_##name(MP_NEW, p, len))); \
- }
-LOADOP(mp, MP, loadl)
-LOADOP(mp, MP, loadb)
-LOADOP(mp, MP, loadl2c)
-LOADOP(mp, MP, loadb2c)
-LOADOP(gf, GF, loadl)
-LOADOP(gf, GF, loadb)
-#undef LOADOP
-
/*----- Products of small integers ----------------------------------------*/
typedef struct mpmul_pyobj {
return (z);
}
+static PyObject *meth__GF_fromstring(PyObject *me,
+ PyObject *arg, PyObject *kw)
+{
+ int r = 0;
+ char *p;
+ Py_ssize_t len;
+ PyObject *z = 0;
+ mp *zz;
+ mptext_stringctx sc;
+ static const char *const kwlist[] = { "class", "x", "radix", 0 };
+
+ if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|i:fromstring",
+ KWLIST, &me, &p, &len, &r))
+ goto end;
+ if (!good_radix_p(r, 1)) VALERR("bad radix");
+ sc.buf = p; sc.lim = p + len;
+ if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0 ||
+ MP_NEGP(zz)) {
+ if (zz) MP_DROP(zz);
+ VALERR("bad binary polynomial");
+ }
+ z = Py_BuildValue("(Ns#)", gf_pywrap(zz),
+ sc.buf, (Py_ssize_t)(sc.lim - sc.buf));
+end:
+ return (z);
+}
+
static PyObject *gfmeth_irreduciblep(PyObject *me, PyObject *arg)
{
if (!PyArg_ParseTuple(arg, ":irreduciblep")) return (0);
0 /* @tp_is_gc@ */
};
-static PyObject *meth__GF_fromstring(PyObject *me,
- PyObject *arg, PyObject *kw)
-{
- int r = 0;
- char *p;
- Py_ssize_t len;
- PyObject *z = 0;
- mp *zz;
- mptext_stringctx sc;
- static const char *const kwlist[] = { "class", "x", "radix", 0 };
-
- if (!PyArg_ParseTupleAndKeywords(arg, kw, "Os#|i:fromstring",
- KWLIST, &me, &p, &len, &r))
- goto end;
- if (!good_radix_p(r, 1)) VALERR("bad radix");
- sc.buf = p; sc.lim = p + len;
- if ((zz = mp_read(MP_NEW, r, &mptext_stringops, &sc)) == 0 ||
- MP_NEGP(zz)) {
- if (zz) MP_DROP(zz);
- VALERR("bad binary polynomial");
- }
- z = Py_BuildValue("(Ns#)", gf_pywrap(zz),
- sc.buf, (Py_ssize_t)(sc.lim - sc.buf));
-end:
- return (z);
-}
-
/*----- Sparse poly reduction ---------------------------------------------*/
typedef struct gfreduce_pyobj {