div.c 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
/* crypto/bn/div.c */

#include <stdio.h>
#include "cryptlib.h"
#include "bn.h"

BN_ULONG bn_div_2word();

int BN_div2(dv, rm, num, div,ctx)
BIGNUM *dv;
BIGNUM *rm;
BIGNUM *num;
BIGNUM *div;
BN_CTX *ctx;
	{
	int norm_shift,i,j,nm,nd,loop;
	BIGNUM *tmp,wnum,*snum,*sdiv,*res;
	BN_ULONG *resp,*wnump;
	BN_ULONG d0,d1;
	int num_n,div_n;

#ifdef DEBUG
BN_print(stdout,num); printf(" number\n");
BN_print(stdout,div); printf(" divisor\n");
#endif
	if (BN_is_zero(num))
		{
		BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);
		return(0);
		}

	if (BN_cmp(num,div) < 0)
		{
		if (rm != NULL)
			{ if (BN_copy(rm,num) == NULL) return(0); }
		if (dv != NULL) BN_zero(dv);
		return(1);
		}

	tmp=ctx->bn[ctx->tos]; 
	snum=ctx->bn[ctx->tos+1];
	sdiv=ctx->bn[ctx->tos+2];
	if (dv == NULL)
		res=ctx->bn[ctx->tos+3];
	else	res=dv;

	/* First we normalise the numbers */
	norm_shift=BN_BITS2-((BN_num_bits(div))%BN_BITS2);
	BN_lshift(sdiv,div,norm_shift);
	norm_shift+=BN_BITS2;
	BN_lshift(snum,num,norm_shift);
	div_n=sdiv->top;
	num_n=snum->top;
	loop=num_n-div_n;
#ifdef DEBUG
BN_print(stdout,snum); printf(" shifted num, forget last word\n");
BN_print(stdout,sdiv); printf(" shifted div\n");
#endif

	/* Lets setup a 'win'dow into snum
	 * This is the part that corresponds to the current
	 * 'area' being divided */
	wnum.d=	 &(snum->d[loop]);
	wnum.top= div_n;
	wnum.max= snum->max; /* a bit of a lie */
	wnum.neg= 0;

	/* Get the top 2 words of sdiv */
	i=sdiv->top;
	d0=sdiv->d[div_n-1];
	d1=sdiv->d[div_n-2];

	/* pointer to the 'top' of snum */
	wnump= &(snum->d[num_n-1]);

	/* Setup to 'res' */
	res->neg=0;
	res->top=loop;
	resp= &(res->d[loop-1]);
	bn_expand(res,(loop+1)*BN_BITS2);

	/* space for temp */
	bn_expand(tmp,(div_n+1)*BN_BITS2);

#ifdef DEBUG
printf("wnum="); BN_print(stdout,&wnum); printf(" initial sub check\n");
printf("div ="); BN_print(stdout,sdiv); printf(" loop=%d\n",loop);
#endif
	if (BN_cmp(&wnum,sdiv) >= 0)
		{
		BN_sub(&wnum,&wnum,sdiv);
		*resp=1;
		res->d[res->top-1]=1;
		}
	else
		res->top--;
	resp--;
#ifdef DEBUG
BN_print(stdout,res); printf(" initial result\n");
BN_print(stdout,&wnum); printf(" wnum\n");
#endif

	for (i=0; i<loop-1; i++)
		{
		BN_ULONG q,n0;
		BN_ULLONG t1,t2,t3;
		BN_ULONG l0;

		wnum.d--;
		wnum.top++;

#ifdef DEBUG
BN_print(stderr,&wnum); printf(" to divide\n");
#endif

		q=0;
		n0=wnump[0];
		t1=((BN_ULLONG)n0<<BN_BITS2)|wnump[-1];
		if (n0 == d0)
			q=BN_MASK2;
		else
			{
			t2=(t1/d0);
			q=(t2&BN_MASK2);
#ifdef DEBUG
printf("t1=%08X / d0=%08X = %X (%X)\n",t1,d0,q,t2);
#endif
			}
		for (;;)
			{
			t2=(BN_ULLONG)d1*q;
			t3=t1-(BN_ULLONG)q*d0;
#ifdef DEBUG
printf("d1*q= %X    n01-q*d0 = %X\n",t2,t3);
#endif
			if ((t3>>BN_BITS2) ||
				(t2 <= ((t3<<BN_BITS2)+wnump[-2])))
				break;
#ifdef DEBUG
printf("reduce q\n");
#endif
			q--;
			}
		l0=bn_mul_word(tmp->d,sdiv->d,div_n,q);
		if (l0)
			tmp->d[div_n]=l0;
		else
			tmp->d[div_n]=0;
		for (j=div_n+1; j>0; j--)
			if (tmp->d[j-1]) break;
		tmp->top=j;

#ifdef DEBUG
printf("q=%08X\n",q);
BN_print(stdout,&wnum); printf(" number\n");
BN_print(stdout,tmp); printf(" subtract\n");

BN_print(stdout,snum); printf(" shifted number before\n");
BN_print(stdout,&wnum); printf(" wnum before\n");
#endif
		j=wnum.top;
		BN_sub(&wnum,&wnum,tmp);
		snum->top=snum->top+wnum.top-j;

#ifdef DEBUG
BN_print(stdout,&wnum); printf(" wnum after\n");
BN_print(stdout,snum); printf(" shifted number after\n");
#endif

		if (wnum.neg)
			{
			q--;
			j=wnum.top;
			BN_add(&wnum,&wnum,sdiv);
			snum->top+=wnum.top-j;
			fprintf(stderr,"addback\n");
#ifdef DEBUG
BN_print(stdout,snum); printf("after addback************************:\n");
#endif
			}
		*(resp--)=q;
#ifdef DEBUG
BN_print(stdout,res); printf(" result\n");
#endif
		wnump--;
		}
	if (rm != NULL)
		BN_rshift(rm,snum,norm_shift);
	return(1);
	}

