* [PATCH] lib/crypto: mpi: use a 128-bit multiply for umul_ppmm() on arm64
@ 2026-11-27 17:16 ecordonnier
2026-11-27 19:22 ` Eric Biggers
0 siblings, 1 reply; 2+ messages in thread
From: ecordonnier @ 2026-11-27 17:16 UTC (permalink / raw)
To: ebiggers, Jason, ardb; +Cc: linux-crypto, Etienne Cordonnier
From: Etienne Cordonnier <ecordonnier@snap.com>
longlong.h has no arm64 entry, so umul_ppmm() falls back to the generic
C version, which builds each 64x64->128 product out of four 32x32
multiplications plus carry handling. umul_ppmm() is in the inner loops
of mpihelp_mul_1(), mpihelp_addmul_1(), mpihelp_submul_1() and the
division code, so this slows down every mpi_powm(), e.g. each RSA
signature verification (module signatures, X.509).
Use a u128 multiplication, which the compiler turns into a mul + umulh
pair, as the MIPS64 entry already does with a 128-bit type.
On a Cortex-A55 at 1.5 GHz, the time spent in mpi_powm() per RSA-4096
module signature verification (measured with kprobes on a 6.1 kernel
with this change backported) goes from 1286 us to 1201 us (-6.6%).
A userspace build of the same code shows the same direction at other
modulus sizes (2048/3072/4096 bits: 0.42/0.85/1.44 ms before,
0.35/0.70/1.19 ms after).
The crypto self-tests (CONFIG_CRYPTO_SELFTESTS_FULL) pass for
rsa-generic on arm64 in QEMU.
AI assistance: this patch and its changelog were written by Claude
Sonnet 5.5 (Anthropic), used through Claude Code, and reviewed by the
submitter.
Signed-off-by: Etienne Cordonnier <ecordonnier@snap.com>
---
lib/crypto/mpi/longlong.h | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/lib/crypto/mpi/longlong.h b/lib/crypto/mpi/longlong.h
index 6d31c3a729f1..877f19778df3 100644
--- a/lib/crypto/mpi/longlong.h
+++ b/lib/crypto/mpi/longlong.h
@@ -225,6 +225,20 @@ extern UDItype __udiv_qrnnd(UDItype *, UDItype, UDItype, UDItype);
#define UDIV_TIME 100
#endif /* __arm__ */
+/***************************************
+ ************** ARM64 ****************
+ ***************************************/
+#if defined(__aarch64__) && W_TYPE_SIZE == 64 && \
+ defined(CONFIG_ARCH_SUPPORTS_INT128)
+/* The compiler turns a u128 multiply into mul + umulh. */
+#define umul_ppmm(w1, w0, u, v) \
+do { \
+ u128 __ll = (u128)(u) * (v); \
+ w1 = __ll >> 64; \
+ w0 = __ll; \
+} while (0)
+#endif /* __aarch64__ */
+
/***************************************
************** CLIPPER **************
***************************************/
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] lib/crypto: mpi: use a 128-bit multiply for umul_ppmm() on arm64
2026-11-27 17:16 [PATCH] lib/crypto: mpi: use a 128-bit multiply for umul_ppmm() on arm64 ecordonnier
@ 2026-11-27 19:22 ` Eric Biggers
0 siblings, 0 replies; 2+ messages in thread
From: Eric Biggers @ 2026-11-27 19:22 UTC (permalink / raw)
To: ecordonnier; +Cc: Jason, ardb, linux-crypto
On Fri, Nov 27, 2026 at 06:16:32PM +0100, ecordonnier@snap.com wrote:
> From: Etienne Cordonnier <ecordonnier@snap.com>
>
> longlong.h has no arm64 entry, so umul_ppmm() falls back to the generic
> C version, which builds each 64x64->128 product out of four 32x32
> multiplications plus carry handling. umul_ppmm() is in the inner loops
> of mpihelp_mul_1(), mpihelp_addmul_1(), mpihelp_submul_1() and the
> division code, so this slows down every mpi_powm(), e.g. each RSA
> signature verification (module signatures, X.509).
>
> Use a u128 multiplication, which the compiler turns into a mul + umulh
> pair, as the MIPS64 entry already does with a 128-bit type.
>
> On a Cortex-A55 at 1.5 GHz, the time spent in mpi_powm() per RSA-4096
> module signature verification (measured with kprobes on a 6.1 kernel
> with this change backported) goes from 1286 us to 1201 us (-6.6%).
> A userspace build of the same code shows the same direction at other
> modulus sizes (2048/3072/4096 bits: 0.42/0.85/1.44 ms before,
> 0.35/0.70/1.19 ms after).
>
> The crypto self-tests (CONFIG_CRYPTO_SELFTESTS_FULL) pass for
> rsa-generic on arm64 in QEMU.
>
> AI assistance: this patch and its changelog were written by Claude
> Sonnet 5.5 (Anthropic), used through Claude Code, and reviewed by the
> submitter.
>
> Signed-off-by: Etienne Cordonnier <ecordonnier@snap.com>
> ---
> lib/crypto/mpi/longlong.h | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
>
> diff --git a/lib/crypto/mpi/longlong.h b/lib/crypto/mpi/longlong.h
> index 6d31c3a729f1..877f19778df3 100644
> --- a/lib/crypto/mpi/longlong.h
> +++ b/lib/crypto/mpi/longlong.h
> @@ -225,6 +225,20 @@ extern UDItype __udiv_qrnnd(UDItype *, UDItype, UDItype, UDItype);
> #define UDIV_TIME 100
> #endif /* __arm__ */
>
> +/***************************************
> + ************** ARM64 ****************
> + ***************************************/
> +#if defined(__aarch64__) && W_TYPE_SIZE == 64 && \
> + defined(CONFIG_ARCH_SUPPORTS_INT128)
> +/* The compiler turns a u128 multiply into mul + umulh. */
> +#define umul_ppmm(w1, w0, u, v) \
> +do { \
> + u128 __ll = (u128)(u) * (v); \
> + w1 = __ll >> 64; \
> + w0 = __ll; \
> +} while (0)
> +#endif /* __aarch64__ */
Well, x86_64 has no code in this file either. This code seems to have
been written in the 1990s. It has optimizations for all sorts of CPUs
that no one has ever heard of, and not the ones actually used today.
CONFIG_ARCH_SUPPORTS_INT128 works on x86_64 and other architectures too.
How about dropping the __aarch64__ condition and putting this in the
'#if !defined(umul_ppmm)' section of the file around line 1283?
lib/crypto/mpi/ is past due for a rewrite, though. Minor tweaks like
this don't change that.
- Eric
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-27 19:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 17:16 [PATCH] lib/crypto: mpi: use a 128-bit multiply for umul_ppmm() on arm64 ecordonnier
2026-11-27 19:22 ` Eric Biggers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox