Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: implement BIP 341 and 342 a.k.a complete taproot and tapscript consensus verification logic #1787

Merged
merged 32 commits into from
Mar 16, 2022

Commits on Mar 16, 2022

  1. txscript: add taproot script type

    Add the WitnessV1TaprootTy script class and return it from
    GetScriptClass / typeOfScript.
    
    Bump the btcutil dep to leverage new taproot address type.
    buck54321 authored and Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    bfd0f4a View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    37964e5 View commit details
    Browse the repository at this point in the history
  3. txscript: add taproot support to PayToAddrScript

    Add taproot address handling in PayToAddrScript. Adds a test and
    also some missing tests for p2wsh and p2wpkh addresses.
    buck54321 authored and Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    3ddf1b5 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    cfe801f View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    81a546b View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    30d9327 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    6ecc72e View commit details
    Browse the repository at this point in the history
  8. txscript: implement BIP 341+342 segwit v1 taproot+tapscript

    In this commit, we implement the new BIP 341+342 taproot sighash digest
    computation. The digest is similar, but re-orders some fragments and
    also starts to commit to the input values of all the transactions in the
    SIGHASH_ALL case. A new implicit sighash flag, SIGHASH_DEFAULT has been
    added that allows signatures to always be 64-bytes for the common case.
    
    The hashcache has been updated as well to store both the v0 and v1 mid
    state hashes. The v0 hashes are a double-sha of the contents, while the
    v1 hash is a single sha. As a result, if a transaction spends both v0
    and v1 inputs, then we 're able to re-use all the intermediate hashes.
    
    As the sighash computation needs the input values and scripts, we create
    an abstraction: the PrevOutFetcher to give the caller flexibility w.r.t
    how this is done. We also create a `CannedPrevOutputFetcher` that holds
    the information in a map for a single input.
    
    A series of function options are also added to allow re-use of the same
    base sig hash calculation for both BIP 341 and 342.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    e781b66 View commit details
    Browse the repository at this point in the history
  9. txscript: update SigCache to cache both ECDSA and Schnorr signatures

    In this commit, we make the sigCache slightly more general in order to
    be able to cache both ECDSA and Schnorr signatures. The cache is now
    based off of byte slices (the values) rather than the direct objects. We
    rely on the fact that the sighash for ecdsa and the schnorr types are
    distinct, so we can keep using the same top-level sighash key.
    
    In the future with Go type params, we can use a type param here instead
    as they all have an `IsEqual` method.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    1cd509d View commit details
    Browse the repository at this point in the history
  10. txscript: introduce new signatureVerifier interface to abstract over …

    …schnorr/ecdsa
    
    In this commit, we add a new signatureVerifier interface that will allow
    us to consolidate a lot of code as we'll now have 4 distinct sig+sighash
    types to verify:
      1. pre-segwit
      2. segwit v0
      3. segwit v1 (taproot key spend)
      4. tapscript spends
    
    We'll need to be able to handle 3 of the cases for the modified
    OP_CHECKSIG operator. This new abstraction allows us to keep the
    implementation of the function somewhat succinct.
    
    In this commit we implement a verifier for #3 which is needed to verify
    the top-level taproot keyspend. We expose the verifier using a new
    VerifyTaprootKeySpend function.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    abeaf4e View commit details
    Browse the repository at this point in the history
  11. txscript: use new signature verifiers for existing CHECKSIG ops

    In this commit, we use the recently added checksig verifiers to validate
    signatures for pre-segwit, and segwit v0 scripts.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    1ac34b7 View commit details
    Browse the repository at this point in the history
  12. txscript: add new functions for signing a top-level taproot output

    In this commit, we add two new functions: one for signing a raw
    top-level taproot keyspend, and another for generating a valid witness
    for a keyspend.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    938c193 View commit details
    Browse the repository at this point in the history
  13. txscript: add VM verification logic for top-level taproot keyspends

    In this commit, we add the initial verification logic for top-level
    taproot keyspends. Keyspends use the base BIP 341 sighash digest and
    don't require any tapscript level functionality for validation.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    2ac743d View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    a7c3db4 View commit details
    Browse the repository at this point in the history
  15. txscript: add new ScriptHasOpSuccess utility method

    We'll use this to examine if a script has any OP_SUCCESS op codes during
    pre-processing before we attempt full tapscript execution.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    11dd820 View commit details
    Browse the repository at this point in the history
  16. txscript: introduce new ControlBlock struct along w/ parsing routine

    In this commit, we add a new struct to represent the ControlBlock
    structure used to feed in the tapscript leaf inclusion proof into the
    witness tack. The `ParseControlBlock` parses a would-be control block
    and returns an error if it's incorrectly formatted.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    5c4a29b View commit details
    Browse the repository at this point in the history
  17. txscript: add VerifyTaprootLeafCommitment function

    In this commit, we add a new function to verify the taproot merkle
    commitment of a given tapscript leaf. Along the way we add some helper
    functions which can be used to construct a taproot output given the raw
    script root.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    37f8c8b View commit details
    Browse the repository at this point in the history
  18. txscript: add new RawTxInTapscriptSignature to generate tapsript sigs

    In this commit, we add a new function `RawTxInTapscriptSignature` that
    will be used to generate signatures in the _tapscript_ context. Note
    that this differs from top-level taproot as a distinct sighash is used,
    and we _always_ accept a root hash to perform the proper tweak.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    6fc4199 View commit details
    Browse the repository at this point in the history
  19. txscript: add AssembleTaprootScriptTree func for creating input witne…

    …sses
    
    In this commit, we add a new AssembleTaprootScriptTree function that
    given a list of tapscript leaves, generates a valid tapscript root,
    along with the auxiliary proof data needed to spend each output.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    17e4609 View commit details
    Browse the repository at this point in the history
  20. txscript: implement script path verification

    In this commit, we use the recently added control block and script tree
    verification+generation routines to implement full script path
    verification within the VM. This includes verifying the script reveal
    commitment, and recursing one layer deeper to execute the revealed
    witness script as specified by BIP 342.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    c1eb150 View commit details
    Browse the repository at this point in the history
  21. Configuration menu
    Copy the full SHA
    a7a8ad7 View commit details
    Browse the repository at this point in the history
  22. Configuration menu
    Copy the full SHA
    3c6be73 View commit details
    Browse the repository at this point in the history
  23. txscript: add new OpcodePosition method to tokenizer to save code sep…

    … position
    
    We'll need this to properly generate the sighash during tapscript
    validation later
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    a4beed9 View commit details
    Browse the repository at this point in the history
  24. txscript: implement OP_CHECKSIG semantics for tapscript validation

    In this commit, we implement the new checksig semantics as part of
    tapscript validation. Namely:
    
      * OP_CHECKSIGVERIFY no longer pops the OP_TRUE off the stack (TODO(roasbeef): verify))
    
      * the new sig ops semantics are added where each sig deducts 50 from a
        starting budget of 50+the weight of the witness
    
      * NULLFAIL is always enforced, meaning invalid sigs MUST be an empty sig array
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    3ce6130 View commit details
    Browse the repository at this point in the history
  25. txscript: implement OP_CHECKSIGADD

    In this commit, we implement OP_CHECKSIGADD which replaces
    OP_CHECKMULTISIG* in the tapscript execution environment.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    5f8660e View commit details
    Browse the repository at this point in the history
  26. txscript: add taproot JSON success/fail reference tests

    In this commit, we add a total of 2760 taproot reference tests generated
    by the bitcoind functional tests located at:
    https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_taproot.py.
    The tests aren't deterministic (fresh private keys are generated), so we
    time we go to update the set of tests, we'll end up with fresh hashes
    (the file name is the sha1 of the raw json test) and tests.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    79c314d View commit details
    Browse the repository at this point in the history
  27. chaincfg: add taproot BIP deployment parameters

    In this commit, we add the deployment parameters of taproot as specified
    in the deployment section of BIp 341:
    https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#deployment.
    
    Take note of the custom activation threshold, as well as the specified
    min activation heights for mainnet only.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    ba9fb8e View commit details
    Browse the repository at this point in the history
  28. Configuration menu
    Copy the full SHA
    23cf18b View commit details
    Browse the repository at this point in the history
  29. Configuration menu
    Copy the full SHA
    f7f7bb3 View commit details
    Browse the repository at this point in the history
  30. Configuration menu
    Copy the full SHA
    6ab97a3 View commit details
    Browse the repository at this point in the history
  31. Configuration menu
    Copy the full SHA
    99e4e00 View commit details
    Browse the repository at this point in the history
  32. build: add temporary replace directives for btcec+chainhash

    This PR includes some changes to them, so we'll need to use a temporary
    replace directives to ensure the build passes.
    Roasbeef committed Mar 16, 2022
    Configuration menu
    Copy the full SHA
    30d58b9 View commit details
    Browse the repository at this point in the history