2021-06-28 16:33:03 -04:00
|
|
|
/*****************************************************************************************************
|
|
|
|
* Copyright (c) 2013, 2014, 2017, 2021 Pieter Wuille, Andrew Poelstra, Jonas Nick, Russell O'Connor *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php. *
|
|
|
|
*****************************************************************************************************/
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/* Autotools creates libsecp256k1-config.h, of which ECMULT_WINDOW_SIZE is needed.
|
|
|
|
ifndef guard so downstream users can define their own if they do not use autotools. */
|
|
|
|
#if !defined(ECMULT_WINDOW_SIZE)
|
|
|
|
#include "libsecp256k1-config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../include/secp256k1.h"
|
|
|
|
#include "assumptions.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "field_impl.h"
|
|
|
|
#include "group_impl.h"
|
|
|
|
#include "ecmult.h"
|
2021-12-17 12:02:40 -05:00
|
|
|
#include "ecmult_compute_table_impl.h"
|
2021-12-17 11:52:20 -05:00
|
|
|
|
|
|
|
static void print_table(FILE *fp, const char *name, int window_g, const secp256k1_ge_storage* table, int with_conditionals) {
|
|
|
|
int j;
|
|
|
|
int i;
|
2021-06-28 16:33:03 -04:00
|
|
|
|
|
|
|
fprintf(fp, "static const secp256k1_ge_storage %s[ECMULT_TABLE_SIZE(WINDOW_G)] = {\n", name);
|
|
|
|
fprintf(fp, " S(%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32
|
|
|
|
",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32")\n",
|
2021-12-17 11:52:20 -05:00
|
|
|
SECP256K1_GE_STORAGE_CONST_GET(table[0]));
|
2021-06-28 16:33:03 -04:00
|
|
|
|
|
|
|
j = 1;
|
|
|
|
for(i = 3; i <= window_g; ++i) {
|
|
|
|
if (with_conditionals) {
|
|
|
|
fprintf(fp, "#if ECMULT_TABLE_SIZE(WINDOW_G) > %ld\n", ECMULT_TABLE_SIZE(i-1));
|
|
|
|
}
|
|
|
|
for(;j < ECMULT_TABLE_SIZE(i); ++j) {
|
|
|
|
fprintf(fp, ",S(%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32
|
|
|
|
",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32")\n",
|
2021-12-17 11:52:20 -05:00
|
|
|
SECP256K1_GE_STORAGE_CONST_GET(table[j]));
|
2021-06-28 16:33:03 -04:00
|
|
|
}
|
|
|
|
if (with_conditionals) {
|
|
|
|
fprintf(fp, "#endif\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(fp, "};\n");
|
|
|
|
}
|
|
|
|
|
2021-12-17 11:52:20 -05:00
|
|
|
static void print_two_tables(FILE *fp, int window_g, const secp256k1_ge *g, int with_conditionals) {
|
|
|
|
secp256k1_ge_storage* table = malloc(ECMULT_TABLE_SIZE(window_g) * sizeof(secp256k1_ge_storage));
|
|
|
|
secp256k1_ge_storage* table_128 = malloc(ECMULT_TABLE_SIZE(window_g) * sizeof(secp256k1_ge_storage));
|
2021-06-28 16:33:03 -04:00
|
|
|
|
2021-12-17 11:52:20 -05:00
|
|
|
secp256k1_ecmult_compute_two_tables(table, table_128, window_g, g);
|
|
|
|
|
|
|
|
print_table(fp, "secp256k1_pre_g", window_g, table, with_conditionals);
|
|
|
|
print_table(fp, "secp256k1_pre_g_128", window_g, table_128, with_conditionals);
|
|
|
|
|
|
|
|
free(table);
|
|
|
|
free(table_128);
|
2021-06-28 16:33:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
const secp256k1_ge g = SECP256K1_G;
|
|
|
|
const int window_g_13 = 4;
|
|
|
|
const int window_g_199 = 8;
|
|
|
|
FILE* fp;
|
|
|
|
|
2021-12-17 11:28:48 -05:00
|
|
|
fp = fopen("src/precomputed_ecmult.h","w");
|
2021-06-28 16:33:03 -04:00
|
|
|
if (fp == NULL) {
|
2021-12-17 11:28:48 -05:00
|
|
|
fprintf(stderr, "Could not open src/precomputed_ecmult.h for writing!\n");
|
2021-06-28 16:33:03 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-12-17 11:19:45 -05:00
|
|
|
fprintf(fp, "/* This file was automatically generated by precompute_ecmult. */\n");
|
2021-06-28 16:33:03 -04:00
|
|
|
fprintf(fp, "/* This file contains an array secp256k1_pre_g with odd multiples of the base point G and\n");
|
|
|
|
fprintf(fp, " * an array secp256k1_pre_g_128 with odd multiples of 2^128*G for accelerating the computation of a*P + b*G.\n");
|
|
|
|
fprintf(fp, " */\n");
|
2021-12-17 11:28:48 -05:00
|
|
|
fprintf(fp, "#ifndef SECP256K1_PRECOMPUTED_ECMULT_H\n");
|
|
|
|
fprintf(fp, "#define SECP256K1_PRECOMPUTED_ECMULT_H\n");
|
2021-06-28 16:33:03 -04:00
|
|
|
fprintf(fp, "#include \"group.h\"\n");
|
|
|
|
fprintf(fp, "#ifdef S\n");
|
|
|
|
fprintf(fp, " #error macro identifier S already in use.\n");
|
|
|
|
fprintf(fp, "#endif\n");
|
|
|
|
fprintf(fp, "#define S(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) "
|
|
|
|
"SECP256K1_GE_STORAGE_CONST(0x##a##u,0x##b##u,0x##c##u,0x##d##u,0x##e##u,0x##f##u,0x##g##u,"
|
|
|
|
"0x##h##u,0x##i##u,0x##j##u,0x##k##u,0x##l##u,0x##m##u,0x##n##u,0x##o##u,0x##p##u)\n");
|
|
|
|
fprintf(fp, "#if ECMULT_TABLE_SIZE(ECMULT_WINDOW_SIZE) > %ld\n", ECMULT_TABLE_SIZE(ECMULT_WINDOW_SIZE));
|
2021-12-17 11:28:48 -05:00
|
|
|
fprintf(fp, " #error configuration mismatch, invalid ECMULT_WINDOW_SIZE. Try deleting precomputed_ecmult.h before the build.\n");
|
2021-06-28 16:33:03 -04:00
|
|
|
fprintf(fp, "#endif\n");
|
|
|
|
fprintf(fp, "#if defined(EXHAUSTIVE_TEST_ORDER)\n");
|
|
|
|
fprintf(fp, "#if EXHAUSTIVE_TEST_ORDER == 13\n");
|
|
|
|
fprintf(fp, "#define WINDOW_G %d\n", window_g_13);
|
|
|
|
fprintf(fp, "#elif EXHAUSTIVE_TEST_ORDER == 199\n");
|
|
|
|
fprintf(fp, "#define WINDOW_G %d\n", window_g_199);
|
|
|
|
fprintf(fp, "#else\n");
|
|
|
|
fprintf(fp, " #error No known generator for the specified exhaustive test group order.\n");
|
|
|
|
fprintf(fp, "#endif\n");
|
2021-12-17 12:07:42 -05:00
|
|
|
fprintf(fp, "static secp256k1_ge_storage secp256k1_pre_g[ECMULT_TABLE_SIZE(WINDOW_G)];\n");
|
|
|
|
fprintf(fp, "static secp256k1_ge_storage secp256k1_pre_g_128[ECMULT_TABLE_SIZE(WINDOW_G)];\n");
|
2021-06-28 16:33:03 -04:00
|
|
|
fprintf(fp, "#else /* !defined(EXHAUSTIVE_TEST_ORDER) */\n");
|
|
|
|
fprintf(fp, "#define WINDOW_G ECMULT_WINDOW_SIZE\n");
|
|
|
|
|
|
|
|
print_two_tables(fp, ECMULT_WINDOW_SIZE, &g, 1);
|
|
|
|
|
2021-12-17 12:07:42 -05:00
|
|
|
fprintf(fp, "#endif /* defined(EXHAUSTIVE_TEST_ORDER) */\n");
|
2021-06-28 16:33:03 -04:00
|
|
|
fprintf(fp, "#undef S\n");
|
2021-12-17 11:28:48 -05:00
|
|
|
fprintf(fp, "#endif /* SECP256K1_PRECOMPUTED_ECMULT_H */\n");
|
2021-06-28 16:33:03 -04:00
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|