Skip to content

Commit 5133f78

Browse files
committed
Merge pull request bitcoin#254
733c1e6 Add travis build to test the static context. (Thomas Daede) fbecc38 Add ability to use a statically generated ecmult context. (Thomas Daede)
2 parents b0a60e6 + 733c1e6 commit 5133f78

File tree

10 files changed

+272
-5
lines changed

10 files changed

+272
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ bench_verify
44
bench_recover
55
bench_internal
66
tests
7+
gen_context
78
*.exe
89
*.so
910
*.a
@@ -28,6 +29,7 @@ build-aux/
2829
*~
2930
src/libsecp256k1-config.h
3031
src/libsecp256k1-config.h.in
32+
src/ecmult_static_context.h
3133
m4/libtool.m4
3234
m4/ltoptions.m4
3335
m4/ltsugar.m4

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ compiler:
88
- gcc
99
env:
1010
global:
11-
- FIELD=auto BIGNUM=auto SCALAR=auto ENDOMORPHISM=no ASM=no BUILD=check EXTRAFLAGS= HOST=
11+
- FIELD=auto BIGNUM=auto SCALAR=auto ENDOMORPHISM=no STATICPRECOMPUTATION=no ASM=no BUILD=check EXTRAFLAGS= HOST=
1212
matrix:
1313
- SCALAR=32bit
1414
- SCALAR=64bit
@@ -20,6 +20,7 @@ env:
2020
- FIELD=32bit ENDOMORPHISM=yes
2121
- BIGNUM=no
2222
- BIGNUM=no ENDOMORPHISM=yes
23+
- BIGNUM=no STATICPRECOMPUTATION=yes
2324
- BUILD=distcheck
2425
- EXTRAFLAGS=CFLAGS=-DDETERMINISTIC
2526
matrix:
@@ -55,5 +56,5 @@ before_script: ./autogen.sh
5556
script:
5657
- if [ -n "$HOST" ]; then export USE_HOST="--host=$HOST"; fi
5758
- if [ "x$HOST" = "xi686-linux-gnu" ]; then export CC="$CC -m32"; fi
58-
- ./configure --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM --with-scalar=$SCALAR $EXTRAFLAGS $USE_HOST && make -j2 $BUILD
59+
- ./configure --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM --with-scalar=$SCALAR --enable-ecmult-static-precomputation=$STATICPRECOMPUTATION $EXTRAFLAGS $USE_HOST && make -j2 $BUILD
5960
os: linux

Makefile.am

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,22 @@ tests_LDFLAGS = -static
7474
TESTS = tests
7575
endif
7676

