comparison libtommath/bn_mp_add_d.c @ 1733:d529a52b2f7c coverity coverity

merge coverity from main
author Matt Johnston <matt@ucc.asn.au>
date Fri, 26 Jun 2020 21:07:34 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1643:b59623a64678 1733:d529a52b2f7c
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_ADD_D_C 2 #ifdef BN_MP_ADD_D_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 /* single digit addition */ 6 /* single digit addition */
19 int 7 mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
20 mp_add_d (mp_int * a, mp_digit b, mp_int * c)
21 { 8 {
22 int res, ix, oldused; 9 mp_err err;
23 mp_digit *tmpa, *tmpc, mu; 10 int ix, oldused;
11 mp_digit *tmpa, *tmpc;
24 12
25 /* grow c as required */ 13 /* grow c as required */
26 if (c->alloc < (a->used + 1)) { 14 if (c->alloc < (a->used + 1)) {
27 if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { 15 if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
28 return res; 16 return err;
29 } 17 }
30 } 18 }
31 19
32 /* if a is negative and |a| >= b, call c = |a| - b */ 20 /* if a is negative and |a| >= b, call c = |a| - b */
33 if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) { 21 if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) {
34 /* temporarily fix sign of a */ 22 mp_int a_ = *a;
35 a->sign = MP_ZPOS; 23 /* temporarily fix sign of a */
24 a_.sign = MP_ZPOS;
36 25
37 /* c = |a| - b */ 26 /* c = |a| - b */
38 res = mp_sub_d(a, b, c); 27 err = mp_sub_d(&a_, b, c);
39 28
40 /* fix sign */ 29 /* fix sign */
41 a->sign = c->sign = MP_NEG; 30 c->sign = MP_NEG;
42 31
43 /* clamp */ 32 /* clamp */
44 mp_clamp(c); 33 mp_clamp(c);
45 34
46 return res; 35 return err;
47 } 36 }
48 37
49 /* old number of used digits in c */ 38 /* old number of used digits in c */
50 oldused = c->used; 39 oldused = c->used;
51 40
52 /* source alias */ 41 /* source alias */
53 tmpa = a->dp; 42 tmpa = a->dp;
54 43
55 /* destination alias */ 44 /* destination alias */
56 tmpc = c->dp; 45 tmpc = c->dp;
57 46
58 /* if a is positive */ 47 /* if a is positive */
59 if (a->sign == MP_ZPOS) { 48 if (a->sign == MP_ZPOS) {
60 /* add digit, after this we're propagating 49 /* add digits, mu is carry */
61 * the carry. 50 mp_digit mu = b;
62 */ 51 for (ix = 0; ix < a->used; ix++) {
63 *tmpc = *tmpa++ + b; 52 *tmpc = *tmpa++ + mu;
64 mu = *tmpc >> DIGIT_BIT; 53 mu = *tmpc >> MP_DIGIT_BIT;
65 *tmpc++ &= MP_MASK; 54 *tmpc++ &= MP_MASK;
55 }
56 /* set final carry */
57 ix++;
58 *tmpc++ = mu;
66 59
67 /* now handle rest of the digits */ 60 /* setup size */
68 for (ix = 1; ix < a->used; ix++) { 61 c->used = a->used + 1;
69 *tmpc = *tmpa++ + mu; 62 } else {
70 mu = *tmpc >> DIGIT_BIT; 63 /* a was negative and |a| < b */
71 *tmpc++ &= MP_MASK; 64 c->used = 1;
72 }
73 /* set final carry */
74 ix++;
75 *tmpc++ = mu;
76 65
77 /* setup size */ 66 /* the result is a single digit */
78 c->used = a->used + 1; 67 if (a->used == 1) {
79 } else { 68 *tmpc++ = b - a->dp[0];
80 /* a was negative and |a| < b */ 69 } else {
81 c->used = 1; 70 *tmpc++ = b;
71 }
82 72
83 /* the result is a single digit */ 73 /* setup count so the clearing of oldused
84 if (a->used == 1) { 74 * can fall through correctly
85 *tmpc++ = b - a->dp[0]; 75 */
86 } else { 76 ix = 1;
87 *tmpc++ = b; 77 }
88 }
89 78
90 /* setup count so the clearing of oldused 79 /* sign always positive */
91 * can fall through correctly 80 c->sign = MP_ZPOS;
92 */
93 ix = 1;
94 }
95 81
96 /* sign always positive */ 82 /* now zero to oldused */
97 c->sign = MP_ZPOS; 83 MP_ZERO_DIGITS(tmpc, oldused - ix);
84 mp_clamp(c);
98 85
99 /* now zero to oldused */ 86 return MP_OKAY;
100 while (ix++ < oldused) {
101 *tmpc++ = 0;
102 }
103 mp_clamp(c);
104
105 return MP_OKAY;
106 } 87 }
107 88
108 #endif 89 #endif
109
110 /* ref: $Format:%D$ */
111 /* git commit: $Format:%H$ */
112 /* commit time: $Format:%ai$ */