/- Copyright (c) 2026 Anthropic, PBC. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. SPDX-License-Identifier: Apache-2.0 -/ module public import AppVariablesStatement public import VariableBound.Field.Defs public import PaddedDescent.F.Fuel /-! # Step 2 of Appendix D as a program: the search for the checks Β«Algorithm: start with the empty list; while X(𝓛) > 0, replace 𝓛 by the list (𝓛, Ξ“_u), u ∈ Ξ©, with the smallest Ξ₯; then output F(𝓛).Β» A check is a list of numbers of clauses, a list of checks a list of such lists. The program is generic in the function `evX` that returns X(𝓛) and in the list `fam` of the candidates Ξ“_u; proof/variables/VariableBound/Count puts in the sums over the networks of Step 3 and the family of Lemma 13. Β«The number Ξ₯ is a rational with polynomially many bits, so the comparisons are exactΒ»: all candidates of one round have the same number of checks, so their Ξ₯ have a common denominator, and the program compares the numerators, which are integers (`upsNum`). The loop runs rΜ„ rounds and does nothing once X(𝓛) = 0. The list is cut off by the pad after every round, so that the program is polynomial-time in the pad with no side condition; the cut-off does nothing if the pad is long enough. This file has definitions only. The statements are propositions; proof/variables/VariableBound/Search proves them. -/ @[expose] public section namespace VariableBound open Groundwork PaddedDescent.F AppVariables /-- A list of checks, given by numbers of clauses, as a list of sets of clauses. -/ def chksD (m : β„•) (L : List (List β„•)) : List (Finset (Fin m)) := L.map (Fld.setD m) /-- Ξ£_E 2^load(E). -/ def loadSum (m : β„•) (L : List (List β„•)) : β„• := ((List.range m).map fun E => 2 ^ (L.filter fun l => l.contains E).length).sum /-- The numerator of Ξ₯(𝓛) over the denominator 3^rΒ·2^(Ξ²βˆ’1)Β·2^tΜ„Β·(15nΜ„)^(rΜ„βˆ’r), for a list 𝓛 of r ≀ rΜ„ checks with X(𝓛) = x. -/ def upsNum (m nb Ξ² rb tb : β„•) (x : β„€) (L : List (List β„•)) : β„€ := 4 ^ L.length * 2 ^ tb * (15 * nb) ^ (rb - L.length) * x + 3 ^ L.length * 2 ^ (Ξ² - 1) * (15 * nb + 16) ^ (rb - L.length) * loadSum m L /-- The first member of the list with the smallest key; the default [] on the empty list. -/ def argminD (key : List β„• β†’ β„€) (fam : List (List β„•)) : List β„• := (fam.foldl (fun (best : Option (List β„• Γ— β„€)) l => match best with | none => some (l, key l) | some b => if key l < b.2 then some (l, key l) else some b) none).elim [] fun b => b.1 /-- One round: nothing if X(𝓛) = 0; otherwise the candidate with the smallest Ξ₯ is appended. -/ def roundD (evX : List (List β„•) β†’ β„€) (fam : List (List β„•)) (m nb Ξ² rb tb : β„•) (L : List (List β„•)) : List (List β„•) := if evX L = 0 then L else L ++ [argminD (fun l => upsNum m nb Ξ² rb tb (evX (L ++ [l])) (L ++ [l])) fam] /-- The search: rΜ„ rounds from the empty list. -/ def searchD (pad : U) (evX : List (List β„•) β†’ β„€) (fam : List (List β„•)) (m nb Ξ² rb tb : β„•) : List (List β„•) := (List.range rb).foldl (fun L _ => cap pad [] (roundD evX fam m nb Ξ² rb tb L)) (cap pad [] []) /-- The conditions of Step 2 of Appendix D on a list of checks, here given by numbers: r ≀ rΜ„ checks, each an increasing list of at most sΜ„ = 16m/nΜ„ numbers below m, and load(E) ≀ tΜ„ + 2 for every clause E. -/ def StructD (m nb rb tb : β„•) (L : List (List β„•)) : Prop := L.length ≀ rb ∧ (βˆ€ l ∈ L, l.Pairwise (Β· < Β·) ∧ (βˆ€ E ∈ l, E < m) ∧ nb * l.length ≀ 16 * m) ∧ βˆ€ E < m, (L.filter fun l => l.contains E).length ≀ tb + 2 /-- The search is right. X has at most 2^n elements; Ξ², rΜ„, tΜ„ are the parameters of Β«Setting and parametersΒ» in Appendix D, for m clauses and nΜ„ β‰₯ 1; the candidates are increasing lists of numbers below m, the list of candidates is not empty, and they have (a), (b), (c) of Lemma 13 for v = nΜ„; `evX` returns X(𝓛) on every list that satisfies `StructD`; the word of every such list fits under the pad. Then the list found satisfies `StructD` and X = 0. -/ def StSearchD : Prop := βˆ€ (X : Type) [Fintype X] (m : β„•) (V : X β†’ Finset (Fin m)) (n nb rb tb : β„•) (pad : U) (evX : List (List β„•) β†’ β„€) (fam : List (List β„•)), Fintype.card X ≀ 2 ^ n β†’ 1 ≀ nb β†’ IsRb (beta n m nb) rb β†’ IsTb m nb rb tb β†’ fam β‰  [] β†’ (βˆ€ l ∈ fam, l.Pairwise (Β· < Β·) ∧ βˆ€ E ∈ l, E < m) β†’ IsFamily nb (Finset.univ : Finset (Fin fam.length)) (fun i => Fld.setD m (fam.get i)) β†’ (βˆ€ L, StructD m nb rb tb L β†’ evX L = Xsum V nb (chksD m L)) β†’ (βˆ€ L, StructD m nb rb tb L β†’ len L ≀ pad.length) β†’ StructD m nb rb tb (searchD pad evX fam m nb (beta n m nb) rb tb) ∧ Xsum V nb (chksD m (searchD pad evX fam m nb (beta n m nb) rb tb)) = 0 /-- The search is polynomial-time in the pad, jointly in everything it reads, if `evX` is; m, nΜ„, Ξ², rΜ„, tΜ„ are given in unary. -/ def StCertSearchD : Prop := βˆ€ (Ξ± : Type) [Enc Ξ±] (pad : Ξ± β†’ U) (evX : Ξ± β†’ List (List β„•) β†’ β„€) (fam : Ξ± β†’ List (List β„•)) (m nb Ξ² rb tb : Ξ± β†’ β„•), PolyT pad β†’ (PolyT fun p : Ξ± Γ— List (List β„•) => evX p.1 p.2) β†’ PolyT fam β†’ Unary m β†’ Unary nb β†’ Unary Ξ² β†’ Unary rb β†’ Unary tb β†’ PolyT fun x => searchD (pad x) (evX x) (fam x) (m x) (nb x) (Ξ² x) (rb x) (tb x) end VariableBound