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