Cryptology City: Pedersen commitment, CvHP91 hash and KR00 chameleon hash
DefinitionCryptologyCity_DLogCommitmentsSchemes built from over a fixed cyclic group of order . Conventions follow CryptologyCity_DLogGames.
uniformNonOne G: the uniform law on .uniformNonZero p: the uniform law on . Each falls back to a point mass when the set is empty, which cannot happen when .pedersen g h m r: the Pedersen commitment, which is also the CvHP91 hash and the KR00 chameleon hash .- Pedersen binding game. , then . Win iff , and .
- CvHP91 collision game. , then with . Win iff and .
kr00TCol x m r m'.- KR00 collision game. , then . Win iff and for .
Each advantage is the probability that its game outputs true.
import Definitions.Def_CryptologyCity_DLogGames
import Mathlib.Algebra.Field.ZMod
/-!
# Cryptology City: Pedersen commitments, the CvHP91 hash and the KR00 chameleon hash
Schemes over one fixed finite cyclic group `G = ⟨g⟩` of order `p`, following
<https://cryptology.city/Reductions/dlog-to-com-ped91>,
<https://cryptology.city/Reductions/dlog-to-crhf-cvhp91> and
<https://cryptology.city/Reductions/prime-order-dlog-to-chameleon-hash-function-kr00>.
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.
-/
open scoped ENNReal
namespace CryptologyCity
variable {G : Type*} [Group G]
/-! ## Key distributions -/
/-- The uniform distribution on `G ∖ {1}`. (If `G` is trivial, the point mass at `1`; this case
does not arise for a group of order `p ≥ 2`.) -/
noncomputable def uniformNonOne (G : Type*) [Group G] [Fintype G] [DecidableEq G] : PMF G :=
if h : (Finset.univ.erase (1 : G)).Nonempty then PMF.uniformOfFinset _ h else PMF.pure 1
/-- The uniform distribution on `ℤ_p^* = ZMod p ∖ {0}`. (If `p = 1`, the point mass at `0`;
this case does not arise for `p ≥ 2`.) -/
noncomputable def uniformNonZero (p : ℕ) [NeZero p] : PMF (ZMod p) :=
if h : (Finset.univ.erase (0 : ZMod p)).Nonempty then PMF.uniformOfFinset _ h else PMF.pure 0
/-! ## The Pedersen commitment and the CvHP91 hash -/
/-- The Pedersen commitment to `m ∈ ℤ_p` with randomness `r ∈ ℤ_p` under public parameters
`(g, h)`: `g^m h^r`. As a function of `(m, r)` this is also the CvHP91 hash
`H_{g,h}(x₁, x₂) = g^{x₁} h^{x₂}` and the KR00 chameleon hash `CH((g, h), m; r)`. -/
def pedersen (g h : G) {p : ℕ} (m r : ZMod p) : G := g ^ m.val * h ^ r.val
/-- A binding adversary for the Pedersen commitment receives `h` and outputs a commitment `c`
and two decommitments `(m₀, r₀)`, `(m₁, r₁)`. -/
abbrev PedersenBindingAdversary (G : Type*) (p : ℕ) :=
G → PMF (G × (ZMod p × ZMod p) × (ZMod p × ZMod p))
/-- $\mathrm{Game}^{\mathrm{bind}}$ for the Pedersen commitment: sample `h ← G ∖ {1}`, run the
adversary on `h`, and output whether both decommitments open `c` (`c = g^{m_i} h^{r_i}`) to
distinct messages `m₀ ≠ m₁`. -/
noncomputable def pedersenBindingGame [Fintype G] [DecidableEq G] (g : G) (p : ℕ)
(A : PedersenBindingAdversary G p) : PMF Bool :=
(uniformNonOne G).bind fun h =>
(A h).bind fun out =>
PMF.pure (decide (out.1 = pedersen g h out.2.1.1 out.2.1.2 ∧
out.1 = pedersen g h out.2.2.1 out.2.2.2 ∧ out.2.1.1 ≠ out.2.2.1))
/-- $\mathrm{Adv}^{\mathrm{bind}}_{\mathcal A} := \Pr[\mathrm{Game}^{\mathrm{bind}}_{\mathcal A} = 1]$. -/
noncomputable def pedersenBindingAdv [Fintype G] [DecidableEq G] (g : G) (p : ℕ)
(A : PedersenBindingAdversary G p) : ℝ≥0∞ :=
pedersenBindingGame g p A true
/-- A collision finder for the CvHP91 hash receives `h` and outputs two inputs in `ℤ_p²`. -/
abbrev CvHPAdversary (G : Type*) (p : ℕ) := G → PMF ((ZMod p × ZMod p) × (ZMod p × ZMod p))
/-- $\mathrm{Game}^{\mathrm{cr}}$ for `H_{g,h}(x₁, x₂) = g^{x₁} h^{x₂}`: sample `h` uniformly
from `G ∖ {1}` (the set of generators of `G` when `|G|` is prime), run the adversary on `h`, and output
whether it returned two distinct inputs with the same hash. -/
noncomputable def cvhpCollisionGame [Fintype G] [DecidableEq G] (g : G) (p : ℕ)
(A : CvHPAdversary G p) : PMF Bool :=
(uniformNonOne G).bind fun h =>
(A h).bind fun out =>
PMF.pure (decide (out.1 ≠ out.2 ∧
pedersen g h out.1.1 out.1.2 = pedersen g h out.2.1 out.2.2))
/-- $\mathrm{Adv}^{\mathrm{cr}}_{\mathcal A} := \Pr[\mathrm{Game}^{\mathrm{cr}}_{\mathcal A} = 1]$. -/
noncomputable def cvhpCollisionAdv [Fintype G] [DecidableEq G] (g : G) (p : ℕ)
(A : CvHPAdversary G p) : ℝ≥0∞ :=
cvhpCollisionGame g p A true
/-! ## The KR00 chameleon hash -/
/-- `TCol(x, m, r, m') = r + (m - m')/x` in `ℤ_p`. -/
def kr00TCol {p : ℕ} (x m r m' : ZMod p) : ZMod p := r + (m - m') * x⁻¹
/-- A collision finder for the KR00 chameleon hash receives the hash key `h = g^x` and outputs
`(m, r, m', r')`. -/
abbrev KR00Adversary (G : Type*) (p : ℕ) := G → PMF ((ZMod p × ZMod p) × (ZMod p × ZMod p))
/-- $\mathrm{Game}^{\mathrm{cr}}$ for the KR00 chameleon hash: `KeyGen` samples `x ← ℤ_p^*` and
sets `hk = h = g^x`; the adversary receives `h` and wins if it outputs `(m, r), (m', r')` with
`m ≠ m'` and `CH(h, m; r) = CH(h, m'; r')`. -/
noncomputable def kr00CollisionGame [DecidableEq G] (g : G) (p : ℕ) [NeZero p]
(A : KR00Adversary G p) : PMF Bool :=
(uniformNonZero p).bind fun x =>
(A (g ^ x.val)).bind fun out =>
PMF.pure (decide (out.1.1 ≠ out.2.1 ∧
pedersen g (g ^ x.val) out.1.1 out.1.2 = pedersen g (g ^ x.val) out.2.1 out.2.2))
/-- $\mathrm{Adv}^{\mathrm{cr}}_{\mathcal A} := \Pr[\mathrm{Game}^{\mathrm{cr}}_{\mathcal A} = 1]$. -/
noncomputable def kr00CollisionAdv [DecidableEq G] (g : G) (p : ℕ) [NeZero p]
(A : KR00Adversary G p) : ℝ≥0∞ :=
kr00CollisionGame g p A true
end CryptologyCity
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Throughout, is an arbitrary group (written multiplicatively, identity ), is an arbitrary element and is an arbitrary natural number. Exponents live in (Mathlib's ZMod p; note that for this is ). For , denotes its canonical natural-number representative (in when ; when ), and "" always means , a natural-number power. Nothing in this file assumes that generates , that has order , or that is prime. "Probability" means the mass a probability distribution assigns, valued in . Adversaries are arbitrary functions from their input to a probability distribution over outputs; no efficiency, uniformity or computational model is imposed.
Uniform non-identity element. For a finite group with decidable equality, is the uniform distribution on ; if this set is empty (i.e. is trivial), it is instead the point mass at .
Uniform non-zero exponent. For , is the uniform distribution on ; if this set is empty (i.e. ), it is the point mass at . For composite this set includes non-units of ; it is the set of non-zero residues, not the unit group.
Pedersen function. For and ,
The docstring states that this single function serves as the Pedersen commitment, the CvHP91 hash and the KR00 chameleon hash .
Pedersen binding adversary. A function that, given , outputs a distribution over tuples .
Pedersen binding game and advantage. For finite with decidable equality, , and a binding adversary : sample , then , and output iff
Only the messages must differ; is allowed. is the probability that this game outputs . The value is used only inside ; is not given explicitly (it is a fixed parameter).
CvHP91 collision adversary. A function that, given , outputs a distribution over pairs .
CvHP91 collision game and advantage. For finite with decidable equality: sample , then , and output iff
is the probability of output . The code samples uniformly from for every finite , whatever its order; the docstring says the same and adds that this set is the set of generators of when is prime. Nothing in the definition requires to be prime, so for other orders may be a non-generator.
KR00 trapdoor-collision function. For ,
where is Mathlib's total inverse on : for prime it is the field inverse with ; for non-prime and non-unit it is a junk value (and ).
KR00 collision adversary. A function that, given , outputs a distribution over .
KR00 collision game and advantage. For with decidable equality (not necessarily finite) and : sample , set , run , and output iff
is the probability of output . Here, unlike the CvHP91 game, the messages must differ (differing only in does not count), and is distributed as for uniform non-zero rather than uniform on .
Imported notions used below. means every element of is an integer power of and the order of is exactly (Mathlib's orderOf, which is for an element of infinite order); with prime this makes cyclic of order generated by . For and a DLOG adversary , the DLOG game samples uniformly from all of (including ), runs , and outputs iff ; is the probability of output .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.