comparison libtommath/bn_mp_lshd.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents f52919ffd3b1
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
1 #include "tommath_private.h" 1 #include "tommath_private.h"
2 #ifdef BN_MP_LSHD_C 2 #ifdef BN_MP_LSHD_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 * 4 /* SPDX-License-Identifier: Unlicense */
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
7 *
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
11 *
12 * SPDX-License-Identifier: Unlicense
13 */
14 5
15 /* shift left a certain amount of digits */ 6 /* shift left a certain amount of digits */
16 int mp_lshd(mp_int *a, int b) 7 mp_err mp_lshd(mp_int *a, int b)
17 { 8 {
18 int x, res; 9 int x;
10 mp_err err;
11 mp_digit *top, *bottom;
19 12
20 /* if its less than zero return */ 13 /* if its less than zero return */
21 if (b <= 0) { 14 if (b <= 0) {
22 return MP_OKAY; 15 return MP_OKAY;
23 } 16 }
24 /* no need to shift 0 around */ 17 /* no need to shift 0 around */
25 if (mp_iszero(a) == MP_YES) { 18 if (MP_IS_ZERO(a)) {
26 return MP_OKAY; 19 return MP_OKAY;
27 } 20 }
28 21
29 /* grow to fit the new digits */ 22 /* grow to fit the new digits */
30 if (a->alloc < (a->used + b)) { 23 if (a->alloc < (a->used + b)) {
31 if ((res = mp_grow(a, a->used + b)) != MP_OKAY) { 24 if ((err = mp_grow(a, a->used + b)) != MP_OKAY) {
32 return res; 25 return err;
33 } 26 }
34 } 27 }
35 28
36 { 29 /* increment the used by the shift amount then copy upwards */
37 mp_digit *top, *bottom; 30 a->used += b;
38 31
39 /* increment the used by the shift amount then copy upwards */ 32 /* top */
40 a->used += b; 33 top = a->dp + a->used - 1;
41 34
42 /* top */ 35 /* base */
43 top = a->dp + a->used - 1; 36 bottom = (a->dp + a->used - 1) - b;
44 37
45 /* base */ 38 /* much like mp_rshd this is implemented using a sliding window
46 bottom = (a->dp + a->used - 1) - b; 39 * except the window goes the otherway around. Copying from
40 * the bottom to the top. see bn_mp_rshd.c for more info.
41 */
42 for (x = a->used - 1; x >= b; x--) {
43 *top-- = *bottom--;
44 }
47 45
48 /* much like mp_rshd this is implemented using a sliding window 46 /* zero the lower digits */
49 * except the window goes the otherway around. Copying from 47 MP_ZERO_DIGITS(a->dp, b);
50 * the bottom to the top. see bn_mp_rshd.c for more info.
51 */
52 for (x = a->used - 1; x >= b; x--) {
53 *top-- = *bottom--;
54 }
55 48
56 /* zero the lower digits */
57 top = a->dp;
58 for (x = 0; x < b; x++) {
59 *top++ = 0;
60 }
61 }
62 return MP_OKAY; 49 return MP_OKAY;
63 } 50 }
64 #endif 51 #endif
65
66 /* ref: HEAD -> master, tag: v1.1.0 */
67 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
68 /* commit time: 2019-01-28 20:32:32 +0100 */