The EasyCrypt Tool

136
The EasyCrypt Tool The EasyCrypt Team François Dupressoir IMDEA Software Institute and INRIA Pisa — June 3rd–6th, 2014

Transcript of The EasyCrypt Tool

Page 1: The EasyCrypt Tool

The EasyCrypt Tool

The EasyCrypt TeamFrançois Dupressoir

IMDEA Software Institute and INRIA

Pisa — June 3rd–6th, 2014

Page 2: The EasyCrypt Tool

Welcome

Acknowledgements:I The EasyCrypt team: Gilles Barthe, Pierre-Yves Strub,

Benjamin Grégoire, César Kunz, Juan Manuel Crespo,Benedikt Schmidt

Organization:I Lectures: overview of key components (∼ 1/3)I Labs: hands-on experience (∼ 2/3)

Course URL (slides, exercises, related reading):

https://www.easycrypt.info/trac/wiki/CoursePisa2014

Page 3: The EasyCrypt Tool

EasyCrypt in a nusthell

I EasyCrypt is a tool-assisted platform for proving security ofcryptographic constructions in the computational model

I Views cryptographic proofs as relational verification ofopen parametric probabilistic programs

Goals:I Leverage PL and PV techniques for cryptographic proofsI Be accessible to cryptographers (choice of PL)I Support high-level reasoning principles (still ongoing)I Provide reasonable level of automationI Reuse off-the-shelf verification tools (we use Why3)

Page 4: The EasyCrypt Tool

EasyCrypt usage

I EasyCrypt is generic: no restrictions on+ primitives and protocols+ security notions and assumptions

I Can be used interactively or as a certifying back-end+ for cryptographic compilers (Zero-Knowledge)+ for domain-specific logics (ZooCrypt)

I Can verify implementations+ C-mode (+ CompCert)+ ML code extraction from verified specifications

Page 5: The EasyCrypt Tool

EasyCrypt: Languages

A higher-order pure expression language:I User-extensible,I first-class distributions (α distr),I Used to describe abstract functional primitives.

A typed imperative language (pWhile):I Used to describe schemes, oracles, adversaries, games...

C ::= skip skip| V = E assignment| V = $D random sampling| C; C sequence| if E then C else C conditional| while E do C while loop| V = F(E , . . . , E) procedure call

Page 6: The EasyCrypt Tool

Semantics of programs

Discrete sub-distribution transformers

JcK :M→M distr

Probability of an event

Pr [c,m : E ] = JcKm E

LosslessnessPr [c,m : >] = 1

Page 7: The EasyCrypt Tool

EasyCrypt: Logics

I Hoare Logic[c : P =⇒ Q]

I Probabilistic Hoare Logic

[c : P =⇒ Q]≤ δ [c : P =⇒ Q] = δ [c : P =⇒ Q]≥ δ

I Probabilistic Relational Hoare Logic

[c1 ∼ c2 : P =⇒ Q]

I Ambient higher-order logic

∀ c1, c2,m1,m2.

[c1 ∼ c2 : true =⇒ ={res}]⇒Pr [c1,m1 : res] = Pr [c2,m2 : res]

Page 8: The EasyCrypt Tool

Lecture Plan

1 Functional Programs, Ambient Logic and Interactive Proofs

2 Formalizing Distributions

3 Interactions between EasyCrypt Logics

4 EasyCrypt Modules

5 Proving and Transforming Programs

6 Structuring Proofs

7 Advanced Tactics

Page 9: The EasyCrypt Tool

Lecture 1

Functional Programs, Ambient Logic andInteractive Proofs

Page 10: The EasyCrypt Tool

The Ambient Logic

EasyCrypt’s ambient logic is a general higher-order logic.

In this lecture:I How to specify facts about user-defined operators;I How to prove them when automatic techniques do not

work.

Page 11: The EasyCrypt Tool

Topic 1

The EasyCrypt Core Language

Page 12: The EasyCrypt Tool

Types

EasyCrypt is a typed language:

I Core types: unit, bool, int, real, distrI Declaring abstract types:

type ttype α u

I Defining new types:type α list = [ Nil | Cons of α& α list ]type complex = {| r:real; i:real |}type intlist = int listtype (α ,β ) pair = α ∗ β

Page 13: The EasyCrypt Tool

EasyCrypt Expressions

I Declaring abstract operators:map : (α→ β )→ α list→ β listfold : (β→ α→ β )→ β→ α list→ β

I Defining concrete operators:

op max (x y : int) = (a < b) ? b : a.op length (xs : ’a list) =

with xs = Nil => 0with xs = Cons x xs => 1 + length xs.

op length_fold (xs : ’a list) = fold (fun v _, 1 + v) 0 xs.

I Can make use of let binders:op fst (x:’a ∗ ’b) = let (a,b) = x in a.

Page 14: The EasyCrypt Tool

EasyCrypt Formulas

I Logical operators:

forall (x : t),φ (∀ (x : t),φ ) exists (x : t),φ (∃ (x : t),φ )φ1=>φ2 (φ1⇒φ2) φ1<=>φ2 (φ1⇔φ2)φ1/\φ2 (φ1∧φ2) φ1\/φ2 (φ1∨φ2)φ1&&φ2 (φ1∧ (φ1⇒φ2)) φ1||φ2 (φ1∨ (!φ1⇒φ2))!φ (¬φ )

I Quantification on memories: forall &m, ...,I Quantification on modules: forall (M <: T), ...,I HL, pHL and pRHL judgments,I Probability expressions (given a module M and a memory

&m): Pr[M.f(x) @ &m: E].

Page 15: The EasyCrypt Tool

Axioms / Lemmas

I Specifying operators axiomatically:op count : ’a −> ’a list −> int.axiom count_nil (x : ’a): count x [] = 0.axiom count_cons_eq (x : ’a) (xs : ’a list):

count x (x :: xs) = 1 + (count x xs).axiom count_cons_neq (x y : ’a) (xs : ’a list):

x <> y => count x (y :: xs) = count x xs.I Stating facts:

lemma fact1 (x y : int): x ≤ 0⇒ y ≤ 0⇒ 0 ≤ x ∗ y.lemma fact2 (xs : α list): length xs = length_fold xs.lemma fact3 x (xs : α list): 0 ≤ count x xs ≤ length xs.

Page 16: The EasyCrypt Tool

Topic 2

Interactive Proofs

Page 17: The EasyCrypt Tool

Stating a theorem

lemma mylemma b1 b2 b3 :(b1 => b2) => (b2 => b3) => b1 => b3.

proof. (∗ proof starts here ∗)

b1 : boolb2 : boolb3 : bool

local hypotheses (context)

(b1⇒ b2)⇒ (b2⇒ b3)⇒ b1⇒ b3 }goal︸ ︷︷ ︸ ︸︷︷︸assumptions conclusion

Progress in the proof happens via tactics that allows thesimplification, decomposition into subgoals, or the resolution ofthe goal.

Page 18: The EasyCrypt Tool

Continuing the proof

lemma mylemma b1 b2 b3 : ...proof.

move=> hb12.

b1 : boolb2 : boolb3 : boolhb12 : b1⇒ b2

(b2⇒ b3)⇒ b1⇒ b3

