2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-19 14:24:39 +00:00
|
|
|
#ifdef MP_MUL_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
|
|
|
|
|
|
|
/* high level multiplication (handles sign) */
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2019-04-08 21:48:39 +00:00
|
|
|
mp_err err;
|
|
|
|
int min_len = MP_MIN(a->used, b->used),
|
|
|
|
max_len = MP_MAX(a->used, b->used),
|
|
|
|
digs = a->used + b->used + 1;
|
|
|
|
mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
|
2003-08-05 01:24:44 +00:00
|
|
|
|
2019-04-08 21:48:39 +00:00
|
|
|
if (MP_HAS(S_MP_BALANCE_MUL) &&
|
|
|
|
/* Check sizes. The smaller one needs to be larger than the Karatsuba cut-off.
|
|
|
|
* The bigger one needs to be at least about one MP_KARATSUBA_MUL_CUTOFF bigger
|
|
|
|
* to make some sense, but it depends on architecture, OS, position of the
|
|
|
|
* stars... so YMMV.
|
|
|
|
* Using it to cut the input into slices small enough for fast_s_mp_mul_digs
|
|
|
|
* was actually slower on the author's machine, but YMMV.
|
|
|
|
*/
|
|
|
|
(min_len >= MP_KARATSUBA_MUL_CUTOFF) &&
|
2019-09-02 16:23:32 +00:00
|
|
|
((max_len / 2) >= MP_KARATSUBA_MUL_CUTOFF) &&
|
2019-04-08 21:48:39 +00:00
|
|
|
/* Not much effect was observed below a ratio of 1:2, but again: YMMV. */
|
|
|
|
(max_len >= (2 * min_len))) {
|
|
|
|
err = s_mp_balance_mul(a,b,c);
|
|
|
|
} else if (MP_HAS(S_MP_TOOM_MUL) &&
|
|
|
|
(min_len >= MP_TOOM_MUL_CUTOFF)) {
|
2019-05-19 15:16:13 +00:00
|
|
|
err = s_mp_toom_mul(a, b, c);
|
2019-04-08 21:48:39 +00:00
|
|
|
} else if (MP_HAS(S_MP_KARATSUBA_MUL) &&
|
|
|
|
(min_len >= MP_KARATSUBA_MUL_CUTOFF)) {
|
|
|
|
err = s_mp_karatsuba_mul(a, b, c);
|
|
|
|
} else if (MP_HAS(S_MP_MUL_DIGS_FAST) &&
|
|
|
|
/* can we use the fast multiplier?
|
|
|
|
*
|
|
|
|
* The fast multiplier can be used if the output will
|
|
|
|
* have less than MP_WARRAY digits and the number of
|
|
|
|
* digits won't affect carry propagation
|
|
|
|
*/
|
|
|
|
(digs < MP_WARRAY) &&
|
|
|
|
(min_len <= MP_MAXFAST)) {
|
|
|
|
err = s_mp_mul_digs_fast(a, b, c, digs);
|
|
|
|
} else if (MP_HAS(S_MP_MUL_DIGS)) {
|
|
|
|
err = s_mp_mul_digs(a, b, c, digs);
|
|
|
|
} else {
|
|
|
|
err = MP_VAL;
|
|
|
|
}
|
2017-08-30 17:11:35 +00:00
|
|
|
c->sign = (c->used > 0) ? neg : MP_ZPOS;
|
2019-05-19 15:16:13 +00:00
|
|
|
return err;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|