tommath/bn_fast_s_mp_mul_high_digs.c

98 lines
2.5 KiB
C
Raw Normal View History

2004-10-29 18:07:18 -04:00
#include <tommath.h>
#ifdef BN_FAST_S_MP_MUL_HIGH_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.
*
2003-03-12 21:11:11 -05:00
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
2003-02-28 11:08:34 -05:00
*/
2004-10-29 18:07:18 -04:00
/* this is a modified version of fast_s_mul_digs that only produces
* output digits *above* digs. See the comments for fast_s_mul_digs
2003-02-28 11:08:34 -05:00
* to see how it works.
*
* This is used in the Barrett reduction since for one of the multiplications
* only the higher digits were needed. This essentially halves the work.
2003-02-28 11:09:08 -05:00
*
* Based on Algorithm 14.12 on pp.595 of HAC.
2003-02-28 11:08:34 -05:00
*/
2005-02-12 03:40:15 -05:00
int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
2003-02-28 11:08:34 -05:00
{
2004-10-29 18:07:18 -04:00
int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY];
mp_word _W;
2003-02-28 11:08:34 -05:00
2004-10-29 18:07:18 -04:00
/* grow the destination as required */
pa = a->used + b->used;
if (c->alloc < pa) {
if ((res = mp_grow (c, pa)) != MP_OKAY) {
2003-02-28 11:08:34 -05:00
return res;
}
}
2004-10-29 18:07:18 -04:00
/* number of output digits to produce */
pa = a->used + b->used;
_W = 0;
2004-12-22 21:40:37 -05:00
for (ix = digs; ix < pa; ix++) {
2004-10-29 18:07:18 -04:00
int tx, ty, iy;
mp_digit *tmpx, *tmpy;
2003-02-28 11:08:34 -05:00
2004-10-29 18:07:18 -04:00
/* get offsets into the two bignums */
ty = MIN(b->used-1, ix);
tx = ix - ty;
2003-02-28 11:08:34 -05:00
2004-10-29 18:07:18 -04:00
/* setup temp aliases */
tmpx = a->dp + tx;
tmpy = b->dp + ty;
2003-02-28 11:08:34 -05:00
2004-10-29 18:07:18 -04:00
/* this is the number of times the loop will iterrate, essentially its
while (tx++ < a->used && ty-- >= 0) { ... }
2003-02-28 11:08:34 -05:00
*/
2004-10-29 18:07:18 -04:00
iy = MIN(a->used-tx, ty+1);
2003-02-28 11:08:34 -05:00
2004-10-29 18:07:18 -04:00
/* execute loop */
for (iz = 0; iz < iy; iz++) {
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
2003-02-28 11:08:34 -05:00
}
2004-10-29 18:07:18 -04:00
/* store term */
W[ix] = ((mp_digit)_W) & MP_MASK;
/* make next carry */
_W = _W >> ((mp_word)DIGIT_BIT);
2003-02-28 11:08:34 -05:00
}
2004-12-22 21:40:37 -05:00
/* store final carry */
2005-03-12 06:55:11 -05:00
W[ix] = (mp_digit)(_W & MP_MASK);
2003-02-28 11:08:34 -05:00
/* setup dest */
2004-10-29 18:07:18 -04:00
olduse = c->used;
c->used = pa;
2003-02-28 11:08:34 -05:00
2004-10-29 18:07:18 -04:00
{
register mp_digit *tmpc;
tmpc = c->dp + digs;
for (ix = digs; ix <= pa; ix++) {
/* now extract the previous digit [below the carry] */
*tmpc++ = W[ix];
}
2003-02-28 11:08:34 -05:00
2004-10-29 18:07:18 -04:00
/* clear unused digits [that existed in the old copy of c] */
for (; ix < olduse; ix++) {
*tmpc++ = 0;
}
2003-02-28 11:08:34 -05:00
}
mp_clamp (c);
return MP_OKAY;
}
2004-10-29 18:07:18 -04:00
#endif