comparison libtommath/bn_s_mp_add.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children 1051e4eea25a
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
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 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
7 * 7 *
8 * The library was designed directly after the MPI library by 8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with 9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * SPDX-License-Identifier: Unlicense
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */ 13 */
17 14
18 /* low level addition, based on HAC pp.594, Algorithm 14.7 */ 15 /* low level addition, based on HAC pp.594, Algorithm 14.7 */
19 int 16 int 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 { 17 {
22 mp_int *x; 18 const mp_int *x;
23 int olduse, res, min, max; 19 int olduse, res, min, max;
24 20
25 /* find sizes, we let |a| <= |b| which means we have to sort 21 /* find sizes, we let |a| <= |b| which means we have to sort
26 * them. "x" will point to the input with the most digits 22 * them. "x" will point to the input with the most digits
27 */ 23 */
28 if (a->used > b->used) { 24 if (a->used > b->used) {
29 min = b->used; 25 min = b->used;
30 max = a->used; 26 max = a->used;
31 x = a; 27 x = a;
32 } else { 28 } else {
33 min = a->used; 29 min = a->used;
34 max = b->used; 30 max = b->used;
35 x = b; 31 x = b;
36 } 32 }
37 33
38 /* init result */ 34 /* init result */
39 if (c->alloc < (max + 1)) { 35 if (c->alloc < (max + 1)) {
40 if ((res = mp_grow (c, max + 1)) != MP_OKAY) { 36 if ((res = mp_grow(c, max + 1)) != MP_OKAY) {
41 return res; 37 return res;
42 } 38 }
43 } 39 }
44 40
45 /* get old used digit count and set new one */ 41 /* get old used digit count and set new one */
46 olduse = c->used; 42 olduse = c->used;
47 c->used = max + 1; 43 c->used = max + 1;
48 44
49 { 45 {
50 mp_digit u, *tmpa, *tmpb, *tmpc; 46 mp_digit u, *tmpa, *tmpb, *tmpc;
51 int i; 47 int i;
52 48
53 /* alias for digit pointers */ 49 /* alias for digit pointers */
54 50
55 /* first input */ 51 /* first input */
56 tmpa = a->dp; 52 tmpa = a->dp;
57 53
58 /* second input */ 54 /* second input */
59 tmpb = b->dp; 55 tmpb = b->dp;
60 56
61 /* destination */ 57 /* destination */
62 tmpc = c->dp; 58 tmpc = c->dp;
63 59
64 /* zero the carry */ 60 /* zero the carry */
65 u = 0; 61 u = 0;
66 for (i = 0; i < min; i++) { 62 for (i = 0; i < min; i++) {
67 /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */ 63 /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */
68 *tmpc = *tmpa++ + *tmpb++ + u; 64 *tmpc = *tmpa++ + *tmpb++ + u;
69 65
70 /* U = carry bit of T[i] */ 66 /* U = carry bit of T[i] */
71 u = *tmpc >> ((mp_digit)DIGIT_BIT); 67 u = *tmpc >> (mp_digit)DIGIT_BIT;
72 68
73 /* take away carry bit from T[i] */ 69 /* take away carry bit from T[i] */
74 *tmpc++ &= MP_MASK; 70 *tmpc++ &= MP_MASK;
75 } 71 }
76 72
77 /* now copy higher words if any, that is in A+B 73 /* now copy higher words if any, that is in A+B
78 * if A or B has more digits add those in 74 * if A or B has more digits add those in
79 */ 75 */
80 if (min != max) { 76 if (min != max) {
81 for (; i < max; i++) { 77 for (; i < max; i++) {
82 /* T[i] = X[i] + U */ 78 /* T[i] = X[i] + U */
83 *tmpc = x->dp[i] + u; 79 *tmpc = x->dp[i] + u;
84 80
85 /* U = carry bit of T[i] */ 81 /* U = carry bit of T[i] */
86 u = *tmpc >> ((mp_digit)DIGIT_BIT); 82 u = *tmpc >> (mp_digit)DIGIT_BIT;
87 83
88 /* take away carry bit from T[i] */ 84 /* take away carry bit from T[i] */
89 *tmpc++ &= MP_MASK; 85 *tmpc++ &= MP_MASK;
86 }
90 } 87 }
91 }
92 88
93 /* add carry */ 89 /* add carry */
94 *tmpc++ = u; 90 *tmpc++ = u;
95 91
96 /* clear digits above oldused */ 92 /* clear digits above oldused */
97 for (i = c->used; i < olduse; i++) { 93 for (i = c->used; i < olduse; i++) {
98 *tmpc++ = 0; 94 *tmpc++ = 0;
99 } 95 }
100 } 96 }
101 97
102 mp_clamp (c); 98 mp_clamp(c);
103 return MP_OKAY; 99 return MP_OKAY;
104 } 100 }
105 #endif 101 #endif
106 102
107 /* ref: $Format:%D$ */ 103 /* ref: HEAD -> master, tag: v1.1.0 */
108 /* git commit: $Format:%H$ */ 104 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
109 /* commit time: $Format:%ai$ */ 105 /* commit time: 2019-01-28 20:32:32 +0100 */