decrypt101
SocialOpen ProjectsSupport me My Resumes
  • Preface
    • Motivation
    • Roadmap’s
  • Introduction to Blockchain
    • A Brief History
    • Growth of Blockchain
    • Structure of Blockchain
    • Types of Blockchain
    • Key Technologies of Blockchain
    • Features of Blockchain
    • How Blockchain Works ?
    • Implementation of Blockchain
    • Summary
  • Components of Blockchain Architecture
    • Distributed Ledger
    • Blocks
    • Transaction
    • Chain
    • Peer-to-Peer Network
    • Blockchain Layers
    • Off-Chain & On-Chain
    • Wallet
    • Mining
    • Tokens
    • Assets
    • State Channels
    • Sidechains
    • Oracles on Blockchain
    • Atomic Swaps
    • Decentralized Identity (DID)
    • Blockchain Data Storage
    • Interoperability
    • Data structures for Scaling Blockchain
    • Maximal Extractable Value (MEV)
  • Consensus Mechanisms
    • Proof of Work (PoW)
      • Implemation Using Rust
    • Proof of Stake (PoS)
    • Proof of Burn (PoB)
    • Proof of Capacity (PoC)
    • Proof of Activity (PoAc)
    • Proof of Weight (PoWe)
    • Proof of Luck (PoL)
    • Proof of Ownership (PoO)
    • Proof of Existence (PoE)
    • Proof of Believability (PoBe)
    • Proof of History (PoH)
    • Proof of Authority (PoA)
    • Proof of Elapsed Time (PoET)
  • Cryptographics
    • Encryption & Decryption
      • Symmetric Encryption
      • Asymmetric Encryption
      • Key Management and Exchange
      • Implementation
    • Cryptographic Hashing
      • Secure Hash Algorithms (SHA)
      • Message Digest Algorithms
      • Ethash
      • Blake2
      • SCrypt
      • RIPEMD-160
    • Digital Signature
      • Digital Signature Algorithms
      • Digital Signature in Blockchain
    • Zero-Knowledge Proofs (ZKPs)
      • Types of Zero-Knowledge Proof and Protocols
      • A Case Study of Polygon Platform
    • Multi-Party Computation (MPC)
    • Cryptanalysis
    • Practical Implementation
  • Decentralized Application (DApp)
    • Design and UX in Web3
  • Smart Contract
    • Development Tools
    • Solidity
    • Testing Smart Contract
    • Developing Smart Contract
    • Interacting & Deploying with Smart Contract
    • Verifying Smart Contracts
    • Upgrading Smart Contracts
    • Securing Smart Contract
    • Smart Contract Composability
    • Testnet and Mainnet
    • Blockchain Platform Using Smart Contract
    • Application of Smart Contract
    • Practical Implementation
  • Blockchain Platforms
    • Ethereum
      • Ethereum Virtual Machine (EVM)
      • ETHER and GAS
      • Ethereum transaction
      • Ethereum Accounts
      • Ethereum Stacking
      • Ethereum Network
      • Ethereum Scaling Solutions
      • Ethereum Use-Cases
      • Getting Started with Ethereum
      • Ethereum Ecosystem and Support
    • Solana
      • Solana Architecture
        • Solana Account Model
        • Solana Wallet
        • Transactions and Instructions
        • Solana Programs
        • Program Derived Address (PDA)
        • Cross Program Invocation (CPI)
        • Tokens on Solana
        • Clusters and Public RPC Endpoints
        • Transaction Confirmation & Expiration
        • Retrying Transactions
        • Versioned Transactions
        • Address Lookup Tables
        • State Compression
        • Actions and Blinks
      • Solana Developments
      • Solana Client
      • Advanced Solana
      • Solana Scaling and Performance Architecture
      • Solana Solutions and cases
      • Practical Implemenation
    • Binance Smart Chain (BSC)
      • Create a BEP20 Token
    • Hyperledger Fabric
    • Cosmos
    • Polkadot
    • Quorum
    • Polygon
    • Algorand
    • Corda
    • Avalanche
    • TRON
    • Summary
  • Decentralized Finance (DeFi)
    • DeFi Components
    • DeFi Protocols
    • DeFi Platforms
    • DeFi Risk Classification
      • Infrastructure-layer Attacks
      • Smart Contract Layer-attacks
      • Application Layer-attacks
      • DeFi Risks
    • DeFi and Blockchain
    • DeFi Impact
  • Decentralized Ecosystem and Digital Innovation
    • Layer 2 Scaling Fundamental
    • Tokenomics
    • Cryptocurrency
    • Quantative Trading
    • NFTs
    • GameFi
    • Metaverse
  • Blockchain as a Service (BaaS)
    • Building Fullstack Blockchain Platform
    • Decentralized Digital Identity
    • Build a Cryptocurrencies Exchange
    • Play-to-Earn Gaming
    • Solana Token Airdrop Manager
    • Smart Contract Development on Solana with Rust
    • Quantitative Trading Platform
    • Insurances protocols
    • Flash Loans
    • Asset Management
    • Tokenized Derivatives
    • Automated Market Makers (AMMs)
    • Staking
    • Lending and Borrowing Platforms
    • Yield Farming
    • Stablecoin System
    • Security Token Offerings (STOs)
    • Initial Coin Offerings (ICOs)
    • On-Chain Voting Systems
    • Decentralized Autonomous Organizations (DAOs)
    • NFT Marketplaces
    • Provenance Verification
    • Supply Chain Tracking
    • Commodities Tokenization
    • Real Estate Tokenization
    • Digital Certificates
    • KYC (Know Your Customer)
  • Blockchain Development Across Languages
    • Blockchain using Go(Golang)
    • Blockchain using Rust
    • Blockchain using Python
    • Blockchain using Cairo
  • Distributed Systems & Infrastructure Technology
    • Classification of Distributed Systems
    • Networked systems versus Distributed systems
    • Parallel systems vs Distributed systems
    • Distributed versus Decentralized systems
    • Processes of Distributed Systems
    • Architecture of Distributed systems
    • Infrastructure Technologies
  • Distributed System Patterns
    • Distributed Agreements Algorithms
      • HoneyBadgerBFT
    • Data Replications
    • Data Partition
    • Consistency
    • Distributed Time
    • Cluster Management
    • Communication between Nodes
    • Fault Tolerance and Resilience
      • How to design better fault tolerance systems
      • Resilience Patterns
    • Coordination systems
      • Clock synchronization
    • Security
      • Trust in distributed systems
      • Design of Principal Security
      • Security threats, policies, and mechanisms
      • Authentication and Authorizations
      • Cryptography
      • Monitoring in Security
  • Distributed System Design
    • Page 1
    • Distributed Shared Memory
    • Distributed Data Management
    • Distributed Knowledge Management
    • Distributed Ledger
  • FAQs
  • Support and Community
