bn_mp_copy.c 900 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_COPY_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* copy, b = a */
  6. mp_err mp_copy(const mp_int *a, mp_int *b)
  7. {
  8. int n;
  9. mp_digit *tmpa, *tmpb;
  10. mp_err err;
  11. /* if dst == src do nothing */
  12. if (a == b) {
  13. return MP_OKAY;
  14. }
  15. /* grow dest */
  16. if (b->alloc < a->used) {
  17. if ((err = mp_grow(b, a->used)) != MP_OKAY) {
  18. return err;
  19. }
  20. }
  21. /* zero b and copy the parameters over */
  22. /* pointer aliases */
  23. /* source */
  24. tmpa = a->dp;
  25. /* destination */
  26. tmpb = b->dp;
  27. /* copy all the digits */
  28. for (n = 0; n < a->used; n++) {
  29. *tmpb++ = *tmpa++;
  30. }
  31. /* clear high digits */
  32. MP_ZERO_DIGITS(tmpb, b->used - n);
  33. /* copy used count and sign */
  34. b->used = a->used;
  35. b->sign = a->sign;
  36. return MP_OKAY;
  37. }
  38. #endif