[PATCH] hash-common: avoid integer division to reduce call overhead

Jussi Kivilinna jussi.kivilinna at iki.fi
Mon Oct 14 21:48:17 CEST 2019


* cipher/hash-common.h (gcry_md_block_ctx): Replace 'blocksize' with
'blocksize_shift'.
* cipher/hash-common.c (_gcry_md_block_write): Use bit-level operations
instead of division to get number of blocks.
* cipher/gostr2411-94.c (gost3411_init): Initialize 'blocksize_shift'
instead of 'blocksize'.
* cipher/md2.c (md2_init): Ditto.
* cipher/md4.c (md4_init): Ditto.
* cipher/md5.c (md5_init): Ditto.
* cipher/rmd160.c (rmd160_init): Ditto.
* cipher/sha1.c (sha1_init): Ditto.
* cipher/sha256.c (sha256_common_init): Ditto.
* cipher/sha512.c (sha512_init_common): Ditto.
* cipher/sm3.c (sm3_init): Ditto.
* cipher/stribog.c (stribog_init_512): Ditto.
* cipher/tiger.c (do_init): Ditto.
* cipher/whirlpool.c (whirlpool_init): Ditto.
--

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 cipher/gostr3411-94.c |    2 +-
 cipher/hash-common.c  |    9 +++++----
 cipher/hash-common.h  |    2 +-
 cipher/md2.c          |    2 +-
 cipher/md4.c          |    2 +-
 cipher/md5.c          |    2 +-
 cipher/rmd160.c       |    2 +-
 cipher/sha1.c         |    2 +-
 cipher/sha256.c       |    2 +-
 cipher/sha512.c       |    2 +-
 cipher/sm3.c          |    2 +-
 cipher/stribog.c      |    2 +-
 cipher/tiger.c        |    2 +-
 cipher/whirlpool.c    |    2 +-
 14 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/cipher/gostr3411-94.c b/cipher/gostr3411-94.c
index d9746275e..ebf9e0a24 100644
--- a/cipher/gostr3411-94.c
+++ b/cipher/gostr3411-94.c
@@ -61,7 +61,7 @@ gost3411_init (void *context, unsigned int flags)
 
   hd->bctx.nblocks = 0;
   hd->bctx.count = 0;
-  hd->bctx.blocksize = 32;
+  hd->bctx.blocksize_shift = _gcry_ctz(32);
   hd->bctx.bwrite = transform;
   hd->cryptopro = 0;
 }
diff --git a/cipher/hash-common.c b/cipher/hash-common.c
index 74675d49f..ab486f06e 100644
--- a/cipher/hash-common.c
+++ b/cipher/hash-common.c
@@ -123,7 +123,8 @@ _gcry_md_block_write (void *context, const void *inbuf_arg, size_t inlen)
   gcry_md_block_ctx_t *hd = context;
   unsigned int stack_burn = 0;
   unsigned int nburn;
-  const unsigned int blocksize = hd->blocksize;
+  const unsigned int blocksize_shift = hd->blocksize_shift;
+  const unsigned int blocksize = 1 << blocksize_shift;
   size_t inblocks;
   size_t copylen;
 
@@ -164,14 +165,14 @@ _gcry_md_block_write (void *context, const void *inbuf_arg, size_t inlen)
 
   if (inlen >= blocksize)
     {
-      inblocks = inlen / blocksize;
+      inblocks = inlen >> blocksize_shift;
       nburn = hd->bwrite (hd, inbuf, inblocks);
       stack_burn = nburn > stack_burn ? nburn : stack_burn;
       hd->count = 0;
       hd->nblocks_high += (hd->nblocks + inblocks < inblocks);
       hd->nblocks += inblocks;
-      inlen -= inblocks * blocksize;
-      inbuf += inblocks * blocksize;
+      inlen -= inblocks << blocksize_shift;
+      inbuf += inblocks << blocksize_shift;
     }
 
   if (inlen)
diff --git a/cipher/hash-common.h b/cipher/hash-common.h
index 0b3ade11e..561e77a7e 100644
--- a/cipher/hash-common.h
+++ b/cipher/hash-common.h
@@ -51,7 +51,7 @@ typedef struct gcry_md_block_ctx
     MD_NBLOCKS_TYPE nblocks;
     MD_NBLOCKS_TYPE nblocks_high;
     int count;
-    size_t blocksize;
+    unsigned int blocksize_shift;
     _gcry_md_block_write_t bwrite;
 } gcry_md_block_ctx_t;
 
diff --git a/cipher/md2.c b/cipher/md2.c
index bf2fbee4c..dc4deac3b 100644
--- a/cipher/md2.c
+++ b/cipher/md2.c
@@ -135,7 +135,7 @@ md2_init (void *context, unsigned int flags)
   (void)flags;
 
   memset (ctx, 0, sizeof(*ctx));
-  ctx->bctx.blocksize = 16;
+  ctx->bctx.blocksize_shift = _gcry_ctz(16);
   ctx->bctx.bwrite = transform;
 }
 
diff --git a/cipher/md4.c b/cipher/md4.c
index b75bc5e69..24986c27a 100644
--- a/cipher/md4.c
+++ b/cipher/md4.c
@@ -83,7 +83,7 @@ md4_init (void *context, unsigned int flags)
   ctx->bctx.nblocks = 0;
   ctx->bctx.nblocks_high = 0;
   ctx->bctx.count = 0;
-  ctx->bctx.blocksize = 64;
+  ctx->bctx.blocksize_shift = _gcry_ctz(64);
   ctx->bctx.bwrite = transform;
 }
 
