Hardening the libsecp256k1 API: my 2021 proposals for opaque secret keys

The shape of the curve

y² = x³ + 7, the curve behind Bitcoin

Over the reals y² = x³ + 7

The secp256k1 equation y² = x³ + 7 drawn over the real numbers: one smooth branch, symmetric about the x-axis. A chord through points P and Q meets the curve a third time; reflecting that point across the x-axis gives P + Q. PQ−(P+Q)P+Q

Draw a line through P and Q; it meets the curve once more. Reflect that point and you have P + Q. That chord rule is the whole group law.

Over Fp y² ≡ x³ + 7 (mod 223)

The same equation reduced modulo a small prime: the curve becomes a scattered, mirror-symmetric cloud of points.

252 points, counting the point at infinity. Pick a prime:

secp256k1 is this curve over Fp with p = 2²⁵⁶ − 2³² − 977. Its group has prime order n ≈ 1.16 × 10⁷⁷, cofactor h = 1. Rotate it as a torus →

Interactive figures

All interactive figures →

Part of one story: hardening cryptographic key handling in open-source wallet libraries during my time at Coinbase. The companion post covers the money-tree fix, which removed unsafe OpenSSL patches from a Ruby BIP32 wallet library.

In March 2021 I was a Security Architect at Coinbase, working on wallet and key security: how the keys that protect customer funds are held and handled in code. That meant spending a lot of time with libsecp256k1, the elliptic-curve library that Bitcoin Core uses for secp256k1 signatures and keys. Out of that work I opened two pull requests and four issues proposing changes to the library’s API. None of the pull requests were merged, and all of the issues are closed. They were proposals, and this is a record of what I proposed and why.

The problem: a secret key is just 32 bytes

In the libsecp256k1 API of the time, a public key had an opaque type, but a secret key was a bare unsigned char[32]. Secret keys come in more than one form, though. The same key can be a raw 32-byte big-endian integer, or it can be wrapped in DER/ASN.1. When both look like a pointer to bytes, nothing stops a caller from passing the wrong one, and in wallet code that is exactly the kind of mistake that is easy to make and hard to notice.

PR #911: an opaque secret key and stronger byte types

Pull request #911, “Introduce opaque secret key and stronger types for fixed size byte arrays” (commit 49b4ef4, March 26, 2021), followed issue #912, where I proposed an opaque type for secret keys like the one public keys already had. The reasons were:

The maintainers pointed to the extrakeys module’s keypair type, which holds a private and public key together, as the direction for newer code. That type already answers several of the same concerns for the APIs built on it.

PR #914: a symbol for the library version

Pull request #914, “Introduce symbol to get version of library” (commit 1446af4, March 26, 2021), implemented issue #909. A program linked against a shared libsecp256k1 had no way to ask which version it had loaded. For key-handling code that has to know which behavior it is getting, that question matters.

The other issues

Where this fits

The same month, I worked on the Ruby side of the same problem. In money-tree, a Ruby BIP32 HD wallet library, I removed monkey patches to OpenSSL’s elliptic-curve Point class and a second, FFI-based OpenSSL binding. That pull request was merged in December 2021. Together the two are one piece of work: finding the places where a wallet library can mishandle a key without anyone noticing, and closing them.

This was my first serious look at secp256k1 itself, and it was about the edges: how keys cross an API boundary and what types can prevent at compile time. My later working paper on the SEC 2 prime Koblitz curves comes at the same curve from the other end, looking at how its parameters and generator were chosen. The ECC curve research guide collects that work.