make mp_sqr private (optimization of mp_mul)

This commit is contained in:
Daniel Mendler 2019-11-06 16:49:59 +01:00
parent 4077293f4a
commit 0fa802f24b
No known key found for this signature in database
GPG Key ID: D88ADB2A2693CA43
3 changed files with 29 additions and 41 deletions

View File

@ -12,18 +12,34 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
digs = a->used + b->used + 1; digs = a->used + b->used + 1;
mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
if (MP_HAS(S_MP_MUL_BALANCE) && if ((a == b) &&
/* Check sizes. The smaller one needs to be larger than the Karatsuba cut-off. MP_HAS(S_MP_SQR_TOOM) && /* use Toom-Cook? */
* The bigger one needs to be at least about one MP_MUL_KARATSUBA_CUTOFF bigger (a->used >= MP_SQR_TOOM_CUTOFF)) {
* to make some sense, but it depends on architecture, OS, position of the err = s_mp_sqr_toom(a, c);
* stars... so YMMV. } else if ((a == b) &&
* Using it to cut the input into slices small enough for s_mp_mul_comba MP_HAS(S_MP_SQR_KARATSUBA) && /* Karatsuba? */
* was actually slower on the author's machine, but YMMV. (a->used >= MP_SQR_KARATSUBA_CUTOFF)) {
*/ err = s_mp_sqr_karatsuba(a, c);
(min >= MP_MUL_KARATSUBA_CUTOFF) && } else if ((a == b) &&
((max / 2) >= MP_MUL_KARATSUBA_CUTOFF) && MP_HAS(S_MP_SQR_COMBA) && /* can we use the fast comba multiplier? */
/* Not much effect was observed below a ratio of 1:2, but again: YMMV. */ (((a->used * 2) + 1) < MP_WARRAY) &&
(max >= (2 * min))) { (a->used < (MP_MAX_COMBA / 2))) {
err = s_mp_sqr_comba(a, c);
} else if ((a == b) &&
MP_HAS(S_MP_SQR)) {
err = s_mp_sqr(a, c);
} else if (MP_HAS(S_MP_MUL_BALANCE) &&
/* 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_MUL_KARATSUBA_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 s_mp_mul_comba
* was actually slower on the author's machine, but YMMV.
*/
(min >= MP_MUL_KARATSUBA_CUTOFF) &&
((max / 2) >= MP_MUL_KARATSUBA_CUTOFF) &&
/* Not much effect was observed below a ratio of 1:2, but again: YMMV. */
(max >= (2 * min))) {
err = s_mp_mul_balance(a,b,c); err = s_mp_mul_balance(a,b,c);
} else if (MP_HAS(S_MP_MUL_TOOM) && } else if (MP_HAS(S_MP_MUL_TOOM) &&
(min >= MP_MUL_TOOM_CUTOFF)) { (min >= MP_MUL_TOOM_CUTOFF)) {

View File

@ -1,28 +0,0 @@
#include "tommath_private.h"
#ifdef MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
/* computes b = a*a */
mp_err mp_sqr(const mp_int *a, mp_int *b)
{
mp_err err;
if (MP_HAS(S_MP_SQR_TOOM) && /* use Toom-Cook? */
(a->used >= MP_SQR_TOOM_CUTOFF)) {
err = s_mp_sqr_toom(a, b);
} else if (MP_HAS(S_MP_SQR_KARATSUBA) && /* Karatsuba? */
(a->used >= MP_SQR_KARATSUBA_CUTOFF)) {
err = s_mp_sqr_karatsuba(a, b);
} else if (MP_HAS(S_MP_SQR_COMBA) && /* can we use the fast comba multiplier? */
(((a->used * 2) + 1) < MP_WARRAY) &&
(a->used < (MP_MAX_COMBA / 2))) {
err = s_mp_sqr_comba(a, b);
} else if (MP_HAS(S_MP_SQR)) {
err = s_mp_sqr(a, b);
} else {
err = MP_VAL;
}
b->sign = MP_ZPOS;
return err;
}
#endif

View File

@ -366,7 +366,7 @@ mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
/* b = a*a */ /* b = a*a */
mp_err mp_sqr(const mp_int *a, mp_int *b) MP_WUR; #define mp_sqr(a, b) mp_mul((a), (a), (b))
/* a/b => cb + d == a */ /* a/b => cb + d == a */
mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR; mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR;