1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* high level subtraction (handles signs) */
7 mp_err
mp_sub(const mp_int
*a
, const mp_int
*b
, mp_int
*c
)
9 if (a
->sign
!= b
->sign
) {
10 /* subtract a negative from a positive, OR */
11 /* subtract a positive from a negative. */
12 /* In either case, ADD their magnitudes, */
13 /* and use the sign of the first number. */
15 return s_mp_add(a
, b
, c
);
18 /* subtract a positive from a positive, OR */
19 /* subtract a negative from a negative. */
20 /* First, take the difference between their */
21 /* magnitudes, then... */
22 if (mp_cmp_mag(a
, b
) == MP_LT
) {
23 /* The second has a larger magnitude */
24 /* The result has the *opposite* sign from */
25 /* the first number. */
26 c
->sign
= (!mp_isneg(a
) ? MP_NEG
: MP_ZPOS
);
27 MP_EXCH(const mp_int
*, a
, b
);
29 /* The first has a larger or equal magnitude */
30 /* Copy the sign from the first */
33 return s_mp_sub(a
, b
, c
);