2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-19 14:24:39 +00:00
|
|
|
#ifdef MP_SQR_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
|
|
|
|
|
|
|
/* computes b = a*a */
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_sqr(const mp_int *a, mp_int *b)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2019-05-19 15:16:13 +00:00
|
|
|
mp_err err;
|
2019-04-08 21:48:39 +00:00
|
|
|
if (MP_HAS(S_MP_TOOM_SQR) && /* use Toom-Cook? */
|
2019-09-02 16:23:32 +00:00
|
|
|
(a->used >= MP_TOOM_SQR_CUTOFF)) {
|
2019-05-19 15:16:13 +00:00
|
|
|
err = s_mp_toom_sqr(a, b);
|
2019-04-08 21:48:39 +00:00
|
|
|
} else if (MP_HAS(S_MP_KARATSUBA_SQR) && /* Karatsuba? */
|
2019-09-02 16:23:32 +00:00
|
|
|
(a->used >= MP_KARATSUBA_SQR_CUTOFF)) {
|
2019-04-08 21:48:39 +00:00
|
|
|
err = s_mp_karatsuba_sqr(a, b);
|
|
|
|
} else if (MP_HAS(S_MP_SQR_FAST) && /* can we use the fast comba multiplier? */
|
|
|
|
(((a->used * 2) + 1) < MP_WARRAY) &&
|
|
|
|
(a->used < (MP_MAXFAST / 2))) {
|
|
|
|
err = s_mp_sqr_fast(a, b);
|
|
|
|
} else if (MP_HAS(S_MP_SQR)) {
|
|
|
|
err = s_mp_sqr(a, b);
|
|
|
|
} else {
|
|
|
|
err = MP_VAL;
|
|
|
|
}
|
2017-08-30 17:13:53 +00:00
|
|
|
b->sign = 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
|