bn_mp_mul_2d.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <tommath.h>
  2. #ifdef BN_MP_MUL_2D_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 left by a certain bit count */
  18. int mp_mul_2d (mp_int * a, int b, mp_int * c)
  19. {
  20. mp_digit d;
  21. int res;
  22. /* copy */
  23. if (a != c) {
  24. if ((res = mp_copy (a, c)) != MP_OKAY) {
  25. return res;
  26. }
  27. }
  28. if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) {
  29. if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) {
  30. return res;
  31. }
  32. }
  33. /* shift by as many digits in the bit count */
  34. if (b >= (int)DIGIT_BIT) {
  35. if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) {
  36. return res;
  37. }
  38. }
  39. /* shift any bit count < DIGIT_BIT */
  40. d = (mp_digit) (b % DIGIT_BIT);
  41. if (d != 0) {
  42. register mp_digit *tmpc, shift, mask, r, rr;
  43. register int x;
  44. /* bitmask for carries */
  45. mask = (((mp_digit)1) << d) - 1;
  46. /* shift for msbs */
  47. shift = DIGIT_BIT - d;
  48. /* alias */
  49. tmpc = c->dp;
  50. /* carry */
  51. r = 0;
  52. for (x = 0; x < c->used; x++) {
  53. /* get the higher bits of the current word */
  54. rr = (*tmpc >> shift) & mask;
  55. /* shift the current word and OR in the carry */
  56. *tmpc = ((*tmpc << d) | r) & MP_MASK;
  57. ++tmpc;
  58. /* set the carry to the carry bits of the current word */
  59. r = rr;
  60. }
  61. /* set final carry */
  62. if (r != 0) {
  63. c->dp[(c->used)++] = r;
  64. }
  65. }
  66. mp_clamp (c);
  67. return MP_OKAY;
  68. }
  69. #endif
  70. /* $Source: /cvs/libtom/libtommath/bn_mp_mul_2d.c,v $ */
  71. /* $Revision: 1.3 $ */
  72. /* $Date: 2006/03/31 14:18:44 $ */