Skip to content

bip-0360: added python reference example; test vector fixes#2202

Open
notmike-5 wants to merge 17 commits into
bitcoin:masterfrom
notmike-5:p2mr
Open

bip-0360: added python reference example; test vector fixes#2202
notmike-5 wants to merge 17 commits into
bitcoin:masterfrom
notmike-5:p2mr

Conversation

@notmike-5

@notmike-5 notmike-5 commented Jun 26, 2026

Copy link
Copy Markdown

This PR adds a python reference example for construction of BIP-360 outputs and control blocks.

It updates the test vectors to remove "internalPubkey" key from vectors 7 and 8, and adds a new test for duplicate script leaves.

This PR also brings in fixes to #2102 and #2103 from @jbride

jbride and others added 4 commits June 25, 2026 19:59
This commit adds a python reference example for construction of BIP-360 outputs and control blocks.
This commit also updates the test vectors.

@murchandamus murchandamus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks reasonable at first glance.

cc: @cryptoquick, @EthanHeilman, @Isabelfoxenduke for owner review/sign-off.

@murchandamus murchandamus added Proposed BIP modification PR by non-owner to update BIP content Pending acceptance This BIP modification requires sign-off by the champion of the BIP being modified labels Jun 26, 2026
This refactors `compute_control_block` to improve performance, and make
the function simpler to read (to me at least).

The previous version walked the entire script tree searching for the
first matching instance of the leaf node.

The new version expects the caller to pass in an explicit `path`
parameter, which tells us exactly where the leaf node lives, with
left/right steps encoded as bits in an integer. We walk down the
tree straight to that leaf node, and build the control block as we go.

This might not be the best DX for a real-world API or library, but this
is just reference code, so we can accept poor usage ergonomics if it makes
the code clearer and more explicit.
Comment thread bip-0360/ref-impl/python/p2mr.py Outdated
Comment thread bip-0360/ref-impl/python/p2mr.py
Comment on lines +184 to +185
# Bech32 encoding code is taken from sipa (BIP-0350), and has been tested against the test vectors therein:
# https://github.com/sipa/bech32/blob/master/ref/python/tests.py

@conduition conduition Jun 26, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Most of this code already exists in the BIPs repository, under the reference code of BIP352 (silent payments).

https://github.com/bitcoin/bips/blob/master/bip-0352/bech32m.py

@murchandamus What are your thoughts on the bech32m code duplication here? is it better to duplicate for the sake of compartmentalization, or should we update the existing code and reference it here?

@murchandamus murchandamus Jul 1, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I’d prefer it to be compartmentalized. This is not a software project, so I would prefer if we don’t slip into starting to maintain the reference implementations of all the BIPs here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it be more convenient if we move the BIP360 reference implementations into an external repository?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That sounds great to me.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

OK, i'll see about closing this PR out and moving the code to a new repository. Please give us some time to get the new repo set up and the code moved over

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should the test vectors stay in the bips repo? Test-vectors and reference implementations are inter-connected: reference implementations are tested against test-vectors and test-vectors can be updated (canonized) using a reference implementation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Either is acceptable. Especially for small reference implementations, both are often supplied with the BIP in the auxiliary files. Especially for larger endeavors, the BIPs often just link to the reference implementation, and test vectors may be provided with the BIP or also linked to.

Comment thread bip-0360/ref-impl/python/p2mr.py Outdated
Comment thread bip-0360/ref-impl/python/p2mr.py Outdated
Comment thread bip-0360/ref-impl/python/p2mr.py Outdated
Comment thread bip-0360/ref-impl/python/p2mr.py Outdated
Comment thread bip-0360/ref-impl/python/p2mr.py Outdated
Comment thread bip-0360/ref-impl/rust/tests/p2mr_construction.rs Outdated
Standardizes all P2MR-specific functions to use bytes uniformly for
input/output. Hex conversions are now confined to two boundaries:
reading `script` field out of ScriptTree input, and comparing against
hex-encoded test vector data in `run_single_test`.

bech32 functions and s2w are left unchanged.
Comment thread bip-0360/ref-impl/python/p2mr.py Outdated
@cryptoquick

Copy link
Copy Markdown
Contributor

Thanks for this work. I'm a little confused as to where it's going to land though. Is there a decision being made regarding reference implementations? Will they all be stripped from the BIPs repository? Will they be fragmented or live in a separate repository? Where is this policy and decision making documented? Who is being consulted in this decision and who are the stakeholders?

@murchandamus

murchandamus commented Jul 6, 2026

Copy link
Copy Markdown
Member

Providing the reference implementation with the BIP or linking to a branch that contains it are acceptable.

@cryptoquick: If you expect that BIP360 is going to continue to evolve, it would be preferable for the reference implementation to be maintained outside of this repository to allow you to move independently of pull requests to the BIPs repository. If this PR wraps up planned work on BIP360’s reference implementation, or you are just going to PR a small count of further updates from a development branch it might as well stay here.

If you do plan on completely revamping the ideas behind BIP360 another time, it might make sense to publish the result as a separate BIP, since P2MR has been getting quite a bit of discussion already.

@conduition

conduition commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

I can't speak to what @cryptoquick is working on, but within my sphere of knowledge I expect at least one more major change will be filed towards BIP360 in the near future, which will be to apply @starius's EC key recovery idea to get shorter EC spend witnesses (background).

I believe @starius is currently working on a separate BIP specifically for the signature scheme and plans to file a pull request to change BIP360 later once that spec is more complete. This change would not affect the core functionality or features of BIP360 - it would merely change the order of validation operations slightly. I don't believe the BIP360 python reference code would need to change (IDK about the Rust/JS code). The bulk of the complexity will live in a separate BIP.

@murchandamus

Copy link
Copy Markdown
Member

The way I perceive it, we’re just providing a public service to facilitate people sharing their protocol development ideas in a central publication medium. That said, we are focused on the text documents here, and while we require reference implementations to exist before a proposal is advanced to Complete, reference implementations are not reviewed at the same level as the proposals themselves by the BIP Editors.
Reference implementations would be expected to get more review/attention from implementers after a proposal has been advanced to Complete.
One can be submitted with the BIP, but it is not intended for reference implementations to be developed against the BIPs repository. Especially if the reference implementation of a proposal is still an on-going project, it would be simpler for all parties if the reference implementation were situated in a separate repository.

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

Labels

Pending acceptance This BIP modification requires sign-off by the champion of the BIP being modified Proposed BIP modification PR by non-owner to update BIP content

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants