This introduces a per-txin cache for sighash midstate computation to the script interpreter for legacy (bare) and P2SH.
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 12 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 12 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.
This concludes backport of core#32473
https://github.com/bitcoin/bitcoin/pull/32473/commits/92af9f74d74e76681f7d98f293eab226972137b4
https://github.com/bitcoin/bitcoin/pull/32473/commits/b221aa80a081579b8d3b460e3403f7ac0daa7139
https://github.com/bitcoin/bitcoin/pull/32473/commits/83950275eddacac56c58a7a3648ed435a5593328
Depends on D18848