| 1 |
Require Import coqutil.Byte coqutil.Word.LittleEndianList. |
| 2 |
Require Import Rupicola.Lib.Api. |
| 3 |
Require Import Crypto.Algebra.Hierarchy. |
| 4 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 5 |
Require Import Crypto.Bedrock.Field.Synthesis.Generic.Bignum. |
| 6 |
Require Import Crypto.Bedrock.Field.Common.Arrays.MaxBounds. |
| 7 |
Require Import Crypto.COperationSpecifications. |
| 8 |
Require Import Crypto.Util.ZUtil.ModInv. |
| 9 |
Local Open Scope Z_scope. |
| 10 |
Import bedrock2.Memory. |
| 11 |
|
| 12 |
Class FieldParameters := |
| 13 |
{ (** mathematical parameters **) |
| 14 |
M_pos : positive; (* modulus *) |
| 15 |
M : Z := Z.pos M_pos; |
| 16 |
a24 : F M_pos; (* (a+2) / 4 or (a-2) / 4, depending on the implementation *) |
| 17 |
|
| 18 |
(* special wrapper for copy so that compilation lemmas can recognize it *) |
| 19 |
fe_copy := (@id (F M_pos)); |
| 20 |
|
| 21 |
(** function names **) |
| 22 |
mul : string; add : string; carry_add : string; sub : string; carry_sub : string; opp : string; |
| 23 |
square : string; scmula24 : string; inv : string; |
| 24 |
from_bytes : string; to_bytes : string; |
| 25 |
select_znz : string; |
| 26 |
|
| 27 |
felem_copy : string; |
| 28 |
from_word : string; |
| 29 |
}. |
| 30 |
|
| 31 |
Class FieldParameters_ok {field_parameters : FieldParameters} := { |
| 32 |
M_prime : Znumtheory.prime M |
| 33 |
}. |
| 34 |
|
| 35 |
Class FieldRepresentation |
| 36 |
{field_parameters : FieldParameters} |
| 37 |
{width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte} |
| 38 |
:= |
| 39 |
{ felem := list word; |
| 40 |
feval : felem -> F M_pos; |
| 41 |
|
| 42 |
feval_bytes : list byte -> F M_pos; |
| 43 |
felem_size_in_words : nat; |
| 44 |
felem_size_in_bytes : Z := Z.of_nat felem_size_in_words * bytes_per_word width; (* for stack allocation *) |
| 45 |
encoded_felem_size_in_bytes : nat; (* number of bytes when serialized *) |
| 46 |
bytes_in_bounds : list byte -> Prop; |
| 47 |
|
| 48 |
(* Memory layout *) |
| 49 |
FElem : word -> list word -> mem -> Prop := Bignum felem_size_in_words; |
| 50 |
FElemBytes : word -> list byte -> mem -> Prop := |
| 51 |
fun addr bs => |
| 52 |
(emp (length bs = encoded_felem_size_in_bytes |
| 53 |
/\ bytes_in_bounds bs) |
| 54 |
* array ptsto (word.of_Z 1) addr bs)%sep; |
| 55 |
|
| 56 |
bounds : Type; |
| 57 |
bounded_by : bounds -> felem -> Prop; |
| 58 |
(* for saturated implementations, loose/tight bounds are the same *) |
| 59 |
loose_bounds : bounds; |
| 60 |
tight_bounds : bounds; |
| 61 |
}. |
| 62 |
|
| 63 |
Definition Placeholder |
| 64 |
{field_parameters : FieldParameters} |
| 65 |
{width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte} |
| 66 |
{field_representation : FieldRepresentation(mem:=mem)} |
| 67 |
(p : word) (bs : list byte): mem -> Prop := |
| 68 |
(sep (map.of_list_word_at p bs) |
| 69 |
(emp (Z.of_nat (length bs) = felem_size_in_bytes))). |
| 70 |
|
| 71 |
Class FieldRepresentation_ok |
| 72 |
{field_parameters : FieldParameters} |
| 73 |
{width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte} |
| 74 |
{field_representation : FieldRepresentation} := { |
| 75 |
relax_bounds : |
| 76 |
forall X : felem, bounded_by tight_bounds X |
| 77 |
-> bounded_by loose_bounds X; |
| 78 |
felem_size_ok : felem_size_in_bytes <= 2^width |
| 79 |
}. |
| 80 |
|
| 81 |
Section FunctionSpecs. |
| 82 |
Context {width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte}. |
| 83 |
Context {locals: map.map String.string word}. |
| 84 |
Context {ext_spec: bedrock2.Semantics.ExtSpec}. |
| 85 |
Context {word_ok : word.ok word} {mem_ok : map.ok mem}. |
| 86 |
Context {locals_ok : map.ok locals}. |
| 87 |
Context {ext_spec_ok : Semantics.ext_spec.ok ext_spec}. |
| 88 |
Context {field_parameters : FieldParameters} |
| 89 |
{field_representation : FieldRepresentation}. |
| 90 |
|
| 91 |
Class UnOp (name: string) := |
| 92 |
{ un_model: F M_pos -> F M_pos; |
| 93 |
un_xbounds: bounds; |
| 94 |
un_outbounds: bounds }. |
| 95 |
|
| 96 |
Import WeakestPrecondition. |
| 97 |
|
| 98 |
Definition unop_spec {name} (op: UnOp name) := |
| 99 |
fnspec! name (pout px : word) / (x : felem) out Rr, |
| 100 |
{ requires tr mem := |
| 101 |
bounded_by un_xbounds x |
| 102 |
/\ (exists Ra, (FElem px x * Ra)%sep mem) |
| 103 |
/\ (Placeholder pout out * Rr)%sep mem; |
| 104 |
ensures tr' mem' := |
| 105 |
tr = tr' /\ |
| 106 |
exists out, |
| 107 |
feval out = un_model (feval x) |
| 108 |
/\ bounded_by un_outbounds out |
| 109 |
/\ (FElem pout out * Rr)%sep mem' }. |
| 110 |
|
| 111 |
Instance spec_of_UnOp {name} (op: UnOp name) : spec_of name := |
| 112 |
unop_spec op. |
| 113 |
|
| 114 |
Class BinOp (name: string) := |
| 115 |
{ bin_model: F M_pos -> F M_pos -> F M_pos; |
| 116 |
bin_xbounds: bounds; |
| 117 |
bin_ybounds: bounds; |
| 118 |
bin_outbounds: bounds }. |
| 119 |
|
| 120 |
Definition binop_spec {name} (op: BinOp name) := |
| 121 |
fnspec! name (pout px py : word) / (x y : felem) out Rr, |
| 122 |
{ requires tr mem := |
| 123 |
bounded_by bin_xbounds x |
| 124 |
/\ bounded_by bin_ybounds y |
| 125 |
/\ (exists Rx, (FElem px x * Rx)%sep mem) |
| 126 |
/\ (exists Ry, (FElem py y * Ry)%sep mem) |
| 127 |
/\ (Placeholder pout out * Rr)%sep mem; |
| 128 |
ensures tr' mem' := |
| 129 |
tr = tr' /\ |
| 130 |
exists out, |
| 131 |
feval out = bin_model (feval x) (feval y) |
| 132 |
/\ bounded_by bin_outbounds out |
| 133 |
/\ (FElem pout out * Rr)%sep mem' }. |
| 134 |
|
| 135 |
Instance spec_of_BinOp {name} (op: BinOp name) : spec_of name := |
| 136 |
binop_spec op. |
| 137 |
|
| 138 |
Instance bin_mul : BinOp mul := |
| 139 |
{| bin_model := F.mul; bin_xbounds := loose_bounds; bin_ybounds := loose_bounds; bin_outbounds := tight_bounds |}. |
| 140 |
Instance un_square : UnOp square := |
| 141 |
{| un_model := fun x => F.pow x 2; un_xbounds := loose_bounds; un_outbounds := tight_bounds |}. |
| 142 |
Instance bin_add : BinOp add := |
| 143 |
{| bin_model := F.add; bin_xbounds := tight_bounds; bin_ybounds := tight_bounds; bin_outbounds := loose_bounds |}. |
| 144 |
Instance bin_carry_add : BinOp carry_add := |
| 145 |
{| bin_model := F.add; bin_xbounds := tight_bounds; bin_ybounds := tight_bounds; bin_outbounds := tight_bounds |}. |
| 146 |
Instance bin_sub : BinOp sub := |
| 147 |
{| bin_model := F.sub; bin_xbounds := tight_bounds; bin_ybounds := tight_bounds; bin_outbounds := loose_bounds |}. |
| 148 |
Instance bin_carry_sub : BinOp carry_sub := |
| 149 |
{| bin_model := F.sub; bin_xbounds := tight_bounds; bin_ybounds := tight_bounds; bin_outbounds := tight_bounds |}. |
| 150 |
Instance un_scmula24 : UnOp scmula24 := |
| 151 |
{| un_model := F.mul a24; un_xbounds := loose_bounds; un_outbounds := tight_bounds |}. |
| 152 |
Instance un_inv : UnOp inv := (* TODO: what are the bounds for inv? *) |
| 153 |
{| un_model := F.inv; un_xbounds := tight_bounds; un_outbounds := loose_bounds |}. |
| 154 |
Instance un_opp : UnOp opp := |
| 155 |
{| un_model := F.opp; un_xbounds := tight_bounds; un_outbounds := loose_bounds |}. |
| 156 |
|
| 157 |
Instance spec_of_from_bytes : spec_of from_bytes := |
| 158 |
fnspec! from_bytes (pout px : word) / (out bs : list byte) Rr, |
| 159 |
{ requires tr mem := |
| 160 |
(exists Ra, (array ptsto (word.of_Z 1) px bs * Ra)%sep mem) |
| 161 |
/\ (Placeholder pout out * Rr)%sep mem |
| 162 |
/\ Field.bytes_in_bounds bs; |
| 163 |
ensures tr' mem' := |
| 164 |
tr = tr' /\ |
| 165 |
exists X, feval X = feval_bytes bs |
| 166 |
/\ bounded_by tight_bounds X |
| 167 |
/\ (FElem pout X * Rr)%sep mem' }. |
| 168 |
|
| 169 |
Instance spec_of_to_bytes : spec_of to_bytes := |
| 170 |
fnspec! to_bytes (pout px : word) / (out : list byte) (x : felem) Rr, |
| 171 |
{ requires tr mem := |
| 172 |
(array ptsto (word.of_Z 1) pout out * Rr)%sep mem /\ |
| 173 |
length out = encoded_felem_size_in_bytes /\ |
| 174 |
(exists Ra, (FElem px x * Ra)%sep mem) /\ |
| 175 |
bounded_by tight_bounds x; |
| 176 |
ensures tr' mem' := tr = tr' /\ |
| 177 |
let bs := le_split encoded_felem_size_in_bytes (F.to_Z (feval x)) in |
| 178 |
(array ptsto (word.of_Z 1) pout bs * Rr)%sep mem' /\ |
| 179 |
Field.bytes_in_bounds bs }. |
| 180 |
|
| 181 |
Instance spec_of_felem_copy : spec_of felem_copy := |
| 182 |
fnspec! felem_copy (pout px : word) / (x : felem) out R, |
| 183 |
{ requires tr mem := |
| 184 |
(FElem px x * Placeholder pout out * R)%sep mem; |
| 185 |
ensures tr' mem' := |
| 186 |
tr = tr' /\ |
| 187 |
(FElem px x * FElem pout x * R)%sep mem' }. |
| 188 |
|
| 189 |
Instance spec_of_from_word : spec_of from_word := |
| 190 |
fnspec! from_word (pout x : word) / out R, |
| 191 |
{ requires tr mem := |
| 192 |
(Placeholder pout out * R)%sep mem; |
| 193 |
ensures tr' mem' := |
| 194 |
tr = tr' /\ |
| 195 |
exists X, feval X = F.of_Z _ (word.unsigned x) |
| 196 |
/\ bounded_by tight_bounds X |
| 197 |
/\ (FElem pout X * R)%sep mem' }. |
| 198 |
|
| 199 |
Local Notation bit_range := {|ZRange.lower := 0; ZRange.upper := 1|}. |
| 200 |
|
| 201 |
Instance spec_of_selectznz : spec_of select_znz := |
| 202 |
fnspec! select_znz (pout pc px py : word) / out Rout Rx Ry x y, |
| 203 |
{ |
| 204 |
requires tr mem := |
| 205 |
(Placeholder pout out * Rout)%sep mem /\ |
| 206 |
(FElem px x * Rx)%sep mem /\ |
| 207 |
(FElem py y * Ry)%sep mem /\ |
| 208 |
ZRange.is_bounded_by_bool (word.unsigned pc) bit_range = true; |
| 209 |
ensures tr' mem' := |
| 210 |
if ((word.unsigned pc) =? 1) |
| 211 |
then ((FElem pout y * Rout)%sep mem') |
| 212 |
else ((FElem pout x * Rout)%sep mem') |
| 213 |
}. |
| 214 |
|
| 215 |
(*Parameters for word-by-word Montgomery arithmetic*) |
| 216 |
Definition r := 2 ^ width. |
| 217 |
Definition m' := Z.modinv (- M) r. |
| 218 |
Definition r' := Z.modinv (r) M. |
| 219 |
|
| 220 |
Definition from_mont_model x := F.mul x (@F.of_Z M_pos (r' ^ (Z.of_nat felem_size_in_words)%Z)). |
| 221 |
Definition to_mont_model x := F.mul x (@F.of_Z M_pos (r ^ (Z.of_nat felem_size_in_words)%Z)). |
| 222 |
|
| 223 |
Instance un_from_mont {from_mont : string} : UnOp from_mont := |
| 224 |
{| un_model := from_mont_model; un_xbounds := tight_bounds; un_outbounds := loose_bounds |}. |
| 225 |
|
| 226 |
Instance un_to_mont {to_mont : string} : UnOp to_mont := |
| 227 |
{| un_model := to_mont_model; un_xbounds := tight_bounds; un_outbounds := loose_bounds|}. |
| 228 |
|
| 229 |
End FunctionSpecs. |
| 230 |
|
| 231 |
#[global] |
| 232 |
Existing Instances spec_of_UnOp spec_of_BinOp bin_mul un_square bin_add bin_sub |
| 233 |
un_scmula24 un_inv spec_of_felem_copy spec_of_from_word. |
| 234 |
|
| 235 |
Section SpecProperties. |
| 236 |
Context {width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte}. |
| 237 |
Context {locals: map.map String.string word}. |
| 238 |
Context {ext_spec: bedrock2.Semantics.ExtSpec}. |
| 239 |
Context {word_ok : word.ok word} {mem_ok : map.ok mem}. |
| 240 |
Context {locals_ok : map.ok locals}. |
| 241 |
Context {ext_spec_ok : Semantics.ext_spec.ok ext_spec}. |
| 242 |
Context {field_parameters : FieldParameters} |
| 243 |
{field_representation : FieldRepresentation} |
| 244 |
{field_representation_ok : FieldRepresentation_ok}. |
| 245 |
|
| 246 |
Lemma felem_size_in_bytes_mod : |
| 247 |
felem_size_in_bytes mod Memory.bytes_per_word width = 0. |
| 248 |
Proof. apply Z_mod_mult. Qed. |
| 249 |
|
| 250 |
Local Coercion Z.to_nat : Z >-> nat. |
| 251 |
|
| 252 |
Lemma Placeholder_impl_FElem_bytes p bs : |
| 253 |
Lift1Prop.impl1 (Placeholder p bs) (FElem p (bs2ws (bytes_per_word width) bs)). |
| 254 |
Proof. |
| 255 |
repeat intro. |
| 256 |
pose (Bignum_of_bytes felem_size_in_words p bs) as HBignum. |
| 257 |
epose (array1_iff_eq_of_list_word_at p bs) as HArray. |
| 258 |
pose felem_size_ok. |
| 259 |
cbv [FElem Placeholder felem_size_in_bytes] in *. |
| 260 |
intros. |
| 261 |
extract_ex1_and_emp_in_hyps. apply HBignum. lia. |
| 262 |
apply HArray. lia. assumption. |
| 263 |
Qed. |
| 264 |
|
| 265 |
Lemma FElem_impl_Placeholder p ws : |
| 266 |
Lift1Prop.impl1 (FElem p ws) (Placeholder p (ws2bs (bytes_per_word width) ws)). |
| 267 |
Proof. |
| 268 |
repeat intro. |
| 269 |
pose (Bignum_to_bytes felem_size_in_words p ws) as HBignum. |
| 270 |
pose felem_size_ok. |
| 271 |
cbv [FElem Placeholder felem_size_in_bytes] in *. |
| 272 |
apply HBignum in H. extract_ex1_and_emp_in_hyps. |
| 273 |
extract_ex1_and_emp_in_goal; split. |
| 274 |
apply array1_iff_eq_of_list_word_at; try assumption. |
| 275 |
lia. |
| 276 |
pose Types.word_size_in_bytes_pos. |
| 277 |
lia. |
| 278 |
Qed. |
| 279 |
|
| 280 |
Lemma Placeholder_iff_FElem_bytes p bs : |
| 281 |
Lift1Prop.iff1 (Placeholder p bs) (sep (FElem p ((bs2ws (bytes_per_word width) bs))) (emp (Datatypes.length bs = felem_size_in_bytes))). |
| 282 |
Proof. |
| 283 |
repeat intro. |
| 284 |
pose (Bignum_of_bytes felem_size_in_words p bs) as HBignum. |
| 285 |
epose (array1_iff_eq_of_list_word_at p bs) as HArray. |
| 286 |
pose felem_size_ok. |
| 287 |
cbv [FElem Placeholder felem_size_in_bytes] in *. |
| 288 |
split; intros. |
| 289 |
- sepsimpl. apply HBignum. lia. |
| 290 |
apply HArray. lia. assumption. |
| 291 |
lia. |
| 292 |
- sepsimpl. apply HArray; try lia. apply HBignum; try lia. |
| 293 |
assumption. pose Types.word_size_in_bytes_pos. lia. |
| 294 |
Qed. |
| 295 |
|
| 296 |
Lemma Placeholder_iff_FElem_words p ws : |
| 297 |
Lift1Prop.iff1 (sep (Placeholder p (ws2bs (bytes_per_word width) ws)) (emp (Datatypes.length ws = felem_size_in_words))) (FElem p ws). |
| 298 |
Proof. |
| 299 |
repeat intro. |
| 300 |
pose (Bignum_to_bytes felem_size_in_words p ws) as HBignum. |
| 301 |
pose felem_size_ok. |
| 302 |
cbv [FElem Placeholder felem_size_in_bytes] in *. |
| 303 |
split; intros. |
| 304 |
- sepsimpl. apply HBignum. |
| 305 |
sepsimpl; try lia. |
| 306 |
apply array1_iff_eq_of_list_word_at; try assumption; try lia. |
| 307 |
- apply HBignum in H. sepsimpl. |
| 308 |
apply array1_iff_eq_of_list_word_at; try assumption; lia. |
| 309 |
rewrite H. pose Types.word_size_in_bytes_pos. lia. |
| 310 |
pose Types.word_size_in_bytes_pos. |
| 311 |
rewrite ws2bs_length in H. nia. |
| 312 |
Qed. |
| 313 |
|
| 314 |
(* this is almost bytearray_iff_bytes *) |
| 315 |
Lemma Placeholder_iff_array1 p bs : |
| 316 |
Datatypes.length bs = felem_size_in_bytes -> |
| 317 |
Lift1Prop.iff1 (Placeholder p bs) (array ptsto (word.of_Z 1) p bs). |
| 318 |
Proof. |
| 319 |
repeat intro. |
| 320 |
cbv [Placeholder]. |
| 321 |
pose felem_size_ok. apply iff1_sym. |
| 322 |
rewrite array1_iff_eq_of_list_word_at; try assumption. |
| 323 |
- split; intros; extract_ex1_and_emp_in_goal; extract_ex1_and_emp_in_hyps; try split; try assumption. |
| 324 |
rewrite H. pose Types.word_size_in_bytes_pos. cbv [felem_size_in_bytes]. lia. |
| 325 |
- lia. |
| 326 |
Qed. |
| 327 |
|
| 328 |
Lemma M_nonzero : M <> 0. |
| 329 |
Proof. cbv [M]. congruence. Qed. |
| 330 |
End SpecProperties. |
| 331 |
|
| 332 |
|
| 333 |
(* array1 -> Placeholder, only works if length sidecondition can be solved by ZnWords or lia. *) |
| 334 |
#[export] Hint Extern 1 (Lift1Prop.impl1 (array ptsto (word.of_Z 1) ?p ?stack) (Placeholder (field_representation:=?frep) ?p _)) => ( |
| 335 |
unshelve(erewrite <- (Placeholder_iff_array1 (field_representation:=frep) p _ _)); |
| 336 |
[ (let s := eval lazy in (felem_size_in_bytes (FieldRepresentation:=frep)) in |
| 337 |
change (felem_size_in_bytes (FieldRepresentation:=frep)) with s in *; try lia; ZnWords) | |
| 338 |
apply impl1_refl]) : ecancel_impl. |
| 339 |
|
| 340 |
(* FElem -> Placeholder *) |
| 341 |
#[export] Hint Extern 1 (Lift1Prop.impl1 (FElem ?px ?x) (Placeholder ?px _)) => (apply FElem_impl_Placeholder) : ecancel_impl. |
| 342 |
|