File size: 36,265 Bytes
ea76aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib
import OAI.Computability.DirectedFeedback.Games.ProductLaw

namespace OAI

namespace DFVSGames.Foundations.Games.FiniteDistribution

open scoped BigOperators
noncomputable section

variable {Seed Ω : Type*} [Fintype Seed] [Fintype Ω]

def mixture (seedLaw : FiniteDistribution Seed)
    (laws : Seed → FiniteDistribution Ω) : FiniteDistribution Ω where
  weight x := ∑ seed, seedLaw.weight seed * (laws seed).weight x
  nonnegative x := Finset.sum_nonneg fun seed _ =>
    mul_nonneg (seedLaw.nonnegative seed) ((laws seed).nonnegative x)
  normalized := by
    rw [Finset.sum_comm]
    simp_rw [← Finset.mul_sum, (laws _).normalized, mul_one]
    exact seedLaw.normalized

theorem probability_mixture (seedLaw : FiniteDistribution Seed)
    (laws : Seed → FiniteDistribution Ω) (event : Ω → Bool) :
    (seedLaw.mixture laws).probability event =
      ∑ seed, seedLaw.weight seed * (laws seed).probability event := by
  unfold probability mixture
  calc
    _ = ∑ x, ∑ seed,
        seedLaw.weight seed * (if event x then (laws seed).weight x else 0) := by
      apply Finset.sum_congr rfl
      intro x _
      cases event x <;> simp
    _ = _ := by
      rw [Finset.sum_comm]
      apply Finset.sum_congr rfl
      intro seed _
      rw [Finset.mul_sum]

theorem probability_mixture_le (seedLaw : FiniteDistribution Seed)
    (laws : Seed → FiniteDistribution Ω) (event : Ω → Bool) (bound : ℝ)
    (h : ∀ seed, (laws seed).probability event ≤ bound) :
    (seedLaw.mixture laws).probability event ≤ bound := by
  rw [probability_mixture]
  calc
    _ ≤ ∑ seed, seedLaw.weight seed * bound :=
      Finset.sum_le_sum fun seed _ =>
        mul_le_mul_of_nonneg_left (h seed) (seedLaw.nonnegative seed)
    _ = bound := by rw [← Finset.sum_mul, seedLaw.normalized, one_mul]

end
end DFVSGames.Foundations.Games.FiniteDistribution

namespace DFVSGames.Foundations.Games.FiniteDistribution
open scoped BigOperators
noncomputable section
variable {A B C : Type*} [Fintype A] [Fintype B] [Fintype C]

theorem weight_eq_probability_singleton [DecidableEq A] (μ : FiniteDistribution A) (a : A) :
    μ.weight a = μ.probability (fun x => decide (x = a)) := by
  classical
  simp [probability]

theorem pushforward_comp (μ : FiniteDistribution A) (f : A → B) (g : B → C) :
    (μ.pushforward f).pushforward g = μ.pushforward (fun a => g (f a)) := by
  classical
  apply eq_of_weight_eq
  intro c
  rw [weight_eq_probability_singleton, weight_eq_probability_singleton]
  simp only [probability_pushforward]

theorem transport_eq_pushforward (μ : FiniteDistribution A) (e : A ≃ B) :
    μ.transport e = μ.pushforward e := by
  classical
  apply eq_of_weight_eq
  intro b
  rw [weight_eq_probability_singleton, weight_eq_probability_singleton,
    probability_transport, probability_pushforward]

theorem expectation_pushforward (μ : FiniteDistribution A) (f : A → B) (h : B → ℝ) :
    (μ.pushforward f).expectation h = μ.expectation (fun a => h (f a)) := by
  classical
  simp only [expectation, pushforward, Finset.sum_mul]
  rw [Finset.sum_comm]
  apply Finset.sum_congr rfl
  intro a _
  simp [ite_mul]

theorem pushforward_mixture (μ : FiniteDistribution A) (ν : A → FiniteDistribution B)
    (f : B → C) :
    (μ.mixture ν).pushforward f = μ.mixture (fun a => (ν a).pushforward f) := by
  classical
  apply eq_of_weight_eq
  intro c
  rw [weight_eq_probability_singleton, weight_eq_probability_singleton]
  simp only [probability_pushforward, probability_mixture]

end
end DFVSGames.Foundations.Games.FiniteDistribution

namespace DFVSGames.Clean

open Foundations.Games
open scoped BigOperators

noncomputable section

def bernoulli (p : ℝ) (hp₀ : 0 ≤ p) (hp₁ : p ≤ 1) :
    FiniteDistribution Bool where
  weight b := if b then p else 1 - p
  nonnegative b := by cases b <;> simp [hp₀, sub_nonneg.mpr hp₁]
  normalized := by simp []

def maskCount {k : ℕ} (mask : Fin k → Bool) : ℕ :=
  (Finset.univ.filter fun i => mask i = true).card

theorem product_mask {k : ℕ} (mask : Fin k → Bool) (ρ : ℝ) :
    (∏ i, if mask i then ρ else 1) = ρ ^ maskCount mask := by
  classical
  simp [maskCount, Finset.prod_ite]

theorem iid_product_moment {Ω : Type*} [Fintype Ω]
    (μ : FiniteDistribution Ω) (f : Ω → ℝ) (k : ℕ) :
    (μ.iid k).expectation (fun x => ∏ i, f (x i)) = μ.expectation f ^ k := by
  classical
  unfold FiniteDistribution.expectation FiniteDistribution.iid
  rw [Fintype.sum_pow]
  apply Finset.sum_congr rfl
  intro x _
  rw [Finset.prod_mul_distrib]

theorem bernoulli_mask_moment (p : ℝ) (hp₀ : 0 ≤ p) (hp₁ : p ≤ 1)
    (k : ℕ) (ρ : ℝ) :
    ((bernoulli p hp₀ hp₁).iid k).expectation (fun mask => ρ ^ maskCount mask) =
      (1 - p * (1 - ρ)) ^ k := by
  calc
    _ = ((bernoulli p hp₀ hp₁).iid k).expectation
        (fun mask => ∏ i, if mask i then ρ else 1) :=
      FiniteDistribution.expectation_congr _ (fun mask => (product_mask mask ρ).symm)
    _ = ((bernoulli p hp₀ hp₁).expectation (fun b => if b then ρ else 1)) ^ k :=
      iid_product_moment (bernoulli p hp₀ hp₁) (fun b : Bool => if b then ρ else 1) k
    _ = _ := by
      congr 1
      simp [FiniteDistribution.expectation, bernoulli]
      ring

