libtommath/bn_mp_sqr.c

43 lines
959 B
C
Raw Normal View History

#include "tommath_private.h"
2004-10-29 22:07:18 +00:00
#ifdef BN_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 */
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;
2004-01-25 17:40:21 +00:00
2019-04-12 12:56:29 +00:00
#ifdef BN_S_MP_TOOM_SQR_C
2017-08-30 17:13:53 +00:00
/* use Toom-Cook? */
if (a->used >= MP_TOOM_SQR_CUTOFF) {
2019-05-19 15:16:13 +00:00
err = s_mp_toom_sqr(a, b);
2017-08-30 17:13:53 +00:00
/* Karatsuba? */
} else
2004-10-29 22:07:18 +00:00
#endif
2019-04-12 12:56:29 +00:00
#ifdef BN_S_MP_KARATSUBA_SQR_C
if (a->used >= MP_KARATSUBA_SQR_CUTOFF) {
2019-05-19 15:16:13 +00:00
err = s_mp_karatsuba_sqr(a, b);
2017-08-30 17:13:53 +00:00
} else
2004-10-29 22:07:18 +00:00
#endif
2017-08-30 17:13:53 +00:00
{
2019-04-12 12:56:29 +00:00
#ifdef BN_S_MP_SQR_FAST_C
2017-08-30 17:13:53 +00:00
/* can we use the fast comba multiplier? */
2019-05-23 16:00:21 +00:00
if ((((a->used * 2) + 1) < MP_WARRAY) &&
(a->used < (MP_MAXFAST / 2))) {
2019-05-19 15:16:13 +00:00
err = s_mp_sqr_fast(a, b);
2017-08-30 17:13:53 +00:00
} else
2004-10-29 22:07:18 +00:00
#endif
2017-08-30 17:13:53 +00:00
{
2004-10-29 22:07:18 +00:00
#ifdef BN_S_MP_SQR_C
2019-05-19 15:16:13 +00:00
err = s_mp_sqr(a, b);
2004-10-29 22:07:18 +00:00
#else
2019-05-19 15:16:13 +00:00
err = MP_VAL;
2004-10-29 22:07:18 +00:00
#endif
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