Documentation

CslibFrontier.Languages.Probabilistic.RandomPermutation

Lazy sampling of random permutations and the PRP/PRF switching lemma #

A bidirectional permutation oracle (PermOracleSpec α) answers forward queries (.forward x) and inverse queries (.inverse y) on a type α. Over an association-list cache Cache α = List (α × α), we provide two lazy stateful samplers on finite nonempty types together with polymorphic bidirectional query deduplication over arbitrary [DecidableEq α]:

  1. Exact without-replacement lazy permutation (RandomPermutation.answer): on a cache hit, return the recorded image or preimage; on a cache miss, sample uniformly from the remaining unused range elements (unusedRange cache) for a forward query or from the remaining unused domain elements (unusedDomain cache) for an inverse query. Using OracleComp.run_interpret_simulation_of_invariant and uniform completion complete over the set extPerms cache of permutations extending the partial bijection, we prove that adaptive execution against answer followed by complete has the exact same joint distribution as sampling π ← PMF.uniformOfFintype (Equiv.Perm α) eagerly (run_interpret_answer_complete, uniform_interpret_eq_lazy).
  2. Forgetful with-replacement lazy permutation (RandomPermutation.forgetfulAnswer, [BS23], Section 4.7): on a cache miss, sample uniformly from all of α without domain or range exclusion. By coupling answer and forgetfulAnswer via resampling and OracleComp.IdenticalUntilBad, any Q-query adaptive distinguisher has distinguishing advantage at most Q.choose 2 / |α| between a uniformly random permutation π ← PMF.uniformOfFintype (Equiv.Perm α) and forgetfulAnswer (abs_sub_uniformPerm_forgetful_le). On forward-only programs (liftForward), forgetfulAnswer coincides with memoize (fun _ => PMF.uniformOfFintype α) and therefore with a uniformly random function f ← PMF.uniformOfFintype (α → α), yielding the classical ideal-experiment PRP/PRF switching lemma (abs_sub_uniformPerm_uniformFunc_le, [BS23], Section 4.4.3, Attack Game 4.3 and Theorem 4.6).
  3. Bidirectional query deduplication (RandomPermutation.bidirDedupStep, RandomPermutation.bidirDedup): for any [DecidableEq α], wrap a bidirectional oracle program so that forward hits in dom(cache) and inverse hits in ran(cache) are answered from cache without invoking the underlying oracle, preserving the output distribution against evalPerm π (map_fst_interpret_bidirDedup_evalPerm), preserving query bounds (isQueryBounded_bidirDedup), recording non-repeating underlying queries and the exact reverse-chronological transcript (nodup_transcript_of_mem_support_withTranscript_bidirDedup, cache_eq_of_mem_support_withTranscript_bidirDedup), and maintaining pairwise-distinct domain and range entries under evalPerm π (agreesWith_and_pairwise_of_mem_support_bidirDedup).

Main definitions #

Main results #

References #

Bidirectional permutation query interface #

A bidirectional query to a permutation on α: either evaluate in the forward direction or evaluate the inverse permutation.

  • forward {α : Type} (x : α) : PermQuery α

    Forward query x ↦ π(x).

  • inverse {α : Type} (y : α) : PermQuery α

    Inverse query y ↦ π⁻¹(y).

def Cslib.RandomPermutation.instDecidableEqPermQuery.decEq {α✝ : Type} [DecidableEq α✝] (x✝ x✝¹ : PermQuery α✝) :
Decidable (x✝ = x✝¹)
@[instance_reducible]
@[reducible, inline]

The oracle specification of a bidirectional permutation on α.

Deterministically answer a bidirectional permutation query using π : Equiv.Perm α.

@[simp]

Evaluating evalPerm π on a forward query .forward x applies π to x.

@[simp]

Evaluating evalPerm π on an inverse query .inverse y applies π.symm to y.

def Cslib.RandomPermutation.liftForward {α β : Type} (program : OracleComp (fun (x : α) => α) β) :

Lift a forward-only oracle program into a bidirectional permutation oracle program.

theorem Cslib.RandomPermutation.interpret_evalPerm_liftForward {α β : Type} (π : Equiv.Perm α) (program : OracleComp (fun (x : α) => α) β) :
OracleComp.interpret (fun (q : PermQuery α) => PMF.pure (evalPerm π q)) (liftForward program) = OracleComp.interpret (fun (x : α) => PMF.pure (π x)) program

Interpreting liftForward program against evalPerm π is interpreting program against π.

theorem Cslib.RandomPermutation.isQueryBounded_liftForward {α β : Type} {program : OracleComp (fun (x : α) => α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) :

Lifting a forward-only program preserves query bounds.

Convert a boolean-tagged query (false, x) (forward) or (true, y) (inverse) into a PermQuery α.

@[simp]

Tag false converts (false, x) into the forward query .forward x.

@[simp]

Tag true converts (true, y) into the inverse query .inverse y.

Bidirectional queries as channel-tagged points: .forward x is (false, x) and .inverse y is (true, y). The inverse is PermQuery.ofBoolProd.

def Cslib.RandomPermutation.liftBoolProd {α β : Type} (program : OracleComp (fun (x : Bool × α) => α) β) :

Lift a Bool × α bidirectional oracle program into a PermOracleSpec α program.

theorem Cslib.RandomPermutation.isQueryBounded_liftBoolProd {α β : Type} {program : OracleComp (fun (x : Bool × α) => α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) :

Lifting a Bool × α program preserves query bounds.

Orient a bidirectional query-answer pair ⟨q, a⟩ into an (input, output) cache entry: ⟨.forward x, y⟩ ↦ (x, y) and ⟨.inverse y, x⟩ ↦ (x, y).

@[simp]

A forward query-answer pair ⟨.forward x, y⟩ records the cache entry (x, y).

@[simp]

An inverse query-answer pair ⟨.inverse y, x⟩ records the cache entry (x, y).

Partial bijection cache and lookup lemmas #

@[reducible, inline]

A partial permutation table stored as a list of input-output pairs (x, y).

def Cslib.RandomPermutation.invLookup {α : Type} [DecidableEq α] (cache : Cache α) (y : α) :

Lookup the preimage x of y in cache by searching (y, x) in cache.map Prod.swap.

@[simp]

Inverse lookup in the empty cache returns none.

theorem Cslib.RandomPermutation.invLookup_cons {α : Type} [DecidableEq α] (e : α × α) (cache : Cache α) (y : α) :
invLookup (e :: cache) y = match y == e.2 with | true => some e.1 | false => invLookup cache y

Unfolding invLookup on a non-empty cache e :: cache.

theorem Cslib.RandomPermutation.mem_of_lookup_eq_some {α : Type} [DecidableEq α] {cache : Cache α} {x y : α} (h : List.lookup x cache = some y) :
(x, y) ∈ cache

If cache.lookup x = some y, then the pair (x, y) belongs to cache.

theorem Cslib.RandomPermutation.mem_of_invLookup_eq_some {α : Type} [DecidableEq α] {cache : Cache α} {y x : α} (h : invLookup cache y = some x) :
(x, y) ∈ cache

If invLookup cache y = some x, then the pair (x, y) belongs to cache.

theorem Cslib.RandomPermutation.lookup_eq_none_iff {α : Type} [DecidableEq α] {cache : Cache α} {x : α} :
List.lookup x cache = none ↔ ∀ e ∈ cache, e.1 ≠ x

Forward lookup of x in cache returns none iff no entry in cache has first component x.

theorem Cslib.RandomPermutation.invLookup_eq_none_iff {α : Type} [DecidableEq α] {cache : Cache α} {y : α} :
invLookup cache y = none ↔ ∀ e ∈ cache, e.2 ≠ y

Inverse lookup of y in cache returns none iff no entry in cache has second component y.

def Cslib.RandomPermutation.AgreesWith {α : Type} (cache : Cache α) (π : Equiv.Perm α) :

A permutation π : Equiv.Perm α agrees with cache if π e.1 = e.2 for all e ∈ cache.

Instances For
    @[instance_reducible]
    instance Cslib.RandomPermutation.decidableAgreesWith {α : Type} [DecidableEq α] (cache : Cache α) (π : Equiv.Perm α) :

    Decidability of AgreesWith cache π.

    @[simp]

    Every permutation agrees with the empty cache.

    @[simp]
    theorem Cslib.RandomPermutation.agreesWith_cons {α : Type} (e : α × α) (cache : Cache α) (π : Equiv.Perm α) :
    AgreesWith (e :: cache) π ↔ π e.1 = e.2 ∧ AgreesWith cache π

    A permutation π agrees with e :: cache iff π e.1 = e.2 and π agrees with cache.

    theorem Cslib.RandomPermutation.AgreesWith.apply_of_lookup_eq_some {α : Type} [DecidableEq α] {cache : Cache α} {π : Equiv.Perm α} (hπ : AgreesWith cache π) {x y : α} (h : List.lookup x cache = some y) :
    π x = y

    If π agrees with cache and cache.lookup x = some y, then π x = y.

    theorem Cslib.RandomPermutation.AgreesWith.symm_apply_of_invLookup_eq_some {α : Type} [DecidableEq α] {cache : Cache α} {π : Equiv.Perm α} (hπ : AgreesWith cache π) {y x : α} (h : invLookup cache y = some x) :
    (Equiv.symm π) y = x

    If π agrees with cache and invLookup cache y = some x, then π.symm y = x.

    theorem Cslib.RandomPermutation.AgreesWith.invLookup_apply_eq_none {α : Type} [DecidableEq α] {cache : Cache α} {π : Equiv.Perm α} (hπ : AgreesWith cache π) {x : α} (hx : List.lookup x cache = none) :
    invLookup cache (π x) = none

    If π agrees with cache and x is not in the domain of cache, then π x is not in the range of cache.

    theorem Cslib.RandomPermutation.AgreesWith.lookup_symm_apply_eq_none {α : Type} [DecidableEq α] {cache : Cache α} {π : Equiv.Perm α} (hπ : AgreesWith cache π) {y : α} (hy : invLookup cache y = none) :
    List.lookup ((Equiv.symm π) y) cache = none

    If π agrees with cache and y is not in the range of cache, then π.symm y is not in the domain of cache.

    theorem Cslib.RandomPermutation.AgreesWith.trans_swap_of_lookup_eq_none {α : Type} [DecidableEq α] {cache : Cache α} {π : Equiv.Perm α} (hπ : AgreesWith cache π) {x y : α} (hx : List.lookup x cache = none) (hy : invLookup cache y = none) :
    AgreesWith ((x, y) :: cache) (Equiv.trans π (Equiv.swap (π x) y))

    Post-composing a permutation agreeing with cache by Equiv.swap (π x) y extends agreement to (x, y) :: cache when x and y are fresh.

    theorem Cslib.RandomPermutation.AgreesWith.swap_trans_of_invLookup_eq_none {α : Type} [DecidableEq α] {cache : Cache α} {π : Equiv.Perm α} (hπ : AgreesWith cache π) {x y : α} (hx : List.lookup x cache = none) (hy : invLookup cache y = none) :
    AgreesWith ((x, y) :: cache) (Equiv.trans (Equiv.swap ((Equiv.symm π) y) x) π)

    Pre-composing a permutation agreeing with cache by Equiv.swap (π.symm y) x extends agreement to (x, y) :: cache when x and y are fresh.

    A cache is a partial bijection when at least one permutation on α extends it.

    The empty cache is a partial bijection, witnessed by the identity permutation.

    theorem Cslib.RandomPermutation.IsPartialBijection.cons_of_lookup_eq_none {α : Type} [DecidableEq α] {cache : Cache α} (h : IsPartialBijection cache) {x y : α} (hx : List.lookup x cache = none) (hy : invLookup cache y = none) :

    Extending a partial bijection cache with a fresh domain element x and fresh range element y yields a partial bijection.

    theorem Cslib.RandomPermutation.AgreesWith.pairwise_snd_of_pairwise_fst {α : Type} {cache : Cache α} {π : Equiv.Perm α} (hπ : AgreesWith cache π) (hp1 : List.Pairwise (fun (a b : α × α) => a.1 ≠ b.1) cache) :
    List.Pairwise (fun (a b : α × α) => a.2 ≠ b.2) cache

    If π agrees with cache and the domain entries of cache are pairwise distinct, then the range entries of cache are pairwise distinct.

    Most arguments below treat forward and inverse queries alike: a query either hits cache, or misses it and records the cache entry PermQuery.toCacheEntry ⟨q, a⟩ for its answer a. The recorded answer cachedAnswer and the facts about it below put both directions in this form.

    The recorded answer to q in cache: its image for a forward query, its preimage for an inverse query.

    A recorded answer a to q is stored in cache as the entry PermQuery.toCacheEntry ⟨q, a⟩.

    theorem Cslib.RandomPermutation.AgreesWith.evalPerm_eq_of_cachedAnswer_eq_some {α : Type} [DecidableEq α] {cache : Cache α} {π : Equiv.Perm α} (hπ : AgreesWith cache π) {q : PermQuery α} {a : α} (h : cachedAnswer cache q = some a) :
    evalPerm π q = a

    A permutation that agrees with cache gives every query the answer recorded in cache.

    Bidirectional query deduplication #

    One step of stateful bidirectional query deduplication over a partial-permutation cache cache: answer .forward x from cache.lookup x and .inverse y from invLookup cache y on hits, and otherwise forward the query to the underlying PermOracleSpec α oracle and prepend (x, y) to cache.

    theorem Cslib.RandomPermutation.bidirDedupStep_forward_of_some {α : Type} [DecidableEq α] {x y : α} {cache : Cache α} (h : List.lookup x cache = some y) :

    On a forward cache hit cache.lookup x = some y, bidirDedupStep returns (y, cache) without issuing an oracle query.

    On a forward cache miss cache.lookup x = none, bidirDedupStep queries .forward x and prepends (x, y) to cache.

    theorem Cslib.RandomPermutation.bidirDedupStep_inverse_of_some {α : Type} [DecidableEq α] {y x : α} {cache : Cache α} (h : invLookup cache y = some x) :

    On an inverse cache hit invLookup cache y = some x, bidirDedupStep returns (x, cache) without issuing an oracle query.

    On an inverse cache miss invLookup cache y = none, bidirDedupStep queries .inverse y and prepends (x, y) to cache.

    Deduplicate a bidirectional permutation program starting from the empty cache [], returning both the program output and the final (input, output) cache. Forward queries in dom(cache) and inverse queries in ran(cache) are answered from cache without calling the underlying oracle.

    One step of bidirDedupStep issues at most 1 underlying oracle query.

    theorem Cslib.RandomPermutation.isQueryBounded_run_bidirDedupStep {α : Type} [DecidableEq α] {β : Type} {program : OracleComp (PermOracleSpec α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) (cache : Cache α) :

    Running bidirDedupStep from any initial cache preserves Q-query bounds.

    theorem Cslib.RandomPermutation.isQueryBounded_bidirDedup {α : Type} [DecidableEq α] {β : Type} {program : OracleComp (PermOracleSpec α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) :

    Bidirectional query deduplication bidirDedup preserves Q-query bounds.

    theorem Cslib.RandomPermutation.map_fst_interpret_bidirDedupStep_evalPerm {α : Type} [DecidableEq α] {β : Type} (π : Equiv.Perm α) (program : OracleComp (PermOracleSpec α) β) {cache : Cache α} (hcache : AgreesWith cache π) :

    Evaluating a bidirDedupStep-wrapped program against evalPerm π from any cache agreeing with π produces the exact same output distribution as evaluating the original program against evalPerm π.

    Exact evalPerm equivalence for bidirDedup: deduplicating a bidirectional permutation program does not change its output distribution against evalPerm π.

    theorem Cslib.RandomPermutation.cache_eq_of_mem_support_withTranscript_bidirDedupStep {α : Type} [DecidableEq α] {β σ : Type} (oracle : PermQuery α → StateT σ PMF α) (program : OracleComp (PermOracleSpec α) β) {cache cache' : Cache α} {s s' : σ} {result : β} {transcript : OracleComp.Transcript (PermOracleSpec α)} (hmem : ((result, cache'), s', transcript) ∈ ((OracleComp.interpret (OracleComp.withTranscript oracle) ((OracleComp.interpret bidirDedupStep program).run cache)).run (s, [])).support) :
    cache' = List.map PermQuery.toCacheEntry transcript ++ cache

    Against any stateful handler oracle, the final deduplication cache after running bidirDedupStep from cache equals transcript.map PermQuery.toCacheEntry ++ cache.

    theorem Cslib.RandomPermutation.cache_eq_of_mem_support_withTranscript_bidirDedup {α : Type} [DecidableEq α] {β σ : Type} (oracle : PermQuery α → StateT σ PMF α) (program : OracleComp (PermOracleSpec α) β) {cache' : Cache α} {s s' : σ} {result : β} {transcript : OracleComp.Transcript (PermOracleSpec α)} (hmem : ((result, cache'), s', transcript) ∈ ((OracleComp.interpret (OracleComp.withTranscript oracle) (bidirDedup program)).run (s, [])).support) :

    Against any stateful handler oracle, the final deduplication cache produced by bidirDedup equals transcript.map PermQuery.toCacheEntry.

    theorem Cslib.RandomPermutation.nodup_transcript_of_mem_support_withTranscript_bidirDedupStep {α : Type} [DecidableEq α] {β σ : Type} (oracle : PermQuery α → StateT σ PMF α) (program : OracleComp (PermOracleSpec α) β) {cache cache' : Cache α} {s s' : σ} {result : β} {transcript : OracleComp.Transcript (PermOracleSpec α)} (hmem : ((result, cache'), s', transcript) ∈ ((OracleComp.interpret (OracleComp.withTranscript oracle) ((OracleComp.interpret bidirDedupStep program).run cache)).run (s, [])).support) :
    (List.map Sigma.fst transcript).Nodup

    Against any stateful handler oracle, bidirDedupStep never forwards the same underlying PermQuery α twice.

    theorem Cslib.RandomPermutation.nodup_transcript_of_mem_support_withTranscript_bidirDedup {α : Type} [DecidableEq α] {β σ : Type} (oracle : PermQuery α → StateT σ PMF α) (program : OracleComp (PermOracleSpec α) β) {cache' : Cache α} {s s' : σ} {result : β} {transcript : OracleComp.Transcript (PermOracleSpec α)} (hmem : ((result, cache'), s', transcript) ∈ ((OracleComp.interpret (OracleComp.withTranscript oracle) (bidirDedup program)).run (s, [])).support) :
    (List.map Sigma.fst transcript).Nodup

    Against any stateful handler oracle, bidirDedup never forwards the same underlying PermQuery α twice.

    theorem Cslib.RandomPermutation.length_cache_le_of_mem_support_run_bidirDedupStep {α : Type} [DecidableEq α] {β σ : Type} {program : OracleComp (PermOracleSpec α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) (oracle : PermQuery α → StateT σ PMF α) {cache cache' : Cache α} {s s' : σ} {result : β} (hmem : ((result, cache'), s') ∈ ((OracleComp.interpret oracle ((OracleComp.interpret bidirDedupStep program).run cache)).run s).support) :
    List.length cache' ≤ List.length cache + Q

    Against any stateful handler oracle, running bidirDedupStep on a Q-query-bounded program from cache adds at most Q entries to cache.

    theorem Cslib.RandomPermutation.length_cache_le_of_mem_support_bidirDedup {α : Type} [DecidableEq α] {β σ : Type} {program : OracleComp (PermOracleSpec α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) (oracle : PermQuery α → StateT σ PMF α) {cache' : Cache α} {s s' : σ} {result : β} (hmem : ((result, cache'), s') ∈ ((OracleComp.interpret oracle (bidirDedup program)).run s).support) :
    List.length cache' ≤ Q

    Against any stateful handler oracle, running bidirDedup on a Q-query-bounded program produces a cache of length at most Q.

    theorem Cslib.RandomPermutation.agreesWith_and_pairwise_of_mem_support_run_bidirDedupStep {α : Type} [DecidableEq α] {β : Type} (π : Equiv.Perm α) (program : OracleComp (PermOracleSpec α) β) {cache cache' : Cache α} {result : β} (hπ : AgreesWith cache π) (hp1 : List.Pairwise (fun (a b : α × α) => a.1 ≠ b.1) cache) (hmem : (result, cache') ∈ (OracleComp.interpret (fun (q : PermQuery α) => PMF.pure (evalPerm π q)) ((OracleComp.interpret bidirDedupStep program).run cache)).support) :
    AgreesWith cache' π ∧ List.Pairwise (fun (a b : α × α) => a.1 ≠ b.1) cache' ∧ List.Pairwise (fun (a b : α × α) => a.2 ≠ b.2) cache'

    Evaluating bidirDedupStep against evalPerm π from any cache agreeing with π whose domain entries are pairwise distinct preserves AgreesWith and maintains pairwise-distinct domain and range entries.

    theorem Cslib.RandomPermutation.agreesWith_and_pairwise_of_mem_support_bidirDedup {α : Type} [DecidableEq α] {β : Type} (π : Equiv.Perm α) (program : OracleComp (PermOracleSpec α) β) {cache' : Cache α} {result : β} (hmem : (result, cache') ∈ (OracleComp.interpret (fun (q : PermQuery α) => PMF.pure (evalPerm π q)) (bidirDedup program)).support) :
    AgreesWith cache' π ∧ List.Pairwise (fun (a b : α × α) => a.1 ≠ b.1) cache' ∧ List.Pairwise (fun (a b : α × α) => a.2 ≠ b.2) cache'

    Evaluating bidirDedup against evalPerm π produces a cache agreeing with π whose domain and range entries are both pairwise distinct.

    Finite domain/range complements and extension finsets #

    Elements of α not yet present in the domain of cache.

    Elements of α not yet present in the range of cache.

    Permutations π : Equiv.Perm α that agree with every entry in cache.

    @[simp]
    theorem Cslib.RandomPermutation.mem_unusedDomain_iff {α : Type} [DecidableEq α] [Fintype α] {cache : Cache α} {x : α} :

    An element x belongs to unusedDomain cache iff cache.lookup x = none.

    @[simp]
    theorem Cslib.RandomPermutation.mem_unusedRange_iff {α : Type} [DecidableEq α] [Fintype α] {cache : Cache α} {y : α} :
    y ∈ unusedRange cache ↔ invLookup cache y = none

    An element y belongs to unusedRange cache iff invLookup cache y = none.

    The number of unused domain elements is at least |α| - cache.length.

    The number of unused range elements is at least |α| - cache.length.

    @[simp]
    theorem Cslib.RandomPermutation.mem_extPerms_iff {α : Type} [DecidableEq α] [Fintype α] {cache : Cache α} {π : Equiv.Perm α} :
    π ∈ extPerms cache ↔ AgreesWith cache π

    A permutation π belongs to extPerms cache iff AgreesWith cache π.

    @[simp]

    Every permutation extends the empty cache.

    The extension finset extPerms cache is nonempty iff cache is a partial bijection.

    When cache is a partial bijection and x is outside its domain, unusedRange cache is nonempty.

    When cache is a partial bijection and y is outside its range, unusedDomain cache is nonempty.

    The answers still available to a query that misses cache: unusedRange cache for a forward query and unusedDomain cache for an inverse query.

    A query that misses a partial bijection still has a fresh answer available.

    Recording a fresh answer a to a query q that misses cache adds an entry whose input and output are both new to cache.

    Recording a fresh answer to a query that misses a partial bijection keeps it a partial bijection.

    theorem Cslib.RandomPermutation.card_extPerms_eq_card_unusedRange_mul {α : Type} [DecidableEq α] [Fintype α] {cache : Cache α} {x y : α} (hx : List.lookup x cache = none) (hy : y ∈ unusedRange cache) :
    (extPerms cache).card = (unusedRange cache).card * (extPerms ((x, y) :: cache)).card

    Partitioning extPerms cache by the image π x of a fresh input x shows that all fibers over unusedRange cache have equal size.

    theorem Cslib.RandomPermutation.card_extPerms_eq_card_unusedDomain_mul {α : Type} [DecidableEq α] [Fintype α] {cache : Cache α} {x y : α} (hx : x ∈ unusedDomain cache) (hy : invLookup cache y = none) :
    (extPerms cache).card = (unusedDomain cache).card * (extPerms ((x, y) :: cache)).card

    Partitioning extPerms cache by the preimage π.symm y of a fresh output y shows that all fibers over unusedDomain cache have equal size.

    Exact lazy bidirectional permutation sampler and eager/lazy equivalence #

    noncomputable def Cslib.RandomPermutation.answer {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] (q : PermQuery α) :
    StateT (Cache α) PMF α

    Exact without-replacement lazy bidirectional permutation oracle: return cached answers on hits, and sample uniformly from unusedRange cache (for .forward) or unusedDomain cache (for .inverse) on misses.

    noncomputable def Cslib.RandomPermutation.complete {α : Type} [DecidableEq α] [Fintype α] (cache : Cache α) :

    Uniformly sample a full permutation π : Equiv.Perm α extending cache.

    @[simp]

    Completing the empty cache samples a uniformly random permutation on α.

    theorem Cslib.RandomPermutation.answer_of_cachedAnswer_eq_some {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {q : PermQuery α} {cache : Cache α} {a : α} (h : cachedAnswer cache q = some a) :
    answer q cache = PMF.pure (a, cache)

    answer returns a recorded answer and leaves the cache unchanged.

    theorem Cslib.RandomPermutation.answer_of_cachedAnswer_eq_none {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {q : PermQuery α} {cache : Cache α} (h : cachedAnswer cache q = none) (hne : (freshAnswers cache q).Nonempty) :

    On a query that misses the cache, answer samples a fresh answer uniformly and records it.

    theorem Cslib.RandomPermutation.answer_preserve_isPartialBijection {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] (q : PermQuery α) (cache : Cache α) (hcache : IsPartialBijection cache) (a : α) (cache' : Cache α) (hmem : (a, cache') ∈ (answer q cache).support) :

    answer preserves IsPartialBijection on every supported transition.

    theorem Cslib.RandomPermutation.answer_complete {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] (q : PermQuery α) (cache : Cache α) (hcache : IsPartialBijection cache) :
    ((answer q cache).bind fun (x : α × Cache α) => match x with | (a, cache') => PMF.map (fun (x : Equiv.Perm α) => (a, x)) (complete cache')) = PMF.map (fun (π : Equiv.Perm α) => (evalPerm π q, π)) (complete cache)

    Answering one bidirectional query lazily and then completing the partial bijection has the exact same joint distribution on (answer, π) as completing first and evaluating evalPerm π q.

    theorem Cslib.RandomPermutation.run_interpret_answer_complete {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {β : Type} (program : OracleComp (PermOracleSpec α) β) (cache : Cache α) (hcache : IsPartialBijection cache) :
    (do let __x ← (OracleComp.interpret answer program).run cache match __x with | (result, cache') => PMF.map (fun (x : Equiv.Perm α) => (result, x)) (complete cache')) = do let π ← complete cache PMF.map (fun (x : β) => (x, π)) (OracleComp.interpret (fun (q : PermQuery α) => PMF.pure (evalPerm π q)) program)

    Eager/lazy equivalence for bidirectional permutations: running an adaptive program against answer from any partial bijection cache and then completing cache' to a full permutation gives the same joint distribution on (result, π) as completing cache first and answering queries via evalPerm π.

    Uniform random permutation equals lazy bidirectional sampling: sampling a uniform permutation π ← PMF.uniformOfFintype (Equiv.Perm α) and answering bidirectional queries via evalPerm π has the exact same output distribution as running answer from the empty cache [].

    Forgetful lazy permutation sampler and bidirectional switching bound #

    noncomputable def Cslib.RandomPermutation.forgetfulAnswer {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] (q : PermQuery α) :
    StateT (Cache α) PMF α

    Forgetful lazy bidirectional permutation oracle ([BS23], Section 4.7): return the cached image/preimage on a hit, and sample uniformly from all of α (with replacement) on a miss.

    theorem Cslib.RandomPermutation.forgetfulAnswer_forward_of_some {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {x y : α} {cache : Cache α} (h : List.lookup x cache = some y) :

    On a forward cache hit cache.lookup x = some y, forgetfulAnswer returns (y, cache).

    theorem Cslib.RandomPermutation.forgetfulAnswer_forward_of_none {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {x : α} {cache : Cache α} (h : List.lookup x cache = none) :

    On a forward cache miss cache.lookup x = none, forgetfulAnswer samples y uniformly from α and prepends (x, y) to cache.

    theorem Cslib.RandomPermutation.forgetfulAnswer_inverse_of_some {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {y x : α} {cache : Cache α} (h : invLookup cache y = some x) :

    On an inverse cache hit invLookup cache y = some x, forgetfulAnswer returns (x, cache).

    theorem Cslib.RandomPermutation.forgetfulAnswer_inverse_of_none {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {y : α} {cache : Cache α} (h : invLookup cache y = none) :

    On an inverse cache miss invLookup cache y = none, forgetfulAnswer samples x uniformly from α and prepends (x, y) to cache.

    theorem Cslib.RandomPermutation.mem_support_forgetfulAnswer {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {q : PermQuery α} {cache : Cache α} {r : α × Cache α} (h : r ∈ (forgetfulAnswer q cache).support) :
    cachedAnswer cache q = some r.1 ∧ r.2 = cache ∨ cachedAnswer cache q = none ∧ r.2 = PermQuery.toCacheEntry ⟨q, r.1⟩ :: cache

    A supported forgetfulAnswer step either answers from cache and leaves it unchanged, or misses and prepends the entry recording its answer.

    Interpreting a bidirDedupStep-wrapped program against i.i.d. uniform answers from α produces the exact same joint result and cache distribution as forgetfulAnswer.

    Interpreting bidirDedup program against i.i.d. uniform answers from α produces the exact same joint result and cache distribution as (OracleComp.interpret forgetfulAnswer program).run [].

    theorem Cslib.RandomPermutation.run_interpret_forgetfulAnswer_liftForward {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {β : Type} (program : OracleComp (fun (x : α) => α) β) (cache : Cache α) :

    On forward-only programs (liftForward), forgetfulAnswer never queries invLookup, so its joint result and cache distribution is identical to memoize (fun _ => PMF.uniformOfFintype α).

    noncomputable def Cslib.RandomPermutation.forgetfulWithBad {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] (q : PermQuery α) :

    Forgetful lazy handler instrumented with a bad flag that records whether a fresh draw fell outside freshAnswers: a forward draw collided with ran(cache) or an inverse draw collided with dom(cache).

    noncomputable def Cslib.RandomPermutation.exactWithBad {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] (q : PermQuery α) :

    Coupled exact lazy handler with a bad flag: draw a candidate uniformly from α; if it lies in freshAnswers (unusedRange for a forward query, unusedDomain for an inverse one), use it directly, and otherwise set bad := true and resample with answer.

    theorem Cslib.RandomPermutation.forgetfulWithBad_map_fst {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] (q : PermQuery α) (st : Cache α × Bool) :
    PMF.map (fun (x : α × Cache α × Bool) => match x with | (a, st') => (a, st'.1)) (forgetfulWithBad q st) = forgetfulAnswer q st.1
    theorem Cslib.RandomPermutation.mem_support_forgetfulWithBad {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {q : PermQuery α} {c c' : Cache α} {b b' : Bool} {a : α} (h : (a, c', b') ∈ (forgetfulWithBad q (c, b)).support) :

    A supported forgetfulWithBad step either answers from c without modifying (c, b), or misses, prepends the fresh answer entry, and updates b with whether a ∉ freshAnswers c q.

    theorem Cslib.RandomPermutation.mem_support_exactWithBad {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {q : PermQuery α} {c c' : Cache α} {b b' : Bool} {a : α} (h : (a, c', b') ∈ (exactWithBad q (c, b)).support) :

    A supported exactWithBad step either answers from c without modifying (c, b), draws a fresh answer in freshAnswers c q and prepends it while keeping b, or resamples via answer and sets b' = true.

    theorem Cslib.RandomPermutation.exactWithBad_map_fst {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] (q : PermQuery α) (st : Cache α × Bool) (hst : IsPartialBijection st.1) :
    PMF.map (fun (x : α × Cache α × Bool) => match x with | (a, st') => (a, st'.1)) (exactWithBad q st) = answer q st.1

    Quadratic union bound for linearly growing collision sets #

    theorem Cslib.RandomPermutation.toOuterMeasure_bad_forgetfulWithBad_le {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {β : Type} {program : OracleComp (PermOracleSpec α) β} {n : ℕ} (hn : program.IsQueryBounded n) (cache : Cache α) :
    ((OracleComp.interpret forgetfulWithBad program).run (cache, false)).toOuterMeasure {(fst, fst_1, bad) : β × Cache α × Bool | bad = true} ≤ ↑(n * List.length cache + n.choose 2) * (↑(Fintype.card α))⁻¹

    Over an n-query-bounded program starting from (cache, false), forgetfulWithBad reaches bad = true with probability at most (n * cache.length + n.choose 2) * |α|⁻¹.

    theorem Cslib.RandomPermutation.uniformPerm_bind_apply_le_forgetful {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {β γ : Type} {program : OracleComp (PermOracleSpec α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) (next : β → PMF γ) (c : γ) :
    ((do let π ← Probability.PMF.uniformOfFintype (Equiv.Perm α) OracleComp.interpret (fun (q : PermQuery α) => PMF.pure (evalPerm π q)) program).bind next) c ≤ ((PMF.map Prod.fst ((OracleComp.interpret forgetfulAnswer program).run [])).bind next) c + ↑(Q.choose 2) * (↑(Fintype.card α))⁻¹

    Bidirectional random-permutation switching inequality (ℝ≥0∞, one-sided): for any Q-query adaptive program, evaluating against a uniform permutation accepts within Q.choose 2 * |α|⁻¹ of evaluating against forgetfulAnswer.

    theorem Cslib.RandomPermutation.forgetfulAnswer_bind_apply_le_uniformPerm {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {β γ : Type} {program : OracleComp (PermOracleSpec α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) (next : β → PMF γ) (c : γ) :
    ((PMF.map Prod.fst ((OracleComp.interpret forgetfulAnswer program).run [])).bind next) c ≤ ((do let π ← Probability.PMF.uniformOfFintype (Equiv.Perm α) OracleComp.interpret (fun (q : PermQuery α) => PMF.pure (evalPerm π q)) program).bind next) c + ↑(Q.choose 2) * (↑(Fintype.card α))⁻¹

    Reverse bidirectional random-permutation switching inequality (ℝ≥0∞, one-sided): for any Q-query adaptive program, evaluating against forgetfulAnswer accepts within Q.choose 2 * |α|⁻¹ of evaluating against a uniform permutation.

    theorem Cslib.RandomPermutation.toOuterMeasure_forgetfulAnswer_le_pairwise_and_length_le_add {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {β : Type} {program : OracleComp (PermOracleSpec α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) (P : Cache α → Prop) :
    ((OracleComp.interpret forgetfulAnswer program).run []).toOuterMeasure {x : β × Cache α | P x.2} ≤ ((OracleComp.interpret forgetfulAnswer program).run []).toOuterMeasure {x : β × Cache α | (List.Pairwise (fun (a b : α × α) => a.1 ≠ b.1) x.2 ∧ List.Pairwise (fun (a b : α × α) => a.2 ≠ b.2) x.2 ∧ List.length x.2 ≤ Q) ∧ P x.2} + ↑(Q.choose 2) * (↑(Fintype.card α))⁻¹

    Any final-cache event P under forgetfulAnswer on a Q-query-bounded program has probability at most that of (c.Pairwise (fun a b => a.1 ≠ b.1) ∧ c.Pairwise (fun a b => a.2 ≠ b.2) ∧ c.length ≤ Q) ∧ P c plus the Q.choose 2 * |α|⁻¹ collision probability of forgetfulWithBad.

    theorem Cslib.RandomPermutation.abs_sub_uniformPerm_forgetful_le {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {β γ : Type} {program : OracleComp (PermOracleSpec α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) (next : β → PMF γ) (c : γ) :
    |(((do let π ← Probability.PMF.uniformOfFintype (Equiv.Perm α) OracleComp.interpret (fun (q : PermQuery α) => PMF.pure (evalPerm π q)) program).bind next) c).toReal - (((PMF.map Prod.fst ((OracleComp.interpret forgetfulAnswer program).run [])).bind next) c).toReal| ≤ ↑(Q.choose 2) * (↑(Fintype.card α))⁻¹

    Bidirectional random-permutation switching bound (ℝ, two-sided, [BS23], Section 4.7): for any Q-query adaptive program, the output distributions under a uniform permutation π ← PMF.uniformOfFintype (Equiv.Perm α) and under the forgetful lazy sampler forgetfulAnswer differ in probability on every event by at most Q.choose 2 / |α|.

    theorem Cslib.RandomPermutation.abs_sub_uniformPerm_uniformFunc_le {α : Type} [DecidableEq α] [Fintype α] [Nonempty α] {β γ : Type} {program : OracleComp (fun (x : α) => α) β} {Q : ℕ} (hQ : program.IsQueryBounded Q) (next : β → PMF γ) (c : γ) :
    |(((do let π ← Probability.PMF.uniformOfFintype (Equiv.Perm α) OracleComp.interpret (fun (x : α) => PMF.pure (π x)) program).bind next) c).toReal - (((do let f ← Probability.PMF.uniformOfFintype (α → α) OracleComp.interpret (fun (x : α) => PMF.pure (f x)) program).bind next) c).toReal| ≤ ↑(Q.choose 2) * (↑(Fintype.card α))⁻¹

    The ideal-experiment PRP/PRF switching lemma (ℝ, two-sided, [BS23], Section 4.4.3, Attack Game 4.3 and Theorem 4.6): for any Q-query forward-only adaptive program program : OracleComp (fun _ : α => α) β, the acceptance probabilities under a uniformly random permutation π ← PMF.uniformOfFintype (Equiv.Perm α) and under a uniformly random function f ← PMF.uniformOfFintype (α → α) differ by at most Q.choose 2 / |α|.

    Swapping π x₀ with an independent uniform element y leaves a uniformly random permutation invariant in distribution.