bitcoin-util: Add evalscript subcommand #35788

Open
ajtowns wants to merge 4 commits from 202607-evalscript into master AGit
ajtowns commented 2026-07-24 01:56:45 +00:00 (Migrated from github.com)

Adds an evalscript subcommand for using bitcoin's consensus logic to evaluate script fragments. Intended for debugging and development.

Examples:

# check a script fragment's behaviour
$ bitcoin-util evalscript '1 2 ADD'
{
  "script": {
    "asm": "1 2 OP_ADD",
    "hex": "515293",
    "type": "nonstandard"
  },
  "sigversion": "witness_v0",
  "script_flags": [
    "CHECKLOCKTIMEVERIFY",
    "CHECKSEQUENCEVERIFY",
    "DERSIG",
    "NULLDUMMY",
    "P2SH",
    "TAPROOT",
    "WITNESS"
  ],
  "dummy_sig_checker": true,
  "stack_after": [
    "03"
  ],
  "sigop_cost": 0,
  "success": true
}

# validate a real signature
$ bitcoin-util -sigversion=base \
    -spentoutput=00f2052a0100000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac \
    -tx=0100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000 \
    evalscript '0x410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3 CHECKSIG' \
    '304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901'
{
  "script": {
    "asm": "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3 OP_CHECKSIG",
    "hex": "410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac",
    "type": "pubkey"
  },
  "sigversion": "base",
  "script_flags": [
    "CHECKLOCKTIMEVERIFY",
    "CHECKSEQUENCEVERIFY",
    "DERSIG",
    "NULLDUMMY",
    "P2SH",
    "TAPROOT",
    "WITNESS"
  ],
  "dummy_sig_checker": false,
  "stack_after": [
    "01"
  ],
  "sigop_cost": 4,
  "success": true
}
Adds an `evalscript` subcommand for using bitcoin's consensus logic to evaluate script fragments. Intended for debugging and development. Examples: ``` # check a script fragment's behaviour $ bitcoin-util evalscript '1 2 ADD' { "script": { "asm": "1 2 OP_ADD", "hex": "515293", "type": "nonstandard" }, "sigversion": "witness_v0", "script_flags": [ "CHECKLOCKTIMEVERIFY", "CHECKSEQUENCEVERIFY", "DERSIG", "NULLDUMMY", "P2SH", "TAPROOT", "WITNESS" ], "dummy_sig_checker": true, "stack_after": [ "03" ], "sigop_cost": 0, "success": true } # validate a real signature $ bitcoin-util -sigversion=base \ -spentoutput=00f2052a0100000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac \ -tx=0100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000 \ evalscript '0x410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3 CHECKSIG' \ '304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901' { "script": { "asm": "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3 OP_CHECKSIG", "hex": "410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac", "type": "pubkey" }, "sigversion": "base", "script_flags": [ "CHECKLOCKTIMEVERIFY", "CHECKSEQUENCEVERIFY", "DERSIG", "NULLDUMMY", "P2SH", "TAPROOT", "WITNESS" ], "dummy_sig_checker": false, "stack_after": [ "01" ], "sigop_cost": 4, "success": true } ```
DrahtBot commented 2026-07-24 01:56:52 +00:00 (Migrated from github.com)

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Stale ACK jeanpablojp

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36603 (Improve loadtxoutset performance by ajtowns)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35788 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35788.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35788#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Stale ACK | [jeanpablojp](https://github.com/bitcoin/bitcoin/pull/35788#issuecomment-5346218321) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36603](https://github.com/bitcoin/bitcoin/pull/36603) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36603.svg"></sub> (Improve loadtxoutset performance by ajtowns) 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
ajtowns commented 2026-07-24 02:02:13 +00:00 (Migrated from github.com)

