bn_mp_add.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <tommath.h>
  2. #ifdef BN_MP_ADD_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. /* high level addition (handles signs) */
  18. int mp_add (mp_int * a, mp_int * b, mp_int * c)
  19. {
  20. int sa, sb, res;
  21. /* get sign of both inputs */
  22. sa = a->sign;
  23. sb = b->sign;
  24. /* handle two cases, not four */
  25. if (sa == sb) {
  26. /* both positive or both negative */
  27. /* add their magnitudes, copy the sign */
  28. c->sign = sa;
  29. res = s_mp_add (a, b, c);
  30. } else {
  31. /* one positive, the other negative */
  32. /* subtract the one with the greater magnitude from */
  33. /* the one of the lesser magnitude. The result gets */
  34. /* the sign of the one with the greater magnitude. */
  35. if (mp_cmp_mag (a, b) == MP_LT) {
  36. c->sign = sb;
  37. res = s_mp_sub (b, a, c);
  38. } else {
  39. c->sign = sa;
  40. res = s_mp_sub (a, b, c);
  41. }
  42. }
  43. return res;
  44. }
  45. #endif
  46. /* $Source: /cvs/libtom/libtommath/bn_mp_add.c,v $ */
  47. /* $Revision: 1.3 $ */
  48. /* $Date: 2006/03/31 14:18:44 $ */