Skip to content

Adds bitcoin clarity builtins#7179

Open
brice-stacks wants to merge 21 commits into
stacks-network:pox-wf-integrationfrom
brice-stacks:feat/bitcoin-clarity
Open

Adds bitcoin clarity builtins#7179
brice-stacks wants to merge 21 commits into
stacks-network:pox-wf-integrationfrom
brice-stacks:feat/bitcoin-clarity

Conversation

@brice-stacks
Copy link
Copy Markdown
Contributor

Built on top of #7173. Adds new get-bitcoin-tx-output? and verify-merkle-proof Clarity builtins.

@brice-stacks brice-stacks force-pushed the feat/bitcoin-clarity branch from 1b522a1 to 3dd9da4 Compare May 2, 2026 01:23
@coveralls
Copy link
Copy Markdown

coveralls commented May 2, 2026

Coverage Report for CI Build 25279731464

Warning

No base build found for commit 8096edd on pox-wf-integration.
Coverage changes can't be calculated without a base build.
If a base build is processing, this comment will update automatically when it completes.

Coverage: 62.459%

Details

  • Patch coverage: 527 uncovered changes across 16 files (399 of 926 lines covered, 43.09%).

Uncovered Changes

Top 10 Files by Coverage Impact Changed Covered %
clarity/src/vm/functions/bitcoin.rs 307 98 31.92%
clarity/src/vm/costs/costs_5.rs 438 249 56.85%
clarity/src/vm/analysis/type_checker/v2_1/natives/mod.rs 72 0 0.0%
clarity/src/vm/functions/mod.rs 8 0 0.0%
clarity/src/vm/analysis/errors.rs 7 0 0.0%
clarity/src/vm/costs/costs_1.rs 6 0 0.0%
clarity/src/vm/costs/costs_2.rs 6 0 0.0%
clarity/src/vm/costs/costs_2_testnet.rs 6 0 0.0%
clarity/src/vm/costs/costs_3.rs 6 0 0.0%
clarity/src/vm/costs/costs_4.rs 6 0 0.0%

Coverage Regressions

Requires a base build to compare against. How to fix this →


Coverage Stats

Coverage Status
Relevant Lines: 218016
Covered Lines: 136171
Line Coverage: 62.46%
Coverage Strength: 14040461.82 hits per line

💛 - Coveralls

@brice-stacks brice-stacks force-pushed the feat/bitcoin-clarity branch from 45b50a0 to 19d5b12 Compare May 4, 2026 14:13
@brice-stacks
Copy link
Copy Markdown
Contributor Author

With this change, we can basically revert #7168, but let's do that in a separate PR.

Comment thread clarity/src/vm/analysis/type_checker/v2_1/natives/mod.rs Outdated
@brice-stacks brice-stacks force-pushed the feat/bitcoin-clarity branch from 18855df to 7b3ffe9 Compare May 4, 2026 20:09
@brice-stacks brice-stacks marked this pull request as ready for review May 5, 2026 15:22
@brice-stacks
Copy link
Copy Markdown
Contributor Author

@rob-stacks has volunteered to do the benchmarking to fill in real values for the new cost functions. That can happen in a separate PR built off of this one, so this one is ready for review.

@brice-stacks
Copy link
Copy Markdown
Contributor Author

With this new design, we can remove the p2wsh output DBs and related work from PR #7168. I'll do that in a separate PR as well.

@hstove-stacks
Copy link
Copy Markdown
Contributor

@brice-stacks can you share how you've found test fixtures and/or real data for the merkle proof validation stuff? Ie how to get the arguments you need for a given txid/block?

@brice-stacks
Copy link
Copy Markdown
Contributor Author

brice-stacks commented May 6, 2026

@brice-stacks can you share how you've found test fixtures and/or real data for the merkle proof validation stuff? Ie how to get the arguments you need for a given txid/block?

The real data just came from real bitcoin transactions (genesis, pizza tx, BIP141 SegWit announcement). I also generated some merkle proofs for the merkle_ unit tests. An LLM notified me about the CVE-2012-2459 issue.

EDIT: just realized maybe that's not what you were asking. I got the transactions from public APIs:

curl https://mempool.space/api/tx/a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d/hex > pizza.hex
curl https://blockstream.info/api/tx/a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d/merkle-proof

@rob-stacks
Copy link
Copy Markdown
Contributor

Benchmark results are here: #7179 (comment)

Based on the last iteration (done for secp256r1-verify):

verify-merkle-proof depends on the number of siblings: base cost: 502, cost for each sibling: 125
get-bitcoin-tx-output? is not dependent on the size of the transaction (obviously it will impact the read count): 291

Showed the merkle proofs for the same 3 transactions used for the output
examples, which show off 3 unique scenarios, and then also added lots of
comments to make it clear how to get the data needed.
@brice-stacks
Copy link
Copy Markdown
Contributor Author

@hstove-stacks check out 58b86c0, I added tests for those same 3 transactions and clear comments about where the values come from.

@brice-stacks
Copy link
Copy Markdown
Contributor Author

Thanks @rob-stacks! Added in b871e0c.

@brice-stacks
Copy link
Copy Markdown
Contributor Author

@rob-stacks I was just looking at this again. Shouldn't at least tx.txid() be linear in the size of the transaction?

@rob-stacks
Copy link
Copy Markdown
Contributor

@brice-stacks it should, but the size limit of 1024 bytes for the transaction body makes the difference mostly irrelevant at the cpu side (and we are already charging for the size of the transaction in terms of read)

@brice-stacks
Copy link
Copy Markdown
Contributor Author

@brice-stacks it should, but the size limit of 1024 bytes for the transaction body makes the difference mostly irrelevant at the cpu side (and we are already charging for the size of the transaction in terms of read)

The 1024 byte-limit is just for the requested output. The total size of the tx body is only limited by the Clarity value size limit, so 1MB. Did the benchmark include transactions that are ~1 MB?

@rob-stacks
Copy link
Copy Markdown
Contributor

Damn, my bad I just noticed that the argument limit is BUFFER_MAX and not BUFFER_1024. I will send you the updated numbers asap

@rob-stacks
Copy link
Copy Markdown
Contributor

rob-stacks commented May 12, 2026

@brice-stacks ok, the load almost linearly scales every 1K, I would suggest 125 units every 1024 bytes of transaction.

@brice-stacks
Copy link
Copy Markdown
Contributor Author

@brice-stacks ok, the load almost linearly scales every 1K, I would suggest 125 units every 1024 bytes of transaction.

Got it, thanks Roberto. I'll make that change.

@rob-stacks
Copy link
Copy Markdown
Contributor

@brice-stacks LGTM but do you think you can add a prop-test scrambling outputs and their numbers/amounts ?

@brice-stacks
Copy link
Copy Markdown
Contributor Author

Added proptests in ea51c49

This was referenced May 13, 2026
Comment thread clarity/src/vm/functions/bitcoin.rs
Comment thread clarity/src/vm/functions/mod.rs Outdated
Copy link
Copy Markdown
Contributor

@aaronb-stacks aaronb-stacks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just had a few comments, otherwise looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants