Bitcoin and Public Key Cryptography

Bitcoin uses public key cryptography for creating a key pair that gives access to the funds in bitcoin. The private key allows the user to sign transactions that spend funds; therefore, it needs to be kept secret. The public key derives from the private one and is used to receive bitcoin: it can be safely shared, since from the public key it's not possible to find the private key.



Bitcoin: a new purpose for cryptography

Cryptography was born trying to answer the question: How to make a message intelligible for its intended recipient and unintelligible for everyone else?

For centuries the main application for cryptography has been encryption: how to encrypt (and decrypt) messages.
Bitcoin resorts to cryptography for different reasons: even Bitcoin transactions, routed through its network like messages, are not encrypted.

What does Bitcoin do with cryptography?

Bitcoin uses public key cryptography to generate 2 keys enabling the user to receive and spend bitcoins: the 2 keys are related to each other and the public one derives from the private one. However, given the mathematical relation between the 2, from the public it's not possible to find the private, although it's possible to check the relation between the keys without disclosing the private.

Let's see:

  • every Bitcoin users has 2 keys: a public key and a private key, with "key" being a very big number.
  • the keys give control over the sums, making it possible to receive and spend bitcoins.

We can think to a bank account: you can receive money through your IBAN, which to this aim has to be made public, and spend that money through your pin code, which needs to be known to you only.

We are going to see a bit more about how public key cryptography works.

Public key cryptography

With public key cryptography the user has:

  • a private key, to be kept secret;
  • a public key, derived from the private one through a cryptographic algorithm.

As we have seen, the 2 keys are related to each other, but from the public key it's not possible to find out the private key. This makes it possible to assign the 2 keys different purposes: the private key is to be kept secret while the public key can be safely shared.

Until Bitcoin, public key cryptography has been traditionally used for:

  1. encrypt and decrypt a message;
  2. digital signature, which aims to :
    • prove that a message comes from a particular user;
    • prove that the message hasn't been intercepted and modified after the sender sent it;

Let's see how encryption and digital signature work with public key cryptography.

1 - Encryption

A user shares his or her public key with someone he or she wants to receive an encrypted message from.

The sender uses the recipient's public key to encrypt a message and then send it; the message can be decrypted only by the recipient, or rather by whoever has the private key which generated the public key used to encrypt the message.

The recipient can be sure that nobody will ever guess or found out his or her private key by only knowing the public one: nobody will ever decrypt that message without the private key.

Therefore:

The message is just as secret as the private key: if the key is stolen or lost, the message is either read by someone else or it will remain encrypted and so unreadable for everyone.

2 - Digital signature and the hash

A digital signature is a string that proves that a message is authentic, meaning that it truly comes from its apparent author, and that it's not been altered after it was sent.

A digital signature is produced through a hash algorithm, which takes any input and translates it into a fixed-length, random and impredictable string called hash.

A good hash algorithm is supposed to:

  • produce unique hashes: it won't return the same hash for a different input (an incident called collision);
  • a tiny change in the input produces a different hash and it does so in an unpredictable way;
  • anyone can check that a particular input, using a given hash algorithm, produces a certain hash; however, from the hash you can't find out the input which generated it.

There are different hash algorithms: the one Bitcoin uses is called sha-256

An example: we are going to calculate the hash of the word "ciao" with the sha-256 algorithm, then we add just a number to "ciao" and see how the hash changes.

ciao SHA-256 b133a0c0e9bee3be20163d2ad31d6248db292aa6dcb1ee087a2aa50e0fc75ae2

ciao1 SHA-256 01b2537e787bff61e4518927faee16cfc71f2240bd82fd891c22c7ca9e81b58e

ciao2 SHA-256 21d19e923ae21043fa5810704eab55bda682dc9e036bbf4c50eb732febdff835

ciao3 SHA-256 515275bec0133f4f082f03c3ca0f0f6f44ffdd312b62c1ba930abe6594b98553

It's not possible to guess what the hash of "ciao4" would it be, without trying. You can find hash calculators online, give it a try.

How to sign a message and verify the digital signature?

The sender signs a message by doing this:

  1. calculates the hash of the message;
  2. encrypt the hash with his or her private key;
  3. send the encrypted hash together with the message.

The recipient verifies the signature by doing this:

  1. calculates the hash of the message;
  2. decrypt the encrypted hash (the digital signature) received together with the message using the public key of the sender;
  3. compares the hash with the one he received from the sender: they need to be identical. If they're different, it means that the original message has been altered. If the message changes, the hash changes and so the digital signature: that's because the hash is calculated from the original message.

As we see, the algorithm that creates the public key from the private one is crucial: from the public key should not be possible to find the private key.

Public Key Cryptography and Bitcoin

Let's answer this question now: what does Bitcoin do with public key cryptography?

Just ilke the sender of an encrypted or digitally signed message, a bitcoin user has a private key and a public key; with Bitcoin though:

  • the public key becomes a bitcoin address, to be shared with anyone willing to send us some bitcoins;
  • the private key signs transactions that send bitcoins to someone else: you pay by signing a transaction moving some of your bitcoins (or fractions) to another address.

Transactions

With Bitcoin there are no encrypted messages to send but transactions to sign: you spend bitcoins by signing transactions. Just like a message, you sign a bitcoin transaction with your private key; the private key gives access to the funds that have been cashed in through the corresponding public key- bitcoin address.

What is a transaction?

A transaction is a set of data; its crucial role is to transfer bitcoins among users, or rather their addresses.
A transaction is signed and the digital signature has to be verified.

Why a transaction has to be verified?

Bitcoin is a decentralized digital currency: there's no authority exercising an oversight or governance, everything runs autonomously according to technical rules.

Therefore, transactions need to comply with some standards to be accepted as valid by the network; most importantly, digital signatures need to be checked in order to prove that the transaction has been signed by the "legitimate" private key: in other words, it has to be proven the link between the digital signature and the private key used to sign the transaction.

How to verify the signature of a transaction?

The procedure is the same than the one we have seen when talking about the public key cryptography in general; the algorithm takes:

  1. the transaction;
  2. its digital signature;
  3. the public key of the user signing the transaction.

That's the same procedure introduced earlier in this article to verify the signature in a digitally signed message: without showing the private key, we reach the conclusion that only the private key, "sister" of the public key provided for the check, could have produced that signature.

Once the transaction is verified, and complies with all the other technical requirements, it is able to transfer sums from address to address; it will then be added to a block in the blockchain.

What is a blockchain?

We might talk about this in another article, but for now we can say that the blockchain is the unmodifiable ledger where all the transactions are recorded, providing the full picture of who, or rather what address, owns what.

Conclusions

With this article we introduced how bitcoin makes use of cryptography in an original and ingenious way, and for a new purpose: a decentralized digital currency running autonomously, without the intervention of any central bank or authority.


About the author

Vincenzo Lalli

Vincenzo Lalli

Founder of Avvocloud.net

Avvocloud is an Italian network of lawyers passionate about law, innovation and technology.
Feel free to reach out for any info: send a message.

Thanks for reading!

Creative Commons License

Contact

The Italian Network of Lawyers

Twitter

Linkedin

Message


Support Avvocloud

Our mission is to promote innovation in law: if you like our project, you may consider a small donation.


Donate with Paypal