2020-12-28 21:40:37 +00:00
|
|
|
#include "tommath_private.h"
|
2014-12-10 21:56:49 +00:00
|
|
|
#ifdef BN_MP_REDUCE_IS_2K_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
|
|
|
|
|
|
|
/* determines if mp_reduce_2k can be used */
|
2020-12-28 21:40:37 +00:00
|
|
|
mp_bool mp_reduce_is_2k(const mp_int *a)
|
2014-12-10 21:56:49 +00:00
|
|
|
{
|
|
|
|
int ix, iy, iw;
|
|
|
|
mp_digit iz;
|
2020-12-28 21:40:37 +00:00
|
|
|
|
2014-12-10 21:56:49 +00:00
|
|
|
if (a->used == 0) {
|
|
|
|
return MP_NO;
|
|
|
|
} else if (a->used == 1) {
|
|
|
|
return MP_YES;
|
|
|
|
} else if (a->used > 1) {
|
|
|
|
iy = mp_count_bits(a);
|
|
|
|
iz = 1;
|
|
|
|
iw = 1;
|
2020-12-28 21:40:37 +00:00
|
|
|
|
2014-12-10 21:56:49 +00:00
|
|
|
/* Test every bit from the second digit up, must be 1 */
|
2020-12-28 21:40:37 +00:00
|
|
|
for (ix = MP_DIGIT_BIT; ix < iy; ix++) {
|
|
|
|
if ((a->dp[iw] & iz) == 0u) {
|
|
|
|
return MP_NO;
|
|
|
|
}
|
|
|
|
iz <<= 1;
|
|
|
|
if (iz > MP_DIGIT_MAX) {
|
|
|
|
++iw;
|
|
|
|
iz = 1;
|
|
|
|
}
|
2014-12-10 21:56:49 +00:00
|
|
|
}
|
2020-12-28 21:40:37 +00:00
|
|
|
return MP_YES;
|
|
|
|
} else {
|
|
|
|
return MP_YES;
|
2014-12-10 21:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|