| 1 |
#include "tommath_private.h" |
| 2 |
#ifdef BN_MP_CNT_LSB_C |
| 3 |
/* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
| 4 |
/* SPDX-License-Identifier: Unlicense */ |
| 5 |
|
| 6 |
static const int lnz[16] = { |
| 7 |
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 |
| 8 |
}; |
| 9 |
|
| 10 |
/* Counts the number of lsbs which are zero before the first zero bit */ |
| 11 |
int mp_cnt_lsb(const mp_int *a) |
| 12 |
{ |
| 13 |
int x; |
| 14 |
mp_digit q, qq; |
| 15 |
|
| 16 |
/* easy out */ |
| 17 |
if (MP_IS_ZERO(a)) { |
| 18 |
return 0; |
| 19 |
} |
| 20 |
|
| 21 |
/* scan lower digits until non-zero */ |
| 22 |
for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {} |
| 23 |
q = a->dp[x]; |
| 24 |
x *= MP_DIGIT_BIT; |
| 25 |
|
| 26 |
/* now scan this digit until a 1 is found */ |
| 27 |
if ((q & 1u) == 0u) { |
| 28 |
do { |
| 29 |
qq = q & 15u; |
| 30 |
x += lnz[qq]; |
| 31 |
q >>= 4; |
| 32 |
} while (qq == 0u); |
| 33 |
} |
| 34 |
return x; |
| 35 |
} |
| 36 |
|
| 37 |
#endif |