#include "tommath_private.h"
#ifdef BN_MP_TOOM_SQR_C
int
mp_toom_sqr(
const
mp_int *a, mp_int *b)
{
mp_int w0, w1, w2, w3, w4, tmp1, a0, a1, a2;
int
res, B;
if
((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL)) != MP_OKAY) {
return
res;
}
B = a->used / 3;
if
((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_copy(a, &a1)) != MP_OKAY) {
goto
LBL_ERR;
}
mp_rshd(&a1, B);
if
((res = mp_mod_2d(&a1, DIGIT_BIT * B, &a1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_copy(a, &a2)) != MP_OKAY) {
goto
LBL_ERR;
}
mp_rshd(&a2, B*2);
if
((res = mp_sqr(&a0, &w0)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sqr(&a2, &w4)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sqr(&tmp1, &w1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sqr(&tmp1, &w3)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sqr(&tmp1, &w2)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_mul_d(&w2, 3uL, &w2)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_add(&w0, &w1, b)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
goto
LBL_ERR;
}
if
((res = mp_add(&tmp1, b, b)) != MP_OKAY) {
goto
LBL_ERR;
}
LBL_ERR:
mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
return
res;
}
#endif