/src/dropbear/libtommath/bn_mp_mul_2.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "tommath_private.h" |
2 | | #ifdef BN_MP_MUL_2_C |
3 | | /* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
4 | | /* SPDX-License-Identifier: Unlicense */ |
5 | | |
6 | | /* b = a*2 */ |
7 | | mp_err mp_mul_2(const mp_int *a, mp_int *b) |
8 | 0 | { |
9 | 0 | int x, oldused; |
10 | 0 | mp_err err; |
11 | | |
12 | | /* grow to accomodate result */ |
13 | 0 | if (b->alloc < (a->used + 1)) { |
14 | 0 | if ((err = mp_grow(b, a->used + 1)) != MP_OKAY) { |
15 | 0 | return err; |
16 | 0 | } |
17 | 0 | } |
18 | | |
19 | 0 | oldused = b->used; |
20 | 0 | b->used = a->used; |
21 | |
|
22 | 0 | { |
23 | 0 | mp_digit r, rr, *tmpa, *tmpb; |
24 | | |
25 | | /* alias for source */ |
26 | 0 | tmpa = a->dp; |
27 | | |
28 | | /* alias for dest */ |
29 | 0 | tmpb = b->dp; |
30 | | |
31 | | /* carry */ |
32 | 0 | r = 0; |
33 | 0 | for (x = 0; x < a->used; x++) { |
34 | | |
35 | | /* get what will be the *next* carry bit from the |
36 | | * MSB of the current digit |
37 | | */ |
38 | 0 | rr = *tmpa >> (mp_digit)(MP_DIGIT_BIT - 1); |
39 | | |
40 | | /* now shift up this digit, add in the carry [from the previous] */ |
41 | 0 | *tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK; |
42 | | |
43 | | /* copy the carry that would be from the source |
44 | | * digit into the next iteration |
45 | | */ |
46 | 0 | r = rr; |
47 | 0 | } |
48 | | |
49 | | /* new leading digit? */ |
50 | 0 | if (r != 0u) { |
51 | | /* add a MSB which is always 1 at this point */ |
52 | 0 | *tmpb = 1; |
53 | 0 | ++(b->used); |
54 | 0 | } |
55 | | |
56 | | /* now zero any excess digits on the destination |
57 | | * that we didn't write to |
58 | | */ |
59 | 0 | MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used); |
60 | 0 | } |
61 | 0 | b->sign = a->sign; |
62 | 0 | return MP_OKAY; |
63 | 0 | } |
64 | | #endif |