<p>Starting from basics, each input spends a previous output:</p>
<p><a href="https://web.archive.org/web/20180529095549/https://bitcoin.org/img/dev/en-tx-overview.svg" rel="nofollow noreferrer"><img src="https://web.archive.org/web/20180529095549/https://bitcoin.org/img/dev/en-tx-overview.svg" alt="Input spending output" /></a></p>
<p>To spend the output, the input has to include a reference to it (called an outpoint).  This includes the TXID that included the output as well as the sequence number of that output in the transaction (called a vout for output vector).</p>
<p><a href="https://web.archive.org/web/20180605020459/https://bitcoin.org/img/dev/en-tx-overview-spending.svg" rel="nofollow noreferrer"><img src="https://web.archive.org/web/20180605020459/https://bitcoin.org/img/dev/en-tx-overview-spending.svg" alt="Spending an output" /></a></p>
<p>In order for Alice to pay Bob in a pay-to-public-key-hash (P2PKH) output, Bob needs to create a private key (which he keeps secret), derive a public key, hash that public key (encoding it as a Bitcoin address), and then give that hash to Alice.  Alice uses that hash in a P2PKH output in TX1.</p>
<p><a href="https://web.archive.org/web/20180529095549/https://bitcoin.org/img/dev/en-creating-p2pkh-output.svg" rel="nofollow noreferrer"><img src="https://web.archive.org/web/20180529095549/https://bitcoin.org/img/dev/en-creating-p2pkh-output.svg" alt="Creating a P2PKH output" /></a></p>
<p>When Bob wants to later spend that output from TX1 (using it as an input), his computer gets the hash (address) from the TX1 output, finds the corresponding public key, inserts that public key into the signature script, then finds the corresponding private key in its secure database, and uses that key to create an ECDSA signature which it also adds to the signature script.</p>
<p><a href="https://web.archive.org/web/20180531050530/https://bitcoin.org/img/dev/en-unlocking-p2pkh-output.svg" rel="nofollow noreferrer"><img src="https://web.archive.org/web/20180531050530/https://bitcoin.org/img/dev/en-unlocking-p2pkh-output.svg" alt="Creating a signature script" /></a></p>
<p>So only one keypair is used per output, although the same keypair may be used in multiple outputs.  (This is called key reuse, and it's discouraged to improve privacy and, possibly, security.)</p>
<p>(The images above are from the <a href="https://bitcoin.org/en/developer-guide#transactions" rel="nofollow noreferrer">Bitcoin.org Developer Guide transactions section</a>, which is under the MIT license.)</p>

CC BY-SA 3.0
2014-12-06T20:45:01.527Z
One thing is not clear yet (anyway I accepted your answer). When I spend the coins from the previous transaction, I use another private key (because as I know unique keypairs should be used for every transaction), not that private key which is the pair of the public key included in the pubkey script, don't I? (yeah, I'm not familiar with this scripting system, yet)
Zoltán
CC BY-SA 3.0
2014-12-06T20:51:25.157Z
I think you're confused about how addresses work.  Give me a sec and I'll edit my answer.
David A. Harding
CC BY-SA 3.0
2014-12-06T21:47:19.237Z
One more question, if you are still here and I'm not bothering you :) To put it in a nutshell, with the signing with earlier private keys I can prove that the earlier transactions were mine? So I also generate (maybe) an address when I spend these bitcoins, however I use private keys from my earlier transactions and this newly generated address is just for receiving the "change"...?
Zoltán
CC BY-SA 3.0
2014-12-06T21:55:39.380Z
Right, signature A in TX2 mathematically corresponds to pubkey A in TX1's output in a provable way.  TX2 may include pubkey B to return change to you.  Pubkey B was created with a different private key than pubkey A.
David A. Harding
CC BY-SA 3.0
2018-02-14T16:33:12.597Z
@Zoltán You can search matt thomas on youtube, his videos on bitcoin are very informative.
Suraj Jain
CC BY-SA 4.0
2014-12-06T20:12:38.513Z
2021-01-13T01:34:27.093Z
2021-01-13T01:34:27.093Z
David A. Harding
<p>I'm just writing a small essay about the basics of BTC and I ran into a small problem.</p>

