1- {-
2-
3- {-# LANGUAGE DerivingStrategies #-}
4- {-# LANGUAGE GeneralizedNewtypeDeriving #-}
5-
6- -}
7-
8-
91module Peras.Chain where
102
11- {-
12- import Data.ByteString (ByteString)
13- import Data.Set (Set)
14- import Data.Word (Word64)
15- import Peras.Block (Block, PartyId (..))
16- import Peras.Crypto (MembershipProof, Signature, isCommitteeMember, verify)
17- -}
18-
193open import Agda.Builtin.Word
204open import Data.Bool
5+ open import Data.List as List using (List; all; foldr)
216open import Level
227open import Data.Tree.AVL.Sets renaming (⟨Set⟩ to set)
8+ open import Relation.Unary using (Pred)
239open import Relation.Binary using (StrictTotalOrder)
2410
11+ import Relation.Binary.PropositionalEquality as Eq
12+ open Eq using (_≡_; refl; cong; sym)
13+
2514open import Peras.Crypto
2615open import Peras.Block
2716
2817record RoundNumber : Set where
2918 field roundNumber : Word64
30- -- deriving newtype (Eq, Show, Ord)
31-
3219
3320record Vote msg : Set where
3421 constructor vote
@@ -37,11 +24,11 @@ record Vote msg : Set where
3724 committeeMembershipProof : MembershipProof
3825 blockHash : msg
3926 signature : Signature
40- -- deriving stock (Eq, Show, Ord)
4127
42- postulate vblEq : Relation.Binary.Rel (Vote Block) zero
43- vblLt : Relation.Binary.Rel (Vote Block) zero
44- vblIs : Relation.Binary.IsStrictTotalOrder vblEq vblLt
28+ postulate
29+ vblEq : Relation.Binary.Rel (Vote Block) zero
30+ vblLt : Relation.Binary.Rel (Vote Block) zero
31+ vblIs : Relation.Binary.IsStrictTotalOrder vblEq vblLt
4532
4633VoteBlockO : StrictTotalOrder zero zero zero
4734VoteBlockO = record {
@@ -53,7 +40,8 @@ VoteBlockO = record {
5340toSignable : ∀ {msg} → Vote msg -> ByteString
5441toSignable _ = emptyBS -- const ""
5542
56- postulate makeVote : ∀ {msg} → RoundNumber -> PartyId -> msg -> Vote msg
43+ postulate
44+ makeVote : ∀ {msg} → RoundNumber -> PartyId -> msg -> Vote msg
5745
5846-- | A vote is valid if the committee-membership proof and the signature are valid.
5947
@@ -65,13 +53,11 @@ isValid v@(vote _ (mkPartyId vkey) committeeMembershipProof _ signature) =
6553record Chain : Set where
6654 constructor chain
6755 field blocks : set BlockO
68- tip : Block
69- -- ^ The tip of this chain, must be a member of `blocks`.
70- votes : set VoteBlockO
71- -- ^ The set of "pending" votes, eg. which have not been included in
72- -- a `Block`.
56+ tip : Block -- The tip of this chain, must be a member of `blocks`
57+ votes : set VoteBlockO -- The set of "pending" votes, eg. which have not been included in a `Block`.
58+
59+
7360
74- -- deriving stock (Eq, Show)
7561
7662-- | Chain validity
7763--
@@ -89,5 +75,105 @@ record Chain : Set where
8975-- TODO: expressing those conditions directly would be very expensive,
9076-- it's more efficient to enforce them whenever the chain is extended.
9177
92- postulate isValidChain : Chain -> Bool
9378
79+ postulate
80+ verifyLeadershipProof : Block → Bool
81+
82+ properlyLinked : Chain → Bool
83+ decreasingSlots : Chain → Bool
84+
85+ correctBlocks : Chain → Bool
86+ correctBlocks (chain blocks _ _) =
87+ let bs = toList BlockO blocks
88+ in all verifyLeadershipProof bs
89+
90+ postulate
91+ isValidChain : Chain -> Bool
92+
93+
94+ {-
95+ Formalizing Nakamoto-Style Proof of Stake
96+ Søren Eller Thomsen and Bas Spitters
97+ -}
98+
99+ -- progress
100+
101+ data Progress : Set where
102+
103+ Ready : Progress
104+ Delivered : Progress
105+ Baked : Progress
106+
107+ record Message : Set where
108+ constructor mkMessage
109+ field
110+ msg : ByteString
111+
112+ -- global state
113+
114+ record GlobalState : Set where
115+ constructor ⟪_,_,_,_⟫
116+ field
117+ slot : Slot
118+ progress : Progress
119+ messages : List Message
120+ execution-order : List PartyId
121+
122+ open GlobalState
123+
124+ postulate
125+ party_bake_step_world : PartyId → GlobalState → GlobalState
126+ party_rcv_step_world : PartyId → GlobalState → GlobalState
127+ incrementSlot : Slot → Slot
128+ permParties : List PartyId → List PartyId
129+ permMessages : List Message → List Message
130+
131+ data _↝_ : GlobalState → GlobalState → Set where
132+
133+ Deliver : ∀ {s ms ps}
134+ → ⟪ s , Ready , ms , ps ⟫ ↝
135+ let gs = List.foldr party_rcv_step_world ⟪ s , Ready , ms , ps ⟫ ps
136+ in record gs { progress = Delivered }
137+
138+ Bake : ∀ {s ms ps}
139+ → ⟪ s , Delivered , ms , ps ⟫ ↝
140+ let gs = List.foldr party_bake_step_world ⟪ s , Delivered , ms , ps ⟫ ps
141+ in record gs { progress = Delivered }
142+
143+ NextRound : ∀ {s ms ps}
144+ → ⟪ s , Baked , ms , ps ⟫ ↝ ⟪ incrementSlot s , Ready , ms , ps ⟫
145+
146+ PermParties : ∀ {s p ms ps}
147+ → ⟪ s , p , ms , ps ⟫ ↝ ⟪ s , p , ms , permParties ps ⟫
148+
149+ PermMsgs : ∀ {s p ms ps}
150+ → ⟪ s , p , ms , ps ⟫ ↝ ⟪ s , p , permMessages ms , ps ⟫
151+
152+ -- reflexive, transitive closure (which is big-step in the paper)
153+
154+ infix 2 _↝⋆_
155+ infixr 2 _↝⟨_⟩_
156+ infix 3 _∎
157+
158+ data _↝⋆_ : GlobalState → GlobalState → Set where
159+
160+ _∎ : ∀ M
161+ -------
162+ → M ↝⋆ M
163+
164+ _↝⟨_⟩_ : ∀ L {M N}
165+ → L ↝ M
166+ → M ↝⋆ N
167+ ------
168+ → L ↝⋆ N
169+
170+ {-
171+ -- knowledge propagation
172+ lemma_1 : ∀ (N₀ N₁ N₂ : GlobalState)
173+ → (p₁ p₂ : PartyId)
174+ → Nₒ ↝⋆ N₁
175+ → N₁ ↝⋆ N₂
176+ → progress N₁ ≡ Ready
177+ → progress N₂ ≡ Delivered
178+ → allBlocks t₁ ⊂ allBlocks t₂
179+ -}
0 commit comments