<p>Are you refering to the pay-to-pubkey-hash script (<a href="https://en.bitcoin.it/wiki/Script#Standard_Transaction_to_Bitcoin_address_.28pay-to-pubkey-hash.29" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Script#Standard_Transaction_to_Bitcoin_address_.28pay-to-pubkey-hash.29</a>)?</p>

<p>In that case it is the hash of the public key, which is 20 bytes. It is NOT the public key itself. </p>

<p>Every op-code correponds to a byte-value in the raw transaction script. All operations are preformed on a stack onto which values can be pushed. The public-key-hash must therefore first be pushed onto the stack, before it can be compared to another value on the stack. </p>

<p>To first push the public-key-hash to the stack we prepend "the number of bytes" which is 20, because this value is itself just an op-code that pushes the next 20 bytes of data to the stack. (See <a href="https://en.bitcoin.it/wiki/Script#Constants" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Script#Constants</a>)</p>

<p>This makes the scripting language more flexible.</p>

CC BY-SA 3.0
2017-07-25T20:28:10.137Z
I see. Prepending the number of bytes allows the OP_CODE to not be "married" with an specific value size.
John Smith
CC BY-SA 3.0
2017-07-25T20:33:43.883Z
Otherwise the protocol client would have to do something like if(OP_HASH160) then push the next 20 bytes to te stack. Right?
John Smith
CC BY-SA 3.0
2017-07-25T10:00:17.347Z
2017-07-25T10:00:17.347Z
Bjarne Magnussen
<p>The OP_HASH160 opcode does not actually do any checking or have any size bytes that follows it. All it does is it takes the top stack item and hashes it with RIPEMD160(SHA256()). Then it pushes that item back to the stack. It does not compare anything nor does it require that any data follows the opcode.</p>

<p>The number of bytes that follows the opcode is actually for an OP_PUSHDATA opcode to push the next 20 bytes onto the stack. That pushes the 20 byte hash that we want to compare to to the stack. Then the OP_EQUAL compares the top two stack items: the hash produced by OP_HASH160 and the 20 bytes that we just pushed to the stack.</p>

CC BY-SA 3.0
2017-07-25T21:19:54.057Z
2017-07-25T21:19:54.057Z
Ava Chow
<blockquote>
  <p>why after the OP_HASH160 the number of bytes of the public key should be included if it's always 20 bytes?</p>
</blockquote>

<p>not always :)</p>

<p><a href="https://bitcointalk.org/index.php?topic=671881.0" rel="nofollow noreferrer">https://bitcointalk.org/index.php?topic=671881.0</a></p>

CC BY-SA 3.0
2017-07-25T12:40:05.617Z
2017-07-25T12:40:05.617Z
amaclin
<p>When creating a raw transaction, why after the OP_HASH160 the number of bytes of the hashed public key should be included if it's always 20 bytes (that's what RIPEMD160 throws)?</p>

<p>Does this happen with every OP_CODE value in scripts?</p>

<p>Please explain. Thanks.</p>

<script>
OP_HASH160 and number of bytes of public key
John Smith
# OP_HASH160 and number of bytes of public key

(Asked by: username_0 on 2017-07-25)

When creating a raw transaction, why after the OP\_HASH160 the number of bytes of the hashed public key should be included if it's always 20 bytes (that's what RIPEMD160 throws)?

Does this happen with every OP\_CODE value in scripts?

Please explain. Thanks.

**Tags:** script


---

## 3 answers

### Answer by username_1 on 2017-07-25. Score: 1

Are you refering to the pay-to-pubkey-hash script (<https://en.bitcoin.it/wiki/Script#Standard_Transaction_to_Bitcoin_address_.28pay-to-pubkey-hash.29>)?

In that case it is the hash of the public key, which is 20 bytes. It is NOT the public key itself.

Every op-code correponds to a byte-value in the raw transaction script. All operations are preformed on a stack onto which values can be pushed. The public-key-hash must therefore first be pushed onto the stack, before it can be compared to another value on the stack.

To first push the public-key-hash to the stack we prepend "the number of bytes" which is 20, because this value is itself just an op-code that pushes the next 20 bytes of data to the stack. (See <https://en.bitcoin.it/wiki/Script#Constants>)

This makes the scripting language more flexible.

> **Comments:**
> **username_0**: I see. Prepending the number of bytes allows the OP_CODE to not be "married" with an specific value size.  
> **username_0**: Otherwise the protocol client would have to do something like if(OP_HASH160) then push the next 20 bytes to te stack. Right?  

---

### Answer by username_2 on 2017-07-25. Score: 0

The OP\_HASH160 opcode does not actually do any checking or have any size bytes that follows it. All it does is it takes the top stack item and hashes it with RIPEMD160(SHA256()). Then it pushes that item back to the stack. It does not compare anything nor does it require that any data follows the opcode.

The number of bytes that follows the opcode is actually for an OP\_PUSHDATA opcode to push the next 20 bytes onto the stack. That pushes the 20 byte hash that we want to compare to to the stack. Then the OP\_EQUAL compares the top two stack items: the hash produced by OP\_HASH160 and the 20 bytes that we just pushed to the stack.


---

### Answer by username_3 on 2017-07-25. Score: 0

> why after the OP\_HASH160 the number of bytes of the public key should be included if it's always 20 bytes?

not always :)

<https://bitcointalk.org/index.php?topic=671881.0>


---

