Venky Book
Venky Book
Blockchain Technology:
Cryptocurrency and other
Applications
by
Sandeep Kumar Shukla
Mohan Dhawan
Venkatesan Subramanian
February 1, 2021
2
Contents
1 Introduction 5
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.1 History . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1.2 Components and Entities . . . . . . . . . . . . . . . . . 7
1.1.3 Blockchain Types . . . . . . . . . . . . . . . . . . . . . 10
1.1.4 Blockchain Features . . . . . . . . . . . . . . . . . . . . 11
1.2 Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.2.1 Symmetric Key Cryptography . . . . . . . . . . . . . . 14
1.2.2 Asymmetric Key Cryptography . . . . . . . . . . . . . 18
1.2.3 Hash Function . . . . . . . . . . . . . . . . . . . . . . . 21
1.2.4 Digital Signature . . . . . . . . . . . . . . . . . . . . . 23
1.2.4.1 Elliptic Curve . . . . . . . . . . . . . . . . . . 25
1.2.4.2 Elliptic Curve Digital Signature Algorithm
(ECDSA) . . . . . . . . . . . . . . . . . . . . 26
1.2.5 Memory Hard Algorithm . . . . . . . . . . . . . . . . . 28
1.2.5.1 ROMix . . . . . . . . . . . . . . . . . . . . . 28
1.2.5.2 HashCash . . . . . . . . . . . . . . . . . . . . 29
1.2.5.3 ASIC Resistance . . . . . . . . . . . . . . . . 30
1.2.5.4 New Identity . . . . . . . . . . . . . . . . . . 30
1.2.6 Proof of Work . . . . . . . . . . . . . . . . . . . . . . . 31
1.3 Distributed System . . . . . . . . . . . . . . . . . . . . . . . . 33
1.3.1 Decentralization . . . . . . . . . . . . . . . . . . . . . . 35
1.3.2 Consensus . . . . . . . . . . . . . . . . . . . . . . . . . 38
1.3.3 Byzantine Generals Problem . . . . . . . . . . . . . . . 38
1.3.4 Faults . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
1.3.5 RAFT . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
1.3.5.1 Leader Election . . . . . . . . . . . . . . . . . 41
3
4 CONTENTS
Introduction
• The underlying concepts that are used for the implementation of blockchain.
1.1 Overview
The blockchain is a distributed data structure, which is replicated at various
nodes or computers. Like linked list, it is a set of blocks which are connected
to each other by a link. In case of a linked list, nodes are connected by
pointers and pointers are basically memory addresses. The blockchain has a
different notion of linking between nodes and each of these nodes are called
blocks. Hence, a blockchain is a series of blocks and each block is connected
to its previous block by a link basically block hash, which is a cryptographic
technique.
5
6 CHAPTER 1. INTRODUCTION
that there if you have a very high computational power, which is almost
impossible for individuals together.
In case you can gather that kind of computation power you can actually
subvert this all the protection and change but since it is virtually impossi-
ble, we would say that this is a tamper resistant log. Hence, having these
properties, we basically use blockchain as a platform to create and transact
cryptocurrency. The bitcoin and ethereum are popular cryptocurrencies.
The first application of blockchain was bitcoin. The whole idea of creating
currency, that whose transactions whose creation whose use everything has to
be put in a tamper proof log, and without a trusted third party or without
a central agency, which keeps track of this logs. After the popularity of
Bitcoin, blockchain is considered as one of the integrity tools for distributed
applications and applied in various applications such as healthcare, Internet
of Things, Supply Chain Management, etc.
1.1.1 History
In 1982, David Chaum first proposed a blockchain-like protocol in his dis-
sertation ”Computer Systems Established, Maintained, and Trusted by Mu-
tually Suspicious Groups” [15]. Further Stuart Haber and W.Scott Stor-
netta [16] in 1991 introduced the concept of linking digital timestamp of
documents to make infeasible for the user in sending the document back
dated or with later date. Later, along with Dave Bayer, Stuart Haber and
W.Scott Stornetta [6] used the merkle tree to make the model efficient. In
2008, Satoshi Nakamoto improved the concept with Proof of Work (PoW),
linking blocks without the requirement of trusted third party and called as
block chain backbone of Bitcoin (later become blockchain). In 2015, an
Ethereum blockchain featuring smart contract functionality was introduced.
In January 2018, Hyperledger released the production-ready Sawtooth 1.0
permissioned blockchain and in January of 2019, the first long-term-support
version of Hyperledger Fabric (v1.4) was announced. Since then there are
various blockchain implementations are released.
etc. The data are transactions, account balances, transaction receipt, etc.
The attributes of blockchain header and data varies according to implementa-
tions. The different components and entities of the blockchain are discussed
in the following.
Block: Blocks are files where data pertaining to the application is stored.
Data in a block are some or recent transactions made in the network and
meta-data such as parent block hash, timestamp, nonce, etc. Each block is
connected to the previous block through the hash pointer.
Genesis Block: The first block of the blockchain is called as the genesis
block which does not have any previous or parent block. All users of the
respective network should start with the same genesis block to ensure the
correctness of the blockchain. For example, let us assume Alice and Bob
wish to have together a private network but both are having different genesis
block. Now, Alice generate the second block and send to Bob to commit in
the chain. When Bob try to link the received block with his genesis block, it
will give incorrect hash because the parent hash in the received second block
and the hash of the Bob genesis block are different. In such case they cannot
have a common blockchain. In case both have the same genesis block then
there will not be hash conflict also they can have the common blockchain
application.
• In a situation that when two or more blocks have the same block height.
For example, in figure 1.2, there are two blocks for block number 2,
three blocks for block number 3, etc. However, only one block of the
respective number can be part of the main chain. Other blocks are
called as the forks.
transactions and solve the puzzle for distributed consensus. The mined block
will be broadcasted to everyone in the network for inclusion in the blockchain.
The consensus protocol varies to one implementation to another implemen-
tation.
Nodes: Nodes are the participants of the blockchain network. The par-
ticipating nodes can be full node or light node. A node which stores blocks
header, data and mine blocks is called as full node. A node, which stores
only the header of the blocks is called as light node. The third type of node
is called as bootstrap node, it is highly available node, node address is hard-
coded in the software and it helps other nodes to connect to the network.
Mostly these nodes are the blockchain application developer nodes.
• Transparency: The data and the process of updating the state is visible
to the public. However, the amount of information that is transparent
to an observer, can differ according to the application requirement.
1.2 Cryptography
Cryptography is a method to enable two or more users to communicate over
an insecure channel in such a way that an opponent cannot understand what
is being communicated. The terms that you hear a lot when we talk about
cryptography are plaintext, or cleartext. The message that you want to
encrypt is a plaintext, because if you read it, you get the meaning of it.
The second thing is encryption or encipher. That is an action you apply
12 CHAPTER 1. INTRODUCTION
on the cleartext or the plaintext and you encode it in a form so that only
people who have a very specific knowledge can actually recover the original
plaintext or original message, others will see garbage. It is a very old idea
we have, we know that Julius Caesar used Caesar cipher. So this is not
something that is very modern. But in modern day, the technology, and the
algorithms for encryption or enciphering has changed drastically from the old
days. Then after the encryption, what we get, we call them ciphertext. It is
basically the encrypted message. And in order to read the message, you have
to do a decryption or decipher. And this basically converts the ciphertext to
plaintext.
Now, if you look at this as some kind of a data flow as in figure 1.3,
then M is your message or plaintext here. And this plaintext goes into an
encryption algorithm or here it is shown as an encryption algorithm box. It
could be hardware or software and then what comes out is the ciphertext
C. The ciphertext is the encrypted version of the original message. And
then you want to read it, then you have to pass it through the decryption
or deciphering algorithm or the hardware and then comes out the cleartext
or the original message. So the following identity must hold. If D applied
to C is M , and M is C applied to E. Then M must be recoverable by a
composition of D and E. That means, if you apply E first on the message,
you get the ciphertext and then on the ciphertext, if you apply D, then you
should get back the plaintext. That is the idea, that is the property of all
encryption systems.
understand it, it is secret to them, and therefore, they cannot do it, unless
somehow they figure out what the algorithm is. And this is what happened
in the 1940s when the Germans used this Enigma code to instruct their naval
ships and U-boats. And this messages were going encrypted.
And then the Britishers, which was a team led by Alan Turing, actu-
ally constructed a method and device that would reverse the process. And
without knowing the algorithm that the Germans were using, they had to
actually come up with the algorithm that is being used. And then they had
to figure out how to reverse it. And that is how they actually encrypted the
Enigma code. But nowadays, we do not keep the algorithm secret, because
keeping an algorithm secret is quite difficult. So we have to use some other
method to keep the secrecy and that is where the idea of a key comes in. A
cryptography key is basically the secret that is an ingredient into the encryp-
tion process. And the key is also an ingredient in the decryption process.
The encryption key and the decryption key, may be same or different.
Based on whether they are same or they are different we have two types of
encryption or decryption method, which are called the symmetric encryption
or asymmetric encryption.
The figure 1.4 contains the second input that is K but it is missing in
the figure 1.3. Now we have the encryption in encryption box which takes
a message M and a key, in this case K and then outcomes, the ciphertext.
When you put that ciphertext C to the decryption algorithm, then you need
another key or may be the same key depending on whether it is a symmetric
case or asymmetric case and then you get the M . This is called as secret key
cryptography. The secret key cryptography means that the key has to be
known and kept completely secret from outsiders. It means only the person
encrypting it should know the key and the person who is supposed to decrypt
it should know the key.
14 CHAPTER 1. INTRODUCTION
The symmetric key algorithm is also called single key algorithm because
you have only one key shared between the receiver and sender. And obviously,
two keys are the same. And most of the time this encryption algorithms are
also pretty simple. They are simple, but they are tedious. So you take the
information that you want to encrypt.
And with the key using the key, you decide on certain substitutions and
the part of the message or permutations of the bits or symbols of the message
and you do this for many, many rounds. And how you do it depends on the
key, the individual bits of the key. And therefore, when you want to decrypt
it, you have to reverse the permutations and reverse the substitutions in
order to get this message back.
And since you have the key, you know what the substitution and per-
mutation sequences were, and by how much the permutations happened, by
how much the substitutions happened. So similarly, you will do the opposite
in the other side. One good thing about this is substitution and permutation
are pretty easy to do in hardware.
Because you are basically taking bits and pushing them through shufflers
and parameters and so on. And you do this many, many times. They are
called rounds. And therefore, the secret key cryptography has the advantage
of being very efficient and fast when you have a hardware support for doing
cryptography. So there are chips that will do this for you in a very, very fast
turnaround time. So now how you encrypt with the help the key is based
on whether you are doing it continuously as you see bits of the message and
you do something with the bits or you divide the message into blocks, chop
1.2. CRYPTOGRAPHY 15
the message into multiple smaller messages, and then you apply the key as a
whole on each of this chopped portions. So those are the depending on how
you do it, whether you want to do it on the stream of bits or on the blocks
of bits.
These are called block ciphers and stream ciphers and block ciphers usu-
ally divide the message into chunks of like 64 or 128 or 256 or 512 bit blocks.
So as mentioned that the symmetric cryptography is very simple and very,
very fast.
Examples of symmetric key cryptography are DES, like Data Encryption
Standard RC4, IDEA and Blowfish, and most common nowadays is the AES,
right. So so AES is the current most common standard and there are variants
of AES, which are also used.
The properties of secret key or symmetric key algorithms are as follows.
• First of all, it assures confidentiality. So which means that if you have
already exchanged the key, and nobody else has the key, then when
you send the ciphertext on the way, if somebody reads your ciphertext
by tapping on to your network, they would not be able to figure out
what what is being sent because unless they have the key they cannot
decipher. So the symmetric key cryptography basically achieves that
privacy or confidentiality of information, provided the key is kept secret.
• Also if the key is kept secret, then authentication is ensured. That is, if
you receive a message from somebody, you want to know that it is that
person who suppose to send you the message is indeed sending you the
message. And somebody is not pretending to be him or her and sending
you the message. Now if you have done the key exchange beforehand
with the authentic person, then when you receive the message and you
apply your key for decryption, and you could and you can decrypt, then
that means the anybody who has sent this message must have the key.
Now assuming that the key has not been stolen from the sender who
you actually exchange the key with, then the message has to be coming
from an authentic source. So authenticity is also proven, provided the
key is kept really secret between the two parties.
• Message integrity means that the message or the data has not been tam-
pered with, nobody has changed the data while the data was in transit.
So that is also assured because if somebody replace the message, and
then he has to also encrypt the message with the key. Otherwise, you
16 CHAPTER 1. INTRODUCTION
would not be able to decipher it. When you try to decipher it with a
key and it was actually encrypted with a different key then you will
get back garbage. So therefore if you can decrypt the message, then
it is actually showing that the message could not have been tampered
with. Hence the message integrity is also proven if you can decipher
the message.
• Non-repudiation means that the sender cannot later say, oh no, I did
not send this message. Because if the sender has used the key, and he
is the one who knows the key and nobody else, then he has to have
sent it. Otherwise, there are two possibilities. One, the key is stolen or
he is lying.
Assuming the key is not stolen, then it is a non-repudiable. That is
that the sender cannot admit to have sent the message, he cannot deny.
So now let us look at non repudiation a little more closely by playing
out a scenario. So let us say and in cryptography literature, you will
always see these names Alice, Bob, Eve. So usually Alice and Bob
are the sender receivers, and Eve is an eavesdropper. So who actually
tries to capture the message while the message is on transit and tries
to decipher. So Eve is usually the attacker, or threat. Let us see two
scenarios.
In scenario 1, suppose Alice sends a stock buy request to Bob, and Bob
does not buy and claims to have never received the message. So at the
end, Alice would suffer some loss because the stock might have gone
up in price. And then Alice says to Bob that I was supposed to have
bought this when the price was low, and I sent you message but you
did not buy it. So I have suffered loss. Bob can say that no, no, I never
got the message so I never bought that stock.So he can deny to have
received the message.
In scenario 2, suppose Alice sends a stock buy request to Bob. And
Bob sends back an acknowledgement that yeah, I am going to do it, I
received your request. And then again Bob fails to buy it. And then
later when Alice comes and says I have suffered loss for you, because
of you. Bob can say that no, no, I did not get the message. Then
Alice shows him that look, you sent me an acknowledgement message.
And therefore, you must have received the message. Now in case they
are not using encryption, or any kind of method of non-repudiable
1.2. CRYPTOGRAPHY 17
communication, then Bob can say that well, it looks like a message
from me. It has on its header that it is from my email address, but
somebody fabricated it. And then without the use of cryptography for
Alice, there is no way that she can prove that the acknowledgement
message was indeed from Bob. Maybe somebody, like an Eve might
have captured Alice’s request in the in the middle of the transit, and
then block the message to receive go to Bob. And she faked a message
from Bob to Alice that okay fine, I am going to buy it, but she does
not do it. So Bob is not the culprit.
So in this case, Bob has repudiability. But in case of the whole thing
was done cryptographically, then Bob could not have done repudiation
unless he claim that my key was stolen, which is another issue outside
the scope. So in a court of law, under the circumstances we described,
Alice cannot prove that the acknowledgement message was indeed from
Bob. So that is about non repudiation.
Usually one generates the key and shares with another through a secure
channel. So that means this key is shared between the encryptor and decryp-
tor. And this sharing is a big problem because how do you let the decryptor
know the secret key?
So one possibility is that you do it out of band which means that you
use some other means like a telephonic call or some other kinds of method
in which you send the key to the receiver, then you send the message and
then the receiver will use the key. But if you send the key with the message,
then anybody who is listening on the communication channel will read the
ciphertext, but then also read the key.
And therefore, they will use the key to decipher because algorithms are
not secret, only the keys are secret. So therefore, there has to be a way
in which the key is shared between the two parties. And that is one of
the problems that the symmetric key cryptography suffers from. And since
before you share a key, you cannot encrypt the key because then there is a
problem that when you have the key, then you decipher it.
But if you do not have the key, then there has to be a way to share it.
And most of the time, this has to done out of band. And which means that
there has to be some other way of communicating between the sender and
the receiver. So disadvantage, is that how do you exchange the key between
the sender and the receiver. And if you want to keep the key in some kind
of a, you know database or something, there is a possibility of the key being
18 CHAPTER 1. INTRODUCTION
required message but only the owner of the key can decrypt ciphered message
using the private key known only to him. There are different asymmetric
key algorithms available such as RSA, Elliptic Curve, Diffie Hellman Key
exchange, etc. Asymmetric key cryptography is mostly for symmetric key
exchange purpose.
message or decipher the message is one possessing the private key. Therefore,
it has to be only Bob who can read it. So that is the idea of public key
cryptography. Now the question is, how is it used to establish a shared
secret, because public key cryptography is usually based on a very complex
mathematical problem compared to secret key cryptography or symmetric
key cryptography where bit shuffling and bit permutation, this kind of stuff
is used. So it is very fast to implement. Whereas in this case, normally what
we do is we try to solve a very complex mathematical problem, rather than a
bit shuffling kind of thing, simple things. Therefore, it is not very fast. So we
have to eventually use a symmetric key cryptography. But to establish the
secret key between the two parties, we need to use public key cryptography.
So that is what public key cryptography is normally used for.
So I am sending a message over the Internet to my friend. Here in the
internet, there are lurking behind many of the computers, there are people
who can capture my message. And then if they know the key, then they will
decipher it. So I have to somehow make the system such that the symmetric
key that I am going to use to communicate very large amount of messaging
or file uploads and all that I will not let anybody else know the key. So public
key cryptography helps me by the following.
If you use public key cryptography, then you never send the secret key
over the network. So what you do is you establish the secret key by using
public key cryptography without having to ever sent the key on the network.
So everybody creates the key on their own computer. And because of the
mathematical process that is followed, the key this party establishes and the
key the other party establishes becomes the same. So that becomes a shared
secret without having gone through the network. So the problem statement
here would be that Alice has a channel for communicating with Bob and they
want to use this channel to establish a shared secret. However, Eve is able
to learn anything that is sent over the channel. So if Alice and Bob has no
other channel to use, how can they establish a shared secret?
As discussed, public key cryptography is hard mathematical problem. So
for example, RSA is dependent on the fact that if I give you a very large
integer N and the N can be factored into two prime numbers, p and q,
finding those p and q to factor N is a very hard problem. It is called the
integer factorization problem and figuring this problem is not very fast and
if N is a very, very large number like 2048 bits. It is a very large number,
let us say. And then you have to know what are the two prime numbers
that produced this number by being multiplied, it is a hard problem. And
1.2. CRYPTOGRAPHY 21
this kind of hard problems are used for public key cryptography. We can
call this as one way function because multiplying p and q to produce N is
easy. And then if you do not tell people p and q, and just give the N for
people to know the p and q is hard. So reversing the process is difficult. So
public key cryptography as you know that the person who wants to send you
the message, you basically send it encrypted with the public key. And then,
when you decipher the message, you do it with the private key.
f {M } → H
M 6← f {H}
f {M1 } → H1
f {M2 } 9 H1
22 CHAPTER 1. INTRODUCTION
In any hash function, collision do exist because the output set is limited when
the input set is high. For example, in figure 1.7, it shows that the input set is
mapped to the output set. The size of input set is higher than the output set.
Hence, there will always be a possibility that collision will occur. According
to Birthday paradox, if the output set size is n with possible 0, 1 combination
n
then there is 50% chance that in 2 2 input, collision will occur. However, it is
computationally hard to achieve the collision because values in output space
has the equal probability of occurrence.
(
V alid If h(M ) == H
V (M, H) = (1.2)
Invalid otherwise
Figure 1.8 and 1.9 shows elliptic curve with point addition and doubling
respectively. The process of computing addition and doubling are shown in
algorithm 1 and 2. The variables P1 = (x1 , y1 ) and P2 = (x2 , y2 ) are points
on the curve E with the condition that P1 , P2 6= ∞. The variable m is to
define the slope of the line on the curve.
x3 = m2 − x1 − x2
y3 = m(x1 − x3 ) − y1
end if
if x1 = x2 AND y1 6= y2 then
P1 + P2 = ∞
end if
k is private key. The size of the elliptic curve determines the difficulty of the
problem.
1.2.5.1 ROMix
It is a memory hard algorithm with a hash function, input and other param-
eter. First part of ROMix shown in algorithm 1.2.5.1 that takes an input
value X, generates the hash of it repeatedly for N iteration and stores in
an array variable Vi . With input X and hash output (V0 , V1 , V2 , ..VN −1 ), the
next part of the ROMix shown in algorithm 6 randomly access the array
values V to ensure that they are all stored in Random Access Memory. The
input value X is given to a bijection function integrify. The random output
j of the integrify function is index to locate the value Vj . The hash value
of the input (X) along with Vj will be computed and assigned to X. This
process will go for N iterations and final mix output is B.
This function ensures that the required memory S need to be allotted so
that the mix generation will be fast that is the output can be computed in
time T . In case the required memory is not allotted or less memory S 0 is
1.2. CRYPTOGRAPHY 29
allotted (S 0 < S) and the hash values produced by algorithm are not stored
then output can be computed in time T 0 where T 0 >> T .
1.2.5.2 HashCash
This is the first Proof of Work (PoW) system to control the email spam and it
was originaly proposed by Cynthia Dwork and Moni Naor in 1992 [8]. Later
it was named by Adam Back [5]. Hashcash is a non-interactive, publicly
auditable and trapdoor-free cost function with unbounded probalistic cost.
k number of times but not able to get the head. The throws k does
not have any bound that is it can go upto ∞. The bounded one has
the limit on which there is an upper bound for example choosing a key
from key space.
In Hashcash, the problem is finding the partial hash collision that is the
left k bits out of full length (l) should be 0 ({0}k {0, 1}l−k ). This can be
achieved only through the brute force. The hashcash is used in bitcoin a the
mining function.
in under the various identities. And therefore people would not be able to
know that Alice is the person who is doing all this.
Therefore, anybody can make any new identity at any time and make
as many as they want. And there is no central point of coordination. And
that was by design, because the anonymity of users was important for the
originator of the bitcoin or other cryptocurrencies blockchain. And these
identities are also called addresses in bitcoin. Because anybody who sends
money to Alice, he sends it to Alice public key and that is why it is called
an address. And it is not connected to Alice real world identity. Nobody is
checking. There is no other linking or anything. So therefore, you cannot
discover ever who is Alice. However, there may be some investigative work
people can do. And if you are not clever enough, then by linking together the
various addresses that you are operating, how what activity you are doing,
and you are transferring money back to one particular identity and so on,
they can somehow infer certain things.
And then once they know one of the identity, one of the public key’s
identity, real world identity, then all the other ones that are also created by
you and somehow they establish the link between all this, they can make
inference. So in that sense, bitcoin blockchain is not fully anonymous. It is
called pseudo anonymous. The researchers have shown that you can actually
do various kinds of data mining to see which of the addresses are probably
being maintained and operated by the same entity and things like that. But
even then, discovering the real identity will require external investigative
work that information cannot be found in the blockchain itself.
h(r|x) ≤ y (1.3)
The target set y is from the set of possible hash values. Let us say we are
using 256 bit hash values, then our output space is 2256 and that is very large.
The target set of the hash value is basically less than, let us say, so many
leading zeros followed by any numbers. So which means; that you are saying
that not any number from 2256 possible numbers can be selected. Only the
ones with so many leading zeros, which mean relatively smaller numbers, any
x that will satisfy this will be winning for the hash puzzle. The hash puzzle
has this property that we discussed earlier, is that the only way to solve this
is through brute force. You start by saying that, x = 0000 will be this. And
then when you try to hash if the hash does not come out, to be in that range,
then you try 000001 and so on. And then you have to, you may have to try
all of them.
Now, many miners let us say there are 3 miners each having 30% of the
resources. So there is a probability that all three will solve them almost at
the same time. And then when they solve it, they think that they have won.
Because, there is no central authority to say that you are the winner. So,
all three will have their blocks as competing blocks for being added to the
blockchain. So therefore, there will be a race condition.
1.3. DISTRIBUTED SYSTEM 33
So, now everybody’s competing hash so, the every second now, I looked
this up and in December 2019, as you can see, that over almost like 110*1012
hashes per second. So, this is Tera hashes. So, that many hashes are com-
puted per second by the blockchain ecosystem. Now, all these hashes are not
computed by single node all nodes are trying. So, this number of hashes per
second is actually the total cumulative number of hashes computed.
And some of them will be doing much less number of fascists and win. The
point here is that the proof of work is a very difficult brute force computation
and there is a lot of computation that goes on at every mining node that is
trying to solve this hash puzzle. Now, the problem is that in the beginning,
everybody was using desktop computers and then the hash rate was much
less as you can see even within like this is from January 19 to December
19. In January 19, the hash rate was 40 million, tera hashes and so if you
look at this, is 44 million. Here we are looking at almost 110 million. So,
within a span of a year the hash rate has increased. This means that people
are throwing in a lot more resources, a lot more parallel computation, a lot
more GPUs and so on. To do this hash computations and therefore, we are
seeing a surge in the hash rate of the entire network. So, therefore, what will
certainly happen is that in the beginning, the let us say I keep I give you a
hash puzzle, and you can solve on an average within 10 minutes, then after
you throw in more computational power.
You can try parallely more many more hash combinations, many more
nonce x combinations, and therefore, you will be solving it faster. And then
more computation you give more computational resources you give you can
compute even more efficiently. So therefore, what happens is that periodically
the nodes automatically recalculate the targets set. If puzzle is solved quickly
then the target set will be reduced otherwise increased to an restricted extend
according to the blockchain implementation.
• Data Replication: All storage will have exactly the same copy of data.
User can access data from any of the storages. Update of data can
happen in
• Vertical Partitioning: The storage devices will have different data which
are vertically partitioned. For example, data of one field or attribute
will be in one storage and another attribute in another storage device.
• Horizontal Partition: The storage devices will have different data which
are horizontally partitioned. For example, record of User A will be in
one storage and User B’s record in another storage.
submits to other storage for their update. Now, the issue arise that how
other storage devices knows that the storage device is sending the original
update not fake. Also, how one storage can ensure that the same data is sent
to other storage. This makes the storage to resist in update of data. Such
problem occurs in all distributed systems.
1.3.1 Decentralization
The first thing that we have to understand is the question of centralization
versus decentralization. For example let us say transactions of your bank
account are kept in a database. Even you can also have one replica of the
database for fault tolerance or for backup, but the point is that the entire
data is stored in a central location or in within the authority of a central,
trusted body, like your bank. This obviously has its own problem first of
all, centralizing everything gives you this notion of, vulnerability in the sense
that if that particular data is somehow lost by a cyber attack or some other
way, then you are going to lose all the information. Second is that the central
authority has full command over what can be done on the data. And you
have to somehow trust that central authority not to manipulate the data or
delete some entries or do something like that.
Also, let us take another example, Know Your Customer (KYC) data with
a central authority. Now, if the central authority maintaining the database
decides to delete your entry, then you no longer have any access, nobody will
be able to access your data and therefore, you will be not recognized through
the KYC process. Now, unless there is a alternative way of doing KYC, you
will be in trouble. That is the problem of the centralization, too much power
in the hands of a central authority.
Therefore, Satoshi Nakamoto he or she or them, worried about these cen-
tralization of monetary infrastructure and monetary information in the hands
of few banks and we should actually create a currency that is completely de-
centralized. So, there is no central authority which creates that money or
a central authority, which, keeps the keeps track of the money or authenti-
cate people to use their money. So that is the basic, so, you know, political
economic underpinning of the introduction of the Bitcoin blockchain.
So centralization and decentralization versus decentralization is a basic
concept that underlies the blockchain technology. Now, centralization has
many advantages.
For example, it is easy to manage. A bank will not have to worry about,
36 CHAPTER 1. INTRODUCTION
you know, having replicas and privacy issues. Because if you have many
replicas, you have to make sure that the data is kept private, all that kind
of stuff. It is easy to provision. So for example, if you want to create a new
account, you have to just make change in the central database. If you want
to delete an account or then also you have to change in one place, it is easy
to ban.
Similarly, if you have a centralized authority, maintaining for example,
your DNS domain name service, then you can easily ban certain domains from
being found, and it is easy to distribute responsibility. So centralization has
all these advantages.
However, decentralization has some of these disadvantages, for example,
it is harder to manage if data is distributed all over the place. It is harder
to distribute work in the sense that you cannot say, you know, you have
this responsibility, and you have that responsibility, because nobody is being
commanded by a central server or central authority. And it is harder to ban
something so you cannot delete information so easily. And it is harder to
provision.
But centralization, as a single trusted party, is its biggest weakness in
terms of not only the fact that they might actually do things in a unilateral
way, which is not good for the consumers. But also the fact that if it gets
cyber attack or any kind of attack, then also it can actually have a devastating
consequences. Whereas if the data is decentralized, and all the functions are
decentralised then it is much harder for an attack or for a central authority
to you know take over this functionalities or change the functionalities and
so on. So, therefore, blockchain is based on the notion of trust.
So, the question is when you have a central authority, you have to trust
that authority and normally in the past we have been doing that. So, we have
been trusting the authority that is managing the DNS, we are trusting the
authority that manages the digital certificates, we are trusting the authority
that is managing our accounts. But at present, we are seeing that a lot of the
times the central authority becomes overly authoritative, they ban things.
They actually can change things that to put you into trouble so therefore,
the trust has to be created from a decentralized system.
Now, in a decentralized system when you have many players and no player
is more important than the other players, which is the case in the centralized
situation, there, creating trust is also a challenging task. Because you may
have a number of players in the system a number of actors in the system,
who are also malicious. So, you have to assume that in the ecosystem that
1.3. DISTRIBUTED SYSTEM 37
some who are actually together doing the computation, keeping track of data,
deciding what is valid and what is not valid are malicious and so, therefore,
your system should be designed in such a way so that in spite of that you
have the ability to trust the system.
So you have to derive trust from untrusted participants or an untrusted
actors for that you may have to make certain assumptions. For example, what
percentage of people are untrustworthy? And what percentage of people are
trustworthy if that assumption sometimes have to be met in order to trust
the entire system. If you did not trust anybody, 100% of the people, then you
cannot build a trusted system out of 100% untrusted actors. Let us see how
bitcoin’s blockchain bring trust and how do we derive trust in the system.
How is blockchain decentralized? The blockchain cryptocurrency applica-
tions maintain the ledger. The ledger is basically the transaction records. In
blockchain, we do not maintain the ledger in a central only one location. It
is replicated among 1000s of different computers and different what we call
nodes. Now who has authority over which transactions are valid?
So, in a regular banking situation, you see that the transaction validity
is checked by the bank. In case of blockchain, actually, every participant
who is interested in data mining or keeping the ledger is going to check the
transaction validity. So, if somebody wants to act maliciously, but others will
not necessarily act maliciously, at least, the majority will not act maliciously
we assume. And therefore, the transaction validity should be within the logic
that we have come up with. Now who creates new bitcoins?
So we know in a banking system, the new currencies created by the central
authority like Reserve Bank of India, or Federal Reserve in the case of in the
United States, but in the bitcoin blockchain, the bitcoins are not created by
any central authority. Here whoever actually wins the competition to create
the new block. And if that block is actually accepted as the new block, then
they get some reward.
And that is the only way to create a new bitcoins. So therefore, it could be
anybody who wins and not every time same person wins. Therefore, bitcoins
are created by many, participants. Now, who determines the rules of the
system change? So bitcoin has now certain system rules that everybody that
is every participant is supposed to follow. So that is how the program they
write to run the mining process or check for transaction validity.
Now, how do you change the rule? The question is that you might want
some rule change because you found some deficiency in the system. And
that is when the rule changes. When rule changes the bitcoin current part
38 CHAPTER 1. INTRODUCTION
of the blockchain that so far you have developed may become obsolete. So,
you may have to fork the blockchain. So, that is also not done centrally
because this forking process takes place kind of automatically through the
entire dynamics of the system.
1.3.2 Consensus
The consensus protocol is required for agreement among a number of pro-
cesses (or agents) for a single data value. Some of the processes (agents)
may fail or be unreliable in other ways, so consensus protocols must be fault
tolerant or resilient. A consensus protocol tolerating failures must satisfy the
following properties.
• Integrity - If all the correct processes proposed the same value v then
any correct process must decide v.
• All loyal generals decide upon the same plan of action: This is possible
only when every loyal general obtain the same information.
In both scenario, lieutenant 1 have two messages that is one attack and
one retreat. Hence, no decision can be taken since there is no majority. The
same problem is possible when there is no message from the participating
Lieutenant.
1.3.4 Faults
The distributed system have multiple components and there are chance that
some component will fail or faulty. A fault can be crash or byzantine. The
crash fault occurs due to the device failure. The Crash fault tolerance (CFT)
is one level of resiliency, where the system can still correctly reach consen-
sus if components fail for example Raft consensus protocol. The byzantine
failure exhibit an arbitrary behavior such as sending conflicting information
40 CHAPTER 1. INTRODUCTION
1.3.5 RAFT
RAFT (Raft) is a consensus algorithm developed by Diego Ongaro and John
Ousterhout in 2014 [13]. The nodes part of the RAFT consensus protocol
can be any one of the following
• Leader - After getting majority in the election, the node become leader.
• Candidate - Node can announce itself as the candidate for the election
to become leader.
In RAFT protocol, the client can communicate only with the Leader in
case any client sends request to the follower node then it will redirected to
the leader node. The RAFT protocol divides time into small terms and each
term is identified by a increasing number, called term number. Every node
part of the protocol will maintain the term number and it will be passed
while communications between nodes. Every term starts with an election
to determine the new leader. The candidates request for votes from other
follower nodes to gather majority and the candidate node secured majority
becomes the leader for the current term. In RAFT protocol, nodes does
following two communications [9].
1.3. DISTRIBUTED SYSTEM 41
• Request Vote: This request will be sent by the Candidate nodes to get
votes during an election
• Append Entries: It will be sent by the Leader node for replicating the
log entries and also as a heartbeat by any nodes to check if a server
is live. If there will be reply for the heartbeat then the server is live
otherwise down.
• A node can vote for the Candidate node only when term number of the
Candidate node is greater than other Candidate nodes in the cluster
and the same Candidate node will be elected as Leader node since all
other nodes in the cluster follow the same protocol.
• The Request Vote also contains information about the candidate’s log
to figure out which one is the latest. If the Candidate requesting the
vote has less updated data than the Follower then teh Follower does
not vote for the said candidate otherwise it may give the vote.
The Leader node sends the logs to all Follower nodes using the Append
Entries to synchronous their logs with the current Leader. Once the majority
of the Followers in the cluster or network successfully copy the new entries
in their logs, it is considered committed and the Leader also commits the
entry in its log to show that it has been successfully replicated. Later, the
leader executes the client request and responds back with the result. The
client entries will be executed in the order they are received.
Leader Crash: There is a possibility that Leader node crash in between
the term. This will lead to inconsistency in the log entries of the Follower
nodes. The newly elected Leader will match index number in the Leader
and Follower, Follower then overwrite with the new entries supplied by the
Leader to match the Follower with the Leader.
We assume that there are n = 3m+1 generals, where m are traitors. Here
”oral message” means each general is supposed to execute some algorithm
that involves sending messages to the other generals, and we assume that
a loyal general correctly executes his process. The processes are given in
the algorithms 7 and 8. In algorithm 7, the commander sends his value
(target or retreat) to the lieutenants and lieutenant uses it. In algorithm 8,
the commander send his value to lieutenants, later each lieutenants act as a
commander and send the received value to other lieutenants. The algorithm
8 will be called recursively till m becomes 0. Finally the decision will be
taken based on the majority value received. Figure 1.12 shows an example
1.3. DISTRIBUTED SYSTEM 43
There are two natural choices for the value of majority(v1 , v2 ..., vn−1 ):
• The majority value among the vi if it exists, otherwise the value RE-
TREAT
44 CHAPTER 1. INTRODUCTION
• The median of the vi , assuming that they come from an ordered set
it to the other lieutenants, who add their signatures and send it to others,
and so on. This means that a lieutenant must effectively receive one signed
message, make several copies of it, and sign and send those copies. It does
not matter how these copies are obtained; a single message might be copied
and sent, or each message might consist of a stack of identical messages
which are signed and distributed as required. Algorithm assumes a function
choice, which is applied to a set of orders to obtain a single one. The only
requirements we make for this function are
• If the set V consists of the single element v, then choice(V ) = v.
• Choice(∅) = RETREAT, where ∅ is the empty set.
is not there already and send the received message to others lieutenants
after signing it. In case, Lieutenant i receive the message v of the form
v : 0 : jl : .. : Jk and v is not in the set V then add it. Also Lieutenant
i further circulate the message by adding its signature if k < m. After the
time out or indication of no message, Lieutenant i will pick the choice from
the set V . Figure 1.13 shows the process of signature based solution with
three generals. Even though, two choices are possible for each lieutenants
they will obey the order received by commander but lieutenants can identify
the traitor that is commander, who has signed for conflict messages.
they must start in the same state. The pBFT will work for asynchronous
distributed system with bounded delay, duplicate or deliver messages out of
order. The pBFT designed to work in asynchronous systems with following
steps.
• The client waits for 1 reply from different replicas with the same result;
this is the result of the operation.
With the help of a hash pointer, we can retrieve the information and
verify that the information or data is not changed. Figure 1.14 shows the
hash pointer for the information block B. In figure, the information B is used
to compute the hash pointer to B. In case B is modified then hash pointer
available cannot point or match the modified B.
Figure 1.15 shows the chaining of blocks using hash pointer. In figure
1.15, if the data block A is modified in later stage then it will be detected
while traversing because data block B holds the hash pointer of data block
A. Similarly for all blocks, the hash pointer prevents modification. The only
way to change the block content is through completely changing the block
sequence or blockchain.
1.4. DATA STRUCTURE 49
near the tail that do not have any pointers coming out of them, compute the
hashes of those and then work our way back, towards the beginning. But in
a structure with cycles, there is no end that we can start with.
00 00
root = h(a ||b )
00 0 0 00 0 0
b = h(c ||d ) a = h(a ||b )
0 0 0 0
d = h(f ||g) c = h(e||f ) b = h(c||d) a = h(a||b)
m8 m7 m6 m5 m4 m3 m2 m1
is then put into a list. And so the list contains, let us say, every 5 seconds,
whatever is created the Merkle tree root of that. Then next 5 second Merkle
tree root will be that. And these things are also hash connected. Then the
government publishes the latest hash. So you have a data structure which
looks like merkle tree. But these are also hash connected. So this hash
connected blocks is called a calendar blockchain.
So this calendar blockchain to convince people more into the government
what they do is that every weekend they publish the latest hash value. The
latest hash value that has been of the block that was created is published in
a newspaper. So if somebody changes something here some internal govern-
ment employee, then he has to change the next one then next one then next
one and then eventually public data hash will change.
Now the public has this hash. Therefore, it will be not very easy if
the public challenges that I believe that some data has been changed here.
Then the government will be in trouble because public can challenge this
information. So that is the idea of Merkle tree.
Likewise, with H(kl) and H(ij), H(klij) can be computed and using H(klij)
and H(hmnop), H(klijhmnop) can be computed. Finally, with H(abcdef gh)
and H(klijhmnop), H(abcdef ghijklmnop) can be computed. The person A
can verify the membership of k in the merkle tree through equation 1.4 using
the available root hash (rh = H(abcdef ghijklmnop)) and the computed root
hash (ch = H(abcdef ghijklmnop)).
(
M ember If rh == ch
Descision = (1.4)
N ot a M ember otherwise
Extension node: Let S be a set of strings (s1 , s2 ..., sn ), and σ the LCS of
S. The extension set of S is the set of characters c such that σc is a prefix
of at least one string in S.
• If f |S| = 1, then T has only one node whose its P I is |S|, and its RP
references sS.
• Let E be the extension set of S. Then, u has |E| child nodes, one for
each character c in E. Specifically, the child node vc for c is the root
of a Patricia trie on the set of strings in S with σc as a prefix.
Figure 1.18 [17] shows an example for the PATRICIA tree constructed
using the steps given above.
In this case ,the leaf is the node containing the element even branch node
will also have the element at the end of the list. The branch node is the one
having the link to further description of the trie tree that is from 0 to 15 (0
to f in hexadecimal). The last field in the branch node is the value belongs
to the string end with that node. An extension node is an advantage in the
Merkle Patricia Tree to have the link to the next level tree. It contains two
field one for the shared substring and other is pointer to the next node.
Since it is an merkle tree, hash pointer is used to safegaurd the leaf
node and branch node values. The leaf node and the extension node are
differentiated using the prefix value. The root of the tree will have the hash
of the complete tree and pointer to the sub nodes.
56 CHAPTER 1. INTRODUCTION
1.5 Exercises
1. In BFT, 3f + 1 nodes are mandatory to achieve consensus. If we go
for less than 3f + 1 then we should have trusted nodes in the network?
Justify the reason why cannot we ensure the consensus without trusted
party if less than 3f + 1 is the network size.
2. What is the difference between BFT and Raft consensus mechanism?
Which one is best with respect to number of faulty nodes?
3. Is the Hash functions NP-hard?
4. What is the state machine in pBFT?
5. Using XOR, perform symmetric key encryption for a message ”Blockchain”.
Take a random value and use as the key.
6. Prove the chance of hash collision using the Birthday problem?
7. What is the difference between SHA 512 and MD5 hash functions?
8. How can we ensure non-repudiation using Digital Signature?
9. The elliptic curve of the form y 2 = x3 + ax + b over finite field with
a = 0, b = 7 and q = 23 has point P = [1, 13]. Find 3[P ].
10. What is the advantage of private permissioned blockchain over conven-
tional distributed database with a trusted third party and distributed
consensus mechanism?
11. What is the advantage of Trie tree over binary search tree. What is the
worst case search complexity of an element of size m in binary search
tree and trie tree. Assume tree has n nodes.
12. Let us assume that, an adversary system takes t second for hash com-
putation, r second for integrif y(), u second for mod. Adversary stores
only V1 , V5 , V1 0...Vn (computed using Algorithm 1.2.5.1) in RAM in-
stead of complete V to minimize the memory utilization. If an at-
tacker wish to compute B, how much time will be taken for n = 15
and j = 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 11, 2, 1, 5, 4. Also compute the time
required when all values of V in RAM.
13. How do you prove Non-membership of Merkle tree ?
Bibliography
[8] Dwork, C., and Naor, M. Pricing via processing or combatting junk
mail. Proceedings of the 12th Annual International Cryptology Confer-
ence on Advances in Cryptology (CRYPTO’92), pp.139–147 (1992).
57
58 BIBLIOGRAPHY
1 Cryptocurrency 3
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Bitcoin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.1 How Bitcoin Works . . . . . . . . . . . . . . . . . . . . 7
1.2.2 Bitcoin Script . . . . . . . . . . . . . . . . . . . . . . . 8
1.2.3 Address . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.3.1 Wallet Import Format (WIF) . . . . . . . . . 11
1.2.3.2 Collisions . . . . . . . . . . . . . . . . . . . . 11
1.2.4 Transaction . . . . . . . . . . . . . . . . . . . . . . . . 12
1.2.4.1 Transaction Creation . . . . . . . . . . . . . . 14
1.2.4.1.1 Coinbase Transaction . . . . . . . . . 16
1.2.4.2 Transaction Validation . . . . . . . . . . . . . 18
1.2.4.3 Unspent Transaction Output . . . . . . . . . 19
1.2.4.4 Transaction Age . . . . . . . . . . . . . . . . 19
1.2.4.5 Transaction ID (TxID) . . . . . . . . . . . . . 21
1.2.4.6 Simplified Payment Verification . . . . . . . . 21
1.2.4.7 Reclaiming Disk Space . . . . . . . . . . . . . 22
1.2.5 Mining . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.2.5.1 Block . . . . . . . . . . . . . . . . . . . . . . 23
1.2.5.1.1 Block Validation . . . . . . . . . . . 26
1.2.5.2 Bitcoin Consensus . . . . . . . . . . . . . . . 26
1.2.5.3 Proof of Work . . . . . . . . . . . . . . . . . . 27
1.2.6 Difficulty and Target . . . . . . . . . . . . . . . . . . . 29
1.2.7 Mining Incentive . . . . . . . . . . . . . . . . . . . . . 31
1.2.8 Bitcoin Wallet . . . . . . . . . . . . . . . . . . . . . . . 31
1.2.8.0.1 Pseudo-Anonymity . . . . . . . . . . 32
1.3 Ethereum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1
2 CONTENTS
Cryptocurrency
1.1 Introduction
Nowadays, there are multiple cryptocurrency implementations available for
the users however basic terminology are same for all. We first discuss the
basic terminologies and later the implementation of popular cryptocurrencies.
3
4 CHAPTER 1. CRYPTOCURRENCY
Light node - An user store only the header of the blockchain and will
have the key pairs and the derived address
Account with key pair and without Blockchain data - An user have
their own key pair and derived address but he/she will not store the
blockchain information.
Account with address and without key pair - An user get the address
from an exchange or third party and use it. He/She will not have their
own key pair and store the blockchain information. Security of the user
is depend on the third party.
Miner: The node that generate or mine the successful block after validating
and including transactions and solving the hard Proof of Work (PoW) prob-
lem.
Block: Block contains record of some or all recent valid transactions along
with other attributes to ensure security.
1.1. INTRODUCTION 5
Double Spending: Spending the coin more than once is called double
spending. It is possible only when the attacker mines two block for same
number with and without the respective transaction. PoW mitigates mining
two same number blocks in the network. Nevertheless there are chances that
attacker mined block (B) may not be largely accepted in the network and
go as stale block. If a recipient react based on that transaction available in
B then it will lead to double spend. Hence any bitcoin transaction cannot
be accepted by the recipient before minimum six blocks build on top of the
block on which respective transaction is included.
dards for Efficient Cryptography (SEC) and Bitcoin uses it for the digital
signature
E : y 2 = x3 + ax + b over Fp (1.1)
The elliptic curve equation given in equation 1.1 and discussed in Intro-
duction section takes the following values for its constants a, b and p along
with base point G, order n of G and co-factor h for SECP256k1.
a=0
b=7
p = 2 − 2 − 2 − 2 − 2 − 2 − 24 − 1
256 32 9 8 7 6
G = (x1 , y1 )
x1 = 79BE667EF 9DCBBAC55A06295CE870B07029BF CDB2DCE28D9
59F 2815B16F 81798
y1 = 483ADA7726A3C4655DA4F BF C0E1108A8F D17B448A6855419
9C47D08F F B10D4B8
n = F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F EBAAEDCE6
AF 48A03BBF D25E8CD0364141
h = 01
Elliptic curve points are derived using the above values and the point based
on the random integer value (that is private key) is picked as the public key
for bitcoin users.
1.2 Bitcoin
Bitcoin is the first fully decentralized cryptocurrency and a popular appli-
cation of a public distributed ledger called Blockchain. It was introduced
in 2008 by unknown individual/group Satoshi Nakamoto and he maximum
1.2. BITCOIN 7
Any user in the network can verify the ownership of the transaction using
the script and the signature, public key and public key hash available in the
transaction. The script execution for validation of transaction is shown in
table 1.1 [16] for the following.
Bitcoin has many script words such as constant, flow control, stack,
splice, bitwise logic, arithmetic, crypto, locktime, pseudocodes and reserved
words [16] however this book only introduces script with an example does
not discuss all the script words.
1.2.3 Address
Every user of bitcoin network should have unique address to receive and
spend bitcoins. A pay-to-pubkey based transaction was used before the bit-
coin address is introduced. In pay-to-pubkey, public key is used like address
but it has the size issue. Later, key hash is introduced as address, which
is of less in size compared to key size. Bitcoin uses addresses, size varying
between 26 and 35 characters to identify a user and a transaction. Bitcoin
transaction does not need f rom address, it should only have the recipient ad-
dress. There are different address types namely Pay-to-Script-Hash(P2SH),
Pay-to-PubKeyHash (P2PKH),and Bech32 segwit address. However, Pay-
to-Script-Hash(P2SH) is by default used and Pay-to-PubKeyHash (P2PKH)
is the predecessor and it can be used if required by user. In both, address
are generated using the compressed or uncompressed public key of the user.
In elliptic curve digital signature algorithm, private key is a scalar value
and public key is a vector value (x,y). If we take x alone then it is called
compressed public key otherwise it is called as uncompressed public key. A
Key type can be identified using the prefix for example, 0x04 is to represent
uncompressed public key, 0x02 is to represent compressed public key, which
has even value for y and prefix 0x03 is to represent compressed public key,
which has odd value for y [48]. Algorithm 1 shows the process of generating
the address using the uncompressed public key.
Bitcoin supports different networks, some are given in table 1.2 along
with its identity to be added at the time of generating address and the prefix
(leading symbol) to be added with address to identify its type. An exam-
ple address 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2 of main network
P2PKH.
1.2.3.2 Collisions
Bitcoin addresses are basically random numbers and it is possible, although
extremely unlikely, that two users independently generate the same address
called as collision. In such case, both the original owner of the address and
the colliding owner could spend money sent to that address. It would not be
possible for the colliding person to spend the original owner’s entire wallet if
12 CHAPTER 1. CRYPTOCURRENCY
different address is used for each transaction. If you were to intentionally try
to make a collision, it would currently take 2107 times longer to generate a
colliding Bitcoin address than to generate a block. As long as the signing and
hashing algorithms remain cryptographically strong, it will likely always be
more profitable to collect generations and transaction fees than try to create
collisions [10].
1.2.4 Transaction
A transaction is transfer of a digital bitcoin for one user to another user. If
Alice wish to transfer or spend a coin then she creates a new transaction by
digitally signing the hash of the previous transaction called as input transac-
tion, adding address of the next owner and her public key as shown in figure
1.1. A payee or any other member in the network can verify the signatures to
confirm the chain of ownership of the coin [44]. In figure 1.1, payer C creates
transaction III by computing signature of its own coin transaction II using
his/her private key, adding its public key and payee D address. Any user
of the network including D can access the public key of the transaction III
and validate the transaction. User can derive C’s address from the available
public key and ensure the transaction is belong to C. User can use the public
key and verify the signature to ensure C is spending the coin.
There are three collection of transactions
Transaction pool - An unordered collection of transactions that are not
in blocks of the main chain, but for which we have input transactions.
It is the pool of transactions to be spent.
Step 1: Using public key, the signed that is encrypted hash will be
decrypted.
than one person to look after all bitcoin transaction. To spend the coin, all of
them have to sign the transaction or M out of N signature is enough. Here,
more than one person indicates more than one private keys are required,
where N is greater than M. It is required that all of them have to agree to
spend the coin or majority should agree on spending the coin.
general information. The signatures in the SegWit are not part of the bitcoin
block size computation. Bitcoin blocks cannot exceed 1MB of size. Since the
signatures are not part of the block size computation, more transactions can
be added to mitigate the waiting time of transaction approval.
Input and Output Transaction:The format of an input transaction
given in table 1.4. The Previous Txout-index refers to the previous trans-
action output index that is in one transaction there may be multiple output
transactions. Index refers the ith output transaction, which is used for creat-
ing new transaction. For example, in Figure 1.2 new transaction is created
using the old (previous) transaction hash and output. Transaction index for
the first old input and output transactions are assigned O and it increase by
one for every next transaction. New transaction input uses the old transac-
tion second output that is index 1.
as parent [13]. It is mandatory for all the blocks to include reward and
transaction fee. Also this is the first transaction of every block.
Size - The size of the transaction is <= MAX BLOCK SIZE and greater
than or equal to 100 bytes.
Insufficient BTC - Reject if the sum of input values less than sum of
output values.
Public key - Verify the scriptPubKey accepts for each input otherwise
reject.
Orphan transaction - For each input, look in the main branch and the
transaction pool to find the referenced output transaction. If the output
transaction is missing for any input, this will be an orphan transaction.
The difference between dust and unprofitable transactions are only with
transaction fee quantity. All dust transactions are unprofitable transactions
but not all unprofitable transactions are dust transactions.
from the transaction pool based on priority and include in the block after
validation. Transactions are prioritized based on the age of UTXO and high
value inputs [29]. The age of a UTXO is the number of blocks that have
elapsed since the UTXO was recorded on the blockchain or number of blocks
that are build on top of the UTXO included block in the blockchain. Prior-
itized transactions will be processed and included in the block without any
transaction fee if space is available in the block. The priority of a transaction
is calculated using equation 1.2.
Pn
i=1 (V alue of inputi ∗ Input Agei )
P riority = (1.2)
T ransaction Size
Where n is the number of input transactions in the respective transaction.
The Input Agei is ith input UTXO age that is number of blocks that are build
on top of the UTXO included block in the blockchain. The V alue of inputi
in equation 1.2 is ith input UTXO value (BTC) in bitcoin base unit that is
Satoshi unit. The satoshi is a smallest unit of the bitcoin currency recorded
on the blockchain and named in collective homage to the original creator of
Bitcoin, Satoshi Nakamoto. It is a one hundred millionth of a single bit-
coin (0.00000001 BTC). The size of a transaction is measured in bytes. The
T ransaction Size is the size of the transaction in total including all input
and output transactions. A transaction can be considered as high priority if
the equation 1.2 result is greater than 57,600,000, which is possible when the
value of input is atleast 1 BTC, aged one day (144 blocks) and size of 250
bytes.
The first 50 kilobytes of transaction space in a block are set aside for high-
priority transactions. Miner will fill the first 50 kilobytes, prioritizing the
highest priority transactions first, regardless of fee. This allows high-priority
transactions to be processed even if they carry zero fees. This concept intro-
duced with notion ”bitcoin days destroyed”.
volume and creates spam in the network. The ”bitcoin days destroyed” is an
idea that to give more weight to coins which have not been spent in a while
and prioritize.
e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b
6585fb468:
block 91,722: 00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c
6cdf3f2574dd08e
block 91,880: 00000000000743f190a18c5577a3c2d2a1f610ae9601ac046
a38084ccb7cd721
d5d27987d2a3dfc724e359870c6644b40e497bdc0589a033220fe154
29d88599:
block 91,812: 00000000000af0aed4792b1acee3d966af36cf5def14935db
8de83d6f9306f2f
block 91,842: 00000000000a4d0a398161ffc163c503763b1f4360639393e
0e4c8e300e0caec
want to verify the transaction, he/she will access the merkle bunch from the
neighboring full node and validates the received transaction and merkle root
hash with the available merkle root hash in the block header. Linking the
transaction in the main chain demonstrates that a network node has accepted
it, and blocks added after it further confirms.
1.2.5 Mining
Bitcoin Mining is the process of generating a new block by validating the
transaction and solving the hard problem. Miner of the network validate
the transaction and mine the blocks by including all valid transactions. A
bitcoin user with sufficient infrastructure can mine the blocks and broadcast
it to the network. An other nodes in the network validates the block and
include in the chain if it is valid. Block mined and successfully linked with
the main block chain is almost impossible to modify. Impossible only when
51% hash power is not with the attacker(s). It means, an attacker can make
the block as orphaned block, which will not contribute to the main chain if
have less hash power. In Bitcoin or any other blockchain application with
Proof of Work, chain with the longest length that is having more blocks
will be the main chain. For example, in figure 1.4, the block with grey
border and pointer is not contributing to the main chain because this block
is orphaned or forked. An effort of miner to mine such block will go useless
and transactions included in that block become unrecorded. Block will be
considered as successful one when it takes part in the main chain. A fork is
1.2. BITCOIN 23
possible when more than one miner mines the same numbered block and it is
quite obvious to happen because any user in the network is allowed to mine
the block. For example, two or more miners mined the same numbered block
(Block:2) and broadcast in the network. After receiving any one of those
blocks, miner will start mining next numbered block. Similarly all miners
will mine next block based on any one block he/she received. There will be
a chance that only one miners block (Block:2) get more block on top hence
part of the main chain. Miners (A) block without getting more block on
top will go as the fork or orphan. However, A will try to bring their block
included chain as main chain but it may go unsuccessful if number of blocks
linked to the main chain is more and other miners will not take those blocks
for building next block because they do not get incentive or reward.
1.2.5.1 Block
Block is a file where data pertaining to bitcoin is permanently recorded. Data
in bitcoin is the bitcoin transactions. In addition to transactions, it contains
meta-data to link with other blocks to ensure security. The parameters of
the bitcoin block are as follows [1] [2]
Target or nBits - This is the value to ensure effort of the miner to mine
the block. The hash of a block should be less than or equal to this
value. The target changes for every 2016 blocks approximately two
weeks considering 10 minutes per block. The method of computing the
target is discussed in later sections. The size of this is 4 bytes.
The bitcoin block parameters are divided into header and data parameters.
The 80 bytes header of the bitcoin block includes only Version, hashPre-
vBlock, hashMerkleRoot, Time, Target and Nonce. Transaction parameters
is the data part of the block. If we say hash of the block that means hash of
1.2. BITCOIN 25
the header. Transactions are not part of the block hash computation how-
ever the transactions merkle root hash is part of it. The parameters magic
number and block size are the additional data to the block for interpretation.
Light nodes, which does not have sufficient storage, stores only the 80 bytes
of header and use simplified payment verification for validation of transac-
tion. The simplified payment verification method is discussed in the later
section.
There are following three types of blocks possible in bitcoin blockchain.
Blocks in the main branch - The transactions in these blocks are con-
sidered as confirmed.
Blocks on side branches off the main branch (fork) - These blocks have
lost the race to be in the main branch and such blocks are called as
stale block. In figure 1.4, the blocks with grey border are the stale
blocks.
Orphan blocks - these are blocks which does not link into the main
branch, normally because of a missing predecessor or nth-level prede-
cessor. However, in all discussion on forums, stale blocks are called as
orphan block [8].
Blocks in the first two categories form a tree rooted at the genesis block,
linked by the previous pointer, which points toward the root. The main
branch is defined as the branch with highest total difficulty, summing the
difficulties for each block in the branch.
The parameters Target and Nonce are used for the Proof of Work (PoW)
distributed consensus to ensure sufficient amount of time is spent to mine
the block. Bitcoin uses Proof of Work to ensure that all participating nodes
agree on the same branch of the blockchain. To achieve PoW, miner solve a
computationally expensive problem, mines the new block and broadcasts it to
the other nodes in the network which can easily validate based on the values
present in the block [45]. There could be thousands or more nodes of the
network around the world simultaneously solving the same PoW problem.
Intention of giving complex problem to miner is to reduce the chances of
nodes solving the problem at the same time and thereby reduce the chance
of forks.
26 CHAPTER 1. CRYPTOCURRENCY
1.2.5.1.1 Block Validation Users validate the block before adding the
block into main chain. The first validation is, the block should follow the
syntactic structure and the hash of the block should be less than the target
otherwise it will be rejected. Following are some other important validations
to be done before including the block in main chain. [25] [37].
Duplicate - Received block should not be duplicate.
Timestamp Validation - Block will be valid if its timestamp is greater
than the median timestamp of previous 11 blocks and less than the
network-adjusted time + 2 hours. The Network-adjusted time is the
median of the timestamps returned by all connected peer nodes [3]
Transaction - Transaction list must have atleast one transaction (coin-
base transaction) or more transactions and it must be valid according
to transaction validation process. First transaction should be coinbase
transaction.
Merkle Hash - Ensure that the merkle hash present in the block header
and the hash computed for the merkle tree containing all transactions
matches.
Previous Block Hash - Verify the previous block hash is in main branch
or side branches. If not add into orphaned blocks.
Target - Check that nBits (target) value matches the difficulty rules.
Miner takes the transaction, validate and include in the block and
broadcast the block to other members of the network, if transaction
is genuine.
Other users of the network validate the block and transaction, accept
if it is valid.
To achieve PoW, Bitcoin miners have to solve hard problem but just in
one attempt other members verify the correctness of the solution. At first,
miner construct the candidate block that is block without nonce. To achieve
PoW, SHA256 hash will be computed for the candidate block and nonce
(start at 0 and increment) till the output is less than or equal to the target
value. The target value will be computed for every 2016 blocks and it is
discussed in the later section. Figure 1.5 shows the procedure of repeating
the hash computation by concatenating the nonce value [9] with candidate
block. Assume ”Hello, World” is the candidate block data in the figure 1.5
and 0,1,...4250 are the nonces for understanding. Target assigned is 2240 .
28 CHAPTER 1. CRYPTOCURRENCY
The condition of PoW is satisfied when the nonce 4250 is concatenated with
”Hello, World”. In the block header, nonce size is of 4 bytes than the maxi-
mum value it can support is 232 −1. There may be a possibility that numbers
from 0 to 232 − 1 cannot not produce the hash less than the target and no
point in again repeating the same nonce because it will produce the same
result because the SHA256 hash algorithm is deterministic, which produces
same output for an input even it is repeated infinite times. In such case,
Proof of Work cannot be achieved. Hence the option of extraNonce and
timestamp update is included in the mining. An extraNonce can be included
and that will be placed in the left node of the merkle tree not on the block
header. In such case, hashMerkleRoot in candidate block gets changed and
as consequence candidate block is changed. Now repeating the same nonce
with modified candidate block will give different hash values because input is
different. Also, if required timestamp of the block can be updated to update
the candidate block data to produce the new hash output with same nonce
series. These two options should be preferred only after all possible nonces
are tried with the first made candidate block.
How the timestamp updated? At the time of making candidate block, miner
uses their system linux timestamp and start computing the hash with differ-
ent nonce. There is a time change when the nonces are exhausted. Latest
timestamp will be included in the candidate block.
Multiple Blocks: There is a possibility, multiple miners can successfully
mine the same numbered block. For example, assume miners are working on
top of block number 4 to mine 5th block. There is a possibility that two
miners (A, B) mined the 5th block but only one block can take part in the
main chain. Both miners broadcast blocks to their neighbors for propagation
in the network. In this case, some of the miners will work on top of the As
5th block and other set of miners will work on top of the Bs 5th block. The
6th block will decide which 5th block will be considered for the main chain
because the longest chain is the main chain. If the miners produce 6th block
from miner As 5th block then As block will be part of the main chain and
Bs block will go as stale block. However, there is a possibility that 6th block
for both A and Bs 5th can be produced at same time or with valid delay. In
such case, the main chain cannot be decided. The main chain can be decided
based on the 7th block. Likewise it will move forward to decide the main
chain and side chain. According to the live bitcoin experience, the success of
block will be decided only after next 7 blocks on top of it. This is the reason
no transaction can be implemented before 7 blocks build on top of the block
1.2. BITCOIN 29
M ax T arget
Difficulty = Current T arget
(1.3)
Max Target = 0X00000000FFFF000000000000000000000000000000000000
0000000000000000
30 CHAPTER 1. CRYPTOCURRENCY
The Current T arget that is the target for next 2016 block will be calcu-
lated using equation 1.4 and 1.6. The old Dif f iculty is the difficulty of the
last 2016 blocks. Initial 2016 blocks target is are computed using the Devia-
tion in equation 1.4 is ratio between Expected T ime to mine 2016 blocks and
Actual T ime time taken to mine [47]. As defined, 10 minutes are required
to mine a bitcoin but it varies. Considering 10 minutes per block, expected
time is 20160 minutes for 2016 blocks.
Expected T ime
Deviation = Actual T ime
(1.4)
M ax T arget
Current Target = Dif f iculty
(1.6)
Use new address for every transactions - A user worries about financial
privacy and does frequent transactions will generate new key pairs and
use different address for each transaction.
Most of the bitcoin users are of second type uses new address for each
transaction and back up the key pairs regularly. Since key pairs are used
to derive the address, backup the key pair is sufficient. In the beginning,
private/public key pair is generated in non-deterministic way that is key
pairs are randomly generated and it should be backup each time you make
a new pair of addresses. The process seems easy but it become more and
more complicated and cumbersome to track/backup these days because of
increase in number of private/public key pairs as the number of transaction
increases for a bitcoin user.
Deterministic Wallet [4] is introduced to overcome the issues of handling
multiple address. In the first version of deterministic wallet, key pairs to
derive address are generated from a known starting string or seed. The
private key is generated by computing the SHA256 hash of the seed added
with an integer.
1.3 Ethereum
Ethereum is the implementation of cryptocurrency with an additional fea-
ture that is smart contract to run the decentralized applications. It takes
less time to mine the blocks than Bitcoin even though it also uses Proof
of Work (PoW). It uses Gas units to specify fee for every transactions and
other operations. The size of a block is defined based on Gas limit. Like
Bitcoin, Ethereum also does not preserve the privacy of the accounts that
if addresses and amounts in the transactions are public. However, map-
ping of account holder and the account number (or) address is not public to
achieve pseudoanonymity. Ethereum works based on the account balances
and transaction unlike Bitcoin, which works only based on transaction.
the block is mined according to the ethereum requirement that is valid trans-
action, valid Proof of Work, valid timestamp, valid previous block hash, etc.
The transaction will be considered as successful after inclusion in the block
and take part in the main blockchain. The miner will get reward as well as
transaction fee as a compensation for mining the block. Users wish to cre-
ate their own decentralized application can create the account in Ethereum
network and store their smart contract code, which will be validated and
included in the blockchain by miners. The smart contract will automatically
get executed when the condition in the contract satisfies. Ethereum uses gas
to measures the amount of computational effort that it will take to execute
certain operations. It restrict users from adding more transactions and per-
forming infinite computations. The gas for each operation is predefined in
the implementation for example
21000 for gas for a transaction
30 gases for SHA3 computation
32000 gases for all contract-creating transactions after the Homestead
transition.
One gas is equal to 109 Wei or 10−9 Ether. Wei is the denomination like
paisa for Indian Rupees. If user A makes transaction then equivalent gas
value will be deducted from A account as transaction fee and deposited in the
miner account. The transaction fee is calculated as gaslimit∗gasprice. Users
not having sufficient gas on his/her account cannot make the transaction in
the ethereum network. However, for mining a block there is no need to have
gas rather miner will get the transaction fee and mining reward on their
account if the block is successful.
2. ftrim (H) → address: Take the rightmost 160 bits of the hash output
H and convert to ASCII to get the address.
Even though there are many reasons, one of the reasons for choosing
the last 40 characters instead of first 40 characters is that the chance for
repetition of characters in first 40 is more compared to the last 40.
1.3.3 Transaction
Transaction is an important property for cryptocurrency. A transaction is
a message that is sent from one account to another account. Message is of
two types: one is message call and another is contract creation. Transaction
orginator may differ from sender in the case when the message call or con-
tract creation comes from the EVM code. Following are the parameters of a
transaction.
gasPrice: A scalar value equal to Wei to be paid per unit of gas spent
for this transaction. gasPrice will be set by the transaction owner. Its
miners choice to choose the transaction for mining or not.
In case, same transaction sent twice with same nonce then any one will
get executed and next will be discarded by the network.
1.3. ETHEREUM 37
1.3.4.1.1 Size Computation The size of the cache and data set DAG is
decided using block number and other initial parameters listed in table 1.6.
The algorithms 4 and 5 [20] shows the process of computing the size of the
cache and DAG respectively. The size will vary based on the block height.
calculated by taking Keccak-256 hash for 32 bytes of zeros and for other
epochs Keccak-256 hash for the previous epoch seed hash as in equation 1.7.
SHi denotes seed hash for the epoch number i.
(
KECCAK − 256(032 ) If Epoch Number(En ) == 1
SHi = (1.7)
KECCAK − 256(SHi − 1) otherwise
Sha3 256 in the Algorithm 6 for seed hash computation is the Keccak-256
hash function.
1.3.4.1.3 Cache computation Using the seed and cache size, the ini-
tial cache will be generated using equation 1.8 till i == 0 to n, where
Cache Size
n = HASH BY T ES
. It takes seed as the initial value for intermediate cache
C0 computation and iterate with Ci−1 to generate Ci till n. Final cache is
computed using the equation 1.9 with input C and CACHE ROUNDS . The
function rmh in equation 1.10 is the RandMemoHash algorithm [41] which
takes arbitrary size input and produces 64 bytes output.
(
KECCAK − 512(s) If i == 0
Ci = (1.8)
KECCAK − 512(Ci − 1) otherwise
x If y == 0
R(x = C, y = CACHE ROU N DS) = ERM H (x) If y == 1
R(ERM H (x), y − 1) otherwise
1.3. ETHEREUM 41
(1.9)
ERM H (x) = (Ermh (x, 0), Ermh (x, 1), ..., Ermh (x, n1)) (1.10)
Figure 1.7 shows the golang based pseudo code to generate the cache data
using equation 1.8 to 1.10.
1.3.4.1.4 Data set computation Using every pieces of cache, data set
will be computed to support the mining process. Equation 1.11 ot 1.13 shows
the method of computing the data. It takes the input as cache (c) and the
index (i) and computes the data by applying the KEC512 or Fowler/Noll/Vo
(FNV) hash functions on the pseudorandomly selected cache nodes . The
variable csize is the size of cache and parameters DAT ASET P AREN T S,
HASH BY T ES and W ORD BY T ES are pre-defined.
(
Eparents (c, i, p + 1, Emix (m, c, i, p + 1)) if p < DAT ASET P AREN T S − 2
Eparents (c, i, p, m) =
Emix (m, c, i, p + 1) otherwise
42 CHAPTER 1. CRYPTOCURRENCY
(1.12)
(
KEC512(c[i mod csize ] ⊕ i) if p = 0
Emix (c, i, p, m) = HASH BY T ES
EF N V (m, c[EF N V (i ⊕ p, m[p mod W ORD BY T ES ]) mod csize ] otherwise
(1.13)
Pseudo code to compute the full data set using the cache and equations
1.11 ot 1.13 is shown in figure 1.8 [20].
1.14 to 1.17 are used to generate the mix hash. The parameter, d is the
data set, m is the mix hash (initially it is empty), s is the seed hash that is
sha3 512(header+revert(nonce)) and i is the iteration index.
(
Emixdataset (d, m, s, i) if i = ACCESSES − 2
Eaccesses (d, m, s, i) =
Eaccesses (Emixdataset (d, m, s, i), s, i + 1) otherwise
(1.14)
M IX BY T ES
Enewdata (d, m, s, i)[j] = d[EF N V (i ⊕ s[0], m[imod )]
W ORD BY T ES
dsize/HASHB Y T ES
mod .nmix + j] (1.16)
nmix
∀j < nmix
m if i >k m k 8
Ecompress (d, m, s, i) = Ecompress (EF N V (EF N V (EF N V (m[i + 4],
m[i + 5]), m[i + 6]), m[i + 7]), i + 8) otherwise
(1.17)
Figure ?? and 1.9 has the pseudo code to compute the mixdigest for the
full node and light node using equation 1.14 to 1.17. If the result less than
the target, then it is considered as the valid nonce and it will be attached
with the block otherwise redo the process. Non mining full node can use the
same mix hash generation technique for the respective header and nonce then
validates the proof. Light nodes can generate the respective part of data on
the fly using available cache and validate the results.
new DAG created and replace the old in buffer. It will give periodic delay
in the blockchain growth, which may bring the centralization thus 51% hash
power attack rises. Double buffering is the solution to overcome the periodic
delay. DAG used for the current epoch X1 will be in buffer B-1 and the
pre-generated next epoch DAG X2 in another buffer B-2. Once the epoch
X1 is over, the DAG in B-2 for epoch X2 can be used for mining without any
delay. The DAG for next epoch X3 will be pre-generated and stored in buffer
B-1. Buffers will be used in cycle to avoid periodic delay in mining. [18]
(
parent d 1 b timestamp − p timestamp < 13
block d = parent d + ∗
2048 −1 otherwise
(1.18)
The time difference between latest or presently mined block and its parent
block are compared with 13 in equation 1.18. The difficulty value goes up
if the time difference is smaller than 13 otherwise difficulty goes down. In
equation 1.18, the difference how far off from 13 seconds was not taken into
consideration. A block mined in a second after the previous one has the
46 CHAPTER 1. CRYPTOCURRENCY
same effect on the difficulty as one mined after 12 seconds. Also, 13 seconds
and more will be considered as same in equation 1.18. To overcome this
issue, homestead version of difficulty computation given in equation 1.19 is
introduced on which for every 10 seconds difference up to a certain limit
will be weighted separately. The genesis block and the lowest difficulty in
ethereum is 131072 which is a power of 2 and magic number does not have
sufficient proof for the usage.
In the Byzantium version of ethereum [50], uncle block headers contribute
to the difficulty computation. Equation 1.20 shows Byzantium version of
difficulty computation. The value of y is 1, if no uncle header in the latest
block otherwise it is 2. The denominator 10 from 9 will ensures the same
block time even, it should decrease by 3% given the current uncle rate of
7%. [33]
2256
target = (1.21)
dif f iculty
Combining 64-bit nonce and the 256 bit mix-hash compute a 256 bits
256
hash value that is n. The value n should be less than or equal to 2 blockdif f
otherwise choose another nonce and repeat the process. This value n shows
to the network that the miner spent sufficient amount of effort to mine this
block. It is called as Proof of Work (PoW) in ethereum.
1.3.6 Block
Block of the ethereum blockchain contains three parts (BH , BT , BU ). The
BH is the block header, BT is the list of transactions and BU is the list of
1.3. ETHEREUM 47
mixHash: It is a 256 bit hash which together with the nonce proves
that a sufficient amount of computation has been carried out on this
block.
nonce: It is a 64 bit hash which together with the mix-hash proves that
a sufficient amount of computation has been carried out on this block.
hash: The Keccak 256-bit hash of its own header in entirety that is
including nonce and mix hash.
Rb is the bloom filter created out of the logs Rl . Bloom filter function
will be called for each logger address Oa and its topics Ot . This reduce
a series of log to 2048 bit (256 byte) value using equation 1.30.
_
M (O) = (M3:2048 (x)) (1.22)
x∈{Oa }∪Ot
M3:2048 is a bloom filter that sets three bits of 2048. Three bits are
chosen based on the lower order 11 bits of first three pairs (0,1; 2,3;
1.3. ETHEREUM 49
Algorithm 8 M (X, Y )
Require: X, Y
1: for i = 0, 2, 4 do {# only for 0,2,4 loop iterates}
2: p = KEC[X][i, i + 1] mod 2048
3: Yp = 1
4: end for
5: return Y
The topics for logs are created based on the input parameters of a trans-
action. The parameters required are (Senderaddress , Receiveraddress , V alue).
50 CHAPTER 1. CRYPTOCURRENCY
The reward policy for the Uncle block miner U BM is given in equation
1.24, where Ui and RBi are block number of Uncle block and Real block
respectively. The maximum difference can be six and same numbered Uncle
block is not allowed to include in the Real Block.
1
Block Reward f or BU = (1 + (Ui − RBi )) ∗ Reward (1.24)
8
52 CHAPTER 1. CRYPTOCURRENCY
Key refers the search index that is ethereum address. For an example,
assume a User A address is ca6d4f dde714f d979de3edf 0f 5aaa9716b898ec8.
This is the key on the trie for A’s account node. Suffix may be e8 and key
part may be 5aaa9716.
Ethereum uses the Recursive Length Prefix (RLP) for the purpose of serial-
izing the arbitrary bytes [26] [49] [15]. The RLP encoding in ethereum is for
the following two items.
Encoding on the items will be different for string and list. Equation 1.25
54 CHAPTER 1. CRYPTOCURRENCY
(
Rb (x) if x belongs to Byte array
RLP (x) = (1.25)
Rl (x) otherwise
x if k x k= 1 ∧ x[0] < 128
Rb (x) = (128+ k x k). x else if k x k< 56
(183+ k BE(k x k) k). BE(k x k) .x otherwise
(1.26)
Equation 1.26 is for byte array encoding. In equation, x is the byte array
and x[0] is the first character. Dot operator performs sequence concatenation.
The RLP encoding or serialization procedure for the byte array are as follows
using equation 1.26
If the input contains only a single byte of size less than 128, then the
input is exactly the output. Example - The string ”d” = [’d’], each
character is 8 bits and it will be represented in hexadecimal as [0x64].
If the input contains lesser than 56 bytes, then the output is input
prefixed by the byte equal to the length of the input byte array plus
128. Example - hello world = [0x8b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
0x77, 0x6f, 0x72, 0x6c, 0x64]
Otherwise that is length upto 2568 , the output is equal to the input
that is (1) prefixed by length of the input byte array in big endian
format that is (2), which is itself prefixed by the length of (2) plus 183
that is (3). This has the limit that is size of (2) cannot be more than
8 and number of bytes on the byte array cannot be more than 2568 .
Example - A string with 5000 ’a’ characters, so the encoding of aaa...
= [0xb9, 0x13, 0x88, 0x61, 0x61,...]
BE in the equation 1.26 and 1.28 is to expand a non-negative integer
value to a big-endian byte array using equation 1.27.
n=0
X
BE(x) = (b0 , b1 , ....) : b0 6= 0 ∧ [bn .256kbk−1−n ] (1.27)
n<kbk
1.3. ETHEREUM 55
Big Endian Example: If we take the input of 5000 ”a” which is 5000 bytes,
the length of it is (k aaaaa... k) is 5000 and Big Endian BE(5000) = 19136
(19 and 136) that is 19 × 2561 + 136 × 2560 , where b0 = 19 and b1 = 136 the
length k BE(k x k) k is 2. The value 256 is the base for BE conversion.
The RLP encoding or serialization procedure for the list is similar to the
serialization of byte-array using equation 1.28 and 1.29
(
(192+ k s(x) k) .s(x) if k s(x) k< 56
Rl (x) =
(247+ k BE(k s(x) k) k) .BE(k s(x) k) .s(x) otherwise
(1.28)
s(x) = RLP (x0 ) .RLP (x1 )... ==> Rb (x0 ) .Rb (x1 )... (1.29)
If the input list is less than 56 bytes in length, then the output is equal
to that list prefixed by the byte equal to the length of this byte array
list plus 192. Example - [hello, world ] = [0xcc, 0x85, 0x68, 0x65, 0x6c,
0x6c, 0x6f, 0x85, 0x77, 0x6f, 0x72, 0x6c, 0x64]. First ’0xcc’ refers base
that is 192 plus length of upcoming bytes that is 204 → 0xc0 plus 0x0c
(length of input that is 10 for helloworld ) and its RLP (value:2 for
mentioning two byte array size, 0x85 and 0x85 as per equation 1.26).
Next for each byte array, RLP encoding is applied according to the
rules in equation 1.26. The first representation ’0x85’ (128 → 0x80)
plus 0x05 refers to number of bytes in that array that is five for hello.
The second representation 0x85 for the next byte array world.
Otherwise, the output is equal to the input list that is (1) prefixed by
the length of the input byte array in big endian form that is (2), which
is itself prefixed by the length of (2) plus 247 that is (3). Example -
[”a”, ”a” upto 100 times]= [0xf8, 0x64, 0x61, 0x61, 0x61, ... ]. The
hex ”0x64” is the length of the list that is 100 and no need to add RLP
prefix because the byte array size is 1 for all 100 and value of the string
is less than 128. The hex ”0x61”, the ASCII value of ”a”. The hex
0xf8 is derived from the base 247 → 0xf7 plus length of BE(100) that
is 1, where b0 = 100 because 100 × 2560 and no b1 or above.
56 CHAPTER 1. CRYPTOCURRENCY
The scalar value will be specified as byte array in the form of Big Endian
format. In case of empty string and list, it will be 128 and 192 respectively.
However, there is no specific encoding format for signed or floating-point
values.
1.3.9.2 Decoding
RLP decoding is the inverse of the encoding scheme. The string interpreta-
tion decision will be based on the first byte of RLP encoded output.
If the first byte is less than 128 and that is the only byte then input is
output.
If the first byte is between 128 and 182 then output starts from second
byte and length is less than 56 byte.
If first byte(1) is more than 183, then the output starts after ((1) - 183)
bytes. Similarly for the list item.
chain only when the total difficulty of the new chain block is greater than
the total difficulty in the present chain last block. According to figure 1.11,
user A will check the total difficulty of the 301 block of the false chain with
real chain that is its own chain’s last block that is 300. The total difficulty
in block 300 is greater than the 301 block in false chain of user B. Hence it
will not be replaced and attack will not be successful.
(σ 0 , g 0 , A) = Cf (σ, s, o, g, p, v, i, e) (1.30)
The address of new account will be created using equation 1.31 by passing two
parameters: sender address (sa ) and its current nonce (sn ) in the WorldState
that is stateRoot trie. The sa and sn − 1 will be concatenated, RLP encoded
58 CHAPTER 1. CRYPTOCURRENCY
and last 160 bits of KEC256 hash is the address of account. sn − 1 instead of
sn is because the nonce get increased for this contract before the creation.
The structure of the newly created state includes account address, nonce
(initially it is zero), balance, storageRoot(will be empty till the next account
created), and the code hash of the empty string. It will be updated once the
code is generated. The sender’s balance that is sender’s main account (or)
externally owned accounts will be reduced with the value v and the balance
of the account created for contract will be increased with value v.
Also, there are information collected after the transaction execution called
as transaction substate A and it is given in equation 1.32.
A = (As , Al , At , Ar ) (1.32)
The first parameter of the tuple is self-destruct set As . Second Al is the log
series for tracking contract-calls. Third, At is the set of touched accounts
during transaction, of which the empty ones are deleted at the end of a
transaction. Ar is the refund balance including the remaining gas value and
self-destruct reward.
at the initial stage, it uses a set of bootstrap nodes whose endpoints are hard
coded in the source code. To change the default bootnodes, user can use the
following command on startup of the node.
0256 is the parent hash, which is sequence of zeroes, 0160 is the beneficiary
or miner address, which is sequence of zeroes of size 160 bits, stateRoot is the
hash of the stateRoot trie root node (initially the stateRoot trie is depend
on the genesis file), next two zeros for transaction root and receipt root, 02048
is the logsbloom value, which is sequence of zeros, 217 is the initial difficulty
value of the blockchain, next two zeroes for block number and gas limit,
3141592 is the gas used for this block, time is the linux timestamp, next zero
for extra data, 0256 is the sequence of zeroes for the mix hash, KEC((42)) is
the Keccak hash of a one byte array of value 42, for nonce, next two empty ()
refers list of uncle block headers and series of transactions index. The genesis
block of goethereum is shown in figure 1.12
1.3.13.1 goEthereum
The goethereum [5] is the implementation of ethereum protocol using the
Go language [38]. This tool can be installed on your machine using Internet
sources. It additionally needs packages like ldb (nosql database) required for
the blockchain storage. Users can use this tool to run the private network to
test.
Once downloaded and installed, try the commands in figure 1.13 after
opening the folder in terminal to compile geth. Once it is successfully com-
piled, move to build/bin folder to start geth.
Figure 1.14 shows the command to initialize the genesis file. It includes
networkid, which makes the geth node to connect to the respective network.
It has datadir, which is to provide data directory to store all data related
60 CHAPTER 1. CRYPTOCURRENCY
Genesis Block:
{
difficulty: 17179869184,
extraData: ”0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e
50b1b82fa”,
gasLimit: 5000,
gasUsed: 0,
hash: ”0x6b319f89979dc640f83445a0595ebf0db06abf0b8ceaeea62139e0f5a48a5f5a”,
logsBloom: ”0x00000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000”,
miner: ”0x0000000000000000000000000000000000000000”,
mixHash: ”0x000000000000000000000000000000000000000000000000000000000
0000000”,
nonce: ”0x0000000000000042”,
number: 0,
parentHash: ”0x0000000000000000000000000000000000000000000000000000000
000000000”,
receiptsRoot: ”0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e
363b421”,
sha3Uncles: ”0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd
40d49347”,
size: 573,
stateRoot: ”0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da305
80f0544”,
timestamp: 0,
totalDifficulty: 17179869184,
transactions: [],
transactionsRoot: ”0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc00162
2fb5e363b421”,
uncles: [],
}
to the respective node. The port is to run the geth node on a particular
port of the machine. Duos datadir and port are required when your run
multiple nodes on the same machine for private test network. If you run
only one node on a machine then no need to provide the datadir and port,
automatically it will be taken as per the goethereum protocol. The param-
eter init genesis.json is to initialize the genesis file. It is required to have
genesis.json file on the bin folder. It may be copied from internet source.
Nodes like to connect to the network should have the same genesis file oth-
erwise it connection cannot be done.
Creating account:
$./geth –networkid=”121” –datadir private22333 –port 22333 ac-
count new
Figure 1.15: goethereum - Account creation Command
Ether to Wei. The gas given for the transaction is 30000. The command
eth.accounts[0] will give the hexadecimal address of the account which is in
0th position similarly for the 1st and others. Instead of giving eth.accounts[0],
directly user hexadecimal address can be provided.
sendTransaction:
>eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],
value:web3.toWei(40,”ether”), gas:30000})
Figure 1.18 shows the command to get the balance of an external account
address eth.accounts[0] and unlock the account eth.accounts[0] by providing
the passphrase. The geth command with admin.addP eer is to connect to
another peer in the network. The parameters are public key, ip address and
port of the remote peer node (enode : //pubkey@ip : port).
1.4 ZeroCash
ZeroCash is used for the implementation of Zcash cryptocurrency [32]. Ze-
roCash [30] is the first popular privacy preserving model implemented with
non interactive zero knowledge proof. In non zero knowledge proof based
cryptocurrency, if user Alice want to prove to Bob that she owns 30 coin
then Alice will simply sign a message using the key that controls the coin
and send to Bob. This leaks the information regarding owner of the coin. In
1.4. ZEROCASH 63
Get Balance:
>eth.getBalance(eth.accounts[0])
Unlock Account:
>personal.unlockaccount(eth.accounts[0])
Connect to Peers:
> admin.addPeer(”enode://f4642fa65af50cfdea8fa7414a5def7bb79
91478b768e296f5e4a54e8b995de102e0ceae2e826f293c481b5325f89be6d
207b003382e18a8ecba66fbaf6416c0@172.31.2.43:22331”)
Public Output: Zerocash has the facility to make the coin as public to
spend in other cryptocurrency.
make the private payment [7] that is transacting a coin from one to another
address. The decentralized anonymous payment scheme(DAP scheme) of
ZeroCash uses the following functions to make the anonymous transactions.
Minting Coins: To mint a coin, user will use the public parameters,
coin value {0, 1, ..., vmax } and destination address public key addrpk .
Pouring coins: It transfers value from input coins into new output
coins, marking the input coins as consumed. It uses public parame-
ters, old coin owner secret address addrsk , merkle tree root with its
authentication path, new public address addrsk and transaction fee to
generate the new coin.
Receiving Coins:User scans the ledger and retrieves unspent coins paid
to a particular user address using the address key pair.
1.5 Exercises
1. Perform the RLP decoding of [0x8b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
0x77, 0x6f, 0x72, 0x6c, 0x64]
3. What is the ethereum reward for the miner who includes two uncle
blocks?
66 CHAPTER 1. CRYPTOCURRENCY
4. What is the ethereum reward for the miner of the uncle block no.1000
when it gets added in the real block no.1004?
9. Compute the bitcoin epoch difficulty, where actual time for 2016 blocks
is 20000 minutes and old difficulty is 2222342.
10. What is the bitcoin value after 2000 block considering 50 bitcoin re-
ward?
13. Consider a bitcoin transaction with value 3BTC and transaction fee as
4BTC. What type of transaction it is?
14. Compute the Cache size of ethereum for block number is 15000. Can
the miner user 2GB GPU to mine the 15001 block?
15. How much memory space is required for the light node in bitcoin when
the number of blocks in the chain are 80000?
16. What is the problem with the frotier difficulty computation? Will it
show any difference in difficulty if timestamp difference is 1 or 12.
1.6. LAB PRACTICES 67
17. Assuming that the total hash power of the network stays constant, what
is the probability that a block will be found in the next 10 minutes?
18. Assuming that the total hash power of the network stays constant, what
is the probability that a block will be found in the next 10 minutes?
20. What factors affect the rate of orphan blocks? Can you derive a formula
for the rate based on these factors?
22. What is the probability, the attacker can replace the chain, if he is z
blocks behind the honest chain?
2. Practice the Python program 1.20 in the lab for cache size computation.
5. Write a smart contract for simple voting system with GUI and run in
goethereum private test network.
68 CHAPTER 1. CRYPTOCURRENCY
#! / u s r / b i n / env python
def i s p r i m e n u m b e r ( x ) :
i f x >= 2 :
for y in range ( 2 , x ) :
i f not ( x % y ) :
return F a l s e
else :
return F a l s e
return True
block number = 1
epoch = 30000
C I = 2 ** 24
C G = 2 ** 17
c = block number / epoch
C sz = C I + C G * c
C sz = C sz − 64
while not ( i s p r i m e n u m b e r ( C sz / 6 4 ) ) :
C sz = C sz − 128
print ( ” C sz=” , C sz )
#! / / b i n / env python
def i s p r i m e number (x ) :
i f x >= 2:
for y in range ( 2 , x ) :
i f not ( x % y ) :
return F a l s e
else :
return F a l s e
return True
block number = 1
epoch = 30000
D I = 2 ** 30
D G = 2 ** 23
c = block number / epoch
D sz = D I + D G * c
D sz = D sz − 128
while not ( i s p r i m e n u m b e r ( D sz / 1 2 8 ) ) :
D sz = D sz − 256
print ( ” D sz=” , D sz )
71
72 BIBLIOGRAPHY
[30] Ben-Sasson, E., Chiesa, A., Garman, C., Green, M., Miers,
I., Tromer, E., and Virza, M. Zerocash: Decentralized anony-
mous payments from bitcoin. http://zerocash-project.org/media/
pdf/zerocash-extended-20140518.pdf, 2014. [Online; accessed 07-
December-2018].
[31] Ben-Sasson, E., Chiesa, A., Tromer, E., and Virza, M. Suc-
cinct non-interactive zero knowledge for a von neumann architecture.
https://eprint.iacr.org/2013/879.pdf, 2015. [].
[35] Dwork, C., and Naor, M. Pricing via processing, or, combatting
junk mail, advances in cryptology. CRYPTO92: Lecture Notes in Com-
puter Science No. 740, Springer: 139147 (1993).
[39] Jakobsson, M., and Juels, A. Proofs of work and bread pudding
protocols. Communications and Multimedia Security, Kluwer Academic
Publishers: 258272 (1999).
[43] Miers, I., Garman, C., Green, M., and Rubin, A. D. Zerocoin:
Anonymous distributed e-cash from bitcoin. http://spar.isi.jhu.
edu/~mgreen/ZerocoinOakland.pdf, 2015. [].
BIBLIOGRAPHY 75
1 Introduction 3
1.1 IOTA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.1 Priliminaries . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.1.1 Encoding . . . . . . . . . . . . . . . . . . . . 7
1.1.1.2 Winternitz signature . . . . . . . . . . . . . . 8
1.1.1.3 Type of Nodes . . . . . . . . . . . . . . . . . 11
1.1.1.4 Snapshot . . . . . . . . . . . . . . . . . . . . 11
1.1.1.5 Peer Discovery . . . . . . . . . . . . . . . . . 12
1.1.2 Address Construction . . . . . . . . . . . . . . . . . . . 12
1.1.2.1 Attach address . . . . . . . . . . . . . . . . . 15
1.1.3 Transaction . . . . . . . . . . . . . . . . . . . . . . . . 16
1.1.3.1 Bundle . . . . . . . . . . . . . . . . . . . . . 17
1.1.3.2 Bundle Hash . . . . . . . . . . . . . . . . . . 17
1.1.3.3 Normalized Bundle Hash . . . . . . . . . . . . 20
1.1.3.4 Signature Computation . . . . . . . . . . . . 21
1.1.3.5 Signature Validation . . . . . . . . . . . . . . 21
1.1.3.6 Transaction Validation . . . . . . . . . . . . . 25
1.1.3.7 Transaction Confirmation . . . . . . . . . . . 26
1.1.4 Tip Selection . . . . . . . . . . . . . . . . . . . . . . . 27
1.1.4.1 Weighted Random Walk . . . . . . . . . . . . 28
1.1.4.2 Why Random? . . . . . . . . . . . . . . . . . 29
1.1.5 Masked Authenticate Messaging (MAM) . . . . . . . . 31
1.1.6 Possible Attacks . . . . . . . . . . . . . . . . . . . . . . 35
1.1.6.1 Double Spending . . . . . . . . . . . . . . . . 35
1.1.6.2 Parasite Chain Attack . . . . . . . . . . . . . 37
1.1.6.3 Splitting Attack . . . . . . . . . . . . . . . . 38
1.2 Corda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
1
2 CONTENTS
1.2.0.1 Services . . . . . . . . . . . . . . . . . . . . . 41
1.2.1 Network Permissioning . . . . . . . . . . . . . . . . . . 42
1.2.1.1 Certificate Hierarchy . . . . . . . . . . . . . . 42
1.2.2 Ledger . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
1.2.2.1 State . . . . . . . . . . . . . . . . . . . . . . . 43
1.2.2.1.1 State Update . . . . . . . . . . . . . 43
1.2.2.2 Vault . . . . . . . . . . . . . . . . . . . . . . 45
1.2.2.3 Transactions . . . . . . . . . . . . . . . . . . 46
1.2.2.4 Transaction chains . . . . . . . . . . . . . . . 49
1.2.2.5 Committing transactions . . . . . . . . . . . . 49
1.2.2.6 Transaction Validity . . . . . . . . . . . . . . 51
1.2.2.7 Reference states . . . . . . . . . . . . . . . . 51
1.2.2.8 Composite Key . . . . . . . . . . . . . . . . . 53
1.2.2.9 Hard Fork . . . . . . . . . . . . . . . . . . . . 55
1.2.2.10 Identity lookups . . . . . . . . . . . . . . . . 55
1.2.3 Oracle . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
1.2.3.1 Transaction Merkle Tree . . . . . . . . . . . . 56
1.2.3.2 Flow . . . . . . . . . . . . . . . . . . . . . . . 58
1.2.3.3 Consensus . . . . . . . . . . . . . . . . . . . . 60
1.2.3.4 Notary . . . . . . . . . . . . . . . . . . . . . . 61
1.2.3.5 Data distribution . . . . . . . . . . . . . . . . 62
1.2.3.6 compatibility zone . . . . . . . . . . . . . . . 63
1.2.3.7 Network parameters . . . . . . . . . . . . . . 63
1.2.3.8 Smart Contract . . . . . . . . . . . . . . . . . 64
1.2.3.9 Privacy . . . . . . . . . . . . . . . . . . . . . 65
1.3 Algorand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
1.3.1 How it works . . . . . . . . . . . . . . . . . . . . . . . 66
1.3.2 Features . . . . . . . . . . . . . . . . . . . . . . . . . . 67
1.4 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Chapter 1
Introduction
There are applications other than cryptocurrency look for the properties
of blockchain such as immutable data storage, decentralization, permitting
non-trusted users, etc. to ensure the security of the data and environment.
However, difficulties of blockchain implementation such as computational
power and storage are the hurdle for the users to implement in the application
especially in lightweight environment. Considering the problem, there are
different implementation proposed following the blockchain properties. This
section discuss about some of such implementations. This chapter is divided
into two sections
IOTA - The first section of the chapter discusses about the IOTA im-
plementation and the possible attacks with solutions
Corda - The third section discuss about the concept and its implemen-
tation.
1.1 IOTA
The popular blockchain implementations such as Bitcoin and Ethereum has
the drawback of transaction fee for transactions of any value. The micro-
payments in future will be more because of the growth of IoT industry, and
paying a transaction fee that is higher than the transaction amount seems
illogical. Also, in Blockchain implementations, it is not easy to get rid of
3
4 CHAPTER 1. INTRODUCTION
transaction fees because that is the incentive for miners of the blocks. IOTA
is the cryptocurrency that does not incorporate Blockchain Technology and
transaction fee. It is specifically designed for the Internet of Things (IoT)
industry [26]. The IOTA cryptocurrency is referred as mIOTA. In IOTA,
Directed Acyclic Graph (DAG) called as tangle is used instead of the global
blockchain. The transactions (called as site) issued by nodes (or users) make
a site set of the tangle graph, which is the ledger for storing transactions and
other messages. Following is the process to form the tangle.
When a new transaction arrives to the network, it must approve k
previous transactions. In the implementation k is 2.
(a) Answer: Alice transaction will be approved only when she approves the
existing transactions otherwise her transaction will be pending without
confirmation
(b) Answer: Alice should propagate the transactions regularly otherwise she
will not be part of the network that is her neighbors will drop her from
theirs neighbor list. Whether she make the transactions or not but to be
active member of the network she has to propagate the transactions.
Now the next question arise that is what is the issue if neighbors drop
her?Alice will not get the growing tangle and when she want to make, ap-
prove and propagate the transaction then she has to look for active node
of the network to help in finding the existing transactions to approve and
propagate her transactions to the network.
1.1.1 Priliminaries
IOTA is a peer to peer network and it was first conceptualized in 2014 and
later founded in 2015 by David Snsteb, Sergey Ivancheglo, Dominik Schiener,
and Dr. Serguei Popov [20]. The cryptocurrency was initially released on 13
June 2017 and the maximum IOTA token is 2779530283277761 [4]. IOTA
allows any user or node to join the network like Bitcoin and Ethereum. It has
the genesis transaction instead of genesis block in blockchain implementation.
The genesis transaction is the first transaction of the tangle created by one
of the founders and has an address with a balance that contained all of the
tokens. The genesis transaction sent these tokens to several other ’founder’
addresses. All of the tokens were created in the genesis transaction and no
tokens can be created in the future. A user wish to join the IOTA network
has to run the IOTA software and connect to the neighbors through the IP
address. User can create and issue the transaction after creating the seed,
private key and public address through a pre-defined protocol discussed in
the later sections. If any user wish to issue a transaction then he/she has to
approve or validate other transactions available in the tangle and his trans-
actions will be approved by other users. Transactions issued to the tangle
are called as transaction bundle, which contains set of transactions. Trans-
actions are validated in two directions: one is transaction data validation
that means Proof of Work (PoW), signature, etc. and second is the transac-
tion value and user account balance availability validation. Transactions will
6 CHAPTER 1. INTRODUCTION
Edge: The edge in the DAG (tangle) from a site A to site B means the
transaction bundle (site) A approves the transaction bundle (site) B.
Tip: Transactions that are not yet approved by any other transactions
in the tangle. In figure 1.1, the site X is the tip which does not have
any approver.
Height: The length of the longest path to the genesis transaction. For
example, in figure 1.2, the height of a site C is 4, F is 2, D is 3 and G
is 1.
Depth: The length of the longest reverse-oriented path to some tip. For
example, in figure 1.2, the depth of a site E is 1, D is 2 and F is 3.
1.1. IOTA 7
1.1.1.1 Encoding
The IOTA uses balanced trinary (trits) encoding scheme. Trinary is the three
bit system containing 0, 1, 2 (unbalanced) and −1, 0, 1 (balanced). IOTA uses
the balanced trinary encoding representation named as Trit (Trinary Digit)
and Tryte refers Trinary Btye. One tryte is 3 trits that is 33 = 27 values
possible for a tryte. Equation 1.1 computes the decimal value for the trits
(1, 0, −1).
−8 = 1 × 30 + 0 × 31 + (−1) × 32 (1.1)
The possible values in balanced trytes are between -13 and 13 and total
combination it can support is 27. To make the trytes human understandable,
8 CHAPTER 1. INTRODUCTION
IoTA team created the 27 tryte characters those are alphabets A to Z and
integer 9. Table 1.1 shows the trits and equivalent tryte alphabet and decimal
value.
Nodes of IOTA network can be any of the following types based on its com-
putational and storage infrastructure.
Perma Node: It is a full-node with the facility to store the entire tangle.
In IOTA, after every time interval, the sub tangle will be deleted after
taking the snapshot (account balance, address, etc) of the tangle. Full
node only store the snapshot and future transactions of the tangle but
the permanent node that is perma node stores entire history of the
Tangle that is from the genesis transaction to upto date transaction.
1.1.1.4 Snapshot
In blockchain, every full node is required to keep all growing blocks but IOTA
made the provision to store only the subtangle along with the snapshot.
The IOTA coordinator performs a signed snapshot once in a while. A new
snapshot deletes all transactions or sites from the Tangle and only stores
the balance of each address. A full node has to store the snapshot and all
subsequent transactions to validate the transactions. [17]
12 CHAPTER 1. INTRODUCTION
Table 1.2 shows the key size in the form of trits and trytes for different
security levels.
1
IOTA mesh net: Each full node only sees one tiny part of the Tangle through their
handful of neighbors. No one has a list of all IPs of all nodes.
1.1. IOTA 13
Security Level Key Size (in trits) Key Size (in trytes)
1 6561 2187
2 13122 4374
3 19683 6561
The process of computing private key, public key and address is shown in
algorithm 4. The private key is generated after repetitive hash computation
of the seed. In simple, let us assume the private key has 27 fragments in
security level 1 and the first fragment of the private key is hash of the sub-
seed, second fragment is hash of the first fragment, third fragment is hash
of the second fragment and so on upto the computation of 27 parts. The
public key is constructed by computing hash of each private key fragment
26 times and computing the hash of all fragments output hash. The public
key concatenated with the checksum (last 9 characters of hash of the public
key) forms the address. The size of address is 90 trytes including the 9 trytes
checksum. The reason for repeating 26 times instead of 27 is explained in
signature generation and validation.
The size of seed is 81 trytes and there is a possibility that two node can
generate the same seed. However, the chance of getting the same seed is very
low. For example, if Alice and Bob both have exactly last 69 or more trytes
equal then one can see the balance of one or more addresses of others. The
chance that Alice and Bob will have the same last 69 trytes of seed is very
small because huge combinations of trytes possible as indicated in equation
1.2.
Figure ?? shows the process of computing the address or public key based
on Algorithm 4.
Address Re-use: IOTA strongly suggests to use different address for every
transaction that is address once used cannot be repeated. Following are the
reasons not to re-use the address.
14 CHAPTER 1. INTRODUCTION
Key disclose in digital Signature - IOTA uses the Lamport based Win-
ternitz digital signature scheme. In Lamport digital signature scheme,
for every signing process part of the key will be disclosed. In case we
use the same key to sign two transactions then the key may be com-
promised. If the private key size is 512 bits then the chance of getting
key in first signature is 1/2256 , and in second signature with same key,
it will be 1/2128 and so on.
What is the need to attach the address separately when address get
attached automatically when we make non-zero value transaction?
Following are the reasons to attach user address to the tangle even the above
questions arise.
1.1.3 Transaction
Transactions are to transfer the IOTA coin (mIOTA) from one account to
another account also supports transfer of message between nodes with or
without coin value. IOTA supports the following transaction. [24].
Input Transaction - This is for the deduction of coin. The coin value
of this transaction will be deducted from the address provided. The
coin value in the transaction will be negative if it is spent for other user
otherwise it is greater than zero for transfer of coin among the owner
addresses.
Output Transaction - This is for the addition of coin. The coin value
of this transaction will be added to the address provided.
The transaction structure [6] is shown in table 1.3. In addition to the at-
tributes give in table 1.3, persistence is another attribute added in the im-
plementation to indicate whether the transaction is confirmed or pending. If
it is true, the transaction is confirmed otherwise it is pending.The transac-
tion hash will be computed after filling all the attributes that is from serial
number 2 to 15 of the table 1.3. For example, if we have 5 transactions, the
fifth transaction hash will be computed first then that will be used for the
fourth transaction trunk hash and so on.
1.1.3.1 Bundle
Bundle is a unit of transactions has the atomicity property, where all trans-
actions in the bundle will get executre or none of the transactions will get
executed. Figure 1.3 shows the structure of the IoTA bundle. Tips shown in
the figure 1.3 are bundles not yet confirmed by any other bundles of trans-
actions.
The trunkTransaction of the first index (currentIndex 0) transaction points
to the second transaction hash, second transaction trunkTransaction is point-
ing to the third transaction hash and so on. But the last transactions trunk-
Transaction points to one of the tip transactions, which is the branch trans-
action for all other transactions.
Address
Value
Obsolete tag
Timestamp
Current index
Last index
Using the Keccak 384 hash function, the bundle hash will be computed
as shown in equation 1.3
The computed bundle hash will be added into all transactions to confirm
that the transaction belongs to the respective bundle.
In case, sum is less than (<) 0 then it is biased on the lower side. To
balance it, take the first tryte of the fragment and increase the value
by one, compute the sum and check whether it is greater than or equal
0. If not repeat the process till first tryte equivalent decimal become
13. If the tryte decimal equivalent is +13 and sum not greater than or
equal to zero then move to the second tryte, third tryte and so on till
sum becomes greater than or equal to zero.
1.1. IOTA 21
In case, the sum is greater than or equal to zero then take the first
tryte and reduce the value by one upto −13 or till the fragment sum
less than 0. In case, the first tryte is −13 then take next tryte and
proceed.
The signature is verified using the normalized bundle hash derived from the
publicly available bundle hash in the transaction bundle. The signature
validation process is shown in the algorithm 8. In validation, each normalized
bundle hash trytes decimal equivalent is added with highest tryte value 13
and equivalent number of times signature fragment is hashed. The hashed
signature fragment will produce the address, which spends the coin. If the
address produced by the validation algorithm and the address available in
the transaction is same then the transaction signature is valid otherwise it is
invalid. If it is valid, the owner is spending the coin otherwise someone else
attempt to spend the coin.
22 CHAPTER 1. INTRODUCTION
During the tip selection process: This is the approval of the transaction
bundle. Each bundles on tip selection path are checked by the bundle
and ledger validator.
Reattach: Transaction which is part of the tangle but not yet approved
in 30 minutes then the chance of respective transaction is very less because
tip selection always prefers new transactions instead of older ones. Reattach-
ing the transaction will increase the chance of confirmation. Reattaching the
transaction means create a new transaction with the same signed bundle as
the original transaction but with new trunk and branch transaction. Two
random tips (trunk and branch) are picked again and added in to the new
transaction, also compute the Proof of Work for the new transaction and
replace the nonce. The following attributes of the new transaction will be
modified remaining will as it was in the old transaction.
Transaction Hash
Trunk hash
Branch hash
Attach timestamp
1.1. IOTA 27
Nonce
Reattach transaction can pave the path to double spend however only one
transaction will be confirmed. The other transaction(s) will be pending.
consuming. Then, once a result is found, the Tangle has already changed.
To achieve the first goal, we can perform a deterministic walk from the gene-
sis to the tips, always going towards the approver with the largest cumulative
weight. However, this would discourage the second goal because only a single
central chain of sites would get approvals and most transactions would be
left behind. Hence, the transition is made through the random walk.
eαHy
Pxy = P αHz
(1.5)
z:z x e
eαHA e2
PXA = = ≈ 0.24 (1.6)
eαHA + eαHB + eαHC e2 + e3 + e
eαHA e3
PXB = = ≈ 0.67 (1.7)
eαHA + eαHB + eαHC e2 + e3 + e
eαHA e
PXC = = 2 ≈ 0.09 (1.8)
eαHA +e αHB +eαH C e + e3 + e
The transaction C has low probability to get chosen than A or B because
of less weight. If we set smaller value to α, we decrease the bias against C.
For example, if we take α = 0.1, we get the following probabilities:
Public Mode - Any one in the network can read the message
Private Mode - A node in the network knows the merkle root of the
message can read it.
Restricted Mode - Node with the side key and merkle root can read
the message.
User wish to send message to the receiver, will mask the message and at-
tach in the tangle. In order to mask the message, following components are
required.
Side Key - The message is encrypted using the side key and the same
is used for decryption. It is needed to be shared in the restricted mode.
32 CHAPTER 1. INTRODUCTION
nextCount -It next merkle tree leaf count. When the message is created
two merkle trees are created: one is the current merkle tree and another
is the next message merkle tree. The nextcount is number of leaves in
the next merkle tree.
nextRoot - This is the root of the next merkle tree to access the next
message in the message chain.
Root - It is the root of the merkle tree. For example, in figure 1.6,
first merkle tree root is ABC. Merkle Root is computed using the hash
value of the child nodes as discussed in the Introduction chapter.
Figure 1.6 shows the masked message payload structure with the following
parameters.
Siblings - All the siblings of the leaf node to verify and compute the
root. For example, in figure 1.7, the siblings are given in blue color and
its value will be provided in this parameter .
34 CHAPTER 1. INTRODUCTION
The IOTA message bundles are created as per the following steps using
the components discussed before.
Step 3 : Compute the key (ki ) for each message using the seed, key
index and security level like private key computation as discussed in
the address construction.
Step 4 : Generate the address Ai for each message using the hash func-
tion - h(ki )
Step 5 : Generate the merkle tree using the available message addresses.
Merkle root is R.
Step 8 : Compute Nonce Ni using the hash function with the input
Sk , R, i + length(mi ) and mi . First 81 trits of the hash output is the
nonce.
Step 9 : Compute bundle hash Bhi using the hash function with the
input Sk , R, i + length(Mi ), mi and N .
1.1. IOTA 35
Step 10 : Compute the signature sigi for message mi using the key ki
and the computed bundle hash Bhi .
of one transaction. For example, transaction 5 in figure 1.8 would see the
conflict and not attach to the elected tips. Instead it would reselect tips until
it found to not conflicting ones in order to be sure itself turns into a valid
transaction. In case transaction 5 approves transaction w and y then future
transactions will not approve it.
It might happen that many transactions after w or y confirms those be-
fore the conflict becomes clear. However, at some point, either w or y will
get confirm, while the other gets abandoned. All subsequent transactions
approved or attached to the abandoned one will also be abandoned. In such
case, even the honest users transactions will be abandoned. To overcome such
issue, those transactions can be reattached to the tangle for a new chance of
confirmation.
In figure 1.8, the transaction 5 finds the conflict and decided to attach
to tips 1 and 4 as shown in figure 1.9. The transaction 7 attaches to tips
2 and 3. In this case, branches are arisen but only one can survive, due to
the double spend in w and y. Based on the random selection of tips, one
of the two branches will receive more child transactions for example users
transaction could just continue attaching to transactions 5, 6 and 8 but not
to transaction 7 anymore. Hence, transactions y, 2, 3 and 7 will never make
it into a fully confirmed state. Hence attacker could spend only once that
is transaction w and y will be abandoned. Transactions y, 2, 3 and 7 could
be reattached to the tangle again for fresh chance of confirmation however
transactions 2, 3 and 7 might become confirmed but y will stay invalid.
1.1. IOTA 37
in a random way.
fluctuation, the network will quickly choose one of the branches and aban-
don the other. In order to make the Markov Chain Monte Carlo algorithm
behave this way, initiate the random walk at a node with large depth using
a decaying function so that it is highly probable that the walk starts from
before the branch bifurcation in the tangle. In such case, the random walk
would choose the heavier branch with high probability, even if the difference
in cumulative weight between the competing branches is small.
There are other difficulty in performing this attack as discussed below
Less confirmation: The most recent transactions will only have around
50% confirmation confidence, and the branches will not grow. In such
case, the honest nodes may decide to start selectively giving their ap-
proval to the transactions that occurred before the split of tangle.
Own weight: Nodes may choose the subtangle, which has larger sum
of own weights. After choosing the subtange, the MCMC tip selection
algorithm be executed.
1.2 Corda
Corda is a platform, which implements the distributed ledger and data such
as transaction, state, etc. of an user is shared with other users only on a
need-to-know basis and no global broadcasts in the network [23]. This plat-
form is permissioned and specially designed for the financial sector. Any user
can join the Corda network after registering with the network operator. The
network operator is a trusted entity to provide specific service to the network
participants. The network operator issue a digital certificate containing the
user identity and other credentials to the participating user. Using the iden-
tity and available credential, nodes can make transaction with other nodes
in the network through the notary. The notary is the set of nodes to store
40 CHAPTER 1. INTRODUCTION
the sub-network global ledger and validate the transaction. The nodes need
to store only the transaction relevant to it not required to store all the trans-
actions sent in the network however nodes may request the notary and keep
all the transactions of the network. The notaries in the network will validate
all transactions to identify the double spend and maintain the decentral-
ized ledger. Nodes including notary can store the transaction details in the
relational database for efficient access. Unlike other blockchain implementa-
tion, in Corda transaction will be sent only to the respective node through
the notary using the IP address mapped with identity provided by Corda
mapping service. The Corda supports smart contract transaction along with
asset transaction. Also it ensures partial privacy by hiding the part of the
transaction whenever required. The important entities for the Corda imple-
mentation are as follows.
Identity: A node wish to take part in Corda should have single well-known
identity and it is used to represent the node in transactions, such as trans-
fer of value and creation of smart contract. Using network map service, the
node identity maps to an Internet Protocol (IP) address, which is used for
messaging between nodes.
1.2.0.1 Services
Corda has following different services to support the decentralized storage
and validation of transactions.
Network Map: Every Corda network has a network map service, which
is composed of multiple cooperating nodes. The network map publishes
the IP addresses of every node to the network, along with the identity
certificates of those nodes and the services they provide. An IP address
is used to connect the nodes and each node will publish one or more
IP addresses to the network through the network map service. The
domain name equivalent of IP address may be helpful for debugging
but are not required.
Root network CA: Root CA’s are implicitly trusted and the operator
of a network.
Own CA: Each node also serves as CA for itself to issue the child
certificates that it uses to sign its identity keys and TLS certificates.
Network CA and the certificate to validate the Own CA will be issued by the
Doorman CA. The certificate type can be identified using the integer identity
present in the certificate. The CAs must follow one of the following public
key cryptographic algorithm and curve to generate the certificate.
1.2.2 Ledger
The Corda decentralized ledger is different from other decentralized ledgers
such as Ethereum and Bitcoin. Each node maintains a separate database
containing its known facts or states not the complete network states or facts.
As a result, each node can see a subset of facts on the ledger, and no node
has the ledger with complete facts of the network [11]. For example, let us
assume a network with five nodes (Alice, Bob, Carl, Demi and Ed ) as in
figure 1.11, where each coloured circle represents a shared fact. The fact 1
is available only with Alice and Bob, the fact 3 is only with Carl, Demi and
Ed, similarly other facts.
1.2.2.1 State
A fact is called as state, which is an immutable object known by one or
more Corda nodes at a specific point in time. A states can contain arbitrary
data, allowing them to represent facts of any kind (e.g. stocks, bonds, loans,
KYC data, identity information, etc.) [12]. For example, the state in figure
1.12 represents an I Owe You (IOU) - an agreement that Alice owes Bob an
amount 10, due date is 01/03/2017 otherwise penalty of 20% and already
paid 5 euro.
amount 10, due date is 01/03/2017 otherwise penalty of 20% interest daily is
made as historic once the agreement is updated. The update occurs because
Alice have paid 5 euros to Bob. Now, Bob cannot claim with the previous
agreement.
1.2.2.2 Vault
In blockchain, nodes have a wallet, or as we call it, a vault. The vault
contains data such as private key or key derivation material of the owner and
stored in a form that can be easily queried [15]. Using vault, a Corda node
can track all the current and historic states that it is aware of, and which it
considers to be relevant to itself. The vault keeps track of both unconsumed
and consumed states:
Consumed (or spent) state: It represent ledger immutable state for the
purpose of transaction reporting, audit and archival.
46 CHAPTER 1. INTRODUCTION
Using the Corda vault, node can create transactions that send value to
someone else by combining fungible states and possibly adding a change
output that makes the values balance called as coin selection.
Soft Locking: A feature provides the ability to automatically prevent
multiple transactions within the same node trying to use the same output
simultaneously. Even though, such transaction will be detected by a notary,
soft locking provides a feature of early detection.
Reference: The Corda provides a facility for attaching descriptive textual
notes against any transaction stored in the vault.
Ledger types: On-ledger - Data refers to distributed ledger state (cash,
deals, trades) to which a firm is participant. Off-ledger - Data refers to a
node’s internal reference, static and systems data.
Fungible It means that an asset is interchangeable for any other identical
instance, and that they can be split/merged. For example a Rs.5 note can
reasonably be exchanged for any other Rs.5 note, and a Rs.10 note can be
exchanged for two Rs.5 notes, or vice-versa.
1.2.2.3 Transactions
The Corda transactions consume zero or more input states and create zero or
more new output states. Transactions are atomic, where all input and output
transactions will be executed or none of the transactions will be executed.
The attributes of the Corda transaction are listed in table 1.4.
Figure 1.14 shows a sample Corda cash issue transaction, which contains
zero input and one output, a newly issued cash state. The output cash state
1.2. CORDA 47
Name Identity
Input It is a hash and output index pair. Hash is
the input transaction hash and index point
to the input transaction, which will be spent
on this transaction.
Output states It is an output transactions, which includes
the contract and data.
Attachments Transactions has an attachment in the form
of zip file. Each zip file may contain code,
data, certificates or supporting documenta-
tion for the transaction. Contract code has
access to the contents of the attachments
when checking the transaction for validity.
In this field hash of the attachment will be
available as pointer to the zip files.
Commands It is a parameter to the contract that speci-
fies more information about the transaction.
Each command has an associated list of pub-
lic keys.
Signatures This is for the transaction owner’s signature
and the number of signature will be equal to
the union of commands public keys.
Type It refers the type of transaction. Transaction
may be of general or notary-change. The val-
idation rules are different for each type.
Timestamp This is to indicate the time on which the
transaction is generated.
Summaries This is to contain the information about
what the transaction does, checked by the
involved smart contracts, etc..
48 CHAPTER 1. INTRODUCTION
1) Details about the cash that has been issued. The details are amount,
currency, issuer, owner and issue date.
2) The contract code, for example verify() function, which is responsible
for verifying this issuance transaction.
3) A hash of a document which may contain legal prose. Figure 1.15
shows an example legal prose and its hash in the state.
The transaction contains a command, which specifies that the intent of
this transaction is to issue cash and the command specifies a public key. The
verify() function is responsible for checking that the public key specified on
the command is of the party whose signatures would be required to make this
transaction valid. In this case, it means that the verify() function must check
that the command has specified a key corresponding to the identity of the
issuer of the cash state. The Corda framework ensures that the transaction
has been signed by all keys listed by all commands in the transaction in
case of multiple commands and parties. It means, a verify() function needs
to ensure that all parties who need to sign the transaction are specified in
commands and the framework responsible for ensuring that the transaction
has been signed by all parties listed in all commands.
1.2. CORDA 49
To become reality, the transaction must receive signatures from all of the
required signers (see Commands, below). Each required signer appends their
signature to the transaction to indicate that they approve the proposal:If all
of the required signatures are gathered, the transaction becomes committed:
This means that:
The transactions outputs become part of the current state of the ledger
1.2. CORDA 51
The contracts for reference states are not executed for the transaction
containing them.
Why is this type of reference data required? In blockchain systems
everyone is sure they see the same as their counterpart - and for this to work
in situations where accurate processing depends on reference data requires
everybody to be operating on the same reference data.
52 CHAPTER 1. INTRODUCTION
Use Case: The KYC data can be distributed as reference data states and
only updatable by the data owner. However, it is usable by any party and
Notary ensures the data is current.
Regular contract states
Currently, the transaction model is too cumbersome to support reference
data as unconsumed states for the following reasons:
Contract verification is required for the ContractStates used as reference
data. This limits the use of states, such as Cash as reference data (unless a
special reference command is added which allows a NOOP state transaction
to assert no that changes were made.) As such, whenever an input state
reference is added to a transaction as reference data, an output state must
be added, otherwise the state will be extinguished. This results in long
chains of unnecessarily duplicated data. Long chains of provenance result in
confidentiality breaches as down-stream users of the reference data state see
all the prior uses of the reference data in the chain of provenance. This is an
important point: it means that two parties, who have no business relationship
and care little about each others transactions nevertheless find themselves
intimately bound: should one of them rely on a piece of common reference
data in a transaction, the other one will not only need to be informed but will
need to be furnished with a copy of the transaction. Reference data states will
likely be used by many parties so they will be come highly contended. Parties
will race to use the reference data. The latest copy must be continually
distributed to all that require it.
Attachments
Of course, attachments can be used to store and share reference data.
This approach does solve the contention issue around reference data as reg-
ular contract states. However, attachments dont allow users to ascertain
whether they are working on the most recent copy of the data. Given that
its crucial to know whether reference data is current, attachments cannot
provide a workable solution here.
The other issue with attachments is that they do not give an intrinsic
format to data, like state objects do. This makes working with attachments
much harder as their contents are effectively bespoke. Whilst a data format
tool could be written, its more convenient to work with state objects.
Oracles
Whilst Oracles could provide a solution for periodically changing reference
data, they introduce unnecessary centralisation and are onerous to implement
for each class of reference data. Oracles dont feel like an optimal solution
1.2. CORDA 53
here.
Keeping reference data off-ledger
It makes sense to push as much verification as possible into the contract
code, otherwise why bother having it? Performing verification inside flows is
generally not a good idea as the flows can be re-written by malicious develop-
ers. In almost all cases, it is much more difficult to change the contract code.
If transaction verification can be conditional on reference data included in
a transaction, as a state, then the result is a more robust and secure ledger
(and audit trail).
Versioning
This can be done in a backwards compatible way. However, a minimum
platform version must be mandated. Nodes running on an older version of
Corda will not be able to verify transactions which include references. Indeed,
contracts which refer to references will fail at run-time for older nodes.
Privacy
Reference states will be visible to all that possess a chain of provenance
including them. There are potential implications from a data protection
perspective here. Creators of reference data must be careful not to include
sensitive personal data.
Use of Composite keys: For example, an assets can be placed under the
control of a 2-of-2 composite key where one leaf key is owned by a user, and
the other by an independent risk analysis system. The risk analysis system
refuses to sign if the transaction made by a user seems suspicious, like if too
much value has been transferred in short span.
1.2.3 Oracle
There will be requirement sometimes to reveal a small part of a transaction
to a counter-party to allow them to check the signatures and sign it. A
typical use case for such case is an oracle, which is a network service that is
trusted to sign transactions containing statements only if the statements are
true. The oracle might check statements like as follows.
This can be done by the smart contract also instead of doing through
an Oracle service. The reason is that all calculations on the ledger must be
deterministic. In the network, everyone must be able to check the validity of
a transaction and arrive at exactly the same answer, at any time, on any kind
of computer. If a smart contract could do things like read the system clock or
fetch arbitrary transaction then it would be possible for some computers to
conclude a transaction was valid, however others concluded it was not (e.g.
if the remote server had gone off line). The solution to solve this problem is
that all the data needed to validate the transaction must be in the ledger so
that there cannot be disagreement among the nodes on a transaction.
An oracles would check the transaction and sign a small data structure
which is then embedded somewhere in a transaction. An oracle uses the
filtered transaction approach to turn off or tear off the unrelated parts of
the transaction before the transaction is sent. It means, the transaction part
which should not be seen by a node will be hided to ensure the privacy.
This is achieved by structuring the transaction as a Merkle hash tree and the
root hash will be used for signing operation. The counterparty can sign the
entire transaction however only being able to see some of it. Additionally,
if the counterparty needs to be convinced that some third party has already
signed the transaction, that can be verified by supplying the necessary path
or branch values to compute the root hash. An oracle with the counterparty
will get the Merkle branches of the command or state that contains the data,
and the timestamp field, and nothing else to validate the transaction.
the identifier of the transaction and it will be used for signing and verification
of data integrity. With merkle property, if any change in a transaction leaf
level then it will change the transaction identifier.
Using merkle tree, hide data and provide the proof by constructing partial
Merkle trees that is Merkle branches. A Merkle branch is a set of hashes
computed from the leaves data, is used to calculate the root hash. The
calculated hash is compared with the hash of a whole transaction and if they
match it means that data we obtained belongs to that particular transaction
otherwise not. Let us see an example of hiding data at the same time verifying
the identity of the transaction. We take the case that only the first command
of the transaction and all signers should be visible to an Oracle from the
figure 1.19. It should provide guarantees that all of the commands requiring
a signature from this oracle should be visible to the oracle entity, but not the
rest. The filtered transaction for the oracle is shown in figure 1.19.
The node c1 shown in blue needs the signature of the Oracle. Hence blue
node and H(c2) are provided to the Oracle service, while the black ones are
omitted. The H(c2) is required for the Oracle to compute H(commandData)
without being to able to see the second command, but at the same time ensur-
ing CommandData1 is part of the transaction. Also all signers (CommandSigners1
and CommandSigners2) are visible, so as to have a proof that no related
command has been maliciously filtered out for the node Oracle. Addition-
ally, hashes of sub-trees that is in violet colored nodes are also provided.
With these data, Oracle can calculate the root of the top tree and com-
pare it with original transaction identifier. If it matches then this command
CommandData1 and time-window belong to this transaction.
58 CHAPTER 1. INTRODUCTION
1.2.3.2 Flow
In Corda, transaction data is not globally broadcast instead it is transmitted
only to the relevant parties according to the requirement. For example, a
coin transaction may involve counter parties and the third party such as a
notary. Unlike traditional blockchain systems in which the primary form
of communication is global broadcast, in Corda all communication takes the
form of small multi-party sub-protocols called flows. The Corda flow provides
the following service to distribute the transactions to update the ledger.
Identity to IP address mapping: The transactions are constructed using
the identities. The Corda flow framework takes care of routing messages
(transactions) to the right IP address for a given identity.
Progress reporting: The Corda flow can provide a progress tracker that
indicates which step they are up to. Steps can have human-meaningful
labels, along with other tagged data like a progress bar. Progress
trackers are hierarchical and steps can have sub-trackers for invoked
sub-flows.
Flow hospital : Transactions can be paused by Corda flow, if transaction
validation throw exceptions or explicitly request human assistance. A
flow that has paused will be available in the flow hospital storage, where
the node’s administrator may decide to kill the flow or provide it with
a solution based on the transaction correctness.
The automated process of Corda flow is shown in figure 1.21. The user
Alice sends a transaction to her counter party Bob. The transaction is sent
1.2. CORDA 59
from Alice after adding the signature. On the receipt of a signed transaction,
Bob verify the transaction, sign, commit and send it to Alice. Alice verifies
the received signed transaction and commit it. The commit in the flow
indicates the update of ledger. The figure 1.21 does not include the notary
for transaction verification and commit.
1.2.3.3 Consensus
Determining whether a proposed transaction is a valid ledger update involves
reaching two types of consensus:
Validity consensus
Validity consensus is the process of checking that the following conditions
hold both for the proposed transaction, and for every transaction in the
transaction chain that generated the inputs to the proposed transaction:
The transaction is accepted by the contracts of every input and output
state The transaction has all the required signatures
It is not enough to verify the proposed transaction itself. We must also
verify every transaction in the chain of transactions that led up to the creation
of the inputs to the proposed transaction.
This is known as walking the chain. Suppose, for example, that a party
on the network proposes a transaction transferring us a treasury bond. We
can only be sure that the bond transfer is valid if:
The treasury bond was issued by the central bank in a valid issuance
transaction Every subsequent transaction in which the bond changed hands
was also valid
The only way to be sure of both conditions is to walk the transactions
chain. We can visualize this process as follows:
When verifying a proposed transaction, a given party may not have every
transaction in the transaction chain that they need to verify. In this case,
they can request the missing transactions from the transaction proposer(s).
1.2. CORDA 61
The transaction proposer(s) will always have the full transaction chain, since
they would have requested it when verifying the transaction that created the
proposed transactions input states.
Uniqueness consensus
Imagine that Bob holds a valid central-bank-issued cash state of 1,000,000.
Bob can now create two transaction proposals:
A transaction transferring the 1,000,000 to Charlie in exchange for 800,000
A transaction transferring the 1,000,000 to Dan in exchange for 900,000
This is a problem because, although both transactions will achieve validity
consensus, Bob has managed to double-spend his USD to get double the
amount of GBP and EUR. We can visualize this as follows:
1.2.3.4 Notary
Notary is a service that provides transaction ordering and timestamping. It
is composed of multiple mutually distrusting parties, which uses standard
consensus algorithm. Notaries accept transactions submitted to them for
processing and return a signature over the transaction if it is valid, or a
rejection error that states that a double spend attempt has occurred. Every
62 CHAPTER 1. INTRODUCTION
state or transaction has an appointed notary cluster, and those notary cluster
will only notarise a transaction if it is the appointed notary cluster of all the
transactions input states.
Multiple notaries
Each Corda network can have multiple notary clusters, each potentially
running a different consensus algorithm. Multiple notaries provides the fol-
lowing benefits.
1.2. CORDA 63
When a node would prefer to use a different notary cluster for a given
transaction due to privacy or efficiency concerns
example adding a notary, setting new fields that were added to enable smooth
network interoperability, or a change of the existing compatibility constants
is required, etc. If the network operator wish to update the parameter then
the network map service starts to advertise the additional information with
the usual network map data. An additional information includes new net-
work parameters hash, description of the change and the update deadline.
After the advertisement, nodes query the network map server for the new set
of parameters.
The node administrator can review the change and decide if they are
going to accept it. The approval should be sent before the update Deadline.
Nodes that do not approve before the deadline will likely be removed from
the network map by the zone operator, but that is a decision that is left
to the operator’s discretion. For example the operator might also choose to
change the deadline instead. The new parameters advertised will overrides
the previous set that means only the latest update can be accepted.
State objects - It is the data held on the ledger, which represent the
current state of an instance of a contract, and are used as inputs and
outputs of transactions
paper contract in figure ??, states are issued by a legal entity which wishes to
create a contract to pay money in the future (the maturity date), in return
for a lesser payment now. They are then transferred to another owner as part
of a transaction where the issuer receives funds in payment, and later that is
after the maturity date it is destroyed (redeemed) by paying the owner the
face value of the commercial paper.
Contract Sandbox: In Corda, transaction verification must be deter-
ministic that is a contract should either always accept or always reject a
given transaction. For example, transaction validity should not depend on
the time at which validation is conducted, or the amount of information the
peer running the contract holds. This is a necessary condition to ensure that
all peers on the network reach consensus regarding the validity of a given
ledger update. In future version, Corda will follow the deterministic sand-
box. The sandbox will have the white list that prevents the contract from
importing libraries that could be a source of non-determinism. For example,
these libraries includes the current time, random number generators. Eventu-
ally, the information available to the contract when verifying the transaction
is the information included in the transaction itself.
1.2.3.9 Privacy
Corda improves user privacy over other distributed ledger systems
Graph pruning: Large transaction graphs that involve liquid assets can
be pruned by requesting the asset issuer to re-issue the asset onto the
66 CHAPTER 1. INTRODUCTION
Mix networks: There are chances that some users or nodes learns about
transactions that are not directly related to trades they are doing, for
example notaries or regulator nodes. Even though, key randomisation
is used, these nodes can still learn valuable identity information by
simply examining the source IP addresses or the authentication certifi-
cates of the nodes sending the data for notarisation. The traditional
cryptographic solution to fix this problem is mix network and the most
famous Tor, but a more appropriate design for Corda would be that
of an anonymous remailer. In a mix network, a message is repeatedly
encrypted like a layer using keys owned by a small set of randomly
selected nodes. Each layer contains the address of the next hop in the
network. The first hop decrypts the message first layer and reveal the
next encrypted layer and forwards it to second hop. The second hop
decrypts the message second layer and reveal the next encrypted layer
and forwards it to the next and similarly till the destination. Adding
mix network in Corda protocol may upgrade user privacy.
1.3 Algorand
Algorand is an implementation of blockchain based cryptocurrency and uses
Byzantine Agreement (BA* - ’*’ is added because of scalability) to achieve
the distributed consensus [22]. It confirms the transaction faster than other
cryptocurrencies, which uses Proof of Work (PoW). Also it is scalable to
many users and ensures no fork on the chain. This section gives the brief
overview of Algorand.
user’s private key transferring money to another user’s public key. Algorand
grows the blockchain in asynchronous rounds like Bitcoin and Ethereum. In
every round, a new block, containing a set of transactions and a pointer to the
previous block, is constructed and appended to the blockchain. The eligible
users identified through a Verifiable Random Function (VRF) [25] generate
the blocks with the available pending transactions and communicate with
other users in the network through a gossip protocol. After receiving the
blocks, eligible users run the Byzantine Agreement to reach the consensus
on one of the blocks, which will be appended with the blockchain. In our
discussion, Byzantine Agreement (BA∗) is used as BA for better represen-
tation.
1.3.2 Features
Algorand is the energy efficient and fast confirmation cryptocurrency. It
utilized different features to achieve the efficiency. The features used in the
implementation of the Algorand are discussed in the following.
Byzantine Agreement: A group of nodes needs to agree on some value.
The protocol needs to
– (a) Terminate within finite time.
– (b) Have all honest nodes reach the same result.
– (c) The result reached must match the input of one of the nodes.
Gossip protocol : Like Bitcoin, Algorand implements a gossip network
where each user selects a small random set of peers to gossip messages
to. To ensure messages cannot be forged, every message is signed by the
private key of its original sender; other users check that the signature
is valid before relaying it. To avoid forwarding loops, users do not relay
the same message more than once until it is required.
Weighted users: In Algorand, every user is assigned with a weight.
The byzantine agreement protocol is designed to guarantee consensus
as long as a weighted fraction (a constant greater than 2/3) of the
users are honest. Users are weighted based on the money in their
account. An adversary can also participate in Algorand and own some
money. In order to successfully attack Algorand, the attacker must
invest substantial financial resources in it.
68 CHAPTER 1. INTRODUCTION
Block Agreement: Above Block proposal does not guarantee that all
users received the same block. To reach consensus on a single block,
users again go for cryptographic sortition to check whether they have
been selected as committee members in that step. The Committee
members then broadcast a message which includes their proof of se-
lection and these steps repeat until, in some step, enough users in the
committee reach consensus on a Block.
1.4 Exercise
1.
70 CHAPTER 1. INTRODUCTION
Bibliography
[6] What is the iota transaction data structure? [Online; accessed 18-April-
2019].
71
72 BIBLIOGRAPHY
[19] Cullen, A., Ferraro, P., King, C., and Shorten, R. Distributed
ledger technology for iot: Parasitechain attacks. [Online; accessed 26-
April-2019].
[22] Gilad, Y., Hemo, R., Micali, S., Vlachos, G., and Zeldovich,
N. Algorand: Scaling byzantine agreementsfor cryptocurrencies. [On-
line; accessed 21-May-2019].
1 Smart Contracts 3
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.1 Smart Contracts . . . . . . . . . . . . . . . . . . . . . 4
1.1.2 Applications of Smart Contracts . . . . . . . . . . . . . 9
1.2 Ethereum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.2.1 Life Cycle of a Smart Contract . . . . . . . . . . . . . 15
1.2.2 Solidity and Smart Contracts . . . . . . . . . . . . . . 20
1.2.3 Decentralized Applications . . . . . . . . . . . . . . . . 23
1.3 Hyperledger Fabric . . . . . . . . . . . . . . . . . . . . . . . . 26
1.3.1 Smart Contracts in Hyperledger Fabric . . . . . . . . . 27
1.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1
List of Figures
2
Chapter 1
Smart Contracts
3
4 CHAPTER 1. SMART CONTRACTS
1.1 Introduction
A key aspect of a Blockchain is that it represents a decentralized ledger
system, which exists between the transacting parties. Thus, there is no
requirement to pay intermediaries, thereby saving time, money and conflict.
While Blockchains have operational constraints, they are more economical
and secure than traditional systems, which is why banks and governments
are adopting them [6, 14].
Nick Szabo [26], a legal scholar and cryptographer, is widely credited for
inventing the idea of a smart contract [37]. He realized that the decentralized
ledger system could be used for enabling self-executing, digital contracts (or
smart contracts), which are ‘a set of promises, specified in the digital form,
including protocols within which the parties perform on these promises’.
These contracts, which is legal tender between the parties that implement
them, could be implemented as computer programs, stored and replicated on
the decentralized ledger, and supervised by the network of computers that run
the Blockchain. Such a framework would result in an automated exchange
of good and services (having digital value) in a transparent, conflict-free way
while avoiding expensive intermediaries.
Figure 1.1: A simple model for smart contracts (adapted from [1]).
3. It performs automatically and its actions are irrevocable, i.e., once ini-
tiated, the outcomes for which a smart contract is encoded to perform
cannot be stopped (unless an outcome depends on an unmet condition).
2. Let us take a little more complex, real-world example and see how
smart contracts can help. Consider that you need to sell a property of
your own. The process of selling properties demands a lot of paper-
work as well as communication with multiple parties. Other than the
communication complexity, it also involves the risk of frauds.
Today, most people who want to deal in properties make their way
ahead through real-estate agents. These agents are responsible for deal-
ing with the paperwork and markets. They act as intermediaries in the
overall process and work on negotiations and overseeing deal. In such
cases, you cannot simply rely on the person that you are dealing with,
and therefore, the agencies provide escrow services which transfer the
funds from one party to the other. When the deal is finalized, you will
have to pay both, the agent and the escrow service their commission in
terms of the decided percentages. This leads to an extra loss of money
and more risk on the seller’s end.
However, in such situations, using smart contracts can result in more
effectiveness by reducing the burden. Smart contracts are designed
to work on rule-based principles (often encoded as the standard asyn-
chronous event model coupled with the if-then-else ladder), which
will resolve the ownership issue by transferring it to the buyer only when
the monetary, as well as other conditions, are agreed upon. Moreover,
when it comes to escrow services, smart contracts can replace those
too. Both money and the right of possession of the property can be
stored in a distributed system, which can be viewed by the involved
parties in real-time. Since the money transfer will be witnessed by all
the network participants, the chances of fraud are eliminated. More-
over, there is no chance of an intermediary to be involved as the trust
between parties is not an issue anymore. All the functions performed
by the estate agent can be coded into the smart contract, thus, saving
a considerable amount of money on both, buyer and seller end.
eral applications other than cryptocurrency. Over the years, Blockchain has
evolved and its use cases are coming forward in different industries.
Smart contracts can be termed as the most utilized and successful appli-
cations of the Blockchain technology in the current times. As listed earlier,
use of smart contracts in place of traditional contracts can significantly re-
move the transaction overheads. All modern Blockchain platforms such as
Ethereum [12] and Hyperledger Fabric [23, 31] support creating smart con-
tracts. They allow the use of Turing-complete languages to enable developers
to quickly script powerful smart contracts that are customized to the devel-
oper’s needs or target an industry use-case. Modern use cases for smart
contracts have spanned different industries and fields such as smart homes,
e-commerce, real-estate and asset management etc. Below we discuss a few
major use cases.
Government
With the increased sophistication in technology, it has become extremely
hard to provide a tamper-resistant governance mechanism. One of the most
critical government activity is to enable fair elections. However, there have
been documented attacks on our voting system [8,17,35], which is now consid-
ered to be insecure and elections are often deemed to be rigged. Blockchain-
based smart contracts allay such concerns by providing a provably more
1.1. INTRODUCTION 11
Management
Supply Chain
Supply chains are often hampered by the numerous approvals that are re-
quired for goods to pass from one intermediary to another. Since, majority
of these approvals often rely on manual, paper-based mechanisms, it exposes
the system to huge amounts of delays and subsequent losses (and even frauds)
incurred due to these delays. Blockchain-based smart contracts, in principle,
eliminate such issues by providing a secure, accessible digital version to all
parties on the chain and automates the tasks and payment. Further, since
smart contracts work on rule-based principles, they also defend against any
tampering and fraud attempts.
There are several examples where Blockchain’s have revolutionized supply
chains across various domains. Let us consider two examples.
12 CHAPTER 1. SMART CONTRACTS
Real Estate
As explained earlier in § 1.1.1, smart contracts can ease handling of physi-
cal goods and assets, like real-estate. The challenges facing the real-estate
industry include few property offerings, the dearth of investors investing in
homes, home price increases, and a lack of transparency in the marketplace,
among other concerns. One major benefit of Blockchain technology is the
idea that tokens can represent physical assets rather than just items that
have no existence offline. For example, if we wanted to rent an apartment,
today we need to pay an intermediary to (a) advertise the property, (b) pre-
pare the legal documents, and (c) confirm that rent has been paid on time
every month. Blockchain-based smart contracts can cut such costs. However,
with smart contracts and Blockchain, it can all be streamlined and done in
a secure and trusted manner. As a result, several governments, including
the European Union and Dubai, have initiated Blockchain-based real-estate
initiatives [5, 10].
Healthcare
While compliance in the pharmaceutical supply chain, and tracking authen-
ticity of returned drugs, etc., seem obvious applications of the Blockchain
technology to the healthcare industry, other applications such as transparency
and traceability of clinical trials and improving patient healthcare are also
1.2. ETHEREUM 13
1.2 Ethereum
Smart Contracts
Accounts
Transactions
A transaction is a digitally signed message that contains details of the sender
of the transaction, its recipient, the amount of ether sent, an optional data
field, a gas limit, and a gas price field.
Messages
A message is similar to a transaction except that is sent from one contract
to another contract, and resembles an ordinary function call in imperative
programming languages. A message is created when the contract account
executes a specific opcode, and its invocation typically modifies the storage
state of the contract.
Ether
Similar to Bitcoin, Ethereum uses its own native currency called Ether, which
can be exchanged against other fiat currencies via various cryptocurrency
exchanges. It is primarily used to pay for invocation of publicly defined
methods in a smart contract.
Gas
Gas is used to pay transaction fees in Ethereum. It is not a currency, but
an internal pricing unit that decouples the market price of the ether from
the cost of transactions. Gas price can be defined by the originator of a
transaction and miners decide whether they want to include the transactions
in their blocks or not. The more lines of code a smart contract executes and
the more memory and storage it uses, the higher is the gas requirement.
words, every node in the entire distributed network performs every program
executed on the platform.
The EVM running on each node in the Ethereum Blockchain can be
thought of as a global 256-bit distributed, decentralized computer contain-
ing millions of executable objects, each with its own permanent data store,
and where all transactions execute atop local nodes in relative synchrony. It
handles smart contract deployment and execution. Specifically, a smart con-
tract runs on the EVM, which in turn runs on each node in the Blockchain
network. While powerful in its own right, the EVM operates at the level
of bytecodes (like the JVM or Java Virtual Machine) and is not convenient
to directly program against. Thus, smart contracts are typically written
in a high-level language, such as Solidity. However, to execute, it must be
compiled down to the low-level EVM bytecode [13]. Specifically, Ethereum
provides the developers with a runtime that support several high-level lan-
guages to write smart contracts encoding operational logic for digital/logical
representation of assets expressed as data or program variables in code) and
their subsequent transfer to other entities upon code execution.
A smart contract can only execute if it is invoked by a transaction from a
user account. In other words, a contract can invoke another contract creating
multiple levels of the invocation call chain. However, the first contract in
such a contract call chain always belongs to a transaction initiated by a user
account. Note that the Ethereum Blockchain transactions are atomic (much
like their database counterparts), i.e., they execute in their entirety, with
any changes in the global state (contracts, accounts, etc.) recorded if and
only if all execution terminates successfully, regardless of the depth of the
contract call chain. A successful termination means that the smart contract
execution proceeded without any exception. If execution fails, all of its state
changes are reverted. A key difference between database transaction and the
Ethereum Blockchain transaction is that failed transactions leave a trace.
In other words, they are still recorded as having been attempted, and the
gas consumed for any contract computation is deducted from the originating
account.
A smart contract is immutable, i.e., it cannot be modified upon deploy-
ment. The exact process of deployment occurs using a special contract cre-
ation transaction and is described later in § 1.2.1. However, a smart contract
can be deleted (only if it has been designed to do so), i.e., removing the code
and internal state (storage) from its address, leaving a blank account. Any
transaction sent to this deleted contract’s address will not result in any code
1.2. ETHEREUM 17
execution since there is no executable code at that address. Note that delet-
ing a contract does not remove the transaction history of the contract since
the Blockchain itself is immutable. To delete a smart contract, the EVM
provides a special bytecode instruction, which if correctly handled within
the smart contract code upon execution issues a gas refund. This refund is
akin to release of network client resources upon the deletion of stored state.
If the smart contract does not handle the concerned opcode, the resources
cannot be recovered.
the Ethereum world state at the time of deployment, setting values in the
contract’s storage and even sending ether or creating further new contracts.
When a transaction results in smart contract execution, an EVM is in-
stantiated with all the state corresponding to the current block being created
and the specific transaction being processed. Specifically, the smart con-
tract’s code is loaded into the EVM’s ROM and the stack program counter
is set to zero. Additionally, the storage is loaded from the contract account’s
storage, the memory is initialized, and the corresponding block and envi-
ronment variables are set. Finally, the gas for the transaction is set to the
amount of gas paid for by the sender. As the smart contract code execution
proceeds, this gas is reduced in accordance with the cost of the bytecode
being executed currently. If at any state the gas drops to zero, the EVM
first halts the smart contract execution by throwing an “Out of Gas” (OOG)
exception, and subsequently aborts the transaction.
Note that since all computation is bounded, the EVM is a “quasi” Turing-
complete state machine. In other words, since a smart contract execution is
subject to a finite number of computational steps limited by the amount of
gas available, there is not execution where the EVM runs forever, thereby
avoiding the “halting problem” by design.
Each smart contract executes within a sandboxed copy of the Ethereum
world state. No changes to the Ethereum state are applied until the transac-
tion commits successfully, except for any updates to the sender’s nonce and
the gas balance to reflect the code execution. If a transaction aborts, the
EVM discards all changes to the Ethereum world state made within the sand-
box. however, if the transaction completes successfully, then the real-world
state is updated to match the sandbox’s version, including any changes to
the contract’s storage data, any new contracts created, and any ether balance
transfers that were initiated.
Note that because a smart contract can itself initiate new transactions,
code execution is a recursive process. A contract can call other contracts,
with each call resulting in another EVM being instantiated around the new
target of the call. Each instantiation has its sandboxed world state initialized
from the sandbox of the EVM at the level above. Each instantiation is also
given a specified amount of gas for computation (not exceeding the amount of
gas remaining in the level above, of course). In case a nested EVM sandbox
encounters an exception and subsequently halts the execution, the sandbox
state is discarded, and execution returns to the EVM at the level above.
20 CHAPTER 1. SMART CONTRACTS
Solidity Basics
Solidity has functionalities similar to C and JavaScript. It supports state
variables, general data types, along with the variable data types. It also
supports the generic value types, namely:
• String: Solidity has limited support for Strings, which can be repre-
sented using either single or double quotes. Unlike C, Solidity string
literals do imply trailing value zeroes. For instance, “bar” will repre-
sent a three byte element instead of four. Similarly, integer literals are
convertible inherently using the corresponding fit, i.e., byte or string.
Solidity also provides basic arrays, enums, operators, and hash values to
create a data structure known as “mappings”. Arrays can be both statically
and dynamically allocated.. For example, uint[][6] initializes a dynamic
array with 6 contiguous memory allocations. Similarly, a two-dimensional
array can be initialized as arr[2][4], where the two indices point towards
the dimensions of the matrix.
(24) }
Line 5 declares the contract’s constructor that is invoked once when the
contract is deployed on to the Blockchain. Specifically, the constructor’s
execution instantiates an initializes an array of candidates who will be con-
testing in the election. Line 8 describes the function totalVotesFor that
returns the total votes a candidate has received so far. Line 12 describes the
voteForCandidate function that increments the vote count for the specified
candidate, which is equivalent to casting a vote. Finally, lines 16 to 23 list the
validCandidate function that is responsible for sanity checking whether the
candidate is a legitimate candidate or not. It compares the given candidate
with candidateList, which stores the list of all candidates.
1.2. ETHEREUM 23
a wide network, with each node (in principle) owned by a different entity.
The higher the number of entities owning nodes of the network, the more
trustful the network in its entirety is. Conversely, networks owned only
by a few owners cannot be considered trustful because they do not truly
decentralize the processing. A centralized application is generally distributed,
but decentralized applications can also be distributed over multiple servers
within each logical node.
not store it into their local ledgers and thus prevent the tampered vote
from being registered on the Blockchain, thereby rendering the mali-
cious modification ineffective.
• Security: If an attacker were to modify the votes on one server in the
network, the alteration would be spotted and rejected by other partici-
pants, as seen earlier. A truly successful modification attempt requires
the support of at least 51% of the nodes in the network to achieve con-
sensus on the modified state of the votes. As is obvious, manipulating
a large fraction of the decentralized network of independently managed
nodes is an incredibly challenging task, thereby ensuring security.
1. The user selects one of the possible voting options from a dropdown
list box on the web client and then clicks the Vote button.
2. The click event is handled by a JavaScript function that captures
the voting selection, and subsequently, through various third-party
JavaScript library functions set up communication with an Ethereum
configured node, and invokes the voteForCandidate function in the
voting smart contract. The invocation of the voteForCandidate func-
tion generates a transaction message, which is digitally signed by the
user’s private key.
3. The contacted local Ethereum node broadcasts the transaction message
to other peers and miners in the network for validation.
4. Based on the transaction metadata, a miner will determine whether it
wants to generate a block corresponding to this transaction or not. If
yes, then the miner executes the code of the function and create a new
block. However, the miner will be able to propagate this block in the
network only if beats other miners in solving a proof-of-work puzzle.
If it does win the race, the miner relays the new block to all its peer
nodes.
26 CHAPTER 1. SMART CONTRACTS
5. Eventually, the local Ethereum nodes receive this new incoming block
and synchronize its local copy of the Blockchain. Upon receiving the
new block, each local node executes all the transactions in the block.
One of these is the voting transaction, which upon successful comple-
tion has been programmed to raise a callback event. This vote confir-
mation event is published to all the clients subscribed to it, including
the DApp web UI.
6. The JavaScript code present on the voting web client itself contains a
callback function registered against the vote confirmation event, which
then gets triggered. Finally, the callback function shows a vote confir-
mation notification on the voter’s screen.
the transaction was accepted or rejected. If the transaction was accepted, the
smart contract layer also returns an attestation of correctness, a state delta,
and any optional ordering hints needed to ensure the transaction dependen-
cies are taken into account. The state delta includes the change sets and
any side effects that should take place when the transaction is successfully
committed by the peers.
1.4 Summary
A smart contract is essentially business logic running on a Blockchain. It
can simply update an account balance, or be more complex to manage and
validate the entire supply chain for a particular product. While public adop-
tion of Blockchain has been spearheaded by permissionless platforms like
Ethereum (and Bitcoin), adopting Blockchain in an enterprise will require
a balancing act. Organizations will not only have to run, but also manage,
and maintain their existing infrastructure. Hyperledger Fabric is one promis-
ing Blockchain platform for the permissioned enterprise scenarios, which has
gained wide prominence with both its modular architecture and ease of de-
veloping smart contracts in general purpose languages.
Bibliography
[3] Bext360 and Coda Coffee Release The Worlds First Blockchain-traced
Coffee from Bean to Cup. https://globenewswire.com/news-
release/2018/04/16/1472230/0/en/bext360-and-Coda-Coffee-
Release-The-World-s-First-Blockchain-traced-Coffee-from-
Bean-to-Cup.html.
[5] Blockchain Used to Sell Real Estate for the First Time in the
EU. https://www.prnewswire.com/news-releases/blockchain-
used-to-sell-real-estate-for-the-first-time-in-the-eu-
808847530.html.
34
BIBLIOGRAPHY 35
[8] Did Russia Affect the 2016 Election? Its Now Undeniable.
https://www.wired.com/story/did-russia-affect-the-2016-
election-its-now-undeniable/.
[15] Full Stack Hello World Voting Ethereum Dapp Tutorial –Part
1. https://medium.com/@mvmurthy/full-stack-hello-world-
voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2.
[31] Elli Androulaki, Artem Barger, Vita Bortnikov, Christian Cachin, Kon-
stantinos Christidis, Angelo De Caro, David Enyeart, Christopher Ferris,
Gennady Laventman, Yacov Manevich, Srinivasan Muralidharan, Chet
BIBLIOGRAPHY 37
Murthy, Binh Nguyen, Manish Sethi, Gari Singh, Keith Smith, Alessan-
dro Sorniotti, Chrysoula Stathakopoulou, Marko Vukolić, Sharon Weed
Cocco, and Jason Yellick. Hyperledger fabric: A distributed operating
system for permissioned blockchains. In Proceedings of the Thirteenth
EuroSys Conference, EuroSys ’18, pages 30:1–30:15, New York, NY,
USA, 2018. ACM.
[33] Sukrit Kalra, Seep Goel, Mohan Dhawan, and Subodh Sharma. ZEUS:
analyzing safety of smart contracts. In 25th Annual Network and Dis-
tributed System Security Symposium, NDSS 2018, San Diego, Califor-
nia, USA, February 18-21, 2018, 2018.
[35] John Kelsey, Andrew Regenscheid, Tal Moran, and David Chaum. To-
wards trustworthy elections. chapter Attacking Paper-based e2e Voting
Systems, pages 370–387. Springer-Verlag, Berlin, Heidelberg, 2010.
[36] Loi Luu, Duc-Hiep Chu, Hrishi Olickel, Prateek Saxena, and Aquinas
Hobor. Making smart contracts smarter. In Proceedings of the 2016
ACM SIGSAC Conference on Computer and Communications Security,
CCS ’16, pages 254–269, New York, NY, USA, 2016. ACM.
[37] Nick Szabo. Smart contracts: Building blocks for digital markets. 1996.