comparison libtommath/bn_mp_div_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_DIV_2_C 2 #ifdef BN_MP_DIV_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_div_2(mp_int * a, mp_int * b) 16 int mp_div_2(const mp_int *a, mp_int *b)
20 { 17 {
21 int x, res, oldused; 18 int x, res, oldused;
22 19
23 /* copy */ 20 /* copy */
24 if (b->alloc < a->used) { 21 if (b->alloc < a->used) {
25 if ((res = mp_grow (b, a->used)) != MP_OKAY) { 22 if ((res = mp_grow(b, a->used)) != 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 mp_digit r, rr, *tmpa, *tmpb; 30 mp_digit r, rr, *tmpa, *tmpb;
34 31
35 /* source alias */ 32 /* source alias */
36 tmpa = a->dp + b->used - 1; 33 tmpa = a->dp + b->used - 1;
37 34
38 /* dest alias */ 35 /* dest alias */
39 tmpb = b->dp + b->used - 1; 36 tmpb = b->dp + b->used - 1;
40 37
41 /* carry */ 38 /* carry */
42 r = 0; 39 r = 0;
43 for (x = b->used - 1; x >= 0; x--) { 40 for (x = b->used - 1; x >= 0; x--) {
44 /* get the carry for the next iteration */ 41 /* get the carry for the next iteration */
45 rr = *tmpa & 1; 42 rr = *tmpa & 1u;
46 43
47 /* shift the current digit, add in carry and store */ 44 /* shift the current digit, add in carry and store */
48 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1)); 45 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
49 46
50 /* forward carry to next iteration */ 47 /* forward carry to next iteration */
51 r = rr; 48 r = rr;
52 } 49 }
53 50
54 /* zero excess digits */ 51 /* zero excess digits */
55 tmpb = b->dp + b->used; 52 tmpb = b->dp + b->used;
56 for (x = b->used; x < oldused; x++) { 53 for (x = b->used; x < oldused; x++) {
57 *tmpb++ = 0; 54 *tmpb++ = 0;
58 } 55 }
59 } 56 }
60 b->sign = a->sign; 57 b->sign = a->sign;
61 mp_clamp (b); 58 mp_clamp(b);
62 return MP_OKAY; 59 return MP_OKAY;
63 } 60 }
64 #endif 61 #endif
65 62
66 /* ref: $Format:%D$ */ 63 /* ref: HEAD -> master, tag: v1.1.0 */
67 /* git commit: $Format:%H$ */ 64 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
68 /* commit time: $Format:%ai$ */ 65 /* commit time: 2019-01-28 20:32:32 +0100 */