Mercurial > dropbear
comparison libtommath/bn_s_mp_add.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_S_MP_ADD_C | 2 #ifdef BN_S_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 /* low level addition, based on HAC pp.594, Algorithm 14.7 */ | 6 /* low level addition, based on HAC pp.594, Algorithm 14.7 */ |
19 int | 7 mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) |
20 s_mp_add (mp_int * a, mp_int * b, mp_int * c) | |
21 { | 8 { |
22 mp_int *x; | 9 const mp_int *x; |
23 int olduse, res, min, max; | 10 mp_err err; |
11 int olduse, min, max; | |
24 | 12 |
25 /* find sizes, we let |a| <= |b| which means we have to sort | 13 /* find sizes, we let |a| <= |b| which means we have to sort |
26 * them. "x" will point to the input with the most digits | 14 * them. "x" will point to the input with the most digits |
27 */ | 15 */ |
28 if (a->used > b->used) { | 16 if (a->used > b->used) { |
29 min = b->used; | 17 min = b->used; |
30 max = a->used; | 18 max = a->used; |
31 x = a; | 19 x = a; |
32 } else { | 20 } else { |
33 min = a->used; | 21 min = a->used; |
34 max = b->used; | 22 max = b->used; |
35 x = b; | 23 x = b; |
36 } | 24 } |
37 | 25 |
38 /* init result */ | 26 /* init result */ |
39 if (c->alloc < (max + 1)) { | 27 if (c->alloc < (max + 1)) { |
40 if ((res = mp_grow (c, max + 1)) != MP_OKAY) { | 28 if ((err = mp_grow(c, max + 1)) != MP_OKAY) { |
41 return res; | 29 return err; |
42 } | 30 } |
43 } | 31 } |
44 | 32 |
45 /* get old used digit count and set new one */ | 33 /* get old used digit count and set new one */ |
46 olduse = c->used; | 34 olduse = c->used; |
47 c->used = max + 1; | 35 c->used = max + 1; |
48 | 36 |
49 { | 37 { |
50 mp_digit u, *tmpa, *tmpb, *tmpc; | 38 mp_digit u, *tmpa, *tmpb, *tmpc; |
51 int i; | 39 int i; |
52 | 40 |
53 /* alias for digit pointers */ | 41 /* alias for digit pointers */ |
54 | 42 |
55 /* first input */ | 43 /* first input */ |
56 tmpa = a->dp; | 44 tmpa = a->dp; |
57 | 45 |
58 /* second input */ | 46 /* second input */ |
59 tmpb = b->dp; | 47 tmpb = b->dp; |
60 | 48 |
61 /* destination */ | 49 /* destination */ |
62 tmpc = c->dp; | 50 tmpc = c->dp; |
63 | 51 |
64 /* zero the carry */ | 52 /* zero the carry */ |
65 u = 0; | 53 u = 0; |
66 for (i = 0; i < min; i++) { | 54 for (i = 0; i < min; i++) { |
67 /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */ | 55 /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */ |
68 *tmpc = *tmpa++ + *tmpb++ + u; | 56 *tmpc = *tmpa++ + *tmpb++ + u; |
69 | 57 |
70 /* U = carry bit of T[i] */ | 58 /* U = carry bit of T[i] */ |
71 u = *tmpc >> ((mp_digit)DIGIT_BIT); | 59 u = *tmpc >> (mp_digit)MP_DIGIT_BIT; |
72 | 60 |
73 /* take away carry bit from T[i] */ | 61 /* take away carry bit from T[i] */ |
74 *tmpc++ &= MP_MASK; | 62 *tmpc++ &= MP_MASK; |
75 } | 63 } |
76 | 64 |
77 /* now copy higher words if any, that is in A+B | 65 /* now copy higher words if any, that is in A+B |
78 * if A or B has more digits add those in | 66 * if A or B has more digits add those in |
79 */ | 67 */ |
80 if (min != max) { | 68 if (min != max) { |
81 for (; i < max; i++) { | 69 for (; i < max; i++) { |
82 /* T[i] = X[i] + U */ | 70 /* T[i] = X[i] + U */ |
83 *tmpc = x->dp[i] + u; | 71 *tmpc = x->dp[i] + u; |
84 | 72 |
85 /* U = carry bit of T[i] */ | 73 /* U = carry bit of T[i] */ |
86 u = *tmpc >> ((mp_digit)DIGIT_BIT); | 74 u = *tmpc >> (mp_digit)MP_DIGIT_BIT; |
87 | 75 |
88 /* take away carry bit from T[i] */ | 76 /* take away carry bit from T[i] */ |
89 *tmpc++ &= MP_MASK; | 77 *tmpc++ &= MP_MASK; |
78 } | |
90 } | 79 } |
91 } | |
92 | 80 |
93 /* add carry */ | 81 /* add carry */ |
94 *tmpc++ = u; | 82 *tmpc++ = u; |
95 | 83 |
96 /* clear digits above oldused */ | 84 /* clear digits above oldused */ |
97 for (i = c->used; i < olduse; i++) { | 85 MP_ZERO_DIGITS(tmpc, olduse - c->used); |
98 *tmpc++ = 0; | 86 } |
99 } | |
100 } | |
101 | 87 |
102 mp_clamp (c); | 88 mp_clamp(c); |
103 return MP_OKAY; | 89 return MP_OKAY; |
104 } | 90 } |
105 #endif | 91 #endif |
106 | |
107 /* ref: $Format:%D$ */ | |
108 /* git commit: $Format:%H$ */ | |
109 /* commit time: $Format:%ai$ */ |