Module 3 Module 5
Module 3 Module 5
DOUBLE SPENDING
Double-spending is a fundamental flaw in digital cash protocols that allows the same digital
token to be spent more than once. In blockchain, double-spending occurs when someone
alters a blockchain network to insert a special block that lets them reacquire a cryptocurrency.
Or
Double spending means the expenditure of the same digital currency twice or more to avail
the multiple services. It is a technical flaw that allows users to duplicate money.
Since digital currencies are nothing but files, a malicious user can create multiple copies
of the same currency file and can use it in multiple places.
This issue can also occur if there is an alteration in the network or copies of the
currency are only used and not the original one.
There are also double spends that allow hackers to reverse transactions so that
transaction happens two times.
By doing this, the user loses money two times one for the fake block created by the
hacker and for the original block as well.
The hacker gets incentives as well for the fake blocks that have been mined and
confirmed.
Double spending can never arise physically . It can happen in online transactions. This
mostly occurs when there is no authority to verify the transaction . It can also happen if the
user’s wallet is not secured. Suppose a user wants to avail of services from Merchant ‘A’
and Merchant ‘B’.
The user first made a digital transaction with Merchant ‘A’.
The copy of the cryptocurrency is stored on the user’s computer.
So the user uses the same cryptocurrency to pay Merchant ‘B’
Now both the merchants have the illusion that the money has been credited since the
transactions were not confirmed by the miners.
Example: Suppose a user has 1 BTC. He/She wants to avail of services from merchant A and
merchant B. The user creates multiple copies of the same BTC and stores it. The user first
sends the original BTC to Merchant A and gets the service. Simultaneously, the user sends
the copied version of 1 BTC to Merchant B. Since the second transaction was not confirmed
by other miners, the merchant accepts the bitcoin and sends the service. But the
cryptocurrency that was sent is invalid. This is the case of Double Spending.
This diagram illustrates a digital signature scheme using a symmetric key and an unkeyed
hash function.
Symmetric Key: In symmetric key cryptography, the same secret key (K) is used for
both encryption and decryption. In this diagram, the symmetric key is used to encrypt and
decrypt the message.
Unkeyed Hash: An unkeyed hash function, also known as a message digest, takes a
variable-length input message (M) and produces a fixed-length output hash value (H(M)).
The hash function is deterministic, meaning to the given same input, it always produces
the same output. Unkeyed hash functions do not use a secret key.
Encrypted: Encrypted data refers to the process of converting plaintext (readable data)
into ciphertext (unreadable data) using an encryption algorithm and a secret key.
Unencrypted: Unencrypted data, also known as plaintext, is data that is not converted
into ciphertext using encryption. It is in a readable format and can be easily understood by
anyone who has access to it.
Message Encrypted:
1. Message Preparation: The sender (Source A) prepares the message (M) to be sent.
2. Hash Calculation: The sender calculates the hash value (H(M)) of the message using
an unkeyed hash function.
3. Encryption: The sender encrypts the message and its hash value using the symmetric
key (K) and a encryption algorithm (E). The encrypted output is E(K, [M || H(M)]),
where || denotes concatenation.
4. Transmission: The encrypted message is sent to the recipient (Destination B).
5. Decryption: The recipient decrypts the received message using the same symmetric
key (K) and decryption algorithm (E).
6. Hash Verification: The recipient calculates the hash value (H(M)) of the decrypted
message and compares it with the decrypted hash value (H(M)) received from the
sender. If the two hash values match, the recipient can be sure that the message has
not been tampered with during transmission.
Message Unencrypted:
1. Message Preparation: The sender (Source A) prepares the message (M) to be
sent.
2. Hash Calculation: The sender calculates the hash value (H(M)) of the message
using an unkeyed hash function.
3. Transmission: The sender sends the message (M) and its hash value (H(M)) to
the recipient (Destination B) without encryption.
4. Hash Verification: The recipient calculates the hash value (H(M)) of the received
message and compares it with the received hash value (H(M)). If the two hash
values match, the recipient can be sure that the message has not been tampered
with during transmission.
Example:
Suppose we have a message "Hello, World!" and we apply a hash function (e.g., SHA-256)
to it. The output hash value might look like this:
H("Hello, World!") =
315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
Proof of Work
Proof of Work(PoW) is the original consensus algorithm in a blockchain network.
The algorithm is used to confirm the transaction and creates a new block to the chain.
In this algorithm, minors (a group of people) compete against each other to complete the
transaction on the network.
The process of competing against each other is called mining. As soon as miners successfully
created a valid block, he gets rewarded. The most famous application of Proof of
Work(PoW) is Bitcoin.
Producing proof of work can be a random process with low probability. In this, a lot of trial
and error is required before a valid proof of work is generated.
The main working principle of proof of work is a mathematical puzzle which can easily
prove the solution. Proof of work can be implemented in a blockchain by the Hash cash proof
of work system.
In the puzzle game, bitcoin software creates a challenge, and there is a game begins.
This game involves all miners competing against each other to solve the challenges, and this
challenge will take approximately 10 minutes to be completed.
Every single miner starts trying to find the solution to that one Nonce that will satisfy the
hash for the block.
At some specific point, one of those miners in the global community with higher speed and
great hardware specs will solve the cryptography challenge and be the winner of the game.
Now, the rest of the community will start verifying that block which is mined by the winner.
If the nonce is correct, it will end up with the new block that will be added to the blockchain.
Merkel Tree
The Merkle tree is a data structure used to encrypt transactions efficiently and securely.
Its made up of the hashes of various data blocks of transactions.
The structure of a Merkle Tree is a hierarchical way to represent hashes of the
transactions.
Merkle Root (or Root Hash): The root of the Merkle tree is known as the Merkle
root and this Merkle root is stored in the header of the block.
Non-Leaf Nodes: The non-leaf nodes contain the hash value of their respective
children. These are also called intermediate nodes because they contain the
intermediate hash values and the hash process continues till the root of the tree.
Leaf Nodes: The leaf nodes contain the hash values of transaction data. Each
transaction in the block has its data hashed and then this hash value (also known as
transaction ID) is stored in leaf nodes.
How does Merkle Tree work?
Merkle tree uses the concept of cryptographic hash functions and public-key cryptography to
encrypt and verify the transactions among all the nodes in the network.
Merkle tree is made up of hashing pairs of nodes repeatedly hashed until only one hash
remains called the Root Hash or Merkle Root. It is built from the bottom using transaction
IDs which are hashes of the individual transactions. It uses cryptographic hash functions to
create digest or hash of the transactions.
In the Merkle tree structure, each non-leaf node is a hash of its previous hash. And every leaf
node is the hash of transactional data of each block.
Example
The above example is Binary Merkle Tree.
There are four transactions in a block- T1,T2,T3,T4. there is a top hash which is the hash of
the entire tree, known as the Root Hash, or the Merkle Root. Each of these is repeatedly
hashed, and stored in each leaf node, resulting in Hash 1,2,3,4.
Consecutive pairs of leaf nodes are the in a parent node by hashing Hash1 and Hash2,
resulting in Hash12, and separately hashing Hash3 and Hash4, resulting in Hash34.
The two hashes (Hash12 and Hash34) are then hashed again to produce the Root Hash or
the Merkle Root.
Merkle Root is stored in the block header. The block header is the part of the bitcoin block
which gets hash in the process of mining. It contains the hash of the last block, a Nonce, and
the Root Hash of all the transactions in the current block in a Merkle Tree. So having the
Merkle root in block header makes the transaction tamper-proof.
The Merkle Tree maintains the integrity of the data. If any single detail of transactions or
order of the transaction's changes, then these changes reflected in the hash of that
transaction.
o It helps in saving the memory or disk space as the proofs, computationally easy and
fast.
Bitcoin Privacy
Wallet privacy
Imagine this scenario. The user is in a public place, and they need to make a payment using
their bitcoin wallet. They open the wallet on their phone, but they don’t feel comfortable
having their payment and balance information clearly visible to strangers who may be
looking over their shoulder, persons lurking, or video surveillance. Hence, by giving users the
ability to hide sensitive information in their wallet, but only when desired, they gain an added
sense of physical privacy and security when using the app in public
The button to quickly hide sensitive information is displayed within close reach of the
balance itself. One tap hides it, and the next tap reveals it again.
This is an easy and convenient way to switch between revealed and hidden states. On the
downside, it makes it relatively easy for anyone else to reveal user information if they have
access to the device.
Unhiding the balance requires tapping and holding for a few seconds to prevent accidental
reveals.
An alternative design approach is to make the show/hide toggle only available in settings.
Having it right on the main screen makes things quite obvious for someone who has access to
a user’s device. If it is only available from settings, a third party who has access to the device
may not immediately know how to reveal information. The downside is that a user cannot
quickly hide their information if the need arises.
Another solution is to invoke the wallet’s hidden state as a default when the app is opened to
protect against prying eyes during initial display. The pre-hidden state can be unveiled after a
tap, PIN entry, or perhaps a short 5-second timer.
This gives users some time to assess their environment before their info is displayed but
could leave them frustrated, having to wait for their information to be revealed, especially in
an urgent situation.
Other privacy aspects #
On-chain address re-use is a common privacy problem due to the traceability across
transactions. Addresses this wallet uses are swap addresses generated by a lightning service
provider. This lifts the responsibility for address management out of the hands of the user.
More on the requesting page.
Just like any other application, privacy can be impacted by the use of third-party services
(analytics, currency exchange rates, etc.) and data transfer itself, as well as any services
provided by the application developer.
Digital Signatures: To prove that you are the owner of the bitcoin you want to send, the
transaction must be signed using your private key through a cryptographic process. This is
known as a digital signature. It's essential to keep your private key secret because it's like
your digital password.
1. Ensuring signatures are valid: This step verifies that the digital signatures provided
for the inputs of the transaction are valid and correctly signed by the private keys
corresponding to the public addresses that hold the Bitcoins being spent. This ensures
that the transaction is authorized by the rightful owner.
2. Determining authenticity: This involves checking that the transaction adheres to the
Bitcoin protocol rules and format. It ensures that the transaction has not been
tampered with and is genuine.
3. Confirming the accuracy of a transaction: This includes checking that all inputs
and outputs are correctly referenced and balanced, meaning the sum of the input
values must equal the sum of the output values plus any fees.
4. Checking available balances and transaction fee payments: This step involves
verifying that the addresses involved have sufficient balances to cover the transaction
amounts and any associated fees. It also ensures that the fees meet the network's
requirements, which can affect the transaction's inclusion speed in the blockchain.
5. Preventing double-spending: This step ensures that the same Bitcoins are not being
spent more than once. It involves checking that the inputs for the transaction have not
already been used in another transaction that has been confirmed in the blockchain.
Here's a detailed breakdown of the validation process:
1. Transaction Format Check: Ensure the transaction adheres to the correct format and
protocol rules.
2. Signature Verification:
o Verify that each input's signature is valid.
o Ensure the signatures match the public keys and that the transaction is
authorized by the correct private keys.
3. Double-Spending Check:
o Ensure the inputs of the transaction have not already been spent in another
confirmed transaction.
o Check the mempool (a pool of unconfirmed transactions) to ensure the inputs
are not being used in another unconfirmed transaction.
4. Input and Output Verification:
o Check that the referenced inputs (UTXOs) exist and are unspent.
o Confirm that the input values are greater than or equal to the output values
plus fees.
5. Balance and Fee Check:
o Verify that the sum of the input values covers the output values and any
required transaction fees.
o Ensure the transaction fee is adequate for the transaction to be processed by
miners.
6. Block Inclusion Check (once included in a block):
o Ensure the transaction is included in a valid block.
o Verify the block's validity, including proof of work, block size, and
timestamp.
7. Consensus Rules Check:
o Ensure the transaction adheres to all consensus rules, such as script validation,
size limits, and any network-specific rules.
Bitcoin transaction means sending bitcoin from one person to the other in the secured
blockchain network. These are messages that are digitally signed using cryptography and
are verified by the miners that are present in the blockchain network.
Here is the interesting part and the topic that we have been waiting to address. From the
above-given steps, step 5, a conflict can arise where multiple miners create blocks at the
same time and try to add them to the last valid block of the blockchain. In this case which
block will be appended to the blockchain?
Consider Steve, Jake and Iris are miners in the blockchain and they simultaneously create
their respective blocks which are known as candidate blocks (portrayed in green, yellow,
and blue respectively). Out of these candidate blocks, a decision has to be made to choose
which of these blocks should be put in the chain.
A conflict arises when Steve, Jake, and Iris mine their block at the same time
This conflict is resolved using the longest chain rule which is adopted by every node in the
network to achieve consensus on the valid structure of the blockchain. To add a node in the
blockchain, a miner in the network needs to have a computer with high processing power.
In this case, let us say Jake’s computer has more processing power as compared to the
computers of Steve and Iris. So, Jake can create a block faster than Steve and Iris.
Meanwhile, other miners are trying to create other valid blocks, so these blocks are created
on the block which has occurred first, and since Jake’s block was created faster than
Steve’s and Iris’s, it occurs first in the chain and the miners add their blocks (given below
in red ) on top of Jake’s blog (given below in yellow).
It can be seen from the above diagram that the unit with Jake’s block is the longest unit and
according to the longest chain rule , the unit with the longest length must be accepted as the
valid version of the blockchain. Therefore, the updated valid version of the blockchain
looks like the one below:
The updated version of the blockchain is distributed in the entire P2P network
CREATION OF BLOCKS
Creation of Blocks
To create a new block, miners must go through a process to solve a math problem.
When finding a valid solution for the network, a new block can be taken for
granted that will be added to the blockchain by consensus.
And for which, the miner who found the solution, will receive a reward for the
new block. This reward is known as the block reward.
A new Bitcoin block is generated approximately every 10 minutes.
The process of mining a new block starts when a user wants to send a certain amount of
cryptocurrency to another person. So send the transaction with the data from your wallet
and wait for the network to confirm the transaction. They remain there until a block is mined
where they can be included and validated.
Each miner will select the transactions they want to include and build their own block. If
there are transactions already validated and included in the previous block, they will be
removed from this one. This new block is known as a candidate, since it is not yet valid
because it does not have a valid proof of work.
In the formation of this new block, a header must be included that contains the hash from the
previous block, the merkle root and data for mining competition.
Once each miner has formed their own transaction block, they will need to find a valid
signature for that block. In other words, carry out a proof of work. Each miner must carry
out a mathematical calculation process that is unique to each block they formed. So, although
the procedure is the same, the result will be totally different for each one. This complex
calculation involves a lot of computational power, and therefore, a large expenditure of
electrical energy. Which will also depend on the system difficulty for the time of mining.
The solution miners must find is known as hash. This function is very difficult to find, but
once found, it is easy to verify by others.
To find a valid output hash, the miners perform the mathematical calculations repetitively
over and over again. Which is a random number that they use and constantly change until
they find an output signature or hash that is valid based on the condition. There is no way to
predict which nonce will solve the problem, so they must use as many as necessary.
In the case of the Bitcoin network, the system determines that the output hash must contain a
certain number of zeros at the beginning of the hash.
When a mining node manages to find a valid output hash for a block, it transmits that block
together with the signature to the other nodes in the network so that they can validate it.
the miner receives the reward established for mining, putting new bitcoins into circulation.
This is registered on its own node, the other nodes on the network will do so in the next step.
In addition, regardless of whether all the bitcoins have been issued or not, the miner also
receives all the mining commissions that users have put in the transactions that make up this
block.
Here also the proof of work is confirmed, that is, the computational power spent to find the
solution, and it is noted that the miner who discovered the block can effectively make use of
the recently received bitcoins.
Once the new block is added to the blockchain, all the others that are added on top of it will
count as a confirmation.
Once a block is generated, all mining nodes must start the process by forming a new block of
transactions. They cannot continue mining the previous block because each block must add
the output hash of the block that precedes it.
Unit-5
Ethereum
Ethereum is a Blockchain network that introduced a built-in Turing-complete
programming language that can be used for creating various decentralized
applications(also called Dapps).
The Ethereum network is fueled by its own cryptocurrency called ‘ether’.
The Ethereum network is currently famous for allowing the implementation of smart
contracts.
Smart contracts can be thought of as ‘cryptographic bank lockers’ which contain
certain values. These cryptographic lockers can only be unlocked when certain
conditions are met.
Ethereum is often called Blockchain 2.0 since it proved the potential of beyond
blockchain technology.
The consensus mechanism used in Ethereum is Proof of Stakes(PoS), which is more
energy efficient when compared to that used in the Bitcoin network, that is, Proof of
Work(PoW).
PoS depends on the amount of stake a node holds.
How Does Ethereum Work?
Ethereum implements an execution environment called Ethereum Virtual Machine
(EVM)
When a transaction triggers a smart contract, all the nodes of the network will execute
every instruction.
All the nodes will run the EVM as part of the block verification, where the nodes will
go through the transactions listed in the block and runs the code as triggered by the
transaction in the EVM.
All the nodes on the network must perform the same calculations for keeping their
ledgers in sync.
Every transaction must include:
o Gas limit.
o Transaction Fee that the sender is willing to pay for the transaction.
If the total amount of gas needed to process the transaction is less than or equal to the
gas limit then the transaction will be processed and if the total amount of the gas
needed is more than the gas limit then the transaction will not be processed the fees
are still lost.
Thus it is safe to send transactions with the gas limit above the estimate to increase
the chances of getting it processed.
Smart Contract Smart contracts on Bitcoin are Smart contracts on Ethereum are
Programming written in programming written in programming languages like
Language languages like Script, Clarity. Solidity, Vyper, etc.
Miner got nearly 6.25 BTC on Miner got nearly 5 BTC along with same
successfully adding new block in additional rewards on successfully
Rewards network. adding new block in network.
Extra notes