Unit 2
Unit 2
WITH CRYPTOCURRENCY
Siva Kumar
Ronanki
Syllabus Topics
(ECDSA)
Key Generation
Each signer must generate a pair of keys, one private and
one public. The signer, let us call him Bob, generates the
two keys using the following steps:
With the public domain parameters and a private key in hand, Bob generates a
digital signature of 320 bytes for message m using the following steps:
Alice knows the public domain parameters and Bob’s public key. Alice is presented
with Bob’s message and digital signature and verifies the signature using the
following steps:
1ANAguGG8bikEv2fYsTBnRUmx7QUcK58wt
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 it 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 is to sends coins to some other user (a bitcoin
address), then it needs to be signed by the sender with their private
key and a reference is also required to the previous transaction in
order to show the origin of the coins.
Coins are, in fact, unspent transaction outputs represented in Satoshis.
Transactions are not encrypted and are publicly visible in the
blockchain. Blocks are made up of transactions and these can be
viewed using any online blockchain explorer.
The transaction life cycle
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 include this transaction in the next block to be mined.
5. Mining starts once a miner who solves the Proof of Work problem
broadcasts the newly mined block to the network. Proof of Work is
explained in detail later in this chapter.
6. The nodes verify the block and propagate the block further, and
confirmation starts to generate.
7. Finally, the confirmations start to appear in the receiver's wallet and
after approximately six confirmations, the transaction is considered
finalized and confirmed. However, 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.
The transaction structure
A transaction at a high level contains metadata, inputs, and outputs.
Transactions are combined to create a block. The transaction structure is
shown in the following table:
MetaData: This part of the transaction contains some values such as the
size of the transaction, the number of inputs and outputs, the hash of the
transaction, and a lock_time field. Every transaction has a prefix specifying
the version number.
Inputs: Generally, each input spends a previous output. Each output is
considered an Unspent Transaction Output (UTXO) until an input consumes it.
Outputs: Outputs have only two fields, and they contain instructions for the
sending of bitcoins. The first field contains the amount of Satoshis, whereas
the second field is a locking script that contains the conditions that need to
be met in order for the output to be spent. More information on transaction
spending using locking and unlocking scripts and producing outputs is
discussed later in this section.
Verification: Verification is performed using bitcoin's scripting language.
Types of transaction
There are various scripts available in bitcoin to handle the value transfer from
the source to the destination. These scripts range from very simple to quite
complex depending upon the requirements of the transaction. Standard
transaction types are discussed here. Standard transactions are evaluated
using IsStandard() and IsStandardTx() tests and only standard transactions
that pass the test are generally allowed to be mined or broadcasted on the
bitcoin network. However, nonstandard transactions are valid and allowed on
the network.
Pay to Public Key Hash (P2PKH): P2PKH is the most commonly used
transaction type and is used to send transactions to the bitcoin addresses.
In a simplified manner, "Pay to Public Key Hash" (P2PKH) is a method used
in Bitcoin transactions to send bitcoins to someone. Instead of directly using
someone's public key, which is a long string of numbers and letters, P2PKH
uses a shorter and more secure version called a hash of the public key. This
adds a layer of protection and privacy to the transaction.
Pay to Script Hash (P2SH): "Pay to Script Hash" (P2SH) is another method
used in Bitcoin transactions to send bitcoins. It allows more advanced and
flexible transaction conditions by using a script (a set of instructions) instead
of a simple public key or public key hash. P2SH is commonly used for
features like multi-signature transactions, time-locked transactions, and
other smart contract capabilities.
MultiSig (Pay to MultiSig):"Pay to Multi-Sig" (P2MS) is a feature in Bitcoin
transactions that allows funds to be sent to an address requiring multiple
digital signatures to spend the bitcoins. It is commonly used to enhance
security and create shared wallets that require approval from multiple
parties before any transaction is executed.
Null data/OP_RETURN: In Bitcoin transactions, the "Null data" or
"OP_RETURN" is an opcode that allows users to embed small pieces of data
directly into the blockchain without transferring any bitcoins. It serves
various purposes, such as storing metadata, timestamps, or other non-
financial information on the Bitcoin blockchain.
UTXO
Unspent Transaction Output (UTXO) is an unspent transaction output that can
be spent as an input to a new transaction. Other concepts related to transactions
in bitcoin are described below.
Transaction fee :Transaction fees are charged by the miners. The fee charged is
dependent upon the size of the transaction. Transaction fees are calculated by
subtracting the sum of the inputs and the sum of the outputs.
The fees are used as an incentive for miners to encourage them to include a user
transaction in the block the miners are creating.
All transactions end up in the memory pool, from where miners pick up
transactions based on their priority to include them in the proposed block.
The calculation of priority is introduced later in this chapter; however, from a
transaction fee point of view, a transaction with a higher fee will be picked up
sooner by the miners.
There are different rules based on which fee is calculated for various types of
actions, such as sending transactions, inclusion in blocks, and relaying by
nodes(They perform duplication checks, validate signatures and other steps, and
than transmit only valid messages). Fees are not fixed by the Bitcoin protocol and
are not mandatory; even a transaction with no fee will be processed in due course
but may take a very long time.
Contracts :As defined in the bitcoin core developer guide, contracts are
basically transactions that use the bitcoin system to enforce a financial
agreement.(lightning layer)
This is a simple definition but has far-reaching consequences as it allows
users to design complex contracts that can be used in many real-world
scenarios.
Contracts allow the development of a completely decentralized,
independent, and reduced risk platform. Various contracts, such as escrow,
arbitration, and micropayment channels, can be built using the bitcoin
scripting language.
The current implementation of a script is very limited, but various types of
contracts are still possible to develop. For example, the release of funds
only if multiple parties sign the transaction or perhaps the release of funds
only after a certain time has elapsed. Both of these scenarios can be
realized using multiSig and transaction lock time options.
Transaction malleability :Transaction malleability in bitcoin was
introduced due to a bug in the bitcoin implementation. Due to this bug, it
becomes possible for an adversary to change the Transaction ID of a
transaction, thus resulting in a scenario where it would appear that a
certain transaction has not been executed. This can allow scenarios where
double deposits or withdrawals can occur. In other words, this bug allows
the changing of the unique ID of a bitcoin transaction before it is
confirmed. If the ID is changed before confirmation, it would seem that the
transaction did not happen at all, which can then allow double deposits or
withdrawal attacks.
Transaction pools :Also known as memory pools, these pools are
basically created in local memory by nodes in order to maintain a
temporary list of transactions that are not yet confirmed in a block.
Transactions are included in a block after passing verification and based on
their priority.
Transaction verification :This verification process is performed by bitcoin
nodes. The following is described in the bitcoin developer guide:
1. Check the syntax and ensure that the syntax of the transaction is
correct.
2. Verify that inputs and outputs are not empty.
3. Check whether the size in bytes is less than the maximum block size,
which is 1 MB currently.
4. The output value must be in the allowed money range (0 to 21 million
BTC).
5. All inputs must have a specified previous output, except for coinbase
transactions, which should not be relayed.
6. Verify that nLockTime must not exceed 31-bits. For a transaction to be
valid, it should not be less than 100 bytes. Also, the number of signature
operands in a standard signature should be less than or not more than 2.
7. A transaction is rejected if there is already a matching transaction in the pool or in
a block in the main branch.
8. The transaction will be rejected if the referenced output for each input exists in
any other transaction in the pool.
9. For each input, there must exist a referenced output transaction. This is searched
in the main branch and the transaction pool to find whether the output transaction is
missing for any input, and this will be considered an orphan transaction. It will be
added to the orphan transactions pool if a matching transaction is not in the pool
already.
10. For each input, if the referenced output transaction is the coinbase, it must have
at least 100 confirmations; otherwise, the transaction will be rejected.
11. For each input, if the referenced output does not exist or has been spent already,
the transaction will be rejected.
12. Using the referenced output transactions to get input values, verify that each
input value, as well as the sum, is in the allowed range of 0-21 million BTC.
13. Reject the transaction if the sum of input values is less than the sum of output
values.
14. Reject the transaction if the transaction fee would be too low to get into an empty
block
The structure of a block
The structure of a block header
The genesis block
This is the first block in the bitcoin blockchain. The genesis block was hardcoded in the
bitcoin core software. It is in the chainparams.cpp file.
https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp
Bitcoin provides protection against double spending by enforcing strict
rules on transaction verification and via mining. Blocks are added in the
blockchain only after strict rule checking and successful Proof of Work
solution. Block height is the number of blocks before a particular block in
the blockchain. The current height (at the time of writing this) of the
blockchain is 800238 blocks. Proof of Work is used to secure the blockchain.
Each block contains one or more transactions, out of which the first
transaction is a coinbase transaction.
Orphan blocks are also called detached blocks and were accepted at one
point in time by the network as valid blocks but were rejected when a
proven longer chain was created that did not include this initially accepted
block. They are not part of the main chain and can occur at times when two
miners manage to produce the blocks at the same time.
If an adversary manages to gain 51% control of the network hash rate
(computational power), then they can impose their own version of transaction
history. Forks in blockchain can occur with the introduction of changes in the
Bitcoin protocol. In case of soft fork, only previous valid blocks are no longer
acceptable, thus making soft fork backward compatible.
In case of soft fork, only miners are required to upgrade to the new client
software in order to make use of the new protocol rules. Planned upgrades do
not necessarily create forks because all users should have updated already. A
hard fork, on the other hand, invalidates previously valid blocks and requires
all users to upgrade. New transaction types are sometimes added as a soft
fork, and any changes such as block structure change or major protocol
changes results in hard fork.