Bump actions/upload-artifacts version
[libtommath.git] / mp_mul_2d.c
blobe4581375bf2105b995eec3fbacd8713d3b3e8b3e
1 #include "tommath_private.h"
2 #ifdef MP_MUL_2D_C
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)
9 mp_err err;
11 if (b < 0) {
12 return MP_VAL;
15 if ((err = mp_copy(a, c)) != MP_OKAY) {
16 return err;
19 if ((err = mp_grow(c, c->used + (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
20 return err;
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) {
26 return err;
30 /* shift any bit count < MP_DIGIT_BIT */
31 b %= MP_DIGIT_BIT;
32 if (b != 0u) {
33 mp_digit shift, mask, r;
34 int x;
36 /* bitmask for carries */
37 mask = ((mp_digit)1 << b) - (mp_digit)1;
39 /* shift for msbs */
40 shift = (mp_digit)(MP_DIGIT_BIT - b);
42 /* carry */
43 r = 0;
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 */
52 r = rr;
55 /* set final carry */
56 if (r != 0u) {
57 c->dp[(c->used)++] = r;
60 mp_clamp(c);
61 return MP_OKAY;
63 #endif