Coverage Report

Created: 2024-09-08 06:28

/src/dropbear/libtommath/bn_mp_rshd.c
Line
Count
Source
1
#include "tommath_private.h"
2
#ifdef BN_MP_RSHD_C
3
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4
/* SPDX-License-Identifier: Unlicense */
5
6
/* shift right a certain amount of digits */
7
void mp_rshd(mp_int *a, int b)
8
7.12M
{
9
7.12M
   int     x;
10
7.12M
   mp_digit *bottom, *top;
11
12
   /* if b <= 0 then ignore it */
13
7.12M
   if (b <= 0) {
14
11.3k
      return;
15
11.3k
   }
16
17
   /* if b > used then simply zero it and return */
18
7.11M
   if (a->used <= b) {
19
79.7k
      mp_zero(a);
20
79.7k
      return;
21
79.7k
   }
22
23
   /* shift the digits down */
24
25
   /* bottom */
26
7.03M
   bottom = a->dp;
27
28
   /* top [offset into digits] */
29
7.03M
   top = a->dp + b;
30
31
   /* this is implemented as a sliding window where
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.
36
37
    b-2 | b-1 | b0 | b1 | b2 | ... | bb |   ---->
38
                /\                   |      ---->
39
                 \-------------------/      ---->
40
    */
41
95.8M
   for (x = 0; x < (a->used - b); x++) {
42
88.8M
      *bottom++ = *top++;
43
88.8M
   }
44
45
   /* zero the top digits */
46
7.03M
   MP_ZERO_DIGITS(bottom, a->used - x);
47
48
   /* remove excess digits */
49
7.03M
   a->used -= b;
50
7.03M
}
51
#endif