2004-10-29 22:07:18 +00:00
|
|
|
#include <tommath.h>
|
|
|
|
#ifdef BN_S_MP_MUL_DIGS_C
|
2003-02-28 16:08:34 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
|
|
|
*
|
2003-08-05 01:24:44 +00:00
|
|
|
* LibTomMath is a library that provides multiple-precision
|
2003-02-28 16:08:34 +00:00
|
|
|
* integer arithmetic as well as number theoretic functionality.
|
|
|
|
*
|
2003-08-05 01:24:44 +00:00
|
|
|
* The library was designed directly after the MPI library by
|
2003-02-28 16:08:34 +00: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 21:55:29 +00:00
|
|
|
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* multiplies |a| * |b| and only computes upto digs digits of result
|
2003-05-17 12:33:54 +00:00
|
|
|
* HAC pp. 595, Algorithm 14.12 Modified so you can control how
|
|
|
|
* many digits of output are created.
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
2005-03-12 11:55:11 +00:00
|
|
|
int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2003-02-28 16:09:08 +00:00
|
|
|
mp_int t;
|
|
|
|
int res, pa, pb, ix, iy;
|
|
|
|
mp_digit u;
|
|
|
|
mp_word r;
|
|
|
|
mp_digit tmpx, *tmpt, *tmpy;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2003-05-17 12:33:54 +00:00
|
|
|
/* can we use the fast multiplier? */
|
|
|
|
if (((digs) < MP_WARRAY) &&
|
|
|
|
MIN (a->used, b->used) <
|
|
|
|
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
|
|
|
return fast_s_mp_mul_digs (a, b, c, digs);
|
|
|
|
}
|
|
|
|
|
2003-02-28 16:08:34 +00: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 12:33:54 +00:00
|
|
|
/* copy of the digit from a used within the nested loop */
|
2003-02-28 16:08:34 +00:00
|
|
|
tmpx = a->dp[ix];
|
2003-05-17 12:33:54 +00:00
|
|
|
|
|
|
|
/* an alias for the destination shifted ix places */
|
|
|
|
tmpt = t.dp + ix;
|
|
|
|
|
|
|
|
/* an alias for the digits of b */
|
2003-02-28 16:08:34 +00: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 */
|
2003-08-29 14:06:56 +00:00
|
|
|
r = ((mp_word)*tmpt) +
|
|
|
|
((mp_word)tmpx) * ((mp_word)*tmpy++) +
|
|
|
|
((mp_word) u);
|
2003-02-28 16:08:34 +00: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 14:06:56 +00:00
|
|
|
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2003-05-17 12:33:54 +00:00
|
|
|
/* set carry if it is placed below digs */
|
|
|
|
if (ix + iy < digs) {
|
2003-02-28 16:08:34 +00:00
|
|
|
*tmpt = u;
|
2003-05-17 12:33:54 +00:00
|
|
|
}
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_clamp (&t);
|
|
|
|
mp_exch (&t, c);
|
|
|
|
|
|
|
|
mp_clear (&t);
|
|
|
|
return MP_OKAY;
|
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|
2005-08-01 16:37:28 +00:00
|
|
|
|
|
|
|
/* $Source$ */
|
|
|
|
/* $Revision$ */
|
|
|
|
/* $Date$ */
|