Powered by GitBook
On this page
  • RSA (Rivest, Shamir, Adleman) Algorithm
  • ElGamal Encryption System
  • DSA (Digital Signature Algorithm)
  • ECDSA (Elliptic Curve Digital Signature Algorithm)
  1. Cryptographics
  2. Digital Signature

Digital Signature Algorithms

Abstract

Digital signatures are a fundamental cryptographic tool used to ensure the authenticity and integrity of data in digital systems. They play a critical role in secure communication, blockchain, and digital identity verification. Several algorithms have been developed to generate these signatures, each with unique strengths and applications. This chapter explores four widely used digital signature algorithms: RSA (Rivest, Shamir, Adleman), ElGamal, DSA (Digital Signature Algorithm), and ECDSA (Elliptic Curve Digital Signature Algorithm). These algorithms vary in their underlying mathematical foundations, computational efficiency, and security properties, offering different solutions for digital authentication in various contexts. By analyzing these algorithms, we can better understand the trade-offs between security, performance, and implementation in real-world applications.

RSA (Rivest, Shamir, Adleman) Algorithm

The RSA algorithm is one of the most widely used public-key cryptosystems, primarily used for secure data transmission and digital signatures. It was developed by Ron Rivest, Adi Shamir, and Leonard Adleman in 1977 and is based on the mathematical difficulty of factoring large prime numbers. The security of RSA relies on the fact that, while it is computationally easy to multiply large prime numbers together, it is extremely difficult to factor their product back into the original primes, especially when the numbers are large.

