Skip to content

Commit ef71171

Browse files
committed
Squashed 'src/secp256k1/' changes from b19c000..ccb23e329d
ccb23e329d valgrind_ctime_test: Add schnorrsig_sign 682a113f3a schnorrsig: Add taproot test case a0a01a4915 schnorrsig: Add benchmark for sign and verify fa5b11cbb4 schnorrsig: Add BIP-340 compatible signing and verification bb888e4f63 schnorrsig: Add BIP-340 nonce function 07963e8843 schnorrsig: Init empty experimental module 87d98a4756 Allow initializing tagged sha256 6f4d0d86a9 extrakeys: Add keypair_xonly_tweak_add 37be855167 extrakeys: Add keypair struct with create, pub and pub_xonly 96ed0dd9d3 Separate helper functions for pubkey_create and seckey_tweak_add 7a3acdcef9 extrakeys: Add xonly_pubkey_tweak_add & xonly_pubkey_tweak_add_test 6d6f3e5fdf Separate helper function for ec_pubkey_tweak_add 4b2febf8fc extrakeys: Add xonly_pubkey with serialize, parse and from_pubkey d2d45afc16 extrakeys: Init empty experimental module 8f247275a0 Make the secp256k1_declassify argument constant f39f99b Merge bitcoin#701: Make ec_ arithmetic more consistent and add documentation 39198a0 Merge bitcoin#732: Retry if r is zero during signing 59a8de8 Merge bitcoin#742: Fix typo in ecmult_const_impl.h 4e28465 Fix typo in ecmult_const_impl.h f862b4c Merge bitcoin#740: Make recovery/main_impl.h non-executable ffef45c Make recovery/main_impl.h non-executable 2361b37 Merge bitcoin#735: build: fix OpenSSL EC detection on macOS 3b7d26b build: add SECP_TEST_INCLUDES to bench_verify CPPFLAGS 84b5fc5 build: fix OpenSSL EC detection on macOS 37ed51a Make ecdsa_sig_sign constant-time again after reverting 25e3cfb 93d343b Revert "ecdsa_impl: replace scalar if-checks with VERIFY_CHECKs in ecdsa_sig_sign" 7e3952a Clarify documentation of tweak functions. 89853a0 Make tweak function documentation more consistent. 41fc785 Make ec_privkey functions aliases for ec_seckey_negate, ec_seckey_tweak_add and ec_seckey_mul 22911ee Rename private key to secret key in public API (with the exception of function names) 5a73f14 Mention that value is unspecified for In/Out parameters if the function returns 0 f03df0e Define valid ECDSA keys in the documentation of seckey_verify 5894e1f Return 0 if the given seckey is invalid in privkey_negate, privkey_tweak_add and privkey_tweak_mul 8f814cd Add test for boundary conditions of scalar_set_b32 with respect to overflows 3fec982 Use scalar_set_b32_seckey in ecdsa_sign, pubkey_create and seckey_verify 9ab2cbe Add scalar_set_b32_seckey which does the same as scalar_set_b32 and also returns whether it's a valid secret key 4f27e34 Merge bitcoin#728: Suppress a harmless variable-time optimization by clang in memczero 0199387 Add test for memczero() 52a0351 Suppress a harmless variable-time optimization by clang in memczero 8f78e20 Merge bitcoin#722: Context isn't freed in the ECDH benchmark ed1b911 Merge bitcoin#700: Allow overriding default flags 85b35af Add running benchmarks regularly and under valgrind in travis ca4906b Pass num of iters to benchmarks as variable, and define envvar 02dd5f1 free the ctx at the end of bench_ecdh e9fccd4 Merge bitcoin#708: Constant-time behaviour test using valgrind memtest. 08fb6c4 Run valgrind_ctime_test in travis 3d23022 Constant-time behaviour test using valgrind memtest. 96d8ccb Merge bitcoin#710: Eliminate harmless non-constant time operations on secret data. 0585b8b Merge bitcoin#718: Clarify that a secp256k1_ecdh_hash_function must return 0 or 1 7b50483 Adds a declassify operation to aid constant-time analysis. 34a67c7 Eliminate harmless non-constant time operations on secret data. ca739cb Compile with optimization flag -O2 by default instead of -O3 eb45ef3 Clarify that a secp256k1_ecdh_hash_function must return 0 or 1 856a01d Merge bitcoin#714: doc: document the length requirements of output parameter. d72b9e2 Merge bitcoin#682: Remove Java Native Interface 4b48a43 doc: document the length requirements of output parameter. 1b4d256 Merge bitcoin#713: Docstrings dabfea7 field: extend docstring of secp256k1_fe_normalize dc7d8fd scalar: extend docstring of secp256k1_scalar_set_b32 074ab58 Merge bitcoin#704: README: add a section for test coverage acb7f97 README: add a section for test coverage 227a4f2 Merge bitcoin#709: Remove secret-dependant non-constant time operation in ecmult_const. d567b77 Clarify comments about use of rzr on ge functions and abs function. 2241ae6 Remove secret-dependant non-constant time operation in ecmult_const. 642cd06 Remove Java Native Interface 83fb1bc Remove -O2 from default CFLAGS because this would override the -O3 flag (see AC_PROG_CC in the Autoconf manual) ecba813 Append instead of Prepend user-CFLAGS to default CFLAGS allowing the user to override default variables 613c34c Remove test in configure.ac because it doesn't have an effect f45d897 Merge bitcoin#703: Overhaul README.md 2e759ec Overhaul README.md d644dda Merge bitcoin#689: Remove "except in benchmarks" exception for fp math bde2a32 Convert bench.h to fixed-point math 387d723 Merge bitcoin#679: Add SECURITY.md 0db61d2 Merge bitcoin#685: Fix issue where travis does not show the ./tests seed… a0771d1 Explicitly disable buffering for stderr in tests fb424fb Make travis show the ./tests seed by removing stdout buffering and always cat tests.log after a travis run. 22a6031 Merge bitcoin#690: Add valgrind check to travis 544002c Merge bitcoin#678: Preventing compiler optimizations in benchmarks without a memory fence dd98cc9 travis: Added a valgrind test without endro and enabled recovery+ecdh b4c1382 Add valgrind check to travis 0c774d8 Merge bitcoin#688: Fix ASM setting in travis 5c5f71e Fix ASM setting in travis e2625f8 Merge bitcoin#684: Make no-float policy explicit bae1bea Make no-float policy explicit 78c3836 Add SECURITY.md 362bb25 Modified bench_scalar_split so it won't get optimized out 73a30c6 Added accumulators and checks on benchmarks so they won't get optimized out 770b3dc Merge bitcoin#677: Remove note about heap allocation in secp256k1_ecmult_odd_multiples_table_storage_var b76142f Remove note about heap allocation in secp256k1_ecmult_odd_multiples_table_storage_var which was removed in 4704527 137d304 Merge bitcoin#647: Increase robustness against UB in secp256k1_scalar_cadd_bit 0d9540b Merge bitcoin#664: Remove mention of ec_privkey_export because it doesn't exist 59782c6 Remove mention of ec_privkey_export because it doesn't exist 96cd94e Merge bitcoin#337: variable sized precomputed table for signing dcb2e3b variable signing precompute table b4bff99 Merge bitcoin#661: Make ./configure string consistent a467047 Make ./configure string consistent e729cc7 Merge bitcoin#657: Fix a nit in the recovery tests b64a2e2 Fix a nit in the recovery tests e028aa3 Merge bitcoin#650: secp256k1/src/tests.c: Properly handle sscanf return value f1e11d3 Merge bitcoin#654: Fix typo (∞) ef83281 Merge pull request bitcoin#656 from real-or-random/patch-1 556caad Fix typo in docs for _context_set_illegal_callback 0d82732 Improve VERIFY_CHECK of overflow in secp256k1_scalar_cadd_bit. This added check ensures that any curve order overflow doesn't go undetected due a uint32_t overflow. 786dfb4 Merge bitcoin#583: JNI: fix use sig array e95f8ab Merge bitcoin#644: Avoid optimizing out a verify_check 384f556 Merge bitcoin#652: README.md: update instruction to run tests ee56acc Merge bitcoin#651: Fix typo in secp256k1_preallocated.h 7b9b117 Merge bitcoin#640: scalar_impl.h: fix includes d99bec2 Merge bitcoin#655: jni: Use only Guava for hex encoding and decoding 2abcf95 jni: Use only Guava for hex encoding and decoding 271582b Fix typo ce6d438 README.md: update instruction to run tests b1e68cb Fix typo in secp256k1_preallocated.h a11c76c secp256k1/src/tests.c: Properly handle sscanf return value 8fe63e5 Increase robustness against UB. Thanks to elichai2 who noted that the literal '1' is a signed integer, and that shifting a signed 32-bit integer by 31 bits causes an overflow and yields undefined behaviour. While 'scalar_low_impl''s 'secp256k1_scalar_cadd_bit' is only used for testing purposes and currently the 'bit' parameter is only 0 or 1, it is better to avoid undefined behaviour in case the used domain of 'secp256k1_scalar_cadd_bit' expands. 94ae7cb Moved a dereference so the null check will be before the dereferencing 2cb73b1 scalar_impl.h: fix includes fa33017 Merge bitcoin#634: Add a descriptive comment for secp256k1_ecmult_const. ee9e68c Add a descriptive comment for secp256k1_ecmult_const. d0d738d Merge bitcoin#631: typo in comment for secp256k1_ec_pubkey_tweak_mul () 6914c25 typo in comment for secp256k1_ec_pubkey_tweak_mul () e541a90 Merge bitcoin#629: Avoid calling _is_zero when _set_b32 fails. f34b0c3 Merge bitcoin#630: Note intention of timing sidechannel freeness. 8d1563b Note intention of timing sidechannel freeness. 1669bb2 Merge bitcoin#628: Fix ability to compile tests without -DVERIFY. ecc94ab Merge bitcoin#627: Guard memcmp in tests against mixed size inputs. 544435f Merge bitcoin#578: Avoid implementation-defined and undefined behavior when dealing with sizes 143dc6e Merge bitcoin#595: Allow to use external default callbacks e49f799 Add missing #(un)defines to base-config.h 77defd2 Add secp256k1_ prefix to default callback functions 908bdce Include stdio.h and stdlib.h explicitly in secp256k1.c 5db782e Allow usage of external default callbacks 6095a86 Replace CHECKs for no_precomp ctx by ARG_CHECKs without a return cd473e0 Avoid calling secp256k1_*_is_zero when secp256k1_*_set_b32 fails. 6c36de7 Merge bitcoin#600: scratch space: use single allocation 98836b1 scratch: replace frames with "checkpoint" system 7623cf2 scratch: save a couple bytes of unnecessarily-allocated memory a7a164f scratch: rename `max_size` to `size`, document that extra will actually be allocated 5a4bc0b scratch: unify allocations c2b028a scratch space: thread `error_callback` into all scratch space functions 0be1a4a scratch: add magic bytes to beginning of structure 92a48a7 scratch space: use single allocation 40839e2 Merge bitcoin#592: Use trivial algorithm in ecmult_multi if scratch space is small dcf3920 Fix ability to compile tests without -DVERIFY. a484e00 Merge bitcoin#566: Enable context creation in preallocated memory 0522caa Explain caller's obligations for preallocated memory 238305f Move _preallocated functions to separate header 695feb6 Export _preallocated functions 814cc78 Add tests for contexts in preallocated memory ba12dd0 Check arguments of _preallocated functions 5feadde Support cloning a context into preallocated memory c4fd5da Switch to a single malloc call ef020de Add size constants for preallocated memory 1bf7c05 Prepare for manual memory management in preallocated memory 248bffb Guard memcmp in tests against mixed size inputs. 36698dc Merge bitcoin#596: Make WINDOW_G configurable a61a93f Clean up ./configure help strings 2842dc5 Make WINDOW_G configurable 1a02d6c Merge bitcoin#626: Revert "Merge bitcoin#620: Install headers automatically" 662918c Revert "Merge bitcoin#620: Install headers automatically" 14c7dbd Simplify control flow in DER parsing ec8f20b Avoid out-of-bound pointers and integer overflows in size comparisons 01ee1b3 Parse DER-enconded length into a size_t instead of an int 912680e Merge bitcoin#561: Respect LDFLAGS and #undef STATIC_PRECOMPUTATION if using basic config 91fae3a Merge bitcoin#620: Install headers automatically 5df77a0 Merge bitcoin#533: Make sure we're not using an uninitialized variable in secp256k1_wnaf_const(...) 975e51e Merge bitcoin#617: Pass scalar by reference in secp256k1_wnaf_const() 735fbde Merge bitcoin#619: Clear a copied secret key after negation 16e8615 Install headers automatically 069870d Clear a copied secret key after negation 8979ec0 Pass scalar by reference in secp256k1_wnaf_const() 84a8085 Merge bitcoin#612: Allow field_10x26_arm.s to compile for ARMv7 architecture d4d270a Allow field_10x26_arm.s to compile for ARMv7 architecture 248f046 Make sure we're not using an uninitialized variable in secp256k1_wnaf_const(...) 9ab96f7 Use trivial algorithm in ecmult_multi if scratch space is small dbed75d Undefine `STATIC_PRECOMPUTATION` if using the basic config 310111e Keep LDFLAGS if `--coverage` 74e2dbd JNI: fix use sig array 3cb057f Fix possible integer overflow in DER parsing git-subtree-dir: src/secp256k1 git-subtree-split: ccb23e329d34e6717528c81c3bed76c7a45c547b
1 parent 5424598 commit ef71171

Some content is hidden

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

73 files changed

+4403
-2394
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
bench_inv
22
bench_ecdh
33
bench_ecmult
4+
bench_schnorrsig
45
bench_sign
56
bench_verify
6-
bench_schnorr_verify
77
bench_recover
88
bench_internal
99
tests
1010
exhaustive_tests
1111
gen_context
12+
valgrind_ctime_test
1213
*.exe
1314
*.so
1415
*.a

.travis.yml

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,34 @@ language: c
22
os: linux
33
addons:
44
apt:
5-
packages: libgmp-dev
5+
packages:
6+
- libgmp-dev
7+
- valgrind
8+
- libtool-bin
69
compiler:
710
- clang
811
- gcc
9-
cache:
10-
directories:
11-
- src/java/guava/
1212
env:
1313
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
14+
- FIELD=auto BIGNUM=auto SCALAR=auto ENDOMORPHISM=no STATICPRECOMPUTATION=yes ECMULTGENPRECISION=auto ASM=no BUILD=check EXTRAFLAGS= HOST= ECDH=no RECOVERY=no SCHNORRSIG=no EXPERIMENTAL=no CTIMETEST=yes BENCH=yes SECP256K1_BENCH_ITERS=2
1615
matrix:
1716
- SCALAR=32bit RECOVERY=yes
18-
- SCALAR=32bit FIELD=32bit ECDH=yes EXPERIMENTAL=yes
17+
- SCALAR=32bit FIELD=32bit ECDH=yes EXPERIMENTAL=yes SCHNORRSIG=yes
1918
- SCALAR=64bit
20-
- FIELD=64bit RECOVERY=yes
19+
- FIELD=64bit RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes
2120
- FIELD=64bit ENDOMORPHISM=yes
22-
- FIELD=64bit ENDOMORPHISM=yes ECDH=yes EXPERIMENTAL=yes
21+
- FIELD=64bit ENDOMORPHISM=yes ECDH=yes EXPERIMENTAL=yes SCHNORRSIG=yes
2322
- FIELD=64bit ASM=x86_64
2423
- FIELD=64bit ENDOMORPHISM=yes ASM=x86_64
2524
- FIELD=32bit ENDOMORPHISM=yes
2625
- BIGNUM=no
27-
- BIGNUM=no ENDOMORPHISM=yes RECOVERY=yes EXPERIMENTAL=yes
26+
- BIGNUM=no ENDOMORPHISM=yes RECOVERY=yes EXPERIMENTAL=yes SCHNORRSIG=yes
2827
- BIGNUM=no STATICPRECOMPUTATION=no
29-
- BUILD=distcheck
28+
- BUILD=distcheck CTIMETEST= BENCH=
3029
- EXTRAFLAGS=CPPFLAGS=-DDETERMINISTIC
3130
- EXTRAFLAGS=CFLAGS=-O0
32-
- BUILD=check-java JNI=yes ECDH=yes EXPERIMENTAL=yes
31+
- ECMULTGENPRECISION=2
32+
- ECMULTGENPRECISION=8
3333
matrix:
3434
fast_finish: true
3535
include:
@@ -40,29 +40,72 @@ matrix:
4040
packages:
4141
- gcc-multilib
4242
- libgmp-dev:i386
43+
- valgrind
44+
- libtool-bin
45+
- libc6-dbg:i386
4346
- compiler: clang
4447
env: HOST=i686-linux-gnu
4548
addons:
4649
apt:
4750
packages:
4851
- gcc-multilib
52+
- valgrind
53+
- libtool-bin
54+
- libc6-dbg:i386
4955
- compiler: gcc
5056
env: HOST=i686-linux-gnu ENDOMORPHISM=yes
5157
addons:
5258
apt:
5359
packages:
5460
- gcc-multilib
61+
- valgrind
62+
- libtool-bin
63+
- libc6-dbg:i386
5564
- compiler: gcc
5665
env: HOST=i686-linux-gnu
5766
addons:
5867
apt:
5968
packages:
6069
- gcc-multilib
6170
- 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
71+
- valgrind
72+
- libtool-bin
73+
- libc6-dbg:i386
74+
- compiler: gcc
75+
env:
76+
- BIGNUM=no ENDOMORPHISM=yes ASM=x86_64 EXPERIMENTAL=yes ECDH=yes RECOVERY=yes
77+
- VALGRIND=yes EXTRAFLAGS="--disable-openssl-tests CPPFLAGS=-DVALGRIND" BUILD=
78+
- compiler: gcc
79+
env: # The same as above but without endomorphism.
80+
- BIGNUM=no ENDOMORPHISM=no ASM=x86_64 EXPERIMENTAL=yes ECDH=yes RECOVERY=yes
81+
- VALGRIND=yes EXTRAFLAGS="--disable-openssl-tests CPPFLAGS=-DVALGRIND" BUILD=
82+
6483
before_script: ./autogen.sh
84+
6585
script:
6686
- if [ -n "$HOST" ]; then export USE_HOST="--host=$HOST"; fi
6787
- 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
88+
- ./configure --enable-experimental=$EXPERIMENTAL --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM --with-asm=$ASM --with-scalar=$SCALAR --enable-ecmult-static-precomputation=$STATICPRECOMPUTATION --with-ecmult-gen-precision=$ECMULTGENPRECISION --enable-module-ecdh=$ECDH --enable-module-recovery=$RECOVERY --enable-module-schnorrsig=$SCHNORRSIG $EXTRAFLAGS $USE_HOST
89+
- if [ -n "$BUILD" ]; then make -j2 $BUILD; fi
90+
- # travis_wait extends the 10 minutes without output allowed (https://docs.travis-ci.com/user/common-build-problems/#build-times-out-because-no-output-was-received)
91+
- # the `--error-exitcode` is required to make the test fail if valgrind found errors, otherwise it'll return 0 (http://valgrind.org/docs/manual/manual-core.html)
92+
- if [ -n "$VALGRIND" ]; then
93+
make -j2 &&
94+
travis_wait 30 valgrind --error-exitcode=42 ./tests 16 &&
95+
travis_wait 30 valgrind --error-exitcode=42 ./exhaustive_tests;
96+
fi
97+
- if [ -n "$BENCH" ]; then
98+
if [ -n "$VALGRIND" ]; then EXEC='libtool --mode=execute valgrind --error-exitcode=42'; else EXEC= ; fi &&
99+
$EXEC ./bench_ecmult &>> bench.log && $EXEC ./bench_internal &>> bench.log && $EXEC ./bench_sign &>> bench.log && $EXEC ./bench_verify &>> bench.log &&
100+
if [ "$RECOVERY" == "yes" ]; then $EXEC ./bench_recover &>> bench.log; fi &&
101+
if [ "$ECDH" == "yes" ]; then $EXEC ./bench_ecdh &>> bench.log; fi;
102+
fi
103+
- if [ -n "$CTIMETEST" ]; then
104+
libtool --mode=execute valgrind ./valgrind_ctime_test &> valgrind_ctime_test.log;
105+
fi
106+
107+
after_script:
108+
- cat ./tests.log
109+
- cat ./exhaustive_tests.log
110+
- cat ./valgrind_ctime_test.log
111+
- cat ./bench.log

Makefile.am

Lines changed: 25 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
@@ -180,3 +152,11 @@ endif
180152
if ENABLE_MODULE_RECOVERY
181153
include src/modules/recovery/Makefile.am.include
182154
endif
155+
156+
if ENABLE_MODULE_EXTRAKEYS
157+
include src/modules/extrakeys/Makefile.am.include
158+
endif
159+
160+
if ENABLE_MODULE_SCHNORRSIG
161+
include src/modules/schnorrsig/Makefile.am.include
162+
endif

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)

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)