chiark / gitweb /
bytestring.c, catacomb/__init__.py: Introduce and use `zero' method.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 11 May 2017 09:42:15 +0000 (10:42 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 14 May 2017 03:29:20 +0000 (04:29 +0100)
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.

bytestring.c
catacomb/__init__.py

index 6dc70f759fca205df6355cc58067c00e6fd6d076..2b74648d91d9ca2793f206ba4091e941123465f1 100644 (file)
@@ -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 }
 };
index 860cfb5e07cd3d119a45771fed15068b360fd572..d4eac309ea67c96589da77afc079815d89acb737 100644 (file)
@@ -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):