Key Concepts of RSA:

  1. Public and Private Keys: RSA uses a pair of keys – a public key for encryption (or signature verification) and a private key for decryption (or signature creation).

  2. Key Generation:

    • Two large prime numbers, p and q, are generated.

    • The product of these primes n = p * q is used as part of the public key.

    • A number e (public exponent) is selected such that 1 < e < φ(n) and gcd(e, φ(n)) = 1, where φ(n) is Euler's totient function φ(n) = (p-1) * (q-1).

    • The private key is determined by computing d such that d * e ≡ 1 (mod φ(n)). This is the modular multiplicative inverse of e modulo φ(n).

  3. Encryption:

    • A plaintext message M is converted into a number m such that 0 <= m < n.

    • The ciphertext c is computed using the public key: c = m^e mod n.

  4. Decryption:

    • The original message m can be recovered using the private key: m = c^d mod n.

  5. Digital Signature:

    • To sign a message, the sender uses their private key to create a signature: s = m^d mod n.

    • The receiver can verify the signature using the sender's public key: m = s^e mod n.

RSA Algorithm Implementation in Golang

Here’s an implementation of the RSA algorithm in Golang:

package main

import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/sha256"
    "fmt"
    "log"
)

// Function to generate RSA keys (private and public)
func generateKeys(bits int) (*rsa.PrivateKey, error) {
    // Generate RSA private key
    privateKey, err := rsa.GenerateKey(rand.Reader, bits)
    if err != nil {
        return nil, err
    }
    return privateKey, nil
}

// Function to encrypt a message using RSA public key
func encryptMessage(publicKey *rsa.PublicKey, message []byte) ([]byte, error) {
    // Encrypt the message using RSA-OAEP with SHA-256
    ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, message, nil)
    if err != nil {
        return nil, err
    }
    return ciphertext, nil
}

// Function to decrypt the ciphertext using RSA private key
func decryptMessage(privateKey *rsa.PrivateKey, ciphertext []byte) ([]byte, error) {
    // Decrypt the message using RSA-OAEP with SHA-256
    plaintext, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, ciphertext, nil)
    if err != nil {
        return nil, err
    }
    return plaintext, nil
}

func main() {
    // Generate RSA key pair (private and public)
    bits := 2048 // Key size
    privateKey, err := generateKeys(bits)
    if err != nil {
        log.Fatalf("Error generating keys: %v", err)
    }

    // Public key is derived from the private key
    publicKey := &privateKey.PublicKey

    // Original message to encrypt
    message := []byte("Hello, this is a secret message!")

    // Encrypt the message
    ciphertext, err := encryptMessage(publicKey, message)
    if err != nil {
        log.Fatalf("Error encrypting message: %v", err)
    }
    fmt.Printf("Encrypted message: %x\n", ciphertext)

    // Decrypt the message
    plaintext, err := decryptMessage(privateKey, ciphertext)
    if err != nil {
        log.Fatalf("Error decrypting message: %v", err)
    }
    fmt.Printf("Decrypted message: %s\n", plaintext)
}

Explanation of Code:

  1. Key Generation:

    • The generateKeys function generates a 2048-bit RSA key pair using the rsa.GenerateKey function. This key pair consists of a public key and a private key.

  2. Encryption:

    • The encryptMessage function encrypts a plaintext message using RSA-OAEP (Optimal Asymmetric Encryption Padding) with SHA-256 as the hash function. This is done using the public key.

  3. Decryption:

    • The decryptMessage function decrypts the ciphertext using the private key. RSA-OAEP with SHA-256 is used here as well for decryption.

