comparison libtommath/bn_mp_sub_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_SUB_D_C 2 #ifdef BN_MP_SUB_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 subtraction */ 15 /* single digit subtraction */
19 int 16 int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
20 mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
21 { 17 {
22 mp_digit *tmpa, *tmpc, mu; 18 mp_digit *tmpa, *tmpc, mu;
23 int res, ix, oldused; 19 int res, ix, oldused;
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 just do an unsigned 28 /* if a is negative just do an unsigned
33 * addition [with fudged signs] 29 * addition [with fudged signs]
34 */ 30 */
35 if (a->sign == MP_NEG) { 31 if (a->sign == MP_NEG) {
36 a->sign = MP_ZPOS; 32 mp_int a_ = *a;
37 res = mp_add_d(a, b, c); 33 a_.sign = MP_ZPOS;
38 a->sign = c->sign = MP_NEG; 34 res = mp_add_d(&a_, b, c);
35 c->sign = MP_NEG;
39 36
40 /* clamp */ 37 /* clamp */
41 mp_clamp(c); 38 mp_clamp(c);
42 39
43 return res; 40 return res;
44 } 41 }
45 42
46 /* setup regs */ 43 /* setup regs */
47 oldused = c->used; 44 oldused = c->used;
48 tmpa = a->dp; 45 tmpa = a->dp;
49 tmpc = c->dp; 46 tmpc = c->dp;
50 47
51 /* if a <= b simply fix the single digit */ 48 /* if a <= b simply fix the single digit */
52 if (((a->used == 1) && (a->dp[0] <= b)) || (a->used == 0)) { 49 if (((a->used == 1) && (a->dp[0] <= b)) || (a->used == 0)) {
53 if (a->used == 1) { 50 if (a->used == 1) {
54 *tmpc++ = b - *tmpa; 51 *tmpc++ = b - *tmpa;
55 } else { 52 } else {
56 *tmpc++ = b; 53 *tmpc++ = b;
57 } 54 }
58 ix = 1; 55 ix = 1;
59 56
60 /* negative/1digit */ 57 /* negative/1digit */
61 c->sign = MP_NEG; 58 c->sign = MP_NEG;
62 c->used = 1; 59 c->used = 1;
63 } else { 60 } else {
64 /* positive/size */ 61 /* positive/size */
65 c->sign = MP_ZPOS; 62 c->sign = MP_ZPOS;
66 c->used = a->used; 63 c->used = a->used;
67 64
68 /* subtract first digit */ 65 /* subtract first digit */
69 *tmpc = *tmpa++ - b; 66 *tmpc = *tmpa++ - b;
70 mu = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1); 67 mu = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u);
71 *tmpc++ &= MP_MASK; 68 *tmpc++ &= MP_MASK;
72 69
73 /* handle rest of the digits */ 70 /* handle rest of the digits */
74 for (ix = 1; ix < a->used; ix++) { 71 for (ix = 1; ix < a->used; ix++) {
75 *tmpc = *tmpa++ - mu; 72 *tmpc = *tmpa++ - mu;
76 mu = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1); 73 mu = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u);
77 *tmpc++ &= MP_MASK; 74 *tmpc++ &= MP_MASK;
78 } 75 }
79 } 76 }
80 77
81 /* zero excess digits */ 78 /* zero excess digits */
82 while (ix++ < oldused) { 79 while (ix++ < oldused) {
83 *tmpc++ = 0; 80 *tmpc++ = 0;
84 } 81 }
85 mp_clamp(c); 82 mp_clamp(c);
86 return MP_OKAY; 83 return MP_OKAY;
87 } 84 }
88 85
89 #endif 86 #endif
90 87
91 /* ref: $Format:%D$ */ 88 /* ref: HEAD -> master, tag: v1.1.0 */
92 /* git commit: $Format:%H$ */ 89 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
93 /* commit time: $Format:%ai$ */ 90 /* commit time: 2019-01-28 20:32:32 +0100 */