comparison libtommath/bn_mp_add.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
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 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 5
18 /* high level addition (handles signs) */ 6 /* high level addition (handles signs) */
19 int mp_add (mp_int * a, mp_int * b, mp_int * c) 7 mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
20 { 8 {
21 int sa, sb, res; 9 mp_sign sa, sb;
10 mp_err err;
22 11
23 /* get sign of both inputs */ 12 /* get sign of both inputs */
24 sa = a->sign; 13 sa = a->sign;
25 sb = b->sign; 14 sb = b->sign;
26 15
27 /* handle two cases, not four */ 16 /* handle two cases, not four */
28 if (sa == sb) { 17 if (sa == sb) {
29 /* both positive or both negative */ 18 /* both positive or both negative */
30 /* add their magnitudes, copy the sign */ 19 /* add their magnitudes, copy the sign */
31 c->sign = sa;
32 res = s_mp_add (a, b, c);
33 } else {
34 /* one positive, the other negative */
35 /* subtract the one with the greater magnitude from */
36 /* the one of the lesser magnitude. The result gets */
37 /* the sign of the one with the greater magnitude. */
38 if (mp_cmp_mag (a, b) == MP_LT) {
39 c->sign = sb;
40 res = s_mp_sub (b, a, c);
41 } else {
42 c->sign = sa; 20 c->sign = sa;
43 res = s_mp_sub (a, b, c); 21 err = s_mp_add(a, b, c);
44 } 22 } else {
45 } 23 /* one positive, the other negative */
46 return res; 24 /* subtract the one with the greater magnitude from */
25 /* the one of the lesser magnitude. The result gets */
26 /* the sign of the one with the greater magnitude. */
27 if (mp_cmp_mag(a, b) == MP_LT) {
28 c->sign = sb;
29 err = s_mp_sub(b, a, c);
30 } else {
31 c->sign = sa;
32 err = s_mp_sub(a, b, c);
33 }
34 }
35 return err;
47 } 36 }
48 37
49 #endif 38 #endif
50
51 /* ref: $Format:%D$ */
52 /* git commit: $Format:%H$ */
53 /* commit time: $Format:%ai$ */