variable {E : Type*} [Fintype E] [Nonempty E]

def cleanProbability (β : ℝ) (E : Type*) [Fintype E] : ℝ :=
  β / (Fintype.card E : ℝ)

omit [Nonempty E] in
theorem cleanProbability_nonneg {β : ℝ} (hβ₀ : 0 ≤ β) :
    0 ≤ cleanProbability β E :=
  div_nonneg hβ₀ (Nat.cast_nonneg _)

theorem cleanProbability_le_one {β : ℝ} (hβ₁ : β ≤ 1) :
    cleanProbability β E ≤ 1 := by
  have hc : (1 : ℝ) ≤ Fintype.card E := by
    exact_mod_cast Fintype.card_pos (α := E)
  apply (div_le_iff₀ (lt_of_lt_of_le zero_lt_one hc)).mpr
  simpa using hβ₁.trans hc

def singletonSlopeLaw (β : ℝ) (hβ₀ : 0 ≤ β) (hβ₁ : β ≤ 1) :
    FiniteDistribution (Bool × E) :=
  (bernoulli β hβ₀ hβ₁).product (FiniteDistribution.uniform E)

def cleanBit (origin : E) (sample : Bool × E) : Bool := by
  classical
  exact sample.1 && decide (sample.2 = origin)

theorem clean_pushforward (β : ℝ) (hβ₀ : 0 ≤ β) (hβ₁ : β ≤ 1) (origin : E) :
    (singletonSlopeLaw (E := E) β hβ₀ hβ₁).pushforward (cleanBit origin) =
      bernoulli (cleanProbability β E)
        (cleanProbability_nonneg hβ₀) (cleanProbability_le_one hβ₁) := by
  classical
  let μ := (singletonSlopeLaw (E := E) β hβ₀ hβ₁).pushforward (cleanBit origin)
  have ht : μ.weight true = cleanProbability β E := by
    simp [μ, FiniteDistribution.pushforward, singletonSlopeLaw,
      FiniteDistribution.product, Fintype.sum_prod_type,
      bernoulli, cleanBit, FiniteDistribution.uniform, cleanProbability, div_eq_mul_inv]
  apply FiniteDistribution.eq_of_weight_eq
  intro b
  cases b with
  | false =>
      change μ.weight false = 1 - cleanProbability β E
      have hn := μ.normalized
      simp only [Fintype.sum_bool] at hn
      linarith
  | true => exact ht

theorem clean_mask_law (β : ℝ) (hβ₀ : 0 ≤ β) (hβ₁ : β ≤ 1)
    (origin : E) (k : ℕ) :
    ((singletonSlopeLaw (E := E) β hβ₀ hβ₁).iid k).pushforward
        (fun sample i => cleanBit origin (sample i)) =
      (bernoulli (cleanProbability β E)
        (cleanProbability_nonneg hβ₀) (cleanProbability_le_one hβ₁)).iid k := by
  rw [← FiniteDistribution.iid_pushforward, clean_pushforward]

theorem clean_mask_moment (β : ℝ) (hβ₀ : 0 ≤ β) (hβ₁ : β ≤ 1)
    (origin : E) (k : ℕ) (ρ : ℝ) :
    ((singletonSlopeLaw (E := E) β hβ₀ hβ₁).iid k).expectation
        (fun sample => ρ ^ maskCount (fun i => cleanBit origin (sample i))) =
      (1 - cleanProbability β E * (1 - ρ)) ^ k := by
  rw [← FiniteDistribution.expectation_pushforward
    ((singletonSlopeLaw (E := E) β hβ₀ hβ₁).iid k)
    (fun sample i => cleanBit origin (sample i)) (fun mask => ρ ^ maskCount mask)]
  rw [clean_mask_law, bernoulli_mask_moment]

theorem cleanProbability_ge_of_card_le {β : ℝ} (hβ₀ : 0 ≤ β) {N : ℝ}
    (hcard : (Fintype.card E : ℝ) ≤ N) :
    β / N ≤ cleanProbability β E := by
  exact div_le_div_of_nonneg_left hβ₀
    (Nat.cast_pos.mpr Fintype.card_pos) hcard

theorem cleanProbability_ge_dimension {β : ℝ} (hβ₀ : 0 ≤ β) (dimW rs : ℕ)
    (hcard : Fintype.card E ≤ 2 ^ (dimW + rs)) :
    β * (2 : ℝ) ^ (-((dimW + rs : ℕ) : ℤ)) ≤ cleanProbability β E := by
  rw [zpow_neg, zpow_natCast, ← div_eq_mul_inv]
  apply cleanProbability_ge_of_card_le hβ₀
  exact_mod_cast hcard

theorem mask_rate_antitone {p q ρ : ℝ} (hp₀ : 0 ≤ p) (hp₁ : p ≤ 1)
    (hqp : q ≤ p) (hρ₀ : 0 ≤ ρ) (hρ₁ : ρ ≤ 1) (k : ℕ) :
    (1 - p * (1 - ρ)) ^ k ≤ (1 - q * (1 - ρ)) ^ k := by
  have hl : 0 ≤ 1 - p * (1 - ρ) := by
    nlinarith [mul_nonneg hp₀ hρ₀]
  apply pow_le_pow_left₀ hl
  exact sub_le_sub_left (mul_le_mul_of_nonneg_right hqp (sub_nonneg.mpr hρ₁)) 1

