Repository navigation
Expand file tree
/
Copy pathCounting.lean
More file actions
1201 lines (1119 loc) · 59.3 KB
/
Copy pathCounting.lean
File metadata and controls
1201 lines (1119 loc) · 59.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module
public import Probability.Distribution
public import Mathlib.Data.Fintype.Perm
public import Mathlib.Data.Fintype.CardEmbedding
public import Mathlib.Data.Nat.Factorial.BigOperators
public import Mathlib.GroupTheory.GroupAction.MultipleTransitivity
public import Mathlib.Algebra.Order.Chebyshev
public import Mathlib.Analysis.Complex.ExponentialBounds
/-!
# Fibers of uniform random functions
The uniform law on function tables `F : X → Y`, for finite `X` and finite nonempty `Y`, and the
counting facts about it used by transcript laws and collision bounds. The values of `F` at
distinct inputs are independent and uniform on `Y`; a repeated input reads the same value.
## Main definitions
* `walkSite par g x₀ y t = g t x₀` if `par t = none`, and `g t (y p)` if `par t = some p`
* `multiShift u δ f x = f x + ∑ s, (if u s = x then δ s else 0)`
## Main results
* `card_function_fiber_finset`, `card_function_fiber_multipoint`,
`card_function_fiber_index`: `#{F | F = g on S} = |Y|^(|X| - |S|)`
* `fTransform_eval_uniform_eq_uniform`, `uniform_mass_eval_eq`: for injective `xs : ι → X`,
`Law(i ↦ F (xs i)) = 𝒰(ι → Y)`, so `Pr[F ∘ xs = ys] = (1/|Y|)^|ι|`
* `uniform_mass_transcript_eq`: a transcript `h` answered by a function has
`Pr[F x = y for all (x, y) ∈ h] = (1/|Y|)^d`, `d` the number of distinct inputs of `h`
* `fTransform_eval_uniform_eq_of_same_fibers`: if `left i = left j ↔ right i = right j`, then
`Law(i ↦ F (left i)) = Law(i ↦ G (right i))` for uniform `F : X → Y`, `G : Z → Y`
* `uniform_mass_adaptive_eval_eq_mul`: a batch chosen from revealed base values, fresh on a
good base event, contributes `(1/|Y|)^|I|` to the joint mass with that event
* `uniform_mass_fresh_eval_eq`, `uniform_mass_not_injOn_le_choose`: fresh-coordinate
independence and an unordered-pair birthday bound
* `uniform_mass_fresh_projected_eval_eq`, `uniform_mass_transcript_projected_collision_le`:
bounds for projected replies while the full table remains sampled
* `uniform_mass_disjoint_events`, `uniform_mass_fresh_outputs_eq`: factorization of
events on disjoint sets of table coordinates
* `uniform_mass_walk_eval_eq`, `uniform_mass_coord_determined_le`,
`uniform_mass_walk_repeat_le`: lazy sampling along a walk
* `card_filter_shift`, `card_filter_shift_univ`: re-randomization fibers
Source: the uniform random function of MauRen16, printed p. 6, "a uniformly chosen function
table F"; first-access sampling with retained answers is Jost, Figure 4.4, printed p. 55.
-/
@[expose] public section
noncomputable section
open scoped BigOperators NNReal
namespace Probability
namespace Counting
/-! ## Function fibers -/
/-- The number of functions agreeing with a prescribed map on a finite input
subset.
This is the shared finite-set function-fiber count used by transcript
normalization arguments. -/
lemma card_function_fiber_finset {X Y : Type*} [Fintype X] [DecidableEq X]
[Fintype Y] [DecidableEq Y] (S : Finset X) (g : S → Y) :
(Finset.univ.filter (fun f : X → Y => ∀ x : S, f x.1 = g x)).card =
Fintype.card Y ^ (Fintype.card X - S.card) := by
classical
rw [show Fintype.card Y ^ (Fintype.card X - S.card) = Fintype.card (↥Sᶜ → Y) from by
rw [Fintype.card_fun, Fintype.card_coe, Finset.card_compl]]
refine Finset.card_bij (fun f _ => fun ⟨x, hx⟩ => f x) (fun _ _ => Finset.mem_univ _) ?_ ?_
· intro f₁ hf₁ f₂ hf₂ h
simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hf₁ hf₂
ext x
by_cases hx : x ∈ S
· rw [hf₁ ⟨x, hx⟩, hf₂ ⟨x, hx⟩]
· exact congr_fun h ⟨x, Finset.mem_compl.mpr hx⟩
· intro h _
refine Exists.intro
(fun x => if hx : x ∈ S then g ⟨x, hx⟩ else h ⟨x, Finset.mem_compl.mpr hx⟩) ?_
refine Exists.intro ?_ ?_
· rw [Finset.mem_filter]
constructor
· exact Finset.mem_univ _
· intro x
simp [x.2]
· ext x
simp [Finset.mem_compl.mp x.2]
/-- The number of functions matching a prescribed output tuple on an injective
input tuple is `|Y|^(|X|-q)`. -/
lemma card_function_fiber_multipoint {X Y : Type*}
[Fintype X] [DecidableEq X] [Fintype Y] [DecidableEq Y]
{q : ℕ} (xs : Fin q → X) (ys : Fin q → Y)
(hxs : Function.Injective xs) :
((Finset.univ : Finset (X → Y)).filter
(fun f : X → Y => (fun i : Fin q => f (xs i)) = ys)).card =
Fintype.card Y ^ (Fintype.card X - q) := by
classical
set S : Finset X := Finset.univ.image xs
have hS_card : S.card = q := by
rw [Finset.card_image_of_injective _ hxs, Finset.card_fin]
let C : Finset X := Finset.univ \ S
rw [show Fintype.card Y ^ (Fintype.card X - q) = Fintype.card (C → Y) from by
have hC_card : C.card = Fintype.card X - S.card := by
exact Finset.card_sdiff_of_subset (by intro x _; exact Finset.mem_univ x)
rw [Fintype.card_fun, Fintype.card_coe, hC_card, hS_card]]
refine Finset.card_bij (fun f _ => fun ⟨x, hx⟩ => f x)
(fun _ _ => Finset.mem_univ _) ?_ ?_
· intro f₁ hf₁ f₂ hf₂ h
simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hf₁ hf₂
ext x
by_cases hx : x ∈ S
· obtain ⟨i, _, rfl⟩ := Finset.mem_image.mp hx
rw [congr_fun hf₁ i, congr_fun hf₂ i]
· exact congr_fun h ⟨x, by simp [C, hx]⟩
· intro g _
have h_ext : ∀ x ∈ S, ∃! i : Fin q, xs i = x := by
intro x hx
obtain ⟨i, _, rfl⟩ := Finset.mem_image.mp hx
exact ⟨i, rfl, fun j hj => hxs hj⟩
refine ⟨fun x =>
if hx : x ∈ S then ys ((h_ext x hx).choose)
else g ⟨x, by simp [C, hx]⟩, ?_, ?_⟩
· rw [Finset.mem_filter]
refine ⟨Finset.mem_univ _, ?_⟩
ext i
have h_mem : xs i ∈ S := Finset.mem_image_of_mem _ (Finset.mem_univ i)
show dite (xs i ∈ S) _ _ = ys i
rw [dite_eq_left h_mem]
have hcs := (h_ext (xs i) h_mem).choose_spec
congr 1
exact hxs hcs.1
· ext ⟨x, hx⟩
show dite (x ∈ S) _ _ = g ⟨x, hx⟩
rw [dite_eq_right ((Finset.mem_sdiff.mp hx).2)]
/-! ## The uniform random function: marginals, joint marginals, and freshness -/
/-- **The fiber of a uniform random function over a
prescribed value tuple at distinct points**, at an arbitrary finite index type:
`card_function_fiber_multipoint` transported along `Fintype.equivFin`. -/
lemma card_function_fiber_index {X Y ι : Type*} [Fintype X] [DecidableEq X]
[Fintype Y] [DecidableEq Y] [Fintype ι] (xs : ι → X) (ys : ι → Y)
(hxs : Function.Injective xs) :
((Finset.univ : Finset (X → Y)).filter
(fun f : X → Y => (fun i => f (xs i)) = ys)).card
= Fintype.card Y ^ (Fintype.card X - Fintype.card ι) := by
classical
set e := Fintype.equivFin ι with he
have hxs' : Function.Injective (fun k => xs (e.symm k)) :=
hxs.comp e.symm.injective
have hpred : ∀ f : X → Y,
((fun i => f (xs i)) = ys)
↔ ((fun k => f (xs (e.symm k))) = fun k => ys (e.symm k)) := by
intro f
constructor
· intro h; funext k; exact congrFun h (e.symm k)
· intro h; funext i
have := congrFun h (e i)
simpa using this
rw [Finset.filter_congr (fun f _ => by rw [hpred f]),
card_function_fiber_multipoint (fun k => xs (e.symm k))
(fun k => ys (e.symm k)) hxs']
/-- **Joint marginals of a uniform random function at
distinct points are uniform.** Pushing the uniform law on `𝒳 → 𝒴` forward
along evaluation at an injective family of points is the uniform law on value
tuples: this is independence of the values at distinct points, in the form this
carrier can state it. -/
lemma fTransform_eval_uniform_eq_uniform {X Y ι : Type*} [Fintype X] [DecidableEq X]
[Fintype Y] [DecidableEq Y] [Nonempty Y] [Fintype ι] [DecidableEq ι]
(xs : ι → X) (hxs : Function.Injective xs) :
Distribution.fTransform (fun f : X → Y => fun i => f (xs i))
(Distribution.uniform (X → Y))
= Distribution.uniform (ι → Y) := by
classical
refine Distribution.fTransform_uniform_eq_uniform_of_card_fiber_mul _ fun ys => ?_
have hcard : Fintype.card ι ≤ Fintype.card X := Fintype.card_le_of_injective xs hxs
rw [card_function_fiber_index xs ys hxs, Fintype.card_fun, Fintype.card_fun,
← pow_add]
congr 1
omega
/-- **The mass of a prescribed value tuple at distinct
points**: `(1/|𝒴|)^{|ι|}`, whatever the tuple. The mass reading of
`fTransform_eval_uniform_eq_uniform`, and the form a transcript-law computation
consumes. -/
lemma uniform_mass_eval_eq {X Y ι : Type*} [Fintype X] [DecidableEq X]
[Fintype Y] [DecidableEq Y] [Nonempty Y] [Fintype ι] [DecidableEq ι]
(xs : ι → X) (hxs : Function.Injective xs) (ys : ι → Y) :
(Distribution.uniform (X → Y)).mass (fun f => (fun i => f (xs i)) = ys)
= (1 / (Fintype.card Y : ℝ)) ^ Fintype.card ι := by
have h := congrArg (fun d : Distribution (ι → Y) => d ys)
(fTransform_eval_uniform_eq_uniform (X := X) (Y := Y) xs hxs)
simp only [Distribution.fTransform_apply_eq_mass, Distribution.uniform_apply] at h
rw [h, Fintype.card_fun, div_pow, one_pow]
push_cast
ring
/-- **A fresh coordinate factors.** Let `F` be uniform on `X → Y`, `x : X`, `y : Y`, and let
`E = event` ignore the value at `x` (`f = g` off `x` gives `E f ↔ E g`). Then
Pr[E(F) ∧ F x = y] = Pr[E(F)] / |Y| .
Splitting the table at `x` (`Equiv.funSplitAt`) gives two independent uniform parts:
```text
F ↦ ( F x , F restricted to {z // z ≠ x} )
"= y" E reads only this part
```
The table model is MauRen16, printed p. 6: "a uniformly chosen function table F". This law is
derived from it for finite tables, not a stated paper theorem. -/
lemma uniform_mass_fresh_eval_eq {X Y : Type*} [Fintype X] [DecidableEq X]
[Fintype Y] [Nonempty Y] (x : X) (y : Y) (event : (X → Y) → Prop)
(hfresh : ∀ f g, (∀ z, z ≠ x → f z = g z) → (event f ↔ event g)) :
(Distribution.uniform (X → Y)).mass (fun f => event f ∧ f x = y) =
(Distribution.uniform (X → Y)).mass event / Fintype.card Y := by
classical
-- `split f = (f x, f off x)`.
let split := Equiv.funSplitAt x Y
-- `rest g`: `E` on the table equal to `g` off `x`, with an arbitrary value at `x`.
let rest : ({z : X // z ≠ x} → Y) → Prop := fun g =>
event (split.symm (Classical.choice ‹Nonempty Y›, g))
-- `E f ↔ rest (f off x)`: the rebuilt table agrees with `f` off `x`.
have hrest (f : X → Y) : event f ↔ rest (split f).2 := by
apply hfresh
intro z hz
dsimp only [split]
rw [Equiv.funSplitAt_symm_apply, dite_eq_right hz]
rfl
-- `Law(split F) = 𝒰(Y) × 𝒰({z // z ≠ x} → Y)`: a bijection keeps `𝒰`, which is a product.
have hpush := Distribution.fTransform_equiv_uniform split
rw [← Distribution.prod_uniform] at hpush
-- `Pr[F x = y ∧ rest (F off x)] = Pr_𝒰(Y)[= y] · Pr_𝒰[rest]` (rectangle event).
have hm := congrArg (fun P : Distribution (Y × ({z : X // z ≠ x} → Y)) =>
P.mass (fun t => t.1 = y ∧ rest t.2)) hpush
rw [Distribution.mass_fTransform] at hm
rw [Distribution.mass_prod_and (Distribution.uniform Y)
(Distribution.uniform ({z : X // z ≠ x} → Y)) (fun a => a = y) rest] at hm
-- `Pr[E(F)] = Pr_𝒰[rest]`: second marginal, the first factor has weight 1.
have he : (Distribution.uniform (X → Y)).mass event =
(Distribution.uniform ({z : X // z ≠ x} → Y)).mass rest := by
calc
_ = (Distribution.fTransform split (Distribution.uniform (X → Y))).mass
(fun t => rest t.2) := by
rw [Distribution.mass_fTransform]
exact Distribution.mass_congr _ hrest
_ = _ := by rw [hpush, Distribution.mass_prod_snd, Distribution.weight_uniform, one_mul]
-- `Pr_𝒰(Y)[= y] = 1/|Y|`.
rw [Distribution.mass_singleton, Distribution.uniform_apply] at hm
rw [he]
-- Rewrite `E(F) ∧ F x = y` as the rectangle event, then `(1/|Y|) · Pr[rest]`.
calc
_ = (Distribution.uniform (X → Y)).mass
(fun f => (split f).1 = y ∧ rest (split f).2) := by
apply Distribution.mass_congr
intro f
exact ⟨fun h => ⟨h.2, (hrest f).mp h.1⟩,
fun h => ⟨(hrest f).mpr h.2, h.1⟩⟩
_ = _ := hm.trans (by ring)
/-- **Predicting one coordinate from another.** For `F` uniform on `X → Y`, `x ≠ z` and any
`predict : Y → Y`,
Pr[F z = predict (F x)] = 1/|Y| .
Given `F x = y`, the coordinate `z` is still fresh: `Σ_y Pr[F x = y] · 1/|Y| = 1/|Y|`.
Derived from the table model of MauRen16, printed p. 6: "a uniformly chosen function table F".
-/
lemma uniform_two_outputs_graph_mass {X Y : Type*} [Fintype X] [DecidableEq X]
[Fintype Y] [Nonempty Y] (x z : X) (hxz : x ≠ z) (predict : Y → Y) :
(Distribution.uniform (X → Y)).mass (fun f => f z = predict (f x)) =
1 / (Fintype.card Y : ℝ) := by
classical
let P := Distribution.uniform (X → Y)
-- Partition by the first reply: `Σ_y Pr[F z = predict (F x) ∧ F x = y]`.
rw [Distribution.mass_eq_sum_mass_fiber P _ (fun f => f x) Finset.univ
(fun _ _ => Finset.mem_univ _)]
-- Each summand is `Pr[F x = y] / |Y|`.
have he (y : Y) : P.mass (fun f => f z = predict (f x) ∧ f x = y) =
P.mass (fun f => f x = y) / Fintype.card Y := by
calc
-- On `F x = y` the target is the fixed value `predict y`.
_ = P.mass (fun f => f x = y ∧ f z = predict y) := by
apply Distribution.mass_congr
intro f
constructor
· rintro ⟨ha, hb⟩
exact ⟨hb, hb ▸ ha⟩
· rintro ⟨ha, hb⟩
exact ⟨ha ▸ hb, ha⟩
-- `F x = y` ignores the fresh coordinate `z`, since `x ≠ z`.
_ = _ := uniform_mass_fresh_eval_eq z (predict y) _
(fun f g hfg => by rw [hfg x hxz])
-- `Σ_y Pr[F x = y] / |Y| = (Σ_y Pr[F x = y]) · |Y|⁻¹`.
simp only [he, div_eq_mul_inv]
rw [← Finset.sum_mul]
-- The fibers of `F x` cover the whole weight `1`.
have hs : ∑ y : Y, P.mass (fun f => f x = y) = 1 := by
have hh := Distribution.mass_eq_sum_mass_fiber P (fun _ => True)
(fun f => f x) Finset.univ (fun _ _ => Finset.mem_univ _)
simpa only [true_and, Distribution.mass_true, P, Distribution.weight_uniform] using hh.symm
rw [hs]
/-- **Two-coordinate collision.** For `F` uniform on `X → Y` and `x ≠ z`,
Pr[F x = F z] = 1/|Y| .
The case `predict = id` of `uniform_two_outputs_graph_mass`, derived from the table model of
MauRen16, printed p. 6. -/
lemma uniform_two_outputs_collision_mass {X Y : Type*} [Fintype X] [DecidableEq X]
[Fintype Y] [Nonempty Y] (x z : X) (hxz : x ≠ z) :
(Distribution.uniform (X → Y)).mass (fun f => f x = f z) =
1 / (Fintype.card Y : ℝ) := by
-- `F x = id (F z)`, predicting `x` from `z`.
exact uniform_two_outputs_graph_mass z x hxz.symm id
/-- **The birthday bound on a fixed set.** For `F` uniform on `X → Y` and a finite `s ⊆ X`,
Pr[F not injective on s] ≤ C(|s|, 2) / |Y| .
Each unordered pair of distinct points of `s` collides with mass `1/|Y|`
(`uniform_two_outputs_collision_mass`), summed by `Distribution.mass_exists_pair_le_choose`.
A law of the finite table model of MauRen16, printed p. 6, not an assumption on generic
systems. CR18, printed p. 51, motivates collision finding for hash functions by "the so-called
birthday paradox"; this finite union bound is not a statement of CR18. -/
lemma uniform_mass_not_injOn_le_choose {X Y : Type*} [Fintype X] [DecidableEq X]
[Fintype Y] [Nonempty Y] (s : Finset X) :
(Distribution.uniform (X → Y)).mass (fun f => ¬ Set.InjOn f (s : Set X)) ≤
(s.card.choose 2 : ℝ) / Fintype.card Y := by
-- Pair union bound, `bad x z = (F x = F z)`, `δ = 1/|Y|`:
-- Pr[∃ x, z ∈ s, x ≠ z ∧ F x = F z] ≤ C(|s|, 2) · (1/|Y|) = C(|s|, 2) / |Y|.
have hcount := Distribution.mass_exists_pair_le_choose
(Distribution.uniform (X → Y)) Distribution.uniform_nonNeg s
(fun x z f => f x = f z) (fun _ _ _ he => he.symm) _
(fun x _ z _ hxz => (uniform_two_outputs_collision_mass x z hxz).le)
rw [mul_one_div] at hcount
-- Every failure of injectivity on `s` is a collision of two distinct points of `s`.
refine (Distribution.mass_mono Distribution.uniform_nonNeg ?_).trans hcount
intro f hf
unfold Set.InjOn at hf
push Not at hf
obtain ⟨x, hx, z, hz, he, hn⟩ := hf
exact ⟨x, hx, z, hz, hn, he⟩
/-- **A projected fresh reply is uniform.** For a uniform table `f : X → Y`, a map
`project : Y → Z` sending the uniform law on `Y` to the uniform law on `Z`, and an `event` that
ignores the coordinate `x`,
`Pr[event f ∧ project (f x) = z] = Pr[event f] / |Z|`.
The table keeps its full replies; only the new test reads `project (f x)`. Model: MauRen16,
printed p. 6, “a uniformly chosen function table F”. -/
lemma uniform_mass_fresh_projected_eval_eq {X Y Z : Type*} [Fintype X] [DecidableEq X]
[Fintype Y] [Nonempty Y] [Fintype Z] [Nonempty Z]
(project : Y → Z)
(hproject : Distribution.fTransform project (Distribution.uniform Y) =
Distribution.uniform Z)
(x : X) (z : Z) (event : (X → Y) → Prop)
(hfresh : ∀ f g : X → Y, (∀ a, a ≠ x → f a = g a) → (event f ↔ event g)) :
(Distribution.uniform (X → Y)).mass (fun f => event f ∧ project (f x) = z) =
(Distribution.uniform (X → Y)).mass event / Fintype.card Z := by
classical
let P := Distribution.uniform (X → Y)
-- Partition by the full reply `y = f x` at the fresh coordinate.
rw [Distribution.mass_eq_sum_mass_fiber P _ (fun f => f x) Finset.univ
(fun _ _ => Finset.mem_univ _)]
-- Fiber `y`: `Pr[event ∧ f x = y] = Pr[event] / |Y|` if `project y = z`, empty otherwise.
have hm (y : Y) : P.mass (fun f => (event f ∧ project (f x) = z) ∧ f x = y) =
(if project y = z then P.mass event / Fintype.card Y else 0) := by
split_ifs with hy
· calc
_ = P.mass (fun f => event f ∧ f x = y) := by
apply Distribution.mass_congr
intro f
exact ⟨fun h => ⟨h.1.1, h.2⟩, fun h => ⟨⟨h.1, h.2 ▸ hy⟩, h.2⟩⟩
_ = _ := uniform_mass_fresh_eval_eq x y event hfresh
· exact Distribution.mass_eq_zero_of_forall_not _ (fun f h => hy (h.2 ▸ h.1.2))
simp only [hm]
-- By `hproject`, `Pr_{y ← U(Y)}[project y = z] = U(Z){z} = 1 / |Z|`.
have he : (Distribution.uniform Y).mass (fun y => project y = z) =
1 / (Fintype.card Z : ℝ) := by
rw [← Distribution.mass_fTransform project (Distribution.uniform Y) (fun t => t = z),
hproject, Distribution.mass_singleton, Distribution.uniform_apply]
-- `∑ y, [project y = z] · Pr[event] / |Y| = Pr[event] · Pr_{y ← U(Y)}[project y = z]`.
have hs : (∑ y : Y, if project y = z then P.mass event / Fintype.card Y else 0) =
P.mass event * (Distribution.uniform Y).mass (fun y => project y = z) := by
rw [Distribution.mass_eq_sum_of_support_subset (Distribution.uniform Y)
(Finset.subset_univ _), Finset.sum_filter, Finset.mul_sum]
apply Finset.sum_congr rfl
intro y _
rw [Distribution.uniform_apply]
split_ifs <;> ring
rw [hs, he]
ring
/-- **Conditional projected collision bound.** Let `h` be a history of inputs and full replies
whose projected replies differ on distinct inputs (`hgood`), and write `f ⊨ h` for
`∀ (a, y) ∈ h, f a = y`. For a uniform table `f : X → Y`,
`Pr[f ⊨ h ∧ ∃ (a, y) ∈ h, a ≠ x ∧ project (f x) = project y] ≤ |h| / |Z| · Pr[f ⊨ h]`.
A fresh `x` costs a union bound; a repeated `x` has no collision under `hgood`. `h` need not be
consistent: an impossible history has mass zero. Model: MauRen16, printed p. 6, “a uniformly
chosen function table F”. -/
lemma uniform_mass_transcript_projected_collision_le {X Y Z : Type*}
[Fintype X] [DecidableEq X] [Fintype Y] [Nonempty Y] [Fintype Z] [Nonempty Z]
(project : Y → Z)
(hproject : Distribution.fTransform project (Distribution.uniform Y) =
Distribution.uniform Z) (h : List (X × Y)) (x : X)
(hgood : ∀ a ∈ h, ∀ b ∈ h, project a.2 = project b.2 → a.1 = b.1) :
(Distribution.uniform (X → Y)).mass (fun f =>
(∀ z ∈ h, f z.1 = z.2) ∧ ∃ z ∈ h, z.1 ≠ x ∧ project (f x) = project z.2) ≤
(h.length : ℝ) / Fintype.card Z *
(Distribution.uniform (X → Y)).mass (fun f => ∀ z ∈ h, f z.1 = z.2) := by
classical
-- Either `x` is fresh (no input of `h`) or it repeats an input of `h`.
by_cases hfresh : ∀ z ∈ h, z.1 ≠ x
· -- Fresh `x`: union bound over the old pairs `(a, y)` of `h`.
have hm := Distribution.mass_exists_le (Distribution.uniform_nonNeg (A := X → Y))
h.toFinset (fun z f => (∀ a ∈ h, f a.1 = a.2) ∧ project (f x) = project z.2)
-- The bad event is the union over `(a, y) ∈ h` of `f ⊨ h ∧ project (f x) = project y`.
have he : (Distribution.uniform (X → Y)).mass (fun f =>
(∀ z ∈ h, f z.1 = z.2) ∧ ∃ z ∈ h, z.1 ≠ x ∧ project (f x) = project z.2) =
(Distribution.uniform (X → Y)).mass (fun f =>
∃ z ∈ h.toFinset, (∀ a ∈ h, f a.1 = a.2) ∧ project (f x) = project z.2) := by
apply Distribution.mass_congr
intro f
constructor
· rintro ⟨ha, z, hz, _, he⟩
exact ⟨z, List.mem_toFinset.mpr hz, ha, he⟩
· rintro ⟨z, hz, ha, he⟩
have hz' := List.mem_toFinset.mp hz
exact ⟨ha, z, hz', hfresh z hz', he⟩
rw [he]
refine hm.trans ?_
-- Each term is `Pr[f ⊨ h] / |Z|`: `f ⊨ h` ignores the fresh coordinate `x`.
simp only [uniform_mass_fresh_projected_eval_eq project hproject x _
(fun f => ∀ a ∈ h, f a.1 = a.2) (fun f g he => by
constructor <;> intro hh z hz
· rw [← he z.1 (hfresh z hz)]; exact hh z hz
· rw [he z.1 (hfresh z hz)]; exact hh z hz), Finset.sum_const,
nsmul_eq_mul]
-- At most `|h|` distinct old pairs.
have hc : (h.toFinset.card : ℝ) ≤ h.length := by
exact_mod_cast List.toFinset_card_le h
have hn := (Distribution.uniform_nonNeg (A := X → Y)).mass_nonneg
(fun f => ∀ z ∈ h, f z.1 = z.2)
calc
_ ≤ (h.length : ℝ) * ((Distribution.uniform (X → Y)).mass
(fun f => ∀ z ∈ h, f z.1 = z.2) / Fintype.card Z) :=
mul_le_mul_of_nonneg_right hc (div_nonneg hn (Nat.cast_nonneg _))
_ = _ := by ring
· -- Repeated `x`: some `(x, y) ∈ h`, so `f ⊨ h` fixes `f x = y`. A match
-- `project y' = project y` with `(b, y') ∈ h` forces `b = x` by `hgood`, contradicting
-- `b ≠ x`. The bad event is empty; the bound is nonnegative.
push Not at hfresh
obtain ⟨a, ha, hax⟩ := hfresh
rw [Distribution.mass_eq_zero_of_forall_not _ (by
rintro f ⟨hh, b, hb, hbx, hby⟩
apply hbx
have hay : f x = a.2 := hax ▸ hh a ha
exact (hgood b hb a ha (hby.symm.trans (congrArg project hay))).trans hax)]
exact mul_nonneg (div_nonneg (Nat.cast_nonneg _) (Nat.cast_nonneg _))
((Distribution.uniform_nonNeg (A := X → Y)).mass_nonneg _)
/-- **A transcript of a uniform table.** Let `h = [(x₁, y₁), …, (x_q, y_q)]` be answered by a
function `g : X → Y` (`g xₖ = yₖ` for every `k`), and let `S = {x₁, …, x_q}` be its set of
distinct inputs. For `F` uniform on `X → Y`,
Pr[F xₖ = yₖ for every k] = (1/|Y|)^|S| .
The event is `F = g on S`: one independent uniform coordinate per distinct input. A repeated
input names a coordinate already fixed and adds no factor.
```text
h: (a, y₁) (b, y₂) (a, y₁) a ≠ b in X
S: a b mass (1/|Y|)^2, not (1/|Y|)^3
```
The formula follows from the table model of MauRen16, printed p. 6: "a uniformly chosen
function table F", queried by "receiving as output the value F (x)". -/
lemma uniform_mass_transcript_eq {X Y : Type*} [Fintype X] [DecidableEq X]
[Fintype Y] [Nonempty Y] (h : List (X × Y)) (g : X → Y)
(hg : ∀ z ∈ h, g z.1 = z.2) :
(Distribution.uniform (X → Y)).mass (fun f => ∀ z ∈ h, f z.1 = z.2) =
(1 / (Fintype.card Y : ℝ)) ^ (h.map Prod.fst).toFinset.card := by
classical
-- `points` is `S`, the set of distinct inputs of `h`.
let points := (h.map Prod.fst).toFinset
-- The points of `S` are distinct, so `Pr[F x = g x for every x ∈ S] = (1/|Y|)^|S|`.
have hm := uniform_mass_eval_eq (fun x : points => x.val) Subtype.val_injective
(fun x : points => g x.val)
rw [Fintype.card_coe] at hm
-- It remains to show that the two events are equal: `F` matches `h` iff `F = g` on `S`.
refine Eq.trans ?_ hm
apply Distribution.mass_congr
intro f
constructor
· intro hf
-- (⇒) Each `x ∈ S` is the input of some `(x, y) ∈ h`, so `F x = y = g x`.
funext x
obtain ⟨z, hz, he⟩ := List.mem_map.mp (List.mem_toFinset.mp x.property)
exact he ▸ (hf z hz).trans (hg z hz).symm
· intro hf z hz
-- (⇐) Each `(x, y) ∈ h`, repeated or not, has `x ∈ S`, so `F x = g x = y`.
have hx : z.1 ∈ points := List.mem_toFinset.mpr (List.mem_map.mpr ⟨z, hz, rfl⟩)
exact (congrFun hf ⟨z.1, hx⟩).trans (hg z hz)
/-- **Query maps with the same equality pattern.** Let `left : I → X` and `right : I → Z` be fixed
query maps with `left i = left j ↔ right i = right j` for all `i, j`. For `F` uniform on
`X → Y` and `G` uniform on `Z → Y`,
Law(i ↦ F (left i)) = Law(i ↦ G (right i)) on I → Y.
Here `X`, `Z` are finite and `Y` is finite and nonempty; `I` may be infinite, since only the
finite images of `left` and `right` are read. The common law gives one independent uniform value
to each class of equal queries, and the same value at every index of the class:
```text
i: 0 1 2 3 …
left i: a b a b … a ≠ b in X
right i: u v u v … u ≠ v in Z
output: U V U V … U, V independent uniform in Y
```
Proof chain, with `R = left(I)`, `sel x` one index with `left (sel x) = x`, and
`recover v i = v (left i)` (`~` is equality of laws):
```text
F ∘ left = recover (x ↦ F x on R) by definition of recover
~ recover (𝒰(R → Y)) R is a set of distinct points of X
~ recover (x ↦ G (right (sel x)) on R) x ↦ right (sel x) is injective
= G ∘ right right (sel (left i)) = right i
```
The table model is MauRen16, printed p. 6: "a uniformly chosen function table F", queried by
"receiving as output the value F (x)". -/
lemma fTransform_eval_uniform_eq_of_same_fibers {I X Z Y : Type*}
[Fintype X] [DecidableEq X] [Fintype Z] [DecidableEq Z]
[Fintype Y] [Nonempty Y]
(left : I → X) (right : I → Z)
(hfibers : ∀ i j, left i = left j ↔ right i = right j) :
Distribution.fTransform (fun f : X → Y => fun i => f (left i))
(Distribution.uniform (X → Y)) =
Distribution.fTransform (fun f : Z → Y => fun i => f (right i))
(Distribution.uniform (Z → Y)) := by
classical
-- `R = left(I) ⊆ X` is finite because `X` is, even for infinite `I`.
let R := Set.range left
let : Fintype R := Fintype.ofFinite R
-- For each `x ∈ R`, choose an index `sel x` with `left (sel x) = x`.
let selected (x : R) : I := x.property.choose
have hselected (x : R) : left (selected x) = x.val := x.property.choose_spec
-- `points x = right (sel x)`: the right query paired with `x`.
let points (x : R) : Z := right (selected x)
-- `points` is injective: `right (sel x) = right (sel x')` gives, by `⇐` of the hypothesis,
-- `x = left (sel x) = left (sel x') = x'`.
have hinj : Function.Injective points := by
intro x y he
apply Subtype.ext
exact (hselected x).symm.trans (((hfibers _ _).mpr he).trans (hselected y))
-- `points (left i) = right i`: `sel (left i)` and `i` have the same left query, hence by `⇒`
-- the same right query.
have hp (i : I) : points ⟨left i, ⟨i, rfl⟩⟩ = right i :=
(hfibers _ _).mp (hselected ⟨left i, ⟨i, rfl⟩⟩)
-- `recover v i = v (left i)`: rebuild the output at every index from one value per class.
let recover (values : R → Y) : I → Y := fun i => values ⟨left i, ⟨i, rfl⟩⟩
-- `(F x)_{x ∈ R}` is uniform on `R → Y`: the points of `R` are distinct.
have hl := fTransform_eval_uniform_eq_uniform (Y := Y)
(fun x : R => x.val) Subtype.val_injective
-- `(G (points x))_{x ∈ R}` is uniform on `R → Y`: `points` is injective.
have hr := fTransform_eval_uniform_eq_uniform (Y := Y) points hinj
-- Equal laws have equal images under `recover`; an image of an image is the image under
-- the composite: `Law(recover (F x)_{x ∈ R}) = Law(recover (G (points x))_{x ∈ R})`.
have he := congrArg (Distribution.fTransform recover) (hl.trans hr.symm)
simp only [Distribution.fTransform_fTransform] at he
-- The left side is `Law(i ↦ F (left i))` by definition of `recover`. On the right,
-- `G (points (left i)) = G (right i)` by `hp`.
refine he.trans ?_
congr 1
funext table i
exact congrArg table (hp i)
/-- **Adaptive fresh evaluations of a uniform table.** Let `F` be uniform on `X → Y`. Fix
finitely many base coordinates `base : B → X` and reveal `b = F ∘ base`. From this one reveal,
choose a good event `good b` and the entire next batch `next b : I → X`; the batch does not
depend on any of its own outputs. If on `good b` the combined family
`Sum.elim base (next b) : B ⊕ I → X` is injective, then for every `values : I → Y`
```text
Pr[good(F ∘ base) ∧ F ∘ next(F ∘ base) = values] = Pr[good(F ∘ base)] · (1/|Y|)^|I|
```
Timing of the single table `F`:
```text
F ←$ (X → Y)
base ──F──▶ b ──▶ good(b), next(b) ──F──▶ F ∘ next(b) =? values
```
Outside `good` no freshness is required: selected coordinates may repeat base ones. The fixed
`base` is injective as soon as some base tuple is good; it may repeat only when no base tuple
is good. The statement is a joint mass, so `Pr[good] = 0` is allowed.
MauRen16, printed p. 6, specifies the URF by "a uniformly chosen function table F" queried by
"giving as input a value x and receiving as output the value F (x)". Jost, Figure 4.4, printed
p. 55, samples `T [x]` only "if T [x] = ⊥" and then does "output T [x]", so a repeated query
reuses its stored reply. The two table models agree; this formula is a derived consequence of
`uniform_mass_eval_eq`, not a stated paper theorem. -/
lemma uniform_mass_adaptive_eval_eq_mul {X Y B I : Type*}
[Fintype X] [DecidableEq X] [Fintype Y] [Nonempty Y]
[Finite B] [Fintype I]
(base : B → X) (next : (B → Y) → I → X) (good : (B → Y) → Prop)
(hfresh : ∀ b, good b → Function.Injective (Sum.elim base (next b))) (values : I → Y) :
(Distribution.uniform (X → Y)).mass (fun f =>
good (fun b => f (base b)) ∧ (fun i => f (next (fun b => f (base b)) i)) = values) =
(Distribution.uniform (X → Y)).mass (fun f => good (fun b => f (base b))) *
(1 / (Fintype.card Y : ℝ)) ^ Fintype.card I := by
classical
let := Fintype.ofFinite B
let F := Distribution.uniform (X → Y)
let observe := fun f : X → Y => fun b => f (base b)
-- Proof chain, summing over revealed base tuples `b`:
--
-- Pr[good ∧ F ∘ next = values] = Σ_b Pr[good ∧ F ∘ next = values ∧ F ∘ base = b]
-- good b: Pr[F ∘ Sum.elim base (next b) = Sum.elim b values] = (1/|Y|)^(|B| + |I|)
-- Pr[good ∧ F ∘ base = b] = Pr[F ∘ base = b] = (1/|Y|)^|B|
-- not good b: both summands are 0
--
-- Partition both masses by the revealed base tuple `F ∘ base`.
rw [Distribution.mass_eq_sum_mass_fiber F _ observe Finset.univ
(fun _ _ => Finset.mem_univ _),
Distribution.mass_eq_sum_mass_fiber F _ observe Finset.univ
(fun _ _ => Finset.mem_univ _), Finset.sum_mul]
apply Finset.sum_congr rfl
intro b _
by_cases hb : good b
· -- Base injectivity is the base part of freshness at this good `b`.
have hbase : Function.Injective base := (Sum.elim_injective.mp (hfresh b hb)).1
-- With `F ∘ base = b` fixed, `next b` is a fixed batch: the joint event is the evaluation
-- of the single injective family `Sum.elim base (next b)` at `Sum.elim b values`.
have hleft : F.mass (fun f =>
(good (observe f) ∧ (fun i => f (next (observe f) i)) = values) ∧ observe f = b) =
F.mass (fun f => (fun c : B ⊕ I => f (Sum.elim base (next b) c)) =
Sum.elim b values) := by
apply Distribution.mass_congr
intro f
constructor
· rintro ⟨⟨_, hy⟩, hf⟩
funext c
cases c with
| inl c => exact congrFun hf c
| inr i => rw [hf] at hy; exact congrFun hy i
· intro he
have hf : observe f = b := funext fun c => congrFun he (.inl c)
refine ⟨⟨hf ▸ hb, ?_⟩, hf⟩
rw [hf]
exact funext fun i => congrFun he (.inr i)
-- The reveal `F ∘ base = b` already implies `good`.
have hright : F.mass (fun f => good (observe f) ∧ observe f = b) =
F.mass (fun f => observe f = b) :=
Distribution.mass_congr _ fun f =>
⟨fun h => h.2, fun h => ⟨h ▸ hb, h⟩⟩
-- Uniform marginals at distinct points; `(1/|Y|)^(|B|+|I|)` splits off `(1/|Y|)^|I|`.
rw [hleft, hright,
uniform_mass_eval_eq _ (hfresh b hb) (Sum.elim b values),
uniform_mass_eval_eq base hbase b, Fintype.card_sum, pow_add]
· -- Outside `good`, both summands are empty events; no freshness is used here.
rw [Distribution.mass_eq_zero_of_forall_not F (by
rintro f ⟨⟨hg, _⟩, hf⟩
exact hb (hf ▸ hg)), Distribution.mass_eq_zero_of_forall_not F (by
rintro f ⟨hg, hf⟩
exact hb (hf ▸ hg)), zero_mul]
/-- **Disjoint coordinates of one uniform table are independent.** For uniform `f : X → Y` and
`Sum.elim base next : B ⊕ I → X` injective,
`Pr[good (f ∘ base) ∧ output (f ∘ next)] = Pr[good (f ∘ base)] · Pr_{g ← U(I → Y)}[output g]`.
```text
f : X → Y, sampled once
base(B) ⊆ X ──f──▶ good
next(I) ⊆ X ──f──▶ output base(B) ∩ next(I) = ∅
```
Model: MauRen16, printed p. 6, "a uniformly chosen function table F". -/
lemma uniform_mass_disjoint_events {X Y B I : Type*}
[Fintype X] [DecidableEq X] [Fintype Y] [Nonempty Y]
[Finite B] [Fintype I] [DecidableEq I]
(base : B → X) (next : I → X)
(hinj : Function.Injective (Sum.elim base next))
(good : (B → Y) → Prop) (output : (I → Y) → Prop) :
(Distribution.uniform (X → Y)).mass (fun f =>
good (fun b => f (base b)) ∧ output (fun i => f (next i))) =
(Distribution.uniform (X → Y)).mass (fun f => good (fun b => f (base b))) *
(Distribution.uniform (I → Y)).mass output := by
classical
-- Partition by the new reply tuple `value = f ∘ next`; expand `Pr[output]` as a sum over
-- `value` and compare the two sums termwise.
rw [Distribution.mass_eq_sum_mass_fiber _ _ (fun f i => f (next i)) Finset.univ
(fun _ _ => Finset.mem_univ _)]
conv_rhs => rw [Distribution.mass_eq_sum (Distribution.uniform (I → Y)) output, Finset.mul_sum]
apply Finset.sum_congr rfl
intro value _
by_cases hv : output value
-- `output value`: the fiber is `good (f ∘ base) ∧ f ∘ next = value`.
· rw [ite_eq_left hv]
have hm : (Distribution.uniform (X → Y)).mass (fun f =>
(good (fun b => f (base b)) ∧ output (fun i => f (next i))) ∧
(fun i => f (next i)) = value) =
(Distribution.uniform (X → Y)).mass (fun f =>
good (fun b => f (base b)) ∧ (fun i => f (next i)) = value) := by
apply Distribution.mass_congr
intro f
exact ⟨fun h => ⟨h.1.1, h.2⟩, fun h => ⟨⟨h.1, h.2.symm ▸ hv⟩, h.2⟩⟩
-- Adaptive lemma with constant `next`:
-- `Pr[good ∧ f ∘ next = value] = Pr[good] / |Y| ^ |I| = Pr[good] · U(I → Y){value}`.
rw [hm, uniform_mass_adaptive_eval_eq_mul base (fun _ => next) good
(fun _ _ => hinj) value, Distribution.uniform_apply, Fintype.card_fun, Nat.cast_pow,
div_pow, one_pow]
-- `¬ output value`: both terms vanish.
· rw [ite_eq_right hv, mul_zero]
exact Distribution.mass_eq_zero_of_forall_not _ (fun _ h => hv (h.2 ▸ h.1.2))
/-- **Fresh outputs factor from an event on a finite base.** For uniform `f : X → Y`, `good`
depending only on `f` on `base`, and `next : I → X` injective with image outside `base`,
`Pr[good f ∧ output (f ∘ next)] = Pr[good f] · Pr_{g ← U(I → Y)}[output g]`.
`fallback` only fills the coordinates outside `base` to represent the restricted table as a
total function; the factorization does not depend on it. This is `uniform_mass_disjoint_events`
with `B = base` and the event `good ∘ extend`. -/
lemma uniform_mass_fresh_outputs_eq {X Y I : Type*}
[Fintype X] [DecidableEq X] [Fintype Y] [Nonempty Y] [Fintype I] [DecidableEq I]
(base : Finset X) (next : I → X) (hinj : Function.Injective next)
(hdisjoint : ∀ i, next i ∉ base) (fallback : Y)
(good : (X → Y) → Prop) (output : (I → Y) → Prop)
(hgood : ∀ f g, (∀ x ∈ base, f x = g x) → (good f ↔ good g)) :
(Distribution.uniform (X → Y)).mass (fun f => good f ∧ output (fun i => f (next i))) =
(Distribution.uniform (X → Y)).mass good * (Distribution.uniform (I → Y)).mass output := by
classical
-- `extend b` is `b` on `base` and `fallback` elsewhere.
let extend (b : base → Y) : X → Y := fun x =>
if hx : x ∈ base then b ⟨x, hx⟩ else fallback
-- `good` reads only `base`, so it cannot see `fallback`.
have hg (f : X → Y) : good (extend (fun x : base => f x)) ↔ good f := by
apply hgood
intro x hx
simp only [extend, dite_eq_left hx]
-- `Sum.elim Subtype.val next : base ⊕ I → X` is injective: each part is injective, and
-- a base point equal to some `next i` contradicts `hdisjoint`.
have hsum : Function.Injective (Sum.elim (fun x : base => x.val) next) := by
intro a b he
cases a with
| inl a =>
cases b with
| inl b => exact congrArg Sum.inl (Subtype.ext he)
| inr b =>
change a.val = next b at he
exact False.elim (hdisjoint b (he ▸ a.2))
| inr a =>
cases b with
| inl b =>
change next a = b.val at he
exact False.elim (hdisjoint a (he.symm ▸ b.2))
| inr b => exact congrArg Sum.inr (hinj he)
-- Disjoint events with `B = base` and `good ∘ extend`, then drop `extend` by `hg`.
have he := uniform_mass_disjoint_events (fun x : base => x.val) next hsum
(fun b => good (extend b)) output
simpa only [hg] using he
/-! ## Lazy sampling along a walk on a uniform random function -/
/-- The input consumed at site `t` when the walk is replayed in the **lazy
world**: against a tuple `y` of recorded outputs rather than against a
function. `par t = none` marks a site that starts from the public initial
state `x₀`. -/
def walkSite {ι X : Type*} (par : ι → Option ι) (g : ι → X → X) (x₀ : X)
(y : ι → X) (t : ι) : X :=
g t ((par t).elim x₀ y)
/-- **A walk always replays itself.** Run the lazy walk
on the very outputs `f` produced, and it reproduces `f`'s own site inputs.
This needs no hypothesis on the walk at all — it is the recursion read
outwards. -/
lemma walkSite_of_eval {ι X : Type*}
(par : ι → Option ι) (g : ι → X → X) (x₀ : X)
(inp : (X → X) → ι → X)
(hinp : ∀ f t, inp f t = g t ((par t).elim x₀ (fun h => f (inp f h))))
(f : X → X) :
inp f = walkSite par g x₀ (fun t => f (inp f t)) := by
funext t
rw [hinp f t]
rfl
/-- **A walk is pinned by a consistent tuple**: if `f`
answers `y t` at every lazily replayed site input, then `f`'s own site inputs
*are* the replayed ones. The converse direction of `walkSite_of_eval`, and the
one that needs the parent relation to be well-founded — supplied here as a
`rank` that strictly decreases towards the parent. -/
lemma eq_walkSite_of_eval_eq {ι X : Type*}
(par : ι → Option ι) (g : ι → X → X) (x₀ : X) (rank : ι → ℕ)
(hrank : ∀ t h, par t = some h → rank h < rank t)
(inp : (X → X) → ι → X)
(hinp : ∀ f t, inp f t = g t ((par t).elim x₀ (fun h => f (inp f h))))
{f : X → X} {y : ι → X}
(hf : ∀ t, f (walkSite par g x₀ y t) = y t) :
inp f = walkSite par g x₀ y := by
have key : ∀ n : ℕ, ∀ t : ι, rank t ≤ n → inp f t = walkSite par g x₀ y t := by
intro n
induction n with
| zero =>
intro t ht
rw [hinp f t]
rcases hp : par t with _ | h
· simp [walkSite, hp]
· exact absurd (hrank t h hp) (by omega)
| succ n ih =>
intro t ht
rw [hinp f t]
rcases hp : par t with _ | h
· simp [walkSite, hp]
· have hh : rank h ≤ n := by have := hrank t h hp; omega
simp only [hp, Option.elim, walkSite]
rw [ih h hh, hf h]
exact funext fun t => key (rank t) t le_rfl
/-- **The law of the walk's evaluations, until the first
repeat.** At a tuple `y` whose replayed walk visits pairwise distinct site
inputs, the uniform random function returns exactly `y` with probability
`(1/|𝒳|)^{|ι|}` — the law of `|ι|` independent uniform draws.
This is the lazy-sampling statement. It is false without the repeat-freeness
hypothesis: at a tuple whose replay repeats an input the two directions of
`walkSite_of_eval` still pin the fiber, but the fiber is the one of a *smaller*
set of distinct evaluation points, so its mass is larger (or zero). -/
lemma uniform_mass_walk_eval_eq {ι X : Type*} [Fintype ι] [DecidableEq ι]
[Fintype X] [DecidableEq X] [Nonempty X]
(par : ι → Option ι) (g : ι → X → X) (x₀ : X) (rank : ι → ℕ)
(hrank : ∀ t h, par t = some h → rank h < rank t)
(inp : (X → X) → ι → X)
(hinp : ∀ f t, inp f t = g t ((par t).elim x₀ (fun h => f (inp f h))))
{y : ι → X} (hy : Function.Injective (walkSite par g x₀ y)) :
(Distribution.uniform (X → X)).mass (fun f => (fun t => f (inp f t)) = y)
= (1 / (Fintype.card X : ℝ)) ^ Fintype.card ι := by
have hev : ∀ f : X → X,
((fun t => f (inp f t)) = y) ↔ ((fun t => f (walkSite par g x₀ y t)) = y) := by
intro f
constructor
· intro h
have hw : inp f = walkSite par g x₀ y := by
rw [walkSite_of_eval par g x₀ inp hinp f, h]
rw [← hw]; exact h
· intro h
have hw : inp f = walkSite par g x₀ y :=
eq_walkSite_of_eval_eq par g x₀ rank hrank inp hinp (congrFun h)
rw [hw]; exact h
rw [Distribution.mass_congr _ hev]
exact uniform_mass_eval_eq (walkSite par g x₀ y) hy y
/-- **The repeat-free region has the same mass in both
worlds.** The probability that a uniform random function drives the walk
without a repeat equals the probability that `|ι|` independent uniform draws
replay it without a repeat. Only the repeat-free region transports — which is
enough, because the birthday argument takes the complement afterwards. -/
lemma uniform_mass_walk_injective_eq {ι X : Type*} [Fintype ι] [DecidableEq ι]
[Fintype X] [DecidableEq X] [Nonempty X]
(par : ι → Option ι) (g : ι → X → X) (x₀ : X) (rank : ι → ℕ)
(hrank : ∀ t h, par t = some h → rank h < rank t)
(inp : (X → X) → ι → X)
(hinp : ∀ f t, inp f t = g t ((par t).elim x₀ (fun h => f (inp f h)))) :
(Distribution.uniform (X → X)).mass (fun f => Function.Injective (inp f))
= (Distribution.uniform (ι → X)).mass
(fun y => Function.Injective (walkSite par g x₀ y)) := by
classical
have h1 : (Distribution.uniform (X → X)).mass (fun f => Function.Injective (inp f))
= (Distribution.fTransform (fun f : X → X => fun t => f (inp f t))
(Distribution.uniform (X → X))).mass
(fun y => Function.Injective (walkSite par g x₀ y)) := by
rw [Distribution.mass_fTransform]
refine Distribution.mass_congr _ fun f => ?_
rw [← walkSite_of_eval par g x₀ inp hinp f]
rw [h1,
Distribution.mass_eq_sum_of_support_subset _ (Finset.subset_univ _),
Distribution.mass_eq_sum_of_support_subset _ (Finset.subset_univ _)]
refine Finset.sum_congr rfl fun y hy => ?_
have hinj : Function.Injective (walkSite par g x₀ y) := (Finset.mem_filter.mp hy).2
rw [Distribution.fTransform_apply_eq_mass, Distribution.uniform_apply,
uniform_mass_walk_eval_eq par g x₀ rank hrank inp hinp hinj,
Fintype.card_fun, div_pow, one_pow]
push_cast
ring
/-- **One coordinate determined is one factor lost.** If
an event on uniform tuples pins the value at coordinate `c` — through an
injective `u` — against a quantity `v` that never reads coordinate `c`, its
probability is at most `1/|𝒳|`.
This is the elementary counting behind every step of a birthday argument, in
the form the walk consumes: `u` is the step at the site being added and `v` is
the input already sitting at the site it might hit. -/
lemma uniform_mass_coord_determined_le {ι X : Type*} [Fintype ι] [DecidableEq ι]
[Fintype X] [DecidableEq X] [Nonempty X]
(c : ι) (u : X → X) (hu : Function.Injective u) (v : (ι → X) → X)
(hv : ∀ y z : ι → X, (∀ i, i ≠ c → y i = z i) → v y = v z) :
(Distribution.uniform (ι → X)).mass (fun y => u (y c) = v y)
≤ 1 / (Fintype.card X : ℝ) := by
classical
have hXpos : (0 : ℝ) < (Fintype.card X : ℝ) := by exact_mod_cast Fintype.card_pos
have hcpos : 0 < Fintype.card ι := Fintype.card_pos_iff.mpr ⟨c⟩
have hsub : Fintype.card {i : ι // i ≠ c} = Fintype.card ι - 1 := by
rw [Fintype.card_subtype_compl (p := fun i : ι => i = c), Fintype.card_subtype_eq]
have hcard : ((Finset.univ : Finset (ι → X)).filter (fun y => u (y c) = v y)).card
≤ Fintype.card X ^ (Fintype.card ι - 1) := by
have hle := Finset.card_le_card_of_injOn
(f := fun (y : ι → X) => fun i : {i : ι // i ≠ c} => y i.1)
(s := (Finset.univ : Finset (ι → X)).filter (fun y => u (y c) = v y))
(t := (Finset.univ : Finset ({i : ι // i ≠ c} → X)))
(fun y _ => Finset.mem_univ _) ?_
· rw [Finset.card_univ, Fintype.card_fun, hsub] at hle
exact hle
· intro y hy z hz hyz
simp only [Finset.coe_filter, Set.mem_ofPred_eq] at hy hz
have hoff : ∀ i, i ≠ c → y i = z i := fun i hi => congrFun hyz ⟨i, hi⟩
have hc : y c = z c := hu (by rw [hy.2, hv y z hoff, ← hz.2])
funext i
by_cases hi : i = c
· subst hi; exact hc
· exact hoff i hi
rw [Distribution.uniform_mass_eq_card_filter, Fintype.card_fun]
push_cast
rw [div_le_div_iff₀ (by positivity) hXpos]
have hpow : (Fintype.card X : ℝ) ^ Fintype.card ι
= (Fintype.card X : ℝ) ^ (Fintype.card ι - 1) * (Fintype.card X : ℝ) := by
rw [← pow_succ]
congr 1
omega
rw [one_mul, hpow]
have hcast : (((Finset.univ : Finset (ι → X)).filter (fun y => u (y c) = v y)).card : ℝ)
≤ (Fintype.card X : ℝ) ^ (Fintype.card ι - 1) := by
exact_mod_cast hcard
exact mul_le_mul_of_nonneg_right hcast hXpos.le
/-- **One step of the birthday argument.** In the lazy
world two distinct sites collide with probability at most `1/|𝒳|`.
The case analysis is the whole point, and both branches are genuinely
different. If the two sites share a parent they read the *same* state, and
`hsib` makes the collision outright impossible — this is the branch that covers
two sites starting from the fixed `x₀`, whose inputs carry no randomness at
all. Otherwise one of the two reads a state that the other does not, and
`uniform_mass_coord_determined_le` charges the step `1/|𝒳|`; note this runs in
*either* direction, so a deterministic site colliding with a randomized one is
charged just as a continuation step is. -/
lemma uniform_mass_walkSite_eq_le {ι X : Type*} [Fintype ι] [DecidableEq ι]
[Fintype X] [DecidableEq X] [Nonempty X]
(par : ι → Option ι) (g : ι → X → X) (x₀ : X)
(hg : ∀ t, Function.Injective (g t))
(hsib : ∀ t t', t ≠ t' → par t = par t' → ∀ x : X, g t x ≠ g t' x)
{t t' : ι} (htt : t ≠ t') :
(Distribution.uniform (ι → X)).mass
(fun y => walkSite par g x₀ y t = walkSite par g x₀ y t')
≤ 1 / (Fintype.card X : ℝ) := by
classical
have hXpos : (0 : ℝ) < (Fintype.card X : ℝ) := by exact_mod_cast Fintype.card_pos
by_cases hpar : par t = par t'
· have hz : (Distribution.uniform (ι → X)).mass
(fun y => walkSite par g x₀ y t = walkSite par g x₀ y t') = 0 :=
Distribution.mass_eq_zero_of_forall_not _ fun y hy =>
hsib t t' htt hpar ((par t').elim x₀ y)
(by simpa only [walkSite, hpar] using hy)
rw [hz]
positivity
· rcases hp : par t with _ | c
· rcases hp' : par t' with _ | c'
· exact absurd (by rw [hp, hp']) hpar
· refine le_of_le_of_eq ?_ rfl
have hdet := uniform_mass_coord_determined_le (ι := ι) (X := X) c' (g t')
(hg t') (fun y => walkSite par g x₀ y t) ?_
· refine le_trans (le_of_eq ?_) hdet
refine Distribution.mass_congr _ fun y => ?_
simp [walkSite, hp, hp', eq_comm]
· intro y z hyz
simp [walkSite, hp]
· have hdet := uniform_mass_coord_determined_le (ι := ι) (X := X) c (g t)
(hg t) (fun y => walkSite par g x₀ y t') ?_
· refine le_trans (le_of_eq ?_) hdet
refine Distribution.mass_congr _ fun y => ?_
simp [walkSite, hp]
· intro y z hyz
rcases hp' : par t' with _ | c'
· simp [walkSite, hp']
· have hne : c' ≠ c := by
intro h
exact hpar (by rw [hp, hp', h])
simp [walkSite, hp', hyz c' hne]
/-- **The birthday bound for a walk, on a set of sites.**
The union bound run one site at a time: adding a site to `s` either repeats a
collision already inside `s`, or hits one of its `|s|` inputs, and the second
costs `|s|/|𝒳|`.
The induction is on the *set of sites*, not on a global statement, because
"repeat-free so far" is what makes the next step's charge legitimate; the