diff --git a/cipher/md5.c b/cipher/md5.c
index 94fcdf033..6859d5664 100644
--- a/cipher/md5.c
+++ b/cipher/md5.c
@@ -67,7 +67,7 @@ md5_init( void *context, unsigned int flags)
   ctx->bctx.nblocks = 0;
   ctx->bctx.nblocks_high = 0;
   ctx->bctx.count = 0;
-  ctx->bctx.blocksize = 64;
+  ctx->bctx.blocksize_shift = _gcry_ctz(64);
   ctx->bctx.bwrite = transform;
 }
 
diff --git a/cipher/rmd160.c b/cipher/rmd160.c
index 24210a077..0608f74cd 100644
--- a/cipher/rmd160.c
+++ b/cipher/rmd160.c
@@ -166,7 +166,7 @@ rmd160_init (void *context, unsigned int flags)
   hd->bctx.nblocks = 0;
   hd->bctx.nblocks_high = 0;
   hd->bctx.count = 0;
-  hd->bctx.blocksize = 64;
+  hd->bctx.blocksize_shift = _gcry_ctz(64);
   hd->bctx.bwrite = transform;
 }
 
diff --git a/cipher/sha1.c b/cipher/sha1.c
index 23aceef32..d3ee982b0 100644
--- a/cipher/sha1.c
+++ b/cipher/sha1.c
@@ -274,7 +274,7 @@ sha1_init (void *context, unsigned int flags)
   hd->bctx.nblocks = 0;
   hd->bctx.nblocks_high = 0;
   hd->bctx.count = 0;
-  hd->bctx.blocksize = 64;
+  hd->bctx.blocksize_shift = _gcry_ctz(64);
 
   /* Order of feature checks is important here; last match will be
    * selected.  Keep slower implementations at the top and faster at
diff --git a/cipher/sha256.c b/cipher/sha256.c
index 562dee9af..175da909d 100644
--- a/cipher/sha256.c
+++ b/cipher/sha256.c
@@ -246,7 +246,7 @@ sha256_common_init (SHA256_CONTEXT *hd)
   hd->bctx.nblocks = 0;
   hd->bctx.nblocks_high = 0;
   hd->bctx.count = 0;
-  hd->bctx.blocksize = 64;
+  hd->bctx.blocksize_shift = _gcry_ctz(64);
 
   /* Order of feature checks is important here; last match will be
    * selected.  Keep slower implementations at the top and faster at
diff --git a/cipher/sha512.c b/cipher/sha512.c
index b05157aa9..df9a449c3 100644
--- a/cipher/sha512.c
+++ b/cipher/sha512.c
@@ -302,7 +302,7 @@ sha512_init_common (SHA512_CONTEXT *ctx, unsigned int flags)
   ctx->bctx.nblocks = 0;
   ctx->bctx.nblocks_high = 0;
   ctx->bctx.count = 0;
-  ctx->bctx.blocksize = 128;
+  ctx->bctx.blocksize_shift = _gcry_ctz(128);
 
   /* Order of feature checks is important here; last match will be
    * selected.  Keep slower implementations at the top and faster at
diff --git a/cipher/sm3.c b/cipher/sm3.c
index b6f0ab28c..aee949874 100644
--- a/cipher/sm3.c
+++ b/cipher/sm3.c
@@ -77,7 +77,7 @@ sm3_init (void *context, unsigned int flags)
   hd->bctx.nblocks = 0;
   hd->bctx.nblocks_high = 0;
   hd->bctx.count = 0;
-  hd->bctx.blocksize = 64;
+  hd->bctx.blocksize_shift = _gcry_ctz(64);
   hd->bctx.bwrite = transform;
 
   (void)features;
diff --git a/cipher/stribog.c b/cipher/stribog.c
index 267872474..c919182ad 100644
--- a/cipher/stribog.c
+++ b/cipher/stribog.c
@@ -1206,7 +1206,7 @@ stribog_init_512 (void *context, unsigned int flags)
 
   memset (hd, 0, sizeof (*hd));
 
-  hd->bctx.blocksize = 64;
+  hd->bctx.blocksize_shift = _gcry_ctz(64);
   hd->bctx.bwrite = transform;
 }
 
diff --git a/cipher/tiger.c b/cipher/tiger.c
index c78e3ac35..b2f166773 100644
--- a/cipher/tiger.c
+++ b/cipher/tiger.c
@@ -601,7 +601,7 @@ do_init (void *context, int variant)
   hd->bctx.nblocks = 0;
   hd->bctx.nblocks_high = 0;
   hd->bctx.count = 0;
-  hd->bctx.blocksize = 64;
+  hd->bctx.blocksize_shift = _gcry_ctz(64);
   hd->bctx.bwrite = transform;
   hd->variant = variant;
 }
diff --git a/cipher/whirlpool.c b/cipher/whirlpool.c
index d9b79cf1a..79b2026b5 100644
--- a/cipher/whirlpool.c
+++ b/cipher/whirlpool.c
@@ -1179,7 +1179,7 @@ whirlpool_init (void *ctx, unsigned int flags)
 
   memset (context, 0, sizeof (*context));
 
-  context->bctx.blocksize = BLOCK_SIZE;
+  context->bctx.blocksize_shift = _gcry_ctz(BLOCK_SIZE);
   context->bctx.bwrite = whirlpool_transform;
   if ((flags & GCRY_MD_FLAG_BUGEMU1))
     {




More information about the Gcrypt-devel mailing list