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 α]:
- 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. UsingOracleComp.run_interpret_simulation_of_invariantand uniform completioncompleteover the setextPerms cacheof permutations extending the partial bijection, we prove that adaptive execution againstanswerfollowed bycompletehas the exact same joint distribution as samplingπ ← PMF.uniformOfFintype (Equiv.Perm α)eagerly (run_interpret_answer_complete,uniform_interpret_eq_lazy). - 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 couplinganswerandforgetfulAnswervia resampling andOracleComp.IdenticalUntilBad, anyQ-query adaptive distinguisher has distinguishing advantage at mostQ.choose 2 / |α|between a uniformly random permutationπ ← PMF.uniformOfFintype (Equiv.Perm α)andforgetfulAnswer(abs_sub_uniformPerm_forgetful_le). On forward-only programs (liftForward),forgetfulAnswercoincides withmemoize (fun _ => PMF.uniformOfFintype α)and therefore with a uniformly random functionf ← 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). - Bidirectional query deduplication (
RandomPermutation.bidirDedupStep,RandomPermutation.bidirDedup): for any[DecidableEq α], wrap a bidirectional oracle program so that forward hits indom(cache)and inverse hits inran(cache)are answered fromcachewithout invoking the underlying oracle, preserving the output distribution againstevalPerm π(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 underevalPerm π(agreesWith_and_pairwise_of_mem_support_bidirDedup).
Main definitions #
Cslib.RandomPermutation.PermQuery,PermQuery.ofBoolProd,PermQuery.equivBoolProd,PermQuery.toCacheEntry,PermOracleSpec,evalPerm,liftForward,liftBoolProd: bidirectional permutation queries, oracle specification, deterministic evaluation, and query liftings.Cslib.RandomPermutation.Cache,invLookup,AgreesWith,IsPartialBijection,extPerms,unusedDomain,unusedRange: partial bijection caches and extension sets.Cslib.RandomPermutation.bidirDedupStep,bidirDedup: stateful bidirectional query deduplication over a partial-permutation cache.Cslib.RandomPermutation.answer,complete,forgetfulAnswer: exact and forgetful lazy bidirectional permutation samplers and uniform completion.
Main results #
Cslib.RandomPermutation.map_fst_interpret_bidirDedupStep_evalPerm,Cslib.RandomPermutation.map_fst_interpret_bidirDedup_evalPerm,Cslib.RandomPermutation.isQueryBounded_bidirDedupStep,Cslib.RandomPermutation.isQueryBounded_bidirDedup,Cslib.RandomPermutation.cache_eq_of_mem_support_withTranscript_bidirDedup,Cslib.RandomPermutation.nodup_transcript_of_mem_support_withTranscript_bidirDedup,Cslib.RandomPermutation.length_cache_le_of_mem_support_bidirDedup,Cslib.RandomPermutation.agreesWith_and_pairwise_of_mem_support_bidirDedup,Cslib.RandomPermutation.interpret_bidirDedup_uniformOfFintype: exactevalPermequivalence, query-bound preservation, transcript-cache provenance and query non-repetition, cache-length and pairwise-uniqueness invariants, andforgetfulAnswercorrespondence for bidirectional query deduplication.Cslib.RandomPermutation.answer_complete,Cslib.RandomPermutation.run_interpret_answer_complete,Cslib.RandomPermutation.uniform_interpret_eq_lazy: exact eager/lazy equivalence for bidirectional random permutations.Cslib.RandomPermutation.card_unusedDomain_ge,Cslib.RandomPermutation.card_unusedRange_ge,Cslib.RandomPermutation.uniformPerm_swap_eq_uniform: cache cardinality lower bounds and invariance of a uniform permutation under left-swap rerandomization.Cslib.RandomPermutation.uniformPerm_bind_apply_le_forgetful,Cslib.RandomPermutation.forgetfulAnswer_bind_apply_le_uniformPerm,Cslib.RandomPermutation.toOuterMeasure_forgetfulAnswer_le_pairwise_and_length_le_add,Cslib.RandomPermutation.abs_sub_uniformPerm_forgetful_le: bidirectional random-permutation switching bounds (Q.choose 2 / |α|).Cslib.RandomPermutation.abs_sub_uniformPerm_uniformFunc_le: the ideal-experiment PRP/PRF switching lemma ([BS23], Section 4.4.3, Attack Game 4.3 and Theorem 4.6).
References #
- D. Boneh, V. Shoup, A Graduate Course in Applied Cryptography, Version 0.6, Section 4.4.3 (Attack Game 4.3, Theorem 4.6, and Theorem 4.7, printed pp. 135–136) and Section 4.7.
Bidirectional permutation query interface #
A bidirectional query to a permutation on α: either evaluate in the forward direction or
evaluate the inverse permutation.
The oracle specification of a bidirectional permutation on α.
Deterministically answer a bidirectional permutation query using π : Equiv.Perm α.
Evaluating evalPerm π on a forward query .forward x applies π to x.
Evaluating evalPerm π on an inverse query .inverse y applies π.symm to y.
Lift a forward-only oracle program into a bidirectional permutation oracle program.
Interpreting liftForward program against evalPerm π is interpreting program against
π.
Lifting a forward-only program preserves query bounds.
Bidirectional queries as channel-tagged points: .forward x is (false, x) and .inverse y is
(true, y). The inverse is PermQuery.ofBoolProd.
Lift a Bool × α bidirectional oracle program into a PermOracleSpec α program.
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).
Partial bijection cache and lookup lemmas #
A partial permutation table stored as a list of input-output pairs (x, y).
Lookup the preimage x of y in cache by searching (y, x) in cache.map Prod.swap.
Inverse lookup in the empty cache returns none.
If cache.lookup x = some y, then the pair (x, y) belongs to cache.
Forward lookup of x in cache returns none iff no entry in cache has first component
x.
Inverse lookup of y in cache returns none iff no entry in cache has second component
y.
A permutation π : Equiv.Perm α agrees with cache if π e.1 = e.2 for all e ∈ cache.
Instances For
Decidability of AgreesWith cache π.
Every permutation agrees with the empty cache.
A permutation π agrees with e :: cache iff π e.1 = e.2 and π agrees with cache.
If π agrees with cache and cache.lookup x = some y, then π x = y.
If π agrees with cache and invLookup cache y = some x, then π.symm y = x.
If π agrees with cache and x is not in the domain of cache, then π x is not in the
range of cache.
If π agrees with cache and y is not in the range of cache, then π.symm y is not in
the domain of cache.
Post-composing a permutation agreeing with cache by Equiv.swap (π x) y extends agreement
to (x, y) :: cache when x and y are fresh.
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.
Extending a partial bijection cache with a fresh domain element x and fresh range element
y yields a partial bijection.
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⟩.
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.
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.
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.
Running bidirDedupStep from any initial cache preserves Q-query bounds.
Bidirectional query deduplication bidirDedup preserves Q-query bounds.
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 π.
Against any stateful handler oracle, the final deduplication cache after running
bidirDedupStep from cache equals transcript.map PermQuery.toCacheEntry ++ cache.
Against any stateful handler oracle, the final deduplication cache produced by bidirDedup
equals transcript.map PermQuery.toCacheEntry.
Against any stateful handler oracle, bidirDedupStep never forwards the same underlying
PermQuery α twice.
Against any stateful handler oracle, bidirDedup never forwards the same underlying
PermQuery α twice.
Against any stateful handler oracle, running bidirDedupStep on a Q-query-bounded program
from cache adds at most Q entries to cache.
Against any stateful handler oracle, running bidirDedup on a Q-query-bounded program
produces a cache of length at most Q.
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.
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.
An element x belongs to unusedDomain cache iff cache.lookup x = 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.
A permutation π belongs to extPerms cache iff AgreesWith cache π.
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.
Partitioning extPerms cache by the image π x of a fresh input x shows that all fibers over
unusedRange cache have equal size.
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 #
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.
Uniformly sample a full permutation π : Equiv.Perm α extending cache.
Completing the empty cache samples a uniformly random permutation on α.
answer returns a recorded answer and leaves the cache unchanged.
On a query that misses the cache, answer samples a fresh answer uniformly and records it.
answer preserves IsPartialBijection on every supported transition.
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.
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 #
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.
On a forward cache hit cache.lookup x = some y, forgetfulAnswer returns (y, cache).
On a forward cache miss cache.lookup x = none, forgetfulAnswer samples y uniformly from
α and prepends (x, y) to cache.
On an inverse cache hit invLookup cache y = some x, forgetfulAnswer returns (x, cache).
On an inverse cache miss invLookup cache y = none, forgetfulAnswer samples x uniformly
from α and prepends (x, y) to 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 [].
On forward-only programs (liftForward), forgetfulAnswer never queries invLookup, so its
joint result and cache distribution is identical to memoize (fun _ => PMF.uniformOfFintype α).
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).
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.
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.
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.
exactWithBad and forgetfulWithBad are identical until bad = true.
Quadratic union bound for linearly growing collision sets #
Over an n-query-bounded program starting from (cache, false), forgetfulWithBad reaches
bad = true with probability at most (n * cache.length + n.choose 2) * |α|⁻¹.
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.
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.
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.
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 / |α|.
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.