This introduces a per-txin cache for sighash midstate computation to the script interpreter for legacy (bare), P2SH, P2WSH, and (as collateral effect, but not actually useful) P2WPKH. This reduces the impact of certain types of quadratic hashing attacks that use standard transactions. It is not known to improve the situation for attacks involving non-standard transaction attacks.
The cache works by remembering for each of the 6 sighash modes a (scriptCode, midstate)
tuple, which gives a midstate CSHA256
object right before the appending of the sighash type itself (to permit all 256, rather than just the 6 ones that match the modes). The midstate is only reused if the scriptCode
matches. This works because - within a single input - only the sighash type and the scriptCode
affect the actual sighash used.
Despite being primarily intended for improving the situation for standard transactions only, the logic as implemented here is active for all script execution, including non-standard transactions, and including for consensus validation (for transactions seen in blocks). I chose this approach as the code paths are already largely common between the two, and this approach I believe involves fewer code changes than a more targetted approach, and furthermore, it should not hurt (it may even help common multisig cases slightly). I’m open to discussing alternatives, however.
Functional tests are included that construct contrived cases with many sighash types (standard and non-standard ones) and OP_CODESEPARATOR
s in all script types (including P2TR, which isn’t modified by this PR).