check fopen

This commit is contained in:
Francois Perrad 2017-12-10 10:12:09 +01:00
parent 0dbea3b69f
commit 44d14a398d
3 changed files with 184 additions and 178 deletions

View File

@ -16,63 +16,65 @@ int main(void)
mp_init_multi(&q, &p, NULL); mp_init_multi(&q, &p, NULL);
out = fopen("2kprime.1", "w"); out = fopen("2kprime.1", "w");
for (x = 0; x < (int)(sizeof(sizes) / sizeof(sizes[0])); x++) { if (out != NULL) {
for (x = 0; x < (int)(sizeof(sizes) / sizeof(sizes[0])); x++) {
top: top:
mp_2expt(&q, sizes[x]); mp_2expt(&q, sizes[x]);
mp_add_d(&q, 3, &q); mp_add_d(&q, 3, &q);
z = -3; z = -3;
t1 = clock(); t1 = clock();
for (;;) { for (;;) {
mp_sub_d(&q, 4, &q); mp_sub_d(&q, 4, &q);
z += 4; z += 4;
if (z > MP_MASK) {
printf("No primes of size %d found\n", sizes[x]);
break;
}
if ((clock() - t1) > CLOCKS_PER_SEC) {
printf(".");
fflush(stdout);
// sleep((clock() - t1 + CLOCKS_PER_SEC/2)/CLOCKS_PER_SEC);
t1 = clock();
}
/* quick test on q */
mp_prime_is_prime(&q, 1, &y);
if (y == 0) {
continue;
}
/* find (q-1)/2 */
mp_sub_d(&q, 1, &p);
mp_div_2(&p, &p);
mp_prime_is_prime(&p, 3, &y);
if (y == 0) {
continue;
}
/* test on q */
mp_prime_is_prime(&q, 3, &y);
if (y == 0) {
continue;
}
if (z > MP_MASK) {
printf("No primes of size %d found\n", sizes[x]);
break; break;
} }
if ((clock() - t1) > CLOCKS_PER_SEC) {
printf(".");
fflush(stdout);
// sleep((clock() - t1 + CLOCKS_PER_SEC/2)/CLOCKS_PER_SEC);
t1 = clock();
}
/* quick test on q */
mp_prime_is_prime(&q, 1, &y);
if (y == 0) { if (y == 0) {
continue; ++sizes[x];
goto top;
} }
/* find (q-1)/2 */ mp_toradix(&q, buf, 10);
mp_sub_d(&q, 1, &p); printf("\n\n%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
mp_div_2(&p, &p); fprintf(out, "%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
mp_prime_is_prime(&p, 3, &y); fflush(out);
if (y == 0) {
continue;
}
/* test on q */
mp_prime_is_prime(&q, 3, &y);
if (y == 0) {
continue;
}
break;
} }
fclose(out);
if (y == 0) {
++sizes[x];
goto top;
}
mp_toradix(&q, buf, 10);
printf("\n\n%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
fprintf(out, "%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
fflush(out);
} }
fclose(out);
return 0; return 0;
} }

View File

@ -14,48 +14,50 @@ int main(void)
mp_init(&b); mp_init(&b);
out = fopen("drprimes.txt", "w"); out = fopen("drprimes.txt", "w");
for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) { if (out != NULL) {
for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
top: top:
printf("Seeking a %d-bit safe prime\n", sizes[x] * DIGIT_BIT); printf("Seeking a %d-bit safe prime\n", sizes[x] * DIGIT_BIT);
mp_grow(&a, sizes[x]); mp_grow(&a, sizes[x]);
mp_zero(&a); mp_zero(&a);
for (y = 1; y < sizes[x]; y++) { for (y = 1; y < sizes[x]; y++) {
a.dp[y] = MP_MASK; a.dp[y] = MP_MASK;
} }
/* make a DR modulus */ /* make a DR modulus */
a.dp[0] = -1; a.dp[0] = -1;
a.used = sizes[x]; a.used = sizes[x];
/* now loop */ /* now loop */
res = 0; res = 0;
for (;;) { for (;;) {
a.dp[0] += 4; a.dp[0] += 4;
if (a.dp[0] >= MP_MASK) break; if (a.dp[0] >= MP_MASK) break;
mp_prime_is_prime(&a, 1, &res); mp_prime_is_prime(&a, 1, &res);
if (res == 0) continue; if (res == 0) continue;
printf("."); printf(".");
fflush(stdout); fflush(stdout);
mp_sub_d(&a, 1, &b); mp_sub_d(&a, 1, &b);
mp_div_2(&b, &b); mp_div_2(&b, &b);
mp_prime_is_prime(&b, 3, &res); mp_prime_is_prime(&b, 3, &res);
if (res == 0) continue; if (res == 0) continue;
mp_prime_is_prime(&a, 3, &res); mp_prime_is_prime(&a, 3, &res);
if (res == 1) break; if (res == 1) break;
} }
if (res != 1) { if (res != 1) {
printf("Error not DR modulus\n"); printf("Error not DR modulus\n");
sizes[x] += 1; sizes[x] += 1;
goto top; goto top;
} else { } else {
mp_toradix(&a, buf, 10); mp_toradix(&a, buf, 10);
printf("\n\np == %s\n\n", buf); printf("\n\np == %s\n\n", buf);
fprintf(out, "%d-bit prime:\np == %s\n\n", mp_count_bits(&a), buf); fprintf(out, "%d-bit prime:\np == %s\n\n", mp_count_bits(&a), buf);
fflush(out); fflush(out);
}
} }
fclose(out);
} }
fclose(out);
mp_clear(&a); mp_clear(&a);
mp_clear(&b); mp_clear(&b);

