Cryptology City: DLOG, CDH, DDH and OMDL games over a cyclic group
DefinitionCryptologyCity_DLogGamesGames for the discrete-logarithm family over one fixed cyclic group, as on Cryptology City.
IsCyclicGroupOfOrder g p: has order and generates . For , isg ^ x.val, withx.val.dlogOf g p Z: an exponent with if one exists, and otherwise. It is unique when generates .- DLOG game. , , win iff . is the winning probability.
- CDH game. , , win iff . is the winning probability.
- DDH game. , , with and , win iff . Here is
false, and in . - Query trees. A deterministic oracle algorithm either stops with an output or queries and continues as a function of the answer.
runreturns the output and the number of queries made.queryAllasks a list of queries in order. - OMDL game with queries. . The adversary receives and outputs a random query tree, which runs against the oracle
dlogOf g p. It wins iff it made at most queries and its output is . omdlOfDLog ℓ A: query , run , and output the answers followed by 's output.
All samples are uniform and independent, and adversaries are arbitrary PMF-valued functions.
import Mathlib.Probability.Distributions.Uniform
import Mathlib.GroupTheory.OrderOfElement
import Mathlib.Data.ZMod.Basic
/-!
# Cryptology City: discrete-logarithm and Diffie–Hellman games
Concrete-security games over one fixed finite cyclic group, following the
assumption pages of Cryptology City:
<https://cryptology.city/Assumptions/discrete-logarithm>,
<https://cryptology.city/Assumptions/computational-diffie-hellman>,
<https://cryptology.city/Assumptions/decisional-diffie-hellman>.
* The group is a `Group G` with a distinguished `g : G` and an order `p : ℕ`;
`IsCyclicGroupOfOrder g p` says `g` generates `G` and has order `p`.
* Exponents are elements of `ZMod p`; the group element $g^x$ is
`g ^ x.val`, where `x.val ∈ {0, …, p - 1}` is the canonical representative.
* An adversary is an arbitrary randomized function (a `PMF`-valued function)
of its inputs. The group description `(G, g, p)` is fixed, so it is not
passed explicitly. No efficiency bound is imposed.
* A game is a `PMF Bool`; it outputs `true` when the adversary wins.
* For DDH the challenge bit `b : Bool` encodes the wiki's `b ∈ {0, 1}` with
`false ↔ 0` (real tuple) and `true ↔ 1` (random tuple).
-/
open scoped ENNReal
namespace CryptologyCity
section Group
variable {G : Type*} [Group G]
/-- `g` generates `G` and has order `p`: the description `(G, g, p)` of a
cyclic group of order `p` with generator `g`. -/
structure IsCyclicGroupOfOrder (g : G) (p : ℕ) : Prop where
/-- Every element of `G` is an integer power of `g`. -/
generates : ∀ x : G, x ∈ Subgroup.zpowers g
/-- `g` has order `p`. -/
orderOf_eq : orderOf g = p
/-- The discrete logarithm of `Z` to the base `g`, as an exponent in `ZMod p`:
some `z` with `g ^ z.val = Z` if one exists, and `0` otherwise. When
`IsCyclicGroupOfOrder g p` holds with `p ≠ 0`, such a `z` exists and is unique. -/
noncomputable def dlogOf (g : G) (p : ℕ) (Z : G) : ZMod p := by
classical
exact if h : ∃ z : ZMod p, g ^ z.val = Z then h.choose else 0
end Group
/-! ## DLOG -/
section DLog
variable {G : Type*} [Group G]
/-- A DLOG adversary receives `X = g^x` and outputs a guess for `x`. -/
abbrev DLogAdversary (G : Type*) (p : ℕ) := G → PMF (ZMod p)
/-- $\mathrm{Game}^{\mathrm{dl}}$: sample `x ← ZMod p` uniformly, run the
adversary on `X = g^x`, and output whether its answer equals `x`. -/
noncomputable def dlogGame (g : G) (p : ℕ) [NeZero p] (A : DLogAdversary G p) :
PMF Bool :=
(PMF.uniformOfFintype (ZMod p)).bind fun x =>
(A (g ^ x.val)).bind fun x' =>
PMF.pure (decide (x' = x))
/-- $\mathrm{Adv}^{\mathrm{dl}}_{\mathcal A} := \Pr[\mathrm{Game}^{\mathrm{dl}}_{\mathcal A} = 1]$. -/
noncomputable def dlogAdv (g : G) (p : ℕ) [NeZero p] (A : DLogAdversary G p) : ℝ≥0∞ :=
dlogGame g p A true
end DLog
/-! ## CDH -/
section CDH
variable {G : Type*} [Group G] [DecidableEq G]
/-- A CDH adversary receives `X = g^x` and `Y = g^y` and outputs a guess for
`g^{xy}`. -/
abbrev CDHAdversary (G : Type*) := G → G → PMF G
/-- $\mathrm{Game}^{\mathrm{cdh}}$: sample `x, y ← ZMod p` uniformly and
independently, run the adversary on `(g^x, g^y)`, and output whether its answer
equals `g^{xy}`, where `xy` is the product in `ZMod p`. -/
noncomputable def cdhGame (g : G) (p : ℕ) [NeZero p] (A : CDHAdversary G) : PMF Bool :=
(PMF.uniformOfFintype (ZMod p)).bind fun x =>
(PMF.uniformOfFintype (ZMod p)).bind fun y =>
(A (g ^ x.val) (g ^ y.val)).bind fun Z =>
PMF.pure (decide (Z = g ^ (x * y).val))
/-- $\mathrm{Adv}^{\mathrm{cdh}}_{\mathcal A} := \Pr[\mathrm{Game}^{\mathrm{cdh}}_{\mathcal A} = 1]$. -/
noncomputable def cdhAdv (g : G) (p : ℕ) [NeZero p] (A : CDHAdversary G) : ℝ≥0∞ :=
cdhGame g p A true
end CDH
/-! ## DDH -/
section DDH
variable {G : Type*} [Group G]
/-- A DDH adversary receives `(X, Y, Z)` and outputs a guess for the challenge
bit (`false` for "real", `true` for "random"). -/
abbrev DDHAdversary (G : Type*) := G → G → G → PMF Bool
/-- $\mathrm{Game}^{\mathrm{ddh}}$: sample `x, y, z ← ZMod p` and a bit
`b ← Bool` uniformly and independently, set `Z_0 = g^{xy}` (real) and
`Z_1 = g^z` (random), run the adversary on `(g^x, g^y, Z_b)`, and output whether
its guess equals `b`. -/
noncomputable def ddhGame (g : G) (p : ℕ) [NeZero p] (A : DDHAdversary G) : PMF Bool :=
(PMF.uniformOfFintype (ZMod p)).bind fun x =>
(PMF.uniformOfFintype (ZMod p)).bind fun y =>
(PMF.uniformOfFintype (ZMod p)).bind fun z =>
(PMF.uniformOfFintype Bool).bind fun b =>
(A (g ^ x.val) (g ^ y.val) (if b then g ^ z.val else g ^ (x * y).val)).bind
fun b' => PMF.pure (decide (b' = b))
/-- $\mathrm{Adv}^{\mathrm{ddh}}_{\mathcal A} :=
\left|2\Pr[\mathrm{Game}^{\mathrm{ddh}}_{\mathcal A} = 1] - 1\right|$. -/
noncomputable def ddhAdv (g : G) (p : ℕ) [NeZero p] (A : DDHAdversary G) : ℝ :=
|2 * (ddhGame g p A true).toReal - 1|
end DDH
/-! ## OMDL -/
/-- A deterministic oracle algorithm with queries in `Q`, answers in `R` and
output in `α`: either it stops with an output, or it makes a query and continues
as a function of the answer. -/
inductive QueryTree (Q R α : Type*) where
/-- Stop and output `a`. -/
| done (a : α) : QueryTree Q R α
/-- Query the oracle on `q` and continue with `k` applied to the answer. -/
| query (q : Q) (k : R → QueryTree Q R α) : QueryTree Q R α
namespace QueryTree
variable {Q R α : Type*}
/-- Run a query tree against the oracle `O`, returning its output and the
number of oracle queries it made. -/
def run (O : Q → R) : QueryTree Q R α → α × ℕ
| done a => (a, 0)
| query q k => ((k (O q)).run O).map id (· + 1)
/-- Query `qs 0, qs 1, …, qs (n - 1)` in order, then output `k` applied to the
vector of answers. -/
def queryAll : (n : ℕ) → (Fin n → Q) → ((Fin n → R) → α) → QueryTree Q R α
| 0, _, k => done (k Fin.elim0)
| n + 1, qs, k =>
query (qs 0) fun r => queryAll n (fun i => qs i.succ) fun rs => k (Fin.cons r rs)
end QueryTree
section OMDL
variable {G : Type*} [Group G]
/-- An OMDL adversary for `ℓ + 1` challenges receives `(X_0, …, X_ℓ)` and
outputs a randomized oracle algorithm that queries a discrete-logarithm oracle
on group elements and outputs guesses for all `ℓ + 1` discrete logarithms.
A randomized oracle algorithm is a distribution over deterministic query
trees, its coins being sampled up front. -/
abbrev OMDLAdversary (G : Type*) (p ℓ : ℕ) :=
(Fin (ℓ + 1) → G) → PMF (QueryTree G (ZMod p) (Fin (ℓ + 1) → ZMod p))
/-- $\mathrm{Game}^{\mathrm{om\text{-}dl}}_{\ell}$: sample `x_0, …, x_ℓ ← ZMod p`
uniformly and independently, run the adversary on `(g^{x_0}, …, g^{x_ℓ})` with
the oracle `Z ↦ dlogOf g p Z`, and output whether it made at most `ℓ` queries and
recovered every `x_i`. -/
noncomputable def omdlGame (g : G) (p : ℕ) [NeZero p] (ℓ : ℕ) (A : OMDLAdversary G p ℓ) :
PMF Bool :=
(PMF.uniformOfFintype (Fin (ℓ + 1) → ZMod p)).bind fun xs =>
(A fun i => g ^ (xs i).val).bind fun T =>
PMF.pure (decide ((T.run (dlogOf g p)).2 ≤ ℓ ∧ (T.run (dlogOf g p)).1 = xs))
/-- $\mathrm{Adv}^{\mathrm{om\text{-}dl}}_{\mathcal A,\ell} :=
\Pr[\mathrm{Game}^{\mathrm{om\text{-}dl}}_{\mathcal A,\ell} = 1]$. -/
noncomputable def omdlAdv (g : G) (p : ℕ) [NeZero p] (ℓ : ℕ) (A : OMDLAdversary G p ℓ) :
ℝ≥0∞ :=
omdlGame g p ℓ A true
/-- The reduction of `omdl-to-prime-order-dlog`: given a DLOG adversary `A`,
query the DL oracle on `X_0, …, X_{ℓ-1}`, run `A` on `X_ℓ`, and output the
oracle's answers followed by `A`'s answer. -/
noncomputable def omdlOfDLog {p : ℕ} (ℓ : ℕ) (A : DLogAdversary G p) : OMDLAdversary G p ℓ :=
fun Xs => (A (Xs (Fin.last ℓ))).map fun x' =>
QueryTree.queryAll ℓ (fun i => Xs i.castSucc) fun zs => Fin.lastCases x' zs
end OMDL
end CryptologyCity
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
This file defines, over an arbitrary group (any type with a group structure; no finiteness, commutativity or decidable equality is assumed unless stated), a distinguished element and a natural number , the following objects. Throughout, "exponents" are elements of , and for an exponent the group element written is the natural-number power , where is the canonical representative of . Every game below requires (so that is finite and uniform sampling makes sense); "uniform" means the uniform distribution on a finite set, and "independent" means sequential sampling. Advantages are valued in the extended non-negative reals except the DDH advantage, which is a real number.
Cyclic-group predicate. holds iff (i) every is an integer power , , and (ii) the order of equals . (In Mathlib, the order of an element of infinite order is , so with this predicate describes an infinite cyclic group; with it forces .) Nothing requires to be prime.
Discrete-log function. is some (non-constructively chosen) with if such a exists, and otherwise. The docstring asserts that under with such a exists and is unique; this claim is not proved in the file. Without the cyclic hypothesis the returned value is an arbitrary preimage, or the junk value when has no preimage.
Adversaries. All adversaries are arbitrary functions from their inputs to probability distributions (no efficiency, size or computability restriction of any kind; they do not receive , or a group description as input, but since they are arbitrary mathematical functions they may depend on them implicitly):
- a DLOG adversary is a map ;
- a CDH adversary is a map ;
- a DDH adversary is a map .
Because no restriction is placed on adversaries, for each game there exist adversaries (e.g. ones that apply directly) achieving the maximal success probability; the definitions alone express no hardness.
DLOG game. Sample uniformly, sample , output iff (as elements of ). Advantage:
CDH game (requires decidable equality on ). Sample uniformly and independently, sample , output iff , where is the product in (i.e. reduced mod before exponentiation). , in .
DDH game. Sample and a bit , all uniformly and independently; set the third input to if and to if ; sample ; output iff . The advantage is the real number
Query trees. A query tree with query set , answer set and output set is inductively either a leaf carrying an output , or a node carrying a query and a continuation that assigns a subtree to every answer . Being inductive, every path from the root is finite (though the depth need not be uniformly bounded when is infinite). Running a tree against a deterministic oracle follows the path determined by the oracle answers and returns the pair (leaf output, number of nodes traversed, i.e. number of oracle queries made). is the tree that queries in that order and then outputs on the answer vector; for it is the leaf .
OMDL adversary and game. For , an OMDL adversary is a map from to a probability distribution over query trees with queries in , answers in , and outputs in (all randomness is drawn before the first query; the tree itself is deterministic). The OMDL game: sample uniformly from (equivalently, independently uniform coordinates), sample a tree , run against the oracle (defined on all of , so the challenges themselves may be queried), and output iff the number of queries made is and the output vector equals coordinate-wise. . The oracle is the chosen-preimage function above; the file does not itself assume in this definition.
Reduction . Given and a DLOG adversary , the OMDL adversary on input samples and returns the deterministic tree with . Thus always makes exactly queries (none when ), and 's coins are sampled before any query.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.