Page 19: The EasyCrypt Tool

Continuing the proof

lemma mylemma b1 b2 b3 : ...proof.

move=> hb12 bh23 hb1.

b1 : boolb2 : boolb3 : boolhb12 : b1⇒ b2hb23 : b2⇒ b3hb1 : b1

b3

Page 20: The EasyCrypt Tool

Continuing the proof

lemma mylemma b1 b2 b3 : ...proof.

move=> hb12 bh23 hb1.apply hb23.

b1 : boolb2 : boolb3 : boolhb12 : b1⇒ b2hb23 : b2⇒ b3hb1 : b1

b2

Page 21: The EasyCrypt Tool

Continuing the proof

lemma mylemma b1 b2 b3 : ...proof.

move=> hb12 bh23 hb1.apply hb23.apply hb12.

b1 : boolb2 : boolb3 : boolhb12 : b1⇒ b2hb23 : b2⇒ b3hb1 : b1

b1

Page 22: The EasyCrypt Tool

Continuing the proof

lemma mylemma b1 b2 b3 : ...proof.

move=> hb12 bh23 hb1.apply hb23.apply hb12.assumption.

Proof completed

Page 23: The EasyCrypt Tool

Continuing the proof

lemma mylemma b1 b2 b3 : ...proof.

move=> hb12 bh23 hb1.apply hb23.apply hb12.assumption.

qed.

Page 24: The EasyCrypt Tool

Topic 3

Core Tactics

Page 25: The EasyCrypt Tool

Propositional logic

I b1⇒ b2⇒ b3

As a goal [move⇒ b1 b2]

b1⇒ b2⇒ b3↪→

b1 : boolb2 : bool

b3

As a hypothesis [apply]

Page 26: The EasyCrypt Tool

Propositional logic

I b1⇒ b2⇒ b3

As a goal [move⇒ b1 b2]

As a hypothesis [apply]

h : b1⇒ b2⇒ b3b3

↪→

1.h : b1⇒ b2⇒ b3

b12.

h : b1⇒ b2⇒ b3b2

Page 27: The EasyCrypt Tool

Propositional logic - connectorsI Conjunction: a ∧ b

As a goal [split] (prove a ∧ b)

a ∧ b↪→ 1.

a2.

b

As a hypothesis [elim ab] (destruct a ∧ b in a and b)

ab : a ∧ bφ

↪→a⇒ b⇒φ

Page 28: The EasyCrypt Tool

Propositional logic - connectorsI Disjunction: a ∨ b

As a goal

[left] (prove a ∨ b by proving a)

a ∨ b↪→

a

[right] (prove a ∨ b by proving b)

a ∨ b↪→

b

As a hypothesis [elim ab] (case analysis on a ∨ b)

ab : a ∨ bφ

↪→ 1.a⇒φ

2.b⇒φ

Page 29: The EasyCrypt Tool

Propositional logic - existentialI Existential: exists x : t,φ (x)

As a goal [exists v] (prove goal by giving a witness)

exists x : t,φ (x)↪→

φ (v)

As a hypothesis [elim h] (extract a witness)

h : exists x : t,φ (x)φ ’

↪→forall (v : t),φ (v)⇒φ ’

Page 30: The EasyCrypt Tool

Case analysisThe tactic case performs a case analysis on boolean orinductive expressions.

a : boolb : boola ⊕ b = (a ∧ !b) || (!a ∧ b)

(case a) leads to

1.

a : boolb : boola⇒ true ⊕ b = (true ∧ !b) ∨ (!true ∧ b)

2.

a : boolb : bool!a⇒ false ⊕ b = (false ∧ !b) ∨ (!false ∧ b)

Page 31: The EasyCrypt Tool

Identification up to computations

EasyCrypt comes with a set of simplification rules.

a : boolb : boolfalse ⊕ b = (false ∧ !b) ∨ (!false ∧ b)

[simplify] leads to

a : boolb : boolb = b

that can be easily solved by reflexivity.

Page 32: The EasyCrypt Tool

Identification up to computations

Computations include (among other notions)I reduction of function applications (β-reduction),I inlining operator bodies (δ-reduction),I logical operators tautology (a ∧ false→ false) ([logic]).

Terms that are equal up to computations areconsidered identical

a : boolb : bool!a⇒ false ⊕ b = (false ∧ !b) ∨ (!false ∧ b)

can be directly solved by reflexivity.

Page 33: The EasyCrypt Tool

Rewrite - replace equals by equals

The tactic rewrite replaces a subterm a of the goalby an equal one b. It requires a proof of a = b or a⇔ b.

rewrite h

h : a = bP a

↪→h : a = b

P b

Currently, one cannot rewrite under quantifiers, but one can[subst]itute variables. a is also substituted in the context.

subst a

h : a = bφ

↪→φ [b/a]

Page 34: The EasyCrypt Tool

Rewrite - replace equals by equals

rewrite comes in different flavor:

I rewrite −h : from right to left

I rewrite mh where m is a multiplier

? as many times as possible! as many times as possible, at least onen? at most n timesn! exactly n times

I rewrite {o}h where o is a sequence of positive integers

Rewrites the oth occurrences only.I rewrite /o where o is a defined operator’s name

Expands (or unfolds) the operator’s definition. (rewrite −/ofolds the definition back in.)

Page 35: The EasyCrypt Tool

Rewrite - replace equals by equals

2 ∗ (a + b) = (b + a) + (a + b)

I rewrite {2}addnC

2 ∗ (a + b) = (b + a) + (b + a)

I rewrite (addnC b a)

2 ∗ (a + b) = (a + b) + (a + b)

I rewrite −!addnA

2 ∗ (a + b) = b + (a + (a + b))

Page 36: The EasyCrypt Tool

Logical cut

The tactic cut:φ allows to do a forward chaining

h: ...φ ’

↪→ 1.h: ...φ

2.h: ...φ⇒φ ’

It is possible to give a name to the new goal (cut my:φ )

h: ...φ ’

↪→ 1.h: ...φ

2.

h: ...my:φφ ’

Page 37: The EasyCrypt Tool

Induction

An induction principle for a type t is any formula of the form:

∀ (p : t→ bool),φ1→ ...→φn, ∀ (x : t),ψ1(x)→ ...→ψn(x)→ p x

For example, for natural numbers:forall (p : int→ bool), p 0⇒

(forall (x : int), 0 ≤ x⇒ p x⇒ p (x + 1))⇒forall (x : int), 0 ≤ x⇒ p x

Page 38: The EasyCrypt Tool

Induction - Induction Principles

Induction principles are generated automatically for inductivedatatypes.

type α list = [Nil | Cons of α& α list].

yields

forall (p : ’a list→ bool),p Nil⇒(forall (x : ’a) (xs : ’a list), p xs⇒ p (Cons x xs))⇒forall (xs : ’a list), p xs

Page 39: The EasyCrypt Tool

Induction

Applying the induction principle via apply can be cumbersome.

The tactic elim/ eases the applications of such principles.

P: int→ bool0 ≤ x⇒ P x

↪→ elim/ind x

1.P : int→ bool

P 02.

P : int→ bool∀ (x : int), 0 ≤ x→ P x→ P (x+1)

Page 40: The EasyCrypt Tool

Induction