How to Run the Code

  1. Install Go (Golang) if you haven't already.

  2. Save the above code in a file, e.g., rsa.go.

  3. Run the code:

    go run rsa.go
  4. You will see the encrypted message in hexadecimal format and the decrypted message back to the original string.

Further Customizations:

You can expand on this by adding:

  • Digital signature: Use the private key to sign a message and the public key to verify it.

  • Larger key sizes: For stronger security, you can use larger key sizes like 4096 bits.

ElGamal Encryption System

The ElGamal encryption system is an asymmetric key encryption algorithm used in cryptography, developed by Taher ElGamal in 1985. Like RSA, it is based on the principles of public-key cryptography. However, instead of relying on the difficulty of factoring large primes like RSA, ElGamal is based on the computational difficulty of solving the Discrete Logarithm Problem (DLP) over finite fields, which forms the basis of many cryptographic systems, including elliptic curve cryptography.

Key Concepts of ElGamal Encryption:

  1. Public and Private Keys:

    • The ElGamal encryption system uses a pair of keys: a public key (used for encryption) and a private key (used for decryption).

  2. Key Generation:

    • Select a large prime number p and a generator g (a primitive root modulo p).

    • Choose a private key x where 1 < x < p-1.

    • The public key is generated as h = g^x mod p, where h is part of the public key, and x is the private key.

  3. Encryption:

    • To encrypt a message m:

      • Choose a random ephemeral key k where 1 < k < p-1.

      • Compute c1 = g^k mod p and c2 = m * h^k mod p.

    • The ciphertext is the pair (c1, c2).

  4. Decryption:

    • To decrypt the ciphertext (c1, c2):

      • Compute m = c2 * (c1^x)^-1 mod p, where x is the private key.

    • This recovers the original message m.

Advantages and Disadvantages:

  • Advantages:

    • Security is based on the difficulty of the discrete logarithm problem.

    • It supports homomorphic encryption, meaning operations can be performed on encrypted data without needing decryption.

  • Disadvantages:

    • ElGamal encryption doubles the size of the plaintext in its ciphertext.

    • It requires random ephemeral keys for each encryption, which can affect performance.

ElGamal Encryption System Implementation in Golang

Here’s a basic implementation of the ElGamal encryption system in Golang:

package main

import (
    "crypto/rand"
    "fmt"
    "math/big"
)

// Function to generate ElGamal keys
func generateElGamalKeys(p *big.Int, g *big.Int) (*big.Int, *big.Int, *big.Int) {
    x, _ := rand.Int(rand.Reader, p) // Private key x (random integer 1 < x < p-1)
    h := new(big.Int).Exp(g, x, p)   // h = g^x mod p (Public key h)
    return x, h, g
}

// ElGamal encryption function
func encryptElGamal(p *big.Int, g *big.Int, h *big.Int, m *big.Int) (*big.Int, *big.Int) {
    k, _ := rand.Int(rand.Reader, p) // Random ephemeral key
    c1 := new(big.Int).Exp(g, k, p)  // c1 = g^k mod p
    s := new(big.Int).Exp(h, k, p)   // s = h^k mod p
    c2 := new(big.Int).Mul(m, s)     // c2 = m * s
    c2.Mod(c2, p)                    // c2 = (m * s) mod p
    return c1, c2                    // Ciphertext (c1, c2)
}

// ElGamal decryption function
func decryptElGamal(p *big.Int, x *big.Int, c1 *big.Int, c2 *big.Int) *big.Int {
    s := new(big.Int).Exp(c1, x, p)  // s = c1^x mod p
    s.ModInverse(s, p)               // s = s^(-1) mod p (Modular inverse of s)
    m := new(big.Int).Mul(c2, s)     // m = c2 * s
    m.Mod(m, p)                      // m = (c2 * s) mod p
    return m                         // Original message m
}