main()
	{
	BIGNUM *a,*b,*c,*d;
	BIGNUM *cc,*dd;
	BN_CTX *ctx;
	int i,x;

	a=BN_new();
	b=BN_new();
	c=BN_new();
	d=BN_new();
	cc=BN_new();
	dd=BN_new();
	ctx=BN_CTX_new();

for (i=0; i<10240; i++)
	{
	BN_rand(a,80,0,0);
	BN_rand(b,60,0,0);
	
	BN_div2(d,c,a,b,ctx);
	BN_div(dd,cc,a,b,ctx);
	if ((BN_cmp(d,dd) != 0) || (BN_cmp(c,cc) != 0))
		{
		BN_print(stderr,a); fprintf(stderr," / ");
		BN_print(stderr,b); fprintf(stderr," d=");
		BN_print(stderr,d); fprintf(stderr," r= ");
		BN_print(stderr,c); fprintf(stderr,"\nd=");
		BN_print(stderr,dd); fprintf(stderr," r= ");
		BN_print(stderr,cc); fprintf(stderr,"\n");
		}

	}

#ifdef undef
/*
	BN_rand(a,600,0,0);
	BN_rand(b,400,0,0);
	for (i=0; i<2000000; i++)
		{
		BN_div2(d,c,a,b,ctx);
		}
*/
/*	for (i=0;;) */
/*	for (i=0; i<0xffffffff; i++)
		{
		BN_ULONG rr,r,a,b,c;
		BN_ULLONG l;

		a=rand()&BN_MASK2;
		b=rand()&BN_MASK2;
		for (;;)
			{
			c=rand()&BN_MASK2;
			if (c) break;
			}
/*		for (x=1; x<256*256; x++) */
			{
			c=x;
			a=i>>8;
			b=i&0xff;
			a&= ~(0xFFFFFF<<(BN_num_bits_word(c)));

			r=bn_div_2word(a,b,c);

			rr=(BN_ULONG)((((BN_ULLONG)a<<BN_BITS2)|b)/c);

			if ((i & 0xfffff) == 0) fprintf(stderr,"%d\n",i,r,rr); 
/*if (x == 255)
	fprintf(stderr,"%6d/%3d = %4d %4d\n",(a<<8)|b,c,r,rr); */
			if (rr != r)
				{
				fprintf(stderr,"%8d %02X%02X / %02X = %02X %02X\n",
					i,a,b,c,rr,r);
				abort();
				}
			}
		}
#endif
	}

/* Divide h-l by d and return the result. */
BN_ULONG bn_div_2word(l,h,d)
BN_ULONG l,h,d;
	{
	BN_ULONG dh,dl,q,ret=0,th,tl,t,top;
	int i,count=2;

	if (d == 0) return(-1);

	i=BN_num_bits_word(d);
	if ((i != BN_BITS2) && (h > 1<<i))
		{
		fprintf(stderr,"Division would overflow\n");
		abort();
		}
	i=BN_BITS2-i;
	if (h >= d) h-=d;

	if (i)
		{
		d<<=i;
		h=(h<<i)|(l>>(BN_BITS2-i));
		l<<=i;
		}
	dh=(d&BN_MASK2h)>>BN_BITS4;
	dl=(d&BN_MASK2l);
	for (;;)
		{
		if ((h>>BN_BITS4) == dh)
			q=BN_MASK2l;
		else
			q=h/dh;

		for (;;)
			{
			t=(h-q*dh);
			if ((t&BN_MASK2h) ||
				((dl*q) <= (
					(t<<BN_BITS4)+
					((l&BN_MASK2h)>>BN_BITS4))))
				break;
			q--;
			}
		th=q*dh;
		tl=q*dl;
		t=(tl>>BN_BITS4);
		tl=(tl<<BN_BITS4)&BN_MASK2h;
		th+=t;

		if (l < tl) th++;
		l-=tl;
		if (h < th)
			{
			fprintf(stderr,"add back\n");
			h+=d;
			q--;
			}
		h-=th;

		if (--count == 0) break;

		ret=q<<BN_BITS4;
		h=((h<<BN_BITS4)|(l>>BN_BITS4))&BN_MASK2;
		l=(l&BN_MASK2l)<<BN_BITS4;
		}
	ret|=q;
	return(ret);
	}