For inductive types, the induction principle is automaticallyselected by the elim tactic.

P : α list→ boolx : α list

P x↪→ elim x

1.P : α list→ bool

P Nil

2.P : α list→ bool∀ (x : α ) (xs : α list), P xs→ P (Cons x xs)

Page 41: The EasyCrypt Tool

Automation

EasyCrypt comes with some automation tactics:I progress breaks the goal by repetead applications of the

introduction tactics (split, move, ...) and elimination onintroduced assumptions. Several variants:

progress [nosplit] does not split the conclusion.trivial leaves the goal intact if not entirely discharged.

I smt: try to solve the goal using external SMT solvers.

Page 42: The EasyCrypt Tool

Topic 4

Tacticals

Page 43: The EasyCrypt Tool

Tacticals I

Tacticals are operators on tactics.

I t1; t2apply t1 and then t2 on all generated subgoals

I t; [t1|...|tn]apply t and then each of the ti to the i th subgoal

I do trepeat t as much as possible, at least one time

this tactic takes the same multiplier of rewritedo! t, do? t, do n! e, do n? t

I try ttry to apply t, or nothing if t cannot by applied

I by t1; ...; tnapply t1; ...; tn and then try to close all the subgoals viatrivial. fail if all the subgoals cannot be solved.

Page 44: The EasyCrypt Tool

Tacticals III t1; first t2

apply t1 and then t2 on the first subgoalI t1; last t2

apply t1 and then t2 on the last subgoalI variants: t1; first n t2, t1; last n t2I t; first n last

apply t and then shift the n first goals to the end

Page 45: The EasyCrypt Tool

Tacticals - Intro PatternsI t=> ip1 ... ipn

apply t and then execute the introduction of ip1 ... ipn

t=> xintroduce a name / a hypothesis

t=> [ip1|...ipn]do a case analysis on the top assumption andexecute ipi on the i th subgoal

t=> −>, or t=> <−introduce an equational hypothesis and rewrite it

t=> {h}clear the hypothesis h

t=> //, or t=> /=, or t=> //=execute trivial, simplify, simplify; trivial

I move is the identity tactic.I In cut ip:φ , ip is an arbitrary intro pattern.

Page 46: The EasyCrypt Tool

Trade-off between interactive / automatic proof

I EasyCrypt has two kinds of tacticslow-level, interactive onesthe SMT hammer

The difficulty is to find the right trade-off between the two.SMT goal resolution success can be very unstableSMT is very good at solving large numbers of smallproblems generated by the program logics

I qed does not mark the end of the proof.

Page 47: The EasyCrypt Tool

Lecture 2

Formalizing Distributions

Page 48: The EasyCrypt Tool

Distributions in EasyCrypt

I Discrete sub-distributionsI Described by their probability mass function:µ : α distr→ (α→ bool)→ real.

I In this lectureBase axioms on distributions,Some example definitions.

Page 49: The EasyCrypt Tool

Properties of distributions

General Axioms

(∗ mu d p is always within the unit interval ∗)axiom mu_bounded (d:’a distr) (p:’a −> bool):

0%r <= mu d p <= 1%r.

(∗ The probability of the false event is 0 ∗)axiom mu_false (d:’a distr): mu d (fun _, false) = 0%r.

(∗ Probability of a disjunction of events ∗)axiom mu_or (d:’a distr) (p q:’a −> bool):

mu d (cpOr p q) = mu d p + mu d q − mu d (cpAnd p q).

(∗ Point−wise equality is equality ∗)axiom pw_eq (d d’:’a distr):

d == d’ => d = d’.

Page 50: The EasyCrypt Tool

An example definition

Example (Uniform distribution on booleans)

op bool_uf: bool distr.

axiom bool_uf_def (p: bool −> bool):mu bool_uf p = (1%r / 2%r) ∗ charfun p true +

(1%r / 2%r) ∗ charfun p false.

Page 51: The EasyCrypt Tool

Derived Operators

Derived Operators

(∗ Probability of a particular element ∗)op mu_x (d:’a distr) (x:’a): real = mu d ((=) x).

(∗ Support of a distribution ∗)op support (d:’a distr) x: bool = 0%r < mu d x.

(∗ Point−wise equality ∗)pred (==) (d1:’a distr) (d2:’a distr) = mu_x d1 == mu_x d2.

Page 52: The EasyCrypt Tool

Some remarksI Some lemmas on distributions can be used with rewrite Pr

to rewrite probability expressions.

Example (rewrite Pr mu_or)Pr[ f, m : A \/ B ]

↓Pr[ f, m : A ] + Pr[ f, m : B ] − Pr[ f, m : A /\ B ]

I It is better, if possible, to define distributions using mu andprove simplification lemmas for mu_x and support.

Example

lemma support_uf_bool (b:bool): support uf_bool b.

lemma mu_x_uf_bool (b:bool): mu_x uf_bool b = 1%r / 2%r.

lemma lossless : mu uf_bool (fun _, true) = 1%r.

Page 53: The EasyCrypt Tool

Distribution Transformers

I The standard library defines distribution transformers.op ( ∗ ): α distr→ β distr→ (α ∗ β ) distr. (in Pair.ec)op dapply: (α→ β )→ α distr→ β distr. (in Distr.ec)...

I Very useful to describe complex distributions.

Page 54: The EasyCrypt Tool

Summary

I A way of axiomatically defining discrete distributions.I Very powerful rewriting results on probability expressions.

Page 55: The EasyCrypt Tool

Lecture 3

Interactions between EasyCrypt Logics

Page 56: The EasyCrypt Tool

Reminder: The EasyCrypt Logics

I Hoare Logic[c : P =⇒ Q]

I Probabilistic Hoare Logic

[c : P =⇒ Q]≤ δ [c : P =⇒ Q] = δ [c : P =⇒ Q]≥ δ

I Probabilistic Relational Hoare Logic

[c1 ∼ c2 : P =⇒ Q]

I Ambient higher-order logic

∀ c1, c2,m1,m2.

[c1 ∼ c2 : true =⇒ ={res}]⇒Pr [c1,m1 : res] = Pr [c2,m2 : res]

Page 57: The EasyCrypt Tool

byphoare, byequiv, bypr, conseq

In this lectureI How to interpret HL, pHL and pRHL judgments as

statements on probability expressions,I How to interpret statements on probability expressions as

HL, pHL or pRHL judgments,I How to combine HL, pHL and pRHL judgments together to

strengthen them.I Warning: Whenever a pre or postcondition appears outside

of aHL, pHL or pRHL judgment, it is parameterized by oneor several memories. I omit this in the slides.

Page 58: The EasyCrypt Tool

Proving probability expressions

I The formula

∀ c1, c2,m1,m2.

[c1 ∼ c2 : true =⇒ ={res}]⇒Pr [c1,m1 : res] = Pr [c2,m2 : res]

is a direct consequence of the definition for [· ∼ · : · =⇒ ·].I Similar rules connect other fragments of the logic together.I These rules can be applied in proofs using the tactics

byequiv, byphoare, hoare, and bypr.

Page 59: The EasyCrypt Tool

Interpreting equiv judgmentsEquivalence: equiv[c1 ~ c2: P ==> Q]

