Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / dropbear / libtommath / bn_mp_toom_mul.c
blobfa29078f98aabe70f47eea8a4fd4d1813e643063
1 #include <tommath.h>
2 #ifdef BN_MP_TOOM_MUL_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
13 * guarantee it works.
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
18 /* multiplication using the Toom-Cook 3-way algorithm
20 * Much more complicated than Karatsuba but has a lower
21 * asymptotic running time of O(N**1.464). This algorithm is
22 * only particularly useful on VERY large inputs
23 * (we're talking 1000s of digits here...).
25 int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
27 mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
28 int res, B;
30 /* init temps */
31 if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4,
32 &a0, &a1, &a2, &b0, &b1,
33 &b2, &tmp1, &tmp2, NULL)) != MP_OKAY) {
34 return res;
37 /* B */
38 B = MIN(a->used, b->used) / 3;
40 /* a = a2 * B**2 + a1 * B + a0 */
41 if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
42 goto ERR;
45 if ((res = mp_copy(a, &a1)) != MP_OKAY) {
46 goto ERR;
48 mp_rshd(&a1, B);
49 mp_mod_2d(&a1, DIGIT_BIT * B, &a1);
51 if ((res = mp_copy(a, &a2)) != MP_OKAY) {
52 goto ERR;
54 mp_rshd(&a2, B*2);
56 /* b = b2 * B**2 + b1 * B + b0 */
57 if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) {
58 goto ERR;
61 if ((res = mp_copy(b, &b1)) != MP_OKAY) {
62 goto ERR;
64 mp_rshd(&b1, B);
65 mp_mod_2d(&b1, DIGIT_BIT * B, &b1);
67 if ((res = mp_copy(b, &b2)) != MP_OKAY) {
68 goto ERR;
70 mp_rshd(&b2, B*2);
72 /* w0 = a0*b0 */
73 if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) {
74 goto ERR;
77 /* w4 = a2 * b2 */
78 if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) {
79 goto ERR;
82 /* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */
83 if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
84 goto ERR;
86 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
87 goto ERR;
89 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
90 goto ERR;
92 if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
93 goto ERR;
96 if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) {
97 goto ERR;
99 if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) {
100 goto ERR;
102 if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) {
103 goto ERR;
105 if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) {
106 goto ERR;
109 if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) {
110 goto ERR;
113 /* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */
114 if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
115 goto ERR;
117 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
118 goto ERR;
120 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
121 goto ERR;
123 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
124 goto ERR;
127 if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) {
128 goto ERR;
130 if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) {
131 goto ERR;
133 if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) {
134 goto ERR;
136 if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
137 goto ERR;
140 if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) {
141 goto ERR;
145 /* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */
146 if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
147 goto ERR;
149 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
150 goto ERR;
152 if ((res = mp_add(&b2, &b1, &tmp2)) != MP_OKAY) {
153 goto ERR;
155 if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
156 goto ERR;
158 if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) {
159 goto ERR;
162 /* now solve the matrix
164 0 0 0 0 1
165 1 2 4 8 16
166 1 1 1 1 1
167 16 8 4 2 1
168 1 0 0 0 0
170 using 12 subtractions, 4 shifts,
171 2 small divisions and 1 small multiplication
174 /* r1 - r4 */
175 if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
176 goto ERR;
178 /* r3 - r0 */
179 if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
180 goto ERR;
182 /* r1/2 */
183 if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
184 goto ERR;
186 /* r3/2 */
187 if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
188 goto ERR;
190 /* r2 - r0 - r4 */
191 if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
192 goto ERR;
194 if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
195 goto ERR;
197 /* r1 - r2 */
198 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
199 goto ERR;
201 /* r3 - r2 */
202 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
203 goto ERR;
205 /* r1 - 8r0 */
206 if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
207 goto ERR;
209 if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
210 goto ERR;
212 /* r3 - 8r4 */
213 if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
214 goto ERR;
216 if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
217 goto ERR;
219 /* 3r2 - r1 - r3 */
220 if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
221 goto ERR;
223 if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
224 goto ERR;
226 if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
227 goto ERR;
229 /* r1 - r2 */
230 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
231 goto ERR;
233 /* r3 - r2 */
234 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
235 goto ERR;
237 /* r1/3 */
238 if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
239 goto ERR;
241 /* r3/3 */
242 if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
243 goto ERR;
246 /* at this point shift W[n] by B*n */
247 if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
248 goto ERR;
250 if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
251 goto ERR;
253 if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
254 goto ERR;
256 if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
257 goto ERR;
260 if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) {
261 goto ERR;
263 if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
264 goto ERR;
266 if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
267 goto ERR;
269 if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) {
270 goto ERR;
273 ERR:
274 mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
275 &a0, &a1, &a2, &b0, &b1,
276 &b2, &tmp1, &tmp2, NULL);
277 return res;
280 #endif
282 /* $Source: /cvs/libtom/libtommath/bn_mp_toom_mul.c,v $ */
283 /* $Revision: 1.3 $ */
284 /* $Date: 2006/03/31 14:18:44 $ */