xref: /minix3/crypto/external/bsd/openssl/dist/crypto/bn/bn_asm.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* crypto/bn/bn_asm.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc #ifndef BN_DEBUG
60ebfedea0SLionel Sambuc # undef NDEBUG                  /* avoid conflicting definitions */
61ebfedea0SLionel Sambuc # define NDEBUG
62ebfedea0SLionel Sambuc #endif
63ebfedea0SLionel Sambuc 
64ebfedea0SLionel Sambuc #include <stdio.h>
65ebfedea0SLionel Sambuc #include <assert.h>
66ebfedea0SLionel Sambuc #include "cryptlib.h"
67ebfedea0SLionel Sambuc #include "bn_lcl.h"
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc #if defined(BN_LLONG) || defined(BN_UMULT_HIGH)
70ebfedea0SLionel Sambuc 
bn_mul_add_words(BN_ULONG * rp,const BN_ULONG * ap,int num,BN_ULONG w)71*0a6a1f1dSLionel Sambuc BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
72*0a6a1f1dSLionel Sambuc                           BN_ULONG w)
73ebfedea0SLionel Sambuc {
74ebfedea0SLionel Sambuc     BN_ULONG c1 = 0;
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc     assert(num >= 0);
77*0a6a1f1dSLionel Sambuc     if (num <= 0)
78*0a6a1f1dSLionel Sambuc         return (c1);
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc # ifndef OPENSSL_SMALL_FOOTPRINT
81*0a6a1f1dSLionel Sambuc     while (num & ~3) {
82ebfedea0SLionel Sambuc         mul_add(rp[0], ap[0], w, c1);
83ebfedea0SLionel Sambuc         mul_add(rp[1], ap[1], w, c1);
84ebfedea0SLionel Sambuc         mul_add(rp[2], ap[2], w, c1);
85ebfedea0SLionel Sambuc         mul_add(rp[3], ap[3], w, c1);
86*0a6a1f1dSLionel Sambuc         ap += 4;
87*0a6a1f1dSLionel Sambuc         rp += 4;
88*0a6a1f1dSLionel Sambuc         num -= 4;
89ebfedea0SLionel Sambuc     }
90ebfedea0SLionel Sambuc # endif
91*0a6a1f1dSLionel Sambuc     while (num) {
92ebfedea0SLionel Sambuc         mul_add(rp[0], ap[0], w, c1);
93*0a6a1f1dSLionel Sambuc         ap++;
94*0a6a1f1dSLionel Sambuc         rp++;
95*0a6a1f1dSLionel Sambuc         num--;
96ebfedea0SLionel Sambuc     }
97ebfedea0SLionel Sambuc 
98ebfedea0SLionel Sambuc     return (c1);
99ebfedea0SLionel Sambuc }
100ebfedea0SLionel Sambuc 
bn_mul_words(BN_ULONG * rp,const BN_ULONG * ap,int num,BN_ULONG w)101ebfedea0SLionel Sambuc BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
102ebfedea0SLionel Sambuc {
103ebfedea0SLionel Sambuc     BN_ULONG c1 = 0;
104ebfedea0SLionel Sambuc 
105ebfedea0SLionel Sambuc     assert(num >= 0);
106*0a6a1f1dSLionel Sambuc     if (num <= 0)
107*0a6a1f1dSLionel Sambuc         return (c1);
108ebfedea0SLionel Sambuc 
109ebfedea0SLionel Sambuc # ifndef OPENSSL_SMALL_FOOTPRINT
110*0a6a1f1dSLionel Sambuc     while (num & ~3) {
111ebfedea0SLionel Sambuc         mul(rp[0], ap[0], w, c1);
112ebfedea0SLionel Sambuc         mul(rp[1], ap[1], w, c1);
113ebfedea0SLionel Sambuc         mul(rp[2], ap[2], w, c1);
114ebfedea0SLionel Sambuc         mul(rp[3], ap[3], w, c1);
115*0a6a1f1dSLionel Sambuc         ap += 4;
116*0a6a1f1dSLionel Sambuc         rp += 4;
117*0a6a1f1dSLionel Sambuc         num -= 4;
118ebfedea0SLionel Sambuc     }
119ebfedea0SLionel Sambuc # endif
120*0a6a1f1dSLionel Sambuc     while (num) {
121ebfedea0SLionel Sambuc         mul(rp[0], ap[0], w, c1);
122*0a6a1f1dSLionel Sambuc         ap++;
123*0a6a1f1dSLionel Sambuc         rp++;
124*0a6a1f1dSLionel Sambuc         num--;
125ebfedea0SLionel Sambuc     }
126ebfedea0SLionel Sambuc     return (c1);
127ebfedea0SLionel Sambuc }
128ebfedea0SLionel Sambuc 
bn_sqr_words(BN_ULONG * r,const BN_ULONG * a,int n)129ebfedea0SLionel Sambuc void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
130ebfedea0SLionel Sambuc {
131ebfedea0SLionel Sambuc     assert(n >= 0);
132*0a6a1f1dSLionel Sambuc     if (n <= 0)
133*0a6a1f1dSLionel Sambuc         return;
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc # ifndef OPENSSL_SMALL_FOOTPRINT
136*0a6a1f1dSLionel Sambuc     while (n & ~3) {
137ebfedea0SLionel Sambuc         sqr(r[0], r[1], a[0]);
138ebfedea0SLionel Sambuc         sqr(r[2], r[3], a[1]);
139ebfedea0SLionel Sambuc         sqr(r[4], r[5], a[2]);
140ebfedea0SLionel Sambuc         sqr(r[6], r[7], a[3]);
141*0a6a1f1dSLionel Sambuc         a += 4;
142*0a6a1f1dSLionel Sambuc         r += 8;
143*0a6a1f1dSLionel Sambuc         n -= 4;
144ebfedea0SLionel Sambuc     }
145ebfedea0SLionel Sambuc # endif
146*0a6a1f1dSLionel Sambuc     while (n) {
147ebfedea0SLionel Sambuc         sqr(r[0], r[1], a[0]);
148*0a6a1f1dSLionel Sambuc         a++;
149*0a6a1f1dSLionel Sambuc         r += 2;
150*0a6a1f1dSLionel Sambuc         n--;
151ebfedea0SLionel Sambuc     }
152ebfedea0SLionel Sambuc }
153ebfedea0SLionel Sambuc 
154*0a6a1f1dSLionel Sambuc #else                           /* !(defined(BN_LLONG) ||
155*0a6a1f1dSLionel Sambuc                                  * defined(BN_UMULT_HIGH)) */
156ebfedea0SLionel Sambuc 
bn_mul_add_words(BN_ULONG * rp,const BN_ULONG * ap,int num,BN_ULONG w)157*0a6a1f1dSLionel Sambuc BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
158*0a6a1f1dSLionel Sambuc                           BN_ULONG w)
159ebfedea0SLionel Sambuc {
160ebfedea0SLionel Sambuc     BN_ULONG c = 0;
161ebfedea0SLionel Sambuc     BN_ULONG bl, bh;
162ebfedea0SLionel Sambuc 
163ebfedea0SLionel Sambuc     assert(num >= 0);
164*0a6a1f1dSLionel Sambuc     if (num <= 0)
165*0a6a1f1dSLionel Sambuc         return ((BN_ULONG)0);
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc     bl = LBITS(w);
168ebfedea0SLionel Sambuc     bh = HBITS(w);
169ebfedea0SLionel Sambuc 
170ebfedea0SLionel Sambuc # ifndef OPENSSL_SMALL_FOOTPRINT
171*0a6a1f1dSLionel Sambuc     while (num & ~3) {
172ebfedea0SLionel Sambuc         mul_add(rp[0], ap[0], bl, bh, c);
173ebfedea0SLionel Sambuc         mul_add(rp[1], ap[1], bl, bh, c);
174ebfedea0SLionel Sambuc         mul_add(rp[2], ap[2], bl, bh, c);
175ebfedea0SLionel Sambuc         mul_add(rp[3], ap[3], bl, bh, c);
176*0a6a1f1dSLionel Sambuc         ap += 4;
177*0a6a1f1dSLionel Sambuc         rp += 4;
178*0a6a1f1dSLionel Sambuc         num -= 4;
179ebfedea0SLionel Sambuc     }
180ebfedea0SLionel Sambuc # endif
181*0a6a1f1dSLionel Sambuc     while (num) {
182ebfedea0SLionel Sambuc         mul_add(rp[0], ap[0], bl, bh, c);
183*0a6a1f1dSLionel Sambuc         ap++;
184*0a6a1f1dSLionel Sambuc         rp++;
185*0a6a1f1dSLionel Sambuc         num--;
186ebfedea0SLionel Sambuc     }
187ebfedea0SLionel Sambuc     return (c);
188ebfedea0SLionel Sambuc }
189ebfedea0SLionel Sambuc 
bn_mul_words(BN_ULONG * rp,const BN_ULONG * ap,int num,BN_ULONG w)190ebfedea0SLionel Sambuc BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
191ebfedea0SLionel Sambuc {
192ebfedea0SLionel Sambuc     BN_ULONG carry = 0;
193ebfedea0SLionel Sambuc     BN_ULONG bl, bh;
194ebfedea0SLionel Sambuc 
195ebfedea0SLionel Sambuc     assert(num >= 0);
196*0a6a1f1dSLionel Sambuc     if (num <= 0)
197*0a6a1f1dSLionel Sambuc         return ((BN_ULONG)0);
198ebfedea0SLionel Sambuc 
199ebfedea0SLionel Sambuc     bl = LBITS(w);
200ebfedea0SLionel Sambuc     bh = HBITS(w);
201ebfedea0SLionel Sambuc 
202ebfedea0SLionel Sambuc # ifndef OPENSSL_SMALL_FOOTPRINT
203*0a6a1f1dSLionel Sambuc     while (num & ~3) {
204ebfedea0SLionel Sambuc         mul(rp[0], ap[0], bl, bh, carry);
205ebfedea0SLionel Sambuc         mul(rp[1], ap[1], bl, bh, carry);
206ebfedea0SLionel Sambuc         mul(rp[2], ap[2], bl, bh, carry);
207ebfedea0SLionel Sambuc         mul(rp[3], ap[3], bl, bh, carry);
208*0a6a1f1dSLionel Sambuc         ap += 4;
209*0a6a1f1dSLionel Sambuc         rp += 4;
210*0a6a1f1dSLionel Sambuc         num -= 4;
211ebfedea0SLionel Sambuc     }
212ebfedea0SLionel Sambuc # endif
213*0a6a1f1dSLionel Sambuc     while (num) {
214ebfedea0SLionel Sambuc         mul(rp[0], ap[0], bl, bh, carry);
215*0a6a1f1dSLionel Sambuc         ap++;
216*0a6a1f1dSLionel Sambuc         rp++;
217*0a6a1f1dSLionel Sambuc         num--;
218ebfedea0SLionel Sambuc     }
219ebfedea0SLionel Sambuc     return (carry);
220ebfedea0SLionel Sambuc }
221ebfedea0SLionel Sambuc 
bn_sqr_words(BN_ULONG * r,const BN_ULONG * a,int n)222ebfedea0SLionel Sambuc void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
223ebfedea0SLionel Sambuc {
224ebfedea0SLionel Sambuc     assert(n >= 0);
225*0a6a1f1dSLionel Sambuc     if (n <= 0)
226*0a6a1f1dSLionel Sambuc         return;
227ebfedea0SLionel Sambuc 
228ebfedea0SLionel Sambuc # ifndef OPENSSL_SMALL_FOOTPRINT
229*0a6a1f1dSLionel Sambuc     while (n & ~3) {
230ebfedea0SLionel Sambuc         sqr64(r[0], r[1], a[0]);
231ebfedea0SLionel Sambuc         sqr64(r[2], r[3], a[1]);
232ebfedea0SLionel Sambuc         sqr64(r[4], r[5], a[2]);
233ebfedea0SLionel Sambuc         sqr64(r[6], r[7], a[3]);
234*0a6a1f1dSLionel Sambuc         a += 4;
235*0a6a1f1dSLionel Sambuc         r += 8;
236*0a6a1f1dSLionel Sambuc         n -= 4;
237ebfedea0SLionel Sambuc     }
238ebfedea0SLionel Sambuc # endif
239*0a6a1f1dSLionel Sambuc     while (n) {
240ebfedea0SLionel Sambuc         sqr64(r[0], r[1], a[0]);
241*0a6a1f1dSLionel Sambuc         a++;
242*0a6a1f1dSLionel Sambuc         r += 2;
243*0a6a1f1dSLionel Sambuc         n--;
244ebfedea0SLionel Sambuc     }
245ebfedea0SLionel Sambuc }
246ebfedea0SLionel Sambuc 
247*0a6a1f1dSLionel Sambuc #endif                          /* !(defined(BN_LLONG) ||
248*0a6a1f1dSLionel Sambuc                                  * defined(BN_UMULT_HIGH)) */
249ebfedea0SLionel Sambuc 
250ebfedea0SLionel Sambuc #if defined(BN_LLONG) && defined(BN_DIV2W)
251ebfedea0SLionel Sambuc 
bn_div_words(BN_ULONG h,BN_ULONG l,BN_ULONG d)252ebfedea0SLionel Sambuc BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
253ebfedea0SLionel Sambuc {
254ebfedea0SLionel Sambuc     return ((BN_ULONG)(((((BN_ULLONG) h) << BN_BITS2) | l) / (BN_ULLONG) d));
255ebfedea0SLionel Sambuc }
256ebfedea0SLionel Sambuc 
257ebfedea0SLionel Sambuc #else
258ebfedea0SLionel Sambuc 
259ebfedea0SLionel Sambuc /* Divide h,l by d and return the result. */
260ebfedea0SLionel Sambuc /* I need to test this some more :-( */
bn_div_words(BN_ULONG h,BN_ULONG l,BN_ULONG d)261ebfedea0SLionel Sambuc BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
262ebfedea0SLionel Sambuc {
263ebfedea0SLionel Sambuc     BN_ULONG dh, dl, q, ret = 0, th, tl, t;
264ebfedea0SLionel Sambuc     int i, count = 2;
265ebfedea0SLionel Sambuc 
266*0a6a1f1dSLionel Sambuc     if (d == 0)
267*0a6a1f1dSLionel Sambuc         return (BN_MASK2);
268ebfedea0SLionel Sambuc 
269ebfedea0SLionel Sambuc     i = BN_num_bits_word(d);
270ebfedea0SLionel Sambuc     assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
271ebfedea0SLionel Sambuc 
272ebfedea0SLionel Sambuc     i = BN_BITS2 - i;
273*0a6a1f1dSLionel Sambuc     if (h >= d)
274*0a6a1f1dSLionel Sambuc         h -= d;
275ebfedea0SLionel Sambuc 
276*0a6a1f1dSLionel Sambuc     if (i) {
277ebfedea0SLionel Sambuc         d <<= i;
278ebfedea0SLionel Sambuc         h = (h << i) | (l >> (BN_BITS2 - i));
279ebfedea0SLionel Sambuc         l <<= i;
280ebfedea0SLionel Sambuc     }
281ebfedea0SLionel Sambuc     dh = (d & BN_MASK2h) >> BN_BITS4;
282ebfedea0SLionel Sambuc     dl = (d & BN_MASK2l);
283*0a6a1f1dSLionel Sambuc     for (;;) {
284ebfedea0SLionel Sambuc         if ((h >> BN_BITS4) == dh)
285ebfedea0SLionel Sambuc             q = BN_MASK2l;
286ebfedea0SLionel Sambuc         else
287ebfedea0SLionel Sambuc             q = h / dh;
288ebfedea0SLionel Sambuc 
289ebfedea0SLionel Sambuc         th = q * dh;
290ebfedea0SLionel Sambuc         tl = dl * q;
291*0a6a1f1dSLionel Sambuc         for (;;) {
292ebfedea0SLionel Sambuc             t = h - th;
293ebfedea0SLionel Sambuc             if ((t & BN_MASK2h) ||
294*0a6a1f1dSLionel Sambuc                 ((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4))))
295ebfedea0SLionel Sambuc                 break;
296ebfedea0SLionel Sambuc             q--;
297ebfedea0SLionel Sambuc             th -= dh;
298ebfedea0SLionel Sambuc             tl -= dl;
299ebfedea0SLionel Sambuc         }
300ebfedea0SLionel Sambuc         t = (tl >> BN_BITS4);
301ebfedea0SLionel Sambuc         tl = (tl << BN_BITS4) & BN_MASK2h;
302ebfedea0SLionel Sambuc         th += t;
303ebfedea0SLionel Sambuc 
304*0a6a1f1dSLionel Sambuc         if (l < tl)
305*0a6a1f1dSLionel Sambuc             th++;
306ebfedea0SLionel Sambuc         l -= tl;
307*0a6a1f1dSLionel Sambuc         if (h < th) {
308ebfedea0SLionel Sambuc             h += d;
309ebfedea0SLionel Sambuc             q--;
310ebfedea0SLionel Sambuc         }
311ebfedea0SLionel Sambuc         h -= th;
312ebfedea0SLionel Sambuc 
313*0a6a1f1dSLionel Sambuc         if (--count == 0)
314*0a6a1f1dSLionel Sambuc             break;
315ebfedea0SLionel Sambuc 
316ebfedea0SLionel Sambuc         ret = q << BN_BITS4;
317ebfedea0SLionel Sambuc         h = ((h << BN_BITS4) | (l >> BN_BITS4)) & BN_MASK2;
318ebfedea0SLionel Sambuc         l = (l & BN_MASK2l) << BN_BITS4;
319ebfedea0SLionel Sambuc     }
320ebfedea0SLionel Sambuc     ret |= q;
321ebfedea0SLionel Sambuc     return (ret);
322ebfedea0SLionel Sambuc }
323ebfedea0SLionel Sambuc #endif                          /* !defined(BN_LLONG) && defined(BN_DIV2W) */
324ebfedea0SLionel Sambuc 
325ebfedea0SLionel Sambuc #ifdef BN_LLONG
bn_add_words(BN_ULONG * r,const BN_ULONG * a,const BN_ULONG * b,int n)326*0a6a1f1dSLionel Sambuc BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
327*0a6a1f1dSLionel Sambuc                       int n)
328ebfedea0SLionel Sambuc {
329ebfedea0SLionel Sambuc     BN_ULLONG ll = 0;
330ebfedea0SLionel Sambuc 
331ebfedea0SLionel Sambuc     assert(n >= 0);
332*0a6a1f1dSLionel Sambuc     if (n <= 0)
333*0a6a1f1dSLionel Sambuc         return ((BN_ULONG)0);
334ebfedea0SLionel Sambuc 
335ebfedea0SLionel Sambuc # ifndef OPENSSL_SMALL_FOOTPRINT
336*0a6a1f1dSLionel Sambuc     while (n & ~3) {
337ebfedea0SLionel Sambuc         ll += (BN_ULLONG) a[0] + b[0];
338ebfedea0SLionel Sambuc         r[0] = (BN_ULONG)ll & BN_MASK2;
339ebfedea0SLionel Sambuc         ll >>= BN_BITS2;
340ebfedea0SLionel Sambuc         ll += (BN_ULLONG) a[1] + b[1];
341ebfedea0SLionel Sambuc         r[1] = (BN_ULONG)ll & BN_MASK2;
342ebfedea0SLionel Sambuc         ll >>= BN_BITS2;
343ebfedea0SLionel Sambuc         ll += (BN_ULLONG) a[2] + b[2];
344ebfedea0SLionel Sambuc         r[2] = (BN_ULONG)ll & BN_MASK2;
345ebfedea0SLionel Sambuc         ll >>= BN_BITS2;
346ebfedea0SLionel Sambuc         ll += (BN_ULLONG) a[3] + b[3];
347ebfedea0SLionel Sambuc         r[3] = (BN_ULONG)ll & BN_MASK2;
348ebfedea0SLionel Sambuc         ll >>= BN_BITS2;
349*0a6a1f1dSLionel Sambuc         a += 4;
350*0a6a1f1dSLionel Sambuc         b += 4;
351*0a6a1f1dSLionel Sambuc         r += 4;
352*0a6a1f1dSLionel Sambuc         n -= 4;
353ebfedea0SLionel Sambuc     }
354ebfedea0SLionel Sambuc # endif
355*0a6a1f1dSLionel Sambuc     while (n) {
356ebfedea0SLionel Sambuc         ll += (BN_ULLONG) a[0] + b[0];
357ebfedea0SLionel Sambuc         r[0] = (BN_ULONG)ll & BN_MASK2;
358ebfedea0SLionel Sambuc         ll >>= BN_BITS2;
359*0a6a1f1dSLionel Sambuc         a++;
360*0a6a1f1dSLionel Sambuc         b++;
361*0a6a1f1dSLionel Sambuc         r++;
362*0a6a1f1dSLionel Sambuc         n--;
363ebfedea0SLionel Sambuc     }
364ebfedea0SLionel Sambuc     return ((BN_ULONG)ll);
365ebfedea0SLionel Sambuc }
366ebfedea0SLionel Sambuc #else                           /* !BN_LLONG */
bn_add_words(BN_ULONG * r,const BN_ULONG * a,const BN_ULONG * b,int n)367*0a6a1f1dSLionel Sambuc BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
368*0a6a1f1dSLionel Sambuc                       int n)
369ebfedea0SLionel Sambuc {
370ebfedea0SLionel Sambuc     BN_ULONG c, l, t;
371ebfedea0SLionel Sambuc 
372ebfedea0SLionel Sambuc     assert(n >= 0);
373*0a6a1f1dSLionel Sambuc     if (n <= 0)
374*0a6a1f1dSLionel Sambuc         return ((BN_ULONG)0);
375ebfedea0SLionel Sambuc 
376ebfedea0SLionel Sambuc     c = 0;
377ebfedea0SLionel Sambuc # ifndef OPENSSL_SMALL_FOOTPRINT
378*0a6a1f1dSLionel Sambuc     while (n & ~3) {
379ebfedea0SLionel Sambuc         t = a[0];
380ebfedea0SLionel Sambuc         t = (t + c) & BN_MASK2;
381ebfedea0SLionel Sambuc         c = (t < c);
382ebfedea0SLionel Sambuc         l = (t + b[0]) & BN_MASK2;
383ebfedea0SLionel Sambuc         c += (l < t);
384ebfedea0SLionel Sambuc         r[0] = l;
385ebfedea0SLionel Sambuc         t = a[1];
386ebfedea0SLionel Sambuc         t = (t + c) & BN_MASK2;
387ebfedea0SLionel Sambuc         c = (t < c);
388ebfedea0SLionel Sambuc         l = (t + b[1]) & BN_MASK2;
389ebfedea0SLionel Sambuc         c += (l < t);
390ebfedea0SLionel Sambuc         r[1] = l;
391ebfedea0SLionel Sambuc         t = a[2];
392ebfedea0SLionel Sambuc         t = (t + c) & BN_MASK2;
393ebfedea0SLionel Sambuc         c = (t < c);
394ebfedea0SLionel Sambuc         l = (t + b[2]) & BN_MASK2;
395ebfedea0SLionel Sambuc         c += (l < t);
396ebfedea0SLionel Sambuc         r[2] = l;
397ebfedea0SLionel Sambuc         t = a[3];
398ebfedea0SLionel Sambuc         t = (t + c) & BN_MASK2;
399ebfedea0SLionel Sambuc         c = (t < c);
400ebfedea0SLionel Sambuc         l = (t + b[3]) & BN_MASK2;
401ebfedea0SLionel Sambuc         c += (l < t);
402ebfedea0SLionel Sambuc         r[3] = l;
403*0a6a1f1dSLionel Sambuc         a += 4;
404*0a6a1f1dSLionel Sambuc         b += 4;
405*0a6a1f1dSLionel Sambuc         r += 4;
406*0a6a1f1dSLionel Sambuc         n -= 4;
407ebfedea0SLionel Sambuc     }
408ebfedea0SLionel Sambuc # endif
409*0a6a1f1dSLionel Sambuc     while (n) {
410ebfedea0SLionel Sambuc         t = a[0];
411ebfedea0SLionel Sambuc         t = (t + c) & BN_MASK2;
412ebfedea0SLionel Sambuc         c = (t < c);
413ebfedea0SLionel Sambuc         l = (t + b[0]) & BN_MASK2;
414ebfedea0SLionel Sambuc         c += (l < t);
415ebfedea0SLionel Sambuc         r[0] = l;
416*0a6a1f1dSLionel Sambuc         a++;
417*0a6a1f1dSLionel Sambuc         b++;
418*0a6a1f1dSLionel Sambuc         r++;
419*0a6a1f1dSLionel Sambuc         n--;
420ebfedea0SLionel Sambuc     }
421ebfedea0SLionel Sambuc     return ((BN_ULONG)c);
422ebfedea0SLionel Sambuc }
423ebfedea0SLionel Sambuc #endif                          /* !BN_LLONG */
424ebfedea0SLionel Sambuc 
bn_sub_words(BN_ULONG * r,const BN_ULONG * a,const BN_ULONG * b,int n)425*0a6a1f1dSLionel Sambuc BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
426*0a6a1f1dSLionel Sambuc                       int n)
427ebfedea0SLionel Sambuc {
428ebfedea0SLionel Sambuc     BN_ULONG t1, t2;
429ebfedea0SLionel Sambuc     int c = 0;
430ebfedea0SLionel Sambuc 
431ebfedea0SLionel Sambuc     assert(n >= 0);
432*0a6a1f1dSLionel Sambuc     if (n <= 0)
433*0a6a1f1dSLionel Sambuc         return ((BN_ULONG)0);
434ebfedea0SLionel Sambuc 
435ebfedea0SLionel Sambuc #ifndef OPENSSL_SMALL_FOOTPRINT
436*0a6a1f1dSLionel Sambuc     while (n & ~3) {
437*0a6a1f1dSLionel Sambuc         t1 = a[0];
438*0a6a1f1dSLionel Sambuc         t2 = b[0];
439ebfedea0SLionel Sambuc         r[0] = (t1 - t2 - c) & BN_MASK2;
440*0a6a1f1dSLionel Sambuc         if (t1 != t2)
441*0a6a1f1dSLionel Sambuc             c = (t1 < t2);
442*0a6a1f1dSLionel Sambuc         t1 = a[1];
443*0a6a1f1dSLionel Sambuc         t2 = b[1];
444ebfedea0SLionel Sambuc         r[1] = (t1 - t2 - c) & BN_MASK2;
445*0a6a1f1dSLionel Sambuc         if (t1 != t2)
446*0a6a1f1dSLionel Sambuc             c = (t1 < t2);
447*0a6a1f1dSLionel Sambuc         t1 = a[2];
448*0a6a1f1dSLionel Sambuc         t2 = b[2];
449ebfedea0SLionel Sambuc         r[2] = (t1 - t2 - c) & BN_MASK2;
450*0a6a1f1dSLionel Sambuc         if (t1 != t2)
451*0a6a1f1dSLionel Sambuc             c = (t1 < t2);
452*0a6a1f1dSLionel Sambuc         t1 = a[3];
453*0a6a1f1dSLionel Sambuc         t2 = b[3];
454ebfedea0SLionel Sambuc         r[3] = (t1 - t2 - c) & BN_MASK2;
455*0a6a1f1dSLionel Sambuc         if (t1 != t2)
456*0a6a1f1dSLionel Sambuc             c = (t1 < t2);
457*0a6a1f1dSLionel Sambuc         a += 4;
458*0a6a1f1dSLionel Sambuc         b += 4;
459*0a6a1f1dSLionel Sambuc         r += 4;
460*0a6a1f1dSLionel Sambuc         n -= 4;
461ebfedea0SLionel Sambuc     }
462ebfedea0SLionel Sambuc #endif
463*0a6a1f1dSLionel Sambuc     while (n) {
464*0a6a1f1dSLionel Sambuc         t1 = a[0];
465*0a6a1f1dSLionel Sambuc         t2 = b[0];
466ebfedea0SLionel Sambuc         r[0] = (t1 - t2 - c) & BN_MASK2;
467*0a6a1f1dSLionel Sambuc         if (t1 != t2)
468*0a6a1f1dSLionel Sambuc             c = (t1 < t2);
469*0a6a1f1dSLionel Sambuc         a++;
470*0a6a1f1dSLionel Sambuc         b++;
471*0a6a1f1dSLionel Sambuc         r++;
472*0a6a1f1dSLionel Sambuc         n--;
473ebfedea0SLionel Sambuc     }
474ebfedea0SLionel Sambuc     return (c);
475ebfedea0SLionel Sambuc }
476ebfedea0SLionel Sambuc 
477ebfedea0SLionel Sambuc #if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
478ebfedea0SLionel Sambuc 
479ebfedea0SLionel Sambuc # undef bn_mul_comba8
480ebfedea0SLionel Sambuc # undef bn_mul_comba4
481ebfedea0SLionel Sambuc # undef bn_sqr_comba8
482ebfedea0SLionel Sambuc # undef bn_sqr_comba4
483ebfedea0SLionel Sambuc 
484ebfedea0SLionel Sambuc /* mul_add_c(a,b,c0,c1,c2)  -- c+=a*b for three word number c=(c2,c1,c0) */
485ebfedea0SLionel Sambuc /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
486ebfedea0SLionel Sambuc /* sqr_add_c(a,i,c0,c1,c2)  -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
487*0a6a1f1dSLionel Sambuc /*
488*0a6a1f1dSLionel Sambuc  * sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number
489*0a6a1f1dSLionel Sambuc  * c=(c2,c1,c0)
490*0a6a1f1dSLionel Sambuc  */
491ebfedea0SLionel Sambuc 
492*0a6a1f1dSLionel Sambuc /*
493*0a6a1f1dSLionel Sambuc  * Keep in mind that carrying into high part of multiplication result
494*0a6a1f1dSLionel Sambuc  * can not overflow, because it cannot be all-ones.
495*0a6a1f1dSLionel Sambuc  */
496ebfedea0SLionel Sambuc # ifdef BN_LLONG
497ebfedea0SLionel Sambuc #  define mul_add_c(a,b,c0,c1,c2) \
498ebfedea0SLionel Sambuc         t=(BN_ULLONG)a*b; \
499ebfedea0SLionel Sambuc         t1=(BN_ULONG)Lw(t); \
500ebfedea0SLionel Sambuc         t2=(BN_ULONG)Hw(t); \
501ebfedea0SLionel Sambuc         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
502ebfedea0SLionel Sambuc         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
503ebfedea0SLionel Sambuc 
504ebfedea0SLionel Sambuc #  define mul_add_c2(a,b,c0,c1,c2) \
505ebfedea0SLionel Sambuc         t=(BN_ULLONG)a*b; \
506ebfedea0SLionel Sambuc         tt=(t+t)&BN_MASK; \
507ebfedea0SLionel Sambuc         if (tt < t) c2++; \
508ebfedea0SLionel Sambuc         t1=(BN_ULONG)Lw(tt); \
509ebfedea0SLionel Sambuc         t2=(BN_ULONG)Hw(tt); \
510ebfedea0SLionel Sambuc         c0=(c0+t1)&BN_MASK2;  \
511ebfedea0SLionel Sambuc         if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
512ebfedea0SLionel Sambuc         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
513ebfedea0SLionel Sambuc 
514ebfedea0SLionel Sambuc #  define sqr_add_c(a,i,c0,c1,c2) \
515ebfedea0SLionel Sambuc         t=(BN_ULLONG)a[i]*a[i]; \
516ebfedea0SLionel Sambuc         t1=(BN_ULONG)Lw(t); \
517ebfedea0SLionel Sambuc         t2=(BN_ULONG)Hw(t); \
518ebfedea0SLionel Sambuc         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
519ebfedea0SLionel Sambuc         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
520ebfedea0SLionel Sambuc 
521ebfedea0SLionel Sambuc #  define sqr_add_c2(a,i,j,c0,c1,c2) \
522ebfedea0SLionel Sambuc         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
523ebfedea0SLionel Sambuc 
524ebfedea0SLionel Sambuc # elif defined(BN_UMULT_LOHI)
525ebfedea0SLionel Sambuc 
526ebfedea0SLionel Sambuc #  define mul_add_c(a,b,c0,c1,c2) {       \
527ebfedea0SLionel Sambuc         BN_ULONG ta=(a),tb=(b);         \
528ebfedea0SLionel Sambuc         BN_UMULT_LOHI(t1,t2,ta,tb);     \
529ebfedea0SLionel Sambuc         c0 += t1; t2 += (c0<t1)?1:0;    \
530ebfedea0SLionel Sambuc         c1 += t2; c2 += (c1<t2)?1:0;    \
531ebfedea0SLionel Sambuc         }
532ebfedea0SLionel Sambuc 
533ebfedea0SLionel Sambuc #  define mul_add_c2(a,b,c0,c1,c2) {      \
534ebfedea0SLionel Sambuc         BN_ULONG ta=(a),tb=(b),t0;      \
535ebfedea0SLionel Sambuc         BN_UMULT_LOHI(t0,t1,ta,tb);     \
536*0a6a1f1dSLionel Sambuc         c0 += t0; t2 = t1+((c0<t0)?1:0);\
537ebfedea0SLionel Sambuc         c1 += t2; c2 += (c1<t2)?1:0;    \
538*0a6a1f1dSLionel Sambuc         c0 += t0; t1 += (c0<t0)?1:0;    \
539*0a6a1f1dSLionel Sambuc         c1 += t1; c2 += (c1<t1)?1:0;    \
540ebfedea0SLionel Sambuc         }
541ebfedea0SLionel Sambuc 
542ebfedea0SLionel Sambuc #  define sqr_add_c(a,i,c0,c1,c2) {       \
543ebfedea0SLionel Sambuc         BN_ULONG ta=(a)[i];             \
544ebfedea0SLionel Sambuc         BN_UMULT_LOHI(t1,t2,ta,ta);     \
545ebfedea0SLionel Sambuc         c0 += t1; t2 += (c0<t1)?1:0;    \
546ebfedea0SLionel Sambuc         c1 += t2; c2 += (c1<t2)?1:0;    \
547ebfedea0SLionel Sambuc         }
548ebfedea0SLionel Sambuc 
549ebfedea0SLionel Sambuc #  define sqr_add_c2(a,i,j,c0,c1,c2)    \
550ebfedea0SLionel Sambuc         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
551ebfedea0SLionel Sambuc 
552ebfedea0SLionel Sambuc # elif defined(BN_UMULT_HIGH)
553ebfedea0SLionel Sambuc 
554ebfedea0SLionel Sambuc #  define mul_add_c(a,b,c0,c1,c2) {       \
555ebfedea0SLionel Sambuc         BN_ULONG ta=(a),tb=(b);         \
556ebfedea0SLionel Sambuc         t1 = ta * tb;                   \
557ebfedea0SLionel Sambuc         t2 = BN_UMULT_HIGH(ta,tb);      \
558ebfedea0SLionel Sambuc         c0 += t1; t2 += (c0<t1)?1:0;    \
559ebfedea0SLionel Sambuc         c1 += t2; c2 += (c1<t2)?1:0;    \
560ebfedea0SLionel Sambuc         }
561ebfedea0SLionel Sambuc 
562ebfedea0SLionel Sambuc #  define mul_add_c2(a,b,c0,c1,c2) {      \
563ebfedea0SLionel Sambuc         BN_ULONG ta=(a),tb=(b),t0;      \
564ebfedea0SLionel Sambuc         t1 = BN_UMULT_HIGH(ta,tb);      \
565ebfedea0SLionel Sambuc         t0 = ta * tb;                   \
566*0a6a1f1dSLionel Sambuc         c0 += t0; t2 = t1+((c0<t0)?1:0);\
567ebfedea0SLionel Sambuc         c1 += t2; c2 += (c1<t2)?1:0;    \
568*0a6a1f1dSLionel Sambuc         c0 += t0; t1 += (c0<t0)?1:0;    \
569*0a6a1f1dSLionel Sambuc         c1 += t1; c2 += (c1<t1)?1:0;    \
570ebfedea0SLionel Sambuc         }
571ebfedea0SLionel Sambuc 
572ebfedea0SLionel Sambuc #  define sqr_add_c(a,i,c0,c1,c2) {       \
573ebfedea0SLionel Sambuc         BN_ULONG ta=(a)[i];             \
574ebfedea0SLionel Sambuc         t1 = ta * ta;                   \
575ebfedea0SLionel Sambuc         t2 = BN_UMULT_HIGH(ta,ta);      \
576ebfedea0SLionel Sambuc         c0 += t1; t2 += (c0<t1)?1:0;    \
577ebfedea0SLionel Sambuc         c1 += t2; c2 += (c1<t2)?1:0;    \
578ebfedea0SLionel Sambuc         }
579ebfedea0SLionel Sambuc 
580ebfedea0SLionel Sambuc #  define sqr_add_c2(a,i,j,c0,c1,c2)      \
581ebfedea0SLionel Sambuc         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
582ebfedea0SLionel Sambuc 
583ebfedea0SLionel Sambuc # else                          /* !BN_LLONG */
584ebfedea0SLionel Sambuc #  define mul_add_c(a,b,c0,c1,c2) \
585ebfedea0SLionel Sambuc         t1=LBITS(a); t2=HBITS(a); \
586ebfedea0SLionel Sambuc         bl=LBITS(b); bh=HBITS(b); \
587ebfedea0SLionel Sambuc         mul64(t1,t2,bl,bh); \
588ebfedea0SLionel Sambuc         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
589ebfedea0SLionel Sambuc         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
590ebfedea0SLionel Sambuc 
591ebfedea0SLionel Sambuc #  define mul_add_c2(a,b,c0,c1,c2) \
592ebfedea0SLionel Sambuc         t1=LBITS(a); t2=HBITS(a); \
593ebfedea0SLionel Sambuc         bl=LBITS(b); bh=HBITS(b); \
594ebfedea0SLionel Sambuc         mul64(t1,t2,bl,bh); \
595ebfedea0SLionel Sambuc         if (t2 & BN_TBIT) c2++; \
596ebfedea0SLionel Sambuc         t2=(t2+t2)&BN_MASK2; \
597ebfedea0SLionel Sambuc         if (t1 & BN_TBIT) t2++; \
598ebfedea0SLionel Sambuc         t1=(t1+t1)&BN_MASK2; \
599ebfedea0SLionel Sambuc         c0=(c0+t1)&BN_MASK2;  \
600ebfedea0SLionel Sambuc         if ((c0 < t1) && (((++t2)&BN_MASK2) == 0)) c2++; \
601ebfedea0SLionel Sambuc         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
602ebfedea0SLionel Sambuc 
603ebfedea0SLionel Sambuc #  define sqr_add_c(a,i,c0,c1,c2) \
604ebfedea0SLionel Sambuc         sqr64(t1,t2,(a)[i]); \
605ebfedea0SLionel Sambuc         c0=(c0+t1)&BN_MASK2; if ((c0) < t1) t2++; \
606ebfedea0SLionel Sambuc         c1=(c1+t2)&BN_MASK2; if ((c1) < t2) c2++;
607ebfedea0SLionel Sambuc 
608ebfedea0SLionel Sambuc #  define sqr_add_c2(a,i,j,c0,c1,c2) \
609ebfedea0SLionel Sambuc         mul_add_c2((a)[i],(a)[j],c0,c1,c2)
610ebfedea0SLionel Sambuc # endif                         /* !BN_LLONG */
611ebfedea0SLionel Sambuc 
bn_mul_comba8(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b)612ebfedea0SLionel Sambuc void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
613ebfedea0SLionel Sambuc {
614ebfedea0SLionel Sambuc # ifdef BN_LLONG
615ebfedea0SLionel Sambuc     BN_ULLONG t;
616ebfedea0SLionel Sambuc # else
617ebfedea0SLionel Sambuc     BN_ULONG bl, bh;
618ebfedea0SLionel Sambuc # endif
619ebfedea0SLionel Sambuc     BN_ULONG t1, t2;
620ebfedea0SLionel Sambuc     BN_ULONG c1, c2, c3;
621ebfedea0SLionel Sambuc 
622ebfedea0SLionel Sambuc     c1 = 0;
623ebfedea0SLionel Sambuc     c2 = 0;
624ebfedea0SLionel Sambuc     c3 = 0;
625ebfedea0SLionel Sambuc     mul_add_c(a[0], b[0], c1, c2, c3);
626ebfedea0SLionel Sambuc     r[0] = c1;
627ebfedea0SLionel Sambuc     c1 = 0;
628ebfedea0SLionel Sambuc     mul_add_c(a[0], b[1], c2, c3, c1);
629ebfedea0SLionel Sambuc     mul_add_c(a[1], b[0], c2, c3, c1);
630ebfedea0SLionel Sambuc     r[1] = c2;
631ebfedea0SLionel Sambuc     c2 = 0;
632ebfedea0SLionel Sambuc     mul_add_c(a[2], b[0], c3, c1, c2);
633ebfedea0SLionel Sambuc     mul_add_c(a[1], b[1], c3, c1, c2);
634ebfedea0SLionel Sambuc     mul_add_c(a[0], b[2], c3, c1, c2);
635ebfedea0SLionel Sambuc     r[2] = c3;
636ebfedea0SLionel Sambuc     c3 = 0;
637ebfedea0SLionel Sambuc     mul_add_c(a[0], b[3], c1, c2, c3);
638ebfedea0SLionel Sambuc     mul_add_c(a[1], b[2], c1, c2, c3);
639ebfedea0SLionel Sambuc     mul_add_c(a[2], b[1], c1, c2, c3);
640ebfedea0SLionel Sambuc     mul_add_c(a[3], b[0], c1, c2, c3);
641ebfedea0SLionel Sambuc     r[3] = c1;
642ebfedea0SLionel Sambuc     c1 = 0;
643ebfedea0SLionel Sambuc     mul_add_c(a[4], b[0], c2, c3, c1);
644ebfedea0SLionel Sambuc     mul_add_c(a[3], b[1], c2, c3, c1);
645ebfedea0SLionel Sambuc     mul_add_c(a[2], b[2], c2, c3, c1);
646ebfedea0SLionel Sambuc     mul_add_c(a[1], b[3], c2, c3, c1);
647ebfedea0SLionel Sambuc     mul_add_c(a[0], b[4], c2, c3, c1);
648ebfedea0SLionel Sambuc     r[4] = c2;
649ebfedea0SLionel Sambuc     c2 = 0;
650ebfedea0SLionel Sambuc     mul_add_c(a[0], b[5], c3, c1, c2);
651ebfedea0SLionel Sambuc     mul_add_c(a[1], b[4], c3, c1, c2);
652ebfedea0SLionel Sambuc     mul_add_c(a[2], b[3], c3, c1, c2);
653ebfedea0SLionel Sambuc     mul_add_c(a[3], b[2], c3, c1, c2);
654ebfedea0SLionel Sambuc     mul_add_c(a[4], b[1], c3, c1, c2);
655ebfedea0SLionel Sambuc     mul_add_c(a[5], b[0], c3, c1, c2);
656ebfedea0SLionel Sambuc     r[5] = c3;
657ebfedea0SLionel Sambuc     c3 = 0;
658ebfedea0SLionel Sambuc     mul_add_c(a[6], b[0], c1, c2, c3);
659ebfedea0SLionel Sambuc     mul_add_c(a[5], b[1], c1, c2, c3);
660ebfedea0SLionel Sambuc     mul_add_c(a[4], b[2], c1, c2, c3);
661ebfedea0SLionel Sambuc     mul_add_c(a[3], b[3], c1, c2, c3);
662ebfedea0SLionel Sambuc     mul_add_c(a[2], b[4], c1, c2, c3);
663ebfedea0SLionel Sambuc     mul_add_c(a[1], b[5], c1, c2, c3);
664ebfedea0SLionel Sambuc     mul_add_c(a[0], b[6], c1, c2, c3);
665ebfedea0SLionel Sambuc     r[6] = c1;
666ebfedea0SLionel Sambuc     c1 = 0;
667ebfedea0SLionel Sambuc     mul_add_c(a[0], b[7], c2, c3, c1);
668ebfedea0SLionel Sambuc     mul_add_c(a[1], b[6], c2, c3, c1);
669ebfedea0SLionel Sambuc     mul_add_c(a[2], b[5], c2, c3, c1);
670ebfedea0SLionel Sambuc     mul_add_c(a[3], b[4], c2, c3, c1);
671ebfedea0SLionel Sambuc     mul_add_c(a[4], b[3], c2, c3, c1);
672ebfedea0SLionel Sambuc     mul_add_c(a[5], b[2], c2, c3, c1);
673ebfedea0SLionel Sambuc     mul_add_c(a[6], b[1], c2, c3, c1);
674ebfedea0SLionel Sambuc     mul_add_c(a[7], b[0], c2, c3, c1);
675ebfedea0SLionel Sambuc     r[7] = c2;
676ebfedea0SLionel Sambuc     c2 = 0;
677ebfedea0SLionel Sambuc     mul_add_c(a[7], b[1], c3, c1, c2);
678ebfedea0SLionel Sambuc     mul_add_c(a[6], b[2], c3, c1, c2);
679ebfedea0SLionel Sambuc     mul_add_c(a[5], b[3], c3, c1, c2);
680ebfedea0SLionel Sambuc     mul_add_c(a[4], b[4], c3, c1, c2);
681ebfedea0SLionel Sambuc     mul_add_c(a[3], b[5], c3, c1, c2);
682ebfedea0SLionel Sambuc     mul_add_c(a[2], b[6], c3, c1, c2);
683ebfedea0SLionel Sambuc     mul_add_c(a[1], b[7], c3, c1, c2);
684ebfedea0SLionel Sambuc     r[8] = c3;
685ebfedea0SLionel Sambuc     c3 = 0;
686ebfedea0SLionel Sambuc     mul_add_c(a[2], b[7], c1, c2, c3);
687ebfedea0SLionel Sambuc     mul_add_c(a[3], b[6], c1, c2, c3);
688ebfedea0SLionel Sambuc     mul_add_c(a[4], b[5], c1, c2, c3);
689ebfedea0SLionel Sambuc     mul_add_c(a[5], b[4], c1, c2, c3);
690ebfedea0SLionel Sambuc     mul_add_c(a[6], b[3], c1, c2, c3);
691ebfedea0SLionel Sambuc     mul_add_c(a[7], b[2], c1, c2, c3);
692ebfedea0SLionel Sambuc     r[9] = c1;
693ebfedea0SLionel Sambuc     c1 = 0;
694ebfedea0SLionel Sambuc     mul_add_c(a[7], b[3], c2, c3, c1);
695ebfedea0SLionel Sambuc     mul_add_c(a[6], b[4], c2, c3, c1);
696ebfedea0SLionel Sambuc     mul_add_c(a[5], b[5], c2, c3, c1);
697ebfedea0SLionel Sambuc     mul_add_c(a[4], b[6], c2, c3, c1);
698ebfedea0SLionel Sambuc     mul_add_c(a[3], b[7], c2, c3, c1);
699ebfedea0SLionel Sambuc     r[10] = c2;
700ebfedea0SLionel Sambuc     c2 = 0;
701ebfedea0SLionel Sambuc     mul_add_c(a[4], b[7], c3, c1, c2);
702ebfedea0SLionel Sambuc     mul_add_c(a[5], b[6], c3, c1, c2);
703ebfedea0SLionel Sambuc     mul_add_c(a[6], b[5], c3, c1, c2);
704ebfedea0SLionel Sambuc     mul_add_c(a[7], b[4], c3, c1, c2);
705ebfedea0SLionel Sambuc     r[11] = c3;
706ebfedea0SLionel Sambuc     c3 = 0;
707ebfedea0SLionel Sambuc     mul_add_c(a[7], b[5], c1, c2, c3);
708ebfedea0SLionel Sambuc     mul_add_c(a[6], b[6], c1, c2, c3);
709ebfedea0SLionel Sambuc     mul_add_c(a[5], b[7], c1, c2, c3);
710ebfedea0SLionel Sambuc     r[12] = c1;
711ebfedea0SLionel Sambuc     c1 = 0;
712ebfedea0SLionel Sambuc     mul_add_c(a[6], b[7], c2, c3, c1);
713ebfedea0SLionel Sambuc     mul_add_c(a[7], b[6], c2, c3, c1);
714ebfedea0SLionel Sambuc     r[13] = c2;
715ebfedea0SLionel Sambuc     c2 = 0;
716ebfedea0SLionel Sambuc     mul_add_c(a[7], b[7], c3, c1, c2);
717ebfedea0SLionel Sambuc     r[14] = c3;
718ebfedea0SLionel Sambuc     r[15] = c1;
719ebfedea0SLionel Sambuc }
720ebfedea0SLionel Sambuc 
bn_mul_comba4(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b)721ebfedea0SLionel Sambuc void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
722ebfedea0SLionel Sambuc {
723ebfedea0SLionel Sambuc # ifdef BN_LLONG
724ebfedea0SLionel Sambuc     BN_ULLONG t;
725ebfedea0SLionel Sambuc # else
726ebfedea0SLionel Sambuc     BN_ULONG bl, bh;
727ebfedea0SLionel Sambuc # endif
728ebfedea0SLionel Sambuc     BN_ULONG t1, t2;
729ebfedea0SLionel Sambuc     BN_ULONG c1, c2, c3;
730ebfedea0SLionel Sambuc 
731ebfedea0SLionel Sambuc     c1 = 0;
732ebfedea0SLionel Sambuc     c2 = 0;
733ebfedea0SLionel Sambuc     c3 = 0;
734ebfedea0SLionel Sambuc     mul_add_c(a[0], b[0], c1, c2, c3);
735ebfedea0SLionel Sambuc     r[0] = c1;
736ebfedea0SLionel Sambuc     c1 = 0;
737ebfedea0SLionel Sambuc     mul_add_c(a[0], b[1], c2, c3, c1);
738ebfedea0SLionel Sambuc     mul_add_c(a[1], b[0], c2, c3, c1);
739ebfedea0SLionel Sambuc     r[1] = c2;
740ebfedea0SLionel Sambuc     c2 = 0;
741ebfedea0SLionel Sambuc     mul_add_c(a[2], b[0], c3, c1, c2);
742ebfedea0SLionel Sambuc     mul_add_c(a[1], b[1], c3, c1, c2);
743ebfedea0SLionel Sambuc     mul_add_c(a[0], b[2], c3, c1, c2);
744ebfedea0SLionel Sambuc     r[2] = c3;
745ebfedea0SLionel Sambuc     c3 = 0;
746ebfedea0SLionel Sambuc     mul_add_c(a[0], b[3], c1, c2, c3);
747ebfedea0SLionel Sambuc     mul_add_c(a[1], b[2], c1, c2, c3);
748ebfedea0SLionel Sambuc     mul_add_c(a[2], b[1], c1, c2, c3);
749ebfedea0SLionel Sambuc     mul_add_c(a[3], b[0], c1, c2, c3);
750ebfedea0SLionel Sambuc     r[3] = c1;
751ebfedea0SLionel Sambuc     c1 = 0;
752ebfedea0SLionel Sambuc     mul_add_c(a[3], b[1], c2, c3, c1);
753ebfedea0SLionel Sambuc     mul_add_c(a[2], b[2], c2, c3, c1);
754ebfedea0SLionel Sambuc     mul_add_c(a[1], b[3], c2, c3, c1);
755ebfedea0SLionel Sambuc     r[4] = c2;
756ebfedea0SLionel Sambuc     c2 = 0;
757ebfedea0SLionel Sambuc     mul_add_c(a[2], b[3], c3, c1, c2);
758ebfedea0SLionel Sambuc     mul_add_c(a[3], b[2], c3, c1, c2);
759ebfedea0SLionel Sambuc     r[5] = c3;
760ebfedea0SLionel Sambuc     c3 = 0;
761ebfedea0SLionel Sambuc     mul_add_c(a[3], b[3], c1, c2, c3);
762ebfedea0SLionel Sambuc     r[6] = c1;
763ebfedea0SLionel Sambuc     r[7] = c2;
764ebfedea0SLionel Sambuc }
765ebfedea0SLionel Sambuc 
bn_sqr_comba8(BN_ULONG * r,const BN_ULONG * a)766ebfedea0SLionel Sambuc void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
767ebfedea0SLionel Sambuc {
768ebfedea0SLionel Sambuc # ifdef BN_LLONG
769ebfedea0SLionel Sambuc     BN_ULLONG t, tt;
770ebfedea0SLionel Sambuc # else
771ebfedea0SLionel Sambuc     BN_ULONG bl, bh;
772ebfedea0SLionel Sambuc # endif
773ebfedea0SLionel Sambuc     BN_ULONG t1, t2;
774ebfedea0SLionel Sambuc     BN_ULONG c1, c2, c3;
775ebfedea0SLionel Sambuc 
776ebfedea0SLionel Sambuc     c1 = 0;
777ebfedea0SLionel Sambuc     c2 = 0;
778ebfedea0SLionel Sambuc     c3 = 0;
779ebfedea0SLionel Sambuc     sqr_add_c(a, 0, c1, c2, c3);
780ebfedea0SLionel Sambuc     r[0] = c1;
781ebfedea0SLionel Sambuc     c1 = 0;
782ebfedea0SLionel Sambuc     sqr_add_c2(a, 1, 0, c2, c3, c1);
783ebfedea0SLionel Sambuc     r[1] = c2;
784ebfedea0SLionel Sambuc     c2 = 0;
785ebfedea0SLionel Sambuc     sqr_add_c(a, 1, c3, c1, c2);
786ebfedea0SLionel Sambuc     sqr_add_c2(a, 2, 0, c3, c1, c2);
787ebfedea0SLionel Sambuc     r[2] = c3;
788ebfedea0SLionel Sambuc     c3 = 0;
789ebfedea0SLionel Sambuc     sqr_add_c2(a, 3, 0, c1, c2, c3);
790ebfedea0SLionel Sambuc     sqr_add_c2(a, 2, 1, c1, c2, c3);
791ebfedea0SLionel Sambuc     r[3] = c1;
792ebfedea0SLionel Sambuc     c1 = 0;
793ebfedea0SLionel Sambuc     sqr_add_c(a, 2, c2, c3, c1);
794ebfedea0SLionel Sambuc     sqr_add_c2(a, 3, 1, c2, c3, c1);
795ebfedea0SLionel Sambuc     sqr_add_c2(a, 4, 0, c2, c3, c1);
796ebfedea0SLionel Sambuc     r[4] = c2;
797ebfedea0SLionel Sambuc     c2 = 0;
798ebfedea0SLionel Sambuc     sqr_add_c2(a, 5, 0, c3, c1, c2);
799ebfedea0SLionel Sambuc     sqr_add_c2(a, 4, 1, c3, c1, c2);
800ebfedea0SLionel Sambuc     sqr_add_c2(a, 3, 2, c3, c1, c2);
801ebfedea0SLionel Sambuc     r[5] = c3;
802ebfedea0SLionel Sambuc     c3 = 0;
803ebfedea0SLionel Sambuc     sqr_add_c(a, 3, c1, c2, c3);
804ebfedea0SLionel Sambuc     sqr_add_c2(a, 4, 2, c1, c2, c3);
805ebfedea0SLionel Sambuc     sqr_add_c2(a, 5, 1, c1, c2, c3);
806ebfedea0SLionel Sambuc     sqr_add_c2(a, 6, 0, c1, c2, c3);
807ebfedea0SLionel Sambuc     r[6] = c1;
808ebfedea0SLionel Sambuc     c1 = 0;
809ebfedea0SLionel Sambuc     sqr_add_c2(a, 7, 0, c2, c3, c1);
810ebfedea0SLionel Sambuc     sqr_add_c2(a, 6, 1, c2, c3, c1);
811ebfedea0SLionel Sambuc     sqr_add_c2(a, 5, 2, c2, c3, c1);
812ebfedea0SLionel Sambuc     sqr_add_c2(a, 4, 3, c2, c3, c1);
813ebfedea0SLionel Sambuc     r[7] = c2;
814ebfedea0SLionel Sambuc     c2 = 0;
815ebfedea0SLionel Sambuc     sqr_add_c(a, 4, c3, c1, c2);
816ebfedea0SLionel Sambuc     sqr_add_c2(a, 5, 3, c3, c1, c2);
817ebfedea0SLionel Sambuc     sqr_add_c2(a, 6, 2, c3, c1, c2);
818ebfedea0SLionel Sambuc     sqr_add_c2(a, 7, 1, c3, c1, c2);
819ebfedea0SLionel Sambuc     r[8] = c3;
820ebfedea0SLionel Sambuc     c3 = 0;
821ebfedea0SLionel Sambuc     sqr_add_c2(a, 7, 2, c1, c2, c3);
822ebfedea0SLionel Sambuc     sqr_add_c2(a, 6, 3, c1, c2, c3);
823ebfedea0SLionel Sambuc     sqr_add_c2(a, 5, 4, c1, c2, c3);
824ebfedea0SLionel Sambuc     r[9] = c1;
825ebfedea0SLionel Sambuc     c1 = 0;
826ebfedea0SLionel Sambuc     sqr_add_c(a, 5, c2, c3, c1);
827ebfedea0SLionel Sambuc     sqr_add_c2(a, 6, 4, c2, c3, c1);
828ebfedea0SLionel Sambuc     sqr_add_c2(a, 7, 3, c2, c3, c1);
829ebfedea0SLionel Sambuc     r[10] = c2;
830ebfedea0SLionel Sambuc     c2 = 0;
831ebfedea0SLionel Sambuc     sqr_add_c2(a, 7, 4, c3, c1, c2);
832ebfedea0SLionel Sambuc     sqr_add_c2(a, 6, 5, c3, c1, c2);
833ebfedea0SLionel Sambuc     r[11] = c3;
834ebfedea0SLionel Sambuc     c3 = 0;
835ebfedea0SLionel Sambuc     sqr_add_c(a, 6, c1, c2, c3);
836ebfedea0SLionel Sambuc     sqr_add_c2(a, 7, 5, c1, c2, c3);
837ebfedea0SLionel Sambuc     r[12] = c1;
838ebfedea0SLionel Sambuc     c1 = 0;
839ebfedea0SLionel Sambuc     sqr_add_c2(a, 7, 6, c2, c3, c1);
840ebfedea0SLionel Sambuc     r[13] = c2;
841ebfedea0SLionel Sambuc     c2 = 0;
842ebfedea0SLionel Sambuc     sqr_add_c(a, 7, c3, c1, c2);
843ebfedea0SLionel Sambuc     r[14] = c3;
844ebfedea0SLionel Sambuc     r[15] = c1;
845ebfedea0SLionel Sambuc }
846ebfedea0SLionel Sambuc 
bn_sqr_comba4(BN_ULONG * r,const BN_ULONG * a)847ebfedea0SLionel Sambuc void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
848ebfedea0SLionel Sambuc {
849ebfedea0SLionel Sambuc # ifdef BN_LLONG
850ebfedea0SLionel Sambuc     BN_ULLONG t, tt;
851ebfedea0SLionel Sambuc # else
852ebfedea0SLionel Sambuc     BN_ULONG bl, bh;
853ebfedea0SLionel Sambuc # endif
854ebfedea0SLionel Sambuc     BN_ULONG t1, t2;
855ebfedea0SLionel Sambuc     BN_ULONG c1, c2, c3;
856ebfedea0SLionel Sambuc 
857ebfedea0SLionel Sambuc     c1 = 0;
858ebfedea0SLionel Sambuc     c2 = 0;
859ebfedea0SLionel Sambuc     c3 = 0;
860ebfedea0SLionel Sambuc     sqr_add_c(a, 0, c1, c2, c3);
861ebfedea0SLionel Sambuc     r[0] = c1;
862ebfedea0SLionel Sambuc     c1 = 0;
863ebfedea0SLionel Sambuc     sqr_add_c2(a, 1, 0, c2, c3, c1);
864ebfedea0SLionel Sambuc     r[1] = c2;
865ebfedea0SLionel Sambuc     c2 = 0;
866ebfedea0SLionel Sambuc     sqr_add_c(a, 1, c3, c1, c2);
867ebfedea0SLionel Sambuc     sqr_add_c2(a, 2, 0, c3, c1, c2);
868ebfedea0SLionel Sambuc     r[2] = c3;
869ebfedea0SLionel Sambuc     c3 = 0;
870ebfedea0SLionel Sambuc     sqr_add_c2(a, 3, 0, c1, c2, c3);
871ebfedea0SLionel Sambuc     sqr_add_c2(a, 2, 1, c1, c2, c3);
872ebfedea0SLionel Sambuc     r[3] = c1;
873ebfedea0SLionel Sambuc     c1 = 0;
874ebfedea0SLionel Sambuc     sqr_add_c(a, 2, c2, c3, c1);
875ebfedea0SLionel Sambuc     sqr_add_c2(a, 3, 1, c2, c3, c1);
876ebfedea0SLionel Sambuc     r[4] = c2;
877ebfedea0SLionel Sambuc     c2 = 0;
878ebfedea0SLionel Sambuc     sqr_add_c2(a, 3, 2, c3, c1, c2);
879ebfedea0SLionel Sambuc     r[5] = c3;
880ebfedea0SLionel Sambuc     c3 = 0;
881ebfedea0SLionel Sambuc     sqr_add_c(a, 3, c1, c2, c3);
882ebfedea0SLionel Sambuc     r[6] = c1;
883ebfedea0SLionel Sambuc     r[7] = c2;
884ebfedea0SLionel Sambuc }
885ebfedea0SLionel Sambuc 
886ebfedea0SLionel Sambuc # ifdef OPENSSL_NO_ASM
887ebfedea0SLionel Sambuc #  ifdef OPENSSL_BN_ASM_MONT
888ebfedea0SLionel Sambuc #   include <alloca.h>
889ebfedea0SLionel Sambuc /*
890ebfedea0SLionel Sambuc  * This is essentially reference implementation, which may or may not
891ebfedea0SLionel Sambuc  * result in performance improvement. E.g. on IA-32 this routine was
892ebfedea0SLionel Sambuc  * observed to give 40% faster rsa1024 private key operations and 10%
893ebfedea0SLionel Sambuc  * faster rsa4096 ones, while on AMD64 it improves rsa1024 sign only
894ebfedea0SLionel Sambuc  * by 10% and *worsens* rsa4096 sign by 15%. Once again, it's a
895ebfedea0SLionel Sambuc  * reference implementation, one to be used as starting point for
896ebfedea0SLionel Sambuc  * platform-specific assembler. Mentioned numbers apply to compiler
897ebfedea0SLionel Sambuc  * generated code compiled with and without -DOPENSSL_BN_ASM_MONT and
898ebfedea0SLionel Sambuc  * can vary not only from platform to platform, but even for compiler
899ebfedea0SLionel Sambuc  * versions. Assembler vs. assembler improvement coefficients can
900ebfedea0SLionel Sambuc  * [and are known to] differ and are to be documented elsewhere.
901ebfedea0SLionel Sambuc  */
bn_mul_mont(BN_ULONG * rp,const BN_ULONG * ap,const BN_ULONG * bp,const BN_ULONG * np,const BN_ULONG * n0p,int num)902*0a6a1f1dSLionel Sambuc int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
903*0a6a1f1dSLionel Sambuc                 const BN_ULONG *np, const BN_ULONG *n0p, int num)
904ebfedea0SLionel Sambuc {
905ebfedea0SLionel Sambuc     BN_ULONG c0, c1, ml, *tp, n0;
906ebfedea0SLionel Sambuc #   ifdef mul64
907ebfedea0SLionel Sambuc     BN_ULONG mh;
908ebfedea0SLionel Sambuc #   endif
909ebfedea0SLionel Sambuc     volatile BN_ULONG *vp;
910ebfedea0SLionel Sambuc     int i = 0, j;
911ebfedea0SLionel Sambuc 
912*0a6a1f1dSLionel Sambuc #   if 0                        /* template for platform-specific
913*0a6a1f1dSLionel Sambuc                                  * implementation */
914*0a6a1f1dSLionel Sambuc     if (ap == bp)
915*0a6a1f1dSLionel Sambuc         return bn_sqr_mont(rp, ap, np, n0p, num);
916ebfedea0SLionel Sambuc #   endif
917ebfedea0SLionel Sambuc     vp = tp = alloca((num + 2) * sizeof(BN_ULONG));
918ebfedea0SLionel Sambuc 
919ebfedea0SLionel Sambuc     n0 = *n0p;
920ebfedea0SLionel Sambuc 
921ebfedea0SLionel Sambuc     c0 = 0;
922ebfedea0SLionel Sambuc     ml = bp[0];
923ebfedea0SLionel Sambuc #   ifdef mul64
924ebfedea0SLionel Sambuc     mh = HBITS(ml);
925ebfedea0SLionel Sambuc     ml = LBITS(ml);
926ebfedea0SLionel Sambuc     for (j = 0; j < num; ++j)
927ebfedea0SLionel Sambuc         mul(tp[j], ap[j], ml, mh, c0);
928ebfedea0SLionel Sambuc #   else
929ebfedea0SLionel Sambuc     for (j = 0; j < num; ++j)
930ebfedea0SLionel Sambuc         mul(tp[j], ap[j], ml, c0);
931ebfedea0SLionel Sambuc #   endif
932ebfedea0SLionel Sambuc 
933ebfedea0SLionel Sambuc     tp[num] = c0;
934ebfedea0SLionel Sambuc     tp[num + 1] = 0;
935ebfedea0SLionel Sambuc     goto enter;
936ebfedea0SLionel Sambuc 
937*0a6a1f1dSLionel Sambuc     for (i = 0; i < num; i++) {
938ebfedea0SLionel Sambuc         c0 = 0;
939ebfedea0SLionel Sambuc         ml = bp[i];
940ebfedea0SLionel Sambuc #   ifdef mul64
941ebfedea0SLionel Sambuc         mh = HBITS(ml);
942ebfedea0SLionel Sambuc         ml = LBITS(ml);
943ebfedea0SLionel Sambuc         for (j = 0; j < num; ++j)
944ebfedea0SLionel Sambuc             mul_add(tp[j], ap[j], ml, mh, c0);
945ebfedea0SLionel Sambuc #   else
946ebfedea0SLionel Sambuc         for (j = 0; j < num; ++j)
947ebfedea0SLionel Sambuc             mul_add(tp[j], ap[j], ml, c0);
948ebfedea0SLionel Sambuc #   endif
949ebfedea0SLionel Sambuc         c1 = (tp[num] + c0) & BN_MASK2;
950ebfedea0SLionel Sambuc         tp[num] = c1;
951ebfedea0SLionel Sambuc         tp[num + 1] = (c1 < c0 ? 1 : 0);
952ebfedea0SLionel Sambuc  enter:
953ebfedea0SLionel Sambuc         c1 = tp[0];
954ebfedea0SLionel Sambuc         ml = (c1 * n0) & BN_MASK2;
955ebfedea0SLionel Sambuc         c0 = 0;
956ebfedea0SLionel Sambuc #   ifdef mul64
957ebfedea0SLionel Sambuc         mh = HBITS(ml);
958ebfedea0SLionel Sambuc         ml = LBITS(ml);
959ebfedea0SLionel Sambuc         mul_add(c1, np[0], ml, mh, c0);
960ebfedea0SLionel Sambuc #   else
961ebfedea0SLionel Sambuc         mul_add(c1, ml, np[0], c0);
962ebfedea0SLionel Sambuc #   endif
963*0a6a1f1dSLionel Sambuc         for (j = 1; j < num; j++) {
964ebfedea0SLionel Sambuc             c1 = tp[j];
965ebfedea0SLionel Sambuc #   ifdef mul64
966ebfedea0SLionel Sambuc             mul_add(c1, np[j], ml, mh, c0);
967ebfedea0SLionel Sambuc #   else
968ebfedea0SLionel Sambuc             mul_add(c1, ml, np[j], c0);
969ebfedea0SLionel Sambuc #   endif
970ebfedea0SLionel Sambuc             tp[j - 1] = c1 & BN_MASK2;
971ebfedea0SLionel Sambuc         }
972ebfedea0SLionel Sambuc         c1 = (tp[num] + c0) & BN_MASK2;
973ebfedea0SLionel Sambuc         tp[num - 1] = c1;
974ebfedea0SLionel Sambuc         tp[num] = tp[num + 1] + (c1 < c0 ? 1 : 0);
975ebfedea0SLionel Sambuc     }
976ebfedea0SLionel Sambuc 
977*0a6a1f1dSLionel Sambuc     if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) {
978ebfedea0SLionel Sambuc         c0 = bn_sub_words(rp, tp, np, num);
979*0a6a1f1dSLionel Sambuc         if (tp[num] != 0 || c0 == 0) {
980*0a6a1f1dSLionel Sambuc             for (i = 0; i < num + 2; i++)
981*0a6a1f1dSLionel Sambuc                 vp[i] = 0;
982ebfedea0SLionel Sambuc             return 1;
983ebfedea0SLionel Sambuc         }
984ebfedea0SLionel Sambuc     }
985*0a6a1f1dSLionel Sambuc     for (i = 0; i < num; i++)
986*0a6a1f1dSLionel Sambuc         rp[i] = tp[i], vp[i] = 0;
987ebfedea0SLionel Sambuc     vp[num] = 0;
988ebfedea0SLionel Sambuc     vp[num + 1] = 0;
989ebfedea0SLionel Sambuc     return 1;
990ebfedea0SLionel Sambuc }
991ebfedea0SLionel Sambuc #  else
992ebfedea0SLionel Sambuc /*
993ebfedea0SLionel Sambuc  * Return value of 0 indicates that multiplication/convolution was not
994ebfedea0SLionel Sambuc  * performed to signal the caller to fall down to alternative/original
995ebfedea0SLionel Sambuc  * code-path.
996ebfedea0SLionel Sambuc  */
bn_mul_mont(BN_ULONG * rp,const BN_ULONG * ap,const BN_ULONG * bp,const BN_ULONG * np,const BN_ULONG * n0,int num)997*0a6a1f1dSLionel Sambuc int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
998*0a6a1f1dSLionel Sambuc                 const BN_ULONG *np, const BN_ULONG *n0, int num)
999*0a6a1f1dSLionel Sambuc {
1000*0a6a1f1dSLionel Sambuc     return 0;
1001*0a6a1f1dSLionel Sambuc }
1002ebfedea0SLionel Sambuc #  endif                        /* OPENSSL_BN_ASM_MONT */
1003ebfedea0SLionel Sambuc # endif
1004ebfedea0SLionel Sambuc 
1005ebfedea0SLionel Sambuc #else                           /* !BN_MUL_COMBA */
1006ebfedea0SLionel Sambuc 
1007ebfedea0SLionel Sambuc /* hmm... is it faster just to do a multiply? */
1008ebfedea0SLionel Sambuc # undef bn_sqr_comba4
bn_sqr_comba4(BN_ULONG * r,const BN_ULONG * a)1009ebfedea0SLionel Sambuc void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
1010ebfedea0SLionel Sambuc {
1011ebfedea0SLionel Sambuc     BN_ULONG t[8];
1012ebfedea0SLionel Sambuc     bn_sqr_normal(r, a, 4, t);
1013ebfedea0SLionel Sambuc }
1014ebfedea0SLionel Sambuc 
1015ebfedea0SLionel Sambuc # undef bn_sqr_comba8
bn_sqr_comba8(BN_ULONG * r,const BN_ULONG * a)1016ebfedea0SLionel Sambuc void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
1017ebfedea0SLionel Sambuc {
1018ebfedea0SLionel Sambuc     BN_ULONG t[16];
1019ebfedea0SLionel Sambuc     bn_sqr_normal(r, a, 8, t);
1020ebfedea0SLionel Sambuc }
1021ebfedea0SLionel Sambuc 
bn_mul_comba4(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b)1022ebfedea0SLionel Sambuc void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
1023ebfedea0SLionel Sambuc {
1024ebfedea0SLionel Sambuc     r[4] = bn_mul_words(&(r[0]), a, 4, b[0]);
1025ebfedea0SLionel Sambuc     r[5] = bn_mul_add_words(&(r[1]), a, 4, b[1]);
1026ebfedea0SLionel Sambuc     r[6] = bn_mul_add_words(&(r[2]), a, 4, b[2]);
1027ebfedea0SLionel Sambuc     r[7] = bn_mul_add_words(&(r[3]), a, 4, b[3]);
1028ebfedea0SLionel Sambuc }
1029ebfedea0SLionel Sambuc 
bn_mul_comba8(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b)1030ebfedea0SLionel Sambuc void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
1031ebfedea0SLionel Sambuc {
1032ebfedea0SLionel Sambuc     r[8] = bn_mul_words(&(r[0]), a, 8, b[0]);
1033ebfedea0SLionel Sambuc     r[9] = bn_mul_add_words(&(r[1]), a, 8, b[1]);
1034ebfedea0SLionel Sambuc     r[10] = bn_mul_add_words(&(r[2]), a, 8, b[2]);
1035ebfedea0SLionel Sambuc     r[11] = bn_mul_add_words(&(r[3]), a, 8, b[3]);
1036ebfedea0SLionel Sambuc     r[12] = bn_mul_add_words(&(r[4]), a, 8, b[4]);
1037ebfedea0SLionel Sambuc     r[13] = bn_mul_add_words(&(r[5]), a, 8, b[5]);
1038ebfedea0SLionel Sambuc     r[14] = bn_mul_add_words(&(r[6]), a, 8, b[6]);
1039ebfedea0SLionel Sambuc     r[15] = bn_mul_add_words(&(r[7]), a, 8, b[7]);
1040ebfedea0SLionel Sambuc }
1041ebfedea0SLionel Sambuc 
1042ebfedea0SLionel Sambuc # ifdef OPENSSL_NO_ASM
1043ebfedea0SLionel Sambuc #  ifdef OPENSSL_BN_ASM_MONT
1044ebfedea0SLionel Sambuc #   include <alloca.h>
bn_mul_mont(BN_ULONG * rp,const BN_ULONG * ap,const BN_ULONG * bp,const BN_ULONG * np,const BN_ULONG * n0p,int num)1045*0a6a1f1dSLionel Sambuc int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
1046*0a6a1f1dSLionel Sambuc                 const BN_ULONG *np, const BN_ULONG *n0p, int num)
1047ebfedea0SLionel Sambuc {
1048ebfedea0SLionel Sambuc     BN_ULONG c0, c1, *tp, n0 = *n0p;
1049ebfedea0SLionel Sambuc     volatile BN_ULONG *vp;
1050ebfedea0SLionel Sambuc     int i = 0, j;
1051ebfedea0SLionel Sambuc 
1052ebfedea0SLionel Sambuc     vp = tp = alloca((num + 2) * sizeof(BN_ULONG));
1053ebfedea0SLionel Sambuc 
1054*0a6a1f1dSLionel Sambuc     for (i = 0; i <= num; i++)
1055*0a6a1f1dSLionel Sambuc         tp[i] = 0;
1056ebfedea0SLionel Sambuc 
1057*0a6a1f1dSLionel Sambuc     for (i = 0; i < num; i++) {
1058ebfedea0SLionel Sambuc         c0 = bn_mul_add_words(tp, ap, num, bp[i]);
1059ebfedea0SLionel Sambuc         c1 = (tp[num] + c0) & BN_MASK2;
1060ebfedea0SLionel Sambuc         tp[num] = c1;
1061ebfedea0SLionel Sambuc         tp[num + 1] = (c1 < c0 ? 1 : 0);
1062ebfedea0SLionel Sambuc 
1063ebfedea0SLionel Sambuc         c0 = bn_mul_add_words(tp, np, num, tp[0] * n0);
1064ebfedea0SLionel Sambuc         c1 = (tp[num] + c0) & BN_MASK2;
1065ebfedea0SLionel Sambuc         tp[num] = c1;
1066ebfedea0SLionel Sambuc         tp[num + 1] += (c1 < c0 ? 1 : 0);
1067*0a6a1f1dSLionel Sambuc         for (j = 0; j <= num; j++)
1068*0a6a1f1dSLionel Sambuc             tp[j] = tp[j + 1];
1069ebfedea0SLionel Sambuc     }
1070ebfedea0SLionel Sambuc 
1071*0a6a1f1dSLionel Sambuc     if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) {
1072ebfedea0SLionel Sambuc         c0 = bn_sub_words(rp, tp, np, num);
1073*0a6a1f1dSLionel Sambuc         if (tp[num] != 0 || c0 == 0) {
1074*0a6a1f1dSLionel Sambuc             for (i = 0; i < num + 2; i++)
1075*0a6a1f1dSLionel Sambuc                 vp[i] = 0;
1076ebfedea0SLionel Sambuc             return 1;
1077ebfedea0SLionel Sambuc         }
1078ebfedea0SLionel Sambuc     }
1079*0a6a1f1dSLionel Sambuc     for (i = 0; i < num; i++)
1080*0a6a1f1dSLionel Sambuc         rp[i] = tp[i], vp[i] = 0;
1081ebfedea0SLionel Sambuc     vp[num] = 0;
1082ebfedea0SLionel Sambuc     vp[num + 1] = 0;
1083ebfedea0SLionel Sambuc     return 1;
1084ebfedea0SLionel Sambuc }
1085ebfedea0SLionel Sambuc #  else
bn_mul_mont(BN_ULONG * rp,const BN_ULONG * ap,const BN_ULONG * bp,const BN_ULONG * np,const BN_ULONG * n0,int num)1086*0a6a1f1dSLionel Sambuc int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
1087*0a6a1f1dSLionel Sambuc                 const BN_ULONG *np, const BN_ULONG *n0, int num)
1088*0a6a1f1dSLionel Sambuc {
1089*0a6a1f1dSLionel Sambuc     return 0;
1090*0a6a1f1dSLionel Sambuc }
1091ebfedea0SLionel Sambuc #  endif                        /* OPENSSL_BN_ASM_MONT */
1092ebfedea0SLionel Sambuc # endif
1093ebfedea0SLionel Sambuc 
1094ebfedea0SLionel Sambuc #endif                          /* !BN_MUL_COMBA */
1095