Skip to content

Commit f466243

Browse files
committed
Merge pull request bnb-chain#1853 from Gustav-Simonsson/libsecp256k1_update
Update libsecp256k1, Go wrapper and tests
2 parents cefe5c8 + f32fa07 commit f466243

89 files changed

Lines changed: 6536 additions & 3022 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎crypto/crypto.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
198198
return nil, fmt.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
199199
}
200200

201-
sig, err = secp256k1.Sign(hash, common.LeftPadBytes(prv.D.Bytes(), prv.Params().BitSize/8))
201+
seckey := common.LeftPadBytes(prv.D.Bytes(), prv.Params().BitSize/8)
202+
defer zeroBytes(seckey)
203+
sig, err = secp256k1.Sign(hash, seckey)
202204
return
203205
}
204206

@@ -326,3 +328,9 @@ func PubkeyToAddress(p ecdsa.PublicKey) common.Address {
326328
pubBytes := FromECDSAPub(&p)
327329
return common.BytesToAddress(Sha3(pubBytes[1:])[12:])
328330
}
331+
332+
func zeroBytes(bytes []byte) {
333+
for i := range bytes {
334+
bytes[i] = 0
335+
}
336+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
bench_inv
2+
bench_ecdh
3+
bench_sign
4+
bench_verify
5+
bench_schnorr_verify
6+
bench_recover
7+
bench_internal
8+
tests
9+
gen_context
10+
*.exe
11+
*.so
12+
*.a
13+
!.gitignore
14+
15+
Makefile
16+
configure
17+
.libs/
18+
Makefile.in
19+
aclocal.m4
20+
autom4te.cache/
21+
config.log
22+
config.status
23+
*.tar.gz
24+
*.la
25+
libtool
26+
.deps/
27+
.dirstamp
28+
build-aux/
29+
*.lo
30+
*.o
31+
*~
32+
src/libsecp256k1-config.h
33+
src/libsecp256k1-config.h.in
34+
src/ecmult_static_context.h
35+
m4/libtool.m4
36+
m4/ltoptions.m4
37+
m4/ltsugar.m4
38+
m4/ltversion.m4
39+
m4/lt~obsolete.m4
40+
src/stamp-h1
41+
libsecp256k1.pc
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
language: c
2+
sudo: false
3+
addons:
4+
apt:
5+
packages: libgmp-dev
6+
compiler:
7+
- clang
8+
- gcc
9+
env:
10+
global:
11+
- FIELD=auto BIGNUM=auto SCALAR=auto ENDOMORPHISM=no STATICPRECOMPUTATION=yes ASM=no BUILD=check EXTRAFLAGS= HOST= ECDH=no schnorr=NO RECOVERY=NO
12+
matrix:
13+
- SCALAR=32bit RECOVERY=yes
14+
- SCALAR=32bit FIELD=32bit ECDH=yes
15+
- SCALAR=64bit
16+
- FIELD=64bit RECOVERY=yes
17+
- FIELD=64bit ENDOMORPHISM=yes
18+
- FIELD=64bit ENDOMORPHISM=yes ECDH=yes
19+
- FIELD=64bit ASM=x86_64
20+
- FIELD=64bit ENDOMORPHISM=yes ASM=x86_64
21+
- FIELD=32bit SCHNORR=yes
22+
- FIELD=32bit ENDOMORPHISM=yes
23+
- BIGNUM=no
24+
- BIGNUM=no ENDOMORPHISM=yes SCHNORR=yes RECOVERY=yes
25+
- BIGNUM=no STATICPRECOMPUTATION=no
26+
- BUILD=distcheck
27+
- EXTRAFLAGS=CFLAGS=-DDETERMINISTIC
28+
matrix:
29+
fast_finish: true
30+
include:
31+
- compiler: clang
32+
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
33+
addons:
34+
apt:
35+
packages:
36+
- gcc-multilib
37+
- libgmp-dev:i386
38+
- compiler: clang
39+
env: HOST=i686-linux-gnu
40+
addons:
41+
apt:
42+
packages:
43+
- gcc-multilib
44+
- compiler: gcc
45+
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
46+
addons:
47+
apt:
48+
packages:
49+
- gcc-multilib
50+
- compiler: gcc
51+
env: HOST=i686-linux-gnu
52+
addons:
53+
apt:
54+
packages:
55+
- gcc-multilib
56+
- libgmp-dev:i386
57+
before_script: ./autogen.sh
58+
script:
59+
- if [ -n "$HOST" ]; then export USE_HOST="--host=$HOST"; fi
60+
- if [ "x$HOST" = "xi686-linux-gnu" ]; then export CC="$CC -m32"; fi
61+
- ./configure --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM --with-scalar=$SCALAR --enable-ecmult-static-precomputation=$STATICPRECOMPUTATION --enable-module-ecdh=$ECDH --enable-module-schnorr=$SCHNORR $EXTRAFLAGS $USE_HOST && make -j2 $BUILD
62+
os: linux
Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ noinst_HEADERS += src/eckey.h
1919
noinst_HEADERS += src/eckey_impl.h
2020
noinst_HEADERS += src/ecmult.h
2121
noinst_HEADERS += src/ecmult_impl.h
22+
noinst_HEADERS += src/ecmult_const.h
23+
noinst_HEADERS += src/ecmult_const_impl.h
2224
noinst_HEADERS += src/ecmult_gen.h
2325
noinst_HEADERS += src/ecmult_gen_impl.h
2426
noinst_HEADERS += src/num.h
@@ -43,19 +45,16 @@ pkgconfigdir = $(libdir)/pkgconfig
4345
pkgconfig_DATA = libsecp256k1.pc
4446

4547
libsecp256k1_la_SOURCES = src/secp256k1.c
46-
libsecp256k1_la_CPPFLAGS = -I$(top_srcdir)/include $(SECP_INCLUDES)
48+
libsecp256k1_la_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src $(SECP_INCLUDES)
4749
libsecp256k1_la_LIBADD = $(SECP_LIBS)
4850

4951

5052
noinst_PROGRAMS =
5153
if USE_BENCHMARK
52-
noinst_PROGRAMS += bench_verify bench_recover bench_sign bench_internal
54+
noinst_PROGRAMS += bench_verify bench_sign bench_internal
5355
bench_verify_SOURCES = src/bench_verify.c
5456
bench_verify_LDADD = libsecp256k1.la $(SECP_LIBS)
5557
bench_verify_LDFLAGS = -static
56-
bench_recover_SOURCES = src/bench_recover.c
57-
bench_recover_LDADD = libsecp256k1.la $(SECP_LIBS)
58-
bench_recover_LDFLAGS = -static
5958
bench_sign_SOURCES = src/bench_sign.c
6059
bench_sign_LDADD = libsecp256k1.la $(SECP_LIBS)
6160
bench_sign_LDFLAGS = -static
@@ -68,10 +67,44 @@ endif
6867
if USE_TESTS
6968
noinst_PROGRAMS += tests
7069
tests_SOURCES = src/tests.c
71-
tests_CPPFLAGS = -DVERIFY $(SECP_INCLUDES) $(SECP_TEST_INCLUDES)
70+
tests_CPPFLAGS = -DVERIFY -I$(top_srcdir)/src $(SECP_INCLUDES) $(SECP_TEST_INCLUDES)
7271
tests_LDADD = $(SECP_LIBS) $(SECP_TEST_LIBS)
7372
tests_LDFLAGS = -static
7473
TESTS = tests
7574
endif
7675

77-
EXTRA_DIST = autogen.sh
76+
if USE_ECMULT_STATIC_PRECOMPUTATION
77+
CPPFLAGS_FOR_BUILD +=-I$(top_srcdir)/
78+
CFLAGS_FOR_BUILD += -Wall -Wextra -Wno-unused-function
79+
80+
gen_context_OBJECTS = gen_context.o
81+
gen_context_BIN = gen_context$(BUILD_EXEEXT)
82+
gen_%.o: src/gen_%.c
83+
$(CC_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
84+
85+
$(gen_context_BIN): $(gen_context_OBJECTS)
86+
$(CC_FOR_BUILD) $^ -o $@
87+
88+
$(libsecp256k1_la_OBJECTS): src/ecmult_static_context.h
89+
$(tests_OBJECTS): src/ecmult_static_context.h
90+
$(bench_internal_OBJECTS): src/ecmult_static_context.h
91+
92+
src/ecmult_static_context.h: $(gen_context_BIN)
93+
./$(gen_context_BIN)
94+
95+
CLEANFILES = $(gen_context_BIN) src/ecmult_static_context.h
96+
endif
97+
98+
EXTRA_DIST = autogen.sh src/gen_context.c src/basic-config.h
99+
100+
if ENABLE_MODULE_ECDH
101+
include src/modules/ecdh/Makefile.am.include
102+
endif
103+
104+
if ENABLE_MODULE_SCHNORR
105+
include src/modules/schnorr/Makefile.am.include
106+
endif
107+
108+
if ENABLE_MODULE_RECOVERY
109+
include src/modules/recovery/Makefile.am.include
110+
endif

crypto/secp256k1/secp256k1/configure.ac renamed to crypto/secp256k1/libsecp256k1/configure.ac

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,19 @@ 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"
2324
fi
2425

26+
AM_PROG_CC_C_O
27+
2528
AC_PROG_CC_C89
2629
if test x"$ac_cv_prog_cc_c89" = x"no"; then
2730
AC_MSG_ERROR([c89 compiler support required])
2831
fi
2932

30-
case $host in
31-
*mingw*)
32-
use_pkgconfig=no
33-
;;
34-
*)
35-
use_pkgconfig=yes
36-
;;
37-
esac
38-
3933
case $host_os in
4034
*darwin*)
4135
if test x$cross_compiling != xyes; then
@@ -80,6 +74,14 @@ AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
8074
CFLAGS="$saved_CFLAGS"
8175
])
8276