77+
if USE_ECMULT_STATIC_PRECOMPUTATION
78+
CPPFLAGS_FOR_BUILD +=-I.
79+
CFLAGS_FOR_BUILD += -Wall -Wextra -Wno-unused-function
80+
81+
gen_context_OBJECTS = gen_context.o
82+
83+
gen_%.o: src/gen_%.c
84+
$(CC_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
85+
86+
gen_context$(BUILD_EXEEXT): $(gen_context_OBJECTS)
87+
$(CC_FOR_BUILD) $^ -o $@
88+
89+
BUILT_SOURCES = src/ecmult_static_context.h
90+
91+
src/ecmult_static_context.h: gen_context
92+
./gen_context
93+
endif
94+
7795
EXTRA_DIST = autogen.sh
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# ===========================================================================
2+
# http://www.gnu.org/software/autoconf-archive/ax_prog_cc_for_build.html
3+
# ===========================================================================
4+
#
5+
# SYNOPSIS
6+
#
7+
# AX_PROG_CC_FOR_BUILD
8+
#
9+
# DESCRIPTION
10+
#
11+
# This macro searches for a C compiler that generates native executables,
12+
# that is a C compiler that surely is not a cross-compiler. This can be
13+
# useful if you have to generate source code at compile-time like for
14+
# example GCC does.
15+
#
16+
# The macro sets the CC_FOR_BUILD and CPP_FOR_BUILD macros to anything
17+
# needed to compile or link (CC_FOR_BUILD) and preprocess (CPP_FOR_BUILD).
18+
# The value of these variables can be overridden by the user by specifying
19+
# a compiler with an environment variable (like you do for standard CC).
20+
#
21+
# It also sets BUILD_EXEEXT and BUILD_OBJEXT to the executable and object
22+
# file extensions for the build platform, and GCC_FOR_BUILD to `yes' if
23+
# the compiler we found is GCC. All these variables but GCC_FOR_BUILD are
24+
# substituted in the Makefile.
25+
#
26+
# LICENSE
27+
#
28+
# Copyright (c) 2008 Paolo Bonzini <[email protected]>
29+
#
30+
# Copying and distribution of this file, with or without modification, are
31+
# permitted in any medium without royalty provided the copyright notice
32+
# and this notice are preserved. This file is offered as-is, without any
33+
# warranty.
34+
35+
#serial 8
36+
37+
AU_ALIAS([AC_PROG_CC_FOR_BUILD], [AX_PROG_CC_FOR_BUILD])
38+
AC_DEFUN([AX_PROG_CC_FOR_BUILD], [dnl
39+
AC_REQUIRE([AC_PROG_CC])dnl
40+
AC_REQUIRE([AC_PROG_CPP])dnl
41+
AC_REQUIRE([AC_EXEEXT])dnl
42+
AC_REQUIRE([AC_CANONICAL_HOST])dnl
43+
44+
dnl Use the standard macros, but make them use other variable names
45+
dnl
46+
pushdef([ac_cv_prog_CPP], ac_cv_build_prog_CPP)dnl
47+
pushdef([ac_cv_prog_gcc], ac_cv_build_prog_gcc)dnl
48+
pushdef([ac_cv_prog_cc_works], ac_cv_build_prog_cc_works)dnl
49+
pushdef([ac_cv_prog_cc_cross], ac_cv_build_prog_cc_cross)dnl
50+
pushdef([ac_cv_prog_cc_g], ac_cv_build_prog_cc_g)dnl
51+
pushdef([ac_cv_exeext], ac_cv_build_exeext)dnl
52+
pushdef([ac_cv_objext], ac_cv_build_objext)dnl
53+
pushdef([ac_exeext], ac_build_exeext)dnl
54+
pushdef([ac_objext], ac_build_objext)dnl
55+
pushdef([CC], CC_FOR_BUILD)dnl
56+
pushdef([CPP], CPP_FOR_BUILD)dnl
57+
pushdef([CFLAGS], CFLAGS_FOR_BUILD)dnl
58+
pushdef([CPPFLAGS], CPPFLAGS_FOR_BUILD)dnl
59+
pushdef([LDFLAGS], LDFLAGS_FOR_BUILD)dnl
60+
pushdef([host], build)dnl
61+
pushdef([host_alias], build_alias)dnl
62+
pushdef([host_cpu], build_cpu)dnl
63+
pushdef([host_vendor], build_vendor)dnl
64+
pushdef([host_os], build_os)dnl
65+
pushdef([ac_cv_host], ac_cv_build)dnl
66+
pushdef([ac_cv_host_alias], ac_cv_build_alias)dnl
67+
pushdef([ac_cv_host_cpu], ac_cv_build_cpu)dnl
68+
pushdef([ac_cv_host_vendor], ac_cv_build_vendor)dnl
69+
pushdef([ac_cv_host_os], ac_cv_build_os)dnl
70+
pushdef([ac_cpp], ac_build_cpp)dnl
71+
pushdef([ac_compile], ac_build_compile)dnl
72+
pushdef([ac_link], ac_build_link)dnl
73+
74+
save_cross_compiling=$cross_compiling
75+
save_ac_tool_prefix=$ac_tool_prefix
76+
cross_compiling=no
77+
ac_tool_prefix=
78+
79+
AC_PROG_CC
80+
AC_PROG_CPP
81+
AC_EXEEXT
82+
83+
ac_tool_prefix=$save_ac_tool_prefix
84+
cross_compiling=$save_cross_compiling
85+
86+
dnl Restore the old definitions
87+
dnl
88+
popdef([ac_link])dnl
89+
popdef([ac_compile])dnl
90+
popdef([ac_cpp])dnl
91+
popdef([ac_cv_host_os])dnl
92+
popdef([ac_cv_host_vendor])dnl
93+
popdef([ac_cv_host_cpu])dnl
94+
popdef([ac_cv_host_alias])dnl
95+
popdef([ac_cv_host])dnl
96+
popdef([host_os])dnl
97+
popdef([host_vendor])dnl
98+
popdef([host_cpu])dnl
99+
popdef([host_alias])dnl
100+
popdef([host])dnl
101+
popdef([LDFLAGS])dnl
102+
popdef([CPPFLAGS])dnl
103+
popdef([CFLAGS])dnl
104+
popdef([CPP])dnl
105+
popdef([CC])dnl
106+
popdef([ac_objext])dnl
107+
popdef([ac_exeext])dnl
108+
popdef([ac_cv_objext])dnl
109+
popdef([ac_cv_exeext])dnl
110+
popdef([ac_cv_prog_cc_g])dnl
111+
popdef([ac_cv_prog_cc_cross])dnl
112+
popdef([ac_cv_prog_cc_works])dnl
113+
popdef([ac_cv_prog_gcc])dnl
114+
popdef([ac_cv_prog_CPP])dnl
115+
116+
dnl Finally, set Makefile variables
117+
dnl
118+
BUILD_EXEEXT=$ac_build_exeext
119+
BUILD_OBJEXT=$ac_build_objext
120+
AC_SUBST(BUILD_EXEEXT)dnl
121+
AC_SUBST(BUILD_OBJEXT)dnl
122+
AC_SUBST([CFLAGS_FOR_BUILD])dnl
123+
AC_SUBST([CPPFLAGS_FOR_BUILD])dnl
124+
AC_SUBST([LDFLAGS_FOR_BUILD])dnl
125+
])

configure.ac

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PKG_PROG_PKG_CONFIG
1717
AC_PATH_TOOL(AR, ar)
1818
AC_PATH_TOOL(RANLIB, ranlib)
1919
AC_PATH_TOOL(STRIP, strip)
20+
AX_PROG_CC_FOR_BUILD
2021

2122
if test "x$CFLAGS" = "x"; then
2223
CFLAGS="-O3 -g"
@@ -95,6 +96,11 @@ AC_ARG_ENABLE(endomorphism,
9596
AS_HELP_STRING([--enable-endomorphism],[enable endomorphism (default is no)]),
9697
[use_endomorphism=$enableval],
9798
[use_endomorphism=no])
99+
100+
AC_ARG_ENABLE(ecmult_static_precomputation,
101+
AS_HELP_STRING([--enable-ecmult-static-precomputation],[enable precomputed ecmult table for signing (default is no)]),
102+
[use_ecmult_static_precomputation=$enableval],
103+
[use_ecmult_static_precomputation=no])
98104

99105
AC_ARG_WITH([field], [AS_HELP_STRING([--with-field=64bit|32bit|auto],
100106
[Specify Field Implementation. Default is auto])],[req_field=$withval], [req_field=auto])
@@ -305,6 +311,10 @@ if test x"$use_endomorphism" = x"yes"; then
305311
AC_DEFINE(USE_ENDOMORPHISM, 1, [Define this symbol to use endomorphism optimization])
306312
fi
307313

314+
if test x"$use_ecmult_static_precomputation" = x"yes"; then
315+
AC_DEFINE(USE_ECMULT_STATIC_PRECOMPUTATION, 1, [Define this symbol to use a statically generated ecmult table])
316+
fi
317+
308318
AC_C_BIGENDIAN()
309319

310320
AC_MSG_NOTICE([Using assembly optimizations: $set_asm])
@@ -321,6 +331,7 @@ AC_SUBST(SECP_TEST_LIBS)
321331
AC_SUBST(SECP_TEST_INCLUDES)
322332
AM_CONDITIONAL([USE_TESTS], [test x"$use_tests" != x"no"])
323333
AM_CONDITIONAL([USE_BENCHMARK], [test x"$use_benchmark" = x"yes"])
334+
AM_CONDITIONAL([USE_ECMULT_STATIC_PRECOMPUTATION], [test x"$use_ecmult_static_precomputation" = x"yes"])
324335

325336
dnl make sure nothing new is exported so that we don't break the cache
326337
PKGCONFIG_PATH_TEMP="$PKG_CONFIG_PATH"

src/basic-config.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**********************************************************************
2+
* Copyright (c) 2013, 2014 Pieter Wuille *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5+
**********************************************************************/
6+
7+
#ifndef _SECP256K1_BASIC_CONFIG_
8+
#define _SECP256K1_BASIC_CONFIG_
9+
10+
#ifdef USE_BASIC_CONFIG
11+
12+
#undef USE_ASM_X86_64
13+
#undef USE_ENDOMORPHISM
14+
#undef USE_FIELD_10X26
15+
#undef USE_FIELD_5X52
16+
#undef USE_FIELD_INV_BUILTIN
17+
#undef USE_FIELD_INV_NUM
18+
#undef USE_NUM_GMP
19+
#undef USE_NUM_NONE
20+
#undef USE_SCALAR_4X64
21+
#undef USE_SCALAR_8X32
22+
#undef USE_SCALAR_INV_BUILTIN
23+
#undef USE_SCALAR_INV_NUM
24+
25+
#define USE_NUM_NONE 1
26+
#define USE_FIELD_INV_BUILTIN 1
27+
#define USE_SCALAR_INV_BUILTIN 1
28+
#define USE_FIELD_10X26 1
29+
#define USE_SCALAR_8X32 1
30+
31+
#endif // USE_BASIC_CONFIG
32+
#endif // _SECP256K1_BASIC_CONFIG_

src/ecmult_gen_impl.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@
1111
#include "group.h"
1212
#include "ecmult_gen.h"
1313
#include "hash_impl.h"
14-
14+
#ifdef USE_ECMULT_STATIC_PRECOMPUTATION
15+
#include "ecmult_static_context.h"
16+
#endif
1517
static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context_t *ctx) {
1618
ctx->prec = NULL;
1719
}
1820

