Skip to contentSkip to conversation

lib/crypto: blake2s: Replace manual unrolling with unrolled_full

PATCH Applied Eric Biggers 2 participants 3 messages Dec 5, 2025 – Dec 9, 2025

Subsystems (2)
lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

PATCH 1/1 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.

---
 lib/crypto/blake2s.c | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
lib/crypto/blake2s.c +16 -22
index 6182c21ed943..71578a084742 100644
@@ -12,10 +12,11 @@
 #include <linux/bug.h>
 #include <linux/export.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/string.h>
+#include <linux/unroll.h>
 #include <linux/types.h>
 
 static const u8 blake2s_sigma[10][16] = {
 	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
 	{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
@@ -69,33 +70,26 @@ blake2s_compress_generic(struct blake2s_ctx *ctx,
 	d = ror32(d ^ a, 8); \
 	c += d; \
 	b = ror32(b ^ c, 7); \
 } while (0)
 
-#define ROUND(r) do { \
-	G(r, 0, v[0], v[ 4], v[ 8], v[12]); \
-	G(r, 1, v[1], v[ 5], v[ 9], v[13]); \
-	G(r, 2, v[2], v[ 6], v[10], v[14]); \
-	G(r, 3, v[3], v[ 7], v[11], v[15]); \
-	G(r, 4, v[0], v[ 5], v[10], v[15]); \
-	G(r, 5, v[1], v[ 6], v[11], v[12]); \
-	G(r, 6, v[2], v[ 7], v[ 8], v[13]); \
-	G(r, 7, v[3], v[ 4], v[ 9], v[14]); \
-} while (0)
-		ROUND(0);
-		ROUND(1);
-		ROUND(2);
-		ROUND(3);
-		ROUND(4);
-		ROUND(5);
-		ROUND(6);
-		ROUND(7);
-		ROUND(8);
-		ROUND(9);
-
+		/*
+		 * Unroll the rounds loop to enable constant-folding of the
+		 * blake2s_sigma values.
+		 */
+		unrolled_full
+		for (int r = 0; r < 10; r++) {
+			G(r, 0, v[0], v[4], v[8], v[12]);
+			G(r, 1, v[1], v[5], v[9], v[13]);
+			G(r, 2, v[2], v[6], v[10], v[14]);
+			G(r, 3, v[3], v[7], v[11], v[15]);
+			G(r, 4, v[0], v[5], v[10], v[15]);
+			G(r, 5, v[1], v[6], v[11], v[12]);
+			G(r, 6, v[2], v[7], v[8], v[13]);
+			G(r, 7, v[3], v[4], v[9], v[14]);
+		}
 #undef G
-#undef ROUND
 
 		for (i = 0; i < 8; ++i)
 			ctx->h[i] ^= v[i] ^ v[i + 8];
 
 		data += BLAKE2S_BLOCK_SIZE;

base-commit: 43dfc13ca972988e620a6edb72956981b75ab6b0

Signature · 1 line
2.52.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Eric Biggers

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.

---
 lib/crypto/blake2s.c | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
Quoted 70 lines
lib/crypto/blake2s.c +16 -22
index 6182c21ed943..71578a084742 100644
@@ -12,10 +12,11 @@
 #include <linux/bug.h>
 #include <linux/export.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/string.h>
+#include <linux/unroll.h>
 #include <linux/types.h>
 
 static const u8 blake2s_sigma[10][16] = {
        { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
        { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
@@ -69,33 +70,26 @@ blake2s_compress_generic(struct blake2s_ctx *ctx,
        d = ror32(d ^ a, 8); \
        c += d; \
        b = ror32(b ^ c, 7); \
 } while (0)
 
-#define ROUND(r) do { \
-       G(r, 0, v[0], v[ 4], v[ 8], v[12]); \
-       G(r, 1, v[1], v[ 5], v[ 9], v[13]); \
-       G(r, 2, v[2], v[ 6], v[10], v[14]); \
-       G(r, 3, v[3], v[ 7], v[11], v[15]); \
-       G(r, 4, v[0], v[ 5], v[10], v[15]); \
-       G(r, 5, v[1], v[ 6], v[11], v[12]); \
-       G(r, 6, v[2], v[ 7], v[ 8], v[13]); \
-       G(r, 7, v[3], v[ 4], v[ 9], v[14]); \
-} while (0)
-               ROUND(0);
-               ROUND(1);
-               ROUND(2);
-               ROUND(3);
-               ROUND(4);
-               ROUND(5);
-               ROUND(6);
-               ROUND(7);
-               ROUND(8);
-               ROUND(9);
-
+               /*
+                * Unroll the rounds loop to enable constant-folding of the
+                * blake2s_sigma values.
+                */
+               unrolled_full
+               for (int r = 0; r < 10; r++) {
+                       G(r, 0, v[0], v[4], v[8], v[12]);
+                       G(r, 1, v[1], v[5], v[9], v[13]);
+                       G(r, 2, v[2], v[6], v[10], v[14]);
+                       G(r, 3, v[3], v[7], v[11], v[15]);
+                       G(r, 4, v[0], v[5], v[10], v[15]);
+                       G(r, 5, v[1], v[6], v[11], v[12]);
+                       G(r, 6, v[2], v[7], v[8], v[13]);
+                       G(r, 7, v[3], v[4], v[9], v[14]);
+               }
 #undef G
-#undef ROUND
 
                for (i = 0; i < 8; ++i)
                        ctx->h[i] ^= v[i] ^ v[i + 8];
 
                data += BLAKE2S_BLOCK_SIZE;

base-commit: 43dfc13ca972988e620a6edb72956981b75ab6b0 --
2.52.0

author maintainer Applied
#2 lore raw
In reply to Eric Biggers
Quoted 8 lines · Eric Biggers

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.

---
 lib/crypto/blake2s.c | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=libcrypto-fixes

- Eric