645d9c53c4examples: let musig use random.h instead of /dev/urandom (Jonas Nick)eccba5b4e5examples: relicense musig example to CC0 public domain (Jonas Nick)7c5af740faci: fix missing EXPERIMENTAL flags (Jonas Nick)03bea1e173configure: add -zkp modules to dev-mode and remove redundant code (Jonas Nick)2adb741c45examples: rename example_musig to musig_example for consistency (Jonas Nick)37d36927dftests: Add tests for _read_be32 and _write_be32 (Tim Ruffing)616b43dd3butil: Remove endianness detection (Tim Ruffing)8d89b9e6e5hash: Make code agnostic of endianness (Tim Ruffing)55512d30b7doc: clean up module help text in configure.ac (Elliott Jin)d9d94a9969doc: mention optional modules in README (Elliott Jin)7f09d0f311README: mention that ARM assembly is experimental (Jonas Nick)80cf4eea5fbuild: stop treating schnorrsig, extrakeys modules as experimental (Jonas Nick)b8f8b99f0fdocs: Fix return value for functions that don't have invalid inputs (Tim Ruffing)f813bb0df3schnorrsig: Adapt example to new API (Tim Ruffing)99e6568fc6schnorrsig: Rename schnorrsig_sign to schnorsig_sign32 and deprecate (Tim Ruffing)fc94a2da44Use SECP256K1_DEPRECATED for existing deprecated API functions (Tim Ruffing)3db0560606Add SECP256K1_DEPRECATED attribute for marking API parts as deprecated (Tim Ruffing)f8d9174357Add SHA256 bit counter tests (Tim Ruffing)9b514ce1d2Add test vector for very long SHA256 messages (Tim Ruffing)8e3dde1137Simplify struct initializer for SHA256 padding (Tim Ruffing)eb28464a8bChange SHA256 byte counter from size_t to uint64_t (Tim Ruffing)21b2ebaf74configure: Remove redundant pkg-config code (Tim Ruffing)0d253d52e8configure: Use modern way to set AR (Tim Ruffing)e0838d663dconfigure: Add hidden --enable-dev-mode to enable all the stuff (Tim Ruffing)fabd579dfaconfigure: Remove redundant code that sets _enable variables (Tim Ruffing)0d4226c051configure: Use canonical variable prefix _enable consistently (Tim Ruffing)7c9502ceceAdd a copy of the CC0 license to the examples (Elichai Turkel)42e03432e6Add usage examples to the readme (Elichai Turkel)517644eab1Optionally compile the examples in autotools, compile+run in travis (Elichai Turkel)422a7cc86aAdd a ecdh shared secret example (Elichai Turkel)b0cfbcc143Add a Schnorr signing and verifying example (Elichai Turkel)fee7d4bf9eAdd an ECDSA signing and verifying example (Elichai Turkel)e848c3799cUpdate sage files for new formulae (Peter Dettman)d64bb5d4f3Add fe_half tests for worst-case inputs (Peter Dettman)4eb8b932ffFurther improve doubling formula using fe_half (Peter Dettman)557b31fac3Doubling formula using fe_half (Pieter Wuille)2cbb4b1a42Run more iterations of run_field_misc (Pieter Wuille)9cc5c257edAdd test for secp256k1_fe_half (Pieter Wuille)925f78d55eAdd _fe_half and use in _gej_add_ge (Peter Dettman)3531a43b5becdh: Make generator_basepoint test depend on global iteration count (Tim Ruffing)c881dd49bdecdh: Add test computing shared_secret=basepoint with random inputs (Tim Ruffing)e51ad3b737ci: Retry `brew update` a few times to avoid random failures (Tim Ruffing)b1cb969e8aci: Revert "Attempt to make macOS builds more reliable" (Tim Ruffing)e0db3f8a25build: Replace use of deprecated autoconf macro AC_PROG_CC_C89 (laanwj)d9396a56daci: Attempt to make macOS builds more reliable (Tim Ruffing)ebb1beea78sage: Ensure that constraints are always fastfracs (Tim Ruffing)d8d54859edci: Run sage prover on CI (Tim Ruffing)77cfa98dbcsage: Normalize sign of polynomial factors in prover (Tim Ruffing)eae75869cfsage: Exit with non-zero status in case of failures (Tim Ruffing)b54d843eacsage: Fix printing of errors (Tim Ruffing)e108d0039csage: Fix incompatibility with sage 9.4 (Tim Ruffing)b797a500ecCreate a SECP256K1_ECMULT_TABLE_VERIFY macro. (Russell O'Connor)a731200cc3Replace ECMULT_TABLE_GET_GE_STORAGE macro with a function. (Russell O'Connor)fe34d9f341Eliminate input_pos state field from ecmult_strauss_wnaf. (Russell O'Connor)0397d00ba0Eliminate na_1 and na_lam state fields from ecmult_strauss_wnaf. (Russell O'Connor)7ba3ffcca0Remove the unused pre_a_lam allocations. (Russell O'Connor)b3b57ad6eeEliminate the pre_a_lam array from ecmult_strauss_wnaf. (Russell O'Connor)ae7ba0f922Remove the unused prej allocations. (Russell O'Connor)e5c18892dbEliminate the prej array from ecmult_strauss_wnaf. (Russell O'Connor)c9da1baad1Move secp256k1_fe_one to field.h (Russell O'Connor)070e772211Faster fixed-input ecmult tests (Pieter Wuille)45f37b6506Modulo-reduce msg32 inside RFC6979 nonce fn to match spec. Fixes #1063. (Paul Miller) Pull request description: [bitcoin-core/secp256k1#1064]: Modulo-reduce msg32 inside RFC6979 nonce fn to match spec. Fixes #1063 [bitcoin-core/secp256k1#1049]: Faster fixed-input ecmult tests [bitcoin-core/secp256k1#899]: Reduce stratch space needed by ecmult_strauss_wnaf. [bitcoin-core/secp256k1#1068]: sage: Fix incompatibility with sage 9.4 [bitcoin-core/secp256k1#1072]: ci: Attempt to make macOS builds more reliable [bitcoin-core/secp256k1#1069]: build: Replace use of deprecated autoconf macro AC_PROG_CC_C89 [bitcoin-core/secp256k1#1074]: ci: Retry brew update a few times to avoid random failures [bitcoin-core/secp256k1#1026]: ecdh: Add test computing shared_secret=basepoint with random inputs [bitcoin-core/secp256k1#1033]: Add _fe_half and use in _gej_add_ge and _gej_double [bitcoin-core/secp256k1#748]: Add usage examples [bitcoin-core/secp256k1#1079]: configure: Add hidden --enable-dev-mode to enable all the stuff [bitcoin-core/secp256k1#1088]: configure: Use modern way to set AR [bitcoin-core/secp256k1#1090]: configure: Remove redundant pkg-config code [bitcoin-core/secp256k1#731]: Change SHA256 byte counter from size_t to uint64_t [bitcoin-core/secp256k1#1089]: Schnorrsig API improvements [bitcoin-core/secp256k1#995]: build: stop treating schnorrsig, extrakeys modules as experimental [bitcoin-core/secp256k1#1094]: doc: Clarify configure flags for optional modules [bitcoin-core/secp256k1#1093]: hash: Make code agnostic of endianness This PR can be recreated with `./sync-upstream.sh range 8746600eec5e7fcd35dabd480839a3a4bdfee87b`. ACKs for top commit: real-or-random: ACK645d9c53c4I rederived the tree, and tested it with MSVC, including the musig example Tree-SHA512: 3b771630806ed8481053958c21820dce6e869371833cd18a5c430a2768bda8064ad2bb247afbe38e3fa37320a8b1dbbe65ad68c8963efb995d96aa29ae574884
0a40a486 d8a24632 85b00a1c 59547943 5dcc6f8d 07752831 3ef94aa5 1253a277 64b34979 ac83be33 0e5cbd01 e0508ee9 587239db 1ac7e31c d0ad5814 912b7ccc 8746600e ' into temp-merge-1093
0a40a486 d8a24632 85b00a1c 59547943 5dcc6f8d 07752831 3ef94aa5 1253a277 64b34979 ac83be33 0e5cbd01 e0508ee9 587239db 1ac7e31c d0ad5814 912b7ccc 8746600e ' into temp-merge-1093
0a40a486 d8a24632 85b00a1c 59547943 5dcc6f8d 07752831 3ef94aa5 1253a277 64b34979 ac83be33 0e5cbd01 e0508ee9 587239db 1ac7e31c d0ad5814 912b7ccc 8746600e ' into temp-merge-1093
0a40a486 d8a24632 85b00a1c 59547943 5dcc6f8d 07752831 3ef94aa5 1253a277 64b34979 ac83be33 0e5cbd01 e0508ee9 587239db 1ac7e31c d0ad5814 912b7ccc 8746600e ' into temp-merge-1093
libsecp256k1
Optimized C library for ECDSA signatures and secret/public key operations on curve secp256k1.
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.
Features:
- secp256k1 ECDSA signing/verification and key generation.
- Additive and multiplicative tweaking of secret/public keys.
- Serialization/parsing of secret keys, public keys, signatures.
- Constant time, constant memory access signing and public key generation.
- Derandomized ECDSA (via RFC6979 or with a caller provided function.)
- Very efficient implementation.
- Suitable for embedded systems.
- Optional module for public key recovery.
- Optional module for ECDH key exchange.
- Optional module for Schnorr signatures according to BIP-340.
- Optional module for ECDSA adaptor signatures (experimental).
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.
Implementation details
- General
- No runtime heap allocation.
- Extensive testing infrastructure.
- Structured to facilitate review and analysis.
- Intended to be portable to any system with a C89 compiler and uint64_t support.
- No use of floating types.
- Expose only higher level interfaces to minimize the API surface and improve application security. ("Be difficult to use insecurely.")
- Field operations
- Optimized implementation of arithmetic modulo the curve's field size (2^256 - 0x1000003D1).
- Using 5 52-bit limbs (including hand-optimized assembly for x86_64, by Diederik Huys).
- Using 10 26-bit limbs (including hand-optimized assembly for 32-bit ARM, by Wladimir J. van der Laan).
- This is an experimental feature that has not received enough scrutiny to satisfy the standard of quality of this library but is made available for testing and review by the community.
- Optimized implementation of arithmetic modulo the curve's field size (2^256 - 0x1000003D1).
- Scalar operations
- Optimized implementation without data-dependent branches of arithmetic modulo the curve's order.
- Using 4 64-bit limbs (relying on __int128 support in the compiler).
- Using 8 32-bit limbs.
- Optimized implementation without data-dependent branches of arithmetic modulo the curve's order.
- Modular inverses (both field elements and scalars) based on safegcd with some modifications, and a variable-time variant (by Peter Dettman).
- Group operations
- Point addition formula specifically simplified for the curve equation (y^2 = x^3 + 7).
- Use addition between points in Jacobian and affine coordinates where possible.
- Use a unified addition/doubling formula where necessary to avoid data-dependent branches.
- Point/x comparison without a field inversion by comparison in the Jacobian coordinate space.
- Point multiplication for verification (aP + bG).
- Use wNAF notation for point multiplicands.
- Use a much larger window for multiples of G, using precomputed multiples.
- Use Shamir's trick to do the multiplication with the public key and the generator simultaneously.
- Use secp256k1's efficiently-computable endomorphism to split the P multiplicand into 2 half-sized ones.
- Point multiplication for signing
- Use a precomputed table of multiples of powers of 16 multiplied with the generator, so general multiplication becomes a series of additions.
- Intended to be completely free of timing sidechannels for secret-key operations (on reasonable hardware/toolchains)
- Access the table with branch-free conditional moves so memory access is uniform.
- No data-dependent branches
- Optional runtime blinding which attempts to frustrate differential power analysis.
- 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.
Build steps
libsecp256k1 is built using autotools:
$ ./autogen.sh
$ ./configure
$ make
$ make check # run the test suite
$ sudo make install # optional
To compile optional modules (such as Schnorr signatures), you need to run ./configure with additional flags (such as --enable-module-schnorrsig). Run ./configure --help to see the full list of available flags.
Usage examples
Usage examples can be found in the examples directory. To compile them you need to configure with --enable-examples.
- ECDSA example
- Schnorr signatures example
- Deriving a shared secret (ECDH) example
To compile the Schnorr signature and ECDH examples, you also need to configure with
--enable-module-schnorrsigand--enable-module-ecdh.
Test coverage
This library aims to have full coverage of the reachable lines and branches.
To create a test coverage report, configure with --enable-coverage (use of GCC is necessary):
$ ./configure --enable-coverage
Run the tests:
$ make check
To create a report, gcovr is recommended, as it includes branch coverage reporting:
$ gcovr --exclude 'src/bench*' --print-summary
To create a HTML report with coloured and annotated source code:
$ mkdir -p coverage
$ gcovr --exclude 'src/bench*' --html --html-details -o coverage/coverage.html
Benchmark
If configured with --enable-benchmark (which is the default), binaries for benchmarking the libsecp256k1 functions will be present in the root directory after the build.
To print the benchmark result to the command line:
$ ./bench_name
To create a CSV file for the benchmark result :
$ ./bench_name | sed '2d;s/ \{1,\}//g' > bench_name.csv
Reporting a vulnerability
See SECURITY.md