1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* shift left by a certain bit count */
7 mp_err
mp_mul_2d(const mp_int
*a
, int b
, mp_int
*c
)
15 if ((err
= mp_copy(a
, c
)) != MP_OKAY
) {
19 if ((err
= mp_grow(c
, c
->used
+ (b
/ MP_DIGIT_BIT
) + 1)) != MP_OKAY
) {
23 /* shift by as many digits in the bit count */
24 if (b
>= MP_DIGIT_BIT
) {
25 if ((err
= mp_lshd(c
, b
/ MP_DIGIT_BIT
)) != MP_OKAY
) {
30 /* shift any bit count < MP_DIGIT_BIT */
33 mp_digit shift
, mask
, r
;
36 /* bitmask for carries */
37 mask
= ((mp_digit
)1 << b
) - (mp_digit
)1;
40 shift
= (mp_digit
)(MP_DIGIT_BIT
- b
);
44 for (x
= 0; x
< c
->used
; x
++) {
45 /* get the higher bits of the current word */
46 mp_digit rr
= (c
->dp
[x
] >> shift
) & mask
;
48 /* shift the current word and OR in the carry */
49 c
->dp
[x
] = ((c
->dp
[x
] << b
) | r
) & MP_MASK
;
51 /* set the carry to the carry bits of the current word */
57 c
->dp
[(c
->used
)++] = r
;