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