HomePhabricator

script: (optimization) introduce sighash midstate caching

Description

script: (optimization) introduce sighash midstate caching

Summary:
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

Test Plan: ninja all check-all bitcoin-fuzzers

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D18849

Details

Provenance
Pieter Wuille <pieter@wuille.net>Authored on Apr 25 2025, 17:31
PiRKCommitted on Fri, Oct 31, 09:57
PiRKPushed on Fri, Oct 31, 09:57
Reviewer
Restricted Project
Differential Revision
D18849: script: (optimization) introduce sighash midstate caching
Parents
rABC8f7582f2e39e: script: (refactor) prepare for introducing sighash midstate cache
Branches
Unknown
Tags
Unknown