From a5c70cbde2039249ddc7543b92342954595ec2a5 Mon Sep 17 00:00:00 2001 From: jsing <> Date: Tue, 5 Aug 2025 15:15:54 +0000 Subject: Speed up bn_{mod,sqr}_mul_words() for specific inputs. Use bn_{mul,sqr}_comba{4,6,8}() and bn_montgomery_reduce_words() for specific input sizes. This is significantly faster than using bn_montgomery_multiply_words(). ok tb@ --- src/lib/libcrypto/bn/bn_mod_words.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src/lib') diff --git a/src/lib/libcrypto/bn/bn_mod_words.c b/src/lib/libcrypto/bn/bn_mod_words.c index 4cc41717b4..d9aee8701a 100644 --- a/src/lib/libcrypto/bn/bn_mod_words.c +++ b/src/lib/libcrypto/bn/bn_mod_words.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_mod_words.c,v 1.2 2025/08/02 16:20:00 jsing Exp $ */ +/* $OpenBSD: bn_mod_words.c,v 1.3 2025/08/05 15:15:54 jsing Exp $ */ /* * Copyright (c) 2024 Joel Sing * @@ -73,7 +73,18 @@ void bn_mod_mul_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, BN_ULONG *t, BN_ULONG m0, size_t n) { - bn_montgomery_multiply_words(r, a, b, m, t, m0, n); + if (n == 4) { + bn_mul_comba4(t, a, b); + bn_montgomery_reduce_words(r, t, m, m0, n); + } else if (n == 6) { + bn_mul_comba6(t, a, b); + bn_montgomery_reduce_words(r, t, m, m0, n); + } else if (n == 8) { + bn_mul_comba8(t, a, b); + bn_montgomery_reduce_words(r, t, m, m0, n); + } else { + bn_montgomery_multiply_words(r, a, b, m, t, m0, n); + } } #endif @@ -87,6 +98,17 @@ void bn_mod_sqr_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *m, BN_ULONG *t, BN_ULONG m0, size_t n) { - bn_montgomery_multiply_words(r, a, a, m, t, m0, n); + if (n == 4) { + bn_sqr_comba4(t, a); + bn_montgomery_reduce_words(r, t, m, m0, n); + } else if (n == 6) { + bn_sqr_comba6(t, a); + bn_montgomery_reduce_words(r, t, m, m0, n); + } else if (n == 8) { + bn_sqr_comba8(t, a); + bn_montgomery_reduce_words(r, t, m, m0, n); + } else { + bn_montgomery_multiply_words(r, a, a, m, t, m0, n); + } } #endif -- cgit v1.2.3-55-g6feb