bn_mp_rshd.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_RSHD_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* shift right a certain amount of digits */
  6. void mp_rshd(mp_int *a, int b)
  7. {
  8. int x;
  9. mp_digit *bottom, *top;
  10. /* if b <= 0 then ignore it */
  11. if (b <= 0) {
  12. return;
  13. }
  14. /* if b > used then simply zero it and return */
  15. if (a->used <= b) {
  16. mp_zero(a);
  17. return;
  18. }
  19. /* shift the digits down */
  20. /* bottom */
  21. bottom = a->dp;
  22. /* top [offset into digits] */
  23. top = a->dp + b;
  24. /* this is implemented as a sliding window where
  25. * the window is b-digits long and digits from
  26. * the top of the window are copied to the bottom
  27. *
  28. * e.g.
  29. b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
  30. /\ | ---->
  31. \-------------------/ ---->
  32. */
  33. for (x = 0; x < (a->used - b); x++) {
  34. *bottom++ = *top++;
  35. }
  36. /* zero the top digits */
  37. MP_ZERO_DIGITS(bottom, a->used - x);
  38. /* remove excess digits */
  39. a->used -= b;
  40. }
  41. #endif