bn_mp_add.c 1008 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_ADD_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* high level addition (handles signs) */
  6. mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
  7. {
  8. mp_sign sa, sb;
  9. mp_err err;
  10. /* get sign of both inputs */
  11. sa = a->sign;
  12. sb = b->sign;
  13. /* handle two cases, not four */
  14. if (sa == sb) {
  15. /* both positive or both negative */
  16. /* add their magnitudes, copy the sign */
  17. c->sign = sa;
  18. err = s_mp_add(a, b, c);
  19. } else {
  20. /* one positive, the other negative */
  21. /* subtract the one with the greater magnitude from */
  22. /* the one of the lesser magnitude. The result gets */
  23. /* the sign of the one with the greater magnitude. */
  24. if (mp_cmp_mag(a, b) == MP_LT) {
  25. c->sign = sb;
  26. err = s_mp_sub(b, a, c);
  27. } else {
  28. c->sign = sa;
  29. err = s_mp_sub(a, b, c);
  30. }
  31. }
  32. return err;
  33. }
  34. #endif