1
0
mirror of http://galexander.org/git/simplesshd.git synced 2024-11-30 19:28:10 +00:00
simplesshd/dropbear/libtommath/bn_mp_cnt_lsb.c

38 lines
791 B
C
Raw Normal View History

2020-12-28 21:40:37 +00:00
#include "tommath_private.h"
2014-12-10 21:56:49 +00:00
#ifdef BN_MP_CNT_LSB_C
2020-12-28 21:40:37 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2014-12-10 21:56:49 +00:00
2020-12-28 21:40:37 +00:00
static const int lnz[16] = {
2014-12-10 21:56:49 +00:00
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
/* Counts the number of lsbs which are zero before the first zero bit */
2020-12-28 21:40:37 +00:00
int mp_cnt_lsb(const mp_int *a)
2014-12-10 21:56:49 +00:00
{
int x;
mp_digit q, qq;
/* easy out */
2020-12-28 21:40:37 +00:00
if (MP_IS_ZERO(a)) {
2014-12-10 21:56:49 +00:00
return 0;
}
/* scan lower digits until non-zero */
2020-12-28 21:40:37 +00:00
for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {}
2014-12-10 21:56:49 +00:00
q = a->dp[x];
2020-12-28 21:40:37 +00:00
x *= MP_DIGIT_BIT;
2014-12-10 21:56:49 +00:00
/* now scan this digit until a 1 is found */
2020-12-28 21:40:37 +00:00
if ((q & 1u) == 0u) {
2014-12-10 21:56:49 +00:00
do {
2020-12-28 21:40:37 +00:00
qq = q & 15u;
2014-12-10 21:56:49 +00:00
x += lnz[qq];
q >>= 4;
2020-12-28 21:40:37 +00:00
} while (qq == 0u);
2014-12-10 21:56:49 +00:00
}
return x;
}
#endif