From 782cda79bf1cc150b258f63c974799b909ae01ad Mon Sep 17 00:00:00 2001 From: czurnieden Date: Wed, 21 Jun 2023 22:15:21 +0200 Subject: [PATCH 1/2] Update of examples in directory "etc" --- .gitignore | 15 +- etc/2kprime.c | 23 +-- etc/drprime.c | 20 +-- etc/drprimes.txt | 9 -- etc/makefile | 2 + etc/mersenne.c | 4 +- etc/mont.c | 18 ++- etc/pprime.c | 363 ++++++++++------------------------------------- makefile | 4 + 9 files changed, 134 insertions(+), 324 deletions(-) delete mode 100644 etc/drprimes.txt diff --git a/.gitignore b/.gitignore index d2f01329f..51f3eee22 100644 --- a/.gitignore +++ b/.gitignore @@ -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 @@ -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 diff --git a/etc/2kprime.c b/etc/2kprime.c index 3a3e28307..5aa83abd0 100644 --- a/etc/2kprime.c +++ b/etc/2kprime.c @@ -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) { @@ -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; } @@ -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; } diff --git a/etc/drprime.c b/etc/drprime.c index 31dff4e99..e2e709b99 100644 --- a/etc/drprime.c +++ b/etc/drprime.c @@ -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; @@ -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; } @@ -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); @@ -60,6 +61,7 @@ int main(void) fclose(out); } +LTM_ERR: mp_clear(&a); mp_clear(&b); diff --git a/etc/drprimes.txt b/etc/drprimes.txt deleted file mode 100644 index 7c97f67b9..000000000 --- a/etc/drprimes.txt +++ /dev/null @@ -1,9 +0,0 @@ -300-bit prime: -p == 2037035976334486086268445688409378161051468393665936250636140449354381298610415201576637819 - -540-bit prime: -p == 3599131035634557106248430806148785487095757694641533306480604458089470064537190296255232548883112685719936728506816716098566612844395439751206810991770626477344739 - -780-bit prime: -p == 6359114106063703798370219984742410466332205126109989319225557147754704702203399726411277962562135973685197744935448875852478791860694279747355800678568677946181447581781401213133886609947027230004277244697462656003655947791725966271167 - diff --git a/etc/makefile b/etc/makefile index 52ad47533..0e178396f 100644 --- a/etc/makefile +++ b/etc/makefile @@ -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 diff --git a/etc/mersenne.c b/etc/mersenne.c index 4d3939e35..f7487ecd0 100644 --- a/etc/mersenne.c +++ b/etc/mersenne.c @@ -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"); } diff --git a/etc/mont.c b/etc/mont.c index 4652410d0..3d844496e 100644 --- a/etc/mont.c +++ b/etc/mont.c @@ -7,10 +7,11 @@ 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++) { @@ -18,18 +19,20 @@ int main(void) 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) { @@ -40,5 +43,6 @@ int main(void) printf("PASSED\n"); } +LTM_ERR: return 0; } diff --git a/etc/pprime.c b/etc/pprime.c index 1d59cab7f..7edbca09e 100644 --- a/etc/pprime.c +++ b/etc/pprime.c @@ -1,179 +1,31 @@ -/* Generates provable primes - * - * See http://gmail.com:8080/papers/pp.pdf for more info. +/* + * Generates provable primes * * Tom St Denis, tomstdenis@gmail.com, http://tom.gmail.com + * */ #include #include -#include "tommath_private.h" - -static int n_prime; -static FILE *primes; - -/* fast square root */ -static mp_digit i_sqrt(mp_word x) -{ - mp_word x1, x2; - - x2 = x; - do { - x1 = x2; - x2 = x1 - ((x1 * x1) - x) / (2u * x1); - } while (x1 != x2); - - if ((x1 * x1) > x) { - --x1; - } - - return x1; -} - - -/* generates a prime digit */ -static void gen_prime(void) -{ - mp_digit r, x, y, next; - FILE *out; - - out = fopen("pprime.dat", "wb"); - if (out != NULL) { - - /* write first set of primes */ - /* *INDENT-OFF* */ - r = 3uL; fwrite(&r, 1uL, sizeof(mp_digit), out); - r = 5uL; fwrite(&r, 1uL, sizeof(mp_digit), out); - r = 7uL; fwrite(&r, 1uL, sizeof(mp_digit), out); - r = 11uL; fwrite(&r, 1uL, sizeof(mp_digit), out); - r = 13uL; fwrite(&r, 1uL, sizeof(mp_digit), out); - r = 17uL; fwrite(&r, 1uL, sizeof(mp_digit), out); - r = 19uL; fwrite(&r, 1uL, sizeof(mp_digit), out); - r = 23uL; fwrite(&r, 1uL, sizeof(mp_digit), out); - r = 29uL; fwrite(&r, 1uL, sizeof(mp_digit), out); - r = 31uL; fwrite(&r, 1uL, sizeof(mp_digit), out); - /* *INDENT-ON* */ - - /* get square root, since if 'r' is composite its factors must be < than this */ - y = i_sqrt(r); - next = (y + 1uL) * (y + 1uL); - - for (;;) { - do { - r += 2uL; /* next candidate */ - r &= MP_MASK; - if (r < 31uL) break; - - /* update sqrt ? */ - if (next <= r) { - ++y; - next = (y + 1uL) * (y + 1uL); - } - - /* loop if divisible by 3,5,7,11,13,17,19,23,29 */ - if ((r % 3uL) == 0uL) { - x = 0uL; - continue; - } - if ((r % 5uL) == 0uL) { - x = 0uL; - continue; - } - if ((r % 7uL) == 0uL) { - x = 0uL; - continue; - } - if ((r % 11uL) == 0uL) { - x = 0uL; - continue; - } - if ((r % 13uL) == 0uL) { - x = 0uL; - continue; - } - if ((r % 17uL) == 0uL) { - x = 0uL; - continue; - } - if ((r % 19uL) == 0uL) { - x = 0uL; - continue; - } - if ((r % 23uL) == 0uL) { - x = 0uL; - continue; - } - if ((r % 29uL) == 0uL) { - x = 0uL; - continue; - } - - /* now check if r is divisible by x + k={1,7,11,13,17,19,23,29} */ - for (x = 30uL; x <= y; x += 30uL) { - if ((r % (x + 1uL)) == 0uL) { - x = 0uL; - break; - } - if ((r % (x + 7uL)) == 0uL) { - x = 0uL; - break; - } - if ((r % (x + 11uL)) == 0uL) { - x = 0uL; - break; - } - if ((r % (x + 13uL)) == 0uL) { - x = 0uL; - break; - } - if ((r % (x + 17uL)) == 0uL) { - x = 0uL; - break; - } - if ((r % (x + 19uL)) == 0uL) { - x = 0uL; - break; - } - if ((r % (x + 23uL)) == 0uL) { - x = 0uL; - break; - } - if ((r % (x + 29uL)) == 0uL) { - x = 0uL; - break; - } - } - } while (x == 0uL); - if (r > 31uL) { - fwrite(&r, 1uL, sizeof(mp_digit), out); - printf("%9lu\r", r); - fflush(stdout); - } - if (r < 31uL) break; - } - - fclose(out); - } -} - -static void load_tab(void) -{ - primes = fopen("pprime.dat", "rb"); - if (primes == NULL) { - gen_prime(); - primes = fopen("pprime.dat", "rb"); - } - fseek(primes, 0L, SEEK_END); - n_prime = ftell(primes) / sizeof(mp_digit); -} +#include "../tommath_private.h" static mp_digit prime_digit(void) { int n; - mp_digit d; + mp_digit d = 0; + mp_int a; + mp_err err; + + n = abs(rand()) % MP_MASK; + if ((err = mp_init_ul(&a, (unsigned long)n)) != MP_OKAY) goto LTM_ERR; + if ((err = mp_prime_next_prime(&a, -1, false)) != MP_OKAY) goto LTM_ERR; + while (a.used > 1) { + if ((err = mp_div_2(&a, &a)) != MP_OKAY) goto LTM_ERR; + if ((err = mp_prime_next_prime(&a, -1, false)) != MP_OKAY) goto LTM_ERR; + } + d = a.dp[0]; - n = abs(rand()) % n_prime; - fseek(primes, n * sizeof(mp_digit), SEEK_SET); - fread(&d, 1uL, sizeof(mp_digit), primes); +LTM_ERR: + mp_clear(&a); return d; } @@ -182,7 +34,7 @@ static mp_digit prime_digit(void) static mp_err pprime(int k, int li, mp_int *p, mp_int *q) { mp_int a, b, c, n, x, y, z, v; - mp_err res; + mp_err err = MP_OKAY; int ii; static const mp_digit bases[] = { 2, 3, 5, 7, 11, 13, 17, 19 }; @@ -192,49 +44,18 @@ static mp_err pprime(int k, int li, mp_int *p, mp_int *q) return MP_OKAY; } - if ((res = mp_init(&c)) != MP_OKAY) { - return res; - } - - if ((res = mp_init(&v)) != MP_OKAY) { - goto LBL_C; + if ((err = mp_init_multi(&a, &b, &c, &n, &x, &y, &z, &v, NULL)) != MP_OKAY) { + return err; } /* product of first 50 primes */ - if ((res = - mp_read_radix(&v, - "19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190", - 10)) != MP_OKAY) { - goto LBL_V; - } - - if ((res = mp_init(&a)) != MP_OKAY) { - goto LBL_V; + if ((err = mp_read_radix(&v, "9NPvy2By/eZ0N6s68ky5K/8UTD0Q7fInhDK9BHnueH92HfzU4+U", 64)) != MP_OKAY) { + goto LTM_ERR; } /* set the prime */ mp_set(&a, prime_digit()); - if ((res = mp_init(&b)) != MP_OKAY) { - goto LBL_A; - } - - if ((res = mp_init(&n)) != MP_OKAY) { - goto LBL_B; - } - - if ((res = mp_init(&x)) != MP_OKAY) { - goto LBL_N; - } - - if ((res = mp_init(&y)) != MP_OKAY) { - goto LBL_X; - } - - if ((res = mp_init(&z)) != MP_OKAY) { - goto LBL_Y; - } - /* now loop making the single digit */ while (mp_count_bits(&a) < k) { fprintf(stderr, "prime has %4d bits left\r", k - mp_count_bits(&a)); @@ -243,146 +64,113 @@ static mp_err pprime(int k, int li, mp_int *p, mp_int *q) mp_set(&b, prime_digit()); /* now compute z = a * b * 2 */ - if ((res = mp_mul(&a, &b, &z)) != MP_OKAY) { /* z = a * b */ - goto LBL_Z; - } - - if ((res = mp_copy(&z, &c)) != MP_OKAY) { /* c = a * b */ - goto LBL_Z; - } - - if ((res = mp_mul_2(&z, &z)) != MP_OKAY) { /* z = 2 * a * b */ - goto LBL_Z; - } - + /* z = a * b */ + if ((err = mp_mul(&a, &b, &z)) != MP_OKAY) goto LTM_ERR; + /* c = a * b */ + if ((err = mp_copy(&z, &c)) != MP_OKAY) goto LTM_ERR; + /* z = 2 * a * b */ + if ((err = mp_mul_2(&z, &z)) != MP_OKAY) goto LTM_ERR; /* n = z + 1 */ - if ((res = mp_add_d(&z, 1uL, &n)) != MP_OKAY) { /* n = z + 1 */ - goto LBL_Z; - } + if ((err = mp_add_d(&z, 1uL, &n)) != MP_OKAY) goto LTM_ERR; + /* check (n, v) == 1; y = (n, v) */ + if ((err = mp_gcd(&n, &v, &y)) != MP_OKAY) goto LTM_ERR; - /* check (n, v) == 1 */ - if ((res = mp_gcd(&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */ - goto LBL_Z; - } - - if (mp_cmp_d(&y, 1uL) != MP_EQ) + if (mp_cmp_d(&y, 1uL) != MP_EQ) { goto top; + } /* now try base x=bases[ii] */ for (ii = 0; ii < li; ii++) { mp_set(&x, bases[ii]); - /* compute x^a mod n */ - if ((res = mp_exptmod(&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */ - goto LBL_Z; - } - + /* compute x^a mod n; y = x^a mod n */ + if ((err = mp_exptmod(&x, &a, &n, &y)) != MP_OKAY) goto LTM_ERR; /* if y == 1 loop */ - if (mp_cmp_d(&y, 1uL) == MP_EQ) + if (mp_cmp_d(&y, 1uL) == MP_EQ) { continue; - - /* now x^2a mod n */ - if ((res = mp_sqrmod(&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */ - goto LBL_Z; } + /* now x^2a mod n ; y = x^2a mod n*/ + if ((err = mp_sqrmod(&y, &n, &y)) != MP_OKAY) goto LTM_ERR; - if (mp_cmp_d(&y, 1uL) == MP_EQ) + if (mp_cmp_d(&y, 1uL) == MP_EQ) { continue; - - /* compute x^b mod n */ - if ((res = mp_exptmod(&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */ - goto LBL_Z; } + /* compute x^b mod n ; y = x^b mod n*/ + if ((err = mp_exptmod(&x, &b, &n, &y)) != MP_OKAY) goto LTM_ERR; + /* if y == 1 loop */ - if (mp_cmp_d(&y, 1uL) == MP_EQ) + if (mp_cmp_d(&y, 1uL) == MP_EQ) { continue; - - /* now x^2b mod n */ - if ((res = mp_sqrmod(&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */ - goto LBL_Z; } - if (mp_cmp_d(&y, 1uL) == MP_EQ) - continue; + /* now x^2b mod n; y = x^2b mod n */ + if ((err = mp_sqrmod(&y, &n, &y)) != MP_OKAY) goto LTM_ERR; - /* compute x^c mod n == x^ab mod n */ - if ((res = mp_exptmod(&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */ - goto LBL_Z; + if (mp_cmp_d(&y, 1uL) == MP_EQ) { + continue; } + /* compute x^c mod n == x^ab mod n ; y = x^ab mod n */ + if ((err = mp_exptmod(&x, &c, &n, &y)) != MP_OKAY) goto LTM_ERR; /* if y == 1 loop */ - if (mp_cmp_d(&y, 1uL) == MP_EQ) + if (mp_cmp_d(&y, 1uL) == MP_EQ) { continue; - - /* now compute (x^c mod n)^2 */ - if ((res = mp_sqrmod(&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */ - goto LBL_Z; } + /* now compute (x^c mod n)^2 ; y = x^2ab mod n */ + if ((err = mp_sqrmod(&y, &n, &y)) != MP_OKAY) goto LTM_ERR; /* y should be 1 */ - if (mp_cmp_d(&y, 1uL) != MP_EQ) + if (mp_cmp_d(&y, 1uL) != MP_EQ) { continue; + } break; } /* no bases worked? */ - if (ii == li) + if (ii == li) { goto top; + } { char buf[4096]; - mp_to_decimal(&n, buf, sizeof(buf)); + if ((err = mp_to_decimal(&n, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR; printf("Certificate of primality for:\n%s\n\n", buf); - mp_to_decimal(&a, buf, sizeof(buf)); + if ((err = mp_to_decimal(&a, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR; printf("A == \n%s\n\n", buf); - mp_to_decimal(&b, buf, sizeof(buf)); + if ((err = mp_to_decimal(&b, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR; printf("B == \n%s\n\nG == %lu\n", buf, bases[ii]); printf("----------------------------------------------------------------\n"); } /* a = n */ - mp_copy(&n, &a); + if ((err = mp_copy(&n, &a)) != MP_OKAY) goto LTM_ERR; } /* get q to be the order of the large prime subgroup */ - mp_sub_d(&n, 1uL, q); - mp_div_2(q, q); - mp_div(q, &b, q, NULL); + if ((err = mp_sub_d(&n, 1uL, q)) != MP_OKAY) goto LTM_ERR; + if ((err = mp_div_2(q, q)) != MP_OKAY) goto LTM_ERR; + if ((err = mp_div(q, &b, q, NULL)) != MP_OKAY) goto LTM_ERR; mp_exch(&n, p); - res = MP_OKAY; -LBL_Z: - mp_clear(&z); -LBL_Y: - mp_clear(&y); -LBL_X: - mp_clear(&x); -LBL_N: - mp_clear(&n); -LBL_B: - mp_clear(&b); -LBL_A: - mp_clear(&a); -LBL_V: - mp_clear(&v); -LBL_C: - mp_clear(&c); - return res; + err = MP_OKAY; +LTM_ERR: + mp_clear_multi(&a, &b, &c, &n, &x, &y, &z, &v, NULL); + return err; } int main(void) { mp_int p, q; + mp_err err; char buf[4096]; int k, li; clock_t t1; srand(time(NULL)); - load_tab(); printf("Enter # of bits: \n"); fgets(buf, sizeof(buf), stdin); @@ -393,8 +181,7 @@ int main(void) sscanf(buf, "%d", &li); - mp_init(&p); - mp_init(&q); + if ((err = mp_init_multi(&p, &q, NULL)) != MP_OKAY) goto LTM_ERR; t1 = clock(); pprime(k, li, &p, &q); @@ -402,10 +189,14 @@ int main(void) printf("\n\nTook %lu ticks, %d bits\n", t1, mp_count_bits(&p)); - mp_to_decimal(&p, buf, sizeof(buf)); + if ((err = mp_to_decimal(&p, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR; printf("P == %s\n", buf); - mp_to_decimal(&q, buf, sizeof(buf)); + if ((err = mp_to_decimal(&q, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR; printf("Q == %s\n", buf); - return 0; + mp_clear_multi(&p, &q, NULL); + exit(EXIT_SUCCESS); +LTM_ERR: + mp_clear_multi(&p, &q, NULL); + exit(EXIT_FAILURE); } diff --git a/makefile b/makefile index f8feff7cc..27b44d7c6 100644 --- a/makefile +++ b/makefile @@ -107,6 +107,10 @@ tune: $(LIBNAME) $(MAKE) -C etc tune CFLAGS="$(LTM_CFLAGS) -I../" $(MAKE) +etc-all: $(LIBNAME) + $(MAKE) -C etc all CFLAGS="$(LTM_CFLAGS) -I../" + $(MAKE) + # You have to create a file .coveralls.yml with the content "repo_token: " # in the base folder to be able to submit to coveralls coveralls: lcov From 942b8b4317a84d9f472f0fdd3341f65e9ccaaf61 Mon Sep 17 00:00:00 2001 From: czurnieden Date: Thu, 22 Jun 2023 03:02:29 +0200 Subject: [PATCH 2/2] Use cryptographic RNG --- etc/pprime.c | 91 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 37 deletions(-) diff --git a/etc/pprime.c b/etc/pprime.c index 7edbca09e..7533a6099 100644 --- a/etc/pprime.c +++ b/etc/pprime.c @@ -8,20 +8,29 @@ #include #include "../tommath_private.h" -static mp_digit prime_digit(void) +static void mp_print(const char *s, const mp_int *a, int radix, FILE *stream) +{ + mp_err err; + fputs(s, stream); + err = mp_fwrite(a, radix, stream); + if (err != MP_OKAY) { + fprintf(stderr,"mp_fwrite in mp_print failed. error = %s\n", mp_error_to_string(err)); + exit(EXIT_FAILURE); + } + fputc('\n',stream); +} + +static mp_digit prime_digit(int bits) { - int n; mp_digit d = 0; mp_int a; mp_err err; - n = abs(rand()) % MP_MASK; - if ((err = mp_init_ul(&a, (unsigned long)n)) != MP_OKAY) goto LTM_ERR; - if ((err = mp_prime_next_prime(&a, -1, false)) != MP_OKAY) goto LTM_ERR; - while (a.used > 1) { - if ((err = mp_div_2(&a, &a)) != MP_OKAY) goto LTM_ERR; - if ((err = mp_prime_next_prime(&a, -1, false)) != MP_OKAY) goto LTM_ERR; + if ((err = mp_init(&a)) != MP_OKAY) { + return 0; } + + if ((err = mp_prime_rand(&a, 1, bits, false)) != MP_OKAY) goto LTM_ERR; d = a.dp[0]; LTM_ERR: @@ -35,12 +44,14 @@ static mp_err pprime(int k, int li, mp_int *p, mp_int *q) { mp_int a, b, c, n, x, y, z, v; mp_err err = MP_OKAY; - int ii; - static const mp_digit bases[] = { 2, 3, 5, 7, 11, 13, 17, 19 }; + int ii, bits; /* single digit ? */ - if (k <= (int) MP_DIGIT_BIT) { - mp_set(p, prime_digit()); + if (k < (int) MP_DIGIT_BIT) { + mp_set(p, prime_digit(k)); + if (mp_iszero(p)) { + return MP_VAL; + } return MP_OKAY; } @@ -54,14 +65,27 @@ static mp_err pprime(int k, int li, mp_int *p, mp_int *q) } /* set the prime */ - mp_set(&a, prime_digit()); + mp_set(&a, prime_digit(MP_DIGIT_BIT)); + if (mp_iszero(&a)) { + err = MP_VAL; + goto LTM_ERR; + } /* now loop making the single digit */ while (mp_count_bits(&a) < k) { - fprintf(stderr, "prime has %4d bits left\r", k - mp_count_bits(&a)); + bits = k - mp_count_bits(&a); + fprintf(stderr, "prime has %4d bits left\r", bits); fflush(stderr); top: - mp_set(&b, prime_digit()); + if (bits < MP_DIGIT_BIT) { + mp_set(&b, prime_digit(bits)); + } else { + mp_set(&b, prime_digit(MP_DIGIT_BIT)); + } + if (mp_iszero(&b)) { + err = MP_VAL; + goto LTM_ERR; + } /* now compute z = a * b * 2 */ /* z = a * b */ @@ -78,10 +102,10 @@ static mp_err pprime(int k, int li, mp_int *p, mp_int *q) if (mp_cmp_d(&y, 1uL) != MP_EQ) { goto top; } - + mp_set(&x, 2u); /* now try base x=bases[ii] */ for (ii = 0; ii < li; ii++) { - mp_set(&x, bases[ii]); + if ((err = mp_prime_next_prime(&x, -1, false)) != MP_OKAY) goto LTM_ERR; /* compute x^a mod n; y = x^a mod n */ if ((err = mp_exptmod(&x, &a, &n, &y)) != MP_OKAY) goto LTM_ERR; @@ -132,17 +156,11 @@ static mp_err pprime(int k, int li, mp_int *p, mp_int *q) goto top; } - { - char buf[4096]; - - if ((err = mp_to_decimal(&n, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR; - printf("Certificate of primality for:\n%s\n\n", buf); - if ((err = mp_to_decimal(&a, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR; - printf("A == \n%s\n\n", buf); - if ((err = mp_to_decimal(&b, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR; - printf("B == \n%s\n\nG == %lu\n", buf, bases[ii]); - printf("----------------------------------------------------------------\n"); - } + mp_print("Certificate of primality for:\n ", &n, 10, stdout); + mp_print("A == ", &a, 10, stdout); + mp_print("B == ", &b, 10, stdout); + mp_print("G == ", &x, 10, stdout); + printf("----------------------------------------------------------------\n"); /* a = n */ if ((err = mp_copy(&n, &a)) != MP_OKAY) goto LTM_ERR; @@ -170,29 +188,28 @@ int main(void) int k, li; clock_t t1; - srand(time(NULL)); - printf("Enter # of bits: \n"); fgets(buf, sizeof(buf), stdin); sscanf(buf, "%d", &k); - printf("Enter number of bases to try (1 to 8):\n"); + printf("Enter number of bases to try\n"); fgets(buf, sizeof(buf), stdin); sscanf(buf, "%d", &li); - if ((err = mp_init_multi(&p, &q, NULL)) != MP_OKAY) goto LTM_ERR; + if ((err = mp_init_multi(&p, &q, NULL)) != MP_OKAY) goto LTM_ERR; t1 = clock(); - pprime(k, li, &p, &q); + if ((err = pprime(k, li, &p, &q)) != MP_OKAY) { + fprintf(stderr, "Something went wrong in function pprime: %s\n", mp_error_to_string(err)); + goto LTM_ERR; + } t1 = clock() - t1; printf("\n\nTook %lu ticks, %d bits\n", t1, mp_count_bits(&p)); - if ((err = mp_to_decimal(&p, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR; - printf("P == %s\n", buf); - if ((err = mp_to_decimal(&q, buf, sizeof(buf))) != MP_OKAY) goto LTM_ERR; - printf("Q == %s\n", buf); + mp_print("P == ", &p, 10, stdout); + mp_print("Q == ", &q, 10, stdout); mp_clear_multi(&p, &q, NULL); exit(EXIT_SUCCESS);