bn_mp_mul_d.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <tommath.h>
  2. #ifdef BN_MP_MUL_D_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. /* multiply by a digit */
  18. int
  19. mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
  20. {
  21. mp_digit u, *tmpa, *tmpc;
  22. mp_word r;
  23. int ix, res, olduse;
  24. /* make sure c is big enough to hold a*b */
  25. if (c->alloc < a->used + 1) {
  26. if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) {
  27. return res;
  28. }
  29. }
  30. /* get the original destinations used count */
  31. olduse = c->used;
  32. /* set the sign */
  33. c->sign = a->sign;
  34. /* alias for a->dp [source] */
  35. tmpa = a->dp;
  36. /* alias for c->dp [dest] */
  37. tmpc = c->dp;
  38. /* zero carry */
  39. u = 0;
  40. /* compute columns */
  41. for (ix = 0; ix < a->used; ix++) {
  42. /* compute product and carry sum for this term */
  43. r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
  44. /* mask off higher bits to get a single digit */
  45. *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
  46. /* send carry into next iteration */
  47. u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
  48. }
  49. /* store final carry [if any] and increment ix offset */
  50. *tmpc++ = u;
  51. ++ix;
  52. /* now zero digits above the top */
  53. while (ix++ < olduse) {
  54. *tmpc++ = 0;
  55. }
  56. /* set used count */
  57. c->used = a->used + 1;
  58. mp_clamp(c);
  59. return MP_OKAY;
  60. }
  61. #endif
  62. /* $Source: /cvs/libtom/libtommath/bn_mp_mul_d.c,v $ */
  63. /* $Revision: 1.3 $ */
  64. /* $Date: 2006/03/31 14:18:44 $ */