bn_mp_cmp_mag.c 810 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "tommath_private.h"
  2. #ifdef BN_MP_CMP_MAG_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. /* compare maginitude of two ints (unsigned) */
  6. mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b)
  7. {
  8. int n;
  9. const mp_digit *tmpa, *tmpb;
  10. /* compare based on # of non-zero digits */
  11. if (a->used > b->used) {
  12. return MP_GT;
  13. }
  14. if (a->used < b->used) {
  15. return MP_LT;
  16. }
  17. /* alias for a */
  18. tmpa = a->dp + (a->used - 1);
  19. /* alias for b */
  20. tmpb = b->dp + (a->used - 1);
  21. /* compare based on digits */
  22. for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
  23. if (*tmpa > *tmpb) {
  24. return MP_GT;
  25. }
  26. if (*tmpa < *tmpb) {
  27. return MP_LT;
  28. }
  29. }
  30. return MP_EQ;
  31. }
  32. #endif