Retvrning to X

I nuked all of my social media accounts years ago and never looked back. My general take is that all legacy web 2.0-style social media stuff needs to be ground under a boot, and then the boot filled with cement and sank to the bottom of a particularly deep ocean. Just in case.

That said, I recently recreated an account on X using my old handle, @jaredtobin (some other dude has nabbed @jtobin, unfortunately), and will be experimenting with it for traditional microblogging purposes for things that don’t warrant a full post on this, my trusty long-form blog.

Why not nostr, etc.? Well, I could. I like nostr, and maybe I’ll mirror my stuff there just for the lulz. But, pragmatically, X appears to be where the action is, and I want my stuff to be easy to access and interact with.

It’s an experiment. I’m aloof by nature, so there’s no guarantee I won’t at some point turn up my nose and redeploy the nukes. But we’ll see how it goes. Follow me there, if you’re into that kind of thing!

Faster Signatures on secp256k1

In my last post I mentioned that both the Schnorr and ECDSA signature schemes on secp256k1 could be made faster via the so-called wNAF method for elliptic curve point multiplication (short for the cumbersome “w-ary non-adjacent form”). I implemented wNAF for ppad-secp256k1 afterwards, adding a bunch of functions that use it internally.

The only “downside” to the use of wNAF is that one needs to supply a context argument that consists of a bunch of precomputed multiples of the secp256k1 base point (this is at least one of the reasons why libsecp256k1, as well as any bindings to it, generally require you to pass a context argument everywhere). It’s not much of a downside, but it does complicate the API to some degree, so I added these functions on top of the existing ones – the original functions are preserved for when terse code, rather than raw speed, is desired.

The wNAF method really shines when it comes to ECDSA. Here are some benchmarks that illustrate the improvement – the wNAF-powered functions are differentiated by a trailing apostrophe:

benchmarking ecdsa/sign_ecdsa
time                 1.795 ms   (1.767 ms .. 1.822 ms)
                     0.998 R²   (0.997 R² .. 0.999 R²)
mean                 1.806 ms   (1.785 ms .. 1.849 ms)
std dev              93.43 μs   (58.86 μs .. 163.7 μs)

benchmarking ecdsa/sign_ecdsa'
time                 243.1 μs   (237.6 μs .. 249.6 μs)
                     0.996 R²   (0.993 R² .. 0.999 R²)
mean                 241.4 μs   (238.1 μs .. 245.3 μs)
std dev              12.06 μs   (9.492 μs .. 16.17 μs)

benchmarking ecdsa/verify_ecdsa
time                 2.473 ms   (2.409 ms .. 2.541 ms)
                     0.995 R²   (0.991 R² .. 0.997 R²)
mean                 2.432 ms   (2.396 ms .. 2.480 ms)
std dev              140.2 μs   (110.4 μs .. 187.0 μs)

benchmarking ecdsa/verify_ecdsa'
time                 1.460 ms   (1.418 ms .. 1.497 ms)
                     0.994 R²   (0.989 R² .. 0.997 R²)
mean                 1.419 ms   (1.398 ms .. 1.446 ms)
std dev              80.76 μs   (66.73 μs .. 104.9 μs)

So, a 7.5x improvement on ECDSA signature creation, and almost a 2x improvement on signature verification. Not bad.

For Schnorr signatures the improvements are less pronounced, but still substantial:

benchmarking schnorr/sign_schnorr
time                 5.368 ms   (5.327 ms .. 5.423 ms)
                     0.999 R²   (0.999 R² .. 1.000 R²)
mean                 5.436 ms   (5.410 ms .. 5.464 ms)
std dev              84.83 μs   (72.17 μs .. 101.4 μs)

benchmarking schnorr/sign_schnorr'
time                 2.557 ms   (2.534 ms .. 2.596 ms)
                     0.998 R²   (0.997 R² .. 0.999 R²)
mean                 2.579 ms   (2.556 ms .. 2.605 ms)
std dev              83.75 μs   (69.50 μs .. 100.5 μs)

benchmarking schnorr/verify_schnorr
time                 2.338 ms   (2.309 ms .. 2.382 ms)
                     0.998 R²   (0.995 R² .. 0.999 R²)
mean                 2.339 ms   (2.316 ms .. 2.366 ms)
std dev              82.73 μs   (65.83 μs .. 105.9 μs)

benchmarking schnorr/verify_schnorr'
time                 1.429 ms   (1.381 ms .. 1.482 ms)
                     0.993 R²   (0.987 R² .. 0.998 R²)
