From 99bf4692bf9b3bd9f10766e8554a4dfc4f9ebe9b Mon Sep 17 00:00:00 2001 From: jsing Date: Wed, 21 Jun 2023 07:48:41 +0000 Subject: [PATCH] Provide and use bn_clzw() in place of bn_word_clz(). On some architectures, we can provide an optimised (often single instruction) count-leading-zero implementation. In order to do this effectively, provide bn_clzw() as a static inline that can be replaced by an architecture specific version. The default implementation defers to the bn_word_clz() function (which may also be architecture specific). ok tb@ --- lib/libcrypto/bn/bn_internal.h | 10 +++++++++- lib/libcrypto/bn/bn_lib.c | 4 ++-- lib/libcrypto/bn/bn_primitives.c | 6 ++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/libcrypto/bn/bn_internal.h b/lib/libcrypto/bn/bn_internal.h index f5c69c5d77c..b712b736f6a 100644 --- a/lib/libcrypto/bn/bn_internal.h +++ b/lib/libcrypto/bn/bn_internal.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_internal.h,v 1.13 2023/06/21 07:41:55 jsing Exp $ */ +/* $OpenBSD: bn_internal.h,v 1.14 2023/06/21 07:48:41 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -58,6 +58,14 @@ bn_ct_eq_zero_mask(BN_ULONG w) } #endif +#ifndef HAVE_BN_CLZW +static inline int +bn_clzw(BN_ULONG w) +{ + return bn_word_clz(w); +} +#endif + /* * Big number primitives are named as the operation followed by a suffix * that indicates the number of words that it operates on, where 'w' means diff --git a/lib/libcrypto/bn/bn_lib.c b/lib/libcrypto/bn/bn_lib.c index b8eb5654971..bac0290efa2 100644 --- a/lib/libcrypto/bn/bn_lib.c +++ b/lib/libcrypto/bn/bn_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_lib.c,v 1.87 2023/06/21 07:41:55 jsing Exp $ */ +/* $OpenBSD: bn_lib.c,v 1.88 2023/06/21 07:48:41 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -162,7 +162,7 @@ BN_value_one(void) int BN_num_bits_word(BN_ULONG w) { - return BN_BITS2 - bn_word_clz(w); + return BN_BITS2 - bn_clzw(w); } int diff --git a/lib/libcrypto/bn/bn_primitives.c b/lib/libcrypto/bn/bn_primitives.c index e9caec48184..66427a90468 100644 --- a/lib/libcrypto/bn/bn_primitives.c +++ b/lib/libcrypto/bn/bn_primitives.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_primitives.c,v 1.1 2023/06/21 07:41:55 jsing Exp $ */ +/* $OpenBSD: bn_primitives.c,v 1.2 2023/06/21 07:48:41 jsing Exp $ */ /* * Copyright (c) 2023 Joel Sing * @@ -21,6 +21,7 @@ #include "bn_internal.h" #include "bn_local.h" +#ifndef HAVE_BN_CLZW #ifndef HAVE_BN_WORD_CLZ int bn_word_clz(BN_ULONG w) @@ -41,6 +42,7 @@ bn_word_clz(BN_ULONG w) return BN_BITS2 - bits; } #endif +#endif #ifndef HAVE_BN_BITSIZE int @@ -58,6 +60,6 @@ bn_bitsize(const BIGNUM *bn) i++; } - return (n + 1) * BN_BITS2 - bn_word_clz(x); + return (n + 1) * BN_BITS2 - bn_clzw(x); } #endif -- 2.20.1