7f4c3ff3b2cdb366d2044697440aa5a834a4aafb
[openssl.git] / crypto / bn / bn_asm.c
1 /* crypto/bn/bn_asm.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 "cryptlib.h"
61 #include "bn_lcl.h"
62
63 #ifdef BN_LLONG 
64
65 BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)
66         {
67         BN_ULONG c1=0;
68
69         bn_check_num(num);
70         if (num <= 0) return(c1);
71
72         for (;;)
73                 {
74                 mul_add(rp[0],ap[0],w,c1);
75                 if (--num == 0) break;
76                 mul_add(rp[1],ap[1],w,c1);
77                 if (--num == 0) break;
78                 mul_add(rp[2],ap[2],w,c1);
79                 if (--num == 0) break;
80                 mul_add(rp[3],ap[3],w,c1);
81                 if (--num == 0) break;
82                 ap+=4;
83                 rp+=4;
84                 }
85         
86         return(c1);
87         } 
88
89 BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)
90         {
91         BN_ULONG c1=0;
92
93         bn_check_num(num);
94         if (num <= 0) return(c1);
95
96         /* for (;;) */
97         while (1) /* circumvent egcs-1.1.2 bug */
98                 {
99                 mul(rp[0],ap[0],w,c1);
100                 if (--num == 0) break;
101                 mul(rp[1],ap[1],w,c1);
102                 if (--num == 0) break;
103                 mul(rp[2],ap[2],w,c1);
104                 if (--num == 0) break;
105                 mul(rp[3],ap[3],w,c1);
106                 if (--num == 0) break;
107                 ap+=4;
108                 rp+=4;
109                 }
110         return(c1);
111         } 
112
113 void bn_sqr_words(BN_ULONG *r, BN_ULONG *a, int n)
114         {
115         bn_check_num(n);
116         if (n <= 0) return;
117         for (;;)
118                 {
119                 BN_ULLONG t;
120
121                 t=(BN_ULLONG)(a[0])*(a[0]);
122                 r[0]=Lw(t); r[1]=Hw(t);
123                 if (--n == 0) break;
124
125                 t=(BN_ULLONG)(a[1])*(a[1]);
126                 r[2]=Lw(t); r[3]=Hw(t);
127                 if (--n == 0) break;
128
129                 t=(BN_ULLONG)(a[2])*(a[2]);
130                 r[4]=Lw(t); r[5]=Hw(t);
131                 if (--n == 0) break;
132
133                 t=(BN_ULLONG)(a[3])*(a[3]);
134                 r[6]=Lw(t); r[7]=Hw(t);
135                 if (--n == 0) break;
136
137                 a+=4;
138                 r+=8;
139                 }
140         }
141
142 #else
143
144 BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)
145         {
146         BN_ULONG c=0;
147         BN_ULONG bl,bh;
148
149         bn_check_num(num);
150         if (num <= 0) return((BN_ULONG)0);
151
152         bl=LBITS(w);
153         bh=HBITS(w);
154
155         for (;;)
156                 {
157                 mul_add(rp[0],ap[0],bl,bh,c);
158                 if (--num == 0) break;
159                 mul_add(rp[1],ap[1],bl,bh,c);
160                 if (--num == 0) break;
161                 mul_add(rp[2],ap[2],bl,bh,c);
162                 if (--num == 0) break;
163                 mul_add(rp[3],ap[3],bl,bh,c);
164                 if (--num == 0) break;
165                 ap+=4;
166                 rp+=4;
167                 }
168         return(c);
169         } 
170
171 BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)
172         {
173         BN_ULONG carry=0;
174         BN_ULONG bl,bh;
175
176         bn_check_num(num);
177         if (num <= 0) return((BN_ULONG)0);
178
179         bl=LBITS(w);
180         bh=HBITS(w);
181
182         for (;;)
183                 {
184                 mul(rp[0],ap[0],bl,bh,carry);
185                 if (--num == 0) break;
186                 mul(rp[1],ap[1],bl,bh,carry);
187                 if (--num == 0) break;
188                 mul(rp[2],ap[2],bl,bh,carry);
189                 if (--num == 0) break;
190                 mul(rp[3],ap[3],bl,bh,carry);
191                 if (--num == 0) break;
192                 ap+=4;
193                 rp+=4;
194                 }
195         return(carry);
196         } 
197
198 void bn_sqr_words(BN_ULONG *r, BN_ULONG *a, int n)
199         {
200         bn_check_num(n);
201         if (n <= 0) return;
202         for (;;)
203                 {
204                 sqr64(r[0],r[1],a[0]);
205                 if (--n == 0) break;
206
207                 sqr64(r[2],r[3],a[1]);
208                 if (--n == 0) break;
209
210                 sqr64(r[4],r[5],a[2]);
211                 if (--n == 0) break;
212
213                 sqr64(r[6],r[7],a[3]);
214                 if (--n == 0) break;
215
216                 a+=4;
217                 r+=8;
218                 }
219         }
220
221 #endif
222
223 #if defined(BN_LLONG) && defined(BN_DIV2W)
224
225 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
226         {
227         return((BN_ULONG)(((((BN_ULLONG)h)<<BN_BITS2)|l)/(BN_ULLONG)d));
228         }
229
230 #else
231
232 /* Divide h-l by d and return the result. */
233 /* I need to test this some more :-( */
234 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
235         {
236         BN_ULONG dh,dl,q,ret=0,th,tl,t;
237         int i,count=2;
238
239         if (d == 0) return(BN_MASK2);
240
241         i=BN_num_bits_word(d);
242         if ((i != BN_BITS2) && (h > (BN_ULONG)1<<i))
243                 {
244 #if !defined(NO_STDIO) && !defined(WIN16)
245                 fprintf(stderr,"Division would overflow (%d)\n",i);
246 #endif
247                 abort();
248                 }
249         i=BN_BITS2-i;
250         if (h >= d) h-=d;
251
252         if (i)
253                 {
254                 d<<=i;
255                 h=(h<<i)|(l>>(BN_BITS2-i));
256                 l<<=i;
257                 }
258         dh=(d&BN_MASK2h)>>BN_BITS4;
259         dl=(d&BN_MASK2l);
260         for (;;)
261                 {
262                 if ((h>>BN_BITS4) == dh)
263                         q=BN_MASK2l;
264                 else
265                         q=h/dh;
266
267                 for (;;)
268                         {
269                         t=(h-q*dh);
270                         if ((t&BN_MASK2h) ||
271                                 ((dl*q) <= (
272                                         (t<<BN_BITS4)+
273                                         ((l&BN_MASK2h)>>BN_BITS4))))
274                                 break;
275                         q--;
276                         }
277                 th=q*dh;
278                 tl=q*dl;
279                 t=(tl>>BN_BITS4);
280                 tl=(tl<<BN_BITS4)&BN_MASK2h;
281                 th+=t;
282
283                 if (l < tl) th++;
284                 l-=tl;
285                 if (h < th)
286                         {
287                         h+=d;
288                         q--;
289                         }
290                 h-=th;
291
292                 if (--count == 0) break;
293
294                 ret=q<<BN_BITS4;
295                 h=((h<<BN_BITS4)|(l>>BN_BITS4))&BN_MASK2;
296                 l=(l&BN_MASK2l)<<BN_BITS4;
297                 }
298         ret|=q;
299         return(ret);
300         }
301 #endif
302
303 #ifdef BN_LLONG
304 BN_ULONG bn_add_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
305         {
306         BN_ULLONG ll=0;
307
308         bn_check_num(n);
309         if (n <= 0) return((BN_ULONG)0);
310
311         for (;;)
312                 {
313                 ll+=(BN_ULLONG)a[0]+b[0];
314                 r[0]=(BN_ULONG)ll&BN_MASK2;
315                 ll>>=BN_BITS2;
316                 if (--n <= 0) break;
317
318                 ll+=(BN_ULLONG)a[1]+b[1];
319                 r[1]=(BN_ULONG)ll&BN_MASK2;
320                 ll>>=BN_BITS2;
321                 if (--n <= 0) break;
322
323                 ll+=(BN_ULLONG)a[2]+b[2];
324                 r[2]=(BN_ULONG)ll&BN_MASK2;
325                 ll>>=BN_BITS2;
326                 if (--n <= 0) break;
327
328                 ll+=(BN_ULLONG)a[3]+b[3];
329                 r[3]=(BN_ULONG)ll&BN_MASK2;
330                 ll>>=BN_BITS2;
331                 if (--n <= 0) break;
332
333                 a+=4;
334                 b+=4;
335                 r+=4;
336                 }
337         return((BN_ULONG)ll);
338         }
339 #else
340 BN_ULONG bn_add_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
341         {
342         BN_ULONG c,l,t;
343
344         bn_check_num(n);
345         if (n <= 0) return((BN_ULONG)0);
346
347         c=0;
348         for (;;)
349                 {
350                 t=a[0];
351                 t=(t+c)&BN_MASK2;
352                 c=(t < c);
353                 l=(t+b[0])&BN_MASK2;
354                 c+=(l < t);
355                 r[0]=l;
356                 if (--n <= 0) break;
357
358                 t=a[1];
359                 t=(t+c)&BN_MASK2;
360                 c=(t < c);
361                 l=(t+b[1])&BN_MASK2;
362                 c+=(l < t);
363                 r[1]=l;
364                 if (--n <= 0) break;
365
366                 t=a[2];
367                 t=(t+c)&BN_MASK2;
368                 c=(t < c);
369                 l=(t+b[2])&BN_MASK2;
370                 c+=(l < t);
371                 r[2]=l;
372                 if (--n <= 0) break;
373
374                 t=a[3];
375                 t=(t+c)&BN_MASK2;
376                 c=(t < c);
377                 l=(t+b[3])&BN_MASK2;
378                 c+=(l < t);
379                 r[3]=l;
380                 if (--n <= 0) break;
381
382                 a+=4;
383                 b+=4;
384                 r+=4;
385                 }
386         return((BN_ULONG)c);
387         }
388 #endif
389
390 BN_ULONG bn_sub_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
391         {
392         BN_ULONG t1,t2;
393         int c=0;
394
395         bn_check_num(n);
396         if (n <= 0) return((BN_ULONG)0);
397
398         for (;;)
399                 {
400                 t1=a[0]; t2=b[0];
401                 r[0]=(t1-t2-c)&BN_MASK2;
402                 if (t1 != t2) c=(t1 < t2);
403                 if (--n <= 0) break;
404
405                 t1=a[1]; t2=b[1];
406                 r[1]=(t1-t2-c)&BN_MASK2;
407                 if (t1 != t2) c=(t1 < t2);
408                 if (--n <= 0) break;
409
410                 t1=a[2]; t2=b[2];
411                 r[2]=(t1-t2-c)&BN_MASK2;
412                 if (t1 != t2) c=(t1 < t2);
413                 if (--n <= 0) break;
414
415                 t1=a[3]; t2=b[3];
416                 r[3]=(t1-t2-c)&BN_MASK2;
417                 if (t1 != t2) c=(t1 < t2);
418                 if (--n <= 0) break;
419
420                 a+=4;
421                 b+=4;
422                 r+=4;
423                 }
424         return(c);
425         }
426
427 #ifdef BN_MUL_COMBA
428
429 #undef bn_mul_comba8
430 #undef bn_mul_comba4
431 #undef bn_sqr_comba8
432 #undef bn_sqr_comba4
433
434 #ifdef BN_LLONG
435 #define mul_add_c(a,b,c0,c1,c2) \
436         t=(BN_ULLONG)a*b; \
437         t1=(BN_ULONG)Lw(t); \
438         t2=(BN_ULONG)Hw(t); \
439         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
440         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
441
442 #define mul_add_c2(a,b,c0,c1,c2) \
443         t=(BN_ULLONG)a*b; \
444         tt=(t+t)&BN_MASK; \
445         if (tt < t) c2++; \
446         t1=(BN_ULONG)Lw(tt); \
447         t2=(BN_ULONG)Hw(tt); \
448         c0=(c0+t1)&BN_MASK2;  \
449         if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
450         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
451
452 #define sqr_add_c(a,i,c0,c1,c2) \
453         t=(BN_ULLONG)a[i]*a[i]; \
454         t1=(BN_ULONG)Lw(t); \
455         t2=(BN_ULONG)Hw(t); \
456         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
457         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
458
459 #define sqr_add_c2(a,i,j,c0,c1,c2) \
460         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
461 #else
462 #define mul_add_c(a,b,c0,c1,c2) \
463         t1=LBITS(a); t2=HBITS(a); \
464         bl=LBITS(b); bh=HBITS(b); \
465         mul64(t1,t2,bl,bh); \
466         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
467         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
468
469 #define mul_add_c2(a,b,c0,c1,c2) \
470         t1=LBITS(a); t2=HBITS(a); \
471         bl=LBITS(b); bh=HBITS(b); \
472         mul64(t1,t2,bl,bh); \
473         if (t2 & BN_TBIT) c2++; \
474         t2=(t2+t2)&BN_MASK2; \
475         if (t1 & BN_TBIT) t2++; \
476         t1=(t1+t1)&BN_MASK2; \
477         c0=(c0+t1)&BN_MASK2;  \
478         if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
479         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
480
481 #define sqr_add_c(a,i,c0,c1,c2) \
482         sqr64(t1,t2,(a)[i]); \
483         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
484         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
485
486 #define sqr_add_c2(a,i,j,c0,c1,c2) \
487         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
488 #endif
489
490 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
491         {
492 #ifdef BN_LLONG
493         BN_ULLONG t;
494 #else
495         BN_ULONG bl,bh;
496 #endif
497         BN_ULONG t1,t2;
498         BN_ULONG c1,c2,c3;
499
500         c1=0;
501         c2=0;
502         c3=0;
503         mul_add_c(a[0],b[0],c1,c2,c3);
504         r[0]=c1;
505         c1=0;
506         mul_add_c(a[0],b[1],c2,c3,c1);
507         mul_add_c(a[1],b[0],c2,c3,c1);
508         r[1]=c2;
509         c2=0;
510         mul_add_c(a[2],b[0],c3,c1,c2);
511         mul_add_c(a[1],b[1],c3,c1,c2);
512         mul_add_c(a[0],b[2],c3,c1,c2);
513         r[2]=c3;
514         c3=0;
515         mul_add_c(a[0],b[3],c1,c2,c3);
516         mul_add_c(a[1],b[2],c1,c2,c3);
517         mul_add_c(a[2],b[1],c1,c2,c3);
518         mul_add_c(a[3],b[0],c1,c2,c3);
519         r[3]=c1;
520         c1=0;
521         mul_add_c(a[4],b[0],c2,c3,c1);
522         mul_add_c(a[3],b[1],c2,c3,c1);
523         mul_add_c(a[2],b[2],c2,c3,c1);
524         mul_add_c(a[1],b[3],c2,c3,c1);
525         mul_add_c(a[0],b[4],c2,c3,c1);
526         r[4]=c2;
527         c2=0;
528         mul_add_c(a[0],b[5],c3,c1,c2);
529         mul_add_c(a[1],b[4],c3,c1,c2);
530         mul_add_c(a[2],b[3],c3,c1,c2);
531         mul_add_c(a[3],b[2],c3,c1,c2);
532         mul_add_c(a[4],b[1],c3,c1,c2);
533         mul_add_c(a[5],b[0],c3,c1,c2);
534         r[5]=c3;
535         c3=0;
536         mul_add_c(a[6],b[0],c1,c2,c3);
537         mul_add_c(a[5],b[1],c1,c2,c3);
538         mul_add_c(a[4],b[2],c1,c2,c3);
539         mul_add_c(a[3],b[3],c1,c2,c3);
540         mul_add_c(a[2],b[4],c1,c2,c3);
541         mul_add_c(a[1],b[5],c1,c2,c3);
542         mul_add_c(a[0],b[6],c1,c2,c3);
543         r[6]=c1;
544         c1=0;
545         mul_add_c(a[0],b[7],c2,c3,c1);
546         mul_add_c(a[1],b[6],c2,c3,c1);
547         mul_add_c(a[2],b[5],c2,c3,c1);
548         mul_add_c(a[3],b[4],c2,c3,c1);
549         mul_add_c(a[4],b[3],c2,c3,c1);
550         mul_add_c(a[5],b[2],c2,c3,c1);
551         mul_add_c(a[6],b[1],c2,c3,c1);
552         mul_add_c(a[7],b[0],c2,c3,c1);
553         r[7]=c2;
554         c2=0;
555         mul_add_c(a[7],b[1],c3,c1,c2);
556         mul_add_c(a[6],b[2],c3,c1,c2);
557         mul_add_c(a[5],b[3],c3,c1,c2);
558         mul_add_c(a[4],b[4],c3,c1,c2);
559         mul_add_c(a[3],b[5],c3,c1,c2);
560         mul_add_c(a[2],b[6],c3,c1,c2);
561         mul_add_c(a[1],b[7],c3,c1,c2);
562         r[8]=c3;
563         c3=0;
564         mul_add_c(a[2],b[7],c1,c2,c3);
565         mul_add_c(a[3],b[6],c1,c2,c3);
566         mul_add_c(a[4],b[5],c1,c2,c3);
567         mul_add_c(a[5],b[4],c1,c2,c3);
568         mul_add_c(a[6],b[3],c1,c2,c3);
569         mul_add_c(a[7],b[2],c1,c2,c3);
570         r[9]=c1;
571         c1=0;
572         mul_add_c(a[7],b[3],c2,c3,c1);
573         mul_add_c(a[6],b[4],c2,c3,c1);
574         mul_add_c(a[5],b[5],c2,c3,c1);
575         mul_add_c(a[4],b[6],c2,c3,c1);
576         mul_add_c(a[3],b[7],c2,c3,c1);
577         r[10]=c2;
578         c2=0;
579         mul_add_c(a[4],b[7],c3,c1,c2);
580         mul_add_c(a[5],b[6],c3,c1,c2);
581         mul_add_c(a[6],b[5],c3,c1,c2);
582         mul_add_c(a[7],b[4],c3,c1,c2);
583         r[11]=c3;
584         c3=0;
585         mul_add_c(a[7],b[5],c1,c2,c3);
586         mul_add_c(a[6],b[6],c1,c2,c3);
587         mul_add_c(a[5],b[7],c1,c2,c3);
588         r[12]=c1;
589         c1=0;
590         mul_add_c(a[6],b[7],c2,c3,c1);
591         mul_add_c(a[7],b[6],c2,c3,c1);
592         r[13]=c2;
593         c2=0;
594         mul_add_c(a[7],b[7],c3,c1,c2);
595         r[14]=c3;
596         r[15]=c1;
597         }
598
599 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
600         {
601 #ifdef BN_LLONG
602         BN_ULLONG t;
603 #else
604         BN_ULONG bl,bh;
605 #endif
606         BN_ULONG t1,t2;
607         BN_ULONG c1,c2,c3;
608
609         c1=0;
610         c2=0;
611         c3=0;
612         mul_add_c(a[0],b[0],c1,c2,c3);
613         r[0]=c1;
614         c1=0;
615         mul_add_c(a[0],b[1],c2,c3,c1);
616         mul_add_c(a[1],b[0],c2,c3,c1);
617         r[1]=c2;
618         c2=0;
619         mul_add_c(a[2],b[0],c3,c1,c2);
620         mul_add_c(a[1],b[1],c3,c1,c2);
621         mul_add_c(a[0],b[2],c3,c1,c2);
622         r[2]=c3;
623         c3=0;
624         mul_add_c(a[0],b[3],c1,c2,c3);
625         mul_add_c(a[1],b[2],c1,c2,c3);
626         mul_add_c(a[2],b[1],c1,c2,c3);
627         mul_add_c(a[3],b[0],c1,c2,c3);
628         r[3]=c1;
629         c1=0;
630         mul_add_c(a[3],b[1],c2,c3,c1);
631         mul_add_c(a[2],b[2],c2,c3,c1);
632         mul_add_c(a[1],b[3],c2,c3,c1);
633         r[4]=c2;
634         c2=0;
635         mul_add_c(a[2],b[3],c3,c1,c2);
636         mul_add_c(a[3],b[2],c3,c1,c2);
637         r[5]=c3;
638         c3=0;
639         mul_add_c(a[3],b[3],c1,c2,c3);
640         r[6]=c1;
641         r[7]=c2;
642         }
643
644 void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a)
645         {
646 #ifdef BN_LLONG
647         BN_ULLONG t,tt;
648 #else
649         BN_ULONG bl,bh;
650 #endif
651         BN_ULONG t1,t2;
652         BN_ULONG c1,c2,c3;
653
654         c1=0;
655         c2=0;
656         c3=0;
657         sqr_add_c(a,0,c1,c2,c3);
658         r[0]=c1;
659         c1=0;
660         sqr_add_c2(a,1,0,c2,c3,c1);
661         r[1]=c2;
662         c2=0;
663         sqr_add_c(a,1,c3,c1,c2);
664         sqr_add_c2(a,2,0,c3,c1,c2);
665         r[2]=c3;
666         c3=0;
667         sqr_add_c2(a,3,0,c1,c2,c3);
668         sqr_add_c2(a,2,1,c1,c2,c3);
669         r[3]=c1;
670         c1=0;
671         sqr_add_c(a,2,c2,c3,c1);
672         sqr_add_c2(a,3,1,c2,c3,c1);
673         sqr_add_c2(a,4,0,c2,c3,c1);
674         r[4]=c2;
675         c2=0;
676         sqr_add_c2(a,5,0,c3,c1,c2);
677         sqr_add_c2(a,4,1,c3,c1,c2);
678         sqr_add_c2(a,3,2,c3,c1,c2);
679         r[5]=c3;
680         c3=0;
681         sqr_add_c(a,3,c1,c2,c3);
682         sqr_add_c2(a,4,2,c1,c2,c3);
683         sqr_add_c2(a,5,1,c1,c2,c3);
684         sqr_add_c2(a,6,0,c1,c2,c3);
685         r[6]=c1;
686         c1=0;
687         sqr_add_c2(a,7,0,c2,c3,c1);
688         sqr_add_c2(a,6,1,c2,c3,c1);
689         sqr_add_c2(a,5,2,c2,c3,c1);
690         sqr_add_c2(a,4,3,c2,c3,c1);
691         r[7]=c2;
692         c2=0;
693         sqr_add_c(a,4,c3,c1,c2);
694         sqr_add_c2(a,5,3,c3,c1,c2);
695         sqr_add_c2(a,6,2,c3,c1,c2);
696         sqr_add_c2(a,7,1,c3,c1,c2);
697         r[8]=c3;
698         c3=0;
699         sqr_add_c2(a,7,2,c1,c2,c3);
700         sqr_add_c2(a,6,3,c1,c2,c3);
701         sqr_add_c2(a,5,4,c1,c2,c3);
702         r[9]=c1;
703         c1=0;
704         sqr_add_c(a,5,c2,c3,c1);
705         sqr_add_c2(a,6,4,c2,c3,c1);
706         sqr_add_c2(a,7,3,c2,c3,c1);
707         r[10]=c2;
708         c2=0;
709         sqr_add_c2(a,7,4,c3,c1,c2);
710         sqr_add_c2(a,6,5,c3,c1,c2);
711         r[11]=c3;
712         c3=0;
713         sqr_add_c(a,6,c1,c2,c3);
714         sqr_add_c2(a,7,5,c1,c2,c3);
715         r[12]=c1;
716         c1=0;
717         sqr_add_c2(a,7,6,c2,c3,c1);
718         r[13]=c2;
719         c2=0;
720         sqr_add_c(a,7,c3,c1,c2);
721         r[14]=c3;
722         r[15]=c1;
723         }
724
725 void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a)
726         {
727 #ifdef BN_LLONG
728         BN_ULLONG t,tt;
729 #else
730         BN_ULONG bl,bh;
731 #endif
732         BN_ULONG t1,t2;
733         BN_ULONG c1,c2,c3;
734
735         c1=0;
736         c2=0;
737         c3=0;
738         sqr_add_c(a,0,c1,c2,c3);
739         r[0]=c1;
740         c1=0;
741         sqr_add_c2(a,1,0,c2,c3,c1);
742         r[1]=c2;
743         c2=0;
744         sqr_add_c(a,1,c3,c1,c2);
745         sqr_add_c2(a,2,0,c3,c1,c2);
746         r[2]=c3;
747         c3=0;
748         sqr_add_c2(a,3,0,c1,c2,c3);
749         sqr_add_c2(a,2,1,c1,c2,c3);
750         r[3]=c1;
751         c1=0;
752         sqr_add_c(a,2,c2,c3,c1);
753         sqr_add_c2(a,3,1,c2,c3,c1);
754         r[4]=c2;
755         c2=0;
756         sqr_add_c2(a,3,2,c3,c1,c2);
757         r[5]=c3;
758         c3=0;
759         sqr_add_c(a,3,c1,c2,c3);
760         r[6]=c1;
761         r[7]=c2;
762         }
763 #else
764
765 /* hmm... is it faster just to do a multiply? */
766 #undef bn_sqr_comba4
767 void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a)
768         {
769         BN_ULONG t[8];
770         bn_sqr_normal(r,a,4,t);
771         }
772
773 #undef bn_sqr_comba8
774 void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a)
775         {
776         BN_ULONG t[16];
777         bn_sqr_normal(r,a,8,t);
778         }
779
780 void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
781         {
782         r[4]=bn_mul_words(    &(r[0]),a,4,b[0]);
783         r[5]=bn_mul_add_words(&(r[1]),a,4,b[1]);
784         r[6]=bn_mul_add_words(&(r[2]),a,4,b[2]);
785         r[7]=bn_mul_add_words(&(r[3]),a,4,b[3]);
786         }
787
788 void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
789         {
790         r[ 8]=bn_mul_words(    &(r[0]),a,8,b[0]);
791         r[ 9]=bn_mul_add_words(&(r[1]),a,8,b[1]);
792         r[10]=bn_mul_add_words(&(r[2]),a,8,b[2]);
793         r[11]=bn_mul_add_words(&(r[3]),a,8,b[3]);
794         r[12]=bn_mul_add_words(&(r[4]),a,8,b[4]);
795         r[13]=bn_mul_add_words(&(r[5]),a,8,b[5]);
796         r[14]=bn_mul_add_words(&(r[6]),a,8,b[6]);
797         r[15]=bn_mul_add_words(&(r[7]),a,8,b[7]);
798         }
799
800 #endif /* BN_COMBA */