2018-05-02 15:43:17 -04:00
|
|
|
#include "tommath_private.h"
|
2004-10-29 18:07:18 -04:00
|
|
|
#ifdef BN_S_MP_MUL_DIGS_C
|
2003-02-28 11:08:34 -05:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
|
|
|
*
|
2003-08-04 21:24:44 -04:00
|
|
|
* LibTomMath is a library that provides multiple-precision
|
2003-02-28 11:08:34 -05:00
|
|
|
* integer arithmetic as well as number theoretic functionality.
|
|
|
|
*
|
2003-08-04 21:24:44 -04:00
|
|
|
* The library was designed directly after the MPI library by
|
2003-02-28 11:08:34 -05:00
|
|
|
* Michael Fromberger but has been written from scratch with
|
|
|
|
* additional optimizations in place.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* multiplies |a| * |b| and only computes upto digs digits of result
|
2017-08-29 23:51:11 -04:00
|
|
|
* HAC pp. 595, Algorithm 14.12 Modified so you can control how
|
2003-05-17 08:33:54 -04:00
|
|
|
* many digits of output are created.
|
2003-02-28 11:08:34 -05:00
|
|
|
*/
|
2017-09-20 10:59:43 -04:00
|
|
|
int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
|
2003-02-28 11:08:34 -05:00
|
|
|
{
|
2017-08-30 13:15:27 -04:00
|
|
|
mp_int t;
|
|
|
|
int res, pa, pb, ix, iy;
|
|
|
|
mp_digit u;
|
|
|
|
mp_word r;
|
|
|
|
mp_digit tmpx, *tmpt, *tmpy;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
/* can we use the fast multiplier? */
|
2017-10-15 13:58:35 -04:00
|
|
|
if ((digs < (int)MP_WARRAY) &&
|
2017-08-30 13:15:27 -04:00
|
|
|
(MIN(a->used, b->used) <
|
2017-10-15 13:57:12 -04:00
|
|
|
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
|
2017-08-30 13:15:27 -04:00
|
|
|
return fast_s_mp_mul_digs(a, b, c, digs);
|
|
|
|
}
|
2003-05-17 08:33:54 -04:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
if ((res = mp_init_size(&t, digs)) != MP_OKAY) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
t.used = digs;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
/* compute the digits of the product directly */
|
|
|
|
pa = a->used;
|
|
|
|
for (ix = 0; ix < pa; ix++) {
|
|
|
|
/* set the carry to zero */
|
|
|
|
u = 0;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
/* limit ourselves to making digs digits of output */
|
|
|
|
pb = MIN(b->used, digs - ix);
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
/* setup some aliases */
|
|
|
|
/* copy of the digit from a used within the nested loop */
|
|
|
|
tmpx = a->dp[ix];
|
2017-08-29 23:51:11 -04:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
/* an alias for the destination shifted ix places */
|
|
|
|
tmpt = t.dp + ix;
|
2017-08-29 23:51:11 -04:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
/* an alias for the digits of b */
|
|
|
|
tmpy = b->dp;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
/* compute the columns of the output and propagate the carry */
|
|
|
|
for (iy = 0; iy < pb; iy++) {
|
|
|
|
/* compute the column as a mp_word */
|
|
|
|
r = (mp_word)*tmpt +
|
|
|
|
((mp_word)tmpx * (mp_word)*tmpy++) +
|
|
|
|
(mp_word)u;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
/* the new column is the lower part of the result */
|
2017-10-15 13:58:35 -04:00
|
|
|
*tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
/* get the carry word from the result */
|
2017-10-15 13:58:35 -04:00
|
|
|
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
|
2017-08-30 13:15:27 -04:00
|
|
|
}
|
|
|
|
/* set carry if it is placed below digs */
|
|
|
|
if ((ix + iy) < digs) {
|
|
|
|
*tmpt = u;
|
|
|
|
}
|
|
|
|
}
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
mp_clamp(&t);
|
|
|
|
mp_exch(&t, c);
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2017-08-30 13:15:27 -04:00
|
|
|
mp_clear(&t);
|
|
|
|
return MP_OKAY;
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
2004-10-29 18:07:18 -04:00
|
|
|
#endif
|
2005-08-01 12:37:28 -04:00
|
|
|
|
2017-08-28 10:27:26 -04:00
|
|
|
/* ref: $Format:%D$ */
|
|
|
|
/* git commit: $Format:%H$ */
|
|
|
|
/* commit time: $Format:%ai$ */
|