File size: 7,605 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import OAI.Algorithms.SubexponentialSAT.SqrtLog.ProgramTapeReplayFootprint
import OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.StreamResources

/-! All-prefix resource transport for the existing exact-time tape replay.
The imported avoidance certificate excludes reads as well as writes, including
`peek`. Erasing hidden caller data is a proof construction only: the actual
instruction table and its measured number of steps remain unchanged. -/

namespace OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.CommandStackTransport

open BitProgram StructuredCalls SqrtLog.ProgramTapeReplay

variable {r s l : ℕ}

theorem rebaseState_restore (H : Fin s → Prop) [DecidablePred H]
    (frame : Fin s → List Bool) (a : State r s) :
    rebaseState H a.stacks (rebaseState H frame a) = a := by
  cases a with
  | mk regs words =>
      apply congrArg (fun ws => (⟨regs, ws⟩ : State r s))
      funext i
      by_cases hi : H i <;> simp [rebaseState, rebaseStacks, hi]

theorem rebaseCfg_restore (H : Fin s → Prop) [DecidablePred H]
    (frame : Fin s → List Bool) (a : Cfg r s l) :
    rebaseCfg H a.stacks (rebaseCfg H frame a) = a := by
  cases a with
  | mk pc regs words =>
      apply congrArg (fun ws => (⟨pc, regs, ws⟩ : Cfg r s l))
      funext i
      by_cases hi : H i <;> simp [rebaseCfg, rebaseStacks, hi]

/-- A concrete parameter bank disjoint from hidden storage is unchanged by
the proof-only shadow projection. Registers are always retained. -/
theorem projectState_rebase {R S : ℕ} (H : Fin S → Prop) [DecidablePred H]
    (frame : Fin S → List Bool) (a : State R S)
    (register : Fin r → Fin R) (stack : Fin s → Fin S)
    (hstack : ∀ i, ¬ H (stack i)) :
    projectState register stack (rebaseState H frame a) = projectState register stack a := by
  apply congrArg₂ StructuredProgram.State.mk
  · rfl
  · funext i
    exact rebaseStacks_of_not_mem H frame a.stacks (stack i) (hstack i)

/-- The shadow state retains only visible inputs. Its storage bound does not
contain the size of the hidden collector or incumbent. -/
theorem shadow_stack_bound (H : Fin s → Prop) [DecidablePred H]
    (a : State r s) (bound : ℕ)
    (ha : ∀ i, ¬ H i → (a.stacks i).length ≤ bound) :
    ∀ i, ((rebaseState H (fun _ => []) a).stacks i).length ≤ bound := by
  intro i
  by_cases hi : H i
  · simp [rebaseState, rebaseStacks, hi]
  · simpa only [rebaseState, rebaseStacks_of_not_mem H (fun _ => []) a.stacks i hi]
      using ha i hi

/-- An arbitrary prefix of the rebased execution is the rebase of the exact
same-length prefix of the original execution. -/
theorem trace_prefix_rebase (H : Fin s → Prop) [DecidablePred H]
    {p : Program r s l} (hp : AvoidsProgram H p)
    (frame : Fin s → List Bool) {steps prefixSteps : ℕ} {a b : Cfg r s l}
    (ht : Trace p steps a b) {intermediate : Cfg r s l}
    (hle : prefixSteps ≤ steps)
    (hprefix : Trace p prefixSteps (rebaseCfg H frame a) intermediate) :
    ∃ source, Trace p prefixSteps a source ∧
      intermediate = rebaseCfg H frame source := by
  obtain ⟨source, hs⟩ := trace_prefix_exists ht hle
  exact ⟨source, hs, trace_same_length_unique hprefix (trace_rebase H hp hs frame)⟩

