Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
130 commits
Select commit Hold shift + click to select a range
6094ec9
feat(exactTriangle): relate lopsided queries and matrix products
edemaine Oct 28, 2026
dec963a
feat(exactTriangle): prove modular counts and the selected-prime bound
edemaine Oct 28, 2026
0d8da3e
doc(threeSum): cite the lopsided triangle paper and clarify adaptatio…
edemaine Oct 28, 2026
8d80ecc
doc(bibliography): sort the new paper entry
edemaine Oct 28, 2026
fd69be7
fix(exactTriangle): repair the prime-window header and hashing docume…
edemaine Oct 28, 2026
ce646c7
feat(exactTriangle): split modular queries and recover witnesses
edemaine Oct 28, 2026
2cae26c
doc(threeSum): record finite splitting and recovery milestone
edemaine Oct 28, 2026
dc4206a
refactor(exactTriangle): reuse the natural residue bound and clarify …
edemaine Oct 28, 2026
2434f47
feat(matrix): prove the tensor recursion and pruned evaluation core
edemaine Oct 28, 2026
62dc601
doc(matrix): clarify the overlapping contributing leaf
edemaine Oct 28, 2026
4f3226c
doc(matrix): make tensor and pruning explanations standalone
edemaine Oct 28, 2026
19ee942
feat(wordram): count Chan–He collisions with native histograms
edemaine Oct 28, 2026
a712205
fix(wordram): preserve collision-counting parameters explicitly
edemaine Oct 28, 2026
844522a
feat(matrix): prove finite thin-product correctness and sparse bounds
edemaine Oct 28, 2026
5cbb93d
feat(wordram): compute Chan–He parameters from native input
edemaine Oct 28, 2026
b4936ce
doc(matrix): explain finite thin-product guarantees in the Lean API
edemaine Oct 28, 2026
0505439
fix(wordram): repair parameter proof hygiene and ceiling-log docs
edemaine Oct 28, 2026
ad3e097
feat(wordram): implement shared tensor encodings
edemaine Oct 28, 2026
3b9234d
test(wordram): connect encoder witnesses to charged runs
edemaine Oct 28, 2026
46e9e72
feat(wordram): checkpoint native pruned-query controller proofs
edemaine Oct 28, 2026
68bbe40
refactor(schonhage): reuse binomial bounds and sharpen signed tests
edemaine Oct 28, 2026
cb261f6
feat(WordRAM): prepare and encode every shared matrix band
edemaine Oct 28, 2026
87ff44c
feat(wordram): checkpoint native pruned accumulation and frame transi…
edemaine Oct 28, 2026
b3b8047
feat(wordram): checkpoint actual pruned-query root-frame stores
edemaine Oct 28, 2026
6603615
fix(nat): reuse existing tuple digit identity in band preparation
edemaine Oct 28, 2026
79c9407
feat(wordram): checkpoint complete native pruned-controller phases
edemaine Oct 28, 2026
99b8a1b
doc(matrix): replace copied source prose with API descriptions
edemaine Oct 28, 2026
12abeee
refactor(finset): use order-embedding names in OrderGlue
edemaine Oct 28, 2026
829613f
feat(WordRAM): prove all-depth pruned controller execution
edemaine Oct 28, 2026
a93c021
style(WordRAM): normalize native proof file modes
edemaine Oct 28, 2026
0c2e0eb
feat(wordram): complete initialized sparse pruned-query execution
edemaine Oct 28, 2026
60bec64
chore(wordram): combine band and query evaluator prerequisites
edemaine Oct 29, 2026
06b3563
feat(wordram): checkpoint computed wanted-query routing
edemaine Oct 29, 2026
7772a34
feat(wordram): checkpoint wanted count-pass proofs
edemaine Oct 29, 2026
702c14b
doc(finset): restore ordered-gluing source credit
SamuelSchlesinger Oct 29, 2026
ce77926
fix(wordram): address pruned-query review findings
edemaine Oct 29, 2026
48a0fa3
chore(exactTriangle): merge current main for sibling integration
codex Oct 29, 2026
3fb98bd
chore(exactTriangle): join reviewed query splitting and counting layers
codex Oct 29, 2026
0b5cfe4
chore(wordram): retain reviewed pruned-query parent repair
edemaine Oct 29, 2026
d1bc583
chore(exactTriangle): reconcile modular-hashing integration documenta…
SamuelSchlesinger Oct 29, 2026
c0b0511
chore(exactTriangle): carry hashing integration through query splitting
SamuelSchlesinger Oct 29, 2026
a76a7d3
chore(exactTriangle): carry documentation integration through native …
SamuelSchlesinger Oct 29, 2026
4f6de79
fix(imports): deduplicate aggregate after counting integration
SamuelSchlesinger Oct 29, 2026
02f1b49
fix(imports): deduplicate aggregate after query-splitting integration
SamuelSchlesinger Oct 29, 2026
60dfe9a
Merge commit '02f1b495322d8cfee81969186795670e3574defa' into coordina…
SamuelSchlesinger Oct 29, 2026
6f48d5c
Merge remote-tracking branch 'origin/main' into coordinator/integrate…
SamuelSchlesinger Oct 29, 2026
b17ece3
Merge commit '4f6de797203eb5dac02b2cadd9e8fa33e7427d57' into coordina…
SamuelSchlesinger Oct 29, 2026
e031b72
Merge remote-tracking branch 'origin/main' into coordinator/integrate…
SamuelSchlesinger Oct 29, 2026
b79ae8e
fix(imports): regenerate aggregate after Chan-He parameter integration
SamuelSchlesinger Oct 29, 2026
3187bca
chore(wordram): retain reviewed sibling source-credit fix
edemaine Oct 29, 2026
a6e4005
feat(wordram): route and group wanted thin-product queries
edemaine Oct 29, 2026
249f960
Merge commit '6f48d5c0efdfea1984c4accf368de9eaa9716b95' into coordina…
SamuelSchlesinger Oct 29, 2026
08a0415
Merge remote-tracking branch 'origin/main' into coordinator/integrate…
SamuelSchlesinger Oct 29, 2026
b0fa45b
Merge commit '08a0415413677534785113b7cf643b1ba9a6ee78' into coordina…
SamuelSchlesinger Oct 29, 2026
bd9bd2b
fix(imports): regenerate aggregate after shared-encoder integration
SamuelSchlesinger Oct 29, 2026
ab3e96c
chore(integration): reconcile splitting layer with landed hashing parent
SamuelSchlesinger Oct 29, 2026
9c8b9fc
perf(wordram): reduce native parameter proof heartbeat budget
SamuelSchlesinger Oct 29, 2026
0c9b346
Merge remote-tracking branch 'github/main' into coordinator-heartbeat…
SamuelSchlesinger Oct 29, 2026
da6a301
chore(integration): adopt splitting layer after its parent landed
SamuelSchlesinger Oct 29, 2026
452924e
chore(wordram): scope measured parameter proof budget to reduction task
SamuelSchlesinger Oct 29, 2026
6aaf569
Merge commit 'da6a301aca1e0c03e6953b3329474b2236f0fac8' into coordina…
SamuelSchlesinger Oct 29, 2026
5f6aad5
chore(integration): refresh tensor recursion supplier after hashing l…
SamuelSchlesinger Oct 29, 2026
d91296f
perf(schonhage): focus cell arithmetic and use the default heartbeat …
SamuelSchlesinger Oct 29, 2026
d8f4162
fix(doc): restore unrelated upstream coordination records
SamuelSchlesinger Oct 29, 2026
a63864f
chore(three-sum): merge corrected supplier preserving scope and sourc…
SamuelSchlesinger Oct 29, 2026
781297c
chore(three-sum): merge refreshed thin-product supplier preserving en…
SamuelSchlesinger Oct 29, 2026
1f2cfc4
Merge commit '781297cef838ef99c33e4036195e58b06ca46779' into coordina…
SamuelSchlesinger Oct 29, 2026
b4aa7b5
Merge remote-tracking branch 'origin/main' into coordinator/integrate…
SamuelSchlesinger Oct 29, 2026
780531b
fix(imports): regenerate band-preparation aggregate from merged inven…
SamuelSchlesinger Oct 29, 2026
04349d4
Merge commit '780531b426051080bb274a0511f49142af03557c' into coordina…
SamuelSchlesinger Oct 29, 2026
5bf1f0e
doc(order-glue): preserve reviewed source credit through supplier merge
SamuelSchlesinger Oct 29, 2026
ec46a4f
chore(schonhage): integrate reviewed query and band suppliers
SamuelSchlesinger Oct 29, 2026
a9d3fb9
chore(schonhage): integrate wanted routing with refreshed query supplier
SamuelSchlesinger Oct 29, 2026
91f485e
chore(schonhage): adopt actual reviewed naming-refactor supplier
SamuelSchlesinger Oct 29, 2026
a677276
Merge remote-tracking branch 'origin/main' into coordinator/refresh-1…
SamuelSchlesinger Oct 29, 2026
8a81617
Merge remote-tracking branch 'github/main' into coordinator-refresh-1…
SamuelSchlesinger Oct 29, 2026
9e9cb10
Merge commit 'a67727699b41ff9fa7d7227964d67c26699ab7f6' into coordina…
SamuelSchlesinger Oct 29, 2026
c3e1bcd
doc(order-glue): restore paper citation and finite scope
SamuelSchlesinger Oct 29, 2026
606fd61
Merge commit 'c3e1bcd3c8d9bb1dc4819d1b0a994bf52119731b' into coordina…
SamuelSchlesinger Oct 29, 2026
59c6f80
feat(wordram): checkpoint native queried-product construction (#1581)
edemaine Oct 29, 2026
9d33c95
Merge commit '606fd614454016f738d69d2cd768cb4d9a9ad795' into coordina…
SamuelSchlesinger Oct 29, 2026
936457e
Merge commit '9d33c950c695a353b20fe43fd7524f25663a4bce' into coordina…
SamuelSchlesinger Oct 29, 2026
4b2d75c
Merge remote-tracking branch 'github/main' into coordinator-queue-146…
SamuelSchlesinger Oct 29, 2026
ea38fc5
Merge remote-tracking branch 'origin/main' into coordinator/refresh-1…
SamuelSchlesinger Oct 29, 2026
6a86e25
fix(imports): regenerate thin-product aggregate after main integration
SamuelSchlesinger Oct 29, 2026
6dbd0cc
Merge commit '6a86e258db83b7724f5bd93c43c0cbc4221a992f' into coordina…
SamuelSchlesinger Oct 29, 2026
c94a52f
Merge commit '4b2d75ce50dbca1101868ed3726170770d0a3e77' into coordina…
SamuelSchlesinger Oct 29, 2026
d072322
Merge remote-tracking branch 'origin/main' into coordinator/repair-15…
SamuelSchlesinger Oct 29, 2026
61e7fe7
doc(wordram): separate shared-encoder integration records
SamuelSchlesinger Oct 29, 2026
e21cbd0
chore(integration): preserve local band-refresh preparation
SamuelSchlesinger Oct 29, 2026
d2e8386
Merge commit '38f32f97e76ad36666b99d1f0d39a64fc0ef3306' into coordina…
SamuelSchlesinger Oct 29, 2026
f55b418
Merge commit 'd2e8386e3352241f97fc1c3faa3da53c1fbb1f68' into coordina…
SamuelSchlesinger Oct 29, 2026
8f25b31
Merge commit 'f04ad02616791b3fa37ff236ac27b0a8690ca985' into coordina…
SamuelSchlesinger Oct 29, 2026
debd649
doc(matrix): keep progress links out of shared reference append block
SamuelSchlesinger Oct 29, 2026
af20863
feat(wordram): checkpoint complete queried-product source before supp…
edemaine Oct 29, 2026
cf25502
chore(wordram): merge actual reviewed queried-product supplier
edemaine Oct 29, 2026
ff31104
feat(wordram): execute queried thin-matrix products (#1581)
edemaine Oct 29, 2026
9a57e13
style(Schonhage): remove trailing space in queried products
edemaine Oct 29, 2026
37306ef
doc(Schonhage): retain band catalogue encoding contract
edemaine Oct 29, 2026
b71ae0e
Merge commit 'cf63e382d02ba396b4f8d460958d70b15735f23e' into coordina…
SamuelSchlesinger Oct 29, 2026
8527c1f
Merge commit 'cf63e382d02ba396b4f8d460958d70b15735f23e' into coordina…
SamuelSchlesinger Oct 29, 2026
82f86fe
Merge commit '8527c1f4d25246fee901303e7c0f1310d30306c0' into coordina…
SamuelSchlesinger Oct 29, 2026
d429833
Merge commit '82f86fec9b93d546f96542987255aa9f16377b3b' into coordina…
SamuelSchlesinger Oct 29, 2026
f43c26e
Merge commit '82f86fec9b93d546f96542987255aa9f16377b3b' into coordina…
SamuelSchlesinger Oct 29, 2026
168a8be
Merge commit 'f43c26edef5bc586602e0678be9f8a59759bd5f2' into coordina…
SamuelSchlesinger Oct 29, 2026
7d82da6
Merge commit 'd429833391f88fe96223bef8124be7e6b21a117f' into coordina…
SamuelSchlesinger Oct 29, 2026
70898da
fix(imports): regenerate wanted-routing aggregate from actual inventory
SamuelSchlesinger Oct 29, 2026
a8cb7c9
chore(schonhage): refresh queried products onto actual wanted parent
SamuelSchlesinger Oct 29, 2026
7cb7ade
wip(wordram): checkpoint callable thin-product source
edemaine Oct 29, 2026
904c620
chore(wordram): merge reviewed queried-product parent into callable c…
edemaine Oct 29, 2026
f5e6949
feat(WordRAM): run queried thin products in caller-owned memory
edemaine Oct 29, 2026
fd1f2fc
chore(WordRAM): merge pinned landed main into callable product child
edemaine Oct 29, 2026
ce10624
refactor(WordRAM): align ArrayNormalize module name and admission prose
edemaine Oct 29, 2026
8f46341
feat(ExactTriangle): checkpoint actual record product composition
edemaine Oct 29, 2026
8612aff
chore(ExactTriangle): merge pinned main into record product child
edemaine Oct 29, 2026
96842bb
feat(ExactTriangle): validate initialized record product adapter
edemaine Oct 29, 2026
0e10700
feat(ExactTriangle): checkpoint native candidate-verification source
edemaine Oct 29, 2026
b461744
feat(ExactTriangle): compose initialized per-record native witness re…
edemaine Oct 29, 2026
c02ed27
chore(ExactTriangle): merge pinned main for witness validation
edemaine Oct 29, 2026
888918d
chore(deps): track CSLib main and Lean rc2
SamuelSchlesinger Oct 29, 2026
9a1b754
refactor(probability): use generic infinite-sum lemmas
SamuelSchlesinger Oct 29, 2026
d7dcd79
fix(build): adapt proofs to the rc2 Mathlib API
SamuelSchlesinger Oct 29, 2026
7925011
Merge remote-tracking branch 'origin/main' into work/cslib-rc2-sync-1…
SamuelSchlesinger Oct 29, 2026
f5a70cb
test(quantum): qualify the protected matrix lemma
SamuelSchlesinger Oct 29, 2026
bb3e910
fix(build): normalize goals before proof automation
SamuelSchlesinger Oct 29, 2026
d4b0493
chore(WordRAM): finalize native record witness validation
edemaine Oct 29, 2026
0d38523
Merge commit '96fd02a85579bbbce97c02110ed80da02684cb2e' into work/csl…
SamuelSchlesinger Oct 29, 2026
2e388dc
doc(ExactTriangle): state native code bound inclusively
edemaine Oct 29, 2026
0edd088
Merge commit '0d38523e96f8b431aeadc86c548c9994714f9b00' into coordina…
edemaine Oct 29, 2026
741051c
Merge commit '0ba8b2e59d4d7009c814096d8bbb4a9823d06963' into coordina…
edemaine Oct 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Complexity.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module -- shake: keep-all --deprecated_module: ignore
public import Complexity.Algorithms.ExactTriangle.Defs
public import Complexity.Algorithms.ExactTriangle.Hashing
public import Complexity.Algorithms.ExactTriangle.Lopsided
public import Complexity.Algorithms.ExactTriangle.PieceBounds
public import Complexity.Algorithms.ExactTriangle.Splitting
public import Complexity.Algorithms.ExactTriangle.Weights
public import Complexity.Algorithms.ExactTriangle.Witnesses
Expand Down Expand Up @@ -107,6 +108,7 @@ public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Bounds
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Copy
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Decode
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Detection
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Entry
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Exec
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.First
Expand All @@ -118,9 +120,35 @@ public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Prepare
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Producer
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.ProducerQueries
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Program
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.QueryValues
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.RawInput
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.SourceBounds
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Consume
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Entry
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Exec
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Failures
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Flat
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Header
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Kernel
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Original
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Pair
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Prepare
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Program
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Queries
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Readback
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.SourceBounds
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Spec
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Step
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.TimeBound
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Trace
public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordWitness.Width
public import Complexity.Algorithms.WordRAM.ExactTriangle.ResidueClasses
public import Complexity.Algorithms.WordRAM.ExactTriangle.WitnessScan.Cell
public import Complexity.Algorithms.WordRAM.ExactTriangle.WitnessScan.Exec
public import Complexity.Algorithms.WordRAM.ExactTriangle.WitnessScan.Finite
public import Complexity.Algorithms.WordRAM.ExactTriangle.WitnessScan.Program
public import Complexity.Algorithms.WordRAM.ExactTriangle.WitnessScan.Setup
public import Complexity.Algorithms.WordRAM.ExactTriangle.WriteMatrices
public import Complexity.Algorithms.WordRAM.FusionTree.Binary
public import Complexity.Algorithms.WordRAM.FusionTree.Boundary
Expand Down
65 changes: 65 additions & 0 deletions Complexity/Algorithms/ExactTriangle/PieceBounds.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/-
Copyright (c) 2026 Anthropic, PBC, Erik Demaine. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anthropic, PBC, Erik Demaine
-/

module
-- upstream: candidate

public import Complexity.Algorithms.ExactTriangle.Splitting

/-!
# Numeric coordinates of ExactTriangle pieces

These bounds connect the existing finite pieces with the truncated original-vertex intervals
used by both native matrix preparation and witness recovery. They expose the same coordinate
facts previously private to the prepared-adjacency consumer, without changing the finite pieces.
-/

public section

namespace Cslib.Algorithms.ExactTriangle

/-- The active piece/label rectangle fits in the source middle dimension. -/
theorem pieceLength_mul_le_of_mem_sqrtPrimeWindow {D g p n k : ℕ} (hg : 1 ≤ g)
(hp : p ∈ Nat.sqrtPrimeWindow D) :
min (pieceSize D g) (n - k * pieceSize D g) * p ≤ D := by
have hpS : p ≤ sOf D := Nat.le_sqrt'.mpr (Nat.mem_sqrtPrimeWindow_iff_sq.mp hp).2.2.le
have hlen : min (pieceSize D g) (n - k * pieceSize D g) ≤ sOf D :=
(min_le_left _ _).trans (pieceSize_le_sOf hg)
exact (Nat.mul_le_mul hlen hpS).trans (Nat.sqrt_le D)

/-- An original piece vertex has its computed local coordinate in the actual truncated interval. -/
theorem piece_coordinate {n D g k : ℕ} (hD : 16 ≤ D) (hg : 1 ≤ g)
(c : Fin n) (hc : c ∈ piece n D g k) :
k * pieceSize D g + c.val % pieceSize D g = c.val ∧
c.val % pieceSize D g < min (pieceSize D g) (n - k * pieceSize D g) := by
have hB := pieceSize_pos hD hg
have hdiv := (mem_piece c).mp hc
have hsplit := Nat.div_add_mod c.val (pieceSize D g)
rw [hdiv, Nat.mul_comm] at hsplit
have hmod := Nat.mod_lt c.val hB
exact ⟨hsplit, lt_min hmod (by have := c.isLt; omega)⟩

/-- The finite piece is precisely its original-coordinate truncated interval. -/
theorem mem_piece_iff_interval {n D g k : ℕ} (hD : 16 ≤ D) (hg : 1 ≤ g) (c : Fin n) :
c ∈ piece n D g k ↔ k * pieceSize D g ≤ c.val ∧
c.val < k * pieceSize D g + min (pieceSize D g) (n - k * pieceSize D g) := by
constructor
· intro hc
obtain ⟨he, hv⟩ := piece_coordinate hD hg c hc
omega
· rintro ⟨hlo, hhi⟩
apply (mem_piece c).mpr
have hB := pieceSize_pos hD hg
have hsmall : c.val - k * pieceSize D g < pieceSize D g := by
have := min_le_left (pieceSize D g) (n - k * pieceSize D g)
omega
have he : c.val = k * pieceSize D g + (c.val - k * pieceSize D g) := by omega
have hsmall' : c.val - pieceSize D g * k < pieceSize D g := by
simpa only [Nat.mul_comm k (pieceSize D g)] using hsmall
rw [he, Nat.mul_comm k (pieceSize D g), Nat.mul_add_div hB,
Nat.div_eq_of_lt hsmall', Nat.add_zero]

end Cslib.Algorithms.ExactTriangle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module
-- upstream: candidate

public import Complexity.Algorithms.WordRAM.ExactTriangle.PreparationSpec
public import Complexity.Algorithms.ExactTriangle.PieceBounds

/-!
# Adjacency in prepared native ExactTriangle matrices
Expand All @@ -34,25 +35,6 @@ namespace Cslib.Algorithms.WordRAM.ExactTriangle.PreparationSpec

open Cslib.WordRAM Cslib.Algorithms.ExactTriangle Cslib.Matrix

private theorem prepared_middle_fits {D g p n k : ℕ} (hg : 1 ≤ g)
(hp : p ∈ Nat.sqrtPrimeWindow D) :
min (pieceSize D g) (n - k * pieceSize D g) * p ≤ D := by
have hpS : p ≤ sOf D := Nat.le_sqrt'.mpr (Nat.mem_sqrtPrimeWindow_iff_sq.mp hp).2.2.le
have hlen : min (pieceSize D g) (n - k * pieceSize D g) ≤ sOf D :=
(min_le_left _ _).trans (pieceSize_le_sOf hg)
exact (Nat.mul_le_mul hlen hpS).trans (Nat.sqrt_le D)

private theorem piece_coordinate {n D g k : ℕ} (hD : 16 ≤ D) (hg : 1 ≤ g)
(c : Fin n) (hc : c ∈ piece n D g k) :
k * pieceSize D g + c.val % pieceSize D g = c.val ∧
c.val % pieceSize D g < min (pieceSize D g) (n - k * pieceSize D g) := by
have hB := pieceSize_pos hD hg
have hdiv := (mem_piece c).mp hc
have hsplit := Nat.div_add_mod c.val (pieceSize D g)
rw [hdiv, Nat.mul_comm] at hsplit
have hmod := Nat.mod_lt c.val hB
exact ⟨hsplit, lt_min hmod (by have := c.isLt; omega)⟩

/-- The actual prepared matrix cells express the existing finite graph's adjacency, and the
unused middle positions are zero. The prime-window and piece hypotheses discharge every
padding and local-coordinate premise of the matrix-entry API. -/
Expand Down Expand Up @@ -100,7 +82,7 @@ theorem instance_adjacency {n D g p : ℕ} (values : ℕ → ℤ)
(fun j => WriteMatrices.entry false A (fun q => R (2 * N + q)) j) at hL
change (Block.mk A'.dst (n * D)).Realizes y.mem
(fun j => WriteMatrices.entry true A' (fun q => R (N + q)) j) at hR
have hpad : A.length * A.p ≤ A.D := prepared_middle_fits hg hp
have hpad : A.length * A.p ≤ A.D := pieceLength_mul_le_of_mem_sqrtPrimeWindow hg hp
change min B (n - idx.2.2 * B) * p ≤ D at hpad
have hloadL (a c : Fin n) :
R (2 * N + (a.val * n + c.val)) = (wAC a c).natMod p := by
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/-
Copyright (c) 2026 Anthropic, PBC, Erik Demaine. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anthropic, PBC, Erik Demaine
-/

module
-- upstream: candidate

public import Complexity.Algorithms.WordRAM.ExactTriangle.RecordProduct.Producer
public import Complexity.Algorithms.ExactTriangle.PieceBounds
public import Complexity.Algorithms.ExactTriangle.Witnesses
public import Mathlib.Data.Nat.Count
import Complexity.Algorithms.WordRAM.ExactTriangle.MatrixSpec

/-!
# Actual record products detect the existing modular graph

The zero/one value matrices used by the real product adapter have a nonzero product entry
exactly at a common neighbor of the existing finite `lopInstance`. Active middle coordinates
and zero padding follow from the original source prime window and piece geometry. Thus a
consumer can derive detection semantics from the actual product, without an answer oracle.
-/

public section

namespace Cslib.Algorithms.WordRAM.ExactTriangle.RecordProduct

open Cslib.Matrix Cslib.Algorithms.ExactTriangle
open Cslib.Algorithms.MatrixMultiplication.Schonhage

/-- The value matrices written by the producer and consumed by the actual native product
have precisely the common-neighbor semantics of the existing finite modular instance. -/
theorem product_ne_zero_iff {m n g p : ℕ} (values : ℕ → ℤ)
(wAB wBC wAC : Fin n → Fin n → ℤ)
(hD : 16 ≤ D m) (hg : 1 ≤ g) (hp : p ∈ Nat.sqrtPrimeWindow (D m))
(hBC : ∀ b c, values (n * n + pairIndex (b, c)) = wBC b c)
(hAC : ∀ a c, values (2 * (n * n) + pairIndex (a, c)) = wAC a c)
(idx : InstanceIndex p) (a b : Fin n) :
(leftMatrix m n g p values idx * rightMatrix m n g p values idx) a b ≠ 0 ↔
(lopInstance wAB wBC wAC (D m) g p idx).HasCommonNeighbor a b := by
classical
let X := leftMatrix m n g p values idx
let Y := rightMatrix m n g p values idx
let G := lopInstance wAB wBC wAC (D m) g p idx
let A := matrixArgs n (D m) g p idx.1.val idx.2.2
let Rleft (q : ℕ) := (values (2 * (n * n) + q) % (p : ℤ)).toNat
let Rright (q : ℕ) := (values (n * n + q) % (p : ℤ)).toNat
have hpad : A.length * p ≤ D m := pieceLength_mul_le_of_mem_sqrtPrimeWindow hg hp
have hp0 : 0 < p := (Nat.mem_sqrtPrimeWindow_iff_sq.mp hp).1.pos
have loadLeft (u c : Fin n) : Rleft (u.val * n + c.val) = (wAC u c).natMod p := by
change (values (2 * (n * n) + pairIndex (u, c)) % (p : ℤ)).toNat = _
rw [hAC]
rfl
have loadRight (u c : Fin n) : Rright (u.val * n + c.val) = (wBC u c).natMod p := by
change (values (n * n + pairIndex (u, c)) % (p : ℤ)).toNat = _
rw [hBC]
rfl
have edges (c : Fin n) (hc : c ∈ piece n (D m) g idx.2.2)
(v : Fin A.length) (label : Fin p) (j : Fin (D m))
(hv : A.first + v.val = c.val) (hj : j.val = v.val * p + label.val) :
(X a j = 1 ↔ G.adjA a (⟨c, hc⟩, label)) ∧
(Y j b = 1 ↔ G.adjB (⟨c, hc⟩, label) b) := by
have hl := MatrixSpec.left_lopInstance A Rleft wAB wBC wAC g idx.2.1 idx.2.2
idx.1 rfl a c v label hc hv hpad loadLeft
have hr := MatrixSpec.right_lopInstance A Rright wAB wBC wAC g idx.2.1 idx.2.2
idx.1 b c v label hc hv loadRight
constructor
· change (WriteMatrices.entry false A Rleft (a.val * D m + j.val) : ℤ) = 1 ↔ _
rw [hj]
exact_mod_cast hl
· change (WriteMatrices.entry true A Rright (j.val * n + b.val) : ℤ) = 1 ↔ _
rw [hj]
exact_mod_cast hr
obtain ⟨hX, hY⟩ := matrices_zero_one m n g p values idx
have hx : ∀ a j, X a j = 0 ∨ X a j = 1 := by
intro a j
have := hX a j
change 0 ≤ X a j ∧ X a j ≤ 1 at this
omega
have hy : ∀ j b, Y j b = 0 ∨ Y j b = 1 := by
intro j b
have := hY j b
change 0 ≤ Y j b ∧ Y j b ≤ 1 at this
omega
rw [LopsidedGraph.mul_apply_eq_commonNeighborCount_ofMatrices X Y ∅ hx hy]
have hnz := (LopsidedGraph.ofMatrices X Y ∅).commonNeighborCount_ne_zero_iff a b
rw [show ((LopsidedGraph.ofMatrices X Y ∅).commonNeighborCount a b : ℤ) ≠ 0 ↔
(LopsidedGraph.ofMatrices X Y ∅).commonNeighborCount a b ≠ 0 by exact_mod_cast Iff.rfl]
rw [hnz]
change (∃ j : Fin (D m), X a j = 1 ∧ Y j b = 1) ↔ G.HasCommonNeighbor a b
constructor
· rintro ⟨j, hleft, hright⟩
have hj : j.val / p < A.length := by
by_contra h
have hz := MatrixSpec.left_padding A Rleft a j (by change A.length ≤ j.val / p; omega)
change WriteMatrices.entry false A Rleft (a.val * D m + j.val) = 0 at hz
have hl : (WriteMatrices.entry false A Rleft (a.val * D m + j.val) : ℤ) = 1 := hleft
rw [hz] at hl
norm_num at hl
let v : Fin A.length := ⟨j.val / p, hj⟩
let label : Fin p := ⟨j.val % p, Nat.mod_lt _ hp0⟩
have hn : A.first + v.val < n := by
have hl := min_le_right (pieceSize (D m) g) (n - A.first)
have hv := v.isLt
change v.val < min (pieceSize (D m) g) (n - A.first) at hv
omega
let c : Fin n := ⟨A.first + v.val, hn⟩
have hc : c ∈ piece n (D m) g idx.2.2 := by
apply (mem_piece_iff_interval hD hg c).mpr
change idx.2.2 * pieceSize (D m) g ≤ A.first + v.val ∧
A.first + v.val < idx.2.2 * pieceSize (D m) g + A.length
change A.first ≤ A.first + v.val ∧ A.first + v.val < A.first + A.length
exact ⟨by omega, Nat.add_lt_add_left v.isLt _⟩
have he : j.val = v.val * p + label.val := by
simpa only [v, label, Nat.mul_comm] using (Nat.div_add_mod j.val p).symm
obtain ⟨heL, heR⟩ := edges c hc v label j rfl he
exact ⟨(⟨c, hc⟩, label), heL.mp hleft, heR.mp hright⟩
· rintro ⟨⟨⟨c, hc⟩, label⟩, hleft, hright⟩
obtain ⟨hv, hlen⟩ := piece_coordinate hD hg c hc
let v : Fin A.length := ⟨c.val % pieceSize (D m) g, hlen⟩
have hj : v.val * p + label.val < D m := by
have hm := Nat.mul_le_mul_right p v.isLt
rw [Nat.succ_mul] at hm
have hl := label.isLt
omega
let j : Fin (D m) := ⟨v.val * p + label.val, hj⟩
obtain ⟨heL, heR⟩ := edges c hc v label j hv rfl
exact ⟨j, heL.mpr hleft, heR.mpr hright⟩

/-- The mathematical answers computed by the actual product discharge the finite recovery
contract. Correct answers are a conclusion here, not a solver assumption. -/
theorem product_answers_correct {m n g p : ℕ} (values : ℕ → ℤ)
(wAB wBC wAC : Fin n → Fin n → ℤ)
(hD : 16 ≤ D m) (hg : 1 ≤ g) (hp : p ∈ Nat.sqrtPrimeWindow (D m))
(hBC : ∀ b c, values (n * n + pairIndex (b, c)) = wBC b c)
(hAC : ∀ a c, values (2 * (n * n) + pairIndex (a, c)) = wAC a c) :
AnswersCorrect wAB wBC wAC (D m) g p (fun idx q => decide
((leftMatrix m n g p values idx * rightMatrix m n g p values idx) q.1 q.2 ≠ 0)) := by
intro idx _ q _
simp only [decide_eq_true_eq]
exact propext (product_ne_zero_iff values wAB wBC wAC hD hg hp hBC hAC idx q.1 q.2)

/-- Distinct occurrences of one actual query chunk that are accepted by the product but
have no original witness inject into the existing finite failed scans. Their number is
therefore bounded by the existing original-weight false-positive count. -/
theorem failed_occurrences_le {m n g p : ℕ} (values : ℕ → ℤ)
(wAB wBC wAC : Fin n → Fin n → ℤ)
(hD : 16 ≤ D m) (hg : 1 ≤ g) (hp : p ∈ Nat.sqrtPrimeWindow (D m))
(hBC : ∀ b c, values (n * n + pairIndex (b, c)) = wBC b c)
(hAC : ∀ a c, values (2 * (n * n) + pairIndex (a, c)) = wAC a c)
(idx : InstanceIndex p) (hidx : idx ∈ instanceIndices wAB (D m) g p)
(qs : List (Fin n × Fin n)) (default : Fin n × Fin n) (hqs : qs.Nodup)
(hquery : ∀ q ∈ qs, q ∈ chunkOf wAB (D m) p idx.1 idx.2.1) :
Nat.count (fun i =>
(leftMatrix m n g p values idx * rightMatrix m n g p values idx)
(qs.getD i default).1 (qs.getD i default).2 ≠ 0 ∧
scanPiece wAB wBC wAC (D m) g idx.2.2
(qs.getD i default).1 (qs.getD i default).2 = none) qs.length ≤
falsePositiveCount wAB wBC wAC p := by
classical
let ans : InstanceIndex p → Fin n × Fin n → Bool := fun idx q => decide
((leftMatrix m n g p values idx * rightMatrix m n g p values idx) q.1 q.2 ≠ 0)
let P (i : ℕ) : Prop :=
(leftMatrix m n g p values idx * rightMatrix m n g p values idx)
(qs.getD i default).1 (qs.getD i default).2 ≠ 0 ∧
scanPiece wAB wBC wAC (D m) g idx.2.2
(qs.getD i default).1 (qs.getD i default).2 = none
let := Nat.CountSet.fintype P qs.length
let failures := failedScans wAB wBC wAC (D m) g p ans
have hget (i : ℕ) (hi : i < qs.length) : qs.getD i default = qs.get ⟨i, hi⟩ := by
rw [List.getD_eq_getElem _ _ hi, List.get_eq_getElem]
let f : {i : ℕ // i < qs.length ∧ P i} → {s // s ∈ failures} := fun i =>
⟨(idx, qs.get ⟨i.val, i.prop.1⟩), by
apply Finset.mem_filter.mpr
refine ⟨mem_acceptedPairs.mpr ⟨hidx, ?_, ?_⟩, ?_⟩
· exact hquery _ (List.get_mem _ _)
· have he := i.prop.2.1
simpa only [ans, decide_eq_true_eq, hget i.val i.prop.1] using he
· have he := i.prop.2.2
simpa only [scanOf, hget i.val i.prop.1] using he⟩
have hf : Function.Injective f := by
intro i j hij
have hpair : qs.get ⟨i.val, i.prop.1⟩ = qs.get ⟨j.val, j.prop.1⟩ :=
congrArg (fun s => s.val.2) hij
have hi := List.nodup_iff_injective_get.mp hqs hpair
exact Subtype.ext (congrArg Fin.val hi)
change Nat.count P qs.length ≤ _
rw [Nat.count_eq_card_fintype]
calc
_ ≤ Fintype.card {s // s ∈ failures} := Fintype.card_le_of_injective f hf
_ = failures.card := Fintype.card_coe _
_ ≤ falsePositiveCount wAB wBC wAC p := card_failedScans_le
(Nat.mem_sqrtPrimeWindow_iff_sq.mp hp).1.ne_zero
(product_answers_correct values wAB wBC wAC hD hg hp hBC hAC)

end Cslib.Algorithms.WordRAM.ExactTriangle.RecordProduct
Loading
Loading