From: Mark Wooding Date: Thu, 11 May 2017 09:42:15 +0000 (+0100) Subject: bytestring.c, catacomb/__init__.py: Introduce and use `zero' method. X-Git-Tag: 1.2.0~15 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/catacomb-python/commitdiff_plain/5a8b43b3b20b540f20877f14ecb9cb0dae8ddaf3 bytestring.c, catacomb/__init__.py: Introduce and use `zero' method. Seems like strings of zero bytes are especially useful. Add a class method to `ByteString' to generate them efficiently, and use it to make the magic `Z128' constant. --- diff --git a/bytestring.c b/bytestring.c index 6dc70f7..2b74648 100644 --- a/bytestring.c +++ b/bytestring.c @@ -73,6 +73,17 @@ end: return (0); } +static PyObject *meth__ByteString_zero(PyObject *me, PyObject *arg) +{ + size_t sz; + PyObject *rc = 0; + if (!PyArg_ParseTuple(arg, "OO&:zero", &me, convszt, &sz)) goto end; + rc = bytestring_pywrap(0, sz); + memset(PyString_AS_STRING(rc), 0, sz); +end: + return (rc); +} + static PyObject *bytestring_pyrichcompare(PyObject *me, PyObject *you, int op) { @@ -228,6 +239,7 @@ static PyTypeObject bytestring_pytype_skel = { static PyMethodDef methods[] = { #define METHNAME(func) meth_##func METH (ctstreq, "ctstreq(S, T) -> BOOL") + METH (_ByteString_zero, "zero(N) -> 0000...00") #undef METHNAME { 0 } }; diff --git a/catacomb/__init__.py b/catacomb/__init__.py index 860cfb5..d4eac30 100644 --- a/catacomb/__init__.py +++ b/catacomb/__init__.py @@ -56,7 +56,8 @@ def _init(): for i in b: if i[0] != '_': d[i] = b[i]; - for i in ['MP', 'GF', 'Field', + for i in ['ByteString', + 'MP', 'GF', 'Field', 'ECPt', 'ECPtCurve', 'ECCurve', 'ECInfo', 'DHInfo', 'BinDHInfo', 'RSAPriv', 'BBSPriv', 'PrimeFilter', 'RabinMiller', @@ -738,7 +739,7 @@ _augment(KCDSAPriv, _tmp) X25519_BASE = MP(9).storel(32) X448_BASE = MP(5).storel(56) -Z128 = bytes('00000000000000000000000000000000') +Z128 = ByteString.zero(16) class _BoxyPub (object): def __init__(me, pub, *args, **kw):