OTP.PerfectSecrecy.lean #
This file proves properties of perfect secrecy in the one-time pad (OTP) cryptographic scheme. It includes:
- The definition of perfect secrecy
- The proof that OTP achieves perfect secrecy
- The relationship between the key and ciphertext distributions
- The uniformity of the ciphertext distribution given a message
- The uniformity of the ciphertext distribution overall
- The law of total probability for the ciphertext distribution
Files in this series:
- OTP.Basic: Basic definitions and operations for OTP
- OTP.KeyUniqueness: Properties of keys in OTP
- OTP.Distributions: Probability distributions related to OTP
- OTP.PerfectSecrecy: Properties of perfect secrecy in OTP
- OTP.Examples: Concrete examples and demos of OTP operations and properties
- OTP.SimpleSecrecy: Simplified version of OTP.PerfectSecrecy where we assume the message distribution is uniform.
A brief note about coercions #
Coercion is a mechanism to convert a term of one type to another to prevent a type error.
For instance, given n : ℕ
and a function that expects an integer f (x : ℤ)
, you can
explicitly write f ↑n
to show the coercion from ℕ
to ℤ
.
As far as I can tell, ↑a
signifies coercion of "standard" types, while ⇑e
signifies
coercion of more complicated types.
To type the coercion symbol in the VSCode editor with the Lean4 extension,
For
↑
, type\u
or\uparrow
followed by a space or tab.For
⇑
, type\u=
or\Uparrow
followed by a space or tab.
You can discover all available Unicode symbol abbreviations within VSCode by
opening the Command Palette (Ctrl+Shift+P
or Cmd+Shift+P
on Mac) and
searching for "Lean 4: Show Unicode Input Abbreviations".
A brief note about universe levels #
Types form a hierarchy that is parameterized by universe levels. The assertion
that α
is a type at universe level ℓ
is expressed as α : Type ℓ
. Fortunately,
in most cases we can elide this technicality by declaring our types with the
special Lean syntax α : Type*
, which tells Lean to select a fresh universe level
for α
and make that level an implicit parameter.
LEMMA 1. Mapping a uniform PMF through a bijection is uniform #
This lemma involves proving that two PMFs are equal. How is this done?
Let's look at the definition of PMF
in
mathlib/Mathlib/Probability/ProbabilityMassFunction/Basic.lean
.
definition of PMF:
def PMF.{u} (α : Type u) : Type u :=
{ f : α → ℝ≥0∞ // HasSum f 1 }
namespace PMF
instance instFunLike : FunLike (PMF α) α ℝ≥0∞ where
coe p a := p.1 a
coe_injective' _ _ h := Subtype.eq h
@[ext]
protected theorem ext {p q : PMF α} (h : ∀ x, p x = q x) : p = q :=
DFunLike.ext p q h
There is an instance of FunLike
for PMF α
, which means that we
can treat a PMF as a function from α
to ℝ≥0∞
. This is crucial for
proving that two PMFs are equal: we can show that they assign the same
probability to each possible value of the random variable. Indeed, the
ext
theorem for PMF
is essentially function extensionality for PMFs.
LEMMA 2. The ciphertext-given-message distribution is uniform #
Equations
Example #
For a 3-bit message and ciphertext, the conditional distribution is uniform:
∀ c, P(C = c | M = m) = 1/8
LEMMA 2: The overall ciphertext distribution μC
is also uniform.----------- #
The probability P(C = c)
that ciphertext c
is observed (which is (μC μM) c
),
is uniform over the ciphertext space. That is: (μC μM) c = 1 / (card (Ciphertext n))
Since card (Key n) = card (Ciphertext n)
(due to xorEquiv
), this would imply
that (μC μM) c
is also equal 1 / card (Key n)
.
Law of Total Probability for Discrete Distributions in Lean 4 #
REVIEW: Probability in Lean/Mathlib #
In Mathlib, discrete probability distributions are represented using the PMF
(Probability Mass Function) type.
PMF α
represents a probability distribution over a typeα
- For
p : PMF α
anda : α
, the expressionp a
gives the probability of outcomea
- Probabilities are represented as
NNReal
(non-negative real numbers) in [0, ∞) - When doing arithmetic, we often coerce to
ENNReal
(extended non-negative reals) in [0, ∞]
REVIEW: Random Variables and Transformations #
In probability theory, if X is a random variable with distribution μX, and Y = f(X) for some function f, then the distribution of Y can be computed using:
PMF.map f μX
- the distribution of f(X)PMF.bind μX g
- for dependent distributions, where g : α → PMF β
REVIEW: The Law of Total Probability #
For random variables X and Y, the law of total probability states: P(Y = y) = Σ_x P(Y = y , X = x) = Σ_x P(Y = y | X = x) P(X = x)
In our case:
- X is the message M with distribution μM
- Y is the ciphertext C with distribution μC
- P(C = c | M = m) is given by μC_M m c
The Most Basic Statement: P(C = c) = Σ_{m,k} P(C = c ∧ M = m ∧ K = k) #
Perfect Secrecy Theorem --------------------------------------------- #
The theorem states that the probability of a ciphertext given a specific plaintext is equal to the probability of that plaintext, which is the essence of perfect secrecy. This means that knowing the ciphertext does not give any information about the plaintext. The proof uses the uniformity of the ciphertext distribution and the independence of the key.