2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-29 19:52:29 +00:00
|
|
|
#ifdef MP_ROOT_N_C
|
2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2003-02-28 16:08:34 +00:00
|
|
|
|
2019-05-25 04:42:01 +00:00
|
|
|
/* find the n'th root of an integer
|
|
|
|
*
|
|
|
|
* Result found such that (c)**b <= a and (c+1)**b > a
|
|
|
|
*
|
|
|
|
* This algorithm uses Newton's approximation
|
|
|
|
* x[i+1] = x[i] - f(x[i])/f'(x[i])
|
|
|
|
* which will find the root in log(N) time where
|
|
|
|
* each step involves a fair bit.
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
2019-10-29 19:52:29 +00:00
|
|
|
mp_err mp_root_n(const mp_int *a, int b, mp_int *c)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2019-05-25 04:42:01 +00:00
|
|
|
mp_int t1, t2, t3, a_;
|
|
|
|
int ilog2;
|
|
|
|
mp_err err;
|
|
|
|
|
2019-10-29 19:52:29 +00:00
|
|
|
if (b < 0 || (unsigned)b > (unsigned)MP_DIGIT_MAX) {
|
|
|
|
return MP_VAL;
|
|
|
|
}
|
|
|
|
|
2019-05-25 04:42:01 +00:00
|
|
|
/* input must be positive if b is even */
|
2019-10-29 19:52:29 +00:00
|
|
|
if (((b & 1) == 0) && mp_isneg(a)) {
|
2019-05-25 04:42:01 +00:00
|
|
|
return MP_VAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = mp_init_multi(&t1, &t2, &t3, NULL)) != MP_OKAY) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if a is negative fudge the sign but keep track */
|
|
|
|
a_ = *a;
|
|
|
|
a_.sign = MP_ZPOS;
|
|
|
|
|
|
|
|
/* Compute seed: 2^(log_2(n)/b + 2)*/
|
|
|
|
ilog2 = mp_count_bits(a);
|
|
|
|
|
|
|
|
/*
|
2019-05-29 18:36:18 +00:00
|
|
|
If "b" is larger than INT_MAX it is also larger than
|
|
|
|
log_2(n) because the bit-length of the "n" is measured
|
|
|
|
with an int and hence the root is always < 2 (two).
|
2019-05-25 04:42:01 +00:00
|
|
|
*/
|
2019-10-29 19:52:29 +00:00
|
|
|
if (b > INT_MAX/2) {
|
2019-05-29 18:36:18 +00:00
|
|
|
mp_set(c, 1uL);
|
|
|
|
c->sign = a->sign;
|
|
|
|
err = MP_OKAY;
|
|
|
|
goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
}
|
2019-05-29 18:36:18 +00:00
|
|
|
|
2019-05-25 04:42:01 +00:00
|
|
|
/* "b" is smaller than INT_MAX, we can cast safely */
|
2019-10-29 19:52:29 +00:00
|
|
|
if (ilog2 < b) {
|
2019-05-25 04:42:01 +00:00
|
|
|
mp_set(c, 1uL);
|
|
|
|
c->sign = a->sign;
|
|
|
|
err = MP_OKAY;
|
|
|
|
goto LBL_ERR;
|
|
|
|
}
|
2019-10-29 19:52:29 +00:00
|
|
|
ilog2 = ilog2 / b;
|
2019-05-25 04:42:01 +00:00
|
|
|
if (ilog2 == 0) {
|
|
|
|
mp_set(c, 1uL);
|
|
|
|
c->sign = a->sign;
|
|
|
|
err = MP_OKAY;
|
|
|
|
goto LBL_ERR;
|
|
|
|
}
|
|
|
|
/* Start value must be larger than root */
|
|
|
|
ilog2 += 2;
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_2expt(&t2,ilog2)) != MP_OKAY) goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
do {
|
|
|
|
/* t1 = t2 */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_copy(&t2, &t1)) != MP_OKAY) goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
|
|
|
|
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
|
|
|
|
|
|
|
/* t3 = t1**(b-1) */
|
2019-10-29 19:52:29 +00:00
|
|
|
if ((err = mp_expt_n(&t1, b - 1, &t3)) != MP_OKAY) goto LBL_ERR;
|
2019-09-04 06:40:39 +00:00
|
|
|
|
2019-05-25 04:42:01 +00:00
|
|
|
/* numerator */
|
|
|
|
/* t2 = t1**b */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_mul(&t3, &t1, &t2)) != MP_OKAY) goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
|
|
|
|
/* t2 = t1**b - a */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub(&t2, &a_, &t2)) != MP_OKAY) goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
|
|
|
|
/* denominator */
|
|
|
|
/* t3 = t1**(b-1) * b */
|
2019-10-29 19:52:29 +00:00
|
|
|
if ((err = mp_mul_d(&t3, (mp_digit)b, &t3)) != MP_OKAY) goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
|
|
|
|
/* t3 = (t1**b - a)/(b * t1**(b-1)) */
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY) goto LBL_ERR;
|
|
|
|
|
|
|
|
if ((err = mp_sub(&t1, &t3, &t2)) != MP_OKAY) goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Number of rounds is at most log_2(root). If it is more it
|
|
|
|
got stuck, so break out of the loop and do the rest manually.
|
|
|
|
*/
|
|
|
|
if (ilog2-- == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (mp_cmp(&t1, &t2) != MP_EQ);
|
|
|
|
|
|
|
|
/* result can be off by a few so check */
|
|
|
|
/* Loop beneath can overshoot by one if found root is smaller than actual root */
|
|
|
|
for (;;) {
|
2019-10-29 19:52:29 +00:00
|
|
|
mp_ord cmp;
|
|
|
|
if ((err = mp_expt_n(&t1, b, &t2)) != MP_OKAY) goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
cmp = mp_cmp(&t2, &a_);
|
|
|
|
if (cmp == MP_EQ) {
|
|
|
|
err = MP_OKAY;
|
|
|
|
goto LBL_ERR;
|
|
|
|
}
|
|
|
|
if (cmp == MP_LT) {
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_add_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* correct overshoot from above or from recurrence */
|
|
|
|
for (;;) {
|
2019-10-29 19:52:29 +00:00
|
|
|
if ((err = mp_expt_n(&t1, b, &t2)) != MP_OKAY) goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
if (mp_cmp(&t2, &a_) == MP_GT) {
|
2019-09-04 06:40:39 +00:00
|
|
|
if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_ERR;
|
2019-05-25 04:42:01 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the result */
|
|
|
|
mp_exch(&t1, c);
|
|
|
|
|
|
|
|
/* set the sign of the result */
|
|
|
|
c->sign = a->sign;
|
|
|
|
|
|
|
|
LBL_ERR:
|
|
|
|
mp_clear_multi(&t1, &t2, &t3, NULL);
|
|
|
|
return err;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2014-02-14 10:26:07 +00:00
|
|
|
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|