Skip to content

Commit

Permalink
refactor: Decouple table generation and ecmult_gen context
Browse files Browse the repository at this point in the history
  • Loading branch information
real-or-random committed Nov 19, 2021
1 parent 22dc2c0 commit e43ba02
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/ecmult_gen_prec.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

#include "ecmult_gen.h"

static const size_t ECMULT_GEN_PREC_TABLE_SIZE = ROUND_TO_ALIGN(sizeof(*((secp256k1_ecmult_gen_context*) NULL)->prec));
static const size_t ECMULT_GEN_PREC_TABLE_SIZE = ECMULT_GEN_PREC_N * ECMULT_GEN_PREC_G * sizeof(secp256k1_ge_storage);

static void secp256k1_ecmult_gen_create_prec_table(secp256k1_ecmult_gen_context *ctx, void **prealloc);
static void secp256k1_ecmult_gen_create_prec_table(secp256k1_ge_storage* table);

#endif /* SECP256K1_ECMULT_GEN_PREC_H */
7 changes: 2 additions & 5 deletions src/ecmult_gen_prec_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
#include "field_impl.h"
#include "ecmult_gen.h"

static void secp256k1_ecmult_gen_create_prec_table(secp256k1_ecmult_gen_context *ctx, void **prealloc) {
static void secp256k1_ecmult_gen_create_prec_table(secp256k1_ge_storage* table) {
secp256k1_ge prec[ECMULT_GEN_PREC_N * ECMULT_GEN_PREC_G];
secp256k1_gej gj;
secp256k1_gej nums_gej;
int i, j;
size_t const prealloc_size = ECMULT_GEN_PREC_TABLE_SIZE;
void* const base = *prealloc;
ctx->prec = (secp256k1_ge_storage (*)[ECMULT_GEN_PREC_N][ECMULT_GEN_PREC_G])manual_alloc(prealloc, prealloc_size, base, prealloc_size);

/* get the generator */
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
Expand Down Expand Up @@ -70,7 +67,7 @@ static void secp256k1_ecmult_gen_create_prec_table(secp256k1_ecmult_gen_context
}
for (j = 0; j < ECMULT_GEN_PREC_N; j++) {
for (i = 0; i < ECMULT_GEN_PREC_G; i++) {
secp256k1_ge_to_storage(&(*ctx->prec)[j][i], &prec[j*ECMULT_GEN_PREC_G + i]);
secp256k1_ge_to_storage(&table[j*ECMULT_GEN_PREC_G + i], &prec[j*ECMULT_GEN_PREC_G + i]);
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/gen_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ static const secp256k1_callback default_error_callback = {
};

int main(int argc, char **argv) {
secp256k1_ecmult_gen_context ctx;
void *prealloc, *base;
secp256k1_ge_storage* table;
int inner;
int outer;
FILE* fp;
Expand All @@ -59,13 +58,12 @@ int main(int argc, char **argv) {
fprintf(fp, "#endif\n");
fprintf(fp, "static const secp256k1_ge_storage secp256k1_ecmult_static_context[ECMULT_GEN_PREC_N][ECMULT_GEN_PREC_G] = {\n");

base = checked_malloc(&default_error_callback, ECMULT_GEN_PREC_TABLE_SIZE);
prealloc = base;
secp256k1_ecmult_gen_create_prec_table(&ctx, &prealloc);
table = checked_malloc(&default_error_callback, ECMULT_GEN_PREC_TABLE_SIZE);
secp256k1_ecmult_gen_create_prec_table(table);
for(outer = 0; outer != ECMULT_GEN_PREC_N; outer++) {
fprintf(fp,"{\n");
for(inner = 0; inner != ECMULT_GEN_PREC_G; inner++) {
fprintf(fp," SC(%uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu)", SECP256K1_GE_STORAGE_CONST_GET((*ctx.prec)[outer][inner]));
fprintf(fp," SC(%uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu)", SECP256K1_GE_STORAGE_CONST_GET(table[outer * ECMULT_GEN_PREC_G + inner]));
if (inner != ECMULT_GEN_PREC_G - 1) {
fprintf(fp,",\n");
} else {
Expand All @@ -79,7 +77,7 @@ int main(int argc, char **argv) {
}
}
fprintf(fp,"};\n");
free(base);
free(table);

fprintf(fp, "#undef SC\n");
fprintf(fp, "#endif\n");
Expand Down

0 comments on commit e43ba02

Please sign in to comment.