Cryptology City: Schnorr signatures in the random-oracle model
DefinitionCryptologyCity_SchnorrROMcryptographycryptology-citydiscrete-logarithmrandom-oracle-model
Schnorr signatures in the random-oracle model over a fixed cyclic group of order , with a lazily sampled random oracle for a nonempty . Conventions follow CryptologyCity_DLogGames and CryptologyCity_DLogIdentification.
SigProg G M pis a deterministic forger program:hash R m kasks and continues withk,sign m kasks for a signature on and continues withk, anddone m R soutputs the forgery .P.Within qH qSholds iff every branch makes at mostqHhash andqSsigning queries.roQuery C hC T yanswers a query at from table : the recorded answer if is in , and otherwise a fresh , which is recorded.runRealexecutes a program with secret key , the lazily sampled oracle and the honest signing oracle (signing : , , , answer ), and records the signed messages.- A
SchnorrForgermaps the public key to a distribution over programs. EUF-CMA gameschnorrEUFGame: , run the forger on from the empty table, query on the forgery with the same table, and win iffschnorrAccepts g (g^x) R e sand was never signed.schnorrEUFAdvis the probability of winning. runSimandschnorrSimForgerun a program deterministically from signing coins and a list of oracle answers: fresh hash queries take the next list answer with its index; a signing query answers and programs , raisingbadif that point is already recorded; lists answer when exhausted.schnorrSimForgereturnssome (J, (R, m), e, s)iff the forgery verifies with , was not signed,badwas not raised, and is the list answer with index .schnorrSignCoin C hCis uniform on .- Forking reduction
schnorrFork g p C hC qH qS A. On : sample a program from , coins and answers , and runschnorrSimForge. If it returns , sample fresh answers , rerun on the answers for and for , and if this returns with , output . Output in every other case.
Definition code
import Definitions.Def_CryptologyCity_DLogIdentification
/-!
# Cryptology City: Schnorr signatures in the random-oracle model
The Schnorr signature scheme over one fixed finite cyclic group `G = ⟨g⟩` of order `p`, with a
random oracle `H : G × M → C` for a nonempty challenge set `C ⊆ ℤ_p`, following
<https://cryptology.city/Primitives/digital-signature#schnorr-signatures>,
<https://cryptology.city/Glossary/random-oracle-model> and
<https://cryptology.city/Reductions/dlog-and-rom-to-schnorr-signatures-sch91>.
Conventions are those of `Definitions.Def_CryptologyCity_DLogGames`.
* The random oracle is **lazily sampled** from a table `T` that starts empty and is shared by the
forger and the signing oracle: a first query to a point receives a fresh uniform element of
`C`, a repeated query the recorded answer (the lazy-sampling formulation on the wiki's
random-oracle page).
* A forger is a randomized strategy: on the public key it samples a deterministic interactive
program (`SigProg`) that asks hash and signing queries and finally outputs a forgery. A
randomized interactive forger is exactly a distribution over such programs.
* Query budgets are a property of the program (`SigProg.Within`), checked on every branch.
-/
open scoped ENNReal
universe u v
namespace CryptologyCity
/-- A deterministic forger program against Schnorr signatures in the ROM. `hash R m k` asks
`H(R, m)` and continues with `k` on the answer; `sign m k` asks for a signature on `m` and
continues with `k` on the signature `(R, s)`; `done m R s` outputs the forgery `(m, (R, s))`. -/
inductive SigProg (G : Type u) (M : Type v) (p : ℕ) : Type (max u v)
| done (m : M) (R : G) (s : ZMod p) : SigProg G M p
| hash (R : G) (m : M) (k : ZMod p → SigProg G M p) : SigProg G M p
| sign (m : M) (k : G × ZMod p → SigProg G M p) : SigProg G M p
namespace SigProg
variable {G : Type u} {M : Type v} {p : ℕ}
/-- `P.Within qH qS`: on every branch, `P` makes at most `qH` hash queries and at most `qS`
signing queries. -/
def Within : SigProg G M p → ℕ → ℕ → Prop
| done _ _ _, _, _ => True
| hash _ _ k, qH, qS => 0 < qH ∧ ∀ e, (k e).Within (qH - 1) qS
| sign _ k, qH, qS => 0 < qS ∧ ∀ σ, (k σ).Within qH (qS - 1)
end SigProg
variable {G : Type u} [Group G] {M : Type v}
/-- A random-oracle table: recorded points `(R, m)` with their answers. -/
abbrev ROTable (G : Type u) (M : Type v) (p : ℕ) := List ((G × M) × ZMod p)
/-- One lazily sampled random-oracle query at `y`: the recorded answer if `y` is in the table,
and otherwise a fresh `e ← C`, which is recorded. -/
noncomputable def roQuery [DecidableEq G] [DecidableEq M] {p : ℕ} (C : Finset (ZMod p))
(hC : C.Nonempty) (T : ROTable G M p) (y : G × M) : PMF (ZMod p × ROTable G M p) :=
match T.lookup y with
| some e => PMF.pure (e, T)
| none => (PMF.uniformOfFinset C hC).map fun e => (e, (y, e) :: T)
/-- Execution of a forger program with secret key `x` against the lazily sampled random oracle
and the honest signing oracle, from table `T` and list `Q` of signed messages. A signing query
on `m` samples `r ← ℤ_p`, sets `R = g^r`, queries `e = H(R, m)` and answers `(R, r + e x)`.
Returns the forgery `(m, R, s)`, the final table and the list of signed messages. -/
noncomputable def SigProg.runReal [DecidableEq G] [DecidableEq M] (g : G) (p : ℕ) [NeZero p]
(C : Finset (ZMod p)) (hC : C.Nonempty) (x : ZMod p) :
SigProg G M p → ROTable G M p → List M → PMF ((M × G × ZMod p) × ROTable G M p × List M)
| .done m R s, T, Q => PMF.pure ((m, R, s), T, Q)
| .hash R m k, T, Q => (roQuery C hC T (R, m)).bind fun eT => (k eT.1).runReal g p C hC x eT.2 Q
| .sign m k, T, Q => (PMF.uniformOfFintype (ZMod p)).bind fun r =>
(roQuery C hC T (g ^ r.val, m)).bind fun eT =>
(k (g ^ r.val, r + eT.1 * x)).runReal g p C hC x eT.2 (m :: Q)
/-- A forger: on the public key, a distribution over forger programs. -/
abbrev SchnorrForger (G : Type u) (M : Type v) (p : ℕ) := G → PMF (SigProg G M p)
/-- $\mathrm{Game}^{\mathrm{euf\text{-}cma}}$ for Schnorr signatures in the ROM: `x ← ℤ_p`,
`h = g^x`; run the forger with the lazily sampled oracle and the signing oracle from an empty
table; on the forgery `(m, (R, s))`, query `e = H(R, m)` (with the same table) and win iff
`g^s = R · h^e` and `m` was never submitted to the signing oracle. -/
noncomputable def schnorrEUFGame [DecidableEq G] [DecidableEq M] (g : G) (p : ℕ) [NeZero p]
(C : Finset (ZMod p)) (hC : C.Nonempty) (A : SchnorrForger G M p) : PMF Bool :=
(PMF.uniformOfFintype (ZMod p)).bind fun x =>
(A (g ^ x.val)).bind fun P =>
(P.runReal g p C hC x [] []).bind fun out =>
(roQuery C hC out.2.1 (out.1.2.1, out.1.1)).map fun eT =>
schnorrAccepts g (g ^ x.val) out.1.2.1 eT.1 out.1.2.2 && !(out.2.2.contains out.1.1)
/-- $\mathrm{Adv}^{\mathrm{euf\text{-}cma}}_{\mathcal A} :=
\Pr[\mathrm{Game}^{\mathrm{euf\text{-}cma}}_{\mathcal A} = 1]$. -/
noncomputable def schnorrEUFAdv [DecidableEq G] [DecidableEq M] (g : G) (p : ℕ) [NeZero p]
(C : Finset (ZMod p)) (hC : C.Nonempty) (A : SchnorrForger G M p) : ℝ≥0∞ :=
schnorrEUFGame g p C hC A true
/-! ## The forking reduction -/
/-- The reduction's random-oracle table: each recorded point carries its answer and, if the answer
came from the list of oracle answers, its index in that list (`none` for programmed points). -/
abbrev SimTable (G : Type u) (M : Type v) (p : ℕ) := List ((G × M) × ZMod p × Option ℕ)
/-- Result of a simulated run: the forgery `(m, R, s)`, the answer `e = H(R, m)` with its list
index (`none` if programmed), the signed messages, and the flag `bad`. -/
abbrev SimResult (G : Type u) (M : Type v) (p : ℕ) :=
(M × G × ZMod p) × (ZMod p × Option ℕ) × List M × Bool
/-- The simulated signing commitment for coin `(e, s)` and public key `X`: `R = g^s · (X^e)⁻¹`. -/
def simCommit (g X : G) {p : ℕ} (es : ZMod p × ZMod p) : G := g ^ es.2.val * (X ^ es.1.val)⁻¹
/-- Simulated execution of a forger program on public key `X`, deterministic in its coins, from
table `T` with `k` list answers consumed so far.
* A fresh hash query is answered by the next entry of the list `hs`, recorded with index `k`.
* A signing query on `m` takes the next coin `(e, s)` of `σs`, sets `R = g^s · (X^e)⁻¹`,
answers `(R, s)` and programs `H(R, m) := e`; if `(R, m)` is already in the table it raises
`bad` instead (and leaves the table unchanged).
* On the forgery `(m, (R, s))` it evaluates `H(R, m)` in the same way.
Exhausted lists answer `0`. -/
def SigProg.runSim [DecidableEq G] [DecidableEq M] (g : G) {p : ℕ} (X : G) :
SigProg G M p → SimTable G M p → ℕ → List (ZMod p × ZMod p) → List (ZMod p) → List M →
Bool → SimResult G M p
| .done m R s, T, k, _, hs, Q, b =>
match T.lookup (R, m) with
| some eJ => ((m, R, s), eJ, Q, b)
| none => ((m, R, s), (hs.headD 0, some k), Q, b)
| .hash R m c, T, k, σs, hs, Q, b =>
match T.lookup (R, m) with
| some eJ => (c eJ.1).runSim g X T k σs hs Q b
| none =>
(c (hs.headD 0)).runSim g X (((R, m), hs.headD 0, some k) :: T) (k + 1) σs hs.tail Q b
| .sign m c, T, k, σs, hs, Q, b =>
match T.lookup (simCommit g X (σs.headD (0, 0)), m) with
| some _ => (c (simCommit g X (σs.headD (0, 0)), (σs.headD (0, 0)).2)).runSim
g X T k σs.tail hs (m :: Q) true
| none => (c (simCommit g X (σs.headD (0, 0)), (σs.headD (0, 0)).2)).runSim
g X (((simCommit g X (σs.headD (0, 0)), m), (σs.headD (0, 0)).1, none) :: T) k σs.tail hs
(m :: Q) b
/-- The outcome of a simulated run from the empty table: `some (J, (R, m), e, s)` if the forgery
`(m, (R, s))` verifies with `e = H(R, m)`, `m` was not signed, `bad` was not raised, and `e` is
the list answer with index `J`; otherwise `none`. -/
def schnorrSimForge [DecidableEq G] [DecidableEq M] (g : G) {p : ℕ} (X : G) (P : SigProg G M p)
(σs : List (ZMod p × ZMod p)) (hs : List (ZMod p)) :
Option (ℕ × (G × M) × ZMod p × ZMod p) :=
match P.runSim g X [] 0 σs hs [] false with
| ((m, R, s), (e, some J), Q, false) =>
if schnorrAccepts g X R e s = true ∧ m ∉ Q then some (J, (R, m), e, s) else none
| _ => none
/-- The uniform law on `C × ℤ_p`, for the simulated signing coins `(e, s)`. -/
noncomputable def schnorrSignCoin {p : ℕ} [NeZero p] (C : Finset (ZMod p)) (hC : C.Nonempty) :
PMF (ZMod p × ZMod p) :=
(PMF.uniformOfFinset C hC).bind fun e => (PMF.uniformOfFintype (ZMod p)).map fun s => (e, s)
/-- The forking reduction from a Schnorr forger with budgets `qH`, `qS` to DLOG. On `X`: sample
a forger program `P ← 𝒜(X)`, `qS` signing coins, and `q = qH + 1` oracle answers
`h ← C^q`, and run the simulation. If it returns `(J, y, e, s)`, sample fresh `h' ← C^q`, rerun
with the same program and coins on `h''`, where `h''_i = h_i` for `i < J` and `h''_i = h'_i`
otherwise, and if this returns `(J, y, e', s')` with the same `J` and `y` and `e ≠ e'`, output
`(s - s')(e - e')⁻¹`. In every other case output `0`. -/
noncomputable def schnorrFork [DecidableEq G] [DecidableEq M] (g : G) (p : ℕ) [NeZero p]
(C : Finset (ZMod p)) (hC : C.Nonempty) (qH qS : ℕ) (A : SchnorrForger G M p) :
DLogAdversary G p := fun X =>
(A X).bind fun P =>
(iidPMF (schnorrSignCoin C hC) qS).bind fun σ =>
(iidPMF (PMF.uniformOfFinset C hC) (qH + 1)).bind fun h =>
match schnorrSimForge g X P (List.ofFn σ) (List.ofFn h) with
| none => PMF.pure 0
| some (J, y, e, s) =>
(iidPMF (PMF.uniformOfFinset C hC) (qH + 1)).map fun h' =>
match schnorrSimForge g X P (List.ofFn σ)
(List.ofFn fun i => if i.val < J then h i else h' i) with
| some (J', y', e', s') =>
if J' = J ∧ y' = y ∧ e ≠ e' then (s - s') * (e - e')⁻¹ else 0
| none => 0
end CryptologyCity
Source
Cryptology City, https://cryptology.city/Primitives/digital-signature#schnorr-signatures, https://cryptology.city/Glossary/random-oracle-model (lazy sampling), https://cryptology.city/Reductions/dlog-and-rom-to-schnorr-signatures-sch91 (Sketch: signing simulation and forking), https://cryptology.city/Folklore/forking-lemma.
Human review
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.