mean                 1.372 ms   (1.355 ms .. 1.396 ms)
std dev              67.72 μs   (50.44 μs .. 110.5 μs)

So here about a 2x improvement, plus or minus change, across the board. I hope to hammer these down a good bit further in the future if at all possible!

Signatures on secp256k1

I’ve released a library supporting BIP340 Schnorr signatures and deterministic ECDSA on the elliptic curve secp256k1. Get it while it’s hot – for when you just aren’t feeling libsecp256k1!

This is another “minimal” library in the ppad suite of libraries I’m working on. Minimal in the sense that it is pure Haskell (no FFI – you can check out ppad-csecp256k1 if you want that) and depends only on ‘base’, ‘bytestring’, and my own HMAC-DRBG and SHA256 libraries. The feature set also intentionally remains rather lean for the time being (though if you could use other features in there, let me know!).

Performance is decent, though unsurprisingly it still pales in comparison to the low-level and battle-hardened libsecp256k1 (think 5ms vs 50μs to create a Schnorr signature, for example). There’s ample room for optimisation, though. Probably the lowest-hanging fruit is that scalar multiplication on secp256k1 can seemingly be made much more efficient via the so-called wNAF method that relies on precomputed points, such that we might be looking at more like 500μs to create a Schnorr signature, with a similar improvement for ECDSA. It would require slightly more annoying UX, probably warranting its own set of user-facing functions that would also accept a context argument, but does not appear difficult to implement.

A few things I observed or noted while writing this library:

  • The modular arithmetic functions for arbitrary-precision Integers contained in GHC.Num.Integer can be extremely fast, compared to hand-rolled alternatives. Things like integerPowMod# and integerRecipMod# absolutely fly, and will probably beat any hand-rolled variant.

  • Arbitrary-precision Integers are still slow, compared to fixed-width stuff that modern computers can positively chew through (this is not news to me, but still). I achieved a staggering speedup on some basic integer parsing by using a custom Word256 type (built from a bunch of Word64 values) under the hood, and converting to Integer only at the end.

    They can also be annoying when one wants to achieve constant-time execution of cryptographically-sensitive functions, for obvious reasons. It would be nice to have good fixed-width support for stuff like Word128, Word256, Word512, and so on – I briefly considered implementing and using a custom Word256 type for everything, but this would be a ton of work, and I’m not sure I could beat GHC’s native bigint support for e.g. modular multiplication, exponentiation, and inversion anyway. We’ll stick with plain-old Integer for the time being – it’s still no slouch.

  • Algorithmically constant-time operations can still fail to be constant-time in practice due to factors largely outside the programmer’s control. A good example is found in bit operations; looping through a bytestring and performing some “equivalent-looking” work on every byte may still result in execution time discrepancies between zero and nonzero bytes, for example, and these can be very hard to eliminate. This stuff can depend on the compiler or runtime, the architecture/processor used, etc.

  • The necessary organization of this kind of “catch-all” library is kind of unsatisfying. Rather than picking a single curve, and then implementing every feature one can possibly think of for it, it would intuitively be better to implement a generic curve library or libraries (for Weierstrass, Edwards, Montgomery, etc.), and then implement e.g. ECDSA or EdDSA or ECDH or or whatever as separate libraries depending on those generic curves as appropriate. One could then use everything in more of a plug-and-play fashion – this might be a design I’ll explore further in the future.

  • ByteString seems to provide a good user-facing “interface” for libraries like this. It’s reliable, familiar to Haskellers, has a great API, and is very well-tuned. It’s possible one might want to standardize on something else for internals, though; unboxed vectors are an obvious choice, though I would actually be inclined to use the primitive library’s PrimArrays directly, favouring simplicity, and eschewing vector’s harder-core optimisations.

    The idea here in any case would be that one would use ByteString only at the user-facing layer, and then work with PrimArrays (or whatever) everywhere internally. It’s perhaps worth exploring further – bytestring is very fast (strict bytestrings are, after all, merely pointers to cstrings), but so are PrimArrays, and mutation à la MutablePrimArray could be very helpful to have here and there.

    (FWIW, though, I’ve benchmarked PrimArray/MutablePrimArray in ppad-sha256 and found them to yield equivalent-or-slower performance compared to ByteString in that setting.)

The library has been tested on the Project Wycheproof and official BIP340 test vectors, as well as noble-secp256k1’s test suite, and care has been taken around timing of functions that operate on secret data. Kick the tires on it, if you feel so inclined!

(I mentioned this in my last post as well, but I’m indebted to Paul Miller’s noble cryptography project for this work, both as inspiration and also as a reference.)