Due to using the bitcoin serialization routines within CHashWriter, TapLeaf hasher is fed with an excessive byte, which contains the length of a hashed script.
Expected behavior
BIP341 refers TapLeaf hash calculation as tagged_hash function having reference implementation as https://github.com/bitcoin/bips/blob/b1791c24aa163eb6578d0bfaadcf44997484eeaf/bip-0340/reference.py#L25 :
def tagged_hash(tag: str, msg: bytes) -> bytes:
tag_hash = hashlib.sha256(tag.encode()).digest()
return hashlib.sha256(tag_hash + tag_hash + msg).digest()
Actual behavior
CScript, and its parent - prevector, are fed to CHashWriter as <compact length>||<prevector bytes> which leads to the wrong hash calculation compared to the reference implementation from BIP.
To reproduce
Here is the sample test which shows the behavior: https://github.com/layer1dot5/l15-core/blob/barebones/test/src/test_taptree.cpp
System information
Bitcoin v23.0 and master