2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-19 14:24:39 +00:00
|
|
|
#ifdef MP_DIV_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2004-10-29 22:07:18 +00:00
|
|
|
|
2019-05-12 22:22:18 +00:00
|
|
|
mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
|
2004-10-29 22:07:18 +00:00
|
|
|
{
|
2019-05-19 15:16:13 +00:00
|
|
|
mp_err err;
|
2004-10-29 22:07:18 +00:00
|
|
|
|
2017-08-30 18:23:46 +00:00
|
|
|
/* is divisor zero ? */
|
2019-10-24 15:43:31 +00:00
|
|
|
if (mp_iszero(b)) {
|
2017-08-30 18:23:46 +00:00
|
|
|
return MP_VAL;
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:41:09 +00:00
|
|
|
/* if a < b then q = 0, r = a */
|
2017-08-30 18:23:46 +00:00
|
|
|
if (mp_cmp_mag(a, b) == MP_LT) {
|
|
|
|
if (d != NULL) {
|
2019-10-29 17:41:25 +00:00
|
|
|
if ((err = mp_copy(a, d)) != MP_OKAY) {
|
|
|
|
return err;
|
|
|
|
}
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
|
|
|
if (c != NULL) {
|
|
|
|
mp_zero(c);
|
|
|
|
}
|
2019-10-29 17:41:25 +00:00
|
|
|
return MP_OKAY;
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 15:41:09 +00:00
|
|
|
if (MP_HAS(S_MP_DIV_RECURSIVE)
|
2019-11-24 04:37:43 +00:00
|
|
|
&& (b->used > (2 * MP_MUL_KARATSUBA_CUTOFF))
|
2019-10-24 16:26:25 +00:00
|
|
|
&& (b->used <= ((a->used/3)*2))) {
|
2019-10-04 15:41:09 +00:00
|
|
|
err = s_mp_div_recursive(a, b, c, d);
|
|
|
|
} else if (MP_HAS(S_MP_DIV_SCHOOL)) {
|
|
|
|
err = s_mp_div_school(a, b, c, d);
|
2019-10-29 17:41:25 +00:00
|
|
|
} else if (MP_HAS(S_MP_DIV_SMALL)) {
|
2019-10-04 15:41:09 +00:00
|
|
|
err = s_mp_div_small(a, b, c, d);
|
2019-10-29 17:41:25 +00:00
|
|
|
} else {
|
|
|
|
err = MP_VAL;
|
2017-08-30 18:23:46 +00:00
|
|
|
}
|
2003-05-17 12:33:54 +00:00
|
|
|
|
2019-05-19 15:16:13 +00:00
|
|
|
return err;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|