bn_mp_mul_2.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <tommath.h>
  2. #ifdef BN_MP_MUL_2_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. /* b = a*2 */
  18. int mp_mul_2(mp_int * a, mp_int * b)
  19. {
  20. int x, res, oldused;
  21. /* grow to accomodate result */
  22. if (b->alloc < a->used + 1) {
  23. if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) {
  24. return res;
  25. }
  26. }
  27. oldused = b->used;
  28. b->used = a->used;
  29. {
  30. register mp_digit r, rr, *tmpa, *tmpb;
  31. /* alias for source */
  32. tmpa = a->dp;
  33. /* alias for dest */
  34. tmpb = b->dp;
  35. /* carry */
  36. r = 0;
  37. for (x = 0; x < a->used; x++) {
  38. /* get what will be the *next* carry bit from the
  39. * MSB of the current digit
  40. */
  41. rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
  42. /* now shift up this digit, add in the carry [from the previous] */
  43. *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
  44. /* copy the carry that would be from the source
  45. * digit into the next iteration
  46. */
  47. r = rr;
  48. }
  49. /* new leading digit? */
  50. if (r != 0) {
  51. /* add a MSB which is always 1 at this point */
  52. *tmpb = 1;
  53. ++(b->used);
  54. }
  55. /* now zero any excess digits on the destination
  56. * that we didn't write to
  57. */
  58. tmpb = b->dp + b->used;
  59. for (x = b->used; x < oldused; x++) {
  60. *tmpb++ = 0;
  61. }
  62. }
  63. b->sign = a->sign;
  64. return MP_OKAY;
  65. }
  66. #endif
  67. /* $Source: /cvs/libtom/libtommath/bn_mp_mul_2.c,v $ */
  68. /* $Revision: 1.3 $ */
  69. /* $Date: 2006/03/31 14:18:44 $ */