Skip to content

Commit

Permalink
Merge pull request #561 from czurnieden/pprime
Browse files Browse the repository at this point in the history
Update of examples in directory "etc"
  • Loading branch information
sjaeckel committed Mar 11, 2024
2 parents fc17cf1 + 942b8b4 commit 027ae66
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 340 deletions.
15 changes: 14 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ mtest.exe
mtest_opponent
mtest_opponent.exe

2kprime
2kprime.exe
drprime
drprime.exe
mersenne
mersenne.exe
mont
mont.exe
pprime
pprime.exe

# ignore eclipse project files
.cproject
.project
Expand Down Expand Up @@ -66,9 +77,11 @@ perf.data.old
# ignore tommath_amalgam.c generated by make
tommath_amalgam.c

# ignore file generated by make tune
# ignore file generated by make 'tune and friends'
tuning_list
etc/tune
2kprime.1
drprimes.txt

# ignore stuff generated by "make manual" and "make poster"
*.aux
Expand Down
23 changes: 12 additions & 11 deletions etc/2kprime.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ int main(void)
size_t x;
bool y;
mp_int q, p;
mp_err err;
FILE *out;
clock_t t1;
mp_digit z;

mp_init_multi(&q, &p, NULL);
if ((err = mp_init_multi(&q, &p, NULL)) != MP_OKAY) goto LTM_ERR;

