bn_mp_mul_d.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_MUL_D_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* multiply by a digit */
  6. mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
  7. {
  8. mp_digit u, *tmpa, *tmpc;
  9. mp_word r;
  10. mp_err err;
  11. int ix, olduse;
  12. /* make sure c is big enough to hold a*b */
  13. if (c->alloc < (a->used + 1)) {
  14. if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
  15. return err;
  16. }
  17. }
  18. /* get the original destinations used count */
  19. olduse = c->used;
  20. /* set the sign */
  21. c->sign = a->sign;
  22. /* alias for a->dp [source] */
  23. tmpa = a->dp;
  24. /* alias for c->dp [dest] */
  25. tmpc = c->dp;
  26. /* zero carry */
  27. u = 0;
  28. /* compute columns */
  29. for (ix = 0; ix < a->used; ix++) {
  30. /* compute product and carry sum for this term */
  31. r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
  32. /* mask off higher bits to get a single digit */
  33. *tmpc++ = (mp_digit)(r & (mp_word)MP_MASK);
  34. /* send carry into next iteration */
  35. u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
  36. }
  37. /* store final carry [if any] and increment ix offset */
  38. *tmpc++ = u;
  39. ++ix;
  40. /* now zero digits above the top */
  41. MP_ZERO_DIGITS(tmpc, olduse - ix);
  42. /* set used count */
  43. c->used = a->used + 1;
  44. mp_clamp(c);
  45. return MP_OKAY;
  46. }
  47. #endif