1921
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context_t *ctx) {
22+
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
2023
secp256k1_ge_t prec[1024];
2124
secp256k1_gej_t gj;
2225
secp256k1_gej_t nums_gej;
2326
int i, j;
27+
#endif
2428

2529
if (ctx->prec != NULL) {
2630
return;
2731
}
28-
32+
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
2933
ctx->prec = (secp256k1_ge_storage_t (*)[64][16])checked_malloc(sizeof(*ctx->prec));
3034

3135
/* get the generator */
@@ -75,6 +79,9 @@ static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context_t *c
7579
secp256k1_ge_to_storage(&(*ctx->prec)[j][i], &prec[j*16 + i]);
7680
}
7781
}
82+
#else
83+
ctx->prec = (secp256k1_ge_storage_t (*)[64][16])secp256k1_ecmult_static_context;
84+
#endif
7885
secp256k1_ecmult_gen_blind(ctx, NULL);
7986
}
8087

@@ -87,15 +94,21 @@ static void secp256k1_ecmult_gen_context_clone(secp256k1_ecmult_gen_context_t *d
8794
if (src->prec == NULL) {
8895
dst->prec = NULL;
8996
} else {
97+
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
9098
dst->prec = (secp256k1_ge_storage_t (*)[64][16])checked_malloc(sizeof(*dst->prec));
9199
memcpy(dst->prec, src->prec, sizeof(*dst->prec));
100+
#else
101+
dst->prec = src->prec;
102+
#endif
92103
dst->initial = src->initial;
93104
dst->blind = src->blind;
94105
}
95106
}
96107

