Skip to content

Commit 915aa02

Browse files
authored
Merge pull request bitcoin#1215 from JeremyRubin/ctv-updates
Minor Updates to BIP-119
2 parents d459bb9 + 6058f2f commit 915aa02

File tree

1 file changed

+142
-43
lines changed

1 file changed

+142
-43
lines changed

bip-0119.mediawiki

Lines changed: 142 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ for purposes of confirmation using CHECKTEMPLATEVERIFY. Then, some time later, t
5757
be expanded out of that UTXO when the demand for blockspace is decreased. These payments can be
5858
structured in a tree-like fashion to reduce individual costs of redemption.
5959

60-
6160
The below chart showcases the structure of these transactions in comparison to
6261
normal transactions and batched transactions.
6362

@@ -71,6 +70,7 @@ is provided in this BIP's subdirectory.
7170
<img src="bip-0119/fifty.png" align="middle"></img>
7271

7372
===Payment Channels===
73+
7474
There are numerous payment channel related uses.
7575

7676
====Channel Factories====
@@ -84,6 +84,7 @@ penultimate transaction node.
8484
Thus, coins sent using a congestion controlled transaction can still enjoy instant liquidity.
8585

8686
====Non-Interactive Channels====
87+
8788
When opening a traditional payment channel, both parties to the channel must participate. This is
8889
because the channel uses pre-signed multi-sig transactions to ensure that a channel can always be
8990
exited by either party, before entering.
@@ -94,6 +95,7 @@ for their private key to be online.
9495
<img src="bip-0119/nic.svg" align="middle"></img>
9596

9697
====Increased Channel Routes====
98+
9799
In the Lightning Network protocol, Hashed Time Locked Contracts (HTLCS) are used in the construction
98100
of channels. A new HTLC is required per route that the channel is serving in.
99101
In BOLT #2, this maximum number of HTLCs in a channel is hard limited to 483 as the maximum safe
@@ -107,7 +109,6 @@ HTLCS.
107109
Because each HTLC can have its own relative time lock in the tree, this also improves the latency
108110
sensitivity of the lightning protocol on contested channel close.
109111

110-
111112
===Wallet Vaults===
112113

113114
When greater security is required for cold storage solutions, there can be
@@ -133,15 +134,22 @@ before. Further Each participant doesn't need to know the totality of the outpu
133134
that output, they only have to verify their own sub-tree will pay them.
134135

135136
==Detailed Specification==
137+
136138
The below code is the main logic for verifying CHECKTEMPLATEVERIFY, and is the canonical
137139
specification for the semantics of OP_CHECKTEMPLATEVERIFY.
138140

139141
case OP_CHECKTEMPLATEVERIFY:
140142
{
141143
// if flags not enabled; treat as a NOP4
142-
if (!(flags & SCRIPT_VERIFY_DEFAULT_CHECK_TEMPLATE_VERIFY_HASH)) break;
144+
if (!(flags & SCRIPT_VERIFY_DEFAULT_CHECK_TEMPLATE_VERIFY_HASH)) {
145+
if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS)
146+
return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS);
147+
break;
148+
}
149+
143150
if (stack.size() < 1)
144151
return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
152+
145153
// If the argument was not 32 bytes, treat as OP_NOP4:
146154
switch (stack.back().size()) {
147155
case 32:
@@ -197,7 +205,6 @@ The hash is computed as follows:
197205
return h.GetSHA256();
198206
}
199207
200-
201208
A PayToBareDefaultCheckTemplateVerifyHash output matches the following template:
202209

203210
bool CScript::IsPayToBareDefaultCheckTemplateVerifyHash() const
@@ -210,16 +217,23 @@ A PayToBareDefaultCheckTemplateVerifyHash output matches the following template:
210217
211218
==Deployment==
212219

213-
Deployment should be done via BIP 9 VersionBits.
220+
Deployment should be done via BIP 9 VersionBits deployed through Speedy Trial.
214221

215222
The start time and bit in the implementation are currently set to bit 5 and
216-
March 1st, 2020, but this is subject to change while the BIP is a draft.
223+
NEVER_ACTIVE/NO_TIMEOUT, but this is subject to change while the BIP is a draft.
217224

218-
For the avoidance of unclarity, the parameters are:
225+
For the avoidance of unclarity, the parameters to be determined are:
219226

227+
// Deployment of CTV (BIP 119)
220228
consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].bit = 5;
221-
consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].nStartTime = 1583020800; // March 1, 2020
222-
consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].nTimeout = 1614556800; // March 1, 2021
229+
consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].nStartTime = Consensus::BIP9Deployment::NEVER_ACTIVE;
230+
consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
231+
consensus.vDeployments[Consensus::DEPLOYMENT_CHECKTEMPLATEVERIFY].min_activation_height = 0;
232+
233+
Until BIP-119 reaches ACTIVE state and the
234+
SCRIPT_VERIFY_DEFAULT_CHECK_TEMPLATE_VERIFY_HASH flag is set, the network should
235+
execute a NOP4 as SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS for policy and a NOP for
236+
consensus.
223237

224238
In order to facilitate using CHECKTEMPLATEVERIFY, the common case of a
225239
PayToBareDefaultCheckTemplateVerifyHash
@@ -231,26 +245,20 @@ standardized later as policy changes.
231245
A reference implementation and tests are available here:
232246
https://github.com/JeremyRubin/bitcoin/tree/checktemplateverify.
233247

234-
235248
==Rationale==
236249

237250
The goal of CHECKTEMPLATEVERIFY is to be minimal impact on the existing codebase -- in the
238251
future, as we become aware of more complex but shown to be safe use cases new template types can be added.
239252

240-
241253
Below we'll discuss the rules one-by-one:
242254

243-
244-
245255
====The DefaultCheckTemplateVerifyHash of the transaction at the current input index matches the top of the stack====
246256

247257
The set of data committed to is a superset of data which can impact the TXID of the transaction,
248258
other than the inputs. This ensures that for a given known input, the TXIDs can also be known ahead
249259
of time. Otherwise, CHECKTEMPLATEVERIFY would not be usable for Channel Factory type constructions
250260
as the redemption TXID could be malleated and pre-signed transactions invalidated.
251261

252-
253-
254262
=====Committing to the version and locktime=====
255263

256264
Were these values not committed, it would be possible to delay the spending of
@@ -282,7 +290,6 @@ precomputed for each transaction to optimize SIGHASH_ALL signatures.
282290
Committing to the hash additionally makes it simpler to construct DefaultCheckTemplateVerifyHash safely and unambiguously from
283291
script.
284292

285-
286293
=====Committing to the number of inputs=====
287294

288295
If we allow more than one input to be spent in the transaction then it would be
@@ -380,12 +387,41 @@ Furthermore, if OP_SHA256STREAM is added in the future, it may be possible to wr
380387
allows adding a single output to a list of outputs without incurring O(n) overhead by committing to
381388
a hash midstate in the script.
382389

