Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions EthLib/Cryptography/Proofs/HashSig.lean
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ A read of a fixed-size object succeeds exactly when the buffer has room, and rea
A read out of a concatenation reads the part it covers.

In the identity monad, a vector computed in order is the vector of its entries.

A map of monads that keeps `pure` and `bind` commutes with computing a vector in order.
-/

@[expose] public section
Expand Down Expand Up @@ -127,4 +129,14 @@ theorem packBytes_readBytes (b : ByteArray) (offset n : Nat) (h) :
| zero => simp
| succ i => simp [Fin.succ]

/-- A map of monads that keeps `pure` and `bind` commutes with computing a vector in order. -/
theorem ofFnInOrder_map_monad {m n : Type → Type} [Monad m] [Monad n]
(φ : ∀ {β : Type}, m β → n β) (map_pure : ∀ {β : Type} (b : β), φ (pure b) = pure b)
(map_bind : ∀ {β γ : Type} (x : m β) (f : β → m γ), φ (x >>= f) = φ x >>= fun b => φ (f b))
{α : Type} :
∀ (k : Nat) (f : Fin k → m α), φ (ofFnInOrder k f) = ofFnInOrder k fun i => φ (f i)
| 0, _ => map_pure _
| k + 1, f => by
simp only [ofFnInOrder, map_bind, map_pure, ofFnInOrder_map_monad φ map_pure map_bind k]

