Mercurial > dropbear
comparison libtommath/bn_s_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_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 * SPDX-License-Identifier: Unlicense | |
13 */ | |
14 | 5 |
15 /* low level addition, based on HAC pp.594, Algorithm 14.7 */ | 6 /* low level addition, based on HAC pp.594, Algorithm 14.7 */ |
16 int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) | 7 mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) |
17 { | 8 { |
18 const mp_int *x; | 9 const mp_int *x; |
19 int olduse, res, min, max; | 10 mp_err err; |
11 int olduse, min, max; | |
20 | 12 |
21 /* 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 |
22 * them. "x" will point to the input with the most digits | 14 * them. "x" will point to the input with the most digits |
23 */ | 15 */ |
24 if (a->used > b->used) { | 16 if (a->used > b->used) { |
31 x = b; | 23 x = b; |
32 } | 24 } |
33 | 25 |
34 /* init result */ | 26 /* init result */ |
35 if (c->alloc < (max + 1)) { | 27 if (c->alloc < (max + 1)) { |
36 if ((res = mp_grow(c, max + 1)) != MP_OKAY) { | 28 if ((err = mp_grow(c, max + 1)) != MP_OKAY) { |
37 return res; | 29 return err; |
38 } | 30 } |
39 } | 31 } |
40 | 32 |
41 /* get old used digit count and set new one */ | 33 /* get old used digit count and set new one */ |
42 olduse = c->used; | 34 olduse = c->used; |
62 for (i = 0; i < min; i++) { | 54 for (i = 0; i < min; i++) { |
63 /* 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 */ |
64 *tmpc = *tmpa++ + *tmpb++ + u; | 56 *tmpc = *tmpa++ + *tmpb++ + u; |
65 | 57 |
66 /* U = carry bit of T[i] */ | 58 /* U = carry bit of T[i] */ |
67 u = *tmpc >> (mp_digit)DIGIT_BIT; | 59 u = *tmpc >> (mp_digit)MP_DIGIT_BIT; |
68 | 60 |
69 /* take away carry bit from T[i] */ | 61 /* take away carry bit from T[i] */ |
70 *tmpc++ &= MP_MASK; | 62 *tmpc++ &= MP_MASK; |
71 } | 63 } |
72 | 64 |
77 for (; i < max; i++) { | 69 for (; i < max; i++) { |
78 /* T[i] = X[i] + U */ | 70 /* T[i] = X[i] + U */ |
79 *tmpc = x->dp[i] + u; | 71 *tmpc = x->dp[i] + u; |
80 | 72 |
81 /* U = carry bit of T[i] */ | 73 /* U = carry bit of T[i] */ |
82 u = *tmpc >> (mp_digit)DIGIT_BIT; | 74 u = *tmpc >> (mp_digit)MP_DIGIT_BIT; |
83 | 75 |
84 /* take away carry bit from T[i] */ | 76 /* take away carry bit from T[i] */ |
85 *tmpc++ &= MP_MASK; | 77 *tmpc++ &= MP_MASK; |
86 } | 78 } |
87 } | 79 } |
88 | 80 |
89 /* add carry */ | 81 /* add carry */ |
90 *tmpc++ = u; | 82 *tmpc++ = u; |
91 | 83 |
92 /* clear digits above oldused */ | 84 /* clear digits above oldused */ |
93 for (i = c->used; i < olduse; i++) { | 85 MP_ZERO_DIGITS(tmpc, olduse - c->used); |
94 *tmpc++ = 0; | |
95 } | |
96 } | 86 } |
97 | 87 |
98 mp_clamp(c); | 88 mp_clamp(c); |
99 return MP_OKAY; | 89 return MP_OKAY; |
100 } | 90 } |
101 #endif | 91 #endif |
102 | |
103 /* ref: HEAD -> master, tag: v1.1.0 */ | |
104 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ | |
105 /* commit time: 2019-01-28 20:32:32 +0100 */ |