diff options
author | gahr <gahr@gahr.ch> | 2016-03-11 17:13:38 (GMT) |
---|---|---|
committer | gahr <gahr@gahr.ch> | 2016-03-11 17:13:38 (GMT) |
commit | 48a3d3e4dca22efb2981fd8eb9b505b19438a704 (patch) | |
tree | f0acc29307113ad908a79330d8b8c924c8ad79ae /libtommath/bn_mp_n_root.c | |
parent | 5137357e74c887368ad68c3cb09998b154e68d4f (diff) | |
download | tcl-48a3d3e4dca22efb2981fd8eb9b505b19438a704.zip tcl-48a3d3e4dca22efb2981fd8eb9b505b19438a704.tar.gz tcl-48a3d3e4dca22efb2981fd8eb9b505b19438a704.tar.bz2 |
[e6f27aa56f] Initial import of libtommath-1.0
This commit brings in libtommath-1.0, as of tag v1.0 of the upstream repository
at https://github.com/libtom/libtommath.
Only the top-level directory has been imported: the demo, etc, logs, mtest,
pics, pre_gen, and tombc directories have been removed from our repo.
The stubs tables have been regenerated to accomodate for the new function:
mp_expt_d_ex(mp_int *a, mp_digit b, mp_int *c, int fast);
See https://github.com/libtom/libtommath/commit/e9b1837 for details.
Only the unix build system has been modified for now. Windows and Mac OSX will
come later.
Diffstat (limited to 'libtommath/bn_mp_n_root.c')
-rw-r--r-- | libtommath/bn_mp_n_root.c | 118 |
1 files changed, 10 insertions, 108 deletions
diff --git a/libtommath/bn_mp_n_root.c b/libtommath/bn_mp_n_root.c index b2700a8..a14ee67 100644 --- a/libtommath/bn_mp_n_root.c +++ b/libtommath/bn_mp_n_root.c @@ -1,4 +1,4 @@ -#include <tommath.h> +#include <tommath_private.h> #ifdef BN_MP_N_ROOT_C /* LibTomMath, multiple-precision integer library -- Tom St Denis * @@ -12,117 +12,19 @@ * The library is free for all purposes without any express * guarantee it works. * - * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com + * Tom St Denis, tstdenis82@gmail.com, http://libtom.org */ -/* 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. This is not meant to - * find huge roots [square and cube, etc]. +/* wrapper function for mp_n_root_ex() + * computes c = (a)**(1/b) such that (c)**b <= a and (c+1)**b > a */ int mp_n_root (mp_int * a, mp_digit b, mp_int * c) { - mp_int t1, t2, t3; - int res, neg; - - /* 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) { - goto LBL_T1; - } - - if ((res = mp_init (&t3)) != MP_OKAY) { - goto LBL_T2; - } - - /* if a is negative fudge the sign but keep track */ - neg = a->sign; - a->sign = MP_ZPOS; - - /* t2 = 2 */ - mp_set (&t2, 2); - - do { - /* t1 = t2 */ - if ((res = mp_copy (&t2, &t1)) != MP_OKAY) { - goto LBL_T3; - } - - /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */ - - /* t3 = t1**(b-1) */ - if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) { - goto LBL_T3; - } - - /* numerator */ - /* t2 = t1**b */ - if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) { - goto LBL_T3; - } - - /* t2 = t1**b - a */ - if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) { - goto LBL_T3; - } - - /* denominator */ - /* t3 = t1**(b-1) * b */ - if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) { - goto LBL_T3; - } - - /* t3 = (t1**b - a)/(b * t1**(b-1)) */ - if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) { - goto LBL_T3; - } - - if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) { - goto LBL_T3; - } - } while (mp_cmp (&t1, &t2) != MP_EQ); - - /* result can be off by a few so check */ - for (;;) { - if ((res = mp_expt_d (&t1, b, &t2)) != MP_OKAY) { - goto LBL_T3; - } - - if (mp_cmp (&t2, a) == MP_GT) { - if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) { - goto LBL_T3; - } - } 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; - -LBL_T3:mp_clear (&t3); -LBL_T2:mp_clear (&t2); -LBL_T1:mp_clear (&t1); - return res; + return mp_n_root_ex(a, b, c, 0); } + #endif + +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ |