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

BIP442: OP_PAIRCOMMIT #1699

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.mediawiki
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,13 @@ Those proposing changes should consider that ultimately consent may rest with th
| Gloria Zhao
| Informational
| Draft
|-
| [[bip-0442.md|442]]
| Consensus (soft fork)
| OP_PAIRCOMMIT
| moonsettler
| Standard
| Draft
|}

<!-- IMPORTANT! See the instructions at the top of this page, do NOT JUST add BIPs here! -->
265 changes: 265 additions & 0 deletions bip-0442.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
<pre>
BIP: 442
Layer: Consensus (soft fork)
Title: OP_PAIRCOMMIT
Author: moonsettler <[email protected]>
Comments-URI: https://github.com/bitcoin/bips/wiki/Comments:BIP-0442
Status: Draft
Type: Standards Track
Created: 2024-12-09
License: BSD-3-Clause
</pre>

## Abstract

This BIP describes a new tapscript opcode `OP_PAIRCOMMIT`, which
provides limited vector commitment functionality in tapscript.

When evaluated, the `OP_PAIRCOMMIT` instruction:
* Pops the top two values off the stack,
* takes the "PairCommit" tagged SHA256 hash of the stack elements with size
commitments,
* pushes the resulting 32-byte hash to the top of stack.

## Motivation

Currently, bitcoin lacks a way to hash multiple stack elements together. Which
means building Merkle trees or verifying inclusion in a tree is not supported.
Comment on lines +26 to +27
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Currently, bitcoin lacks a way to hash multiple stack elements together. Which
means building Merkle trees or verifying inclusion in a tree is not supported.
Currently, bitcoin lacks a way to commit to multiple stack elements together. It is common practice to hash a single item as part of a bitcoin script, either in hash/time locked contracts, or in pay-to-pubkey-hash (P2PKH) scripts, but there is no way to commit to multiple items with a single hash.
In a contrived but demonstrative example, if PAIRCOMMIT existed in legacy script, P2PKH could be extended to pay-to-pubkey-time-hash with scriptPubKey `2DUP PAIRCOMMIT RIPEMD160 <hash> EQUALVERIFY CHECKLOCKTIMEVERIFY DROP CHECKSIG`. This script format for single signature, time-locked bitcoin could be transformed into an address format.
With the ability to commit to pairs of elements, PAIRCOMMIT can be generalized Merklized commitments to a tree of elements with a single hash. On its own, this could enable a hash lock contract where the holder of any pre-image in a Merkle tree can unlock the spend, for example.
If `OP_CHECKSIGFROMSTACK` is combined with PAIRCOMMIT, the ability to sign commitments to multiple items enables both of the above but as a form of delegation. In cases where any single item can be used to unlock an output, additional off chain signatures be simply can be used as an alternative to PAIRCOMMIT. However in cases where multiple items must be used for spending together, PAIRCOMMIT removes the need for complex laddering schemes to ensure that all items were authorized together.


`OP_PAIRCOMMIT` is a simple and efficient tool to commit to two stack elements,
in a way that makes length redistribution attacks infeasible.

The number of SHA256 iterations is minimized in the typical use cases we can
optimize for. Since the Tag can be pre-computed as mid-state, it would only
Copy link
Member

Choose a reason for hiding this comment

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

Does "Tag" here refer to this, as you are writing elsewhere in this draft?

Suggested change
optimize for. Since the Tag can be pre-computed as mid-state, it would only
optimize for. Since the tagged SHA256 hash can be pre-computed mid-state, it would only
  • drop "as"?
  • s/in validation/to validate/ (line 34)?

take 1 or 2 hash cycles in validation for the unilateral close scenario.
Comment on lines +32 to +34
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like something that belongs in a footnote not in the main text.


## Specification

Repurpose opcode 205 (currently `OP_SUCCESS`) as follows:

`OP_PAIRCOMMIT` pops two elements off the stack, then concatenates them along
with their size commitments and takes the tagged SHA256 hash of that
concatenated string, then pushes the resulting hash back on the stack.

Given the stack `[x1, x2]`, where `x2` is at the top of the stack:

`OP_PAIRCOMMIT` will push `SHA256(tagPC|cs(x1)|x1|cs(x2)|x2)` onto the stack.

Where `|` denotes concatenation and `tagPC` is calculated according to
[BIP-340] tagged hash as `SHA256("PairCommit")|SHA256("PairCommit")` and
`cs(x)` means `CompactSize(x)`.
moonsettler marked this conversation as resolved.
Show resolved Hide resolved

### Implementation

```c++
case OP_PAIRCOMMIT: {
// OP_PAIRCOMMIT is only available in Tapscript
// ...
// x1 x2 -- hash
if (stack.size() < 2) {
return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
}
const valtype& vch1 = stacktop(-2);
const valtype& vch2 = stacktop(-1);

uint256 hash = PairCommitHash(vch1, vch2);

stack.pop_back();
stack.pop_back();
stack.emplace_back(hash.begin(), hash.end());
break;
}
```
```c++
const HashWriter HASHER_PAIRCOMMIT{TaggedHash("PairCommit")};

uint256 PairCommitHash(const std::vector<unsigned char>& x1, const std::vector<unsigned char>& x2)
{
return (HashWriter{HASHER_PAIRCOMMIT} << x1 << x2).GetSHA256();
}
```
### Use in script

`OP_PAIRCOMMIT` can be used to commit to a vector of stack elements in a way
that is not vulnerable to various forms of witness malleability. It is, however,
highly optimized for just 2 stack elements.

```text
# pc-hash = PC(a, PC(b, c))

<a> <b> <c> | PC PC <pc-hash> OP_EQUALVERIFY
```

### Use in LN-Symmetry
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a subsection of the Specification section, but does "Use in LN-Symmetry" actually belong to the Specification? Wouldn’t it make more sense for it to be in the Rationale or a section with example uses?


To do LN-Symmetry contracts that don't require the nodes to keep old states,
we need to solve the data availability problem presented by unilateral closes.
Channel peers must be able to reconstruct the script that spends an
intermediate state.

Using in sequence `OP_CHECKTEMPLATEVERIFY`, `OP_PAIRCOMMIT`, `OP_INTERNALKEY`
Copy link
Member

Choose a reason for hiding this comment

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

The pseudocode below might be clearer if the acronyms are defined beforehand

Suggested change
Using in sequence `OP_CHECKTEMPLATEVERIFY`, `OP_PAIRCOMMIT`, `OP_INTERNALKEY`
Using in sequence `OP_CHECKTEMPLATEVERIFY` (`CTV`), `OP_PAIRCOMMIT` (`PC`), `OP_INTERNALKEY` (`IK`)

(idem for CSFS on the next line)

and `OP_CHECKSIGFROMSTACK` we can construct a rebindable channel that is also
[optimal].

The following assembly-like pseudo-code shows a possible LN-Symmetry channel
construction that provides data availability to spend to the latest state from
an earlier state pushed on-chain with a forced close by channel partner.


```text
# S = 500000000
# IK -> A+B
<sig> <state-n-recovery-data> <state-n-hash> | CTV PC IK CSFS <S+1> CLTV DROP
```
before funding, sign the first state:
```text
# state-n-hash { nLockTime(S+n), out(contract, amount(A)+amount(B)) }
# settlement-n-hash { nSequence(2w), out(A, amount(A)), out(B, amount(B)) }
# state-n-recovery-data { settlement-n-hash or state-n-balance }

# contract for state n < m
IF
<sig> <state-m-recovery-data> <state-m-hash> | CTV PC IK CSFS <S+n+1> CLTV DROP
ELSE
<settlement-n-hash> CTV
ENDIF
```

### Use with future updates
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, "Use with future updates" doesn’t seem to belong to the Specification.

Copy link
Member

@jonatack jonatack Jan 4, 2025

Choose a reason for hiding this comment

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

Using ## instead of ### for this section and for "Use in LN-Symmetry" might suffice (e.g. moving them out of the Specification section).


Detailed introspection opcodes would also need vector commitments when used
with `OP_CHECKSIGFROMSTACK`.
Copy link
Member

Choose a reason for hiding this comment

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

nit, could link to or mention BIP348 with CHECKSIGFROMSTACK here.


`OP_CHECKCONTRACTVERIFY` would also need a way to carry complex data.
Copy link
Member

@jonatack jonatack Jan 4, 2025

Choose a reason for hiding this comment

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

This isn't part of any BIP (number) yet, maybe add it later if that changes.


## Reference Implementation

A reference implementation is provided here:

https://github.com/lnhance/bitcoin/pull/6/files

## Rationale
Copy link
Member

Choose a reason for hiding this comment

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

Suggest moving this Rationale section up to just after Motivation, before Specification.


If `OP_CAT` was available, it could be used to combine multiple stack elements
Copy link
Member

Choose a reason for hiding this comment

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

Could link to or mention BIP347 with OP_CAT here.

that get verified with `OP_CHECKSIGFROMSTACK` as a valid state update.
moonsettler marked this conversation as resolved.
Show resolved Hide resolved

Using `OP_CAT` for this purpose requires additional opcodes to prevent witness
malleability (e.g. `0x0102 0x03 OP_CAT` is identical to `0x01 0x0203 OP_CAT`).

`OP_PAIRCOMMIT` solves this specific problem without introducing a wide range
of potentially controversial new behaviors like fully detailed introspection,
which includes the ability to inspect parent transactions and novel 2-way peg
mechanisms. ([CAT-tricks-I] and [CAT-tricks-II] by Andrew Poelstra)

Alternatively `OP_RETURN` could be used to ensure the availability of the state
recovery data, as `OP_CHECKTEMPLATEVERIFY` naturally commits to all outputs.
However, its cost in weight units would be over 4 times higher than that of
using `OP_PAIRCOMMIT`.

moonsettler marked this conversation as resolved.
Show resolved Hide resolved
One way to think about the 3 opcodes (`OP_CHECKSIGFROMSTACK`, `OP_INTERNALKEY`,
`OP_PAIRCOMMIT`) is we decompose a `OP_CHECKSIGFROMSTACK` variant that can use
a 1-byte `OP_TRUE` public key (substituting for the *taproot internal key*) and
can commit to a number of stack elements as a message.

### Behaviors LNhance tries to avoid introducing
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this section belong in the PC BIP?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we should have an LNhance BIP that depends on the others and move some stuff over?

Copy link
Member

Choose a reason for hiding this comment

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

I think it may be fine here if this section is moved up next to the Motivation section and the term LNHANCE is introduced clearly, ideally with a link to lnhance.org if that url is viable long-term.


The following behaviors are out of scope for LNhance and should not be enabled
Comment on lines +164 to +166
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the first mention of LNHANCE in this BIP, but is not further explained. LNHANCE is not introduced in this repository (except for a cursory mention in BIP 348 OP_CHECKSIGFROMSTACK). It would probably make sense to introduce the term, if this section is retained.

as a side effect without explicit consensus:

* Fine-grained introspection
* State-carrying covenants
* Bigint operations
* New arithmetic capabilities using lookup tables

### Alternative approaches

The following list of alternative approaches were discussed and rejected for
various reasons, either for expanding the scope or for unnecessary complexity:

* OP_CAT
Copy link
Member

Choose a reason for hiding this comment

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

could link to or mention BIP347

* SHA256 streaming opcodes
* Merkle operation opcodes
* 'Kitty' CAT: result or inputs arbitrarily limited in size
* OP_CHECKTEMPLATEVERIFY committing to the taproot annex in tapscript
Copy link
Member

Choose a reason for hiding this comment

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

Could link to or mention BIP119

* OP_CHECKSIGFROMSTACK on n elements as message
Copy link
Member

Choose a reason for hiding this comment

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

could link to or mention BIP348

* OP_VECTORCOMMIT: generalized form for n > 2 elements
* ReKey: key delegation and multiple use of OP_CHECKSIGFROMSTACK

### Cost comparison of LN-Symmetry constructions
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
### Cost comparison of LN-Symmetry constructions
### Weight comparison of LN-Symmetry constructions

Copy link
Member

Choose a reason for hiding this comment

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

It may make sense to put this section next to (or in) the "Use in LN-Symmetry" section.


