Skip to content

Commit 8903a1a

Browse files
committed
Update src/secp256k1 subtree
2 parents 9ad6f14 + 67f232b commit 8903a1a

Some content is hidden

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

63 files changed

+2392
-2436
lines changed

src/secp256k1/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bench_internal
99
tests
1010
exhaustive_tests
1111
gen_context
12+
valgrind_ctime_test
1213
*.exe
1314
*.so
1415
*.a

src/secp256k1/.travis.yml

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
language: c
2-
os: linux
2+
os:
3+
- linux
4+
- osx
5+
6+
dist: bionic
7+
# Valgrind currently supports upto macOS 10.13, the latest xcode of that version is 10.1
8+
osx_image: xcode10.1
39
addons:
410
apt:
5-
packages: libgmp-dev
11+
packages:
12+
- libgmp-dev
13+
- valgrind
14+
- libtool-bin
615
compiler:
716
- clang
817
- gcc
9-
cache:
10-
directories:
11-
- src/java/guava/
1218
env:
1319
global:
14-
- FIELD=auto BIGNUM=auto SCALAR=auto ENDOMORPHISM=no STATICPRECOMPUTATION=yes ASM=no BUILD=check EXTRAFLAGS= HOST= ECDH=no RECOVERY=no EXPERIMENTAL=no JNI=no
15-
- GUAVA_URL=https://search.maven.org/remotecontent?filepath=com/google/guava/guava/18.0/guava-18.0.jar GUAVA_JAR=src/java/guava/guava-18.0.jar
20+
- FIELD=auto BIGNUM=auto SCALAR=auto ENDOMORPHISM=no STATICPRECOMPUTATION=yes ECMULTGENPRECISION=auto ASM=no BUILD=check EXTRAFLAGS= HOST= ECDH=no RECOVERY=no EXPERIMENTAL=no CTIMETEST=yes BENCH=yes ITERS=2
1621
matrix:
1722
- SCALAR=32bit RECOVERY=yes
1823
- SCALAR=32bit FIELD=32bit ECDH=yes EXPERIMENTAL=yes
@@ -26,43 +31,78 @@ env:
2631
- BIGNUM=no
2732
- BIGNUM=no ENDOMORPHISM=yes RECOVERY=yes EXPERIMENTAL=yes
2833
- BIGNUM=no STATICPRECOMPUTATION=no
29-
- BUILD=distcheck
30-
- EXTRAFLAGS=CPPFLAGS=-DDETERMINISTIC
31-
- EXTRAFLAGS=CFLAGS=-O0
32-
- BUILD=check-java JNI=yes ECDH=yes EXPERIMENTAL=yes
34+
- BUILD=distcheck CTIMETEST= BENCH=
35+
- CPPFLAGS=-DDETERMINISTIC
36+
- CFLAGS=-O0 CTIMETEST=
37+
- ECMULTGENPRECISION=2
38+
- ECMULTGENPRECISION=8
39+
- VALGRIND=yes ENDOMORPHISM=yes BIGNUM=no ASM=x86_64 EXPERIMENTAL=yes ECDH=yes RECOVERY=yes EXTRAFLAGS="--disable-openssl-tests" CPPFLAGS=-DVALGRIND BUILD=
40+
- VALGRIND=yes BIGNUM=no ASM=x86_64 EXPERIMENTAL=yes ECDH=yes RECOVERY=yes EXTRAFLAGS="--disable-openssl-tests" CPPFLAGS=-DVALGRIND BUILD=
3341
matrix:
3442
fast_finish: true
3543
include:
3644
- compiler: clang
45+
os: linux
3746
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
3847
addons:
3948
apt:
4049
packages:
4150
- gcc-multilib
4251
- libgmp-dev:i386
52+
- valgrind
53+
- libtool-bin
54+
- libc6-dbg:i386
4355
- compiler: clang
4456
env: HOST=i686-linux-gnu
57+
os: linux
4558
addons:
4659
apt:
4760
packages:
4861
- gcc-multilib
62+
- valgrind
63+
- libtool-bin
64+
- libc6-dbg:i386
4965
- compiler: gcc
5066
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
67+
os: linux
5168
addons:
5269
apt:
5370
packages:
5471
- gcc-multilib
72+
- valgrind
73+
- libtool-bin
74+
- libc6-dbg:i386
5575
- compiler: gcc
76+
os: linux
5677
env: HOST=i686-linux-gnu
5778
addons:
5879
apt:
5980
packages:
6081
- gcc-multilib
6182
- libgmp-dev:i386
62-
before_install: mkdir -p `dirname $GUAVA_JAR`
63-
install: if [ ! -f $GUAVA_JAR ]; then wget $GUAVA_URL -O $GUAVA_JAR; fi
83+
- valgrind
84+
- libtool-bin
85+
- libc6-dbg:i386
86+
87+
# We use this to install macOS dependencies instead of the built in `homebrew` plugin,
88+
# because in xcode earlier than 11 they have a bug requiring updating the system which overall takes ~8 minutes.
89+
# https://travis-ci.community/t/macos-build-fails-because-of-homebrew-bundle-unknown-command/7296
90+
before_install:
91+
- if [ "${TRAVIS_OS_NAME}" = "osx" ]; then HOMEBREW_NO_AUTO_UPDATE=1 brew install gmp valgrind gcc@9; fi
92+
6493
before_script: ./autogen.sh
94+
95+
# travis auto terminates jobs that go for 10 minutes without printing to stdout, but travis_wait doesn't work well with forking programs like valgrind (https://docs.travis-ci.com/user/common-build-problems/#build-times-out-because-no-output-was-received https://github.com/bitcoin-core/secp256k1/pull/750#issuecomment-623476860)
6596
script:
66-
- if [ -n "$HOST" ]; then export USE_HOST="--host=$HOST"; fi
67-
- if [ "x$HOST" = "xi686-linux-gnu" ]; then export CC="$CC -m32"; fi
68-
- ./configure --enable-experimental=$EXPERIMENTAL --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM --with-scalar=$SCALAR --enable-ecmult-static-precomputation=$STATICPRECOMPUTATION --enable-module-ecdh=$ECDH --enable-module-recovery=$RECOVERY --enable-jni=$JNI $EXTRAFLAGS $USE_HOST && make -j2 $BUILD
97+
- function keep_alive() { while true; do echo -en "\a"; sleep 60; done }
98+
- keep_alive &
99+
- ./contrib/travis.sh
100+
- kill %keep_alive
101+
102+
after_script:
103+
- cat ./tests.log
104+
- cat ./exhaustive_tests.log
105+
- cat ./valgrind_ctime_test.log
106+
- cat ./bench.log
107+
- $CC --version
108+
- valgrind --version

src/secp256k1/Makefile.am

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
ACLOCAL_AMFLAGS = -I build-aux/m4
22

33
lib_LTLIBRARIES = libsecp256k1.la
4-
if USE_JNI
5-
JNI_LIB = libsecp256k1_jni.la
6-
noinst_LTLIBRARIES = $(JNI_LIB)
7-
else
8-
JNI_LIB =
9-
endif
104
include_HEADERS = include/secp256k1.h
5+
include_HEADERS += include/secp256k1_preallocated.h
116
noinst_HEADERS =
127
noinst_HEADERS += src/scalar.h
138
noinst_HEADERS += src/scalar_4x64.h
@@ -39,8 +34,6 @@ noinst_HEADERS += src/field_5x52.h
3934
noinst_HEADERS += src/field_5x52_impl.h
4035
noinst_HEADERS += src/field_5x52_int128_impl.h
4136
noinst_HEADERS += src/field_5x52_asm_impl.h
42-
noinst_HEADERS += src/java/org_bitcoin_NativeSecp256k1.h
43-
noinst_HEADERS += src/java/org_bitcoin_Secp256k1Context.h
4437
noinst_HEADERS += src/util.h
4538
noinst_HEADERS += src/scratch.h
4639
noinst_HEADERS += src/scratch_impl.h
@@ -74,16 +67,19 @@ endif
7467