77+
saved_CFLAGS="$CFLAGS"
78+
CFLAGS="$CFLAGS -fvisibility=hidden"
79+
AC_MSG_CHECKING([if ${CC} supports -fvisibility=hidden])
80+
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
81+
[ AC_MSG_RESULT([yes]) ],
82+
[ AC_MSG_RESULT([no])
83+
CFLAGS="$saved_CFLAGS"
84+
])
8385

8486
AC_ARG_ENABLE(benchmark,
8587
AS_HELP_STRING([--enable-benchmark],[compile benchmark (default is no)]),
@@ -95,6 +97,26 @@ AC_ARG_ENABLE(endomorphism,
9597
AS_HELP_STRING([--enable-endomorphism],[enable endomorphism (default is no)]),
9698
[use_endomorphism=$enableval],
9799
[use_endomorphism=no])
100+
101+
AC_ARG_ENABLE(ecmult_static_precomputation,
102+
AS_HELP_STRING([--enable-ecmult-static-precomputation],[enable precomputed ecmult table for signing (default is yes)]),
103+
[use_ecmult_static_precomputation=$enableval],
104+
[use_ecmult_static_precomputation=yes])
105+
106+
AC_ARG_ENABLE(module_ecdh,
107+
AS_HELP_STRING([--enable-module-ecdh],[enable ECDH shared secret computation (default is no)]),
108+
[enable_module_ecdh=$enableval],
109+
[enable_module_ecdh=no])
110+
111+
AC_ARG_ENABLE(module_schnorr,
112+
AS_HELP_STRING([--enable-module-schnorr],[enable Schnorr signature module (default is no)]),
113+
[enable_module_schnorr=$enableval],
114+
[enable_module_schnorr=no])
115+
116+
AC_ARG_ENABLE(module_recovery,
117+
AS_HELP_STRING([--enable-module-recovery],[enable ECDSA pubkey recovery module (default is no)]),
118+
[enable_module_recovery=$enableval],
119+
[enable_module_recovery=no])
98120

99121
AC_ARG_WITH([field], [AS_HELP_STRING([--with-field=64bit|32bit|auto],
100122
[Specify Field Implementation. Default is auto])],[req_field=$withval], [req_field=auto])
@@ -305,13 +327,33 @@ if test x"$use_endomorphism" = x"yes"; then
305327
AC_DEFINE(USE_ENDOMORPHISM, 1, [Define this symbol to use endomorphism optimization])
306328
fi
307329

330+
if test x"$use_ecmult_static_precomputation" = x"yes"; then
331+
AC_DEFINE(USE_ECMULT_STATIC_PRECOMPUTATION, 1, [Define this symbol to use a statically generated ecmult table])
332+
fi
333+
334+
if test x"$enable_module_ecdh" = x"yes"; then
335+
AC_DEFINE(ENABLE_MODULE_ECDH, 1, [Define this symbol to enable the ECDH module])
336+
fi
337+
338+
if test x"$enable_module_schnorr" = x"yes"; then
339+
AC_DEFINE(ENABLE_MODULE_SCHNORR, 1, [Define this symbol to enable the Schnorr signature module])
340+
fi
341+
342+
if test x"$enable_module_recovery" = x"yes"; then
343+
AC_DEFINE(ENABLE_MODULE_RECOVERY, 1, [Define this symbol to enable the ECDSA pubkey recovery module])
344+
fi
345+
308346
AC_C_BIGENDIAN()
309347

310348
AC_MSG_NOTICE([Using assembly optimizations: $set_asm])
311349
AC_MSG_NOTICE([Using field implementation: $set_field])
312350
AC_MSG_NOTICE([Using bignum implementation: $set_bignum])
313351
AC_MSG_NOTICE([Using scalar implementation: $set_scalar])
314352
AC_MSG_NOTICE([Using endomorphism optimizations: $use_endomorphism])
353+
AC_MSG_NOTICE([Building ECDH module: $enable_module_ecdh])
354+
355+
AC_MSG_NOTICE([Building Schnorr signatures module: $enable_module_schnorr])
356+
AC_MSG_NOTICE([Building ECDSA pubkey recovery module: $enable_module_recovery])
315357

316358
AC_CONFIG_HEADERS([src/libsecp256k1-config.h])
317359
AC_CONFIG_FILES([Makefile libsecp256k1.pc])
@@ -321,6 +363,10 @@ AC_SUBST(SECP_TEST_LIBS)
321363
AC_SUBST(SECP_TEST_INCLUDES)
322364
AM_CONDITIONAL([USE_TESTS], [test x"$use_tests" != x"no"])
323365
AM_CONDITIONAL([USE_BENCHMARK], [test x"$use_benchmark" = x"yes"])
366+
AM_CONDITIONAL([USE_ECMULT_STATIC_PRECOMPUTATION], [test x"$use_ecmult_static_precomputation" = x"yes"])
367+
AM_CONDITIONAL([ENABLE_MODULE_ECDH], [test x"$enable_module_ecdh" = x"yes"])
368+
AM_CONDITIONAL([ENABLE_MODULE_SCHNORR], [test x"$enable_module_schnorr" = x"yes"])
369+
AM_CONDITIONAL([ENABLE_MODULE_RECOVERY], [test x"$enable_module_recovery" = x"yes"])
324370

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

0 commit comments

Comments
 (0)