theorem clean_mask_moment_le (β : ℝ) (hβ₀ : 0 ≤ β) (hβ₁ : β ≤ 1)
    (origin : E) (k : ℕ) {N : ℝ} (hcard : (Fintype.card E : ℝ) ≤ N) :
    ((singletonSlopeLaw (E := E) β hβ₀ hβ₁).iid k).expectation
        (fun sample => (1 - (1 : ℝ) / 3600) ^
          maskCount (fun i => cleanBit origin (sample i))) ≤
      (1 - (β / N) / 3600) ^ k := by
  rw [clean_mask_moment]
  have h := mask_rate_antitone (cleanProbability_nonneg (E := E) hβ₀)
    (cleanProbability_le_one (E := E) hβ₁)
    (cleanProbability_ge_of_card_le hβ₀ hcard)
    (ρ := 1 - (1 : ℝ) / 3600) (by norm_num) (by norm_num) k
  convert h using 1 ; congr 1 ; ring

theorem clean_mask_moment_le_dimension (β : ℝ) (hβ₀ : 0 ≤ β) (hβ₁ : β ≤ 1)
    (origin : E) (k dimW rs : ℕ) (hcard : Fintype.card E ≤ 2 ^ (dimW + rs)) :
    ((singletonSlopeLaw (E := E) β hβ₀ hβ₁).iid k).expectation
        (fun sample => (1 - (1 : ℝ) / 3600) ^
          maskCount (fun i => cleanBit origin (sample i))) ≤
      (1 - (β / (2 : ℝ) ^ (dimW + rs)) / 3600) ^ k := by
  apply clean_mask_moment_le β hβ₀ hβ₁ origin k
  exact_mod_cast hcard

theorem mask_rate_le_exp {x : ℝ} (hx : x ≤ 1) (k : ℕ) :
    (1 - x) ^ k ≤ Real.exp (-(k : ℝ) * x) := by
  calc
    (1 - x) ^ k ≤ Real.exp (-x) ^ k :=
      pow_le_pow_left₀ (sub_nonneg.mpr hx) (Real.one_sub_le_exp_neg x) k
    _ = Real.exp (-(k : ℝ) * x) := by
      rw [← Real.exp_nat_mul]
      congr 1
      ring

theorem clean_mask_moment_le_exp (β : ℝ) (hβ₀ : 0 ≤ β) (hβ₁ : β ≤ 1)
    (origin : E) (k : ℕ) {N : ℝ} (hcard : (Fintype.card E : ℝ) ≤ N) :
    ((singletonSlopeLaw (E := E) β hβ₀ hβ₁).iid k).expectation
        (fun sample => (1 - (1 : ℝ) / 3600) ^
          maskCount (fun i => cleanBit origin (sample i))) ≤
      Real.exp (-(k : ℝ) * (β / N) / 3600) := by
  have hN : 1 ≤ N := by
    have hc : (1 : ℝ) ≤ Fintype.card E := by
      exact_mod_cast Fintype.card_pos (α := E)
    exact hc.trans hcard
  have hq : β / N ≤ 1 := (div_le_iff₀ (by linarith)).mpr (by simpa using hβ₁.trans hN)
  have hx : β / N / 3600 ≤ 1 := (div_le_iff₀ (by norm_num)).mpr (by linarith)
  have h := (clean_mask_moment_le β hβ₀ hβ₁ origin k hcard).trans
    (mask_rate_le_exp hx k)
  simpa only [mul_div_assoc] using h

theorem cube_singleton_probability_nonneg (n : ℕ) : 0 ≤ (1 / (n : ℝ)) ^ 2 :=
  sq_nonneg _

