comparison libtommath/bn_mp_mul_2d.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_MUL_2D_C 2 #ifdef BN_MP_MUL_2D_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 /* shift left by a certain bit count */ 15 /* shift left by a certain bit count */
19 int mp_mul_2d (mp_int * a, int b, mp_int * c) 16 int mp_mul_2d(const mp_int *a, int b, mp_int *c)
20 { 17 {
21 mp_digit d; 18 mp_digit d;
22 int res; 19 int res;
23 20
24 /* copy */ 21 /* copy */
25 if (a != c) { 22 if (a != c) {
26 if ((res = mp_copy (a, c)) != MP_OKAY) { 23 if ((res = mp_copy(a, c)) != MP_OKAY) {
27 return res; 24 return res;
28 } 25 }
29 } 26 }
30 27
31 if (c->alloc < (int)(c->used + (b / DIGIT_BIT) + 1)) { 28 if (c->alloc < (c->used + (b / DIGIT_BIT) + 1)) {
32 if ((res = mp_grow (c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) { 29 if ((res = mp_grow(c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) {
33 return res; 30 return res;
34 } 31 }
35 } 32 }
36 33
37 /* shift by as many digits in the bit count */ 34 /* shift by as many digits in the bit count */
38 if (b >= (int)DIGIT_BIT) { 35 if (b >= DIGIT_BIT) {
39 if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) { 36 if ((res = mp_lshd(c, b / DIGIT_BIT)) != MP_OKAY) {
40 return res; 37 return res;
41 } 38 }
42 } 39 }
43 40
44 /* shift any bit count < DIGIT_BIT */ 41 /* shift any bit count < DIGIT_BIT */
45 d = (mp_digit) (b % DIGIT_BIT); 42 d = (mp_digit)(b % DIGIT_BIT);
46 if (d != 0) { 43 if (d != 0u) {
47 mp_digit *tmpc, shift, mask, r, rr; 44 mp_digit *tmpc, shift, mask, r, rr;
48 int x; 45 int x;
49 46
50 /* bitmask for carries */ 47 /* bitmask for carries */
51 mask = (((mp_digit)1) << d) - 1; 48 mask = ((mp_digit)1 << d) - (mp_digit)1;
52 49
53 /* shift for msbs */ 50 /* shift for msbs */
54 shift = DIGIT_BIT - d; 51 shift = (mp_digit)DIGIT_BIT - d;
55 52
56 /* alias */ 53 /* alias */
57 tmpc = c->dp; 54 tmpc = c->dp;
58 55
59 /* carry */ 56 /* carry */
60 r = 0; 57 r = 0;
61 for (x = 0; x < c->used; x++) { 58 for (x = 0; x < c->used; x++) {
62 /* get the higher bits of the current word */ 59 /* get the higher bits of the current word */
63 rr = (*tmpc >> shift) & mask; 60 rr = (*tmpc >> shift) & mask;
64 61
65 /* shift the current word and OR in the carry */ 62 /* shift the current word and OR in the carry */
66 *tmpc = ((*tmpc << d) | r) & MP_MASK; 63 *tmpc = ((*tmpc << d) | r) & MP_MASK;
67 ++tmpc; 64 ++tmpc;
68 65
69 /* set the carry to the carry bits of the current word */ 66 /* set the carry to the carry bits of the current word */
70 r = rr; 67 r = rr;
71 } 68 }
72 69
73 /* set final carry */ 70 /* set final carry */
74 if (r != 0) { 71 if (r != 0u) {
75 c->dp[(c->used)++] = r; 72 c->dp[(c->used)++] = r;
76 } 73 }
77 } 74 }
78 mp_clamp (c); 75 mp_clamp(c);
79 return MP_OKAY; 76 return MP_OKAY;
80 } 77 }
81 #endif 78 #endif
82 79
83 /* ref: $Format:%D$ */ 80 /* ref: HEAD -> master, tag: v1.1.0 */
84 /* git commit: $Format:%H$ */ 81 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
85 /* commit time: $Format:%ai$ */ 82 /* commit time: 2019-01-28 20:32:32 +0100 */