bn_div.c revision e915367e40b579d18ac13c9c58c15fec614d9890
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * All rights reserved.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * This package is an SSL implementation written
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * by Eric Young (eay@cryptsoft.com).
15a44745412679c30a6d022733925af70a38b715David Lawrence * The implementation was written so as to conform with Netscapes SSL.
15a44745412679c30a6d022733925af70a38b715David Lawrence * This library is free for commercial and non-commercial use as long as
15a44745412679c30a6d022733925af70a38b715David Lawrence * the following conditions are aheared to. The following conditions
15a44745412679c30a6d022733925af70a38b715David Lawrence * apply to all code found in this distribution, be it the RC4, RSA,
15a44745412679c30a6d022733925af70a38b715David Lawrence * lhash, DES, etc., code; not just the SSL code. The SSL documentation
15a44745412679c30a6d022733925af70a38b715David Lawrence * included with this distribution is covered by the same copyright terms
15a44745412679c30a6d022733925af70a38b715David Lawrence * except that the holder is Tim Hudson (tjh@cryptsoft.com).
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Copyright remains Eric Young's, and as such any Copyright notices in
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews * the code are not to be removed.
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * If this package is used in a product, Eric Young should be given attribution
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff * as the author of the parts of the library used.
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff * This can be in the form of a textual message at program startup or
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley * in documentation (online or textual) provided with the package.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Redistribution and use in source and binary forms, with or without
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews * modification, are permitted provided that the following conditions
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * 1. Redistributions of source code must retain the copyright
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * notice, this list of conditions and the following disclaimer.
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley * 2. Redistributions in binary form must reproduce the above copyright
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley * notice, this list of conditions and the following disclaimer in the
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * documentation and/or other materials provided with the distribution.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * 3. All advertising materials mentioning features or use of this software
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * must display the following acknowledgement:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * "This product includes cryptographic software written by
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Eric Young (eay@cryptsoft.com)"
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * The word 'cryptographic' can be left out if the rouines from the library
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * being used are not cryptographic related :-).
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * 4. If you include any Windows specific code (or a derivative thereof) from
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * the apps directory (application code) you must include an acknowledgement:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrews * SUCH DAMAGE.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * The licence and distribution terms for any publically available version or
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * derivative of this code cannot be changed. i.e. this code cannot simply be
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews * copied and put under another distribution licence
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley * [including the GNU Public Licence.]
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/* The old slow way */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleyint BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (BN_ucmp(m,d) < 0)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley /* The next 2 are needed so we can do a dv->d[0]|=1 later
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley * since BN_lshift1 will only work once there is a value :-) */
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley/* CAN IMPROVE (and have now :=) */
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley#if !defined(NO_ASM) && !defined(NO_INLINE_ASM) && !defined(PEDANTIC) && !defined(BN_DIV3W)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley# if defined(__i386)
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff * There were two reasons for implementing this template:
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * - GNU C generates a call to a function (__udivdi3 to be exact)
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson * in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson * understand why...);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * - divl doesn't only calculate quotient, but also leaves
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * remainder in %edx which we can definitely use here:-)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * <appro@fy.chalmers.se>
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ({ asm volatile ( \
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley# endif /* __<cpu> */
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley# endif /* __GNUC__ */
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley#endif /* NO_ASM */
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halleyint BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley /* First we normalise the numbers */
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley /* Lets setup a 'window' into snum
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley * This is the part that corresponds to the current
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley * 'area' being divided */
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley /* Get the top 2 words of sdiv */
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley /* i=sdiv->top; */
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley /* pointer to the 'top' of snum */
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley /* Setup to 'res' */
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley /* space for temp */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley else /* n0 < d0 */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley q=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * rem doesn't have to be BN_ULLONG. The least we
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * know it's less that d0, isn't it?
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (rem < d0) break; /* don't let rem overflow */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson#else /* !BN_LLONG */
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (rem < d0) break; /* don't let rem overflow */
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley#endif /* !BN_LLONG */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson#endif /* !BN_DIV3W */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonint BN_mod(BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson#if 0 /* The old slow way */