libtommath/s_mp_mul_comba.c

79 lines
2.1 KiB
C
Raw Normal View History

#include "tommath_private.h"
#ifdef S_MP_MUL_COMBA_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
/* Fast (comba) multiplier
*
2017-08-30 03:51:11 +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
2003-05-17 12:33:54 +00:00
* 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.
*
2017-08-30 03:51:11 +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
2003-05-17 12:33:54 +00:00
* 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
*/
mp_err s_mp_mul_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs)
2003-02-28 16:08:34 +00:00
{
2019-10-29 19:06:20 +00:00
int oldused, pa, ix;
2019-05-19 15:16:13 +00:00
mp_err err;
2017-08-29 20:23:48 +00:00
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 */
if ((err = mp_grow(c, digs)) != MP_OKAY) {
return err;
2017-08-29 20:23:48 +00:00
}
2003-02-28 16:08:34 +00:00
2017-08-29 20:23:48 +00:00
/* number of output digits to produce */
pa = MP_MIN(digs, a->used + b->used);
2003-02-28 16:08:34 +00:00
2017-08-29 20:23:48 +00:00
/* clear the carry */
_W = 0;
for (ix = 0; ix < pa; ix++) {
2019-10-29 19:06:20 +00:00
int tx, ty, iy, iz;
2003-02-28 16:08:34 +00:00
2004-10-29 22:07:18 +00:00
/* get offsets into the two bignums */
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
2017-08-30 03:51: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
*/
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) {
2019-10-29 19:06:20 +00:00
_W += (mp_word)a->dp[tx + iz] * (mp_word)b->dp[ty - iz];
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 */
2019-04-13 06:46:57 +00:00
_W = _W >> (mp_word)MP_DIGIT_BIT;
2017-08-29 20:23:48 +00:00
}
2004-12-23 02:40:37 +00:00
2017-08-29 20:23:48 +00:00
/* setup dest */
2019-10-29 19:06:20 +00:00
oldused = c->used;
2017-08-29 20:23:48 +00:00
c->used = pa;
2003-02-28 16:08:34 +00:00
2019-10-29 19:06:20 +00:00
for (ix = 0; ix < pa; ix++) {
/* now extract the previous digit [below the carry] */
c->dp[ix] = W[ix];
2017-08-29 20:23:48 +00:00
}
2019-10-29 19:06:20 +00:00
/* clear unused digits [that existed in the old copy of c] */
s_mp_zero_digs(c->dp + c->used, oldused - c->used);
2019-10-29 19:06:20 +00:00
2017-08-29 20:23:48 +00:00
mp_clamp(c);
return MP_OKAY;
2003-02-28 16:08:34 +00:00
}
2004-10-29 22:07:18 +00:00
#endif