Skip to content

Commit fad8e56

Browse files
Address PR #473 review feedback
- Rename Diffie-Hellman.lean → DiffieHellman.lean (valid module name). - Align namespaces under ...Cryptographic.KeyExchange; DH lives in ...KeyExchange.DH as a sibling sub-namespace. - Tighten the DiffieHellman class with [Fintype G], hq : Fintype.card G = q, and hg : orderOf g = q, so that g is a generator of a cyclic group of order q (previously under-constrained). - Remove dead Fact q.Prime hypothesis. - Rewrite module docstrings to match the actual generality of the code and add an explicit Scope note: correctness only, not DLP/CDH/DDH. - Upgrade pow_mod_q / secret_eq prose to /-- ... -/ doc-strings. - references.bib: switch BonehShoup entry to the online draft with URL. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent c028505 commit fad8e56

4 files changed

Lines changed: 104 additions & 114 deletions

File tree

Cslib/Systems/Distributed/Protocols/Cryptographic/KeyExchange/Basic.lean

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ public import Mathlib.Tactic
1313
/-!
1414
# Key Exchange Protocol
1515
16-
A *key exchange protocol* allows two parties (Alice and Bob) to establish a shared secret
17-
over an insecure channel.
16+
An abstract typeclass capturing the structure of a two-party key-exchange protocol. A
17+
protocol is given by three types — `PrivateKey`, `PublicValue`, `SharedSecret` — together
18+
with:
1819
19-
The protocol proceeds as follows:
20+
* `pub : PrivateKey → PublicValue`, the value a party publishes;
21+
* `sharedSecret : PublicValue → PrivateKey → SharedSecret`, what each party computes from
22+
the peer's public value and its own private key;
23+
* `agreement`, the correctness law
24+
`sharedSecret (pub β) α = sharedSecret (pub α) β` for all `α, β`.
2025
21-
1. Alice picks a private key α, computes her public value pub(α), and sends it to Bob.
22-
2. Bob picks a private key β, computes his public value pub(β), and sends it to Alice.
23-
3. Alice computes the shared secret as sharedSecret(pub(β), α).
24-
4. Bob computes the shared secret as sharedSecret(pub(α), β).
26+
Concrete protocols (e.g. Diffie-Hellman) arise by instantiating the three types and
27+
supplying the three fields. This file captures only the correctness equation; security
28+
assumptions belong to concrete instances.
2529
26-
The fundamental correctness property (*agreement*) states that both parties compute the same
27-
shared secret:
30+
## Reference
2831
29-
sharedSecret(pub(β), α) = sharedSecret(pub(α), β)
30-
31-
Reference:
32-
33-
* [D. Boneh and V. Shoup,V., *A Graduate Course in Applied Cryptography*][BonehShoup],
34-
Section 10.4.
32+
* [Boneh, Shoup, *A Graduate Course in Applied Cryptography*][BonehShoup], Section 10.4.1
3533
-/
3634

35+
namespace Cslib.Systems.Distributed.Protocols.Cryptographic.KeyExchange
36+
3737
universe u v w
3838

3939
class KeyExchangeProtocol
@@ -46,3 +46,5 @@ class KeyExchangeProtocol
4646
/-- Agreement: both parties compute the same shared secret. -/
4747
agreement : ∀ (α β : PrivateKey),
4848
sharedSecret (pub β) α = sharedSecret (pub α) β
49+
50+
end Cslib.Systems.Distributed.Protocols.Cryptographic.KeyExchange

Cslib/Systems/Distributed/Protocols/Cryptographic/KeyExchange/Diffie-Hellman.lean

