bn_mp_mod_2d.c 971 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_MOD_2D_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* calc a value mod 2**b */
  6. mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c)
  7. {
  8. int x;
  9. mp_err err;
  10. /* if b is <= 0 then zero the int */
  11. if (b <= 0) {
  12. mp_zero(c);
  13. return MP_OKAY;
  14. }
  15. /* if the modulus is larger than the value than return */
  16. if (b >= (a->used * MP_DIGIT_BIT)) {
  17. return mp_copy(a, c);
  18. }
  19. /* copy */
  20. if ((err = mp_copy(a, c)) != MP_OKAY) {
  21. return err;
  22. }
  23. /* zero digits above the last digit of the modulus */
  24. x = (b / MP_DIGIT_BIT) + (((b % MP_DIGIT_BIT) == 0) ? 0 : 1);
  25. MP_ZERO_DIGITS(c->dp + x, c->used - x);
  26. /* clear the digit that is not completely outside/inside the modulus */
  27. c->dp[b / MP_DIGIT_BIT] &=
  28. ((mp_digit)1 << (mp_digit)(b % MP_DIGIT_BIT)) - (mp_digit)1;
  29. mp_clamp(c);
  30. return MP_OKAY;
  31. }
  32. #endif