## Bitwalker

The Bitwalker case study is about interacting with a stream of bytes: copying from the byte stream to a 64-bit unsigned integer, and conversely. This case study is detailed in Specification and proof of high-level functional properties of bit-level programs.

**Authors:** Claude Marché / Clément Fumex

**Topics:** Bitwise operations

**Tools:** Why3

**References:** ProofInUse joint laboratory

see also the index (by topic, by tool, by reference, by year)

module Bitwalker use int.Int use int.EuclideanDivision use array.Array use ref.Ref (* use bv.BV32 *) (* use bv.BV8 *) (* use bv.BV64 *) use mach.bv.BVCheck8 as BV8 use mach.bv.BVCheck32 as BV32 use mach.bv.BVCheck64 as BV64 use bv.BVConverter_32_64 as C32_64 use bv.BVConverter_8_32 as C8_32 (* file missing in repository use mach.bv.BV *) function nth8_stream (stream : array BV8.t) (pos : int) : bool = BV8.nth stream[div pos 8] (7 - mod pos 8) lemma nth64: forall value:BV64.t, i:int. 0 <= i < 64 -> BV64.nth value i = BV64.nth_bv value (C32_64.toBig (BV32.of_int i)) lemma nth8: forall value:BV8.t, i:int. 0 <= i < 8 -> BV8.nth value i = BV8.nth_bv value (C8_32.toSmall (BV32.of_int i)) (* *) let function maxvalue (len : BV32.t) : BV64.t = BV64.lsl_bv (BV64.of_int 1) (C32_64.toBig len) let lemma nth_ultpre0 (x:BV64.t) (len:BV32.t) requires { BV32.t'int len < 64} ensures { BV64.eq_sub x BV64.zeros (BV32.t'int len) (64 - BV32.t'int len) <-> BV64.t'int x < BV64.t'int (maxvalue len) } = assert { BV32.ult len (BV32.of_int 64) }; assert { BV64.eq_sub_bv x BV64.zeros (C32_64.toBig len) (BV64.sub (BV64.of_int 64) (C32_64.toBig len)) <-> BV64.ult x (maxvalue len) } let poke_64bit_bv (value : BV64.t) (left : BV32.t) (flag : bool) : BV64.t requires { BV32.t'int left < 64 } ensures { forall i. 0 <= i < 64 /\ i <> 63 - BV32.t'int left -> BV64.nth result i = BV64.nth value i } ensures { flag = BV64.nth result (63 - BV32.t'int left) } = assert { BV32.ult left (BV32.of_int 64) }; begin ensures { forall i:BV32.t. i <> BV32.sub (BV32.of_int 63) left -> BV64.nth_bv result (C32_64.toBig i) = BV64.nth_bv value (C32_64.toBig i) } ensures { flag = BV64.nth_bv result (C32_64.toBig (BV32.sub (BV32.of_int 63) left)) } let mask = BV64.lsl_bv (1:BV64.t) (C32_64.toBig (BV32.sub_check (63:BV32.t) left)) in match flag with | True -> BV64.bw_or value mask | False -> BV64.bw_and value (BV64.bw_not mask) end end

return `value`

with the bit of index `left`

from the left set to `flag`

(* version where `left` is an int and not a bitvector, which is closer to the result of the SPARK translation from signed integers *) let poke_64bit (value : BV64.t) (left : int) (flag : bool) : BV64.t requires { 0 <= left < 64 } ensures { forall i. 0 <= i < 64 /\ i <> 63 - left -> BV64.nth result i = BV64.nth value i } ensures { flag = BV64.nth result (63 - left) } = let ghost left_bv = BV64.of_int left in assert { BV64.ult left_bv (BV64.of_int 64) }; assert { (BV64.sub (BV64.of_int 63) left_bv) = BV64.of_int (63 - left) }; begin ensures { forall i:BV64.t. BV64.ule i (BV64.of_int 63) -> i <> BV64.sub (BV64.of_int 63) left_bv -> BV64.nth_bv result i = BV64.nth_bv value i } ensures { flag = BV64.nth_bv result (BV64.sub (BV64.of_int 63) left_bv) } let mask = BV64.lsl_bv (1:BV64.t) (BV64.of_int (63 - left)) in match flag with | True -> BV64.bw_or value mask | False -> BV64.bw_and value (BV64.bw_not mask) end end

