RE: [PATCH 4/5] crypto: lib/sha256 - Unroll SHA256 loop 8 times intead of 64

From: David Laight
Date: Tue Oct 20 2020 - 03:41:40 EST


From: Arvind Sankar> Sent: 19 October 2020 16:30
> To: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>; David S. Miller <davem@xxxxxxxxxxxxx>; linux-
> crypto@xxxxxxxxxxxxxxx
> Cc: linux-kernel@xxxxxxxxxxxxxxx
> Subject: [PATCH 4/5] crypto: lib/sha256 - Unroll SHA256 loop 8 times intead of 64
>
> This reduces code size substantially (on x86_64 with gcc-10 the size of
> sha256_update() goes from 7593 bytes to 1952 bytes including the new
> SHA256_K array), and on x86 is slightly faster than the full unroll.

The speed will depend on exactly which cpu type is used.
It is even possible that the 'not unrolled at all' loop
(with the all the extra register moves) is faster on some x86-64 cpu.

>
> Signed-off-by: Arvind Sankar <nivedita@xxxxxxxxxxxx>
> ---
> lib/crypto/sha256.c | 164 ++++++++------------------------------------
> 1 file changed, 28 insertions(+), 136 deletions(-)
>
> diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c
> index c6bfeacc5b81..9f0b71d41ea0 100644
> --- a/lib/crypto/sha256.c
> +++ b/lib/crypto/sha256.c
> @@ -18,6 +18,17 @@
> #include <crypto/sha.h>
> #include <asm/unaligned.h>
...
>
> +#define SHA256_ROUND(i, a, b, c, d, e, f, g, h) do { \
> + u32 t1, t2; \
> + t1 = h + e1(e) + Ch(e, f, g) + SHA256_K[i] + W[i]; \
> + t2 = e0(a) + Maj(a, b, c); d += t1; h = t1 + t2; \

Split to 3 lines.

If you can put SHA256_K[] and W[] into a struct then the
compiler can use the same register to address into both
arrays (using an offset of 64*4 for the second one).
(ie keep the two arrays, not an array of struct).
This should reduce the register pressure slightly.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)