-
Notifications
You must be signed in to change notification settings - Fork 130
feat(Protocols): Key exchange protocols and Diffie-Hellman #473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChristianoBraga
wants to merge
2
commits into
leanprover:main
Choose a base branch
from
Beneficial-AI-Foundation:key_exchange_pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
Cslib/Systems/Distributed/Protocols/Cryptographic/KeyExchange/Basic.lean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /- | ||
| Copyright (c) 2026 Christiano Braga. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Christiano Braga | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Mathlib.Tactic | ||
|
|
||
| @[expose] public section | ||
|
|
||
| /-! | ||
| # Key Exchange Protocol | ||
|
|
||
| An abstract typeclass capturing the structure of a two-party key-exchange protocol. A | ||
| protocol is given by three types — `PrivateKey`, `PublicValue`, `SharedSecret` — together | ||
| with: | ||
|
|
||
| * `pub : PrivateKey → PublicValue`, the value a party publishes; | ||
| * `sharedSecret : PublicValue → PrivateKey → SharedSecret`, what each party computes from | ||
| the peer's public value and its own private key; | ||
| * `agreement`, the correctness law | ||
| `sharedSecret (pub β) α = sharedSecret (pub α) β` for all `α, β`. | ||
|
|
||
| Concrete protocols (e.g. Diffie-Hellman) arise by instantiating the three types and | ||
| supplying the three fields. This file captures only the correctness equation; security | ||
| assumptions belong to concrete instances. | ||
|
|
||
| ## Reference | ||
|
|
||
| * [Boneh, Shoup, *A Graduate Course in Applied Cryptography*][BonehShoup], Section 10.4.1 | ||
| -/ | ||
|
|
||
| namespace Cslib.Systems.Distributed.Protocols.Cryptographic.KeyExchange | ||
|
|
||
| universe u v w | ||
|
|
||
| class KeyExchangeProtocol | ||
| (PrivateKey : Type u) (PublicValue : Type v) | ||
| (SharedSecret : Type w) where | ||
| /-- Compute public value from private key. This is sent to the other party. -/ | ||
| pub : PrivateKey → PublicValue | ||
| /-- Compute shared secret from received public value and own private key. -/ | ||
| sharedSecret : PublicValue → PrivateKey → SharedSecret | ||
| /-- Agreement: both parties compute the same shared secret. -/ | ||
| agreement : ∀ (α β : PrivateKey), | ||
| sharedSecret (pub β) α = sharedSecret (pub α) β | ||
|
|
||
| end Cslib.Systems.Distributed.Protocols.Cryptographic.KeyExchange | ||
76 changes: 76 additions & 0 deletions
76
Cslib/Systems/Distributed/Protocols/Cryptographic/KeyExchange/DiffieHellman.lean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /- | ||
| Copyright (c) 2026 Christiano Braga. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Christiano Braga | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Mathlib.Tactic | ||
| public import Cslib.Systems.Distributed.Protocols.Cryptographic.KeyExchange.Basic | ||
|
|
||
| @[expose] public section | ||
|
|
||
| /-! | ||
| # Diffie-Hellman protocol | ||
|
|
||
| Diffie-Hellman key exchange as an instance of `KeyExchangeProtocol` in a finite cyclic | ||
| group `G` of cardinality `q` with a generator `g`. Two parties sample private keys | ||
| `α, β : ZMod q`, publish `g ^ α.val` and `g ^ β.val`, and each raises the peer's public | ||
| value to its own private key. Correctness: both arrive at `g ^ (α · β).val`. | ||
|
|
||
| ## Scope | ||
|
|
||
| This file formalizes only the *correctness* (agreement) of the exchange. | ||
|
|
||
| ## Main declarations | ||
|
|
||
| * `DiffieHellman g q hq hg` — the protocol, extending `KeyExchangeProtocol (ZMod q) G G`. | ||
| Two setup invariants are carried as fields: | ||
| - `hq : Fintype.card G = q` pins down `q` as the cardinality of `G`. This is what lets | ||
| private keys live in `ZMod q` faithfully: by Lagrange `x ^ Fintype.card G = 1` for every | ||
| `x : G`, hence `hq` gives `x ^ q = 1`, so exponents depend only on their residue | ||
| modulo `q`. | ||
| - `hg : orderOf g = q` says `g` has order `q`. Combined with `hq`, it means | ||
| `orderOf g = Fintype.card G`, which in a cyclic group is exactly the statement that `g` | ||
| is a generator. | ||
| * `secret_eq` — `(g ^ β.val) ^ α.val = g ^ (α * β).val`: the algebraic core of agreement. | ||
|
|
||
| ## Reference | ||
|
|
||
| * [Boneh, Shoup, *A Graduate Course in Applied Cryptography*][BonehShoup], Section 10.4.2 | ||
| -/ | ||
|
|
||
| namespace Cslib.Systems.Distributed.Protocols.Cryptographic.KeyExchange.DH | ||
|
|
||
| open KeyExchange | ||
|
|
||
| class DiffieHellman {G : Type u} [Group G] [Fintype G] [IsCyclic G] | ||
| (g : G) (q : ℕ) (hq : Fintype.card G = q) (hg : orderOf g = q) | ||
| extends KeyExchangeProtocol (ZMod q) G G where | ||
| pub α := g ^ α.val | ||
| sharedSecret u α := u ^ α.val | ||
| agreement := by | ||
| intro α β | ||
| show (g ^ β.val) ^ α.val = (g ^ α.val) ^ β.val | ||
| rw [← pow_mul, ← pow_mul, mul_comm] | ||
|
|
||
| variable {G : Type u} [Group G] [Fintype G] | ||
| variable (g : G) (q : ℕ) (hq : Fintype.card G = q) | ||
| include hq | ||
|
|
||
| /-- In a finite group of cardinality `q`, exponents may be reduced modulo `q`. Together with | ||
| `ZMod.val_mul` this lets `ℕ`-valued exponents be treated as living in `ZMod q`. -/ | ||
| private lemma pow_mod_q (x : G) (n : ℕ) : | ||
| x ^ (n % q) = x ^ n := by | ||
| conv_rhs => rw [← Nat.div_add_mod n q] | ||
| have hxq : x ^ q = 1 := hq ▸ pow_card_eq_one | ||
| rw [pow_add, pow_mul, hxq, one_pow, one_mul] | ||
|
|
||
| /-- The Diffie-Hellman shared secret `(g ^ β.val) ^ α.val` equals `g ^ (α * β).val`, | ||
| independently of which party computes it. This is the algebraic core of `agreement`. -/ | ||
| theorem secret_eq (α β : ZMod q) : | ||
| (g ^ β.val) ^ α.val = g ^ (α * β).val := by | ||
| rw [← pow_mul, ZMod.val_mul, mul_comm β.val α.val, pow_mod_q q hq] | ||
|
|
||
| end Cslib.Systems.Distributed.Protocols.Cryptographic.KeyExchange.DH |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should define this in a namespace, not in the root. This is true across the whole file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes!, I will fix it.