Skip to content

Commit b1e369c

Browse files
committed
[Dijkstra] CIP-159-11b: Prove UTxO and UTxOW preservation of value (#1186)
1 parent c4f6a1b commit b1e369c

5 files changed

Lines changed: 880 additions & 0 deletions

File tree

src/Ledger/Dijkstra/Specification/Utxo/Properties.lagda.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ module Ledger.Dijkstra.Specification.Utxo.Properties where
1414

1515
```agda
1616
open import Ledger.Dijkstra.Specification.Utxo.Properties.Computational
17+
open import Ledger.Dijkstra.Specification.Utxo.Properties.Base
18+
open import Ledger.Dijkstra.Specification.Utxo.Properties.PoV
1719
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
source_branch: 1123-cip-159-11-prove-pov-and-invariants
3+
source_path: src/Ledger/Dijkstra/Specification/Utxo/Properties/Base.lagda.md
4+
---
5+
6+
# UTXO Properties: Base lemmas (Dijkstra era)
7+
8+
This module collects era-independent helper lemmas used by
9+
`Utxo.Properties.PoV`{.AgdaModule}. It currently contains:
10+
11+
+ `∙-homo-Coin`{.AgdaFunction}: the `coin` monoid-homomorphism's `_∙_` property.
12+
+ `newTxid⇒disj`{.AgdaFunction}: freshness of `TxIdOf tx` in an existing
13+
`utxo` implies `disjoint'`{.AgdaFunction} of `dom utxo`{.AgdaFunction} and
14+
`dom (outs tx)`{.AgdaFunction}.
15+
+ `outs-disjoint`{.AgdaFunction}: the specialisation used by
16+
`Utxo.Properties.PoV`{.AgdaModule}.
17+
18+
**Not yet ported from Conway**: the `balance`{.AgdaFunction}-arithmetic lemmas
19+
(`balance-cong`{.AgdaFunction}, `balance-cong-coin`{.AgdaFunction},
20+
`balance-∪`{.AgdaFunction}, `split-balance`{.AgdaFunction}). Conway's versions
21+
rely on `indexedSumᵐ-cong`{.AgdaFunction} over a finite map of hashed TxOuts;
22+
Dijkstra's `balance`{.AgdaFunction} is defined as `∑ˢ[ v ← valuesOfUTxO utxo ] v`
23+
(a sum over a `ℙ Value`{.AgdaFunction}), so the Conway proofs don't apply
24+
verbatim. Porting them requires the set-theoretic cong lemmas from
25+
`abstract-set-theory.FiniteSetTheory`{.AgdaModule}; for now, `balance-∪`{.AgdaFunction}
26+
and `split-balance`{.AgdaFunction} remain as module parameters to
27+
`Utxo.Properties.PoV`{.AgdaModule}.
28+
29+
<!--
30+
```agda
31+
{-# OPTIONS --safe #-}
32+
33+
open import Ledger.Dijkstra.Specification.Abstract using (AbstractFunctions)
34+
open import Ledger.Dijkstra.Specification.Transaction
35+
36+
module Ledger.Dijkstra.Specification.Utxo.Properties.Base
37+
(txs : _) (open TransactionStructure txs)
38+
(abs : AbstractFunctions txs) (open AbstractFunctions abs)
39+
where
40+
41+
open import Prelude; open Equivalence
42+
open import Ledger.Prelude hiding (≤-trans; ≤-antisym; All); open Properties
43+
open import Ledger.Dijkstra.Specification.Utxo txs abs
44+
45+
open import Algebra.Morphism using (module MonoidMorphisms; IsMagmaHomomorphism)
46+
47+
open MonoidMorphisms.IsMonoidHomomorphism
48+
49+
private variable
50+
ℓ : TxLevel
51+
```
52+
-->
53+
54+
## `∙-homo-Coin`
55+
56+
```agda
57+
∙-homo-Coin : ∀ (x y : Value) → coin (x + y) ≡ coin x + coin y
58+
∙-homo-Coin = IsMagmaHomomorphism.homo (isMagmaHomomorphism coinIsMonoidHomomorphism)
59+
```
60+
61+
## `coin-∑ˡ`
62+
63+
`coin`{.AgdaField} is a monoid homomorphism from `Value`{.AgdaField} (under `+ᵛ`/`ε`) to
64+
``{.AgdaDatatype} (under `+`/`0`), so it distributes over a list-indexed sum. This
65+
is the "coin version" of the generic fact that a monoid homomorphism commutes with
66+
`foldr _∙_ ε`.
67+
68+
```agda
69+
coin-∑ˡ : ∀ {A : Type} (f : A → Value) (xs : List A)
70+
→ coin (∑ˡ[ x ← xs ] f x) ≡ sum (map (coin ∘ f) xs)
71+
coin-∑ˡ f [] = ε-homo coinIsMonoidHomomorphism
72+
coin-∑ˡ f (x ∷ xs) = trans (∙-homo-Coin (f x) (∑ˡ[ z ← xs ] f z))
73+
(cong (coin (f x) +_) (coin-∑ˡ f xs))
74+
```
75+
76+
77+
## Freshness ⇒ disjointness
78+
79+
```agda
80+
module _ (tx : Tx ℓ) (let open Tx tx; open TxBody txBody)
81+
{utxo : UTxO} where
82+
83+
newTxid⇒disj : TxIdOf tx ∉ mapˢ proj₁ (dom utxo)
84+
→ disjoint' (dom utxo) (dom (outs tx))
85+
newTxid⇒disj id∉utxo =
86+
disjoint⇒disjoint' λ h h' → id∉utxo $ to ∈-map
87+
(-, (case from ∈-map h' of λ where
88+
(_ , refl , h'') → case from ∈-map h'' of λ where (_ , refl , _) → refl) , h)
89+
90+
-- Specialisation used by Utxo.Properties.PoV:
91+
-- freshness ⇒ (utxo ∣ SpendInputs ᶜ) and outs are disjoint.
92+
outs-disjoint : TxIdOf tx ∉ mapˢ proj₁ (dom utxo)
93+
→ disjoint (dom (utxo ∣ SpendInputsOf tx ᶜ)) (dom (outs tx))
94+
outs-disjoint fresh =
95+
λ h₁ h₂ → ∉-∅ $ proj₁ (newTxid⇒disj fresh) $ to ∈-∩ (res-comp-domᵐ h₁ , h₂)
96+
```

0 commit comments

Comments
 (0)