comparison libtommath/bn_mp_rshd.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_RSHD_C 2 #ifdef BN_MP_RSHD_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 right a certain amount of digits */ 6 /* shift right a certain amount of digits */
16 void mp_rshd(mp_int *a, int b) 7 void mp_rshd(mp_int *a, int b)
17 { 8 {
18 int x; 9 int x;
10 mp_digit *bottom, *top;
19 11
20 /* if b <= 0 then ignore it */ 12 /* if b <= 0 then ignore it */
21 if (b <= 0) { 13 if (b <= 0) {
22 return; 14 return;
23 } 15 }
26 if (a->used <= b) { 18 if (a->used <= b) {
27 mp_zero(a); 19 mp_zero(a);
28 return; 20 return;
29 } 21 }
30 22
31 { 23 /* shift the digits down */
32 mp_digit *bottom, *top;
33 24
34 /* shift the digits down */ 25 /* bottom */
26 bottom = a->dp;
35 27
36 /* bottom */ 28 /* top [offset into digits] */
37 bottom = a->dp; 29 top = a->dp + b;
38 30
39 /* top [offset into digits] */ 31 /* this is implemented as a sliding window where
40 top = a->dp + b; 32 * the window is b-digits long and digits from
33 * the top of the window are copied to the bottom
34 *
35 * e.g.
41 36
42 /* this is implemented as a sliding window where 37 b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
43 * the window is b-digits long and digits from 38 /\ | ---->
44 * the top of the window are copied to the bottom 39 \-------------------/ ---->
45 * 40 */
46 * e.g. 41 for (x = 0; x < (a->used - b); x++) {
42 *bottom++ = *top++;
43 }
47 44
48 b-2 | b-1 | b0 | b1 | b2 | ... | bb | ----> 45 /* zero the top digits */
49 /\ | ----> 46 MP_ZERO_DIGITS(bottom, a->used - x);
50 \-------------------/ ---->
51 */
52 for (x = 0; x < (a->used - b); x++) {
53 *bottom++ = *top++;
54 }
55
56 /* zero the top digits */
57 for (; x < a->used; x++) {
58 *bottom++ = 0;
59 }
60 }
61 47
62 /* remove excess digits */ 48 /* remove excess digits */
63 a->used -= b; 49 a->used -= b;
64 } 50 }
65 #endif 51 #endif
66
67 /* ref: HEAD -> master, tag: v1.1.0 */
68 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
69 /* commit time: 2019-01-28 20:32:32 +0100 */