README.md (7240B)
1 libsecp256k1 2 ============ 3 4 ![Dependencies: None](https://img.shields.io/badge/dependencies-none-success) 5 [![irc.libera.chat #secp256k1](https://img.shields.io/badge/irc.libera.chat-%23secp256k1-success)](https://web.libera.chat/#secp256k1) 6 7 High-performance high-assurance C library for digital signatures and other cryptographic primitives on the secp256k1 elliptic curve. 8 9 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. 10 11 Features: 12 * secp256k1 ECDSA signing/verification and key generation. 13 * Additive and multiplicative tweaking of secret/public keys. 14 * Serialization/parsing of secret keys, public keys, signatures. 15 * Constant time, constant memory access signing and public key generation. 16 * Derandomized ECDSA (via RFC6979 or with a caller provided function.) 17 * Very efficient implementation. 18 * Suitable for embedded systems. 19 * No runtime dependencies. 20 * Optional module for public key recovery. 21 * Optional module for ECDH key exchange. 22 * Optional module for Schnorr signatures according to [BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki). 23 24 Implementation details 25 ---------------------- 26 27 * General 28 * No runtime heap allocation. 29 * Extensive testing infrastructure. 30 * Structured to facilitate review and analysis. 31 * Intended to be portable to any system with a C89 compiler and uint64_t support. 32 * No use of floating types. 33 * Expose only higher level interfaces to minimize the API surface and improve application security. ("Be difficult to use insecurely.") 34 * Field operations 35 * Optimized implementation of arithmetic modulo the curve's field size (2^256 - 0x1000003D1). 36 * Using 5 52-bit limbs 37 * Using 10 26-bit limbs (including hand-optimized assembly for 32-bit ARM, by Wladimir J. van der Laan). 38 * 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. 39 * Scalar operations 40 * Optimized implementation without data-dependent branches of arithmetic modulo the curve's order. 41 * Using 4 64-bit limbs (relying on __int128 support in the compiler). 42 * Using 8 32-bit limbs. 43 * Modular inverses (both field elements and scalars) based on [safegcd](https://gcd.cr.yp.to/index.html) with some modifications, and a variable-time variant (by Peter Dettman). 44 * Group operations 45 * Point addition formula specifically simplified for the curve equation (y^2 = x^3 + 7). 46 * Use addition between points in Jacobian and affine coordinates where possible. 47 * Use a unified addition/doubling formula where necessary to avoid data-dependent branches. 48 * Point/x comparison without a field inversion by comparison in the Jacobian coordinate space. 49 * Point multiplication for verification (a*P + b*G). 50 * Use wNAF notation for point multiplicands. 51 * Use a much larger window for multiples of G, using precomputed multiples. 52 * Use Shamir's trick to do the multiplication with the public key and the generator simultaneously. 53 * Use secp256k1's efficiently-computable endomorphism to split the P multiplicand into 2 half-sized ones. 54 * Point multiplication for signing 55 * Use a precomputed table of multiples of powers of 16 multiplied with the generator, so general multiplication becomes a series of additions. 56 * Intended to be completely free of timing sidechannels for secret-key operations (on reasonable hardware/toolchains) 57 * Access the table with branch-free conditional moves so memory access is uniform. 58 * No data-dependent branches 59 * Optional runtime blinding which attempts to frustrate differential power analysis. 60 * 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. 61 62 Building with Autotools 63 ----------------------- 64 65 $ ./autogen.sh 66 $ ./configure 67 $ make 68 $ make check # run the test suite 69 $ sudo make install # optional 70 71 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. 72 73 Building with CMake (experimental) 74 ---------------------------------- 75 76 To maintain a pristine source tree, CMake encourages to perform an out-of-source build by using a separate dedicated build tree. 77 78 ### Building on POSIX systems 79 80 $ mkdir build && cd build 81 $ cmake .. 82 $ cmake --build . 83 $ ctest # run the test suite 84 $ sudo cmake --build . --target install # optional 85 86 To compile optional modules (such as Schnorr signatures), you need to run `cmake` with additional flags (such as `-DSECP256K1_ENABLE_MODULE_SCHNORRSIG=ON`). Run `cmake .. -LH` to see the full list of available flags. 87 88 ### Cross compiling 89 90 To alleviate issues with cross compiling, preconfigured toolchain files are available in the `cmake` directory. 91 For example, to cross compile for Windows: 92 93 $ cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/x86_64-w64-mingw32.toolchain.cmake 94 95 To cross compile for Android with [NDK](https://developer.android.com/ndk/guides/cmake) (using NDK's toolchain file, and assuming the `ANDROID_NDK_ROOT` environment variable has been set): 96 97 $ cmake .. -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake" -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=28 98 99 ### Building on Windows 100 101 To build on Windows with Visual Studio, a proper [generator](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html#visual-studio-generators) must be specified for a new build tree. 102 103 The following example assumes using of Visual Studio 2022 and CMake v3.21+. 104 105 In "Developer Command Prompt for VS 2022": 106 107 >cmake -G "Visual Studio 17 2022" -A x64 -S . -B build 108 >cmake --build build --config RelWithDebInfo 109 110 Usage examples 111 ----------- 112 Usage examples can be found in the [examples](examples) directory. To compile them you need to configure with `--enable-examples`. 113 * [ECDSA example](examples/ecdsa.c) 114 * [Schnorr signatures example](examples/schnorr.c) 115 * [Deriving a shared secret (ECDH) example](examples/ecdh.c) 116 117 To compile the Schnorr signature and ECDH examples, you also need to configure with `--enable-module-schnorrsig` and `--enable-module-ecdh`. 118 119 Benchmark 120 ------------ 121 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. 122 123 To print the benchmark result to the command line: 124 125 $ ./bench_name 126 127 To create a CSV file for the benchmark result : 128 129 $ ./bench_name | sed '2d;s/ \{1,\}//g' > bench_name.csv 130 131 Reporting a vulnerability 132 ------------ 133 134 See [SECURITY.md](SECURITY.md) 135 136 Contributing to libsecp256k1 137 ------------ 138 139 See [CONTRIBUTING.md](CONTRIBUTING.md)