out = fopen("2kprime.1", "w");
if (out != NULL) {
for (x = 0; x < (sizeof(sizes) / sizeof(sizes[0])); x++) {
top:
mp_2expt(&q, sizes[x]);
mp_add_d(&q, 3uL, &q);
if ((err = mp_2expt(&q, sizes[x])) != MP_OKAY) goto LTM_ERR;
if ((err = mp_add_d(&q, 3uL, &q)) != MP_OKAY) goto LTM_ERR;
z = -3;

t1 = clock();
for (;;) {
mp_sub_d(&q, 4uL, &q);
if ((err = mp_sub_d(&q, 4uL, &q)) != MP_OKAY) goto LTM_ERR;
z += 4uL;

if (z > MP_MASK) {
Expand All @@ -42,21 +43,21 @@ int main(void)
}

/* quick test on q */
mp_prime_is_prime(&q, 1, &y);
if ((err = mp_prime_is_prime(&q, 1, &y)) != MP_OKAY) goto LTM_ERR;
if (!y) {
continue;
}

/* find (q-1)/2 */
mp_sub_d(&q, 1uL, &p);
mp_div_2(&p, &p);
mp_prime_is_prime(&p, 3, &y);
if ((err = mp_sub_d(&q, 1uL, &p)) != MP_OKAY) goto LTM_ERR;
if ((err = mp_div_2(&p, &p)) != MP_OKAY) goto LTM_ERR;
if ((err = mp_prime_is_prime(&p, 3, &y)) != MP_OKAY) goto LTM_ERR;
if (!y) {
continue;
}

/* test on q */
mp_prime_is_prime(&q, 3, &y);
if ((err = mp_prime_is_prime(&q, 3, &y)) != MP_OKAY) goto LTM_ERR;
if (!y) {
continue;
}
Expand All @@ -69,13 +70,13 @@ int main(void)
goto top;
}

mp_to_decimal(&q, buf, sizeof(buf));
if ((err = mp_to_decimal(&q, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR;
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);
}

LTM_ERR:
return 0;
}
20 changes: 11 additions & 9 deletions etc/drprime.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ int main(void)
char buf[4096];
FILE *out;
mp_int a, b;
mp_err err;

mp_init(&a);
mp_init(&b);
if ((err = mp_init(&a)) != MP_OKAY) goto LTM_ERR;
if ((err = mp_init(&b)) != MP_OKAY) goto LTM_ERR;

out = fopen("drprimes.txt", "w");
if (out != NULL) {
for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
top:
printf("Seeking a %d-bit safe prime\n", sizes[x] * MP_DIGIT_BIT);
mp_grow(&a, sizes[x]);
if ((err = mp_grow(&a, sizes[x])) != MP_OKAY) goto LTM_ERR;
mp_zero(&a);
for (y = 1; y < sizes[x]; y++) {
a.dp[y] = MP_MASK;
Expand All @@ -34,15 +35,15 @@ int main(void)
for (;;) {
a.dp[0] += 4uL;
if (a.dp[0] >= MP_MASK) break;
mp_prime_is_prime(&a, 1, &res);
if ((err = mp_prime_is_prime(&a, 1, &res)) != MP_OKAY) goto LTM_ERR;
if (!res) continue;
printf(".");
fflush(stdout);
mp_sub_d(&a, 1uL, &b);
mp_div_2(&b, &b);
mp_prime_is_prime(&b, 3, &res);
if ((err = mp_sub_d(&a, 1uL, &b)) != MP_OKAY) goto LTM_ERR;
if ((err = mp_div_2(&b, &b)) != MP_OKAY) goto LTM_ERR;
if ((err = mp_prime_is_prime(&b, 3, &res)) != MP_OKAY) goto LTM_ERR;
if (!res) continue;
mp_prime_is_prime(&a, 3, &res);
if ((err = mp_prime_is_prime(&a, 3, &res)) != MP_OKAY) goto LTM_ERR;
if (res) break;
}

Expand All @@ -51,7 +52,7 @@ int main(void)
sizes[x] += 1;
goto top;
} else {
mp_to_decimal(&a, buf, sizeof(buf));
if ((err = mp_to_decimal(&a, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR;
printf("\n\np == %s\n\n", buf);
fprintf(out, "%d-bit prime:\np == %s\n\n", mp_count_bits(&a), buf);
fflush(out);
Expand All @@ -60,6 +61,7 @@ int main(void)
fclose(out);
}

LTM_ERR:
mp_clear(&a);
mp_clear(&b);

Expand Down
9 changes: 0 additions & 9 deletions etc/drprimes.txt

This file was deleted.

2 changes: 2 additions & 0 deletions etc/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ LTM_TUNE_CFLAGS = $(CFLAGS) $(LTM_CFLAGS) -Wall -W -Wextra -Wshadow -O3 -I../
# libname when you can't install the lib with install
LIBNAME=../libtommath.a

all: pprime tune test_standalone mersenne drprime 2kprime mont

#provable primes
pprime: pprime.o
$(CC) $(LTM_TUNE_CFLAGS) pprime.o $(LIBNAME) -o pprime
Expand Down
4 changes: 3 additions & 1 deletion etc/mersenne.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ static mp_err is_mersenne(long s, bool *pp)

/* if u == 0 then its prime */
if (mp_iszero(&u)) {
mp_prime_is_prime(&n, 8, pp);
if ((res = mp_prime_is_prime(&n, 8, pp)) != MP_OKAY) {
goto LBL_MU;
}
if (!*pp) printf("FAILURE\n");
}

Expand Down
18 changes: 11 additions & 7 deletions etc/mont.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,32 @@ int main(void)
{
mp_int modulus, R, p, pp;
mp_digit mp;
mp_err err;
int x, y;

srand(time(NULL));
mp_init_multi(&modulus, &R, &p, &pp, NULL);
if ((err = mp_init_multi(&modulus, &R, &p, &pp, NULL)) != MP_OKAY) goto LTM_ERR;

/* loop through various sizes */
for (x = 4; x < 256; x++) {
printf("DIGITS == %3d...", x);
fflush(stdout);

/* make up the odd modulus */
mp_rand(&modulus, x);
if ((err = mp_rand(&modulus, x)) != MP_OKAY) goto LTM_ERR;
modulus.dp[0] |= 1uL;

/* now find the R value */
mp_montgomery_calc_normalization(&R, &modulus);
mp_montgomery_setup(&modulus, &mp);
if ((err = mp_montgomery_calc_normalization(&R, &modulus)) != MP_OKAY) goto LTM_ERR;
if ((err = mp_montgomery_setup(&modulus, &mp)) != MP_OKAY) goto LTM_ERR;

/* now run through a bunch tests */
for (y = 0; y < 1000; y++) {
mp_rand(&p, x/2); /* p = random */
mp_mul(&p, &R, &pp); /* pp = R * p */
mp_montgomery_reduce(&pp, &modulus, mp);
/* p = random */
if ((err = mp_rand(&p, x/2)) != MP_OKAY) goto LTM_ERR;
/* pp = R * p */
if ((err = mp_mul(&p, &R, &pp)) != MP_OKAY) goto LTM_ERR;
if ((err = mp_montgomery_reduce(&pp, &modulus, mp)) != MP_OKAY) goto LTM_ERR;

/* should be equal to p */
if (mp_cmp(&pp, &p) != MP_EQ) {
Expand All @@ -40,5 +43,6 @@ int main(void)
printf("PASSED\n");
}

LTM_ERR:
return 0;
}
Loading

0 comments on commit 027ae66

Please sign in to comment.