lib: Add Taproot support to libconsensus #21158

pull jrawsthorne wants to merge 4 commits into bitcoin:master from jrawsthorne:libconsensus_taproot changing 5 files +141 −10
  1. jrawsthorne commented at 10:59 pm on February 11, 2021: none

    This change exposes a new function in libconsensus that allows passing spent outputs for Taproot verification. It also adds a VERIFY_TAPROOT flag.

    If spent outputs are not provided when the VERIFY_TAPROOT flag is enabled, there is a new error returned SPENT_OUTPUTS_REQUIRED. If the VERIFY_TAPROOT flag is enabled when verify or verify_with_amount then the same error is returned.

    I also included testing the consensus lib in the qa asset test.

    Closes #21133

  2. DrahtBot added the label Consensus on Feb 11, 2021
  3. jrawsthorne force-pushed on Feb 12, 2021
  4. jrawsthorne force-pushed on Feb 12, 2021
  5. jrawsthorne force-pushed on Feb 12, 2021
  6. jrawsthorne marked this as ready for review on Feb 12, 2021
  7. in src/script/bitcoinconsensus.cpp:16 in 883cac2efe outdated
    12@@ -13,13 +13,13 @@
    13 namespace {
    14 
    15 /** A class that deserializes a single CTransaction one time. */
    16-class TxInputStream
    17+class InputStream
    


    ariard commented at 1:09 pm on February 13, 2021:
    I would advice introducing the code style changes in their own commit. Also, I think it’s clearer to keep the Tx prefix, you have other class of inputs in the codebase beyond transaction like hash elements.
  8. in src/script/bitcoinconsensus.cpp:115 in 883cac2efe outdated
    107     } catch (const std::exception&) {
    108         return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
    109     }
    110 }
    111 
    112+int bitcoinconsensus_verify_script_with_spent_outputs(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
    


    ariard commented at 1:37 pm on February 13, 2021:

    Have a look on b7dbeb2, I think you should bump the API version, at least it was done for segwit support.

    I think you should also add a commit to document this change in doc/shared-libraries.md.

  9. in src/script/bitcoinconsensus.cpp:94 in 883cac2efe outdated
    87         CTransaction tx(deserialize, stream);
    88+        std::vector<CTxOut> spent_outputs;
    89+        if (spentOutputs != nullptr) {
    90+            InputStream spent_outputs_stream(PROTOCOL_VERSION, spentOutputs, spentOutputsLen);
    91+            spent_outputs_stream >> spent_outputs;
    92+        }
    


    ariard commented at 1:40 pm on February 13, 2021:
    Check the equality between inputs/outputs against tx.vin.size() (a bitcoinconsensus_ERR_OUTP_INDEX ?)
  10. ariard commented at 1:42 pm on February 13, 2021: member
    Thanks for adding this. If the API is bumped, take care to notify library users through mentioning the change in 0.22 release note (doc/release-noted.md)
  11. jrawsthorne commented at 1:46 am on February 14, 2021: none

    Thanks for adding this. If the API is bumped, take care to notify library users through mentioning the change in 0.22 release note (doc/release-noted.md) @ariard Thanks for the review. Do you have a suggestion about which section the change should be mentioned in?

  12. ariard commented at 10:06 pm on February 14, 2021: member
    @jrawsthorne “Tools and Utilities” sounds good to me.
  13. jrawsthorne force-pushed on Feb 14, 2021
  14. in doc/shared-libraries.md:18 in 01086293a7 outdated
    15+`bitcoinconsensus_version` returns an `unsigned int` with the API version *(currently `2`)*.
    16 
    17 #### Script Validation
    18 
    19-`bitcoinconsensus_verify_script` returns an `int` with the status of the verification. It will be `1` if the input script correctly spends the previous output `scriptPubKey`.
    20+`bitcoinconsensus_verify_script`, `bitcoinconsensus_verify_script_with_amounts` and `bitcoinconsensus_verify_script_with_spent_outputs` return an `int` with the status of the verification. It will be `1` if the input script correctly spends the previous output `scriptPubKey`.
    


    SomberNight commented at 5:56 am on February 17, 2021:

    there is a typo in the function name

    0`bitcoinconsensus_verify_script`, `bitcoinconsensus_verify_script_with_amount` and `bitcoinconsensus_verify_script_with_spent_outputs` return an `int` with the status of the verification. It will be `1` if the input script correctly spends the previous output `scriptPubKey`.
    
  15. in doc/shared-libraries.md:30 in 01086293a7 outdated
    26@@ -26,6 +27,28 @@ The interface is defined in the C header `bitcoinconsensus.h` located in `src/sc
    27 - `unsigned int flags` - The script validation flags *(see below)*.
    28 - `bitcoinconsensus_error* err` - Will have the error/success code for the operation *(see below)*.
    29 
    30+###### bitcoinconsensus_verify_script_with_amounts
    


    SomberNight commented at 5:59 am on February 17, 2021:
    same as above, typo in function name
  16. jrawsthorne force-pushed on Feb 17, 2021
  17. jrawsthorne requested review from ariard on Feb 18, 2021
  18. in doc/shared-libraries.md:72 in 28245a8641 outdated
    65@@ -42,6 +66,8 @@ The interface is defined in the C header `bitcoinconsensus.h` located in `src/sc
    66 - `bitcoinconsensus_ERR_DESERIALIZE` - An error deserializing `txTo`
    67 - `bitcoinconsensus_ERR_AMOUNT_REQUIRED` - Input amount is required if WITNESS is used
    68 - `bitcoinconsensus_ERR_INVALID_FLAGS` - Script verification `flags` are invalid (i.e. not part of the libconsensus interface)
    69+- `bitcoinconsensus_ERR_SPENT_OUTPUTS_REQUIRED` - Spent outputs are required if TAPROOT is used
    70+- `bitcoinconsensus_ERR_SPENT_OUTPUTS_MISMATCH` - Spent outputs size doesn't match tx inputs size
    71 
    72 ### Example Implementations
    


    ariard commented at 3:39 pm on February 23, 2021:
    Can you update this to point to rust-bitcoinconsensus :) ?
  19. ariard commented at 3:40 pm on February 23, 2021: member

    Code Review ACK 28245a8, thanks for the changes

    Up to you to take the nit.

  20. DrahtBot commented at 11:39 am on March 2, 2021: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  21. DrahtBot commented at 4:50 pm on March 15, 2021: contributor

    🕵️ @harding has been requested to review this pull request as specified in the REVIEWERS file.

  22. DrahtBot added the label Needs rebase on Apr 13, 2021
  23. jrawsthorne force-pushed on Apr 15, 2021
  24. DrahtBot removed the label Needs rebase on Apr 15, 2021
  25. ariard commented at 4:50 pm on April 16, 2021: member
    Code Review ACK 7c85ec9. Changes from last ACK is complying VerifyScript call to new MissingDataBehavior introduced by b77b0cc and mention of rust-bitcoin lib.
  26. DrahtBot added the label Needs rebase on Apr 20, 2021
  27. ariard commented at 5:33 pm on April 26, 2021: member
    @jrawsthorne I think this needs rebase, but that would be cool to land this for downstream projects.
  28. jrawsthorne force-pushed on Apr 26, 2021
  29. jrawsthorne commented at 5:44 pm on April 26, 2021: none
    Thanks @ariard, rebased
  30. DrahtBot removed the label Needs rebase on Apr 26, 2021
  31. ariard commented at 1:59 pm on April 30, 2021: member

    Code Review ACK 096ab5c.

    Diff since last ack is swallowing release-note changes.

  32. ariard commented at 2:00 pm on April 30, 2021: member
    @SomberNight I guess you’re also interested to for downstream support, if you have time to review again, that would be cool :)
  33. DrahtBot added the label Needs rebase on Jun 12, 2021
  34. refactor: Rename variables in preparation for generic deserialization 78447f3d11
  35. jrawsthorne force-pushed on Jun 12, 2021
  36. DrahtBot removed the label Needs rebase on Jun 12, 2021
  37. in src/test/script_tests.cpp:1763 in d0c6b1a9b6 outdated
    1758         for (const auto flags : ALL_CONSENSUS_FLAGS) {
    1759             // If a test is supposed to fail with test_flags, it should also fail with any superset thereof.
    1760             if ((flags & test_flags) == test_flags) {
    1761                 bool ret = VerifyScript(tx.vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.vin[idx].scriptWitness, flags, txcheck, nullptr);
    1762                 BOOST_CHECK(!ret);
    1763+                #if defined(HAVE_CONSENSUS_LIB)
    


    sipa commented at 3:48 am on June 13, 2021:
    Generally macro statements aren’t indented like the code they’re included in.

    jrawsthorne commented at 1:28 pm on June 13, 2021:
    Thanks, didn’t know that. Fixed in d82e5de6a41794cc8fd1e3afaa12af26bb7be926
  38. in src/script/bitcoinconsensus.h:58 in d0c6b1a9b6 outdated
    54@@ -53,9 +55,11 @@ enum
    55     bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9), // enable CHECKLOCKTIMEVERIFY (BIP65)
    56     bitcoinconsensus_SCRIPT_FLAGS_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10), // enable CHECKSEQUENCEVERIFY (BIP112)
    57     bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS             = (1U << 11), // enable WITNESS (BIP141)
    58+    bitcoinconsensus_SCRIPT_FLAGS_VERIFY_TAPROOT             = (1U << 17), // enable TAPROOT (BIP341-343)
    


    sipa commented at 3:56 am on June 13, 2021:
    BIP341-342 (343 isn’t a thing).

    jrawsthorne commented at 1:28 pm on June 13, 2021:
    Oops, fixed in d82e5de6a41794cc8fd1e3afaa12af26bb7be926
  39. sipa commented at 3:57 am on June 13, 2021: member
    utACK 698391966afd0004d6d31fc590ffb8e4222408b8
  40. lib: Add Taproot support to libconsensus d82e5de6a4
  41. docs: Add docs for additional libconsensus functions 3dd932ef22
  42. docs: Link to rust-bitcoinconsensus c23996ad35
  43. jrawsthorne force-pushed on Jun 13, 2021
  44. jrawsthorne commented at 1:29 pm on June 13, 2021: none
    Thanks for your review @sipa, I’ve addressed your comments. Also pinging @ariard
  45. in src/script/bitcoinconsensus.cpp:90 in c23996ad35
    83@@ -83,6 +84,14 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP
    84     try {
    85         TxInputStream stream(PROTOCOL_VERSION, txTo, txToLen);
    86         CTransaction tx(deserialize, stream);
    87+        std::vector<CTxOut> spent_outputs;
    88+        if (spentOutputs != nullptr) {
    89+            TxInputStream spent_outputs_stream(PROTOCOL_VERSION, spentOutputs, spentOutputsLen);
    90+            spent_outputs_stream >> spent_outputs;
    


    luke-jr commented at 1:56 am on June 23, 2021:
    Might be nicer to let the caller pass in an array rather than have to serialise the vector?

    jrawsthorne commented at 11:17 am on July 4, 2021:
    Correct me if I’m wrong but I thought it would have to be a vector and that can’t be exposed as part of the C API

    sipa commented at 6:18 pm on August 20, 2021:
    It could take in a pointer to an array of {pointer to utxo, length} structs or so, as well as the number of inputs. That’s not particularly convenient to use, but neither is the current interface (as it probably requires serialization code on the caller side).
  46. in doc/shared-libraries.md:46 in c23996ad35
    41+- `const unsigned char *scriptPubKey` - The previous output script that encumbers spending.
    42+- `unsigned int scriptPubKeyLen` - The number of bytes for the `scriptPubKey`.
    43+- `int64_t amount` - The amount spent in the input
    44+- `const unsigned char *txTo` - The transaction with the input that is spending the previous output.
    45+- `unsigned int txToLen` - The number of bytes for the `txTo`.
    46+- `const unsigned char *spentOutputs` - Previous outputs spent in the transaction
    


    luke-jr commented at 1:56 am on June 23, 2021:
    This leaves me guessing how to call this function.

    jrawsthorne commented at 11:17 am on July 4, 2021:
    Do you want to add something like “serialized as a vector of TxOut” or something more substantial?
  47. luke-jr changes_requested
  48. DrahtBot added the label Needs rebase on Jul 21, 2021
  49. DrahtBot commented at 10:00 am on July 21, 2021: contributor

    🐙 This pull request conflicts with the target branch and needs rebase.

    Want to unsubscribe from rebase notifications on this pull request? Just convert this pull request to a “draft”.

  50. DrahtBot commented at 12:28 pm on December 22, 2021: contributor
    • Is it still relevant? ➡️ Please solve the conflicts to make it ready for review and to ensure the CI passes.
    • Is it no longer relevant? ➡️ Please close.
    • Did the author lose interest or time to work on this? ➡️ Please close it and mark it ‘Up for grabs’ with the label, so that it can be picked up in the future.
  51. MarcoFalke added the label Up for grabs on Apr 29, 2022
  52. MarcoFalke commented at 2:17 pm on April 29, 2022: member
    Marked “up for grabs”
  53. fanquake closed this on May 12, 2022

  54. bitcoin locked this on May 12, 2023
  55. fanquake removed the label Up for grabs on Sep 27, 2023
  56. fanquake removed the label Needs rebase on Sep 27, 2023

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: 2024-07-05 19:13 UTC

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