elements/test/functional/test_framework
merge-script eb64d2486f Merge bitcoin/bitcoin#32473: Introduce per-txin sighash midstate cache for legacy/p2sh/segwitv0 scripts
83950275eddacac56c58a7a3648ed435a5593328 qa: unit test sighash caching (Antoine Poinsot)
b221aa80a081579b8d3b460e3403f7ac0daa7139 qa: simple differential fuzzing for sighash with/without caching (Antoine Poinsot)
92af9f74d74e76681f7d98f293eab226972137b4 script: (optimization) introduce sighash midstate caching (Pieter Wuille)
8f3ddb0bccebc930836b4a6745a7cf29b41eb302 script: (refactor) prepare for introducing sighash midstate cache (Pieter Wuille)
9014d4016ad9351cb59b587541895e55f5d589cc tests: add sighash caching tests to feature_taproot (Pieter Wuille)

Pull request description:

  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.

  The PR implements two different approaches:
  * The initial commits introduce the caching effect always, for both consensus and relay relation validation. Despite being primarily intended for improving the situation for standard transactions only, 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).
  * The final commit changes the behavior to only using the cache for non-consensus script validation. I'm open to feedback about whether adding this commit is worth it.

  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).

ACKs for top commit:
  achow101:
    ACK 83950275eddacac56c58a7a3648ed435a5593328
  dergoegge:
    Code review ACK 83950275eddacac56c58a7a3648ed435a5593328
  darosior:
    re-ACK 83950275eddacac56c58a7a3648ed435a5593328

Tree-SHA512: 65ae8635429a4d563b19969bac8128038ac2cbe01d9c9946abd4cac3c0780974d1e8b9aae9bb83f414e5d247a59f4a18fef5b37d93ad59ed41b6f11c3fe05af4
2025-11-21 15:55:19 +00:00
..
__init__.py
address.py Merge 7164e00e1b into merged_master (Bitcoin PR bitcoin/bitcoin#24324) 2023-06-30 17:34:42 +00:00
authproxy.py test: Remove Windows workaround in authproxy 2021-09-25 09:33:16 +02:00
bdb.py scripted-diff: Bump copyright headers 2021-11-10 11:10:24 +01:00
bip340_test_vectors.csv tests: add BIP340 Schnorr signature support to test framework 2020-10-12 17:18:47 -07:00
blocktools.py Merge 807169e10b into merged_master (Bitcoin PR bitcoin/bitcoin#24035) 2023-06-20 09:57:58 +00:00
coverage.py scripted-diff: Bump copyright headers 2021-11-10 11:10:24 +01:00
descriptors.py Output a descriptor in createmultisig and addmultisigaddress 2020-01-30 23:55:36 -05:00
key.py Merge 5ccab7187b into merged_master (Bitcoin PR bitcoin/bitcoin#23394) 2023-05-26 09:14:20 +00:00
liquid_addr.py fix len check in blech32 decode() in test framework 2019-09-13 20:24:23 +05:00
messages.py Merge 807169e10b into merged_master (Bitcoin PR bitcoin/bitcoin#24035) 2023-06-20 09:57:58 +00:00
muhash.py test: Change MuHash Python implementation to match cpp version again 2020-12-22 01:48:34 +01:00
netutil.py test: remove import socket in test_ipv6_local 2022-02-14 19:27:33 -03:00
p2p.py Merge 63c0d0e937 into merged_master (Bitcoin PR bitcoin/bitcoin#21327) 2023-06-01 05:27:06 +00:00
ripemd160.py Add pure Python RIPEMD-160 2021-12-08 14:29:34 -05:00
script.py Add Simplicity Deployment structure 2024-10-07 14:39:10 -04:00
script_util.py scripted-diff: Bump copyright headers 2021-11-10 11:10:24 +01:00
segwit_addr.py Use Bech32m encoding for v1+ segwit addresses 2021-08-17 22:32:47 +00:00
siphash.py
socks5.py scripted-diff: Update copyright in ./test 2019-03-02 10:58:35 -05:00
test_framework.py Merge f1ce67f09f into merged_master (Bitcoin PR bitcoin/bitcoin#19013) 2023-07-02 15:42:51 +00:00
test_node.py Merge 267917f563 into merged_master (Bitcoin PR bitcoin/bitcoin#23304) 2023-07-03 10:06:51 +00:00
test_shell.py TestShell: Return self from setup() 2019-11-05 12:55:52 +01:00
util.py Merge eca694a4e7 into merged_master (Bitcoin PR bitcoin/bitcoin#24239) 2023-06-30 10:01:50 +00:00
wallet.py Re-activate assertion which was disabled in the merge of bitcoin/bitcoin#23371 in 2110cca 2023-08-01 10:28:20 +02:00
wallet_util.py Merge bitcoin/bitcoin#32473: Introduce per-txin sighash midstate cache for legacy/p2sh/segwitv0 scripts 2025-11-21 15:55:19 +00:00