| Method | ChannelSc | UpdateSc | UpdateW | ForceC | Contest | Settle |
| :------------ | --------: | -------: | ------: | ------: | ------: | :----: |
| APO-Annex | 8 WU | 113 WU | 100 WU | 1221 WU | 627 WU | SigOp |
| APO-Return | 8 WU | 113 WU | 66 WU | 1359 WU | 765 WU | SigOp |
| CTV+CSFS+IKEY | 10 WU | 48 WU | 98 WU | 1328 WU | 732 WU | CTV |
| CTV+CSFS | 43 WU | 81 WU | 98 WU | 1394 WU | 765 WU | CTV |
| LNhance | 11 WU | 49 WU | 131 WU | 1191 WU | 594 WU | CTV |

*ChannelSc: channel script, UpdateSc: update script, UpdateW: witness is the
same size for both Force Close and Contest in LN-Symmetry, ForceC: total cost of unilateral close transactions*
Comment on lines +190 to +199
Copy link
Contributor

Choose a reason for hiding this comment

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

Some of these abbreviations are just a few characters shorter than the actual term. How about something along the lines of:

Suggested change
| Method | ChannelSc | UpdateSc | UpdateW | ForceC | Contest | Settle |
| :------------ | --------: | -------: | ------: | ------: | ------: | :----: |
| APO-Annex | 8 WU | 113 WU | 100 WU | 1221 WU | 627 WU | SigOp |
| APO-Return | 8 WU | 113 WU | 66 WU | 1359 WU | 765 WU | SigOp |
| CTV+CSFS+IKEY | 10 WU | 48 WU | 98 WU | 1328 WU | 732 WU | CTV |
| CTV+CSFS | 43 WU | 81 WU | 98 WU | 1394 WU | 765 WU | CTV |
| LNhance | 11 WU | 49 WU | 131 WU | 1191 WU | 594 WU | CTV |
*ChannelSc: channel script, UpdateSc: update script, UpdateW: witness is the
same size for both Force Close and Contest in LN-Symmetry, ForceC: total cost of unilateral close transactions*
| Method | Channel Script | Update Script | Update Witness¹ | Force Close² | Contest | Settlement Mechanism |
| :------------ | --------: | -------: | ------: | ------: | ------: | :----: |
| APO-Annex | 8 WU | 113 WU | 100 WU | 1221 WU | 627 WU | SigOp |
| APO-Return | 8 WU | 113 WU | 66 WU | 1359 WU | 765 WU | SigOp |
| CTV+CSFS+IKEY | 10 WU | 48 WU | 98 WU | 1328 WU | 732 WU | CTV |
| CTV+CSFS | 43 WU | 81 WU | 98 WU | 1394 WU | 765 WU | CTV |
| LNhance | 11 WU | 49 WU | 131 WU | 1191 WU | 594 WU | CTV |
¹ *witness is the same weight for both Force Close and Contest in LN-Symmetry*
² *total cost of unilateral close transactions*

Copy link
Contributor

Choose a reason for hiding this comment

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

This table is also provided barren of context. It would probably be useful to link to the compared schemas, or to the source of the table, and to add a couple sentence as to what we are looking at in the first place.


### Proving general computation

Merkle trees can be used to prove computation where the root of the tree
represents the *function* and the leaves represent the *inputs* and *output*.
There are practical limits to the entropy space for the *inputs* as they need
to be iterated over and hashed into a Merkle root.

Taproot MAST trees can currently cover 128 bits of entropy space, which is over
Copy link
Contributor

Choose a reason for hiding this comment

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

What are "Taproot MAST trees"? The taproot BIPs speaks of "Script Trees" and MAST would be understood as "Merklized Alternative Script Trees" in the context of Taproot. On the other hand, MAST as described in BIP 114 and BIP 117 speaks of Merklized Abstract Syntax Trees where multiple leaves are used in combination.

You mention MAST here in the context taproot, where only a single leaf is ever used at once, but it seems to me that you might be thinking of Abstract Syntax Trees. Either way, it is not clear to me what you are referring to here, when you speak of the "entropy space covered by Taproot MAST trees" while treating leaves as "inputs and outputs".