7568
libsecp256k1_la_SOURCES = src/secp256k1.c
7669
libsecp256k1_la_CPPFLAGS = -DSECP256K1_BUILD -I$(top_srcdir)/include -I$(top_srcdir)/src $(SECP_INCLUDES)
77-
libsecp256k1_la_LIBADD = $(JNI_LIB) $(SECP_LIBS) $(COMMON_LIB)
70+
libsecp256k1_la_LIBADD = $(SECP_LIBS) $(COMMON_LIB)
7871

79-
libsecp256k1_jni_la_SOURCES = src/java/org_bitcoin_NativeSecp256k1.c src/java/org_bitcoin_Secp256k1Context.c
80-
libsecp256k1_jni_la_CPPFLAGS = -DSECP256K1_BUILD $(JNI_INCLUDES)
72+
if VALGRIND_ENABLED
73+
libsecp256k1_la_CPPFLAGS += -DVALGRIND
74+
endif
8175

8276
noinst_PROGRAMS =
8377
if USE_BENCHMARK
8478
noinst_PROGRAMS += bench_verify bench_sign bench_internal bench_ecmult
8579
bench_verify_SOURCES = src/bench_verify.c
8680
bench_verify_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
81+
# SECP_TEST_INCLUDES are only used here for CRYPTO_CPPFLAGS
82+
bench_verify_CPPFLAGS = -DSECP256K1_BUILD $(SECP_TEST_INCLUDES)
8783
bench_sign_SOURCES = src/bench_sign.c
8884
bench_sign_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
8985
bench_internal_SOURCES = src/bench_internal.c
@@ -99,6 +95,12 @@ if USE_TESTS
9995
noinst_PROGRAMS += tests
10096
tests_SOURCES = src/tests.c
10197
tests_CPPFLAGS = -DSECP256K1_BUILD -I$(top_srcdir)/src -I$(top_srcdir)/include $(SECP_INCLUDES) $(SECP_TEST_INCLUDES)
98+
if VALGRIND_ENABLED
99+
tests_CPPFLAGS += -DVALGRIND
100+
noinst_PROGRAMS += valgrind_ctime_test
101+
valgrind_ctime_test_SOURCES = src/valgrind_ctime_test.c
102+
valgrind_ctime_test_LDADD = libsecp256k1.la $(SECP_LIBS) $(SECP_TEST_LIBS) $(COMMON_LIB)
103+
endif
102104
if !ENABLE_COVERAGE
103105
tests_CPPFLAGS += -DVERIFY
104106
endif
@@ -119,42 +121,12 @@ exhaustive_tests_LDFLAGS = -static
119121
TESTS += exhaustive_tests
120122
endif
121123

122-
JAVAROOT=src/java
123-
JAVAORG=org/bitcoin
124-
JAVA_GUAVA=$(srcdir)/$(JAVAROOT)/guava/guava-18.0.jar
125-
CLASSPATH_ENV=CLASSPATH=$(JAVA_GUAVA)
126-
JAVA_FILES= \
127-
$(JAVAROOT)/$(JAVAORG)/NativeSecp256k1.java \
128-
$(JAVAROOT)/$(JAVAORG)/NativeSecp256k1Test.java \
129-
$(JAVAROOT)/$(JAVAORG)/NativeSecp256k1Util.java \
130-
$(JAVAROOT)/$(JAVAORG)/Secp256k1Context.java
131-
132-
if USE_JNI
133-
134-
$(JAVA_GUAVA):
135-
@echo Guava is missing. Fetch it via: \
136-
wget https://search.maven.org/remotecontent?filepath=com/google/guava/guava/18.0/guava-18.0.jar -O $(@)
137-
@false
138-
139-
.stamp-java: $(JAVA_FILES)
140-
@echo Compiling $^
141-
$(AM_V_at)$(CLASSPATH_ENV) javac $^
142-
@touch $@
143-
144-
if USE_TESTS
145-
146-
check-java: libsecp256k1.la $(JAVA_GUAVA) .stamp-java
147-
$(AM_V_at)java -Djava.library.path="./:./src:./src/.libs:.libs/" -cp "$(JAVA_GUAVA):$(JAVAROOT)" $(JAVAORG)/NativeSecp256k1Test
148-
149-
endif
150-
endif
151-
152124
if USE_ECMULT_STATIC_PRECOMPUTATION
153-
CPPFLAGS_FOR_BUILD +=-I$(top_srcdir)
125+
CPPFLAGS_FOR_BUILD +=-I$(top_srcdir) -I$(builddir)/src
154126

