Blockchain

Week 2 - Blockchain and Cryptography

"Under the Hood"

Real World Applications

TxCity Visualizer

https://txcity.io

Cryptography Methods in Blockchains

  • Hash Functions
  • Symmetric Cryptography
  • Public Key Cryptography
  • Digital Signatures
  • Merkle Trees and Variants
  • Address Derivation

What is Cryptography?

Science of making information secure, Ciphers are algorithms used to encrypt/decrypt

Symmetric Cryptography

  • Same key is used for both encryption and decryption
  • Algorithms: Data Encryption Standard DES, Advanced Encryption Standard AES
  • Block Ciphers: Operate on fixed-size blocks of data (e.g., 128 bits for AES)
  • Stream Ciphers: Encrypt data one bit or byte at a time
  • Faster than asymmetric cryptography but requires secure key distribution

Try Out...

Download OpenSSL: https://openssl-library.org/source/

AES256

openssl enc -aes-256-cbc -in message.txt -out message.enc -iter +10
openssl enc -d -aes-256-cbc -in message.enc -out decrypted-message.txt -iter +10

DES

openssl enc -des-ede3-cbc -in message.txt -out message.enc -iter +10
openssl enc -d -des-ede3-cbc -in message.enc -out decrypted-message.txt -iter +10

Asymmetric Cryptography

  • Uses a pair of keys: public key and private key
  • Public key can be shared openly, private key must be kept secret
  • Data encrypted with one key can only be decrypted with the other key
  • Slower than symmetric cryptography but provides secure key distribution
  • Algorithms: RSA, ECC (Elliptic Curve Cryptography)

Public/Private Keys

openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in private_key.pem -out public_key.pem
openssl rsa -text -in private_key.pem

Encrypt/Decrypt

echo "Hello, World!" > message.txt
openssl pkeyutl -encrypt -inkey public_key.pem -pubin -in message.txt -out message.enc
openssl pkeyutl -decrypt -inkey private_key.pem -in message.enc -out decrypted-message.txt

Hash Functions

  • Create unique fingerprints of data, called hashes
  • Even minor changes in input produce vastly different outputs(Avalanche effect)
  • Ideally changing one input bit should flip ~50% of output bits
  • Bitcoin uses SHA-256, Ethereum uses Keccak-256

SHA256/Keccak-256 Hash Function

  • Input: any data
  • Output: 256-bit (32-byte) hash value
  • Deterministic: same input always yields same output
  • sha256("Hello, World!") = dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
  • keccak256("Hello, World!") = acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f

Digital Signatures

  • Cryptographic scheme to verify authenticity and integrity of messages or documents
  • Sealed, tamper-proof digital stamp attached to a message
  • Private Key: Used by the sender to create the signature, must remain secret
  • Public Key: Used by the recipient to verify the signature, can be shared openly

Addresses

  • A bitcoin address is created by hashing public key using SHA-256 and then RIPEMD-160
  • Ethereum address is created by taking the last 20 bytes of the Keccak-256 hash of the public key
address = RIPEMD160(SHA256(public_key))  # Bitcoin
address = last_20_bytes(Keccak256(public_key))  # Ethereum
  • Bitcoin address: 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
  • Ethereum address: 0x742d35Cc6634C0532925a3b844Bc454e4438f44e

JSON Bitcoin

{
"lock_time":0,
"size":226,
"inputs":[
  {
    "prev_out":{
      "index":139,
      "hash":"40120e43f00ff96e098a9173f14f1371655b3478bc0a558d6dc17a4ab176387d"
    },
    "script":"483045022100de6fd8120d9f142a82d5da9389e271caa3a757b01757c8e4fa7afbf92e74257c02202a78d4fbd52ae9f3a0083760d76f84643cf8ab80f5
  }
],
"version":1,
"vin_sz":1,
"hash":"d28ca5a59b2239864eac1c96d3fd1c23b747f0ded8f5af0161bae8a616b56a1d",
"vout_sz":2,
"out":[
    {
      "script_string":"OP_DUP OP_HASH160 c568ffeb46c6a9362e44a5a49deaa6eab05a619a OP_EQUALVERIFY OP_CHECKSIG",
      "address":"1JzouJCVmMQBmTcd8K4Y5BP36gEFNn1ZJ3",
      "value":33324,
      "script":"76a914c568ffeb46c6a9362e44a5a49deaa6eab05a619a88ac"
      255
    },
    {
      "script_string":"OP_DUP OP_HASH160 9386c8c880488e80a6ce8f186f788f3585f74aee OP_EQUALVERIFY OP_CHECKSIG",
      "address":"1ET3oBGf8JpunjytE7owyVtmBjmvcDycQe",
      "value":93376,
      "script":"76a9149386c8c880488e80a6ce8f186f788f3585f74aee88ac"
    }
  ]
}

