tommath/bn_mp_karatsuba_sqr.c

122 lines
2.9 KiB
C
Raw Normal View History

#include <tommath_private.h>
2004-10-29 18:07:18 -04:00
#ifdef BN_MP_KARATSUBA_SQR_C
2003-02-28 11:08:34 -05:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
2003-08-04 21:24:44 -04:00
* LibTomMath is a library that provides multiple-precision
2003-02-28 11:08:34 -05:00
* integer arithmetic as well as number theoretic functionality.
*
2003-08-04 21:24:44 -04:00
* The library was designed directly after the MPI library by
2003-02-28 11:08:34 -05:00
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
2003-02-28 11:08:34 -05:00
*/
2003-05-29 09:35:26 -04:00
/* Karatsuba squaring, computes b = a*a using three
* half size squarings
2003-02-28 11:08:34 -05:00
*
2004-10-29 18:07:18 -04:00
* See comments of karatsuba_mul for details. It
2003-05-29 09:35:26 -04:00
* is essentially the same algorithm but merely
* tuned to perform recursive squarings.
2003-02-28 11:08:34 -05:00
*/
2004-04-11 16:46:22 -04:00
int mp_karatsuba_sqr (mp_int * a, mp_int * b)
2003-02-28 11:08:34 -05:00
{
2003-02-28 11:09:08 -05:00
mp_int x0, x1, t1, t2, x0x0, x1x1;
2003-03-12 21:11:11 -05:00
int B, err;
2003-02-28 11:08:34 -05:00
err = MP_MEM;
/* min # of digits */
B = a->used;
/* now divide in two */
2004-04-11 16:46:22 -04:00
B = B >> 1;
2003-02-28 11:08:34 -05:00
/* init copy all the temps */
if (mp_init_size (&x0, B) != MP_OKAY)
goto ERR;
if (mp_init_size (&x1, a->used - B) != MP_OKAY)
goto X0;
/* init temps */
2003-03-12 21:11:11 -05:00
if (mp_init_size (&t1, a->used * 2) != MP_OKAY)
2003-02-28 11:08:34 -05:00
goto X1;
2003-03-12 21:11:11 -05:00
if (mp_init_size (&t2, a->used * 2) != MP_OKAY)
2003-02-28 11:08:34 -05:00
goto T1;
2003-03-12 21:11:11 -05:00
if (mp_init_size (&x0x0, B * 2) != MP_OKAY)
2003-02-28 11:08:34 -05:00
goto T2;
2003-03-12 21:11:11 -05:00
if (mp_init_size (&x1x1, (a->used - B) * 2) != MP_OKAY)
2003-02-28 11:08:34 -05:00
goto X0X0;
2003-03-12 21:11:11 -05:00
{
register int x;
register mp_digit *dst, *src;
src = a->dp;
/* now shift the digits */
dst = x0.dp;
for (x = 0; x < B; x++) {
*dst++ = *src++;
}
2003-02-28 11:08:34 -05:00
2003-03-12 21:11:11 -05:00
dst = x1.dp;
for (x = B; x < a->used; x++) {
*dst++ = *src++;
}
2003-02-28 11:08:34 -05:00
}
x0.used = B;
x1.used = a->used - B;
mp_clamp (&x0);
/* now calc the products x0*x0 and x1*x1 */
if (mp_sqr (&x0, &x0x0) != MP_OKAY)
2003-05-29 09:35:26 -04:00
goto X1X1; /* x0x0 = x0*x0 */
2003-02-28 11:08:34 -05:00
if (mp_sqr (&x1, &x1x1) != MP_OKAY)
2003-05-29 09:35:26 -04:00
goto X1X1; /* x1x1 = x1*x1 */
2003-02-28 11:08:34 -05:00
2005-08-01 12:37:28 -04:00
/* now calc (x1+x0)**2 */
if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
2003-05-29 09:35:26 -04:00
goto X1X1; /* t1 = x1 - x0 */
2003-02-28 11:08:34 -05:00
if (mp_sqr (&t1, &t1) != MP_OKAY)
2003-05-29 09:35:26 -04:00
goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
2003-02-28 11:08:34 -05:00
/* add x0y0 */
2003-03-12 21:11:11 -05:00
if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
2003-05-29 09:35:26 -04:00
goto X1X1; /* t2 = x0x0 + x1x1 */
2005-08-01 12:37:28 -04:00
if (s_mp_sub (&t1, &t2, &t1) != MP_OKAY)
goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
2003-02-28 11:08:34 -05:00
/* shift by B */
if (mp_lshd (&t1, B) != MP_OKAY)
2003-05-29 09:35:26 -04:00
goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
2003-02-28 11:08:34 -05:00
if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
2003-05-29 09:35:26 -04:00
goto X1X1; /* x1x1 = x1x1 << 2*B */
2003-02-28 11:08:34 -05:00
if (mp_add (&x0x0, &t1, &t1) != MP_OKAY)
2003-05-29 09:35:26 -04:00
goto X1X1; /* t1 = x0x0 + t1 */
2003-02-28 11:08:34 -05:00
if (mp_add (&t1, &x1x1, b) != MP_OKAY)
2003-05-29 09:35:26 -04:00
goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
2003-02-28 11:08:34 -05:00
err = MP_OKAY;
X1X1:mp_clear (&x1x1);
X0X0:mp_clear (&x0x0);
T2:mp_clear (&t2);
T1:mp_clear (&t1);
X1:mp_clear (&x1);
X0:mp_clear (&x0);
ERR:
return err;
}
2004-10-29 18:07:18 -04:00
#endif
2005-08-01 12:37:28 -04:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */