Skip to content

Commit 083c357

Browse files
authored
Merge pull request bitcoin#632 from maaku/mast-patch-1
BIP-0117: Correct the examples to use the most recent version of MERKLEBRANCHVERIFY
2 parents b7e0c6f + 7cd6c2f commit 083c357

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

bip-0117.mediawiki

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,17 @@ The possible execution pathways through the script are then enumerated, with con
117117
The list of possible execution pathways is then put into a Merkle tree, with the flattened policy scripts as the leaves of this tree.
118118
The final redeem script which funds are sent to is as follows:
119119

120-
redeemScript: OVER HASH256 <root> 1 MERKLEBRANCHVERIFY 2DROP 2DROP
120+
redeemScript: <nowiki><root> 2 MERKLEBRANCHVERIFY 2DROP DROP</nowiki>
121121
witness: <nowiki><argN> ... <arg1> <policyScript> <proof></nowiki>
122122
123123
Where <code>policyScript</code> is the flattened execution pathway, <code>proof</code> is the serialized Merkle branch and path that proves the policyScript is drawn from the set used to construct the Merkle tree <code>root</code>, and <code>arg1</code> through <code>argN</code> are the arguments required by <code>policyScript</code>.
124-
The <code>OVER HASH256</code> copies the subscript and performs the double-SHA256 hash necessary to prepare the leaf hash value for MERKLEBRANCHVERIFY, and the <code>2DROP 2DROP</code> is necessary to remove the arguments to MERKLEBRANCHVERIFY from the stack.
124+
The <code>2</code> indicates that a single leaf (<code>1 << 1</code>) follows, and the leaf value is not pre-hashed.
125+
The <code>2DROP DROP</code> is necessary to remove the arguments to MERKLEBRANCHVERIFY from the stack.
125126

126127
The above example was designed for clarity, but actually violates the CLEANSTACK rule of segwit v0 script execution.
127128
Unless the CLEANSTACK rule is dropped or modified in a new segwit output version, this would script would have to be modified to use the alt-stack, as follows:
128129

129-
redeemScript: <nowiki>[TOALTSTACK]*N OVER HASH256 <root> 1 MERKLEBRANCHVERIFY 2DROP 2DROP</nowiki>
130+
redeemScript: <nowiki>[TOALTSTACK]*N <root> 2 MERKLEBRANCHVERIFY 2DROP DROP</nowiki>
130131
witness: <nowiki><policyScript> <proof> <arg1> ... <argN></nowiki>
131132
132133
Where <code>[TOALTSTACK]*N</code> is the TOALTSTACK opcode repeated N times.
@@ -142,7 +143,7 @@ The following redeem script allows between 1 and 3 witness arguments in addition
142143
DEPTH 2 SUB // Calculate number of optional elements, ignoring policyScript and proof
143144
DUP IF SWAP TOALTSTACK 1SUB ENDIF // Save 2nd element (optional) to alt-stack, if it is present
144145
IF TOALTSTACK ENDIF // Save 3rd element (optional) to alt-stack, if it is present; consume counter
145-
OVER HASH256 <nowiki><root></nowiki> 1 MERKLEBRANCHVERIFY 2DROP 2DROP
146+
<nowiki><root></nowiki> 2 MERKLEBRANCHVERIFY 2DROP DROP
146147
alt-stack: <nowiki><N+2> <argN> ... <arg1></nowiki>
147148
148149
Because the number of witness elements is pushed onto the alt-stack, this enables policy scripts to verify the number of arguments passed, even though the size of the alt-stack is not usually accessible to script.

0 commit comments

Comments
 (0)