2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-04-12 12:56:29 +00:00
|
|
|
#ifdef BN_S_MP_MUL_HIGH_DIGS_FAST_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2004-10-29 22:07:18 +00: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 16:08:34 +00: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 16:09:08 +00:00
|
|
|
*
|
|
|
|
* Based on Algorithm 14.12 on pp.595 of HAC.
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
2019-04-12 12:56:29 +00:00
|
|
|
int s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2017-08-29 20:23:48 +00:00
|
|
|
int olduse, res, pa, ix, iz;
|
|
|
|
mp_digit W[MP_WARRAY];
|
|
|
|
mp_word _W;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* grow the destination as required */
|
|
|
|
pa = a->used + b->used;
|
|
|
|
if (c->alloc < pa) {
|
|
|
|
if ((res = mp_grow(c, pa)) != MP_OKAY) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* number of output digits to produce */
|
|
|
|
pa = a->used + b->used;
|
|
|
|
_W = 0;
|
|
|
|
for (ix = digs; ix < pa; ix++) {
|
2004-10-29 22:07:18 +00:00
|
|
|
int tx, ty, 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 */
|
2019-04-09 09:08:26 +00:00
|
|
|
ty = MP_MIN(b->used-1, ix);
|
2004-10-29 22:07:18 +00:00
|
|
|
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
|
|
|
|
2017-08-30 03:51:11 +00:00
|
|
|
/* this is the number of times the loop will iterrate, essentially its
|
2004-10-29 22:07:18 +00:00
|
|
|
while (tx++ < a->used && ty-- >= 0) { ... }
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
2019-04-09 09:08:26 +00:00
|
|
|
iy = MP_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++) {
|
2017-10-15 17:58:35 +00:00
|
|
|
_W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
|
|
|
|
/* store term */
|
2017-10-15 17:58:35 +00:00
|
|
|
W[ix] = (mp_digit)_W & MP_MASK;
|
2004-10-29 22:07:18 +00:00
|
|
|
|
|
|
|
/* make next carry */
|
2017-10-15 17:58:35 +00:00
|
|
|
_W = _W >> (mp_word)DIGIT_BIT;
|
2017-08-29 20:23:48 +00:00
|
|
|
}
|
2017-08-30 03:51:11 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* setup dest */
|
|
|
|
olduse = c->used;
|
|
|
|
c->used = pa;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
{
|
|
|
|
mp_digit *tmpc;
|
2004-10-29 22:07:18 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
tmpc = c->dp + digs;
|
|
|
|
for (ix = digs; ix < pa; ix++) {
|
|
|
|
/* now extract the previous digit [below the carry] */
|
|
|
|
*tmpc++ = W[ix];
|
|
|
|
}
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2017-08-29 20:23:48 +00:00
|
|
|
/* clear unused digits [that existed in the old copy of c] */
|
|
|
|
for (; ix < olduse; ix++) {
|
|
|
|
*tmpc++ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_clamp(c);
|
|
|
|
return MP_OKAY;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|