Introduction
Understanding the foundational elements of Web3—mnemonic phrases, private keys, public keys, and addresses—is crucial for anyone venturing into blockchain technology. This article demystifies their generation principles and interrelationships using concise explanations and practical Go code examples.
Key Takeaways
- Private keys are 256-bit randomly generated numbers.
- Public keys derive from private keys via elliptic curve cryptography.
- Addresses are compressed versions of public keys.
- Mnemonic phrases simplify private key management.
Core Concepts
1. Elliptic Curve Cryptography
Ethereum and Bitcoin use the secp256k1 elliptic curve. Public keys are (x, y) coordinates on this curve, uniquely determined by the private key:
- Non-compressed public key: 65 bytes (
04prefix + 64-byte x/y coordinates). - Compressed public key: 33 bytes (
02/03prefix + 32-byte x-coordinate).
👉 Learn more about elliptic curves
2. Private Keys
A 256-bit cryptographically secure random number. Critical notes:
- Never lose your private key—recovery is impossible in decentralized systems.
- Use trusted random sources (e.g.,
crypto/randin Go).
3. Mnemonic Phrases
Developed to manage multiple private keys efficiently. Standards like BIP-39 define their generation (covered in-depth in future articles).
Code Implementation in Go
Step-by-Step Process
1. Import Required Packages
import (
"crypto/elliptic"
"crypto/rand"
"encoding/hex"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
)2. Generate Private Key (32 bytes)
curve := secp256k1.S256()
b := make([]byte, 32)
rand.Read(b)
privateKey := new(big.Int).SetBytes(b)
fmt.Println("Private Key:", hex.EncodeToString(privateKey.Bytes()))3. Derive Public Key (64 bytes)
x, y := curve.ScalarBaseMult(privateKey.Bytes())
publicKey := elliptic.Marshal(curve, x, y)
fmt.Println("Public Key:", hex.EncodeToString(publicKey))4. Generate Address (20 bytes)
hash := crypto.Keccak256(publicKey[1:])
address := common.BytesToAddress(hash[12:])
fmt.Println("Address:", address.Hex())5. Verify with MetaMask
Import the private key into MetaMask and confirm the address matches the computed result.
FAQs
Q1: Why is secp256k1 used?
A: It offers a balance of security and computational efficiency, making it ideal for blockchain applications.
Q2: Can I reuse an address for multiple transactions?
A: Yes, but for privacy reasons, generating new addresses per transaction is recommended.
👉 Explore Web3 security practices
Q3: What happens if I lose my mnemonic phrase?
A: All derived private keys and funds become irrecoverable—always store backups securely.
Q4: Are compressed public keys less secure?
A: No, they’re mathematically equivalent to non-compressed keys but save storage space.
Conclusion
Mastering these fundamentals empowers you to:
- Generate keys/addresses programmatically.
- Understand wallet security critically.
- Build deeper blockchain applications.
Remember: With great decentralization comes great responsibility. Keep learning and stay secure!