bn_mp_rshd.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <tommath.h>
  2. #ifdef BN_MP_RSHD_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis
  4. *
  5. * LibTomMath is a library that provides multiple-precision
  6. * integer arithmetic as well as number theoretic functionality.
  7. *
  8. * The library was designed directly after the MPI library by
  9. * Michael Fromberger but has been written from scratch with
  10. * additional optimizations in place.
  11. *
  12. * The library is free for all purposes without any express
  13. * guarantee it works.
  14. *
  15. * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
  16. */
  17. /* shift right a certain amount of digits */
  18. void mp_rshd (mp_int * a, int b)
  19. {
  20. int x;
  21. /* if b <= 0 then ignore it */
  22. if (b <= 0) {
  23. return;
  24. }
  25. /* if b > used then simply zero it and return */
  26. if (a->used <= b) {
  27. mp_zero (a);
  28. return;
  29. }
  30. {
  31. register mp_digit *bottom, *top;
  32. /* shift the digits down */
  33. /* bottom */
  34. bottom = a->dp;
  35. /* top [offset into digits] */
  36. top = a->dp + b;
  37. /* this is implemented as a sliding window where
  38. * the window is b-digits long and digits from
  39. * the top of the window are copied to the bottom
  40. *
  41. * e.g.
  42. b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
  43. /\ | ---->
  44. \-------------------/ ---->
  45. */
  46. for (x = 0; x < (a->used - b); x++) {
  47. *bottom++ = *top++;
  48. }
  49. /* zero the top digits */
  50. for (; x < a->used; x++) {
  51. *bottom++ = 0;
  52. }
  53. }
  54. /* remove excess digits */
  55. a->used -= b;
  56. }
  57. #endif
  58. /* $Source: /cvs/libtom/libtommath/bn_mp_rshd.c,v $ */
  59. /* $Revision: 1.3 $ */
  60. /* $Date: 2006/03/31 14:18:44 $ */