Hash :
93f8e760
Author :
Date :
2019-11-05T18:04:05
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include "tommath_private.h"
#ifdef S_MP_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
/* multiplies |a| * |b| and only computes upto digs digits of result
* HAC pp. 595, Algorithm 14.12 Modified so you can control how
* many digits of output are created.
*/
mp_err s_mp_mul(const mp_int *a, const mp_int *b, mp_int *c, int digs)
{
mp_int t;
mp_err err;
int pa, ix;
/* can we use the fast multiplier? */
if ((digs < MP_WARRAY) &&
(MP_MIN(a->used, b->used) < MP_MAX_COMBA)) {
return s_mp_mul_comba(a, b, c, digs);
}
if ((err = mp_init_size(&t, digs)) != MP_OKAY) {
return err;
}
t.used = digs;
/* compute the digits of the product directly */
pa = a->used;
for (ix = 0; ix < pa; ix++) {
int iy, pb;
mp_digit u = 0;
/* limit ourselves to making digs digits of output */
pb = MP_MIN(b->used, digs - ix);
/* compute the columns of the output and propagate the carry */
for (iy = 0; iy < pb; iy++) {
/* compute the column as a mp_word */
mp_word r = (mp_word)t.dp[ix + iy] +
((mp_word)a->dp[ix] * (mp_word)b->dp[iy]) +
(mp_word)u;
/* the new column is the lower part of the result */
t.dp[ix + iy] = (mp_digit)(r & (mp_word)MP_MASK);
/* get the carry word from the result */
u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
}
/* set carry if it is placed below digs */
if ((ix + iy) < digs) {
t.dp[ix + pb] = u;
}
}
mp_clamp(&t);
mp_exch(&t, c);
mp_clear(&t);
return MP_OKAY;
}
#endif