Bitcoin Core mirror and no, I don't give a fuck about Monero.
Find a file
fanquake 41797f8ab9 Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61
642c885b61 Merge bitcoin-core/secp256k1#1575: release: prepare for 0.5.1
cdf08c1a2b Merge bitcoin-core/secp256k1#1576: doc: mention `needs-changelog` github label in release process
40d87b8e45 release: prepare for 0.5.1
5770226176 changelog: clarify CMake option
759bd4bbc8 doc: mention `needs-changelog` github label in release process
fded437c4c Merge bitcoin-core/secp256k1#1574: Fix compilation when extrakeys module isn't enabled
763d938cf0 ci: only enable extrakeys module when schnorrsig is enabled
af551ab9db tests: do not use functions from extrakeys module
0055b86780 Merge bitcoin-core/secp256k1#1551: Add ellswift usage example
ea2d5f0f17 Merge bitcoin-core/secp256k1#1563: doc: Add convention for defaults
ca06e58b2c Merge bitcoin-core/secp256k1#1564: build, ci: Adjust the default size of the precomputed table for signing
e2af491263 ci: Switch to the new default value of the precomputed table for signing
d94a9273f8 build: Adjust the default size of the precomputed table for signing
fcc5d7381b Merge bitcoin-core/secp256k1#1565: cmake: Bump CMake minimum required version up to 3.16
9420eece24 cmake: Bump CMake minimum required version up to 3.16
16685649d2 doc: Add convention for defaults
a5269373fa Merge bitcoin-core/secp256k1#1555: Fixed O3 replacement
b8fe33332b cmake: Fixed O3 replacement
31f84595c4 Add ellswift usage example
fe4fbaa7f3 examples: fix case typos in secret clearing paragraphs (s/, Or/, or/)

git-subtree-dir: src/secp256k1
git-subtree-split: 642c885b6102725e25623738529895a95addc4f4
2024-08-02 11:32:47 +01:00
.github Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
build-aux/m4 Squashed 'src/secp256k1/' changes from 06bff6dec8..4af241b320 2024-06-25 15:01:00 +01:00
ci Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
cmake Squashed 'src/secp256k1/' changes from 06bff6dec8..4af241b320 2024-06-25 15:01:00 +01:00
contrib Squashed 'src/secp256k1/' changes from efe85c70a2..d8311688bd 2024-04-04 12:05:16 +01:00
doc Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
examples Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
include Squashed 'src/secp256k1/' changes from d8311688bd..06bff6dec8 2024-05-16 10:35:52 +08:00
sage Squashed 'src/secp256k1/' changes from 705ce7ed8c..c545fdc374 2023-07-18 15:25:05 +01:00
src Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
tools Squashed 'src/secp256k1/' changes from d8311688bd..06bff6dec8 2024-05-16 10:35:52 +08:00
.cirrus.yml Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
.gitattributes Squashed 'src/secp256k1/' changes from 0559fc6e41..8746600eec 2022-04-06 20:20:30 +01:00
.gitignore Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
autogen.sh Squashed 'src/secp256k1/' content from commit ad2028f 2014-11-18 18:06:36 +01:00
CHANGELOG.md Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
CMakeLists.txt Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
CMakePresets.json Squashed 'src/secp256k1/' changes from 4258c54f4e..705ce7ed8c 2023-06-21 11:04:00 -04:00
configure.ac Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
CONTRIBUTING.md Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
COPYING Squashed 'src/secp256k1/' content from commit ad2028f 2014-11-18 18:06:36 +01:00
libsecp256k1.pc.in Squashed 'src/secp256k1/' changes from 21ffe4b22a9..bdf39000b9c 2023-03-08 17:41:24 -05:00
Makefile.am Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
README.md Squashed 'src/secp256k1/' changes from 4af241b320..642c885b61 2024-08-02 11:32:47 +01:00
SECURITY.md Squashed 'src/secp256k1/' changes from be8d9c262f..0559fc6e41 2021-12-15 09:19:50 -05:00

libsecp256k1

Dependencies: None irc.libera.chat #secp256k1

High-performance high-assurance C library for digital signatures and other cryptographic primitives on the secp256k1 elliptic curve.

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.
  • No runtime dependencies.
  • Optional module for public key recovery.
  • Optional module for ECDH key exchange.
  • Optional module for Schnorr signatures according to BIP-340.
  • Optional module for ElligatorSwift key exchange according to BIP-324.

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
      • 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.
  • 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.
  • 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.

Building with 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.

Building with CMake (experimental)

To maintain a pristine source tree, CMake encourages to perform an out-of-source build by using a separate dedicated build tree.

Building on POSIX systems

$ mkdir build && cd build
$ cmake ..
$ cmake --build .
$ ctest  # run the test suite
$ sudo cmake --install .  # optional

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.

Cross compiling

To alleviate issues with cross compiling, preconfigured toolchain files are available in the cmake directory. For example, to cross compile for Windows:

$ cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/x86_64-w64-mingw32.toolchain.cmake

To cross compile for Android with NDK (using NDK's toolchain file, and assuming the ANDROID_NDK_ROOT environment variable has been set):

$ cmake .. -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake" -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=28

Building on Windows

To build on Windows with Visual Studio, a proper generator must be specified for a new build tree.

The following example assumes using of Visual Studio 2022 and CMake v3.21+.

In "Developer Command Prompt for VS 2022":

>cmake -G "Visual Studio 17 2022" -A x64 -S . -B build
>cmake --build build --config RelWithDebInfo

Usage examples

Usage examples can be found in the examples directory. To compile them you need to configure with --enable-examples.

To compile the Schnorr signature and ECDH examples, you also need to configure with --enable-module-schnorrsig and --enable-module-ecdh.

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

Contributing to libsecp256k1

See CONTRIBUTING.md