/- 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 CryptoDefs.Blocks public import CryptoDefs.Foundations public import CryptoDefs.Problem public import Mathlib.Algebra.BigOperators.Group.Finset.Defs public import Mathlib.Algebra.Group.Int.Defs public import Mathlib.Data.Fintype.Basic public import Mathlib.Logic.Equiv.Fin.Basic public import Mathlib.Order.Filter.AtTopBot.Defs public import Mathlib.Order.Filter.Map public import Mathlib.SetTheory.Cardinal.Finite public import Mathlib.Analysis.SpecialFunctions.Pow.Real public import Mathlib.Data.Fintype.Pi public import Mathlib.Data.Fintype.Prod public import Mathlib.Data.Nat.Prime.Defs public import Mathlib.LinearAlgebra.Matrix.NonsingularInverse public import Mathlib.LinearAlgebra.Matrix.PosDef public import Mathlib.Algebra.Order.Round public import Mathlib.Data.Fintype.Sigma public import Mathlib.Data.Rat.Floor public import Mathlib.Tactic.DeriveFintype /-! # Paper 1: cases of the main theorems, with numbers of fixed width Eight short statements. Each names a problem, a time and a probability of success, and says that one quantum program achieves them. The first four follow from printed theorems, and the text before each names the full statement. The other four add Module-LWE with square matrices and make a paragraph of Section 12.5 concrete. The file reads the machine and Mathlib only. It defines how numbers are written, then the problems and the schemes; the statements stand at the end. Differences from the print: `docs/departures.md`. -/ @[expose] public section /-! ## Numbers of fixed width How the problems write matrices, and the words in which the statements speak of programs and times. ### Integer matrices in memory An entry takes `κ` machine words (`64 κ` bits, two's complement, low word first), and entries stand row after row. An element of `ℤ[X]/(Xⁿ + 1)` is its row of `n` coefficients. -/ namespace QL.Minimal open Crypto /-- The words of `M`; exact if every entry lies in `[-2^(64 κ - 1), 2^(64 κ - 1))`. -/ def write (κ : ℕ) {r c : ℕ} (M : Fin r → Fin c → ℤ) : List Word := toWords (pack fun i : Fin (r * c) => BitVec.ofInt (wordBits * κ) (M i.divNat i.modNat)) /-- The matrix that `x` spells: entry `(i, j)` is block `c i + j`. -/ def read (κ : ℕ) {r c : ℕ} (x : BitVec (wordBits * κ * (r * c))) (i : Fin r) (j : Fin c) : ℤ := (blocks x (finProdFinEquiv (i, j))).toInt /-- The product in `ℤ[X]/(Xⁿ + 1)`; the index `t - i` is taken modulo `n`. -/ def negacyclic {n : ℕ} (f g : Fin n → ℤ) (t : Fin n) : ℤ := ∑ i, if i ≤ t then f i * g (t - i) else -(f i * g (t - i)) /-- The product `A v` of a matrix with coefficients in `[0, q)` and a vector, over `ℤ[X]/(Xⁿ + 1)`. -/ def negacyclicMulVec {n q d k : ℕ} (A : Fin d → Fin k → Fin n → Fin q) (v : Fin k → Fin n → ℤ) (i : Fin d) (t : Fin n) : ℤ := ∑ j, negacyclic (fun u => (A i j u : ℕ)) (v j) t /-- The words of `q` and then of `A`, entry after entry, with `κ` words a number. -/ def writeModulusAndMatrix (κ : ℕ) {n q d k : ℕ} (A : Fin d → Fin k → Fin n → Fin q) : List Word := write κ (fun _ _ : Fin 1 => q) ++ write κ fun (row : Fin (d * k)) t => (A row.divNat row.modNat t : ℕ) end QL.Minimal /-! ### Programs and times -/ namespace QL.Minimal open Crypto /-- A program of the machine with gates, measurements and coins, and no oracle. -/ abbrev QuantumProgram : Type 1 := Prog (Adjoin Empty) Empty /-- «For all `n = 2^a` with `a` large enough». -/ def largePowersOfTwo : Filter ℕ := Filter.atTop.map (2 ^ ·) /-- «For all `x` but at most this fraction of them». -/ def ForAllButFraction (fraction : ℝ) {α : Type} [Fintype α] (holds : α → Prop) : Prop := Nat.card {x // ¬holds x} ≤ fraction * Fintype.card α namespace QuantumProgram variable (program : QuantumProgram) (problem : Problem Empty Empty) (told : List ℕ) (time : ℕ) /-- Told these numbers, the program wins on every instance within `time` steps with probability at least `2/3`, over its coins and its measurements. A run starts on no qubit, and a qubit costs a step when it is taken. -/ def SolvesEveryInstance : Prop := ∀ x, 2 / 3 ≤ problem.successProb program told time x /-- The same on all instances but at most this fraction of them. -/ def SolvesAllInstancesButFraction [Fintype problem.Inst] (fraction : ℝ) : Prop := ForAllButFraction fraction fun x => 2 / 3 ≤ problem.successProb program told time x end QuantumProgram end QL.Minimal /-! ## The lattice problems ### Rotations of `ℤ^m`; vectors of norm one, from the Gram matrix alone -/ namespace QL.Minimal open Crypto Matrix /-- The number of bits of a square matrix with `entryWords` machine words an entry. -/ abbrev squareBits (rank entryWords : ℕ) : ℕ := wordBits * entryWords * (rank * rank) /-- The square integer matrix that `x` spells. -/ abbrev squareMatrix {rank entryWords : ℕ} (x : BitVec (squareBits rank entryWords)) : Matrix (Fin rank) (Fin rank) ℤ := of (read entryWords x) open Classical in /-- The lattice isomorphism problem for `ℤ^m`, `m = rank`: given `G = Bᵀ B` with `B ∈ GL_m(ℤ)`, find `U ∈ GL_m(ℤ)` with `Uᵀ G U = 1`. An entry of `U` has `β ^ outputDegree` words, where `β` is the number of bits of `G`. -/ noncomputable def orthonormalBasisOfRotation (rank entryWords outputDegree : ℕ) : Problem Empty Empty where ar := .empty Inst := {G : BitVec (squareBits rank entryWords) // ∃ B, IsUnit B ∧ squareMatrix G = Bᵀ * B} input G := toWords G.1 oracle _ := nofun outLen := squareBits rank (squareBits rank entryWords ^ outputDegree) score G _ U := winScore (IsUnit (squareMatrix U) ∧ (squareMatrix U)ᵀ * squareMatrix G.1 * squareMatrix U = 1) /-- The Gram matrix `G` of a basis of an integral lattice; each entry fits in `entryWords` machine words. -/ structure GramMatrix (rank entryWords : ℕ) : Type where G : Matrix (Fin rank) (Fin rank) ℤ posDef : (G.map (↑) : Matrix (Fin rank) (Fin rank) ℚ).PosDef fits : ∀ i j, |G i j| < 2 ^ (wordBits * entryWords - 1) /-- The number of bits in which a Gram matrix is written. -/ def gramMatrixBits (rank entryWords : ℕ) : ℕ := wordBits * entryWords * rank ^ 2 open Classical in /-- The problem: given `G`, write `rank` vectors as the rows of `Y`, with `gramMatrixBits ^ outputDegree` words an entry, among which every `v ∈ ℤ^rank` with `vᵀ G v = 1` occurs up to sign. -/ noncomputable def unitVectorsFromGramMatrix (rank entryWords outputDegree : ℕ) : Problem Empty Empty where ar := .empty Inst := GramMatrix rank entryWords input x := write entryWords x.G oracle _ := nofun outLen := wordBits * gramMatrixBits rank entryWords ^ outputDegree * (rank * rank) score x _ Y := winScore (∀ v, v ⬝ᵥ x.G *ᵥ v = 1 → ∃ i, read _ Y i = v ∨ read _ Y i = -v) end QL.Minimal /-! ### Module-SIS; search Module-LWE with square matrices and bounded coefficients -/ namespace QL.Minimal open Crypto /-- `y ≠ 0`, `A y = 0` modulo `q`, and the Euclidean norm of all coefficients of `y` is at most `bound`. -/ def IsShortKernelVector {n rows columns q : ℕ} (A : Fin rows → Fin columns → Fin n → Fin q) (bound : ℝ) (y : Fin columns → Fin n → ℤ) : Prop := y ≠ 0 ∧ (∀ i t, (q : ℤ) ∣ negacyclicMulVec A y i t) ∧ √(∑ j, ∑ t, (y j t : ℝ) ^ 2) ≤ bound open Classical in /-- Module-SIS over `R_q = ℤ_q[X]/(Xⁿ + 1)`. Instance: a matrix `A` over `R_q`. Input: `q` and `A`, with `n` words a number. The output, the rows of `y` with `n + columns n` words a number, wins if `y` is a short vector of the kernel. -/ noncomputable abbrev moduleSIS (n rows columns q : ℕ) (bound : ℝ) : Problem Empty Empty where ar := .empty Inst := Fin rows → Fin columns → Fin n → Fin q input := writeModulusAndMatrix n oracle := nofun outLen := wordBits * (n + columns * n) * (columns * n) score A _ y := winScore (IsShortKernelVector A bound (read (n + columns * n) y)) /-- The bound `columns · n · q^(rows / columns)`. -/ noncomputable def normBound (n rows columns q : ℕ) : ℝ := columns * n * (q : ℝ) ^ (rows / columns : ℝ) /-- Vectors of `rank` elements of `ℤ[X]/(Xⁿ + 1)` with coefficients in `[-noise, noise]`. -/ abbrev BoundedVector (n rank noise : ℕ) : Type := Fin rank → Fin n → Finset.Icc (-noise : ℤ) noise /-- Search Module-LWE at a square matrix `A` over `R_q = ℤ_q[X]/(Xⁿ + 1)`. Instance: `s` and `e` with coefficients in `[-noise, noise]`. Input: `q`, `A` and `A s + e mod q`, each number in `n` words. The output wins if it is `s`. -/ noncomputable abbrev moduleLWE {n rank q : ℕ} (A : Fin rank → Fin rank → Fin n → Fin q) (noise : ℕ) : Problem Empty Empty where ar := .empty Inst := BoundedVector n rank noise × BoundedVector n rank noise input := fun (s, e) => writeModulusAndMatrix n A ++ write n fun i t => (negacyclicMulVec A (s · ·) i t + e i t) % q oracle := nofun outLen := wordBits * n * (rank * n) score := fun (s, _) _ y => winScore (∀ j t, read n y j t = s j t) end QL.Minimal /-! ### Search Module-LWE of any rank -/ namespace QL.Minimal.ModuleLWEGeneral open Crypto Real /-- The machine words of one coefficient: room for `q` and a sign bit. -/ def width (q : ℕ) : ℕ := bitLen q / wordBits + 1 /-- Search Module-LWE of rank `d` with `m` samples. An instance is `A ∈ R_q^(m×d)` and a seed for each coefficient of `s` and `e`; the coefficient is `χ` of its seed. The input is `q`, `A` and `A s + e mod q`. The output is `1 + d` rows. The first row is a mark (ones, so that an answer differs from the zero word, «failure») and does not count; the output wins if the other rows are `s`. -/ noncomputable abbrev problem (n d m q : ℕ) {Seed : Type} (χ : Seed → ℤ) : Problem Empty Empty where ar := .empty Inst := (Fin m → Fin d → Fin n → Fin q) × (Fin d → Fin n → Seed) × (Fin m → Fin n → Seed) input := fun (A, s, e) => writeModulusAndMatrix (width q) A ++ write (width q) fun i t => (negacyclicMulVec A (fun j u => χ (s j u)) i t + χ (e i t)) % q oracle := nofun outLen := wordBits * width q * ((1 + d) * n) score := fun (_, s, _) _ y => winScore (∀ j t, read (width q) y (j.natAdd 1) t = χ (s j t)) /-- The degree `n = 2^a`, the rank `d`, the number `m` of samples, the modulus `q`, and the widths `σ` and `B σ`. -/ structure Parameters : Type where (a d m q : ℕ) (σ B : ℝ) namespace Parameters variable (x : Parameters) /-- The degree. -/ abbrev n : ℕ := 2 ^ x.a /-- The rank of the lattice over the ring. -/ abbrev k : ℕ := x.d + x.m /-- The hypotheses of Theorem 9.1 on the numbers. -/ structure Admissible : Prop where prime : x.q.Prime rank : 1 ≤ x.d samples : 1 ≤ x.m bound : 1 ≤ x.B largeModulus : 2 * (10 * (log x.n + 2)) ^ x.k ≤ (x.q : ℝ) ^ ((x.d * x.m : ℝ) / x.k) smallNoise : 3 * x.σ * √(x.k * x.n) ≤ (x.q : ℝ) ^ ((x.m : ℝ) / x.k) /-- `|χ| ≤ B σ` and `𝔼 χ² ≤ σ²` for a uniform seed. -/ structure IsSampler {Seed : Type} [Fintype Seed] [Nonempty Seed] (χ : Seed → ℤ) : Prop where bounded : ∀ w, |(χ w : ℝ)| ≤ x.B * x.σ moment : Ex (uniform Seed) (fun w => (χ w : ℝ) ^ 2) ≤ x.σ ^ 2 /-- The numbers that the program is told. -/ def told : List ℕ := [x.n, width x.q, x.d, x.m] /-- `2^(C (k² log n + k⁴)²) β^C`, where `β` is the number of bits of the input. -/ def time (C : ℕ) : ℕ := 2 ^ (C * (x.k ^ 2 * x.a + x.k ^ 4) ^ 2) * (wordBits * width x.q * (1 + x.m * x.d * x.n + x.m * x.n)) ^ C /-- The bound of Theorem 9.1 for the fraction of bad instances. -/ noncomputable def badFraction : ℝ := 13 * (3 / 2) ^ x.k * log (2 * x.q) * exp (-x.n / (10 * x.B ^ 4)) end Parameters end QL.Minimal.ModuleLWEGeneral /-! ## Schemes like K-PKE, the encryption scheme inside ML-KEM (Section 12.5) Polynomials are rows of coefficients. The matrix `A` is uniform and is given in full, and the bits of the sampler are uniform, as when the hash functions of FIPS 203 are random oracles. -/ namespace QL.Minimal.MLKEMLike open Crypto /-! ### Keys -/ /-- The numbers of a parameter set of FIPS 203 other than the degree `n` and the modulus `q`. In the families `dv` is taken literally; `du` only bounds the rounding step of `u`: `q / 2^uBits ≤ 3329 / 2^du`. -/ structure ParameterSet : Type where (rank η₁ η₂ du dv : ℕ) /-- ML-KEM-512, ML-KEM-768, ML-KEM-1024. -/ def parameterSets : List ParameterSet := [⟨2, 3, 2, 10, 4⟩, ⟨3, 2, 2, 10, 4⟩, ⟨4, 2, 2, 11, 5⟩] /-- Two strings of `η` bits. -/ structure SamplerBits (η : ℕ) : Type where (first second : Fin η → Bool) deriving Fintype /-- The centered binomial sample: the ones in the first string minus the ones in the second. -/ def centeredBinomial {η : ℕ} (bits : SamplerBits η) : ℤ := ∑ j, (bits.first j).toInt - ∑ j, (bits.second j).toInt /-- The randomness of key generation: `A` over `R_q = ℤ_q[X]/(Xⁿ + 1)`, and bits for each coefficient of `s`, `e`. -/ structure KeyCoins (p : ParameterSet) (n q : ℕ) : Type where A : Fin p.rank → Fin p.rank → Fin n → Fin q (secretBits errorBits : Fin p.rank → Fin n → SamplerBits p.η₁) deriving Fintype namespace KeyCoins variable {p : ParameterSet} {n q : ℕ} (key : KeyCoins p n q) /-- The secret key. -/ abbrev s (i : Fin p.rank) (j : Fin n) : ℤ := centeredBinomial (key.secretBits i j) /-- The error in the public key. -/ abbrev e (i : Fin p.rank) (j : Fin n) : ℤ := centeredBinomial (key.errorBits i j) /-- `t = A s + e mod q`. -/ abbrev t (i : Fin p.rank) (j : Fin n) : ℤ := (negacyclicMulVec key.A key.s i j + key.e i j) % q /-- The public key: `q`, `A` and `t`, each number in `n` words. -/ abbrev publicKey : List Word := writeModulusAndMatrix n key.A ++ write n key.t end KeyCoins /-- Key recovery. Input: the public key. The output wins if it is the secret key `s`. -/ noncomputable abbrev keyRecovery (p : ParameterSet) (n q : ℕ) : Problem Empty Empty where ar := .empty Inst := KeyCoins p n q input key := key.publicKey oracle := nofun outLen := wordBits * n * (p.rank * n) score key _ y := winScore (∀ i j, read n y i j = key.s i j) /-! ### Ciphertexts -/ /-- `Compress_d` of FIPS 203: `⌈(2^d / q) x⌋ mod 2^d`, halves rounded up. -/ def compress (q d : ℕ) (x : ℤ) : ℤ := round (2 ^ d * x / q : ℚ) % 2 ^ d /-- `Decompress_1` of FIPS 203: `0` or `⌈q / 2⌋`. -/ def decompressBit (q : ℕ) (bit : Bool) : ℤ := if bit then round (q / 2 : ℚ) else 0 /-- The randomness of encryption: bits for each coefficient of `y`, `e₁`, `e₂`. -/ structure EncryptionCoins (p : ParameterSet) (n : ℕ) : Type where yBits : Fin p.rank → Fin n → SamplerBits p.η₁ e₁Bits : Fin p.rank → Fin n → SamplerBits p.η₂ e₂Bits : Fin n → SamplerBits p.η₂ deriving Fintype namespace EncryptionCoins variable {p : ParameterSet} {n q : ℕ} (coins : EncryptionCoins p n) (key : KeyCoins p n q) /-- The secret of the encryption. -/ abbrev y (i : Fin p.rank) (j : Fin n) : ℤ := centeredBinomial (coins.yBits i j) /-- The error in `u`. -/ abbrev e₁ (i : Fin p.rank) (j : Fin n) : ℤ := centeredBinomial (coins.e₁Bits i j) /-- The error in `v`. -/ abbrev e₂ (j : Fin n) : ℤ := centeredBinomial (coins.e₂Bits j) /-- `u = Compress(Aᵀ y + e₁)`, to `uBits` bits. -/ abbrev u (uBits : ℕ) (i : Fin p.rank) (j : Fin n) : ℤ := compress q uBits (negacyclicMulVec (fun i i' => key.A i' i) coins.y i j + coins.e₁ i j) /-- `v = Compress(tᵀ y + e₂ + Decompress_1(message))`, to `d_v` bits. -/ abbrev v (message : BitVec n) (j : Fin n) : ℤ := compress q p.dv (∑ i, negacyclic (key.t i) (coins.y i) j + coins.e₂ j + decompressBit q message[j]) end EncryptionCoins /-- Message recovery. Input: the public key, the number `uBits` and the ciphertext `(u, v)`, each number in `n` words. The output, `n` bits, wins if it is the message. -/ noncomputable abbrev messageRecovery (p : ParameterSet) (n q uBits : ℕ) (message : BitVec n) : Problem Empty Empty where ar := .empty Inst := KeyCoins p n q × EncryptionCoins p n input := fun (key, coins) => key.publicKey ++ write n (fun _ _ : Fin 1 => uBits) ++ write n (coins.u key uBits) ++ write n fun _ : Fin 1 => coins.v key message oracle := nofun outLen := n score _ _ output := winScore (output = message) end QL.Minimal.MLKEMLike /-! ## The keys of schemes like ML-DSA (Section 12.5) Polynomials are rows of coefficients. The matrix `A` is uniform and is given in full, and the coefficients of `s₁`, `s₂` are uniform, as when the hash functions of FIPS 204 are random oracles. -/ namespace QL.Minimal.MLDSALike open Crypto /-- The numbers of a parameter set of FIPS 204 that key generation reads, other than `n` and `q`. -/ structure ParameterSet : Type where (rows columns η : ℕ) /-- ML-DSA-44, ML-DSA-65, ML-DSA-87. -/ def parameterSets : List ParameterSet := [⟨4, 4, 2⟩, ⟨6, 5, 4⟩, ⟨8, 7, 2⟩] /-- The randomness of key generation: `A` over `R_q = ℤ_q[X]/(Xⁿ + 1)`, and `η - s₁`, `η - s₂` with coefficients in `[0, 2 η]`. -/ structure KeyCoins (p : ParameterSet) (n q : ℕ) : Type where A : Fin p.rows → Fin p.columns → Fin n → Fin q first : Fin p.columns → Fin n → Fin (2 * p.η + 1) second : Fin p.rows → Fin n → Fin (2 * p.η + 1) deriving Fintype namespace KeyCoins variable {p : ParameterSet} {n q : ℕ} (key : KeyCoins p n q) /-- The secret. -/ abbrev s₁ (i : Fin p.columns) (j : Fin n) : ℤ := p.η - (key.first i j : ℕ) /-- The error. -/ abbrev s₂ (i : Fin p.rows) (j : Fin n) : ℤ := p.η - (key.second i j : ℕ) /-- `t = A s₁ + s₂ mod q`. -/ abbrev t (i : Fin p.rows) (j : Fin n) : ℤ := (negacyclicMulVec key.A key.s₁ i j + key.s₂ i j) % q /-- The high part of Power2Round: `t = 2^13 t₁ + t₀` with `-2^12 < t₀ ≤ 2^12`. -/ abbrev t₁ (i : Fin p.rows) (j : Fin n) : ℤ := (key.t i j + 2 ^ 12 - 1) / 2 ^ 13 end KeyCoins /-- Key recovery. Input: the public key `q`, `A`, `t₁`, each number in `n` words. The output wins if it is `s₁`. -/ noncomputable abbrev keyRecovery (p : ParameterSet) (n q : ℕ) : Problem Empty Empty where ar := .empty Inst := KeyCoins p n q input key := writeModulusAndMatrix n key.A ++ write n key.t₁ oracle := nofun outLen := wordBits * n * (p.columns * n) score key _ y := winScore (∀ i j, read n y i j = key.s₁ i j) end QL.Minimal.MLDSALike namespace QL.Minimal open Crypto /-! ## Main theorems, in short cases ### Integral lattices Rotations of `ℤ^m`: a consequence of Theorem 1.1 (`Theorem_1_1` in `PartOne`). -/ /-- One quantum program finds an orthonormal basis of every rotation of `ℤ^m`, given by a Gram matrix of `β` bits, for every rank `m`, in time `2^(O(log² m)) β^(O(1))`. -/ def RotationTheorem : Prop := ∃ program : QuantumProgram, ∃ c outputDegree : ℕ, ∀ rank ≥ 1, ∀ entryWords ≥ 1, program.SolvesEveryInstance (orthonormalBasisOfRotation rank entryWords outputDegree) (told := [rank, entryWords]) (time := 2 ^ (c * rank.log2 ^ 2) * squareBits rank entryWords ^ c) /-! Vectors of norm one, from the Gram matrix alone: a consequence of Theorem 31.1 (`Theorem_31_1` in `PartFive`). -/ /-- One quantum program writes `rank` rows among which, up to sign, are all vectors of norm one, for every integral lattice of every rank, in time `2^(O(log² rank))` times a polynomial in the length of the input. -/ def UnitVectorsFromGramTheorem : Prop := ∃ program : QuantumProgram, ∃ c outputDegree : ℕ, ∀ rank ≥ 1, ∀ entryWords ≥ 1, program.SolvesEveryInstance (unitVectorsFromGramMatrix rank entryWords outputDegree) (told := [rank, entryWords]) (time := 2 ^ (c * rank.log2 ^ 2) * gramMatrixBits rank entryWords ^ c) /-! ### Module-SIS and Module-LWE Module-SIS: a consequence of Theorem D (a) (`Theorem_Da` in `PartTwo`). -/ /-- One quantum program, told the degree and the shape of the matrix, finds a nonzero kernel vector of norm at most `normBound` for every matrix in time `2^(O(columns⁸ log² n))`, for all shapes with more columns than rows, all powers of two `n ≥ 32` and all moduli `q ≤ 2ⁿ` above the bound (so that `(q, 0, …, 0)` is no solution). -/ def ModuleSISTheorem : Prop := ∃ program : QuantumProgram, ∃ c : ℕ, ∀ rows ≥ 1, ∀ columns > rows, ∀ n ≥ 32, n.isPowerOfTwo → ∀ q ≤ 2 ^ n, normBound n rows columns q < q → program.SolvesEveryInstance (moduleSIS n rows columns q (normBound n rows columns q)) (told := [n, rows, columns]) (time := 2 ^ (c * columns ^ 8 * n.log2 ^ 2)) /-! Search Module-LWE of any rank: a consequence of Theorem 9.1 (`Theorem_9_1` in `PartTwo`), for coefficients with one common law. -/ namespace ModuleLWEGeneral /-- One program and one constant `C` for all admissible numbers and samplers: the program halts on every input within `time C` steps and qubits, and it finds `s` with probability at least `(1 - badFraction) / 2`, over a uniform instance and its own coins and measurements. -/ def AllRanksTheorem : Prop := ∃ (P : QuantumProgram) (C : ℕ), ∀ (x : Parameters) [NeZero x.q] (Seed : Type) [Fintype Seed] [Nonempty Seed] (χ : Seed → ℤ), x.Admissible → x.IsSampler χ → P.Uses Arity.empty.withCoin x.told { time := x.time C, qubits := x.time C } ∧ (1 - x.badFraction) / 2 ≤ (problem x.n x.d x.m x.q χ).adv P x.told (x.time C) end ModuleLWEGeneral /-! ## Further theorems ### Search Module-LWE with square matrices and bounded coefficients By the algorithm of Theorem C (a) and Proposition 12.7. The sentence of the abstract is `Abstract_moduleLWE` in `PartTwo`. -/ /-- One quantum program, told the degree and the shape of `A`, finds the secret for all matrices `A` but a fraction `2^(-n / (240 rank))` and, at these, for every `s` and `e`, in time `2^(O(rank⁸ log² n))`, for all ranks, all powers of two `n` that are large for the rank, all noise bounds and all moduli `11 rank noise² n ≤ q ≤ 2ⁿ`. -/ def ModuleLWETheorem : Prop := ∃ program : QuantumProgram, ∃ c : ℕ, ∀ rank ≥ (1 : ℕ), ∀ᶠ n : ℕ in largePowersOfTwo, ∀ noise ≥ 1, ∀ q ∈ Set.Icc (11 * rank * noise ^ 2 * n) (2 ^ n), ForAllButFraction (2 ^ (-(n : ℝ) / (240 * rank))) fun A : Fin rank → Fin rank → Fin n → Fin q => program.SolvesEveryInstance (moduleLWE A noise) (told := [n, rank, rank]) (time := 2 ^ (c * rank ^ 8 * n.log2 ^ 2)) /-! ### Schemes like ML-KEM and ML-DSA (Section 12.5) The parameter sets of the standards, carried to large degrees `n`: keys and messages in quantum time `2^(O(log² n))`. The printed form is `Statement_parameterFamilies` in `PaperDetails.P01.PartTwo`. -/ namespace MLKEMLike /-- One quantum program, told the degree and the rank, which determines the parameter set, finds the secret key for all coins of key generation but a fraction `2^(-n / 2^17)`, in time `2^(O(log² n))`, for all parameter sets, all powers of two `n ≥ 2^29` and all primes `13 n ≤ q ≤ 2ⁿ` (the standard has `3329 = 13 · 256 + 1`). -/ def KeyRecoveryInFamiliesTheorem : Prop := ∃ program : QuantumProgram, ∃ c : ℕ, ∀ p ∈ parameterSets, ∀ n ≥ 2 ^ 29, n.isPowerOfTwo → ∀ q ∈ Set.Icc (13 * n) (2 ^ n), q.Prime → program.SolvesAllInstancesButFraction (keyRecovery p n q) (told := [n, p.rank]) (time := 2 ^ (c * n.log2 ^ 2)) (fraction := 2 ^ (-(n : ℝ) / 2 ^ 17)) /-! For ciphertexts, `dv` is taken literally; `du` only bounds the rounding step of `u`: `q / 2^uBits ≤ 3329 / 2^du`. The printed bounds for the rounding errors follow from this. -/ /-- One quantum program, told the degree and the rank, finds every message, each for all coins of key generation and encryption but a fraction `2^(-n / 2^19)`, in time `2^(O(log² n))`, for all parameter sets, all powers of two `n ≥ 2^30`, all primes `13 n ≤ q ≤ 2ⁿ` and all `uBits ≤ n` that compress `u` no more than the standard does. -/ def MessageRecoveryInFamiliesTheorem : Prop := ∃ program : QuantumProgram, ∃ c : ℕ, ∀ p ∈ parameterSets, ∀ n ≥ 2 ^ 30, n.isPowerOfTwo → ∀ q ∈ Set.Icc (13 * n) (2 ^ n), q.Prime → ∀ uBits ≤ n, 2 ^ p.du * q ≤ 3329 * 2 ^ uBits → ∀ message, program.SolvesAllInstancesButFraction (messageRecovery p n q uBits message) (told := [n, p.rank]) (time := 2 ^ (c * n.log2 ^ 2)) (fraction := 2 ^ (-(n : ℝ) / 2 ^ 19)) end MLKEMLike /-! For ML-DSA the output is `s₁`; `s₂ - t₀` follows from `s₁`, `A` and `t₁`. The printed condition on the second moments of the rounding errors follows from `q ≥ 32736 n`. -/ namespace MLDSALike /-- One quantum program, told the degree and the number of rows, which determines the parameter set, finds `s₁` for all coins of key generation but a fraction `2^(-n / 2^15)`, in time `2^(O(log² n))`, for all parameter sets, all powers of two `n ≥ 2^30` and all primes `32736 n ≤ q ≤ 2ⁿ` (the standard has `8380417 = 32736 · 256 + 1`). -/ def KeyRecoveryInFamiliesTheorem : Prop := ∃ program : QuantumProgram, ∃ c : ℕ, ∀ p ∈ parameterSets, ∀ n ≥ 2 ^ 30, n.isPowerOfTwo → ∀ q ∈ Set.Icc (32736 * n) (2 ^ n), q.Prime → program.SolvesAllInstancesButFraction (keyRecovery p n q) (told := [n, p.rows]) (time := 2 ^ (c * n.log2 ^ 2)) (fraction := 2 ^ (-(n : ℝ) / 2 ^ 15)) end MLDSALike end QL.Minimal