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 / Model.lean
| 1 | /-! |
| 2 | # Abstract model of the Rust admission receipt (DEVELOPMENT_ORACLE) |
| 3 | |
| 4 | Source modelled: aien-dev/aienux `crates/aienos-artifact/src/receipt.rs` at 2ee61b4 |
| 5 | (`validate`, `decode`, `encode`, `valid_reason`; consumer `build_receipt` in |
| 6 | `crates/aienos-kernel/src/artifact_loader.rs` is read for context only). |
| 7 | |
| 8 | Invariant: AIEN.INV.RECEIPT.REJECTION_CONSISTENCY.V1 (formal/invariants/artifact-receipt-v1.toml). |
| 9 | |
| 10 | Abstractions (each is an ASSUMPTION, not a proof): |
| 11 | * Cryptography. SHA-256 nonce derivation is an arbitrary function `nf`; every theorem holds for |
| 12 | EVERY `nf`. Ed25519 signature checking is not modelled (it lives in `verify`, not `validate`). |
| 13 | The signature block is reduced to one Boolean (`sigOk`: all zero, or well formed algorithm id). |
| 14 | * Digests (32 bytes) are naturals; "all zero" is `= 0`. Fixed-width integer overflow is not modelled. |
| 15 | * Little-endian byte packing is not modelled. `Raw` is the record after splitting into fixed-width |
| 16 | fields at the offsets of `AienReceipt.Layout`. That the byte packing is a bijection per field is assumed. |
| 17 | * `exit_status` is an `Int` (Rust `i32`). |
| 18 | * Presence flags and result flags are structures of Booleans, so unknown bits cannot be written. |
| 19 | `decode` rejects unknown bits on the Raw word first (see `Layout.lean`). |
| 20 | -/ |
| 21 | namespace AienReceipt |
| 22 | |
| 23 | inductive Decision | admitted | rejected | canaryFailed | destroyed |
| 24 | deriving DecidableEq, Repr |
| 25 | |
| 26 | inductive Tier | seed0bQemu | seed0bMachine1 |
| 27 | deriving DecidableEq, Repr |
| 28 | |
| 29 | /-- Rust `ExecutionStatusCode` (offset 64). -/ |
| 30 | inductive Status | notRun | exited | timeout | fault | badSyscall | resourceOverrun | canaryFailed |
| 31 | deriving DecidableEq, Repr |
| 32 | |
| 33 | /-- Presence flags (offset 16): bits 0 machine id, 1 context id, 2 timestamp. -/ |
| 34 | structure Presence where |
| 35 | machine : Bool |
| 36 | context : Bool |
| 37 | time : Bool |
| 38 | deriving DecidableEq, Repr |
| 39 | |
| 40 | /-- Result flags (offset 72): bits 0..7 in the order of the Rust `RESULT_*` constants. -/ |
| 41 | structure ResultFlags where |
| 42 | readOk : Bool |
| 43 | writeDenied : Bool |
| 44 | reclaimed : Bool |
| 45 | canaryPassed : Bool |
| 46 | forgedDenied : Bool |
| 47 | mappedMatch : Bool |
| 48 | executedMatch : Bool |
| 49 | wxSealed : Bool |
| 50 | deriving DecidableEq, Repr |
| 51 | |
| 52 | /-- In-memory receipt. Digest fields that no rule inspects are kept as one opaque list. -/ |
| 53 | structure Receipt where |
| 54 | pres : Presence |
| 55 | decision : Decision |
| 56 | tier : Tier |
| 57 | sequence : Nat |
| 58 | nonce : Nat |
| 59 | observed : Nat |
| 60 | status : Status |
| 61 | exitCode : Int |
| 62 | rflags : ResultFlags |
| 63 | stage : Nat |
| 64 | reason : Nat |
| 65 | syscalls : Nat |
| 66 | reads : Nat |
| 67 | denials : Nat |
| 68 | frames : Nat |
| 69 | digests : List Nat |
| 70 | verifier : Nat |
| 71 | machineId : Nat |
| 72 | generation : Nat |
| 73 | contextId : Nat |
| 74 | deriving DecidableEq, Repr |
| 75 | |
| 76 | /-- Rust `MAX_REJECTION_STAGE`. -/ |
| 77 | def maxStage : Nat := 9 |
| 78 | |
| 79 | /-- Rust `valid_reason`: 1..=19 or 0x101..=0x10a. -/ |
| 80 | def validReason (c : Nat) : Bool := |
| 81 | (decide (1 ≤ c) && decide (c ≤ 19)) || (decide (0x101 ≤ c) && decide (c ≤ 0x10a)) |
| 82 | |
| 83 | /-- `result_flags & !RESULT_RECLAIMED == 0`. -/ |
| 84 | def ResultFlags.onlyReclaimed (f : ResultFlags) : Bool := |
| 85 | !f.readOk && !f.writeDenied && !f.canaryPassed && !f.forgedDenied && |
| 86 | !f.mappedMatch && !f.executedMatch && !f.wxSealed |
| 87 | |
| 88 | /-- Rust lines 322-330: an absent presence flag forces the value to its zero. -/ |
| 89 | def presenceOk (r : Receipt) : Bool := |
| 90 | (r.pres.machine || r.machineId == 0) && |
| 91 | (r.pres.context || (r.generation == 0 && r.contextId == 0)) && |
| 92 | (r.pres.time || r.observed == 0) |
| 93 | |
| 94 | /-- Rust lines 331-335. -/ |
| 95 | def codesOk (r : Receipt) : Bool := |
| 96 | decide (r.stage ≤ maxStage) && (r.reason == 0 || validReason r.reason) |
| 97 | |
| 98 | /-- Rust lines 336-355: the decision match. -/ |
| 99 | def decisionOk (r : Receipt) : Bool := |
| 100 | match r.decision with |
| 101 | | .rejected => |
| 102 | r.stage != 0 && r.reason != 0 && r.status == .notRun && r.exitCode == 0 && |
| 103 | r.syscalls == 0 && r.reads == 0 && r.denials == 0 && r.rflags.onlyReclaimed |
| 104 | | _ => r.stage == 0 && r.reason == 0 |
| 105 | |
| 106 | /-- Rust lines 356-360. -/ |
| 107 | def canaryOk (r : Receipt) : Bool := |
| 108 | !r.rflags.canaryPassed || (r.status == .exited && r.exitCode == 0) |
| 109 | |
| 110 | /-- Rust line 361, with the SHA-256 nonce function `nf` abstracted. -/ |
| 111 | def nonceOk (nf : Nat → Nat → Nat) (r : Receipt) : Bool := |
| 112 | r.nonce == nf r.verifier r.sequence |
| 113 | |
| 114 | /-- Rust `validate`, `Ok` exactly when this is `true`. -/ |
| 115 | def validate (nf : Nat → Nat → Nat) (r : Receipt) : Bool := |
| 116 | presenceOk r && codesOk r && decisionOk r && canaryOk r && nonceOk nf r |
| 117 | |
| 118 | end AienReceipt |