Cryptology City: Schnorr and Okamoto identification protocols
DefinitionCryptologyCity_DLogIdentificationThe Schnorr and Okamoto identification protocols over a fixed cyclic group of order . Conventions follow CryptologyCity_DLogGames. is a nonempty challenge set, and is uniform on .
schnorrAccepts g h a e zholds iff .schnorrHonestTranscript g p C hC xis the law of for and .schnorrSimTranscript g p C hC his the law of for and .iidPMF μ nis the law of independent samples of .- A
SchnorrPassiveImpersonatorconsists of a number of observed transcripts, a commit phase (public key, transcripts) (commitment , state), and a response phase (state, challenge) response . - Passive game
schnorrPassiveGame. , honest transcripts for , commit on , , respond. Win iffschnorrAccepts g (g^x) a e z.schnorrPassiveAdvis the probability of winning. - Rewinding reduction
schnorrRewind. On : simulated transcripts for , commit, independently, two responses from the same state. Output if both are accepted for and , and otherwise. okamotoAccepts g g₂ X A e z₁ z₂holds iff .
import Definitions.Def_CryptologyCity_DLogGames
import Mathlib.Algebra.Field.ZMod
/-!
# Cryptology City: the Schnorr and Okamoto identification protocols
Three-move protocols over one fixed finite cyclic group `G = ⟨g⟩` of order `p`, following
<https://cryptology.city/Primitives/identification-scheme> and
<https://cryptology.city/Reductions/dlog-to-schnorr-signatures-sch91>,
<https://cryptology.city/Reductions/prime-order-dlog-to-okamoto-identification-protocol-oka92>.
Conventions are those of `Definitions.Def_CryptologyCity_DLogGames`: exponents are in `ZMod p`,
`g^x` is `g ^ x.val`, adversaries are arbitrary `PMF`-valued functions, and games output `true`
when the adversary wins. The challenge space is a nonempty finite set `C ⊆ ℤ_p`, and the
verifier's challenge is uniform on `C`.
-/
open scoped ENNReal
namespace CryptologyCity
variable {G : Type*} [Group G]
/-! ## Schnorr -/
/-- A Schnorr transcript `(a, e, z)`: commitment, challenge, response. -/
abbrev SchnorrTranscript (G : Type*) (p : ℕ) := G × ZMod p × ZMod p
/-- The Schnorr verifier for public key `h` accepts `(a, e, z)` iff `g^z = a · h^e`. -/
def schnorrAccepts [DecidableEq G] (g h : G) {p : ℕ} (a : G) (e z : ZMod p) : Bool :=
decide (g ^ z.val = a * h ^ e.val)
/-- The law of an honest Schnorr transcript for secret key `x`: `r ← ℤ_p`, `a = g^r`,
`e ← C`, `z = r + e x`. -/
noncomputable def schnorrHonestTranscript (g : G) (p : ℕ) [NeZero p] (C : Finset (ZMod p))
(hC : C.Nonempty) (x : ZMod p) : PMF (SchnorrTranscript G p) :=
(PMF.uniformOfFintype (ZMod p)).bind fun r =>
(PMF.uniformOfFinset C hC).bind fun e =>
PMF.pure (g ^ r.val, e, r + e * x)
/-- The honest-verifier simulator for public key `h`: `z ← ℤ_p`, `e ← C`,
`a = g^z · (h^e)⁻¹`. -/
noncomputable def schnorrSimTranscript (g : G) (p : ℕ) [NeZero p] (C : Finset (ZMod p))
(hC : C.Nonempty) (h : G) : PMF (SchnorrTranscript G p) :=
(PMF.uniformOfFintype (ZMod p)).bind fun z =>
(PMF.uniformOfFinset C hC).bind fun e =>
PMF.pure (g ^ z.val * (h ^ e.val)⁻¹, e, z)
/-- `n` independent samples of `μ`. -/
noncomputable def iidPMF {α : Type*} (μ : PMF α) : (n : ℕ) → PMF (Fin n → α)
| 0 => PMF.pure Fin.elim0
| n + 1 => μ.bind fun a => (iidPMF μ n).bind fun v => PMF.pure (Fin.cons a v)
/-- A passive impersonator against the Schnorr protocol. It sees `n` honest transcripts
(`n` is part of the adversary), and then plays the prover: on the public key and the
transcripts it outputs a commitment `a` with a state of type `St`, and on the challenge `e`
it outputs a response `z`. -/
structure SchnorrPassiveImpersonator (G : Type*) (p : ℕ) (St : Type*) where
/-- The number of honest transcripts it observes. -/
n : ℕ
/-- Commit phase: public key and transcripts to a commitment and a state. -/
commit : G → (Fin n → SchnorrTranscript G p) → PMF (G × St)
/-- Response phase: state and challenge to a response. -/
respond : St → ZMod p → PMF (ZMod p)
/-- $\mathrm{Game}^{\mathrm{imp\text{-}pa}}$ for Schnorr: `x ← ℤ_p`, `h = g^x`; the impersonator
sees `n` independent honest transcripts for `x`, commits to `a`, receives `e ← C`, answers `z`,
and wins iff the verifier accepts `(a, e, z)` for `h`. -/
noncomputable def schnorrPassiveGame [DecidableEq G] (g : G) (p : ℕ) [NeZero p]
(C : Finset (ZMod p)) (hC : C.Nonempty) {St : Type*} (A : SchnorrPassiveImpersonator G p St) :
PMF Bool :=
(PMF.uniformOfFintype (ZMod p)).bind fun x =>
(iidPMF (schnorrHonestTranscript g p C hC x) A.n).bind fun ts =>
(A.commit (g ^ x.val) ts).bind fun aSt =>
(PMF.uniformOfFinset C hC).bind fun e =>
(A.respond aSt.2 e).bind fun z =>
PMF.pure (schnorrAccepts g (g ^ x.val) aSt.1 e z)
/-- $\mathrm{Adv}^{\mathrm{imp\text{-}pa}}_{\mathcal A} :=
\Pr[\mathrm{Game}^{\mathrm{imp\text{-}pa}}_{\mathcal A} = 1]$. -/
noncomputable def schnorrPassiveAdv [DecidableEq G] (g : G) (p : ℕ) [NeZero p]
(C : Finset (ZMod p)) (hC : C.Nonempty) {St : Type*} (A : SchnorrPassiveImpersonator G p St) :
ℝ≥0∞ :=
schnorrPassiveGame g p C hC A true
/-- The rewinding reduction from a passive Schnorr impersonator to DLOG. On `X`: simulate `n`
transcripts for public key `X`, run the commit phase to get `(a, st)`, draw two independent
challenges `e, e' ← C`, run the response phase on each from the same `st` to get `z, z'`, and
output `(z - z')(e - e')⁻¹` if both transcripts are accepted and `e ≠ e'`, and `0` otherwise. -/
noncomputable def schnorrRewind [DecidableEq G] (g : G) (p : ℕ) [NeZero p]
(C : Finset (ZMod p)) (hC : C.Nonempty) {St : Type*} (A : SchnorrPassiveImpersonator G p St) :
DLogAdversary G p := fun X =>
(iidPMF (schnorrSimTranscript g p C hC X) A.n).bind fun ts =>
(A.commit X ts).bind fun aSt =>
(PMF.uniformOfFinset C hC).bind fun e =>
(PMF.uniformOfFinset C hC).bind fun e' =>
(A.respond aSt.2 e).bind fun z =>
(A.respond aSt.2 e').bind fun z' =>
PMF.pure (if schnorrAccepts g X aSt.1 e z ∧ schnorrAccepts g X aSt.1 e' z' ∧ e ≠ e'
then (z - z') * (e - e')⁻¹ else 0)
/-! ## Okamoto -/
/-- The Okamoto verifier for generators `g₁ = g`, `g₂` and public key `X` accepts
`(A, e, z₁, z₂)` iff `g₁^{z₁} g₂^{z₂} = A · X^e`. -/
def okamotoAccepts [DecidableEq G] (g g₂ X : G) {p : ℕ} (A : G) (e z₁ z₂ : ZMod p) : Bool :=
decide (g ^ z₁.val * g₂ ^ z₂.val = A * X ^ e.val)
end CryptologyCity
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
This file defines no theorems. It defines objects over an arbitrary group (written multiplicatively, not assumed finite, abelian or cyclic at this stage), a distinguished element and a natural number . Exponents live in (the integers modulo ), and for the power means , where is the canonical representative. Wherever a uniform distribution on is used, is assumed. Throughout, is a finite nonempty set (the challenge set) and "" means is uniform on . All "adversaries" are arbitrary probability distributions (no efficiency or computability bound), and games are distributions on .
Notions imported from the companion DLOG file and used here:
- Cyclic group hypothesis : every element of equals for some integer , and the order of equals . (Mathlib's order is for elements of infinite order, so would mean is infinite cyclic; with it forces .)
- A DLOG adversary is a map from to probability distributions on . The DLOG game samples uniformly, samples , and outputs iff . The DLOG advantage is the probability that this game outputs (an extended nonnegative real, in fact in ).
Definitions in this file:
-
A Schnorr transcript is a triple .
-
Schnorr verifier. For and , iff
(both exponents by canonical representatives).
-
Honest transcript distribution for (requires ): sample uniformly, then independently, and output , with computed in .
-
Simulated transcript distribution for : sample uniformly, then independently, and output .
-
i.i.d. product : the distribution of independent samples from , as a vector indexed by (for , the point mass on the empty vector).
-
A passive Schnorr impersonator with state type (an arbitrary type) consists of: a natural number (chosen by the adversary, possibly ); a commit map taking a public key and a vector of transcripts to a distribution on pairs ; and a respond map taking a state and a challenge to a distribution on responses .
-
Passive impersonation game (requires ): sample uniformly (so is possible); sample ; sample ; sample ; sample ; output . The passive advantage is the probability the game outputs .
-
Rewinding reduction , a DLOG adversary: on input , sample (simulated, not honest, transcripts for key ); sample ; sample and independently (they may coincide); sample and, independently, (fresh respond randomness each time, same state). Output
if , and all hold; otherwise output . (Inversion in uses the convention ; for prime and it is the true inverse.)
- Okamoto verifier. For and , iff
No Okamoto honest-transcript distribution, game, or reduction is defined in this file; is an arbitrary element of with no stated relation to .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.