bn_mp_decr.c 790 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_DECR_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* Decrement "a" by one like "a--". Changes input! */
  6. mp_err mp_decr(mp_int *a)
  7. {
  8. if (MP_IS_ZERO(a)) {
  9. mp_set(a,1uL);
  10. a->sign = MP_NEG;
  11. return MP_OKAY;
  12. } else if (a->sign == MP_NEG) {
  13. mp_err err;
  14. a->sign = MP_ZPOS;
  15. if ((err = mp_incr(a)) != MP_OKAY) {
  16. return err;
  17. }
  18. /* There is no -0 in LTM */
  19. if (!MP_IS_ZERO(a)) {
  20. a->sign = MP_NEG;
  21. }
  22. return MP_OKAY;
  23. } else if (a->dp[0] > 1uL) {
  24. a->dp[0]--;
  25. if (a->dp[0] == 0u) {
  26. mp_zero(a);
  27. }
  28. return MP_OKAY;
  29. } else {
  30. return mp_sub_d(a, 1uL,a);
  31. }
  32. }
  33. #endif