2
|
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2 * |
|
3 * LibTomMath is a library that provides multiple-precision |
|
4 * integer arithmetic as well as number theoretic functionality. |
|
5 * |
|
6 * The library was designed directly after the MPI library by |
|
7 * Michael Fromberger but has been written from scratch with |
|
8 * additional optimizations in place. |
|
9 * |
|
10 * The library is free for all purposes without any express |
|
11 * guarantee it works. |
|
12 * |
|
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
14 */ |
|
15 #include <tommath.h> |
|
16 |
|
17 /* shift right a certain amount of digits */ |
|
18 void mp_rshd (mp_int * a, int b) |
|
19 { |
|
20 int x; |
|
21 |
|
22 /* if b <= 0 then ignore it */ |
|
23 if (b <= 0) { |
|
24 return; |
|
25 } |
|
26 |
|
27 /* if b > used then simply zero it and return */ |
|
28 if (a->used <= b) { |
|
29 mp_zero (a); |
|
30 return; |
|
31 } |
|
32 |
|
33 { |
|
34 register mp_digit *bottom, *top; |
|
35 |
|
36 /* shift the digits down */ |
|
37 |
|
38 /* bottom */ |
|
39 bottom = a->dp; |
|
40 |
|
41 /* top [offset into digits] */ |
|
42 top = a->dp + b; |
|
43 |
|
44 /* this is implemented as a sliding window where |
|
45 * the window is b-digits long and digits from |
|
46 * the top of the window are copied to the bottom |
|
47 * |
|
48 * e.g. |
|
49 |
|
50 b-2 | b-1 | b0 | b1 | b2 | ... | bb | ----> |
|
51 /\ | ----> |
|
52 \-------------------/ ----> |
|
53 */ |
|
54 for (x = 0; x < (a->used - b); x++) { |
|
55 *bottom++ = *top++; |
|
56 } |
|
57 |
|
58 /* zero the top digits */ |
|
59 for (; x < a->used; x++) { |
|
60 *bottom++ = 0; |
|
61 } |
|
62 } |
|
63 |
|
64 /* remove excess digits */ |
|
65 a->used -= b; |
|
66 } |