in AWSCognitoIdentityProvider/Internal/JKBigInteger/LibTomMath/tommath.c [977:1074]
int aws_mp_karatsuba_sqr(aws_mp_int *a, aws_mp_int *b)
{
aws_mp_int x0, x1, t1, t2, x0x0, x1x1;
int B, err;
err = AWS_MP_MEM;
/* min # of digits */
B = a->used;
/* now divide in two */
B = B >> 1;
/* init copy all the temps */
if (aws_mp_init_size(&x0, B) != AWS_MP_OKAY)
goto ERR;
if (aws_mp_init_size(&x1, a->used - B) != AWS_MP_OKAY)
goto X0;
/* init temps */
if (aws_mp_init_size(&t1, a->used * 2) != AWS_MP_OKAY)
goto X1;
if (aws_mp_init_size(&t2, a->used * 2) != AWS_MP_OKAY)
goto T1;
if (aws_mp_init_size(&x0x0, B * 2) != AWS_MP_OKAY)
goto T2;
if (aws_mp_init_size(&x1x1, (a->used - B) * 2) != AWS_MP_OKAY)
goto X0X0;
{
register int x;
register aws_mp_digit *dst, *src;
src = a->dp;
/* now shift the digits */
dst = x0.dp;
for (x = 0; x < B; x++) {
*dst++ = *src++;
}
dst = x1.dp;
for (x = B; x < a->used; x++) {
*dst++ = *src++;
}
}
x0.used = B;
x1.used = a->used - B;
aws_mp_clamp(&x0);
/* now calc the products x0*x0 and x1*x1 */
if (aws_mp_sqr(&x0, &x0x0) != AWS_MP_OKAY)
goto X1X1; /* x0x0 = x0*x0 */
if (aws_mp_sqr(&x1, &x1x1) != AWS_MP_OKAY)
goto X1X1; /* x1x1 = x1*x1 */
/* now calc (x1+x0)**2 */
if (aws_s_mp_add(&x1, &x0, &t1) != AWS_MP_OKAY)
goto X1X1; /* t1 = x1 - x0 */
if (aws_mp_sqr(&t1, &t1) != AWS_MP_OKAY)
goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
/* add x0y0 */
if (aws_s_mp_add(&x0x0, &x1x1, &t2) != AWS_MP_OKAY)
goto X1X1; /* t2 = x0x0 + x1x1 */
if (aws_s_mp_sub(&t1, &t2, &t1) != AWS_MP_OKAY)
goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
/* shift by B */
if (aws_mp_lshd(&t1, B) != AWS_MP_OKAY)
goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
if (aws_mp_lshd(&x1x1, B * 2) != AWS_MP_OKAY)
goto X1X1; /* x1x1 = x1x1 << 2*B */
if (aws_mp_add(&x0x0, &t1, &t1) != AWS_MP_OKAY)
goto X1X1; /* t1 = x0x0 + t1 */
if (aws_mp_add(&t1, &x1x1, b) != AWS_MP_OKAY)
goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
err = AWS_MP_OKAY;
X1X1:
aws_mp_clear(&x1x1);
X0X0:
aws_mp_clear(&x0x0);
T2:
aws_mp_clear(&t2);
T1:
aws_mp_clear(&t1);
X1:
aws_mp_clear(&x1);
X0:
aws_mp_clear(&x0);
ERR:
return err;
}