diff options
| author | J08nY | 2017-04-05 00:18:01 +0200 |
|---|---|---|
| committer | J08nY | 2017-04-05 00:23:33 +0200 |
| commit | f87dd8a285755f9b1d838b3efdfd952fae81ee55 (patch) | |
| tree | 07916d786c9c68e8020e4ee969b328d19eebbc0f /src | |
| parent | b77fd8c4eb3f1dba399d8451909fefc52b436c35 (diff) | |
| download | ecgen-f87dd8a285755f9b1d838b3efdfd952fae81ee55.tar.gz ecgen-f87dd8a285755f9b1d838b3efdfd952fae81ee55.tar.zst ecgen-f87dd8a285755f9b1d838b3efdfd952fae81ee55.zip | |
Add exhaustive_gen_retry, fix infinite loop, add retry limit to exhaustive gen
Diffstat (limited to 'src')
| -rw-r--r-- | src/exhaustive/exhaustive.c | 34 | ||||
| -rw-r--r-- | src/exhaustive/exhaustive.h | 15 | ||||
| -rw-r--r-- | src/invalid/invalid.c | 3 | ||||
| -rw-r--r-- | src/io/input.c | 5 | ||||
| -rw-r--r-- | src/math/order.c | 2 |
5 files changed, 49 insertions, 10 deletions
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c index 3b16349..42c174f 100644 --- a/src/exhaustive/exhaustive.c +++ b/src/exhaustive/exhaustive.c @@ -99,15 +99,19 @@ void exhaustive_ainit(arg_t **argss, config_t *cfg) { } } -int exhaustive_gen(curve_t *curve, config_t *cfg, gen_t generators[], - arg_t *argss[], int start_offset, int end_offset) { +int exhaustive_gen_retry(curve_t *curve, config_t *cfg, gen_t generators[], + arg_t *argss[], int start_offset, int end_offset, + int retry) { if (start_offset == end_offset) { return 1; } if (start_offset > end_offset) { - return 0; + return -1; } - pari_sp tops[end_offset - start_offset]; + int num_gens = end_offset - start_offset; + pari_sp tops[num_gens]; + int tries[num_gens]; + memset(tries, 0, sizeof(int) * num_gens); int state = start_offset; while (state < end_offset) { @@ -115,9 +119,19 @@ int exhaustive_gen(curve_t *curve, config_t *cfg, gen_t generators[], int diff = generators[state](curve, cfg, argss ? argss[state] : NULL); if (diff == INT_MIN) { - fprintf(stderr, "Error generating a curve. %i\n", state); + fprintf(stderr, "Error generating a curve. state = %i\n", state); return 0; } + if (diff == 0) { + int tried = ++tries[state - start_offset]; + if (retry && tried >= retry) { + fprintf(stderr, "Reached retry limit: %i, state = %i\n", retry, + state); + return 0; + } + } else if (diff > 0) { + tries[state - start_offset] = 0; + } state += diff; if (diff <= 0) { @@ -141,6 +155,12 @@ int exhaustive_gen(curve_t *curve, config_t *cfg, gen_t generators[], return 1; } +int exhaustive_gen(curve_t *curve, config_t *cfg, gen_t generators[], + arg_t *argss[], int start_offset, int end_offset) { + return exhaustive_gen_retry(curve, cfg, generators, argss, start_offset, + end_offset, 0); +} + void exhaustive_quit(arg_t *argss[]) { equation_quit(); for (size_t i = 0; i < OFFSET_END; ++i) { @@ -158,8 +178,8 @@ int exhaustive_do(config_t *cfg) { for (long i = 0; i < cfg->count; ++i) { curve_t *curve = curve_new(); - if (!exhaustive_gen(curve, cfg, generators, argss, OFFSET_SEED, - OFFSET_END)) { + if (!exhaustive_gen_retry(curve, cfg, generators, argss, OFFSET_SEED, + OFFSET_END, 10)) { curve_free(&curve); return 1; } diff --git a/src/exhaustive/exhaustive.h b/src/exhaustive/exhaustive.h index ee81874..76e8000 100644 --- a/src/exhaustive/exhaustive.h +++ b/src/exhaustive/exhaustive.h @@ -13,6 +13,21 @@ /** * * @param curve + * @param cfg + * @param generators + * @param argss + * @param start_offset + * @param end_offset + * @param retry + * @return + */ +int exhaustive_gen_retry(curve_t *curve, config_t *cfg, gen_t generators[], + arg_t *argss[], int start_offset, int end_offset, + int retry); + +/** + * + * @param curve * @param config * @param generators * @param argss diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index 98052fd..c882b79 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -201,7 +201,8 @@ int invalid_do(config_t *cfg) { invalid_ginit(gen, cfg); // actually generate the curve - if (!exhaustive_gen(curve, cfg, gen, argss, OFFSET_FIELD, OFFSET_POINTS)) { + if (!exhaustive_gen_retry(curve, cfg, gen, argss, OFFSET_FIELD, + OFFSET_POINTS, 1)) { curve_free(&curve); return 1; } diff --git a/src/io/input.c b/src/io/input.c index 4697d05..348302d 100644 --- a/src/io/input.c +++ b/src/io/input.c @@ -18,6 +18,9 @@ GEN input_i(const char *prompt, long bits) { size_t n = 0; ssize_t len = getdelim(&line, &n, delim, in); + if (len <= 0) { + return gen_m1; + } if (len == 1) { free(line); return gen_m1; @@ -26,7 +29,7 @@ GEN input_i(const char *prompt, long bits) { ; pari_sp ltop = avma; - if (len <= 3 || line[0] != '0' && (line[1] != 'x' || line[1] != 'X')) { + if (len <= 3 || (line[0] != '0' && (line[1] != 'x' || line[1] != 'X'))) { char *new_line = realloc(line, (size_t)(len + 2)); if (!new_line) { perror("Couldn't alloc."); diff --git a/src/math/order.c b/src/math/order.c index e7abf56..1bcbb3e 100644 --- a/src/math/order.c +++ b/src/math/order.c @@ -17,7 +17,7 @@ int order_smallfact(curve_t *curve, config_t *cfg, arg_t *args) { pari_ulong smallfact = *(pari_ulong *)args->args; pari_sp ltop = avma; GEN fact = mpfact(smallfact); - if (lgefint(fact) > 3) { + if (lgefint(fact) > 3) { smallfact = 0; } else { smallfact = itou(fact); |
