| |
| |
|
|
| import Init |
|
|
| namespace HyperKitty.Integration |
|
|
| |
| |
| |
|
|
| / |
| structure Ledger where |
| s : Int |
| delta : Int |
| iota : Int |
| omega : Int |
|
|
| / |
| inductive RhetoricSymbol : Type where |
| | Pi | Gamma | Delta | Omega | Lambda | Psi |
|
|
| / |
| structure QRAState where |
| current : RhetoricSymbol |
| previous : RhetoricSymbol |
|
|
| |
| |
| |
|
|
| / |
| def wire_encode (sym : RhetoricSymbol) : Nat := |
| match sym with |
| | RhetoricSymbol.Pi => 0x01 |
| | RhetoricSymbol.Gamma => 0x03 |
| | RhetoricSymbol.Delta => 0x04 |
| | RhetoricSymbol.Omega => 0x0A |
| | RhetoricSymbol.Lambda => 0xFF |
| | RhetoricSymbol.Psi => 0x0B |
|
|
| / |
| def wire_decode (byte : Nat) : Option RhetoricSymbol := |
| match byte with |
| | 0x01 => some RhetoricSymbol.Pi |
| | 0x03 => some RhetoricSymbol.Gamma |
| | 0x04 => some RhetoricSymbol.Delta |
| | 0x0A => some RhetoricSymbol.Omega |
| | 0xFF => some RhetoricSymbol.Lambda |
| | 0x0B => some RhetoricSymbol.Psi |
| | _ => none |
|
|
| |
| |
| |
|
|
| / |
| def ledger_to_rhetoric (l : Ledger) : RhetoricSymbol := |
| if l.delta > 0 then RhetoricSymbol.Pi |
| else if l.delta < 0 then RhetoricSymbol.Omega |
| else RhetoricSymbol.Lambda |
|
|
| |
| |
| |
|
|
| / |
| theorem wire_encode_injective : ∀ a b : RhetoricSymbol, |
| wire_encode a = wire_encode b → a = b := by |
| intro a b hab |
| cases a <;> cases b <;> simp at hab ⊢ <;> try rfl |
|
|
| theorem wire_decode_encode (sym : RhetoricSymbol) : |
| wire_decode (wire_encode sym) = some sym := by |
| cases sym <;> rfl |
|
|
| |
| |
| |
|
|
| / |
| def predict_next (state : QRAState) : RhetoricSymbol := |
| match state.current, state.previous with |
| | RhetoricSymbol.Pi, RhetoricSymbol.Pi => RhetoricSymbol.Gamma |
| | RhetoricSymbol.Pi, RhetoricSymbol.Gamma => RhetoricSymbol.Delta |
| | RhetoricSymbol.Pi, RhetoricSymbol.Delta => RhetoricSymbol.Omega |
| | RhetoricSymbol.Pi, RhetoricSymbol.Omega => RhetoricSymbol.Lambda |
| | RhetoricSymbol.Pi, RhetoricSymbol.Lambda => RhetoricSymbol.Psi |
| | RhetoricSymbol.Pi, RhetoricSymbol.Psi => RhetoricSymbol.Pi |
| | RhetoricSymbol.Gamma, RhetoricSymbol.Pi => RhetoricSymbol.Delta |
| | RhetoricSymbol.Gamma, RhetoricSymbol.Gamma => RhetoricSymbol.Omega |
| | RhetoricSymbol.Gamma, RhetoricSymbol.Delta => RhetoricSymbol.Lambda |
| | RhetoricSymbol.Gamma, RhetoricSymbol.Omega => RhetoricSymbol.Psi |
| | RhetoricSymbol.Gamma, RhetoricSymbol.Lambda => RhetoricSymbol.Pi |
| | RhetoricSymbol.Gamma, RhetoricSymbol.Psi => RhetoricSymbol.Gamma |
| | RhetoricSymbol.Delta, RhetoricSymbol.Pi => RhetoricSymbol.Omega |
| | RhetoricSymbol.Delta, RhetoricSymbol.Gamma => RhetoricSymbol.Lambda |
| | RhetoricSymbol.Delta, RhetoricSymbol.Delta => RhetoricSymbol.Psi |
| | RhetoricSymbol.Delta, RhetoricSymbol.Omega => RhetoricSymbol.Pi |
| | RhetoricSymbol.Delta, RhetoricSymbol.Lambda => RhetoricSymbol.Gamma |
| | RhetoricSymbol.Delta, RhetoricSymbol.Psi => RhetoricSymbol.Delta |
| | RhetoricSymbol.Omega, _ => RhetoricSymbol.Omega |
| | RhetoricSymbol.Lambda, RhetoricSymbol.Pi => RhetoricSymbol.Pi |
| | RhetoricSymbol.Lambda, RhetoricSymbol.Gamma => RhetoricSymbol.Gamma |
| | RhetoricSymbol.Lambda, RhetoricSymbol.Delta => RhetoricSymbol.Delta |
| | RhetoricSymbol.Lambda, RhetoricSymbol.Omega => RhetoricSymbol.Omega |
| | RhetoricSymbol.Lambda, RhetoricSymbol.Lambda => RhetoricSymbol.Lambda |
| | RhetoricSymbol.Lambda, RhetoricSymbol.Psi => RhetoricSymbol.Psi |
| | RhetoricSymbol.Psi, RhetoricSymbol.Pi => RhetoricSymbol.Psi |
| | RhetoricSymbol.Psi, RhetoricSymbol.Gamma => RhetoricSymbol.Pi |
| | RhetoricSymbol.Psi, RhetoricSymbol.Delta => RhetoricSymbol.Gamma |
| | RhetoricSymbol.Psi, RhetoricSymbol.Omega => RhetoricSymbol.Delta |
| | RhetoricSymbol.Psi, RhetoricSymbol.Lambda => RhetoricSymbol.Omega |
| | RhetoricSymbol.Psi, RhetoricSymbol.Psi => RhetoricSymbol.Psi |
|
|
| |
| |
| |
|
|
| / |
| def Ledger.evolve (l : Ledger) (d_l : Ledger) : Option Ledger := |
| if d_l.s = 0 ∧ d_l.iota + d_l.delta = 0 then |
| some { |
| s := l.s + d_l.delta |
| delta := l.delta + d_l.delta |
| iota := -(l.delta + d_l.delta) |
| omega := l.omega + d_l.omega |
| } |
| else |
| none |
|
|
| / |
| / |
| theorem evolve_valid (l d_l : Ledger) |
| (h_balance : d_l.iota + d_l.delta = 0) |
| (h_inv : d_l.s = 0) : |
| ∃ l' : Ledger, l.evolve d_l = some l' := by |
| use { |
| s := l.s + d_l.delta |
| delta := l.delta + d_l.delta |
| iota := -(l.delta + d_l.delta) |
| omega := l.omega + d_l.omega |
| } |
| show Ledger.evolve l d_l = some _ |
| unfold Ledger.evolve |
| simp [h_inv, h_balance] |
|
|
| |
| |
| |
|
|
| / |
| theorem ledger_to_qra_evolution (l1 l2 : Ledger) : |
| ∃ next : RhetoricSymbol, |
| next = predict_next ⟨ledger_to_rhetoric l1, ledger_to_rhetoric l2⟩ := |
| ⟨predict_next ⟨ledger_to_rhetoric l1, ledger_to_rhetoric l2⟩, rfl⟩ |
|
|
| |
| |
| |
|
|
| / |
| def serialize_ledger (l : Ledger) : List Nat := |
| [wire_encode (ledger_to_rhetoric l), l.delta.natAbs % 256, l.omega.natAbs % 256] |
|
|
| / |
| theorem serialize_length (l : Ledger) : |
| (serialize_ledger l).length = 3 := rfl |
|
|
| / |
| theorem serialize_symbol_preserved (l : Ledger) : |
| wire_decode (wire_encode (ledger_to_rhetoric l)) = some (ledger_to_rhetoric l) := |
| wire_decode_encode (ledger_to_rhetoric l) |
|
|
| |
| |
| |
|
|
| / |
| theorem wire_roundtrip (l : Ledger) : |
| wire_decode (wire_encode (ledger_to_rhetoric l)) = some (ledger_to_rhetoric l) := |
| wire_decode_encode (ledger_to_rhetoric l) |
|
|
| |
| |
| |
|
|
| / |
| theorem sla_to_wire_deterministic (l : Ledger) : |
| let sym := ledger_to_rhetoric l |
| let byte := wire_encode sym |
| wire_decode byte = some sym := |
| wire_decode_encode (ledger_to_rhetoric l) |
|
|
| / |
| theorem symbol_has_encoding (sym : RhetoricSymbol) : |
| ∃ byte : Nat, wire_encode sym = byte := |
| ⟨wire_encode sym, rfl⟩ |
|
|
| end HyperKitty.Integration |
|
|