comparison libtommath/bn_mp_cmp_mag.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_CMP_MAG_C 2 #ifdef BN_MP_CMP_MAG_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 * 4 /* SPDX-License-Identifier: Unlicense */
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, [email protected], http://libtom.org
16 */
17 5
18 /* compare maginitude of two ints (unsigned) */ 6 /* compare maginitude of two ints (unsigned) */
19 int mp_cmp_mag (mp_int * a, mp_int * b) 7 mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b)
20 { 8 {
21 int n; 9 int n;
22 mp_digit *tmpa, *tmpb; 10 const mp_digit *tmpa, *tmpb;
23 11
24 /* compare based on # of non-zero digits */ 12 /* compare based on # of non-zero digits */
25 if (a->used > b->used) { 13 if (a->used > b->used) {
26 return MP_GT; 14 return MP_GT;
27 } 15 }
28
29 if (a->used < b->used) {
30 return MP_LT;
31 }
32 16
33 /* alias for a */ 17 if (a->used < b->used) {
34 tmpa = a->dp + (a->used - 1); 18 return MP_LT;
19 }
35 20
36 /* alias for b */ 21 /* alias for a */
37 tmpb = b->dp + (a->used - 1); 22 tmpa = a->dp + (a->used - 1);
38 23
39 /* compare based on digits */ 24 /* alias for b */
40 for (n = 0; n < a->used; ++n, --tmpa, --tmpb) { 25 tmpb = b->dp + (a->used - 1);
41 if (*tmpa > *tmpb) {
42 return MP_GT;
43 }
44 26
45 if (*tmpa < *tmpb) { 27 /* compare based on digits */
46 return MP_LT; 28 for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
47 } 29 if (*tmpa > *tmpb) {
48 } 30 return MP_GT;
49 return MP_EQ; 31 }
32
33 if (*tmpa < *tmpb) {
34 return MP_LT;
35 }
36 }
37 return MP_EQ;
50 } 38 }
51 #endif 39 #endif
52
53 /* ref: $Format:%D$ */
54 /* git commit: $Format:%H$ */
55 /* commit time: $Format:%ai$ */