Proof of Work (PoW)

  • A consensus mechanism where a random node (miner) is selected to create the next block based on solving a computational puzzle
H( N \\ p_hash || Tx || Tx || ... || Tx ) < Target
  • N = nonce (a random value that miners change to find a valid hash) with brute force method
  • p_hash = previous block's hash
  • Tx = transactions in the block
  • Target = a value that the hash must be less than to be considered valid

Cryptographic Nonce

hash(data + nonce) = digest || target

Only changing the nonce value provides a mechanism for obtaining different digest values while keeping the same data input.

Transaction Pools

  • Memory pool or Mempool is a collection of unconfirmed transactions
  • Transactions are broadcast to the network and added to mempools
  • Transactions with higher fees are prioritized by miners
  • Mempool size can vary based on network activity and congestion
  • Example: https://mempool.space/

Merkle Trees

Next Steps...Build a Simple Blockchain

  • openssl
  • NodeJS
  • npm
  • npx
{ 
    "name": "Sabin"
}

Developer Basics

  1. C Programming
  2. Javascript/Typescript Basics -> Web Application
  3. NodeJS -> Backend Application
  4. SQL/PostgresSQL/MySQL/MongoDB -> Database
  5. Docker -> Containerization
  6. ReactJS -> Frontend Framework
  7. Git/GitHub -> Version Control

Developer Basics (Contd.)

  1. Command Line -> Terminal
  2. Markdown -> Documentation
  3. JSON -> Share Data between Systems
  4. Go, Rust, NodeJS
  5. DSA -> Algorithms and Data Structures
  6. Testing -> Cypress, Playwright, Mocha, Chai, Jest

Advanced:

  1. Kubernetes -> Container Orchestration
  2. CI/CD -> Continuous Integration/Continuous Deployment
  3. Micro Services -> Service-Oriented Architecture
  4. RabbitMQ/Kafka -> Messaging Systems
  5. GraphQL -> API Query Language
  6. WebAssembly -> High-Performance Web Applications

Game Development

  • Unity -> 2D, 3D games
  • C, C++, Unity
  • Lua -> Game Scripting

AI:

  • Python -> AI/ML
  • R -> Statistical Computing
  • TensorFlow, PyTorch -> Machine Learning Frameworks
  • Jupyter Notebooks -> Interactive Coding

DevOps:

  • AWS, Linux, Azure, Virtualization, Xen, Hyper-V
  • Bash Shell
  • Python Scripting
  • Terraform -> Infrastructure as Code
  • Monitoring Tools -> Prometheus, Grafana
  • Networking -> TCP/IP, DNS, HTTP/HTTPS
  • Nginx, Apache -> Web Servers
  • Load Balancers -> HAProxy, Nginx

Development Environment:

  • Editors: VsCode, Sublime Text, Atom, Cursor, NVim, EMACS
  • Bash: npm run dev
  • Ports, IP Address, Docker

Blockchain:

  • Bitcoin, JSON RPC(Remote Procedure Call), ZeroMQ
  • Ethereum, Smart Contracts, Solidity, Remix IDE, hardhat
  • Web3 -> Web3js, Etherjs, Wagmi, Viem, Geth
  • Avalanche, Arbitrum
  • Tenderly
  • Blockchain Explorers
  • Interface Proxy, Testnet, Auditing, Tests

Installations

  • node/npm -> curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - && sudo apt-get install -y nodejs
  • npx -> npm install -g npx
  • typescript -> npm install --save-dev typescript

Installations for Smart Contracts