<p>The digital signatures which are included in a transaction input to authorize the spending commit to (largely) the entire spending transaction. If any part of it changes (including which outputs are spent by it, or where the resulting funds are sent), the signatures are invalidated.</p>
<p>The &quot;cryptographic puzzles&quot; you refer to, if we ignore the script system, boil down to a cryptographic primitive called a digital signature. It is a collection of 3 algorithms:</p>
<ul>
<li>KeyGen(): generate a private key with corresponding public key</li>
<li>Sign(message, private_key): <strong>given a message</strong> to sign, and a private key to sign with, produce a signature.</li>
<li>Verify(message, public_key, signature): given a message that was allegedly signed, a public key, and a signature, verify if that signature was created by the corresponding private key <strong>on that message</strong>.</li>
</ul>
<p>The key generation is generally handled by key derivation logic, which you've already been asking about here. The signing/verify functions come from ECDSA (for pre-segwit, or segwit v0) or from BIP340 (for segwit v1). The message argument is important here: when the message changed between signing and verifying, verification will (almost certainly) fail. If an attacker can construct a signature on a different message than messages they've already seen signed, without access to the private key, we consider the signature scheme broken. ECDSA and BIP340 are, as far as we know, not broken.</p>
<p>In the context of Bitcoin transactions, this message is called the &quot;sighash&quot;. The details are somewhat complicated, but slightly simplified, it consists of most of the fields of the <em>spending</em> transaction (so the transaction which contains the signatures).</p>
<p>If an attacker takes a valid transaction's inputs, and reconstructs a new transaction with different outputs with those same inputs, the signatures won't verify anymore, because some of the message being signed changed.</p>