155127
gen_context_OBJECTS = gen_context.o
156128
gen_context_BIN = gen_context$(BUILD_EXEEXT)
157-
gen_%.o: src/gen_%.c
129+
gen_%.o: src/gen_%.c src/libsecp256k1-config.h
158130
$(CC_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
159131

160132
$(gen_context_BIN): $(gen_context_OBJECTS)
@@ -168,10 +140,10 @@ $(bench_ecmult_OBJECTS): src/ecmult_static_context.h
168140
src/ecmult_static_context.h: $(gen_context_BIN)
169141
./$(gen_context_BIN)
170142

171-
CLEANFILES = $(gen_context_BIN) src/ecmult_static_context.h $(JAVAROOT)/$(JAVAORG)/*.class .stamp-java
143+
CLEANFILES = $(gen_context_BIN) src/ecmult_static_context.h
172144
endif
173145

174-
EXTRA_DIST = autogen.sh src/gen_context.c src/basic-config.h $(JAVA_FILES)
146+
EXTRA_DIST = autogen.sh src/gen_context.c src/basic-config.h
175147

176148
if ENABLE_MODULE_ECDH
177149
include src/modules/ecdh/Makefile.am.include

src/secp256k1/README.md

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ libsecp256k1
33

44
[![Build Status](https://travis-ci.org/bitcoin-core/secp256k1.svg?branch=master)](https://travis-ci.org/bitcoin-core/secp256k1)
55

6-
Optimized C library for EC operations on curve secp256k1.
6+
Optimized C library for ECDSA signatures and secret/public key operations on curve secp256k1.
77

8-
This library is a work in progress and is being used to research best practices. Use at your own risk.
8+
This library is intended to be the highest quality publicly available library for cryptography on the secp256k1 curve. However, the primary focus of its development has been for usage in the Bitcoin system and usage unlike Bitcoin's may be less well tested, verified, or suffer from a less well thought out interface. Correct usage requires some care and consideration that the library is fit for your application's purpose.
99

1010
Features:
1111
* secp256k1 ECDSA signing/verification and key generation.
12-
* Adding/multiplying private/public keys.
13-
* Serialization/parsing of private keys, public keys, signatures.
14-
* Constant time, constant memory access signing and pubkey generation.
15-
* Derandomized DSA (via RFC6979 or with a caller provided function.)
12+
* Additive and multiplicative tweaking of secret/public keys.
13+
* Serialization/parsing of secret keys, public keys, signatures.
14+
* Constant time, constant memory access signing and public key generation.
15+
* Derandomized ECDSA (via RFC6979 or with a caller provided function.)
1616
* Very efficient implementation.
17+
* Suitable for embedded systems.
18+
* Optional module for public key recovery.
19+
* Optional module for ECDH key exchange (experimental).
20+
21+
Experimental features have not received enough scrutiny to satisfy the standard of quality of this library but are made available for testing and review by the community. The APIs of these features should not be considered stable.
1722

1823
Implementation details
1924
----------------------
@@ -23,11 +28,12 @@ Implementation details
2328
* Extensive testing infrastructure.
2429
* Structured to facilitate review and analysis.
2530
* Intended to be portable to any system with a C89 compiler and uint64_t support.
31+
* No use of floating types.
2632
* Expose only higher level interfaces to minimize the API surface and improve application security. ("Be difficult to use insecurely.")
2733
* Field operations
2834
* Optimized implementation of arithmetic modulo the curve's field size (2^256 - 0x1000003D1).
2935
* Using 5 52-bit limbs (including hand-optimized assembly for x86_64, by Diederik Huys).
30-
* Using 10 26-bit limbs.
36+
* Using 10 26-bit limbs (including hand-optimized assembly for 32-bit ARM, by Wladimir J. van der Laan).
3137
* Field inverses and square roots using a sliding window over blocks of 1s (by Peter Dettman).
3238
* Scalar operations
3339
* Optimized implementation without data-dependent branches of arithmetic modulo the curve's order.
@@ -45,9 +51,11 @@ Implementation details
4551
* Optionally (off by default) use secp256k1's efficiently-computable endomorphism to split the P multiplicand into 2 half-sized ones.
4652
* Point multiplication for signing
4753
* Use a precomputed table of multiples of powers of 16 multiplied with the generator, so general multiplication becomes a series of additions.
48-
* Access the table with branch-free conditional moves so memory access is uniform.
49-
* No data-dependent branches
50-
* The precomputed tables add and eventually subtract points for which no known scalar (private key) is known, preventing even an attacker with control over the private key used to control the data internally.
54+
* Intended to be completely free of timing sidechannels for secret-key operations (on reasonable hardware/toolchains)
55+
* Access the table with branch-free conditional moves so memory access is uniform.
56+
* No data-dependent branches
57+
* Optional runtime blinding which attempts to frustrate differential power analysis.
58+
* The precomputed tables add and eventually subtract points for which no known scalar (secret key) is known, preventing even an attacker with control over the secret key used to control the data internally.
5159

5260
Build steps
5361
-----------
@@ -57,5 +65,40 @@ libsecp256k1 is built using autotools:
5765
$ ./autogen.sh
5866
$ ./configure
5967
$ make
60-
$ ./tests
68+
$ make check
6169
$ sudo make install # optional
70+
71+
Exhaustive tests
72+
-----------
73+
74+
$ ./exhaustive_tests
75+
76+
With valgrind, you might need to increase the max stack size:
77+
78+
$ valgrind --max-stackframe=2500000 ./exhaustive_tests
79+
80+
Test coverage
81+
-----------
82+
83+
This library aims to have full coverage of the reachable lines and branches.
84+
85+
To create a test coverage report, configure with `--enable-coverage` (use of GCC is necessary):
86+
87+
$ ./configure --enable-coverage
88+
89+
Run the tests:
90+
91+
$ make check
92+
93+
To create a report, `gcovr` is recommended, as it includes branch coverage reporting:
94+
95+
$ gcovr --exclude 'src/bench*' --print-summary
96+
97+
To create a HTML report with coloured and annotated source code:
98+
99+
$ gcovr --exclude 'src/bench*' --html --html-details -o coverage.html
100+
101+
Reporting a vulnerability
102+
------------
103+
104+
See [SECURITY.md](SECURITY.md)

src/secp256k1/SECURITY.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Security Policy
2+
3+
## Reporting a Vulnerability
4+
5+
To report security issues send an email to [email protected] (not for support).
6+
7+
The following keys may be used to communicate sensitive information to developers:
8+
9+
| Name | Fingerprint |
10+
|------|-------------|
11+
| Pieter Wuille | 133E AC17 9436 F14A 5CF1 B794 860F EB80 4E66 9320 |
12+
| Andrew Poelstra | 699A 63EF C17A D3A9 A34C FFC0 7AD0 A91C 40BD 0091 |
13+
| Tim Ruffing | 09E0 3F87 1092 E40E 106E 902B 33BC 86AB 80FF 5516 |
14+
15+
You can import a key by running the following command with that individual’s fingerprint: `gpg --recv-keys "<fingerprint>"` Ensure that you put quotes around fingerprints containing spaces.

0 commit comments

Comments
 (0)