end EthLib.Cryptography.HashSig
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ theorem findFirst_map_monad {κ β : Type} (trial : Nat → κ) (test : κ → m
/-- The chain starts commute with the map. -/
theorem chainStarts_map_monad (pp : PublicParam) (key : OtsKey) :
φ (chainStarts pp prf key) = chainStarts pp (mapPrf φ prf) key := by
rw [chainStarts, chainStarts, map_bind, ofFnInOrder_map_monad φ map_pure map_bind]
rw [chainStarts, chainStarts, map_bind, HashSig.ofFnInOrder_map_monad φ map_pure map_bind]
exact congrArg _ (funext fun _ => map_pure _)

/-- A one-time key's leaf, from the seed, commutes with the map. -/
theorem otsPublicLeaf_map_monad (pp : PublicParam) (key : OtsKey) :
φ (otsPublicLeaf H pp prf key) = otsPublicLeaf (fun x => φ (H x)) pp (mapPrf φ prf) key := by
rw [otsPublicLeaf, otsPublicLeaf, map_bind, chainStarts_map_monad φ map_pure map_bind]
refine congrArg _ (funext fun starts => ?_)
simp only [map_bind, ofFnInOrder_map_monad φ map_pure map_bind, chain,
chain_map_monad φ map_pure map_bind, chainStep_map_monad φ map_pure map_bind, otsLeafHash,
simp only [map_bind, HashSig.ofFnInOrder_map_monad φ map_pure map_bind, chain,
Wots.chain_map_monad φ map_pure map_bind, chainStep_map_monad φ map_pure map_bind, otsLeafHash,
th_map_monad φ map_pure map_bind]

/-- The counter search commutes with the map. -/
Expand All @@ -108,8 +108,8 @@ theorem otsSign_map_monad [LawfulMonad m] [LawfulMonad n] (pp : PublicParam) (ke
| none => exact map_pure _
| some found =>
simp only [map_bind, map_pure, chainStarts_map_monad φ map_pure map_bind,
ofFnInOrder_map_monad φ map_pure map_bind, chain, chain_map_monad φ map_pure map_bind,
chainStep_map_monad φ map_pure map_bind]
HashSig.ofFnInOrder_map_monad φ map_pure map_bind, chain,
Wots.chain_map_monad φ map_pure map_bind, chainStep_map_monad φ map_pure map_bind]

/-- A hypertree node commutes with the map. -/
theorem node_map_monad (pp : PublicParam) (layer : Layer) (tree level j : Nat) :
Expand Down Expand Up @@ -159,7 +159,7 @@ theorem forsNode_map_monad (pp : PublicParam) (idx : Nat) (kappa : Fin FORS_TREE
/-- A FORS key commutes with the map. -/
theorem forsKey_map_monad (pp : PublicParam) (idx : Nat) :
φ (forsKey H pp prf idx) = forsKey (fun x => φ (H x)) pp (mapPrf φ prf) idx := by
rw [forsKey, forsKey, ofFnInOrder_map_monad φ map_pure map_bind]
rw [forsKey, forsKey, HashSig.ofFnInOrder_map_monad φ map_pure map_bind]
simp only [map_bind, map_pure, forsNode_map_monad φ map_pure map_bind]

/-- A FORS opening commutes with the map. -/
Expand All @@ -176,7 +176,7 @@ theorem forsSign_map_monad (pp : PublicParam) (idx : Nat)
(leaves : Vector (Fin (2 ^ FORS_HEIGHT)) FORS_TREES) :
φ (forsSign H pp prf idx leaves)
= forsSign (fun x => φ (H x)) pp (mapPrf φ prf) idx leaves := by
rw [forsSign, forsSign, ofFnInOrder_map_monad φ map_pure map_bind]
rw [forsSign, forsSign, HashSig.ofFnInOrder_map_monad φ map_pure map_bind]
simp only [treeOpen_map_monad φ map_pure map_bind]

/-- The randomizer commutes with the map. -/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module

public import EthLib.Cryptography.Proofs.HashSig
public import EthLib.Cryptography.Proofs.Merkle.Completeness
public import EthLib.Cryptography.Proofs.Sphincs.Encoding
public import EthLib.Cryptography.Proofs.Sphincs.Unforgeability.Game
Expand Down Expand Up @@ -115,25 +116,6 @@ theorem map_monad_map {β γ : Type} (g : β → γ) (x : m β) : φ (g <$> x) =
rw [map_eq_bind_pure_comp, map_eq_bind_pure_comp, map_bind]
simp only [Function.comp_def, map_pure]

omit [LawfulMonad m] [LawfulMonad n] in
/-- Computing a vector in order commutes with the map. -/
theorem ofFnInOrder_map_monad {α : Type} :
∀ (k : Nat) (f : Fin k → m α),
φ (HashSig.ofFnInOrder k f) = HashSig.ofFnInOrder k fun i => φ (f i)
| 0, _ => map_pure _
| k + 1, f => by
simp only [HashSig.ofFnInOrder, map_bind, map_pure, ofFnInOrder_map_monad k]

omit [LawfulMonad m] [LawfulMonad n] in
/-- A hash chain commutes with the map. -/
theorem chain_map_monad {α : Type} {k : Nat} (step : Fin k → α → m α) (start : Nat) :
∀ (steps : Nat) (h : start + steps ≤ k) (value : α),
φ (Wots.chain step start steps h value)
= Wots.chain (fun i v => φ (step i v)) start steps h value
| 0, _, _ => map_pure _
| s + 1, h, value => by
rw [Wots.chain_succ, Wots.chain_succ, map_bind, chain_map_monad step start s]

omit [LawfulMonad m] [LawfulMonad n] in
/-- A tweakable hash call commutes with the map. -/
theorem th_map_monad (pp : PublicParam) (A : Address) (payload : ByteArray) :
Expand Down Expand Up @@ -169,7 +151,7 @@ omit [LawfulMonad m] [LawfulMonad n] in
/-- The message digest commutes with the map. -/
theorem messageDigest_map_monad (pp : PublicParam) (msg : Message) (rho : Randomizer) :
φ (messageDigest H pp msg rho) = messageDigest (fun x => φ (H x)) pp msg rho := by
rw [messageDigest, messageDigest, map_bind, ofFnInOrder_map_monad φ map_pure map_bind]
rw [messageDigest, messageDigest, map_bind, HashSig.ofFnInOrder_map_monad φ map_pure map_bind]
exact congrArg _ (funext fun _ => map_pure _)

omit [LawfulMonad m] [LawfulMonad n] in
Expand All @@ -191,7 +173,7 @@ theorem forsRecover_map_monad (pp : PublicParam) (idx : Nat)
(openings : Vector TreeOpening FORS_TREES) :
φ (forsRecover H pp idx leaves openings)
= forsRecover (fun x => φ (H x)) pp idx leaves openings := by
rw [forsRecover, forsRecover, ofFnInOrder_map_monad φ map_pure map_bind]
rw [forsRecover, forsRecover, HashSig.ofFnInOrder_map_monad φ map_pure map_bind]
simp only [treeRecover_map_monad φ map_pure map_bind]

/-- Recovering a one-time leaf commutes with the map. -/
Expand All @@ -204,9 +186,9 @@ theorem otsLeaf_map_monad (pp : PublicParam) (key : OtsKey) (msg : ByteArray) (c
cases x with
| none => exact map_pure _
| some x =>
simp only [map_bind, map_pure, ofFnInOrder_map_monad φ map_pure map_bind, chain,
chain_map_monad φ map_pure map_bind, chainStep_map_monad φ map_pure map_bind, otsLeafHash,
th_map_monad φ map_pure map_bind]
simp only [map_bind, map_pure, HashSig.ofFnInOrder_map_monad φ map_pure map_bind, chain,
Wots.chain_map_monad φ map_pure map_bind, chainStep_map_monad φ map_pure map_bind,
otsLeafHash, th_map_monad φ map_pure map_bind]

/-- A layer's root commutes with the map. -/
theorem layerRoot_map_monad (pp : PublicParam) (layer : Layer) (idx : Nat) (msg : ByteArray)
Expand Down
13 changes: 13 additions & 0 deletions EthLib/Cryptography/Proofs/Wots.lean
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public import VCVio.OracleComp.QueryTracking.RandomOracle.Simulation

A walk of `s₁` steps, then `s₂` more, is a walk of `s₁ + s₂` steps.

A map of monads that keeps `pure` and `bind` commutes with a walk.

The search returns the least admissible trial in its window.

When the hash is a random oracle, a search over fresh inputs fails with an exact probability.
Expand Down Expand Up @@ -60,6 +62,17 @@ theorem chain_add [LawfulMonad m] {s₁ s₂ mid : Nat} (hmid : mid = start + s
Fin.ext (Nat.add_assoc _ _ _)
rw [hpos]

/-- A map of monads that keeps `pure` and `bind` commutes with a walk. -/
theorem chain_map_monad {m' : Type → Type} [Monad m'] (φ : ∀ {β : Type}, m β → m' β)
(map_pure : ∀ {β : Type} (b : β), φ (pure b) = pure b)
(map_bind : ∀ {β γ : Type} (x : m β) (f : β → m γ), φ (x >>= f) = φ x >>= fun b => φ (f b))
(step : Fin n → α → m α) (start : Nat) :
∀ (steps : Nat) (h : start + steps ≤ n) (value : α),
φ (chain step start steps h value) = chain (fun i v => φ (step i v)) start steps h value
| 0, _, _ => map_pure _
| s + 1, h, value => by
rw [chain_succ, chain_succ, map_bind, chain_map_monad φ map_pure map_bind step start s]

end Chain

/-! ## The search -/
Expand Down
Loading