func main() {
    // Large prime number (p) and generator (g)
    p, _ := new(big.Int).SetString("1461501637330902918203684832716283019653785059327", 10)
    g := big.NewInt(2) // Simple generator g = 2

    // Key generation
    privateKey, publicKey, generator := generateElGamalKeys(p, g)
    fmt.Printf("Private key: %s\n", privateKey)
    fmt.Printf("Public key: h = %s, g = %s\n", publicKey, generator)

    // Message to encrypt (as a number)
    message := big.NewInt(123456) // This is the message to encrypt
    fmt.Printf("Original message: %s\n", message)

    // Encryption
    c1, c2 := encryptElGamal(p, g, publicKey, message)
    fmt.Printf("Encrypted message: c1 = %s, c2 = %s\n", c1, c2)

    // Decryption
    decryptedMessage := decryptElGamal(p, privateKey, c1, c2)
    fmt.Printf("Decrypted message: %s\n", decryptedMessage)
}

Explanation of Code:

  1. Key Generation:

    • The generateElGamalKeys function generates the ElGamal key pair (private and public). It selects a private key x and computes the public key h = g^x mod p.

  2. Encryption:

    • The encryptElGamal function encrypts a message m using the public key h. It generates a random ephemeral key k, computes c1 = g^k mod p and c2 = m * h^k mod p, which are the ciphertext values.

  3. Decryption:

    • The decryptElGamal function decrypts the ciphertext (c1, c2) using the private key x. It computes s = c1^x mod p, the modular inverse of s, and recovers the message m = (c2 * s) mod p.

Running the Code:

  1. Install Go (Golang) if you haven't already.

  2. Save the above code in a file, e.g., elgamal.go.

  3. Run the code:

    go run elgamal.go

You will see the generated private key, public key, original message, ciphertext, and the decrypted message.

Notes:

  • Prime Number Selection: In this example, a large prime number p is chosen along with the generator g. You can change the size of p for different security levels.

  • Ephemeral Key: For each encryption, a new random ephemeral key k is chosen. Reusing k is a significant security risk in ElGamal, as it could reveal the private key.

DSA (Digital Signature Algorithm)

The Digital Signature Algorithm (DSA) is a widely used public key algorithm that creates a digital signature for a message, providing authentication and data integrity. DSA is a federal standard for digital signatures as part of the Digital Signature Standard (DSS) adopted by the U.S. government in 1993. It is based on the mathematical difficulty of solving the Discrete Logarithm Problem (DLP), similar to ElGamal.

Unlike encryption algorithms such as RSA, DSA is used only for digital signatures, not encryption. The security of DSA relies on choosing large prime numbers and hash functions, and it ensures that each message has a unique signature, even if the message content is identical.

Key Concepts of DSA:

  1. Public and Private Keys:

    • DSA involves a pair of keys: a private key (used to sign messages) and a public key (used to verify signatures).

  2. Key Generation:

    • Select a large prime number p and a prime divisor q where q divides p-1.

    • Choose a generator g such that g = h^((p-1)/q) mod p for some random h.

    • The private key x is selected randomly from the range [1, q-1].

    • The public key is y = g^x mod p.

  3. Signature Generation:

    • For a message hash H(m):

      • Choose a random ephemeral key k from the range [1, q-1].

      • Compute r = (g^k mod p) mod q.

      • Compute s = (k^(-1) * (H(m) + x*r)) mod q.

    • The signature is the pair (r, s).

  4. Signature Verification:

    • To verify the signature (r, s):

      • Verify that 0 < r < q and 0 < s < q.

      • Compute the hash of the message H(m).

      • Compute w = s^(-1) mod q.

      • Compute u1 = (H(m) * w) mod q and u2 = (r * w) mod q.

      • Verify that v = ((g^u1 * y^u2) mod p) mod q equals r.

    • If v == r, the signature is valid.

Advantages and Disadvantages:

  • Advantages:

    • DSA signatures are smaller than those generated by RSA.

    • It is more efficient in terms of key size, especially with elliptic curve variants (ECDSA).

  • Disadvantages:

    • Each message requires a unique random k, and reusing k can lead to exposing the private key.

    • DSA is slower than RSA for signature verification, but faster for signing.

DSA Algorithm Implementation in Golang

