comparison libtommath/bn_mp_sub.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents f52919ffd3b1
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
1 #include "tommath_private.h" 1 #include "tommath_private.h"
2 #ifdef BN_MP_SUB_C 2 #ifdef BN_MP_SUB_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 * SPDX-License-Identifier: Unlicense
13 */
14 5
15 /* high level subtraction (handles signs) */ 6 /* high level subtraction (handles signs) */
16 int mp_sub(const mp_int *a, const mp_int *b, mp_int *c) 7 mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
17 { 8 {
18 int sa, sb, res; 9 mp_sign sa = a->sign, sb = b->sign;
19 10 mp_err err;
20 sa = a->sign;
21 sb = b->sign;
22 11
23 if (sa != sb) { 12 if (sa != sb) {
24 /* subtract a negative from a positive, OR */ 13 /* subtract a negative from a positive, OR */
25 /* subtract a positive from a negative. */ 14 /* subtract a positive from a negative. */
26 /* In either case, ADD their magnitudes, */ 15 /* In either case, ADD their magnitudes, */
27 /* and use the sign of the first number. */ 16 /* and use the sign of the first number. */
28 c->sign = sa; 17 c->sign = sa;
29 res = s_mp_add(a, b, c); 18 err = s_mp_add(a, b, c);
30 } else { 19 } else {
31 /* subtract a positive from a positive, OR */ 20 /* subtract a positive from a positive, OR */
32 /* subtract a negative from a negative. */ 21 /* subtract a negative from a negative. */
33 /* First, take the difference between their */ 22 /* First, take the difference between their */
34 /* magnitudes, then... */ 23 /* magnitudes, then... */
35 if (mp_cmp_mag(a, b) != MP_LT) { 24 if (mp_cmp_mag(a, b) != MP_LT) {
36 /* Copy the sign from the first */ 25 /* Copy the sign from the first */
37 c->sign = sa; 26 c->sign = sa;
38 /* The first has a larger or equal magnitude */ 27 /* The first has a larger or equal magnitude */
39 res = s_mp_sub(a, b, c); 28 err = s_mp_sub(a, b, c);
40 } else { 29 } else {
41 /* The result has the *opposite* sign from */ 30 /* The result has the *opposite* sign from */
42 /* the first number. */ 31 /* the first number. */
43 c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS; 32 c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS;
44 /* The second has a larger magnitude */ 33 /* The second has a larger magnitude */
45 res = s_mp_sub(b, a, c); 34 err = s_mp_sub(b, a, c);
46 } 35 }
47 } 36 }
48 return res; 37 return err;
49 } 38 }
50 39
51 #endif 40 #endif
52
53 /* ref: HEAD -> master, tag: v1.1.0 */
54 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
55 /* commit time: 2019-01-28 20:32:32 +0100 */