libtommath/etc/mersenne.c

138 lines
2.3 KiB
C
Raw Normal View History

2017-10-17 18:51:19 +00:00
/* Finds Mersenne primes using the Lucas-Lehmer test
2003-02-28 16:06:56 +00:00
*
2006-04-06 19:49:59 +00:00
* Tom St Denis, tomstdenis@gmail.com
2003-02-28 16:06:56 +00:00
*/
#include <time.h>
2003-03-13 02:11:11 +00:00
#include <tommath.h>
2003-02-28 16:06:56 +00:00
2018-02-01 21:28:43 +00:00
static int is_mersenne(long s, int *pp)
2003-02-28 16:06:56 +00:00
{
2017-10-18 08:44:06 +00:00
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 */
2018-02-05 19:22:17 +00:00
if ((res = mp_2expt(&n, (int)s)) != MP_OKAY) {
2004-12-23 02:40:37 +00:00
goto LBL_MU;
2017-10-18 08:44:06 +00:00
}
2018-02-13 18:04:25 +00:00
if ((res = mp_sub_d(&n, 1uL, &n)) != MP_OKAY) {
2004-12-23 02:40:37 +00:00
goto LBL_MU;
2017-10-18 08:44:06 +00:00
}
/* set u=4 */
2018-02-13 18:04:25 +00:00
mp_set(&u, 4uL);
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
/* for k=1 to s-2 do */
for (k = 1; k <= (s - 2); k++) {
2017-10-18 08:44:06 +00:00
/* u = u^2 - 2 mod n */
if ((res = mp_sqr(&u, &u)) != MP_OKAY) {
goto LBL_MU;
}
2018-02-13 18:04:25 +00:00
if ((res = mp_sub_d(&u, 2uL, &u)) != MP_OKAY) {
2004-12-23 02:40:37 +00:00
goto LBL_MU;
2003-02-28 16:06:56 +00:00
}
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
/* make sure u is positive */
while (u.sign == MP_NEG) {
if ((res = mp_add(&u, &n, &u)) != MP_OKAY) {
goto LBL_MU;
}
}
/* reduce */
2018-02-13 18:04:25 +00:00
if ((res = mp_reduce_2k(&u, &n, 1uL)) != MP_OKAY) {
2017-10-18 08:44:06 +00:00
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;
2003-02-28 16:06:56 +00:00
}
/* square root of a long < 65536 */
2018-02-01 21:28:43 +00:00
static long i_sqrt(long x)
2003-02-28 16:06:56 +00:00
{
2017-10-18 08:44:06 +00:00
long x1, x2;
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
x2 = 16;
do {
x1 = x2;
x2 = x1 - ((x1 * x1) - x) / (2 * x1);
} while (x1 != x2);
2003-02-28 16:08:34 +00:00
if ((x1 * x1) > x) {
2017-10-18 08:44:06 +00:00
--x1;
}
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
return x1;
2003-02-28 16:06:56 +00:00
}
/* is the long prime by brute force */
2018-02-01 21:28:43 +00:00
static int isprime(long k)
2003-02-28 16:06:56 +00:00
{
2017-10-18 08:44:06 +00:00
long y, z;
y = i_sqrt(k);
for (z = 2; z <= y; z++) {
if ((k % z) == 0)
return 0;
}
return 1;
2003-02-28 16:08:34 +00:00
}
2017-10-18 08:44:06 +00:00
int main(void)
2003-02-28 16:06:56 +00:00
{
2017-10-18 08:44:06 +00:00
int pp;
long k;
clock_t tt;
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
k = 3;
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
for (;;) {
/* start time */
tt = clock();
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
/* test if 2^k - 1 is prime */
if (is_mersenne(k, &pp) != MP_OKAY) {
printf("Whoa error\n");
return -1;
}
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
if (pp == 1) {
/* count time */
tt = clock() - tt;
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
/* display if prime */
printf("2^%-5ld - 1 is prime, test took %ld ticks\n", k, tt);
}
2003-02-28 16:08:34 +00:00
2017-10-18 08:44:06 +00:00
/* goto next odd exponent */
2003-02-28 16:06:56 +00:00
k += 2;
2017-10-18 08:44:06 +00:00
/* but make sure its prime */
while (isprime(k) == 0) {
k += 2;
}
}
2003-02-28 16:06:56 +00:00
}