Bitcoin Core  28.1.0
P2P Digital Currency
precompute_ecmult_gen.c
Go to the documentation of this file.
1 /*********************************************************************************
2  * Copyright (c) 2013, 2014, 2015, 2021 Thomas Daede, Cory Fields, Pieter Wuille *
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 #include "../include/secp256k1.h"
11 
12 #include "assumptions.h"
13 #include "util.h"
14 
15 #include "group.h"
16 #include "int128_impl.h"
17 #include "ecmult_gen.h"
19 
20 static const int CONFIGS[][2] = {
21  {2, 5},
22  {11, 6},
23  {43, 6}
24 };
25 
26 static void print_table(FILE* fp, int blocks, int teeth) {
27  int spacing = CEIL_DIV(256, blocks * teeth);
28  size_t points = ((size_t)1) << (teeth - 1);
29  int outer;
30  size_t inner;
31 
33  secp256k1_ecmult_gen_compute_table(table, &secp256k1_ge_const_g, blocks, teeth, spacing);
34 
35  fprintf(fp, "#elif (COMB_BLOCKS == %d) && (COMB_TEETH == %d) && (COMB_SPACING == %d)\n", blocks, teeth, spacing);
36  for (outer = 0; outer != blocks; outer++) {
37  fprintf(fp,"{");
38  for (inner = 0; inner != points; inner++) {
39  fprintf(fp, "S(%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32
40  ",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32")",
41  SECP256K1_GE_STORAGE_CONST_GET(table[outer * points + inner]));
42  if (inner != points - 1) {
43  fprintf(fp,",\n");
44  }
45  }
46  if (outer != blocks - 1) {
47  fprintf(fp,"},\n");
48  } else {
49  fprintf(fp,"}\n");
50  }
51  }
52  free(table);
53 }
54 
55 int main(int argc, char **argv) {
56  const char outfile[] = "src/precomputed_ecmult_gen.c";
57  FILE* fp;
58  size_t config;
59  int did_current_config = 0;
60 
61  (void)argc;
62  (void)argv;
63 
64  fp = fopen(outfile, "w");
65  if (fp == NULL) {
66  fprintf(stderr, "Could not open %s for writing!\n", outfile);
67  return -1;
68  }
69 
70  fprintf(fp, "/* This file was automatically generated by precompute_ecmult_gen. */\n");
71  fprintf(fp, "/* See ecmult_gen_impl.h for details about the contents of this file. */\n");
72  fprintf(fp, "#include \"group.h\"\n");
73  fprintf(fp, "#include \"ecmult_gen.h\"\n");
74  fprintf(fp, "#include \"precomputed_ecmult_gen.h\"\n");
75  fprintf(fp, "#ifdef EXHAUSTIVE_TEST_ORDER\n");
76  fprintf(fp, "# error Cannot compile precomputed_ecmult_gen.c in exhaustive test mode\n");
77  fprintf(fp, "#endif /* EXHAUSTIVE_TEST_ORDER */\n");
78  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");
79 
80  fprintf(fp, "const secp256k1_ge_storage secp256k1_ecmult_gen_prec_table[COMB_BLOCKS][COMB_POINTS] = {\n");
81  fprintf(fp, "#if 0\n");
82  for (config = 0; config < sizeof(CONFIGS) / sizeof(*CONFIGS); ++config) {
83  print_table(fp, CONFIGS[config][0], CONFIGS[config][1]);
84  if (CONFIGS[config][0] == COMB_BLOCKS && CONFIGS[config][1] == COMB_TEETH) {
85  did_current_config = 1;
86  }
87  }
88  if (!did_current_config) {
90  }
91  fprintf(fp, "#else\n");
92  fprintf(fp, "# error Configuration mismatch, invalid COMB_* parameters. Try deleting precomputed_ecmult_gen.c before the build.\n");
93  fprintf(fp, "#endif\n");
94 
95  fprintf(fp, "};\n");
96  fprintf(fp, "#undef S\n");
97  fclose(fp);
98 
99  return 0;
100 }
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:26
static const int CONFIGS[][2]
static const secp256k1_ge secp256k1_ge_const_g
Definition: group_impl.h:70
#define COMB_BLOCKS
Definition: ecmult_gen.h:66
#define SECP256K1_GE_STORAGE_CONST_GET(t)
Definition: group.h:45
static void print_table(FILE *fp, int blocks, int teeth)
static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage *table, const secp256k1_ge *gen, int blocks, int teeth, int spacing)
#define COMB_TEETH
Definition: ecmult_gen.h:72
#define CEIL_DIV(x, y)
Definition: util.h:174
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
Definition: util.h:156
static const secp256k1_callback default_error_callback
Definition: util.h:111
int main(int argc, char **argv)