diff options
| author | J08nY | 2017-04-05 16:27:02 +0200 |
|---|---|---|
| committer | J08nY | 2017-04-05 16:27:02 +0200 |
| commit | 9718963a643f0ae15120ceea2d26899892ab1ed4 (patch) | |
| tree | 0987ce122d298d60aa7d637b84583ce42abf6658 /src | |
| parent | 605049a142aff150d36711c66935c80875e84e36 (diff) | |
| download | ecgen-9718963a643f0ae15120ceea2d26899892ab1ed4.tar.gz ecgen-9718963a643f0ae15120ceea2d26899892ab1ed4.tar.zst ecgen-9718963a643f0ae15120ceea2d26899892ab1ed4.zip | |
Fix some leaks, add option to specify PARI stack size
Diffstat (limited to 'src')
| -rw-r--r-- | src/ecgen.c | 2 | ||||
| -rw-r--r-- | src/exhaustive/exhaustive.c | 16 | ||||
| -rw-r--r-- | src/exhaustive/seed.c | 2 | ||||
| -rw-r--r-- | src/invalid/invalid.c | 3 | ||||
| -rw-r--r-- | src/io/cli.c | 26 | ||||
| -rw-r--r-- | src/io/cli.h | 3 | ||||
| -rw-r--r-- | src/math/curve.c | 1 | ||||
| -rw-r--r-- | src/math/field.c | 2 | ||||
| -rw-r--r-- | src/math/random.c | 2 |
9 files changed, 43 insertions, 14 deletions
diff --git a/src/ecgen.c b/src/ecgen.c index bd33b04..7d457f7 100644 --- a/src/ecgen.c +++ b/src/ecgen.c @@ -44,7 +44,7 @@ static struct config_t cfg; bool init(void) { // Init PARI, 1GB stack, 1M primes - pari_init(1000000000, 1000000); + pari_init(cfg.memory, 1000000); // Init PARI PRNG if (!random_init()) return false; diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c index 03d37ca..969c2ff 100644 --- a/src/exhaustive/exhaustive.c +++ b/src/exhaustive/exhaustive.c @@ -115,6 +115,7 @@ int exhaustive_gen_retry(curve_t *curve, config_t *cfg, gen_t generators[], int state = start_offset; while (state < end_offset) { + // remember pari stack tops[state - start_offset] = avma; int diff = generators[state](curve, cfg, argss ? argss[state] : NULL); @@ -122,6 +123,15 @@ int exhaustive_gen_retry(curve_t *curve, config_t *cfg, gen_t generators[], fprintf(stderr, "Error generating a curve. state = %i\n", state); return 0; } + + if (state + diff < start_offset) { + // what now? + // TODO + } else if (diff <= 0) { + // rewind pari stack + avma = tops[state + diff - start_offset]; + } + if (diff == 0) { int tried = ++tries[state - start_offset]; if (retry && tried >= retry) { @@ -134,10 +144,6 @@ int exhaustive_gen_retry(curve_t *curve, config_t *cfg, gen_t generators[], } state += diff; - if (diff <= 0) { - avma = tops[state - start_offset]; - } - if (cfg->verbose) { if (diff > 0) { fprintf(debug, "+"); @@ -176,7 +182,7 @@ int exhaustive_do(config_t *cfg) { exhaustive_ginit(generators, cfg); exhaustive_ainit(argss, cfg); - for (long i = 0; i < cfg->count; ++i) { + for (unsigned long i = 0; i < cfg->count; ++i) { curve_t *curve = curve_new(); if (!exhaustive_gen_retry(curve, cfg, generators, argss, OFFSET_SEED, OFFSET_END, 10)) { diff --git a/src/exhaustive/seed.c b/src/exhaustive/seed.c index 0286fcf..85fab98 100644 --- a/src/exhaustive/seed.c +++ b/src/exhaustive/seed.c @@ -37,7 +37,7 @@ static GEN seed_stoi(const char *cstr) { GEN s = stoi(cstr[i]); s = shifti(s, (len - i - 1) * 8); seed = addii(seed, s); - if (gc_needed(btop, 1)) gerepileall(btop, 1, seed); + gerepileall(btop, 1, &seed); } return gerepilecopy(ltop, seed); diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c index 9c46df6..65c6c4b 100644 --- a/src/invalid/invalid.c +++ b/src/invalid/invalid.c @@ -185,10 +185,11 @@ static size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes, // primes from range divided order. Thus remove it // like it never existed. - points_free_deep(&invalid->points, invalid->npoints); avma = btop; } } + curve_free(&invalid); + return ncurves; } diff --git a/src/io/cli.c b/src/io/cli.c index d55f59c..84f5794 100644 --- a/src/io/cli.c +++ b/src/io/cli.c @@ -26,6 +26,7 @@ enum opt_keys { OPT_INPUT = 'f', OPT_APPEND = 'a', OPT_VERBOSE = 'v', + OPT_MEMORY = 'm', OPT_FP = 1, OPT_F2M, OPT_POINTS @@ -45,7 +46,7 @@ struct argp_option options[] = { {"order", OPT_ORDER, "ORDER", 0, "Generate a curve with given order (using Complex Multiplication).", 2}, {"koblitz", OPT_KOBLITZ, 0, 0, "Generate a Koblitz curve (a = 0).", 2}, {"unique", OPT_UNIQUE, 0, 0, "Generate a curve with only one generator.", 2}, - {"points", OPT_POINTS, "TYPE", 0, "Generate points of given type (random/prime/none).", 2}, + {"points", OPT_POINTS, "TYPE", 0, "Generate points of given type (random/prime/none).", 2}, {"count", OPT_COUNT, "COUNT", 0, "Generate multiple curves.", 2}, {0, 0, 0, 0, "Input/Output options:", 3}, {"format", OPT_FORMAT, "FORMAT", 0, "Format to output in. One of [csv,json], default is json.", 3}, @@ -54,7 +55,8 @@ struct argp_option options[] = { {"append", OPT_APPEND, 0, 0, "Append to output file (don't overwrite).", 3}, {"verbose", OPT_VERBOSE, "FILE", OPTION_ARG_OPTIONAL, "Verbose logging (to stdout or file).", 3}, {0, 0, 0, 0, "Other:", 4}, - {"data-dir", OPT_DATADIR, "DIR", 0, "PARI/GP data directory (containing seadata package).", 4}, + {"data-dir", OPT_DATADIR, "DIR", 0, "Set PARI/GP data directory (containing seadata package).", 4}, + {"memory", OPT_MEMORY, "SIZE", 0, "Use PARI stack of SIZE (can have suffix k/m/g).", 4}, {0} }; // clang-format on @@ -66,9 +68,24 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) { case OPT_DATADIR: cfg->datadir = arg; break; + case OPT_MEMORY: + if (arg) { + char *suffix = NULL; + unsigned long read = strtoul(arg, &suffix, 10); + if (suffix) { + if (*suffix == 'k' || *suffix == 'K') { + read *= 1000; + } else if (*suffix == 'm' || *suffix == 'M') { + read *= 1000000; + } else if (*suffix == 'g' || *suffix == 'G') { + read *= 1000000000; + } + } + cfg->memory = read; + } case OPT_COUNT: if (arg) { - cfg->count = strtol(arg, NULL, 10); + cfg->count = strtoul(arg, NULL, 10); } break; case OPT_FORMAT: @@ -203,6 +220,9 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) { if (!cfg->count) { cfg->count = 1; } + if (!cfg->memory) { + cfg->memory = 1000000000; + } break; case ARGP_KEY_NO_ARGS: argp_usage(state); diff --git a/src/io/cli.h b/src/io/cli.h index e45e939..a62f95b 100644 --- a/src/io/cli.h +++ b/src/io/cli.h @@ -29,7 +29,7 @@ typedef struct config_t { bool binary_field; bool prime_field; - long count; + unsigned long count; bool random; bool prime; bool invalid; @@ -44,6 +44,7 @@ typedef struct config_t { struct points_s points; char *datadir; + unsigned long memory; enum format_e format; char *output; char *input; diff --git a/src/math/curve.c b/src/math/curve.c index 6b0742c..1703f78 100644 --- a/src/math/curve.c +++ b/src/math/curve.c @@ -41,6 +41,7 @@ curve_t *curve_copy(curve_t *src, curve_t *dest) { void curve_free(curve_t **curve) { if (*curve) { seed_free(&(*curve)->seed); + points_free_deep(&(*curve)->generators, (*curve)->ngens); points_free_deep(&(*curve)->points, (*curve)->npoints); pari_free(*curve); *curve = NULL; diff --git a/src/math/field.c b/src/math/field.c index 3774040..cc3656f 100644 --- a/src/math/field.c +++ b/src/math/field.c @@ -128,7 +128,7 @@ GEN field_params(GEN field) { gel(out, j) = stoi(i); j++; } - if (gc_needed(btop, 1)) gerepileall(btop, 2, &out, &c); + gerepileall(btop, 2, &out, &c); } } return gerepilecopy(ltop, out); diff --git a/src/math/random.c b/src/math/random.c index a1d88ab..20098e1 100644 --- a/src/math/random.c +++ b/src/math/random.c @@ -47,7 +47,7 @@ GEN random_prime(long bits) { pari_sp btop = avma; do { p = randomprime(range); - if (gc_needed(btop, 1)) p = gerepilecopy(btop, p); + p = gerepileupto(btop, p); } while (!isprime(p)); } |
