Digital Signatures
Digital signature algorithms with mathematical foundations.
Mathematical Definition
A digital signature scheme consists of:
$$ \begin{aligned} (pk, sk) &\leftarrow KeyGen() \ \sigma &= Sign(sk, M) \ {0,1} &= Verify(pk, M, \sigma) \end{aligned} $$
Properties:
- Authentication: Proves message from holder of $sk$
- Integrity: Detects tampering
- Non-repudiation: Signer can't deny signing
RSA Signatures (RSA-PSS)
Signing
$$ \sigma = (H(M))^d \mod n $$
Verification
$$ H(M) \stackrel{?}{=} \sigma^e \mod n $$
Where $H$ is a hash function (SHA-256).
ECDSA (Elliptic Curve DSA)
Signing
- Choose random $k \in [1, n-1]$
- Compute $R = k \cdot G = (x_R, y_R)$
- Compute $r = x_R \mod n$
- Compute $s = k^{-1}(H(M) + r \cdot sk) \mod n$
$$ \sigma = (r, s) $$
Verification
- Compute $w = s^{-1} \mod n$
- Compute $u_1 = H(M) \cdot w \mod n$
- Compute $u_2 = r \cdot w \mod n$
- Compute $R' = u_1 \cdot G + u_2 \cdot pk$
- Verify $r \stackrel{?}{=} x_{R'} \mod n$
Why It Works
$$ \begin{aligned} R' &= u_1 \cdot G + u_2 \cdot pk \ &= (H(M) \cdot s^{-1}) \cdot G + (r \cdot s^{-1}) \cdot (sk \cdot G) \ &= s^{-1}(H(M) + r \cdot sk) \cdot G \ &= k \cdot G = R \end{aligned} $$
EdDSA (Ed25519)
Signing
- Compute $r = H(hash_prefix, M)$
- Compute $R = r \cdot G$
- Compute $h = H(R, pk, M)$
- Compute $s = (r + h \cdot sk) \mod \ell$
$$ \sigma = (R, s) $$
Verification
$$ s \cdot G \stackrel{?}{=} R + H(R, pk, M) \cdot pk $$
Advantages over ECDSA:
- Deterministic (no random $k$)
- Faster
- Smaller signatures
- Side-channel resistant
Python Implementation
Ed25519 (Recommended)
1from cryptography.hazmat.primitives.asymmetric import ed25519
2
3# Generate key pair
4private_key = ed25519.Ed25519PrivateKey.generate()
5public_key = private_key.public_key()
6
7# Sign
8message = b"Important message"
9signature = private_key.sign(message)
10
11# Verify
12try:
13 public_key.verify(signature, message)
14 print("✅ Signature valid!")
15except:
16 print("❌ Signature invalid!")
ECDSA
1from cryptography.hazmat.primitives.asymmetric import ec
2from cryptography.hazmat.primitives import hashes
3
4# Generate key pair
5private_key = ec.generate_private_key(ec.SECP256K1())
6public_key = private_key.public_key()
7
8# Sign
9signature = private_key.sign(
10 message,
11 ec.ECDSA(hashes.SHA256())
12)
13
14# Verify
15try:
16 public_key.verify(signature, message, ec.ECDSA(hashes.SHA256()))
17 print("✅ Signature valid!")
18except:
19 print("❌ Signature invalid!")
Schnorr Signatures (Bitcoin Taproot)
Signing
- Choose random $k$
- Compute $R = k \cdot G$
- Compute $e = H(R | pk | M)$
- Compute $s = k + e \cdot sk$
$$ \sigma = (R, s) $$
Verification
$$ s \cdot G \stackrel{?}{=} R + H(R | pk | M) \cdot pk $$
Advantages:
- Simple and elegant
- Supports key aggregation (MuSig)
- Smaller multi-signatures
Comparison
| Algorithm | Signature Size | Speed | Security | Use Case |
|---|---|---|---|---|
| Ed25519 | 64 bytes | Very fast | High | Modern, SSH |
| ECDSA | ~72 bytes | Fast | High | Bitcoin, Ethereum |
| RSA-PSS | 256-512 bytes | Slow | High | Legacy, TLS |
| Schnorr | 64 bytes | Fast | High | Bitcoin Taproot |
Related Snippets
- Asymmetric Encryption & Key Exchange
Asymmetric (public-key) cryptography with mathematical foundations, including … - Cryptographic Hash Functions
Cryptographic hash functions with mathematical properties and practical … - Encrypt/Decrypt with Key Pairs
Encrypt and decrypt data using public/private key pairs and derive symmetric … - Generate Public/Private Key Pairs
Generate public/private key pairs on Linux for various cryptographic purposes. … - Hash and Sign Text with Key Pairs
Hash and digitally sign text using public/private key pairs. Hash Text (OpenSSL) … - Homomorphic Encryption Schemes
Homomorphic encryption allows computation on encrypted data without decryption, … - Key Derivation Functions
Key Derivation Functions (KDFs) for password hashing and key derivation. … - Key Sharding (Secret Sharing)
Key sharding splits a secret into multiple shares where a threshold of shares is … - Multi-Signature (Multisig) Schemes
Multi-signature schemes require multiple parties to sign a transaction or … - PGP Signature Operations
PGP/GPG signature operations for files, emails, and git commits. Generate GPG … - Setup PGP with Git (Auto-sign Commits)
Setup GPG/PGP to automatically sign Git commits and tags. Generate GPG Key for … - Symmetric Encryption
Symmetric encryption algorithms with mathematical foundations and practical … - Threshold Signatures
Threshold signatures enable a group to sign messages without ever reconstructing …