]>
git.saurik.com Git - apple/security.git/blob - AppleCSP/open_ssl/bn/bn_div.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 /* crypto/bn/bn_div.c */
20 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
21 * All rights reserved.
23 * This package is an SSL implementation written
24 * by Eric Young (eay@cryptsoft.com).
25 * The implementation was written so as to conform with Netscapes SSL.
27 * This library is free for commercial and non-commercial use as long as
28 * the following conditions are aheared to. The following conditions
29 * apply to all code found in this distribution, be it the RC4, RSA,
30 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
31 * included with this distribution is covered by the same copyright terms
32 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
34 * Copyright remains Eric Young's, and as such any Copyright notices in
35 * the code are not to be removed.
36 * If this package is used in a product, Eric Young should be given attribution
37 * as the author of the parts of the library used.
38 * This can be in the form of a textual message at program startup or
39 * in documentation (online or textual) provided with the package.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * "This product includes cryptographic software written by
52 * Eric Young (eay@cryptsoft.com)"
53 * The word 'cryptographic' can be left out if the rouines from the library
54 * being used are not cryptographic related :-).
55 * 4. If you include any Windows specific code (or a derivative thereof) from
56 * the apps directory (application code) you must include an acknowledgement:
57 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
59 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * The licence and distribution terms for any publically available version or
72 * derivative of this code cannot be changed. i.e. this code cannot simply be
73 * copied and put under another distribution licence
74 * [including the GNU Public Licence.]
78 #include <openssl/bn.h>
82 /* The old slow way */
84 int BN_div(BIGNUM
*dv
, BIGNUM
*rem
, const BIGNUM
*m
, const BIGNUM
*d
,
95 BNerr(BN_F_BN_DIV
,BN_R_DIV_BY_ZERO
);
102 { if (BN_copy(rem
,m
) == NULL
) return(0); }
103 if (dv
!= NULL
) BN_zero(dv
);
109 if (dv
== NULL
) dv
= BN_CTX_get(ctx
);
110 if (rem
== NULL
) rem
= BN_CTX_get(ctx
);
111 if (D
== NULL
|| dv
== NULL
|| rem
== NULL
)
116 if (BN_copy(D
,d
) == NULL
) goto end
;
117 if (BN_copy(rem
,m
) == NULL
) goto end
;
119 /* The next 2 are needed so we can do a dv->d[0]|=1 later
120 * since BN_lshift1 will only work once there is a value :-) */
125 if (!BN_lshift(D
,D
,nm
-nd
)) goto end
;
126 for (i
=nm
-nd
; i
>=0; i
--)
128 if (!BN_lshift1(dv
,dv
)) goto end
;
129 if (BN_ucmp(rem
,D
) >= 0)
132 if (!BN_usub(rem
,rem
,D
)) goto end
;
134 /* CAN IMPROVE (and have now :=) */
135 if (!BN_rshift1(D
,D
)) goto end
;
137 rem
->neg
=BN_is_zero(rem
)?0:m
->neg
;
138 dv
->neg
=m
->neg
^d
->neg
;
147 #if !defined(NO_ASM) && !defined(NO_INLINE_ASM) && !defined(PEDANTIC) && !defined(BN_DIV3W)
148 # if defined(__GNUC__) && __GNUC__>=2
151 * There were two reasons for implementing this template:
152 * - GNU C generates a call to a function (__udivdi3 to be exact)
153 * in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to
154 * understand why...);
155 * - divl doesn't only calculate quotient, but also leaves
156 * remainder in %edx which we can definitely use here:-)
158 * <appro@fy.chalmers.se>
160 # define bn_div_words(n0,n1,d0) \
163 : "=a"(q), "=d"(rem) \
164 : "a"(n1), "d"(n0), "g"(d0) \
168 # define REMAINDER_IS_ALREADY_CALCULATED
169 # endif /* __<cpu> */
170 # endif /* __GNUC__ */
173 int BN_div(BIGNUM
*dv
, BIGNUM
*rm
, const BIGNUM
*num
, const BIGNUM
*divisor
,
176 int norm_shift
,i
,j
,loop
;
177 BIGNUM
*tmp
,wnum
,*snum
,*sdiv
,*res
;
178 BN_ULONG
*resp
,*wnump
;
183 bn_check_top(divisor
);
185 if (BN_is_zero(divisor
))
187 BNerr(BN_F_BN_DIV
,BN_R_DIV_BY_ZERO
);
191 if (BN_ucmp(num
,divisor
) < 0)
194 { if (BN_copy(rm
,num
) == NULL
) return(0); }
195 if (dv
!= NULL
) BN_zero(dv
);
202 snum
=BN_CTX_get(ctx
);
203 sdiv
=BN_CTX_get(ctx
);
207 if (res
== NULL
) goto err
;
209 /* First we normalise the numbers */
210 norm_shift
=BN_BITS2
-((BN_num_bits(divisor
))%BN_BITS
2);
211 BN_lshift(sdiv
,divisor
,norm_shift
);
213 norm_shift
+=BN_BITS2
;
214 BN_lshift(snum
,num
,norm_shift
);
220 /* Lets setup a 'window' into snum
221 * This is the part that corresponds to the current
222 * 'area' being divided */
224 wnum
.d
= &(snum
->d
[loop
]);
226 wnum
.max
= snum
->max
+1; /* a bit of a lie */
228 /* Get the top 2 words of sdiv */
231 d1
=(div_n
== 1)?0:sdiv
->d
[div_n
-2];
233 /* pointer to the 'top' of snum */
234 wnump
= &(snum
->d
[num_n
-1]);
237 res
->neg
= (num
->neg
^divisor
->neg
);
238 if (!bn_wexpand(res
,(loop
+1))) goto err
;
240 resp
= &(res
->d
[loop
-1]);
243 if (!bn_wexpand(tmp
,(div_n
+1))) goto err
;
245 if (BN_ucmp(&wnum
,sdiv
) >= 0)
247 if (!BN_usub(&wnum
,&wnum
,sdiv
)) goto err
;
249 res
->d
[res
->top
-1]=1;
255 for (i
=0; i
<loop
-1; i
++)
259 q
=bn_div_3_words(wnump
,d1
,d0
);
261 BN_ULONG n0
,n1
,rem
=0;
272 #if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
273 q
=(BN_ULONG
)(((((BN_ULLONG
)n0
)<<BN_BITS2
)|n1
)/d0
);
275 q
=bn_div_words(n0
,n1
,d0
);
278 #ifndef REMAINDER_IS_ALREADY_CALCULATED
280 * rem doesn't have to be BN_ULLONG. The least we
281 * know it's less that d0, isn't it?
283 rem
=(n1
-q
*d0
)&BN_MASK2
;
289 if (t2
<= ((((BN_ULLONG
)rem
)<<BN_BITS2
)|wnump
[-2]))
293 if (rem
< d0
) break; /* don't let rem overflow */
296 #else /* !BN_LLONG */
297 BN_ULONG t2l
,t2h
,ql
,qh
;
299 q
=bn_div_words(n0
,n1
,d0
);
300 #ifndef REMAINDER_IS_ALREADY_CALCULATED
301 rem
=(n1
-q
*d0
)&BN_MASK2
;
306 t2h
= BN_UMULT_HIGH(d1
,q
);
308 t2l
=LBITS(d1
); t2h
=HBITS(d1
);
309 ql
=LBITS(q
); qh
=HBITS(q
);
310 mul64(t2l
,t2h
,ql
,qh
); /* t2=(BN_ULLONG)d1*q; */
316 ((t2h
== rem
) && (t2l
<= wnump
[-2])))
320 if (rem
< d0
) break; /* don't let rem overflow */
321 if (t2l
< d1
) t2h
--; t2l
-= d1
;
323 #endif /* !BN_LLONG */
325 #endif /* !BN_DIV3W */
327 l0
=bn_mul_words(tmp
->d
,sdiv
->d
,div_n
,q
);
328 wnum
.d
--; wnum
.top
++;
330 for (j
=div_n
+1; j
>0; j
--)
331 if (tmp
->d
[j
-1]) break;
335 BN_sub(&wnum
,&wnum
,tmp
);
337 snum
->top
=snum
->top
+wnum
.top
-j
;
343 BN_add(&wnum
,&wnum
,sdiv
);
344 snum
->top
+=wnum
.top
-j
;
351 BN_rshift(rm
,snum
,norm_shift
);
364 int BN_mod(BIGNUM
*rem
, const BIGNUM
*m
, const BIGNUM
*d
, BN_CTX
*ctx
)
366 #if 0 /* The old slow way */
370 if (BN_ucmp(m
,d
) < 0)
371 return((BN_copy(rem
,m
) == NULL
)?0:1);
376 if (!BN_copy(rem
,m
)) goto err
;
380 if (!BN_lshift(dv
,d
,nm
-nd
)) goto err
;
381 for (i
=nm
-nd
; i
>=0; i
--)
383 if (BN_cmp(rem
,dv
) >= 0)
385 if (!BN_sub(rem
,rem
,dv
)) goto err
;
387 if (!BN_rshift1(dv
,dv
)) goto err
;
395 return(BN_div(NULL
,rem
,m
,d
,ctx
));