Bitcoin Core  31.0.0
P2P Digital Currency
precompute_ecmult.c
Go to the documentation of this file.
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 #include <stdlib.h>
10 
11 #include "../include/secp256k1.h"
12 
13 #include "assumptions.h"
14 #include "util.h"
15 
16 #include "field_impl.h"
17 #include "group_impl.h"
18 #include "int128_impl.h"
19 #include "ecmult.h"
21 
22 static void print_table(FILE *fp, const char *name, int window_g, const secp256k1_ge_storage* table) {
23  size_t j;
24  int i;
25 
26  fprintf(fp, "const secp256k1_ge_storage %s[ECMULT_TABLE_SIZE(WINDOW_G)] = {\n", name);
27  fprintf(fp, " S(%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32
28  ",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32")\n",
30 
31  j = 1;
32  for(i = 3; i <= window_g; ++i) {
33  fprintf(fp, "#if WINDOW_G > %d\n", i-1);
34  for(;j < ECMULT_TABLE_SIZE(i); ++j) {
35  fprintf(fp, ",S(%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32
36  ",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32")\n",
38  }
39  fprintf(fp, "#endif\n");
40  }
41  fprintf(fp, "};\n");
42 }
43 
44 static void print_two_tables(FILE *fp, int window_g) {
45  secp256k1_ge_storage* table = malloc(ECMULT_TABLE_SIZE(window_g) * sizeof(secp256k1_ge_storage));
46  secp256k1_ge_storage* table_128 = malloc(ECMULT_TABLE_SIZE(window_g) * sizeof(secp256k1_ge_storage));
47 
48  secp256k1_ecmult_compute_two_tables(table, table_128, window_g, &secp256k1_ge_const_g);
49 
50  print_table(fp, "secp256k1_pre_g", window_g, table);
51  print_table(fp, "secp256k1_pre_g_128", window_g, table_128);
52 
53  free(table);
54  free(table_128);
55 }
56 
57 int main(void) {
58  /* Always compute all tables for window sizes up to 15. */
59  int window_g = (ECMULT_WINDOW_SIZE < 15) ? 15 : ECMULT_WINDOW_SIZE;
60  const char outfile[] = "src/precomputed_ecmult.c";
61  FILE* fp;
62 
63  fp = fopen(outfile, "w");
64  if (fp == NULL) {
65  fprintf(stderr, "Could not open %s for writing!\n", outfile);
66  return EXIT_FAILURE;
67  }
68 
69  fprintf(fp, "/* This file was automatically generated by precompute_ecmult. */\n");
70  fprintf(fp, "/* This file contains an array secp256k1_pre_g with odd multiples of the base point G and\n");
71  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");
72  fprintf(fp, " */\n");
73  fprintf(fp, "#include \"group.h\"\n");
74  fprintf(fp, "#include \"ecmult.h\"\n");
75  fprintf(fp, "#include \"precomputed_ecmult.h\"\n");
76  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");
77  fprintf(fp, "#if ECMULT_WINDOW_SIZE > %d\n", window_g);
78  fprintf(fp, " #error configuration mismatch, invalid ECMULT_WINDOW_SIZE. Try deleting precomputed_ecmult.c before the build.\n");
79  fprintf(fp, "#endif\n");
80  fprintf(fp, "#ifdef EXHAUSTIVE_TEST_ORDER\n");
81  fprintf(fp, "# error Cannot compile precomputed_ecmult.c in exhaustive test mode\n");
82  fprintf(fp, "#endif /* EXHAUSTIVE_TEST_ORDER */\n");
83  fprintf(fp, "#define WINDOW_G ECMULT_WINDOW_SIZE\n");
84 
85  print_two_tables(fp, window_g);
86 
87  fprintf(fp, "#undef S\n");
88  fclose(fp);
89 
90  return EXIT_SUCCESS;
91 }
return EXIT_SUCCESS
#define ECMULT_TABLE_SIZE(w)
The number of entries a table with precomputed multiples needs to have.
Definition: ecmult.h:41
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
static void print_two_tables(FILE *fp, int window_g)
static void print_table(FILE *fp, const char *name, int window_g, const secp256k1_ge_storage *table)
static const secp256k1_ge secp256k1_ge_const_g
Definition: group_impl.h:72
const char * name
Definition: rest.cpp:48
#define SECP256K1_GE_STORAGE_CONST_GET(t)
Definition: group.h:45
int main(void)
#define ECMULT_WINDOW_SIZE
Definition: ecmult.h:15
static void secp256k1_ecmult_compute_two_tables(secp256k1_ge_storage *table, secp256k1_ge_storage *table_128, int window_g, const secp256k1_ge *gen)