interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / chain / Ledger.lean
| 1 | /-! |
| 2 | # The INTERPLANE ledger: its head fixes every record |
| 3 | |
| 4 | A ledger is a list of records, newest first. A record carries a `body` (sequence number, time, |
| 5 | kind, actor, subject and payload digest, as one value) and `prev`, the digest of the record before |
| 6 | it. `h body prev` models the SHA-256 digest of the record's line. |
| 7 | |
| 8 | `tamper_evident` says: if two valid ledgers have the same head digest, they are the same ledger, |
| 9 | provided `h` never maps two different (body, prev) pairs to one digest and never produces the |
| 10 | genesis value. So changing, removing, inserting or reordering any record changes the head, unless |
| 11 | someone finds a SHA-256 collision. Collision resistance itself is an assumption, not proved here. |
| 12 | -/ |
| 13 | |
| 14 | structure Rec (D : Type) where |
| 15 | body : D |
| 16 | prev : D |
| 17 | |
| 18 | /-- The digest a ledger is known by: the genesis value when empty, else its newest record's digest. -/ |
| 19 | def headOf {D : Type} (h : D → D → D) (g : D) : List (Rec D) → D |
| 20 | | [] => g |
| 21 | | r :: _ => h r.body r.prev |
| 22 | |
| 23 | /-- Every record names the head of the ledger below it. -/ |
| 24 | def Valid {D : Type} (h : D → D → D) (g : D) : List (Rec D) → Prop |
| 25 | | [] => True |
| 26 | | r :: rs => r.prev = headOf h g rs ∧ Valid h g rs |
| 27 | |
| 28 | theorem tamper_evident {D : Type} (h : D → D → D) (g : D) |
| 29 | (hinj : ∀ a b a' b', h a b = h a' b' → a = a' ∧ b = b') |
| 30 | (hg : ∀ a b, h a b ≠ g) : |
| 31 | ∀ l1 l2 : List (Rec D), Valid h g l1 → Valid h g l2 → |
| 32 | headOf h g l1 = headOf h g l2 → l1 = l2 := by |
| 33 | intro l1 |
| 34 | induction l1 with |
| 35 | | nil => |
| 36 | intro l2 _ _ hh |
| 37 | cases l2 with |
| 38 | | nil => rfl |
| 39 | | cons r rs => exact absurd hh.symm (hg r.body r.prev) |
| 40 | | cons r rs ih => |
| 41 | intro l2 v1 v2 hh |
| 42 | cases l2 with |
| 43 | | nil => exact absurd hh (hg r.body r.prev) |
| 44 | | cons r' rs' => |
| 45 | have hp := hinj _ _ _ _ hh |
| 46 | have hrs : rs = rs' := by |
| 47 | apply ih rs' v1.2 v2.2 |
| 48 | rw [← v1.1, ← v2.1] |
| 49 | exact hp.2 |
| 50 | cases r with |
| 51 | | mk b p => |
| 52 | cases r' with |
| 53 | | mk b' p' => |
| 54 | have hb : b = b' := hp.1 |
| 55 | have hq : p = p' := hp.2 |
| 56 | rw [hb, hq, hrs] |
| 57 | |
| 58 | #print axioms tamper_evident |