logo academy

Public and private key: what are they and how do they work? 

January 12, 2023

8 min

Public and private key: what are they and how do they work? 
Beginner

Have you ever wondered why they are called ‘cryptocurrencies’? The origin of this term goes back to cryptography, a branch of mathematics useful for protecting online data and making networks more secure. Specifically, asymmetric cryptography makes it possible to exchange crypto, thanks to a public key and a private key: let’s find out together what they are, what the differences are and how they are used to perform transactions on blockchain.

Public and private key: asymmetric encryption

The history of cryptography teaches us that, originally, this science was used to encrypt and decrypt important military and government information.

Those who own cryptocurrencies, on the other hand, use cryptography for the custody of their funds, i.e. to prove their exclusive ownership. Specifically, blockchain technology employs asymmetric or public key cryptography: each user of the respective network is assigned a public key and a private key, a pair of cryptographic codes linked by mathematical logic. 

More precisely, cryptographic keys are associated with a cryptocurrency wallet: a tool similar to a ‘digital wallet’ that allows you to administer, send, receive and store your cryptocurrencies; it is in these operations that you can see the difference between public and private keys. 

The latter allows cryptocurrencies held in the wallet to be used, signing transactions so that they are approved. The private key is a code similar to a PIN or password, only the owner of the wallet should know it. 

The public key, on the other hand, can be shared with everyone, so that the authenticity of the signatures associated with the transaction can be verified. The public key can also be used to receive cryptocurrencies, as an alternative to the wallet address. 

The relationship between public key and private key is unidirectional, as an algorithm generates the former from the latter and the operation cannot be reversed. Let’s place this mechanism, fundamental to cryptocurrency security, in the process of creating a wallet.

Private key: what is it for?

To any wallet, first of all, corresponds a seed phrase, also known as a recovery phrase. This is a sequence of 12 or 24 words, randomly generated by an algorithm, that allows the user to access their wallet or recover it on another device. You can consider the seed phrase as a sort of ‘master password‘, from which all public and private keys associated with the wallet are derived. 

The seed phrase, through a hash function, is then transformed into an alphanumeric sequence that corresponds to the wallet private key.

This string of letters and numbers no longer makes any sense and it is mathematically impossible to ‘invert’ it to trace back to the seed phrase; in practice, the private key is in a form similar to this one: 

534EL5ntg8oh34o5KE34534jnl3qtWqF45o3O4JK34h4thapfnqc8tgpnap

Think of the private key as a personal PIN, or your signature, through which you can manage the funds you hold. In particular, this personal code is needed to initiate transactions and arrange any other operation related to one’s crypto. Usually, in a wallet, a different private key is associated with each blockchain network, but derived from the same seed phrase. For example, for Bitcoin and Ethereum we will have separate keys and addresses.   

To carefully guard your private key, you need to adopt the basic principles of password management: anyone who knows it could use your cryptocurrencies! Some so-called non-custodial wallets require the user to store their own seed phrase and thus their private keys; Metamask, the leading crypto wallet for Ethereum, is an example. Alternatively, if you want to simplify your key management, you could outsource key storage to experienced third parties, using custodial wallets such as the one integrated in Young Platform’s exchange. In any case, choose the wallet solution that is best for you, suited to your experience and uses.

We said that the private key is used to sign transactions, so that anyone can verify that it was the legitimate owner of those funds who initiated the transaction. However, to recognise the authenticity of signatures and to indicate the recipient of cryptocurrencies, other cryptographic codes are needed: let us therefore delve into what a public key and a wallet address are. 

How is the public key calculated?

Starting from the private key, it is possible to derive the public key associated to the same wallet address: it will result from a particular geometric function, called ECC (Elliptic Curve Cryptography). In practice, the public key (P) is linked to the private key (k) according to the formula P=k*G, where G is a constant. On the Cartesian plane, the two keys are the coordinates of a point on the ECC curve. 

In simpler words, given the private key, it is possible to find the public key, but the opposite is impossible, as it would require calculations that are too complex, even for quantum computers.

So, the public key is another alphanumeric code, similar to the private key, but which does not reveal any information about the latter. For this reason, the former can be shared without risk: it allows users to receive cryptocurrencies and verify the authenticity of transactions.

Process of creation of public and private keys.

Each time a transaction is made on blockchain, in fact, the network assesses whether the sender’s public key is consistent with the digital signature associated with the transaction, according to a mechanism that we will explain later. Furthermore, from the public key derives the wallet address: a string of 42 characters, indicated by the recipient to receive cryptocurrencies. In a nutshell, the wallet address functions like the IBAN of our bank account.

Now that we know what the public key and the private key are, we can actually understand how they are used by simulating a cryptocurrency mailing. 

Digital signatures: verifying a transaction on blockchain

Suppose Andrea wants to send 1 BTC to Beatrice:

  1. Andrea, through his wallet, tells the blockchain that he wants to send 1 BTC to Beatrice’s address, generating a message with the necessary information; 
  2. These indications are ‘summarised’ by a hash function in a more compact code;
  3. Andrea encrypts the hash with his private key, thus generating a digital signature. This approves the transaction;
  4. The digital signature is attached to the source information, not yet translated into a hash, and together they are sent to the validator network.

Bitcoin nodes, before communicating the information to the miners, must verify the validity of the transaction: 

  1. They enter the information, related to the transaction, into the same hash function used by Andrea, thus receiving a hash as output;
  2. Using the public key associated with Andrea’s wallet, they decrypt the digital signature of the transaction, deriving another hash string as output. Remember that each public key can decrypt its respective private key and vice versa.
  3. They check that the two hashes match: if they are the same, it will be irrefutable proof that it was indeed Andrea who sent the cryptocurrencies to Beatrice, i.e. the rightful owner of the funds. This is because, if the information has not changed, the hash function will always return the same combination, i.e. the hash from which the digital signature was also created.
How do private and public keys work?

In the example we have just given, the wallet only needs Andrea’s signature to move funds; however, there are wallets that require multiple digital signatures to approve a transaction. These, in a nutshell, are controlled by several co-owners, each associated with a private key: these are multisig wallets. 

Now that we have discovered what the public key and private key are, you can use your wallets with greater awareness and thus security.

Related