2004-10-29 22:07:18 +00:00
|
|
|
#include <tommath.h>
|
|
|
|
#ifdef BN_MP_N_ROOT_C
|
2003-02-28 16:08:34 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
|
|
|
*
|
2003-08-05 01:24:44 +00:00
|
|
|
* LibTomMath is a library that provides multiple-precision
|
2003-02-28 16:08:34 +00:00
|
|
|
* integer arithmetic as well as number theoretic functionality.
|
|
|
|
*
|
2003-08-05 01:24:44 +00:00
|
|
|
* The library was designed directly after the MPI library by
|
2003-02-28 16:08:34 +00:00
|
|
|
* Michael Fromberger but has been written from scratch with
|
|
|
|
* additional optimizations in place.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*
|
2003-03-13 02:11:11 +00:00
|
|
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* find the n'th root of an integer
|
|
|
|
*
|
2003-06-19 10:04:50 +00:00
|
|
|
* Result found such that (c)**b <= a and (c+1)**b > a
|
2003-02-28 16:09:08 +00:00
|
|
|
*
|
2003-06-19 10:04:50 +00:00
|
|
|
* 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. This is not meant to
|
|
|
|
* find huge roots [square and cube, etc].
|
2003-02-28 16:08:34 +00:00
|
|
|
*/
|
2003-12-24 18:59:22 +00:00
|
|
|
int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
|
2003-02-28 16:08:34 +00:00
|
|
|
{
|
2003-02-28 16:09:08 +00:00
|
|
|
mp_int t1, t2, t3;
|
|
|
|
int res, neg;
|
2003-02-28 16:08:34 +00:00
|
|
|
|
|
|
|
/* input must be positive if b is even */
|
|
|
|
if ((b & 1) == 0 && a->sign == MP_NEG) {
|
|
|
|
return MP_VAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = mp_init (&t1)) != MP_OKAY) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = mp_init (&t2)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T1;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = mp_init (&t3)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T2;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if a is negative fudge the sign but keep track */
|
2003-08-05 01:24:44 +00:00
|
|
|
neg = a->sign;
|
2003-02-28 16:08:34 +00:00
|
|
|
a->sign = MP_ZPOS;
|
|
|
|
|
|
|
|
/* t2 = 2 */
|
|
|
|
mp_set (&t2, 2);
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* t1 = t2 */
|
|
|
|
if ((res = mp_copy (&t2, &t1)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T3;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
2003-06-19 10:04:50 +00:00
|
|
|
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
|
|
|
|
|
|
|
/* t3 = t1**(b-1) */
|
|
|
|
if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T3;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* numerator */
|
2003-06-19 10:04:50 +00:00
|
|
|
/* t2 = t1**b */
|
|
|
|
if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T3;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
2003-06-19 10:04:50 +00:00
|
|
|
/* t2 = t1**b - a */
|
|
|
|
if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T3;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
2003-06-19 10:04:50 +00:00
|
|
|
/* denominator */
|
|
|
|
/* t3 = t1**(b-1) * b */
|
|
|
|
if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T3;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
2003-06-19 10:04:50 +00:00
|
|
|
/* t3 = (t1**b - a)/(b * t1**(b-1)) */
|
|
|
|
if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T3;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T3;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
2003-06-19 10:04:50 +00:00
|
|
|
} while (mp_cmp (&t1, &t2) != MP_EQ);
|
2003-02-28 16:08:34 +00:00
|
|
|
|
|
|
|
/* result can be off by a few so check */
|
|
|
|
for (;;) {
|
|
|
|
if ((res = mp_expt_d (&t1, b, &t2)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T3;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mp_cmp (&t2, a) == MP_GT) {
|
|
|
|
if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) {
|
2004-12-23 02:40:37 +00:00
|
|
|
goto LBL_T3;
|
2003-02-28 16:08:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset the sign of a first */
|
|
|
|
a->sign = neg;
|
|
|
|
|
|
|
|
/* set the result */
|
|
|
|
mp_exch (&t1, c);
|
|
|
|
|
|
|
|
/* set the sign of the result */
|
|
|
|
c->sign = neg;
|
|
|
|
|
|
|
|
res = MP_OKAY;
|
|
|
|
|
2004-12-23 02:40:37 +00:00
|
|
|
LBL_T3:mp_clear (&t3);
|
|
|
|
LBL_T2:mp_clear (&t2);
|
|
|
|
LBL_T1:mp_clear (&t1);
|
2003-02-28 16:08:34 +00:00
|
|
|
return res;
|
|
|
|
}
|
2004-10-29 22:07:18 +00:00
|
|
|
#endif
|