Previously included in bitcoin-inquisition (https://github.com/bitcoin-inquisition/bitcoin/pull/58, also https://github.com/bitcoin-inquisition/bitcoin/pull/64 and https://github.com/bitcoin-inquisition/bitcoin/pull/79).

Makes minimal changes to consensus logic, but still has to duplicate a bunch of code to get equivalent behaviour, might be worthwhile simplifying that API in followups, and potentially exposing as a kernel API of some sort?

Previously included in bitcoin-inquisition (https://github.com/bitcoin-inquisition/bitcoin/pull/58, also https://github.com/bitcoin-inquisition/bitcoin/pull/64 and https://github.com/bitcoin-inquisition/bitcoin/pull/79). Makes minimal changes to consensus logic, but still has to duplicate a bunch of code to get equivalent behaviour, might be worthwhile simplifying that API in followups, and potentially exposing as a kernel API of some sort?
jeanpablojp commented 2026-08-19 18:16:16 +00:00 (Migrated from github.com)

tACK a64f2ac5b1

Ran tool_utils, the script unit tests and feature_taproot on the head, and both examples from the description reproduce here.

The PR description mentions -tx, but I noticed no case in bitcoin-util-test.json uses -tx, so that path goes uncovered. To confirm, I disabled the -input range check and the suite still passes. Would it be worth adding a case like that?

tACK a64f2ac5b11ac95a43d11c2846e461de0b6eb48a Ran tool_utils, the script unit tests and feature_taproot on the head, and both examples from the description reproduce here. The PR description mentions `-tx`, but I noticed no case in bitcoin-util-test.json uses `-tx`, so that path goes uncovered. To confirm, I disabled the `-input` range check and the suite still passes. Would it be worth adding a case like that?
First-time contributor

4 additional verified findings in the Review report.

Earlier concerns neither republished nor resolved: 1 minor finding. See the Run archive linked from the Review report for their assessments.

🟠 Minor · evalscript omits the annex length prefix

With -tx -sigversion=tapscript, evalscript reconstructs the wrong signature message for annex-bearing inputs and can reject otherwise-valid Schnorr signatures; annexes are non-standard for relay.

Location: src/bitcoin-util.cpp:384 (head).

🟠 Minor · Negative -spentoutput amounts can abort evalscript

A user-supplied negative prevout amount can make a witness_v0 signature check abort bitcoin-util instead of reporting a normal argument error.

Location: src/bitcoin-util.cpp:373 (head).

🟠 Minor · Detached tapscript budget omits the minimum control-block weight

The hidden, approximate tapscript allowance omits at least 34 serialized control-block bytes, so near-budget fragments can fail even when a real spend with the same stack and script has sufficient validation weight.

Location: src/bitcoin-util.cpp:409 (head).

🔵 Suggestion · Hex autodetection changes valid ASM inputs

evalscript 10 executes raw byte 0x10 instead of OP_10, and adding whitespace changes which parser handles that same ASM token.

Location: src/bitcoin-util.cpp:307 (head).

🔵 Suggestion · Real-checker fixtures cover only base sighashing

Both real-checker fixtures select base, so neither prevout-amount wiring nor tapscript annex and tapleaf reconstruction is tested.

Location: test/functional/data/util/bitcoin-util-test.json:903 (head).

🔵 Suggestion · Dummy-mode fixtures do not distinguish execution rules

The tapscript, custom-flags and empty-stack fixtures cannot detect incorrect interpreter forwarding or rejection of only empty witness stacks instead of all non-singleton stacks.

Location: test/functional/data/util/bitcoin-util-test.json:855 (head).

Review coverage was incomplete.


Reviewed: f0acf4ed7e81 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:806938d40e279b6d701b2e78128bb1a09b55193a0b697245a1dce6241d3081c1:3607c760b63c01fb7f1f349adfc3fd0614b148a7df02e42f2f0edc2d448925a1 --> <!-- ralph-base:dd96035e031edf56fcf66a09d443f257a346af1f --> <!-- ralph-head:f0acf4ed7e81415834c067bccf34696e7f14ccc5 --> <!-- ralph-case:06d044f8577ecf5f37f587ea017ef96846379b3642c11d913c48034b587299a3 --> [4 additional verified findings in the Review report](<https://review.fish.foo/report/35788-f0acf4ed7e81415834c067bccf34696e7f14ccc5-3237-5.html>). Earlier concerns neither republished nor resolved: 1 minor finding. See the Run archive linked from the Review report for their assessments. **🟠 Minor · `evalscript` omits the annex length prefix** With `-tx -sigversion=tapscript`, `evalscript` reconstructs the wrong signature message for annex-bearing inputs and can reject otherwise-valid Schnorr signatures; annexes are non-standard for relay. Location: [`src/bitcoin-util.cpp:384`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f0acf4ed7e81415834c067bccf34696e7f14ccc5/src/bitcoin-util.cpp#L384>) (head). **🟠 Minor · Negative `-spentoutput` amounts can abort `evalscript`** A user-supplied negative prevout amount can make a `witness_v0` signature check abort `bitcoin-util` instead of reporting a normal argument error. Location: [`src/bitcoin-util.cpp:373`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f0acf4ed7e81415834c067bccf34696e7f14ccc5/src/bitcoin-util.cpp#L373>) (head). **🟠 Minor · Detached tapscript budget omits the minimum control-block weight** The hidden, approximate tapscript allowance omits at least 34 serialized control-block bytes, so near-budget fragments can fail even when a real spend with the same stack and script has sufficient validation weight. Location: [`src/bitcoin-util.cpp:409`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f0acf4ed7e81415834c067bccf34696e7f14ccc5/src/bitcoin-util.cpp#L409>) (head). **🔵 Suggestion · Hex autodetection changes valid ASM inputs** `evalscript 10` executes raw byte `0x10` instead of `OP_10`, and adding whitespace changes which parser handles that same ASM token. Location: [`src/bitcoin-util.cpp:307`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f0acf4ed7e81415834c067bccf34696e7f14ccc5/src/bitcoin-util.cpp#L307>) (head). **🔵 Suggestion · Real-checker fixtures cover only `base` sighashing** Both real-checker fixtures select `base`, so neither prevout-amount wiring nor tapscript annex and tapleaf reconstruction is tested. Location: [`test/functional/data/util/bitcoin-util-test.json:903`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f0acf4ed7e81415834c067bccf34696e7f14ccc5/test/functional/data/util/bitcoin-util-test.json#L903>) (head). **🔵 Suggestion · Dummy-mode fixtures do not distinguish execution rules** The tapscript, custom-flags and empty-stack fixtures cannot detect incorrect interpreter forwarding or rejection of only empty witness stacks instead of all non-singleton stacks. Location: [`test/functional/data/util/bitcoin-util-test.json:855`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/f0acf4ed7e81415834c067bccf34696e7f14ccc5/test/functional/data/util/bitcoin-util-test.json#L855>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`f0acf4ed7e81`](<https://git.fish.foo/bitcoin/bitcoin/commit/f0acf4ed7e81415834c067bccf34696e7f14ccc5>) · [Review report](<https://review.fish.foo/report/35788-f0acf4ed7e81415834c067bccf34696e7f14ccc5-3237-5.html>) · [Previous reports](<https://review.fish.foo/traces/35788/>) [permalink](<https://review.fish.foo/report/35788-f0acf4ed7e81415834c067bccf34696e7f14ccc5-3237-5.html>)
DrahtBot commented 2026-09-29 13:53:40 +00:00 (Migrated from github.com)

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

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
ajtowns commented 2026-10-01 01:26:57 +00:00 (Migrated from github.com)

Rebased, added a test that uses -tx

Rebased, added a test that uses `-tx`
fametrano commented 2026-10-02 12:01:02 +00:00 (Migrated from github.com)

Tested 598427fd07.

I ran the 1214 rows of script_tests.json that evalscript can express, each as a chain of calls (scriptSig, then scriptPubKey, then the redeem or witness script) with -tx and -spentoutput. All 1214 gave the expected result, and so did the 5 tapscript rows. The other 18 rows fail in checks that VerifyScript makes outside EvalScript.

I also ran 10,000 random scripts under each sigversion with NONE, MANDATORY and STANDARD flags, 90,000 runs, and compared them with btclib's interpreter. Success and error matched on all but 246 runs. In those, both sides failed with different error codes, and all come from three btclib bugs. evalscript never crashed.

One trap: a script argument that is valid hex is read as raw bytes. evalscript 16 runs the byte 0x16 and fails with "Opcode missing or not understood", while evalscript '16 ' runs OP_16. Could the help say so, or raw hex need a 0x prefix?

Tested 598427fd07. I ran the 1214 rows of script_tests.json that evalscript can express, each as a chain of calls (scriptSig, then scriptPubKey, then the redeem or witness script) with -tx and -spentoutput. All 1214 gave the expected result, and so did the 5 tapscript rows. The other 18 rows fail in checks that VerifyScript makes outside EvalScript. I also ran 10,000 random scripts under each sigversion with NONE, MANDATORY and STANDARD flags, 90,000 runs, and compared them with btclib's interpreter. Success and error matched on all but 246 runs. In those, both sides failed with different error codes, and all come from three btclib bugs. evalscript never crashed. One trap: a script argument that is valid hex is read as raw bytes. `evalscript 16` runs the byte 0x16 and fails with "Opcode missing or not understood", while `evalscript '16 '` runs OP_16. Could the help say so, or raw hex need a `0x` prefix?
DrahtBot commented 2026-11-26 17:43:11 +00:00 (Migrated from github.com)

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

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
DrahtBot commented 2026-11-27 07:57:02 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task tidy: https://github.com/bitcoin/bitcoin/actions/runs/45956875614/job/140365283975
LLM reason (✨ experimental): CI failed because clang-tidy treated r += "," in bitcoin-util.cpp:50 as an error; use the character overload r += ','.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `tidy`: https://github.com/bitcoin/bitcoin/actions/runs/45956875614/job/140365283975</sub> <sub>LLM reason (✨ experimental): CI failed because clang-tidy treated `r += ","` in `bitcoin-util.cpp:50` as an error; use the character overload `r += ','`.</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35788
No description provided.