Skip to content

Commit 68b233b

Browse files
author
Eric Biggers
committed
lib/crypto: blake2s: Replace manual unrolling with unrolled_full
As we're doing in the BLAKE2b code, use unrolled_full to make the compiler handle the loop unrolling. This simplifies the code slightly. The generated object code is nearly the same with both gcc and clang. Reviewed-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20251205051155.25274-1-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 2e8f7b1 commit 68b233b

1 file changed

Lines changed: 16 additions & 22 deletions

File tree

lib/crypto/blake2s.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/kernel.h>
1515
#include <linux/module.h>
1616
#include <linux/string.h>
17+
#include <linux/unroll.h>
1718
#include <linux/types.h>
1819

1920
static const u8 blake2s_sigma[10][16] = {
@@ -71,29 +72,22 @@ blake2s_compress_generic(struct blake2s_ctx *ctx,
7172
b = ror32(b ^ c, 7); \
7273
} while (0)
7374

74-
#define ROUND(r) do { \
75-
G(r, 0, v[0], v[ 4], v[ 8], v[12]); \
76-
G(r, 1, v[1], v[ 5], v[ 9], v[13]); \
77-
G(r, 2, v[2], v[ 6], v[10], v[14]); \
78-
G(r, 3, v[3], v[ 7], v[11], v[15]); \
79-
G(r, 4, v[0], v[ 5], v[10], v[15]); \
80-
G(r, 5, v[1], v[ 6], v[11], v[12]); \
81-
G(r, 6, v[2], v[ 7], v[ 8], v[13]); \
82-
G(r, 7, v[3], v[ 4], v[ 9], v[14]); \
83-
} while (0)
84-
ROUND(0);
85-
ROUND(1);
86-
ROUND(2);
87-
ROUND(3);
88-
ROUND(4);
89-
ROUND(5);
90-
ROUND(6);
91-
ROUND(7);
92-
ROUND(8);
93-
ROUND(9);
94-
75+
/*
76+
* Unroll the rounds loop to enable constant-folding of the
77+
* blake2s_sigma values.
78+
*/
79+
unrolled_full
80+
for (int r = 0; r < 10; r++) {
81+
G(r, 0, v[0], v[4], v[8], v[12]);
82+
G(r, 1, v[1], v[5], v[9], v[13]);
83+
G(r, 2, v[2], v[6], v[10], v[14]);
84+
G(r, 3, v[3], v[7], v[11], v[15]);
85+
G(r, 4, v[0], v[5], v[10], v[15]);
86+
G(r, 5, v[1], v[6], v[11], v[12]);
87+
G(r, 6, v[2], v[7], v[8], v[13]);
88+
G(r, 7, v[3], v[4], v[9], v[14]);
89+
}
9590
#undef G
96-
#undef ROUND
9791

9892
for (i = 0; i < 8; ++i)
9993
ctx->h[i] ^= v[i] ^ v[i + 8];

0 commit comments

Comments
 (0)