bn_mul.c revision 1.34
1/* $OpenBSD: bn_mul.c,v 1.34 2023/02/22 05:57:19 jsing Exp $ */
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 <assert.h>
60#include <stdio.h>
61#include <string.h>
62
63#include <openssl/opensslconf.h>
64
65#include "bn_arch.h"
66#include "bn_internal.h"
67#include "bn_local.h"
68
69/*
70 * bn_mul_comba4() computes r[] = a[] * b[] using Comba multiplication
71 * (https://everything2.com/title/Comba+multiplication), where a and b are both
72 * four word arrays, producing an eight word array result.
73 */
74#ifndef HAVE_BN_MUL_COMBA4
75void
76bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
77{
78	BN_ULONG c0, c1, c2;
79
80	bn_mulw_addtw(a[0], b[0],  0,  0,  0, &c2, &c1, &r[0]);
81
82	bn_mulw_addtw(a[0], b[1],  0, c2, c1, &c2, &c1, &c0);
83	bn_mulw_addtw(a[1], b[0], c2, c1, c0, &c2, &c1, &r[1]);
84
85	bn_mulw_addtw(a[2], b[0],  0, c2, c1, &c2, &c1, &c0);
86	bn_mulw_addtw(a[1], b[1], c2, c1, c0, &c2, &c1, &c0);
87	bn_mulw_addtw(a[0], b[2], c2, c1, c0, &c2, &c1, &r[2]);
88
89	bn_mulw_addtw(a[0], b[3],  0, c2, c1, &c2, &c1, &c0);
90	bn_mulw_addtw(a[1], b[2], c2, c1, c0, &c2, &c1, &c0);
91	bn_mulw_addtw(a[2], b[1], c2, c1, c0, &c2, &c1, &c0);
92	bn_mulw_addtw(a[3], b[0], c2, c1, c0, &c2, &c1, &r[3]);
93
94	bn_mulw_addtw(a[3], b[1],  0, c2, c1, &c2, &c1, &c0);
95	bn_mulw_addtw(a[2], b[2], c2, c1, c0, &c2, &c1, &c0);
96	bn_mulw_addtw(a[1], b[3], c2, c1, c0, &c2, &c1, &r[4]);
97
98	bn_mulw_addtw(a[2], b[3],  0, c2, c1, &c2, &c1, &c0);
99	bn_mulw_addtw(a[3], b[2], c2, c1, c0, &c2, &c1, &r[5]);
100
101	bn_mulw_addtw(a[3], b[3],  0, c2, c1, &c2, &r[7], &r[6]);
102}
103#endif
104
105/*
106 * bn_mul_comba8() computes r[] = a[] * b[] using Comba multiplication
107 * (https://everything2.com/title/Comba+multiplication), where a and b are both
108 * eight word arrays, producing a 16 word array result.
109 */
110#ifndef HAVE_BN_MUL_COMBA8
111void
112bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
113{
114	BN_ULONG c0, c1, c2;
115
116	bn_mulw_addtw(a[0], b[0],  0,  0,  0, &c2, &c1, &r[0]);
117
118	bn_mulw_addtw(a[0], b[1],  0, c2, c1, &c2, &c1, &c0);
119	bn_mulw_addtw(a[1], b[0], c2, c1, c0, &c2, &c1, &r[1]);
120
121	bn_mulw_addtw(a[2], b[0],  0, c2, c1, &c2, &c1, &c0);
122	bn_mulw_addtw(a[1], b[1], c2, c1, c0, &c2, &c1, &c0);
123	bn_mulw_addtw(a[0], b[2], c2, c1, c0, &c2, &c1, &r[2]);
124
125	bn_mulw_addtw(a[0], b[3],  0, c2, c1, &c2, &c1, &c0);
126	bn_mulw_addtw(a[1], b[2], c2, c1, c0, &c2, &c1, &c0);
127	bn_mulw_addtw(a[2], b[1], c2, c1, c0, &c2, &c1, &c0);
128	bn_mulw_addtw(a[3], b[0], c2, c1, c0, &c2, &c1, &r[3]);
129
130	bn_mulw_addtw(a[4], b[0],  0, c2, c1, &c2, &c1, &c0);
131	bn_mulw_addtw(a[3], b[1], c2, c1, c0, &c2, &c1, &c0);
132	bn_mulw_addtw(a[2], b[2], c2, c1, c0, &c2, &c1, &c0);
133	bn_mulw_addtw(a[1], b[3], c2, c1, c0, &c2, &c1, &c0);
134	bn_mulw_addtw(a[0], b[4], c2, c1, c0, &c2, &c1, &r[4]);
135
136	bn_mulw_addtw(a[0], b[5],  0, c2, c1, &c2, &c1, &c0);
137	bn_mulw_addtw(a[1], b[4], c2, c1, c0, &c2, &c1, &c0);
138	bn_mulw_addtw(a[2], b[3], c2, c1, c0, &c2, &c1, &c0);
139	bn_mulw_addtw(a[3], b[2], c2, c1, c0, &c2, &c1, &c0);
140	bn_mulw_addtw(a[4], b[1], c2, c1, c0, &c2, &c1, &c0);
141	bn_mulw_addtw(a[5], b[0], c2, c1, c0, &c2, &c1, &r[5]);
142
143	bn_mulw_addtw(a[6], b[0],  0, c2, c1, &c2, &c1, &c0);
144	bn_mulw_addtw(a[5], b[1], c2, c1, c0, &c2, &c1, &c0);
145	bn_mulw_addtw(a[4], b[2], c2, c1, c0, &c2, &c1, &c0);
146	bn_mulw_addtw(a[3], b[3], c2, c1, c0, &c2, &c1, &c0);
147	bn_mulw_addtw(a[2], b[4], c2, c1, c0, &c2, &c1, &c0);
148	bn_mulw_addtw(a[1], b[5], c2, c1, c0, &c2, &c1, &c0);
149	bn_mulw_addtw(a[0], b[6], c2, c1, c0, &c2, &c1, &r[6]);
150
151	bn_mulw_addtw(a[0], b[7],  0, c2, c1, &c2, &c1, &c0);
152	bn_mulw_addtw(a[1], b[6], c2, c1, c0, &c2, &c1, &c0);
153	bn_mulw_addtw(a[2], b[5], c2, c1, c0, &c2, &c1, &c0);
154	bn_mulw_addtw(a[3], b[4], c2, c1, c0, &c2, &c1, &c0);
155	bn_mulw_addtw(a[4], b[3], c2, c1, c0, &c2, &c1, &c0);
156	bn_mulw_addtw(a[5], b[2], c2, c1, c0, &c2, &c1, &c0);
157	bn_mulw_addtw(a[6], b[1], c2, c1, c0, &c2, &c1, &c0);
158	bn_mulw_addtw(a[7], b[0], c2, c1, c0, &c2, &c1, &r[7]);
159
160	bn_mulw_addtw(a[7], b[1],  0, c2, c1, &c2, &c1, &c0);
161	bn_mulw_addtw(a[6], b[2], c2, c1, c0, &c2, &c1, &c0);
162	bn_mulw_addtw(a[5], b[3], c2, c1, c0, &c2, &c1, &c0);
163	bn_mulw_addtw(a[4], b[4], c2, c1, c0, &c2, &c1, &c0);
164	bn_mulw_addtw(a[3], b[5], c2, c1, c0, &c2, &c1, &c0);
165	bn_mulw_addtw(a[2], b[6], c2, c1, c0, &c2, &c1, &c0);
166	bn_mulw_addtw(a[1], b[7], c2, c1, c0, &c2, &c1, &r[8]);
167
168	bn_mulw_addtw(a[2], b[7],  0, c2, c1, &c2, &c1, &c0);
169	bn_mulw_addtw(a[3], b[6], c2, c1, c0, &c2, &c1, &c0);
170	bn_mulw_addtw(a[4], b[5], c2, c1, c0, &c2, &c1, &c0);
171	bn_mulw_addtw(a[5], b[4], c2, c1, c0, &c2, &c1, &c0);
172	bn_mulw_addtw(a[6], b[3], c2, c1, c0, &c2, &c1, &c0);
173	bn_mulw_addtw(a[7], b[2], c2, c1, c0, &c2, &c1, &r[9]);
174
175	bn_mulw_addtw(a[7], b[3],  0, c2, c1, &c2, &c1, &c0);
176	bn_mulw_addtw(a[6], b[4], c2, c1, c0, &c2, &c1, &c0);
177	bn_mulw_addtw(a[5], b[5], c2, c1, c0, &c2, &c1, &c0);
178	bn_mulw_addtw(a[4], b[6], c2, c1, c0, &c2, &c1, &c0);
179	bn_mulw_addtw(a[3], b[7], c2, c1, c0, &c2, &c1, &r[10]);
180
181	bn_mulw_addtw(a[4], b[7],  0, c2, c1, &c2, &c1, &c0);
182	bn_mulw_addtw(a[5], b[6], c2, c1, c0, &c2, &c1, &c0);
183	bn_mulw_addtw(a[6], b[5], c2, c1, c0, &c2, &c1, &c0);
184	bn_mulw_addtw(a[7], b[4], c2, c1, c0, &c2, &c1, &r[11]);
185
186	bn_mulw_addtw(a[7], b[5],  0, c2, c1, &c2, &c1, &c0);
187	bn_mulw_addtw(a[6], b[6], c2, c1, c0, &c2, &c1, &c0);
188	bn_mulw_addtw(a[5], b[7], c2, c1, c0, &c2, &c1, &r[12]);
189
190	bn_mulw_addtw(a[6], b[7],  0, c2, c1, &c2, &c1, &c0);
191	bn_mulw_addtw(a[7], b[6], c2, c1, c0, &c2, &c1, &r[13]);
192
193	bn_mulw_addtw(a[7], b[7],  0, c2, c1, &c2, &r[15], &r[14]);
194}
195#endif
196
197/*
198 * bn_mul_words() computes (carry:r[i]) = a[i] * w + carry, where a is an array
199 * of words and w is a single word. This should really be called bn_mulw_words()
200 * since only one input is an array. This is used as a step in the multiplication
201 * of word arrays.
202 */
203#ifndef HAVE_BN_MUL_WORDS
204BN_ULONG
205bn_mul_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w)
206{
207	BN_ULONG carry = 0;
208
209	assert(num >= 0);
210	if (num <= 0)
211		return 0;
212
213#ifndef OPENSSL_SMALL_FOOTPRINT
214	while (num & ~3) {
215		bn_mulw_addw(a[0], w, carry, &carry, &r[0]);
216		bn_mulw_addw(a[1], w, carry, &carry, &r[1]);
217		bn_mulw_addw(a[2], w, carry, &carry, &r[2]);
218		bn_mulw_addw(a[3], w, carry, &carry, &r[3]);
219		a += 4;
220		r += 4;
221		num -= 4;
222	}
223#endif
224	while (num) {
225		bn_mulw_addw(a[0], w, carry, &carry, &r[0]);
226		a++;
227		r++;
228		num--;
229	}
230	return carry;
231}
232#endif
233
234/*
235 * bn_mul_add_words() computes (carry:r[i]) = a[i] * w + r[i] + carry, where
236 * a is an array of words and w is a single word. This should really be called
237 * bn_mulw_add_words() since only one input is an array. This is used as a step
238 * in the multiplication of word arrays.
239 */
240#ifndef HAVE_BN_MUL_ADD_WORDS
241BN_ULONG
242bn_mul_add_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w)
243{
244	BN_ULONG carry = 0;
245
246	assert(num >= 0);
247	if (num <= 0)
248		return 0;
249
250#ifndef OPENSSL_SMALL_FOOTPRINT
251	while (num & ~3) {
252		bn_mulw_addw_addw(a[0], w, r[0], carry, &carry, &r[0]);
253		bn_mulw_addw_addw(a[1], w, r[1], carry, &carry, &r[1]);
254		bn_mulw_addw_addw(a[2], w, r[2], carry, &carry, &r[2]);
255		bn_mulw_addw_addw(a[3], w, r[3], carry, &carry, &r[3]);
256		a += 4;
257		r += 4;
258		num -= 4;
259	}
260#endif
261	while (num) {
262		bn_mulw_addw_addw(a[0], w, r[0], carry, &carry, &r[0]);
263		a++;
264		r++;
265		num--;
266	}
267
268	return carry;
269}
270#endif
271
272void
273bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
274{
275	BN_ULONG *rr;
276
277
278	if (na < nb) {
279		int itmp;
280		BN_ULONG *ltmp;
281
282		itmp = na;
283		na = nb;
284		nb = itmp;
285		ltmp = a;
286		a = b;
287		b = ltmp;
288
289	}
290	rr = &(r[na]);
291	if (nb <= 0) {
292		(void)bn_mul_words(r, a, na, 0);
293		return;
294	} else
295		rr[0] = bn_mul_words(r, a, na, b[0]);
296
297	for (;;) {
298		if (--nb <= 0)
299			return;
300		rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
301		if (--nb <= 0)
302			return;
303		rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
304		if (--nb <= 0)
305			return;
306		rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
307		if (--nb <= 0)
308			return;
309		rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
310		rr += 4;
311		r += 4;
312		b += 4;
313	}
314}
315
316#ifdef BN_RECURSION
317/* Karatsuba recursive multiplication algorithm
318 * (cf. Knuth, The Art of Computer Programming, Vol. 2) */
319
320/* r is 2*n2 words in size,
321 * a and b are both n2 words in size.
322 * n2 must be a power of 2.
323 * We multiply and return the result.
324 * t must be 2*n2 words in size
325 * We calculate
326 * a[0]*b[0]
327 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
328 * a[1]*b[1]
329 */
330/* dnX may not be positive, but n2/2+dnX has to be */
331void
332bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, int dna,
333    int dnb, BN_ULONG *t)
334{
335	int n = n2 / 2, c1, c2;
336	int tna = n + dna, tnb = n + dnb;
337	unsigned int neg, zero;
338	BN_ULONG ln, lo, *p;
339
340# ifdef BN_MUL_COMBA
341#  if 0
342	if (n2 == 4) {
343		bn_mul_comba4(r, a, b);
344		return;
345	}
346#  endif
347	/* Only call bn_mul_comba 8 if n2 == 8 and the
348	 * two arrays are complete [steve]
349	 */
350	if (n2 == 8 && dna == 0 && dnb == 0) {
351		bn_mul_comba8(r, a, b);
352		return;
353	}
354# endif /* BN_MUL_COMBA */
355	/* Else do normal multiply */
356	if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
357		bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
358		if ((dna + dnb) < 0)
359			memset(&r[2*n2 + dna + dnb], 0,
360			    sizeof(BN_ULONG) * -(dna + dnb));
361		return;
362	}
363	/* r=(a[0]-a[1])*(b[1]-b[0]) */
364	c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
365	c2 = bn_cmp_part_words(&(b[n]), b,tnb, tnb - n);
366	zero = neg = 0;
367	switch (c1 * 3 + c2) {
368	case -4:
369		bn_sub(t, n, &a[n], tna, a, n);	/* - */
370		bn_sub(&t[n], n, b, n, &b[n], tnb);	/* - */
371		break;
372	case -3:
373		zero = 1;
374		break;
375	case -2:
376		bn_sub(t, n, &a[n], tna, a, n);	/* - */
377		bn_sub(&t[n], n, &b[n], tnb, b, n);	/* + */
378		neg = 1;
379		break;
380	case -1:
381	case 0:
382	case 1:
383		zero = 1;
384		break;
385	case 2:
386		bn_sub(t, n, a, n, &a[n], tna);	/* + */
387		bn_sub(&t[n], n, b, n, &b[n], tnb);	/* - */
388		neg = 1;
389		break;
390	case 3:
391		zero = 1;
392		break;
393	case 4:
394		bn_sub(t, n, a, n, &a[n], tna);
395		bn_sub(&t[n], n, &b[n], tnb, b, n);
396		break;
397	}
398
399# ifdef BN_MUL_COMBA
400	if (n == 4 && dna == 0 && dnb == 0) /* XXX: bn_mul_comba4 could take
401					       extra args to do this well */
402	{
403		if (!zero)
404			bn_mul_comba4(&(t[n2]), t, &(t[n]));
405		else
406			memset(&(t[n2]), 0, 8 * sizeof(BN_ULONG));
407
408		bn_mul_comba4(r, a, b);
409		bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
410	} else if (n == 8 && dna == 0 && dnb == 0) /* XXX: bn_mul_comba8 could
411						    take extra args to do this
412						    well */
413	{
414		if (!zero)
415			bn_mul_comba8(&(t[n2]), t, &(t[n]));
416		else
417			memset(&(t[n2]), 0, 16 * sizeof(BN_ULONG));
418
419		bn_mul_comba8(r, a, b);
420		bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
421	} else
422# endif /* BN_MUL_COMBA */
423	{
424		p = &(t[n2 * 2]);
425		if (!zero)
426			bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
427		else
428			memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG));
429		bn_mul_recursive(r, a, b, n, 0, 0, p);
430		bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
431	}
432
433	/* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
434	 * r[10] holds (a[0]*b[0])
435	 * r[32] holds (b[1]*b[1])
436	 */
437
438	c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
439
440	if (neg) /* if t[32] is negative */
441	{
442		c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
443	} else {
444		/* Might have a carry */
445		c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
446	}
447
448	/* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
449	 * r[10] holds (a[0]*b[0])
450	 * r[32] holds (b[1]*b[1])
451	 * c1 holds the carry bits
452	 */
453	c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
454	if (c1) {
455		p = &(r[n + n2]);
456		lo= *p;
457		ln = (lo + c1) & BN_MASK2;
458		*p = ln;
459
460		/* The overflow will stop before we over write
461		 * words we should not overwrite */
462		if (ln < (BN_ULONG)c1) {
463			do {
464				p++;
465				lo= *p;
466				ln = (lo + 1) & BN_MASK2;
467				*p = ln;
468			} while (ln == 0);
469		}
470	}
471}
472
473/* n+tn is the word length
474 * t needs to be n*4 is size, as does r */
475/* tnX may not be negative but less than n */
476void
477bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n, int tna,
478    int tnb, BN_ULONG *t)
479{
480	int i, j, n2 = n * 2;
481	int c1, c2, neg;
482	BN_ULONG ln, lo, *p;
483
484	if (n < 8) {
485		bn_mul_normal(r, a, n + tna, b, n + tnb);
486		return;
487	}
488
489	/* r=(a[0]-a[1])*(b[1]-b[0]) */
490	c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
491	c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
492	neg = 0;
493	switch (c1 * 3 + c2) {
494	case -4:
495		bn_sub(t, n, &a[n], tna, a, n);		/* - */
496		bn_sub(&t[n], n, b, n, &b[n], tnb);	/* - */
497		break;
498	case -3:
499		/* break; */
500	case -2:
501		bn_sub(t, n, &a[n], tna, a, n);		/* - */
502		bn_sub(&t[n], n, &b[n], tnb, b, n);	/* + */
503		neg = 1;
504		break;
505	case -1:
506	case 0:
507	case 1:
508		/* break; */
509	case 2:
510		bn_sub(t, n, a, n, &a[n], tna);		/* + */
511		bn_sub(&t[n], n, b, n, &b[n], tnb);	/* - */
512		neg = 1;
513		break;
514	case 3:
515		/* break; */
516	case 4:
517		bn_sub(t, n, a, n, &a[n], tna);
518		bn_sub(&t[n], n, &b[n], tnb, b, n);
519		break;
520	}
521		/* The zero case isn't yet implemented here. The speedup
522		   would probably be negligible. */
523# if 0
524	if (n == 4) {
525		bn_mul_comba4(&(t[n2]), t, &(t[n]));
526		bn_mul_comba4(r, a, b);
527		bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);
528		memset(&(r[n2 + tn * 2]), 0, sizeof(BN_ULONG) * (n2 - tn * 2));
529	} else
530# endif
531		if (n == 8) {
532		bn_mul_comba8(&(t[n2]), t, &(t[n]));
533		bn_mul_comba8(r, a, b);
534		bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
535		memset(&(r[n2 + tna + tnb]), 0,
536		    sizeof(BN_ULONG) * (n2 - tna - tnb));
537	} else {
538		p = &(t[n2*2]);
539		bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
540		bn_mul_recursive(r, a, b, n, 0, 0, p);
541		i = n / 2;
542		/* If there is only a bottom half to the number,
543		 * just do it */
544		if (tna > tnb)
545			j = tna - i;
546		else
547			j = tnb - i;
548		if (j == 0) {
549			bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),
550			    i, tna - i, tnb - i, p);
551			memset(&(r[n2 + i * 2]), 0,
552			    sizeof(BN_ULONG) * (n2 - i * 2));
553		}
554		else if (j > 0) /* eg, n == 16, i == 8 and tn == 11 */
555		{
556			bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),
557			    i, tna - i, tnb - i, p);
558			memset(&(r[n2 + tna + tnb]), 0,
559			    sizeof(BN_ULONG) * (n2 - tna - tnb));
560		}
561		else /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
562		{
563			memset(&(r[n2]), 0, sizeof(BN_ULONG) * n2);
564			if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL &&
565			    tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
566				bn_mul_normal(&(r[n2]), &(a[n]), tna,
567				    &(b[n]), tnb);
568			} else {
569				for (;;) {
570					i /= 2;
571					/* these simplified conditions work
572					 * exclusively because difference
573					 * between tna and tnb is 1 or 0 */
574					if (i < tna || i < tnb) {
575						bn_mul_part_recursive(&(r[n2]),
576						    &(a[n]), &(b[n]), i,
577						    tna - i, tnb - i, p);
578						break;
579					} else if (i == tna || i == tnb) {
580						bn_mul_recursive(&(r[n2]),
581						    &(a[n]), &(b[n]), i,
582						    tna - i, tnb - i, p);
583						break;
584					}
585				}
586			}
587		}
588	}
589
590	/* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
591	 * r[10] holds (a[0]*b[0])
592	 * r[32] holds (b[1]*b[1])
593	 */
594
595	c1 = (int)(bn_add_words(t, r,&(r[n2]), n2));
596
597	if (neg) /* if t[32] is negative */
598	{
599		c1 -= (int)(bn_sub_words(&(t[n2]), t,&(t[n2]), n2));
600	} else {
601		/* Might have a carry */
602		c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
603	}
604
605	/* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
606	 * r[10] holds (a[0]*b[0])
607	 * r[32] holds (b[1]*b[1])
608	 * c1 holds the carry bits
609	 */
610	c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
611	if (c1) {
612		p = &(r[n + n2]);
613		lo= *p;
614		ln = (lo + c1)&BN_MASK2;
615		*p = ln;
616
617		/* The overflow will stop before we over write
618		 * words we should not overwrite */
619		if (ln < (BN_ULONG)c1) {
620			do {
621				p++;
622				lo= *p;
623				ln = (lo + 1) & BN_MASK2;
624				*p = ln;
625			} while (ln == 0);
626		}
627	}
628}
629#endif /* BN_RECURSION */
630
631#ifndef HAVE_BN_MUL
632#ifndef BN_RECURSION
633int
634bn_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, int rn, BN_CTX *ctx)
635{
636	bn_mul_normal(r->d, a->d, a->top, b->d, b->top);
637
638	return 1;
639}
640
641#else /* BN_RECURSION */
642int
643bn_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, int rn, BN_CTX *ctx)
644{
645	BIGNUM *t = NULL;
646	int al, bl, i, k;
647	int j = 0;
648	int ret = 0;
649
650	BN_CTX_start(ctx);
651
652	al = a->top;
653	bl = b->top;
654
655	i = al - bl;
656
657	if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
658		if (i >= -1 && i <= 1) {
659			/* Find out the power of two lower or equal
660			   to the longest of the two numbers */
661			if (i >= 0) {
662				j = BN_num_bits_word((BN_ULONG)al);
663			}
664			if (i == -1) {
665				j = BN_num_bits_word((BN_ULONG)bl);
666			}
667			j = 1 << (j - 1);
668			assert(j <= al || j <= bl);
669			k = j + j;
670			if ((t = BN_CTX_get(ctx)) == NULL)
671				goto err;
672			if (al > j || bl > j) {
673				if (!bn_wexpand(t, k * 4))
674					goto err;
675				if (!bn_wexpand(r, k * 4))
676					goto err;
677				bn_mul_part_recursive(r->d, a->d, b->d,
678				    j, al - j, bl - j, t->d);
679			}
680			else	/* al <= j || bl <= j */
681			{
682				if (!bn_wexpand(t, k * 2))
683					goto err;
684				if (!bn_wexpand(r, k * 2))
685					goto err;
686				bn_mul_recursive(r->d, a->d, b->d,
687				    j, al - j, bl - j, t->d);
688			}
689			r->top = rn;
690			goto end;
691		}
692#if 0
693		if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {
694			BIGNUM *tmp_bn = (BIGNUM *)b;
695			if (!bn_wexpand(tmp_bn, al))
696				goto err;
697			tmp_bn->d[bl] = 0;
698			bl++;
699			i--;
700		} else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {
701			BIGNUM *tmp_bn = (BIGNUM *)a;
702			if (!bn_wexpand(tmp_bn, bl))
703				goto err;
704			tmp_bn->d[al] = 0;
705			al++;
706			i++;
707		}
708		if (i == 0) {
709			/* symmetric and > 4 */
710			/* 16 or larger */
711			j = BN_num_bits_word((BN_ULONG)al);
712			j = 1 << (j - 1);
713			k = j + j;
714			if ((t = BN_CTX_get(ctx)) == NULL)
715				goto err;
716			if (al == j) /* exact multiple */
717			{
718				if (!bn_wexpand(t, k * 2))
719					goto err;
720				if (!bn_wexpand(r, k * 2))
721					goto err;
722				bn_mul_recursive(r->d, a->d, b->d, al, t->d);
723			} else {
724				if (!bn_wexpand(t, k * 4))
725					goto err;
726				if (!bn_wexpand(r, k * 4))
727					goto err;
728				bn_mul_part_recursive(r->d, a->d, b->d,
729				    al - j, j, t->d);
730			}
731			r->top = top;
732			goto end;
733		}
734#endif
735	}
736
737	bn_mul_normal(r->d, a->d, al, b->d, bl);
738
739 end:
740	ret = 1;
741 err:
742	BN_CTX_end(ctx);
743
744	return ret;
745}
746#endif /* BN_RECURSION */
747#endif /* HAVE_BN_MUL */
748
749int
750BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
751{
752	BIGNUM *rr;
753	int rn;
754	int ret = 0;
755
756	BN_CTX_start(ctx);
757
758	if (BN_is_zero(a) || BN_is_zero(b)) {
759		BN_zero(r);
760		goto done;
761	}
762
763	rr = r;
764	if (rr == a || rr == b)
765		rr = BN_CTX_get(ctx);
766	if (rr == NULL)
767		goto err;
768
769	rn = a->top + b->top;
770	if (rn < a->top)
771		goto err;
772	if (!bn_wexpand(rr, rn))
773		goto err;
774
775	if (a->top == 4 && b->top == 4) {
776		bn_mul_comba4(rr->d, a->d, b->d);
777	} else if (a->top == 8 && b->top == 8) {
778		bn_mul_comba8(rr->d, a->d, b->d);
779	} else {
780		if (!bn_mul(rr, a, b, rn, ctx))
781			goto err;
782	}
783
784	rr->top = rn;
785	bn_correct_top(rr);
786
787	BN_set_negative(rr, a->neg ^ b->neg);
788
789	if (r != rr)
790		BN_copy(r, rr);
791 done:
792	ret = 1;
793 err:
794	BN_CTX_end(ctx);
795
796	return ret;
797}
798