theorem cube_singleton_probability_le_one {n : ℕ} (hn : 1 ≤ n) :
    (1 / (n : ℝ)) ^ 2 ≤ 1 := by
  have hn' : (1 : ℝ) ≤ n := by exact_mod_cast hn
  have hq₀ : 0 ≤ 1 / (n : ℝ) := div_nonneg zero_le_one (Nat.cast_nonneg _)
  have hq₁ : 1 / (n : ℝ) ≤ 1 :=
    (div_le_iff₀ (by linarith)).mpr (by simpa using hn')
  nlinarith [mul_nonneg hq₀ (sub_nonneg.mpr hq₁)]

theorem cube_mask_rate_le_exp {n : ℕ} (hn : 1 ≤ n) {N : ℝ} (hN : 1 ≤ N) :
    (1 - ((1 / (n : ℝ)) ^ 2 / N) / 3600) ^ (n ^ 3) ≤
      Real.exp (-(n : ℝ) / (3600 * N)) := by
  have hn₀ : (n : ℝ) ≠ 0 := by
    have hn' : (1 : ℝ) ≤ n := by exact_mod_cast hn
    linarith
  have hN₀ : N ≠ 0 := by linarith
  have hq : (1 / (n : ℝ)) ^ 2 / N ≤ 1 :=
    (div_le_iff₀ (by linarith)).mpr
      (by simpa using (cube_singleton_probability_le_one hn).trans hN)
  have hx : (1 / (n : ℝ)) ^ 2 / N / 3600 ≤ 1 :=
    (div_le_iff₀ (by norm_num)).mpr (by linarith)
  have he : -((n ^ 3 : ℕ) : ℝ) * ((1 / (n : ℝ)) ^ 2 / N / 3600) =
      -(n : ℝ) / (3600 * N) := by
    simp only [Nat.cast_pow]
    field_simp [hn₀, hN₀]

  calc
    _ ≤ Real.exp (-((n ^ 3 : ℕ) : ℝ) * ((1 / (n : ℝ)) ^ 2 / N / 3600)) :=
      mask_rate_le_exp hx (n ^ 3)
    _ = _ := congrArg Real.exp he

theorem exists_cube_exponential_lt {N error : ℝ} (hN : 0 < N) (herror : 0 < error) :
    ∃ n : ℕ, 1 ≤ n ∧ Real.exp (-(n : ℝ) / (3600 * N)) < error := by
  let rate := Real.exp (-(1 / (3600 * N)))
  have hrate₀ : 0 ≤ rate := (Real.exp_pos _).le
  have hrate₁ : rate < 1 := Real.exp_lt_one_iff.mpr
    (neg_lt_zero.mpr (one_div_pos.mpr (mul_pos (by norm_num) hN)))
  have ht := tendsto_pow_atTop_nhds_zero_of_lt_one hrate₀ hrate₁
  obtain ⟨m, hm⟩ := (ht.eventually (gt_mem_nhds herror)).exists
  refine ⟨m + 1, Nat.succ_le_succ (Nat.zero_le m), ?_⟩
  have hp : rate ^ (m + 1) < error :=
    lt_of_le_of_lt (by
      rw [pow_succ]
      exact mul_le_of_le_one_right (pow_nonneg hrate₀ m) hrate₁.le) hm
  have he : Real.exp (-((m + 1 : ℕ) : ℝ) / (3600 * N)) = rate ^ (m + 1) := by
    rw [← Real.exp_nat_mul]
    congr 1
    ring
  rwa [he]

theorem exists_cube_mask_rate_lt {N error : ℝ} (hN : 1 ≤ N) (herror : 0 < error) :
    ∃ n : ℕ, 1 ≤ n ∧
      (1 - ((1 / (n : ℝ)) ^ 2 / N) / 3600) ^ (n ^ 3) < error := by
  obtain ⟨n, hn, he⟩ := exists_cube_exponential_lt (by linarith : 0 < N) herror
  exact ⟨n, hn, (cube_mask_rate_le_exp hn hN).trans_lt he⟩

end
end DFVSGames.Clean

namespace DFVSGames.Clean.Experiment

open scoped BigOperators
open Foundations.Games
open Soundness
open Soundness.ConditionalIncidences Soundness.ConditionalSimulation
open Soundness.ConditionalGameLaw

noncomputable section

variable {R O N : Type} [Fintype R] [DecidableEq R]
  [Fintype O] [DecidableEq O] [Fintype N] [DecidableEq N]

abbrev Coefficient (R : Type) := ZeroInformation.Bits R
abbrev Additional (k : ℕ) (R : Type) := Coefficient R × (Fin k → Coefficient R × Coefficient R)
abbrev SparseSeed (k : ℕ) (R : Type) := Fin k → Bool × Coefficient R

def singletonSet {k : ℕ} (seed : SparseSeed k R) : Finset (Fin k) :=
  Finset.univ.filter fun i => (seed i).1 = true

def coefficients {k : ℕ} (extra : Additional k R) (seed : SparseSeed k R) :
    RawCoefficients (singletonSet seed) (Coefficient R) :=
  fun slot => match slot with
  | none => extra.1
  | some (.inl (i, j)) => if j = 0 then (extra.2 i.val).1 else (extra.2 i.val).2
  | some (.inr i) => (seed i.val).2

omit [DecidableEq R] in

theorem zeroSet_eq_cleanMask {k : ℕ} (extra : Additional k R) (seed : SparseSeed k R) :
    zeroSet (singletonSet seed) (coefficients extra seed) =
      Finset.univ.filter (fun i => cleanBit (ZeroInformation.zero : Coefficient R) (seed i) = true) := by
  classical
  ext i
  rw [mem_zeroSet]
  simp [singletonSet, coefficients, cleanBit]
  intro _
  rfl

omit [DecidableEq R] in
theorem zeroSet_card {k : ℕ} (extra : Additional k R) (seed : SparseSeed k R) :
    (zeroSet (singletonSet seed) (coefficients extra seed)).card =
      maskCount (fun i => cleanBit (ZeroInformation.zero : Coefficient R) (seed i)) := by
  rw [zeroSet_eq_cleanMask]
  rfl

abbrev LocalStrategies (k : ℕ) (R O N : Type) :=
  OuterStrategy (P := Fin k) (R := R) (O := O) (N := N)

def success (k : ℕ) (μ : FiniteDistribution (O × Fin 3))
    (g : IncidenceExtraction.Incidence O N)
    (extraLaw : FiniteDistribution (Additional k R))
    (β : ℝ) (hβ₀ : 0 ≤ β) (hβ₁ : β ≤ 1)
    (strategy : Additional k R → SparseSeed k R → LocalStrategies k R O N) : ℝ :=
  extraLaw.expectation fun extra =>
    ((singletonSlopeLaw (E := Coefficient R) β hβ₀ hβ₁).iid k).expectation fun seed =>
      UpperBound.fixedAdviceSuccess μ (singletonSet seed) g (coefficients extra seed)
        (strategy extra seed)

private theorem expectation_mono_inline_Experiment {Ω : Type*} [Fintype Ω]
    (μ : FiniteDistribution Ω) {f g : Ω → ℝ} (h : ∀ x, f x ≤ g x) :
    μ.expectation f ≤ μ.expectation g := by
  unfold FiniteDistribution.expectation
  exact Finset.sum_le_sum fun x _ => mul_le_mul_of_nonneg_left (h x) (μ.nonnegative x)

private theorem expectation_const_inline_Experiment {Ω : Type*} [Fintype Ω]
    (μ : FiniteDistribution Ω) (c : ℝ) : μ.expectation (fun _ => c) = c := by
  rw [FiniteDistribution.expectation, ← Finset.sum_mul, μ.normalized, one_mul]

theorem success_le_of_repetition (k : ℕ) (μ : FiniteDistribution (O × Fin 3))
    (g : IncidenceExtraction.Incidence O N)
    (extraLaw : FiniteDistribution (Additional k R))
    (β : ℝ) (hβ₀ : 0 ≤ β) (hβ₁ : β ≤ 1)
    (strategy : Additional k R → SparseSeed k R → LocalStrategies k R O N)
    (distinct : ∀ o i j, g.name o i = g.name o j → i = j)
    (ρ : ℝ)
    (rate : ∀ n, ((incidenceGame g (μ.pushforward (incidence g.name))).repetition n).value ≤ ρ ^ n) :
    success k μ g extraLaw β hβ₀ hβ₁ strategy ≤
      (1 - (β / (Fintype.card (Coefficient R) : ℝ)) * (1 - ρ)) ^ k := by
  unfold success
  calc
    _ ≤ extraLaw.expectation (fun _ =>
      (1 - (β / (Fintype.card (Coefficient R) : ℝ)) * (1 - ρ)) ^ k) := by
      apply expectation_mono_inline_Experiment
      intro extra
      calc
        _ ≤ ((singletonSlopeLaw (E := Coefficient R) β hβ₀ hβ₁).iid k).expectation
            (fun seed => ρ ^ maskCount (fun i => cleanBit (ZeroInformation.zero : Coefficient R) (seed i))) := by
          apply expectation_mono_inline_Experiment
          intro seed
          have bound := (UpperBound.fixed_advice_success_le_repeated_value μ
            (singletonSet seed) g (coefficients extra seed) (strategy extra seed) distinct).trans (rate _)
          simpa only [zeroSet_card] using bound
        _ = _ := clean_mask_moment β hβ₀ hβ₁ (ZeroInformation.zero : Coefficient R) k ρ
    _ = _ := expectation_const_inline_Experiment _ _

theorem randomized_success_le_of_repetition {Seed : Type*} [Fintype Seed]
    (k : ℕ) (μ : FiniteDistribution (O × Fin 3))
    (g : IncidenceExtraction.Incidence O N)
    (extraLaw : FiniteDistribution (Additional k R))
    (β : ℝ) (hβ₀ : 0 ≤ β) (hβ₁ : β ≤ 1)
    (seedLaw : FiniteDistribution Seed)
    (strategy : Seed → Additional k R → SparseSeed k R → LocalStrategies k R O N)
    (distinct : ∀ o i j, g.name o i = g.name o j → i = j)
    (ρ : ℝ)
    (rate : ∀ n, ((incidenceGame g (μ.pushforward (incidence g.name))).repetition n).value ≤ ρ ^ n) :
    seedLaw.expectation (fun seed => success k μ g extraLaw β hβ₀ hβ₁ (strategy seed)) ≤
      (1 - (β / (Fintype.card (Coefficient R) : ℝ)) * (1 - ρ)) ^ k := by
  calc
    _ ≤ seedLaw.expectation (fun _ =>
      (1 - (β / (Fintype.card (Coefficient R) : ℝ)) * (1 - ρ)) ^ k) :=
      expectation_mono_inline_Experiment _ (fun seed =>
        success_le_of_repetition k μ g extraLaw β hβ₀ hβ₁ (strategy seed) distinct ρ rate)
    _ = _ := expectation_const_inline_Experiment _ _

end
end DFVSGames.Clean.Experiment

namespace DFVSGames.Soundness.SubsetIntersection

open scoped BigOperators

def subsets (k t : ℕ) : Finset (Finset (Fin k)) :=
  (Finset.univ : Finset (Fin k)).powersetCard t

def uniformMean (k t : ℕ) (f : Finset (Fin k) → ℚ) : ℚ :=
  (∑ a ∈ subsets k t, f a) / (subsets k t).card

def independentMean (k t : ℕ) (f : Finset (Fin k) → Finset (Fin k) → ℚ) : ℚ :=
  uniformMean k t fun a => uniformMean k t fun b => f a b

def memberIndicator {k : ℕ} (i : Fin k) (a : Finset (Fin k)) : ℚ :=
  if i ∈ a then 1 else 0

def overlapProbability (k t : ℕ) : ℚ :=
  independentMean k t fun a b => if (a ∩ b).Nonempty then 1 else 0

theorem independentMean_eq_double_sum (k t : ℕ)
    (f : Finset (Fin k) → Finset (Fin k) → ℚ) :
    independentMean k t f =
      (∑ a ∈ subsets k t, ∑ b ∈ subsets k t, f a b) / ((subsets k t).card : ℚ)^2 := by
  unfold independentMean uniformMean
  simp only [div_eq_mul_inv, ← Finset.sum_mul, pow_two, mul_inv_rev]
  ring

theorem uniformMean_mul_left (k t : ℕ) (c : ℚ) (f : Finset (Fin k) → ℚ) :
    uniformMean k t (fun a => c * f a) = c * uniformMean k t f := by
  unfold uniformMean
  rw [← Finset.mul_sum]
  ring

theorem uniformMean_mul_right (k t : ℕ) (c : ℚ) (f : Finset (Fin k) → ℚ) :
    uniformMean k t (fun a => f a * c) = uniformMean k t f * c := by
  unfold uniformMean
  rw [← Finset.sum_mul]
  ring

theorem independentMean_product (k t : ℕ) (f g : Finset (Fin k) → ℚ) :
    independentMean k t (fun a b => f a * g b) = uniformMean k t f * uniformMean k t g := by
  simp only [independentMean, uniformMean_mul_left, uniformMean_mul_right]

theorem subsets_card (k t : ℕ) : (subsets k t).card = k.choose t := by
  simp [subsets]

theorem subsets_nonempty {k t : ℕ} (htk : t ≤ k) : (subsets k t).Nonempty := by
  apply Finset.powersetCard_nonempty.mpr
  simpa using htk

theorem subsets_card_ne_zero {k t : ℕ} (htk : t ≤ k) :
    ((subsets k t).card : ℚ) ≠ 0 := by
  exact_mod_cast (Finset.card_ne_zero.mpr (subsets_nonempty htk))

theorem count_containing {k t : ℕ} (ht : 0 < t) (i : Fin k) :
    ((subsets k t).filter (fun a => i ∈ a)).card =
      (k - 1).choose (t - 1) := by
  simpa [subsets] using
    (Finset.card_filter_powersetCard_subset ({i} : Finset (Fin k)) Finset.univ t
      (by simp) (by simpa using Nat.succ_le_of_lt ht))

theorem choose_predecessor_ratio {k t : ℕ} (hk : 0 < k) (ht : 0 < t) (htk : t ≤ k) :
    ((k - 1).choose (t - 1) : ℚ) / k.choose t = (t : ℚ) / k := by
  have hk0 : (k : ℚ) ≠ 0 := by exact_mod_cast (Nat.ne_of_gt hk)
  have hc0 : (k.choose t : ℚ) ≠ 0 := by exact_mod_cast (Nat.ne_of_gt (Nat.choose_pos htk))
  have hrel : k * (k - 1).choose (t - 1) = k.choose t * t := by
    simpa [Nat.sub_add_cancel hk, Nat.sub_add_cancel ht] using
      (Nat.add_one_mul_choose_eq (k - 1) (t - 1))
  apply (div_eq_div_iff hc0 hk0).2
  exact_mod_cast (by simpa [Nat.mul_comm] using hrel)

theorem inclusion_probability {k t : ℕ} (hk : 0 < k) (htk : t ≤ k) (i : Fin k) :
    uniformMean k t (memberIndicator i) = (t : ℚ) / k := by
  by_cases ht : t = 0
  · subst t
    simp [uniformMean, subsets, memberIndicator]
  · have htpos : 0 < t := Nat.pos_of_ne_zero ht
    unfold uniformMean memberIndicator
    rw [Finset.sum_boole, count_containing htpos i, subsets_card]
    exact choose_predecessor_ratio hk htpos htk

theorem independent_inclusion_probability {k t : ℕ} (hk : 0 < k) (htk : t ≤ k) (i : Fin k) :
    independentMean k t (fun a b => memberIndicator i a * memberIndicator i b) =
      ((t : ℚ) / k)^2 := by
  rw [independentMean_product, inclusion_probability hk htk]
  ring

theorem uniformMean_congr {k t : ℕ} {f g : Finset (Fin k) → ℚ}
    (h : ∀ a ∈ subsets k t, f a = g a) : uniformMean k t f = uniformMean k t g := by
  unfold uniformMean
  rw [Finset.sum_congr rfl h]

theorem uniformMean_const {k t : ℕ} (htk : t ≤ k) (c : ℚ) :
    uniformMean k t (fun _ => c) = c := by
  have hn := subsets_card_ne_zero htk
  simp only [uniformMean, Finset.sum_const, nsmul_eq_mul]
  field_simp

theorem uniformMean_finset_sum {k t : ℕ} {α : Type*} (s : Finset α)
    (f : α → Finset (Fin k) → ℚ) :
    uniformMean k t (fun a => ∑ i ∈ s, f i a) =
      ∑ i ∈ s, uniformMean k t (f i) := by
  unfold uniformMean
  rw [Finset.sum_comm]
  simp only [div_eq_mul_inv, Finset.sum_mul]

theorem uniformMean_mono {k t : ℕ} {f g : Finset (Fin k) → ℚ}
    (h : ∀ a ∈ subsets k t, f a ≤ g a) : uniformMean k t f ≤ uniformMean k t g := by
  apply div_le_div_of_nonneg_right (Finset.sum_le_sum h)
  exact Nat.cast_nonneg _

theorem independentMean_mono {k t : ℕ} {f g : Finset (Fin k) → Finset (Fin k) → ℚ}
    (h : ∀ a ∈ subsets k t, ∀ b ∈ subsets k t, f a b ≤ g a b) :
    independentMean k t f ≤ independentMean k t g := by
  apply uniformMean_mono
  intro a ha
  apply uniformMean_mono
  exact h a ha

theorem intersection_card_indicator {k : ℕ} (a b : Finset (Fin k)) :
    ((a ∩ b).card : ℚ) = ∑ i ∈ a, memberIndicator i b := by
  simp [memberIndicator]

theorem expected_intersection_fixed {k t : ℕ} (hk : 0 < k) (htk : t ≤ k)
    (a : Finset (Fin k)) :
    uniformMean k t (fun b => ((a ∩ b).card : ℚ)) = a.card * ((t : ℚ) / k) := by
  simp_rw [intersection_card_indicator]
  rw [uniformMean_finset_sum]
  simp [inclusion_probability hk htk, nsmul_eq_mul]

theorem expected_intersection {k t : ℕ} (hk : 0 < k) (htk : t ≤ k) :
    independentMean k t (fun a b => ((a ∩ b).card : ℚ)) = (t : ℚ)^2 / k := by
  unfold independentMean
  simp_rw [expected_intersection_fixed hk htk]
  calc
    uniformMean k t (fun a => a.card * ((t : ℚ) / k)) =
        uniformMean k t (fun _ => (t : ℚ) * ((t : ℚ) / k)) := by
      apply uniformMean_congr
      intro a ha
      have hcard : a.card = t := (Finset.mem_powersetCard.mp ha).2
      rw [hcard]
    _ = (t : ℚ) * ((t : ℚ) / k) := uniformMean_const htk _
    _ = (t : ℚ)^2 / k := by ring

theorem nonempty_indicator_le_card {k : ℕ} (a b : Finset (Fin k)) :
    (if (a ∩ b).Nonempty then (1 : ℚ) else 0) ≤ (a ∩ b).card := by
  split_ifs with h
  · exact_mod_cast (Finset.card_pos.mpr h)
  · exact Nat.cast_nonneg _

theorem overlap_probability_le {k t : ℕ} (hk : 0 < k) (htk : t ≤ k) :
    overlapProbability k t ≤ (t : ℚ)^2 / k := by
  calc
    overlapProbability k t ≤ independentMean k t (fun a b => ((a ∩ b).card : ℚ)) :=
      independentMean_mono (fun a _ b _ => nonempty_indicator_le_card a b)
    _ = (t : ℚ)^2 / k := expected_intersection hk htk

end DFVSGames.Soundness.SubsetIntersection

namespace DFVSGames.Soundness.PartnerSampling

open scoped BigOperators
open SubsetIntersection (subsets)

abbrev ActiveSet (k t : ℕ) := ↥(subsets k t)

abbrev Partner (k t : ℕ) := Σ a : ActiveSet k t, (↥a.val → Fin 3)

theorem activeSet_card {k t : ℕ} (a : ActiveSet k t) : a.val.card = t :=
  (Finset.mem_powersetCard.mp a.property).2

theorem slotTuples_card {k t : ℕ} (a : ActiveSet k t) :
    Fintype.card (↥a.val → Fin 3) = 3 ^ t := by
  simp [activeSet_card a]

theorem partner_card (k t : ℕ) :
    Fintype.card (Partner k t) = (subsets k t).card * 3 ^ t := by
  rw [Fintype.card_sigma]
  simp only [slotTuples_card, Finset.sum_const, Finset.card_univ, nsmul_eq_mul,
    Fintype.card_coe, Nat.cast_id]

theorem partner_nonempty {k t : ℕ} (htk : t ≤ k) : Nonempty (Partner k t) := by
  obtain ⟨a, ha⟩ := SubsetIntersection.subsets_nonempty htk
  exact ⟨⟨⟨a, ha⟩, fun _ => 0⟩⟩

theorem uniformMean_real_cast (k t : ℕ) (f : Finset (Fin k) → ℚ) :
    (SubsetIntersection.uniformMean k t f : ℝ) =
      (𝔼 a ∈ subsets k t, (f a : ℝ)) := by
  rw [Finset.expect_eq_sum_div_card]
  simp [SubsetIntersection.uniformMean]

theorem independentMean_real_cast (k t : ℕ)
    (f : Finset (Fin k) → Finset (Fin k) → ℚ) :
    (SubsetIntersection.independentMean k t f : ℝ) =
      (𝔼 a ∈ subsets k t, 𝔼 b ∈ subsets k t, (f a b : ℝ)) := by
  simp only [SubsetIntersection.independentMean, uniformMean_real_cast]

theorem subset_overlap_probability_le {k t : ℕ} (hk : 0 < k) (htk : t ≤ k) :
    (𝔼 a ∈ subsets k t, 𝔼 b ∈ subsets k t,
      if (a ∩ b).Nonempty then (1 : ℝ) else 0) ≤ (t : ℝ)^2 / k := by
  have h : (SubsetIntersection.overlapProbability k t : ℝ) ≤ (t : ℝ)^2 / k := by
    have h := SubsetIntersection.overlap_probability_le hk htk
    have hh := (Rat.cast_le (K := ℝ)).mpr h
    simpa using hh
  simpa only [SubsetIntersection.overlapProbability, independentMean_real_cast,
    apply_ite, Rat.cast_one, Rat.cast_zero] using h

theorem activeSet_expect (k t : ℕ) (f : Finset (Fin k) → ℝ) :
    (𝔼 a : ActiveSet k t, f a.val) = (𝔼 a ∈ subsets k t, f a) := by
  simp only [Finset.expect_eq_sum_div_card, Finset.card_univ, Fintype.card_coe,
    Finset.sum_coe_sort]

theorem partner_expect_activeSet (k t : ℕ) (f : ActiveSet k t → ℝ) :
    (𝔼 p : Partner k t, f p.1) = (𝔼 a : ActiveSet k t, f a) := by
  have hsum : (∑ p : Partner k t, f p.1) = (3 : ℝ)^t * ∑ a : ActiveSet k t, f a := by
    rw [Fintype.sum_sigma]
    simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul, slotTuples_card,
      Nat.cast_pow, Nat.cast_ofNat]
    rw [Finset.mul_sum]
  rw [Fintype.expect_eq_sum_div_card, Fintype.expect_eq_sum_div_card, hsum, partner_card]
  simp only [Nat.cast_mul, Nat.cast_pow, Nat.cast_ofNat, Fintype.card_coe]
  have hc : (3 : ℝ)^t ≠ 0 := pow_ne_zero _ (by norm_num)
  simpa only [mul_comm] using
    (mul_div_mul_left (∑ a : ActiveSet k t, f a) ((subsets k t).card : ℝ) hc)

