Cryptography is the cornerstone of blockchain security, ensuring data confidentiality, integrity, authenticity, and non-repudiability through mathematical algorithms.
1. Hash Functions
Definition and Characteristics
Hash functions are one-way functions that convert variable-length input into fixed-length output.
Key Characteristics:
| Characteristic | Description | Blockchain Application |
|---|---|---|
| Deterministic | Same input always produces same output | Data verification |
| One-way | Cannot reverse-engineer original data from hash | Privacy protection |
| Collision Resistance | Difficult to find two different inputs producing same output | Tamper-proofing |
| Avalanche Effect | Small input changes cause huge output differences | Tamper detection |
Common Hash Algorithms
SHA-256 (used by Bitcoin):
shellInput: "Hello Blockchain" Output: a8f5f167f44f4964e6c998dee827110c9a0c5e1e7a5b6e5f8d9c2b1a4e7f3d6 (Fixed 256 bits / 64 hexadecimal characters)
Keccak-256 (used by Ethereum):
- Variant of SHA-3 standard
- Resistant to length extension attacks
- Used for generating Ethereum addresses
Applications in Blockchain
shellHash applications in block structure: ┌─────────────────────────────────────┐ │ Block Header │ ├─────────────────────────────────────┤ │ Previous Hash │ ← Links blocks, forms chain │ Merkle Root │ ← Transaction data integrity verification │ Timestamp │ │ Nonce │ └─────────────────────────────────────┘ Block Hash = SHA256(SHA256(Block Header))
2. Digital Signatures
Asymmetric Encryption Basics
shellKey Pair Generation: ┌──────────────┐ ┌──────────────┐ │ Private Key │ ←─────→ │ Public Key │ │ (Secret) │ Math │ (Public) │ └──────────────┘ Link └──────────────┘ ↓ ↓ Used for signing Used for verification
Common Algorithms:
- ECDSA (Elliptic Curve Digital Signature Algorithm): Used by Bitcoin, Ethereum
- EdDSA (Edwards-curve Digital Signature Algorithm): Faster and more secure
Signing and Verification Process
shellTransaction Signing Process: 1. Prepare transaction data {"from": "0xabc...", "to": "0xdef...", "value": 100} 2. Calculate transaction hash txHash = Keccak256(transaction data) 3. Sign with private key signature = ECDSA_Sign(privateKey, txHash) 4. Broadcast transaction Transaction data + Signature + Public key Verification Process: ECDSA_Verify(publicKey, txHash, signature) → true/false
Ethereum Address Generation
shell1. Generate private key (256-bit random number) Private Key = Random number 2. Calculate public key through elliptic curve Public Key = Private Key × G (curve base point) 3. Calculate public key hash hash = Keccak256(Public Key) 4. Take last 20 bytes as address Address = "0x" + hash[12:32]
3. Merkle Tree
Structure and Principle
shellMerkle Tree Structure: Root Hash / \ Hash 1-2 Hash 3-4 / \ / \ Hash 1 Hash 2 Hash 3 Hash 4 | | | | Tx 1 Tx 2 Tx 3 Tx 4 (Each Hash = SHA256(child node hashes concatenated))"
Merkle Proof
Light Node Transaction Verification:
shellVerify if Tx 3 exists in block: Required data: - Hash of Tx 3 - Hash 4 (sibling node) - Hash 1-2 (uncle node) - Root Hash (in block header) Verification steps: 1. Hash 3 = SHA256(Tx 3) 2. Hash 3-4 = SHA256(Hash 3 + Hash 4) 3. Calculate Root = SHA256(Hash 1-2 + Hash 3-4) 4. Compare calculated Root with Root in block header
Applications in Blockchain
- Transaction Integrity Verification: Quickly verify large numbers of transactions
- Light Node Synchronization: SPV (Simple Payment Verification) only needs to download block headers
- State Proofs: Ethereum's state tree uses Merkle Patricia Tree
4. Zero-Knowledge Proof
Basic Concept
Definition: Prover proves to verifier that a statement is true without revealing any useful information.
zk-SNARKs (used by Ethereum ZK Rollup):
- Succinct: Small proof size
- Non-interactive: No interaction required
- ARgument of Knowledge: Proof of knowledge
Application Scenarios
shellPrivacy Transaction Example: ┌─────────────────────────────────────┐ │ Proof: I know a secret x such that │ │ hash(x) = 0xabc... │ │ │ │ Without revealing: The actual value │ │ of x │ └─────────────────────────────────────┘
Interview Key Points
- Understand one-way and collision-resistant properties of hash functions
- Master the relationship between public and private keys and digital signature principles
- Be able to explain how Merkle Trees efficiently verify data
- Understand basic concepts and applications of zero-knowledge proofs
- Know specific algorithms used by different blockchains (SHA-256 vs Keccak-256)