From 3b29364d61d7ef5ca901240f7343fed9a7815485 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Wed, 26 Apr 2023 21:53:30 +0100 Subject: [PATCH 1/2] Fix VS2022 build error Signed-off-by: Dave Rodgman --- library/bignum_core.c | 33 --------------------------------- library/bignum_core.h | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/library/bignum_core.c b/library/bignum_core.c index a99b3be2bb..a416a015f0 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -33,39 +33,6 @@ #include "bn_mul.h" #include "constant_time_internal.h" -inline size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a) -{ -#if defined(__has_builtin) -#if __has_builtin(__builtin_clz) - if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned int)) { - return (size_t) __builtin_clz(a); - } -#endif -#if __has_builtin(__builtin_clzl) - if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long)) { - return (size_t) __builtin_clzl(a); - } -#endif -#if __has_builtin(__builtin_clzll) - if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long long)) { - return (size_t) __builtin_clzll(a); - } -#endif -#endif - size_t j; - mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); - - for (j = 0; j < biL; j++) { - if (a & mask) { - break; - } - - mask >>= 1; - } - - return j; -} - size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs) { int i; diff --git a/library/bignum_core.h b/library/bignum_core.h index 158d2b3222..d8731b39ad 100644 --- a/library/bignum_core.h +++ b/library/bignum_core.h @@ -109,7 +109,38 @@ * \return The number of leading zero bits in \p a, if \p a != 0. * If \p a == 0, the result is undefined. */ -size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a); +static inline size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a) +{ +#if defined(__has_builtin) +#if __has_builtin(__builtin_clz) + if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned int)) { + return (size_t) __builtin_clz(a); + } +#endif +#if __has_builtin(__builtin_clzl) + if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long)) { + return (size_t) __builtin_clzl(a); + } +#endif +#if __has_builtin(__builtin_clzll) + if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long long)) { + return (size_t) __builtin_clzll(a); + } +#endif +#endif + size_t j; + mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); + + for (j = 0; j < biL; j++) { + if (a & mask) { + break; + } + + mask >>= 1; + } + + return j; +} /** Return the minimum number of bits required to represent the value held * in the MPI. From 914347bfa3cd7889297059c4688bc2fe4e634ec7 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Thu, 27 Apr 2023 14:20:30 +0100 Subject: [PATCH 2/2] Don't explicitly inline mbedtls_mpi_core_clz Signed-off-by: Dave Rodgman --- library/bignum_core.c | 33 +++++++++++++++++++++++++++++++++ library/bignum_core.h | 33 +-------------------------------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/library/bignum_core.c b/library/bignum_core.c index a416a015f0..b0ffa37d3c 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -33,6 +33,39 @@ #include "bn_mul.h" #include "constant_time_internal.h" +size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a) +{ +#if defined(__has_builtin) +#if __has_builtin(__builtin_clz) + if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned int)) { + return (size_t) __builtin_clz(a); + } +#endif +#if __has_builtin(__builtin_clzl) + if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long)) { + return (size_t) __builtin_clzl(a); + } +#endif +#if __has_builtin(__builtin_clzll) + if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long long)) { + return (size_t) __builtin_clzll(a); + } +#endif +#endif + size_t j; + mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); + + for (j = 0; j < biL; j++) { + if (a & mask) { + break; + } + + mask >>= 1; + } + + return j; +} + size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs) { int i; diff --git a/library/bignum_core.h b/library/bignum_core.h index d8731b39ad..158d2b3222 100644 --- a/library/bignum_core.h +++ b/library/bignum_core.h @@ -109,38 +109,7 @@ * \return The number of leading zero bits in \p a, if \p a != 0. * If \p a == 0, the result is undefined. */ -static inline size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a) -{ -#if defined(__has_builtin) -#if __has_builtin(__builtin_clz) - if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned int)) { - return (size_t) __builtin_clz(a); - } -#endif -#if __has_builtin(__builtin_clzl) - if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long)) { - return (size_t) __builtin_clzl(a); - } -#endif -#if __has_builtin(__builtin_clzll) - if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned long long)) { - return (size_t) __builtin_clzll(a); - } -#endif -#endif - size_t j; - mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1); - - for (j = 0; j < biL; j++) { - if (a & mask) { - break; - } - - mask >>= 1; - } - - return j; -} +size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a); /** Return the minimum number of bits required to represent the value held * in the MPI.