Here is an implementation of the DSA signature generation and verification in Golang:

package main

import (
	"crypto/dsa"
	"crypto/rand"
	"crypto/sha256"
	"fmt"
	"math/big"
)

func main() {
	// DSA Parameter Sizes: L is size of p, N is size of q
	params := new(dsa.Parameters)
	if err := dsa.GenerateParameters(params, rand.Reader, dsa.L1024N160); err != nil {
		fmt.Println("Error generating parameters:", err)
		return
	}

	// Key generation: Private and public key
	privateKey := new(dsa.PrivateKey)
	privateKey.PublicKey.Parameters = *params
	if err := dsa.GenerateKey(privateKey, rand.Reader); err != nil {
		fmt.Println("Error generating private key:", err)
		return
	}
	publicKey := &privateKey.PublicKey

	fmt.Println("DSA Key generation successful")

	// Message to be signed
	message := "This is a secret message!"
	hash := sha256.Sum256([]byte(message))

	// Signature generation
	r, s, err := dsa.Sign(rand.Reader, privateKey, hash[:])
	if err != nil {
		fmt.Println("Error signing message:", err)
		return
	}
	fmt.Printf("Signature: r = %s, s = %s\n", r.String(), s.String())

	// Signature verification
	isValid := dsa.Verify(publicKey, hash[:], r, s)
	if isValid {
		fmt.Println("Signature verification successful: Valid signature")
	} else {
		fmt.Println("Signature verification failed: Invalid signature")
	}
}

Explanation of Code:

  1. Key Generation:

    • DSA parameters are generated with dsa.GenerateParameters, using the standard L1024N160, which corresponds to a 1024-bit prime p and a 160-bit prime q. The dsa.GenerateKey function generates the private and public keys based on these parameters.

  2. Message Hashing:

    • The message is hashed using SHA-256 (crypto/sha256) before signing it. In this example, the message is "This is a secret message!".

  3. Signature Generation:

    • The dsa.Sign function generates a signature pair (r, s) for the hashed message using the private key.

  4. Signature Verification:

    • The dsa.Verify function verifies the signature (r, s) with the public key and the hash of the message. If the signature is valid, it prints that the signature verification was successful.

How to Run the Code:

  1. Install Go (Golang) if not already installed.

  2. Save the above code in a file, e.g., dsa.go.

  3. Run the code:

    go run dsa.go

This code will generate a DSA key pair, sign a hashed message, and verify the signature. If successful, it will print that the signature is valid.

Notes:

  • Random k Value: The value of k must be unique for every signature. Reusing k can expose the private key.

  • Security Parameter Sizes: This example uses 1024-bit p and 160-bit q, but larger sizes (e.g., L2048N224 or L3072N256) offer stronger security.

DSA is a foundational cryptographic algorithm used in many real-world applications, including secure email systems and digital certificates.

ECDSA (Elliptic Curve Digital Signature Algorithm)

The Elliptic Curve Digital Signature Algorithm (ECDSA) is a variant of the Digital Signature Algorithm (DSA) that uses elliptic curve cryptography (ECC). ECDSA is widely used because it provides the same level of security as other signature algorithms like RSA and DSA but with much smaller key sizes, resulting in faster computations and reduced storage requirements.

The security of ECDSA is based on the Elliptic Curve Discrete Logarithm Problem (ECDLP), which is computationally hard to solve, providing strong cryptographic security.

