| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import String. |
| 3 |
From Coq Require Import List. |
| 4 |
Require Import bedrock2.Semantics. |
| 5 |
Require Import bedrock2.Map.Separation. |
| 6 |
Require Import bedrock2.Array bedrock2.Scalars. |
| 7 |
Require Import coqutil.Map.Interface. |
| 8 |
Require Import coqutil.Word.Interface. |
| 9 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 10 |
Require Import Crypto.Language.API. |
| 11 |
Local Open Scope Z_scope. |
| 12 |
|
| 13 |
Import API.Compilers. |
| 14 |
Import Types.Notations. |
| 15 |
|
| 16 |
Section Equivalent. |
| 17 |
Context |
| 18 |
{width BW word mem locals ext_spec varname_gen error} |
| 19 |
`{parameters_sentinel : @parameters |
| 20 |
width BW word mem locals ext_spec varname_gen error}. |
| 21 |
Local Notation parameters := (ltac:(let t := type of parameters_sentinel in exact t)) (only parsing). |
| 22 |
Context {listZ : rep.rep base_listZ}. |
| 23 |
Existing Instance rep.Z. |
| 24 |
|
| 25 |
(* relation that states whether a fiat-crypto value and a bedrock2 value are |
| 26 |
equivalent in a given bedrock2 context *) |
| 27 |
Fixpoint equivalent_base {t} |
| 28 |
: base.interp t -> (* fiat-crypto value *) |
| 29 |
base_rtype t -> (* bedrock2 value *) |
| 30 |
base_access_sizes t -> (* size in memory (if applicable) *) |
| 31 |
locals -> |
| 32 |
mem -> Prop := |
| 33 |
match t with |
| 34 |
| base.type.prod a b => |
| 35 |
fun (x : base.interp a * base.interp b) |
| 36 |
(y : base_rtype a * base_rtype b) |
| 37 |
(s : base_access_sizes a * base_access_sizes b) |
| 38 |
locals => |
| 39 |
sep (equivalent_base (fst x) (fst y) (fst s) locals) |
| 40 |
(equivalent_base (snd x) (snd y) (snd s) locals) |
| 41 |
| base_listZ => rep.equiv |
| 42 |
| base_Z => rep.equiv |
| 43 |
| _ => fun _ _ _ _ => emp False |
| 44 |
end. |
| 45 |
|
| 46 |
(* produces a separation-logic condition stating that the values of |
| 47 |
arguments are equivalent *) |
| 48 |
Fixpoint equivalent_args {t} |
| 49 |
: type.for_each_lhs_of_arrow |
| 50 |
API.interp_type t -> (* fiat-crypto value *) |
| 51 |
type.for_each_lhs_of_arrow rtype t -> (* bedrock2 value *) |
| 52 |
type.for_each_lhs_of_arrow |
| 53 |
access_sizes t -> (* sizes in memory *) |
| 54 |
locals -> |
| 55 |
mem -> Prop := |
| 56 |
match t with |
| 57 |
| type.base b => fun _ _ _ _ _ => True |
| 58 |
| type.arrow (type.base a) b => |
| 59 |
fun (x : base.interp a * _) (y : base_rtype a * _) |
| 60 |
(s : base_access_sizes a * _) locals mem => |
| 61 |
(exists R, |
| 62 |
sep (equivalent_base (fst x) (fst y) (fst s) locals) R mem) |
| 63 |
/\ (equivalent_args (snd x) (snd y) (snd s) locals mem) |
| 64 |
| _ => fun _ _ _ _ _ => False |
| 65 |
end. |
| 66 |
|
| 67 |
Definition locally_equivalent_base {t} x y locals := |
| 68 |
@equivalent_base t x y (base_dummy_access_sizes t) locals map.empty. |
| 69 |
|
| 70 |
Definition locally_equivalent_args {t} x y locals := |
| 71 |
@equivalent_args t x y (dummy_access_sizes_args t) locals map.empty. |
| 72 |
|
| 73 |
(* wrapper that uses non-base types *) |
| 74 |
Definition equivalent {t : API.type} |
| 75 |
: API.interp_type t -> (* fiat-crypto value *) |
| 76 |
rtype t -> (* bedrock2 value *) |
| 77 |
access_sizes t -> (* sizes in memory *) |
| 78 |
locals -> |
| 79 |
mem -> Prop := |
| 80 |
match t with |
| 81 |
| type.base b => equivalent_base |
| 82 |
| _ => fun _ _ _ _ _ => False |
| 83 |
end. |
| 84 |
Definition locally_equivalent {t} x y locals := |
| 85 |
@equivalent t x y (dummy_access_sizes t) locals map.empty. |
| 86 |
|
| 87 |
Fixpoint equivalent_listonly {t} |
| 88 |
: base.interp t -> (* fiat-crypto value *) |
| 89 |
listonly_base_rtype t -> (* bedrock2 value *) |
| 90 |
base_access_sizes t -> |
| 91 |
locals -> |
| 92 |
mem -> Prop := |
| 93 |
match t with |
| 94 |
| base.type.prod a b => |
| 95 |
fun x y s locals => |
| 96 |
sep (equivalent_listonly (fst x) (fst y) (fst s) locals) |
| 97 |
(equivalent_listonly (snd x) (snd y) (snd s) locals) |
| 98 |
| base_listZ => rep.equiv |
| 99 |
| base_Z => fun _ _ _ _ => emp True |
| 100 |
| _ => fun _ _ _ _ => emp False |
| 101 |
end. |
| 102 |
|
| 103 |
Fixpoint equivalent_listexcl {t} |
| 104 |
: base.interp t -> (* fiat-crypto value *) |
| 105 |
listexcl_base_rtype t -> (* bedrock2 value *) |
| 106 |
base_access_sizes t -> |
| 107 |
locals -> |
| 108 |
mem -> Prop := |
| 109 |
match t with |
| 110 |
| base.type.prod a b => |
| 111 |
fun x y s locals => |
| 112 |
sep (equivalent_listexcl (fst x) (fst y) (fst s) locals) |
| 113 |
(equivalent_listexcl (snd x) (snd y) (snd s) locals) |
| 114 |
| base_listZ => fun _ _ _ _ => emp True |
| 115 |
| base_Z => rep.equiv |
| 116 |
| _ => fun _ _ _ _ => emp False |
| 117 |
end. |
| 118 |
End Equivalent. |
| 119 |
|
| 120 |
(* equivalence with flat lists of words *) |
| 121 |
Section EquivalentFlat. |
| 122 |
Context |
| 123 |
{width BW word mem locals ext_spec varname_gen error} |
| 124 |
`{parameters_sentinel : @parameters |
| 125 |
width BW word mem locals ext_spec varname_gen error}. |
| 126 |
Local Notation parameters := (ltac:(let t := type of parameters_sentinel in exact t)) (only parsing). |
| 127 |
Existing Instances rep.listZ_mem rep.Z. |
| 128 |
|
| 129 |
Fixpoint equivalent_flat_base {t} |
| 130 |
: base.interp t -> |
| 131 |
list word -> |
| 132 |
base_access_sizes t -> |
| 133 |
mem -> Prop := |
| 134 |
match t as t0 return |
| 135 |
base.interp t0 -> list word |
| 136 |
-> base_access_sizes t0 -> _ with |
| 137 |
| base.type.prod a b => |
| 138 |
fun (x : base.interp a * base.interp b) words sizes => |
| 139 |
Lift1Prop.ex1 |
| 140 |
(fun i => |
| 141 |
sep (equivalent_flat_base (fst x) (firstn i words) (fst sizes)) |
| 142 |
(equivalent_flat_base (snd x) (skipn i words) (snd sizes))) |
| 143 |
| base_listZ => |
| 144 |
fun (x : list Z) words (sizes : rep.size) => |
| 145 |
(* since this is in-memory representation, [words] should be one word |
| 146 |
that indicates the memory location of the head of the list *) |
| 147 |
sep |
| 148 |
(map:=mem) |
| 149 |
(emp (length words = 1%nat)) |
| 150 |
(let addr := word.unsigned (hd (word.of_Z 0%Z) words) in |
| 151 |
rep.equiv (rep:=rep.listZ_mem) |
| 152 |
x (Syntax.expr.literal addr) sizes map.empty) |
| 153 |
| base_Z => |
| 154 |
fun (x : Z) words sizes => |
| 155 |
sep |
| 156 |
(map:=mem) |
| 157 |
(emp (length words = 1%nat)) |
| 158 |
(let w := word.unsigned (hd (word.of_Z 0%Z) words) in |
| 159 |
rep.equiv (rep:=rep.Z) x |
| 160 |
(Syntax.expr.literal w) sizes map.empty) |
| 161 |
| _ => fun _ _ _ => emp False |
| 162 |
end. |
| 163 |
|
| 164 |
Fixpoint equivalent_flat_args {t} |
| 165 |
: type.for_each_lhs_of_arrow API.interp_type t -> |
| 166 |
list word -> |
| 167 |
type.for_each_lhs_of_arrow access_sizes t -> |
| 168 |
mem -> Prop := |
| 169 |
match t as t0 |
| 170 |
return type.for_each_lhs_of_arrow _ t0 -> _ -> |
| 171 |
type.for_each_lhs_of_arrow _ t0 -> _ |
| 172 |
with |
| 173 |
| type.base _ => fun (_:unit) words _ _ => words = nil |
| 174 |
| type.arrow (type.base a) b => |
| 175 |
fun x words sizes mem => |
| 176 |
exists i, |
| 177 |
(exists R, |
| 178 |
sep (equivalent_flat_base |
| 179 |
(fst x) (firstn i words) (fst sizes)) R mem) |
| 180 |
/\ (equivalent_flat_args |
| 181 |
(snd x) (skipn i words) (snd sizes) mem) |
| 182 |
| _ => fun _ _ _ _ => False (* invalid argument *) |
| 183 |
end. |
| 184 |
Fixpoint equivalent_listexcl_flat_base {t} |
| 185 |
: base.interp t -> |
| 186 |
list word -> |
| 187 |
base_access_sizes t -> |
| 188 |
mem -> Prop := |
| 189 |
match t as t0 return |
| 190 |
base.interp t0 -> _ -> base_access_sizes t0 |
| 191 |
-> mem -> Prop with |
| 192 |
| base.type.prod a b => |
| 193 |
fun (x : base.interp a * base.interp b) words s=> |
| 194 |
Lift1Prop.ex1 |
| 195 |
(fun i => |
| 196 |
sep (equivalent_listexcl_flat_base |
| 197 |
(fst x) (firstn i words) (fst s)) |
| 198 |
(equivalent_listexcl_flat_base |
| 199 |
(snd x) (skipn i words) (snd s))) |
| 200 |
| base_listZ => fun _ words _ => emp (words = nil) |
| 201 |
| base_Z => equivalent_flat_base |
| 202 |
| _ => fun _ _ _ => emp False |
| 203 |
end. |
| 204 |
|
| 205 |
Fixpoint equivalent_listonly_flat_base {t} |
| 206 |
: base.interp t -> |
| 207 |
list word -> |
| 208 |
base_access_sizes t -> |
| 209 |
mem -> Prop := |
| 210 |
match t as t0 return |
| 211 |
base.interp t0 -> _ -> base_access_sizes t0 |
| 212 |
-> mem -> Prop with |
| 213 |
| base.type.prod a b => |
| 214 |
fun (x : base.interp a * base.interp b) words sizes => |
| 215 |
Lift1Prop.ex1 |
| 216 |
(fun i => |
| 217 |
sep (equivalent_listonly_flat_base |
| 218 |
(fst x) (firstn i words) (fst sizes)) |
| 219 |
(equivalent_listonly_flat_base |
| 220 |
(snd x) (skipn i words) (snd sizes))) |
| 221 |
| base_listZ => equivalent_flat_base |
| 222 |
| base_Z => fun _ words _ => emp (words = nil) |
| 223 |
| _ => fun _ _ _ => emp False |
| 224 |
end. |
| 225 |
End EquivalentFlat. |