Skip to content

feat(Cryptography): formalise perfect secrecy and the one-time pad#464

Merged
fmontesi merged 12 commits intoleanprover:mainfrom
SamuelSchlesinger:perfect-secrecy
Apr 15, 2026
Merged

feat(Cryptography): formalise perfect secrecy and the one-time pad#464
fmontesi merged 12 commits intoleanprover:mainfrom
SamuelSchlesinger:perfect-secrecy

Conversation

@SamuelSchlesinger
Copy link
Copy Markdown
Contributor

Adds Cslib.Cryptography.PerfectSecrecy with information-theoretic private-key encryption schemes and perfect secrecy following Katz-Lindell, Chapter 2:

  • EncScheme: private-key encryption (Definition 2.1)
  • PerfectlySecret: perfect secrecy (Definition 2.3)
  • perfectlySecret_iff_ciphertextIndist: ciphertext indistinguishability characterization (Lemma 2.5)
  • otp: the one-time pad construction (Construction 2.9)
  • otp_perfectlySecret: the OTP is perfectly secret (Theorem 2.10)
  • perfectlySecret_keySpace_ge: Shannon's theorem, |K| ≥ |M| (Theorem 2.12)

For some context, Katz reviewed this in its original home: SamuelSchlesinger/introduction-to-modern-cryptography#1.

Adds `Cslib.Cryptography.PerfectSecrecy` with information-theoretic
private-key encryption schemes and perfect secrecy following
Katz-Lindell, Chapter 2:

- `EncScheme`: private-key encryption (Definition 2.1)
- `PerfectlySecret`: perfect secrecy (Definition 2.3)
- `perfectlySecret_iff_ciphertextIndist`: ciphertext indistinguishability
  characterization (Lemma 2.5)
- `otp`: the one-time pad construction (Construction 2.9)
- `otp_perfectlySecret`: the OTP is perfectly secret (Theorem 2.10)
- `perfectlySecret_keySpace_ge`: Shannon's theorem, |K| ≥ |M|
  (Theorem 2.12)
@SamuelSchlesinger
Copy link
Copy Markdown
Contributor Author

I'd be quite interested in helping with future review in this Cryptography folder, if there is interest in including it.

Comment thread Cslib/Cryptography/PerfectSecrecy/Internal/OneTimePad.lean Outdated
Comment thread Cslib/Cryptography/PerfectSecrecy/Internal/OneTimePad.lean Outdated
Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Encryption.lean Outdated
Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Encryption.lean Outdated
Comment thread Cslib/Cryptography/PerfectSecrecy/PerfectSecrecy.lean Outdated
Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Defs.lean Outdated
Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Defs.lean Outdated
Comment thread Cslib/Cryptography/PerfectSecrecy/OneTimePad.lean Outdated
Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Internal/PerfectSecrecy.lean
Comment thread Cslib/Cryptography/PerfectSecrecy/Internal/PerfectSecrecy.lean Outdated
Comment thread Cslib/Cryptography/PerfectSecrecy/Internal/PerfectSecrecy.lean Outdated
Comment thread Cslib/Cryptography/PerfectSecrecy/Internal/PerfectSecrecy.lean Outdated
Comment thread Cslib/Cryptography/PerfectSecrecy/Internal/PerfectSecrecy.lean Outdated
Comment thread Cslib/Cryptography/PerfectSecrecy/Internal/PerfectSecrecy.lean Outdated
Copy link
Copy Markdown

@linesthatinterlace linesthatinterlace left a comment

Choose a reason for hiding this comment

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

In general I think this is a good start on this stuff. My concern right now is about theorem organization and structure (including naming), and about whether any of the results you've proved should be Mathlib results or at least separated out (i.e. they are not really results about cryptographic concepts specifically).

@SamuelSchlesinger
Copy link
Copy Markdown
Contributor Author

In general I think this is a good start on this stuff. My concern right now is about theorem organization and structure (including naming), and about whether any of the results you've proved should be Mathlib results or at least separated out (i.e. they are not really results about cryptographic concepts specifically).

Thanks for the feedback! I'll work on a follow up commit today that addresses each of these concerns. Re: Mathlib results, not totally sure how to approach it, I'll wait for more advice there.

@fmontesi
Copy link
Copy Markdown
Collaborator

fmontesi commented Apr 1, 2026

Regarding the directory name, is it open also to cryptology stuff in the future? If so, we could consider calling it simply 'Crypto'.

@SamuelSchlesinger
Copy link
Copy Markdown
Contributor Author

Regarding the directory name, is it open also to cryptology stuff in the future? If so, we could consider calling it simply 'Crypto'.

Great idea. I was actually considering adding some of the ciphers from the first chapter of the book originally, that could be fun to have around pedagogically.

Comment thread Cslib/Cryptography/PerfectSecrecy/Internal/PerfectSecrecy.lean Outdated
Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Defs.lean
Comment thread Cslib/Cryptography/PerfectSecrecy/PerfectSecrecy.lean Outdated
Comment thread Cslib/Cryptography/PerfectSecrecy.lean Outdated
Addresses reviewer comments from linesthatinterlace and fmontesi.

