Skip to content

Commit be6944a

Browse files
Merge #1042: Follow-ups to making all tables fully static
e05da9e Fix c++ build (Pieter Wuille) c45386d Cleanup preprocessor indentation in precompute{,d}_ecmult{,_gen} (Pieter Wuille) 19d96e1 Split off .c file from precomputed_ecmult.h (Pieter Wuille) 1a6691a Split off .c file from precomputed_ecmult_gen.h (Pieter Wuille) bb36331 Simplify precompute_ecmult_print_* (Pieter Wuille) 38cd84a Compute ecmult tables at runtime for tests_exhaustive (Pieter Wuille) e458ec2 Move ecmult table computation code to separate file (Pieter Wuille) fc1bf9f Split ecmult table computation and printing (Pieter Wuille) 31feab0 Rename function secp256k1_ecmult_gen_{create_prec -> compute}_table (Pieter Wuille) 725370c Rename ecmult_gen_prec -> ecmult_gen_compute_table (Pieter Wuille) 075252c Rename ecmult_static_pre_g -> precomputed_ecmult (Pieter Wuille) 7cf47f7 Rename ecmult_gen_static_prec_table -> precomputed_ecmult_gen (Pieter Wuille) f95b810 Rename gen_ecmult_static_pre_g -> precompute_ecmult (Pieter Wuille) bae7768 Rename gen_ecmult_gen_static_prec_table -> precompute_ecmult_gen (Pieter Wuille) Pull request description: This PR implements a number of changes to follow up after merging #988: * Naming consistency: * All precomputed table files now have name `precomputed_*.*` * All source files related to the creation of the precomputed table files have name `precompute_*.*`. * All source files related to the computation of tables (whether they go in precomputed files or not) have name `*_compute_table.*`. * Make the tables for exhaustive tests be computed at runtime rather than compile time (this was already the case for ecmult_gen, but not ecmult). This is a preparation for the next point, as the alternative would be to have separate precomputed libraries for the exhaustive tests and other binaries. * Moves the actual tables to separate `precomputed_*.c` files, which are compiled only once as part of a new `libsecp256k1_precomputed.la`, included where relevant. The corresponding `precomputed_*.h` file are normal source files. Retry of #1041. ACKs for top commit: real-or-random: ACK e05da9e jonasnick: ACK e05da9e Tree-SHA512: 71eadd66e30e511b786e910755e0eda53330dfa163b37e33602c3392f7b893569f56d3ca9870e85cbb3de83880fc5aef61ac3d55d759d7395086a69023f13f03
2 parents 0559fc6 + e05da9e commit be6944a

18 files changed

+319
-371
lines changed

.gitattributes

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
src/ecmult_static_pre_g.h linguist-generated
2-
src/ecmult_gen_static_prec_table.h linguist-generated
1+
src/precomputed_ecmult.c linguist-generated
2+
src/precomputed_ecmult_gen.c linguist-generated

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ bench_ecmult
33
bench_internal
44
tests
55
exhaustive_tests
6-
gen_ecmult_gen_static_prec_table
7-
gen_ecmult_static_pre_g
6+
precompute_ecmult_gen
7+
precompute_ecmult
88
valgrind_ctime_test
99
*.exe
1010
*.so

Makefile.am

