Coverage Report

Created: 2026-09-04 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dropbear/libtommath/bn_mp_mul_2d.c
Line
Count
Source
1
#include "tommath_private.h"
2
#ifdef BN_MP_MUL_2D_C
3
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4
/* SPDX-License-Identifier: Unlicense */
5
6
/* shift left by a certain bit count */
7
mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c)
8
287k
{
9
287k
   mp_digit d;
10
287k
   mp_err   err;
11
12
287k
   if (b < 0) {
13
0
      return MP_VAL;
14
0
   }
15
16
   /* copy */
17
287k
   if (a != c) {
18
0
      if ((err = mp_copy(a, c)) != MP_OKAY) {
19
0
         return err;
20
0
      }
21
0
   }
22
23
287k
   if (c->alloc < (c->used + (b / MP_DIGIT_BIT) + 1)) {
24
6.11k
      if ((err = mp_grow(c, c->used + (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
25
0
         return err;
26
0
      }
27
6.11k
   }
28
29
   /* shift by as many digits in the bit count */
30
287k
   if (b >= MP_DIGIT_BIT) {
31
0
      if ((err = mp_lshd(c, b / MP_DIGIT_BIT)) != MP_OKAY) {
32
0
         return err;
33
0
      }
34
0
   }
35
36
   /* shift any bit count < MP_DIGIT_BIT */
37
287k
   d = (mp_digit)(b % MP_DIGIT_BIT);
38
287k
   if (d != 0u) {
39
287k
      mp_digit *tmpc, shift, mask, r, rr;
40
287k
      int x;
41
42
      /* bitmask for carries */
43
287k
      mask = ((mp_digit)1 << d) - (mp_digit)1;
44
45
      /* shift for msbs */
46
287k
      shift = (mp_digit)MP_DIGIT_BIT - d;
47
48
      /* alias */
49
287k
      tmpc = c->dp;
50
51
      /* carry */
52
287k
      r    = 0;
53
6.27M
      for (x = 0; x < c->used; x++) {
54
         /* get the higher bits of the current word */
55
5.98M
         rr = (*tmpc >> shift) & mask;
56
57
         /* shift the current word and OR in the carry */
58
5.98M
         *tmpc = ((*tmpc << d) | r) & MP_MASK;
59
5.98M
         ++tmpc;
60
61
         /* set the carry to the carry bits of the current word */
62
5.98M
         r = rr;
63
5.98M
      }
64
65
      /* set final carry */
66
287k
      if (r != 0u) {
67
360
         c->dp[(c->used)++] = r;
68
360
      }
69
287k
   }
70
287k
   mp_clamp(c);
71
287k
   return MP_OKAY;
72
287k
}
73
#endif