libtlafmt 0.4.1

A formatter library for TLA+ specs, core of tlafmt
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
---
source: libtlafmt/tests/format.rs
expression: output
input_file: libtlafmt/tests/corpus/Nano.tla
---
--------------------------------- MODULE Nano ----------------------------------
(***************************************************************************)
(* An outdated and not-ultimately-useful specification of the original     *)
(* protocol used by the Nano blockchain. Primarily interesting as an       *)
(* example of how to model hash functions and cryptographic signatures,    *)
(* and the difficulties in using finite modelchecking to analyze           *)
(* blockchain-like data structures or anything else that records action    *)
(* history in an ordered way.                                              *)
(***************************************************************************)

EXTENDS
    Naturals,
    Bags

CONSTANTS
    Hash,                   \* The set of all 256-bit Blake2b block hashes
    CalculateHash(_,_,_) ,  \* An action calculating the hash of a block
    PrivateKey,             \* The set of all Ed25519 private keys
    PublicKey,              \* The set of all Ed25519 public keys
    KeyPair,                \* The public key paired with each private key
    Node,                   \* The set of all nodes in the network
    GenesisBalance,         \* The total number of coins in the network
    Ownership               \* The private key owned by each node

VARIABLES
    lastHash,               \* The last calculated block hash
    distributedLedger,      \* The distributed ledger of confirmed blocks
    received                \* The blocks received but not yet validated

ASSUME
    /\ \A data, oldHash, newHash :
        /\ CalculateHash(data, oldHash, newHash) \in BOOLEAN
    /\ KeyPair \in [PrivateKey -> PublicKey]
    /\ GenesisBalance \in Nat
    /\ Ownership \in [Node -> PrivateKey]

--------------------------------------------------------------------------------

(***************************************************************************)
(* Functions to sign hashes with private key and validate signatures       *)
(* against public key.                                                     *)
(***************************************************************************)

SignHash(hash, privateKey) ==
    [data |-> hash,
        signedWith |-> privateKey]

ValidateSignature(signature, expectedPublicKey, expectedHash) ==
    LET publicKey == KeyPair[signature.signedWith] IN
        /\ publicKey = expectedPublicKey
        /\ signature.data = expectedHash

Signature ==
    [data: Hash,
        signedWith: PrivateKey]

(***************************************************************************)
(* Defines the set of protocol-conforming blocks.                          *)
(***************************************************************************)

AccountBalance == 0..GenesisBalance

GenesisBlock ==
    [type: {"genesis"},
        account: PublicKey,
        balance: {GenesisBalance}]

SendBlock ==
    [previous: Hash,
        balance: AccountBalance,
        destination: PublicKey,
        type: {"send"}]

OpenBlock ==
    [account: PublicKey,
        source: Hash,
        rep: PublicKey,
        type: {"open"}]

ReceiveBlock ==
    [previous: Hash,
        source: Hash,
        type: {"receive"}]

ChangeRepBlock ==
    [previous: Hash,
        rep: PublicKey,
        type: {"change"}]

Block ==
    GenesisBlock
    \union SendBlock
    \union OpenBlock
    \union ReceiveBlock
    \union ChangeRepBlock

SignedBlock ==
    [block: Block,
        signature: Signature]

NoBlock == CHOOSE b: b \notin SignedBlock

NoHash == CHOOSE h: h \notin Hash

Ledger == [Hash -> SignedBlock \union {NoBlock}]

(***************************************************************************)
(* Utility functions to calculate block lattice properties.                *)
(***************************************************************************)

GenesisBlockExists ==
    /\ lastHash /= NoHash

IsAccountOpen(ledger, publicKey) ==
    /\ \E hash \in Hash:
        LET signedBlock == ledger[hash] IN
            /\ signedBlock /= NoBlock
            /\ signedBlock.block.type \in {"genesis", "open"}
            /\ signedBlock.block.account = publicKey

IsSendReceived(ledger, sourceHash) ==
    /\ \E hash \in Hash:
        LET signedBlock == ledger[hash] IN
            /\ signedBlock /= NoBlock
            /\ signedBlock.block.type \in {"open", "receive"}
            /\ signedBlock.block.source = sourceHash

RECURSIVE PublicKeyOf(_,_)
PublicKeyOf(ledger, blockHash) ==
    LET
        signedBlock == ledger[blockHash]
        block == signedBlock.block
    IN
        IF block.type \in {"genesis", "open"}
        THEN block.account
        ELSE PublicKeyOf(ledger, block.previous)

TopBlock(ledger, publicKey) ==
    CHOOSE hash \in Hash:
        LET signedBlock == ledger[hash] IN
            /\ signedBlock /= NoBlock
            /\ PublicKeyOf(ledger, hash) = publicKey
            /\ ~\E otherHash \in Hash:
                LET otherSignedBlock == ledger[otherHash] IN
                    /\ otherSignedBlock /= NoBlock
                    /\ otherSignedBlock.block.type \in {"send", "receive", "change"}
                    /\ otherSignedBlock.block.previous = hash

RECURSIVE BalanceAt(_, _)
RECURSIVE ValueOfSendBlock(_, _)

BalanceAt(ledger, hash) ==
    LET
        signedBlock == ledger[hash]
        block == signedBlock.block
    IN
        CASE block.type = "open" -> ValueOfSendBlock(ledger, block.source)
            [] block.type = "send" -> block.balance
            [] block.type = "receive" ->
                BalanceAt(ledger, block.previous)
                + ValueOfSendBlock(ledger, block.source)
            [] block.type = "change" -> BalanceAt(ledger, block.previous)
            [] block.type = "genesis" -> block.balance

ValueOfSendBlock(ledger, hash) ==
    LET
        signedBlock == ledger[hash]
        block == signedBlock.block
    IN BalanceAt(ledger, block.previous) - block.balance

(***************************************************************************)
(* The type & safety invariants.                                           *)
(***************************************************************************)

TypeInvariant ==
    /\ lastHash \in Hash \union {NoHash}
    /\ distributedLedger \in [Node -> Ledger]
    /\ received \in [Node -> SUBSET SignedBlock]

CryptographicInvariant ==
    /\ \A node \in Node:
        LET ledger == distributedLedger[node] IN
            /\ \A hash \in Hash:
                LET signedBlock == ledger[hash] IN
                    /\ signedBlock /= NoBlock =>
                        LET publicKey == PublicKeyOf(ledger, hash) IN
                            /\ ValidateSignature(
                                signedBlock.signature,
                                publicKey,
                                hash)

RECURSIVE SumBag(_)
SumBag(B) ==
    LET S == BagToSet(B) IN
        IF S = {}
        THEN 0
        ELSE
            LET e == CHOOSE x \in S: TRUE IN
                e + SumBag(B (-) SetToBag({e}))

BalanceInvariant ==
    /\ \A node \in Node:
            LET
                ledger == distributedLedger[node]
                openAccounts == {account \in PublicKey: IsAccountOpen(ledger, account)}
                topBlocks == {TopBlock(ledger, account): account \in openAccounts}
                accountBalances ==
                    LET ledgerBalanceAt(hash) == BalanceAt(ledger, hash) IN
                        BagOfAll(ledgerBalanceAt, SetToBag(topBlocks))
            IN
                /\ GenesisBlockExists =>
                    /\ SumBag(accountBalances) <= GenesisBalance

SafetyInvariant ==
    /\ CryptographicInvariant

(***************************************************************************)
(* Creates the genesis block.                                              *)
(***************************************************************************)