Lines changed: 0 additions & 99 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/-
2+
Copyright (c) 2026 Christiano Braga. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Christiano Braga
5+
-/
6+
7+
module
8+
9+
public import Mathlib.Tactic
10+
public import Cslib.Systems.Distributed.Protocols.Cryptographic.KeyExchange.Basic
11+
12+
@[expose] public section
13+
14+
/-!
15+
# Diffie-Hellman protocol
16+
17+
Diffie-Hellman key exchange as an instance of `KeyExchangeProtocol` in a finite cyclic
18+
group `G` of cardinality `q` with a generator `g`. Two parties sample private keys
19+
`α, β : ZMod q`, publish `g ^ α.val` and `g ^ β.val`, and each raises the peer's public
20+
value to its own private key. Correctness: both arrive at `g ^ (α · β).val`.
21+
22+
## Scope
23+
24+
This file formalizes only the *correctness* (agreement) of the exchange.
25+
26+
## Main declarations
27+
28+
* `DiffieHellman g q hq hg` — the protocol, extending `KeyExchangeProtocol (ZMod q) G G`.
29+
Two setup invariants are carried as fields:
30+
- `hq : Fintype.card G = q` pins down `q` as the cardinality of `G`. This is what lets
31+
private keys live in `ZMod q` faithfully: by Lagrange `x ^ Fintype.card G = 1` for every
32+
`x : G`, hence `hq` gives `x ^ q = 1`, so exponents depend only on their residue
33+
modulo `q`.
34+
- `hg : orderOf g = q` says `g` has order `q`. Combined with `hq`, it means
35+
`orderOf g = Fintype.card G`, which in a cyclic group is exactly the statement that `g`
36+
is a generator.
37+
* `secret_eq` — `(g ^ β.val) ^ α.val = g ^ (α * β).val`: the algebraic core of agreement.
38+
39+
## Reference
40+
41+
* [Boneh, Shoup, *A Graduate Course in Applied Cryptography*][BonehShoup], Section 10.4.2
42+
-/
43+
44+
namespace Cslib.Systems.Distributed.Protocols.Cryptographic.KeyExchange.DH
45+
46+
open KeyExchange
47+
48+
class DiffieHellman {G : Type u} [Group G] [Fintype G] [IsCyclic G]
49+
(g : G) (q : ℕ) (hq : Fintype.card G = q) (hg : orderOf g = q)
50+
extends KeyExchangeProtocol (ZMod q) G G where
51+
pub α := g ^ α.val
52+
sharedSecret u α := u ^ α.val
53+
agreement := by
54+
intro α β
55+
show (g ^ β.val) ^ α.val = (g ^ α.val) ^ β.val
56+
rw [← pow_mul, ← pow_mul, mul_comm]
57+
58+
variable {G : Type u} [Group G] [Fintype G]
59+
variable (g : G) (q : ℕ) (hq : Fintype.card G = q)
60+
include hq
61+
62+
/-- In a finite group of cardinality `q`, exponents may be reduced modulo `q`. Together with
63+
`ZMod.val_mul` this lets `ℕ`-valued exponents be treated as living in `ZMod q`. -/
64+
private lemma pow_mod_q (x : G) (n : ℕ) :
65+
x ^ (n % q) = x ^ n := by
66+
conv_rhs => rw [← Nat.div_add_mod n q]
67+
have hxq : x ^ q = 1 := hq ▸ pow_card_eq_one
68+
rw [pow_add, pow_mul, hxq, one_pow, one_mul]
69+
70+
/-- The Diffie-Hellman shared secret `(g ^ β.val) ^ α.val` equals `g ^ (α * β).val`,
71+
independently of which party computes it. This is the algebraic core of `agreement`. -/
72+
theorem secret_eq (α β : ZMod q) :
73+
(g ^ β.val) ^ α.val = g ^ (α * β).val := by
74+
rw [← pow_mul, ZMod.val_mul, mul_comm β.val α.val, pow_mod_q q hq]
75+
76+
end Cslib.Systems.Distributed.Protocols.Cryptographic.KeyExchange.DH

references.bib

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,14 @@ @article{ ShepherdsonSturgis1963
264264
publisher = {Association for Computing Machinery},
265265
address = {New York, NY, USA}
266266
}
267+
268+
@online{ BonehShoup,
269+
author = {Boneh, Dan and Shoup, Victor},
270+
title = {A Graduate Course in Applied Cryptography},
271+
month = {Jan.}
272+
year = {2023},
273+
edition = {Version 0.6},
274+
note = {Free online},
275+
url = {https://toc.cryptobook.us/},
276+
urldate = {2026-04-22}
277+
}

0 commit comments

Comments
 (0)