| 1 |
Require Import coqutil.Datatypes.List Coq.Lists.List. |
| 2 |
Require Import Bedrock.P256.Specs. |
| 3 |
Require Import Bedrock.P256.Platform. |
| 4 |
Import bedrock2.NotationsCustomEntry Specs.NotationsCustomEntry. |
| 5 |
Import bedrock2Examples.shrd. |
| 6 |
|
| 7 |
Definition p256_coord_nonzero := func! (p_x) ~> nz { |
| 8 |
nz = load(p_x) | load(p_x.+$4) | load(p_x.+$4.+$4) | load(p_x.+$4.+$4.+$4); |
| 9 |
nz = nz | load(p_x.+$4.+$4.+$4.+$4); |
| 10 |
nz = nz | load(p_x.+$4.+$4.+$4.+$4.+$4); |
| 11 |
nz = nz | load(p_x.+$4.+$4.+$4.+$4.+$4.+$4); |
| 12 |
nz = nz | load(p_x.+$4.+$4.+$4.+$4.+$4.+$4.+$4); |
| 13 |
unpack! nz = br_broadcast_nonzero(nz) |
| 14 |
}. |
| 15 |
|
| 16 |
(* |
| 17 |
Definition p256_coord_sub := func!(out, x, y) { |
| 18 |
unpack! t0, borrow = full_sub(load(x), load(y), $0); |
| 19 |
unpack! t1, borrow = full_sub(load(x+$4), load(y+$4), borrow); |
| 20 |
unpack! t2, borrow = full_sub(load(x+$4+$4), load(y+$4+$4), borrow); |
| 21 |
unpack! t3, borrow = full_sub(load(x+$4+$4+$4), load(y+$4+$4+$4), borrow); |
| 22 |
unpack! t4, borrow = full_sub(load(x+$4+$4+$4+$4), load(y+$4+$4+$4+$4), borrow); |
| 23 |
unpack! t5, borrow = full_sub(load(x+$4+$4+$4+$4+$4), load(y+$4+$4+$4+$4+$4), borrow); |
| 24 |
unpack! t6, borrow = full_sub(load(x+$4+$4+$4+$4+$4+$4), load(y+$4+$4+$4+$4+$4+$4), borrow); |
| 25 |
unpack! t7, borrow = full_sub(load(x+$4+$4+$4+$4+$4+$4+$4), load(y+$4+$4+$4+$4+$4+$4+$4), borrow); |
| 26 |
unpack! mask = br_value_barrier(-borrow); |
| 27 |
unpack! r0, carry = full_add(t0, mask, $0); |
| 28 |
unpack! r1, carry = full_add(t1, mask, carry); |
| 29 |
unpack! r2, carry = full_add(t2, mask, carry); |
| 30 |
unpack! r3, carry = full_add(t3, $0, carry); |
| 31 |
unpack! r4, carry = full_add(t4, $0, carry); |
| 32 |
unpack! r5, carry = full_add(t5, $0, carry); |
| 33 |
unpack! r6, carry = full_add(t6, borrow, carry); |
| 34 |
unpack! r7, carry = full_add(t7, mask, carry); |
| 35 |
store(out, r0); |
| 36 |
store(out+$4, r1); |
| 37 |
store(out+$4+$4, r2); |
| 38 |
store(out+$4+$4+$4, r3); |
| 39 |
store(out+$4+$4+$4+$4, r4); |
| 40 |
store(out+$4+$4+$4+$4+$4, r5); |
| 41 |
store(out+$4+$4+$4+$4+$4+$4, r6); |
| 42 |
store(out+$4+$4+$4+$4+$4+$4+$4, r7) |
| 43 |
}. |
| 44 |
*) |
| 45 |
|
| 46 |
Definition u256_shr := func!(p_out, p_x, n) { |
| 47 |
x0 = load(p_x); |
| 48 |
x1 = load(p_x+$4); |
| 49 |
x2 = load(p_x+$4+$4); |
| 50 |
x3 = load(p_x+$4+$4+$4); |
| 51 |
x4 = load(p_x+$4+$4+$4+$4); |
| 52 |
x5 = load(p_x+$4+$4+$4+$4+$4); |
| 53 |
x6 = load(p_x+$4+$4+$4+$4+$4+$4); |
| 54 |
x7 = load(p_x+$4+$4+$4+$4+$4+$4+$4); |
| 55 |
unpack! y0 = shrd(x0, x1, n); |
| 56 |
unpack! y1 = shrd(x1, x2, n); |
| 57 |
unpack! y2 = shrd(x2, x3, n); |
| 58 |
unpack! y3 = shrd(x3, x4, n); |
| 59 |
unpack! y4 = shrd(x4, x5, n); |
| 60 |
unpack! y5 = shrd(x5, x6, n); |
| 61 |
unpack! y6 = shrd(x6, x7, n); |
| 62 |
y7 = x7 >> n; |
| 63 |
store(p_out, y0); |
| 64 |
store(p_out+$4, y1); |
| 65 |
store(p_out+$4+$4, y2); |
| 66 |
store(p_out+$4+$4+$4, y3); |
| 67 |
store(p_out+$4+$4+$4+$4, y4); |
| 68 |
store(p_out+$4+$4+$4+$4+$4, y5); |
| 69 |
store(p_out+$4+$4+$4+$4+$4+$4, y6); |
| 70 |
store(p_out+$4+$4+$4+$4+$4+$4+$4, y7) |
| 71 |
}. |
| 72 |
|
| 73 |
Definition u256_set_p256_minushalf_conditional := func!(p_out, mask) { |
| 74 |
mh0 = -$1; mh1 = mh0; mh2 = mh0>>$1; mh3 = $0; mh4 = $0; mh5 = $1<<$31; mh6 = mh5; mh7 = mh2; |
| 75 |
store(p_out, mask&mh0); |
| 76 |
store(p_out+$4, mask&mh1); |
| 77 |
store(p_out+$4+$4, mask&mh2); |
| 78 |
store(p_out+$4+$4+$4, mask&mh3); |
| 79 |
store(p_out+$4+$4+$4+$4, mask&mh4); |
| 80 |
store(p_out+$4+$4+$4+$4+$4, mask&mh5); |
| 81 |
store(p_out+$4+$4+$4+$4+$4+$4, mask&mh6); |
| 82 |
store(p_out+$4+$4+$4+$4+$4+$4+$4, mask&mh7) |
| 83 |
}. |
| 84 |
|
| 85 |
Import String List bedrock2.ToCString Macros.WithBaseName. |
| 86 |
Local Open Scope string_scope. Local Open Scope list_scope. |
| 87 |
|
| 88 |
Definition coord32 := &[, |
| 89 |
shrd; |
| 90 |
p256_coord_nonzero; |
| 91 |
u256_shr; |
| 92 |
u256_set_p256_minushalf_conditional |
| 93 |
]. |
| 94 |
|
| 95 |
From Coq Require Import String List. |
| 96 |
Compute String.concat LF (List.map (fun f => "static inline "++ c_func f) coord32)%string. |
| 97 |
|
| 98 |
From bedrock2 Require Import BasicC32Semantics. |
| 99 |
|
| 100 |
Import bedrock2.Syntax bedrock2.NotationsCustomEntry |
| 101 |
LittleEndianList |
| 102 |
ZArith.BinInt |
| 103 |
BinInt BinNat Init.Byte |
| 104 |
PrimeFieldTheorems ModInv |
| 105 |
micromega.Lia |
| 106 |
coqutil.Byte |
| 107 |
Lists.List micromega.Lia |
| 108 |
Jacobian |
| 109 |
Coq.Strings.String Coq.Lists.List |
| 110 |
ProgramLogic WeakestPrecondition |
| 111 |
ProgramLogic.Coercions |
| 112 |
Word.Interface OfListWord Separation SeparationLogic SeparationMemory |
| 113 |
letexists |
| 114 |
ListIndexNotations |
| 115 |
SepAutoArray |
| 116 |
symmetry |
| 117 |
PeanoNat micromega.Lia |
| 118 |
Tactics |
| 119 |
UniquePose |
| 120 |
micromega.Lia Word.Properties |
| 121 |
coqutil.Tactics.Tactics. |
| 122 |
|
| 123 |
Import ListIndexNotations. |
| 124 |
Local Open Scope list_index_scope. |
| 125 |
Local Open Scope Z_scope. |
| 126 |
Local Open Scope bool_scope. |
| 127 |
Local Open Scope string_scope. |
| 128 |
Local Open Scope list_scope. |
| 129 |
|
| 130 |
Local Notation "xs $@ a" := (map.of_list_word_at a xs) |
| 131 |
(at level 10, format "xs $@ a"). |
| 132 |
Local Notation "$ n" := (match word.of_Z n return word with w => w end) (at level 9, format "$ n"). |
| 133 |
Local Notation "p .+ n" := (word.add p (word.of_Z n)) (at level 50, format "p .+ n", left associativity). |
| 134 |
Local Coercion F.to_Z : F >-> Z. |
| 135 |
|
| 136 |
Import Specs.NotationsCustomEntry Specs.coord Specs.point. |
| 137 |
Local Ltac length_tac := |
| 138 |
repeat rewrite |
| 139 |
?length_coord, ?length_point, |
| 140 |
?app_length, ?firstn_length, ?skipn_length, ?length_le_split, ?length_nil, |
| 141 |
?Nat.min_l |
| 142 |
by (trivial; lia); trivial; lia. |
| 143 |
|
| 144 |
|
| 145 |
Lemma fiat_coord_nonzero_ok : program_logic_goal_for_function! p256_coord_nonzero. |
| 146 |
Proof. |
| 147 |
cbv [spec_of_p256_coord_nonzero]; |
| 148 |
repeat straightline. |
| 149 |
|
| 150 |
rename H0 into Hm. |
| 151 |
cbv [coord.to_bytes] in *. |
| 152 |
set (x * coord.R)%F as xR in *. |
| 153 |
do 8 ( |
| 154 |
rewrite <-(firstn_skipn 4 (le_split _ _)), List.firstn_le_split, skipn_le_split, ?Z.shiftr_shiftr in Hm by lia; |
| 155 |
simpl Nat.min in Hm; simpl Nat.sub in Hm; set (le_split 4 _) in Hm); |
| 156 |
rewrite List.le_split_0_l in Hm. |
| 157 |
simpl Z.mul in *; simpl Z.add in *. |
| 158 |
subst l0 l1 l2 l3 l4 l5 l6 l7. |
| 159 |
|
| 160 |
repeat (seprewrite_in_by (@sep_eq_of_list_word_at_app) Hm length_tac). |
| 161 |
repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) Hm length_tac. |
| 162 |
repeat seprewrite_in_by @Scalars.scalar_of_bytes Hm length_tac. |
| 163 |
rewrite ?le_combine_split, ?Z.shiftr_div_pow2 in Hm by length_tac. |
| 164 |
simpl Z.of_nat in *. |
| 165 |
simpl Z.mul in *. |
| 166 |
|
| 167 |
repeat (straightline || straightline_call). |
| 168 |
clear Hm. |
| 169 |
|
| 170 |
subst x1. f_equal. f_equal. apply Bool.eq_true_iff_eq. |
| 171 |
rewrite Z.eqb_eq, F.eqb_eq. |
| 172 |
rewrite <-word.unsigned_of_Z_0, !word.unsigned_inj_iff by exact _. |
| 173 |
subst nz nz'0 nz'1 nz'2 v. |
| 174 |
rewrite !word.lor_0_iff, !word.zero_of_Z_iff, !Zdiv.Zmod_mod by exact _. |
| 175 |
|
| 176 |
rewrite coord.zero_iff; fold xR. |
| 177 |
rewrite F.zero_iff_to_Z. |
| 178 |
pose proof F.to_Z_range xR eq_refl as range. |
| 179 |
clearbody xR; clear x; set (F.to_Z xR) as x in *; clearbody x. |
| 180 |
clear -range. |
| 181 |
Time PreOmega.Z.to_euclidean_division_equations. |
| 182 |
cbv [p256] in *. |
| 183 |
lia. |
| 184 |
Qed. |
| 185 |
|
| 186 |
Import shrd. |
| 187 |
Lemma u256_shr_ok : program_logic_goal_for_function! u256_shr. |
| 188 |
Proof. |
| 189 |
cbv [spec_of_u256_shr]. |
| 190 |
straightline. |
| 191 |
clear H2 H3 H4 H5. |
| 192 |
repeat straightline_cleanup. |
| 193 |
|
| 194 |
cbv [coord.to_bytes] in *. |
| 195 |
let domem Hm := |
| 196 |
do 8 ( |
| 197 |
rewrite <-(firstn_skipn 4 (le_split _ _)), List.firstn_le_split, skipn_le_split, ?Z.shiftr_shiftr in Hm by lia; |
| 198 |
simpl Nat.min in Hm; simpl Nat.sub in Hm; set (le_split 4 _) in Hm) in |
| 199 |
domem H2. |
| 200 |
rewrite <-(firstn_skipn 4 out), <-(firstn_skipn 4 out[_:]), <-(firstn_skipn 4 out[_:][_:]), |
| 201 |
<-(firstn_skipn 4 out[_:][_:][_:]), |
| 202 |
<-(firstn_skipn 4 out[_:][_:][_:][_:]), |
| 203 |
<-(firstn_skipn 4 out[_:][_:][_:][_:][_:]), |
| 204 |
<-(firstn_skipn 4 out[_:][_:][_:][_:][_:][_:]), |
| 205 |
?skipn_skipn, ?firstn_skipn in H3. |
| 206 |
subst l l0 l1 l2 l3 l4 l5 l6. |
| 207 |
|
| 208 |
let domem Hm := |
| 209 |
repeat seprewrite_in_by (@sep_eq_of_list_word_at_app) Hm length_tac; |
| 210 |
repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) Hm length_tac; |
| 211 |
repeat seprewrite_in_by @Scalars.scalar_of_bytes Hm length_tac; |
| 212 |
rewrite ?le_combine_split in Hm by lia |
| 213 |
in domem H2; domem H3. |
| 214 |
|
| 215 |
simpl Z.of_nat in *; simpl Z.mul in *; simpl Z.add in *; simpl Nat.add in *. |
| 216 |
progress repeat (straightline || straightline_call); try ZnWords.ZnWords. |
| 217 |
|
| 218 |
rename H14 into H14'; rename H22 into H14. |
| 219 |
cbv [Scalars.scalar Scalars.truncated_word Scalars.truncated_scalar] in H14. |
| 220 |
repeat seprewrite_in_by ptsto_bytes_iff_eq_of_list_word_at H14 ltac:(try rewrite ?length_le_split, ?Scalars.bytes_per_width_bytes_per_word; cbv [Memory.bytes_per_word]; try ZnWords.ZnWords). |
| 221 |
rewrite ?Scalars.bytes_per_width_bytes_per_word in H14. |
| 222 |
progress change (Memory.bytes_per access_size.word) with 4%nat in H14. |
| 223 |
seprewrite_in_by (@list_word_at_app_of_adjacent_eq) H14 ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords). |
| 224 |
seprewrite_in_by (@list_word_at_app_of_adjacent_eq) H14 ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords). |
| 225 |
seprewrite_in_by (@list_word_at_app_of_adjacent_eq) H14 ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords). |
| 226 |
seprewrite_in_by (@list_word_at_app_of_adjacent_eq) H14 ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords). |
| 227 |
seprewrite_in_by (@list_word_at_app_of_adjacent_eq) H14 ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords). |
| 228 |
seprewrite_in_by (@list_word_at_app_of_adjacent_eq) H14 ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords). |
| 229 |
seprewrite_in_by (@list_word_at_app_of_adjacent_eq) H14 ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords). |
| 230 |
rewrite <-?app_assoc in H14. |
| 231 |
revert H14; |
| 232 |
eassert ((_ ++ _) = _) as ->; [|intros;ecancel_assumption]. |
| 233 |
eapply le_combine_inj; rewrite ?app_length, ?length_le_combine, ?length_le_split; trivial. |
| 234 |
rewrite !le_combine_app, !le_combine_split, ?length_le_split; change (2^(8%nat*8)) with (2^64). |
| 235 |
rewrite ?Z.mod_small by (cbv [p256] in *; ZnWords.ZnWords). |
| 236 |
subst y7; rewrite word.unsigned_sru_nowrap, Z.shiftr_div_pow2 by ZnWords.ZnWords. |
| 237 |
subst x7 x6 x5 x4 x3 x2 x1 x0. |
| 238 |
progress rewrite ?word.unsigned_of_Z in *; cbv [word.wrap] in *; rewrite <-?Z.land_ones in * by lia. |
| 239 |
pose proof word.unsigned_range n. |
| 240 |
DestructHead.destruct_head' @and. |
| 241 |
simpl Z.mul. |
| 242 |
all : rewrite !H11, !H12, !H13, !H14', !H15, !H16, !H17. |
| 243 |
Import bitblast. |
| 244 |
all : apply Z.bits_inj'; intros i Hi; |
| 245 |
repeat rewrite <-?Z.shiftr_div_pow2, ?Z.land_spec, ?Z.lor_spec, ?Z.shiftr_spec', ?Z.shiftl_spec', ?Z.testbit_ones by try ZnWords.ZnWords. |
| 246 |
all: repeat (rewrite |
| 247 |
?Bool.andb_true_l, ?Bool.andb_true_r, ?Bool.orb_true_l, ?Bool.orb_true_r, |
| 248 |
?Bool.andb_false_l, ?Bool.andb_false_r, ?Bool.orb_false_l, ?Bool.orb_false_r, |
| 249 |
?Z.testbit_0_l, ?Z.testbit_neg_r, ?Z.testbit_high |
| 250 |
by intuition (idtac; |
| 251 |
match goal with |
| 252 |
| H : ?x < ?y^?a |- ?x < ?y^?b => |
| 253 |
apply (Z.lt_le_trans _ (y^a)), Z.pow_le_mono_r; lia |
| 254 |
| _ => lia |
| 255 |
end); |
| 256 |
cbn [negb]; trivial; try lia |
| 257 |
|| ((case Z.ltb_spec; [|]; intros)||(case Z.leb_spec; [|]; intros))). |
| 258 |
all: try (f_equal; lia). |
| 259 |
Qed. |
| 260 |
|
| 261 |
Lemma u256_set_p256_minushalf_conditional_ok : program_logic_goal_for_function! u256_set_p256_minushalf_conditional. |
| 262 |
Proof. |
| 263 |
cbv [spec_of_p256_coord_set_minushalf_conditional]. |
| 264 |
straightline; repeat straightline_cleanup. |
| 265 |
rename H into Hm. |
| 266 |
|
| 267 |
rewrite <-(firstn_skipn 4 out), <-(firstn_skipn 4 out[_:]), <-(firstn_skipn 4 out[_:][_:]), |
| 268 |
<-(firstn_skipn 4 out[_:][_:][_:]), |
| 269 |
<-(firstn_skipn 4 out[_:][_:][_:][_:]), |
| 270 |
<-(firstn_skipn 4 out[_:][_:][_:][_:][_:]), |
| 271 |
<-(firstn_skipn 4 out[_:][_:][_:][_:][_:][_:]), |
| 272 |
?skipn_skipn, ?firstn_skipn in Hm. |
| 273 |
repeat seprewrite_in_by (@sep_eq_of_list_word_at_app) Hm length_tac. |
| 274 |
repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) Hm length_tac. |
| 275 |
repeat seprewrite_in_by @Scalars.scalar_of_bytes Hm length_tac. |
| 276 |
simpl Z.of_nat in *. |
| 277 |
|
| 278 |
repeat straightline; ssplit; trivial. |
| 279 |
|
| 280 |
(* postcondition *) |
| 281 |
|
| 282 |
clear Hm; rename H7 into Hm. |
| 283 |
cbv [Scalars.scalar Scalars.truncated_word Scalars.truncated_scalar] in Hm. |
| 284 |
repeat seprewrite_in_by ptsto_bytes_iff_eq_of_list_word_at Hm ltac:(try rewrite ?length_le_split, ?Scalars.bytes_per_width_bytes_per_word; cbv [Memory.bytes_per_word]; try ZnWords.ZnWords). |
| 285 |
rewrite ?HList.tuple.to_list_of_list, ?Scalars.bytes_per_width_bytes_per_word in Hm. |
| 286 |
change (Memory.bytes_per access_size.word) with 4%nat in Hm. |
| 287 |
repeat seprewrite_in_by (@list_word_at_app_of_adjacent_eq) Hm ltac:(rewrite ?app_length, ?length_le_split, ?length_nil; try ZnWords.ZnWords). |
| 288 |
rewrite <-?app_assoc in Hm. |
| 289 |
simpl Nat.add in Hm. |
| 290 |
|
| 291 |
revert Hm; eassert ((_ ++ _) = _)%list as ->; [|intros;ecancel_assumption]. |
| 292 |
eapply le_combine_inj. { length_tac. } |
| 293 |
repeat match goal with x := _ |- _ => subst x end. |
| 294 |
case b; Decidable.vm_decide. |
| 295 |
Qed. |