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/etc/mersenne.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/etc/mersenne.c')
-rw-r--r-- | libtommath/etc/mersenne.c | 140 |
1 files changed, 0 insertions, 140 deletions
diff --git a/libtommath/etc/mersenne.c b/libtommath/etc/mersenne.c deleted file mode 100644 index 28ac834..0000000 --- a/libtommath/etc/mersenne.c +++ /dev/null @@ -1,140 +0,0 @@ -/* Finds Mersenne primes using the Lucas-Lehmer test - * - * Tom St Denis, tomstdenis@gmail.com - */ -#include <time.h> -#include <tommath.h> - -int -is_mersenne (long s, int *pp) -{ - mp_int n, u; - int res, k; - - *pp = 0; - - if ((res = mp_init (&n)) != MP_OKAY) { - return res; - } - - if ((res = mp_init (&u)) != MP_OKAY) { - goto LBL_N; - } - - /* n = 2^s - 1 */ - if ((res = mp_2expt(&n, s)) != MP_OKAY) { - goto LBL_MU; - } - if ((res = mp_sub_d (&n, 1, &n)) != MP_OKAY) { - goto LBL_MU; - } - - /* set u=4 */ - mp_set (&u, 4); - - /* for k=1 to s-2 do */ - for (k = 1; k <= s - 2; k++) { - /* u = u^2 - 2 mod n */ - if ((res = mp_sqr (&u, &u)) != MP_OKAY) { - goto LBL_MU; - } - if ((res = mp_sub_d (&u, 2, &u)) != MP_OKAY) { - goto LBL_MU; - } - - /* make sure u is positive */ - while (u.sign == MP_NEG) { - if ((res = mp_add (&u, &n, &u)) != MP_OKAY) { - goto LBL_MU; - } - } - - /* reduce */ - if ((res = mp_reduce_2k (&u, &n, 1)) != MP_OKAY) { - goto LBL_MU; - } - } - - /* if u == 0 then its prime */ - if (mp_iszero (&u) == 1) { - mp_prime_is_prime(&n, 8, pp); - if (*pp != 1) printf("FAILURE\n"); - } - - res = MP_OKAY; -LBL_MU:mp_clear (&u); -LBL_N:mp_clear (&n); - return res; -} - -/* square root of a long < 65536 */ -long -i_sqrt (long x) -{ - long x1, x2; - - x2 = 16; - do { - x1 = x2; - x2 = x1 - ((x1 * x1) - x) / (2 * x1); - } while (x1 != x2); - - if (x1 * x1 > x) { - --x1; - } - - return x1; -} - -/* is the long prime by brute force */ -int -isprime (long k) -{ - long y, z; - - y = i_sqrt (k); - for (z = 2; z <= y; z++) { - if ((k % z) == 0) - return 0; - } - return 1; -} - - -int -main (void) -{ - int pp; - long k; - clock_t tt; - - k = 3; - - for (;;) { - /* start time */ - tt = clock (); - - /* test if 2^k - 1 is prime */ - if (is_mersenne (k, &pp) != MP_OKAY) { - printf ("Whoa error\n"); - return -1; - } - - if (pp == 1) { - /* count time */ - tt = clock () - tt; - - /* display if prime */ - printf ("2^%-5ld - 1 is prime, test took %ld ticks\n", k, tt); - } - - /* goto next odd exponent */ - k += 2; - - /* but make sure its prime */ - while (isprime (k) == 0) { - k += 2; - } - } - return 0; -} |