comparison libtommath/bn_mp_mul_2.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_2_C 2 #ifdef BN_MP_MUL_2_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 /* b = a*2 */ 15 /* b = a*2 */
19 int mp_mul_2(mp_int * a, mp_int * b) 16 int mp_mul_2(const mp_int *a, mp_int *b)
20 { 17 {
21 int x, res, oldused; 18 int x, res, oldused;
22 19
23 /* grow to accomodate result */ 20 /* grow to accomodate result */
24 if (b->alloc < (a->used + 1)) { 21 if (b->alloc < (a->used + 1)) {
25 if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) { 22 if ((res = mp_grow(b, a->used + 1)) != MP_OKAY) {
26 return res; 23 return res;
27 } 24 }
28 } 25 }
29 26
30 oldused = b->used; 27 oldused = b->used;
31 b->used = a->used; 28 b->used = a->used;
32 29
33 { 30 {
34 mp_digit r, rr, *tmpa, *tmpb; 31 mp_digit r, rr, *tmpa, *tmpb;
35 32
36 /* alias for source */ 33 /* alias for source */
37 tmpa = a->dp; 34 tmpa = a->dp;
38
39 /* alias for dest */
40 tmpb = b->dp;
41 35
42 /* carry */ 36 /* alias for dest */
43 r = 0; 37 tmpb = b->dp;
44 for (x = 0; x < a->used; x++) { 38
45 39 /* carry */
46 /* get what will be the *next* carry bit from the 40 r = 0;
47 * MSB of the current digit 41 for (x = 0; x < a->used; x++) {
42
43 /* get what will be the *next* carry bit from the
44 * MSB of the current digit
45 */
46 rr = *tmpa >> (mp_digit)(DIGIT_BIT - 1);
47
48 /* now shift up this digit, add in the carry [from the previous] */
49 *tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK;
50
51 /* copy the carry that would be from the source
52 * digit into the next iteration
53 */
54 r = rr;
55 }
56
57 /* new leading digit? */
58 if (r != 0u) {
59 /* add a MSB which is always 1 at this point */
60 *tmpb = 1;
61 ++(b->used);
62 }
63
64 /* now zero any excess digits on the destination
65 * that we didn't write to
48 */ 66 */
49 rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); 67 tmpb = b->dp + b->used;
50 68 for (x = b->used; x < oldused; x++) {
51 /* now shift up this digit, add in the carry [from the previous] */ 69 *tmpb++ = 0;
52 *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK; 70 }
53 71 }
54 /* copy the carry that would be from the source 72 b->sign = a->sign;
55 * digit into the next iteration 73 return MP_OKAY;
56 */
57 r = rr;
58 }
59
60 /* new leading digit? */
61 if (r != 0) {
62 /* add a MSB which is always 1 at this point */
63 *tmpb = 1;
64 ++(b->used);
65 }
66
67 /* now zero any excess digits on the destination
68 * that we didn't write to
69 */
70 tmpb = b->dp + b->used;
71 for (x = b->used; x < oldused; x++) {
72 *tmpb++ = 0;
73 }
74 }
75 b->sign = a->sign;
76 return MP_OKAY;
77 } 74 }
78 #endif 75 #endif
79 76
80 /* ref: $Format:%D$ */ 77 /* ref: HEAD -> master, tag: v1.1.0 */
81 /* git commit: $Format:%H$ */ 78 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
82 /* commit time: $Format:%ai$ */ 79 /* commit time: 2019-01-28 20:32:32 +0100 */