week2
week2
Smart Contracts
Week – 2
Pritom Rajkhowa
Quick Recap from Last Week
What is a hash function?
• CHOP & MIX
Plan Text
Plan Text
What is Hashing or Hash Function?
Hash Function
Hash or Digest
Qualities of Hash Functions
• Unique Hash Values: It should be impossible to generate same output
for different output.
https://emn178.github.io/online-tools/
https://www.tools4noobs.com/online_tools/hash/
HASH BASED MESSAGE
AUTHENTICATION CODE(HMAC)
Plan Text
Plan Text
Shared Key
Data Integrity
Hashing is used to provide Integrity
• Sender calculate Digest from the message
• Sender sent message and Digest
• Receiver calculate Digest from received message
• Receiver compare both Digest
o If both match, then message is integral
o If both donot match, then message is compomise
Data Integrity
Hashing is used to provide Integrity
• Both parties establish mutual private key
• Sender combained the message + private key to generate the digest
• Receiver verifies by calculating the message + private key
o Message is not definatly modified in transit Intergrtiy
o Sender must have identical key Authenatication
USER PASSWORD
SYMMETRIC ENCRYPTION
Public Key Encryption
Digital Signature
Digital Signature
Ledger
INVALID
Eve's Balan
ce
Ledger
$100 +
1. Bob get $100 $50 -
$0 -
2. Alice get $100
Overdrawn
3. Eve get $100
INVALID
Ledger This is the step
that detached
real money from
1. Bob get $100 this process
INVALID
Ledger
LD
The problem of the Byzantine Generals
“It is not sufficient that everyone knows X. We also need everyone to know that everyone knows X, and that everyone knows
that everyone knows that everyone knows X — which, as in the Byzantine Generals’ problem, is the classic hard problem of
distributed data processing.”
- James A. Donald
What to add
here?
Computation Work
Nonce
0000DF2E57FB432A
000082BAE315474D
Hash is a number ALL POSSIBLE HASH VALUES
Largest
6D7CD92FDFD7E920
= 7889419446888425760
0000DF2E57FB432A
= 0000245390137574186
00000082BAE31547
= 000000561481192775
smallest
Hash is a number ALL POSSIBLE HASH VALUES
Largest
6D7CD92FDFD7E920
= 7889419446888425760
0000DF2E57FB432A
= 0000245390137574186
00000082BAE31547
= 000000561481192775
TARGET
smallest
Hash is a number ALL POSSIBLE HASH VALUES
Largest
6D7CD92FDFD7E920
= 7889419446888425760
0000DF2E57FB432A
= 0000245390137574186
00000082BAE31547
= 000000561481192775
TARGET
Express Target with leading zeros smallest
Hash is a number ALL POSSIBLE HASH VALUES
Largest
6D7CD92FDFD7E920
= 7889419446888425760
0000DF2E57FB432A
= 0000245390137574186
00000082BAE31547
= 000000561481192775
TARGET
Express Target with leading zeros smallest
Largest
Block No # Nonce 23
Nonce Nonce 21
0000DF2E57FB432A
000082BAE315474D
TARGET
Nonce 12
smallest
Probability 1 / 230 ≈ 1/ 1,000,000,000
Ledger
30 Zero
1. Bob paid Alice LD20
1786498 SHA256
Computation Work
Bob paid Alice LD50 Alice paid Bob LD20
Bob paid Alice LD20
{Signature} {Signature} {Signature}
Bob paid Eve LD20 Eve paid Alice LD30 Alice paid Eve LD100
{Signature} {Signature} {Signature}
Blocks
Bob paid Alice LD50 Alice paid Bob LD20
Bob paid Alice LD20
{Signature} {Signature} {Signature}
Bob paid Eve LD20 Eve paid Alice LD30 Alice paid Eve LD100
{Signature} {Signature} {Signature}
Blocks
Bob paid Alice LD50 Alice paid Bob LD20
Bob paid Alice LD20
{Signature} {Signature} {Signature}
Bob paid Eve LD20 Eve paid Alice LD30 Alice paid Eve LD100
{Signature} {Signature} {Signature}
SHA256
Transaction
85665780
Bob paid Alice LD50 Alice paid Bob LD20
Bob paid Alice LD20
{Signature} {Signature} {Signature}
Bob paid Eve LD20 Eve paid Alice LD30 Alice paid Eve LD100
{Signature} {Signature} {Signature}
Bob paid Eve LD20 Eve paid Alice LD30 Alice paid Eve LD100
{Signature} {Signature} {Signature}
85665780 78567901
81224567
LEDGER
BLOCK CHAIN
Immutable Ledger
Traditional Ledger
Block Creator 1
Block Creator 2
Block Creator 3
Bob paid Alice LD20
{Signature}
Block Creator 2
Block Creator 3
Previous Hash
7567454092
Block Creator 2
SHA256
Block Creator 3
Block Creator 1
Block Creator 2
Block Creator 3
Previous Hash
Reword LD10
7567454092
Block Creator 2
SHA256
Block Creator 3
Conflicting Transaction
TRUST CENTRAL
AUTHORITY
COMPUTATION
WORK
Eve want to fraud
Bob with fake
transaction
Transaction
85665780
Eve want to fraud
Bob with fake
transaction
Transaction
85665780
Ethereum
Publish in 2014
Smart Contract
• Written using EVM
3. Balance 3. Balance
1. Address
2. Private Key
1. Address
3. Balance
2. Private Key
3. Balance
1. Address 1. Address
3. Balance 3. Balance
1. Address 1. Address
2. Private Key 2. Private Key
3. Balance
3. Balance
1. Address 1. Address
3. Balance 3. Balance
1. Address 1. Address 1. Address
2. Private Key 2. Private Key 2. Private Key
3. Balance 3. Balance 3. Balance
dAPP
9
3 3
3
9
4 2.5
1.5
9
3 3
3
X
Natural Disasters
Data
I need 10% of all collected sum.
I can also gain profit from
currency exchange
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
The Remix Editor
Solidity supports three types of
variables.
• State Variables − Variables whose values are permanently stored in a
contract storage.
• Local Variables − Variables whose values are present till function is
executing.
• Global Variables − Special variables exists in the global namespace
used to get information about the blockchain.
Solidity : A Deep Dive into its
Mechanism
Ethereum Virtual Machine (EVM)
First, click the Create New File icon in the File Explorers
tab. The icon looks like a page of paper with a corner
folded over. Name the new file FirstContract.sol. Use
the .sol extension to show that the file contains Solidity
code.
• The first line shows the license under which somebody may use and distribute the
software. The code in the above example uses the GPL version 3.0. You can replace this
with any other license like the MIT license.
• The second line shows a pragma directive that tells the compiler to use any Solidity
version from 0.8.1 to 0.9.0 but not including 0.9.0. That is, >= 0.8.1 to < 0.9.0.
• There are usually minor changes, or patches, within the x.y.Z versions. Breaking changes
are normally present in x.Y.z versions. This is why the pragma directive doesn't include
the 0.9.0 version in the above code.
• Solidity is an object-oriented language. The contract keyword on line four is
similar in use to the class keyword in other object-oriented languages.
• Contracts can contain functions, state variables, and other advanced types.
The contract FirstContract contains an unsigned integer (unit) called var1.
• The two functions named set() and get() are setter and getter functions,
respectively, for the variable var1. You can define a function with the
keyword function followed by the function name and parentheses.
• In the parentheses, you can declare the parameters which your function will
take. You should write them in a similar way to variable definitions: state the
data type followed by the parameter name.
• that the definitions of the set() and get() functions contain the
keyword public. This declares that any other contract can call them.