libtommath/s_mp_karatsuba_sqr.c

93 lines
2.5 KiB
C
Raw Normal View History

#include "tommath_private.h"
#ifdef S_MP_KARATSUBA_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
2017-08-30 03:51:11 +00:00
/* Karatsuba squaring, computes b = a*a using three
2003-05-29 13:35:26 +00:00
* half size squarings
2003-02-28 16:08:34 +00:00
*
2017-08-30 03:51:11 +00:00
* See comments of karatsuba_mul for details. It
* is essentially the same algorithm but merely
2003-05-29 13:35:26 +00:00
* tuned to perform recursive squarings.
2003-02-28 16:08:34 +00:00
*/
mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b)
2003-02-28 16:08:34 +00:00
{
2017-08-30 17:11:35 +00:00
mp_int x0, x1, t1, t2, x0x0, x1x1;
int B;
2019-10-29 19:05:30 +00:00
mp_err err;
2017-08-30 17:11:35 +00:00
/* min # of digits */
B = a->used;
/* now divide in two */
B = B >> 1;
/* init copy all the temps */
2019-10-29 19:05:30 +00:00
if ((err = mp_init_size(&x0, B)) != MP_OKAY)
goto LBL_ERR;
2019-10-29 19:05:30 +00:00
if ((err = mp_init_size(&x1, a->used - B)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X0;
/* init temps */
2019-10-29 19:05:30 +00:00
if ((err = mp_init_size(&t1, a->used * 2)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1;
2019-10-29 19:05:30 +00:00
if ((err = mp_init_size(&t2, a->used * 2)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto T1;
2019-10-29 19:05:30 +00:00
if ((err = mp_init_size(&x0x0, B * 2)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto T2;
2019-10-29 19:05:30 +00:00
if ((err = mp_init_size(&x1x1, (a->used - B) * 2)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X0X0;
2019-10-29 19:05:30 +00:00
/* now shift the digits */
2017-08-30 17:11:35 +00:00
x0.used = B;
x1.used = a->used - B;
s_mp_copy_digs(x0.dp, a->dp, x0.used);
s_mp_copy_digs(x1.dp, a->dp + B, x1.used);
2017-08-30 17:11:35 +00:00
mp_clamp(&x0);
/* now calc the products x0*x0 and x1*x1 */
2019-10-29 19:05:30 +00:00
if ((err = mp_sqr(&x0, &x0x0)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1X1; /* x0x0 = x0*x0 */
2019-10-29 19:05:30 +00:00
if ((err = mp_sqr(&x1, &x1x1)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1X1; /* x1x1 = x1*x1 */
/* now calc (x1+x0)**2 */
2019-10-29 19:05:30 +00:00
if ((err = s_mp_add(&x1, &x0, &t1)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1X1; /* t1 = x1 - x0 */
2019-10-29 19:05:30 +00:00
if ((err = mp_sqr(&t1, &t1)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
/* add x0y0 */
2019-10-29 19:05:30 +00:00
if ((err = s_mp_add(&x0x0, &x1x1, &t2)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1X1; /* t2 = x0x0 + x1x1 */
2019-10-29 19:05:30 +00:00
if ((err = s_mp_sub(&t1, &t2, &t1)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
/* shift by B */
2019-10-29 19:05:30 +00:00
if ((err = mp_lshd(&t1, B)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
2019-10-29 19:05:30 +00:00
if ((err = mp_lshd(&x1x1, B * 2)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1X1; /* x1x1 = x1x1 << 2*B */
2019-10-29 19:05:30 +00:00
if ((err = mp_add(&x0x0, &t1, &t1)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1X1; /* t1 = x0x0 + t1 */
2019-10-29 19:05:30 +00:00
if ((err = mp_add(&t1, &x1x1, b)) != MP_OKAY)
2017-08-30 17:11:35 +00:00
goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
2017-08-28 20:34:46 +00:00
X1X1:
2017-08-30 17:11:35 +00:00
mp_clear(&x1x1);
2017-08-28 20:34:46 +00:00
X0X0:
2017-08-30 17:11:35 +00:00
mp_clear(&x0x0);
2017-08-28 20:34:46 +00:00
T2:
2017-08-30 17:11:35 +00:00
mp_clear(&t2);
2017-08-28 20:34:46 +00:00
T1:
2017-08-30 17:11:35 +00:00
mp_clear(&t1);
2017-08-28 20:34:46 +00:00
X1:
2017-08-30 17:11:35 +00:00
mp_clear(&x1);
2017-08-28 20:34:46 +00:00
X0:
2017-08-30 17:11:35 +00:00
mp_clear(&x0);
LBL_ERR:
2017-08-30 17:11:35 +00:00
return err;
2003-02-28 16:08:34 +00:00
}
2004-10-29 22:07:18 +00:00
#endif