bn_mp_reduce_is_2k.c 838 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_REDUCE_IS_2K_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* determines if mp_reduce_2k can be used */
  6. mp_bool mp_reduce_is_2k(const mp_int *a)
  7. {
  8. int ix, iy, iw;
  9. mp_digit iz;
  10. if (a->used == 0) {
  11. return MP_NO;
  12. } else if (a->used == 1) {
  13. return MP_YES;
  14. } else if (a->used > 1) {
  15. iy = mp_count_bits(a);
  16. iz = 1;
  17. iw = 1;
  18. /* Test every bit from the second digit up, must be 1 */
  19. for (ix = MP_DIGIT_BIT; ix < iy; ix++) {
  20. if ((a->dp[iw] & iz) == 0u) {
  21. return MP_NO;
  22. }
  23. iz <<= 1;
  24. if (iz > MP_DIGIT_MAX) {
  25. ++iw;
  26. iz = 1;
  27. }
  28. }
  29. return MP_YES;
  30. } else {
  31. return MP_YES;
  32. }
  33. }
  34. #endif