/-- Only hidden caller stacks contribute the frame bound. Every visible stack
is controlled by the actual local trace, with no multiplicative recurrence. -/
theorem trace_stack_bound (H : Fin s → Prop) [DecidablePred H]
    {p : Program r s l} (hp : AvoidsProgram H p)
    (frame : Fin s → List Bool) {steps bound frameBound : ℕ} {a b : Cfg r s l}
    (ht : Trace p steps a b)
    (hsource : ∀ prefixSteps source, prefixSteps ≤ steps →
      Trace p prefixSteps a source →
      ∀ i, ¬ H i → (source.stacks i).length ≤ bound)
    (hframe : ∀ i, H i → (frame i).length ≤ frameBound) :
    ∀ prefixSteps intermediate, prefixSteps ≤ steps →
      Trace p prefixSteps (rebaseCfg H frame a) intermediate →
      ∀ i, (intermediate.stacks i).length ≤ max bound frameBound := by
  intro prefixSteps intermediate hle hprefix i
  obtain ⟨source, hs, rfl⟩ := trace_prefix_rebase H hp frame ht hle hprefix
  by_cases hi : H i
  · change (rebaseStacks H frame source.stacks i).length ≤ _
    rw [rebaseStacks_of_mem H frame source.stacks i hi]
    exact (hframe i hi).trans (Nat.le_max_right _ _)
  · change (rebaseStacks H frame source.stacks i).length ≤ _
    rw [rebaseStacks_of_not_mem H frame source.stacks i hi]
    exact (hsource prefixSteps source hle hs i hi).trans (Nat.le_max_left _ _)

/-- A local execution on the empty hidden shadow yields an execution of the
same fixed command on the actual caller, at exactly the same step count. -/
theorem eval_shadow (H : Fin s → Prop) [DecidablePred H]
    {command : Cmd r s} (hc : AvoidsCmd H command)
    {steps : ℕ} {a b : State r s}
    (he : Eval command steps (rebaseState H (fun _ => []) a) b) :
    Eval command steps a (rebaseState H a.stacks b) := by
  have h := eval_rebase H hc he a.stacks
  rw [rebaseState_restore] at h
  exact h

/-- The local all-prefix bound is transported through the compiled command,
including its final halt. Hidden external words are restored unchanged. -/
theorem eval_shadow_prefix_bound (H : Fin s → Prop) [DecidablePred H]
    {command : Cmd r s} (hc : AvoidsCmd H command) (input output : Fin s)
    {steps bound frameBound : ℕ} {a b : State r s}
    (he : Eval command steps (rebaseState H (fun _ => []) a) b)
    (hsource : ∀ prefixSteps source, prefixSteps ≤ steps + 1 →
      Trace (compiled command input output) prefixSteps
        ((rebaseState H (fun _ => []) a).withPC
          (some (compiled command input output).entry)) source →
      ∀ i, ¬ H i → (source.stacks i).length ≤ bound)
    (hframe : ∀ i, H i → (a.stacks i).length ≤ frameBound) :
    ∀ prefixSteps intermediate, prefixSteps ≤ steps + 1 →
      Trace (compiled command input output) prefixSteps
        (a.withPC (some (compiled command input output).entry)) intermediate →
      ∀ i, (intermediate.stacks i).length ≤ max bound frameBound := by
  have h := trace_stack_bound H (avoids_compiled H command hc input output)
    a.stacks (compiled_halt_trace he input output) hsource hframe
  rw [rebaseCfg_withPC, rebaseState_restore] at h
  exact h

/-- Hidden caller words are preserved at every prefix of this same replayed
execution, including prefixes within called finite instruction tables. -/
theorem eval_shadow_prefix_frame (H : Fin s → Prop) [DecidablePred H]
    {command : Cmd r s} (hc : AvoidsCmd H command) (input output : Fin s)
    {steps : ℕ} {a b : State r s}
    (he : Eval command steps (rebaseState H (fun _ => []) a) b)
    {prefixSteps : ℕ} {intermediate : Cfg r s (size command + 1)}
    (hle : prefixSteps ≤ steps + 1)
    (hprefix : Trace (compiled command input output) prefixSteps
      (a.withPC (some (compiled command input output).entry)) intermediate)
    (i : Fin s) (hi : H i) : intermediate.stacks i = a.stacks i := by
  have hstart : rebaseCfg H a.stacks
      ((rebaseState H (fun _ => []) a).withPC
        (some (compiled command input output).entry)) =
      a.withPC (some (compiled command input output).entry) := by
    rw [rebaseCfg_withPC, rebaseState_restore]
  rw [← hstart] at hprefix
  obtain ⟨source, _, rfl⟩ := trace_prefix_rebase H
    (avoids_compiled H command hc input output) a.stacks
    (compiled_halt_trace he input output) hle hprefix
  exact rebaseStacks_of_mem H a.stacks source.stacks i hi

end OAI.Algorithms.SubexponentialSAT.SqrtLogSpace.CommandStackTransport