Chapter 2: Cryptography Fundamentals
Cryptography is the cornerstone of blockchain technology. This chapter delves deep into the core cryptographic concepts that underpin blockchain security, including hash functions, public key cryptography, digital signatures, and other key technologies.
Chapter Objectives:
- Understand the core role of cryptography in blockchain
- Master the characteristics and applications of hash functions
- Learn public/private key encryption mechanisms
- Understand how digital signatures work
- Explore advanced cryptographic structures like Merkle trees
2.1 Introduction to Cryptography
Cryptography is the science of secure communication in adversarial environments. In blockchain, cryptography provides the following key functions:
Core Goals of Cryptography
-
Confidentiality
- Ensures information can only be read by authorized parties
- Prevents unauthorized access
-
Integrity
- Detects whether data has been tampered with
- Ensures data has not been modified
-
Authentication
- Verifies the identity of communicating parties
- Confirms message origin
-
Non-repudiation
- Sender cannot deny having sent a message
- Provides evidence of actions
Cryptographic Applications in Blockchain
2.2 Hash Functions
What is a Hash Function?
A Hash Function is a one-way function that maps input data of arbitrary length to fixed-length output.
Key Properties of Hash Functions
-
Deterministic
- Same input always produces same output
- Reproducible and verifiable
-
Fast Computation
- Can quickly compute hash values
- Efficient verification process
-
One-way
- Cannot reverse engineer original data from hash
- Pre-image resistance
-
Avalanche Effect
- Small input changes cause massive output changes
- Enhances security
-
Collision Resistance
- Extremely difficult to find two different inputs producing same output
- Prevents forgery attacks
SHA-256 Algorithm
SHA-256 (Secure Hash Algorithm 256-bit) is the primary hash algorithm used by Bitcoin.
SHA-256 Characteristics
- Output length: 256 bits (32 bytes)
- Typically represented as 64 hexadecimal characters
- Fast computation, high security
Example
1 | // SHA-256 hash example |
Hash Applications in Blockchain
1. Block Linking
2. Proof of Work (PoW)
1 | // Simplified PoW example |
3. Address Generation
Other Important Hash Algorithms
RIPEMD-160
- Output: 160 bits (20 bytes)
- Use: Bitcoin address generation
- Feature: Shorter output, saves space
Keccak-256
- Used in: Ethereum
- Output: 256 bits
- Feature: SHA-3 variant
Blake2
- Used in: Some emerging blockchains
- Feature: Faster than SHA-256
- Security: Comparable to SHA-3
2.3 Symmetric and Asymmetric Encryption
Symmetric Encryption
Uses the same key for encryption and decryption.
How It Works
Common Symmetric Encryption Algorithms
-
AES (Advanced Encryption Standard)
- Most widely used
- Supports 128, 192, 256-bit keys
-
DES/3DES
- Older standard
- No longer recommended
Pros and Cons
Pros:
- Fast encryption speed
- Suitable for large data volumes
- Low computational resource consumption
Cons:
- Difficult key distribution
- Complex key management
- Not suitable for public networks
Asymmetric Encryption
Uses a key pair: Public Key and Private Key.
Core Concept
Encrypted Communication Flow
Common Asymmetric Encryption Algorithms
1. RSA
- Based on integer factorization problem
- Key length: 2048-4096 bits
- Use: TLS/SSL, digital signatures
1 | // RSA concept example (simplified) |
2. ECC (Elliptic Curve Cryptography)
- Based on elliptic curve discrete logarithm problem
- Key length: 256 bits (equivalent to RSA 3072-bit security)
- Advantage: Shorter keys, more efficient
Common Curves in Blockchain:
- secp256k1: Used by Bitcoin, Ethereum
- Ed25519: Used by Solana, Polkadot
- secp256r1: Some enterprise blockchains
How ECC Works
Example: Ethereum Key Pair
1 | // Ethereum key pair generation example |
Symmetric vs Asymmetric Encryption Comparison
| Feature | Symmetric | Asymmetric |
|---|---|---|
| Keys | Single key | Public + Private |
| Speed | Fast | Slow (10-1000x) |
| Key Distribution | Difficult | Easy |
| Use Case | Large data encryption | Key exchange, signatures |
| Examples | AES, DES | RSA, ECC |
| Blockchain App | Wallet encryption | Transaction signing |
2.4 Digital Signatures
Digital Signatures use a private key to sign data, and anyone can verify the signature’s authenticity using the corresponding public key.
Purpose of Digital Signatures
- Authentication: Proves message truly comes from private key holder
- Data Integrity: Proves message hasn’t been tampered with
- Non-repudiation: Signer cannot deny having signed
Digital Signature Workflow
ECDSA (Elliptic Curve Digital Signature Algorithm)
The signature algorithm used by Bitcoin and Ethereum.
Signature Generation
1 | // Ethereum transaction signing example |
Signature Verification
1 | // Verify signature |
Bitcoin Transaction Signing
Multi-Signature (MultiSig)
Requires multiple private keys to sign together to complete a transaction.
M-of-N MultiSig
Bitcoin P2SH MultiSig Script
1 | # 2-of-3 multisig script |
2.5 Merkle Trees
A Merkle Tree, also known as a hash tree, is a tree data structure used to efficiently verify the integrity of large datasets.
Merkle Tree Structure
Building a Merkle Tree
1 | // Merkle tree implementation example |
Merkle Proof
Lightweight verification without downloading all data.
Merkle Proof Code Implementation
1 | class MerkleTree { |
Applications of Merkle Trees
1. Bitcoin Block Structure
2. SPV Light Nodes
SPV (Simplified Payment Verification) nodes only download block headers, not all transactions.
3. State Tree
Ethereum uses Merkle Patricia Trie to store account state.
2.6 Advanced Cryptographic Concepts
Zero-Knowledge Proof (ZKP)
Zero-Knowledge Proof allows a prover to convince a verifier that a statement is true without revealing any additional information.
Classic Example: Ali Baba’s Cave
zk-SNARKs
zk-SNARKs (Zero-Knowledge Succinct Non-Interactive Argument of Knowledge)
Applications:
- Zcash: Privacy transactions
- Tornado Cash: Ethereum mixer
- zkSync: Layer 2 scaling
Homomorphic Encryption
Homomorphic Encryption allows computation directly on ciphertext, with decryption yielding the result of operations on plaintext.
Threshold Signature
Multiple parties jointly hold fragments of a private key, requiring t-of-n fragments to generate a valid signature.
Chapter Summary
Navigation:
- Previous Chapter: Chapter 1: Blockchain Fundamentals
- Next Chapter: Chapter 3: Cryptocurrency Fundamentals
- Back to Contents: CCBook Contents