#include <openssl/bn.h>
#include <assert.h>
#include <limits.h>
#include <openssl/err.h>
#include "internal.h"
#if !defined(BN_ULLONG)
static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) {
BN_ULONG dh, dl, q, ret = 0, th, tl, t;
int i, count = 2;
if (d == 0) {
return BN_MASK2;
}
i = BN_num_bits_word(d);
assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
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;
}
th = q * dh;
tl = dl * q;
for (;;) {
t = h - th;
if ((t & BN_MASK2h) ||
((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4)))) {
break;
}
q--;
th -= dh;
tl -= dl;
}
t = (tl >> BN_BITS4);
tl = (tl << BN_BITS4) & BN_MASK2h;
th += t;
if (l < tl) {
th++;
}
l -= tl;
if (h < th) {
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;
}
#endif
static inline void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out,
BN_ULONG n0, BN_ULONG n1, BN_ULONG d0) {
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && defined(__GNUC__)
__asm__ volatile (
"divl %4"
: "=a"(*quotient_out), "=d"(*rem_out)
: "a"(n1), "d"(n0), "g"(d0)
: "cc" );
#elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__GNUC__)
__asm__ volatile (
"divq %4"
: "=a"(*quotient_out), "=d"(*rem_out)
: "a"(n1), "d"(n0), "g"(d0)
: "cc" );
#else
#if defined(BN_ULLONG)
BN_ULLONG n = (((BN_ULLONG)n0) << BN_BITS2) | n1;
*quotient_out = (BN_ULONG)(n / d0);
#else
*quotient_out = bn_div_words(n0, n1, d0);
#endif
*rem_out = n1 - (*quotient_out * d0);
#endif
}
int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor) {
int norm_shift, i, loop;
BN_ULONG *resp, *wnump;
BN_ULONG d0, d1;
int num_n, div_n;
if ((num->top > 0 && num->d[num->top - 1] == 0) ||
(divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {
OPENSSL_PUT_ERROR(BN, BN_R_NOT_INITIALIZED);
return 0;
}
if (BN_is_zero(divisor)) {
OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
return 0;
}
BIGNUM snum;
BN_init(&snum);
BIGNUM sdiv;
BN_init(&sdiv);
BIGNUM tmp;
BN_init(&tmp);
BIGNUM res_tmp;
BN_init(&res_tmp);
BIGNUM wnum;
BIGNUM *res;
if (dv == NULL) {
res = &res_tmp;
} else {
res = dv;
}
int ret = 0;
norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
if (!(BN_lshift(&sdiv, divisor, norm_shift))) {
goto err;
}
sdiv.neg = 0;
norm_shift += BN_BITS2;
if (!(BN_lshift(&snum, num, norm_shift))) {
goto err;
}
snum.neg = 0;
if (snum.top <= sdiv.top + 1) {
if (bn_wexpand(&snum, sdiv.top + 2) == NULL) {
goto err;
}
for (i = snum.top; i < sdiv.top + 2; i++) {
snum.d[i] = 0;
}
snum.top = sdiv.top + 2;
} else {
if (bn_wexpand(&snum, snum.top + 1) == NULL) {
goto err;
}
snum.d[snum.top] = 0;
snum.top++;
}
div_n = sdiv.top;
num_n = snum.top;
loop = num_n - div_n;
wnum.neg = 0;
wnum.d = &(snum.d[loop]);
wnum.top = div_n;
wnum.dmax = snum.dmax - loop;
d0 = sdiv.d[div_n - 1];
d1 = (div_n == 1) ? 0 : sdiv.d[div_n - 2];
wnump = &(snum.d[num_n - 1]);
res->neg = (num->neg ^ divisor->neg);
if (!bn_wexpand(res, (loop + 1))) {
goto err;
}
res->top = loop - 1;
resp = &(res->d[loop - 1]);
if (!bn_wexpand(&tmp, (div_n + 1))) {
goto err;
}
if (res->top == 0) {
res->neg = 0;
} else {
resp--;
}
for (i = 0; i < loop - 1; i++, wnump--, resp--) {
BN_ULONG q, l0;
BN_ULONG n0, n1, rem = 0;
n0 = wnump[0];
n1 = wnump[-1];
if (n0 == d0) {
q = BN_MASK2;
} else {
bn_div_rem_words(&q, &rem, n0, n1, d0);
#ifdef BN_ULLONG
BN_ULLONG t2 = (BN_ULLONG)d1 * q;
for (;;) {
if (t2 <= ((((BN_ULLONG)rem) << BN_BITS2) | wnump[-2])) {
break;
}
q--;
rem += d0;
if (rem < d0) {
break;
}
t2 -= d1;
}
#else
BN_ULONG t2l, t2h;
bn_umult_lohi(&t2l, &t2h, d1, q);
for (;;) {
if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2]))) {
break;
}
q--;
rem += d0;
if (rem < d0) {
break;
}
if (t2l < d1) {
t2h--;
}
t2l -= d1;
}
#endif
}
l0 = bn_mul_words(tmp.d, sdiv.d, div_n, q);
tmp.d[div_n] = l0;
wnum.d--;
if (bn_sub_words(wnum.d, wnum.d, tmp.d, div_n + 1)) {
q--;
if (bn_add_words(wnum.d, wnum.d, sdiv.d, div_n)) {
(*wnump)++;
}
}
*resp = q;
}
bn_correct_top(&snum);
if (rm != NULL) {
int neg = num->neg;
if (!BN_rshift(rm, &snum, norm_shift)) {
goto err;
}
if (!BN_is_zero(rm)) {
rm->neg = neg;
}
}
bn_correct_top(res);
ret = 1;
err:
BN_free(&tmp);
BN_free(&snum);
BN_free(&sdiv);
BN_free(&res_tmp);
return ret;
}
int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d) {
if (!(BN_mod(r, m, d))) {
return 0;
}
if (!r->neg) {
return 1;
}
return (d->neg ? BN_sub : BN_add)(r, r, d);
}
int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
const BIGNUM *m) {
if (!BN_sub(r, a, b)) {
return 0;
}
if (r->neg) {
return BN_add(r, r, m);
}
return 1;
}