This section is jumping right in the middle of something. I’m not sure that the scenario is described sufficiently to understand what the supposed concern is, let alone why the supposed concern is not a concern. If this section is relevant to this BIP, I reckon that more context is necessary for the reader to understand why and how, e.g., add aa link to a description of this scheme, or provide a bit more explanation what this section is talking about.

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO this is a problem in the taproot BIP series, where various individuals decided it pertinent to change a well understood acronym (MAST) to suit taproot, rather than resepcting an existing body of literature including other BIPs.

I'd rather the taproot BIPs get updated to reflect the terminology Merklized Abstract Syntax Trees (or Tap Script Merkle Commitments, TSMC, if we're fond of confusing backronyming but should be less ambiguous in context) rather than Merklized Alternative Script Trees, which was a backronym.

Copy link
Contributor

Choose a reason for hiding this comment

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

correction: it seems there is no mention of alt script tree in the bips repos, just abstract syntax tree, including in the BIPs for Taproot itself. It's just a confusing rename people have made outside of this repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So should it be "Taptrees" instead of "Taproot MAST trees"? Or "Tapscript trees"?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think "Taproot script tree" or "Taproot Merkle tree" would both be clear, I prefer the former.

the practical limits to iterate over and merklize. Therefore, we conclude this
capability does not materially extend what computations are possible to prove
in bitcoin script. While `OP_PAIRCOMMIT` is not limited to a height of 128,
that should not be practically feasible to utilize.
moonsettler marked this conversation as resolved.
Show resolved Hide resolved

There is a way to reduce the size of the witness for proving computation,
moonsettler marked this conversation as resolved.
Show resolved Hide resolved
by eliminating the Merkle path inclusion proofs, using `OP_CHECKSIGFROMSTACK`
together with `OP_PAIRCOMMIT`. This method involves deleted key assumptions,
most likely using MPC to create an enormous amount of signatures for the stack
elements representing the *inputs* and the *output* of the *function*.

## Backward Compatibility

By constraining the behavior of OP_SUCCESS opcodes, deployment of the BIP
can be done in a backwards-compatible, soft-fork manner. If anyone were to
rely on the OP_SUCCESS behavior of `OP_SUCCESS205`, `OP_PAIRCOMMIT` would
invalidate their spend.

## Deployment

TBD

## Credits

Jeremy Rubin, Brandon Black, Salvatore Ingala, Anthony Towns, Ademan555

## Copyright

This document is licensed under the 3-clause BSD license.

## References

1. LNhance bitcoin repository: [lnhance]
2. LN-Symmetry: [eltoo]
3. OP_CAT: [BIP-347], [BIN-2024-0001]
4. OP_CHECKTEMPLATEVERIFY: [BIP-119]
5. OP_CHECKSIGFROMSTACK: [BIP-348], [BIN-2024-0003]
6. OP_INTERNALKEY: [BIP-349], [BIN-2024-0004]
7. Tagged hash: [BIP-340]

[lnhance]: https://github.com/lnhance/bitcoin
[eltoo]: https://github.com/instagibbs/bolts/blob/eltoo_draft/XX-eltoo-transactions.md
[CAT-tricks-I]: https://medium.com/blockstream/cat-and-schnorr-tricks-i-faf1b59bd298
[CAT-tricks-II]: https://medium.com/blockstream/cat-and-schnorr-tricks-ii-2f6ede3d7bb5

[//]: # (BIPs referenced)
[BIP-119]: https://github.com/bitcoin/bips/tree/master/bip-0119.mediawiki
[BIP-340]: https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
[BIP-347]: https://github.com/bitcoin/bips/blob/master/bip-0347.mediawiki
[BIP-348]: https://github.com/bitcoin/bips/blob/master/bip-0348.md
[BIP-349]: https://github.com/bitcoin/bips/blob/master/bip-0349.md
[BIN-2024-0001]: https://github.com/bitcoin-inquisition/binana/blob/master/2024/BIN-2024-0001.md
[BIN-2024-0003]: https://github.com/bitcoin-inquisition/binana/blob/master/2024/BIN-2024-0003.md
[BIN-2024-0004]: https://github.com/bitcoin-inquisition/binana/blob/master/2024/BIN-2024-0004.md

[//]: # (Internal links)
[optimal]: #cost-comparison-of-ln-symmetry-constructions
Loading