aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / bootstrap / formal-oracles / lean / AienReceipt / Theorems.lean
| 1 | import AienReceipt.Model |
| 2 | namespace AienReceipt |
| 3 | |
| 4 | /-! Rules enforced by the code (AIEN.INV.RECEIPT.REJECTION_CONSISTENCY.V1). |
| 5 | Every theorem holds for every nonce function `nf` (crypto is an assumption). -/ |
| 6 | |
| 7 | /-- Unpack `validate = true` into its five groups. -/ |
| 8 | theorem validate_parts {nf : Nat → Nat → Nat} {r : Receipt} (h : validate nf r = true) : |
| 9 | presenceOk r = true ∧ codesOk r = true ∧ decisionOk r = true ∧ canaryOk r = true ∧ |
| 10 | nonceOk nf r = true := by |
| 11 | simp only [validate, Bool.and_eq_true] at h |
| 12 | exact ⟨h.1.1.1.1, h.1.1.1.2, h.1.1.2, h.1.2, h.2⟩ |
| 13 | |
| 14 | /-- the full Rejected arm of `validate`, as propositions -/ |
| 15 | theorem rejected_arm {nf} {r : Receipt} (h : validate nf r = true) (hr : r.decision = .rejected) : |
| 16 | r.stage ≠ 0 ∧ r.reason ≠ 0 ∧ r.status = .notRun ∧ r.exitCode = 0 ∧ r.syscalls = 0 ∧ |
| 17 | r.reads = 0 ∧ r.denials = 0 ∧ r.rflags.onlyReclaimed = true := by |
| 18 | have := (validate_parts h).2.2.1 |
| 19 | simp only [decisionOk, hr, Bool.and_eq_true, bne_iff_ne, ne_eq, beq_iff_eq] at this |
| 20 | obtain ⟨⟨⟨⟨⟨⟨⟨a, b⟩, c⟩, d⟩, e⟩, f⟩, g⟩, i⟩ := this |
| 21 | exact ⟨a, b, c, d, e, f, g, i⟩ |
| 22 | |
| 23 | /-- rejected => execution status NotRun -/ |
| 24 | theorem rejected_not_run {nf} {r : Receipt} (h : validate nf r = true) |
| 25 | (hr : r.decision = .rejected) : r.status = .notRun := |
| 26 | (rejected_arm h hr).2.2.1 |
| 27 | |
| 28 | /-- rejected => no successful execution evidence: not Exited, zero exit status, zero syscalls, |
| 29 | zero object reads, zero denials, and CANARY_PASSED clear (only RECLAIMED may be set). -/ |
| 30 | theorem rejected_no_execution_evidence {nf} {r : Receipt} (h : validate nf r = true) |
| 31 | (hr : r.decision = .rejected) : |
| 32 | r.status ≠ .exited ∧ r.exitCode = 0 ∧ r.syscalls = 0 ∧ r.reads = 0 ∧ r.denials = 0 ∧ |
| 33 | r.rflags.canaryPassed = false := by |
| 34 | obtain ⟨_, _, hs, hx, hc, hr', hd, hf⟩ := rejected_arm h hr |
| 35 | refine ⟨by rw [hs]; decide, hx, hc, hr', hd, ?_⟩ |
| 36 | simp only [ResultFlags.onlyReclaimed, Bool.and_eq_true, Bool.not_eq_true'] at hf |
| 37 | simp [hf] |
| 38 | |
| 39 | /-- rejected => a rejection stage (1..9) and a valid rejection reason are present -/ |
| 40 | theorem rejected_stage_reason_present {nf} {r : Receipt} (h : validate nf r = true) |
| 41 | (hr : r.decision = .rejected) : |
| 42 | (1 ≤ r.stage ∧ r.stage ≤ maxStage) ∧ r.reason ≠ 0 ∧ validReason r.reason = true := by |
| 43 | obtain ⟨hs, hq, _⟩ := rejected_arm h hr |
| 44 | have hc := (validate_parts h).2.1 |
| 45 | simp only [codesOk, Bool.and_eq_true, decide_eq_true_eq, Bool.or_eq_true, beq_iff_eq] at hc |
| 46 | refine ⟨⟨by omega, hc.1⟩, hq, ?_⟩ |
| 47 | rcases hc.2 with h0 | h1 |
| 48 | · exact absurd h0 hq |
| 49 | · exact h1 |
| 50 | |
| 51 | /-- not rejected => the rejection-only fields (stage, reason) are absent -/ |
| 52 | theorem not_rejected_no_rejection_fields {nf} {r : Receipt} (h : validate nf r = true) |
| 53 | (hr : r.decision ≠ .rejected) : r.stage = 0 ∧ r.reason = 0 := by |
| 54 | have := (validate_parts h).2.2.1 |
| 55 | unfold decisionOk at this |
| 56 | cases hd : r.decision <;> simp_all |
| 57 | |
| 58 | /-- CANARY_PASSED => execution succeeded (status Exited, exit status zero) -/ |
| 59 | theorem canary_passed_executed {nf} {r : Receipt} (h : validate nf r = true) |
| 60 | (hc : r.rflags.canaryPassed = true) : r.status = .exited ∧ r.exitCode = 0 := by |
| 61 | have := (validate_parts h).2.2.2.1 |
| 62 | simp [canaryOk, hc] at this |
| 63 | exact this |
| 64 | |
| 65 | /-- absent machine id flag => the machine id digest is zero (not readable as present) -/ |
| 66 | theorem absent_machine_zero {nf} {r : Receipt} (h : validate nf r = true) |
| 67 | (ha : r.pres.machine = false) : r.machineId = 0 := by |
| 68 | have := (validate_parts h).1 |
| 69 | simp [presenceOk, ha] at this |
| 70 | exact this.1.1 |
| 71 | |
| 72 | /-- absent context id flag => generation and context id are zero -/ |
| 73 | theorem absent_context_zero {nf} {r : Receipt} (h : validate nf r = true) |
| 74 | (ha : r.pres.context = false) : r.generation = 0 ∧ r.contextId = 0 := by |
| 75 | have := (validate_parts h).1 |
| 76 | simp [presenceOk, ha] at this |
| 77 | exact this.1.2 |
| 78 | |
| 79 | /-- absent timestamp flag => observed time is zero -/ |
| 80 | theorem absent_time_zero {nf} {r : Receipt} (h : validate nf r = true) |
| 81 | (ha : r.pres.time = false) : r.observed = 0 := by |
| 82 | have := (validate_parts h).1 |
| 83 | simp [presenceOk, ha] at this |
| 84 | exact this.2 |
| 85 | |
| 86 | /-- the nonce is exactly the derived one (for the assumed derivation `nf`) -/ |
| 87 | theorem nonce_derived {nf} {r : Receipt} (h : validate nf r = true) : |
| 88 | r.nonce = nf r.verifier r.sequence := by |
| 89 | have := (validate_parts h).2.2.2.2 |
| 90 | simpa [nonceOk] using this |
| 91 | |
| 92 | /-! ## Finding: CanaryFailed decision is not tied to the CanaryFailed status |
| 93 | |
| 94 | `validate` has one arm for `Rejected` and one arm `_` for every other decision (Rust lines 336-354). |
| 95 | The `_` arm checks only stage = 0 and reason = 0. Nothing relates `ReceiptDecision::CanaryFailed` |
| 96 | (or `Destroyed`) to `ExecutionStatusCode::CanaryFailed`. The two theorems below state the |
| 97 | consequence exactly: for decisions other than Rejected, validity does not depend on which of the |
| 98 | three non-rejected decisions is recorded. -/ |
| 99 | |
| 100 | /-- swapping among the non-rejected decisions never changes validity -/ |
| 101 | theorem validate_decision_irrelevant_when_not_rejected (nf : Nat → Nat → Nat) (r : Receipt) |
| 102 | (d : Decision) (h0 : r.decision ≠ .rejected) (h1 : d ≠ .rejected) : |
| 103 | validate nf { r with decision := d } = validate nf r := by |
| 104 | have key : decisionOk { r with decision := d } = decisionOk r := by |
| 105 | unfold decisionOk |
| 106 | cases hd : r.decision <;> cases d <;> simp_all |
| 107 | simp [validate, presenceOk, codesOk, canaryOk, nonceOk, key] |
| 108 | |
| 109 | /-- a valid receipt with decision CanaryFailed need not have status CanaryFailed: |
| 110 | validity does not force the status either way (it only constrains Rejected and CANARY_PASSED). -/ |
| 111 | theorem canary_decision_status_unconstrained (nf : Nat → Nat → Nat) (r : Receipt) |
| 112 | (h : validate nf r = true) (hd : r.decision = .canaryFailed) (s : Status) |
| 113 | (hs : r.rflags.canaryPassed = false) : |
| 114 | validate nf { r with status := s } = true := by |
| 115 | have hp := validate_parts h |
| 116 | simp only [validate, Bool.and_eq_true] |
| 117 | refine ⟨⟨⟨⟨?_, ?_⟩, ?_⟩, ?_⟩, ?_⟩ |
| 118 | · exact hp.1 |
| 119 | · exact hp.2.1 |
| 120 | · have := hp.2.2.1 |
| 121 | simpa [decisionOk, hd] using this |
| 122 | · simp [canaryOk, hs] |
| 123 | · exact hp.2.2.2.2 |
| 124 | |
| 125 | end AienReceipt |