As a hypothesis [byequiv (_: P =⇒ Q)]

Pr[c1, m1: E1] = Pr[c2, m2: E2]

↪→1.

P m1 m22.

equiv[c1 ~ c2: P =⇒ Q]

3.∀m1’ m2’, Q m1’ m2’⇒ (E1 m1’⇔ E2 m2’)

I Pr[c1, m1: E1] ≤ Pr[c2, m2: E2] (with E1 m1’⇒ E2 m2’)I Pr[c1, m1: E1] ≥ Pr[c2, m2: E2] (with E2 m2’⇒ E1 m1’)

Page 60: The EasyCrypt Tool

Interpreting equiv judgmentsEquivalence: equiv[c1 ~ c2: P ==> Q]

As a goal bypr (X1) (X2) (prove by point-wise equality)

equiv[c1 ~ c2: P ==> Q]

↪→

1.

m1: memorym2: memoryH : P m1 m2∀X, Pr[c1, m1: X = X1] = Pr[c2, m2: X = X2]

2.

m1:memorym2:memory∀X, X = X1{m1}⇒ X = X2{m2}⇒ Q m1 m2

Page 61: The EasyCrypt Tool

Interpreting phoare judgmentsProbabilistic Hoare Triple: phoare[c: P ==> Q] = d

As a hypothesis [byphoare (_: P =⇒ Q)]

Pr[c, m: E] = d]

↪→1.

P m2.

phoare[c: P =⇒ Q] = d

3.∀m’, Q m’⇔ E m’

I Pr[c, m: E] ≤ d (with phoare[c: P =⇒ Q] ≤ d andE m’⇒ Q m’)

I Pr[c, m: E] ≥ d (with phoare[c: P =⇒ Q] ≥ d andE m’⇒ Q m’)

Page 62: The EasyCrypt Tool

Interpreting phoare judgmentsProbabilistic Hoare Triple: phoare[c: P ==> Q] = d

As a goal bypr (prove by point mass)

phoare[c: P ==> Q] = d↪→

m: memoryH: P mPr[c, m: Q] d

Page 63: The EasyCrypt Tool

Intepreting hoare judgments

Hoare Triple: hoare[c: P ==> Q]

As a goal bypr (zero probability)

hoare[c: P ==> Q]↪→

m: memoryH: P mPr[c, m: !Q] = 0

Page 64: The EasyCrypt Tool

Strenghtening contracts

A simple rule of consequence: conseq (_: P =⇒ Q)

hoare[c: P’ =⇒ Q’]

↪→

1.P⇒ P’

2.hoare[c: P =⇒ Q]

3.Q’⇒ Q

Also works for pHL and pRHL judgments. In pHL, the boundcan also be strengthened. Useful variants:

I conseq (_: _ =⇒ Q), weaken only the postcondition,I conseq (_: P =⇒ Q), weaken only the precondition,I conseq∗ (_: P =⇒ Q), quantify only on variables that may

be modified by the statement (frame rule).

Page 65: The EasyCrypt Tool

Combining contracts: Hoare + Hoare

The conseq tactic can also be used to combine togetherseveral judgments.[conseq (_: P’ =⇒ Q’) (_: P’’ =⇒ Q’’)]:

hoare[c: P =⇒ Q]

↪→

1.P⇒ P’ ∧ P’’

2.hoare[c: P’ =⇒ Q’]

3.hoare[c: P’’ =⇒ Q’’]

4.Q’ ∧ Q’’⇒ Q

Page 66: The EasyCrypt Tool

Combining contracts: pHoare + Hoare

[conseq (_: P’ =⇒ Q’: =d) (_: P’’ =⇒ Q’’)] (also with ≤, ≥):

phoare[c: P =⇒ Q]: = d’

↪→

1.P⇒ P’ ∧ P’’

2.phoare[c: P’ =⇒ Q’] = d

3.hoare[c: P’’ =⇒ Q’’]

4.Q’ ∧ Q’’⇒ Q

Page 67: The EasyCrypt Tool

Combining contracts: equiv + Hoare[conseq (_: P’ =⇒ Q’) (_: P1 =⇒ Q1) (_: P2 =⇒ Q2)]:

equiv[c1 ~ c2: P =⇒ Q]

↪→

1.m1:memorym2:memory

P m1 m2⇒ P’ m1 m2 / P1 m1 / P2 m2

2.equiv[c1 ~ c2: P’ =⇒ Q’]

3.hoare[c1: P1 =⇒ Q1]

4.hoare[c2: P2 =⇒ Q2]

5.m1:memorym2:memory

Q’ m1 m2 ∧ Q1 m1 ∧ Q2 m2⇒ Q m1 m2

Page 68: The EasyCrypt Tool

Combining contracts: equiv + pHoare

[conseq (_: P1 =⇒ Q1) (_: P2 =⇒ Q2: =1%r)]:

equiv[c1 ~ skip: P =⇒ Q]

↪→

1.m1:memorym2:memory

P m1 m2⇒ P1 m1 m2

2.m1:memorym2:memory

Q1 m1 m2⇒ Q m1 m2

3.m1:memorym2:memory

P1 m1 m2⇒ P2 m2

4.m1:memorym2:memory

Q2 m2⇒ Q1 m1 m2

5.phoare[c1: P2 =⇒ Q2]

Page 69: The EasyCrypt Tool

On the notation (_: ... )

I A tactic parameter of the form (_: ...) can be replaced witha proof term whose conclusion have the correct form.

I For example, if you have the following lemmalemma c_ll (b’:bool): phoare[c: b = b’ =⇒ true] = 1%r,

you can writeconseq (c_ll true)

instead ofconseq (_: b = true =⇒ true); first by apply (c_ll true).

Page 70: The EasyCrypt Tool

A few words on modules

I All the judgments and tactics we discussed talk aboutprograms,

I But we’ve said nothing about what those programs are...I They can be procedures in modules, or just statements.I Modules are objects that define a memory space and a set

of procedures that may use variables in that space.I They can be abstracted by module types that define a set

of required procedures.

Page 71: The EasyCrypt Tool

Conclusions

I A lot can be proved about programs without looking at theirbody.

I Combining judgments can save a lot of time and effort:Real-world programs have complex Hoare invariants.Real-world cryptographic proofs have complex relationalinvariantsIt is almost always beneficial to consider them separatelywhen doing proofs, until they are both needed.

I It is almost as easy to reason about probabilities of eventsin programs as it is to reason about probabilities infunctional distributions.

I In fact, programs define distributions!

Page 72: The EasyCrypt Tool

Lecture 4

EasyCrypt Modules

Page 73: The EasyCrypt Tool

So far, we’ve seen

I how to specify and prove properties of mathematicaloperators;

I how to specify and prove properties of (discretesub-)distributions;

I how to force the interpretation of procedures as distributiontransformers (and vice-versa);

I how to combine and strengthen judgments about abstractprocedures.

But we still don’t know how to write a procedure...

Page 74: The EasyCrypt Tool

Modules

Objectives:I Manage complexity by abstractionI Instantiate generic transformations (simplified syntax)

forall &m (A <: AdvCCA), exists (B <: AdvCPA),Pr[CCA(FO(S),A) @ &m : b’ = b ] <=Pr[CPA(S,B) @ &m : b’ = b] + ....

