2015-11-11 19:49:07 -05: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.
|
|
|
|
*
|
2015-10-30 17:55:29 -04:00
|
|
|
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
2003-02-28 11:08:34 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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
|
|
|
*/
|
2005-03-12 06:55:11 -05:00
|
|
|
int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
2003-02-28 11:08:34 -05:00
|
|
|
{
|
2003-02-28 11:09:08 -05: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
|
|
|
|
2003-05-17 08:33:54 -04:00
|
|
|
/* can we use the fast multiplier? */
|
|
|
|
if (((digs) < MP_WARRAY) &&
|
2017-08-29 23:51:11 -04:00
|
|
|
(MIN (a->used, b->used) <
|
2015-11-13 04:28:23 -05:00
|
|
|
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
2003-05-17 08:33:54 -04:00
|
|
|
return fast_s_mp_mul_digs (a, b, c, digs);
|
|
|
|
}
|
|
|
|
|
2003-02-28 11:08:34 -05:00
|
|
|
if ((res = mp_init_size (&t, digs)) != MP_OKAY) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
t.used = digs;
|
|
|
|
|
|
|
|
/* compute the digits of the product directly */
|
|
|
|
pa = a->used;
|
|
|
|
for (ix = 0; ix < pa; ix++) {
|
|
|
|
/* set the carry to zero */
|
|
|
|
u = 0;
|
|
|
|
|
|
|
|
/* limit ourselves to making digs digits of output */
|
|
|
|
pb = MIN (b->used, digs - ix);
|
|
|
|
|
|
|
|
/* setup some aliases */
|
2003-05-17 08:33:54 -04:00
|
|
|
/* copy of the digit from a used within the nested loop */
|
2003-02-28 11:08:34 -05:00
|
|
|
tmpx = a->dp[ix];
|
2017-08-29 23:51:11 -04:00
|
|
|
|
2003-05-17 08:33:54 -04:00
|
|
|
/* an alias for the destination shifted ix places */
|
|
|
|
tmpt = t.dp + ix;
|
2017-08-29 23:51:11 -04:00
|
|
|
|
2003-05-17 08:33:54 -04:00
|
|
|
/* an alias for the digits of b */
|
2003-02-28 11:08:34 -05:00
|
|
|
tmpy = b->dp;
|
|
|
|
|
|
|
|
/* compute the columns of the output and propagate the carry */
|
|
|
|
for (iy = 0; iy < pb; iy++) {
|
|
|
|
/* compute the column as a mp_word */
|
2015-11-13 04:28:23 -05:00
|
|
|
r = (mp_word)*tmpt +
|
|
|
|
((mp_word)tmpx * (mp_word)*tmpy++) +
|
|
|
|
(mp_word)u;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
|
|
|
/* the new column is the lower part of the result */
|
|
|
|
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
|
|
|
|
|
|
|
|
/* get the carry word from the result */
|
2003-08-29 10:06:56 -04:00
|
|
|
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
2003-05-17 08:33:54 -04:00
|
|
|
/* set carry if it is placed below digs */
|
2015-11-13 04:28:23 -05:00
|
|
|
if ((ix + iy) < digs) {
|
2003-02-28 11:08:34 -05:00
|
|
|
*tmpt = u;
|
2003-05-17 08:33:54 -04:00
|
|
|
}
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_clamp (&t);
|
|
|
|
mp_exch (&t, c);
|
|
|
|
|
|
|
|
mp_clear (&t);
|
|
|
|
return MP_OKAY;
|
|
|
|
}
|
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$ */
|