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)
14 if ((err = mp_copy(a, c)) != MP_OKAY) {
19 if (c->alloc < (c->used + (b / MP_DIGIT_BIT) + 1)) {
20 if ((err = mp_grow(c, c->used + (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
25 /* shift by as many digits in the bit count */
26 if (b >= MP_DIGIT_BIT) {
27 if ((err = mp_lshd(c, b / MP_DIGIT_BIT)) != MP_OKAY) {
32 /* shift any bit count < MP_DIGIT_BIT */
33 d = (mp_digit)(b % MP_DIGIT_BIT);
35 mp_digit *tmpc, shift, mask, r, rr;
38 /* bitmask for carries */
39 mask = ((mp_digit)1 << d) - (mp_digit)1;
42 shift = (mp_digit)MP_DIGIT_BIT - d;
49 for (x = 0; x < c->used; x++) {
50 /* get the higher bits of the current word */
51 rr = (*tmpc >> shift) & mask;
53 /* shift the current word and OR in the carry */
54 *tmpc = ((*tmpc << d) | r) & MP_MASK;
57 /* set the carry to the carry bits of the current word */
63 c->dp[(c->used)++] = r;