1 | /* crypto/bn/bn_mul.c */
|
---|
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * This package is an SSL implementation written
|
---|
6 | * by Eric Young (eay@cryptsoft.com).
|
---|
7 | * The implementation was written so as to conform with Netscapes SSL.
|
---|
8 | *
|
---|
9 | * This library is free for commercial and non-commercial use as long as
|
---|
10 | * the following conditions are aheared to. The following conditions
|
---|
11 | * apply to all code found in this distribution, be it the RC4, RSA,
|
---|
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
---|
13 | * included with this distribution is covered by the same copyright terms
|
---|
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
---|
15 | *
|
---|
16 | * Copyright remains Eric Young's, and as such any Copyright notices in
|
---|
17 | * the code are not to be removed.
|
---|
18 | * If this package is used in a product, Eric Young should be given attribution
|
---|
19 | * as the author of the parts of the library used.
|
---|
20 | * This can be in the form of a textual message at program startup or
|
---|
21 | * in documentation (online or textual) provided with the package.
|
---|
22 | *
|
---|
23 | * Redistribution and use in source and binary forms, with or without
|
---|
24 | * modification, are permitted provided that the following conditions
|
---|
25 | * are met:
|
---|
26 | * 1. Redistributions of source code must retain the copyright
|
---|
27 | * notice, this list of conditions and the following disclaimer.
|
---|
28 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
29 | * notice, this list of conditions and the following disclaimer in the
|
---|
30 | * documentation and/or other materials provided with the distribution.
|
---|
31 | * 3. All advertising materials mentioning features or use of this software
|
---|
32 | * must display the following acknowledgement:
|
---|
33 | * "This product includes cryptographic software written by
|
---|
34 | * Eric Young (eay@cryptsoft.com)"
|
---|
35 | * The word 'cryptographic' can be left out if the rouines from the library
|
---|
36 | * being used are not cryptographic related :-).
|
---|
37 | * 4. If you include any Windows specific code (or a derivative thereof) from
|
---|
38 | * the apps directory (application code) you must include an acknowledgement:
|
---|
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
---|
40 | *
|
---|
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
---|
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
---|
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
51 | * SUCH DAMAGE.
|
---|
52 | *
|
---|
53 | * The licence and distribution terms for any publically available version or
|
---|
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be
|
---|
55 | * copied and put under another distribution licence
|
---|
56 | * [including the GNU Public Licence.]
|
---|
57 | */
|
---|
58 |
|
---|
59 | #include <stdio.h>
|
---|
60 | #include <string.h>
|
---|
61 | #include "bn_lcl.h"
|
---|
62 | #include "openssl_mods.h"
|
---|
63 |
|
---|
64 | #ifdef BN_RECURSION
|
---|
65 | /* Karatsuba recursive multiplication algorithm
|
---|
66 | * (cf. Knuth, The Art of Computer Programming, Vol. 2) */
|
---|
67 |
|
---|
68 | /* r is 2*n2 words in size,
|
---|
69 | * a and b are both n2 words in size.
|
---|
70 | * n2 must be a power of 2.
|
---|
71 | * We multiply and return the result.
|
---|
72 | * t must be 2*n2 words in size
|
---|
73 | * We calculate
|
---|
74 | * a[0]*b[0]
|
---|
75 | * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
|
---|
76 | * a[1]*b[1]
|
---|
77 | */
|
---|
78 | void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
|
---|
79 | BN_ULONG *t)
|
---|
80 | {
|
---|
81 | int n=n2/2,c1,c2;
|
---|
82 | unsigned int neg,zero;
|
---|
83 | BN_ULONG ln,lo,*p;
|
---|
84 |
|
---|
85 | # ifdef BN_COUNT
|
---|
86 | printf(" bn_mul_recursive %d * %d\n",n2,n2);
|
---|
87 | # endif
|
---|
88 | # ifdef BN_MUL_COMBA
|
---|
89 | # if 0
|
---|
90 | if (n2 == 4)
|
---|
91 | {
|
---|
92 | bn_mul_comba4(r,a,b);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 | # endif
|
---|
96 | if (n2 == 8)
|
---|
97 | {
|
---|
98 | bn_mul_comba8(r,a,b);
|
---|
99 | return;
|
---|
100 | }
|
---|
101 | # endif /* BN_MUL_COMBA */
|
---|
102 | if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)
|
---|
103 | {
|
---|
104 | /* This should not happen */
|
---|
105 | bn_mul_normal(r,a,n2,b,n2);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | /* r=(a[0]-a[1])*(b[1]-b[0]) */
|
---|
109 | c1=bn_cmp_words(a,&(a[n]),n);
|
---|
110 | c2=bn_cmp_words(&(b[n]),b,n);
|
---|
111 | zero=neg=0;
|
---|
112 | switch (c1*3+c2)
|
---|
113 | {
|
---|
114 | case -4:
|
---|
115 | bn_sub_words(t, &(a[n]),a, n); /* - */
|
---|
116 | bn_sub_words(&(t[n]),b, &(b[n]),n); /* - */
|
---|
117 | break;
|
---|
118 | case -3:
|
---|
119 | zero=1;
|
---|
120 | break;
|
---|
121 | case -2:
|
---|
122 | bn_sub_words(t, &(a[n]),a, n); /* - */
|
---|
123 | bn_sub_words(&(t[n]),&(b[n]),b, n); /* + */
|
---|
124 | neg=1;
|
---|
125 | break;
|
---|
126 | case -1:
|
---|
127 | case 0:
|
---|
128 | case 1:
|
---|
129 | zero=1;
|
---|
130 | break;
|
---|
131 | case 2:
|
---|
132 | bn_sub_words(t, a, &(a[n]),n); /* + */
|
---|
133 | bn_sub_words(&(t[n]),b, &(b[n]),n); /* - */
|
---|
134 | neg=1;
|
---|
135 | break;
|
---|
136 | case 3:
|
---|
137 | zero=1;
|
---|
138 | break;
|
---|
139 | case 4:
|
---|
140 | bn_sub_words(t, a, &(a[n]),n);
|
---|
141 | bn_sub_words(&(t[n]),&(b[n]),b, n);
|
---|
142 | break;
|
---|
143 | }
|
---|
144 |
|
---|
145 | # ifdef BN_MUL_COMBA
|
---|
146 | if (n == 4)
|
---|
147 | {
|
---|
148 | if (!zero)
|
---|
149 | bn_mul_comba4(&(t[n2]),t,&(t[n]));
|
---|
150 | else
|
---|
151 | memset(&(t[n2]),0,8*sizeof(BN_ULONG));
|
---|
152 |
|
---|
153 | bn_mul_comba4(r,a,b);
|
---|
154 | bn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));
|
---|
155 | }
|
---|
156 | else if (n == 8)
|
---|
157 | {
|
---|
158 | if (!zero)
|
---|
159 | bn_mul_comba8(&(t[n2]),t,&(t[n]));
|
---|
160 | else
|
---|
161 | memset(&(t[n2]),0,16*sizeof(BN_ULONG));
|
---|
162 |
|
---|
163 | bn_mul_comba8(r,a,b);
|
---|
164 | bn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));
|
---|
165 | }
|
---|
166 | else
|
---|
167 | # endif /* BN_MUL_COMBA */
|
---|
168 | {
|
---|
169 | p= &(t[n2*2]);
|
---|
170 | if (!zero)
|
---|
171 | bn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);
|
---|
172 | else
|
---|
173 | memset(&(t[n2]),0,n2*sizeof(BN_ULONG));
|
---|
174 | bn_mul_recursive(r,a,b,n,p);
|
---|
175 | bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,p);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
|
---|
179 | * r[10] holds (a[0]*b[0])
|
---|
180 | * r[32] holds (b[1]*b[1])
|
---|
181 | */
|
---|
182 |
|
---|
183 | c1=(int)(bn_add_words(t,r,&(r[n2]),n2));
|
---|
184 |
|
---|
185 | if (neg) /* if t[32] is negative */
|
---|
186 | {
|
---|
187 | c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | /* Might have a carry */
|
---|
192 | c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));
|
---|
193 | }
|
---|
194 |
|
---|
195 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
|
---|
196 | * r[10] holds (a[0]*b[0])
|
---|
197 | * r[32] holds (b[1]*b[1])
|
---|
198 | * c1 holds the carry bits
|
---|
199 | */
|
---|
200 | c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));
|
---|
201 | if (c1)
|
---|
202 | {
|
---|
203 | p= &(r[n+n2]);
|
---|
204 | lo= *p;
|
---|
205 | ln=(lo+c1)&BN_MASK2;
|
---|
206 | *p=ln;
|
---|
207 |
|
---|
208 | /* The overflow will stop before we over write
|
---|
209 | * words we should not overwrite */
|
---|
210 | if (ln < (BN_ULONG)c1)
|
---|
211 | {
|
---|
212 | do {
|
---|
213 | p++;
|
---|
214 | lo= *p;
|
---|
215 | ln=(lo+1)&BN_MASK2;
|
---|
216 | *p=ln;
|
---|
217 | } while (ln == 0);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | /* n+tn is the word length
|
---|
223 | * t needs to be n*4 is size, as does r */
|
---|
224 | void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int tn,
|
---|
225 | int n, BN_ULONG *t)
|
---|
226 | {
|
---|
227 | int i,j,n2=n*2;
|
---|
228 | unsigned int c1,c2,neg,zero;
|
---|
229 | BN_ULONG ln,lo,*p;
|
---|
230 |
|
---|
231 | # ifdef BN_COUNT
|
---|
232 | printf(" bn_mul_part_recursive %d * %d\n",tn+n,tn+n);
|
---|
233 | # endif
|
---|
234 | if (n < 8)
|
---|
235 | {
|
---|
236 | i=tn+n;
|
---|
237 | bn_mul_normal(r,a,i,b,i);
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /* r=(a[0]-a[1])*(b[1]-b[0]) */
|
---|
242 | c1=bn_cmp_words(a,&(a[n]),n);
|
---|
243 | c2=bn_cmp_words(&(b[n]),b,n);
|
---|
244 | zero=neg=0;
|
---|
245 | switch (c1*3+c2)
|
---|
246 | {
|
---|
247 | case -4:
|
---|
248 | bn_sub_words(t, &(a[n]),a, n); /* - */
|
---|
249 | bn_sub_words(&(t[n]),b, &(b[n]),n); /* - */
|
---|
250 | break;
|
---|
251 | case -3:
|
---|
252 | zero=1;
|
---|
253 | /* break; */
|
---|
254 | case -2:
|
---|
255 | bn_sub_words(t, &(a[n]),a, n); /* - */
|
---|
256 | bn_sub_words(&(t[n]),&(b[n]),b, n); /* + */
|
---|
257 | neg=1;
|
---|
258 | break;
|
---|
259 | case -1:
|
---|
260 | case 0:
|
---|
261 | case 1:
|
---|
262 | zero=1;
|
---|
263 | /* break; */
|
---|
264 | case 2:
|
---|
265 | bn_sub_words(t, a, &(a[n]),n); /* + */
|
---|
266 | bn_sub_words(&(t[n]),b, &(b[n]),n); /* - */
|
---|
267 | neg=1;
|
---|
268 | break;
|
---|
269 | case 3:
|
---|
270 | zero=1;
|
---|
271 | /* break; */
|
---|
272 | case 4:
|
---|
273 | bn_sub_words(t, a, &(a[n]),n);
|
---|
274 | bn_sub_words(&(t[n]),&(b[n]),b, n);
|
---|
275 | break;
|
---|
276 | }
|
---|
277 | /* The zero case isn't yet implemented here. The speedup
|
---|
278 | would probably be negligible. */
|
---|
279 | # if 0
|
---|
280 | if (n == 4)
|
---|
281 | {
|
---|
282 | bn_mul_comba4(&(t[n2]),t,&(t[n]));
|
---|
283 | bn_mul_comba4(r,a,b);
|
---|
284 | bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);
|
---|
285 | memset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2));
|
---|
286 | }
|
---|
287 | else
|
---|
288 | # endif
|
---|
289 | if (n == 8)
|
---|
290 | {
|
---|
291 | bn_mul_comba8(&(t[n2]),t,&(t[n]));
|
---|
292 | bn_mul_comba8(r,a,b);
|
---|
293 | bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);
|
---|
294 | memset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2));
|
---|
295 | }
|
---|
296 | else
|
---|
297 | {
|
---|
298 | p= &(t[n2*2]);
|
---|
299 | bn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);
|
---|
300 | bn_mul_recursive(r,a,b,n,p);
|
---|
301 | i=n/2;
|
---|
302 | /* If there is only a bottom half to the number,
|
---|
303 | * just do it */
|
---|
304 | j=tn-i;
|
---|
305 | if (j == 0)
|
---|
306 | {
|
---|
307 | bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),i,p);
|
---|
308 | memset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2));
|
---|
309 | }
|
---|
310 | else if (j > 0) /* eg, n == 16, i == 8 and tn == 11 */
|
---|
311 | {
|
---|
312 | bn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]),
|
---|
313 | j,i,p);
|
---|
314 | memset(&(r[n2+tn*2]),0,
|
---|
315 | sizeof(BN_ULONG)*(n2-tn*2));
|
---|
316 | }
|
---|
317 | else /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
|
---|
318 | {
|
---|
319 | memset(&(r[n2]),0,sizeof(BN_ULONG)*n2);
|
---|
320 | if (tn < BN_MUL_RECURSIVE_SIZE_NORMAL)
|
---|
321 | {
|
---|
322 | bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);
|
---|
323 | }
|
---|
324 | else
|
---|
325 | {
|
---|
326 | for (;;)
|
---|
327 | {
|
---|
328 | i/=2;
|
---|
329 | if (i < tn)
|
---|
330 | {
|
---|
331 | bn_mul_part_recursive(&(r[n2]),
|
---|
332 | &(a[n]),&(b[n]),
|
---|
333 | tn-i,i,p);
|
---|
334 | break;
|
---|
335 | }
|
---|
336 | else if (i == tn)
|
---|
337 | {
|
---|
338 | bn_mul_recursive(&(r[n2]),
|
---|
339 | &(a[n]),&(b[n]),
|
---|
340 | i,p);
|
---|
341 | break;
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
|
---|
349 | * r[10] holds (a[0]*b[0])
|
---|
350 | * r[32] holds (b[1]*b[1])
|
---|
351 | */
|
---|
352 |
|
---|
353 | c1=(int)(bn_add_words(t,r,&(r[n2]),n2));
|
---|
354 |
|
---|
355 | if (neg) /* if t[32] is negative */
|
---|
356 | {
|
---|
357 | c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));
|
---|
358 | }
|
---|
359 | else
|
---|
360 | {
|
---|
361 | /* Might have a carry */
|
---|
362 | c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));
|
---|
363 | }
|
---|
364 |
|
---|
365 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
|
---|
366 | * r[10] holds (a[0]*b[0])
|
---|
367 | * r[32] holds (b[1]*b[1])
|
---|
368 | * c1 holds the carry bits
|
---|
369 | */
|
---|
370 | c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));
|
---|
371 | if (c1)
|
---|
372 | {
|
---|
373 | p= &(r[n+n2]);
|
---|
374 | lo= *p;
|
---|
375 | ln=(lo+c1)&BN_MASK2;
|
---|
376 | *p=ln;
|
---|
377 |
|
---|
378 | /* The overflow will stop before we over write
|
---|
379 | * words we should not overwrite */
|
---|
380 | if (ln < c1)
|
---|
381 | {
|
---|
382 | do {
|
---|
383 | p++;
|
---|
384 | lo= *p;
|
---|
385 | ln=(lo+1)&BN_MASK2;
|
---|
386 | *p=ln;
|
---|
387 | } while (ln == 0);
|
---|
388 | }
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | /* a and b must be the same size, which is n2.
|
---|
393 | * r needs to be n2 words and t needs to be n2*2
|
---|
394 | */
|
---|
395 | void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
|
---|
396 | BN_ULONG *t)
|
---|
397 | {
|
---|
398 | int n=n2/2;
|
---|
399 |
|
---|
400 | # ifdef BN_COUNT
|
---|
401 | printf(" bn_mul_low_recursive %d * %d\n",n2,n2);
|
---|
402 | # endif
|
---|
403 |
|
---|
404 | bn_mul_recursive(r,a,b,n,&(t[0]));
|
---|
405 | if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL)
|
---|
406 | {
|
---|
407 | bn_mul_low_recursive(&(t[0]),&(a[0]),&(b[n]),n,&(t[n2]));
|
---|
408 | bn_add_words(&(r[n]),&(r[n]),&(t[0]),n);
|
---|
409 | bn_mul_low_recursive(&(t[0]),&(a[n]),&(b[0]),n,&(t[n2]));
|
---|
410 | bn_add_words(&(r[n]),&(r[n]),&(t[0]),n);
|
---|
411 | }
|
---|
412 | else
|
---|
413 | {
|
---|
414 | bn_mul_low_normal(&(t[0]),&(a[0]),&(b[n]),n);
|
---|
415 | bn_mul_low_normal(&(t[n]),&(a[n]),&(b[0]),n);
|
---|
416 | bn_add_words(&(r[n]),&(r[n]),&(t[0]),n);
|
---|
417 | bn_add_words(&(r[n]),&(r[n]),&(t[n]),n);
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | /* a and b must be the same size, which is n2.
|
---|
422 | * r needs to be n2 words and t needs to be n2*2
|
---|
423 | * l is the low words of the output.
|
---|
424 | * t needs to be n2*3
|
---|
425 | */
|
---|
426 | void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2,
|
---|
427 | BN_ULONG *t)
|
---|
428 | {
|
---|
429 | int i,n;
|
---|
430 | int c1,c2;
|
---|
431 | int neg,oneg,zero;
|
---|
432 | BN_ULONG ll,lc,*lp,*mp;
|
---|
433 |
|
---|
434 | # ifdef BN_COUNT
|
---|
435 | printf(" bn_mul_high %d * %d\n",n2,n2);
|
---|
436 | # endif
|
---|
437 | n=n2/2;
|
---|
438 |
|
---|
439 | /* Calculate (al-ah)*(bh-bl) */
|
---|
440 | neg=zero=0;
|
---|
441 | c1=bn_cmp_words(&(a[0]),&(a[n]),n);
|
---|
442 | c2=bn_cmp_words(&(b[n]),&(b[0]),n);
|
---|
443 | switch (c1*3+c2)
|
---|
444 | {
|
---|
445 | case -4:
|
---|
446 | bn_sub_words(&(r[0]),&(a[n]),&(a[0]),n);
|
---|
447 | bn_sub_words(&(r[n]),&(b[0]),&(b[n]),n);
|
---|
448 | break;
|
---|
449 | case -3:
|
---|
450 | zero=1;
|
---|
451 | break;
|
---|
452 | case -2:
|
---|
453 | bn_sub_words(&(r[0]),&(a[n]),&(a[0]),n);
|
---|
454 | bn_sub_words(&(r[n]),&(b[n]),&(b[0]),n);
|
---|
455 | neg=1;
|
---|
456 | break;
|
---|
457 | case -1:
|
---|
458 | case 0:
|
---|
459 | case 1:
|
---|
460 | zero=1;
|
---|
461 | break;
|
---|
462 | case 2:
|
---|
463 | bn_sub_words(&(r[0]),&(a[0]),&(a[n]),n);
|
---|
464 | bn_sub_words(&(r[n]),&(b[0]),&(b[n]),n);
|
---|
465 | neg=1;
|
---|
466 | break;
|
---|
467 | case 3:
|
---|
468 | zero=1;
|
---|
469 | break;
|
---|
470 | case 4:
|
---|
471 | bn_sub_words(&(r[0]),&(a[0]),&(a[n]),n);
|
---|
472 | bn_sub_words(&(r[n]),&(b[n]),&(b[0]),n);
|
---|
473 | break;
|
---|
474 | }
|
---|
475 |
|
---|
476 | oneg=neg;
|
---|
477 | /* t[10] = (a[0]-a[1])*(b[1]-b[0]) */
|
---|
478 | /* r[10] = (a[1]*b[1]) */
|
---|
479 | # ifdef BN_MUL_COMBA
|
---|
480 | if (n == 8)
|
---|
481 | {
|
---|
482 | bn_mul_comba8(&(t[0]),&(r[0]),&(r[n]));
|
---|
483 | bn_mul_comba8(r,&(a[n]),&(b[n]));
|
---|
484 | }
|
---|
485 | else
|
---|
486 | # endif
|
---|
487 | {
|
---|
488 | bn_mul_recursive(&(t[0]),&(r[0]),&(r[n]),n,&(t[n2]));
|
---|
489 | bn_mul_recursive(r,&(a[n]),&(b[n]),n,&(t[n2]));
|
---|
490 | }
|
---|
491 |
|
---|
492 | /* s0 == low(al*bl)
|
---|
493 | * s1 == low(ah*bh)+low((al-ah)*(bh-bl))+low(al*bl)+high(al*bl)
|
---|
494 | * We know s0 and s1 so the only unknown is high(al*bl)
|
---|
495 | * high(al*bl) == s1 - low(ah*bh+s0+(al-ah)*(bh-bl))
|
---|
496 | * high(al*bl) == s1 - (r[0]+l[0]+t[0])
|
---|
497 | */
|
---|
498 | if (l != NULL)
|
---|
499 | {
|
---|
500 | lp= &(t[n2+n]);
|
---|
501 | c1=(int)(bn_add_words(lp,&(r[0]),&(l[0]),n));
|
---|
502 | }
|
---|
503 | else
|
---|
504 | {
|
---|
505 | c1=0;
|
---|
506 | lp= &(r[0]);
|
---|
507 | }
|
---|
508 |
|
---|
509 | if (neg)
|
---|
510 | neg=(int)(bn_sub_words(&(t[n2]),lp,&(t[0]),n));
|
---|
511 | else
|
---|
512 | {
|
---|
513 | bn_add_words(&(t[n2]),lp,&(t[0]),n);
|
---|
514 | neg=0;
|
---|
515 | }
|
---|
516 |
|
---|
517 | if (l != NULL)
|
---|
518 | {
|
---|
519 | bn_sub_words(&(t[n2+n]),&(l[n]),&(t[n2]),n);
|
---|
520 | }
|
---|
521 | else
|
---|
522 | {
|
---|
523 | lp= &(t[n2+n]);
|
---|
524 | mp= &(t[n2]);
|
---|
525 | for (i=0; i<n; i++)
|
---|
526 | lp[i]=((~mp[i])+1)&BN_MASK2;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /* s[0] = low(al*bl)
|
---|
530 | * t[3] = high(al*bl)
|
---|
531 | * t[10] = (a[0]-a[1])*(b[1]-b[0]) neg is the sign
|
---|
532 | * r[10] = (a[1]*b[1])
|
---|
533 | */
|
---|
534 | /* R[10] = al*bl
|
---|
535 | * R[21] = al*bl + ah*bh + (a[0]-a[1])*(b[1]-b[0])
|
---|
536 | * R[32] = ah*bh
|
---|
537 | */
|
---|
538 | /* R[1]=t[3]+l[0]+r[0](+-)t[0] (have carry/borrow)
|
---|
539 | * R[2]=r[0]+t[3]+r[1](+-)t[1] (have carry/borrow)
|
---|
540 | * R[3]=r[1]+(carry/borrow)
|
---|
541 | */
|
---|
542 | if (l != NULL)
|
---|
543 | {
|
---|
544 | lp= &(t[n2]);
|
---|
545 | c1= (int)(bn_add_words(lp,&(t[n2+n]),&(l[0]),n));
|
---|
546 | }
|
---|
547 | else
|
---|
548 | {
|
---|
549 | lp= &(t[n2+n]);
|
---|
550 | c1=0;
|
---|
551 | }
|
---|
552 | c1+=(int)(bn_add_words(&(t[n2]),lp, &(r[0]),n));
|
---|
553 | if (oneg)
|
---|
554 | c1-=(int)(bn_sub_words(&(t[n2]),&(t[n2]),&(t[0]),n));
|
---|
555 | else
|
---|
556 | c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),&(t[0]),n));
|
---|
557 |
|
---|
558 | c2 =(int)(bn_add_words(&(r[0]),&(r[0]),&(t[n2+n]),n));
|
---|
559 | c2+=(int)(bn_add_words(&(r[0]),&(r[0]),&(r[n]),n));
|
---|
560 | if (oneg)
|
---|
561 | c2-=(int)(bn_sub_words(&(r[0]),&(r[0]),&(t[n]),n));
|
---|
562 | else
|
---|
563 | c2+=(int)(bn_add_words(&(r[0]),&(r[0]),&(t[n]),n));
|
---|
564 |
|
---|
565 | if (c1 != 0) /* Add starting at r[0], could be +ve or -ve */
|
---|
566 | {
|
---|
567 | i=0;
|
---|
568 | if (c1 > 0)
|
---|
569 | {
|
---|
570 | lc=c1;
|
---|
571 | do {
|
---|
572 | ll=(r[i]+lc)&BN_MASK2;
|
---|
573 | r[i++]=ll;
|
---|
574 | lc=(lc > ll);
|
---|
575 | } while (lc);
|
---|
576 | }
|
---|
577 | else
|
---|
578 | {
|
---|
579 | lc= -c1;
|
---|
580 | do {
|
---|
581 | ll=r[i];
|
---|
582 | r[i++]=(ll-lc)&BN_MASK2;
|
---|
583 | lc=(lc > ll);
|
---|
584 | } while (lc);
|
---|
585 | }
|
---|
586 | }
|
---|
587 | if (c2 != 0) /* Add starting at r[1] */
|
---|
588 | {
|
---|
589 | i=n;
|
---|
590 | if (c2 > 0)
|
---|
591 | {
|
---|
592 | lc=c2;
|
---|
593 | do {
|
---|
594 | ll=(r[i]+lc)&BN_MASK2;
|
---|
595 | r[i++]=ll;
|
---|
596 | lc=(lc > ll);
|
---|
597 | } while (lc);
|
---|
598 | }
|
---|
599 | else
|
---|
600 | {
|
---|
601 | lc= -c2;
|
---|
602 | do {
|
---|
603 | ll=r[i];
|
---|
604 | r[i++]=(ll-lc)&BN_MASK2;
|
---|
605 | lc=(lc > ll);
|
---|
606 | } while (lc);
|
---|
607 | }
|
---|
608 | }
|
---|
609 | }
|
---|
610 | #endif /* BN_RECURSION */
|
---|
611 |
|
---|
612 | int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
|
---|
613 | {
|
---|
614 | int top,al,bl;
|
---|
615 | BIGNUM *rr;
|
---|
616 | int ret = 0;
|
---|
617 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
|
---|
618 | int i;
|
---|
619 | #endif
|
---|
620 | #ifdef BN_RECURSION
|
---|
621 | BIGNUM *t;
|
---|
622 | int j,k;
|
---|
623 | #endif
|
---|
624 |
|
---|
625 | #ifdef BN_COUNT
|
---|
626 | printf("BN_mul %d * %d\n",a->top,b->top);
|
---|
627 | #endif
|
---|
628 |
|
---|
629 | bn_check_top(a);
|
---|
630 | bn_check_top(b);
|
---|
631 | bn_check_top(r);
|
---|
632 |
|
---|
633 | al=a->top;
|
---|
634 | bl=b->top;
|
---|
635 |
|
---|
636 | if ((al == 0) || (bl == 0))
|
---|
637 | {
|
---|
638 | BN_zero(r);
|
---|
639 | return(1);
|
---|
640 | }
|
---|
641 | top=al+bl;
|
---|
642 |
|
---|
643 | BN_CTX_start(ctx);
|
---|
644 | if ((r == a) || (r == b))
|
---|
645 | {
|
---|
646 | if ((rr = BN_CTX_get(ctx)) == NULL) goto err;
|
---|
647 | }
|
---|
648 | else
|
---|
649 | rr = r;
|
---|
650 | rr->neg=a->neg^b->neg;
|
---|
651 |
|
---|
652 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
|
---|
653 | i = al-bl;
|
---|
654 | #endif
|
---|
655 | #ifdef BN_MUL_COMBA
|
---|
656 | if (i == 0)
|
---|
657 | {
|
---|
658 | # if 0
|
---|
659 | if (al == 4)
|
---|
660 | {
|
---|
661 | if (bn_wexpand(rr,8) == NULL) goto err;
|
---|
662 | rr->top=8;
|
---|
663 | bn_mul_comba4(rr->d,a->d,b->d);
|
---|
664 | goto end;
|
---|
665 | }
|
---|
666 | # endif
|
---|
667 | if (al == 8)
|
---|
668 | {
|
---|
669 | if (bn_wexpand(rr,16) == NULL) goto err;
|
---|
670 | rr->top=16;
|
---|
671 | bn_mul_comba8(rr->d,a->d,b->d);
|
---|
672 | goto end;
|
---|
673 | }
|
---|
674 | }
|
---|
675 | #endif /* BN_MUL_COMBA */
|
---|
676 | #ifdef BN_RECURSION
|
---|
677 | if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))
|
---|
678 | {
|
---|
679 | if (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))
|
---|
680 | {
|
---|
681 | bn_wexpand(b,al);
|
---|
682 | b->d[bl]=0;
|
---|
683 | bl++;
|
---|
684 | i--;
|
---|
685 | }
|
---|
686 | else if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))
|
---|
687 | {
|
---|
688 | bn_wexpand(a,bl);
|
---|
689 | a->d[al]=0;
|
---|
690 | al++;
|
---|
691 | i++;
|
---|
692 | }
|
---|
693 | if (i == 0)
|
---|
694 | {
|
---|
695 | /* symmetric and > 4 */
|
---|
696 | /* 16 or larger */
|
---|
697 | j=BN_num_bits_word((BN_ULONG)al);
|
---|
698 | j=1<<(j-1);
|
---|
699 | k=j+j;
|
---|
700 | t = BN_CTX_get(ctx);
|
---|
701 | if (al == j) /* exact multiple */
|
---|
702 | {
|
---|
703 | bn_wexpand(t,k*2);
|
---|
704 | bn_wexpand(rr,k*2);
|
---|
705 | bn_mul_recursive(rr->d,a->d,b->d,al,t->d);
|
---|
706 | }
|
---|
707 | else
|
---|
708 | {
|
---|
709 | bn_wexpand(a,k);
|
---|
710 | bn_wexpand(b,k);
|
---|
711 | bn_wexpand(t,k*4);
|
---|
712 | bn_wexpand(rr,k*4);
|
---|
713 | for (i=a->top; i<k; i++)
|
---|
714 | a->d[i]=0;
|
---|
715 | for (i=b->top; i<k; i++)
|
---|
716 | b->d[i]=0;
|
---|
717 | bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);
|
---|
718 | }
|
---|
719 | rr->top=top;
|
---|
720 | goto end;
|
---|
721 | }
|
---|
722 | }
|
---|
723 | #endif /* BN_RECURSION */
|
---|
724 | if (bn_wexpand(rr,top) == NULL) goto err;
|
---|
725 | rr->top=top;
|
---|
726 | bn_mul_normal(rr->d,a->d,al,b->d,bl);
|
---|
727 |
|
---|
728 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
|
---|
729 | end:
|
---|
730 | #endif
|
---|
731 | bn_fix_top(rr);
|
---|
732 | if (r != rr) BN_copy(r,rr);
|
---|
733 | ret=1;
|
---|
734 | err:
|
---|
735 | BN_CTX_end(ctx);
|
---|
736 | return(ret);
|
---|
737 | }
|
---|
738 |
|
---|
739 | void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
|
---|
740 | {
|
---|
741 | BN_ULONG *rr;
|
---|
742 |
|
---|
743 | #ifdef BN_COUNT
|
---|
744 | printf(" bn_mul_normal %d * %d\n",na,nb);
|
---|
745 | #endif
|
---|
746 |
|
---|
747 | if (na < nb)
|
---|
748 | {
|
---|
749 | int itmp;
|
---|
750 | BN_ULONG *ltmp;
|
---|
751 |
|
---|
752 | itmp=na; na=nb; nb=itmp;
|
---|
753 | ltmp=a; a=b; b=ltmp;
|
---|
754 |
|
---|
755 | }
|
---|
756 | rr= &(r[na]);
|
---|
757 | rr[0]=bn_mul_words(r,a,na,b[0]);
|
---|
758 |
|
---|
759 | for (;;)
|
---|
760 | {
|
---|
761 | if (--nb <= 0) return;
|
---|
762 | rr[1]=bn_mul_add_words(&(r[1]),a,na,b[1]);
|
---|
763 | if (--nb <= 0) return;
|
---|
764 | rr[2]=bn_mul_add_words(&(r[2]),a,na,b[2]);
|
---|
765 | if (--nb <= 0) return;
|
---|
766 | rr[3]=bn_mul_add_words(&(r[3]),a,na,b[3]);
|
---|
767 | if (--nb <= 0) return;
|
---|
768 | rr[4]=bn_mul_add_words(&(r[4]),a,na,b[4]);
|
---|
769 | rr+=4;
|
---|
770 | r+=4;
|
---|
771 | b+=4;
|
---|
772 | }
|
---|
773 | }
|
---|
774 |
|
---|
775 | void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
|
---|
776 | {
|
---|
777 | #ifdef BN_COUNT
|
---|
778 | printf(" bn_mul_low_normal %d * %d\n",n,n);
|
---|
779 | #endif
|
---|
780 | bn_mul_words(r,a,n,b[0]);
|
---|
781 |
|
---|
782 | for (;;)
|
---|
783 | {
|
---|
784 | if (--n <= 0) return;
|
---|
785 | bn_mul_add_words(&(r[1]),a,n,b[1]);
|
---|
786 | if (--n <= 0) return;
|
---|
787 | bn_mul_add_words(&(r[2]),a,n,b[2]);
|
---|
788 | if (--n <= 0) return;
|
---|
789 | bn_mul_add_words(&(r[3]),a,n,b[3]);
|
---|
790 | if (--n <= 0) return;
|
---|
791 | bn_mul_add_words(&(r[4]),a,n,b[4]);
|
---|
792 | r+=4;
|
---|
793 | b+=4;
|
---|
794 | }
|
---|
795 | }
|
---|