TapLeaf hash is calculated differently from BIP340 reference implementation #25103

issue l2xl opened this issue on May 10, 2022
  1. l2xl commented at 8:15 PM on May 10, 2022: none

    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

  2. l2xl added the label Bug on May 10, 2022
  3. sipa commented at 8:20 PM on May 10, 2022: member

    BIP341 explicitly mentions the fact that the script length is included in the tapleaf hash.

    Specifically:

    Let k0 = hashTapLeaf(v || compact_size(size of s) || s); also call it the tapleaf hash.

  4. l2xl commented at 8:46 PM on May 10, 2022: none

    Makes sense... My apologies.

  5. l2xl closed this on May 10, 2022

  6. DrahtBot locked this on May 10, 2023
Contributors
Labels

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-03 15:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me