+32-23
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ noinst_HEADERS += src/eckey.h
2626
noinst_HEADERS += src/eckey_impl.h
2727
noinst_HEADERS += src/ecmult.h
2828
noinst_HEADERS += src/ecmult_impl.h
29+
noinst_HEADERS += src/ecmult_compute_table.h
30+
noinst_HEADERS += src/ecmult_compute_table_impl.h
2931
noinst_HEADERS += src/ecmult_const.h
3032
noinst_HEADERS += src/ecmult_const_impl.h
3133
noinst_HEADERS += src/ecmult_gen.h
3234
noinst_HEADERS += src/ecmult_gen_impl.h
33-
noinst_HEADERS += src/ecmult_gen_prec.h
34-
noinst_HEADERS += src/ecmult_gen_prec_impl.h
35+
noinst_HEADERS += src/ecmult_gen_compute_table.h
36+
noinst_HEADERS += src/ecmult_gen_compute_table_impl.h
3537
noinst_HEADERS += src/field_10x26.h
3638
noinst_HEADERS += src/field_10x26_impl.h
3739
noinst_HEADERS += src/field_5x52.h
@@ -42,6 +44,8 @@ noinst_HEADERS += src/modinv32.h
4244
noinst_HEADERS += src/modinv32_impl.h
4345
noinst_HEADERS += src/modinv64.h
4446
noinst_HEADERS += src/modinv64_impl.h
47+
noinst_HEADERS += src/precomputed_ecmult.h
48+
noinst_HEADERS += src/precomputed_ecmult_gen.h
4549
noinst_HEADERS += src/assumptions.h
4650
noinst_HEADERS += src/util.h
4751
noinst_HEADERS += src/scratch.h
@@ -60,12 +64,17 @@ noinst_HEADERS += contrib/lax_der_parsing.c
6064
noinst_HEADERS += contrib/lax_der_privatekey_parsing.h
6165
noinst_HEADERS += contrib/lax_der_privatekey_parsing.c
6266

67+
PRECOMPUTED_LIB = libsecp256k1_precomputed.la
68+
noinst_LTLIBRARIES = $(PRECOMPUTED_LIB)
69+
libsecp256k1_precomputed_la_SOURCES = src/precomputed_ecmult.c src/precomputed_ecmult_gen.c
70+
libsecp256k1_precomputed_la_CPPFLAGS = $(SECP_INCLUDES)
71+
6372
if USE_EXTERNAL_ASM
6473
COMMON_LIB = libsecp256k1_common.la
65-
noinst_LTLIBRARIES = $(COMMON_LIB)
6674
else
6775
COMMON_LIB =
6876
endif
77+
noinst_LTLIBRARIES += $(COMMON_LIB)
6978

7079
pkgconfigdir = $(libdir)/pkgconfig
7180
pkgconfig_DATA = libsecp256k1.pc
@@ -78,7 +87,7 @@ endif
7887

7988
libsecp256k1_la_SOURCES = src/secp256k1.c
8089
libsecp256k1_la_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src $(SECP_INCLUDES)
81-
libsecp256k1_la_LIBADD = $(SECP_LIBS) $(COMMON_LIB)
90+
libsecp256k1_la_LIBADD = $(SECP_LIBS) $(COMMON_LIB) $(PRECOMPUTED_LIB)
8291
libsecp256k1_la_LDFLAGS = -no-undefined
8392

8493
if VALGRIND_ENABLED
@@ -91,10 +100,10 @@ noinst_PROGRAMS += bench bench_internal bench_ecmult
91100
bench_SOURCES = src/bench.c
92101
bench_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
93102
bench_internal_SOURCES = src/bench_internal.c
94-
bench_internal_LDADD = $(SECP_LIBS) $(COMMON_LIB)
103+
bench_internal_LDADD = $(SECP_LIBS) $(COMMON_LIB) $(PRECOMPUTED_LIB)
95104
bench_internal_CPPFLAGS = $(SECP_INCLUDES)
96105
bench_ecmult_SOURCES = src/bench_ecmult.c
97-
bench_ecmult_LDADD = $(SECP_LIBS) $(COMMON_LIB)
106+
bench_ecmult_LDADD = $(SECP_LIBS) $(COMMON_LIB) $(PRECOMPUTED_LIB)
98107
bench_ecmult_CPPFLAGS = $(SECP_INCLUDES)
99108
endif
100109

