<p>It's a quirk of the original implementation of the message signing logic which was <a href="https://github.com/bitcoin/bitcoin/pull/524" rel="nofollow noreferrer">introduced in Bitcoin Core in 2011</a>.</p>

<p>When the message is signed, it must be serialized before it is hashed. The serialize methods in Bitcoin Core are shared between the network, hash preimage, file, etc. serializations, most of which require that the data be prepended with a compact size uint that specifies the length of the data. Thus those methods will prepend such a compact size uint when serializing whatever data.</p>

<p>The implementation of the message signing logic used those same serialization methods in order to produce the preimage that would be hashed. Thus the strings must be prepended with their lengths for the hash preimage.</p>

CC BY-SA 3.0
2018-01-17T22:35:08.393Z
2018-01-17T22:35:08.393Z
Ava Chow
<p>the message format is:</p>

<pre><code>length(prefix),prefix,length(message),message
with prefix="Bitcoin Signed Message:\n"
</code></pre>

<p>now I am wondering why the length(message) in the middle is needed as it should be able to just calculate it from <code>total_length-prefix_length</code></p>

<p>Some context here: <a href="https://github.com/ethereum/EIPs/pull/683#issuecomment-358460381" rel="nofollow noreferrer">https://github.com/ethereum/EIPs/pull/683#issuecomment-358460381</a></p>

<message-signing>
Explicit message length in Bitcoin Signed message
ligi
# Explicit message length in Bitcoin Signed message

(Asked by: username_0 on 2018-01-17)

the message format is:

```
length(prefix),prefix,length(message),message
with prefix="Bitcoin Signed Message:\n"

```

now I am wondering why the length(message) in the middle is needed as it should be able to just calculate it from `total_length-prefix_length`

Some context here: <https://github.com/ethereum/EIPs/pull/683#issuecomment-358460381>

**Tags:** message-signing


---

## 1 answers

### Answer by username_1 on 2018-01-17. Score: 2 | Accepted answer

It's a quirk of the original implementation of the message signing logic which was [introduced in Bitcoin Core in 2011](https://github.com/bitcoin/bitcoin/pull/524).

When the message is signed, it must be serialized before it is hashed. The serialize methods in Bitcoin Core are shared between the network, hash preimage, file, etc. serializations, most of which require that the data be prepended with a compact size uint that specifies the length of the data. Thus those methods will prepend such a compact size uint when serializing whatever data.

The implementation of the message signing logic used those same serialization methods in order to produce the preimage that would be hashed. Thus the strings must be prepended with their lengths for the hash preimage.


---

