bn_div.c revision 68651
1155852Sgallatin/* crypto/bn/bn_div.c */
2155852Sgallatin/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3247795Sgallatin * All rights reserved.
4155852Sgallatin *
5155852Sgallatin * This package is an SSL implementation written
6155852Sgallatin * by Eric Young (eay@cryptsoft.com).
7155852Sgallatin * The implementation was written so as to conform with Netscapes SSL.
8155852Sgallatin *
9155852Sgallatin * This library is free for commercial and non-commercial use as long as
10155852Sgallatin * the following conditions are aheared to.  The following conditions
11155852Sgallatin * apply to all code found in this distribution, be it the RC4, RSA,
12171405Sgallatin * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13155852Sgallatin * included with this distribution is covered by the same copyright terms
14155852Sgallatin * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15155852Sgallatin *
16155852Sgallatin * Copyright remains Eric Young's, and as such any Copyright notices in
17155852Sgallatin * the code are not to be removed.
18155852Sgallatin * If this package is used in a product, Eric Young should be given attribution
19155852Sgallatin * as the author of the parts of the library used.
20155852Sgallatin * This can be in the form of a textual message at program startup or
21155852Sgallatin * in documentation (online or textual) provided with the package.
22155852Sgallatin *
23155852Sgallatin * Redistribution and use in source and binary forms, with or without
24155852Sgallatin * modification, are permitted provided that the following conditions
25155852Sgallatin * are met:
26155852Sgallatin * 1. Redistributions of source code must retain the copyright
27155852Sgallatin *    notice, this list of conditions and the following disclaimer.
28155852Sgallatin * 2. Redistributions in binary form must reproduce the above copyright
29155852Sgallatin *    notice, this list of conditions and the following disclaimer in the
30155852Sgallatin *    documentation and/or other materials provided with the distribution.
31155852Sgallatin * 3. All advertising materials mentioning features or use of this software
32159571Sgallatin *    must display the following acknowledgement:
33159571Sgallatin *    "This product includes cryptographic software written by
34159571Sgallatin *     Eric Young (eay@cryptsoft.com)"
35159571Sgallatin *    The word 'cryptographic' can be left out if the rouines from the library
36159571Sgallatin *    being used are not cryptographic related :-).
37155852Sgallatin * 4. If you include any Windows specific code (or a derivative thereof) from
38159571Sgallatin *    the apps directory (application code) you must include an acknowledgement:
39159571Sgallatin *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40169871Sgallatin *
41155852Sgallatin * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42181964Sgallatin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43181964Sgallatin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44175579Sgallatin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45175579Sgallatin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46175579Sgallatin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47175579Sgallatin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48175579Sgallatin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49193311Sgallatin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50193311Sgallatin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51193311Sgallatin * SUCH DAMAGE.
52193311Sgallatin *
53176261Sgallatin * The licence and distribution terms for any publically available version or
54176261Sgallatin * derivative of this code cannot be changed.  i.e. this code cannot simply be
55176261Sgallatin * copied and put under another distribution licence
56176261Sgallatin * [including the GNU Public Licence.]
57176261Sgallatin */
58176261Sgallatin
59176261Sgallatin#include <stdio.h>
60176261Sgallatin#include <openssl/bn.h>
61176261Sgallatin#include "cryptlib.h"
62176261Sgallatin#include "bn_lcl.h"
63176261Sgallatin
64176261Sgallatin/* The old slow way */
65176261Sgallatin#if 0
66176261Sgallatinint BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
67176261Sgallatin	   BN_CTX *ctx)
68176261Sgallatin	{
69176261Sgallatin	int i,nm,nd;
70176261Sgallatin	int ret = 0;
71176261Sgallatin	BIGNUM *D;
72176261Sgallatin
73176261Sgallatin	bn_check_top(m);
74176261Sgallatin	bn_check_top(d);
75176261Sgallatin	if (BN_is_zero(d))
76247320Sgallatin		{
77247320Sgallatin		BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);
78247320Sgallatin		return(0);
79247320Sgallatin		}
80247320Sgallatin
81247320Sgallatin	if (BN_ucmp(m,d) < 0)
82247320Sgallatin		{
83247320Sgallatin		if (rem != NULL)
84247320Sgallatin			{ if (BN_copy(rem,m) == NULL) return(0); }
85247320Sgallatin		if (dv != NULL) BN_zero(dv);
86247320Sgallatin		return(1);
87247320Sgallatin		}
88176261Sgallatin
89176261Sgallatin	BN_CTX_start(ctx);
90176261Sgallatin	D = BN_CTX_get(ctx);
91176261Sgallatin	if (dv == NULL) dv = BN_CTX_get(ctx);
92247320Sgallatin	if (rem == NULL) rem = BN_CTX_get(ctx);
93247320Sgallatin	if (D == NULL || dv == NULL || rem == NULL)
94247320Sgallatin		goto end;
95176261Sgallatin
96247320Sgallatin	nd=BN_num_bits(d);
97247320Sgallatin	nm=BN_num_bits(m);
98247320Sgallatin	if (BN_copy(D,d) == NULL) goto end;
99247320Sgallatin	if (BN_copy(rem,m) == NULL) goto end;
100247320Sgallatin
101247320Sgallatin	/* The next 2 are needed so we can do a dv->d[0]|=1 later
102247320Sgallatin	 * since BN_lshift1 will only work once there is a value :-) */
103247320Sgallatin	BN_zero(dv);
104155852Sgallatin	bn_wexpand(dv,1);
105155852Sgallatin	dv->top=1;
106155852Sgallatin
107155852Sgallatin	if (!BN_lshift(D,D,nm-nd)) goto end;
108155852Sgallatin	for (i=nm-nd; i>=0; i--)
109159571Sgallatin		{
110155852Sgallatin		if (!BN_lshift1(dv,dv)) goto end;
111155852Sgallatin		if (BN_ucmp(rem,D) >= 0)
112159612Sgallatin			{
113159612Sgallatin			dv->d[0]|=1;
114159612Sgallatin			if (!BN_usub(rem,rem,D)) goto end;
115159612Sgallatin			}
116159612Sgallatin/* CAN IMPROVE (and have now :=) */
117169871Sgallatin		if (!BN_rshift1(D,D)) goto end;
118159612Sgallatin		}
119155852Sgallatin	rem->neg=BN_is_zero(rem)?0:m->neg;
120155852Sgallatin	dv->neg=m->neg^d->neg;
121155852Sgallatin	ret = 1;
122155852Sgallatin end:
123155852Sgallatin	BN_CTX_end(ctx);
124155852Sgallatin	return(ret);
125159571Sgallatin	}
126155852Sgallatin
127159612Sgallatin#else
128155852Sgallatin
129155852Sgallatin#if !defined(NO_ASM) && !defined(NO_INLINE_ASM) && !defined(PEDANTIC) && !defined(BN_DIV3W)
130155852Sgallatin# if defined(__GNUC__) && __GNUC__>=2
131155852Sgallatin#  if defined(__i386)
132159612Sgallatin   /*
133159612Sgallatin    * There were two reasons for implementing this template:
134159612Sgallatin    * - GNU C generates a call to a function (__udivdi3 to be exact)
135159612Sgallatin    *   in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to
136159612Sgallatin    *   understand why...);
137159612Sgallatin    * - divl doesn't only calculate quotient, but also leaves
138155852Sgallatin    *   remainder in %edx which we can definitely use here:-)
139155852Sgallatin    *
140155852Sgallatin    *					<appro@fy.chalmers.se>
141155852Sgallatin    */
142159612Sgallatin#  define bn_div_words(n0,n1,d0)		\
143155852Sgallatin	({  asm volatile (			\
144155852Sgallatin		"divl	%4"			\
145155852Sgallatin		: "=a"(q), "=d"(rem)		\
146169840Sgallatin		: "a"(n1), "d"(n0), "g"(d0)	\
147169840Sgallatin		: "cc");			\
148155852Sgallatin	    q;					\
149155852Sgallatin	})
150193250Sgallatin#  define REMAINDER_IS_ALREADY_CALCULATED
151175365Sgallatin#  endif /* __<cpu> */
152155852Sgallatin# endif /* __GNUC__ */
153155852Sgallatin#endif /* NO_ASM */
154155852Sgallatin
155175365Sgallatinint BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
156191567Sgallatin	   BN_CTX *ctx)
157191562Sgallatin	{
158191562Sgallatin	int norm_shift,i,j,loop;
159155852Sgallatin	BIGNUM *tmp,wnum,*snum,*sdiv,*res;
160191562Sgallatin	BN_ULONG *resp,*wnump;
161191562Sgallatin	BN_ULONG d0,d1;
162155852Sgallatin	int num_n,div_n;
163155852Sgallatin
164162322Sgallatin	bn_check_top(num);
165159612Sgallatin	bn_check_top(divisor);
166155852Sgallatin
167155852Sgallatin	if (BN_is_zero(divisor))
168155852Sgallatin		{
169155852Sgallatin		BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);
170159612Sgallatin		return(0);
171169871Sgallatin		}
172191562Sgallatin
173191562Sgallatin	if (BN_ucmp(num,divisor) < 0)
174191562Sgallatin		{
175166345Sgallatin		if (rm != NULL)
176166345Sgallatin			{ if (BN_copy(rm,num) == NULL) return(0); }
177166373Sgallatin		if (dv != NULL) BN_zero(dv);
178166373Sgallatin		return(1);
179171917Sgallatin		}
180175365Sgallatin
181175365Sgallatin	BN_CTX_start(ctx);
182175365Sgallatin	tmp=BN_CTX_get(ctx);
183155852Sgallatin	tmp->neg=0;
184175365Sgallatin	snum=BN_CTX_get(ctx);
185175365Sgallatin	sdiv=BN_CTX_get(ctx);
186175365Sgallatin	if (dv == NULL)
187175365Sgallatin		res=BN_CTX_get(ctx);
188175365Sgallatin	else	res=dv;
189175365Sgallatin	if (res == NULL) goto err;
190175365Sgallatin
191175365Sgallatin	/* First we normalise the numbers */
192159612Sgallatin	norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);
193159612Sgallatin	BN_lshift(sdiv,divisor,norm_shift);
194175365Sgallatin	sdiv->neg=0;
195175365Sgallatin	norm_shift+=BN_BITS2;
196191562Sgallatin	BN_lshift(snum,num,norm_shift);
197194751Sgallatin	snum->neg=0;
198194751Sgallatin	div_n=sdiv->top;
199191562Sgallatin	num_n=snum->top;
200191562Sgallatin	loop=num_n-div_n;
201247472Sgallatin
202175365Sgallatin	/* Lets setup a 'window' into snum
203175365Sgallatin	 * This is the part that corresponds to the current
204175365Sgallatin	 * 'area' being divided */
205175365Sgallatin	BN_init(&wnum);
206175365Sgallatin	wnum.d=	 &(snum->d[loop]);
207175365Sgallatin	wnum.top= div_n;
208175365Sgallatin	wnum.dmax= snum->dmax+1; /* a bit of a lie */
209175365Sgallatin
210175365Sgallatin	/* Get the top 2 words of sdiv */
211175365Sgallatin	/* i=sdiv->top; */
212169840Sgallatin	d0=sdiv->d[div_n-1];
213175365Sgallatin	d1=(div_n == 1)?0:sdiv->d[div_n-2];
214175365Sgallatin
215155852Sgallatin	/* pointer to the 'top' of snum */
216159612Sgallatin	wnump= &(snum->d[num_n-1]);
217155852Sgallatin
218159571Sgallatin	/* Setup to 'res' */
219159571Sgallatin	res->neg= (num->neg^divisor->neg);
220155852Sgallatin	if (!bn_wexpand(res,(loop+1))) goto err;
221176281Sgallatin	res->top=loop;
222155852Sgallatin	resp= &(res->d[loop-1]);
223155852Sgallatin
224155852Sgallatin	/* space for temp */
225159612Sgallatin	if (!bn_wexpand(tmp,(div_n+1))) goto err;
226155852Sgallatin
227166370Sgallatin	if (BN_ucmp(&wnum,sdiv) >= 0)
228166370Sgallatin		{
229155852Sgallatin		if (!BN_usub(&wnum,&wnum,sdiv)) goto err;
230155852Sgallatin		*resp=1;
231155852Sgallatin		res->d[res->top-1]=1;
232155852Sgallatin		}
233175365Sgallatin	else
234155852Sgallatin		res->top--;
235155852Sgallatin	resp--;
236155852Sgallatin
237175365Sgallatin	for (i=0; i<loop-1; i++)
238175365Sgallatin		{
239175365Sgallatin		BN_ULONG q,l0;
240155852Sgallatin#ifdef BN_DIV3W
241175365Sgallatin		q=bn_div_3_words(wnump,d1,d0);
242155852Sgallatin#else
243159571Sgallatin		BN_ULONG n0,n1,rem=0;
244155852Sgallatin
245166875Sgallatin		n0=wnump[0];
246166875Sgallatin		n1=wnump[-1];
247166875Sgallatin		if (n0 == d0)
248166875Sgallatin			q=BN_MASK2;
249155852Sgallatin		else 			/* n0 < d0 */
250155852Sgallatin			{
251159612Sgallatin#ifdef BN_LLONG
252159612Sgallatin			BN_ULLONG t2;
253159612Sgallatin
254162328Sgallatin#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
255164513Sgallatin			q=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);
256169840Sgallatin#else
257197391Sgallatin			q=bn_div_words(n0,n1,d0);
258169871Sgallatin#endif
259171917Sgallatin
260171917Sgallatin#ifndef REMAINDER_IS_ALREADY_CALCULATED
261175365Sgallatin			/*
262175365Sgallatin			 * rem doesn't have to be BN_ULLONG. The least we
263194909Sgallatin			 * know it's less that d0, isn't it?
264206662Sgallatin			 */
265206662Sgallatin			rem=(n1-q*d0)&BN_MASK2;
266247320Sgallatin#endif
267166370Sgallatin			t2=(BN_ULLONG)d1*q;
268166373Sgallatin
269198250Sgallatin			for (;;)
270198250Sgallatin				{
271175365Sgallatin				if (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))
272175365Sgallatin					break;
273159612Sgallatin				q--;
274169871Sgallatin				rem += d0;
275197645Sgallatin				if (rem < d0) break; /* don't let rem overflow */
276159612Sgallatin				t2 -= d1;
277159612Sgallatin				}
278166370Sgallatin#else /* !BN_LLONG */
279166370Sgallatin			BN_ULONG t2l,t2h,ql,qh;
280175365Sgallatin
281155852Sgallatin			q=bn_div_words(n0,n1,d0);
282159571Sgallatin#ifndef REMAINDER_IS_ALREADY_CALCULATED
283159571Sgallatin			rem=(n1-q*d0)&BN_MASK2;
284172162Sgallatin#endif
285188736Sgallatin
286188736Sgallatin#ifdef BN_UMULT_HIGH
287171917Sgallatin			t2l = d1 * q;
288188736Sgallatin			t2h = BN_UMULT_HIGH(d1,q);
289197391Sgallatin#else
290197391Sgallatin			t2l=LBITS(d1); t2h=HBITS(d1);
291155852Sgallatin			ql =LBITS(q);  qh =HBITS(q);
292206662Sgallatin			mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */
293206662Sgallatin#endif
294206662Sgallatin
295206662Sgallatin			for (;;)
296206662Sgallatin				{
297206662Sgallatin				if ((t2h < rem) ||
298159571Sgallatin					((t2h == rem) && (t2l <= wnump[-2])))
299155852Sgallatin					break;
300159571Sgallatin				q--;
301155852Sgallatin				rem += d0;
302171917Sgallatin				if (rem < d0) break; /* don't let rem overflow */
303171917Sgallatin				if (t2l < d1) t2h--; t2l -= d1;
304171917Sgallatin				}
305171917Sgallatin#endif /* !BN_LLONG */
306171917Sgallatin			}
307171917Sgallatin#endif /* !BN_DIV3W */
308155852Sgallatin
309247320Sgallatin		l0=bn_mul_words(tmp->d,sdiv->d,div_n,q);
310247320Sgallatin		wnum.d--; wnum.top++;
311247320Sgallatin		tmp->d[div_n]=l0;
312247320Sgallatin		for (j=div_n+1; j>0; j--)
313247320Sgallatin			if (tmp->d[j-1]) break;
314247320Sgallatin		tmp->top=j;
315247320Sgallatin
316247320Sgallatin		j=wnum.top;
317247320Sgallatin		BN_sub(&wnum,&wnum,tmp);
318155852Sgallatin
319155852Sgallatin		snum->top=snum->top+wnum.top-j;
320155852Sgallatin
321185162Skmacy		if (wnum.neg)
322185162Skmacy			{
323155852Sgallatin			q--;
324155852Sgallatin			j=wnum.top;
325185255Sgallatin			BN_add(&wnum,&wnum,sdiv);
326155852Sgallatin			snum->top+=wnum.top-j;
327185255Sgallatin			}
328155852Sgallatin		*(resp--)=q;
329185255Sgallatin		wnump--;
330155852Sgallatin		}
331185255Sgallatin	if (rm != NULL)
332155852Sgallatin		{
333155852Sgallatin		BN_rshift(rm,snum,norm_shift);
334155852Sgallatin		rm->neg=num->neg;
335155852Sgallatin		}
336155852Sgallatin	BN_CTX_end(ctx);
337185162Skmacy	return(1);
338185162Skmacyerr:
339155852Sgallatin	BN_CTX_end(ctx);
340159571Sgallatin	return(0);
341155852Sgallatin	}
342155852Sgallatin
343155852Sgallatin#endif
344155852Sgallatin
345155852Sgallatin/* rem != m */
346155852Sgallatinint BN_mod(BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
347155852Sgallatin	{
348155852Sgallatin#if 0 /* The old slow way */
349155852Sgallatin	int i,nm,nd;
350155852Sgallatin	BIGNUM *dv;
351155852Sgallatin
352155852Sgallatin	if (BN_ucmp(m,d) < 0)
353155852Sgallatin		return((BN_copy(rem,m) == NULL)?0:1);
354155852Sgallatin
355155852Sgallatin	BN_CTX_start(ctx);
356175365Sgallatin	dv=BN_CTX_get(ctx);
357175365Sgallatin
358175365Sgallatin	if (!BN_copy(rem,m)) goto err;
359169840Sgallatin
360155852Sgallatin	nm=BN_num_bits(rem);
361169840Sgallatin	nd=BN_num_bits(d);
362155852Sgallatin	if (!BN_lshift(dv,d,nm-nd)) goto err;
363155852Sgallatin	for (i=nm-nd; i>=0; i--)
364155852Sgallatin		{
365155852Sgallatin		if (BN_cmp(rem,dv) >= 0)
366155852Sgallatin			{
367155852Sgallatin			if (!BN_sub(rem,rem,dv)) goto err;
368155852Sgallatin			}
369155852Sgallatin		if (!BN_rshift1(dv,dv)) goto err;
370		}
371	BN_CTX_end(ctx);
372	return(1);
373 err:
374	BN_CTX_end(ctx);
375	return(0);
376#else
377	return(BN_div(NULL,rem,m,d,ctx));
378#endif
379	}
380
381