@@ -112,7 +121,7 @@ endif
112121
if !ENABLE_COVERAGE
113122
tests_CPPFLAGS += -DVERIFY
114123
endif
115-
tests_LDADD = $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
124+
tests_LDADD = $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB) $(PRECOMPUTED_LIB)
116125
tests_LDFLAGS = -static
117126
TESTS += tests
118127
endif
@@ -124,38 +133,38 @@ exhaustive_tests_CPPFLAGS = $(SECP_INCLUDES)
124133
if !ENABLE_COVERAGE
125134
exhaustive_tests_CPPFLAGS += -DVERIFY
126135
endif
136+
# Note: do not include $(PRECOMPUTED_LIB) in exhaustive_tests (it uses runtime-generated tables).
127137
exhaustive_tests_LDADD = $(SECP_LIBS) $(COMMON_LIB)
128138
exhaustive_tests_LDFLAGS = -static
129139
TESTS += exhaustive_tests
130140
endif
131141

132142
### Precomputed tables
133-
EXTRA_PROGRAMS = gen_ecmult_static_pre_g gen_ecmult_gen_static_prec_table
143+
EXTRA_PROGRAMS = precompute_ecmult precompute_ecmult_gen
134144
CLEANFILES = $(EXTRA_PROGRAMS)
135145

136-
gen_ecmult_static_pre_g_SOURCES = src/gen_ecmult_static_pre_g.c
137-
gen_ecmult_static_pre_g_CPPFLAGS = $(SECP_INCLUDES)
138-
gen_ecmult_static_pre_g_LDADD = $(SECP_LIBS) $(COMMON_LIB)
146+
precompute_ecmult_SOURCES = src/precompute_ecmult.c
147+
precompute_ecmult_CPPFLAGS = $(SECP_INCLUDES)
148+
precompute_ecmult_LDADD = $(SECP_LIBS) $(COMMON_LIB)
139149

140-
gen_ecmult_gen_static_prec_table_SOURCES = src/gen_ecmult_gen_static_prec_table.c
141-
gen_ecmult_gen_static_prec_table_CPPFLAGS = $(SECP_INCLUDES)
142-
gen_ecmult_gen_static_prec_table_LDADD = $(SECP_LIBS) $(COMMON_LIB)
150+
precompute_ecmult_gen_SOURCES = src/precompute_ecmult_gen.c
151+
precompute_ecmult_gen_CPPFLAGS = $(SECP_INCLUDES)
152+
precompute_ecmult_gen_LDADD = $(SECP_LIBS) $(COMMON_LIB)
143153

144154
# See Automake manual, Section "Errors with distclean".
145155
# We don't list any dependencies for the prebuilt files here because
146156
# otherwise make's decision whether to rebuild them (even in the first
147157
# build by a normal user) depends on mtimes, and thus is very fragile.
148158
# This means that rebuilds of the prebuilt files always need to be
149159
# forced by deleting them, e.g., by invoking `make clean-precomp`.
150-
src/ecmult_static_pre_g.h:
151-
$(MAKE) $(AM_MAKEFLAGS) gen_ecmult_static_pre_g$(EXEEXT)
152-
./gen_ecmult_static_pre_g$(EXEEXT)
153-
src/ecmult_gen_static_prec_table.h:
154-
$(MAKE) $(AM_MAKEFLAGS) gen_ecmult_gen_static_prec_table$(EXEEXT)
155-
./gen_ecmult_gen_static_prec_table$(EXEEXT)
156-
157-
PRECOMP = src/ecmult_gen_static_prec_table.h src/ecmult_static_pre_g.h
158-
noinst_HEADERS += $(PRECOMP)
160+
src/precomputed_ecmult.c:
161+
$(MAKE) $(AM_MAKEFLAGS) precompute_ecmult$(EXEEXT)
162+
./precompute_ecmult$(EXEEXT)
163+
src/precomputed_ecmult_gen.c:
164+
$(MAKE) $(AM_MAKEFLAGS) precompute_ecmult_gen$(EXEEXT)
165+
./precompute_ecmult_gen$(EXEEXT)
166+
167+
PRECOMP = src/precomputed_ecmult_gen.c src/precomputed_ecmult.c
159168
precomp: $(PRECOMP)
160169