return `value`

with the bit of index `left`

from the left set to `flag`

(* return the bit of `byte` at position `left` starting from the left *) let peek_8bit_bv (byte : BV8.t) (left : BV32.t) : bool requires { 0 <= BV32.t'int left < 8 } ensures { result = BV8.nth byte (7 - BV32.t'int left) } = assert {BV32.ult left (BV32.of_int 8)}; begin ensures { result = BV8.nth_bv byte (BV8.sub (BV8.of_int 7) (C8_32.toSmall left))} let mask = BV32.lsl_bv (1:BV32.t) (BV32.sub_check (7:BV32.t) left) in not (BV32.eq (BV32.bw_and (C8_32.toBig byte) mask) BV32.zeros) end (* return the bit of the `left`/8 element of `addr` at position mod `left` 8 starting from the left *) let peek_8bit_array (addr : array BV8.t) (left : BV32.t) : bool requires { 8 * (length addr) < BV32.two_power_size } requires { BV32.t'int left < 8 * length addr } ensures { result = nth8_stream addr (BV32.t'int left) } = peek_8bit_bv (addr[ BV32.to_uint (BV32.udiv_check left (8:BV32.t)) ]) (BV32.urem_check left (8:BV32.t)) (* return a bitvector of 64 bits with its `len` bits of the right set to the bits between `start` and `start+len` of `addr` *) let peek (start : BV32.t) (len : BV32.t) (addr : array BV8.t) : BV64.t requires { BV32.t'int len <= 64 } requires { BV32.t'int start + BV32.t'int len < BV32.two_power_size } requires { 8 * length addr < BV32.two_power_size } ensures { BV32.t'int start + BV32.t'int len > (8 * length addr) -> result = BV64.zeros } ensures { BV32.t'int start + BV32.t'int len <= (8 * length addr) -> (forall i:int. 0 <= i < BV32.t'int len -> BV64.nth result i = nth8_stream addr (BV32.t'int start + BV32.t'int len - i - 1)) /\ (forall i:int. BV32.t'int len <= i < 64 -> BV64.nth result i = False) } = if (BV32.to_uint (BV32.add_check start len) > ( 8 *length addr )) then BV64.zeros else let retval = ref BV64.zeros in let i = ref BV32.zeros in let lstart = BV32.sub_check (BV32.of_int 64) len in while BV32.ult !i len do variant{ BV32.t'int len - BV32.t'int !i } invariant {0 <= BV32.t'int !i <= BV32.t'int len} invariant {forall j:int. BV32.t'int len - BV32.t'int !i <= j < BV32.t'int len -> BV64.nth !retval j = nth8_stream addr (BV32.t'int start + BV32.t'int len - j - 1)} invariant {forall j:int. 0 <= j < BV32.t'int len - BV32.t'int !i -> BV64.nth !retval j = False} invariant {forall j:int. BV32.t'int len <= j < 64 -> BV64.nth !retval j = False} let flag = peek_8bit_array addr (BV32.add_check start !i) in retval := poke_64bit_bv !retval (BV32.add_check lstart !i) flag; i := BV32.add_check !i (1:BV32.t); done; !retval let peek_64bit (value : BV64.t) (left : BV32.t) : bool requires {BV32.t'int left < 64} ensures {result = BV64.nth value (63 - BV32.t'int left)} = assert {BV32.ult left (BV32.of_int 64)}; begin ensures {result = BV64.nth_bv value (BV64.sub (BV64.of_int 63) (C32_64.toBig left))} let mask = BV64.lsl_bv (1:BV64.t) (C32_64.toBig (BV32.sub_check (63:BV32.t) left)) in not (BV64.eq (BV64.bw_and value mask) BV64.zeros) end (* static inline uint8_t PokeBit8(uint8_t byte, uint32_t left, int flag) { uint8_t mask = ((uint8_t) 1) << (7u - left); return (flag == 0) ? (byte & ~mask) : (byte | mask); } *) (* return `byte` with the bit at index `left` from the left set to `flag` *) let poke_8bit (byte : BV8.t) (left : BV32.t) (flag : bool) : BV8.t requires { BV32.t'int left < 8 } ensures { forall i:int. 0 <= i < 8 -> i <> 7 - BV32.t'int left -> BV8.nth result i = BV8.nth byte i } ensures { BV8.nth result (7 - BV32.t'int left) = flag } = assert { BV32.ult left (BV32.of_int 8) }; begin ensures { forall i:BV32.t. BV32.ult i (BV32.of_int 8) -> i <> BV32.sub (BV32.of_int 7) left -> BV8.nth_bv result (C8_32.toSmall i) = BV8.nth_bv byte (C8_32.toSmall i) } ensures { BV8.nth_bv result (BV8.sub (BV8.of_int 7) (C8_32.toSmall left)) = flag } let mask = BV8.lsl_bv (1:BV8.t) (BV8.sub_check (7:BV8.t) (C8_32.toSmall left)) in match flag with | True -> BV8.bw_or byte mask | False -> BV8.bw_and byte (BV8.bw_not mask) end end let poke_8bit_array (addr : array BV8.t) (left : BV32.t) (flag : bool) requires { 8 * (length addr) < BV32.two_power_size } requires { BV32.t'int left < 8 * length addr } writes { addr } ensures { forall i:int. 0 <= i < 8 * length addr -> i <> BV32.t'int left -> nth8_stream addr i = nth8_stream (old addr) i} ensures { nth8_stream addr (BV32.t'int left) = flag } = let i = BV32.udiv_check left (8:BV32.t) in let k = BV32.urem_check left (8:BV32.t) in addr[BV32.to_uint i] <- poke_8bit addr[BV32.to_uint i] k flag let poke (start : BV32.t) (len : BV32.t) (addr : array BV8.t) (value : BV64.t) : int writes { addr } requires{ BV32.t'int len < 64 } (* could be lower or equal if maxvalue and the condition to return -2 is corrected *) requires{ BV32.t'int start + BV32.t'int len < BV32.two_power_size } requires{ 8 * length addr < BV32.two_power_size } ensures { -2 <= result <= 0 } ensures { result = -1 <-> BV32.t'int start + BV32.t'int len > 8 * length addr } ensures { result = -2 <-> BV64.t'int (maxvalue len) <= BV64.t'int value /\ BV32.t'int start + BV32.t'int len <= 8 * length addr } ensures { result = 0 <-> BV64.t'int (maxvalue len) > BV64.t'int value /\ BV32.t'int start + BV32.t'int len <= 8 * length addr } ensures { result = 0 -> (forall i:int. 0 <= i < BV32.t'int start -> nth8_stream (old addr) i = nth8_stream addr i) /\ (forall i:int. BV32.t'int start <= i < BV32.t'int start + BV32.t'int len -> nth8_stream addr i = BV64.nth value (BV32.t'int len - i - 1 + BV32.t'int start)) /\ (forall i:int. BV32.t'int start + BV32.t'int len <= i < 8 * length addr -> nth8_stream addr i = nth8_stream (old addr) i) } = if BV32.to_uint (BV32.add_check start len) > 8 * length addr then -1 (*error: invalid_bit_sequence*) else if BV64.uge value (maxvalue len) (* should be len <> 64 && _..._ *) then -2 (*error: value_too_big*) else let lstart = BV32.sub_check (BV32.of_int 64) len in let i = ref BV32.zeros in while BV32.ult !i len do variant { BV32.t'int len - BV32.t'int !i } invariant {0 <= BV32.t'int !i <= BV32.t'int len} invariant {forall j:int. 0 <= j < BV32.t'int start -> nth8_stream (old addr) j = nth8_stream addr j} invariant {forall j:int. BV32.t'int start <= j < BV32.t'int start + BV32.t'int !i -> nth8_stream addr j = BV64.nth value (BV32.t'int len - j - 1 + BV32.t'int start) } invariant {forall j:int. BV32.t'int start + BV32.t'int !i <= j < 8 * length addr -> nth8_stream addr j = nth8_stream (old addr) j } let flag = peek_64bit value (BV32.add_check lstart !i) in poke_8bit_array addr (BV32.add_check start !i) flag; assert {nth8_stream addr (BV32.t'int start + BV32.t'int !i) = BV64.nth value ((BV32.t'int len - BV32.t'int !i - 1))}; assert { forall k. BV32.t'int start <= k < BV32.t'int start + BV32.t'int !i -> k <> BV32.t'int start + BV32.t'int !i && 0 <= k < 8 * length addr && nth8_stream addr k = BV64.nth value (BV32.t'int start + BV32.t'int len - k - 1)}; i := BV32.add_check !i (1:BV32.t); done; 0 let peekthenpoke (start len : BV32.t) (addr : array BV8.t) requires {8 * length addr < BV32.two_power_size} requires {0 <= BV32.t'int len < 64} requires {BV32.t'int start + BV32.t'int len <= 8 * length addr} ensures {result = 0} ensures {forall i. 0 <= i < 8 * length addr -> nth8_stream addr i = nth8_stream (old addr) i} = let value = peek start len addr in let res = poke start len addr value in assert {res = 0}; assert {forall i. BV32.t'int start <= i < BV32.t'int start + BV32.t'int len -> nth8_stream addr i = nth8_stream (old addr) i}; res let pokethenpeek (start len : BV32.t) (addr : array BV8.t) (value : BV64.t) writes {addr} requires {8 * length addr < BV32.two_power_size} requires {0 <= BV32.t'int len < 64} requires {BV32.t'int start + BV32.t'int len <= 8 * length addr} requires {BV64.t'int value < BV64.t'int (maxvalue len)} ensures {result = value} = assert { forall i:int. BV32.t'int len <= i < 64 -> BV64.nth value i = False }; let poke_result = poke start len addr value in assert { poke_result = 0 }; let peek_result = peek start len addr in assert { BV64.(==) peek_result value }; peek_result end

download ZIP archive

# Why3 Proof Results for Project "bitwalker"

## Theory "bitwalker.Bitwalker": fully verified

Obligations | Alt-Ergo 2.0.0 | CVC4 1.5 | Z3 4.5.0 | |||

nth64 | 0.02 | --- | --- | |||

nth8 | 0.06 | --- | --- | |||

VC nth_ultpre0 | --- | --- | --- | |||

split_goal_right | ||||||

VC nth_ultpre0.0 | --- | --- | 0.05 | |||

VC nth_ultpre0.1 | --- | 0.08 | --- | |||

VC nth_ultpre0.2 | --- | --- | 0.58 | |||

VC poke_64bit_bv | --- | --- | --- | |||

split_goal_right | ||||||

VC poke_64bit_bv.0 | --- | --- | 0.02 | |||

VC poke_64bit_bv.1 | --- | 0.02 | --- | |||

VC poke_64bit_bv.2 | --- | 0.12 | --- | |||

VC poke_64bit_bv.3 | --- | 0.06 | --- | |||

VC poke_64bit_bv.4 | --- | --- | 0.08 | |||

VC poke_64bit_bv.5 | --- | --- | 0.12 | |||

VC poke_64bit | --- | --- | --- | |||

split_goal_right | ||||||

VC poke_64bit.0 | --- | --- | 0.05 | |||

VC poke_64bit.1 | --- | --- | 0.09 | |||

VC poke_64bit.2 | --- | 0.04 | --- | |||

VC poke_64bit.3 | --- | 0.05 | --- | |||

VC poke_64bit.4 | --- | 0.05 | --- | |||

VC poke_64bit.5 | --- | 0.04 | --- | |||

VC peek_8bit_bv | --- | --- | --- | |||

split_goal_right | ||||||

VC peek_8bit_bv.0 | --- | --- | 0.04 | |||

VC peek_8bit_bv.1 | --- | 0.02 | --- | |||

VC peek_8bit_bv.2 | --- | 0.04 | --- | |||

VC peek_8bit_bv.3 | --- | --- | 0.05 | |||

VC peek_8bit_array | --- | --- | --- | |||

split_goal_right | ||||||

VC peek_8bit_array.0 | --- | 0.01 | --- | |||

VC peek_8bit_array.1 | --- | 0.01 | --- | |||

VC peek_8bit_array.2 | --- | 0.02 | --- | |||

VC peek_8bit_array.3 | --- | 0.04 | --- | |||

VC peek_8bit_array.4 | --- | --- | 0.02 | |||

VC peek | --- | --- | --- | |||

split_goal_right | ||||||

VC peek.0 | --- | 0.02 | --- | |||

VC peek.1 | --- | 0.01 | --- | |||

VC peek.2 | --- | 0.03 | --- | |||

VC peek.3 | --- | 0.02 | --- | |||

VC peek.4 | --- | 0.02 | --- | |||

VC peek.5 | --- | 0.01 | --- | |||

VC peek.6 | --- | --- | 0.03 | |||

VC peek.7 | --- | --- | 0.02 | |||

VC peek.8 | --- | 0.02 | --- | |||

VC peek.9 | --- | 0.01 | --- | |||

VC peek.10 | --- | --- | 0.08 | |||

VC peek.11 | --- | 0.02 | --- | |||

VC peek.12 | --- | --- | 0.06 | |||

VC peek.13 | --- | 0.02 | --- | |||

VC peek.14 | --- | 0.03 | --- | |||

VC peek.15 | --- | --- | 0.16 | |||

VC peek.16 | --- | --- | 1.36 | |||

VC peek.17 | --- | --- | 0.12 | |||

VC peek.18 | --- | --- | 0.03 | |||

VC peek.19 | --- | 0.02 | --- | |||

VC peek.20 | --- | --- | 0.06 | |||

VC peek_64bit | --- | --- | --- | |||

split_goal_right | ||||||

VC peek_64bit.0 | --- | --- | 0.02 | |||

VC peek_64bit.1 | --- | 0.02 | --- | |||

VC peek_64bit.2 | --- | 0.06 | --- | |||

VC peek_64bit.3 | --- | --- | 0.41 | |||

VC poke_8bit | --- | --- | --- | |||

split_goal_right | ||||||

VC poke_8bit.0 | --- | --- | 0.02 | |||

VC poke_8bit.1 | --- | 0.03 | --- | |||

VC poke_8bit.2 | --- | 0.06 | --- | |||

VC poke_8bit.3 | --- | 0.04 | --- | |||

VC poke_8bit.4 | --- | --- | 0.07 | |||

VC poke_8bit.5 | --- | --- | 0.19 | |||

VC poke_8bit_array | --- | --- | --- | |||

split_goal_right | ||||||

VC poke_8bit_array.0 | --- | 0.01 | --- | |||

VC poke_8bit_array.1 | --- | 0.02 | --- | |||

VC poke_8bit_array.2 | --- | 0.04 | --- | |||

VC poke_8bit_array.3 | --- | 0.03 | --- | |||

VC poke_8bit_array.4 | --- | 0.03 | --- | |||

VC poke_8bit_array.5 | --- | --- | --- | |||

introduce_premises | ||||||

VC poke_8bit_array.5.0 | --- | --- | --- | |||

case (div i 8 = o) | ||||||

VC poke_8bit_array.5.0.0 | 0.15 | --- | --- | |||

VC poke_8bit_array.5.0.1 | 0.12 | --- | --- | |||

VC poke_8bit_array.6 | 0.10 | --- | --- | |||

VC poke | --- | --- | --- | |||

split_goal_right | ||||||

VC poke.0 | --- | 0.02 | --- | |||

VC poke.1 | --- | 0.01 | --- | |||

VC poke.2 | --- | 0.02 | --- | |||

VC poke.3 | --- | 0.02 | --- | |||

VC poke.4 | --- | 0.02 | --- | |||

VC poke.5 | --- | 0.01 | --- | |||

VC poke.6 | --- | 0.02 | --- | |||

VC poke.7 | --- | 0.03 | --- | |||

VC poke.8 | --- | --- | 0.18 | |||

VC poke.9 | --- | 0.02 | --- | |||

VC poke.10 | --- | 0.02 | --- | |||

VC poke.11 | --- | 0.04 | --- | |||

VC poke.12 | --- | 0.02 | --- | |||

VC poke.13 | --- | 0.01 | --- | |||

VC poke.14 | --- | 0.01 | --- | |||

VC poke.15 | --- | 0.02 | --- | |||

VC poke.16 | --- | 0.04 | --- | |||

VC poke.17 | --- | --- | 0.04 | |||

VC poke.18 | --- | 0.03 | --- | |||

VC poke.19 | --- | 0.02 | --- | |||

VC poke.20 | --- | --- | 0.08 | |||

VC poke.21 | --- | --- | --- | |||

split_goal_right | ||||||

VC poke.21.0 | --- | --- | 0.11 | |||

VC poke.22 | --- | --- | --- | |||

split_goal_right | ||||||

VC poke.22.0 | --- | 0.02 | --- | |||

VC poke.22.1 | --- | 0.02 | --- | |||

VC poke.22.2 | --- | 0.03 | --- | |||

VC poke.22.3 | --- | 0.21 | --- | |||

VC poke.23 | --- | 0.03 | --- | |||

VC poke.24 | --- | 0.04 | --- | |||

VC poke.25 | --- | --- | 0.17 | |||

VC poke.26 | --- | 0.12 | --- | |||

VC poke.27 | --- | --- | 0.14 | |||

VC poke.28 | --- | 0.14 | --- | |||

VC poke.29 | --- | 0.01 | --- | |||

VC poke.30 | --- | 0.02 | --- | |||

VC poke.31 | --- | --- | 0.69 | |||

VC poke.32 | --- | 0.02 | --- | |||

VC poke.33 | --- | --- | 0.39 | |||

VC peekthenpoke | --- | --- | --- | |||

split_goal_right | ||||||

VC peekthenpoke.0 | --- | 0.02 | --- | |||

VC peekthenpoke.1 | --- | 0.02 | --- | |||

VC peekthenpoke.2 | --- | 0.01 | --- | |||

VC peekthenpoke.3 | --- | 0.02 | --- | |||

VC peekthenpoke.4 | --- | 0.02 | --- | |||

VC peekthenpoke.5 | --- | 0.01 | --- | |||

VC peekthenpoke.6 | --- | --- | 0.04 | |||

VC peekthenpoke.7 | --- | 0.24 | --- | |||

VC peekthenpoke.8 | --- | 0.02 | --- | |||

VC peekthenpoke.9 | --- | 0.07 | --- | |||

VC pokethenpeek | --- | --- | --- | |||

split_goal_right | ||||||

VC pokethenpeek.0 | --- | --- | 0.17 | |||

VC pokethenpeek.1 | --- | 0.01 | --- | |||

VC pokethenpeek.2 | --- | 0.02 | --- | |||

VC pokethenpeek.3 | --- | 0.02 | --- | |||

VC pokethenpeek.4 | --- | 0.02 | --- | |||

VC pokethenpeek.5 | --- | 0.02 | --- | |||

VC pokethenpeek.6 | --- | 0.02 | --- | |||

VC pokethenpeek.7 | --- | 0.04 | --- | |||

VC pokethenpeek.8 | --- | 0.32 | --- | |||

VC pokethenpeek.9 | 0.09 | --- | --- |