comparison libtommath/bn_s_mp_sub.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_SUB_C 2 #ifdef BN_S_MP_SUB_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 subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */ 15 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
19 int 16 int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
20 s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
21 { 17 {
22 int olduse, res, min, max; 18 int olduse, res, min, max;
23 19
24 /* find sizes */ 20 /* find sizes */
25 min = b->used; 21 min = b->used;
26 max = a->used; 22 max = a->used;
27 23
28 /* init result */ 24 /* init result */
29 if (c->alloc < max) { 25 if (c->alloc < max) {
30 if ((res = mp_grow (c, max)) != MP_OKAY) { 26 if ((res = mp_grow(c, max)) != MP_OKAY) {
31 return res; 27 return res;
32 } 28 }
33 } 29 }
34 olduse = c->used; 30 olduse = c->used;
35 c->used = max; 31 c->used = max;
36 32
37 { 33 {
38 mp_digit u, *tmpa, *tmpb, *tmpc; 34 mp_digit u, *tmpa, *tmpb, *tmpc;
39 int i; 35 int i;
40 36
41 /* alias for digit pointers */ 37 /* alias for digit pointers */
42 tmpa = a->dp; 38 tmpa = a->dp;
43 tmpb = b->dp; 39 tmpb = b->dp;
44 tmpc = c->dp; 40 tmpc = c->dp;
45 41
46 /* set carry to zero */ 42 /* set carry to zero */
47 u = 0; 43 u = 0;
48 for (i = 0; i < min; i++) { 44 for (i = 0; i < min; i++) {
49 /* T[i] = A[i] - B[i] - U */ 45 /* T[i] = A[i] - B[i] - U */
50 *tmpc = (*tmpa++ - *tmpb++) - u; 46 *tmpc = (*tmpa++ - *tmpb++) - u;
51 47
52 /* U = carry bit of T[i] 48 /* U = carry bit of T[i]
53 * Note this saves performing an AND operation since 49 * Note this saves performing an AND operation since
54 * if a carry does occur it will propagate all the way to the 50 * if a carry does occur it will propagate all the way to the
55 * MSB. As a result a single shift is enough to get the carry 51 * MSB. As a result a single shift is enough to get the carry
56 */ 52 */
57 u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1)); 53 u = *tmpc >> (((size_t)CHAR_BIT * sizeof(mp_digit)) - 1u);
58 54
59 /* Clear carry from T[i] */ 55 /* Clear carry from T[i] */
60 *tmpc++ &= MP_MASK; 56 *tmpc++ &= MP_MASK;
61 } 57 }
62 58
63 /* now copy higher words if any, e.g. if A has more digits than B */ 59 /* now copy higher words if any, e.g. if A has more digits than B */
64 for (; i < max; i++) { 60 for (; i < max; i++) {
65 /* T[i] = A[i] - U */ 61 /* T[i] = A[i] - U */
66 *tmpc = *tmpa++ - u; 62 *tmpc = *tmpa++ - u;
67 63
68 /* U = carry bit of T[i] */ 64 /* U = carry bit of T[i] */
69 u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1)); 65 u = *tmpc >> (((size_t)CHAR_BIT * sizeof(mp_digit)) - 1u);
70 66
71 /* Clear carry from T[i] */ 67 /* Clear carry from T[i] */
72 *tmpc++ &= MP_MASK; 68 *tmpc++ &= MP_MASK;
73 } 69 }
74 70
75 /* clear digits above used (since we may not have grown result above) */ 71 /* clear digits above used (since we may not have grown result above) */
76 for (i = c->used; i < olduse; i++) { 72 for (i = c->used; i < olduse; i++) {
77 *tmpc++ = 0; 73 *tmpc++ = 0;
78 } 74 }
79 } 75 }
80 76
81 mp_clamp (c); 77 mp_clamp(c);
82 return MP_OKAY; 78 return MP_OKAY;
83 } 79 }
84 80
85 #endif 81 #endif
86 82
87 /* ref: $Format:%D$ */ 83 /* ref: HEAD -> master, tag: v1.1.0 */
88 /* git commit: $Format:%H$ */ 84 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
89 /* commit time: $Format:%ai$ */ 85 /* commit time: 2019-01-28 20:32:32 +0100 */