161170
# Ensure the prebuilt files will be build first (only if they don't exist,

configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ AC_ARG_WITH([ecmult-window], [AS_HELP_STRING([--with-ecmult-window=SIZE|auto],
152152
[window size for ecmult precomputation for verification, specified as integer in range [2..24].]
153153
[Larger values result in possibly better performance at the cost of an exponentially larger precomputed table.]
154154
[The table will store 2^(SIZE-1) * 64 bytes of data but can be larger in memory due to platform-specific padding and alignment.]
155-
[A window size larger than 15 will require you delete the prebuilt ecmult_static_pre_g.h file so that it can be rebuilt.]
155+
[A window size larger than 15 will require you delete the prebuilt precomputed_ecmult.c file so that it can be rebuilt.]
156156
[For very large window sizes, use "make -j 1" to reduce memory use during compilation.]
157157
["auto" is a reasonable setting for desktop machines (currently 15). [default=auto]]
158158
)],

src/ecmult_compute_table.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
#ifndef SECP256K1_ECMULT_COMPUTE_TABLE_H
8+
#define SECP256K1_ECMULT_COMPUTE_TABLE_H
9+
10+
/* Construct table of all odd multiples of gen in range 1..(2**(window_g-1)-1). */
11+
static void secp256k1_ecmult_compute_table(secp256k1_ge_storage* table, int window_g, const secp256k1_gej* gen);
12+
13+
/* Like secp256k1_ecmult_compute_table, but one for both gen and gen*2^128. */
14+
static void secp256k1_ecmult_compute_two_tables(secp256k1_ge_storage* table, secp256k1_ge_storage* table_128, int window_g, const secp256k1_ge* gen);
15+
16+
#endif /* SECP256K1_ECMULT_COMPUTE_TABLE_H */

src/ecmult_compute_table_impl.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
#ifndef SECP256K1_ECMULT_COMPUTE_TABLE_IMPL_H
8+
#define SECP256K1_ECMULT_COMPUTE_TABLE_IMPL_H
9+
10+
#include "ecmult_compute_table.h"
11+
#include "group_impl.h"
12+
#include "field_impl.h"
13+
#include "ecmult.h"
14+
#include "util.h"
15+
16+
static void secp256k1_ecmult_compute_table(secp256k1_ge_storage* table, int window_g, const secp256k1_gej* gen) {
17+
secp256k1_gej gj;
18+
secp256k1_ge ge, dgen;
19+
int j;
20+
21+
gj = *gen;
22+
secp256k1_ge_set_gej_var(&ge, &gj);
23+
secp256k1_ge_to_storage(&table[0], &ge);
24+
25+
secp256k1_gej_double_var(&gj, gen, NULL);
26+
secp256k1_ge_set_gej_var(&dgen, &gj);
27+
28+
for (j = 1; j < ECMULT_TABLE_SIZE(window_g); ++j) {
29+
secp256k1_gej_set_ge(&gj, &ge);
30+
secp256k1_gej_add_ge_var(&gj, &gj, &dgen, NULL);
31+
secp256k1_ge_set_gej_var(&ge, &gj);
32+
secp256k1_ge_to_storage(&table[j], &ge);
33+
}
34+
}
35+
36+
/* Like secp256k1_ecmult_compute_table, but one for both gen and gen*2^128. */
37+
static void secp256k1_ecmult_compute_two_tables(secp256k1_ge_storage* table, secp256k1_ge_storage* table_128, int window_g, const secp256k1_ge* gen) {
38+
secp256k1_gej gj;
39+
int i;
40+
41+
secp256k1_gej_set_ge(&gj, gen);
42+
secp256k1_ecmult_compute_table(table, window_g, &gj);
43+
for (i = 0; i < 128; ++i) {
44+
secp256k1_gej_double_var(&gj, &gj, NULL);
45+
}
46+
secp256k1_ecmult_compute_table(table_128, window_g, &gj);
47+
}
48+
49+
#endif /* SECP256K1_ECMULT_COMPUTE_TABLE_IMPL_H */

src/ecmult_gen_prec.h src/ecmult_gen_compute_table.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
55
***********************************************************************/
66

7-
#ifndef SECP256K1_ECMULT_GEN_PREC_H
8-
#define SECP256K1_ECMULT_GEN_PREC_H
7+
#ifndef SECP256K1_ECMULT_GEN_COMPUTE_TABLE_H
8+
#define SECP256K1_ECMULT_GEN_COMPUTE_TABLE_H
99

1010
#include "ecmult_gen.h"
1111

12-
static void secp256k1_ecmult_gen_create_prec_table(secp256k1_ge_storage* table, const secp256k1_ge* gen, int bits);
12+
static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage* table, const secp256k1_ge* gen, int bits);
1313

