Formal Proofs for Computer Arithmetics Computer arithmetics ...

1 downloads 43 Views 836KB Size Report
Formal Proofs for. Computer Arithmetics. Laurent Théry. Marelle Project INRIA Sophia Antipolis. Computer Arithmetics – p.1. Computer arithmetics. Numbers are ...
Computer arithmetics Formal Proofs for Computer Arithmetics Laurent Théry Marelle Project INRIA Sophia­Antipolis

Numbers are everywhere: Billing system Weather forecast Computer vision Cryptography ...

Computer Arithmetics – p.1

Computer arithmetics

Computer Arithmetics – p.2

Formal Proofs Therac­25:

Different flavours:

Arithmetic overflows could cause the software to bypass safety checks.

Integer Arithmetic: a ∈ N

Rational Arithmetic: a ∈ Q

Floating­Point Arithmetic: a ∈ F

Interval Arithmetic: [a, b] ∈ F × F Exact Arithmetic: a ∈ R

Patriot Missile: The calculation of the time since boot was inacurate.

Pentium FDIV Some division operations were wrong by a very small amount.

Ariane 5: A conversion 64 bit floating­point number to 16 bit signed integer failed.

...

Computer Arithmetics – p.3

Computer Arithmetics – p.4

An Example

Outline •

A simple example that illustrates various aspects of formal verification.



An overview of the formalisation of floating­point arithmetic.

Take a program that given a parameter n returns the list of the first n prime numbers Prove it correct Knuth: The Art of Computer Programming

Computer Arithmetics – p.5

Computer Arithmetics – p.6

Natural Numbers

Addition

Inductive N: Set := O : N | S : N→N.

Fixpoint + [a,b:N] : match a with O => b | S a0 => S(a0 + b) end.

O, S(O), S(S(O)), ...

N :=

P (O) ∧

∀n, P (n) ⇒ P (S(n))

⇒ ∀n, P (n)

Computer Arithmetics – p.7

Computer Arithmetics – p.8

Multiplication

Divisibility Definition a|b := ∃c, b = c * a.

Fixpoint * [a,b:N] : N := match a with O => O | S a0 => b + (a0 * b) end.

Computer Arithmetics – p.9

Primality

Computer Arithmetics – p.10

Program Correctness Verification Condition Generator

Definition prime(p) := ∀n, n|p ⇒ n = 1 ∨ n = p ∧ p 6= 1.

Program

+ Annotations ⇒ Conditions

Krakatoa: Java −→ Coq Computer Arithmetics – p.11

Computer Arithmetics – p.12

Program Correctness

Program Correctness Loop:

{ Pre-conditions } P { Post-conditions }

while (C) { { invariant I variant V } P

Example { odd(x) } x = x + 2; { odd(x) }

}

Generated condition: ∀x, odd(x) ⇒ odd(x + 2) Computer Arithmetics – p.13

Computer Arithmetics – p.14

Program Correctness Example:

Knuth Algorithm int[] firstPrimes(int n) { int[] res = new int[n];

while (0 < i--) { { invariant odd(x) variant i } x = x + 2 }

{

∧ ∧

∀k, j. 0 ≤ k < j < n ⇒ res[k] < res[j] ∀k. ∧

Generated conditions: ∀x. odd(x) ⇒ odd(x + 2) ∀i. 0 ≤ i − 1 < i

∀k. 0 ≤ k < n ⇒ prime(res[k])

0 ≤ k ≤ res[n − 1] prime(k)

⇒ ∃j. ∧

res[j] = k

} return res; }

Computer Arithmetics – p.15

Knuth Algorithm

0≤j