MODULE 4
Bitcoin Definition
• Bitcoin is a decentralized, peer-to-peer digital currency and payment system
introduced in 2008 by Satoshi Nakamoto.
• It operates without a central bank or administrator, using a public distributed
ledger called the blockchain to record transactions.
• Bitcoin can be defined in various ways; it's a protocol, a digital currency, and
a platform.
• It is a combination of a peer-to-peer network, protocols, and software that
facilitates the creation and usage of the digital currency.
• Nodes in the network communicate using the Bitcoin protocol.
• It enables decentralized currency, meaning no central authority controls it.
• It solves the double-spending problem using a distributed ledger
(blockchain).
Bitcoin: Challenges and Solutions
• Volatility – Bitcoin prices can swing dramatically, hindering its use as a
stable unit of account.
• Scalability – The network can process only a limited number of transactions
per second (roughly 7 TPS).
• Energy Consumption – Mining requires significant electricity, leading to
environmental concerns.
• Security/Key Management – Losing private keys means permanent loss of
assets.
Cryptographic keys
• Cryptographic keys are special mathematical codes used in cryptography to
secure data and transactions.
• In blockchain and cryptocurrencies like Bitcoin, cryptographic keys help
protect ownership, verify transactions, and ensure security.
• There are mainly two types of cryptographic keys:
1. Private Key
2. Public Key
• From the public key, a cryptocurrency address is generated.
Private Keys
• A private key is a secret number used to access and control bitcoins.
• It is used to sign transactions in the Bitcoin network.
• Private keys are usually 256-bit randomly generated numbers.
• They must be kept secret, because anyone with the private key can spend the
bitcoins.
• For example, the consider the following private key:
• When converted into WIF format, it looks as shown here:
Wallet Import Format (WIF)
• WIF is a special format used to store private keys.
• It converts the private key into Base58 format.
• Makes the key easier to copy, store, and import into wallets.
Mini Private Key Format
• A short version of the private key.
• Usually 30 characters or less.
• Used where space is limited.
• Often used in physical bitcoins and QR codes.
QR Code for Private Keys
• Private keys can also be stored as QR codes.
• Easy to scan using mobile wallets.
• Used in paper wallets and physical bitcoins.
• QR codes provide better error correction and easy storage.
Public keys in Bitcoin
• Public keys exist on the blockchain and all network participants can see
them.
• Public keys are derived from private keys due to their special
mathematical relationship with those private keys.
• Once a transaction signed with the private key is broadcast on the Bitcoin
network, public keys are used by the nodes to verify that the transaction
has indeed been signed with the corresponding private key. This process of
verification proves the ownership of the bitcoin.
• Bitcoin uses ECC based on the SECP256K1 standard. More specifically, it
makes use of an Elliptic Curve Digital Signature Algorithm (ECDSA)
to ensure that funds remain secure and can only be spent by the legitimate
owner.
• Public keys can be represented in uncompressed or compressed format,
and are fundamentally x and y coordinates on an elliptic curve.
• In uncompressed format, public keys are presented with a prefix of 0x4 in
hexadecimal format. x and y coordinates are both 32 bytes in length.
• In total, a compressed public key is 33 bytes long, compared to the 65-byte
uncompressed format. The compressed version of public keys include only
the x part, since the y part can be derived from it.
• The reason why the compressed version of public keys works is that if the
ECC graph is visualized, it reveals that the y coordinate can be either below
the x axis or above the x axis, and as the curve is symmetric, only the
location in the prime field is required to be stored.
• If y is even then its value lies above the x axis, and if it is odd then it is
below the x axis. This means that instead of storing both x and y as the
public key, only x needs to be stored with the information about whether y is
even or odd.
• Initially, the Bitcoin client used uncompressed keys, but starting from
Bitcoin Core client 0.6, compressed keys are used as standard. This
resulted in an almost 50% reduction of space used to store public keys
in the blockchain.
Keys are identified by various prefixes, described as follows:
• Uncompressed public keys use 0x04 as the prefix. Uncompressed public
keys are 65 bytes long. They are encoded as 256-bit unsigned big-endian
integers (32 bytes), which are concatenated together and finally prefixed
with a byte 0x04. This means 1 byte for the 0x04 prefix, 32 bytes for the
x integer, and 32 bytes for y integer, which makes it 65 bytes in total.
• Compressed public keys start with 0x03 if the y 32-byte (256-bit) part of
the public key is odd. It is 33 bytes in length as 1 byte is used by the 0x03
prefix (depicting an odd y) and 32 bytes for storing the x coordinate.
• Compressed public keys start with 0x02 if the y 32-byte (256-bit) part of
the public key is even. It is 33 bytes in length as 1 byte is used by the
0x02 prefix (depicting an even y) and 32 bytes for storing the x
coordinate.
Addresses in Bitcoin
• A Bitcoin address is created by taking the corresponding public key of
a private key and hashing it twice, first with the SHA256 algorithm
and then with RIPEMD160.
• The resultant 160-bit hash is then prefixed with a version number
and finally encoded with a Base58Check encoding scheme.
• The Bitcoin addresses are 26-35 characters long and begin with digits
1 or 3.
• A typical Bitcoin address looks like the string shown here:
1ANAguGG8bikEv2fYsTBnRUmx7QUcK58wt
• Addresses are also commonly encoded in a QR code for easy
distribution.
• The QR code of the preceding Bitcoin address is shown in the following
image:
Figure 6.10: QR code of the Bitcoin address 1ANAguGG8bikEv2fYsTBnRUmx7QUcK58wt
• Currently, there are two types of addresses, the commonly used P2PKH and
another P2SH type, starting with numbers 1 and 3, respectively.
• In the early days, Bitcoin used direct Pay-to- Pubkey, which is now
superseded by P2PKH.
• However, direct Pay-to-Pubkey is still used in Bitcoin for coinbase
addresses.
• Addresses should not be used more than once; otherwise, privacy and
security issues can arise.
• Avoiding address reuse circumvents anonymity issues to an extent, but
Bitcoin has some other security issues as well, such as transaction
malleability, Sybil attacks, race attacks, and selfish mining, all of which
require different approaches to resolve.
Base58Check encoding
• Bitcoin addresses are encoded using the Base58Check encoding.
• This encoding is used to limit the confusion between various
characters, such as 0OIl, as they can look the same in different fonts.
• The encoding basically takes the binary byte arrays and converts them
into human- readable strings.
• This string is composed by utilizing a set of 58 alphanumeric symbols.
The following diagram shows how an address is generated, from generating
the private key to the final output of the Bitcoin address:
Figure 6.12: Address generation in Bitcoin
• In the preceding diagram, there are several steps that we will now explain:
1. In the first step, we have a randomly generated ECDSA private key.
2. The public key is derived from the ECDSA private key.
3. The public key is hashed using the SHA-256 cryptographic hash function.
4. The hash generated in step 3 is hashed using the RIPEMD-160 hash
function.
5. The version number is prefixed to the RIPEMD-160 hash generated in
step 4.
6. The result produced in step 5 is hashed using the SHA-256 cryptographic
hash function.
7. SHA-256 is applied again.
8. The first 4 bytes of the result produced from step 7 is the address
checksum.
9. This checksum is appended to the RIPEMD-160 hash generated in step 4.
10. The resultant byte string is encoded into a Base58-encoded string by
applying the Base58 encoding function.
11. Finally, the result is a typical Bitcoin address.
In addition to common types of addresses in Bitcoin, there are some
advanced types of addresses available in Bitcoin too.
Vanity addresses
• As Bitcoin addresses are based on Base58 encoding, it is possible to
generate addresses that contain human-readable messages. An example is
shown as follows—note that in the first line, the name BasHir appears:
• Vanity addresses are generated using a purely brute-force method. An
example of a paper wallet with a vanity address is shown in the following
screenshot:
• In the preceding screenshot, on the right-hand bottom corner, the public
vanity address is displayed with a QR code. The paper wallets can be stored
physically as an alternative to the electronic storage of private keys.
Multi-signature addresses
• As the name implies, these addresses require multiple private keys.
• In practical terms, this means that in order to release the coins, a certain set
number of signatures is required.
• This is also known as M of N multisig.
• Here, M represents the threshold or minimum number of signatures required
from N number of keys to release the Bitcoins.
Transactions
• Transactions are at the core of the Bitcoin ecosystem.
• Transactions can be as simple as just sending some bitcoins to a Bitcoin
address, or can be quite complex, depending on the requirements.
• Each transaction is composed of at least one input and output.
• Inputs can be thought of as coins being spent that have been created in a
previous transaction, and outputs as coins being created.
• If a transaction is minting new coins, then there is no input, and therefore
no signature is needed.
• If a transaction should send coins to some other user (a Bitcoin address),
then it needs to be signed by the sender with their private key.
• In this case, a reference is also required to the previous transaction to show
the origin of the coins.
• Coins are unspent transaction outputs represented in Satoshis.
• Transactions are not encrypted and are publicly visible on the blockchain.
Blocks are made up of transactions
The Transaction Lifecycle
The steps of the process are as follows:
1. A user/sender sends a transaction using wallet software or some other
interface.
2. The wallet software signs the transaction using the sender's private key.
3. The transaction is broadcasted to the Bitcoin network using a flooding
algorithm.
4. Mining nodes (miners) who are listening for the transactions verify and
include this transaction in the next block to be mined. Just before the
transactions are placed in the block, they are placed in a special memory
buffer called the transaction pool.
5. Next, the mining starts, which is the process through which the blockchain
is secured and new coins are generated as a reward for the miners who spend
appropriate computational resources. Once a miner solves the PoW problem, it
broadcasts the newly mined block to the network. The nodes verify the block
and propagate the block further, and confirmations start to generate.
6. Finally, the confirmations start to appear in the receiver's wallet and after
approximately three confirmations, the transaction is considered finalized and
confirmed. However, three to six is just a recommended number; the
transaction can be considered final even after the first confirmation. The key
idea behind waiting for six confirmations is that the probability of double
spending is virtually eliminated after six confirmations. When a transaction is
created by a user and sent to the network, it ends up in a special area on each
Bitcoin software client. This special area is called the transaction pool or
memory pool.
Coinbase Transactions
• A coinbase transaction or generation transaction is always created by a miner
and is the first transaction in a block.
• It is used to create new coins. It includes a special field, also called the
coinbase, which acts as an input to the coinbase transaction.
• This transaction also allows up to 100 bytes of arbitrary data storage.
• A coinbase transaction input has the same number of fields as a usual
transaction input, but the structure contains the coinbase data size and fields
instead of the unlocking script size and fields.
• Also, it does not have a reference pointer to the previous transaction. This
structure is shown in the following table:
Transaction Validation
This verification process is performed by Bitcoin nodes. There are three main
things that nodes check when verifying a transaction:
1. That transaction inputs are previously unspent. This validation step
prevents double spending by verifying that the transaction inputs have not
already been spent by someone else.
2. That the sum of the transaction outputs is not more than the total sum of
the transaction inputs. However, both input and output sums can be the
same, or the sum of the input (total value) could be more than the total
value of the outputs. This check ensures that no new bitcoins are created
out of thin air.
3. That the digital signatures are valid, which ensures that the script is valid.
Even though transaction construction and validation are generally a secure and
sound process, some vulnerabilities exist in Bitcoin. We will now introduce
some Bitcoin's infamous shortcomings.
Mining
• Mining is a process by which new blocks are added to the blockchain.
• Blocks contain transactions that are validated via the mining process by
mining nodes on the Bitcoin network.
• Blocks, once mined and verified, are added to the blockchain, which keeps
the blockchain growing.
• This process is resource-intensive due to the requirements of PoW, where
miners compete to find a number less than the difficulty target of the
network.
• This difficulty in finding the correct value (also called sometimes the
mathematical puzzle) is there to ensure that miners have spent the required
resources before a new proposed block can be accepted.
• The miners mint new coins by solving the PoW problem, also known as the
partial hash inversion problem.
• This process consumes a high amount of resources, including computing
power and electricity.
• This process also secures the system against fraud and double-spending
attacks while adding more virtual currency to the Bitcoin ecosystem.
• Roughly one new block is created (mined) every 10 minutes to control the
frequency of generation of bitcoins. This frequency needs to be maintained
by the Bitcoin network.
• It is encoded in the Bitcoin Core client to control the "money supply."
Approximately 144 blocks, that is, 1,728 bitcoins, are generated per day.
• The number of actual coins can vary per day; however, the number of blocks
remains at an average of 144 per day.
• Bitcoin supply is also limited. In 2140, all 21 million bitcoins will be finally
created, and no new bitcoins can be created after that.
• Bitcoin miners, however, will still be able to profit from the ecosystem by
charging transaction fees.
Tasks of the miners
Once a node connects to the Bitcoin network, there are several tasks that a
Bitcoin miner performs:
1. Synching up with the network: Once a new node joins the Bitcoin
network, it downloads the blockchain by requesting historical blocks from
other nodes. This is mentioned here in the context of the Bitcoin miner;
however, this not necessarily a task that only concerns miners.
2. Transaction validation: Transactions broadcast on the network are
validated by full nodes by verifying and validating signatures and outputs.
3. Block validation: Miners and full nodes can start validating blocks
received by them by evaluating them against certain rules. This includes
the verification of each transaction in the block along with verification of
the nonce value.
4. Create a new block: Miners propose a new block by combining
transactions broadcast on the network after validating them.
5. Perform PoW: This task is the core of the mining process and this is where
miners find a valid block by solving a computational puzzle.
The block header contains a 32-bit nonce field and miners are required to
repeatedly vary the nonce until the resultant hash is less than a predetermined
target.
6. Fetch reward: Once a node solves the hash puzzle (PoW), it immediately
broadcasts the results, and other nodes verify it and accept the block.
There is a slight chance that the newly minted block will not be accepted by
other miners on the network due to a clash with another block found at roughly
the same time, but once accepted, the miner is rewarded with 12.5 bitcoins and
any associated transaction fees.
The Mining Algorithm
The mining algorithm consists of the following steps:
1. The previous block's header is retrieved from the Bitcoin network.
2. Assemble a set of transactions broadcast on the network into a block to be
proposed.
3. Compute the double hash of the previous block's header, combined with a
nonce and the newly proposed block, using the SHA256 algorithm.
4. Check if the resulting hash is lower than the current difficulty level (the
target), then PoW is solved. As a result of successful PoW, the discovered
block is broadcasted to the network and miners fetch the reward.
5. If the resultant hash is not less than the current difficulty level (target), then
repeat the process after incrementing the nonce.
As the hash rate of the Bitcoin network increased, the total amount of the 32-bit
nonce was exhausted too quickly.
In order to address this issue, the extra nonce solution was implemented, whereby the
coinbase transaction is used to provide a larger range of nonces to be searched by the
miners.
Flowchart
Figure 6.19: Mining process