Skip to content

feat(Protocols): Key exchange protocols and Diffie-Hellman#473

Open
ChristianoBraga wants to merge 2 commits intoleanprover:mainfrom
Beneficial-AI-Foundation:key_exchange_pr
Open

feat(Protocols): Key exchange protocols and Diffie-Hellman#473
ChristianoBraga wants to merge 2 commits intoleanprover:mainfrom
Beneficial-AI-Foundation:key_exchange_pr

Conversation

@ChristianoBraga
Copy link
Copy Markdown

@ChristianoBraga ChristianoBraga commented Apr 6, 2026

Summary

  • Add a KeyExchangeProtocol class capturing the structure of key exchange with pub, sharedSecret, and an agreement property ensuring both parties compute the same shared secret.
  • Add Diffie-Hellman key exchange as an instance in a finite cyclic group of order q with a generator g, carrying hq : Fintype.card G = q and hg : orderOf g = q. Includes helper lemmas pow_mod_q and secret_eq.

Scope

This PR formalises only the correctness (agreement) of the exchange. It does not model the Diffie-Hellman security assumptions (DLP, CDH, DDH) or any adversarial setting — those require a probabilistic framework to be added separately.

Files

  • Cslib/Systems/Distributed/Protocols/Cryptographic/KeyExchange/Basic.lean — abstract key-exchange typeclass.
  • Cslib/Systems/Distributed/Protocols/Cryptographic/KeyExchange/DiffieHellman.lean — Diffie-Hellman instance.
  • references.bib — add BonehShoup entry (online draft).

References

  • Boneh & Shoup, A Graduate Course in Applied Cryptography, Section 10.4.

Test plan

  • lake build passes with no errors on both files.
  • CI workflow succeeds.

AI disclosure

Claude Code (Anthropic) was used as a pair-programming assistant throughout this PR: drafting and iterating on the typeclass design, suggesting Mathlib lemmas (e.g. pow_card_eq_one, Nat.div_add_mod), proposing and refining the module documentation, and helping address reviewer feedback. All algebraic content and Lean proofs were reviewed and accepted by the author; the underlying mathematics follows Boneh & Shoup, Section 10.4.

@@ -0,0 +1,99 @@
/-
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file doesn't build because Diffie-Hellman.lean isn't a valid module name.

Copy link
Copy Markdown
Author

@ChristianoBraga ChristianoBraga Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since my contribution is stand alone, I only compiled the files themselves and not the entire cslib.
I will fix it!

Copy link
Copy Markdown
Contributor

@SamuelSchlesinger SamuelSchlesinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, it appears the author did not build this to ensure it worked locally, so there are several issues. I tried to give a thorough review despite this.

Let G := {gᵃ : a = 0, …, q − 1}, so that G is a subset of ℤₚ* of cardinality q. It is not hard
to see that G is closed under multiplication and inversion; that is, for all u, v ∈ G,
we have u·v ∈ G and u⁻¹ ∈ G. Indeed, gᵃ · gᵇ = gᵃ⁺ᵇ = gᶜ with c := (a + b) mod q, and (gᵃ)⁻¹ = gᵈ
with d := (−a) mod q. In the language of algebra, G is called a subgroup of the group ℤₚ*.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module documentation describes a specific cryptographic setup with primes p, q, and a subgroup of ℤₚ*, but this instance is for any CommGroup G, any g : G, and any q : ℕ. The documentation should say the formalization only models the agreement equation, not the full DH setup/security assumptions. It should also be as general as the definition.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will rewrite the whole documentation to make it more consistent.

agreement := by
intro α β
show (g ^ β.val) ^ α.val = (g ^ α.val) ^ β.val
rw [← pow_mul, ← pow_mul, mul_comm]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This proves the algebraic agreement equation for exponentiation in a commutative group, but it is
more general and weaker than the Diffie-Hellman setup described in the module documentation. The
instance does not encode that G is a cyclic subgroup of order q, that g has order q, that q is
prime, or that public values live in the subgroup generated by g. If this is intended as a
formalization of Diffie-Hellman, those parameters and invariants should be represented explicitly.
Otherwise, the documentation should describe this as the exponentiation-based key-agreement
skeleton rather than the Diffie-Hellman protocol.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will tight this up, thank you!

Reference:

* [D. Boneh and V. Shoup,V., *A Graduate Course in Applied Cryptography*, One-time pad][BonehShoup],
Section 10.4.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect, referencing the one-time pad not DH.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix it!


Reference:

* [D. Boneh and V. Shoup,V., *A Graduate Course in Applied Cryptography*][BonehShoup],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This citation is missing from the references.bib.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix it!

Section 10.4.
-/

instance DiffieHellmanKE {G : Type u} [CommGroup G] (g : G) (q : ℕ) :
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be an instance. The instance is not actually synthesizable after I renamed it on my end. Just make it a struct and a def.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be in a namespace.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it conceptually wrong to have DH as an instance of KeyExchangeProtocol?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not conceptually wrong, but ergonomically wrong in this case because you're defining it for a free variable G and ZMod q. If you want to do it this way, defining wrappers here is probably the way, or using some singleton tag type. I would personally prefer to use it as a structure myself.


universe u v w

class KeyExchangeProtocol
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Author

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.

omit [Fact q.Prime] in
theorem secret_eq (α β : ZMod q) :
(g ^ β.val) ^ α.val = g ^ (α * β).val := by
rw [← pow_mul, ZMod.val_mul, mul_comm β.val α.val, pow_mod_q hG]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fact q.Prime appears to be dead code.

Copy link
Copy Markdown
Collaborator

@chenson2018 chenson2018 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ChristianoBraga, thanks for your interest in contributing! Can I ask that you disclose any AI usage in the PR description? There seem to be some patterns of AI usage based on the review that was just left.

ChristianoBraga added a commit to Beneficial-AI-Foundation/cslib-protocols-lean that referenced this pull request Apr 22, 2026
- 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>
ChristianoBraga and others added 2 commits April 22, 2026 23:14
Add KeyExchangeProtocol class with agreement property and
Diffie-Hellman as an instance over commutative groups.
- 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants