2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-30 16:26:27 +00:00
|
|
|
#ifdef S_MP_MUL_TOOM_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2003-08-05 01:24:44 +00:00
|
|
|
|
2015-11-12 00:18:15 +00:00
|
|
|
/* multiplication using the Toom-Cook 3-way algorithm
|
2004-10-29 22:07:18 +00:00
|
|
|
*
|
2015-11-12 00:18:15 +00:00
|
|
|
* Much more complicated than Karatsuba but has a lower
|
|
|
|
* asymptotic running time of O(N**1.464). This algorithm is
|
|
|
|
* only particularly useful on VERY large inputs
|
2005-03-12 11:55:11 +00:00
|
|
|
* (we're talking 1000s of digits here...).
|
2004-10-29 22:07:18 +00:00
|
|
|
*/
|
2019-05-16 21:51:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
This file contains code from J. Arndt's book "Matters Computational"
|
|
|
|
and the accompanying FXT-library with permission of the author.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Setup from
|
|
|
|
|
|
|
|
Chung, Jaewook, and M. Anwar Hasan. "Asymmetric squaring formulae."
|
|
|
|
18th IEEE Symposium on Computer Arithmetic (ARITH'07). IEEE, 2007.
|
|
|
|
|
|
|
|
The interpolation from above needed one temporary variable more
|
|
|
|
than the interpolation here:
|
|
|
|
|
|
|
|
Bodrato, Marco, and Alberto Zanoni. "What about Toom-Cook matrices optimality."
|
|
|
|
Centro Vito Volterra Universita di Roma Tor Vergata (2006)
|
|
|
|
*/
|
|
|
|
|
2019-10-30 16:26:27 +00:00
|
|
|
mp_err s_mp_mul_toom(const mp_int *a, const mp_int *b, mp_int *c)
|
2003-08-05 01:24:44 +00:00
|
|
|
{
|
2019-05-16 21:51:02 +00:00
|
|
|
mp_int S1, S2, T1, a0, a1, a2, b0, b1, b2;
|
2019-10-29 20:48:50 +00:00
|
|
|
int B;
|
2019-10-19 15:14:46 +00:00
|
|
|
mp_err err;
|
2015-11-12 00:18:15 +00:00
|
|
|
|
2017-08-30 17:15:27 +00:00
|
|
|
/* init temps */
|
2019-05-16 21:51:02 +00:00
|
|
|
if ((err = mp_init_multi(&S1, &S2, &T1, NULL)) != MP_OKAY) {
|
2019-05-19 15:16:13 +00:00
|
|
|
return err;
|
2017-08-30 17:15:27 +00:00
|
|
|
}
|
2015-11-12 00:18:15 +00:00
|
|
|
|
2017-08-30 17:15:27 +00:00
|
|
|
/* B */
|
2019-04-09 09:08:26 +00:00
|
|
|
B = MP_MIN(a->used, b->used) / 3;
|
2015-11-12 00:18:15 +00:00
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** a = a2 * x^2 + a1 * x + a0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_init_size(&a0, B)) != MP_OKAY) goto LBL_ERRa0;
|
2019-10-29 20:48:50 +00:00
|
|
|
if ((err = mp_init_size(&a1, B)) != MP_OKAY) goto LBL_ERRa1;
|
|
|
|
if ((err = mp_init_size(&a2, a->used - 2 * B)) != MP_OKAY) goto LBL_ERRa2;
|
2019-09-04 06:40:39 +00:00
|
|
|
|
2019-10-29 20:48:50 +00:00
|
|
|
a0.used = a1.used = B;
|
|
|
|
a2.used = a->used - 2 * B;
|
|
|
|
s_mp_copy_digs(a0.dp, a->dp, a0.used);
|
|
|
|
s_mp_copy_digs(a1.dp, a->dp + B, a1.used);
|
|
|
|
s_mp_copy_digs(a2.dp, a->dp + 2 * B, a2.used);
|
2019-05-16 21:51:02 +00:00
|
|
|
mp_clamp(&a0);
|
|
|
|
mp_clamp(&a1);
|
2019-10-02 03:47:04 +00:00
|
|
|
mp_clamp(&a2);
|
2003-08-05 01:24:44 +00:00
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** b = b2 * x^2 + b1 * x + b0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_init_size(&b0, B)) != MP_OKAY) goto LBL_ERRb0;
|
|
|
|
if ((err = mp_init_size(&b1, B)) != MP_OKAY) goto LBL_ERRb1;
|
2019-10-29 20:48:50 +00:00
|
|
|
if ((err = mp_init_size(&b2, b->used - 2 * B)) != MP_OKAY) goto LBL_ERRb2;
|
|
|
|
|
|
|
|
b0.used = b1.used = B;
|
|
|
|
b2.used = b->used - 2 * B;
|
|
|
|
s_mp_copy_digs(b0.dp, b->dp, b0.used);
|
|
|
|
s_mp_copy_digs(b1.dp, b->dp + B, b1.used);
|
|
|
|
s_mp_copy_digs(b2.dp, b->dp + 2 * B, b2.used);
|
|
|
|
mp_clamp(&b0);
|
2019-05-16 21:51:02 +00:00
|
|
|
mp_clamp(&b1);
|
2019-10-02 03:47:04 +00:00
|
|
|
mp_clamp(&b2);
|
2015-11-12 00:18:15 +00:00
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** \\ S1 = (a2+a1+a0) * (b2+b1+b0); */
|
|
|
|
/** T1 = a2 + a1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add(&a2, &a1, &T1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S2 = T1 + a0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add(&T1, &a0, &S2)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** c = b2 + b1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add(&b2, &b1, c)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S1 = c + b0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add(c, &b0, &S1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S1 = S1 * S2; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_mul(&S1, &S2, &S1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** \\S2 = (4*a2+2*a1+a0) * (4*b2+2*b1+b0); */
|
|
|
|
/** T1 = T1 + a2; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add(&T1, &a2, &T1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** T1 = T1 << 1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_mul_2(&T1, &T1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** T1 = T1 + a0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add(&T1, &a0, &T1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** c = c + b2; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add(c, &b2, c)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** c = c << 1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_mul_2(c, c)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** c = c + b0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add(c, &b0, c)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S2 = T1 * c; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_mul(&T1, c, &S2)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** \\S3 = (a2-a1+a0) * (b2-b1+b0); */
|
|
|
|
/** a1 = a2 - a1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&a2, &a1, &a1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** a1 = a1 + a0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add(&a1, &a0, &a1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** b1 = b2 - b1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&b2, &b1, &b1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** b1 = b1 + b0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add(&b1, &b0, &b1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** a1 = a1 * b1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_mul(&a1, &b1, &a1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** b1 = a2 * b2; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_mul(&a2, &b2, &b1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** \\S2 = (S2 - S3)/3; */
|
|
|
|
/** S2 = S2 - a1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&S2, &a1, &S2)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S2 = S2 / 3; \\ this is an exact division */
|
2019-11-06 15:51:51 +00:00
|
|
|
if ((err = s_mp_div_3(&S2, &S2, NULL)) != MP_OKAY) goto LBL_ERR;
|
2019-09-04 06:40:39 +00:00
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** a1 = S1 - a1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&S1, &a1, &a1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** a1 = a1 >> 1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_div_2(&a1, &a1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** a0 = a0 * b0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_mul(&a0, &b0, &a0)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S1 = S1 - a0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&S1, &a0, &S1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S2 = S2 - S1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&S2, &S1, &S2)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S2 = S2 >> 1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_div_2(&S2, &S2)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S1 = S1 - a1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&S1, &a1, &S1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S1 = S1 - b1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&S1, &b1, &S1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** T1 = b1 << 1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_mul_2(&b1, &T1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** S2 = S2 - T1; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&S2, &T1, &S2)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** a1 = a1 - S2; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&a1, &S2, &a1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
2015-11-12 00:18:15 +00:00
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** P = b1*x^4+ S2*x^3+ S1*x^2+ a1*x + a0; */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_lshd(&b1, 4 * B)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
if ((err = mp_lshd(&S2, 3 * B)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
if ((err = mp_add(&b1, &S2, &b1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
if ((err = mp_lshd(&S1, 2 * B)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
if ((err = mp_add(&b1, &S1, &b1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
if ((err = mp_lshd(&a1, 1 * B)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
if ((err = mp_add(&b1, &a1, &b1)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
if ((err = mp_add(&b1, &a0, c)) != MP_OKAY) goto LBL_ERR;
|
2015-11-12 00:18:15 +00:00
|
|
|
|
2019-05-16 21:51:02 +00:00
|
|
|
/** a * b - P */
|
2015-11-12 00:18:15 +00:00
|
|
|
|
|
|
|
|
2019-09-03 03:51:56 +00:00
|
|
|
LBL_ERR:
|
2019-05-16 21:51:02 +00:00
|
|
|
mp_clear(&b2);
|
2019-09-03 03:51:56 +00:00
|
|
|
LBL_ERRb2:
|
2019-05-16 21:51:02 +00:00
|
|
|
mp_clear(&b1);
|
2019-09-03 03:51:56 +00:00
|
|
|
LBL_ERRb1:
|
2019-05-16 21:51:02 +00:00
|
|
|
mp_clear(&b0);
|
2019-09-03 03:51:56 +00:00
|
|
|
LBL_ERRb0:
|
2019-05-16 21:51:02 +00:00
|
|
|
mp_clear(&a2);
|
2019-09-03 03:51:56 +00:00
|
|
|
LBL_ERRa2:
|
2019-05-16 21:51:02 +00:00
|
|
|
mp_clear(&a1);
|
2019-09-03 03:51:56 +00:00
|
|
|
LBL_ERRa1:
|
2019-05-16 21:51:02 +00:00
|
|
|
mp_clear(&a0);
|
2019-09-03 03:51:56 +00:00
|
|
|
LBL_ERRa0:
|
2019-05-16 21:51:02 +00:00
|
|
|
mp_clear_multi(&S1, &S2, &T1, NULL);
|
2019-05-19 15:16:13 +00:00
|
|
|
return err;
|
2015-11-12 00:18:15 +00:00
|
|
|
}
|
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|