libtommath/bn_mp_mul.c

87 lines
2.4 KiB
C
Raw Normal View History

#include "tommath_private.h"
2004-10-29 22:07:18 +00:00
#ifdef BN_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) */
2017-09-20 14:59:43 +00:00
int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
2003-02-28 16:08:34 +00:00
{
2017-08-30 17:11:35 +00:00
int res, neg;
2019-04-12 12:56:29 +00:00
#ifdef BN_S_MP_BALANCE_MUL_C
int len_b, len_a;
#endif
2017-08-30 17:11:35 +00:00
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
2019-04-12 12:56:29 +00:00
#ifdef BN_S_MP_BALANCE_MUL_C
len_a = a->used;
len_b = b->used;
if (len_a == len_b) {
goto GO_ON;
}
/*
* Check sizes. The smaller one needs to be larger than the Karatsuba cut-off.
* The bigger one needs to be at least about one 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.
*/
if ((MP_MIN(len_a, len_b) < MP_KARATSUBA_MUL_CUTOFF)
|| ((MP_MAX(len_a, len_b) / 2) < MP_KARATSUBA_MUL_CUTOFF)) {
goto GO_ON;
}
/*
* Not much effect was observed below a ratio of 1:2, but again: YMMV.
*/
if ((MP_MAX(len_a, len_b) / MP_MIN(len_a, len_b)) < 2) {
goto GO_ON;
}
2019-04-12 12:56:29 +00:00
res = s_mp_balance_mul(a,b,c);
goto END;
GO_ON:
#endif
2003-08-05 01:24:44 +00:00
2017-08-30 17:11:35 +00:00
/* use Toom-Cook? */
2019-04-12 12:56:29 +00:00
#ifdef BN_S_MP_TOOM_MUL_C
if (MP_MIN(a->used, b->used) >= MP_TOOM_MUL_CUTOFF) {
2019-04-12 12:56:29 +00:00
res = s_mp_toom_mul(a, b, c);
2017-08-30 17:11:35 +00:00
} else
2004-10-29 22:07:18 +00:00
#endif
2019-04-12 12:56:29 +00:00
#ifdef BN_S_MP_KARATSUBA_MUL_C
2017-08-30 17:11:35 +00:00
/* use Karatsuba? */
if (MP_MIN(a->used, b->used) >= MP_KARATSUBA_MUL_CUTOFF) {
2019-04-12 12:56:29 +00:00
res = s_mp_karatsuba_mul(a, b, c);
2017-08-30 17:11:35 +00:00
} else
2004-10-29 22:07:18 +00:00
#endif
2017-08-30 17:11:35 +00:00
{
/* 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
*/
int digs = a->used + b->used + 1;
2003-02-28 16:09:08 +00:00
2019-04-12 12:56:29 +00:00
#ifdef BN_S_MP_MUL_DIGS_FAST_C
2017-10-15 17:58:35 +00:00
if ((digs < (int)MP_WARRAY) &&
(MP_MIN(a->used, b->used) <=
(int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT))))) {
2019-04-12 12:56:29 +00:00
res = s_mp_mul_digs_fast(a, b, c, digs);
2017-08-30 17:11:35 +00:00
} else
2004-10-29 22:07:18 +00:00
#endif
2017-08-30 17:11:35 +00:00
{
2004-10-29 22:07:18 +00:00
#ifdef BN_S_MP_MUL_DIGS_C
res = s_mp_mul_digs(a, b, c, a->used + b->used + 1);
2004-10-29 22:07:18 +00:00
#else
2017-08-30 17:11:35 +00:00
res = MP_VAL;
2004-10-29 22:07:18 +00:00
#endif
2017-08-30 17:11:35 +00:00
}
}
END:
2017-08-30 17:11:35 +00:00
c->sign = (c->used > 0) ? neg : MP_ZPOS;
return res;
2003-02-28 16:08:34 +00:00
}
2004-10-29 22:07:18 +00:00
#endif