1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* multiply by a digit */
7 mp_err
mp_mul_d(const mp_int
*a
, mp_digit b
, mp_int
*c
)
18 if (MP_HAS(MP_MUL_2
) && (b
== 2u)) {
19 return mp_mul_2(a
, c
);
21 if (MP_HAS(MP_MUL_2D
) && MP_IS_2EXPT(b
)) {
23 while ((ix
< MP_DIGIT_BIT
) && (b
!= (((mp_digit
)1)<<ix
))) {
26 return mp_mul_2d(a
, ix
, c
);
29 /* make sure c is big enough to hold a*b */
30 if ((err
= mp_grow(c
, a
->used
+ 1)) != MP_OKAY
) {
34 /* get the original destinations used count */
44 for (ix
= 0; ix
< a
->used
; ix
++) {
45 /* compute product and carry sum for this term */
46 mp_word r
= (mp_word
)u
+ ((mp_word
)a
->dp
[ix
] * (mp_word
)b
);
48 /* mask off higher bits to get a single digit */
49 c
->dp
[ix
] = (mp_digit
)(r
& (mp_word
)MP_MASK
);
51 /* send carry into next iteration */
52 u
= (mp_digit
)(r
>> (mp_word
)MP_DIGIT_BIT
);
55 /* store final carry [if any] and increment ix offset */
59 c
->used
= a
->used
+ 1;
61 /* now zero digits above the top */
62 s_mp_zero_digs(c
->dp
+ c
->used
, oldused
- c
->used
);