Digital signatures and DSA
Digital signatures prove who signed a message and that it was not changed; DSA is a standardized signature scheme based on the discrete logarithm problem and uses parameters p, q, g, a private x and public y = g^x mod p.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Digital signatures let an engineer ensure a message came from a claimed sender and was not altered. You use them for transaction signing, smart-contract interactions, committing software, and any blockchain operation that requires authentication and non-repudiation. DSA (the Digital Signature Algorithm) is one common scheme used in many protocols and textbooks; understanding it is required for questions on signature generation, verification, and attacks (for example, broken nonce reuse).
Key ideas
Private key: secret value only known to the signer. Public key: value derived from the private key, published so verifiers can check signatures.
Hash function: a function H mapping arbitrary-length input to a fixed-length output. Collision-resistant means it is computationally infeasible to find m1 ≠ m2 with H(m1)=H(m2). DSA assumes H is collision-resistant; the hash value is converted to an integer z, then reduced modulo q (or truncated to the leftmost N bits equal to the bitlength of q) for use in DSA.
Discrete Logarithm Problem (DLP): given prime p, generator g and value y = g^x mod p, it is hard to find x. DSA security assumes DLP is hard in the subgroup used. Define the difficulty precisely: for chosen p, q and g of order q, no efficient algorithm exists to find x from (p,g,y) except with negligible probability.
DSA uses domain parameters (p, q, g): p is a prime modulus, q is a prime divisor of p-1, and g is an element of multiplicative group modulo p with order q. The signer chooses private key x with 0 < x < q and public key y = g^x mod p.
Signing uses an ephemeral random nonce k (also called an ephemeral key). This must be random and never reused. If k is reused for two different messages with same x, the private key can be recovered.
Exact DSA definition (Signature generation and verification): see the Formulas section for precise algorithmic statements. Below is the high-level flow you should be able to draw in an exam: two boxes labelled Signer and Verifier. Under Signer: inputs (m, x, params), compute H = H(m), pick k, compute r and s, output (r,s). Under Verifier: inputs (m, r, s, y, params), compute H = H(m), compute w = s^{-1} mod q, u1 = H·w mod q, u2 = r·w mod q, compute v = (g^{u1}·y^{u2} mod p) mod q, accept if v = r.
Correctness theorem (what you must be able to derive step by step): given signature (r,s) computed honestly, the verification calculation yields v = r. The derivation uses y = g^x and algebra modulo q and p; derive that exponent becomes k (mod q) and so v = (g^k mod p) mod q = r. A concise derivation: from s = k^{-1}(H + x·r) mod q we have k ≡ (H + x·r)·w mod q with w = s^{-1}; then g^{u1}·y^{u2} ≡ g^{H·w}·(g^x)^{r·w} ≡ g^{H·w + x·r·w} ≡ g^{k} (mod p) because exponents are taken modulo q in the order‑q subgroup, hence v = (g^k mod p) mod q = r.
Security assumptions and warnings: DSA security requires (1) p, q, g chosen correctly (q divides p-1; g has order q), (2) private key x kept secret, (3) ephemeral k chosen uniformly at random for each signature and kept secret. Deterministic nonces (RFC 6979) are allowed and recommended: they compute k deterministically from H(m) and x in a way that still produces unique per-message k.
Formulas
q | (p - 1) — q divides p−1. Applies when generating DSA domain parameters; p and q are primes (dimensionless integers).
y = g^x mod p — public key. x is the private key (integer, 0 < x < q), y is public (integer 1..p-1), all arithmetic modulo p.
r = (g^k mod p) mod q — first signature component. k is ephemeral nonce with 0 < k < q.
s = k^{-1} · (H(m) + x·r) mod q — second signature component. k^{-1} is modular inverse of k modulo q; H(m) is hash reduced modulo q. If r = 0 or s = 0, the signer must choose a new k and recompute.
w = s^{-1} mod q — modular inverse of s modulo q used by verifier.
u1 = H(m) · w mod q — intermediate value for verification.
u2 = r · w mod q — intermediate value for verification.
v = ( (g^{u1} · y^{u2}) mod p ) mod q — verification value; signature valid iff v = r.
All symbols: p (prime modulus, dimensionless integer), q (prime order divisor of p−1), g (generator of the order-q subgroup), x (private key integer), y (public key integer), k (nonce integer), r,s,u1,u2,w,v,H(m) (integers modulo q or p as indicated). No SI units; apply when using DSA.
Worked examples
Example 1 — small numeric DSA signing and verification (student-level arithmetic, show each step):
Parameters (small for teaching): p = 23, q = 11 (note q | p−1 because 11 | 22), compute g = h^{(p-1)/q} mod p with h = 2: g = 2^2 mod 23 = 4. Choose private key x = 3 (0 < x < q). Public key y = g^x mod p = 4^3 mod 23 = 64 mod 23 = 18.
Message hash (example): H = H(m) = 9 (treat hash already reduced mod q). Choose nonce k = 2 (0 < k < q).
Signature generation:
- Compute g^k mod p = 4^2 mod 23 = 16.
- r = (g^k mod p) mod q = 16 mod 11 = 5.
- Compute k^{-1} mod q: inverse of 2 modulo 11 is 6 (because 2·6 = 12 ≡ 1 mod 11).
- Compute s = k^{-1} · (H + x·r) mod q = 6 · (9 + 3·5) mod 11 = 6 · (9 + 15 = 24) mod 11 = 6 · 2 mod 11 = 12 mod 11 = 1.
Signature is (r, s) = (5, 1). Final answer (signature): (5, 1).
Signature verification:
- Check 0 < r < q and 0 < s < q: yes (5 and 1 are in 1..10).
- Compute w = s^{-1} mod q = inverse of 1 mod 11 = 1.
- u1 = H·w mod q = 9·1 mod 11 = 9.
- u2 = r·w mod q = 5·1 mod 11 = 5.
- Compute g^{u1} mod p = 4^9 mod 23. Using repeated squaring produce 4^9 mod 23 = 13 (compute: 4^1=4, 4^2=16, 4^3=18, 4^4=3, 4^5=12, 4^6=2, 4^7=8, 4^8=9, 4^9=13).
- Compute y^{u2} mod p = 18^5 mod 23 = 3 (compute 18^2=2, 18^4=4, 18^5=4·18=72 mod 23 = 3).
- Multiply: (g^{u1}·y^{u2}) mod p = 13·3 mod 23 = 39 mod 23 = 16.
- v = 16 mod q = 16 mod 11 = 5.
Compare v to r: v = 5 and r = 5 match, so signature valid. Final verification: valid.
Example 2 — why reusing k breaks private key (short derivation + numeric demo):
Derivation: for messages with hashes H1 and H2 signed with same k, we have s1 = k^{-1}(H1 + x r) and s2 = k^{-1}(H2 + x r). Subtract: s1 − s2 = k^{-1}(H1 − H2) ⇒ k = (H1 − H2)·(s1 − s2)^{-1} (mod q). Once k known, from s = k^{-1}(H + x r) we rearrange x = (s·k − H)·r^{-1} (mod q). Thus reuse of k leaks x.
Numeric demo with previous params: H1 = 9 produced s1 = 1 (from Example 1). Suppose H2 = 5 and same k = 2 gives r = 5 (same) and compute s2 = k^{-1}(H2 + x r) = 6 · (5 + 3·5 = 20) mod 11 = 6·9 (20 mod 11 = 9) so 6·9 = 54 mod 11 = 54 − 44 = 10. Attacker computes k = (9 − 5)·(1 − 10)^{-1} mod 11 = 4·(−9 ≡ 2)^{-1} = 4·6 = 24 ≡ 2, recovers k correctly, then x = (s1·k − H1)·r^{-1} = (1·2 − 9 = −7 ≡ 4)·5^{-1}; inverse of 5 mod 11 is 9, so x = 4·9 = 36 ≡ 3, recovers x. This shows reuse of k is catastrophic. Final answer: private key recovered.
Common mistakes
- Forgetting to check r and s are non-zero and in interval (0, q).
- Mixing mod p and mod q reductions (verification does multiplication mod p then reduction mod q).
- Confusing DSA with RSA signature algorithm (DSA uses discrete logs; RSA uses integer factorization and modular exponent with public exponent e).
- Using incorrect inverse (mod q vs mod p) or computing the inverse modulo the wrong modulus.
- Not proving correctness by showing v = r step-by-step; examiners expect the algebraic manipulation.
For your exam
Types of questions you will see: definitions (define digital signature, nonce, discrete log), short notes (advantages/limitations of DSA, role of hash function), derivations (prove v = r), numericals (compute a full sign/verify with small p,q), attack analysis (show private-key leakage when k reused), and diagrams (draw sign and verify flow). A full-marks answer always:
- Gives clear definitions of every bold term used.
- States assumptions (p, q prime; q | p−1; g of order q; hash reduction; k unique and secret).
- For derivations: write each modular step, indicate whether arithmetic is mod p or mod q, and conclude v = r.
- For numericals: show every arithmetic step, compute inverses explicitly, check ranges of r and s and final verification equality.
- For attack questions: show algebraic steps to recover k or x and do a small numeric demonstration if asked.
Quick check
- What is the role of the ephemeral nonce k in DSA?
- Write the DSA verification equation that must equal r.
- Why is reusing k for two different messages dangerous?
Answers: 1) k is a fresh random secret used once per signature to ensure non-determinism and to hide x; if reused it leaks x. 2) v = ((g^{u1}·y^{u2} mod p) mod q) with u1 = H·s^{-1} mod q and u2 = r·s^{-1} mod q; signature valid iff v = r. 3) Because from s1 and s2 with same r you can solve for k and then for x (private key) using modular arithmetic, so reuse breaks secrecy of x.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?