CC BY-SA 4.0
2023-07-24T23:01:47.307Z
So, the digital signatures within the input, besides proving that the previous UTXO can be spent, are also connected to the outputs within the current transaction, i.e. through them the entire transaction is verified? Is that "sighash" (this is that digital signature I guess) part of the unlocking script because I didn't see it as a part of the transaction itself (https://en.bitcoin.it/wiki/Protocol_documentation#tx).
dassd
CC BY-SA 4.0
2023-07-24T23:08:28.730Z
Yes, the signatures are connected to the outputs sent to (and other transaction data, like version number, locktime, ...). No, the sighash is computed implicitly by the OP_CHECKSIG or related opcodes, not computed by the script.
Pieter Wuille
CC BY-SA 4.0
2023-07-24T23:18:33.033Z
Alright, clear, I see now that there is a section in the book that deals with "sighash". I guess I was a little hasty with this topic. Anyway, thanks for your help Pieter for the umpteenth time. All the best!
dassd
CC BY-SA 4.0
2023-07-24T22:44:18.790Z
2023-07-24T22:53:09.840Z
2023-07-24T22:53:09.840Z
Pieter Wuille
<p>I'm relatively new to Bitcoin and I'm wondering how Bitcoin defends against the following scenario and if it's even possible.</p>
<p>Before I start with the scenario, just two notes:</p>
<p><strong>1. There is a lot of text because I just tried to explain in detail what I mean.</strong></p>
<p><strong>2. Please note that I am relatively new to bitcoin</strong></p>
<p>So let's start.</p>
<p>Suppose we have an initial transaction (T1) from which everything starts. This transaction has been part of the blockchain for some time and has the following 2 inputs and 3 outputs. So we can write this transaction as follows:</p>
<pre><code>T1(T1I1, T1I2; T1O1, T1O2, T1O3)
</code></pre>
<p>To spend funds from any of these outputs it is necessary to solve cryptographic puzzle set by locking script in corresponding output. For example, if we want to spend funds from T1O2, we will need to create an input in a new transaction with an unlocking script that solves the cryptographic puzzle set by T1O2.</p>
<p>Now, let's say someone who knows how to solve all these cryptographic puzzles wants to spend outputs 2 and 3 from the previous transaction (T1). So a new transaction will be created and, for example, it will look like this:</p>
<pre><code>T2(T2I1, T2O2; T2O1, T2O2)
</code></pre>
<ul>
<li>T2I1 solves the cryptographic puzzle from T1O2</li>
<li>T2I2 solves the cryptographic puzzle from T1O3</li>
</ul>
<p>Also, let's assume that the fee for this transaction is set to 1 BTC which means that this transaction will be accepted within 6 blocks (this is a very high fee, but for simplicity I put 1 BTC). So, with this assumption, the transaction is put into the mempool and will be accepted in the next, say, 1-2 hours.</p>
<p>Now we come to the crux of the whole story. Suppose a hacker has access to the mempool and monitors all new transactions. As he watches, he notices our transaction T2 and wants to steal funds from it, that is, from T1 whose funds transaction T2 is consuming. He can't do this in the normal way because he is unable to solve the cryptographic puzzles from the outputs O2 and O3 in T1, so he can't make &quot;valid&quot; inputs. He also can't steal funds using the outputs from T2 because he doesn't know the solution to these cryptographic puzzles either. But what he can do is the following (I guess he can, that's my question). The procedure that the hacker does is presented in steps</p>
<ol>
<li><p>Since the hacker does not know how to create unlocking scripts to solve the cryptographic puzzles set by locking scripts in the outputs of transaction T1, he will just take the solutions (inputs, unlocking scripts) from T2. Let's say he only takes the first input (T2I1).</p>
</li>
<li><p>After that, he creates new outputs with locking scripts that he will know how to solve.</p>
</li>
<li><p>Since he has inputs and outputs, he creates a transaction that will look like this:</p>
<p>T3(T2I1, T3O1)</p>
</li>
</ol>
<ul>
<li>input T2I1 hacker took from valid transaction T2</li>
<li>output T3O1 he just created with locking script he knows solution to</li>
</ul>
<ol start="4">
<li>Hacker now takes this transaction and sends it through the bitcoin network and sets the fee at 5 bitcoins (5 times the original transaction) which ensures that the transaction is immediately accepted in the next block.</li>
</ol>
<p>These steps lead to the fact that there are two conflicting transactions in the mempool because they consume the same input (T2I1). When a miner creates a new block, he will select a set of transactions to include in the new block based on the fee. Since a malicious transaction (hacker transaction) has a high fee, it will be immediately included in the next block. <strong>In this way, a hacker's transaction is one that is included in the blockchain and its outputs (for which he knows the solution) become part of the blockchain, while a valid transaction and those outputs are discarded as malicious. By doing so, he stole bitcoins.</strong></p>
<p>Based on everything written, I have two questions:</p>
<p><strong>1. Is this a possible scenario?</strong></p>
<p><strong>2. How does Bitcoin defend against this scenario?</strong></p>

CC BY-SA 4.0
2023-07-25T00:40:17.063Z
Does this answer your question: https://bitcoin.stackexchange.com/q/95564/5406?
Murch
<security><signature><transaction-verification><sighash>
Taking the existing INPUT and changing the OUTPUT to steal bitcoins. Is this a possible bitcoin theft scenario and how to prevent it?
dassd
# Taking the existing INPUT and changing the OUTPUT to steal bitcoins. Is this a possible bitcoin theft scenario and how to prevent it?

(Asked by: username_0 on 2023-07-24)

I'm relatively new to Bitcoin and I'm wondering how Bitcoin defends against the following scenario and if it's even possible.

Before I start with the scenario, just two notes:

**1. There is a lot of text because I just tried to explain in detail what I mean.**

**2. Please note that I am relatively new to bitcoin**

So let's start.

Suppose we have an initial transaction (T1) from which everything starts. This transaction has been part of the blockchain for some time and has the following 2 inputs and 3 outputs. So we can write this transaction as follows:

```
T1(T1I1, T1I2; T1O1, T1O2, T1O3)

```

To spend funds from any of these outputs it is necessary to solve cryptographic puzzle set by locking script in corresponding output. For example, if we want to spend funds from T1O2, we will need to create an input in a new transaction with an unlocking script that solves the cryptographic puzzle set by T1O2.

Now, let's say someone who knows how to solve all these cryptographic puzzles wants to spend outputs 2 and 3 from the previous transaction (T1). So a new transaction will be created and, for example, it will look like this:

```
T2(T2I1, T2O2; T2O1, T2O2)

```

* T2I1 solves the cryptographic puzzle from T1O2
* T2I2 solves the cryptographic puzzle from T1O3

Also, let's assume that the fee for this transaction is set to 1 BTC which means that this transaction will be accepted within 6 blocks (this is a very high fee, but for simplicity I put 1 BTC). So, with this assumption, the transaction is put into the mempool and will be accepted in the next, say, 1-2 hours.

Now we come to the crux of the whole story. Suppose a hacker has access to the mempool and monitors all new transactions. As he watches, he notices our transaction T2 and wants to steal funds from it, that is, from T1 whose funds transaction T2 is consuming. He can't do this in the normal way because he is unable to solve the cryptographic puzzles from the outputs O2 and O3 in T1, so he can't make "valid" inputs. He also can't steal funds using the outputs from T2 because he doesn't know the solution to these cryptographic puzzles either. But what he can do is the following (I guess he can, that's my question). The procedure that the hacker does is presented in steps

1. Since the hacker does not know how to create unlocking scripts to solve the cryptographic puzzles set by locking scripts in the outputs of transaction T1, he will just take the solutions (inputs, unlocking scripts) from T2. Let's say he only takes the first input (T2I1).
2. After that, he creates new outputs with locking scripts that he will know how to solve.
3. Since he has inputs and outputs, he creates a transaction that will look like this:

   T3(T2I1, T3O1)

* input T2I1 hacker took from valid transaction T2
* output T3O1 he just created with locking script he knows solution to

4. Hacker now takes this transaction and sends it through the bitcoin network and sets the fee at 5 bitcoins (5 times the original transaction) which ensures that the transaction is immediately accepted in the next block.

These steps lead to the fact that there are two conflicting transactions in the mempool because they consume the same input (T2I1). When a miner creates a new block, he will select a set of transactions to include in the new block based on the fee. Since a malicious transaction (hacker transaction) has a high fee, it will be immediately included in the next block. **In this way, a hacker's transaction is one that is included in the blockchain and its outputs (for which he knows the solution) become part of the blockchain, while a valid transaction and those outputs are discarded as malicious. By doing so, he stole bitcoins.**

Based on everything written, I have two questions:

**1. Is this a possible scenario?**

**2. How does Bitcoin defend against this scenario?**

**Tags:** security, signature, transaction-verification, sighash

> **Comments:**
> **username_1**: Does this answer your question: https://bitcoin.stackexchange.com/q/95564/5406?  

---

## 1 answers

### Answer by username_2 on 2023-07-24. Score: 3 | Accepted answer

The digital signatures which are included in a transaction input to authorize the spending commit to (largely) the entire spending transaction. If any part of it changes (including which outputs are spent by it, or where the resulting funds are sent), the signatures are invalidated.

The "cryptographic puzzles" you refer to, if we ignore the script system, boil down to a cryptographic primitive called a digital signature. It is a collection of 3 algorithms:

* KeyGen(): generate a private key with corresponding public key
* Sign(message, private\_key): **given a message** to sign, and a private key to sign with, produce a signature.
* Verify(message, public\_key, signature): given a message that was allegedly signed, a public key, and a signature, verify if that signature was created by the corresponding private key **on that message**.

The key generation is generally handled by key derivation logic, which you've already been asking about here. The signing/verify functions come from ECDSA (for pre-segwit, or segwit v0) or from BIP340 (for segwit v1). The message argument is important here: when the message changed between signing and verifying, verification will (almost certainly) fail. If an attacker can construct a signature on a different message than messages they've already seen signed, without access to the private key, we consider the signature scheme broken. ECDSA and BIP340 are, as far as we know, not broken.

In the context of Bitcoin transactions, this message is called the "sighash". The details are somewhat complicated, but slightly simplified, it consists of most of the fields of the *spending* transaction (so the transaction which contains the signatures).

If an attacker takes a valid transaction's inputs, and reconstructs a new transaction with different outputs with those same inputs, the signatures won't verify anymore, because some of the message being signed changed.

> **Comments:**
> **username_0**: So, the digital signatures within the input, besides proving that the previous UTXO can be spent, are also connected to the outputs within the current transaction, i.e. through them the entire transaction is verified? Is that "sighash" (this is that digital signature I guess) part of the unlocking script because I didn't see it as a part of the transaction itself (https://en.bitcoin.it/wiki/Protocol_documentation#tx).  
> **username_2**: Yes, the signatures are connected to the outputs sent to (and other transaction data, like version number, locktime, ...). No, the sighash is computed implicitly by the OP_CHECKSIG or related opcodes, not computed by the script.  
> **username_0**: Alright, clear, I see now that there is a section in the book that deals with "sighash". I guess I was a little hasty with this topic. Anyway, thanks for your help username_2 for the umpteenth time. All the best!  

---