97108
static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context_t *ctx) {
109+
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
98110
free(ctx->prec);
111+
#endif
99112
secp256k1_scalar_clear(&ctx->blind);
100113
secp256k1_gej_clear(&ctx->initial);
101114
ctx->prec = NULL;

src/field_10x26.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ typedef struct {
4343
} secp256k1_fe_storage_t;
4444

4545
#define SECP256K1_FE_STORAGE_CONST(d7, d6, d5, d4, d3, d2, d1, d0) {{ (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }}
46-
46+
#define SECP256K1_FE_STORAGE_CONST_GET(d) d.n[7], d.n[6], d.n[5], d.n[4],d.n[3], d.n[2], d.n[1], d.n[0]
4747
#endif

src/gen_context.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**********************************************************************
2+
* Copyright (c) 2013, 2014, 2015 Thomas Daede, Cory Fields *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5+
**********************************************************************/
6+
7+
#define USE_BASIC_CONFIG 1
8+
9+
#include "basic-config.h"
10+
#include "include/secp256k1.h"
11+
#include "field_impl.h"
12+
#include "scalar_impl.h"
13+
#include "group_impl.h"
14+
#include "ecmult_gen_impl.h"
15+
16+
int main(int argc, char **argv) {
17+
secp256k1_ecmult_gen_context_t ctx;
18+
int inner;
19+
int outer;
20+
FILE* fp;
21+
22+
(void)argc;
23+
(void)argv;
24+
25+
fp = fopen("src/ecmult_static_context.h","w");
26+
if (fp == NULL) {
27+
fprintf(stderr, "Could not open src/ecmult_static_context.h for writing!\n");
28+
return -1;
29+
}
30+
31+
fprintf(fp, "#ifndef _SECP256K1_ECMULT_STATIC_CONTEXT_\n");
32+
fprintf(fp, "#define _SECP256K1_ECMULT_STATIC_CONTEXT_\n");
33+
fprintf(fp, "#include \"group.h\"\n");
34+
fprintf(fp, "#define SC SECP256K1_GE_STORAGE_CONST\n");
35+
fprintf(fp, "static const secp256k1_ge_storage_t secp256k1_ecmult_static_context[64][16] = {\n");
36+
37+
secp256k1_ecmult_gen_context_init(&ctx);
38+
secp256k1_ecmult_gen_context_build(&ctx);
39+
for(outer = 0; outer != 64; outer++) {
40+
fprintf(fp,"{\n");
41+
for(inner = 0; inner != 16; inner++) {
42+
fprintf(fp," SC(%uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu, %uu)", SECP256K1_GE_STORAGE_CONST_GET((*ctx.prec)[outer][inner]));
43+
if (inner != 15) {
44+
fprintf(fp,",\n");
45+
} else {
46+
fprintf(fp,"\n");
47+
}
48+
}
49+
if (outer != 63) {
50+
fprintf(fp,"},\n");
51+
} else {
52+
fprintf(fp,"}\n");
53+
}
54+
}
55+
fprintf(fp,"};\n");
56+
secp256k1_ecmult_gen_context_clear(&ctx);
57+
58+
fprintf(fp, "#undef SC\n");
59+
fprintf(fp, "#endif\n");
60+
fclose(fp);
61+
62+
return 0;
63+
}

0 commit comments

Comments
 (0)