I Support high-level reasoning stepsCentral to EasyCrypt:

I Used to specify schemes, oracles, cryptographicassumptions, adversaries and game-based properties.

Page 75: The EasyCrypt Tool

Contents of a module

module M = { (∗ name of the module ∗)var m : t (∗ global variable declarations ∗)var m1, m2 : t

proc h(x:int) : int = { (∗ procedure definitions ∗)...

}

module N = ... (∗ sub−module definitions ∗)}.

Some restrictions:I Types, operators and predicates cannot be

declared/defined inside a moduleI No polymorphism: variables and procedures are

monomorphic

Page 76: The EasyCrypt Tool

My first module

module RO = {var m : (int, int) map

proc h (x:int) : int = {var r : int;r = $[0..10];if (!in_dom x m) m.[x] = r;return proj (m.[x]);

}}.

A module RO with a global variable m and a procedure h.Outside the module, the global variable is denoted RO.m andthe procedure RO.f

Page 77: The EasyCrypt Tool

Modules can call and use the memory of others

module M1 = {var x : intproc f (i:int) : int = { ... }

}.

module M2 = {proc g (i:int) : int = {

M1.x = M1.x + 1;i = M1.f(i);return i;

}}.

Page 78: The EasyCrypt Tool

Module types

A module type is an abstraction of a module

module type ADV = { (∗ name of the module type ∗)proc choose (pk:pkey) : msg ∗ msg (∗ procedure declarations ∗)proc guess (c:cipher) : bool

}.

Remarks:I Module types cannot contain variable and module

declarationsI A module M has type I if it contains at least the procedures

declared in I (with the correct types)

Page 79: The EasyCrypt Tool

Modules can be parameterized by other modules:Functors

