Mercurial > dropbear
comparison libtommath/bn_mp_add.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_ADD_C | 2 #ifdef BN_MP_ADD_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 addition (handles signs) */ | 6 /* high level addition (handles signs) */ |
16 int mp_add(const mp_int *a, const mp_int *b, mp_int *c) | 7 mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c) |
17 { | 8 { |
18 int sa, sb, res; | 9 mp_sign sa, sb; |
10 mp_err err; | |
19 | 11 |
20 /* get sign of both inputs */ | 12 /* get sign of both inputs */ |
21 sa = a->sign; | 13 sa = a->sign; |
22 sb = b->sign; | 14 sb = b->sign; |
23 | 15 |
24 /* handle two cases, not four */ | 16 /* handle two cases, not four */ |
25 if (sa == sb) { | 17 if (sa == sb) { |
26 /* both positive or both negative */ | 18 /* both positive or both negative */ |
27 /* add their magnitudes, copy the sign */ | 19 /* add their magnitudes, copy the sign */ |
28 c->sign = sa; | 20 c->sign = sa; |
29 res = s_mp_add(a, b, c); | 21 err = s_mp_add(a, b, c); |
30 } else { | 22 } else { |
31 /* one positive, the other negative */ | 23 /* one positive, the other negative */ |
32 /* subtract the one with the greater magnitude from */ | 24 /* subtract the one with the greater magnitude from */ |
33 /* the one of the lesser magnitude. The result gets */ | 25 /* the one of the lesser magnitude. The result gets */ |
34 /* the sign of the one with the greater magnitude. */ | 26 /* the sign of the one with the greater magnitude. */ |
35 if (mp_cmp_mag(a, b) == MP_LT) { | 27 if (mp_cmp_mag(a, b) == MP_LT) { |
36 c->sign = sb; | 28 c->sign = sb; |
37 res = s_mp_sub(b, a, c); | 29 err = s_mp_sub(b, a, c); |
38 } else { | 30 } else { |
39 c->sign = sa; | 31 c->sign = sa; |
40 res = s_mp_sub(a, b, c); | 32 err = s_mp_sub(a, b, c); |
41 } | 33 } |
42 } | 34 } |
43 return res; | 35 return err; |
44 } | 36 } |
45 | 37 |
46 #endif | 38 #endif |
47 | |
48 /* ref: HEAD -> master, tag: v1.1.0 */ | |
49 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ | |
50 /* commit time: 2019-01-28 20:32:32 +0100 */ |