Skip to main content
5 of 12
Correction for witness item counts
Murch
  • 77.9k
  • 35
  • 190
  • 641

TL;DR: A P2WPKH input should be conservatively estimated with 68.0 vbytes. Valid inputs generally weigh 67.75 or 68.0 vbytes, depending on whether the r-value is high or low in the signature. 68.0 vbytes is the conservative estimate allowing for high-r signatures. A wallet using signature grinding will always produce a 67.75 vbyte input which is highly recommended.


Each input commits to spending a specific UTXO by providing its transaction outpoint:

PREVOUT: hash (32 bytes)
         index (4 bytes)

The scriptsig for a P2WPKH input is empty, however, the scriptsig length must be provided as 0:

SCRIPTSIG: length (1 byte)
  no content

Each transaction input has its own sequence number:

sequence (4 bytes)

A P2WPKH input requires a witness in the transaction's witness block:

WITNESS: item count (1 byte)
  signature length (1 byte)
  signature: (71 or 72 bytes)¹
  pubkey length (1 byte)
  pubkey (33 bytes)

¹ depending whether r-value is high or low. A non-standard high-s and high-r signatures would be 73 bytes, also see What is the maximum size of a DER encoded ECDSA signature?.

Therefore, a P2WPKH transaction input to a transactions adds a maximum of…

weight:

4 × (32 + 4 + 1 + 4) + 1 + 1 + 72 + 1 + 33 = 272 WU

vsize:

32 + 4 + 1 + 4 + (1 + 1 + 72 + 1 + 33) / 4 = 68 vbytes

serialized byte length:

32 + 4 + 1 + 4 + 1 + 1 + 72 + 1 + 33 = 149 bytes

If the signing wallet uses signature grinding, the r-value is always 32 bytes, reducing the signature to 71 bytes, and the above maxima to 271 WU, 67.75 vbytes, and 148 bytes respectively. (A non-standard signature with high-s and high-r weighs 273 WU, 68.25, and 149 vbytes.)

Note that adding a segwit input adds 2 witness bytes to the transaction header, the witness marker and witness flag. When there is at least one segwit input every input must have a witness item count, which must be provided as 0 for non-segwit inputs. The overall transaction elements of a segwit transaction are:

version (4 bytes)
witness marker (1 WU)
witness flag (1 WU)
input count (1 byte)
  inputs (variable size)
output count (1 byte)
  outputs (variable size)
witness item counts (1 WU per input)
  witnesses (variable weight)
locktime (4 bytes)
Murch
  • 77.9k
  • 35
  • 190
  • 641