52e5ef2f1e0945b5d0cee359b55e5930b92673f5
[openssl.git] / crypto / bn / bn_mul.c
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 <assert.h>
61 #include "cryptlib.h"
62 #include "bn_lcl.h"
63
64 /* Here follows specialised variants of bn_add_words() and
65    bn_sub_words().  They have the property performing operations on
66    arrays of different sizes.  The sizes of those arrays is expressed through
67    cl, which is the common length ( basicall, min(len(a),len(b)) ), and dl,
68    which is the delta between the two lengths, calculated as len(a)-len(b).
69    All lengths are the number of BN_ULONGs...  For the operations that require
70    a result array as parameter, it must have the length cl+abs(dl).
71    These functions should probably end up in bn_asm.c as soon as there are
72    assembler counterparts for the systems that use assembler files.  */
73
74 BN_ULONG bn_sub_part_words(BN_ULONG *r,
75         const BN_ULONG *a, const BN_ULONG *b,
76         int cl, int dl)
77         {
78         BN_ULONG c, t;
79
80         assert(cl >= 0);
81         c = bn_sub_words(r, a, b, cl);
82
83         if (dl == 0)
84                 return c;
85
86         r += cl;
87         a += cl;
88         b += cl;
89
90         if (dl < 0)
91                 {
92 #ifdef BN_COUNT
93                 fprintf(stderr, "  bn_sub_part_words %d + %d (dl < 0, c = %d)\n", cl, dl, c);
94 #endif
95                 for (;;)
96                         {
97                         t = b[0];
98                         r[0] = (0-t-c)&BN_MASK2;
99                         if (t != 0) c=1;
100                         if (++dl >= 0) break;
101
102                         t = b[1];
103                         r[1] = (0-t-c)&BN_MASK2;
104                         if (t != 0) c=1;
105                         if (++dl >= 0) break;
106
107                         t = b[2];
108                         r[2] = (0-t-c)&BN_MASK2;
109                         if (t != 0) c=1;
110                         if (++dl >= 0) break;
111
112                         t = b[3];
113                         r[3] = (0-t-c)&BN_MASK2;
114                         if (t != 0) c=1;
115                         if (++dl >= 0) break;
116
117                         b += 4;
118                         r += 4;
119                         }
120                 }
121         else
122                 {
123                 int save_dl = dl;
124 #ifdef BN_COUNT
125                 fprintf(stderr, "  bn_sub_part_words %d + %d (dl > 0, c = %d)\n", cl, dl, c);
126 #endif
127                 while(c)
128                         {
129                         t = a[0];
130                         r[0] = (t-c)&BN_MASK2;
131                         if (t != 0) c=0;
132                         if (--dl <= 0) break;
133
134                         t = a[1];
135                         r[1] = (t-c)&BN_MASK2;
136                         if (t != 0) c=0;
137                         if (--dl <= 0) break;
138
139                         t = a[2];
140                         r[2] = (t-c)&BN_MASK2;
141                         if (t != 0) c=0;
142                         if (--dl <= 0) break;
143
144                         t = a[3];
145                         r[3] = (t-c)&BN_MASK2;
146                         if (t != 0) c=0;
147                         if (--dl <= 0) break;
148
149                         save_dl = dl;
150                         a += 4;
151                         r += 4;
152                         }
153                 if (dl > 0)
154                         {
155 #ifdef BN_COUNT
156                         fprintf(stderr, "  bn_sub_part_words %d + %d (dl > 0, c == 0)\n", cl, dl);
157 #endif
158                         if (save_dl > dl)
159                                 {
160                                 switch (save_dl - dl)
161                                         {
162                                 case 1:
163                                         r[1] = a[1];
164                                         if (--dl <= 0) break;
165                                 case 2:
166                                         r[2] = a[2];
167                                         if (--dl <= 0) break;
168                                 case 3:
169                                         r[3] = a[3];
170                                         if (--dl <= 0) break;
171                                         }
172                                 a += 4;
173                                 r += 4;
174                                 }
175                         }
176                 if (dl > 0)
177                         {
178 #ifdef BN_COUNT
179                         fprintf(stderr, "  bn_sub_part_words %d + %d (dl > 0, copy)\n", cl, dl);
180 #endif
181                         for(;;)
182                                 {
183                                 r[0] = a[0];
184                                 if (--dl <= 0) break;
185                                 r[1] = a[1];
186                                 if (--dl <= 0) break;
187                                 r[2] = a[2];
188                                 if (--dl <= 0) break;
189                                 r[3] = a[3];
190                                 if (--dl <= 0) break;
191
192                                 a += 4;
193                                 r += 4;
194                                 }
195                         }
196                 }
197         return c;
198         }
199
200 BN_ULONG bn_add_part_words(BN_ULONG *r,
201         const BN_ULONG *a, const BN_ULONG *b,
202         int cl, int dl)
203         {
204         BN_ULONG c, l, t;
205
206         assert(cl >= 0);
207         c = bn_add_words(r, a, b, cl);
208
209         if (dl == 0)
210                 return c;
211
212         r += cl;
213         a += cl;
214         b += cl;
215
216         if (dl < 0)
217                 {
218                 int save_dl = dl;
219 #ifdef BN_COUNT
220                 fprintf(stderr, "  bn_add_part_words %d + %d (dl < 0, c = %d)\n", cl, dl, c);
221 #endif
222                 while (c)
223                         {
224                         l=(c+b[0])&BN_MASK2;
225                         c=(l < c);
226                         r[0]=l;
227                         if (++dl >= 0) break;
228
229                         l=(c+b[1])&BN_MASK2;
230                         c=(l < c);
231                         r[1]=l;
232                         if (++dl >= 0) break;
233
234                         l=(c+b[2])&BN_MASK2;
235                         c=(l < c);
236                         r[2]=l;
237                         if (++dl >= 0) break;
238
239                         l=(c+b[3])&BN_MASK2;
240                         c=(l < c);
241                         r[3]=l;
242                         if (++dl >= 0) break;
243
244                         save_dl = dl;
245                         b+=4;
246                         r+=4;
247                         }
248                 if (dl < 0)
249                         {
250 #ifdef BN_COUNT
251                         fprintf(stderr, "  bn_add_part_words %d + %d (dl < 0, c == 0)\n", cl, dl);
252 #endif
253                         if (save_dl < dl)
254                                 {
255                                 switch (dl - save_dl)
256                                         {
257                                 case 1:
258                                         r[1] = b[1];
259                                         if (++dl >= 0) break;
260                                 case 2:
261                                         r[2] = b[2];
262                                         if (++dl >= 0) break;
263                                 case 3:
264                                         r[3] = b[3];
265                                         if (++dl >= 0) break;
266                                         }
267                                 b += 4;
268                                 r += 4;
269                                 }
270                         }
271                 if (dl < 0)
272                         {
273 #ifdef BN_COUNT
274                         fprintf(stderr, "  bn_add_part_words %d + %d (dl < 0, copy)\n", cl, dl);
275 #endif
276                         for(;;)
277                                 {
278                                 r[0] = b[0];
279                                 if (++dl >= 0) break;
280                                 r[1] = b[1];
281                                 if (++dl >= 0) break;
282                                 r[2] = b[2];
283                                 if (++dl >= 0) break;
284                                 r[3] = b[3];
285                                 if (++dl >= 0) break;
286
287                                 b += 4;
288                                 r += 4;
289                                 }
290                         }
291                 }
292         else
293                 {
294                 int save_dl = dl;
295 #ifdef BN_COUNT
296                 fprintf(stderr, "  bn_add_part_words %d + %d (dl > 0)\n", cl, dl);
297 #endif
298                 while (c)
299                         {
300                         t=(a[0]+c)&BN_MASK2;
301                         c=(t < c);
302                         r[0]=t;
303                         if (--dl <= 0) break;
304
305                         t=(a[1]+c)&BN_MASK2;
306                         c=(t < c);
307                         r[1]=t;
308                         if (--dl <= 0) break;
309
310                         t=(a[2]+c)&BN_MASK2;
311                         c=(t < c);
312                         r[2]=t;
313                         if (--dl <= 0) break;
314
315                         t=(a[3]+c)&BN_MASK2;
316                         c=(t < c);
317                         r[3]=t;
318                         if (--dl <= 0) break;
319
320                         save_dl = dl;
321                         a+=4;
322                         r+=4;
323                         }
324 #ifdef BN_COUNT
325                 fprintf(stderr, "  bn_add_part_words %d + %d (dl > 0, c == 0)\n", cl, dl);
326 #endif
327                 if (dl > 0)
328                         {
329                         if (save_dl > dl)
330                                 {
331                                 switch (save_dl - dl)
332                                         {
333                                 case 1:
334                                         r[1] = a[1];
335                                         if (--dl <= 0) break;
336                                 case 2:
337                                         r[2] = a[2];
338                                         if (--dl <= 0) break;
339                                 case 3:
340                                         r[3] = a[3];
341                                         if (--dl <= 0) break;
342                                         }
343                                 a += 4;
344                                 r += 4;
345                                 }
346                         }
347                 if (dl > 0)
348                         {
349 #ifdef BN_COUNT
350                         fprintf(stderr, "  bn_add_part_words %d + %d (dl > 0, copy)\n", cl, dl);
351 #endif
352                         for(;;)
353                                 {
354                                 r[0] = a[0];
355                                 if (--dl <= 0) break;
356                                 r[1] = a[1];
357                                 if (--dl <= 0) break;
358                                 r[2] = a[2];
359                                 if (--dl <= 0) break;
360                                 r[3] = a[3];
361                                 if (--dl <= 0) break;
362
363                                 a += 4;
364                                 r += 4;
365                                 }
366                         }
367                 }
368         return c;
369         }
370
371 #ifdef BN_RECURSION
372 /* Karatsuba recursive multiplication algorithm
373  * (cf. Knuth, The Art of Computer Programming, Vol. 2) */
374
375 /* r is 2*n2 words in size,
376  * a and b are both n2 words in size.
377  * n2 must be a power of 2.
378  * We multiply and return the result.
379  * t must be 2*n2 words in size
380  * We calculate
381  * a[0]*b[0]
382  * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
383  * a[1]*b[1]
384  */
385 void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
386              BN_ULONG *t)
387         {
388         int n=n2/2,c1,c2;
389         unsigned int neg,zero;
390         BN_ULONG ln,lo,*p;
391
392 # ifdef BN_COUNT
393         fprintf(stderr," bn_mul_recursive %d * %d\n",n2,n2);
394 # endif
395 # ifdef BN_MUL_COMBA
396 #  if 0
397         if (n2 == 4)
398                 {
399                 bn_mul_comba4(r,a,b);
400                 return;
401                 }
402 #  endif
403         if (n2 == 8)
404                 {
405                 bn_mul_comba8(r,a,b);
406                 return; 
407                 }
408 # endif /* BN_MUL_COMBA */
409         if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)
410                 {
411                 /* This should not happen */
412                 bn_mul_normal(r,a,n2,b,n2);
413                 return;
414                 }
415         /* r=(a[0]-a[1])*(b[1]-b[0]) */
416         c1=bn_cmp_words(a,&(a[n]),n);
417         c2=bn_cmp_words(&(b[n]),b,n);
418         zero=neg=0;
419         switch (c1*3+c2)
420                 {
421         case -4:
422                 bn_sub_words(t,      &(a[n]),a,      n); /* - */
423                 bn_sub_words(&(t[n]),b,      &(b[n]),n); /* - */
424                 break;
425         case -3:
426                 zero=1;
427                 break;
428         case -2:
429                 bn_sub_words(t,      &(a[n]),a,      n); /* - */
430                 bn_sub_words(&(t[n]),&(b[n]),b,      n); /* + */
431                 neg=1;
432                 break;
433         case -1:
434         case 0:
435         case 1:
436                 zero=1;
437                 break;
438         case 2:
439                 bn_sub_words(t,      a,      &(a[n]),n); /* + */
440                 bn_sub_words(&(t[n]),b,      &(b[n]),n); /* - */
441                 neg=1;
442                 break;
443         case 3:
444                 zero=1;
445                 break;
446         case 4:
447                 bn_sub_words(t,      a,      &(a[n]),n);
448                 bn_sub_words(&(t[n]),&(b[n]),b,      n);
449                 break;
450                 }
451
452 # ifdef BN_MUL_COMBA
453         if (n == 4)
454                 {
455                 if (!zero)
456                         bn_mul_comba4(&(t[n2]),t,&(t[n]));
457                 else
458                         memset(&(t[n2]),0,8*sizeof(BN_ULONG));
459                 
460                 bn_mul_comba4(r,a,b);
461                 bn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));
462                 }
463         else if (n == 8)
464                 {
465                 if (!zero)
466                         bn_mul_comba8(&(t[n2]),t,&(t[n]));
467                 else
468                         memset(&(t[n2]),0,16*sizeof(BN_ULONG));
469                 
470                 bn_mul_comba8(r,a,b);
471                 bn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));
472                 }
473         else
474 # endif /* BN_MUL_COMBA */
475                 {
476                 p= &(t[n2*2]);
477                 if (!zero)
478                         bn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);
479                 else
480                         memset(&(t[n2]),0,n2*sizeof(BN_ULONG));
481                 bn_mul_recursive(r,a,b,n,p);
482                 bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,p);
483                 }
484
485         /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
486          * r[10] holds (a[0]*b[0])
487          * r[32] holds (b[1]*b[1])
488          */
489
490         c1=(int)(bn_add_words(t,r,&(r[n2]),n2));
491
492         if (neg) /* if t[32] is negative */
493                 {
494                 c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));
495                 }
496         else
497                 {
498                 /* Might have a carry */
499                 c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));
500                 }
501
502         /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
503          * r[10] holds (a[0]*b[0])
504          * r[32] holds (b[1]*b[1])
505          * c1 holds the carry bits
506          */
507         c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));
508         if (c1)
509                 {
510                 p= &(r[n+n2]);
511                 lo= *p;
512                 ln=(lo+c1)&BN_MASK2;
513                 *p=ln;
514
515                 /* The overflow will stop before we over write
516                  * words we should not overwrite */
517                 if (ln < (BN_ULONG)c1)
518                         {
519                         do      {
520                                 p++;
521                                 lo= *p;
522                                 ln=(lo+1)&BN_MASK2;
523                                 *p=ln;
524                                 } while (ln == 0);
525                         }
526                 }
527         }
528
529 /* n+tn is the word length
530  * t needs to be n*4 is size, as does r */
531 void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int tn,
532              int n, BN_ULONG *t)
533         {
534         int i,j,n2=n*2;
535         unsigned int c1,c2,neg,zero;
536         BN_ULONG ln,lo,*p;
537
538 # ifdef BN_COUNT
539         fprintf(stderr," bn_mul_part_recursive (%d+%d) * (%d+%d)\n",
540                 tn, n,tn, n);
541 # endif
542         if (n < 8)
543                 {
544                 i=tn+n;
545                 bn_mul_normal(r,a,i,b,i);
546                 return;
547                 }
548
549         /* r=(a[0]-a[1])*(b[1]-b[0]) */
550         c1=bn_cmp_part_words(a,&(a[n]),tn,n-tn);
551         c2=bn_cmp_part_words(&(b[n]),b,tn,tn-n);
552         zero=neg=0;
553         switch (c1*3+c2)
554                 {
555         case -4:
556                 bn_sub_part_words(t,      &(a[n]),a,      tn,tn-n); /* - */
557                 bn_sub_part_words(&(t[n]),b,      &(b[n]),tn,n-tn); /* - */
558                 break;
559         case -3:
560                 zero=1;
561                 /* break; */
562         case -2:
563                 bn_sub_part_words(t,      &(a[n]),a,      tn,tn-n); /* - */
564                 bn_sub_part_words(&(t[n]),&(b[n]),b,      tn,tn-n); /* + */
565                 neg=1;
566                 break;
567         case -1:
568         case 0:
569         case 1:
570                 zero=1;
571                 /* break; */
572         case 2:
573                 bn_sub_part_words(t,      a,      &(a[n]),tn,n-tn); /* + */
574                 bn_sub_part_words(&(t[n]),b,      &(b[n]),tn,n-tn); /* - */
575                 neg=1;
576                 break;
577         case 3:
578                 zero=1;
579                 /* break; */
580         case 4:
581                 bn_sub_part_words(t,      a,      &(a[n]),tn,n-tn);
582                 bn_sub_part_words(&(t[n]),&(b[n]),b,      tn,tn-n);
583                 break;
584                 }
585                 /* The zero case isn't yet implemented here. The speedup
586                    would probably be negligible. */
587 # if 0
588         if (n == 4)
589                 {
590                 bn_mul_comba4(&(t[n2]),t,&(t[n]));
591                 bn_mul_comba4(r,a,b);
592                 bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);
593                 memset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2));
594                 }
595         else
596 # endif
597         if (n == 8)
598                 {
599                 bn_mul_comba8(&(t[n2]),t,&(t[n]));
600                 bn_mul_comba8(r,a,b);
601                 bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);
602                 memset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2));
603                 }
604         else
605                 {
606                 p= &(t[n2*2]);
607                 bn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);
608                 bn_mul_recursive(r,a,b,n,p);
609                 i=n/2;
610                 /* If there is only a bottom half to the number,
611                  * just do it */
612                 j=tn-i;
613                 if (j == 0)
614                         {
615                         bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),i,p);
616                         memset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2));
617                         }
618                 else if (j > 0) /* eg, n == 16, i == 8 and tn == 11 */
619                                 {
620                                 bn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]),
621                                         j,i,p);
622                                 memset(&(r[n2+tn*2]),0,
623                                         sizeof(BN_ULONG)*(n2-tn*2));
624                                 }
625                 else /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
626                         {
627                         memset(&(r[n2]),0,sizeof(BN_ULONG)*n2);
628                         if (tn < BN_MUL_RECURSIVE_SIZE_NORMAL)
629                                 {
630                                 bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);
631                                 }
632                         else
633                                 {
634                                 for (;;)
635                                         {
636                                         i/=2;
637                                         if (i < tn)
638                                                 {
639                                                 bn_mul_part_recursive(&(r[n2]),
640                                                         &(a[n]),&(b[n]),
641                                                         tn-i,i,p);
642                                                 break;
643                                                 }
644                                         else if (i == tn)
645                                                 {
646                                                 bn_mul_recursive(&(r[n2]),
647                                                         &(a[n]),&(b[n]),
648                                                         i,p);
649                                                 break;
650                                                 }
651                                         }
652                                 }
653                         }
654                 }
655
656         /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
657          * r[10] holds (a[0]*b[0])
658          * r[32] holds (b[1]*b[1])
659          */
660
661         c1=(int)(bn_add_words(t,r,&(r[n2]),n2));
662
663         if (neg) /* if t[32] is negative */
664                 {
665                 c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));
666                 }
667         else
668                 {
669                 /* Might have a carry */
670                 c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));
671                 }
672
673         /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
674          * r[10] holds (a[0]*b[0])
675          * r[32] holds (b[1]*b[1])
676          * c1 holds the carry bits
677          */
678         c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));
679         if (c1)
680                 {
681                 p= &(r[n+n2]);
682                 lo= *p;
683                 ln=(lo+c1)&BN_MASK2;
684                 *p=ln;
685
686                 /* The overflow will stop before we over write
687                  * words we should not overwrite */
688                 if (ln < c1)
689                         {
690                         do      {
691                                 p++;
692                                 lo= *p;
693                                 ln=(lo+1)&BN_MASK2;
694                                 *p=ln;
695                                 } while (ln == 0);
696                         }
697                 }
698         }
699
700 /* a and b must be the same size, which is n2.
701  * r needs to be n2 words and t needs to be n2*2
702  */
703 void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
704              BN_ULONG *t)
705         {
706         int n=n2/2;
707
708 # ifdef BN_COUNT
709         fprintf(stderr," bn_mul_low_recursive %d * %d\n",n2,n2);
710 # endif
711
712         bn_mul_recursive(r,a,b,n,&(t[0]));
713         if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL)
714                 {
715                 bn_mul_low_recursive(&(t[0]),&(a[0]),&(b[n]),n,&(t[n2]));
716                 bn_add_words(&(r[n]),&(r[n]),&(t[0]),n);
717                 bn_mul_low_recursive(&(t[0]),&(a[n]),&(b[0]),n,&(t[n2]));
718                 bn_add_words(&(r[n]),&(r[n]),&(t[0]),n);
719                 }
720         else
721                 {
722                 bn_mul_low_normal(&(t[0]),&(a[0]),&(b[n]),n);
723                 bn_mul_low_normal(&(t[n]),&(a[n]),&(b[0]),n);
724                 bn_add_words(&(r[n]),&(r[n]),&(t[0]),n);
725                 bn_add_words(&(r[n]),&(r[n]),&(t[n]),n);
726                 }
727         }
728
729 /* a and b must be the same size, which is n2.
730  * r needs to be n2 words and t needs to be n2*2
731  * l is the low words of the output.
732  * t needs to be n2*3
733  */
734 void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2,
735              BN_ULONG *t)
736         {
737         int i,n;
738         int c1,c2;
739         int neg,oneg,zero;
740         BN_ULONG ll,lc,*lp,*mp;
741
742 # ifdef BN_COUNT
743         fprintf(stderr," bn_mul_high %d * %d\n",n2,n2);
744 # endif
745         n=n2/2;
746
747         /* Calculate (al-ah)*(bh-bl) */
748         neg=zero=0;
749         c1=bn_cmp_words(&(a[0]),&(a[n]),n);
750         c2=bn_cmp_words(&(b[n]),&(b[0]),n);
751         switch (c1*3+c2)
752                 {
753         case -4:
754                 bn_sub_words(&(r[0]),&(a[n]),&(a[0]),n);
755                 bn_sub_words(&(r[n]),&(b[0]),&(b[n]),n);
756                 break;
757         case -3:
758                 zero=1;
759                 break;
760         case -2:
761                 bn_sub_words(&(r[0]),&(a[n]),&(a[0]),n);
762                 bn_sub_words(&(r[n]),&(b[n]),&(b[0]),n);
763                 neg=1;
764                 break;
765         case -1:
766         case 0:
767         case 1:
768                 zero=1;
769                 break;
770         case 2:
771                 bn_sub_words(&(r[0]),&(a[0]),&(a[n]),n);
772                 bn_sub_words(&(r[n]),&(b[0]),&(b[n]),n);
773                 neg=1;
774                 break;
775         case 3:
776                 zero=1;
777                 break;
778         case 4:
779                 bn_sub_words(&(r[0]),&(a[0]),&(a[n]),n);
780                 bn_sub_words(&(r[n]),&(b[n]),&(b[0]),n);
781                 break;
782                 }
783                 
784         oneg=neg;
785         /* t[10] = (a[0]-a[1])*(b[1]-b[0]) */
786         /* r[10] = (a[1]*b[1]) */
787 # ifdef BN_MUL_COMBA
788         if (n == 8)
789                 {
790                 bn_mul_comba8(&(t[0]),&(r[0]),&(r[n]));
791                 bn_mul_comba8(r,&(a[n]),&(b[n]));
792                 }
793         else
794 # endif
795                 {
796                 bn_mul_recursive(&(t[0]),&(r[0]),&(r[n]),n,&(t[n2]));
797                 bn_mul_recursive(r,&(a[n]),&(b[n]),n,&(t[n2]));
798                 }
799
800         /* s0 == low(al*bl)
801          * s1 == low(ah*bh)+low((al-ah)*(bh-bl))+low(al*bl)+high(al*bl)
802          * We know s0 and s1 so the only unknown is high(al*bl)
803          * high(al*bl) == s1 - low(ah*bh+s0+(al-ah)*(bh-bl))
804          * high(al*bl) == s1 - (r[0]+l[0]+t[0])
805          */
806         if (l != NULL)
807                 {
808                 lp= &(t[n2+n]);
809                 c1=(int)(bn_add_words(lp,&(r[0]),&(l[0]),n));
810                 }
811         else
812                 {
813                 c1=0;
814                 lp= &(r[0]);
815                 }
816
817         if (neg)
818                 neg=(int)(bn_sub_words(&(t[n2]),lp,&(t[0]),n));
819         else
820                 {
821                 bn_add_words(&(t[n2]),lp,&(t[0]),n);
822                 neg=0;
823                 }
824
825         if (l != NULL)
826                 {
827                 bn_sub_words(&(t[n2+n]),&(l[n]),&(t[n2]),n);
828                 }
829         else
830                 {
831                 lp= &(t[n2+n]);
832                 mp= &(t[n2]);
833                 for (i=0; i<n; i++)
834                         lp[i]=((~mp[i])+1)&BN_MASK2;
835                 }
836
837         /* s[0] = low(al*bl)
838          * t[3] = high(al*bl)
839          * t[10] = (a[0]-a[1])*(b[1]-b[0]) neg is the sign
840          * r[10] = (a[1]*b[1])
841          */
842         /* R[10] = al*bl
843          * R[21] = al*bl + ah*bh + (a[0]-a[1])*(b[1]-b[0])
844          * R[32] = ah*bh
845          */
846         /* R[1]=t[3]+l[0]+r[0](+-)t[0] (have carry/borrow)
847          * R[2]=r[0]+t[3]+r[1](+-)t[1] (have carry/borrow)
848          * R[3]=r[1]+(carry/borrow)
849          */
850         if (l != NULL)
851                 {
852                 lp= &(t[n2]);
853                 c1= (int)(bn_add_words(lp,&(t[n2+n]),&(l[0]),n));
854                 }
855         else
856                 {
857                 lp= &(t[n2+n]);
858                 c1=0;
859                 }
860         c1+=(int)(bn_add_words(&(t[n2]),lp,  &(r[0]),n));
861         if (oneg)
862                 c1-=(int)(bn_sub_words(&(t[n2]),&(t[n2]),&(t[0]),n));
863         else
864                 c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),&(t[0]),n));
865
866         c2 =(int)(bn_add_words(&(r[0]),&(r[0]),&(t[n2+n]),n));
867         c2+=(int)(bn_add_words(&(r[0]),&(r[0]),&(r[n]),n));
868         if (oneg)
869                 c2-=(int)(bn_sub_words(&(r[0]),&(r[0]),&(t[n]),n));
870         else
871                 c2+=(int)(bn_add_words(&(r[0]),&(r[0]),&(t[n]),n));
872         
873         if (c1 != 0) /* Add starting at r[0], could be +ve or -ve */
874                 {
875                 i=0;
876                 if (c1 > 0)
877                         {
878                         lc=c1;
879                         do      {
880                                 ll=(r[i]+lc)&BN_MASK2;
881                                 r[i++]=ll;
882                                 lc=(lc > ll);
883                                 } while (lc);
884                         }
885                 else
886                         {
887                         lc= -c1;
888                         do      {
889                                 ll=r[i];
890                                 r[i++]=(ll-lc)&BN_MASK2;
891                                 lc=(lc > ll);
892                                 } while (lc);
893                         }
894                 }
895         if (c2 != 0) /* Add starting at r[1] */
896                 {
897                 i=n;
898                 if (c2 > 0)
899                         {
900                         lc=c2;
901                         do      {
902                                 ll=(r[i]+lc)&BN_MASK2;
903                                 r[i++]=ll;
904                                 lc=(lc > ll);
905                                 } while (lc);
906                         }
907                 else
908                         {
909                         lc= -c2;
910                         do      {
911                                 ll=r[i];
912                                 r[i++]=(ll-lc)&BN_MASK2;
913                                 lc=(lc > ll);
914                                 } while (lc);
915                         }
916                 }
917         }
918 #endif /* BN_RECURSION */
919
920 int BN_mul(BIGNUM *r, /* almost const */ const BIGNUM *a, /* almost const */ const BIGNUM *b, BN_CTX *ctx)
921         {
922         int top,al,bl;
923         BIGNUM *rr;
924         int ret = 0;
925 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
926         int i;
927 #endif
928 #ifdef BN_RECURSION
929         BIGNUM *t;
930         int j,k;
931 #endif
932         BIGNUM *free_a = NULL, *free_b = NULL;
933
934 #ifdef BN_COUNT
935         fprintf(stderr,"BN_mul %d * %d\n",a->top,b->top);
936 #endif
937
938         bn_check_top(a);
939         bn_check_top(b);
940         bn_check_top(r);
941
942         al=a->top;
943         bl=b->top;
944
945         if ((al == 0) || (bl == 0))
946                 {
947                 BN_zero(r);
948                 return(1);
949                 }
950         top=al+bl;
951
952         BN_CTX_start(ctx);
953         if ((r == a) || (r == b))
954                 {
955                 if ((rr = BN_CTX_get(ctx)) == NULL) goto err;
956                 }
957         else
958                 rr = r;
959         rr->neg=a->neg^b->neg;
960
961 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
962         i = al-bl;
963 #endif
964 #ifdef BN_MUL_COMBA
965         if (i == 0)
966                 {
967 # if 0
968                 if (al == 4)
969                         {
970                         if (bn_wexpand(rr,8) == NULL) goto err;
971                         rr->top=8;
972                         bn_mul_comba4(rr->d,a->d,b->d);
973                         goto end;
974                         }
975 # endif
976                 if (al == 8)
977                         {
978                         if (bn_wexpand(rr,16) == NULL) goto err;
979                         rr->top=16;
980                         bn_mul_comba8(rr->d,a->d,b->d);
981                         goto end;
982                         }
983                 }
984 #endif /* BN_MUL_COMBA */
985 #ifdef BN_RECURSION
986         if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))
987                 {
988                 if (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))
989                         {
990                         BIGNUM *tmp_bn = (BIGNUM *)b;
991                         bn_wexpand(tmp_bn,al);
992                         tmp_bn->d[bl]=0;
993                         bl++;
994                         i--;
995                         }
996                 else if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))
997                         {
998                         BIGNUM *tmp_bn = (BIGNUM *)a;
999                         bn_wexpand(tmp_bn,bl);
1000                         tmp_bn->d[al]=0;
1001                         al++;
1002                         i++;
1003                         }
1004                 if (i == 0)
1005                         {
1006                         /* symmetric and > 4 */
1007                         /* 16 or larger */
1008                         j=BN_num_bits_word((BN_ULONG)al);
1009                         j=1<<(j-1);
1010                         k=j+j;
1011                         t = BN_CTX_get(ctx);
1012                         if (al == j) /* exact multiple */
1013                                 {
1014                                 bn_wexpand(t,k*2);
1015                                 bn_wexpand(rr,k*2);
1016                                 bn_mul_recursive(rr->d,a->d,b->d,al,t->d);
1017                                 }
1018                         else
1019                                 {
1020                                 bn_wexpand(t,k*4);
1021                                 bn_wexpand(rr,k*4);
1022                                 bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);
1023                                 }
1024                         rr->top=top;
1025                         goto end;
1026                         }
1027                 }
1028 #endif /* BN_RECURSION */
1029         if (bn_wexpand(rr,top) == NULL) goto err;
1030         rr->top=top;
1031         bn_mul_normal(rr->d,a->d,al,b->d,bl);
1032
1033 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
1034 end:
1035 #endif
1036         bn_fix_top(rr);
1037         if (r != rr) BN_copy(r,rr);
1038         ret=1;
1039 err:
1040         if (free_a) BN_free(free_a);
1041         if (free_b) BN_free(free_b);
1042         BN_CTX_end(ctx);
1043         return(ret);
1044         }
1045
1046 void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
1047         {
1048         BN_ULONG *rr;
1049
1050 #ifdef BN_COUNT
1051         fprintf(stderr," bn_mul_normal %d * %d\n",na,nb);
1052 #endif
1053
1054         if (na < nb)
1055                 {
1056                 int itmp;
1057                 BN_ULONG *ltmp;
1058
1059                 itmp=na; na=nb; nb=itmp;
1060                 ltmp=a;   a=b;   b=ltmp;
1061
1062                 }
1063         rr= &(r[na]);
1064         rr[0]=bn_mul_words(r,a,na,b[0]);
1065
1066         for (;;)
1067                 {
1068                 if (--nb <= 0) return;
1069                 rr[1]=bn_mul_add_words(&(r[1]),a,na,b[1]);
1070                 if (--nb <= 0) return;
1071                 rr[2]=bn_mul_add_words(&(r[2]),a,na,b[2]);
1072                 if (--nb <= 0) return;
1073                 rr[3]=bn_mul_add_words(&(r[3]),a,na,b[3]);
1074                 if (--nb <= 0) return;
1075                 rr[4]=bn_mul_add_words(&(r[4]),a,na,b[4]);
1076                 rr+=4;
1077                 r+=4;
1078                 b+=4;
1079                 }
1080         }
1081
1082 void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
1083         {
1084 #ifdef BN_COUNT
1085         fprintf(stderr," bn_mul_low_normal %d * %d\n",n,n);
1086 #endif
1087         bn_mul_words(r,a,n,b[0]);
1088
1089         for (;;)
1090                 {
1091                 if (--n <= 0) return;
1092                 bn_mul_add_words(&(r[1]),a,n,b[1]);
1093                 if (--n <= 0) return;
1094                 bn_mul_add_words(&(r[2]),a,n,b[2]);
1095                 if (--n <= 0) return;
1096                 bn_mul_add_words(&(r[3]),a,n,b[3]);
1097                 if (--n <= 0) return;
1098                 bn_mul_add_words(&(r[4]),a,n,b[4]);
1099                 r+=4;
1100                 b+=4;
1101                 }
1102         }