|
| 1 | +/***************************************************************************************************** |
| 2 | + * Copyright (c) 2013, 2014, 2017, 2021 Pieter Wuille, Andrew Poelstra, Jonas Nick, Russell O'Connor * |
| 3 | + * Distributed under the MIT software license, see the accompanying * |
| 4 | + * file COPYING or https://www.opensource.org/licenses/mit-license.php. * |
| 5 | + *****************************************************************************************************/ |
| 6 | + |
| 7 | +#include <inttypes.h> |
| 8 | +#include <stdio.h> |
| 9 | + |
| 10 | +/* Autotools creates libsecp256k1-config.h, of which ECMULT_WINDOW_SIZE is needed. |
| 11 | + ifndef guard so downstream users can define their own if they do not use autotools. */ |
| 12 | +#if !defined(ECMULT_WINDOW_SIZE) |
| 13 | +#include "libsecp256k1-config.h" |
| 14 | +#endif |
| 15 | + |
| 16 | +/* In principle we could use ASM, but this yields only a minor speedup in |
| 17 | + build time and it's very complicated. In particular when cross-compiling, we'd |
| 18 | + need to build the ASM for the build and the host machine. */ |
| 19 | +#undef USE_EXTERNAL_ASM |
| 20 | +#undef USE_ASM_X86_64 |
| 21 | + |
| 22 | +#include "../include/secp256k1.h" |
| 23 | +#include "assumptions.h" |
| 24 | +#include "util.h" |
| 25 | +#include "field_impl.h" |
| 26 | +#include "group_impl.h" |
| 27 | +#include "ecmult.h" |
| 28 | + |
| 29 | +void print_table(FILE *fp, const char *name, int window_g, const secp256k1_gej *gen, int with_conditionals) { |
| 30 | + static secp256k1_gej gj; |
| 31 | + static secp256k1_ge ge, dgen; |
| 32 | + static secp256k1_ge_storage ges; |
| 33 | + int j; |
| 34 | + int i; |
| 35 | + |
| 36 | + gj = *gen; |
| 37 | + secp256k1_ge_set_gej_var(&ge, &gj); |
| 38 | + secp256k1_ge_to_storage(&ges, &ge); |
| 39 | + |
| 40 | + fprintf(fp, "static const secp256k1_ge_storage %s[ECMULT_TABLE_SIZE(WINDOW_G)] = {\n", name); |
| 41 | + fprintf(fp, " S(%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32 |
| 42 | + ",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32")\n", |
| 43 | + SECP256K1_GE_STORAGE_CONST_GET(ges)); |
| 44 | + |
| 45 | + secp256k1_gej_double_var(&gj, gen, NULL); |
| 46 | + secp256k1_ge_set_gej_var(&dgen, &gj); |
| 47 | + |
| 48 | + j = 1; |
| 49 | + for(i = 3; i <= window_g; ++i) { |
| 50 | + if (with_conditionals) { |
| 51 | + fprintf(fp, "#if ECMULT_TABLE_SIZE(WINDOW_G) > %ld\n", ECMULT_TABLE_SIZE(i-1)); |
| 52 | + } |
| 53 | + for(;j < ECMULT_TABLE_SIZE(i); ++j) { |
| 54 | + secp256k1_gej_set_ge(&gj, &ge); |
| 55 | + secp256k1_gej_add_ge_var(&gj, &gj, &dgen, NULL); |
| 56 | + secp256k1_ge_set_gej_var(&ge, &gj); |
| 57 | + secp256k1_ge_to_storage(&ges, &ge); |
| 58 | + |
| 59 | + fprintf(fp, ",S(%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32 |
| 60 | + ",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32")\n", |
| 61 | + SECP256K1_GE_STORAGE_CONST_GET(ges)); |
| 62 | + } |
| 63 | + if (with_conditionals) { |
| 64 | + fprintf(fp, "#endif\n"); |
| 65 | + } |
| 66 | + } |
| 67 | + fprintf(fp, "};\n"); |
| 68 | +} |
| 69 | + |
| 70 | +void print_two_tables(FILE *fp, int window_g, const secp256k1_ge *g, int with_conditionals) { |
| 71 | + secp256k1_gej gj; |
| 72 | + int i; |
| 73 | + |
| 74 | + secp256k1_gej_set_ge(&gj, g); |
| 75 | + print_table(fp, "secp256k1_pre_g", window_g, &gj, with_conditionals); |
| 76 | + for (i = 0; i < 128; ++i) { |
| 77 | + secp256k1_gej_double_var(&gj, &gj, NULL); |
| 78 | + } |
| 79 | + print_table(fp, "secp256k1_pre_g_128", window_g, &gj, with_conditionals); |
| 80 | +} |
| 81 | + |
| 82 | +int main(void) { |
| 83 | + const secp256k1_ge g = SECP256K1_G; |
| 84 | + const secp256k1_ge g_13 = SECP256K1_G_ORDER_13; |
| 85 | + const secp256k1_ge g_199 = SECP256K1_G_ORDER_199; |
| 86 | + const int window_g_13 = 4; |
| 87 | + const int window_g_199 = 8; |
| 88 | + FILE* fp; |
| 89 | + |
| 90 | + fp = fopen("src/ecmult_static_pre_g.h","w"); |
| 91 | + if (fp == NULL) { |
| 92 | + fprintf(stderr, "Could not open src/ecmult_static_pre_g.h for writing!\n"); |
| 93 | + return -1; |
| 94 | + } |
| 95 | + |
| 96 | + fprintf(fp, "/* This file was automatically generated by gen_ecmult_static_pre_g. */\n"); |
| 97 | + fprintf(fp, "/* This file contains an array secp256k1_pre_g with odd multiples of the base point G and\n"); |
| 98 | + 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"); |
| 99 | + fprintf(fp, " */\n"); |
| 100 | + fprintf(fp, "#ifndef SECP256K1_ECMULT_STATIC_PRE_G_H\n"); |
| 101 | + fprintf(fp, "#define SECP256K1_ECMULT_STATIC_PRE_G_H\n"); |
| 102 | + fprintf(fp, "#include \"group.h\"\n"); |
| 103 | + fprintf(fp, "#ifdef S\n"); |
| 104 | + fprintf(fp, " #error macro identifier S already in use.\n"); |
| 105 | + fprintf(fp, "#endif\n"); |
| 106 | + fprintf(fp, "#define S(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) " |
| 107 | + "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," |
| 108 | + "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"); |
| 109 | + fprintf(fp, "#if ECMULT_TABLE_SIZE(ECMULT_WINDOW_SIZE) > %ld\n", ECMULT_TABLE_SIZE(ECMULT_WINDOW_SIZE)); |
| 110 | + fprintf(fp, " #error configuration mismatch, invalid ECMULT_WINDOW_SIZE. Try deleting ecmult_static_pre_g.h before the build.\n"); |
| 111 | + fprintf(fp, "#endif\n"); |
| 112 | + fprintf(fp, "#if defined(EXHAUSTIVE_TEST_ORDER)\n"); |
| 113 | + fprintf(fp, "#if EXHAUSTIVE_TEST_ORDER == 13\n"); |
| 114 | + fprintf(fp, "#define WINDOW_G %d\n", window_g_13); |
| 115 | + |
| 116 | + print_two_tables(fp, window_g_13, &g_13, 0); |
| 117 | + |
| 118 | + fprintf(fp, "#elif EXHAUSTIVE_TEST_ORDER == 199\n"); |
| 119 | + fprintf(fp, "#define WINDOW_G %d\n", window_g_199); |
| 120 | + |
| 121 | + print_two_tables(fp, window_g_199, &g_199, 0); |
| 122 | + |
| 123 | + fprintf(fp, "#else\n"); |
| 124 | + fprintf(fp, " #error No known generator for the specified exhaustive test group order.\n"); |
| 125 | + fprintf(fp, "#endif\n"); |
| 126 | + fprintf(fp, "#else /* !defined(EXHAUSTIVE_TEST_ORDER) */\n"); |
| 127 | + fprintf(fp, "#define WINDOW_G ECMULT_WINDOW_SIZE\n"); |
| 128 | + |
| 129 | + print_two_tables(fp, ECMULT_WINDOW_SIZE, &g, 1); |
| 130 | + |
| 131 | + fprintf(fp, "#endif\n"); |
| 132 | + fprintf(fp, "#undef S\n"); |
| 133 | + fprintf(fp, "#endif\n"); |
| 134 | + fclose(fp); |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
0 commit comments