14-
#endif /* SECP256K1_ECMULT_GEN_PREC_H */
14+
#endif /* SECP256K1_ECMULT_GEN_COMPUTE_TABLE_H */

src/ecmult_gen_prec_impl.h src/ecmult_gen_compute_table_impl.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
55
***********************************************************************/
66

7-
#ifndef SECP256K1_ECMULT_GEN_PREC_IMPL_H
8-
#define SECP256K1_ECMULT_GEN_PREC_IMPL_H
7+
#ifndef SECP256K1_ECMULT_GEN_COMPUTE_TABLE_IMPL_H
8+
#define SECP256K1_ECMULT_GEN_COMPUTE_TABLE_IMPL_H
99

10-
#include "ecmult_gen_prec.h"
10+
#include "ecmult_gen_compute_table.h"
1111
#include "group_impl.h"
1212
#include "field_impl.h"
1313
#include "ecmult_gen.h"
1414
#include "util.h"
1515

16-
static void secp256k1_ecmult_gen_create_prec_table(secp256k1_ge_storage* table, const secp256k1_ge* gen, int bits) {
16+
static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage* table, const secp256k1_ge* gen, int bits) {
1717
int g = ECMULT_GEN_PREC_G(bits);
1818
int n = ECMULT_GEN_PREC_N(bits);
1919

@@ -78,4 +78,4 @@ static void secp256k1_ecmult_gen_create_prec_table(secp256k1_ge_storage* table,
7878
free(prec);
7979
}
8080

81-
#endif /* SECP256K1_ECMULT_GEN_PREC_IMPL_H */
81+
#endif /* SECP256K1_ECMULT_GEN_COMPUTE_TABLE_IMPL_H */

src/ecmult_gen_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "group.h"
1313
#include "ecmult_gen.h"
1414
#include "hash_impl.h"
15-
#include "ecmult_gen_static_prec_table.h"
15+
#include "precomputed_ecmult_gen.h"
1616

1717
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx) {
1818
secp256k1_ecmult_gen_blind(ctx, NULL);

src/ecmult_impl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "group.h"
1515
#include "scalar.h"
1616
#include "ecmult.h"
17-
#include "ecmult_static_pre_g.h"
17+
#include "precomputed_ecmult.h"
1818

1919
#if defined(EXHAUSTIVE_TEST_ORDER)
2020
/* We need to lower these values for exhaustive tests because
@@ -103,7 +103,7 @@ static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej *prej, sec
103103
* It only operates on tables sized for WINDOW_A wnaf multiples.
104104
*
105105
* To compute a*P + b*G, we compute a table for P using this function,
106-
* and use the precomputed table in <ecmult_static_pre_g.h> for G.
106+
* and use the precomputed table in <precomputed_ecmult.c> for G.
107107
*/
108108
static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *pre, secp256k1_fe *globalz, const secp256k1_gej *a) {
109109
secp256k1_gej prej[ECMULT_TABLE_SIZE(WINDOW_A)];

0 commit comments

Comments
 (0)