Document undefined case. Clarify test code.
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
parent
678e63007c
commit
bbf881053d
2 changed files with 19 additions and 9 deletions
|
@ -33,11 +33,18 @@
|
|||
#include "bn_mul.h"
|
||||
#include "constant_time_internal.h"
|
||||
|
||||
/**
|
||||
* \brief Count leading zeros
|
||||
*
|
||||
* \warning The result is undefined if \p a == 0
|
||||
*
|
||||
* \param a The value to operate on
|
||||
*
|
||||
* \return The number of leading zeros, if \p a != 0. If \p a == 0, the result
|
||||
* is undefined.
|
||||
*/
|
||||
inline size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
|
||||
{
|
||||
/* Note: the result is undefined for a == 0
|
||||
* (because this is the behaviour of __builtin_clz).
|
||||
*/
|
||||
#if defined(__has_builtin)
|
||||
#if __has_builtin(__builtin_clz)
|
||||
if (sizeof(mbedtls_mpi_uint) == sizeof(unsigned int)) {
|
||||
|
|
|
@ -311,25 +311,28 @@ exit:
|
|||
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_clz(int lz, int tz)
|
||||
void mpi_core_clz(int leading_zeros, int trailing_zeros)
|
||||
{
|
||||
if ((size_t) (lz + tz) >= (sizeof(mbedtls_mpi_uint) * 8)) {
|
||||
if ((size_t) (leading_zeros + trailing_zeros) >= (sizeof(mbedtls_mpi_uint) * 8)) {
|
||||
// can't fit required number of leading and trailing zeros - skip test
|
||||
goto exit;
|
||||
}
|
||||
|
||||
// Construct a test input value where the count of leading zeros and
|
||||
// trailing zeros is given in the test case, and we add ones to fill
|
||||
// the gap.
|
||||
mbedtls_mpi_uint x;
|
||||
if ((lz + tz) > 0) {
|
||||
if ((leading_zeros + trailing_zeros) > 0) {
|
||||
// some zero bits
|
||||
uint32_t s = (sizeof(mbedtls_mpi_uint) * 8 - lz - tz);
|
||||
x = ((((mbedtls_mpi_uint) 1) << s) - 1) << tz;
|
||||
uint32_t s = (sizeof(mbedtls_mpi_uint) * 8 - leading_zeros - trailing_zeros);
|
||||
x = ((((mbedtls_mpi_uint) 1) << s) - 1) << trailing_zeros;
|
||||
} else {
|
||||
// all bits set
|
||||
x = ~((mbedtls_mpi_uint) 0);
|
||||
}
|
||||
|
||||
size_t n = mbedtls_mpi_core_clz(x);
|
||||
TEST_EQUAL(n, lz);
|
||||
TEST_EQUAL(n, leading_zeros);
|
||||
exit:
|
||||
;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue