Mercurial > dropbear
comparison libtommath/bn_mp_add_d.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_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 * |
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 /* single digit addition */ | 15 /* single digit addition */ |
19 int | 16 int 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 { | 17 { |
22 int res, ix, oldused; | 18 int res, ix, oldused; |
23 mp_digit *tmpa, *tmpc, mu; | 19 mp_digit *tmpa, *tmpc, mu; |
24 | 20 |
25 /* grow c as required */ | 21 /* grow c as required */ |
26 if (c->alloc < (a->used + 1)) { | 22 if (c->alloc < (a->used + 1)) { |
27 if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { | 23 if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { |
28 return res; | 24 return res; |
29 } | 25 } |
30 } | 26 } |
31 | 27 |
32 /* if a is negative and |a| >= b, call c = |a| - b */ | 28 /* if a is negative and |a| >= b, call c = |a| - b */ |
33 if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) { | 29 if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) { |
34 /* temporarily fix sign of a */ | 30 mp_int a_ = *a; |
35 a->sign = MP_ZPOS; | 31 /* temporarily fix sign of a */ |
32 a_.sign = MP_ZPOS; | |
36 | 33 |
37 /* c = |a| - b */ | 34 /* c = |a| - b */ |
38 res = mp_sub_d(a, b, c); | 35 res = mp_sub_d(&a_, b, c); |
39 | 36 |
40 /* fix sign */ | 37 /* fix sign */ |
41 a->sign = c->sign = MP_NEG; | 38 c->sign = MP_NEG; |
42 | 39 |
43 /* clamp */ | 40 /* clamp */ |
44 mp_clamp(c); | 41 mp_clamp(c); |
45 | 42 |
46 return res; | 43 return res; |
47 } | 44 } |
48 | 45 |
49 /* old number of used digits in c */ | 46 /* old number of used digits in c */ |
50 oldused = c->used; | 47 oldused = c->used; |
51 | 48 |
52 /* source alias */ | 49 /* source alias */ |
53 tmpa = a->dp; | 50 tmpa = a->dp; |
54 | 51 |
55 /* destination alias */ | 52 /* destination alias */ |
56 tmpc = c->dp; | 53 tmpc = c->dp; |
57 | 54 |
58 /* if a is positive */ | 55 /* if a is positive */ |
59 if (a->sign == MP_ZPOS) { | 56 if (a->sign == MP_ZPOS) { |
60 /* add digit, after this we're propagating | 57 /* add digit, after this we're propagating |
61 * the carry. | 58 * the carry. |
62 */ | 59 */ |
63 *tmpc = *tmpa++ + b; | 60 *tmpc = *tmpa++ + b; |
64 mu = *tmpc >> DIGIT_BIT; | 61 mu = *tmpc >> DIGIT_BIT; |
65 *tmpc++ &= MP_MASK; | 62 *tmpc++ &= MP_MASK; |
66 | 63 |
67 /* now handle rest of the digits */ | 64 /* now handle rest of the digits */ |
68 for (ix = 1; ix < a->used; ix++) { | 65 for (ix = 1; ix < a->used; ix++) { |
69 *tmpc = *tmpa++ + mu; | 66 *tmpc = *tmpa++ + mu; |
70 mu = *tmpc >> DIGIT_BIT; | 67 mu = *tmpc >> DIGIT_BIT; |
71 *tmpc++ &= MP_MASK; | 68 *tmpc++ &= MP_MASK; |
72 } | 69 } |
73 /* set final carry */ | 70 /* set final carry */ |
74 ix++; | 71 ix++; |
75 *tmpc++ = mu; | 72 *tmpc++ = mu; |
76 | 73 |
77 /* setup size */ | 74 /* setup size */ |
78 c->used = a->used + 1; | 75 c->used = a->used + 1; |
79 } else { | 76 } else { |
80 /* a was negative and |a| < b */ | 77 /* a was negative and |a| < b */ |
81 c->used = 1; | 78 c->used = 1; |
82 | 79 |
83 /* the result is a single digit */ | 80 /* the result is a single digit */ |
84 if (a->used == 1) { | 81 if (a->used == 1) { |
85 *tmpc++ = b - a->dp[0]; | 82 *tmpc++ = b - a->dp[0]; |
86 } else { | 83 } else { |
87 *tmpc++ = b; | 84 *tmpc++ = b; |
88 } | 85 } |
89 | 86 |
90 /* setup count so the clearing of oldused | 87 /* setup count so the clearing of oldused |
91 * can fall through correctly | 88 * can fall through correctly |
92 */ | 89 */ |
93 ix = 1; | 90 ix = 1; |
94 } | 91 } |
95 | 92 |
96 /* sign always positive */ | 93 /* sign always positive */ |
97 c->sign = MP_ZPOS; | 94 c->sign = MP_ZPOS; |
98 | 95 |
99 /* now zero to oldused */ | 96 /* now zero to oldused */ |
100 while (ix++ < oldused) { | 97 while (ix++ < oldused) { |
101 *tmpc++ = 0; | 98 *tmpc++ = 0; |
102 } | 99 } |
103 mp_clamp(c); | 100 mp_clamp(c); |
104 | 101 |
105 return MP_OKAY; | 102 return MP_OKAY; |
106 } | 103 } |
107 | 104 |
108 #endif | 105 #endif |
109 | 106 |
110 /* ref: $Format:%D$ */ | 107 /* ref: HEAD -> master, tag: v1.1.0 */ |
111 /* git commit: $Format:%H$ */ | 108 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ |
112 /* commit time: $Format:%ai$ */ | 109 /* commit time: 2019-01-28 20:32:32 +0100 */ |