Coordinate-guessing recovery from a distinguisher
DefinitionCityLWE_RecoveryTestThe coordinate-guessing recovery algorithm, which turns a decision-LWE distinguisher into a search solver.
Fix a modulus , dimension , block size , a number of runs per test , a shift width , a guess-slot count , a threshold weight , and a distinguisher with coins. The input is a transcript with samples, split into consecutive column blocks of samples each, and a list of fair coins. Every run reads coins; run number reads coins . Its first coins give shift residues , each the value of bits (least significant first) reduced mod , and the remaining coins are the coins of one activation of , exactly as in the decision experiment.
- Planted test (test number , for a coordinate and a guess ): run replaces each sample of block by , using its own shift for that sample, and records the bit of on the transformed block.
- Reference test (test number ): run replaces each sample by .
Let and be the numbers of runs in which answers . Coordinate of the output is the least with , and if there is none.
recoverProb is the probability that this algorithm, with Ptot fair coins, outputs the secret of LWE samples with a uniform secret and errors drawn from .
import Definitions.Def_CityLWE_AgreementTest
set_option autoImplicit false
open QAlgorithms.Complexity PPSLattice
namespace CityLWE
/-- The residue coded by a bit string, least significant bit first, reduced modulo `q`. -/
def bitsZq (q : ℕ) (w : List Bool) : ZMod q := ((bitsVal w : ℕ) : ZMod q)
/-- The `m` shift residues of one run, read from its coins in blocks of `Lb` bits. -/
def runShift (q m Lb : ℕ) (c : List Bool) : Vec q m :=
fun j => bitsZq q ((c.drop ((j : ℕ) * Lb)).take Lb)
/-- Planting the guess `k` for coordinate `i`: column `j` becomes `(a_j + l_j eᵢ, b_j + l_j k)`. -/
def plant {q n m : ℕ} (i : Fin n) (k : ZMod q) (l : Vec q m) (A : Mat q n m) (b : Vec q m) :
Mat q n m × Vec q m :=
(Matrix.of fun i' j => A i' j + (if i' = i then l j else 0), fun j => b j + l j * k)
/-- The reference transformation: column `j` becomes `(a_j, b_j + l_j)`. -/
def smear {q n m : ℕ} (l : Vec q m) (A : Mat q n m) (b : Vec q m) : Mat q n m × Vec q m :=
(A, fun j => b j + l j)
/-- The bit returned by one activation of `D` on the transcript `(A, b)` with coins `dc`. -/
def distBit (q n : ℕ) {m : ℕ} (D : RestrictedDec.Adversary) (A : Mat q n m) (b : Vec q m)
(dc : List Bool) : Bool :=
(((BeyondDS.untup (D.f (BeyondDS.tup [encNat n, [], BeyondDS.tup [encMat q A, encVec q b],
dc]))).getD 1 []).take (D.p.eval n)).headD false
/-- Column block `j` of a matrix with `T·m` columns. -/
def blockA {q n T m : ℕ} (A : Mat q n (T * m)) (j : Fin T) : Mat q n m :=
Matrix.of fun i c => A i (finProdFinEquiv (j, c))
/-- Block `j` of a vector of length `T·m`. -/
def blockB {q T m : ℕ} (b : Vec q (T * m)) (j : Fin T) : Vec q m :=
fun c => b (finProdFinEquiv (j, c))
/-- The coins of run `r` when every run reads `R` coins. -/
def runCoins (R r : ℕ) (c : List Bool) : List Bool := (c.drop (r * R)).take R
/-- One run: read the shift from the first `m·Lb` coins, transform, and run `D` on the rest. -/
def runBit (q n m Lb : ℕ) (D : RestrictedDec.Adversary)
(tr : Vec q m → Mat q n m → Vec q m → Mat q n m × Vec q m)
(A : Mat q n m) (b : Vec q m) (rc : List Bool) : Bool :=
distBit q n D (tr (runShift q m Lb rc) A b).1 (tr (runShift q m Lb rc) A b).2 (rc.drop (m * Lb))
/-- The number of accepting runs of test number `u`: run `j` uses sample block `j` and the
coins of run `u·T + j`. -/
def testCount (q n m T Lb : ℕ) (D : RestrictedDec.Adversary)
(tr : Vec q m → Mat q n m → Vec q m → Mat q n m × Vec q m) (u : ℕ)
(A : Mat q n (T * m)) (b : Vec q (T * m)) (c : List Bool) : ℕ :=
(Finset.univ.filter fun j : Fin T =>
runBit q n m Lb D tr (blockA A j) (blockB b j)
(runCoins (m * Lb + D.p.eval n) (u * T + (j : ℕ)) c) = true).card
/-- The recovered secret. Test `i·Qs + k` plants the guess `k` in coordinate `i` and test `n·Qs`
is the reference. Coordinate `i` is the least `k < q` whose count differs from the reference by
more than `T / (4W)`, and `0` if there is none. -/
def recoverGuess (q n m T Lb Qs W : ℕ) (D : RestrictedDec.Adversary)
(A : Mat q n (T * m)) (b : Vec q (T * m)) (c : List Bool) : Vec q n :=
fun i => ((((List.range q).find? fun k : ℕ =>
decide (T < 4 * W * Int.natAbs
((testCount q n m T Lb D (plant i (k : ZMod q)) ((i : ℕ) * Qs + k) A b c : ℤ) -
(testCount q n m T Lb D smear (n * Qs) A b c : ℤ)))).getD 0 : ℕ) : ZMod q)
/-- The probability that the recovery algorithm, given `T·m` samples and `Ptot` fair coins,
outputs the secret. -/
noncomputable def recoverProb (q n m T Lb Qs W : ℕ) (D : RestrictedDec.Adversary) (Ptot : ℕ)
(χ : PMF (ZMod q)) : ℝ :=
(((joint q n (T * m) χ).bind fun w => (coinLaw Ptot).map fun c =>
decide (recoverGuess q n m T Lb Qs W D w.2.1 w.2.2 c = w.1)) true).toReal
end CityLWE