Definitions and naming:
- Define `CiphertextIndist` predicate for ciphertext indistinguishability
- Define `posteriorMsgDist : PMF M` proving the posterior is a valid
  distribution, with `perfectlySecret_iff_posteriorEq`
- Rename `backward`/`forward` to `perfectlySecret_of_ciphertextIndist`
  and `ciphertextIndist_of_perfectlySecret`
- Extract `xor_right_eq_iff` and `encrypt_key_injective` as lemmas

OTP and types:
- Use `BitVec l` instead of `Fin l → Bool` for the one-time pad,
  with a `Fintype (BitVec n)` instance via `Fin (2^n)`

Generalisations:
- Weaken Shannon's bound from `[Fintype]` to `[Finite]` with `Nat.card`
- Shorten `perfectlySecret_iff_indep` proof

Housekeeping:
- Add KatzLindell2020 to references.bib (3rd ed., CRC Press, 2020)
- Fix citation key from KatzLindell2021 to KatzLindell2020
- Eliminate `PerfectSecrecy/PerfectSecrecy/` double directory
- Remove umbrella re-export module (not a pattern used in cslib)
- Revert unrelated CI workflow and .gitignore changes
@SamuelSchlesinger
Copy link
Copy Markdown
Contributor Author

I believe all the current feedback should be addressed now, let me know if there are more nits or issues.

Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Internal/PerfectSecrecy.lean
Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Internal/PerfectSecrecy.lean Outdated
Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Internal/PerfectSecrecy.lean Outdated
Replace the pointwise posteriorMsgProb definition with posteriorMsgDist
(a proper PMF) and redefine PerfectlySecret as equality of distributions.
Extract reusable PMF bind/posterior lemmas into PMFUtilities. Golf proofs
across all PerfectSecrecy files (~16 lines removed).
Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Defs.lean
Comment thread Cslib/Crypto/Protocols/PerfectSecrecy/Encryption.lean Outdated
open PMF ENNReal

universe u
variable {α β : Type u}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Where possible these should be in different universes. You might need to specify them separately per theorem

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Given that these are going to be upstreamed anyways, I'll make this change in those/that PR/s rather than making this one more wordy.

Copy link
Copy Markdown
Contributor

@Shreyas4991 Shreyas4991 Apr 7, 2026

Choose a reason for hiding this comment

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

I think you want to have the most general version here. Upstreaming should not be anything more than a chore. Consider that the upatreaming process is a slow one and people will rely on this definition until upstreaming is complete.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reading through mathlib now, I feel like this might already be there in terms of kernels. I wonder if I can get rid of this entirely...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, no. I'd need to rephrase a lot more.

@SamuelSchlesinger
Copy link
Copy Markdown
Contributor Author

I think everyone's comments have been addressed. Let me know if there is any more feedback.

@SamuelSchlesinger
Copy link
Copy Markdown
Contributor Author

SamuelSchlesinger commented Apr 7, 2026

@linesthatinterlace I made a PR to my fork of Mathlib with the posterior definition and friends here: SamuelSchlesinger/mathlib4#1. Hoping for some pre-review before bothering the mathlib reviewers for the first time :)

@fmontesi
Copy link
Copy Markdown
Collaborator

fmontesi commented Apr 7, 2026

LGTM. @linesthatinterlace @eric-wieser can anybody give me a ping of approval? Then I'll merge.

@linesthatinterlace
Copy link
Copy Markdown

As long as there is a plan to back-port relevant things to Mathlib and improve proof later, I am happy for this to merge.

@SamuelSchlesinger
Copy link
Copy Markdown
Contributor Author

I have a PR to Mathlib to do so here: leanprover-community/mathlib4#37938. I'm happy to make a followup to Cslib once we have access to these. Let me know what other aspects of these proofs needs to be improved in the meantime.

@fmontesi fmontesi enabled auto-merge April 15, 2026 12:24
@SamuelSchlesinger
Copy link
Copy Markdown
Contributor Author

It got mad at my unicode caution symbols :) I'll delete those and run the lints locally to make sure everything works before I push back up.

auto-merge was automatically disabled April 15, 2026 12:42

Head branch was pushed to by a user without write access

@SamuelSchlesinger
Copy link
Copy Markdown
Contributor Author

@fmontesi I think you might need to turn back on auto-merge and trigger the ci-checks again.

@chenson2018 chenson2018 enabled auto-merge April 15, 2026 12:43
@chenson2018
Copy link
Copy Markdown
Collaborator

I've put it back in the queue.

@fmontesi fmontesi disabled auto-merge April 15, 2026 12:45
@fmontesi fmontesi enabled auto-merge April 15, 2026 12:46
@fmontesi
Copy link
Copy Markdown
Collaborator

Whoops, concurrent PR management, sorry. Back in the queue. :-)

@fmontesi fmontesi added this pull request to the merge queue Apr 15, 2026
Merged via the queue into leanprover:main with commit 906574d Apr 15, 2026
2 checks passed
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.

7 participants