diff --git a/lib/cm-impl.h b/lib/cm-impl.h index 934fbbb4fc4f5d110a41ce3ef13628d4c16fa98b..9dee7c31d8dd286001064ab3fe91116fc9b53036 100644 --- a/lib/cm-impl.h +++ b/lib/cm-impl.h @@ -261,6 +261,8 @@ extern bool cm_file_read_primorial (const char *tmpdir, mpz_ptr prim, const int i); extern bool cm_file_write_factor (const char *tmpdir, mpzx_srcptr factor, mpzx_srcptr F, mpz_srcptr p); +extern bool cm_file_read_factor (const char *tmpdir, mpzx_ptr factor, + mpzx_srcptr F, mpz_srcptr p); extern bool cm_write_ecpp_cert1_line (FILE *f, mpz_t *line, cm_stat_t stat); extern bool cm_write_ecpp_cert2_line (FILE *f, mpz_t *line, int no, cm_stat_t stat); diff --git a/lib/file.c b/lib/file.c index 872accc48b49c0ad28fe22f87e0f6ebfb6ca9bbc..13ae283f3f2fb9f040196c2441abec0dbe58c14f 100644 --- a/lib/file.c +++ b/lib/file.c @@ -444,6 +444,58 @@ bool cm_file_write_factor (const char *tmpdir, mpzx_srcptr factor, /*****************************************************************************/ +bool cm_file_read_factor (const char *tmpdir, mpzx_ptr factor, + mpzx_srcptr F, mpz_srcptr p) + /* If a corresponding file can be opened, try to read a factor of the + monic polynomial F modulo p and return it in factor, or leave factor + unchanged. The return value reflects the success of the operation. */ +{ + char *filename; + uint64_t hash; + FILE *f; + unsigned int len; + bool res; + mpz_t ploc; + mpzx_t Floc; + + len = strlen (tmpdir) + 29; + filename = (char *) malloc (len * sizeof (char)); + hash = mpzx_mod_hash (F, p); + snprintf (filename, len, "%s/factor_%016"PRIx64".dat", tmpdir, hash); + + f = fopen (filename, "r"); + res = (f != NULL); + + if (res) { + /* Read p and F and check whether they are the same; otherwise + there has been a hash collision. */ + mpz_init (ploc); + mpzx_init (Floc, -1); + res &= (mpz_inp_str (ploc, f, 10) != 0); + res &= mpzx_inp_str (Floc, f, 10); + if (mpz_cmp (p, ploc) != 0 || mpzx_cmp (F, Floc) != 0) { + printf ("***** Warning: Hash collision in reading a factor\n"); + printf ("p "); + mpz_out_str (stdout, 10, ploc); + printf ("\nF "); + mpzx_out_str (stdout, 10, Floc); + printf ("\n"); + res = false; + } + else + res &= mpzx_inp_str (Floc, f, 10); + res &= (fclose (f) == 0); + if (res) + mpzx_set (factor, Floc); + mpz_clear (ploc); + mpzx_clear (Floc); + } + + return res; +} + +/*****************************************************************************/ + static bool write_stat (FILE *f, cm_stat_t stat) /* Write the content of stat to the file f. */ {