TeachMeBitcoin

Bytecode Structures of SegWit Outputs

From TeachMeBitcoin, the free encyclopedia ⏱️ 3 min read

Bytecode Structures of P2WPKH and P2WSH Outputs

Under Segregated Witness rules, locking scripts (scriptPubKeys) are significantly more compact and elegant than their legacy counterparts.

Let's dissect the exact bytecode layouts of the two primary SegWit locking formats: P2WPKH (Pay-to-Witness-Public-Key-Hash) and P2WSH (Pay-to-Witness-Script-Hash).


🔑 1. P2WPKH (Pay-to-Witness-Public-Key-Hash)

P2WPKH is the SegWit equivalent of legacy P2PKH, used for standard payments to a single public key.

⚙️ The locking script (scriptPubKey)

OP_0  [20-Byte Public Key Hash]

🔍 Bytecode Breakdown:


📝 2. P2WSH (Pay-to-Witness-Script-Hash)

P2WSH is the SegWit equivalent of legacy P2SH, used to lock funds to complex smart contracts, such as multisig agreements or timelocked escrows.

⚙️ The locking script (scriptPubKey)

OP_0  [32-Byte Witness Script Hash]

🔍 Bytecode Breakdown:


🛡️ Why P2WSH Upgraded to a 32-Byte Hash (SHA-256)

A critical security difference exists between legacy P2SH and Native SegWit P2WSH:

💥 The Birthday Attack Risk

In cryptography, the Birthday Paradox states that finding a collision (two different scripts that yield the exact same hash) is much easier than reversing a specific hash. * For a 160-bit hash (legacy P2SH), the theoretical effort required to find a script collision is:

$$2^{160/2} = 2^{80} \text{ operations}$$

While $2^{80}$ is currently computationally difficult, highly funded adversaries or supercomputing grids could potentially generate two different smart contracts (one benign, one malicious) that hash to the same 20-byte legacy P2SH address, allowing them to steal locked escrow funds.

$$2^{256/2} = 2^{128} \text{ operations}$$

An effort of $2^{128}$ operations is mathematically secure and completely immune to birthday-attack collisions for the foreseeable future, ensuring ultimate security for advanced smart contracts on Bitcoin!

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!