Libsecp256k1: The Unsung Hero of Bitcoin’s Security
Common phrases heard among Bitcoiners include “don’t trust, verify” or “not your keys, not your coins”, sometimes even claiming that it’s “backed by math”. But what do these proverbs ultimately boil down to, and how exactly is this involved math put into practice? Most readers are surely aware that a fundamental ingredient in the design of Bitcoin is public-key cryptography and more specifically digital signatures, which are essential to prove ownership without needing a central entity. Probably less well-known is what piece of software is under the hood to make that elliptic curve math perform and what efforts are involved to ensure that this happens in the most secure and performant way, with continuous improvements. Let’s dive into the exciting history and evolution of “libsecp256k1”, a library that started out as a small hobby project and over the years evolved into an essential part of consensus rules protecting a multi-trillion dollar asset.
The Genesis
For reasons that remain unclear, Satoshi Nakamoto chose the elliptic curve named “secp256k1” for creating and verifying digital signatures in Bitcoin. The Elliptic Curve Digital Signature Algorithm (ECDSA) is a core component of Bitcoin’s security model. The initial version of the Bitcoin client relied on the widespread OpenSSL library for signing and verifying transactions. While using a third-party library seemed reasonable from a software engineering perspective, this choice proved problematic due to inconsistencies in signature parsing, potentially leading to chain splits. This experience highlighted that OpenSSL wasn’t suitable for a consensus-critical system like Bitcoin.
The issue was addressed by BIP66, which enforced strict encoding of ECDSA signatures. Subsequently, Bitcoin Core v0.12, released in early 2016, replaced OpenSSL with libsecp256k1.
The initial motivation for starting the libsecp256k1 project was to explore potential speed improvements. In 2012, Bitcoin Core developer Pieter Wuille, also known as “sipa”, discovered a bitcointalk thread by Hal Finney, the recipient of the first Bitcoin transaction in 2009. Finney discussed an optimization using an “endomorphism” (specifically the GLV-method, Gallant-Lambert-Vanstone) – a technique applicable to certain elliptic curves, including secp256k1. Although Finney’s implementation showed a 20% speedup, it wasn’t merged due to concerns about code complexity and cryptographic soundness.
Wuille then started libsecp256k1 from scratch, with the first commit dating back to March 5, 2013. Within two weeks, the library could verify the entire blockchain (at block height ~225000), and signing functionality followed shortly after. After further testing, it replaced OpenSSL in Bitcoin Core, first for wallet signing (v0.10, 2015) and then for ECDSA signature verification in consensus (v0.12, 2016). This proved worthwhile, offering a 2.5 to 5.5 times speedup. The endomorphism optimization wasn’t initially enabled due to patent concerns, but was activated in 2020 after the patent expired (v0.20), providing another 16% speed boost.
A Growing Team
Over time, libsecp256k1 attracted more contributors, including Gregory Maxwell and Andrew Poelstra from Blockstream. Jonas Nick and Tim Ruffing later joined, becoming maintainers of the library. Ruffing describes himself and his colleagues as “full-stack cryptographers”, responsible for both specifying recent cryptographic protocols and implementing them. Occasionally, cryptographers from outside the Bitcoin space have contributed, such as Peter Dettman, a maintainer of the BouncyCastle cryptography library, who has suggested performance improvements.
Why Reinvent The Wheel?
The goal of libsecp256k1 is to provide a high-quality library for cryptographic operations on the secp256k1 curve, primarily for the Bitcoin ecosystem. Its API is designed to be robust and prevent insecure operations that could lead to fund loss. By focusing solely on secp256k1 and operations relevant to Bitcoin, the code remains faster, simpler to review, and easier to maintain.
Written in C with no external dependencies, libsecp256k1 can run on constrained devices like microcontrollers used in hardware wallets.
Measure Twice, Cut Once
From its inception, libsecp256k1 has prioritized quality assurance. It now boasts nearly 100% test code coverage, and new modules require maintaining this level. “Exhaustive testing” is also employed, exercising the library’s functionality across the entire curve space (using a smaller, similar curve for feasibility). Constant-time behavior is also tested using tools like valgrind to prevent side-channel attacks.
Schnorr: A Whole New World
A major milestone was the introduction of Schnorr signatures, a key part of the Schnorr/Taproot soft-fork activated in late 2021. Schnorr signatures offer advantages over ECDSA, including provable security, compactness, and enabling constructions like key and signature aggregation for more efficient multisignature schemes. The specification and implementation were created by Wuille, Nick, and Ruffing.
Good For Your Node And The Network
Verifying digital signatures is a critical operation in Bitcoin’s consensus engine. Rapid signature verification is essential for fast transaction and block propagation, and for speeding up Initial Block Download (IBD) for new network participants. Libsecp256k1 is now approximately 8 times faster than OpenSSL for ECDSA signature verification.
Signing Can Be Dangerous, So Do It Right
Signing, the process of creating a digital signature, is delicate due to the involvement of secret key material. Libsecp256k1 combats side-channel attacks by avoiding data-dependent branches, a challenging task requiring careful attention to compiler optimizations. The constant-time property is tested using valgrind to detect potential side-channel risks. Care is also taken to erase secret material from memory to prevent leaks.
Some Happy Accidents
During development, unexpected discoveries were made. In 2014, Wuille and Maxwell discovered a bug in OpenSSL’s bignum squaring function (CVE-2014-3570) through extensive testing. Later, Wuille proposed a new method for computing a bound for the “safegcd” algorithm, leading to further speedups. Gregory Maxwell then discovered an even more efficient variant of the algorithm. The correctness of the “safegcd” implementation has been formally verified using the Rocq theorem proving software and the “Verifiable C” program logic, with the potential to verify the entirety of libsecp256k1.
Cryptography Is Still Evolving
libsecp256k1 continues to evolve, incorporating new cryptographic operations relevant to Bitcoin, such as ElligatorSwift (for encrypted P2P communication – BIP324) and MuSig2 (for key aggregation). Ongoing efforts include Silent Payments, a proposal for privacy-preserving static reusable addresses.
Readers interested in libsecp256k1 are encouraged to explore secp256k1lab, a Python implementation for prototyping and experimentation.
Keep reading