Download data/lean/OAI/Combinatorics/LowSpaceSubsetSum/Counting/NestedPrimes.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 11.2 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/LowSpaceSubsetSum/Counting/NestedPrimes.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/LowSpaceSubsetSum/Counting/NestedPrimes.lean
-
curl -L -o NestedPrimes.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/LowSpaceSubsetSum/Counting/NestedPrimes.lean
11.2 kB
| import OAI.Combinatorics.LowSpaceSubsetSum.Counting.PrimeHashCollision | |
| import OAI.Combinatorics.LowSpaceSubsetSum.Counting.ProductHash | |
| namespace OAI | |
| /-! | |
| # The at-most-three-factor nested prime construction | |
| An increment below four contributes the factor one. Larger increments use | |
| the actual prime range. Seed identities are retained in the product sampling | |
| space even when numerical factors coincide. This proves the size and | |
| collision bounds used in `alg:filter-bounds` without a coprimality premise. | |
| -/ | |
| namespace LowSpaceSubsetSum | |
| open scoped BigOperators | |
| def factorRange (ell : β) : Finset β := | |
| if ell < 4 then {1} else primeRange ell | |
| theorem factorRange_nonempty (ell : β) : (factorRange ell).Nonempty := by | |
| by_cases h : ell < 4 | |
| Β· simp [factorRange, h] | |
| Β· simpa [factorRange, h] using primeRange_nonempty (by omega : 4 β€ ell) | |
| theorem factor_size_bounds {ell u p : β} (hu : 1 β€ u) (hell : ell β€ u) | |
| (hp : p β factorRange ell) : | |
| (2 : β) ^ ell β€ (32 * u : β) * p β§ (p : β) β€ (2 : β) ^ ell := by | |
| by_cases h : ell < 4 | |
| Β· have hp1 : p = 1 := by simpa [factorRange, h] using hp | |
| subst p | |
| have hpow : (2 : β) ^ ell β€ 8 := by | |
| have hpowN : (2 : β) ^ ell β€ 2 ^ 3 := Nat.pow_le_pow_right (by decide) (by omega) | |
| exact_mod_cast hpowN | |
| have huR : (1 : β) β€ u := by exact_mod_cast hu | |
| constructor | |
| Β· norm_num only [Nat.cast_one, mul_one] | |
| linarith | |
| Β· simpa only [Nat.cast_one] using | |
| (one_le_powβ (by norm_num : (1 : β) β€ 2) : (1 : β) β€ 2 ^ ell) | |
| Β· have hprime : p β primeRange ell := by simpa [factorRange, h] using hp | |
| have hlower : (2 : β) ^ ell β€ (4 * ell : β) * p := by | |
| exact_mod_cast (Finset.mem_filter.mp hprime).2 | |
| have huR : (ell : β) β€ u := by exact_mod_cast hell | |
| have hpR : (0 : β) β€ p := Nat.cast_nonneg _ | |
| constructor | |
| Β· nlinarith | |
| Β· exact_mod_cast (primeRange_lt (by omega : 4 β€ ell) hprime).le | |
| theorem factor_divisor_probability_le {ell u d : β} (hu : 1 β€ u) (hell : ell β€ u) | |
| (hd : 0 < d) (hbits : d < 2 ^ (2 * u)) : | |
| (((factorRange ell).filter fun p => p β£ d).card : β) / (factorRange ell).card β€ | |
| 64 * (u : β) ^ 2 / (2 : β) ^ ell := by | |
| by_cases h : ell < 4 | |
| Β· have huR : (1 : β) β€ u := by exact_mod_cast hu | |
| have hpow : (2 : β) ^ ell β€ 8 := by | |
| have hpowN : (2 : β) ^ ell β€ 2 ^ 3 := Nat.pow_le_pow_right (by decide) (by omega) | |
| exact_mod_cast hpowN | |
| simp only [factorRange, h, βreduceIte, Finset.filter_singleton, one_dvd, | |
| βreduceIte, Finset.card_singleton, Nat.cast_one, div_self (by norm_num : (1 : β) β 0)] | |
| apply (le_div_iffβ (by positivity : (0 : β) < (2 : β) ^ ell)).mpr | |
| nlinarith | |
| Β· simp only [factorRange, h, βreduceIte] | |
| apply (prime_divisor_probability_le (by omega) hell hd hbits).trans | |
| apply div_le_div_of_nonneg_right _ (by positivity) | |
| nlinarith [sq_nonneg (u : β)] | |
| def tripleRange (a b c : β) : Finset ((β Γ β) Γ β) := | |
| (factorRange a ΓΛ’ factorRange b) ΓΛ’ factorRange c | |
| def tripleModulus (z : (β Γ β) Γ β) : β := z.1.1 * z.1.2 * z.2 | |
| theorem tripleRange_nonempty (a b c : β) : (tripleRange a b c).Nonempty := | |
| ((factorRange_nonempty a).product (factorRange_nonempty b)).product | |
| (factorRange_nonempty c) | |
| theorem cumulative_moduli_dvd (z : (β Γ β) Γ β) : | |
| z.1.1 β£ z.1.1 * z.1.2 β§ z.1.1 * z.1.2 β£ tripleModulus z := | |
| β¨dvd_mul_right _ _, dvd_mul_right _ _β© | |
| theorem triple_size_bounds {a b c u : β} (hu : 1 β€ u) | |
| (ha : a β€ u) (hb : b β€ u) (hc : c β€ u) | |
| {z : (β Γ β) Γ β} (hz : z β tripleRange a b c) : | |
| (2 : β) ^ (a + b + c) β€ (32 * u : β) ^ 3 * tripleModulus z β§ | |
| (tripleModulus z : β) β€ (2 : β) ^ (a + b + c) := by | |
| obtain β¨hab, hczβ© := Finset.mem_product.mp hz | |
| obtain β¨haz, hbzβ© := Finset.mem_product.mp hab | |
| obtain β¨ha0, ha1β© := factor_size_bounds hu ha haz | |
| obtain β¨hb0, hb1β© := factor_size_bounds hu hb hbz | |
| obtain β¨hc0, hc1β© := factor_size_bounds hu hc hcz | |
| rw [pow_add, pow_add] | |
| constructor | |
| Β· have hab0 := mul_le_mul ha0 hb0 (by positivity) (by positivity) | |
| have habc0 := mul_le_mul hab0 hc0 (by positivity) (by positivity) | |
| convert habc0 using 1 | |
| simp only [tripleModulus, Nat.cast_mul] | |
| ring | |
| Β· have hab1 := mul_le_mul ha1 hb1 (by positivity) (by positivity) | |
| have habc1 := mul_le_mul hab1 hc1 (by positivity) (by positivity) | |
| simpa only [tripleModulus, Nat.cast_mul] using habc1 | |
| theorem triple_divisor_probability_le {a b c u d : β} (hu : 1 β€ u) | |
| (ha : a β€ u) (hb : b β€ u) (hc : c β€ u) | |
| (hd : 0 < d) (hbits : d < 2 ^ (2 * u)) : | |
| (((tripleRange a b c).filter fun z => tripleModulus z β£ d).card : β) / | |
| (tripleRange a b c).card β€ | |
| (64 * (u : β) ^ 2) ^ 3 / (2 : β) ^ (a + b + c) := by | |
| have hab := product_divisor_probability_le (factorRange a) (factorRange b) | |
| id id d (factorRange_nonempty a) (factorRange_nonempty b) (by positivity) | |
| (factor_divisor_probability_le hu ha hd hbits) | |
| (factor_divisor_probability_le hu hb hd hbits) | |
| have habc := product_divisor_probability_le (factorRange a ΓΛ’ factorRange b) | |
| (factorRange c) (fun z => z.1 * z.2) id d | |
| ((factorRange_nonempty a).product (factorRange_nonempty b)) | |
| (factorRange_nonempty c) (by positivity) hab | |
| (factor_divisor_probability_le hu hc hd hbits) | |
| apply habc.trans_eq | |
| rw [pow_add, pow_add] | |
| ring | |
| /-- The real log scale differs from the total integer increments by less than | |
| one. All discarded increments and repeated factors are included. -/ | |
| theorem nested_size_bounds {a b c u : β} {L : β} (hu : 256 β€ u) | |
| (ha : a β€ u) (hb : b β€ u) (hc : c β€ u) | |
| (hL0 : (a + b + c : β) β€ L) (hL1 : L < (a + b + c : β) + 1) | |
| {z : (β Γ β) Γ β} (hz : z β tripleRange a b c) : | |
| (2 : β) ^ L / (u : β) ^ 5 β€ tripleModulus z β§ | |
| (tripleModulus z : β) β€ (2 : β) ^ L := by | |
| have huR : (256 : β) β€ u := by exact_mod_cast hu | |
| have hu0 : (0 : β) < u := by linarith | |
| obtain β¨hlower, hupperβ© := triple_size_bounds (by omega : 1 β€ u) ha hb hc hz | |
| have hpow : (2 : β) ^ L β€ 2 * (2 : β) ^ (a + b + c) := by | |
| have h := Real.rpow_le_rpow_of_exponent_le (by norm_num : (1 : β) β€ 2) hL1.le | |
| simpa only [Real.rpow_add (by norm_num : (0 : β) < 2), Real.rpow_one, | |
| Real.rpow_natCast, mul_comm] using h | |
| have hcoef : 2 * (32 * (u : β)) ^ 3 β€ (u : β) ^ 5 := by | |
| have hu2 : (256 : β) ^ 2 β€ (u : β) ^ 2 := | |
| pow_le_pow_leftβ (by norm_num) huR 2 | |
| nlinarith [mul_nonneg (by nlinarith : (0 : β) β€ u ^ 2 - 65536) | |
| (pow_nonneg hu0.le 3)] | |
| constructor | |
| Β· apply (div_le_iffβ (pow_pos hu0 5)).mpr | |
| have hscale := mul_le_mul_of_nonneg_right hcoef | |
| (Nat.cast_nonneg (tripleModulus z) : (0 : β) β€ tripleModulus z) | |
| exact hpow.trans ((mul_le_mul_of_nonneg_left hlower (by norm_num)).trans | |
| (by nlinarith [hscale])) | |
| Β· exact hupper.trans (by | |
| simpa only [Real.rpow_natCast] using | |
| Real.rpow_le_rpow_of_exponent_le (by norm_num : (1 : β) β€ 2) hL0) | |
| theorem nested_divisor_probability_le {a b c u d : β} {L : β} (hu : 256 β€ u) | |
| (ha : a β€ u) (hb : b β€ u) (hc : c β€ u) | |
| (hL1 : L < (a + b + c : β) + 1) | |
| (hd : 0 < d) (hbits : d < 2 ^ (2 * u)) : | |
| (((tripleRange a b c).filter fun z => tripleModulus z β£ d).card : β) / | |
| (tripleRange a b c).card β€ (u : β) ^ 10 / (2 : β) ^ L := by | |
| have huR : (256 : β) β€ u := by exact_mod_cast hu | |
| have hu0 : (0 : β) < u := by linarith | |
| have hpow : (2 : β) ^ L β€ 2 * (2 : β) ^ (a + b + c) := by | |
| have h := Real.rpow_le_rpow_of_exponent_le (by norm_num : (1 : β) β€ 2) hL1.le | |
| simpa only [Real.rpow_add (by norm_num : (0 : β) < 2), Real.rpow_one, | |
| Real.rpow_natCast, mul_comm] using h | |
| have hcoef : 2 * (64 * (u : β) ^ 2) ^ 3 β€ (u : β) ^ 10 := by | |
| have hu4 : (256 : β) ^ 4 β€ (u : β) ^ 4 := | |
| pow_le_pow_leftβ (by norm_num) huR 4 | |
| have hprod := mul_nonneg (by nlinarith : (0 : β) β€ u ^ 4 - 524288) | |
| (pow_nonneg hu0.le 6) | |
| nlinarith | |
| apply (triple_divisor_probability_le (by omega : 1 β€ u) ha hb hc hd hbits).trans | |
| apply (div_le_div_iffβ (by positivity) (Real.rpow_pos_of_pos (by norm_num) L)).mpr | |
| have hscale := mul_le_mul_of_nonneg_right hcoef | |
| (by positivity : (0 : β) β€ (2 : β) ^ (a + b + c)) | |
| exact (mul_le_mul_of_nonneg_left hpow (by positivity)).trans | |
| (by nlinarith [hscale]) | |
| noncomputable def tripleProbability (a b c : β) (_z : (β Γ β) Γ β) : β := | |
| 1 / (tripleRange a b c).card | |
| theorem tripleProbability_sum (a b c : β) : | |
| (β z β tripleRange a b c, tripleProbability a b c z) = 1 := by | |
| have h : ((tripleRange a b c).card : β) β 0 := by | |
| exact_mod_cast (tripleRange_nonempty a b c).card_pos.ne' | |
| simp [tripleProbability, h] | |
| /-- The collision-energy interface is supplied for the actual product | |
| sampler, under the same polynomial-bit difference bound as the single prime. -/ | |
| theorem nested_pairwise_collision_bound (X : Finset β€) {a b c u : β} {L : β} | |
| (hu : 256 β€ u) (ha : a β€ u) (hb : b β€ u) (hc : c β€ u) | |
| (hL1 : L < (a + b + c : β) + 1) | |
| (hbits : β x β X, β y β X, (x - y).natAbs < 2 ^ (2 * u)) : | |
| PairwiseCollisionBound (tripleRange a b c) (tripleProbability a b c) X | |
| (fun z x => x % (tripleModulus z : β€)) ((u : β) ^ 10 / (2 : β) ^ L) := by | |
| intro x hx y hy hxy | |
| have hd : 0 < (x - y).natAbs := Int.natAbs_pos.mpr (sub_ne_zero.mpr hxy) | |
| have hiff (z : (β Γ β) Γ β) : | |
| x % (tripleModulus z : β€) = y % (tripleModulus z : β€) β | |
| tripleModulus z β£ (x - y).natAbs := by | |
| rw [Int.emod_eq_emod_iff_emod_sub_eq_zero, β Int.dvd_iff_emod_eq_zero, | |
| Int.natCast_dvd] | |
| have heq : (β z β tripleRange a b c, tripleProbability a b c z * | |
| if x % (tripleModulus z : β€) = y % (tripleModulus z : β€) then (1 : β) else 0) = | |
| (((tripleRange a b c).filter fun z => tripleModulus z β£ (x - y).natAbs).card : β) / | |
| (tripleRange a b c).card := by | |
| simp_rw [hiff, mul_ite, mul_one, mul_zero] | |
| rw [β Finset.sum_filter] | |
| simp [tripleProbability, div_eq_mul_inv, mul_comm] | |
| rw [heq] | |
| exact nested_divisor_probability_le hu ha hb hc hL1 hd (hbits x hx y hy) | |
| theorem nested_occupied_probability (X : Finset β€) {a b c u : β} {L : β} | |
| (hu : 256 β€ u) (ha : a β€ u) (hb : b β€ u) (hc : c β€ u) | |
| (hL1 : L < (a + b + c : β) + 1) | |
| (hbits : β x β X, β y β X, (x - y).natAbs < 2 ^ (2 * u)) | |
| {T : β} (hT : 0 < T) : | |
| 1 - ((X.card : β) + ((u : β) ^ 10 / (2 : β) ^ L) * (X.card : β) ^ 2) / T β€ | |
| β z β (tripleRange a b c).filter | |
| (fun z => (X.card : β) ^ 2 / T β€ | |
| ((X.image (fun x => x % (tripleModulus z : β€))).card : β)), | |
| tripleProbability a b c z := by | |
| apply occupied_probability_of_collision_bound | |
| Β· intro z _ | |
| exact div_nonneg zero_le_one (Nat.cast_nonneg _) | |
| Β· exact tripleProbability_sum a b c | |
| Β· positivity | |
| Β· exact hT | |
| Β· exact nested_pairwise_collision_bound X hu ha hb hc hL1 hbits | |
| end LowSpaceSubsetSum | |
| end OAI | |