<p>I know, that there are digital signatures in our bitcoins' earlier transactions (our transaction inputs) and in the next owner's public key. How can the system verify, if the input is really from our earlier output? We can just use a random (unspent) output from an earlier block, can't we? I know that we are the next owner and our public key is in the transaction, but we can (and should) generate a a new public-private key for our next transaction, so the new private key won't match our "old" public key... What is that thing I don't know or understand?</p>

<transactions><transaction-verification>
Input validation in transactions
Zoltán
# Input validation in transactions

(Asked by: username_0 on 2014-12-06)

I'm just writing a small essay about the basics of BTC and I ran into a small problem.

I know, that there are digital signatures in our bitcoins' earlier transactions (our transaction inputs) and in the next owner's public key. How can the system verify, if the input is really from our earlier output? We can just use a random (unspent) output from an earlier block, can't we? I know that we are the next owner and our public key is in the transaction, but we can (and should) generate a a new public-private key for our next transaction, so the new private key won't match our "old" public key... What is that thing I don't know or understand?

**Tags:** transactions, transaction-verification


---

## 1 answers

### Answer by username_1 on 2014-12-06. Score: 2 | Accepted answer

Starting from basics, each input spends a previous output:

[![Input spending output](https://web.archive.org/web/20180529095549/https://bitcoin.org/img/dev/en-tx-overview.svg)](https://web.archive.org/web/20180529095549/https://bitcoin.org/img/dev/en-tx-overview.svg)

To spend the output, the input has to include a reference to it (called an outpoint). This includes the TXID that included the output as well as the sequence number of that output in the transaction (called a vout for output vector).

[![Spending an output](https://web.archive.org/web/20180605020459/https://bitcoin.org/img/dev/en-tx-overview-spending.svg)](https://web.archive.org/web/20180605020459/https://bitcoin.org/img/dev/en-tx-overview-spending.svg)

In order for Alice to pay Bob in a pay-to-public-key-hash (P2PKH) output, Bob needs to create a private key (which he keeps secret), derive a public key, hash that public key (encoding it as a Bitcoin address), and then give that hash to Alice. Alice uses that hash in a P2PKH output in TX1.

[![Creating a P2PKH output](https://web.archive.org/web/20180529095549/https://bitcoin.org/img/dev/en-creating-p2pkh-output.svg)](https://web.archive.org/web/20180529095549/https://bitcoin.org/img/dev/en-creating-p2pkh-output.svg)

When Bob wants to later spend that output from TX1 (using it as an input), his computer gets the hash (address) from the TX1 output, finds the corresponding public key, inserts that public key into the signature script, then finds the corresponding private key in its secure database, and uses that key to create an ECDSA signature which it also adds to the signature script.

[![Creating a signature script](https://web.archive.org/web/20180531050530/https://bitcoin.org/img/dev/en-unlocking-p2pkh-output.svg)](https://web.archive.org/web/20180531050530/https://bitcoin.org/img/dev/en-unlocking-p2pkh-output.svg)

So only one keypair is used per output, although the same keypair may be used in multiple outputs. (This is called key reuse, and it's discouraged to improve privacy and, possibly, security.)

(The images above are from the [Bitcoin.org Developer Guide transactions section](https://bitcoin.org/en/developer-guide#transactions), which is under the MIT license.)

> **Comments:**
> **username_0**: One thing is not clear yet (anyway I accepted your answer). When I spend the coins from the previous transaction, I use another private key (because as I know unique keypairs should be used for every transaction), not that private key which is the pair of the public key included in the pubkey script, don't I? (yeah, I'm not familiar with this scripting system, yet)  
> **username_1**: I think you're confused about how addresses work.  Give me a sec and I'll edit my answer.  
> **username_0**: One more question, if you are still here and I'm not bothering you :) To put it in a nutshell, with the signing with earlier private keys I can prove that the earlier transactions were mine? So I also generate (maybe) an address when I spend these bitcoins, however I use private keys from my earlier transactions and this newly generated address is just for receiving the "change"...?  
> **username_1**: Right, signature A in TX2 mathematically corresponds to pubkey A in TX1's output in a provable way.  TX2 may include pubkey B to return change to you.  Pubkey B was created with a different private key than pubkey A.  
> **username_2**: @username_0 You can search matt thomas on youtube, his videos on bitcoin are very informative.  

---

