Coverage Report

Created: 2025-08-29 06:39

/src/dropbear/libtommath/bn_s_mp_add.c
Line
Count
Source (jump to first uncovered line)
1
#include "tommath_private.h"
2
#ifdef BN_S_MP_ADD_C
3
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4
/* SPDX-License-Identifier: Unlicense */
5
6
/* low level addition, based on HAC pp.594, Algorithm 14.7 */
7
mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c)
8
34.8M
{
9
34.8M
   const mp_int *x;
10
34.8M
   mp_err err;
11
34.8M
   int     olduse, min, max;
12
13
   /* find sizes, we let |a| <= |b| which means we have to sort
14
    * them.  "x" will point to the input with the most digits
15
    */
16
34.8M
   if (a->used > b->used) {
17
66.7k
      min = b->used;
18
66.7k
      max = a->used;
19
66.7k
      x = a;
20
34.7M
   } else {
21
34.7M
      min = a->used;
22
34.7M
      max = b->used;
23
34.7M
      x = b;
24
34.7M
   }
25
26
   /* init result */
27
34.8M
   if (c->alloc < (max + 1)) {
28
7.54k
      if ((err = mp_grow(c, max + 1)) != MP_OKAY) {
29
0
         return err;
30
0
      }
31
7.54k
   }
32
33
   /* get old used digit count and set new one */
34
34.8M
   olduse = c->used;
35
34.8M
   c->used = max + 1;
36
37
34.8M
   {
38
34.8M
      mp_digit u, *tmpa, *tmpb, *tmpc;
39
34.8M
      int i;
40
41
      /* alias for digit pointers */
42
43
      /* first input */
44
34.8M
      tmpa = a->dp;
45
46
      /* second input */
47
34.8M
      tmpb = b->dp;
48
49
      /* destination */
50
34.8M
      tmpc = c->dp;
51
52
      /* zero the carry */
53
34.8M
      u = 0;
54
530M
      for (i = 0; i < min; i++) {
55
         /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */
56
495M
         *tmpc = *tmpa++ + *tmpb++ + u;
57
58
         /* U = carry bit of T[i] */
59
495M
         u = *tmpc >> (mp_digit)MP_DIGIT_BIT;
60
61
         /* take away carry bit from T[i] */
62
495M
         *tmpc++ &= MP_MASK;
63
495M
      }
64
65
      /* now copy higher words if any, that is in A+B
66
       * if A or B has more digits add those in
67
       */
68
34.8M
      if (min != max) {
69
91.4M
         for (; i < max; i++) {
70
            /* T[i] = X[i] + U */
71
84.1M
            *tmpc = x->dp[i] + u;
72
73
            /* U = carry bit of T[i] */
74
84.1M
            u = *tmpc >> (mp_digit)MP_DIGIT_BIT;
75
76
            /* take away carry bit from T[i] */
77
84.1M
            *tmpc++ &= MP_MASK;
78
84.1M
         }
79
7.34M
      }
80
81
      /* add carry */
82
34.8M
      *tmpc++ = u;
83
84
      /* clear digits above oldused */
85
34.8M
      MP_ZERO_DIGITS(tmpc, olduse - c->used);
86
34.8M
   }
87
88
34.8M
   mp_clamp(c);
89
34.8M
   return MP_OKAY;
90
34.8M
}
91
#endif