module LabelExpr.Stamping where
open import Data.Nat
open import Data.Unit using (β€; tt)
open import Data.Bool using (true; false) renaming (Bool to πΉ)
open import Data.List hiding ([_])
open import Data.Product renaming (_,_ to β¨_,_β©)
open import Data.Sum using (_β_)
open import Data.Maybe
open import Relation.Nullary using (Β¬_; Dec; yes; no; recompute)
open import Relation.Nullary.Negation using (contradiction; Β¬?)
open import Relation.Binary.PropositionalEquality using (_β‘_; _β’_; refl; cong; sym)
open import Function using (case_of_)
open import Common.Utils
open import Common.SecurityLabels
open import Common.BlameLabels
open import CoercionExpr.CoercionExpr
renaming (_β to _ββ ; _βββ¨_β©_ to _ββββ¨_β©_; β -trans to β -transβ)
hiding (Progress; progress; plug-cong)
open import CoercionExpr.SyntacComp
open import CoercionExpr.Precision renaming (precββ to precβββ)
open import CoercionExpr.SecurityLevel renaming (β₯_β₯ to β₯_β₯β)
open import CoercionExpr.Stamping
open import LabelExpr.LabelExpr
stampβ : β V β LVal V β StaticLabel β LExpr
stampβ (l β) v-l low = l β
stampβ (l low) v-l high = l low βͺ id (l low) β¨Ύ β β«
stampβ (l high) v-l high = l high
stampβ (l β βͺ cΜ
β«) (v-cast (ir π _)) ββ² = l β βͺ stampβ cΜ
π ββ² β«
stamp!β : β V β LVal V β StaticLabel β LExpr
stamp!β (l β ) v-l ββ² = l β βͺ stamp!β (id (l β)) id ββ² β«
stamp!β (l β βͺ cΜ
β«) (v-cast (ir π _)) ββ² = l β βͺ stamp!β cΜ
π ββ² β«
stampβ-wt : β {V g β}
β (v : LVal V)
β β’ V β g
β β’ stampβ V v β β (g βΜ l β)
stampβ-wt {g = g} {low} v-l β’V rewrite gβΜlowβ‘g {g} = β’V
stampβ-wt {β = high} (v-l {low}) β’l = β’cast β’l
stampβ-wt {β = high} (v-l {high}) β’l = β’l
stampβ-wt (v-cast (ir π _)) (β’cast β’l) = β’cast β’l
stamp!β-wt : β {V g β}
β (v : LVal V)
β β’ V β g
β β’ stamp!β V v β β β
stamp!β-wt v-l β’l = β’cast β’l
stamp!β-wt (v-cast (ir _ _)) (β’cast β’l) = β’cast β’l
stampβ-LVal : β {V β}
β (v : LVal V)
β LVal (stampβ V v β)
stampβ-LVal {V} {low} v-l = v-l
stampβ-LVal {V} {high} (v-l {low}) = v-cast (ir (up id) (Ξ» ()))
stampβ-LVal {V} {high} (v-l {high}) = v-l
stampβ-LVal {V} {β} (v-cast (ir π x)) =
v-cast (ir (stampβ-CVal _ π β) (stamp-not-id π x))
stamp!β-LVal : β {V β}
β (v : LVal V)
β LVal (stamp!β V v β)
stamp!β-LVal {_} {β} v-l = v-cast (ir (stamp!β-CVal _ id β) (Ξ» ()))
stamp!β-LVal {V} {β} (v-cast (ir π x)) = v-cast (ir (stamp!β-CVal _ π β) Ξ» ())
stampβ-prec : β {β} {V Vβ² g gβ²}
β (v : LVal V)
β (vβ² : LVal Vβ²)
β β’ V β Vβ² β g β gβ²
β β’ stampβ V v β β stampβ Vβ² vβ² β β (g βΜ l β) β (gβ² βΜ l β)
stampβ-prec {low} (v-l {β}) v-l β-l rewrite ββlowβ‘β {β} = β-l
stampβ-prec {high} (v-l {low}) v-l β-l = β-cast β-l (prec-refl _)
stampβ-prec {high} (v-l {high}) v-l β-l = β-l
stampβ-prec v-l (v-cast (ir id x)) (β-castr β-l (β-id lβl)) =
contradiction refl (recompute (Β¬? (_ ==? _)) x)
stampβ-prec v-l (v-cast (ir (inj id) x)) (β-castr β-l (β-cast _ lβl ()))
stampβ-prec v-l (v-cast (ir (inj (up id)) x)) (β-castr β-l (β-cast _ () _))
stampβ-prec v-l (v-cast (ir (up id) x)) (β-castr β-l (β-cast _ lβl ()))
stampβ-prec {β} (v-cast (ir id x)) v-l (β-castl β-l cΜ
βββ²) =
contradiction refl (recompute (Β¬? (_ ==? _)) x)
stampβ-prec {low} (v-cast (ir (inj (id {l β})) _)) v-l (β-castl β-l cΜ
βββ²)
rewrite ββlowβ‘β {β} = β-castl β-l (β-cast (β-id lβl) lβl ββ)
stampβ-prec {high} (v-cast (ir (inj (id {l low})) _)) v-l (β-castl β-l cΜ
βββ²) =
β-cast β-l (β-castl (prec-refl _) lβl ββ)
stampβ-prec {high} (v-cast (ir (inj (id {l high})) _)) v-l (β-castl β-l cΜ
βββ²) =
β-castl β-l (β-cast (β-id lβl) lβl ββ)
stampβ-prec {β} (v-cast (ir (inj (up id)) _)) v-l (β-castl β-l (β-cast _ () ββ))
stampβ-prec {β} (v-cast (ir (up id) _)) v-l (β-castl β-l (β-cast _ lβl ()))
stampβ-prec (v-cast (ir π _ )) (v-cast (ir πβ² _)) MβMβ²
with precββ’ MβMβ²
... | β¨ β’cast β’l , β’cast β’l β©
with prec-inv MβMβ²
... | β¨ refl , cΜ
βcΜ
β² β© =
β-cast β-l (stampβ-prec π πβ² cΜ
βcΜ
β²)
stamp!β-left-prec : β {ββ ββ} {V Vβ² g gβ²}
β (v : LVal V)
β (vβ² : LVal Vβ²)
β β’ V β Vβ² β g β gβ²
β ββ βΌ ββ
β β’ stamp!β V v ββ β stampβ Vβ² vβ² ββ β β β (gβ² βΜ l ββ)
stamp!β-left-prec {low} (v-l {β}) v-l β-l lβΌl
rewrite ββlowβ‘β {β} = β-castl β-l (β-cast (β-id lβl) lβl ββ)
stamp!β-left-prec {low} (v-l {low}) v-l β-l lβΌh = β-cast β-l !ββ
stamp!β-left-prec {low} (v-l {high}) v-l β-l lβΌh = β-castl β-l (β-cast (β-id lβl) lβl ββ)
stamp!β-left-prec {high} (v-l {low}) v-l β-l hβΌh = β-cast β-l β!ββ
stamp!β-left-prec {high} (v-l {high}) v-l β-l hβΌh = β-castl β-l (β-cast (β-id lβl) lβl ββ)
stamp!β-left-prec v-l (v-cast (ir id x)) (β-castr β-l (β-id lβl)) βββΌββ =
contradiction refl (recompute (Β¬? (_ ==? _)) x)
stamp!β-left-prec v-l (v-cast (ir (inj id) x)) (β-castr β-l (β-cast _ lβl ()))
stamp!β-left-prec v-l (v-cast (ir (inj (up id)) x)) (β-castr β-l (β-cast _ () _))
stamp!β-left-prec v-l (v-cast (ir (up id) x)) (β-castr β-l (β-cast _ lβl ()))
stamp!β-left-prec {β} (v-cast (ir id x)) v-l (β-castl β-l cΜ
βββ²) βββΌββ =
contradiction refl (recompute (Β¬? (_ ==? _)) x)
stamp!β-left-prec {low} (v-cast (ir (inj (id {l β})) _)) v-l (β-castl β-l cΜ
βββ²) lβΌl
rewrite ββlowβ‘β {β} = β-castl β-l (β-cast (β-id lβl) lβl ββ)
stamp!β-left-prec {low} (v-cast (ir (inj (id {l low})) _)) v-l (β-castl β-l cΜ
βββ²) lβΌh =
β-cast β-l !ββ
stamp!β-left-prec {low} (v-cast (ir (inj (id {l high})) _)) v-l (β-castl β-l cΜ
βββ²) lβΌh =
β-castl β-l (β-cast (β-id lβl) lβl ββ)
stamp!β-left-prec {high} (v-cast (ir (inj (id {l low})) _)) v-l (β-castl β-l cΜ
βββ²) hβΌh =
β-cast β-l (β-castl (prec-refl _) lβl ββ)
stamp!β-left-prec {high} (v-cast (ir (inj (id {l high})) _)) v-l (β-castl β-l cΜ
βββ²) hβΌh =
β-castl β-l (β-cast (β-id lβl) lβl ββ)
stamp!β-left-prec {β} (v-cast (ir (inj (up id)) _)) v-l (β-castl β-l (β-cast _ () ββ))
stamp!β-left-prec {β} (v-cast (ir (up id) _)) v-l (β-castl β-l (β-cast _ lβl ()))
stamp!β-left-prec (v-cast (ir π _ )) (v-cast (ir πβ² _)) MβMβ² βββΌββ
with precββ’ MβMβ²
... | β¨ β’cast β’l , β’cast β’l β©
with prec-inv MβMβ²
... | β¨ refl , cΜ
βcΜ
β² β© = β-cast β-l (stamp!β-left-prec π πβ² cΜ
βcΜ
β² βββΌββ)
stamp!β-prec : β {β ββ²} {V Vβ² g gβ²}
β (v : LVal V)
β (vβ² : LVal Vβ²)
β β’ V β Vβ² β g β gβ²
β β βΌ ββ²
β β’ stamp!β V v β β stamp!β Vβ² vβ² ββ² β β β β
stamp!β-prec v-l v-l β-l ββΌββ² = β-cast β-l (stamp!β-prec id id (β-id lβl) ββΌββ²)
stamp!β-prec v-l (v-cast (ir id _)) (β-castr β-l ββcΜ
) lβΌl = β-cast β-l (prec-refl _)
stamp!β-prec v-l (v-cast (ir (inj id) _)) (β-castr β-l ββcΜ
) lβΌl = β-cast β-l (prec-refl _)
stamp!β-prec v-l (v-cast (ir (inj (up id)) _)) (β-castr β-l ββcΜ
) lβΌl = β-cast β-l !ββ!
stamp!β-prec v-l (v-cast (ir (up id) _)) (β-castr β-l ββcΜ
) lβΌl = β-cast β-l !ββ!
stamp!β-prec v-l (v-cast (ir id x)) (β-castr β-l ββcΜ
) lβΌh =
contradiction refl (recompute (Β¬? (_ ==? _)) x)
stamp!β-prec (v-l {high}) (v-cast (ir (inj id) _)) (β-castr β-l ββcΜ
) lβΌh =
β-cast β-l (prec-refl _)
stamp!β-prec (v-l {low}) (v-cast (ir (inj id) _)) (β-castr β-l ββcΜ
) lβΌh = β-cast β-l !ββ!
stamp!β-prec v-l (v-cast (ir (inj (up id)) _)) (β-castr β-l ββcΜ
) lβΌh = β-cast β-l !ββ!
stamp!β-prec v-l (v-cast (ir (up id) _)) (β-castr β-l ββcΜ
) lβΌh = β-cast β-l !ββ!
stamp!β-prec v-l (v-cast (ir id x)) (β-castr β-l ββcΜ
) hβΌh =
contradiction refl (recompute (Β¬? (_ ==? _)) x)
stamp!β-prec (v-l {high}) (v-cast (ir (inj id) _)) (β-castr β-l ββcΜ
) hβΌh =
β-cast β-l (prec-refl _)
stamp!β-prec (v-l {low}) (v-cast (ir (inj id) _)) (β-castr β-l ββcΜ
) hβΌh =
β-cast β-l (prec-refl _)
stamp!β-prec v-l (v-cast (ir (inj (up id)) _)) (β-castr β-l (β-cast _ () _)) hβΌh
stamp!β-prec v-l (v-cast (ir (up id) _)) (β-castr β-l (β-cast _ _ ())) hβΌh
stamp!β-prec (v-cast (ir id x)) v-l (β-castl β-l _) ββΌββ² =
contradiction refl (recompute (Β¬? (_ ==? _)) x)
stamp!β-prec (v-cast (ir (inj id) _)) (v-l {β}) (β-castl β-l x) lβΌl = β-cast β-l (prec-refl _)
stamp!β-prec (v-cast {high} (ir (inj id) _)) (v-l {.high}) (β-castl β-l x) lβΌh =
β-cast β-l (prec-refl _)
stamp!β-prec (v-cast {low} (ir (inj id) _)) (v-l {.low}) (β-castl β-l x) lβΌh =
β-cast β-l !ββ!
stamp!β-prec (v-cast {high} (ir (inj id) _)) (v-l {.high}) (β-castl β-l x) hβΌh = β-cast β-l (prec-refl _)
stamp!β-prec (v-cast {low} (ir (inj id) _)) (v-l {.low}) (β-castl β-l x) hβΌh = β-cast β-l (prec-refl _)
stamp!β-prec (v-cast (ir (inj (up id)) _)) v-l (β-castl VβVβ² (β-cast (β-cast x lβl ()) _ _))
stamp!β-prec (v-cast (ir (up id) _)) v-l (β-castl VβVβ² (β-cast x lβl ()))
stamp!β-prec (v-cast (ir v _)) (v-cast (ir vβ² _)) VβVβ² ββΌββ²
with precββ’ VβVβ²
... | β¨ β’cast β’l , β’cast β’l β©
with prec-inv VβVβ²
... | β¨ refl , cΜ
βcΜ
β² β© =
β-cast β-l (stamp!β-prec v vβ² cΜ
βcΜ
β² ββΌββ²)