# Formal verification of Tezos smart contracts

What are the analyses of Tezos smart contracts that could benefit dapps writers the most?

Just to be clear, by “analysis” here I mean “static program analysis”. See for instance here.

Basically the idea would be that before committing a smart contract to the chain one would perform a static analysis on either the high level source code or alternatively directly the compilation target in michelson in order to assess various runtime properties of the program.

If we agree that the purpose of analyses is to both prove properties and help users of smart contracts to understand them, I would say:

1. Values: studying what values each element of the storage can take in the future.
2. Effects: studying what effects can occur in the future: typically what transfers can occur and on what conditions.
3. Ownership: who can trigger a change on what part of the storage.

So this is a huge question and I think there are many people more qualified than me, but I'll offer some initial guidance.

In Software Foundations, a book on Coq, they talk about an implied language called Imp. Imp has a syntax like:

``````Z ::= X;;
Y ::= 1;;
WHILE ~(Z = 0) DO
Y ::= Y * Z;;
Z ::= Z - 1
END
``````

Which should be somewhat easily understood as assignment and some simple looping. `::=` is for assignment, a while loop until z is 0. In python this would be:

``````def foo(x):
z = x
y = 1
while z != 0:
y = y * z
z -= 1
``````

We can then define some of the underlying logic for the symbols. For example,

``````Fixpoint aeval (a : aexp) : nat :=
match a with
| ANum n ⇒ n
| APlus a1 a2 ⇒ (aeval a1) + (aeval a2)
| AMinus a1 a2 ⇒ (aeval a1) - (aeval a2)
| AMult a1 a2 ⇒ (aeval a1) * (aeval a2)
end.
``````

This will define arithmetic operations.

You could also parse out reserved words, like:

``````Inductive com : Type :=
| CSkip
| CBreak (* <--- NEW *)
| CAss (x : string) (a : aexp)
| CSeq (c1 c2 : com)
| CIf (b : bexp) (c1 c2 : com)
| CWhile (b : bexp) (c : com).
``````

Then you could map the program to these defined types in Coq, like:

``````CSeq (CAss Z X)
(CSeq (CAss Y (S O))
(CWhile (BNot (BEq Z O))
(CSeq (CAss Y (AMult Y Z))
(CAss Z (AMinus Z (S O))))))
``````

We can then make some proofs about the functions or statements made in this language using formal logic. Here is an example proving that if z is not 4, then x is not 2:

``````Example ceval_example1:
empty_st =[
X ::= 2;;
TEST X ≤ 1
THEN Y ::= 3
ELSE Z ::= 4
FI
]⇒ (Z !-> 4 ; X !-> 2).
Proof.
(* We must supply the intermediate state *)
apply E_Seq with (X !-> 2).
- (* assignment command *)
apply E_Ass. reflexivity.
- (* if command *)
apply E_IfFalse.
reflexivity.
apply E_Ass. reflexivity.
Qed.
``````

By now I hope the application to a smart contract is somewhat apparent. If you could abstract the smart contract into Coq, you could use Coq to prove some components of your smart contract rigorously. There is also potential to outline conditions of a smart contract in Coq and compile it to Michelson, but that's just a possibility and I haven't seen any evidence of its construction.

• Thanks for the detailed answer. It seems you are explaining to me a strategy of how to make smart contracts amenable to formal analysis, here by using Coq. I guess my question was more focused on what sorts of results/guarantees are at the intersection of of what is achieveable by static analysis and desireable from a blockchain application perspective.
– Ezy
Jan 31, 2019 at 12:57
• if that's the question, you could just build a fuzzer. Contracts have very rigid inputs so it wouldn't be too hard to try a wide variety of inputs and see the responses given. en.wikipedia.org/wiki/Fuzzing
– Rob
Feb 1, 2019 at 18:46
• @Rob I feel that smart contracts must live in an adversarial world so simple debugging tools such are fuzzers might not be enough.
– FFF
Feb 2, 2019 at 3:43
• you could ALWAYS do more, but considering the very strict constraints on inputs fuzz testing from a variety of adversarial contracts would probably cover a large number of possible scenarios. I think a honeypot scenario like in solidity would be easier to test for and harder to orchestrate since all external calls happen after the contract's completion.
– Rob
Feb 2, 2019 at 17:47