2015-11-12 00:49:07 +00:00
|
|
|
#include <tommath_private.h>
|
2004-10-29 22:07:18 +00:00
|
|
|
#ifdef BN_FAST_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
|
|
|
*/
|
|
|
|
|
|
|
|
/* Fast (comba) multiplier
|
|
|
|
*
|
2003-05-17 12:33:54 +00:00
|
|
|
* This is the fast column-array [comba] multiplier. It is
|
|
|
|
* designed to compute the columns of the product first
|
|
|
|
* then handle the carries afterwards. This has the effect
|
|
|
|
* of making the nested loops that compute the columns very
|
2003-02-28 16:08:34 +00:00
|
|
|
* simple and schedulable on super-scalar processors.
|
|
|
|
*
|
2003-05-17 12:33:54 +00:00
|
|
|
* This has been modified to produce a variable number of
|
|
|
|
* digits of output so if say only a half-product is required
|
|
|
|
* you don't have to compute the upper half (a feature
|
|
|
|
* required for fast Barrett reduction).
|
2003-02-28 16:09:08 +00:00
|
|
|
*
|
|
|
|
* Based on Algorithm 14.12 on pp.595 of HAC.
|
|
|
|
*
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
2005-02-12 08:40:15 +00:00
|
|
|
int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2004-10-29 22:07:18 +00:00
|
|
|
int olduse, res, pa, ix, iz;
|
|
|
|
mp_digit W[MP_WARRAY];
|
2015-11-25 20:59:46 +00:00
|
|
|
mp_word _W;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2003-02-28 16:09:08 +00:00
|
|
|
/* grow the destination as required */
|
2003-02-28 16:08:34 +00:00
|
|
|
if (c->alloc < digs) {
|
|
|
|
if ((res = mp_grow (c, digs)) != MP_OKAY) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
/* number of output digits to produce */
|
|
|
|
pa = MIN(digs, a->used + b->used);
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
/* clear the carry */
|
|
|
|
_W = 0;
|
2004-12-23 02:40:37 +00:00
|
|
|
for (ix = 0; ix < pa; ix++) {
|
2004-10-29 22:07:18 +00:00
|
|
|
int tx, ty;
|
|
|
|
int iy;
|
|
|
|
mp_digit *tmpx, *tmpy;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
/* get offsets into the two bignums */
|
|
|
|
ty = MIN(b->used-1, ix);
|
|
|
|
tx = ix - ty;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
/* setup temp aliases */
|
|
|
|
tmpx = a->dp + tx;
|
|
|
|
tmpy = b->dp + ty;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2005-03-12 11:55:11 +00:00
|
|
|
/* this is the number of times the loop will iterrate, essentially
|
2004-10-29 22:07:18 +00:00
|
|
|
while (tx++ < a->used && ty-- >= 0) { ... }
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
2004-10-29 22:07:18 +00:00
|
|
|
iy = MIN(a->used-tx, ty+1);
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
/* execute loop */
|
|
|
|
for (iz = 0; iz < iy; ++iz) {
|
|
|
|
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
|
2005-08-01 16:37:28 +00:00
|
|
|
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
/* store term */
|
|
|
|
W[ix] = ((mp_digit)_W) & MP_MASK;
|
|
|
|
|
|
|
|
/* make next carry */
|
|
|
|
_W = _W >> ((mp_word)DIGIT_BIT);
|
2015-10-25 15:09:26 +00:00
|
|
|
}
|
2004-12-23 02:40:37 +00:00
|
|
|
|
2003-02-28 16:08:34 +00:00
|
|
|
/* setup dest */
|
2004-08-09 22:15:59 +00:00
|
|
|
olduse = c->used;
|
2005-03-12 11:55:11 +00:00
|
|
|
c->used = pa;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2003-02-28 16:09:08 +00:00
|
|
|
{
|
2015-11-25 20:59:46 +00:00
|
|
|
mp_digit *tmpc;
|
2003-02-28 16:09:08 +00:00
|
|
|
tmpc = c->dp;
|
2015-11-13 09:28:23 +00:00
|
|
|
for (ix = 0; ix < (pa + 1); ix++) {
|
2003-07-12 14:31:43 +00:00
|
|
|
/* now extract the previous digit [below the carry] */
|
2004-10-29 22:07:18 +00:00
|
|
|
*tmpc++ = W[ix];
|
2003-02-28 16:09:08 +00:00
|
|
|
}
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2003-07-12 14:31:43 +00:00
|
|
|
/* clear unused digits [that existed in the old copy of c] */
|
2003-02-28 16:09:08 +00:00
|
|
|
for (; ix < olduse; ix++) {
|
|
|
|
*tmpc++ = 0;
|
|
|
|
}
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
mp_clamp (c);
|
|
|
|
return MP_OKAY;
|
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|
2005-08-01 16:37:28 +00:00
|
|
|
|
2017-08-28 14:27:26 +00:00
|
|
|
/* ref: $Format:%D$ */
|
|
|
|
/* git commit: $Format:%H$ */
|
|
|
|
/* commit time: $Format:%ai$ */
|