module CPA (S:Scheme, A:ADV) = {proc main () : bool = {

var ...(pk,sk) = S.kg();(m0,m1) = A.choose(pk);b = ${0,1};challenge = S.enc(pk, b?m1:m0);b’ = A.guess(challenge);return b’ = b;

}.

Remark: The procedures A.choose and A.guess can shareprocedures and memory (active adversary)

Restriction: A sub-module cannot be a functor

Page 80: The EasyCrypt Tool

Functor application

It is possible to define a module by (partially) applying a functorto other modules.

module A : ADV = { .... }. (∗ Structure ∗)

module CPA’ = CPA. (∗ Alias ∗)

module CPAS = CPA(S). (∗ Partial application ∗)

module CPASA = CPA(S,A). (∗ Full application ∗)

Module types are checked when applying functors.

Page 81: The EasyCrypt Tool

The memory model

module M1 = { var x : int }.

module M2 = M1.

module T = {proc f () : unit = { M1.x = 1; M2.x = 2 }

}.

Questions: After the execution of T.fI what is the value of M2.x?I what is the value of M1.x?

Page 82: The EasyCrypt Tool

The memory model

module type Empty = {}.module E : Empty = {}.

module F(I:Empty) = { var x : int }.

module M1 = F(E).module M2 = F(E).

module T = {proc f () : unit = { M1.x = 1; M2.x = 2 }

}.

Question: After the execution of T.f what is the value of M2.xand M1.x in?

Page 83: The EasyCrypt Tool

Functor application is not generative

A module should be understood as a pair of:I A memory space (the global variables declared in the

module)I A set of procedures (possibly parameterized by the

procedures provided by module parameters)Remarks:

I Functor application does not generate a new memoryspace.

I Global variables of a functor can be read or written withoutapplying the functor: F.x = F.x + 1;

I Procedures cannot be called until the functor is fullyapplied.

Page 84: The EasyCrypt Tool

Back to the CPA game

module CPA (A:Adv) = {proc main () : bool = {

var ...(pk,sk) = S.kg();(m0,m1) = A.choose(pk);b = ${0,1};challenge = S.enc(pk, b?m1:m0);b’ = A.guess(c);return b’ = b;

}.

In the literature, the IND-CPA, IND-CCA1, IND-CCA propertiesare all defined using the same basic game. Only theadversary’s capabilities change.

Page 85: The EasyCrypt Tool

Capabilities of adversary

IND-CPA IND-CCA1 IND-CCAA.choose _ S.dec(sk) S.dec(sk)A.guess _ _ S.dec(sk) \{c}

Sometimes the number of queries allowed to S.dec(sk) is alsolimited

The module system can capture these notions

Page 86: The EasyCrypt Tool

A first try, declaration of the IND-CCA adversary

module type DEC = {proc dec(c:cipher) : msg option

}.

module type ADV(D:Dec) = {proc choose (pk:pkey) : msg ∗ msgproc guess (c:cipher) : bool

}.

Remark: This does not capture the notion of IND-CCA1adversary, since the guess function can call the decryptionoracle

Page 87: The EasyCrypt Tool

A more restrictive module type system

For each procedure of a module type it is possible to selectwhich procedures provided by the module parameters can becalled.

module type DEC = { proc dec(c:cipher) : msg }module type ADVCCA1(D:DEC) = {

proc choose (pk:pkey) : msg ∗ msg { D.dec }proc guess (c:cipher) : bool { }

}.

Here choose can call D.dec whereas guess cannot.

the notation

proc choose (pk:pkey) : msg ∗ msg

is a shortcut for

proc choose (pk:pkey) : msg ∗ msg { all procedures }

Page 88: The EasyCrypt Tool

IND-CCA: using the type module system

We can split the decryption oracle in two (one for choose andone for guess)

module type DEC2 = {proc dec_c(c:cipher) : msgproc dec_g(c:cipher) : msg

}.module type ADVCCA(D:DEC2) = {

proc choose (pk:pkey) : msg ∗ msg { D.dec_c }proc guess (c:cipher) : bool { D.dec_g}

}.

Page 89: The EasyCrypt Tool

IND-CCA: the decryption oracle

The decryption oracle in the guess stage can now “reject”queries on the challenge.

module D : DEC2 = {var sk : skeyvar challenge : cipher

proc dec_c (c:cipher) : msg option = {var r : msg option;r = S.dec(sk, c);return r;

}proc dec_g (c:cipher) : msg option = {

var r : msg = None;if (c <> challenge) r = S.dec(sk, c);return r;

}}.

Page 90: The EasyCrypt Tool

EasyCrypt allows quantification over modules

forall &m (A <: Adv) :exists (I <: Inverter),

Pr[CPA(A).main() @ &m : res] − (1%r/2%r) <=Pr[OW(I).main() @ &m : res].

forall (A<:Adv), equiv [CCA(A).main ~ G(A).main : ... ==> ...]

Can express formulas like:I Forall adversary A there exists a simulator B ...I There exists a simulator B such that forall adversary A ...

Page 91: The EasyCrypt Tool

Memory constraints and access control

module X = { var x : int }.module G(A:Adv) = {

proc g () : unit = {X.x = 3;A.f();

}}.

lemma F : forall (A<:Adv), hoare[G(A).g : true ==> X.x = 3].

Can we prove such a lemma ?

Page 92: The EasyCrypt Tool

Negative constraints

The answer is clearly “no”: take the following module A1.

module G(A:Adv) = {proc g () : unit = { X.x = 3; A.f(); }

}.

module A1 = {proc f() : unit = { X.x = 4; }

}

lemma F (A<:Adv): hoare[G(A).g : true ==> X.x = 3].

But F becomes true, if we restrict the quantification to modulesthat do not “use the memory of X”.

Page 93: The EasyCrypt Tool

Negative constraints

EasyCrypt allows quantifications over restricted classes ofmodules using negative constraints:

lemma T : forall (A <: Adv{X}), hoare[G(A).g : true ==> X.x = 3].

The “forall (A<:Adv{X})” should be understood as

for all “adversary” A whose implementation does not use the“memory space” of X.

Page 94: The EasyCrypt Tool

What is the memory space of a module ?

The memory space of a module M is:I The global variables declared inside the module (and its

sub-modules)I The global variables of the external modules used in M

(also indirectly)

Page 95: The EasyCrypt Tool

Restrictions are checked during instantiation

lemma T : forall (A <: Adv {X}), hoare[G(A).g : true ==> X.x = 3].

module A2 = { }.lemma Error1 : hoare[G(A2).g : true ==> X.x = 3].

Error message: invalid module application: arguments do notmatch required interfaces

lemma Error2 : hoare[G(A1).g : true ==> X.x = 3].apply (T A1).

Error message: the module A1 should not use X

Page 96: The EasyCrypt Tool

I Modules are used for everything stateful.I Functors are used to specify and prove systems in a

compositional way.I One module, one memory space: there are never two

copies of a global.I The full importance of restricting the memory of quantified

modules will become clear when we discuss tactics.

Page 97: The EasyCrypt Tool

Lecture 5

Proving and Transforming Programs

Page 98: The EasyCrypt Tool

Tactics for the Program Logics

Three kinds:I Tactics that operate on the contracts (Lecture 3),I Tactics that deduce properties of the program from their

semantics,I Tactics that transform programs into equivalent ones.

An overview of the proof rules for the program logics was givenin week 1... (if you remember that far)

Page 99: The EasyCrypt Tool

Overview

I Consider a HL, pHL or pRHL judgment.I Program logics turn it into a purely logical formula.I Follow the program’s structure from the bottom-up.I Use the language semantics to transform the pre and post

according to the statement that is being dealt with.I The tactics themselves implement the core proof rules

from Week 1 composed with the sequential compositionand conseq rule.

Page 100: The EasyCrypt Tool

Starting a proof about a concrete procedure:[proc]

Assume that M.f has the following definition.module M = { proc f(): t = { var r; c; return r; } }.

hoare[M.f: P =⇒ Q] ↪→ hoare[c: P =⇒ Q[r/res]]

hoare[M.f: P =⇒ Q]↪→

[proc ∗] hoare[r = M.f(): P =⇒ Q[r/res]]

Similar rules for other types of judgments.

Page 101: The EasyCrypt Tool

Dealing with the empty program: [skip]

hoare[skip: P =⇒ Q] ↪→ ∀&m, P m⇒ Q m

phoare[skip: P =⇒ Q] = 1%r ↪→ ∀&m, P m⇒ Q m

equiv[skip ~ skip: P =⇒ Q]↪→

∀&m1 &m2, P m1 m2⇒ Q m1 m2

I The phoare version can also be used to prove otherstatements, but you will never be able to provephoare[skip: P =⇒ Q] = d (or ≤ d) when d < 1.

Page 102: The EasyCrypt Tool

Straight-line deterministic code: [wp] and [sp]

I [wp] consumes uninterrupted sequences of deterministicassignments at the end of a program and transform thepostcondition.

I [sp] consumes uninterrupted sequences of deterministicassignments at the beginning of a program and transformthe precondition.

I Variants:[wp n] and [sp n] (HL and pHL): Consume up to (sp: downto) line number n.[wp n n’] and [sp n n’] (pRHL): Consume up to (sp: down to)line number n in the left program, and line number n’ in theright program.

Page 103: The EasyCrypt Tool

Sequential Composition: [seq]

As expected for HL, pRHL and lower-bound pHL:

hoare[c; c’: P =⇒ Q]↪→ [seq n: (φ )] with n = |c|

1. hoare[c: P =⇒φ ] 2. hoare[c’:φ=⇒ Q]

equiv[c1; c2 ~ c1’; c2’: P =⇒ Q]↪→[seq n n’: (φ )] with n = |c1|, n’ = |c1’|

1. equiv[c1 ~ c2: P =⇒φ ] 2. hoare[c1’ ~ c2’:φ=⇒ Q]

phoare[c; c’: P =⇒ Q] ≥ d↪→ [seq n: (φ ) d1 d2] with n = |c|

1. phoare[c: P =⇒φ ] ≥ d1 2. hoare[c’:φ=⇒ Q] ≥ d23. d1 ∗ d2 ≥ d

Page 104: The EasyCrypt Tool

Sequential Composition for pHLProbabilistic intermediate invariants cause issues:

phoare[c; c’: P =⇒ Q] = d↪→ [seq n: (φ ) d1 d2 d3 d4 (ψ )] with n = |c|1. hoare[c: P =⇒ψ ]

2. phoare[c: P =⇒φ ] = d1 3. phoare[c’:φ∧ψ=⇒ Q] = d24. phoare[c: P =⇒ !φ ] = d3 5. phoare[c’: !φ∧ψ=⇒ Q] = d4

6. d1 ∗ d2 + d3 ∗ d4 = d

I ψ is a deterministic invariant: we want it to hold wheneverwe reach the end of c.

I φ is a probabilistic invariant: it may or may not hold whenwe reach the end of c depending on random samplings.

I Some simplifications possible:if ψ is true, it can be omitted,if one of d1, d2 (or d3, d4) is 0, the other can be _.

I Note the hidden application of conseq: makes the tacticmore applicable than the proof rule from Week 1.

Page 105: The EasyCrypt Tool

Case analysis on program variables: [case]

Introduces a case analysis on some expression of the programvariables in the precondition.

hoare[c: P =⇒ Q]↪→ [case (φ )]

1. hoare[c: P ∧φ=⇒ Q] 2. hoare[c: P ∧ !φ=⇒ Q]

phoare[c: P =⇒ Q] = d↪→ [case (φ )]

1. phoare[c: P ∧φ=⇒ Q] = d 2. phoare[c: P ∧ !φ=⇒ Q] = d

equiv[c ~ c’: P =⇒ Q]↪→ [case (φ )]

1. equiv[c ~ c’: P ∧φ=⇒ Q] 2. hoare[c’ ~ c’: P ∧ !φ=⇒ Q]

Page 106: The EasyCrypt Tool

One-sided pRHL tactics

I Almost all pRHL tactics from here on have one-sidedvariants.

I Allow relational proofs on programs that have differentcontrol-flow structures.

I The one-sided tactics are built by composition of pRHL[seq], the pRHL + pHL version of conseq, and thecorresponding pHL tactic.

I To apply the one-sided variant of a tactic, use {1} or {2}immediately after the tactic’s name.

Page 107: The EasyCrypt Tool

Random assignments: [rnd]

hoare[c; x $← dx: P =⇒ Q]↪→ [rnd]

hoare[c: P =⇒ ∀ v, support dx v⇒ Q](Non-Deterministic interpretation of random sampling.)

phoare[c; x $← dx: P =⇒ Q] = d↪→ [rnd (φ )]

phoare[c: P =⇒ µdx True = d ∧∀ v, support dx v⇒φ v⇔ Q[v/x]] = 1%r

(If φ is omitted, the postcondition Q is used.)

equiv[c; x $← dx ~ c’; y $← dx’: P =⇒ Q]↪→ [rnd (f) (f’)]

“f and f’ are inverse bijections and preserve weights +hoare-style non-deterministic post-condition”

Page 108: The EasyCrypt Tool

Conditionals: [if]Do a case analysis on a conditional test at the beginning of theprogram. pRHL variant is synchronized.

hoare[if (e) { c1 } else { c2 }; c: P =⇒ Q]↪→ [if]

1. hoare[c1; c: P ∧ e =⇒ Q] 2. hoare[c2; c: P ∧ !e =⇒ Q]

phoare[if (e) { c1 } else { c2 }; c: P =⇒ Q] = d↪→ [if]

1. phoare[c1; c: P ∧ e =⇒ Q] = d2. phoare[c2; c: P ∧ !e =⇒ Q] = d

equiv[if (e) { c1 } else { c2 }; c ~ if (e’) { c1’ } else { c2’ }; c’: P =⇒ Q]↪→ [if]

1. ∀m m’, P m m’⇒ e{m}⇔ e’{m’}2. equiv[c1; c ~ c1’; c’: P ∧ e =⇒ Q]3. equiv[c2; c ~ c2’; c’: P ∧ !e =⇒ Q]

Page 109: The EasyCrypt Tool

Desynchronized Conditionals: [if{.}]

equiv[if (e) { c1 } else { c2 }; c ~ c’: P =⇒ Q]↪→ [if{1}]

1. equiv[c1; c ~ c’: P ∧ e =⇒ Q]2. equiv[c2; c ~ c’: P ∧ !e =⇒ Q]

equiv[c ~ if (e’) { c1’ } else { c2’ }; c’: P =⇒ Q]↪→ [if{2}]

1. equiv[c ~ c1’; c’: P ∧ e’ =⇒ Q]2. equiv[c ~ c2’; c’: P ∧ !e’ =⇒ Q]

Page 110: The EasyCrypt Tool

Procedure calls: [call]hoare[c; x = M.f(y): P =⇒ Q

↪→ [call (_: P’ =⇒ Q’)]1. hoare[M.f: P’ =⇒ Q’]

2. hoare[c: P =⇒ P’ ∧ (∀ v, Q’[v/x]⇒ Q[v/x])](The “quantification on v” introduces fresh values for every

program variable x that may be modified by M.f.)

phoare[c; x = M.f(y): P =⇒ Q] = d↪→ [call (_: P’ =⇒ Q’)]

1. phoare[M.f: P’ =⇒ Q’] = d2. phoare[c: P =⇒ P’ ∧ (∀ v, Q’[v/x]⇒ Q[v/x])]

(Use [seq] if you need to adjust the probability use for M.f.)

equiv[c; x = M.f(y) ~ c’; x’ = M’.f(y’): P =⇒ Q↪→ [call (_: P’ =⇒ Q’)]

1. equiv[M.f ~ M’.f: P’ =⇒ Q’]2. equiv[c ~ c’: P =⇒ P’ ∧ (∀ v, Q’[v/x]⇒ Q[v/x])]

(A useful variant to try out call (_:φ ).)

Page 111: The EasyCrypt Tool

Loops: [while]

pRHL loops must be synchronized (or the one-sided rule mustbe used...)

hoare[c1; while (e) { c2 }: P =⇒ Q]↪→ [while (φ )]

1. hoare[c2:φ∧ e =⇒φ ]2. hoare[c1: P =⇒φ∧ (∀ v,φ [v/x]⇒ Q[v/x])]

equiv[c1; while (e) { c2 } ~ c1’ while (e’) { c2’ }: P =⇒ Q]↪→ [while (φ )]

1. equiv[c2 ~ c2’:φ∧ e ∧ e’ =⇒φ∧ e⇔ e’]2. equiv[c1 ~ c1’: P =⇒φ∧ (∀ v, !e[v/x]⇒ !e’[v/x]⇒φ [v/x]⇒ Q[v/x]]

Page 112: The EasyCrypt Tool

pHL Loops: [while]

Loops in pHL are more complicated.

phoare[c1; while (e) { c2 }: P =⇒ Q] = d↪→ [while (φ ) (va)]

1. ∀ z, phoare[c2:φ∧ e ∧ va = z =⇒φ∧ va < z] = 1%r2. phoare[c1: P =⇒φ∧ (∀ v,

(φ [v/x]⇒ e[v/x]⇒ va[v/x] ≤ 0⇒ !e[v/x]) ∧(!e[v/x]⇒φ [v/x]⇒ Q[v/x]] = d

I The rule works with ≤ and ≥ as well.I There are rules for while loops where termination does not

matter (≤ ) or is not variant-based (“sample until”). Theyare discussed in the final lecture (if we make it).

Page 113: The EasyCrypt Tool

Dealing with abstract proceduresI We need rules to perform proofs on universally quantified

modulesI The rules should be valid independently of the

implementation of the moduleI In the following, assume that A.f is abstract and may query

an oracle o.

hoare[A.f:φ=⇒φ↪→ [proc (φ )]

1. “φ does not depend on the memory of A”2. hoare[o:φ=⇒φ ]

equiv[A.f ~ A.f: ={arg} ∧φ=⇒ ={res} ∧φ ]↪→ [proc (φ )]

1. “φ does not depend on the memory of A”2. rgesequiv[o ~ o: =a ∧φ=⇒ =r ∧φ ]

Page 114: The EasyCrypt Tool

Fundamental Lemma

A very frequent step in cryptographic proof

Pr[G: E] ≤ Pr[G’: E] + Pr[G’: F]

This can be established using the following pRHL lemma (seetutorial on conseq)

equiv[G.f ~ G’.f: !F{2} =⇒ E{1} = E{2}]

How do we prove this judgment?

Page 115: The EasyCrypt Tool

Upto failure abstract procedure call

Several conditions on the oracles:I Assuming the failure event has not occured, prove the

invariant;I Prove that the failure event is monotonic;I Since execution is no longer synchronized after the failure

event, need to prove losslessness of everyone involved.

equiv[A.f ~ A.f: ={arg} ∧φ∧ !bad{2} =⇒ !bad{2}⇒ ={res} ∧φ ]↪→ [proc (bad) (φ )]

1. “bad, φ do not depend on the memory of A”2. equiv[o ~ o: ={arg} ∧φ∧ !bad{2} =⇒ !bad{2}⇒ ={res} ∧φ ]

3. equiv[o ~ o: !bad{2} =⇒ !bad{2}]4. “losslessness conditions”

A more precise version takes a second invariant that shouldhold after bad becomes true: [proc (bad) (φ ) (φ ’)].

Page 116: The EasyCrypt Tool

Transforming Programs

I We’ve seen tactics that operate on the pre andpostconditions,

I and tactics that consume a program to prove propertiesabout it.

I We can also transform a program into an equivalent one.

Page 117: The EasyCrypt Tool

Inlining a procedure call: [inline]

I inline M.f inlines the code of M.f, with some additionalassignments to avoid overwriting of local variables.

I inline{1} M.f inlines only in the left program.I inline ∗ inline all concrete procedures iteratively until no

more inlining is possible.

Page 118: The EasyCrypt Tool

Swapping statements: [swap]

I swap n off move statement at line n to line ‘n + off’ (if offsetis negative, it is moved up)

I swap{1} n off, swap{2} n off...I swap [n..m] off move the block between lines n and m and

move it by offset off.I The swapped statements need to be independent from all

the statements they cross.

Page 119: The EasyCrypt Tool

Reducing conditional: [rcondt, rcondf]

I rcondt n asks to prove that the condition at line n is trueand inlines the appropriate branch.

I rcondf n does the same but with the else branchI a side needs to be specified in pRHLI can be used to unroll the first iteration of a loop, or make a

loop disappear if its condition is initially false!I Note that the if tactic is a composition of case and rcond.

Page 120: The EasyCrypt Tool

Advanced loop tactics: [splitwhile,fusion,fission]

Warning: Syntax is ugly, broken and unstable.I splitwhile (cond): {side} n: splits the loop at line n using

additional condition cond.I fusion can merge two loops with the same indices into a

single loopI fission can split a loop into two distinct loops if the loop

body can be split into independent chunksI We won’t be using these, but it’s good to know they exist

Page 121: The EasyCrypt Tool

Conclusions

I Which tactic to apply can usually be decided by the laststatement in the program(s).

I When dealing with an abstract procedure, the invariantsand the proof obligations should be about the oracles thatprocedure is allowed to query.

I Don’t forget about one-sided tactic variants in pRHL.I In pRHL, losslessness conditions arise whenever programs

are out of sync. They are annoying, but easy to deal with.

Page 122: The EasyCrypt Tool

Automation

I In pRHL, when proving simple properties of programs thatare very similar, try using [sim].

I In all program logics, the [auto] tactic may also becomeuseful.

Page 123: The EasyCrypt Tool

Lecture 6

Structuring Proofs

Page 124: The EasyCrypt Tool

Instantiation

I Concrete schemes and abstract adversariesI Reuse existing proofs when realizing cryptographic

assumptions? (e.g., one-way trapdoor with RSA)I Sections hide proof artifacts from final theorems,

automatically infer restrictions on adversaries, andgeneralize theorems with module quantification.

I Cloning avoids user-level code duplication by instantiatingabstract types and operators with concrete values, creatingmodule copies with disjoint memories.

Page 125: The EasyCrypt Tool

Sectionssection.

declare module Adv : A{Prop,Hyp}.

local module G1 = {var count:int (∗ some state ∗)fun main() = { (∗ uses A ∗) }

}.

local module Dist : D = { (∗ uses A ∗) }

local equiv Prop_G1:[Prop(A).main ~ G1.main: true ==> ={res}].

local equiv G1_Hyp:[G1.main ~ Hyp(D).main: true ==> ={res}].

lemma &m final: exists (Dist<:D),Pr[Prop(A).main() @ m: res] = Pr[Hyp(D).main() @ m: res].

end section.

Page 126: The EasyCrypt Tool

Sections

I Inside the section, declared modules are independent frommodules defined (declared) after it.

I print axiom final.

yields (after the section is closed)

lemma final (Adv:>A{Prop,Hyp}) &m:exists (Dist:>D),

Pr[Prop(A).main() @ &m: res] = Pr[Hyp(D).main() @ &m:res].

I Declared modules become parameters to lemmas.I Local lemmas disappear.I Local modules disappear and restrictions can be dropped.

Page 127: The EasyCrypt Tool

Usages of Sections

I Simplify proofs by inferring adversary restrictions andquantifications.

module G(Adv:A) = { ... }.

lemma foo (Adv<:A{G}):equiv [ Pr[G(A).f() ~ ... ].

section.declare Adv<:A.module G = { ... }.

lemma foo:equiv [ Pr[G.f() ~ ...].

end section.

I Generalize theorem statements by hiding proof artifacts.Adversary restrictions,Intermediate games,Intermediate equivs, lemmas and proofs.

Page 128: The EasyCrypt Tool

Theories: Generalities

Theories provide an additional layer of generalization:I declared abstract types yield “polymorphism”,I declared constants and operators yield “universal

quantifications”,I in forms that EasyCrypt cannot reason about...I ... but that allow efficient code reuse.

Page 129: The EasyCrypt Tool

Theories: A simple example

theory Monoid.type t.

op one: t.op ( ∗ ): t −> t −> t.

axiom mul1m (x : t): one ∗ x = x.axiom mulm1 (x : t): x ∗ one = x.axiom mulmA (x y z : t): (x ∗ y) ∗ z = x ∗ (y ∗ z).

end Monoid.

Page 130: The EasyCrypt Tool

Cloning: A simple example

require import Int.clone Monoid as MInt with

type t <− int,op one = 1,op ( ∗ ) <− ( ∗ )proof ∗ by smt.

print theory MInt. (∗ yields ∗)

theory MInt.op one: int = 1.

lemma mul1m (x : int): one ∗ x = x.lemma mulm1 (x : int): x ∗ one = x.lemma mulmA (x y z : int): (x ∗ y) ∗ z = x ∗ (y ∗ z).

end MInt.

Page 131: The EasyCrypt Tool

Theories: Cloning and Realization

When cloning, you can:I define (=) or override (<−)

abstract types,abstract operators (and constants),

I define abstract sub-theories,All declared types and operators are abstract,the theory contains only axioms (no lemmas).

I discharge some (or all) axiomsby giving a single proof for all axioms (usually smt),or by giving individual proofs.

Page 132: The EasyCrypt Tool

Theories: Cloning modules

I You can clone theories that contain modules.I You get an exact copy of the module that works in a

separate memory space.I This is useful for code reuse and may have unforeseen

applications in proofs.

Page 133: The EasyCrypt Tool

Cloning: An example

theory ROM.module RO = {

var m: (word,word) map

fun init(): unit = {m = empty;

}

fun h(x:word): word = {if (!in_dom x m) m.[x] =

$dword;return m.[x];

}}.

end ROM.

theory ROM’.module RO = {

var m: (word,word) map

fun init(): unit = {m = empty;

}

fun h(x:word): word = {if (!in_dom x m) m.[x] =

$dword;return m.[x];

}}.

end ROM’.

Or just use clone ROM as ROM’.

Page 134: The EasyCrypt Tool

Cloning: Some notes

I Cloning a theory that declares abstract types creates new,distinct abstract types unless you define or override them.

I Cloning is not especially suited to equipping existingtheories with new algebraic structures.

I When used carelessly, cloning can cause SMT to give up.

Page 135: The EasyCrypt Tool

Summary

Several ways to generalize proofs and theorems:I Automatically infer module dependencies and adversary

restrictions using sections.I Abstract away the proof and its artifacts and keep only the

relevant theorems and hypotheses using local modulesand lemmas.

I Perform crypto proofs on abstract modules and operatorsbefore instantiating them using theories and cloning.

Page 136: The EasyCrypt Tool

Lecture 7

Advanced Tactics