Finite secrecy systems, perfect secrecy, and the cyclic system of Fig. 5
Definitionshannon_secrecy_systemThis file fixes the model of a finite secrecy system used throughout the mission, following Shannon's Communication Theory of Secrecy Systems (1949), §2 and §10.
A secrecy system consists of three finite sets: the messages , the keys and the cryptograms . Enciphering is a family of maps , one for each key , and each is required to be non-singular (injective), so that a receiver who knows the key recovers the message uniquely. Each key carries an a priori probability with ; these are the enemy cryptanalyst's a priori probabilities for the key choice.
An a priori message distribution is any with and . The message and the key are chosen independently. From these data the file defines the four quantities Shannon uses on p. 680:
- , the probability of the cryptogram given that the message was chosen — the total probability of all keys carrying to ;
- , the probability of obtaining the cryptogram from any cause;
- , the a posteriori probability of the message once has been intercepted;
- perfect secrecy: for every a priori message distribution and every cryptogram of positive probability, for all . Quantifying over all is how Shannon's clause "independently of the values of " is rendered.
The file also defines the Shannon entropy of a finite distribution, and the cyclic system of Fig. 5 on p. 681: messages, keys and cryptograms are the residues modulo , key sends message to the cryptogram , and the keys are equally likely.
Formalization Note Probabilities are plain real numbers together with explicit non-negativity and normalization hypotheses, rather than PMF, so that sums stay finite Finset sums. is a quotient of real numbers, so it evaluates to by Lean's convention when ; every statement about it therefore carries the hypothesis , exactly the cryptograms that can occur. Entropy is taken with the natural logarithm ( in nats) via Real.negMulLog; Shannon leaves the base unspecified, and a change of base only rescales every entropy by the same positive constant.
import Mathlib
namespace ShannonSecrecy
open Finset
/-- A finite probability distribution on a finite type. -/
def IsPMF {α : Type*} [Fintype α] (p : α → ℝ) : Prop :=
(∀ a : α, 0 ≤ p a) ∧ ∑ a : α, p a = 1
/-- A finite secrecy system in the sense of Shannon (1949), §2 and §10:
finitely many messages `M`, keys `K` and cryptograms `E`; enciphering with key `k`
is the map `m ↦ encipher k m`, which is non-singular (injective), so that unique
deciphering is possible when the key is known; every key carries an a priori
probability. -/
structure Cipher (M K E : Type*) [Fintype M] [Fintype K] [Fintype E] [DecidableEq E] where
/-- `encipher k m` is the cryptogram obtained from the message `m` with the key `k`. -/
encipher : K → M → E
/-- Enciphering with a fixed key is non-singular, so deciphering is unique. -/
encipher_injective : ∀ k : K, Function.Injective (encipher k)
/-- The a priori probability of each key. -/
keyProb : K → ℝ
keyProb_nonneg : ∀ k : K, 0 ≤ keyProb k
keyProb_sum : ∑ k : K, keyProb k = 1
variable {M K E : Type*} [Fintype M] [Fintype K] [Fintype E] [DecidableEq E]
/-- `P_M(E)`: the conditional probability of the cryptogram `e` given that the message
`m` was chosen, i.e. the total probability of all keys that transform `m` into `e`. -/
def msgToCrypto (C : Cipher M K E) (m : M) (e : E) : ℝ :=
∑ k : K, if C.encipher k m = e then C.keyProb k else 0
/-- `P(E)`: the probability of obtaining the cryptogram `e` from any cause, when the
a priori message distribution is `p` and the key is chosen independently. -/
def cryptoProb (C : Cipher M K E) (p : M → ℝ) (e : E) : ℝ :=
∑ m : M, ∑ k : K, if C.encipher k m = e then p m * C.keyProb k else 0
/-- `P_E(M)`: the a posteriori probability of the message `m` after the cryptogram `e`
has been intercepted, i.e. the joint probability of `(m, e)` divided by the probability
of `e`. -/
noncomputable def postProb (C : Cipher M K E) (p : M → ℝ) (e : E) (m : M) : ℝ :=
(∑ k : K, if C.encipher k m = e then p m * C.keyProb k else 0) / cryptoProb C p e
/-- Perfect secrecy (Shannon 1949, §10): for every a priori message distribution, and
for every cryptogram that can actually occur, the a posteriori probability of each
message equals its a priori probability. Quantifying over all a priori distributions
renders Shannon's requirement that the equality hold "independently of the values of
`P(M)`". -/
def PerfectSecrecy (C : Cipher M K E) : Prop :=
∀ p : M → ℝ, IsPMF p → ∀ e : E, cryptoProb C p e ≠ 0 → ∀ m : M, postProb C p e m = p m
/-- Shannon entropy `H = -∑ p log p` of a finite distribution, in nats. -/
noncomputable def entropy {α : Type*} [Fintype α] (p : α → ℝ) : ℝ :=
∑ a : α, Real.negMulLog (p a)
/-- The cyclic system of Shannon (1949), §10, Fig. 5: messages, keys and cryptograms are
the residues mod `n`, the key `i` sends the message `j` to the cryptogram `i + j (mod n)`,
and all `n` keys are equally likely. -/
noncomputable def cyclicCipher (n : ℕ) [NeZero n] : Cipher (ZMod n) (ZMod n) (ZMod n) where
encipher k m := k + m
encipher_injective k := fun _ _ h => by simpa using h
keyProb _ := (n : ℝ)⁻¹
keyProb_nonneg _ := by positivity
keyProb_sum := by
have hn : (Fintype.card (ZMod n) : ℝ) = (n : ℝ) := by
simp [ZMod.card]
have hn0 : (n : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr (NeZero.ne n)
rw [Finset.sum_const, Finset.card_univ, nsmul_eq_mul, hn]
field_simp
end ShannonSecrecy
Read-back
What the Lean code literally says, in plain math · Aristotle (Harmonic) — NON-BLIND: same agent that drafted the statement
Provenance note — this read-back is NOT independent testimony. It is non-blind: it was written by the same agent that drafted the Lean statement it describes, working from the source paper and from its own formalization intent, and not by an independent auditor with a fresh context who saw only the Lean code. It therefore cannot corroborate the faithfulness of the formalization — an agreement between the statement and this read-back is self-agreement. Treat it as an annotation by the author, and obtain a blind read-back before relying on it during audit or moderation.
This bundle introduces five notions over finite types.
Distributions. For a finite type , a function is called a PMF when for every and . Nothing forces to be nonempty; for an empty the sum is , so no function on an empty type is a PMF.
Cipher. For finite types , , with decidable equality on , a Cipher is a record consisting of: a function ; a proof that for each key the map is injective; a function ; a proof that for all ; and a proof that . No condition relates different keys, and need not be surjective. Note that an empty cannot occur, since the empty sum is ; and may be empty, and if is empty every injectivity condition holds vacuously.
Derived quantities. For a cipher , messages , cryptograms and an arbitrary function (no hypothesis on is built into these definitions):
The last is a quotient of real numbers, so when it equals by the convention that division by zero yields zero.
Perfect secrecy. A cipher satisfies when: for every function that is a PMF, for every cryptogram with , and for every message , one has . The quantification is over all PMFs , not one fixed distribution; cryptograms of probability zero are excluded; if is empty there is no PMF at all and the condition holds vacuously for every cipher on an empty message set.
Entropy. For a finite type and , , using the natural logarithm and the convention for ; in particular terms with contribute , and the definition is applied to arbitrary real-valued functions, not only to PMFs.
Cyclic cipher. For a natural number with , is the cipher whose messages, keys and cryptograms are all , whose enciphering map is in , and whose key probability is the constant for every key.