Download data/lean/OAI/NumberTheory/TwoPoint/Circuits/BitEncoding.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 6.11 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/NumberTheory/TwoPoint/Circuits/BitEncoding.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/NumberTheory/TwoPoint/Circuits/BitEncoding.lean
-
curl -L -o BitEncoding.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/NumberTheory/TwoPoint/Circuits/BitEncoding.lean
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 | |