theorem partner_expect_active (k t : ℕ) (f : Finset (Fin k) → ℝ) :
    (𝔼 p : Partner k t, f p.1.val) = (𝔼 a ∈ subsets k t, f a) := by
  rw [partner_expect_activeSet k t (fun a => f a.val), activeSet_expect]

theorem partner_pair_expect_active (k t : ℕ)
    (f : Finset (Fin k) → Finset (Fin k) → ℝ) :
    (𝔼 p : Partner k t, 𝔼 q : Partner k t, f p.1.val q.1.val) =
      (𝔼 a ∈ subsets k t, 𝔼 b ∈ subsets k t, f a b) := by
  calc
    _ = 𝔼 p : Partner k t, 𝔼 b ∈ subsets k t, f p.1.val b := by
      apply Finset.expect_congr rfl
      intro p _
      exact partner_expect_active k t (fun b => f p.1.val b)
    _ = _ := partner_expect_active k t (fun a => 𝔼 b ∈ subsets k t, f a b)

theorem partner_active_overlap_probability_le {k t : ℕ} (hk : 0 < k) (htk : t ≤ k) :
    (𝔼 p : Partner k t, 𝔼 q : Partner k t,
      if (p.1.val ∩ q.1.val).Nonempty then (1 : ℝ) else 0) ≤ (t : ℝ)^2 / k := by
  rw [partner_pair_expect_active k t (fun a b => if (a ∩ b).Nonempty then (1 : ℝ) else 0)]
  exact subset_overlap_probability_le hk htk

