1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/* Finds Mersenne primes using the Lucas-Lehmer test
*
* Tom St Denis, tomstdenis@iahu.ca
*/
#include <time.h>
#include <bn.h>
int is_mersenne(long s, int *pp)
{
mp_int n, u, mu;
int res, k;
long ss;
*pp = 0;
if ((res = mp_init(&n)) != MP_OKAY) {
return res;
}
if ((res = mp_init(&u)) != MP_OKAY) {
goto __N;
}
if ((res = mp_init(&mu)) != MP_OKAY) {
goto __U;
}
/* n = 2^s - 1 */
mp_set(&n, 1);
ss = s;
while (ss--) {
if ((res = mp_mul_2(&n, &n)) != MP_OKAY) {
goto __MU;
}
}
if ((res = mp_sub_d(&n, 1, &n)) != MP_OKAY) {
goto __MU;
}
/* setup mu */
if ((res = mp_reduce_setup(&mu, &n)) != MP_OKAY) {
goto __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 __MU;
}
if ((res = mp_sub_d(&u, 2, &u)) != MP_OKAY) {
goto __MU;
}
/* make sure u is positive */
if (u.sign == MP_NEG) {
if ((res = mp_add(&u, &n, &u)) != MP_OKAY) {
goto __MU;
}
}
/* reduce */
if ((res = mp_reduce(&u, &n, &mu)) != MP_OKAY) {
goto __MU;
}
}
/* if u == 0 then its prime */
if (mp_iszero(&u) == 1) {
*pp = 1;
}
res = MP_OKAY;
__MU: mp_clear(&mu);
__U: mp_clear(&u);
__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;
}