X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/c461c9b380b56a41399e5f540e4bb97cfd9ceffe..refs/heads/mdw/freebin:/share.c diff --git a/share.c b/share.c index ad56d31..30bff92 100644 --- a/share.c +++ b/share.c @@ -53,7 +53,7 @@ static PyObject *gfsget_threshold(PyObject *me, void *hunoz) static PyObject *gfsget_size(PyObject *me, void *hunoz) { return (PyInt_FromLong(GFSHARE_S(me)->sz)); } -static PyGetSetDef gfshare_pygetset[]= { +static const PyGetSetDef gfshare_pygetset[]= { #define GETSETNAME(op, name) gfs##op##_##name GET (threshold, "S.threshold -> THRESHOLD") GET (size, "S.size -> SECRETSZ") @@ -61,8 +61,8 @@ static PyGetSetDef gfshare_pygetset[]= { { 0 } }; -static PyTypeObject gfshare_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ +static const PyTypeObject gfshare_pytype_skel = { + PyVarObject_HEAD_INIT(0, 0) /* Header */ "GFShare", /* @tp_name@ */ sizeof(gfshare_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -86,7 +86,7 @@ static PyTypeObject gfshare_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Binary-field secret sharing base class.", + "Binary-field secret sharing base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -96,7 +96,7 @@ static PyTypeObject gfshare_pytype_skel = { 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ - gfshare_pygetset, /* @tp_getset@ */ + PYGETSET(gfshare), /* @tp_getset@ */ 0, /* @tp_base@ */ 0, /* @tp_dict@ */ 0, /* @tp_descr_get@ */ @@ -112,19 +112,19 @@ static PyTypeObject gfshare_pytype_skel = { static PyObject *gfsharesplit_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { - char *p; - int n; + struct bin in; unsigned t; grand *r = &rand_global; gfshare_pyobj *s; - char *kwlist[] = { "threshold", "secret", "rng", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&s#|O&:new", kwlist, - convuint, &t, &p, &n, convgrand, &r)) + static const char *const kwlist[] = { "threshold", "secret", "rng", 0 }; + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&:new", KWLIST, + convuint, &t, convbin, &in, + convgrand, &r)) goto end; if (!t || t > 255) VALERR("threshold must be nonzero and < 256"); s = (gfshare_pyobj *)ty->tp_alloc(ty, 0); - gfshare_create(&s->s, t, n); - gfshare_mkshares(&s->s, r, p); + gfshare_create(&s->s, t, in.sz); + gfshare_mkshares(&s->s, r, in.p); return ((PyObject *)s); end: return (0); @@ -137,20 +137,20 @@ static PyObject *gfsmeth_get(PyObject *me, PyObject *arg) if (!PyArg_ParseTuple(arg, "O&:get", convuint, &i)) goto end; if (i >= 255) VALERR("index must be < 255"); rc = bytestring_pywrap(0, GFSHARE_S(me)->sz); - gfshare_get(GFSHARE_S(me), i, PyString_AS_STRING(rc)); + gfshare_get(GFSHARE_S(me), i, BIN_PTR(rc)); end: return (rc); } -static PyMethodDef gfsharesplit_pymethods[] = { +static const PyMethodDef gfsharesplit_pymethods[] = { #define METHNAME(name) gfsmeth_##name METH (get, "S.get(I) -> SHARE") #undef METHNAME { 0 } }; -static PyTypeObject gfsharesplit_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ +static const PyTypeObject gfsharesplit_pytype_skel = { + PyVarObject_HEAD_INIT(0, 0) /* Header */ "GFShareSplit", /* @tp_name@ */ sizeof(gfshare_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -174,7 +174,8 @@ static PyTypeObject gfsharesplit_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Binary field secret sharing: split secret into shares.", + "GFShareSplit(THRESHOLD, SECRET, [rng = rand]): binary-field sharing:\n" + " split secret into shares.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -182,7 +183,7 @@ static PyTypeObject gfsharesplit_pytype_skel = { 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ - gfsharesplit_pymethods, /* @tp_methods@ */ + PYMETHODS(gfsharesplit), /* @tp_methods@ */ 0, /* @tp_members@ */ 0, /* @tp_getset@ */ 0, /* @tp_base@ */ @@ -202,8 +203,8 @@ static PyObject *gfsharejoin_pynew(PyTypeObject *ty, { unsigned t, sz; gfshare_pyobj *s; - char *kwlist[] = { "threshold", "size", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", kwlist, + static const char *const kwlist[] = { "threshold", "size", 0 }; + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", KWLIST, convuint, &t, convuint, &sz)) goto end; if (!t || t > 255) VALERR("threshold must be nonzero and < 256"); @@ -227,35 +228,34 @@ end: static PyObject *gfsmeth_add(PyObject *me, PyObject *arg) { unsigned i; - char *p; - int n; - if (!PyArg_ParseTuple(arg, "O&s#:add", convuint, &i, &p, &n)) goto end; + struct bin s; + if (!PyArg_ParseTuple(arg, "O&O&:add", convuint, &i, convbin, &s)) + goto end; if (i > 254) VALERR("index must be < 255"); - if (n != GFSHARE_S(me)->sz) VALERR("bad share size"); + if (s.sz != GFSHARE_S(me)->sz) VALERR("bad share size"); if (gfshare_addedp(GFSHARE_S(me), i)) VALERR("this share already added"); if (GFSHARE_S(me)->i >= GFSHARE_S(me)->t) VALERR("enough shares already"); - gfshare_add(GFSHARE_S(me), i, p); + gfshare_add(GFSHARE_S(me), i, s.p); return (PyInt_FromLong(GFSHARE_S(me)->t - GFSHARE_S(me)->i)); end: return (0); } -static PyObject *gfsmeth_combine(PyObject *me, PyObject *arg) +static PyObject *gfsmeth_combine(PyObject *me) { PyObject *rc = 0; - if (!PyArg_ParseTuple(arg, ":combine")) goto end; if (GFSHARE_S(me)->i < GFSHARE_S(me)->t) VALERR("not enough shares yet"); rc = bytestring_pywrap(0, GFSHARE_S(me)->sz); - gfshare_combine(GFSHARE_S(me), PyString_AS_STRING(rc)); + gfshare_combine(GFSHARE_S(me), BIN_PTR(rc)); end: return (rc); } -static PyMethodDef gfsharejoin_pymethods[] = { +static const PyMethodDef gfsharejoin_pymethods[] = { #define METHNAME(name) gfsmeth_##name METH (addedp, "S.addedp(I) -> BOOL") METH (add, "S.add(I, SHARE) -> REMAIN") - METH (combine, "S.combine() -> SECRET") + NAMETH(combine, "S.combine() -> SECRET") #undef METHNAME { 0 } }; @@ -263,15 +263,15 @@ static PyMethodDef gfsharejoin_pymethods[] = { static PyObject *gfsget_remain(PyObject *me, void *hunoz) { return (PyInt_FromLong(GFSHARE_S(me)->t - GFSHARE_S(me)->i)); } -static PyGetSetDef gfsharejoin_pygetset[]= { +static const PyGetSetDef gfsharejoin_pygetset[]= { #define GETSETNAME(op, name) gfs##op##_##name GET (remain, "S.remain -> REMAIN") #undef GETSETNAME { 0 } }; -static PyTypeObject gfsharejoin_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ +static const PyTypeObject gfsharejoin_pytype_skel = { + PyVarObject_HEAD_INIT(0, 0) /* Header */ "GFShareJoin", /* @tp_name@ */ sizeof(gfshare_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -295,7 +295,8 @@ static PyTypeObject gfsharejoin_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Binary field secret sharing: join shares to recover secret.", + "GFShareJoin(THRESHOLD, SIZE): binary field sharing:\n" + " join shares to recover secret.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -303,9 +304,9 @@ static PyTypeObject gfsharejoin_pytype_skel = { 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ - gfsharejoin_pymethods, /* @tp_methods@ */ + PYMETHODS(gfsharejoin), /* @tp_methods@ */ 0, /* @tp_members@ */ - gfsharejoin_pygetset, /* @tp_getset@ */ + PYGETSET(gfsharejoin), /* @tp_getset@ */ 0, /* @tp_base@ */ 0, /* @tp_dict@ */ 0, /* @tp_descr_get@ */ @@ -343,7 +344,7 @@ static PyObject *sget_threshold(PyObject *me, void *hunoz) static PyObject *sget_modulus(PyObject *me, void *hunoz) { return (mp_pywrap(SHARE_S(me)->p)); } -static PyGetSetDef share_pygetset[]= { +static const PyGetSetDef share_pygetset[]= { #define GETSETNAME(op, name) s##op##_##name GET (threshold, "S.threshold -> THRESHOLD") GET (modulus, "S.modulus -> MODULUS") @@ -351,8 +352,8 @@ static PyGetSetDef share_pygetset[]= { { 0 } }; -static PyTypeObject share_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ +static const PyTypeObject share_pytype_skel = { + PyVarObject_HEAD_INIT(0, 0) /* Header */ "Share", /* @tp_name@ */ sizeof(share_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -376,7 +377,7 @@ static PyTypeObject share_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Prime-field secret sharing base class.", + "Prime-field secret sharing base class.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -386,7 +387,7 @@ static PyTypeObject share_pytype_skel = { 0, /* @tp_iternext@ */ 0, /* @tp_methods@ */ 0, /* @tp_members@ */ - share_pygetset, /* @tp_getset@ */ + PYGETSET(share), /* @tp_getset@ */ 0, /* @tp_base@ */ 0, /* @tp_dict@ */ 0, /* @tp_descr_get@ */ @@ -407,8 +408,10 @@ static PyObject *sharesplit_pynew(PyTypeObject *ty, grand *r = &rand_global; mp *m = 0; share_pyobj *s; - char *kwlist[] = { "threshold", "secret", "modulus", "rng", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&O&:new", kwlist, + static const char *const kwlist[] = + { "threshold", "secret", "modulus", "rng", 0 }; + + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&O&:new", KWLIST, convuint, &t, convmp, &sec, convmp, &m, convgrand, &r)) goto end; @@ -435,15 +438,15 @@ end: return (rc); } -static PyMethodDef sharesplit_pymethods[] = { +static const PyMethodDef sharesplit_pymethods[] = { #define METHNAME(name) smeth_##name METH (get, "S.get(I) -> SHARE") #undef METHNAME { 0 } }; -static PyTypeObject sharesplit_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ +static const PyTypeObject sharesplit_pytype_skel = { + PyVarObject_HEAD_INIT(0, 0) /* Header */ "ShareSplit", /* @tp_name@ */ sizeof(share_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -467,7 +470,8 @@ static PyTypeObject sharesplit_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Prime field secret sharing: split secret into shares.", + "ShareSplit(THRESHOLD, SECRET, [modulus = ?], [rng = rand]):\n" + " prime field secret sharing: split secret into shares.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -475,7 +479,7 @@ static PyTypeObject sharesplit_pytype_skel = { 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ - sharesplit_pymethods, /* @tp_methods@ */ + PYMETHODS(sharesplit), /* @tp_methods@ */ 0, /* @tp_members@ */ 0, /* @tp_getset@ */ 0, /* @tp_base@ */ @@ -496,8 +500,8 @@ static PyObject *sharejoin_pynew(PyTypeObject *ty, unsigned t; mp *m = 0; share_pyobj *s; - char *kwlist[] = { "threshold", "modulus", 0 }; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", kwlist, + static const char *const kwlist[] = { "threshold", "modulus", 0 }; + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", KWLIST, convuint, &t, convmp, &m)) goto end; if (!t) VALERR("threshold must be nonzero"); @@ -536,21 +540,20 @@ end: return (rc); } -static PyObject *smeth_combine(PyObject *me, PyObject *arg) +static PyObject *smeth_combine(PyObject *me) { PyObject *rc = 0; - if (!PyArg_ParseTuple(arg, ":combine")) goto end; if (SHARE_S(me)->i < SHARE_S(me)->t) VALERR("not enough shares yet"); rc = mp_pywrap(share_combine(SHARE_S(me))); end: return (rc); } -static PyMethodDef sharejoin_pymethods[] = { +static const PyMethodDef sharejoin_pymethods[] = { #define METHNAME(name) smeth_##name METH (addedp, "S.addedp(I) -> BOOL") METH (add, "S.add(I, SHARE) -> REMAIN") - METH (combine, "S.combine() -> SECRET") + NAMETH(combine, "S.combine() -> SECRET") #undef METHNAME { 0 } }; @@ -558,15 +561,15 @@ static PyMethodDef sharejoin_pymethods[] = { static PyObject *sget_remain(PyObject *me, void *hunoz) { return (PyInt_FromLong(SHARE_S(me)->t - SHARE_S(me)->i)); } -static PyGetSetDef sharejoin_pygetset[]= { +static const PyGetSetDef sharejoin_pygetset[]= { #define GETSETNAME(op, name) s##op##_##name GET (remain, "S.remain -> REMAIN") #undef GETSETNAME { 0 } }; -static PyTypeObject sharejoin_pytype_skel = { - PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ +static const PyTypeObject sharejoin_pytype_skel = { + PyVarObject_HEAD_INIT(0, 0) /* Header */ "ShareJoin", /* @tp_name@ */ sizeof(share_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -590,7 +593,8 @@ static PyTypeObject sharejoin_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Prime field secret sharing: join shares to recover secret.", + "ShareJoin(THRESHOLD, MODULUS): prime field secret sharing:\n" + " join shares to recover secret.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -598,9 +602,9 @@ static PyTypeObject sharejoin_pytype_skel = { 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ - sharejoin_pymethods, /* @tp_methods@ */ + PYMETHODS(sharejoin), /* @tp_methods@ */ 0, /* @tp_members@ */ - sharejoin_pygetset, /* @tp_getset@ */ + PYGETSET(sharejoin), /* @tp_getset@ */ 0, /* @tp_base@ */ 0, /* @tp_dict@ */ 0, /* @tp_descr_get@ */