def active {k t : ℕ} (p : Partner k t) (j : Fin k) : Bool :=
  decide (j ∈ p.1.val)

def decodeSlot (i : Fin 3) : PartnerProjection.Slot :=
  if i.val = 0 then .first else if i.val = 1 then .second else .third

def slots {k t : ℕ} (p : Partner k t) (j : Fin k) : PartnerProjection.Slot :=
  if hj : j ∈ p.1.val then decodeSlot (p.2 ⟨j, hj⟩) else .first

noncomputable def kernel {k t : ℕ} (rhs : Fin k → Bool) (p : Partner k t) :
    Submodule (ZMod 2) (PartnerProjection.SourcePoint rhs) :=
  (PartnerLinear.projection rhs (active p) (slots p)).ker

def activeSubtypeEquiv {k t : ℕ} (p : Partner k t) :
    PartnerLinear.Active (active p) ≃ ↥p.1.val :=
  Equiv.subtypeEquivRight (fun j => by simp [active])

theorem kernel_finrank {k t : ℕ} (rhs : Fin k → Bool) (p : Partner k t) :
    Module.finrank (ZMod 2) (kernel rhs p) = t := by
  rw [kernel, PartnerLinear.projection_ker_finrank]
  calc
    Fintype.card (PartnerLinear.Active (active p)) = Fintype.card ↥p.1.val :=
      Fintype.card_congr (activeSubtypeEquiv p)
    _ = t := by simpa using activeSet_card p.1

theorem kernel_inf_finrank_le {k t : ℕ} (rhs : Fin k → Bool) (p q : Partner k t) :
    Module.finrank (ZMod 2) ↥(kernel rhs p ⊓ kernel rhs q : Submodule (ZMod 2) (PartnerProjection.SourcePoint rhs)) ≤ t := by
  calc
    Module.finrank (ZMod 2) ↥(kernel rhs p ⊓ kernel rhs q : Submodule (ZMod 2) (PartnerProjection.SourcePoint rhs)) ≤
        Module.finrank (ZMod 2) (kernel rhs p) := Submodule.finrank_mono inf_le_left
    _ = t := kernel_finrank rhs p

theorem kernel_inf_eq_bot_of_no_overlap {k t : ℕ}
    (rhs : Fin k → Bool) (p q : Partner k t)
    (h : ¬ (p.1.val ∩ q.1.val).Nonempty) :
    kernel rhs p ⊓ kernel rhs q = ⊥ := by
  apply PartnerLinear.disjoint_projection_kernels
    rhs (active p) (slots p) (active q) (slots q)
  intro j hj
  have hp : j ∈ p.1.val := by simpa [active] using hj
  have hq : j ∉ q.1.val := by
    intro hq
    exact h ⟨j, Finset.mem_inter.mpr ⟨hp, hq⟩⟩
  simp [active, hq]

