bn_mp_lshd.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_LSHD_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* shift left a certain amount of digits */
  6. mp_err mp_lshd(mp_int *a, int b)
  7. {
  8. int x;
  9. mp_err err;
  10. mp_digit *top, *bottom;
  11. /* if its less than zero return */
  12. if (b <= 0) {
  13. return MP_OKAY;
  14. }
  15. /* no need to shift 0 around */
  16. if (MP_IS_ZERO(a)) {
  17. return MP_OKAY;
  18. }
  19. /* grow to fit the new digits */
  20. if (a->alloc < (a->used + b)) {
  21. if ((err = mp_grow(a, a->used + b)) != MP_OKAY) {
  22. return err;
  23. }
  24. }
  25. /* increment the used by the shift amount then copy upwards */
  26. a->used += b;
  27. /* top */
  28. top = a->dp + a->used - 1;
  29. /* base */
  30. bottom = (a->dp + a->used - 1) - b;
  31. /* much like mp_rshd this is implemented using a sliding window
  32. * except the window goes the otherway around. Copying from
  33. * the bottom to the top. see bn_mp_rshd.c for more info.
  34. */
  35. for (x = a->used - 1; x >= b; x--) {
  36. *top-- = *bottom--;
  37. }
  38. /* zero the lower digits */
  39. MP_ZERO_DIGITS(a->dp, b);
  40. return MP_OKAY;
  41. }
  42. #endif