math / data /lean /OAI /NumberTheory /TwoPoint /Circuits /BitEncoding.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
6.11 kB
import OAI.NumberTheory.TwoPoint.Bounds.CRTComparison
import OAI.NumberTheory.TwoPoint.Circuits.FourierCorrection
import Mathlib.Logic.Equiv.Fin.Basic
import Mathlib.Data.Fintype.EquivFin
import Mathlib.Algebra.BigOperators.Ring.Finset
namespace OAI
/-!
# Exact finite jitter encoding
For `Q = 2^B`, the integer code `⌊(r Q + j)/sβŒ‹`, with `j` uniform in
`{0,…,Q-1}`, has exactly the distribution of the first `B` binary digits
of `(r + U)/s` for uniform `U` in `[0,1)`. This finite construction avoids
continuous auxiliary probability spaces. Jointly uniform residue and
jitter give a uniform code. Decoding can fail for at most `s` jitter values
at each fixed residue, giving the stronger bound `s/Q`.
-/
open Finset
namespace TwoPointCorrelations
/-- Regroup the `s Q` equally likely elementary outcomes into `Q` blocks of
size `s`. -/
def jitterPermutation (s Q : β„•) : Fin s Γ— Fin Q ≃ Fin Q Γ— Fin s :=
finProdFinEquiv.trans ((finCongr (Nat.mul_comm s Q)).trans finProdFinEquiv.symm)
/-- Finite version of a uniformly jittered residue, encoded in `Q` cells. -/
def jitterCode {s Q : β„•} (r : Fin s) (j : Fin Q) : Fin Q :=
(jitterPermutation s Q (r, j)).1
lemma jitterCode_val {s Q : β„•} (r : Fin s) (j : Fin Q) :
(jitterCode r j).val = (j.val + Q * r.val) / s := rfl
/-- Exact averaging identity for uniform residue and independent jitter. -/
lemma jitterCode_sum (s Q : β„•) (F : Fin Q β†’ ℝ) :
(βˆ‘ r : Fin s, βˆ‘ j : Fin Q, F (jitterCode r j)) =
(s : ℝ) * βˆ‘ k : Fin Q, F k := by
rw [← Fintype.sum_prod_type']
change (βˆ‘ x : Fin s Γ— Fin Q,
(fun y : Fin Q Γ— Fin s => F y.1) (jitterPermutation s Q x)) = _
rw [(jitterPermutation s Q).sum_comp (fun y : Fin Q Γ— Fin s => F y.1),
Fintype.sum_prod_type]
simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul]
rw [← Finset.mul_sum]
/-- The induced code kernel sends the uniform residue law to the uniform
code law, exactly. -/
lemma jitterCode_uniform {s Q : β„•} (hs : 0 < s) (hQ : 0 < Q) (k : Fin Q) :
(βˆ‘ r : Fin s, (1 / (s : ℝ)) * finiteRandomKernel (@jitterCode s Q) r k) =
1 / (Q : ℝ) := by
let : Nonempty (Fin Q) := ⟨⟨0, hQ⟩⟩
have hs0 : (s : ℝ) β‰  0 := by exact_mod_cast hs.ne'
have hQ0 : (Q : ℝ) β‰  0 := by exact_mod_cast hQ.ne'
unfold finiteRandomKernel
rw [← Finset.mul_sum, ← Finset.sum_div]
rw [jitterCode_sum s Q (fun z => if z = k then (1 : ℝ) else 0)]
simp only [Finset.sum_ite_eq', Finset.mem_univ, ite_true, Fintype.card_fin]
field_simp
/-- Decode the residue from the left endpoint of a code cell. -/
def jitterDecode {s Q : β„•} (hs : 0 < s) (hQ : 0 < Q) (k : Fin Q) : Fin s :=
⟨s * k.val / Q, (Nat.div_lt_iff_lt_mul hQ).mpr
(Nat.mul_lt_mul_of_pos_left k.isLt hs)⟩
/-- All jitter positions outside the first `s` positions decode correctly. -/
lemma jitterDecode_code {s Q : β„•} (hs : 0 < s) (hQ : 0 < Q)
(r : Fin s) (j : Fin Q) (hj : s ≀ j.val) :
jitterDecode hs hQ (jitterCode r j) = r := by
apply Fin.ext
change s * (jitterCode r j).val / Q = r.val
rw [jitterCode_val]
have hmod := Nat.mod_lt (j.val + Q * r.val) hs
have hsplit := Nat.mod_add_div (j.val + Q * r.val) s
have hmod0 := Nat.zero_le ((j.val + Q * r.val) % s)
apply Nat.div_eq_of_lt_le
Β· nlinarith
Β· have hjQ := j.isLt
nlinarith
/-- Conditional decoding error for every fixed residue, independent of the
law from which that residue was sampled. -/
theorem jitterDecode_error {s Q : β„•} (hs : 0 < s) (hQ : 0 < Q) (r : Fin s) :
(βˆ‘ j : Fin Q, if jitterDecode hs hQ (jitterCode r j) β‰  r then (1 : ℝ) else 0) /
(Q : ℝ) ≀ (s : ℝ) / Q := by
let E : Finset (Fin Q) := Finset.univ.filter
(fun j => jitterDecode hs hQ (jitterCode r j) β‰  r)
have hsmall (j : E) : j.val.val < s := by
have he := (Finset.mem_filter.mp j.property).2
by_contra h
exact he (jitterDecode_code hs hQ r j.val (Nat.le_of_not_gt h))
let emb : E β†’ Fin s := fun j => ⟨j.val.val, hsmall j⟩
have hinj : Function.Injective emb := by
intro j k h
apply Subtype.ext
apply Fin.ext
simpa only [emb] using congrArg (fun z : Fin s => z.val) h
have hcard : E.card ≀ s := by
simpa using Fintype.card_le_of_injective emb hinj
have hsum : (βˆ‘ j : Fin Q,
if jitterDecode hs hQ (jitterCode r j) β‰  r then (1 : ℝ) else 0) = (E.card : ℝ) := by
exact Finset.sum_boole _ _
rw [hsum]
apply div_le_div_of_nonneg_right _ (Nat.cast_nonneg Q)
exact_mod_cast hcard
/-- Any fixed bijective labeling of the `2^B` cells by `B` bits suffices:
the decoded equalities are represented by their truth tables. -/
noncomputable def binaryCellEquiv (B : β„•) : Fin (2 ^ B) ≃ BooleanCube B :=
Fintype.equivOfCardEq (by simp [BooleanCube])
noncomputable def jitterBits {s B : β„•} (r : Fin s) (j : Fin (2 ^ B)) : BooleanCube B :=
binaryCellEquiv B (jitterCode r j)
noncomputable def decodeBits {s B : β„•} (hs : 0 < s) (x : BooleanCube B) : Fin s :=
jitterDecode hs (by positivity) ((binaryCellEquiv B).symm x)
@[simp] lemma decode_jitterBits {s B : β„•} (hs : 0 < s) (r : Fin s) (j : Fin (2 ^ B)) :
decodeBits hs (jitterBits r j) = jitterDecode hs (by positivity) (jitterCode r j) := by
simp [decodeBits, jitterBits]
/-- Exact independent uniformity of the encoded bits under uniform residues. -/
lemma jitterBits_uniform {s B : β„•} (hs : 0 < s) (x : BooleanCube B) :
(βˆ‘ r : Fin s, (1 / (s : ℝ)) * finiteRandomKernel (@jitterBits s B) r x) =
1 / (2 ^ B : β„•) := by
have heq (r : Fin s) : finiteRandomKernel (@jitterBits s B) r x =
finiteRandomKernel (@jitterCode s (2 ^ B)) r ((binaryCellEquiv B).symm x) := by
unfold finiteRandomKernel
congr 1
apply Finset.sum_congr rfl
intro j _
have heq : jitterBits r j = x ↔ jitterCode r j = (binaryCellEquiv B).symm x := by
change binaryCellEquiv B (jitterCode r j) = x ↔ _
exact (binaryCellEquiv B).eq_symm_apply.symm
simp only [heq]
simp_rw [heq]
exact jitterCode_uniform hs (by positivity) _
end TwoPointCorrelations
end OAI