X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/46e6ad89d596d0987f00440c77e42c36f7d347f7..637b91402d2497db1318debd3cb3868a5abb8f01:/share.c diff --git a/share.c b/share.c index 38d966e..16d376e 100644 --- a/share.c +++ b/share.c @@ -1,13 +1,11 @@ /* -*-c-*- * - * $Id$ - * * Secret sharing * * (c) 2005 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the Python interface to Catacomb. * @@ -15,12 +13,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * Catacomb/Python is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with Catacomb/Python; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -55,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") @@ -65,7 +63,7 @@ static PyGetSetDef gfshare_pygetset[]= { static PyTypeObject gfshare_pytype_skel = { PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.GFShare", /* @tp_name@ */ + "GFShare", /* @tp_name@ */ sizeof(gfshare_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -88,17 +86,17 @@ 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@ */ 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 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@ */ @@ -115,12 +113,12 @@ static PyObject *gfsharesplit_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { char *p; - int n; + Py_ssize_t n; 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, + static const char *const kwlist[] = { "threshold", "secret", "rng", 0 }; + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&s#|O&:new", KWLIST, convuint, &t, &p, &n, convgrand, &r)) goto end; if (!t || t > 255) VALERR("threshold must be nonzero and < 256"); @@ -144,7 +142,7 @@ 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 @@ -153,7 +151,7 @@ static PyMethodDef gfsharesplit_pymethods[] = { static PyTypeObject gfsharesplit_pytype_skel = { PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.GFShareSplit", /* @tp_name@ */ + "GFShareSplit", /* @tp_name@ */ sizeof(gfshare_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -176,15 +174,16 @@ 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@ */ 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ - gfsharesplit_pymethods, /* @tp_methods@ */ + 0, /* @tp_iternext@ */ + PYMETHODS(gfsharesplit), /* @tp_methods@ */ 0, /* @tp_members@ */ 0, /* @tp_getset@ */ 0, /* @tp_base@ */ @@ -204,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"); @@ -230,7 +229,7 @@ static PyObject *gfsmeth_add(PyObject *me, PyObject *arg) { unsigned i; char *p; - int n; + Py_ssize_t n; if (!PyArg_ParseTuple(arg, "O&s#:add", convuint, &i, &p, &n)) goto end; if (i > 254) VALERR("index must be < 255"); if (n != GFSHARE_S(me)->sz) VALERR("bad share size"); @@ -253,7 +252,7 @@ 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") @@ -265,7 +264,7 @@ 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 @@ -274,7 +273,7 @@ static PyGetSetDef gfsharejoin_pygetset[]= { static PyTypeObject gfsharejoin_pytype_skel = { PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.GFShareJoin", /* @tp_name@ */ + "GFShareJoin", /* @tp_name@ */ sizeof(gfshare_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -297,17 +296,18 @@ 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@ */ 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ - gfsharejoin_pymethods, /* @tp_methods@ */ + 0, /* @tp_iternext@ */ + 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@ */ @@ -345,7 +345,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") @@ -355,7 +355,7 @@ static PyGetSetDef share_pygetset[]= { static PyTypeObject share_pytype_skel = { PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.Share", /* @tp_name@ */ + "Share", /* @tp_name@ */ sizeof(share_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -378,17 +378,17 @@ 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@ */ 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ + 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@ */ @@ -409,8 +409,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&: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; @@ -437,7 +439,7 @@ 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 @@ -446,7 +448,7 @@ static PyMethodDef sharesplit_pymethods[] = { static PyTypeObject sharesplit_pytype_skel = { PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.ShareSplit", /* @tp_name@ */ + "ShareSplit", /* @tp_name@ */ sizeof(share_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -469,15 +471,16 @@ 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@ */ 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ - sharesplit_pymethods, /* @tp_methods@ */ + 0, /* @tp_iternext@ */ + PYMETHODS(sharesplit), /* @tp_methods@ */ 0, /* @tp_members@ */ 0, /* @tp_getset@ */ 0, /* @tp_base@ */ @@ -498,8 +501,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"); @@ -548,7 +551,7 @@ 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") @@ -560,7 +563,7 @@ 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 @@ -569,7 +572,7 @@ static PyGetSetDef sharejoin_pygetset[]= { static PyTypeObject sharejoin_pytype_skel = { PyObject_HEAD_INIT(&PyType_Type) 0, /* Header */ - "catacomb.ShareJoin", /* @tp_name@ */ + "ShareJoin", /* @tp_name@ */ sizeof(share_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -592,17 +595,18 @@ 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@ */ 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ - 0, /* @tp_iternexr@ */ - sharejoin_pymethods, /* @tp_methods@ */ + 0, /* @tp_iternext@ */ + 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@ */