t7x/deps/libtommath/s_mp_mul_comba.c

88 lines
2.2 KiB
C
Raw Normal View History

2023-12-06 17:43:39 -05:00
#include "tommath_private.h"
#ifdef S_MP_MUL_COMBA_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
/* Fast (comba) multiplier
*
* 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
* simple and schedulable on super-scalar processors.
*
* 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).
*
* Based on Algorithm 14.12 on pp.595 of HAC.
*
*/
mp_err s_mp_mul_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs)
{
int oldused, pa, ix;
mp_err err;
2024-06-17 19:04:36 -04:00
mp_digit MP_ALLOC_WARRAY(W);
2023-12-06 17:43:39 -05:00
mp_word _W;
2024-06-17 19:04:36 -04:00
MP_CHECK_WARRAY(W);
2023-12-06 17:43:39 -05:00
if (digs < 0) {
2024-06-17 19:04:36 -04:00
MP_FREE_WARRAY(W);
2023-12-06 17:43:39 -05:00
return MP_VAL;
}
/* grow the destination as required */
if ((err = mp_grow(c, digs)) != MP_OKAY) {
2024-06-17 19:04:36 -04:00
MP_FREE_WARRAY(W);
2023-12-06 17:43:39 -05:00
return err;
}
/* number of output digits to produce */
pa = MP_MIN(digs, a->used + b->used);
/* clear the carry */
_W = 0;
for (ix = 0; ix < pa; ix++) {
int tx, ty, iy, iz;
/* get offsets into the two bignums */
ty = MP_MIN(b->used-1, ix);
tx = ix - ty;
/* this is the number of times the loop will iterate, essentially
while (tx++ < a->used && ty-- >= 0) { ... }
*/
iy = MP_MIN(a->used-tx, ty+1);
/* execute loop */
for (iz = 0; iz < iy; ++iz) {
_W += (mp_word)a->dp[tx + iz] * (mp_word)b->dp[ty - iz];
}
/* store term */
W[ix] = (mp_digit)_W & MP_MASK;
/* make next carry */
_W = _W >> (mp_word)MP_DIGIT_BIT;
}
/* setup dest */
oldused = c->used;
c->used = pa;
for (ix = 0; ix < pa; ix++) {
/* now extract the previous digit [below the carry] */
c->dp[ix] = W[ix];
}
/* clear unused digits [that existed in the old copy of c] */
s_mp_zero_digs(c->dp + c->used, oldused - c->used);
mp_clamp(c);
2024-06-17 19:04:36 -04:00
MP_FREE_WARRAY(W);
2023-12-06 17:43:39 -05:00
return MP_OKAY;
}
#endif