bn_mp_mul_2.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_MUL_2_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* b = a*2 */
  6. mp_err mp_mul_2(const mp_int *a, mp_int *b)
  7. {
  8. int x, oldused;
  9. mp_err err;
  10. /* grow to accomodate result */
  11. if (b->alloc < (a->used + 1)) {
  12. if ((err = mp_grow(b, a->used + 1)) != MP_OKAY) {
  13. return err;
  14. }
  15. }
  16. oldused = b->used;
  17. b->used = a->used;
  18. {
  19. mp_digit r, rr, *tmpa, *tmpb;
  20. /* alias for source */
  21. tmpa = a->dp;
  22. /* alias for dest */
  23. tmpb = b->dp;
  24. /* carry */
  25. r = 0;
  26. for (x = 0; x < a->used; x++) {
  27. /* get what will be the *next* carry bit from the
  28. * MSB of the current digit
  29. */
  30. rr = *tmpa >> (mp_digit)(MP_DIGIT_BIT - 1);
  31. /* now shift up this digit, add in the carry [from the previous] */
  32. *tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK;
  33. /* copy the carry that would be from the source
  34. * digit into the next iteration
  35. */
  36. r = rr;
  37. }
  38. /* new leading digit? */
  39. if (r != 0u) {
  40. /* add a MSB which is always 1 at this point */
  41. *tmpb = 1;
  42. ++(b->used);
  43. }
  44. /* now zero any excess digits on the destination
  45. * that we didn't write to
  46. */
  47. MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
  48. }
  49. b->sign = a->sign;
  50. return MP_OKAY;
  51. }
  52. #endif