|
| 1 | +==== Introduction ==== |
| 2 | + |
| 3 | +This document is a modified copy of [https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki BIP-0341] which describes the signature hash used for Taproot outputs in Elements. |
| 4 | +Items which differ from BIP-0341 are indicated with '''''NEW'''''. |
| 5 | + |
| 6 | +In addition to the changes in this document, the four tagged hash tags ''TapLeaf'', ''TapBranch'', ''TapTweak'' and ''TapSighash'' in BIPs 341 and 342 are suffixed with <tt>/elements</tt>. |
| 7 | + |
| 8 | +==== Common signature message ==== |
| 9 | + |
| 10 | +The function ''SigMsg(hash_type, ext_flag)'' computes the message being signed as a byte array. It is implicitly also a function of the spending transaction and the outputs it spends, but these are not listed to keep notation simple. |
| 11 | + |
| 12 | +The parameter ''hash_type'' is an 8-bit unsigned value. The <code>SIGHASH</code> encodings from the legacy script system are reused, including <code>SIGHASH_ALL</code>, <code>SIGHASH_NONE</code>, <code>SIGHASH_SINGLE</code>, and <code>SIGHASH_ANYONECANPAY</code>, plus the default ''hash_type'' value ''0x00'' which results in signing over the whole transaction just as for <code>SIGHASH_ALL</code>. The following restrictions apply, which cause validation failure if violated: |
| 13 | +* Using any undefined ''hash_type'' (not ''0x00'', ''0x01'', ''0x02'', ''0x03'', ''0x81'', ''0x82'', or ''0x83''). |
| 14 | +* Using <code>SIGHASH_SINGLE</code> without a "corresponding output" (an output with the same index as the input being verified). |
| 15 | +
|
| 16 | +'''''NEW''''' If the input under consideration is a pegin input, the fields ''nAsset'', ''nValue'' and ''scriptPubKey'', where they appear, are taken from the pegin witness data. The asset used is the asset ID on the sidechain, not that on the parent chain (if any). |
| 17 | + |
| 18 | +'''''NEW''''' The epoch field prepended before signature hash is completely dropped. If there are new updates to the taproot signature hashes in elements, they will use new tagged hashes instead of incrementing epochs |
| 19 | + |
| 20 | +The parameter ''ext_flag'' is an integer in range 0-127, and is used for indicating (in the message) that extensions are added at the end of the message. |
| 21 | + |
| 22 | +If the parameters take acceptable values, the message is the concatenation of the following data, in order (with byte size of each item listed in parentheses). Numerical values in 2, 4, or 8-byte are encoded in little-endian. |
| 23 | + |
| 24 | + |
| 25 | +* '''''NEW''''' Blockchain data: (Rationale: Signatures cannot be reused across different Elements instances even if pubkeys are reused.) |
| 26 | +** ''genesis_block'' (32): the hash of the blockchain's genesis block |
| 27 | +** ''genesis_block'' (32): the genesis hash again |
| 28 | +* Control: |
| 29 | +** ''hash_type'' (1). |
| 30 | +* Transaction data: |
| 31 | +** ''nVersion'' (4): the ''nVersion'' of the transaction. |
| 32 | +** ''nLockTime'' (4): the ''nLockTime'' of the transaction. |
| 33 | +** If the ''hash_type & 0x80'' does not equal <code>SIGHASH_ANYONECANPAY</code>: |
| 34 | +*** '''''NEW''''' ''sha_outpoint_flags'' (32): the SHA256 of the serialization of the concatenation of one byte per input of the input's outpoint flags shifted right by 24 bits. (The byte for an pegin input would be 0x40. The byte for an issuance input would be 0x80. The byte for both a pegin and issuance would be 0xc0.) |
| 35 | +*** ''sha_prevouts'' (32): the SHA256 of the serialization of all input outpoints. Each prevout output index is the masked version excluding outpoint flags. As mentioned above, for pegin inputs these will be prevouts on the parent chain. |
| 36 | +*** '''''NEW''''' ''sha_asset_amounts'' (32): the SHA256 of the serialization of ''nAsset||nValue'' for each output. As mentioned above, for pegin inputs an explicit value (from the parent chain) and asset (the peg asset) will be used. |
| 37 | +*** ''sha_scriptpubkeys'' (32): the SHA256 of the serialization of all spent output ''scriptPubKey''s. |
| 38 | +*** ''sha_sequences'' (32): the SHA256 of the serialization of all input ''nSequence''. |
| 39 | +*** '''''NEW''''' ''sha_issuances'' (32): the SHA256 of the serialization of the concatenation of asset issuance data for inputs with an issuance or ''0x00'' for inputs with no issuance. (This matches the Segwit v0 encoding of this hash.) |
| 40 | +*** '''''NEW''''' ''sha_issuance_rangeproofs'' (32): the SHA256 of the serialization of the concatenation of ''issuanceAmountRangeproof||inflationKeysRangeproof'' for all inputs, where each field is encoded as ''0x00'' if absent (either if there is no issuance, or if there is an explicit issuance) |
| 41 | +** If ''hash_type & 3'' does not equal <code>SIGHASH_NONE</code> or <code>SIGHASH_SINGLE</code>: |
| 42 | +*** ''sha_outputs'' (32): the SHA256 of the serialization of all outputs in <code>CTxOut</code> format. |
| 43 | +*** '''''NEW''''' ''sha_output_witnesses'' (32): the SHA256 of the serialization of all output witnesses (rangeproof and surjection proof) in <code>CTxOutWitness</code> format. |
| 44 | +* Data about this input: |
| 45 | +** ''spend_type'' (1): equal to ''(ext_flag * 2) + annex_present'', where ''annex_present'' is 0 if no annex is present, or 1 otherwise (the original witness stack has two or more witness elements, and the first byte of the last element is ''0x50'') |
| 46 | +** If ''hash_type & 0x80'' equals <code>SIGHASH_ANYONECANPAY</code>: |
| 47 | +*** '''''NEW''''' ''outpoint_flag'' (1): the input's outpoint flags shifted right by 24 bits. (Compare ''sha_outpoint_flags'' above.) |
| 48 | +*** ''outpoint'' (36): the <code>COutPoint</code> of this input (32-byte hash + 4-byte little-endian) where the output index excludes the outpoint flags. |
| 49 | +*** '''''NEW''''' ''nAsset'' (33): (possibly confidential) assetID of the previous output spent by this input |
| 50 | +*** '''''NEW''''' ''nValue'' (9-33): (possibly confidential) amount of the previous output spent by this input |
| 51 | +*** ''scriptPubKey'' (35): ''scriptPubKey'' of the previous output spent by this input, serialized as script inside <code>CTxOut</code>. Its size is always 35 bytes. |
| 52 | +*** ''nSequence'' (4): ''nSequence'' of this input. |
| 53 | +*** '''''NEW''''' ''asset_issuance'' (1-130): if ''outpoint_flag & 0x80 == 0x80'': asset issuance data for this input, or ''0x00'' otherwise |
| 54 | +*** '''''NEW''''' ''sha_single_issuance_rangeproofs'' (0-32): if ''outpoint_flag & 0x80 == 0x80'': the SHA256 of the serialization of the concatenation of ''issuanceAmountRangeproof||inflationKeysRangeproof'' for this input, where each field is encoded as ''0x00'' if absent |
| 55 | +** If ''hash_type & 0x80'' does not equal <code>SIGHASH_ANYONECANPAY</code>: |
| 56 | +*** ''input_index'' (4): index of this input in the transaction input vector. Index of the first input is 0. |
| 57 | +** If an annex is present (the lowest bit of ''spend_type'' is set): |
| 58 | +*** ''sha_annex'' (32): the SHA256 of ''(compact_size(size of annex) || annex)'', where ''annex'' includes the mandatory ''0x50'' prefix. |
| 59 | +* Data about this output: |
| 60 | +** If ''hash_type & 3'' equals <code>SIGHASH_SINGLE</code>: |
| 61 | +*** ''sha_single_output'' (32): the SHA256 of the corresponding output in <code>CTxOut</code> format. |
| 62 | +*** '''''NEW''''' ''sha_single_output_witness'' (32): the SHA256 of the serialization of the corresponding output witnesses (rangeproof and surjection proof) in <code>CTxOutWitness</code> format. |
| 63 | +
|
| 64 | +The total length of ''SigMsg()'' is ''257--442'' bytes for <code>ANYONECANPAY</code> sighashes, ''366'' bytes for non-<code>ANYONECANPAY</code> sighashes, and both numbers are reduced by 64 bytes for <code>SIGHASH_NONE</code> sighashes, and increased by 32 bytes if an annex is present. Note that this does not include the size of sub-hashes such as ''sha_prevouts'', which may be cached across signatures of the same transaction. |
| 65 | + |
0 commit comments