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