390+
=====Using SHA256=====
391+
392+
SHA256 is a 32 byte hash which meets Bitcoin's security standards and is
393+
available already inside of Bitcoin Script for programmatic creation of template
394+
programs.
395+
396+
RIPEMD160, a 20 byte hash, might also be a viable hash in some contexts and has some benefits. For fee efficiency,
397+
RIPEMD160 saves 12 bytes. However, RIPEMD160 was not chosen for BIP-119 because it introduces
398+
risks around the verification of programs created by third parties to be subject to a
399+
[birthday-attack https://bitcoin.stackexchange.com/questions/54841/birthday-attack-on-p2sh] on
400+
transaction preimages.
401+
402+
=====Using Non-Tagged Hashes=====
403+
404+
The Taproot/Schnorr BIPs use Tagged Hashes
405+
(`SHA256(SHA256(tag)||SHA256(tag)||msg)`) to prevent taproot leafs, branches,
406+
tweaks, and signatures from overlapping in a way that might introduce a security
407+
[vulnerability https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2018-June/016091.html].
408+
409+
OP_CHECKTEMPLATEVERIFY is not subject to this sort of vulnerability as the
410+
hashes are effectively tagged externally, that is, by OP_CHECKTEMPLATEVERIFY
411+
itself and therefore cannot be confused for another hash.
412+
413+
It would be a conservative design decisison to make it a tagged hash even if
414+
there was no obvious benefit and no cost. However, in the future, if OP_CAT were
415+
to be introduced to Bitcoin, it would make programs which dynamically build
416+
OP_CHECKTEMPLATEVERIFY hashes less space-efficient. Therefore, bare untagged hashes
417+
are used in BIP-119.
383418

384419
=====The Ordering of Fields=====
385420

386-
Strictly speaking, the ordering of fields is insignificant. However, with a carefully selected
387-
order, the efficiency of future scripts (e.g., those using a OP_CAT or OP_SHA256STREAM) may be
388-
improved.
421+
Strictly speaking, the ordering of fields is insignificant. However, with a
422+
carefully selected order, the efficiency of future scripts (e.g., those using a
423+
OP_CAT or OP_SHA256STREAM) may be improved (as described in the Future Upgrades
424+
section).
389425

390426
In particular, the order is selected in order of least likely to change to most.
391427

@@ -416,13 +452,6 @@ does not make sense for input index to be the last field. However, given the des
416452
able to express a "don't care" index easily (e.g., for decentralized kickstarter-type transactions),
417453
this value is placed last.
418454

419-
As an example, the following code checks an input index argument and concatenates it to the template and
420-
checks the template matches the transaction.
421-
422-
OP_SIZE 4 OP_EQUALVERIF
423-
<nVersion || nLockTime || input count || sequences hash || output count || outputs hash>
424-
OP_SWAP OP_CAT OP_SHA256 OP_CHECKTEMPLATEVERIFY
425-
426455
===Design Tradeoffs and Risks===
427456
Covenants have historically been controversial given their potential for fungibility risks -- coins
428457
could be minted which have a permanent restriction on how they may or may not be spent or required
@@ -437,17 +466,18 @@ transactions which create all the inputs directly in this regard.
437466
Furthermore, templates are restricted to be spendable as a known number of inputs only, preventing
438467
unintentional introduction of the 'half spend' problem.
439468

440-
441469
Templates, as restricted as they are, bear some risks.
442470

443471
====Permanently Unspendable Outputs====
472+
444473
The preimage argument passed to CHECKTEMPLATEVERIFY may be unknown or otherwise unsatisfiable.
445474
However, requiring knowledge that an address is spendable from is incompatible with sender's ability
446475
to spend to any address (especially, OP_RETURN). If a sender needs to know the template can be spent
447476
from before sending, they may request a signature of an provably non-transaction challenge string
448477
from the leafs of the CHECKTEMPLATEVERIFY tree.
449478

450479
====Forwarding Addresses====
480+
451481
Key-reuse with CHECKTEMPLATEVERIFY may be used as a form of "forwarding address contract".
452482
A forwarding address is an address which can automatically execute in a predefined way.
453483
For example, a exchange's hot wallet might use an address which can automatically be moved to a cold
@@ -473,7 +503,6 @@ reuse-unsafe.
473503
Because CHECKTEMPLATEVERIFY commits to the input index currently being spent, reused-keys are
474504
guaranteed to execute in separate transactions which reduces the risk of "half-spend" type issues.
475505

476-
477506
====NOP-Default and Standardness Rules====
478507

479508
If the argument length is not exactly 32, CHECKTEMPLATEVERIFY treats it as a NOP.
@@ -486,8 +515,8 @@ stricter standardness rules to be enforced during consensus. Should that develop
486515
transaction directly to the network relying on standardness rejection, an standardness-invalid but
487516
consensus-valid transaction may be caused, leading to a potential loss of funds.
488517

489-
490518
====Feature Redundancy====
519+
491520
CHECKTEMPLATEVERIFY templates are substantially less risky than other covenant systems. If
492521
implemented, other covenant systems could make the CHECKTEMPLATEVERIFY's functionality redundant.
493522
However, given CHECKTEMPLATEVERIFY's simple semantics and low on chain cost it's likely that it
@@ -501,26 +530,91 @@ unintended behavior.
501530
Alternatively, SIGHASH_ANYPREVOUTANYSCRIPT based covenant designs can implement
502531
something similar to templates, via a scriptPubKey like:
503532

504-
505533
<sig of desired TX with PK and fixed nonce R || SIGHASH_ANYPREVOUTANYSCRIPT <PK with public SK> OP_CHECKSIG
506534
507-
SIGHASH_ANYPREVOUTANYSCRIPT bears additional technical and implementation risks that may preclude
508-
its viability for inclusion in Bitcoin, but the capabilities above are similar to what
509-
CHECKTEMPLATEVERIFY offers. However, CHECKTEMPLATEVERIFY has benefits in terms of verification
510-
speed, as it requires only hash computation rather than signature operations. This can be
511-
significant when constructing large payment trees or programmatic compilations. CHECKTEMPLATEVERIFY
512-
also has a feature-wise benefit in that it provides a robust pathway for future template upgrades.
513-
514-
CHECKSIGFROMSTACK along with OP_CAT may also be used to emulate CHECKTEMPLATEVERIFY. However such
515-
constructions are more complicated to use than CHECKTEMPLATEVERIFY, and encumbers additional
516-
verification overhead absent from CHECKTEMPLATEVERIFY. These types of covenants also bear similar
517-
potential recursion issues to OP_COV which make it unlikely for inclusion in Bitcoin.
518-
535+
SIGHASH_ANYPREVOUTANYSCRIPT bears additional technical and implementation risks
536+
that may preclude its viability for inclusion in Bitcoin, but the capabilities
537+
above are similar to what CHECKTEMPLATEVERIFY offers. The key functional
538+
difference between SIGHASH_ANYPREVOUTANYSCRIPT and OP_CHECKTEMPLATEVERIFY is
539+
that OP_CHECKTEMPLATEVERIFY restricts the number of additional inputs and
540+
precludes dynamically determined change outputs while
541+
SIGHASH_ANYPREVOUTANYSCRIPT can be combined with SIGHASH_SINGLE or
542+
SIGHASH_ANYONECANPAY. For the additional inputs, OP_CHECKTEMPLATEVERIFY also
543+
commits to the scriptsig and sequence, which allows for specifying specific P2SH
544+
scripts (or segwit v0 P2SH) which have some use cases. Furthermore,
545+
CHECKTEMPLATEVERIFY has benefits in terms of script size (depending on choice of
546+
PK, SIGHASH_ANYPREVOUTANYSCRIPT may use about 2x-3x the bytes) and verification
547+
speed, as OP_CHECKTEMPLATEVERIFY requires only hash computation rather than
548+
signature operations. This can be significant when constructing large payment
549+
trees or programmatic compilations. CHECKTEMPLATEVERIFY also has a feature-wise
550+
benefit in that it provides a robust pathway for future template upgrades.
551+
552+
OP_CHECKSIGFROMSTACKVERIFY along with OP_CAT may also be used to emulate
553+
CHECKTEMPLATEVERIFY. However such constructions are more complicated to use
554+
than CHECKTEMPLATEVERIFY, and encumbers additional verification overhead absent
555+
from CHECKTEMPLATEVERIFY. These types of covenants also bear similar potential
556+
recursion issues to OP_COV which make it unlikely for inclusion in Bitcoin.
519557

520558
Given the simplicity of this approach to implement and analyze, and the benefits realizable by user
521559
applications, CHECKTEMPLATEVERIFY's template based approach is proposed in lieu of more complete
522560
covenants system.
523561

562+
====Future Upgrades====
563+
564+
This section describes updates to OP_CHECKTEMPLATEVERIFY that are possible in
565+
the future as well as synergies with other possible upgrades.
566+
567+
=====CHECKTEMPLATEVERIFY Versions=====
568+
569+
OP_CHECKTEMPLATEVERIFY currently only verifies properties of 32 byte arguments.
570+
In the future, meaning could be ascribed to other length arguments. For
571+
example, a 33-byte argument could just the last byte as a control program. In
572+
that case, DefaultCheckTemplateVerifyHash could be computed when the flag byte
573+
is set to CTVHASH_ALL. Other programs could be added similar to SIGHASH_TYPEs.
574+
For example, CTVHASH_GROUP could read data from the Taproot Annex for
575+
compatibility with SIGHASH_GROUP type proposals and allow dynamic malleability
576+
of which indexes get hashed for bundling.
577+
578+
=====Eltoo with OP_CHECKSIGFROMSTACKVERIFY=====
579+
580+
Were both OP_CHECKTEMPLATEVERIFY and OP_CHECKSIGFROMSTACKVERIFY to be added to
581+
Bitcoin, it would be possible to implement a variant of Eltoo's floating
582+
transactions using the following script:
583+
584+
witness(S+n): <sig> <H(tx with nLockTime S+n paying to program(S+n))>
585+
program(S): OP_CHECKTEMPLATEVERIFY <musig_key(pk_update_a, pk_update_b)> OP_CHECKSIGFROMSTACKVERIFY <S+1> OP_CHECKLOCKTIMEVERIFY
586+
587+
Compared to SIGHASH_ANYPREVOUTANYSCRIPT, because OP_CHECKTEMPLATEVERIFY does not
588+
allow something similar to SIGHASH_ANYONECANPAY or SIGHASH_SINGLE, protocol
589+
implementers might elect to sign multiple versions of transactions with CPFP
590+
Anchor Outputs or Inputs for paying fees or an alternative such as transaction
591+
sponsors might be considered.
592+
593+
=====OP_AMOUNTVERIFY=====
594+
595+
An opcode which verifies the exact amount that is being spent in the
596+
transaction, the amount paid as fees, or made available in a given output could
597+
be used to make safer OP_CHECKTEMPLATEVERIFY addressses. For instance, if the
598+
OP_CHECKTEMPLATEVERIFY program P expects exactly S satoshis, sending S-1
599+
satoshis would result in a frozen UTXO and sending S+n satoshis would result in
600+
n satoshis being paid to fee. A range check could restrict the program to only
601+
apply for expected values and default to a keypath otherwise, e.g.:
602+
603+
IF OP_AMOUNTVERIFY <N> OP_GREATER <PK> CHECKSIG ELSE <H> OP_CHECKTEMPLATEVERIFY
604+
605+
=====OP_CAT/OP_SHA256STREAM=====
606+
607+
OP_CHECKTEMPLATEVERIFY is (as described in the Ordering of Fields section)
608+
efficient for building covenants dynamically should Bitcoin get enhanced string
609+
manipulation opcodes.
610+
611+
As an example, the following code checks an input index argument and
612+
concatenates it to the template and checks the template matches the transaction.
613+
614+
OP_SIZE 4 OP_EQUALVERIF
615+
<nVersion || nLockTime || input count || sequences hash || output count || outputs hash>
616+
OP_SWAP OP_CAT OP_SHA256 OP_CHECKTEMPLATEVERIFY
617+
524618
== Backwards Compatibility ==
525619

526620
OP_CHECKTEMPLATEVERIFY replaces a OP_NOP4 with stricter verification semantics. Therefore, scripts
@@ -529,15 +623,18 @@ for an OP_NOP are a soft fork, so existing software will be fully functional wit
529623
for mining and block validation. Similar soft forks for OP_CHECKSEQUENCEVERIFY and OP_CHECKLOCKTIMEVERIFY
530624
(see BIP-0065 and BIP-0112) have similarly changed OP_NOP semantics without introducing compatibility issues.
531625

626+
In contrast to previous forks, OP_CHECKTEMPLATEVERIFY will not make scripts
627+
valid for policy until the new rule is active.
628+
532629
Older wallet software will be able to accept spends from OP_CHECKTEMPLATEVERIFY outputs, but will
533630
require an upgrade in order to treat PayToBareDefaultCheckTemplateVerifyHash chains with a confirmed ancestor as
534631
being "trusted" (i.e., eligible for spending before the transaction is confirmed).
535632

536633
Backports of OP_CHECKTEMPLATEVERIFY can be trivially prepared (see the reference implementation)
537634
for older node versions that can be patched but not upgraded to a newer major release.
538635

539-
540636
== References ==
637+
541638
*[https://utxos.org utxos.org informational site]
542639
*[https://www.youtube.com/watch?v=YxsjdIl0034&t=2451 Scaling Bitcoin Presentation]
543640
*[https://bitcoinops.org/en/newsletters/2019/05/29/ Optech Newsletter Covering OP_CHECKOUTPUTSHASHVERIFY]
@@ -549,6 +646,7 @@ for older node versions that can be patched but not upgraded to a newer major re
549646
550647

551648
===Note on Similar Alternatives===
649+
552650
An earlier version of CHECKTEMPLATEVERIFY, CHECKOUTPUTSHASHVERIFY, is withdrawn
553651
in favor of CHECKTEMPLATEVERIFY. CHECKOUTPUTSHASHVERIFY did not commit to the
554652
version or lock time and was thus insecure.
@@ -561,4 +659,5 @@ CHECKTEMPLATEVERIFY has also been previously referred to as OP_SECURETHEBAG, whi
561659
to aid in searching and referencing discussion on this BIP.
562660

563661
==Copyright==
662+
564663
This document is licensed under the 3-clause BSD license.

0 commit comments

Comments
 (0)