Key Concepts of ECDSA:

  1. Elliptic Curves:

    • An elliptic curve is defined by an equation of the form y^2 = x^3 + ax + b over a finite field. The set of points on the curve, along with an operation called point addition, forms the basis of elliptic curve cryptography.

  2. Public and Private Keys:

    • A private key d is a randomly selected integer from a large range.

    • The public key is a point Q = d * G on the elliptic curve, where G is a predefined point (called the base point or generator point), and * represents scalar multiplication on the curve.

  3. Signature Generation:

    • To sign a message m:

      • Compute the hash of the message H(m).

      • Choose a random ephemeral key k from the range [1, n-1], where n is the order of the curve.

      • Compute the point R = k * G, where R = (x, y) and use r = x mod n as part of the signature.

      • Compute s = (k^(-1) * (H(m) + d * r)) mod n, where d is the private key.

    • The signature is the pair (r, s).

  4. Signature Verification:

    • To verify the signature (r, s):

      • Verify that r and s are within the valid range [1, n-1].

      • Compute w = s^(-1) mod n.

      • Compute u1 = (H(m) * w) mod n and u2 = (r * w) mod n.

      • Compute the point P = u1 * G + u2 * Q, where Q is the public key.

      • The signature is valid if P = (x, y) and r = x mod n.

Advantages and Disadvantages:

  • Advantages:

    • ECDSA offers the same security level as RSA and DSA but with significantly smaller key sizes (e.g., a 256-bit key in ECDSA offers similar security to a 3072-bit RSA key).

    • It is faster and more efficient in terms of computation and bandwidth.

  • Disadvantages:

    • ECDSA requires careful selection of the random value k for each signature. If k is reused, it can lead to the leakage of the private key.

    • More complex than RSA in terms of implementation and requires precise parameter handling to avoid vulnerabilities.

ECDSA Algorithm Implementation in Golang

Here is a basic implementation of ECDSA using Golang's crypto/ecdsa package, which is part of Go's standard library.

package main

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/sha256"
	"fmt"
	"math/big"
)

func main() {
	// Generate a public/private key pair using the P256 curve
	privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	if err != nil {
		fmt.Println("Error generating ECDSA key:", err)
		return
	}
	publicKey := &privateKey.PublicKey

	// Message to sign
	message := "This is a secret message!"
	hash := sha256.Sum256([]byte(message))

	// Sign the hashed message
	r, s, err := ecdsa.Sign(rand.Reader, privateKey, hash[:])
	if err != nil {
		fmt.Println("Error signing message:", err)
		return
	}
	fmt.Printf("Signature: r = %s, s = %s\n", r.String(), s.String())

	// Verify the signature
	isValid := ecdsa.Verify(publicKey, hash[:], r, s)
	if isValid {
		fmt.Println("Signature verification successful: Valid signature")
	} else {
		fmt.Println("Signature verification failed: Invalid signature")
	}
}

Explanation of Code:

  1. Key Generation:

    • The ecdsa.GenerateKey function generates an ECDSA key pair using the P256 elliptic curve, a commonly used curve (also called secp256r1).

  2. Message Hashing:

    • The message is hashed using SHA-256 (crypto/sha256), which produces a fixed-size 256-bit digest. Hashing is essential for fixed-size input when signing messages with ECDSA.

  3. Signature Generation:

    • The ecdsa.Sign function generates an ECDSA signature, a pair of values (r, s), using the private key and the hashed message. It internally selects a random value k for signing.

  4. Signature Verification:

    • The ecdsa.Verify function verifies the signature (r, s) using the public key and the hashed message. If the signature is valid, it prints that verification succeeded.

How to Run the Code:

  1. Install Go (Golang) if not already installed.

  2. Save the above code in a file, e.g., ecdsa.go.

  3. Run the code:

    go run ecdsa.go

This code will generate an ECDSA key pair, sign a message, and verify the signature. If successful, it will confirm that the signature is valid.

Notes:

  • Curve Selection: This example uses the P256 curve. You can also use other curves like P224, P384, and P521 depending on the desired security level.

  • Random k Value: ECDSA requires a random value k for every signature. Reusing k or having a predictable k can compromise the private key.

  • Security: ECDSA provides strong security at smaller key sizes than RSA or DSA, making it efficient for resource-constrained environments like mobile devices or IoT.

ECDSA in Real-World Applications:

  • ECDSA is used extensively in blockchain technologies, including Bitcoin and Ethereum, where digital signatures are required for transactions.

  • It is also employed in TLS (Transport Layer Security) for secure communication on the web.

PreviousDigital SignatureNextDigital Signature in Blockchain

Last updated 7 months ago