diff options
author | beck <> | 2002-05-15 02:29:21 +0000 |
---|---|---|
committer | beck <> | 2002-05-15 02:29:21 +0000 |
commit | b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9 (patch) | |
tree | fa27cf82a1250b64ed3bf5f4a18c7354d470bbcc /src/lib/libcrypto/bn/bn_mul.c | |
parent | e471e1ea98d673597b182ea85f29e30c97cd08b5 (diff) | |
download | openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.gz openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.bz2 openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.zip |
OpenSSL 0.9.7 stable 2002 05 08 merge
Diffstat (limited to 'src/lib/libcrypto/bn/bn_mul.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_mul.c | 503 |
1 files changed, 433 insertions, 70 deletions
diff --git a/src/lib/libcrypto/bn/bn_mul.c b/src/lib/libcrypto/bn/bn_mul.c index 3e8d8b9567..41ea925b8d 100644 --- a/src/lib/libcrypto/bn/bn_mul.c +++ b/src/lib/libcrypto/bn/bn_mul.c | |||
@@ -56,10 +56,325 @@ | |||
56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
57 | */ | 57 | */ |
58 | 58 | ||
59 | #ifndef BN_DEBUG | ||
60 | # undef NDEBUG /* avoid conflicting definitions */ | ||
61 | # define NDEBUG | ||
62 | #endif | ||
63 | |||
59 | #include <stdio.h> | 64 | #include <stdio.h> |
65 | #include <assert.h> | ||
60 | #include "cryptlib.h" | 66 | #include "cryptlib.h" |
61 | #include "bn_lcl.h" | 67 | #include "bn_lcl.h" |
62 | 68 | ||
69 | #if defined(OPENSSL_NO_ASM) || !(defined(__i386) || defined(__i386__))/* Assembler implementation exists only for x86 */ | ||
70 | /* Here follows specialised variants of bn_add_words() and | ||
71 | bn_sub_words(). They have the property performing operations on | ||
72 | arrays of different sizes. The sizes of those arrays is expressed through | ||
73 | cl, which is the common length ( basicall, min(len(a),len(b)) ), and dl, | ||
74 | which is the delta between the two lengths, calculated as len(a)-len(b). | ||
75 | All lengths are the number of BN_ULONGs... For the operations that require | ||
76 | a result array as parameter, it must have the length cl+abs(dl). | ||
77 | These functions should probably end up in bn_asm.c as soon as there are | ||
78 | assembler counterparts for the systems that use assembler files. */ | ||
79 | |||
80 | BN_ULONG bn_sub_part_words(BN_ULONG *r, | ||
81 | const BN_ULONG *a, const BN_ULONG *b, | ||
82 | int cl, int dl) | ||
83 | { | ||
84 | BN_ULONG c, t; | ||
85 | |||
86 | assert(cl >= 0); | ||
87 | c = bn_sub_words(r, a, b, cl); | ||
88 | |||
89 | if (dl == 0) | ||
90 | return c; | ||
91 | |||
92 | r += cl; | ||
93 | a += cl; | ||
94 | b += cl; | ||
95 | |||
96 | if (dl < 0) | ||
97 | { | ||
98 | #ifdef BN_COUNT | ||
99 | fprintf(stderr, " bn_sub_part_words %d + %d (dl < 0, c = %d)\n", cl, dl, c); | ||
100 | #endif | ||
101 | for (;;) | ||
102 | { | ||
103 | t = b[0]; | ||
104 | r[0] = (0-t-c)&BN_MASK2; | ||
105 | if (t != 0) c=1; | ||
106 | if (++dl >= 0) break; | ||
107 | |||
108 | t = b[1]; | ||
109 | r[1] = (0-t-c)&BN_MASK2; | ||
110 | if (t != 0) c=1; | ||
111 | if (++dl >= 0) break; | ||
112 | |||
113 | t = b[2]; | ||
114 | r[2] = (0-t-c)&BN_MASK2; | ||
115 | if (t != 0) c=1; | ||
116 | if (++dl >= 0) break; | ||
117 | |||
118 | t = b[3]; | ||
119 | r[3] = (0-t-c)&BN_MASK2; | ||
120 | if (t != 0) c=1; | ||
121 | if (++dl >= 0) break; | ||
122 | |||
123 | b += 4; | ||
124 | r += 4; | ||
125 | } | ||
126 | } | ||
127 | else | ||
128 | { | ||
129 | int save_dl = dl; | ||
130 | #ifdef BN_COUNT | ||
131 | fprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c = %d)\n", cl, dl, c); | ||
132 | #endif | ||
133 | while(c) | ||
134 | { | ||
135 | t = a[0]; | ||
136 | r[0] = (t-c)&BN_MASK2; | ||
137 | if (t != 0) c=0; | ||
138 | if (--dl <= 0) break; | ||
139 | |||
140 | t = a[1]; | ||
141 | r[1] = (t-c)&BN_MASK2; | ||
142 | if (t != 0) c=0; | ||
143 | if (--dl <= 0) break; | ||
144 | |||
145 | t = a[2]; | ||
146 | r[2] = (t-c)&BN_MASK2; | ||
147 | if (t != 0) c=0; | ||
148 | if (--dl <= 0) break; | ||
149 | |||
150 | t = a[3]; | ||
151 | r[3] = (t-c)&BN_MASK2; | ||
152 | if (t != 0) c=0; | ||
153 | if (--dl <= 0) break; | ||
154 | |||
155 | save_dl = dl; | ||
156 | a += 4; | ||
157 | r += 4; | ||
158 | } | ||
159 | if (dl > 0) | ||
160 | { | ||
161 | #ifdef BN_COUNT | ||
162 | fprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c == 0)\n", cl, dl); | ||
163 | #endif | ||
164 | if (save_dl > dl) | ||
165 | { | ||
166 | switch (save_dl - dl) | ||
167 | { | ||
168 | case 1: | ||
169 | r[1] = a[1]; | ||
170 | if (--dl <= 0) break; | ||
171 | case 2: | ||
172 | r[2] = a[2]; | ||
173 | if (--dl <= 0) break; | ||
174 | case 3: | ||
175 | r[3] = a[3]; | ||
176 | if (--dl <= 0) break; | ||
177 | } | ||
178 | a += 4; | ||
179 | r += 4; | ||
180 | } | ||
181 | } | ||
182 | if (dl > 0) | ||
183 | { | ||
184 | #ifdef BN_COUNT | ||
185 | fprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, copy)\n", cl, dl); | ||
186 | #endif | ||
187 | for(;;) | ||
188 | { | ||
189 | r[0] = a[0]; | ||
190 | if (--dl <= 0) break; | ||
191 | r[1] = a[1]; | ||
192 | if (--dl <= 0) break; | ||
193 | r[2] = a[2]; | ||
194 | if (--dl <= 0) break; | ||
195 | r[3] = a[3]; | ||
196 | if (--dl <= 0) break; | ||
197 | |||
198 | a += 4; | ||
199 | r += 4; | ||
200 | } | ||
201 | } | ||
202 | } | ||
203 | return c; | ||
204 | } | ||
205 | #endif | ||
206 | |||
207 | BN_ULONG bn_add_part_words(BN_ULONG *r, | ||
208 | const BN_ULONG *a, const BN_ULONG *b, | ||
209 | int cl, int dl) | ||
210 | { | ||
211 | BN_ULONG c, l, t; | ||
212 | |||
213 | assert(cl >= 0); | ||
214 | c = bn_add_words(r, a, b, cl); | ||
215 | |||
216 | if (dl == 0) | ||
217 | return c; | ||
218 | |||
219 | r += cl; | ||
220 | a += cl; | ||
221 | b += cl; | ||
222 | |||
223 | if (dl < 0) | ||
224 | { | ||
225 | int save_dl = dl; | ||
226 | #ifdef BN_COUNT | ||
227 | fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, c = %d)\n", cl, dl, c); | ||
228 | #endif | ||
229 | while (c) | ||
230 | { | ||
231 | l=(c+b[0])&BN_MASK2; | ||
232 | c=(l < c); | ||
233 | r[0]=l; | ||
234 | if (++dl >= 0) break; | ||
235 | |||
236 | l=(c+b[1])&BN_MASK2; | ||
237 | c=(l < c); | ||
238 | r[1]=l; | ||
239 | if (++dl >= 0) break; | ||
240 | |||
241 | l=(c+b[2])&BN_MASK2; | ||
242 | c=(l < c); | ||
243 | r[2]=l; | ||
244 | if (++dl >= 0) break; | ||
245 | |||
246 | l=(c+b[3])&BN_MASK2; | ||
247 | c=(l < c); | ||
248 | r[3]=l; | ||
249 | if (++dl >= 0) break; | ||
250 | |||
251 | save_dl = dl; | ||
252 | b+=4; | ||
253 | r+=4; | ||
254 | } | ||
255 | if (dl < 0) | ||
256 | { | ||
257 | #ifdef BN_COUNT | ||
258 | fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, c == 0)\n", cl, dl); | ||
259 | #endif | ||
260 | if (save_dl < dl) | ||
261 | { | ||
262 | switch (dl - save_dl) | ||
263 | { | ||
264 | case 1: | ||
265 | r[1] = b[1]; | ||
266 | if (++dl >= 0) break; | ||
267 | case 2: | ||
268 | r[2] = b[2]; | ||
269 | if (++dl >= 0) break; | ||
270 | case 3: | ||
271 | r[3] = b[3]; | ||
272 | if (++dl >= 0) break; | ||
273 | } | ||
274 | b += 4; | ||
275 | r += 4; | ||
276 | } | ||
277 | } | ||
278 | if (dl < 0) | ||
279 | { | ||
280 | #ifdef BN_COUNT | ||
281 | fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, copy)\n", cl, dl); | ||
282 | #endif | ||
283 | for(;;) | ||
284 | { | ||
285 | r[0] = b[0]; | ||
286 | if (++dl >= 0) break; | ||
287 | r[1] = b[1]; | ||
288 | if (++dl >= 0) break; | ||
289 | r[2] = b[2]; | ||
290 | if (++dl >= 0) break; | ||
291 | r[3] = b[3]; | ||
292 | if (++dl >= 0) break; | ||
293 | |||
294 | b += 4; | ||
295 | r += 4; | ||
296 | } | ||
297 | } | ||
298 | } | ||
299 | else | ||
300 | { | ||
301 | int save_dl = dl; | ||
302 | #ifdef BN_COUNT | ||
303 | fprintf(stderr, " bn_add_part_words %d + %d (dl > 0)\n", cl, dl); | ||
304 | #endif | ||
305 | while (c) | ||
306 | { | ||
307 | t=(a[0]+c)&BN_MASK2; | ||
308 | c=(t < c); | ||
309 | r[0]=t; | ||
310 | if (--dl <= 0) break; | ||
311 | |||
312 | t=(a[1]+c)&BN_MASK2; | ||
313 | c=(t < c); | ||
314 | r[1]=t; | ||
315 | if (--dl <= 0) break; | ||
316 | |||
317 | t=(a[2]+c)&BN_MASK2; | ||
318 | c=(t < c); | ||
319 | r[2]=t; | ||
320 | if (--dl <= 0) break; | ||
321 | |||
322 | t=(a[3]+c)&BN_MASK2; | ||
323 | c=(t < c); | ||
324 | r[3]=t; | ||
325 | if (--dl <= 0) break; | ||
326 | |||
327 | save_dl = dl; | ||
328 | a+=4; | ||
329 | r+=4; | ||
330 | } | ||
331 | #ifdef BN_COUNT | ||
332 | fprintf(stderr, " bn_add_part_words %d + %d (dl > 0, c == 0)\n", cl, dl); | ||
333 | #endif | ||
334 | if (dl > 0) | ||
335 | { | ||
336 | if (save_dl > dl) | ||
337 | { | ||
338 | switch (save_dl - dl) | ||
339 | { | ||
340 | case 1: | ||
341 | r[1] = a[1]; | ||
342 | if (--dl <= 0) break; | ||
343 | case 2: | ||
344 | r[2] = a[2]; | ||
345 | if (--dl <= 0) break; | ||
346 | case 3: | ||
347 | r[3] = a[3]; | ||
348 | if (--dl <= 0) break; | ||
349 | } | ||
350 | a += 4; | ||
351 | r += 4; | ||
352 | } | ||
353 | } | ||
354 | if (dl > 0) | ||
355 | { | ||
356 | #ifdef BN_COUNT | ||
357 | fprintf(stderr, " bn_add_part_words %d + %d (dl > 0, copy)\n", cl, dl); | ||
358 | #endif | ||
359 | for(;;) | ||
360 | { | ||
361 | r[0] = a[0]; | ||
362 | if (--dl <= 0) break; | ||
363 | r[1] = a[1]; | ||
364 | if (--dl <= 0) break; | ||
365 | r[2] = a[2]; | ||
366 | if (--dl <= 0) break; | ||
367 | r[3] = a[3]; | ||
368 | if (--dl <= 0) break; | ||
369 | |||
370 | a += 4; | ||
371 | r += 4; | ||
372 | } | ||
373 | } | ||
374 | } | ||
375 | return c; | ||
376 | } | ||
377 | |||
63 | #ifdef BN_RECURSION | 378 | #ifdef BN_RECURSION |
64 | /* Karatsuba recursive multiplication algorithm | 379 | /* Karatsuba recursive multiplication algorithm |
65 | * (cf. Knuth, The Art of Computer Programming, Vol. 2) */ | 380 | * (cf. Knuth, The Art of Computer Programming, Vol. 2) */ |
@@ -75,14 +390,15 @@ | |||
75 | * a[1]*b[1] | 390 | * a[1]*b[1] |
76 | */ | 391 | */ |
77 | void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, | 392 | void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, |
78 | BN_ULONG *t) | 393 | int dna, int dnb, BN_ULONG *t) |
79 | { | 394 | { |
80 | int n=n2/2,c1,c2; | 395 | int n=n2/2,c1,c2; |
396 | int tna=n+dna, tnb=n+dnb; | ||
81 | unsigned int neg,zero; | 397 | unsigned int neg,zero; |
82 | BN_ULONG ln,lo,*p; | 398 | BN_ULONG ln,lo,*p; |
83 | 399 | ||
84 | # ifdef BN_COUNT | 400 | # ifdef BN_COUNT |
85 | printf(" bn_mul_recursive %d * %d\n",n2,n2); | 401 | fprintf(stderr," bn_mul_recursive %d * %d\n",n2,n2); |
86 | # endif | 402 | # endif |
87 | # ifdef BN_MUL_COMBA | 403 | # ifdef BN_MUL_COMBA |
88 | # if 0 | 404 | # if 0 |
@@ -105,21 +421,21 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, | |||
105 | return; | 421 | return; |
106 | } | 422 | } |
107 | /* r=(a[0]-a[1])*(b[1]-b[0]) */ | 423 | /* r=(a[0]-a[1])*(b[1]-b[0]) */ |
108 | c1=bn_cmp_words(a,&(a[n]),n); | 424 | c1=bn_cmp_part_words(a,&(a[n]),tna,n-tna); |
109 | c2=bn_cmp_words(&(b[n]),b,n); | 425 | c2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n); |
110 | zero=neg=0; | 426 | zero=neg=0; |
111 | switch (c1*3+c2) | 427 | switch (c1*3+c2) |
112 | { | 428 | { |
113 | case -4: | 429 | case -4: |
114 | bn_sub_words(t, &(a[n]),a, n); /* - */ | 430 | bn_sub_part_words(t, &(a[n]),a, tna,tna-n); /* - */ |
115 | bn_sub_words(&(t[n]),b, &(b[n]),n); /* - */ | 431 | bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); /* - */ |
116 | break; | 432 | break; |
117 | case -3: | 433 | case -3: |
118 | zero=1; | 434 | zero=1; |
119 | break; | 435 | break; |
120 | case -2: | 436 | case -2: |
121 | bn_sub_words(t, &(a[n]),a, n); /* - */ | 437 | bn_sub_part_words(t, &(a[n]),a, tna,tna-n); /* - */ |
122 | bn_sub_words(&(t[n]),&(b[n]),b, n); /* + */ | 438 | bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); /* + */ |
123 | neg=1; | 439 | neg=1; |
124 | break; | 440 | break; |
125 | case -1: | 441 | case -1: |
@@ -128,21 +444,22 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, | |||
128 | zero=1; | 444 | zero=1; |
129 | break; | 445 | break; |
130 | case 2: | 446 | case 2: |
131 | bn_sub_words(t, a, &(a[n]),n); /* + */ | 447 | bn_sub_part_words(t, a, &(a[n]),tna,n-tna); /* + */ |
132 | bn_sub_words(&(t[n]),b, &(b[n]),n); /* - */ | 448 | bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); /* - */ |
133 | neg=1; | 449 | neg=1; |
134 | break; | 450 | break; |
135 | case 3: | 451 | case 3: |
136 | zero=1; | 452 | zero=1; |
137 | break; | 453 | break; |
138 | case 4: | 454 | case 4: |
139 | bn_sub_words(t, a, &(a[n]),n); | 455 | bn_sub_part_words(t, a, &(a[n]),tna,n-tna); |
140 | bn_sub_words(&(t[n]),&(b[n]),b, n); | 456 | bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); |
141 | break; | 457 | break; |
142 | } | 458 | } |
143 | 459 | ||
144 | # ifdef BN_MUL_COMBA | 460 | # ifdef BN_MUL_COMBA |
145 | if (n == 4) | 461 | if (n == 4 && dna == 0 && dnb == 0) /* XXX: bn_mul_comba4 could take |
462 | extra args to do this well */ | ||
146 | { | 463 | { |
147 | if (!zero) | 464 | if (!zero) |
148 | bn_mul_comba4(&(t[n2]),t,&(t[n])); | 465 | bn_mul_comba4(&(t[n2]),t,&(t[n])); |
@@ -152,7 +469,9 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, | |||
152 | bn_mul_comba4(r,a,b); | 469 | bn_mul_comba4(r,a,b); |
153 | bn_mul_comba4(&(r[n2]),&(a[n]),&(b[n])); | 470 | bn_mul_comba4(&(r[n2]),&(a[n]),&(b[n])); |
154 | } | 471 | } |
155 | else if (n == 8) | 472 | else if (n == 8 && dna == 0 && dnb == 0) /* XXX: bn_mul_comba8 could |
473 | take extra args to do this | ||
474 | well */ | ||
156 | { | 475 | { |
157 | if (!zero) | 476 | if (!zero) |
158 | bn_mul_comba8(&(t[n2]),t,&(t[n])); | 477 | bn_mul_comba8(&(t[n2]),t,&(t[n])); |
@@ -167,11 +486,11 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, | |||
167 | { | 486 | { |
168 | p= &(t[n2*2]); | 487 | p= &(t[n2*2]); |
169 | if (!zero) | 488 | if (!zero) |
170 | bn_mul_recursive(&(t[n2]),t,&(t[n]),n,p); | 489 | bn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p); |
171 | else | 490 | else |
172 | memset(&(t[n2]),0,n2*sizeof(BN_ULONG)); | 491 | memset(&(t[n2]),0,n2*sizeof(BN_ULONG)); |
173 | bn_mul_recursive(r,a,b,n,p); | 492 | bn_mul_recursive(r,a,b,n,0,0,p); |
174 | bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,p); | 493 | bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,dna,dnb,p); |
175 | } | 494 | } |
176 | 495 | ||
177 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign | 496 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign |
@@ -220,39 +539,39 @@ void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, | |||
220 | 539 | ||
221 | /* n+tn is the word length | 540 | /* n+tn is the word length |
222 | * t needs to be n*4 is size, as does r */ | 541 | * t needs to be n*4 is size, as does r */ |
223 | void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int tn, | 542 | void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n, |
224 | int n, BN_ULONG *t) | 543 | int tna, int tnb, BN_ULONG *t) |
225 | { | 544 | { |
226 | int i,j,n2=n*2; | 545 | int i,j,n2=n*2; |
227 | unsigned int c1,c2,neg,zero; | 546 | unsigned int c1,c2,neg,zero; |
228 | BN_ULONG ln,lo,*p; | 547 | BN_ULONG ln,lo,*p; |
229 | 548 | ||
230 | # ifdef BN_COUNT | 549 | # ifdef BN_COUNT |
231 | printf(" bn_mul_part_recursive %d * %d\n",tn+n,tn+n); | 550 | fprintf(stderr," bn_mul_part_recursive (%d+%d) * (%d+%d)\n", |
551 | tna, n, tnb, n); | ||
232 | # endif | 552 | # endif |
233 | if (n < 8) | 553 | if (n < 8) |
234 | { | 554 | { |
235 | i=tn+n; | 555 | bn_mul_normal(r,a,n+tna,b,n+tnb); |
236 | bn_mul_normal(r,a,i,b,i); | ||
237 | return; | 556 | return; |
238 | } | 557 | } |
239 | 558 | ||
240 | /* r=(a[0]-a[1])*(b[1]-b[0]) */ | 559 | /* r=(a[0]-a[1])*(b[1]-b[0]) */ |
241 | c1=bn_cmp_words(a,&(a[n]),n); | 560 | c1=bn_cmp_part_words(a,&(a[n]),tna,n-tna); |
242 | c2=bn_cmp_words(&(b[n]),b,n); | 561 | c2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n); |
243 | zero=neg=0; | 562 | zero=neg=0; |
244 | switch (c1*3+c2) | 563 | switch (c1*3+c2) |
245 | { | 564 | { |
246 | case -4: | 565 | case -4: |
247 | bn_sub_words(t, &(a[n]),a, n); /* - */ | 566 | bn_sub_part_words(t, &(a[n]),a, tna,tna-n); /* - */ |
248 | bn_sub_words(&(t[n]),b, &(b[n]),n); /* - */ | 567 | bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); /* - */ |
249 | break; | 568 | break; |
250 | case -3: | 569 | case -3: |
251 | zero=1; | 570 | zero=1; |
252 | /* break; */ | 571 | /* break; */ |
253 | case -2: | 572 | case -2: |
254 | bn_sub_words(t, &(a[n]),a, n); /* - */ | 573 | bn_sub_part_words(t, &(a[n]),a, tna,tna-n); /* - */ |
255 | bn_sub_words(&(t[n]),&(b[n]),b, n); /* + */ | 574 | bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); /* + */ |
256 | neg=1; | 575 | neg=1; |
257 | break; | 576 | break; |
258 | case -1: | 577 | case -1: |
@@ -261,16 +580,16 @@ void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int tn, | |||
261 | zero=1; | 580 | zero=1; |
262 | /* break; */ | 581 | /* break; */ |
263 | case 2: | 582 | case 2: |
264 | bn_sub_words(t, a, &(a[n]),n); /* + */ | 583 | bn_sub_part_words(t, a, &(a[n]),tna,n-tna); /* + */ |
265 | bn_sub_words(&(t[n]),b, &(b[n]),n); /* - */ | 584 | bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); /* - */ |
266 | neg=1; | 585 | neg=1; |
267 | break; | 586 | break; |
268 | case 3: | 587 | case 3: |
269 | zero=1; | 588 | zero=1; |
270 | /* break; */ | 589 | /* break; */ |
271 | case 4: | 590 | case 4: |
272 | bn_sub_words(t, a, &(a[n]),n); | 591 | bn_sub_part_words(t, a, &(a[n]),tna,n-tna); |
273 | bn_sub_words(&(t[n]),&(b[n]),b, n); | 592 | bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); |
274 | break; | 593 | break; |
275 | } | 594 | } |
276 | /* The zero case isn't yet implemented here. The speedup | 595 | /* The zero case isn't yet implemented here. The speedup |
@@ -289,54 +608,59 @@ void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int tn, | |||
289 | { | 608 | { |
290 | bn_mul_comba8(&(t[n2]),t,&(t[n])); | 609 | bn_mul_comba8(&(t[n2]),t,&(t[n])); |
291 | bn_mul_comba8(r,a,b); | 610 | bn_mul_comba8(r,a,b); |
292 | bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn); | 611 | bn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb); |
293 | memset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2)); | 612 | memset(&(r[n2+tna+tnb]),0,sizeof(BN_ULONG)*(n2-tna-tnb)); |
294 | } | 613 | } |
295 | else | 614 | else |
296 | { | 615 | { |
297 | p= &(t[n2*2]); | 616 | p= &(t[n2*2]); |
298 | bn_mul_recursive(&(t[n2]),t,&(t[n]),n,p); | 617 | bn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p); |
299 | bn_mul_recursive(r,a,b,n,p); | 618 | bn_mul_recursive(r,a,b,n,0,0,p); |
300 | i=n/2; | 619 | i=n/2; |
301 | /* If there is only a bottom half to the number, | 620 | /* If there is only a bottom half to the number, |
302 | * just do it */ | 621 | * just do it */ |
303 | j=tn-i; | 622 | if (tna > tnb) |
623 | j = tna - i; | ||
624 | else | ||
625 | j = tnb - i; | ||
304 | if (j == 0) | 626 | if (j == 0) |
305 | { | 627 | { |
306 | bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),i,p); | 628 | bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]), |
629 | i,tna-i,tnb-i,p); | ||
307 | memset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2)); | 630 | memset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2)); |
308 | } | 631 | } |
309 | else if (j > 0) /* eg, n == 16, i == 8 and tn == 11 */ | 632 | else if (j > 0) /* eg, n == 16, i == 8 and tn == 11 */ |
310 | { | 633 | { |
311 | bn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]), | 634 | bn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]), |
312 | j,i,p); | 635 | i,tna-i,tnb-i,p); |
313 | memset(&(r[n2+tn*2]),0, | 636 | memset(&(r[n2+tna+tnb]),0, |
314 | sizeof(BN_ULONG)*(n2-tn*2)); | 637 | sizeof(BN_ULONG)*(n2-tna-tnb)); |
315 | } | 638 | } |
316 | else /* (j < 0) eg, n == 16, i == 8 and tn == 5 */ | 639 | else /* (j < 0) eg, n == 16, i == 8 and tn == 5 */ |
317 | { | 640 | { |
318 | memset(&(r[n2]),0,sizeof(BN_ULONG)*n2); | 641 | memset(&(r[n2]),0,sizeof(BN_ULONG)*n2); |
319 | if (tn < BN_MUL_RECURSIVE_SIZE_NORMAL) | 642 | if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL |
643 | && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) | ||
320 | { | 644 | { |
321 | bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn); | 645 | bn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb); |
322 | } | 646 | } |
323 | else | 647 | else |
324 | { | 648 | { |
325 | for (;;) | 649 | for (;;) |
326 | { | 650 | { |
327 | i/=2; | 651 | i/=2; |
328 | if (i < tn) | 652 | if (i < tna && i < tnb) |
329 | { | 653 | { |
330 | bn_mul_part_recursive(&(r[n2]), | 654 | bn_mul_part_recursive(&(r[n2]), |
331 | &(a[n]),&(b[n]), | 655 | &(a[n]),&(b[n]), |
332 | tn-i,i,p); | 656 | i,tna-i,tnb-i,p); |
333 | break; | 657 | break; |
334 | } | 658 | } |
335 | else if (i == tn) | 659 | else if (i <= tna && i <= tnb) |
336 | { | 660 | { |
337 | bn_mul_recursive(&(r[n2]), | 661 | bn_mul_recursive(&(r[n2]), |
338 | &(a[n]),&(b[n]), | 662 | &(a[n]),&(b[n]), |
339 | i,p); | 663 | i,tna-i,tnb-i,p); |
340 | break; | 664 | break; |
341 | } | 665 | } |
342 | } | 666 | } |
@@ -397,10 +721,10 @@ void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, | |||
397 | int n=n2/2; | 721 | int n=n2/2; |
398 | 722 | ||
399 | # ifdef BN_COUNT | 723 | # ifdef BN_COUNT |
400 | printf(" bn_mul_low_recursive %d * %d\n",n2,n2); | 724 | fprintf(stderr," bn_mul_low_recursive %d * %d\n",n2,n2); |
401 | # endif | 725 | # endif |
402 | 726 | ||
403 | bn_mul_recursive(r,a,b,n,&(t[0])); | 727 | bn_mul_recursive(r,a,b,n,0,0,&(t[0])); |
404 | if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) | 728 | if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) |
405 | { | 729 | { |
406 | bn_mul_low_recursive(&(t[0]),&(a[0]),&(b[n]),n,&(t[n2])); | 730 | bn_mul_low_recursive(&(t[0]),&(a[0]),&(b[n]),n,&(t[n2])); |
@@ -431,7 +755,7 @@ void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2, | |||
431 | BN_ULONG ll,lc,*lp,*mp; | 755 | BN_ULONG ll,lc,*lp,*mp; |
432 | 756 | ||
433 | # ifdef BN_COUNT | 757 | # ifdef BN_COUNT |
434 | printf(" bn_mul_high %d * %d\n",n2,n2); | 758 | fprintf(stderr," bn_mul_high %d * %d\n",n2,n2); |
435 | # endif | 759 | # endif |
436 | n=n2/2; | 760 | n=n2/2; |
437 | 761 | ||
@@ -484,8 +808,8 @@ void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2, | |||
484 | else | 808 | else |
485 | # endif | 809 | # endif |
486 | { | 810 | { |
487 | bn_mul_recursive(&(t[0]),&(r[0]),&(r[n]),n,&(t[n2])); | 811 | bn_mul_recursive(&(t[0]),&(r[0]),&(r[n]),n,0,0,&(t[n2])); |
488 | bn_mul_recursive(r,&(a[n]),&(b[n]),n,&(t[n2])); | 812 | bn_mul_recursive(r,&(a[n]),&(b[n]),n,0,0,&(t[n2])); |
489 | } | 813 | } |
490 | 814 | ||
491 | /* s0 == low(al*bl) | 815 | /* s0 == low(al*bl) |
@@ -608,21 +932,21 @@ void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2, | |||
608 | } | 932 | } |
609 | #endif /* BN_RECURSION */ | 933 | #endif /* BN_RECURSION */ |
610 | 934 | ||
611 | int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx) | 935 | int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) |
612 | { | 936 | { |
937 | int ret=0; | ||
613 | int top,al,bl; | 938 | int top,al,bl; |
614 | BIGNUM *rr; | 939 | BIGNUM *rr; |
615 | int ret = 0; | ||
616 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) | 940 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) |
617 | int i; | 941 | int i; |
618 | #endif | 942 | #endif |
619 | #ifdef BN_RECURSION | 943 | #ifdef BN_RECURSION |
620 | BIGNUM *t; | 944 | BIGNUM *t=NULL; |
621 | int j,k; | 945 | int j=0,k; |
622 | #endif | 946 | #endif |
623 | 947 | ||
624 | #ifdef BN_COUNT | 948 | #ifdef BN_COUNT |
625 | printf("BN_mul %d * %d\n",a->top,b->top); | 949 | fprintf(stderr,"BN_mul %d * %d\n",a->top,b->top); |
626 | #endif | 950 | #endif |
627 | 951 | ||
628 | bn_check_top(a); | 952 | bn_check_top(a); |
@@ -675,17 +999,55 @@ int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx) | |||
675 | #ifdef BN_RECURSION | 999 | #ifdef BN_RECURSION |
676 | if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) | 1000 | if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) |
677 | { | 1001 | { |
1002 | if (i >= -1 && i <= 1) | ||
1003 | { | ||
1004 | int sav_j =0; | ||
1005 | /* Find out the power of two lower or equal | ||
1006 | to the longest of the two numbers */ | ||
1007 | if (i >= 0) | ||
1008 | { | ||
1009 | j = BN_num_bits_word((BN_ULONG)al); | ||
1010 | } | ||
1011 | if (i == -1) | ||
1012 | { | ||
1013 | j = BN_num_bits_word((BN_ULONG)bl); | ||
1014 | } | ||
1015 | sav_j = j; | ||
1016 | j = 1<<(j-1); | ||
1017 | assert(j <= al || j <= bl); | ||
1018 | k = j+j; | ||
1019 | t = BN_CTX_get(ctx); | ||
1020 | if (al > j || bl > j) | ||
1021 | { | ||
1022 | bn_wexpand(t,k*4); | ||
1023 | bn_wexpand(rr,k*4); | ||
1024 | bn_mul_part_recursive(rr->d,a->d,b->d, | ||
1025 | j,al-j,bl-j,t->d); | ||
1026 | } | ||
1027 | else /* al <= j || bl <= j */ | ||
1028 | { | ||
1029 | bn_wexpand(t,k*2); | ||
1030 | bn_wexpand(rr,k*2); | ||
1031 | bn_mul_recursive(rr->d,a->d,b->d, | ||
1032 | j,al-j,bl-j,t->d); | ||
1033 | } | ||
1034 | rr->top=top; | ||
1035 | goto end; | ||
1036 | } | ||
1037 | #if 0 | ||
678 | if (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA)) | 1038 | if (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA)) |
679 | { | 1039 | { |
680 | bn_wexpand(b,al); | 1040 | BIGNUM *tmp_bn = (BIGNUM *)b; |
681 | b->d[bl]=0; | 1041 | bn_wexpand(tmp_bn,al); |
1042 | tmp_bn->d[bl]=0; | ||
682 | bl++; | 1043 | bl++; |
683 | i--; | 1044 | i--; |
684 | } | 1045 | } |
685 | else if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA)) | 1046 | else if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA)) |
686 | { | 1047 | { |
687 | bn_wexpand(a,bl); | 1048 | BIGNUM *tmp_bn = (BIGNUM *)a; |
688 | a->d[al]=0; | 1049 | bn_wexpand(tmp_bn,bl); |
1050 | tmp_bn->d[al]=0; | ||
689 | al++; | 1051 | al++; |
690 | i++; | 1052 | i++; |
691 | } | 1053 | } |
@@ -705,19 +1067,14 @@ int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx) | |||
705 | } | 1067 | } |
706 | else | 1068 | else |
707 | { | 1069 | { |
708 | bn_wexpand(a,k); | ||
709 | bn_wexpand(b,k); | ||
710 | bn_wexpand(t,k*4); | 1070 | bn_wexpand(t,k*4); |
711 | bn_wexpand(rr,k*4); | 1071 | bn_wexpand(rr,k*4); |
712 | for (i=a->top; i<k; i++) | ||
713 | a->d[i]=0; | ||
714 | for (i=b->top; i<k; i++) | ||
715 | b->d[i]=0; | ||
716 | bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d); | 1072 | bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d); |
717 | } | 1073 | } |
718 | rr->top=top; | 1074 | rr->top=top; |
719 | goto end; | 1075 | goto end; |
720 | } | 1076 | } |
1077 | #endif | ||
721 | } | 1078 | } |
722 | #endif /* BN_RECURSION */ | 1079 | #endif /* BN_RECURSION */ |
723 | if (bn_wexpand(rr,top) == NULL) goto err; | 1080 | if (bn_wexpand(rr,top) == NULL) goto err; |
@@ -740,7 +1097,7 @@ void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) | |||
740 | BN_ULONG *rr; | 1097 | BN_ULONG *rr; |
741 | 1098 | ||
742 | #ifdef BN_COUNT | 1099 | #ifdef BN_COUNT |
743 | printf(" bn_mul_normal %d * %d\n",na,nb); | 1100 | fprintf(stderr," bn_mul_normal %d * %d\n",na,nb); |
744 | #endif | 1101 | #endif |
745 | 1102 | ||
746 | if (na < nb) | 1103 | if (na < nb) |
@@ -753,7 +1110,13 @@ void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) | |||
753 | 1110 | ||
754 | } | 1111 | } |
755 | rr= &(r[na]); | 1112 | rr= &(r[na]); |
756 | rr[0]=bn_mul_words(r,a,na,b[0]); | 1113 | if (nb <= 0) |
1114 | { | ||
1115 | (void)bn_mul_words(r,a,na,0); | ||
1116 | return; | ||
1117 | } | ||
1118 | else | ||
1119 | rr[0]=bn_mul_words(r,a,na,b[0]); | ||
757 | 1120 | ||
758 | for (;;) | 1121 | for (;;) |
759 | { | 1122 | { |
@@ -774,7 +1137,7 @@ void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) | |||
774 | void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n) | 1137 | void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n) |
775 | { | 1138 | { |
776 | #ifdef BN_COUNT | 1139 | #ifdef BN_COUNT |
777 | printf(" bn_mul_low_normal %d * %d\n",n,n); | 1140 | fprintf(stderr," bn_mul_low_normal %d * %d\n",n,n); |
778 | #endif | 1141 | #endif |
779 | bn_mul_words(r,a,n,b[0]); | 1142 | bn_mul_words(r,a,n,b[0]); |
780 | 1143 | ||