Write-up for TinySig
Prologue
Although the challenge is only a few lines of code, solving it involves traveling back ~25 years to when the parameters of the secp256k1 curve were chosen.
Before we even get to the puzzle, we can take a closer look at the specification of secp256k1 (the curve used by Bitcoin/Ethereum). This was released by Certicom in 2000, but unfortunately, the parameter selection process wasn't super descriptive:
Parameters associated with a Koblitz curve admit especially efficient implementation. The name Koblitz curve is best-known when used to describe binary anomalous curves over which have , [9]. Here it is generalized to refer also to curves over which possess an efficiently computable endomorphism [7]. The recommended parameters associated with a Koblitz curve were chosen by repeatedly selecting parameters admitting an efficiently computable endomorphism until a prime order curve was found.
For discussion about this, we can look at this Bitcoin Talk discussion. Despite the limited documentation, people were able to reverse engineer reasonable explanations for the parameter choices, including:
- being chosen as a generalized Mersenne prime (helps with computing modular reductions).
- The coefficients ( and ) of the curve equation being the smallest values you can choose that result in a prime order (the chosen and also allow for the GLV method optimization, which is even more possible explanation).
But despite all this reverse engineering, there was/is no good explanation for how the generator point was selected. Because of how the other parameters were chosen, pretty much any point on the curve could have been used as , so why not something "normal looking"?
Well, there still isn't a great answer for this. In fact, even more questions arose when Gregory Maxwell discovered the point has an anomalously small -coordinate, as he describes here. This -coordinate is basically the same in for every secp___k1 curve, which implies there was a common initial -coordinate point that was slightly altered and then doubled to get for each curve. The value is ~160 bits, which means it could be a SHA1 output.
0x00000000000000000000000048ce563f89a0ed9414f5aa28ad0d96d6795f9c62
0x00000000000000000554123b78ce563f89a0ed9414f5aa28ad0d96d6795f9c66
0x00000000000000000000003b78ce563f89a0ed9414f5aa28ad0d96d6795f9c63
for secp160k1, secp192k1, and secp256k1.
This only adds to the mystery, since the "doubling" was never documented, doesn't seem necessary, and we don't know this SHA1 preimage. Regardless, the actual choice of doesn't affect the security of the curve in any way, but it does allow a very interesting "trick" in ECDSA.
As you may know, the first step in ECDSA is to choose an integer to get the coordinate of (this is ).
Choosing your own value in the wild is a bad idea, since there are several ways to leak your private key by doing this.
But with a toy private key, the property of we just discussed tells us that setting will make the value equal the -coordinate of , which has an impossibly large number of zeroes—far more zeroes than you would get with any brute-force method.
This trick was used all the way back in 2015, when Bitcoin users leveraged this property to save bytes on ransactions that swept dust from accounts with known private keys.
Solving the puzzle
At this point, we can finally take a look at the CTF. As you can see, users submit bytecode that returns a valid msg hash + signature for a given private key. The challenge hardcodes a different s
value for everyone, and requires that the r
value has 9 leading zero bytes.
We know that choosing will give us our and value with the required zero bytes. To find the right value, we can rearrange the equation for . Also, by looking at other players' solutions on-chain, you could have used this equation to solve for .
To actually submit the solution, the easiest method to return h
, v
and r
in only 32 bytes of EVM code is to forward the call to another contract that has these values hardcoded. This is what the helper contract @0xkarmacoma and @eth_call deployed does.
Epilogue
For anyone interested in the secp256k1 parameter lore, I also recommend looking at this video. I think it's super cool that every single crypto transaction uses a value that is still shrouded in some mystery. Hopefully, we will figure out the entire story one day.