Skip to contentSkip to conversation

lib/crypto: arm64/ghash: Aggregate 4 blocks per loop

RFC PATCH New Qingfang Deng 1 participant 1 message Nov 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 lib/crypto: arm64/ghash: Aggregate 4 blocks per loop

The byte-PMULL GHASH implementation reconstructs and reduces a field product for every input block. Aggregate four blocks using precomputed powers of the hash key, which the ARM64 CE version already uses:

(Y ^ X1) * H^4 ^ X2 * H^3 ^ X3 * H^2 ^ X4 * H

Split each polynomial multiplication into 32-bit Karatsuba products, pair them in SIMD lanes and accumulate them before reconstruction. Reconstruct three 128-bit products and perform one Montgomery reduction for the group. Use three byte PMULL instructions with 0xc2 for reduction modulo x^128 + x^127 + x^126 + x^121 + 1.

Retain the low and high halves of H^4 through H, and their XORs, in x4-x15. Use x16-x17 to construct the inner 32-bit cross operands while independent SIMD products retire. Prepare each next key operand in v9-v11. This avoids an expanded table of key rotations and needs no register spills. Use the existing single-block pmull_ghash_update_p8 helper for tails.

Compare the single-block and this four-block path in the same userspace harness on a Cortex-A53 at 1.2 GHz. Median throughput in Gbit/s:

  Bytes   Baseline   4-block
     16   0.850016   0.844973
     64   1.015665   1.723762
   1440   1.081407   1.968001
  16384   1.085286   2.032019
  65536   1.083460   2.025681

The four-block loop uses 296 instructions, and reaches about 0.980 instructions per cycle in 64 KiB measurements on Cortex-A53.

Assisted-by LLM
---
 include/crypto/gf128hash.h         |   4 +
 lib/crypto/arm64/gf128hash.h       |  22 ++-
 lib/crypto/arm64/ghash-neon-core.S | 207 +++++++++++++++++++++++++++++
 3 files changed, 232 insertions(+), 1 deletion(-)
include/crypto/gf128hash.h +4 -0
index 41c557d55965..f4638e5be087 100644
@@ -50,6 +50,10 @@ struct ghash_key {
 #endif
 	/** @h: The hash key H, in POLYVAL format */
 	struct polyval_elem h;
+#if defined(CONFIG_CRYPTO_LIB_GF128HASH_ARCH) && defined(CONFIG_ARM64)
+	/** @h_powers: GHASH powers H^2 through H^4, in POLYVAL format */
+	struct polyval_elem h_powers[3];
+#endif
 };
 
 /**
lib/crypto/arm64/gf128hash.h +21 -1
index 1d1179f87f8d..afcc5da13ab6 100644
@@ -15,12 +15,32 @@ static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_pmull);
 asmlinkage void pmull_ghash_update_p8(size_t blocks, struct polyval_elem *dg,
 				      const u8 *src,
 				      const struct polyval_elem *h);
+asmlinkage void pmull_ghash_update_p8_4x(size_t blocks, struct polyval_elem *dg,
+					 const u8 *src,
+					 const struct ghash_key *key);
 asmlinkage void polyval_mul_pmull(struct polyval_elem *a,
 				  const struct polyval_elem *b);
 asmlinkage void polyval_blocks_pmull(struct polyval_elem *acc,
 				     const struct polyval_key *key,
 				     const u8 *data, size_t nblocks);
 
+#define ghash_preparekey_arch ghash_preparekey_arch
+static void ghash_preparekey_arch(struct ghash_key *key,
+				  const u8 raw_key[GHASH_BLOCK_SIZE])
+{
+	const struct polyval_elem *prev = &key->h;
+
+	static_assert(offsetof(struct ghash_key, h) == 0);
+	static_assert(offsetof(struct ghash_key, h_powers) == GHASH_BLOCK_SIZE);
+	static_assert(sizeof(*key) == 4 * GHASH_BLOCK_SIZE);
+	ghash_key_to_polyval(raw_key, &key->h);
+	for (int i = 0; i < ARRAY_SIZE(key->h_powers); i++) {
+		key->h_powers[i] = *prev;
+		polyval_mul_generic(&key->h_powers[i], &key->h);
+		prev = &key->h_powers[i];
+	}
+}
+
 #define polyval_preparekey_arch polyval_preparekey_arch
 static void polyval_preparekey_arch(struct polyval_key *key,
 				    const u8 raw_key[POLYVAL_BLOCK_SIZE])
@@ -90,7 +110,7 @@ static void ghash_blocks_arch(struct polyval_elem *acc,
 {
 	if (static_branch_likely(&have_asimd) && may_use_simd()) {
 		scoped_ksimd()
-			pmull_ghash_update_p8(nblocks, acc, data, &key->h);
+			pmull_ghash_update_p8_4x(nblocks, acc, data, key);
 	} else {
 		ghash_blocks_generic(acc, &key->h, data, nblocks);
 	}
lib/crypto/arm64/ghash-neon-core.S +207 -0

210 lines not on this page · raw message

Signature · 1 line
2.53.0