Skip to content
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions Examples/CommitmentScheme/Hiding/CountBounds.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1152,10 +1152,10 @@ abbrev hidingAvgLeftImpl :

/-- The commitment-oracle handler of the averaged hiding experiment: runs the counting
handler `hidingImplCountAll` on each query, lifted into `HidingAvgSpec`. -/
abbrev hidingAvgRightImpl :
@[simps] def hidingAvgRightImpl :
QueryImpl (CMOracle M S C)
(StateT (HidingCountState M S C) (OracleComp (HidingAvgSpec M S C))) :=
fun t =>
(StateT (HidingCountState M S C) (OracleComp (HidingAvgSpec M S C))) where
toFun t :=
StateT.mk fun st =>
OracleComp.liftComp
((hidingImplCountAll (M := M) (S := S) (C := C) t).run st)
Expand Down
19 changes: 11 additions & 8 deletions Examples/CommitmentScheme/Hiding/Defs.lean
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,11 @@ instance : DecidablePred (hidingBad (M := M) (S := S) (C := C)) :=

/-- Real oracle implementation for the hiding game.
Standard caching + increments salt counter when any query has salt `s`. -/
@[simps -isSimp]
def hidingImpl₁ (s : S) :
QueryImpl (CMOracle M S C)
(StateT (QueryCache (CMOracle M S C) × ℕ) (OracleComp (CMOracle M S C))) :=
fun (ms : M × S) => do
(StateT (QueryCache (CMOracle M S C) × ℕ) (OracleComp (CMOracle M S C))) where
toFun (ms : M × S) := do
let (cache, cnt) ← get
match cache ms with
| some u => return u
Expand All @@ -139,8 +140,8 @@ def hidingImpl₁ (s : S) :
with a per-salt miss counter `S → ℕ` updated at the queried salt on cache miss. -/
def hidingImplCountAll :
QueryImpl (CMOracle M S C)
(StateT (QueryCache (CMOracle M S C) × (S → ℕ)) (OracleComp (CMOracle M S C))) :=
fun (ms : M × S) => do
(StateT (QueryCache (CMOracle M S C) × (S → ℕ)) (OracleComp (CMOracle M S C))) where
toFun (ms : M × S) := do
let (cache, counts) ← get
match cache ms with
| some u => return u
Expand Down Expand Up @@ -259,10 +260,11 @@ theorem run_hidingImplCountAll_proj_eq_cachingOracle
/-- Intermediate oracle implementation for the hiding game.
Same as `hidingImpl₁`, except when `cnt ≥ 2` (bad) and cache miss with salt `s`,
queries the underlying oracle at `(default, default)` instead. -/
@[simps]
def hidingImpl₂ [Inhabited M] [Inhabited S] (s : S) :
QueryImpl (CMOracle M S C)
(StateT (QueryCache (CMOracle M S C) × ℕ) (OracleComp (CMOracle M S C))) :=
fun (ms : M × S) => do
(StateT (QueryCache (CMOracle M S C) × ℕ) (OracleComp (CMOracle M S C))) where
toFun (ms : M × S) := do
let (cache, cnt) ← get
match cache ms with
| some u => return u
Expand All @@ -285,10 +287,11 @@ change the marginal distribution of the returned value.
The key difference from `hidingImpl₂` (which only redirects when `cnt ≥ 2`):
`hidingImplSim` redirects ALL salt-s cache misses, including the very first one
(the challenge query). This makes `hidingImplSim` match the simulator's behavior. -/
@[simps]
def hidingImplSim [Inhabited M] [Inhabited S] (s : S) :
QueryImpl (CMOracle M S C)
(StateT (QueryCache (CMOracle M S C) × ℕ) (OracleComp (CMOracle M S C))) :=
fun (ms : M × S) => do
(StateT (QueryCache (CMOracle M S C) × ℕ) (OracleComp (CMOracle M S C))) where
toFun (ms : M × S) := do
let (cache, cnt) ← get
match cache ms with
| some u => return u
Expand Down
4 changes: 3 additions & 1 deletion Examples/ElGamal/ComputationalComplexity.lean
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ end OpenReduction
/-! ## Semantic closing -/

/-- Fair-coin implementation for the right port of the syntactic reduction. -/
def oneTimeDDHFairCoinImpl : QueryImpl coinSpec ProbComp
@[simps]
def oneTimeDDHFairCoinImpl : QueryImpl coinSpec ProbComp where
toFun
| () => ($ᵗ Bool)

section Closing
Expand Down
17 changes: 10 additions & 7 deletions Examples/ElGamal/GenericINDCPA.lean
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ namespace GenericAdversary

/-- Forward private coins and left/right queries, appending the returned ciphertext components
in order. Each response receives fresh handles, including responses served by the game's cache. -/
@[expose] def queryImpl [Field F] [Fintype F] [DecidableEq F] [SampleableType F]
@[expose, simps -isSimp] def queryImpl [Field F] [Fintype F] [DecidableEq F] [SampleableType F]
[AddCommGroup G] [Module F G] [SampleableType G] (gen : G) :
QueryImpl (unifSpec + (Handle × Handle →ₒ Handle × Handle))
(StateT (Tbl (fun _ : Unit => G))
(OracleComp (elGamalAsymmEnc F G gen).IND_CPA_oracleSpec))
(OracleComp (elGamalAsymmEnc F G gen).IND_CPA_oracleSpec)) where
toFun
| .inl n => fun tbl => do
let u ← (elGamalAsymmEnc F G gen).IND_CPA_oracleSpec.query (.inl n)
pure (u, tbl)
Expand Down Expand Up @@ -96,7 +97,7 @@ private def cacheFind (F : Type) (mm : HandlePair) :

private def encrypt [SampleableType F] (m : Handle) :
OracleComp (genSpec F groupSig) HandlePair := do
let r ← simulateQ (fun n => liftM ((genSpec F groupSig).query (.inr n))) ($ᵗ F)
let r ← simulateQ ⟨fun n => liftM ((genSpec F groupSig).query (.inr n))⟩ ($ᵗ F)
let c₁ ← (genSpec F groupSig).query (.inl (.smul () r 0))
let pad ← (genSpec F groupSig).query (.inl (.smul () r 1))
let c₂ ← (genSpec F groupSig).query (.inl (.add () m pad))
Expand All @@ -108,9 +109,11 @@ private def groupStep (z : Handle) (t : Query F groupSig) :
let (u, τ) ← (virtualizeImpl (fun _ => z) (.inl t)).run st.translation
pure (u, { st with translation := τ })

@[simps]
private def suffixImpl [SampleableType F] (z : Handle) :
QueryImpl (spec F groupSig + (unifSpec + (HandlePair →ₒ HandlePair)))
(StateT SimState (OracleComp (genSpec F groupSig)))
(StateT SimState (OracleComp (genSpec F groupSig))) where
toFun
| .inl t => groupStep z t
| .inr (.inl n) => fun st => do
let u ← (genSpec F groupSig).query (.inr n)
Expand Down Expand Up @@ -195,7 +198,7 @@ private theorem cacheFind_bound (mm : HandlePair) (cs : List (HandlePair × Hand
private theorem encrypt_bound [SampleableType F] (m : Handle) :
IsQueryBoundP (encrypt (F := F) m) (fun i => isGroupOp i = true) 3 := by
have hc : IsQueryBoundP
(simulateQ (fun n => liftM ((genSpec F groupSig).query (.inr n))) ($ᵗ F))
(simulateQ ⟨fun n => liftM ((genSpec F groupSig).query (.inr n))⟩ ($ᵗ F))
(fun i => isGroupOp i = true) 0 :=
isQueryBoundP_simulateQ_zero _ (fun n => by
erw [isQueryBoundP_query_iff]; simp [isGroupOp])
Expand Down Expand Up @@ -886,7 +889,7 @@ variable [Field F] [AddCommGroup G] [Module F G]
private def groupResult [DecidableEq G] (z : Handle) (t : Query F groupSig) (st : SimState)
(T : Tbl (fun _ : Unit => G)) :
((spec F groupSig).Range t × SimState) × Tbl (fun _ : Unit => G) :=
run (Model.group G) (fun _ => id) (m := Id) (fun _ => pure 0)
run (Model.group G) (fun _ => id) (m := Id) ⟨fun _ => pure 0⟩
((groupStep z t).run st) T

private theorem runGen_groupStep [DecidableEq G] (z : Handle) (t : Query F groupSig) (st : SimState)
Expand Down Expand Up @@ -946,7 +949,7 @@ private theorem groupResult_read [DecidableEq G] {gen pk : G} {z : Handle} {st :
private theorem runGen_liftCoins [DecidableEq G] {α : Type} (A : ProbComp α)
(T : Tbl (fun _ : Unit => G)) :
runGen (Model.group (F := F) G) (fun _ => id)
(simulateQ (fun n => liftM ((genSpec F groupSig).query (.inr n))) A) T =
(simulateQ ⟨fun n => liftM ((genSpec F groupSig).query (.inr n))⟩ A) T =
(fun a => (a, T)) <$> A := by
induction A using OracleComp.inductionOn with
| pure a => rfl
Expand Down
4 changes: 3 additions & 1 deletion Examples/ElGamal/ReductionCost.lean
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,11 @@ noncomputable def IND_CPA_OneTime_DDHReduction_openCost
_gen A B T

/-- Implementation of the reified one-time IND-CPA interface using a concrete adversary. -/
@[simps]
def oneTimeINDCPAImpl {gen : G}
(adv : AsymmEncAlg.IND_CPA_OneTime_Adversary (elGamalAsymmEnc F G gen)) :
QueryImpl (oneTimeINDCPASpec G G adv.State (G × G)) ProbComp
QueryImpl (oneTimeINDCPASpec G G adv.State (G × G)) ProbComp where
toFun
| .chooseMessages pk => adv.chooseMessages pk
| .distinguish st c => adv.distinguish st c

Expand Down
26 changes: 18 additions & 8 deletions Examples/ElGamal/SSP.lean
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,19 @@ noncomputable def realDhChallenge (gen : G) :

The first exponent `a` is lazily sampled on first access and cached in the state. `GETPK`
returns `a • gen` and `DHCHALLENGE` returns `(b • gen, (a * b) • gen)` for fresh `b`. -/
@[simps -isSimp]
noncomputable def dhTripleReal (gen : G) :
QueryImpl.Stateful unifSpec (dhSpec G) (Option F)
QueryImpl.Stateful unifSpec (dhSpec G) (Option F) where
toFun
| Sum.inl _ => lazyPublicKey (F := F) (G := G) gen
| Sum.inr _ => realDhChallenge (F := F) (G := G) gen

/-- The "random" DDH handler (multi-query, shared-`a`). Identical to `dhTripleReal` except
`DHCHALLENGE` returns `(b • gen, c • gen)` for fresh `b, c`. -/
@[simps -isSimp]
noncomputable def dhTripleRand (gen : G) :
QueryImpl.Stateful unifSpec (dhSpec G) (Option F)
QueryImpl.Stateful unifSpec (dhSpec G) (Option F) where
toFun
| Sum.inl _ => fun st => match st with
| none => do
let a ← ($ᵗ F)
Expand Down Expand Up @@ -194,16 +198,20 @@ noncomputable def dhTripleRand (gen : G) :
The convention `(B, T + m_b)` (rather than `(B, m_b + T)`) matches the DDH-to-LR reduction's
output, so the equivalence with `dhToLR_left.link dhTripleReal` is definitional up to
alpha-renaming of the sampled exponents (`a, b` on the DDH side and `sk, r` here). -/
@[simps -isSimp]
noncomputable def elgamalLR_left (gen : G) :
QueryImpl.Stateful unifSpec (lrSpec G) (Option F)
QueryImpl.Stateful unifSpec (lrSpec G) (Option F) where
toFun
| Sum.inl _ => lazyPublicKey (F := F) (G := G) gen
| Sum.inr (m₀, _) =>
(fun bt => (bt.1, bt.2 + m₀)) <$> realDhChallenge (F := F) (G := G) gen

/-- The "right-message" ElGamal LR game. Same as `elgamalLR_left` except `LR (_, m₁)` returns
`(r • gen, (sk * r) • gen + m₁)`. -/
@[simps -isSimp]
noncomputable def elgamalLR_right (gen : G) :
QueryImpl.Stateful unifSpec (lrSpec G) (Option F)
QueryImpl.Stateful unifSpec (lrSpec G) (Option F) where
toFun
| Sum.inl _ => lazyPublicKey (F := F) (G := G) gen
| Sum.inr (_, m₁) =>
(fun bt => (bt.1, bt.2 + m₁)) <$> realDhChallenge (F := F) (G := G) gen
Expand All @@ -214,15 +222,17 @@ noncomputable def elgamalLR_right (gen : G) :
to `GETPK` on `dh`, and forwards `LR (m₀, _)` to `DHCHALLENGE` on `dh`, returning the pair
`(B, T + m₀)` from the DDH challenge `(B, T)`. -/
def dhToLR_leftHandler {G : Type} [Add G] :
QueryImpl (lrSpec G) (OracleComp (dhSpec G))
QueryImpl (lrSpec G) (OracleComp (dhSpec G)) where
toFun
| Sum.inl _ => dhGetPublicKey
| Sum.inr (m₀, _) => do
let bt ← dhChallenge
pure (bt.1, bt.2 + m₀)

/-- Stateless reduction handler encrypting the *right* message. -/
def dhToLR_rightHandler {G : Type} [Add G] :
QueryImpl (lrSpec G) (OracleComp (dhSpec G))
QueryImpl (lrSpec G) (OracleComp (dhSpec G)) where
toFun
| Sum.inl _ => dhGetPublicKey
| Sum.inr (_, m₁) => do
let bt ← dhChallenge
Expand Down Expand Up @@ -282,7 +292,7 @@ theorem distEquiv_dhToLR_left_link_real_eq_elgamalLR_left (gen : G) :
apply QueryImpl.Stateful.DistEquiv.of_run_eq
intro α A
apply QueryImpl.Stateful.run_link_left_ofStateless_eq_of_compose_eq
funext q state
ext q state : 2
exact composed_real_left_handler_run_eq gen q state

/-- The real DDH reduction and LR-right handler have equal adaptive output measures. -/
Expand All @@ -293,7 +303,7 @@ theorem distEquiv_dhToLR_right_link_real_eq_elgamalLR_right (gen : G) :
apply QueryImpl.Stateful.DistEquiv.of_run_eq
intro α A
apply QueryImpl.Stateful.run_link_left_ofStateless_eq_of_compose_eq
funext q state
ext q state : 2
exact composed_real_right_handler_run_eq gen q state

end ReductionEquivalences
Expand Down
8 changes: 6 additions & 2 deletions Examples/OneTimePad/HeapBasic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ instance instCellSpecUsedFlag : CellSpec UsedFlag where
* **Handler.** On the first `enc m` call (`h .used = false`), sample a
uniform key `k` *locally*, return `k ⊕ m`, and flip `.used := true`.
On subsequent calls (`h .used = true`), short-circuit to `0#sp`. -/
def realImpl (sp : ℕ) : QueryImpl.Stateful unifSpec (otpSpec sp) (Heap UsedFlag)
@[simps]
def realImpl (sp : ℕ) : QueryImpl.Stateful unifSpec (otpSpec sp) (Heap UsedFlag) where
toFun
| .enc m => StateT.mk fun (h : Heap UsedFlag) =>
if h .used then
pure (0#sp, h)
Expand All @@ -154,7 +156,9 @@ The same identifier set as `realImpl` is shared on purpose: it lets the
proof `realImpl_distEquiv_idealImpl` use the simple
`QueryImpl.Stateful.DistEquiv.of_step` constructor (per-handler measure
equality), avoiding any heap bijection bookkeeping. -/
def idealImpl (sp : ℕ) : QueryImpl.Stateful unifSpec (otpSpec sp) (Heap UsedFlag)
@[simps]
def idealImpl (sp : ℕ) : QueryImpl.Stateful unifSpec (otpSpec sp) (Heap UsedFlag) where
toFun
| .enc _ => StateT.mk fun (h : Heap UsedFlag) =>
if h .used then
pure (0#sp, h)
Expand Down
14 changes: 8 additions & 6 deletions Examples/OneTimePad/Reactive.lean
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ attribute [local implicit_reducible] network DynComputation.ofFreeM
@[expose] def implementation (env : Environment Message Cipher Memory)
(ops : Operations Message Cipher S) :
(id : Bool) → Handler (StateT S ProbComp) ((network Message Cipher Memory).effect id)
| false, .choose => fun state => (fun message => (message, state)) <$> env.choose
| false, .observe input view => fun state =>
(fun verdict => (verdict, state)) <$> env.observe input.1 input.2 view
| true, .encrypt message => ops.encrypt message
| true, .allow ciphertext => ops.allow ciphertext
| true, .decrypt ciphertext => ops.decrypt ciphertext
| false => ⟨fun
| .choose => fun state => (fun message => (message, state)) <$> env.choose
| .observe input view => fun state =>
(fun verdict => (verdict, state)) <$> env.observe input.1 input.2 view⟩
| true => ⟨fun
| .encrypt message => ops.encrypt message
| .allow ciphertext => ops.allow ciphertext
| .decrypt ciphertext => ops.decrypt ciphertext⟩

/-- Direct interpretation of the same single-use conversation, retaining shared state. -/
@[expose] def conversation (env : Environment Message Cipher Memory)
Expand Down
15 changes: 8 additions & 7 deletions Examples/OneTimePad/Separated.lean
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,14 @@ abbrev ActorSignature (Message Cipher Random SendKey ReceiveKey Memory Advice :
| .receiver, ⟨_, .inr delivered⟩ => .inl ⟨.environment, ⟨(), .inr delivered⟩⟩
ingress packet := packet.1.elim
handler
| .environment, .choose => env.choose
| .environment, .advise input ciphertext => env.advise input.1 input.2 ciphertext
| .environment, .observe input ciphertext delivered =>
env.observe input.1 input.2 ciphertext delivered
| .setup, _ => draw
| .adversary, input => adversary.allow input.1 input.2
| .sender, operation | .channel, operation | .receiver, operation => operation.elim
| .environment => ⟨fun
| .choose => env.choose
| .advise input ciphertext => env.advise input.1 input.2 ciphertext
| .observe input ciphertext delivered =>
env.observe input.1 input.2 ciphertext delivered⟩
| .setup => ⟨fun _ => draw⟩
| .adversary => ⟨fun input => adversary.allow input.1 input.2⟩
| .sender | .channel | .receiver => ⟨fun operation => operation.elim⟩

/-- A finite closed assembly of the six actual actors. -/
@[expose] def assembly (encoding : Encoding Message Cipher Random SendKey ReceiveKey)
Expand Down
52 changes: 27 additions & 25 deletions Examples/PRFTagReader/Auth.lean
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,39 @@ the authenticator. Models `authTagQueryImpl` with the hash replaced by a PRF ora
def authToPRFTagImpl :
QueryImpl (TagId →ₒ TagTranscript Nonce Digest)
(StateT (AuthState TagId Nonce Digest)
(OracleComp (unifSpec + ((TagId × Nonce) →ₒ Digest)))) := fun tag => do
let st ← get
let nonce ← (OracleComp.liftComp (spec := unifSpec)
(superSpec := unifSpec + ((TagId × Nonce) →ₒ Digest)) ($ᵗ Nonce) :
OracleComp (unifSpec + ((TagId × Nonce) →ₒ Digest)) Nonce)
let auth ← authPRFQuery (TagId := TagId) (Nonce := Nonce) (Digest := Digest) tag nonce
let transcript : TagTranscript Nonce Digest := ⟨nonce, auth⟩
set { st with honestOutputs := insert (tag, transcript) st.honestOutputs }
return transcript
(OracleComp (unifSpec + ((TagId × Nonce) →ₒ Digest)))) where
toFun tag := do
let st ← get
let nonce ← (OracleComp.liftComp (spec := unifSpec)
(superSpec := unifSpec + ((TagId × Nonce) →ₒ Digest)) ($ᵗ Nonce) :
OracleComp (unifSpec + ((TagId × Nonce) →ₒ Digest)) Nonce)
let auth ← authPRFQuery (TagId := TagId) (Nonce := Nonce) (Digest := Digest) tag nonce
let transcript : TagTranscript Nonce Digest := ⟨nonce, auth⟩
set { st with honestOutputs := insert (tag, transcript) st.honestOutputs }
return transcript

/-- Reader-oracle implementation that queries the PRF oracle for every tag at the transcript's
nonce in order to identify the matching tags. Models `authReaderQueryImpl` with the hash
replaced by a PRF oracle call. -/
noncomputable def authToPRFReaderImpl :
QueryImpl ((TagTranscript Nonce Digest) →ₒ ReaderReply)
(StateT (AuthState TagId Nonce Digest)
(OracleComp (unifSpec + ((TagId × Nonce) →ₒ Digest)))) := fun transcript => do
let st ← get
let pairs ←
(Finset.univ : Finset TagId).toList.mapM
(m := OracleComp (unifSpec + ((TagId × Nonce) →ₒ Digest)))
(fun tag => do
let d ← authPRFQuery (TagId := TagId) (Nonce := Nonce) (Digest := Digest)
tag transcript.nonce
return (tag, d))
let accepted : Bool := decide (∃ p ∈ pairs, p.2 = transcript.auth)
let newForged : Finset TagId :=
((pairs.filter fun p => decide (p.2 = transcript.auth ∧
(p.1, transcript) ∉ st.honestOutputs)).map Prod.fst).toFinset
set { st with readerForged := st.readerForged ∪ (newForged.image (·, transcript)) }
return ReaderReply.ofBool accepted
(OracleComp (unifSpec + ((TagId × Nonce) →ₒ Digest)))) where
toFun transcript := do
let st ← get
let pairs ←
(Finset.univ : Finset TagId).toList.mapM
(m := OracleComp (unifSpec + ((TagId × Nonce) →ₒ Digest)))
(fun tag => do
let d ← authPRFQuery (TagId := TagId) (Nonce := Nonce) (Digest := Digest)
tag transcript.nonce
return (tag, d))
let accepted : Bool := decide (∃ p ∈ pairs, p.2 = transcript.auth)
let newForged : Finset TagId :=
((pairs.filter fun p => decide (p.2 = transcript.auth ∧
(p.1, transcript) ∉ st.honestOutputs)).map Prod.fst).toFinset
set { st with readerForged := st.readerForged ∪ (newForged.image (·, transcript)) }
return ReaderReply.ofBool accepted

/-- Combined oracle implementation that simulates the authentication game while hashing through
the PRF oracle. -/
Expand Down Expand Up @@ -255,7 +257,7 @@ private lemma simulateQ_prfReal_authToPRFTagImpl_run [SampleableType Nonce]
((authToPRFTagImpl (TagId := TagId) tag).run s) =
(authTagQueryImpl (fun tag nonce => prfs.evalMultiple k tag nonce) tag).run s := by
let so : QueryImpl ((TagId × Nonce) →ₒ Digest) ProbComp :=
fun d => pure (prfs.multiplePRFScheme.eval k d)
⟨fun d => pure (prfs.multiplePRFScheme.eval k d)⟩
let impl : QueryImpl (unifSpec + ((TagId × Nonce) →ₒ Digest)) ProbComp :=
unifSpec.passthrough + so
have hleft : ∀ {α : Type} (oa : ProbComp α),
Expand Down
Loading
Loading