This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / crypto / bn / bn_sqr.c
1 /* crypto/bn/bn_sqr.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 /* r must not be a */
64 /* I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 */
65 int BN_sqr(r, a, ctx)
66 BIGNUM *r;
67 BIGNUM *a;
68 BN_CTX *ctx;
69         {
70         int max,al;
71         BIGNUM *tmp;
72
73 #ifdef BN_COUNT
74 printf("BN_sqr %d * %d\n",a->top,a->top);
75 #endif
76         bn_check_top(a);
77         tmp= &(ctx->bn[ctx->tos]);
78
79         al=a->top;
80         if (al <= 0)
81                 {
82                 r->top=0;
83                 return(1);
84                 }
85
86         max=(al+al);
87         if (bn_wexpand(r,max+1) == NULL) return(0);
88
89         r->neg=0;
90         if (al == 4)
91                 {
92 #ifndef BN_SQR_COMBA
93                 BN_ULONG t[8];
94                 bn_sqr_normal(r->d,a->d,4,t);
95 #else
96                 bn_sqr_comba4(r->d,a->d);
97 #endif
98                 }
99         else if (al == 8)
100                 {
101 #ifndef BN_SQR_COMBA
102                 BN_ULONG t[16];
103                 bn_sqr_normal(r->d,a->d,8,t);
104 #else
105                 bn_sqr_comba8(r->d,a->d);
106 #endif
107                 }
108         else 
109                 {
110 #if defined(BN_RECURSION)
111                 if (al < BN_SQR_RECURSIVE_SIZE_NORMAL)
112                         {
113                         BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2];
114                         bn_sqr_normal(r->d,a->d,al,t);
115                         }
116                 else
117                         {
118                         if (bn_wexpand(tmp,2*max+1) == NULL) return(0);
119                         bn_sqr_recursive(r->d,a->d,al,tmp->d);
120                         }
121 #else
122                 if (bn_wexpand(tmp,max) == NULL) return(0);
123                 bn_sqr_normal(r->d,a->d,al,tmp->d);
124 #endif
125                 }
126
127         r->top=max;
128         if ((max > 0) && (r->d[max-1] == 0)) r->top--;
129         return(1);
130         }
131
132 /* tmp must have 2*n words */
133 void bn_sqr_normal(r, a, n, tmp)
134 BN_ULONG *r;
135 BN_ULONG *a;
136 int n;
137 BN_ULONG *tmp;
138         {
139         int i,j,max;
140         BN_ULONG *ap,*rp;
141
142         max=n*2;
143         ap=a;
144         rp=r;
145         rp[0]=rp[max-1]=0;
146         rp++;
147         j=n;
148
149         if (--j > 0)
150                 {
151                 ap++;
152                 rp[j]=bn_mul_words(rp,ap,j,ap[-1]);
153                 rp+=2;
154                 }
155
156         for (i=n-2; i>0; i--)
157                 {
158                 j--;
159                 ap++;
160                 rp[j]=bn_mul_add_words(rp,ap,j,ap[-1]);
161                 rp+=2;
162                 }
163
164         bn_add_words(r,r,r,max);
165
166         /* There will not be a carry */
167
168         bn_sqr_words(tmp,a,n);
169
170         bn_add_words(r,r,tmp,max);
171         }
172
173 #ifdef BN_RECURSION
174 /* r is 2*n words in size,
175  * a and b are both n words in size.
176  * n must be a power of 2.
177  * We multiply and return the result.
178  * t must be 2*n words in size
179  * We calulate
180  * a[0]*b[0]
181  * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
182  * a[1]*b[1]
183  */
184 void bn_sqr_recursive(r,a,n2,t)
185 BN_ULONG *r,*a;
186 int n2;
187 BN_ULONG *t;
188         {
189         int n=n2/2;
190         int zero,c1;
191         BN_ULONG ln,lo,*p;
192
193 #ifdef BN_COUNT
194 printf(" bn_sqr_recursive %d * %d\n",n2,n2);
195 #endif
196         if (n2 == 4)
197                 {
198 #ifndef BN_SQR_COMBA
199                 bn_sqr_normal(r,a,4,t);
200 #else
201                 bn_sqr_comba4(r,a);
202 #endif
203                 return;
204                 }
205         else if (n2 == 8)
206                 {
207 #ifndef BN_SQR_COMBA
208                 bn_sqr_normal(r,a,8,t);
209 #else
210                 bn_sqr_comba8(r,a);
211 #endif
212                 return;
213                 }
214         if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL)
215                 {
216                 bn_sqr_normal(r,a,n2,t);
217                 return;
218                 }
219         /* r=(a[0]-a[1])*(a[1]-a[0]) */
220         c1=bn_cmp_words(a,&(a[n]),n);
221         zero=0;
222         if (c1 > 0)
223                 bn_sub_words(t,a,&(a[n]),n);
224         else if (c1 < 0)
225                 bn_sub_words(t,&(a[n]),a,n);
226         else
227                 zero=1;
228
229         /* The result will always be negative unless it is zero */
230         p= &(t[n2*2]);
231
232         if (!zero)
233                 bn_sqr_recursive(&(t[n2]),t,n,p);
234         else
235                 memset(&(t[n2]),0,n*sizeof(BN_ULONG));
236         bn_sqr_recursive(r,a,n,p);
237         bn_sqr_recursive(&(r[n2]),&(a[n]),n,p);
238
239         /* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero
240          * r[10] holds (a[0]*b[0])
241          * r[32] holds (b[1]*b[1])
242          */
243
244         c1=bn_add_words(t,r,&(r[n2]),n2);
245
246         /* t[32] is negative */
247         c1-=bn_sub_words(&(t[n2]),t,&(t[n2]),n2);
248
249         /* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1])
250          * r[10] holds (a[0]*a[0])
251          * r[32] holds (a[1]*a[1])
252          * c1 holds the carry bits
253          */
254         c1+=bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2);
255         if (c1)
256                 {
257                 p= &(r[n+n2]);
258                 lo= *p;
259                 ln=(lo+c1)&BN_MASK2;
260                 *p=ln;
261
262                 /* The overflow will stop before we over write
263                  * words we should not overwrite */
264                 if (ln < (BN_ULONG)c1)
265                         {
266                         do      {
267                                 p++;
268                                 lo= *p;
269                                 ln=(lo+1)&BN_MASK2;
270                                 *p=ln;
271                                 } while (ln == 0);
272                         }
273                 }
274         }
275 #endif