CreateGenesisBlock(privateKey) ==
    LET
        publicKey == KeyPair[privateKey]
        genesisBlock ==
            [type |-> "genesis",
                account |-> publicKey,
                balance |-> GenesisBalance]
    IN
        /\ ~GenesisBlockExists
        /\ CalculateHash(genesisBlock, lastHash, lastHash')
        /\ distributedLedger' =
            LET signedGenesisBlock ==
                [block |-> genesisBlock,
                    signature |-> SignHash(lastHash', privateKey)]
            IN
                [n \in Node |->
                    [distributedLedger[n] EXCEPT
                        ![lastHash'] =
                        signedGenesisBlock]]
        /\ UNCHANGED received
    
(***************************************************************************)
(* Creation, validation, and confirmation of open blocks. Checks include:  *)
(*  - The block is signed by the private key of the account being opened   *)
(*  - The node's ledger contains the referenced source block               *)
(*  - The source block is a send block to the account being opened         *)
(***************************************************************************)

ValidateOpenBlock(ledger, block) ==
    /\ block.type = "open"
    /\ ledger[block.source] /= NoBlock
    /\ ledger[block.source].block.type = "send"
    /\ ledger[block.source].block.destination = block.account

CreateOpenBlock(node) ==
    LET
        privateKey == Ownership[node]
        publicKey == KeyPair[privateKey]
        ledger == distributedLedger[node]
    IN
        /\ \E repPublicKey \in PublicKey:
            /\ \E srcHash \in Hash:
                LET newOpenBlock ==
                    [account |-> publicKey,
                        source |-> srcHash,
                        rep |-> repPublicKey,
                        type |-> "open"]
                IN
                    /\ ValidateOpenBlock(ledger, newOpenBlock)
                    /\ CalculateHash(newOpenBlock, lastHash, lastHash')
                    /\ received' =
                        LET signedOpenBlock ==
                            [block |-> newOpenBlock,
                                signature |-> SignHash(lastHash', privateKey)]
                        IN
                            [n \in Node |->
                                received[n] \union {signedOpenBlock}]
                    /\ UNCHANGED distributedLedger

ProcessOpenBlock(node, signedBlock) ==
    LET
        ledger == distributedLedger[node]
        block == signedBlock.block
    IN
        /\ ValidateOpenBlock(ledger, block)
        /\ ~IsAccountOpen(ledger, block.account)
        /\ CalculateHash(block, lastHash, lastHash')
        /\ ValidateSignature(signedBlock.signature, block.account, lastHash')
        /\ distributedLedger' =
            [distributedLedger EXCEPT
                ![node][lastHash'] = signedBlock]
    
(***************************************************************************)
(* Creation, validation, and confirmation of send blocks. Checks include:  *)
(*  - The node's ledger contains the referenced previous block             *)
(*  - The block is signed by the account sourcing the funds                *)
(*  - The value sent is non-negative                                       *)
(***************************************************************************)

ValidateSendBlock(ledger, block) ==
    /\ block.type = "send"
    /\ ledger[block.previous] /= NoBlock
    /\ block.balance <= BalanceAt(ledger, block.previous)

CreateSendBlock(node) ==
    LET
        privateKey == Ownership[node]
        publicKey == KeyPair[privateKey]
        ledger == distributedLedger[node]
    IN
        /\ \E prevHash \in Hash:
            /\ ledger[prevHash] /= NoBlock
            /\ PublicKeyOf(ledger, prevHash) = publicKey
            /\ \E recipient \in PublicKey:
                /\ \E newBalance \in AccountBalance:
                    LET newSendBlock ==
                        [previous |-> prevHash,
                            balance |-> newBalance,
                            destination |-> recipient,
                            type |-> "send"]
                    IN
                        /\ ValidateSendBlock(ledger, newSendBlock)
                        /\ CalculateHash(newSendBlock, lastHash, lastHash')
                        /\ received' =
                            LET signedSendBlock ==
                                [block |-> newSendBlock,
                                    signature |-> SignHash(lastHash', privateKey)]
                            IN
                                [n \in Node |->
                                    received[n] \union {signedSendBlock}]
                        /\ UNCHANGED distributedLedger

ProcessSendBlock(node, signedBlock) ==
    LET
        ledger == distributedLedger[node]
        block == signedBlock.block
    IN
        /\ ValidateSendBlock(ledger, block)
        /\ CalculateHash(block, lastHash, lastHash')
        /\ ValidateSignature(
            signedBlock.signature,
                PublicKeyOf(ledger, block.previous),
            lastHash')
        /\ distributedLedger' =
            [distributedLedger EXCEPT
                ![node][lastHash'] = signedBlock]
    
(***************************************************************************)
(* Creation, validation, & confirmation of receive blocks. Checks include: *)
(*  - The node's ledger contains the referenced previous & source blocks   *)
(*  - The block is signed by the account sourcing the funds                *)
(*  - The source block is a send block to the receive block's account      *)
(*  - The source block does not already have a corresponding receive/open  *)
(***************************************************************************)

ValidateReceiveBlock(ledger, block) ==
    /\ block.type = "receive"
    /\ ledger[block.previous] /= NoBlock
    /\ ledger[block.source] /= NoBlock
    /\ ledger[block.source].block.type = "send"
    /\ ledger[block.source].block.destination =
        PublicKeyOf(ledger, block.previous)

CreateReceiveBlock(node) ==
    LET
        privateKey == Ownership[node]
        publicKey == KeyPair[privateKey]
        ledger == distributedLedger[node]
    IN
        /\ \E prevHash \in Hash:
            /\ ledger[prevHash] /= NoBlock
            /\ PublicKeyOf(ledger, prevHash) = publicKey
            /\ \E srcHash \in Hash:
                LET newRcvBlock ==
                    [previous |-> prevHash,
                        source |-> srcHash,
                        type |-> "receive"]
                IN
                    /\ ValidateReceiveBlock(ledger, newRcvBlock)
                    /\ CalculateHash(newRcvBlock, lastHash, lastHash')
                    /\ received' =
                        LET signedRcvBlock ==
                            [block |-> newRcvBlock,
                                signature |-> SignHash(lastHash', privateKey)]
                        IN
                            [n \in Node |->
                                received[n] \union {signedRcvBlock}]
                    /\ UNCHANGED distributedLedger

ProcessReceiveBlock(node, signedBlock) ==
    LET
        block == signedBlock.block
        ledger == distributedLedger[node]
    IN
        /\ ValidateReceiveBlock(ledger, block)
        /\ ~IsSendReceived(ledger, block.source)
        /\ CalculateHash(block, lastHash, lastHash')
        /\ ValidateSignature(
            signedBlock.signature,
                PublicKeyOf(ledger, block.previous),
            lastHash')
        /\ distributedLedger' =
            [distributedLedger EXCEPT
                ![node][lastHash'] = signedBlock]
    
(***************************************************************************)
(* Creation, validation, & confirmation of change blocks. Checks include:  *)
(*  - The node's ledger contains the referenced previous block             *)
(*  - The block is signed by the correct account                           *)
(***************************************************************************)

ValidateChangeBlock(ledger, block) ==
    /\ block.type = "change"
    /\ ledger[block.previous] /= NoBlock

CreateChangeRepBlock(node) ==
    LET
        privateKey == Ownership[node]
        publicKey == KeyPair[privateKey]
        ledger == distributedLedger[node]
    IN
        /\ \E prevHash \in Hash:
            /\ ledger[prevHash] /= NoBlock
            /\ PublicKeyOf(ledger, prevHash) = publicKey
            /\ \E newRep \in PublicKey:
                LET newChangeRepBlock ==
                    [previous |-> prevHash,
                        rep |-> newRep,
                        type |-> "change"]
                IN
                    /\ ValidateChangeBlock(ledger, newChangeRepBlock)
                    /\ CalculateHash(newChangeRepBlock, lastHash, lastHash')
                    /\ received' =
                        LET signedChangeRepBlock ==
                            [block |-> newChangeRepBlock,
                                signature |-> SignHash(lastHash', privateKey)]
                        IN
                            [n \in Node |->
                                received[n] \union {signedChangeRepBlock}]
                    /\ UNCHANGED distributedLedger

ProcessChangeRepBlock(node, signedBlock) ==
    LET
        block == signedBlock.block
        ledger == distributedLedger[node]
    IN
        /\ ValidateChangeBlock(ledger, block)
        /\ CalculateHash(block, lastHash, lastHash')
        /\ ValidateSignature(
            signedBlock.signature,
                PublicKeyOf(ledger, block.previous),
            lastHash')
        /\ distributedLedger' =
            [distributedLedger EXCEPT
                ![node][lastHash'] = signedBlock]
    
(***************************************************************************)
(* Top-level actions.                                                      *)
(***************************************************************************)
CreateBlock(node) ==
    \/ CreateOpenBlock(node)
    \/ CreateSendBlock(node)
    \/ CreateReceiveBlock(node)
    \/ CreateChangeRepBlock(node)

ProcessBlock(node) ==
    /\ \E block \in received[node]:
        /\
            \/ ProcessOpenBlock(node, block)
            \/ ProcessSendBlock(node, block)
            \/ ProcessReceiveBlock(node, block)
            \/ ProcessChangeRepBlock(node, block)
        /\ received' = [received EXCEPT ![node] = @ \ {block}]

Init ==
    /\ lastHash = NoHash
    /\ distributedLedger = [n \in Node |-> [h \in Hash |-> NoBlock]]
    /\ received = [n \in Node |-> {}]

Next ==
    \/ \E account \in PrivateKey: CreateGenesisBlock(account)
    \/ \E node \in Node: CreateBlock(node)
    \/ \E node \in Node: ProcessBlock(node)

Spec ==
    /\ Init
    /\ [][Next]_<< lastHash, distributedLedger, received >>

THEOREM Safety == Spec => TypeInvariant /\ SafetyInvariant

================================================================================