bn_mp_incr.c 718 B

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