Bitcoin Core  22.99.0
P2P Digital Currency
gen_ecmult_static_pre_g.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 
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 #include "../include/secp256k1.h"
17 #include "assumptions.h"
18 #include "util.h"
19 #include "field_impl.h"
20 #include "group_impl.h"
21 #include "ecmult.h"
22 
23 void print_table(FILE *fp, const char *name, int window_g, const secp256k1_gej *gen, int with_conditionals) {
24  static secp256k1_gej gj;
25  static secp256k1_ge ge, dgen;
26  static secp256k1_ge_storage ges;
27  int j;
28  int i;
29 
30  gj = *gen;
31  secp256k1_ge_set_gej_var(&ge, &gj);
32  secp256k1_ge_to_storage(&ges, &ge);
33 
34  fprintf(fp, "static const secp256k1_ge_storage %s[ECMULT_TABLE_SIZE(WINDOW_G)] = {\n", name);
35  fprintf(fp, " S(%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32
36  ",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32")\n",
38 
39  secp256k1_gej_double_var(&gj, gen, NULL);
40  secp256k1_ge_set_gej_var(&dgen, &gj);
41 
42  j = 1;
43  for(i = 3; i <= window_g; ++i) {
44  if (with_conditionals) {
45  fprintf(fp, "#if ECMULT_TABLE_SIZE(WINDOW_G) > %ld\n", ECMULT_TABLE_SIZE(i-1));
46  }
47  for(;j < ECMULT_TABLE_SIZE(i); ++j) {
48  secp256k1_gej_set_ge(&gj, &ge);
49  secp256k1_gej_add_ge_var(&gj, &gj, &dgen, NULL);
50  secp256k1_ge_set_gej_var(&ge, &gj);
51  secp256k1_ge_to_storage(&ges, &ge);
52 
53  fprintf(fp, ",S(%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32
54  ",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32",%"PRIx32")\n",
56  }
57  if (with_conditionals) {
58  fprintf(fp, "#endif\n");
59  }
60  }
61  fprintf(fp, "};\n");
62 }
63 
64 void print_two_tables(FILE *fp, int window_g, const secp256k1_ge *g, int with_conditionals) {
65  secp256k1_gej gj;
66  int i;
67 
68  secp256k1_gej_set_ge(&gj, g);
69  print_table(fp, "secp256k1_pre_g", window_g, &gj, with_conditionals);
70  for (i = 0; i < 128; ++i) {
71  secp256k1_gej_double_var(&gj, &gj, NULL);
72  }
73  print_table(fp, "secp256k1_pre_g_128", window_g, &gj, with_conditionals);
74 }
75 
76 int main(void) {
77  const secp256k1_ge g = SECP256K1_G;
79  const secp256k1_ge g_199 = SECP256K1_G_ORDER_199;
80  const int window_g_13 = 4;
81  const int window_g_199 = 8;
82  FILE* fp;
83 
84  fp = fopen("src/ecmult_static_pre_g.h","w");
85  if (fp == NULL) {
86  fprintf(stderr, "Could not open src/ecmult_static_pre_g.h for writing!\n");
87  return -1;
88  }
89 
90  fprintf(fp, "/* This file was automatically generated by gen_ecmult_static_pre_g. */\n");
91  fprintf(fp, "/* This file contains an array secp256k1_pre_g with odd multiples of the base point G and\n");
92  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");
93  fprintf(fp, " */\n");
94  fprintf(fp, "#ifndef SECP256K1_ECMULT_STATIC_PRE_G_H\n");
95  fprintf(fp, "#define SECP256K1_ECMULT_STATIC_PRE_G_H\n");
96  fprintf(fp, "#include \"group.h\"\n");
97  fprintf(fp, "#ifdef S\n");
98  fprintf(fp, " #error macro identifier S already in use.\n");
99  fprintf(fp, "#endif\n");
100  fprintf(fp, "#define S(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) "
101  "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,"
102  "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");
103  fprintf(fp, "#if ECMULT_TABLE_SIZE(ECMULT_WINDOW_SIZE) > %ld\n", ECMULT_TABLE_SIZE(ECMULT_WINDOW_SIZE));
104  fprintf(fp, " #error configuration mismatch, invalid ECMULT_WINDOW_SIZE. Try deleting ecmult_static_pre_g.h before the build.\n");
105  fprintf(fp, "#endif\n");
106  fprintf(fp, "#if defined(EXHAUSTIVE_TEST_ORDER)\n");
107  fprintf(fp, "#if EXHAUSTIVE_TEST_ORDER == 13\n");
108  fprintf(fp, "#define WINDOW_G %d\n", window_g_13);
109 
110  print_two_tables(fp, window_g_13, &g_13, 0);
111 
112  fprintf(fp, "#elif EXHAUSTIVE_TEST_ORDER == 199\n");
113  fprintf(fp, "#define WINDOW_G %d\n", window_g_199);
114 
115  print_two_tables(fp, window_g_199, &g_199, 0);
116 
117  fprintf(fp, "#else\n");
118  fprintf(fp, " #error No known generator for the specified exhaustive test group order.\n");
119  fprintf(fp, "#endif\n");
120  fprintf(fp, "#else /* !defined(EXHAUSTIVE_TEST_ORDER) */\n");
121  fprintf(fp, "#define WINDOW_G ECMULT_WINDOW_SIZE\n");
122 
124 
125  fprintf(fp, "#endif\n");
126  fprintf(fp, "#undef S\n");
127  fprintf(fp, "#endif\n");
128  fclose(fp);
129 
130  return 0;
131 }
field_impl.h
fsbridge::fopen
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:27
group_impl.h
ecmult.h
util.h
SECP256K1_G_ORDER_13
#define SECP256K1_G_ORDER_13
Definition: group_impl.h:13
secp256k1_gej
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:23
assumptions.h
SECP256K1_G
#define SECP256K1_G
Generator for secp256k1, value 'g' defined in "Standards for Efficient Cryptography" (SEC2) 2....
Definition: group_impl.h:28
secp256k1_ge_storage
Definition: group.h:33
SECP256K1_GE_STORAGE_CONST_GET
#define SECP256K1_GE_STORAGE_CONST_GET(t)
Definition: group.h:40
name
const char * name
Definition: rest.cpp:52
main
int main(void)
Definition: gen_ecmult_static_pre_g.c:76
libsecp256k1-config.h
secp256k1_gej_add_ge_var
static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b (with b given in affine coordinates).
secp256k1_ge_set_gej_var
static void secp256k1_ge_set_gej_var(secp256k1_ge *r, secp256k1_gej *a)
Set a group element equal to another which is given in jacobian coordinates.
secp256k1_gej_double_var
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Set r equal to the double of a.
ECMULT_WINDOW_SIZE
#define ECMULT_WINDOW_SIZE
Definition: libsecp256k1-config.h:15
ECMULT_TABLE_SIZE
#define ECMULT_TABLE_SIZE(w)
The number of entries a table with precomputed multiples needs to have.
Definition: ecmult.h:30
secp256k1_ge_to_storage
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
Convert a group element to the storage type.
SECP256K1_G_ORDER_199
#define SECP256K1_G_ORDER_199
Definition: group_impl.h:19
ByteUnit::g
@ g
print_table
void print_table(FILE *fp, const char *name, int window_g, const secp256k1_gej *gen, int with_conditionals)
Definition: gen_ecmult_static_pre_g.c:23
secp256k1_ge
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:13
secp256k1_gej_set_ge
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
print_two_tables
void print_two_tables(FILE *fp, int window_g, const secp256k1_ge *g, int with_conditionals)
Definition: gen_ecmult_static_pre_g.c:64