def activeOverlap {k t : ℕ} (p q : Partner k t) : ℝ :=
  if (p.1.val ∩ q.1.val).Nonempty then 1 else 0

noncomputable def kernelCollision {k t : ℕ}
    (rhs : Fin k → Bool) (p q : Partner k t) : ℝ := by
  classical
  exact if kernel rhs p ⊓ kernel rhs q ≠ ⊥ then 1 else 0

theorem kernelCollision_le_activeOverlap {k t : ℕ}
    (rhs : Fin k → Bool) (p q : Partner k t) :
    kernelCollision rhs p q ≤ activeOverlap p q := by
  classical
  by_cases h : (p.1.val ∩ q.1.val).Nonempty
  · simp only [activeOverlap, ite_eq_left h, kernelCollision]
    split_ifs <;> norm_num
  · have hb := kernel_inf_eq_bot_of_no_overlap rhs p q h
    simp [kernelCollision, activeOverlap, h, hb]

theorem kernel_collision_probability_le {k t : ℕ}
    (hk : 0 < k) (htk : t ≤ k) (rhs : Fin k → Bool) :
    (𝔼 p : Partner k t, 𝔼 q : Partner k t,
      kernelCollision rhs p q) ≤ (t : ℝ)^2 / k := by
  calc
    (𝔼 p : Partner k t, 𝔼 q : Partner k t, kernelCollision rhs p q) ≤
        (𝔼 p : Partner k t, 𝔼 q : Partner k t, activeOverlap p q) := by
      apply Finset.expect_le_expect
      intro p _
      apply Finset.expect_le_expect
      intro q _
      exact kernelCollision_le_activeOverlap rhs p q
    _ ≤ (t : ℝ)^2 / k := partner_active_overlap_probability_le hk htk

theorem kernel_overlap_weight_pointwise {k t : ℕ}
    (rhs : Fin k → Bool) (m : ℕ) (p q : Partner k t) :
    (2 : ℝ) ^ (m * Module.finrank (ZMod 2) ↥(kernel rhs p ⊓ kernel rhs q : Submodule (ZMod 2) (PartnerProjection.SourcePoint rhs))) ≤
      1 + activeOverlap p q * (2 : ℝ) ^ (m * t) := by
  have hcap :
      (2 : ℝ) ^ (m * Module.finrank (ZMod 2) ↥(kernel rhs p ⊓ kernel rhs q : Submodule (ZMod 2) (PartnerProjection.SourcePoint rhs))) ≤
        (2 : ℝ) ^ (m * t) := by
    apply pow_le_pow_right₀ (by norm_num : (1 : ℝ) ≤ 2)
    exact Nat.mul_le_mul_left m (kernel_inf_finrank_le rhs p q)
  by_cases h : (p.1.val ∩ q.1.val).Nonempty
  · simp only [activeOverlap, ite_eq_left h, one_mul]
    linarith
  · have hb := kernel_inf_eq_bot_of_no_overlap rhs p q h
    rw [hb]
    simp [activeOverlap, h]

theorem kernel_overlap_weight_le {k t : ℕ}
    (hk : 0 < k) (htk : t ≤ k) (rhs : Fin k → Bool) (m : ℕ) :
    (𝔼 p : Partner k t, 𝔼 q : Partner k t,
      (2 : ℝ) ^ (m * Module.finrank (ZMod 2) ↥(kernel rhs p ⊓ kernel rhs q : Submodule (ZMod 2) (PartnerProjection.SourcePoint rhs)))) ≤
      1 + ((t : ℝ)^2 / k) * (2 : ℝ) ^ (m * t) := by
  let : Nonempty (Partner k t) := partner_nonempty htk
  have hmean :
      (𝔼 p : Partner k t, 𝔼 q : Partner k t,
        (1 + activeOverlap p q * (2 : ℝ) ^ (m * t))) =
      1 + (𝔼 p : Partner k t, 𝔼 q : Partner k t, activeOverlap p q) *
        (2 : ℝ) ^ (m * t) := by
    simp_rw [Finset.expect_add_distrib, ← Finset.expect_mul, Fintype.expect_const]
  calc
    (𝔼 p : Partner k t, 𝔼 q : Partner k t,
      (2 : ℝ) ^ (m * Module.finrank (ZMod 2) ↥(kernel rhs p ⊓ kernel rhs q : Submodule (ZMod 2) (PartnerProjection.SourcePoint rhs)))) ≤
        (𝔼 p : Partner k t, 𝔼 q : Partner k t,
          (1 + activeOverlap p q * (2 : ℝ) ^ (m * t))) := by
      apply Finset.expect_le_expect
      intro p _
      apply Finset.expect_le_expect
      intro q _
      exact kernel_overlap_weight_pointwise rhs m p q
    _ = 1 + (𝔼 p : Partner k t, 𝔼 q : Partner k t, activeOverlap p q) *
        (2 : ℝ) ^ (m * t) := hmean
    _ ≤ 1 + ((t : ℝ)^2 / k) * (2 : ℝ) ^ (m * t) := by
      apply add_le_add le_rfl
      apply mul_le_mul_of_nonneg_right
      · exact partner_active_overlap_probability_le hk htk
      · exact pow_nonneg (by norm_num) _

end DFVSGames.Soundness.PartnerSampling

end OAI