2
|
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2 * |
|
3 * LibTomMath is a library that provides multiple-precision |
|
4 * integer arithmetic as well as number theoretic functionality. |
|
5 * |
|
6 * The library was designed directly after the MPI library by |
|
7 * Michael Fromberger but has been written from scratch with |
|
8 * additional optimizations in place. |
|
9 * |
|
10 * The library is free for all purposes without any express |
|
11 * guarantee it works. |
|
12 * |
|
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
14 */ |
|
15 #include <tommath.h> |
|
16 |
|
17 /* high level subtraction (handles signs) */ |
|
18 int |
|
19 mp_sub (mp_int * a, mp_int * b, mp_int * c) |
|
20 { |
|
21 int sa, sb, res; |
|
22 |
|
23 sa = a->sign; |
|
24 sb = b->sign; |
|
25 |
|
26 if (sa != sb) { |
|
27 /* subtract a negative from a positive, OR */ |
|
28 /* subtract a positive from a negative. */ |
|
29 /* In either case, ADD their magnitudes, */ |
|
30 /* and use the sign of the first number. */ |
|
31 c->sign = sa; |
|
32 res = s_mp_add (a, b, c); |
|
33 } else { |
|
34 /* subtract a positive from a positive, OR */ |
|
35 /* subtract a negative from a negative. */ |
|
36 /* First, take the difference between their */ |
|
37 /* magnitudes, then... */ |
|
38 if (mp_cmp_mag (a, b) != MP_LT) { |
|
39 /* Copy the sign from the first */ |
|
40 c->sign = sa; |
|
41 /* The first has a larger or equal magnitude */ |
|
42 res = s_mp_sub (a, b, c); |
|
43 } else { |
|
44 /* The result has the *opposite* sign from */ |
|
45 /* the first number. */ |
|
46 c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS; |
|
47 /* The second has a larger magnitude */ |
|
48 res = s_mp_sub (b, a, c); |
|
49 } |
|
50 } |
|
51 return res; |
|
52 } |
|
53 |