View File

@ -36,8 +36,9 @@ static void gen_prime(void)
FILE *out; FILE *out;
out = fopen("pprime.dat", "wb"); out = fopen("pprime.dat", "wb");
if (out != NULL) {
/* write first set of primes */ /* write first set of primes */
/* *INDENT-OFF* */ /* *INDENT-OFF* */
r = 3; fwrite(&r, 1, sizeof(mp_digit), out); r = 3; fwrite(&r, 1, sizeof(mp_digit), out);
r = 5; fwrite(&r, 1, sizeof(mp_digit), out); r = 5; fwrite(&r, 1, sizeof(mp_digit), out);
@ -51,105 +52,106 @@ static void gen_prime(void)
r = 31; fwrite(&r, 1, sizeof(mp_digit), out); r = 31; fwrite(&r, 1, sizeof(mp_digit), out);
/* *INDENT-ON* */ /* *INDENT-ON* */
/* get square root, since if 'r' is composite its factors must be < than this */ /* get square root, since if 'r' is composite its factors must be < than this */
y = i_sqrt(r); y = i_sqrt(r);
next = (y + 1) * (y + 1); next = (y + 1) * (y + 1);
for (;;) { for (;;) {
do { do {
r += 2; /* next candidate */ r += 2; /* next candidate */
r &= MP_MASK; r &= MP_MASK;
if (r < 31) break;
/* update sqrt ? */
if (next <= r) {
++y;
next = (y + 1) * (y + 1);
}
/* loop if divisible by 3,5,7,11,13,17,19,23,29 */
if ((r % 3) == 0) {
x = 0;
continue;
}
if ((r % 5) == 0) {
x = 0;
continue;
}
if ((r % 7) == 0) {
x = 0;
continue;
}
if ((r % 11) == 0) {
x = 0;
continue;
}
if ((r % 13) == 0) {
x = 0;
continue;
}
if ((r % 17) == 0) {
x = 0;
continue;
}
if ((r % 19) == 0) {
x = 0;
continue;
}
if ((r % 23) == 0) {
x = 0;
continue;
}
if ((r % 29) == 0) {
x = 0;
continue;
}
/* now check if r is divisible by x + k={1,7,11,13,17,19,23,29} */
for (x = 30; x <= y; x += 30) {
if ((r % (x + 1)) == 0) {
x = 0;
break;
}
if ((r % (x + 7)) == 0) {
x = 0;
break;
}
if ((r % (x + 11)) == 0) {
x = 0;
break;
}
if ((r % (x + 13)) == 0) {
x = 0;
break;
}
if ((r % (x + 17)) == 0) {
x = 0;
break;
}
if ((r % (x + 19)) == 0) {
x = 0;
break;
}
if ((r % (x + 23)) == 0) {
x = 0;
break;
}
if ((r % (x + 29)) == 0) {
x = 0;
break;
}
}
} while (x == 0);
if (r > 31) {
fwrite(&r, 1, sizeof(mp_digit), out);
printf("%9u\r", r);
fflush(stdout);
}
if (r < 31) break; if (r < 31) break;
/* update sqrt ? */
if (next <= r) {
++y;
next = (y + 1) * (y + 1);
}
/* loop if divisible by 3,5,7,11,13,17,19,23,29 */
if ((r % 3) == 0) {
x = 0;
continue;
}
if ((r % 5) == 0) {
x = 0;
continue;
}
if ((r % 7) == 0) {
x = 0;
continue;
}
if ((r % 11) == 0) {
x = 0;
continue;
}
if ((r % 13) == 0) {
x = 0;
continue;
}
if ((r % 17) == 0) {
x = 0;
continue;
}
if ((r % 19) == 0) {
x = 0;
continue;
}
if ((r % 23) == 0) {
x = 0;
continue;
}
if ((r % 29) == 0) {
x = 0;
continue;
}
/* now check if r is divisible by x + k={1,7,11,13,17,19,23,29} */
for (x = 30; x <= y; x += 30) {
if ((r % (x + 1)) == 0) {
x = 0;
break;
}
if ((r % (x + 7)) == 0) {
x = 0;
break;
}
if ((r % (x + 11)) == 0) {
x = 0;
break;
}
if ((r % (x + 13)) == 0) {
x = 0;
break;
}
if ((r % (x + 17)) == 0) {
x = 0;
break;
}
if ((r % (x + 19)) == 0) {
x = 0;
break;
}
if ((r % (x + 23)) == 0) {
x = 0;
break;
}
if ((r % (x + 29)) == 0) {
x = 0;
break;
}
}
} while (x == 0);
if (r > 31) {
fwrite(&r, 1, sizeof(mp_digit), out);
printf("%9u\r", r);
fflush(stdout);
} }
if (r < 31) break;
}
fclose(out); fclose(out);
}
} }
static void load_tab(void) static void load_tab(void)