| 1 |
Require Import coqutil.Datatypes.List Coq.Lists.List. |
| 2 |
Require Import Bedrock.P256.Specs. |
| 3 |
|
| 4 |
Import Specs.NotationsCustomEntry Specs.coord Specs.point. |
| 5 |
|
| 6 |
Import bedrock2.Syntax bedrock2.NotationsCustomEntry |
| 7 |
LittleEndianList |
| 8 |
Crypto.Util.ZUtil.Modulo Zdiv ZArith BinInt |
| 9 |
BinInt BinNat Init.Byte |
| 10 |
PrimeFieldTheorems ModInv |
| 11 |
micromega.Lia |
| 12 |
coqutil.Byte |
| 13 |
Lists.List micromega.Lia |
| 14 |
Jacobian |
| 15 |
Coq.Strings.String Coq.Lists.List |
| 16 |
ProgramLogic WeakestPrecondition |
| 17 |
ProgramLogic.Coercions |
| 18 |
Word.Interface OfListWord Separation SeparationLogic |
| 19 |
letexists |
| 20 |
BasicC64Semantics |
| 21 |
ListIndexNotations |
| 22 |
SepAutoArray |
| 23 |
symmetry |
| 24 |
PeanoNat micromega.Lia |
| 25 |
Tactics |
| 26 |
UniquePose |
| 27 |
micromega.Lia Word.Properties. |
| 28 |
|
| 29 |
Import Zbool. (*compat 8.20*) |
| 30 |
Import ListIndexNotations. |
| 31 |
Local Open Scope list_index_scope. |
| 32 |
Local Open Scope Z_scope. |
| 33 |
Local Open Scope bool_scope. |
| 34 |
Local Open Scope string_scope. |
| 35 |
Local Open Scope list_scope. |
| 36 |
|
| 37 |
Local Notation "xs $@ a" := (map.of_list_word_at a xs) |
| 38 |
(at level 10, format "xs $@ a"). |
| 39 |
Local Notation "$ n" := (match word.of_Z n return word with w => w end) (at level 9, format "$ n"). |
| 40 |
Local Notation "p .+ n" := (word.add p (word.of_Z n)) (at level 50, format "p .+ n", left associativity). |
| 41 |
Local Coercion F.to_Z : F >-> Z. |
| 42 |
|
| 43 |
|
| 44 |
Import Coq.micromega.Lia. |
| 45 |
Import coqutil.Tactics.Tactics. |
| 46 |
|
| 47 |
Definition p256_point_iszero := func! (p_P) ~> z { |
| 48 |
unpack! nz = p256_coord_nonzero(p_P.+$32.+$32); |
| 49 |
z = ~nz |
| 50 |
}. |
| 51 |
|
| 52 |
|
| 53 |
Lemma p256_point_iszero_ok : program_logic_goal_for_function! p256_point_iszero. |
| 54 |
Proof. |
| 55 |
cbv [spec_of_fiat_p256_point_iszero]. |
| 56 |
repeat straightline. |
| 57 |
|
| 58 |
rename H0 into Hm. |
| 59 |
cbv [point.to_bytes] in *. |
| 60 |
repeat seprewrite_in_by (@Array.sep_eq_of_list_word_at_app) Hm ltac:(rewrite ?app_length, ?length_coord; trivial; try lia). |
| 61 |
simpl Z.of_nat in *. |
| 62 |
|
| 63 |
straightline_call; [eexists; ecancel_assumption|]; repeat straightline. |
| 64 |
|
| 65 |
subst z x0. |
| 66 |
setoid_rewrite word.not_broadcast; rewrite Bool.negb_involutive; trivial. |
| 67 |
Qed. |
| 68 |
|
| 69 |
Definition p256_coord_halve := func!(y, x) { |
| 70 |
stackalloc 32 as mmh; |
| 71 |
unpack! m = br_broadcast_odd(load(x)); |
| 72 |
u256_set_p256_minushalf_conditional(mmh, m); |
| 73 |
u256_shr(y, x, $1); |
| 74 |
p256_coord_sub(y, y, mmh) |
| 75 |
}. |
| 76 |
|
| 77 |
|
| 78 |
Local Ltac length_tac := |
| 79 |
repeat rewrite |
| 80 |
?length_coord, ?length_point, |
| 81 |
?app_length, ?firstn_length, ?skipn_length, ?length_le_split, ?length_nil, |
| 82 |
?Nat.min_l |
| 83 |
by (trivial; lia); trivial; lia. |
| 84 |
|
| 85 |
Lemma p256_coord_halve_ok : |
| 86 |
let '_ := spec_of_p256_coord_sub_nonmont in |
| 87 |
program_logic_goal_for_function! p256_coord_halve. |
| 88 |
Proof. |
| 89 |
cbv [spec_of_p256_coord_halve]. |
| 90 |
straightline; repeat straightline_cleanup. |
| 91 |
|
| 92 |
pose proof (conj H3 H4) as Hm; clear H4 H3; pattern m in Hm. |
| 93 |
progress change (?P m) with (id P m) in Hm. |
| 94 |
repeat straightline. |
| 95 |
eapply sep_and_l_fwd in Hm; case Hm as [Hm Hm']. |
| 96 |
|
| 97 |
letexists; split. |
| 98 |
{ cbn. repeat straightline. eexists; split; [|reflexivity]. |
| 99 |
cbv [coord.to_bytes] in Hm. |
| 100 |
rewrite <-(firstn_skipn 8 (le_split _ _)), List.firstn_le_split, skipn_le_split, ?Z.shiftr_shiftr in Hm by lia. |
| 101 |
seprewrite_in_by (@Array.sep_eq_of_list_word_at_app) Hm ltac:(rewrite ?length_le_split; trivial; lia). |
| 102 |
seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) Hm ltac:(rewrite ?length_le_split; lia). |
| 103 |
seprewrite_in_by @Scalars.scalar_of_bytes Hm ltac:(rewrite ?length_le_split; lia). |
| 104 |
rewrite le_combine_split in Hm. |
| 105 |
repeat straightline. } |
| 106 |
|
| 107 |
straightline_call; [eexists; ecancel_assumption|]; repeat straightline. |
| 108 |
|
| 109 |
seprewrite_in_by Array.array1_iff_eq_of_list_word_at Hm0 ltac:(Lia.lia). |
| 110 |
|
| 111 |
straightline_call; ssplit; [ ecancel_assumption | trivial | exact eq_refl | ]. |
| 112 |
repeat straightline. |
| 113 |
|
| 114 |
eapply sep_and_r_fwd in H10; case H10 as [Hm3 Hm3']. |
| 115 |
|
| 116 |
straightline_call; ssplit. |
| 117 |
{ eexists. cbv [coord.to_bytes] in *. ecancel_assumption. } |
| 118 |
{ cbv [coord.to_bytes] in *. ecancel_assumption. } |
| 119 |
{ trivial. } |
| 120 |
{ eapply F.to_Z_range, eq_refl. } |
| 121 |
{ etransitivity; try eapply F.to_Z_range; eapply eq_refl. } |
| 122 |
{ ZnWords.ZnWords. } |
| 123 |
|
| 124 |
repeat straightline. |
| 125 |
rewrite ?Z.pow_1_r in *. |
| 126 |
|
| 127 |
set (Z.odd _) as b in *. |
| 128 |
|
| 129 |
eapply WeakestPreconditionProperties.Proper_call; cycle -1; |
| 130 |
[ eapply H2 | .. | intros ? ? ? ?]; ssplit. |
| 131 |
{ eexists. |
| 132 |
use_sep_assumption; cancel. |
| 133 |
cancel_seps_at_indices 0%nat 0%nat; [|cancel]. |
| 134 |
|
| 135 |
instantiate (1:=F.of_Z _ (F.to_Z (x*coord.R)%F / 2)). |
| 136 |
rewrite F.to_Z_of_Z, Z.mod_small; trivial. |
| 137 |
specialize (F.to_Z_range (x*coord.R) eq_refl); clear; PreOmega.Z.to_euclidean_division_equations; lia. } |
| 138 |
{ eexists. use_sep_assumption. cancel. |
| 139 |
cancel_seps_at_indices 1%nat 0%nat. { exact eq_refl. } |
| 140 |
cancel. } |
| 141 |
{ ecancel_assumption. } |
| 142 |
{ length_tac. } |
| 143 |
|
| 144 |
repeat straightline. |
| 145 |
|
| 146 |
(* stackdealloc *) |
| 147 |
progress repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) H12 length_tac. |
| 148 |
progress repeat match type of H12 with context [Array.array ptsto _ _ (le_split 32 ?x)] => |
| 149 |
unique pose proof (length_le_split 32 x) end. |
| 150 |
progress repeat straightline. |
| 151 |
progress repeat seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H12 length_tac. |
| 152 |
|
| 153 |
(* postcondition *) |
| 154 |
use_sep_assumption. |
| 155 |
cancel. |
| 156 |
cancel_seps_at_indices 0%nat 0%nat; [|cancel]. |
| 157 |
f_equal. |
| 158 |
f_equal. |
| 159 |
subst x2 b. |
| 160 |
cbv [coord.to_bytes F.div]. |
| 161 |
progress Morphisms.f_equiv; []. |
| 162 |
progress Morphisms.f_equiv; []. |
| 163 |
transitivity (x * coord.R * F.inv (1 + 1))%F; [|ring]. |
| 164 |
set (x*coord.R)%F as xR; set (F.inv (1 + 1)) as i2. |
| 165 |
|
| 166 |
(* NOTE: back-and-forth rewrite between Z.modulo and Z.odd *) |
| 167 |
rewrite word.unsigned_of_Z; cbv [word.wrap]; rewrite Zmod_mod, Zdiv.Zodd_mod, Z.mod_mod_divide by (apply Divide.Z.divide_pow_le with (n:=1); lia). |
| 168 |
symmetry; rewrite <-(F.of_Z_to_Z xR) at 1; rewrite (Z.div_mod xR 2) at 1 by lia. |
| 169 |
rewrite Div.Z.div_sub_mod_exact, Zdiv.Zmod_odd by lia. |
| 170 |
|
| 171 |
assert (F.of_Z p256 2 * i2 = F.one)%F as Hi2 by (cbv [i2]; clear; Decidable.vm_decide). |
| 172 |
case Z.odd; cbn [Z.eqb Pos.eqb Zeq_bool Z.compare Pos.compare Pos.compare_cont]; rewrite ?F.of_Z_add, ?F.of_Z_mul; fold (@F.zero p256); fold (@F.one p256); try ring [Hi2]. |
| 173 |
Qed. |
| 174 |
|
| 175 |
Definition p256_point_add_nz_nz_neq := func! (p_out, p_P, p_Q) ~> ok { |
| 176 |
stackalloc 32 as z1z1; |
| 177 |
stackalloc 32 as z2z2; |
| 178 |
stackalloc 32 as u1; |
| 179 |
stackalloc 32 as u2; |
| 180 |
stackalloc 32 as h; |
| 181 |
stackalloc 32 as s1; |
| 182 |
stackalloc 32 as s2; |
| 183 |
stackalloc 32 as r; |
| 184 |
stackalloc 32 as Hsqr; |
| 185 |
stackalloc 32 as Hcub; |
| 186 |
p256_coord_sqr(z1z1, p_P.+$32.+$32); |
| 187 |
p256_coord_mul(u2, p_Q, z1z1); |
| 188 |
p256_coord_sqr(z2z2, p_Q.+$32.+$32); |
| 189 |
p256_coord_mul(u1, p_P, z2z2); |
| 190 |
p256_coord_sub(h, u2, u1); |
| 191 |
p256_coord_mul(s2, p_P.+$32.+$32, z1z1); |
| 192 |
p256_coord_mul(p_out.+$32.+$32, h, p_P.+$32.+$32); |
| 193 |
p256_coord_mul(p_out.+$32.+$32, p_out.+$32.+$32, p_Q.+$32.+$32); |
| 194 |
p256_coord_mul(s2, s2, p_Q.+$32); |
| 195 |
p256_coord_mul(s1, p_Q.+$32.+$32, z2z2); |
| 196 |
p256_coord_mul(s1, s1, p_P.+$32); |
| 197 |
p256_coord_sub(r, s2, s1); |
| 198 |
p256_coord_sqr(Hsqr, h); |
| 199 |
p256_coord_sqr(p_out, r); |
| 200 |
p256_coord_mul(Hcub, Hsqr, h); |
| 201 |
p256_coord_mul(u2, u1, Hsqr); |
| 202 |
|
| 203 |
unpack! different_x = p256_coord_nonzero(Hcub); |
| 204 |
unpack! different_y = p256_coord_nonzero(p_out); |
| 205 |
unpack! ok = br_value_barrier(different_x | different_y); |
| 206 |
|
| 207 |
p256_coord_sub(p_out, p_out, Hcub); |
| 208 |
p256_coord_sub(p_out, p_out, u2); |
| 209 |
p256_coord_sub(p_out, p_out, u2); |
| 210 |
p256_coord_sub(h, u2, p_out); |
| 211 |
p256_coord_mul(s2, Hcub, s1); |
| 212 |
p256_coord_mul(h, h, r); |
| 213 |
p256_coord_sub(p_out.+$32, h, s2) |
| 214 |
}. |
| 215 |
|
| 216 |
Lemma p256_point_add_nz_nz_neq_ok : program_logic_goal_for_function! p256_point_add_nz_nz_neq. |
| 217 |
Proof. |
| 218 |
cbv [spec_of_p256_point_add_nz_nz_neq]. |
| 219 |
straightline; repeat straightline_cleanup. |
| 220 |
destruct P as (((x1 & y1) & z1) & p1), Q as (((x2 & y2) & z2) & p2); |
| 221 |
cbv [proj1_sig proj2_sig fst snd point.to_bytes] in * |-. |
| 222 |
repeat seprewrite_in_by Array.sep_eq_of_list_word_at_app H25 |
| 223 |
ltac:(rewrite ?app_length, ?length_coord; trivial; try Lia.lia). |
| 224 |
|
| 225 |
repeat straightline. |
| 226 |
repeat seprewrite_in_by Array.array1_iff_eq_of_list_word_at H25 ltac:(Lia.lia). |
| 227 |
|
| 228 |
rewrite <-(firstn_skipn 32 out) in H25. |
| 229 |
rewrite <-(firstn_skipn 32 (skipn _ out)) in H25. |
| 230 |
rewrite ?skipn_skipn in H25. |
| 231 |
rewrite ?app_length, ?length_coord in *. |
| 232 |
repeat seprewrite_in_by Array.sep_eq_of_list_word_at_app H25 (* WHY does first rewrite take 0.1s? *) |
| 233 |
ltac:(repeat rewrite ?app_length, ?firstn_length, ?skipn_length, ?Nat.min_l; try Lia.lia; trivial). |
| 234 |
|
| 235 |
progress change (Z.of_nat 32) with 32 in *. |
| 236 |
|
| 237 |
(* WHY does skipn get unfolded below without this? |
| 238 |
*) repeat (set (List.skipn _ _) in H25 || set (List.firstn _ _) in H25). |
| 239 |
|
| 240 |
repeat (straightline_call; ssplit; |
| 241 |
[ solve [repeat match goal with |
| 242 |
| |- True => exact I |
| 243 |
| |- exists _, _ => letexists |
| 244 |
| |- _ => subst l10 l11 l12; progress rewrite ?length_coord, ?firstn_length, ?skipn_length; Lia.lia |
| 245 |
| _ => ecancel_assumption |
| 246 |
end] .. |
| 247 |
| repeat straightline ]). |
| 248 |
|
| 249 |
(* stackdealloc *) |
| 250 |
progress repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) H93 ltac:(rewrite ?length_coord; Lia.lia). |
| 251 |
progress repeat match type of H93 with context [Array.array ptsto _ _ (coord.to_bytes ?x)] => |
| 252 |
unique pose proof (length_coord x) end. |
| 253 |
repeat straightline. |
| 254 |
progress repeat seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H93 ltac:(rewrite ?length_coord; Lia.lia). |
| 255 |
|
| 256 |
(* postcondition *) |
| 257 |
eexists; ssplit. |
| 258 |
{ |
| 259 |
cbv [proj1_sig proj2_sig fst snd point.to_bytes]. |
| 260 |
repeat seprewrite_in_by Array.list_word_at_app_of_adjacent_eq H93 ltac:(rewrite ?length_coord; listZnWords). |
| 261 |
(* other direction: repeat seprewrite_by Array.sep_eq_of_list_word_at_app ltac:(rewrite ?length_coord, ?app_length; trivial; try Lia.lia). *) |
| 262 |
ecancel_assumption. } |
| 263 |
{ rewrite ?app_length, ?length_point, ?length_coord; trivial. } |
| 264 |
|
| 265 |
case (Properties.word.eqb_spec x3 $0); subst x3; rewrite word.lor_0_iff; [right|left]; split; trivial. |
| 266 |
{ case H121 as [Hx Hy]. |
| 267 |
subst x x0. |
| 268 |
rewrite !word.broadcast_0_iff in *. |
| 269 |
rewrite !Bool.negb_false_iff, !F.eqb_eq in *. |
| 270 |
cbv [fst snd Jacobian.eq Jacobian.iszero proj1_sig] in *. |
| 271 |
case Decidable.dec; intros; try contradiction; split; trivial. |
| 272 |
rewrite Hierarchy.commutative in Hx. |
| 273 |
rewrite <-!F.pow_succ_r in Hx, Hy; simpl N.succ in Hx, Hy. |
| 274 |
cbv [coord] in Hx, Hy; rewrite F.pow_0_iff, Ring.sub_zero_iff in Hx, Hy by (lia||exact _). |
| 275 |
rewrite ?F.pow_3_r, ?F.pow_2_r in Hx. |
| 276 |
rewrite ?F.pow_3_r, ?F.pow_2_r in Hy. |
| 277 |
split; Field.fsatz. } |
| 278 |
{ unshelve eexists ?[pfPneqQ]. |
| 279 |
{ intros HX; cbv [Jacobian.eq Jacobian.iszero proj1_sig fst snd] in H122, H123, HX. |
| 280 |
destruct Decidable.dec in HX; try contradiction; case HX as (Hz&Hx&Hy). |
| 281 |
apply H121. subst x x0. |
| 282 |
rewrite !word.broadcast_0_iff in *. |
| 283 |
rewrite !Bool.negb_false_iff, !F.eqb_eq. |
| 284 |
rewrite ?F.pow_3_r, ?F.pow_2_r, ?Hx, ?Hy, ?(proj2 (Ring.sub_zero_iff _ _)); ssplit; Field.fsatz. } |
| 285 |
cbv [Jacobian.add_inequal_nz_nz point.to_bytes]; cbn [fst snd proj1_sig]. |
| 286 |
rewrite ?F.pow_3_r, ?F.pow_2_r. |
| 287 |
trivial. } |
| 288 |
Qed. |
| 289 |
|
| 290 |
Import coqutil.Tactics.Tactics. |
| 291 |
|
| 292 |
Definition p256_point_add_vartime_if_doubling := func!(p_out, p_P, p_Q) { |
| 293 |
unpack! zeroP = p256_point_iszero(p_P); |
| 294 |
unpack! zeroQ = p256_point_iszero(p_Q); |
| 295 |
stackalloc (3*32) as p_tmp; |
| 296 |
unpack! ok = p256_point_add_nz_nz_neq(p_tmp, p_P, p_Q); |
| 297 |
unpack! ok = br_declassify(zeroP | zeroQ | ok); |
| 298 |
stackalloc (3*32) as p_sel; |
| 299 |
br_memset(p_sel, $0, $(3*32)); |
| 300 |
br_memcxor(p_sel, p_tmp, $(3*32), ~zeroP & ~zeroQ); |
| 301 |
br_memcxor(p_sel, p_P, $(3*32), ~zeroP & zeroQ); |
| 302 |
br_memcxor(p_sel, p_Q, $(3*32), zeroP & ~zeroQ); |
| 303 |
if !ok { p256_point_double(p_sel, p_P) }; |
| 304 |
br_memcpy(p_out, p_sel, $(3*32)) |
| 305 |
}. |
| 306 |
|
| 307 |
Import memcpy. |
| 308 |
Lemma p256_point_add_vartime_if_doubling_ok : program_logic_goal_for_function! p256_point_add_vartime_if_doubling. |
| 309 |
Proof. |
| 310 |
cbv [spec_of_p256_point_add_vartime_if_doubling]. |
| 311 |
repeat straightline. |
| 312 |
straightline_call; repeat straightline. (*iszero*) |
| 313 |
{ letexists. ecancel_assumption. } |
| 314 |
straightline_call; repeat straightline. (*iszero*) |
| 315 |
{ letexists. ecancel_assumption. } |
| 316 |
(* stackalloc *) |
| 317 |
seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H9 ltac:(lia). |
| 318 |
straightline_call; ssplit. (*add*) |
| 319 |
{ ecancel_assumption. } |
| 320 |
{ rewrite length_point; lia. } |
| 321 |
repeat straightline. |
| 322 |
straightline_call; repeat straightline (* br_declassify *). |
| 323 |
(* stackalloc *) |
| 324 |
seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H17 ltac:(lia). |
| 325 |
straightline_call; ssplit. (* memset *) |
| 326 |
{ ecancel_assumption. } |
| 327 |
{ ZnWords.ZnWords. } |
| 328 |
repeat straightline. |
| 329 |
straightline_call; repeat straightline; ssplit (* memcxor *). |
| 330 |
{ ecancel_assumption. } |
| 331 |
{ rewrite ?repeat_length; trivial. } |
| 332 |
{ rewrite H18, length_point; trivial. } |
| 333 |
straightline_call; repeat straightline; ssplit (* memcxor *). |
| 334 |
{ ecancel_assumption. } |
| 335 |
{ rewrite ?repeat_length; trivial. } |
| 336 |
{ rewrite length_point; trivial. } |
| 337 |
straightline_call; repeat straightline; ssplit (* memcxor *). |
| 338 |
{ ecancel_assumption. } |
| 339 |
{ rewrite ?repeat_length; trivial. } |
| 340 |
{ rewrite length_point; trivial. } |
| 341 |
|
| 342 |
rewrite ?word.and_xorm1_l, ?word.and_xorm1_r in *. |
| 343 |
|
| 344 |
subst x x0 x3. |
| 345 |
letexists; ssplit; repeat straightline; subst v (* if ok *). |
| 346 |
{ straightline_call; repeat straightline; ssplit (* memcpy *). |
| 347 |
{ ecancel_assumption. } |
| 348 |
{ rewrite H10, length_point; trivial. } |
| 349 |
{ trivial. } |
| 350 |
{ clear; ZnWords.ZnWords. } |
| 351 |
repeat straightline. |
| 352 |
(* stackdealloc *) |
| 353 |
progress repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) H42 ltac:(rewrite ?length_point in *; lia || ZnWords.ZnWords). |
| 354 |
progress repeat match type of H42 with context [Array.array ptsto _ _ (point.to_bytes ?x)] => |
| 355 |
unique pose proof (length_point x) end. |
| 356 |
assert (Datatypes.length x6 = 96%nat) by ZnWords.ZnWords. |
| 357 |
repeat straightline. |
| 358 |
progress repeat seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H42 ltac:(rewrite ?length_point in *; lia || ZnWords.ZnWords). |
| 359 |
|
| 360 |
rewrite <-word.unsigned_of_Z_0, !word.unsigned_inj_iff in H27 by exact _. |
| 361 |
rewrite !word.lor_0_iff, !word.broadcast_0_iff in H27. |
| 362 |
destruct (iszero P) eqn:HP, (iszero Q) eqn:HQ in *; try intuition discriminate; |
| 363 |
repeat match goal with |
| 364 |
| H : _ = _ -> _ |- _ => specialize (H eq_refl) |
| 365 |
| H : ?x = ?y -> _ |- _ => assert (x = y -> False) as _ by inversion 1; clear H |
| 366 |
end; |
| 367 |
subst x4; subst x5; subst x6; |
| 368 |
rewrite ?Byte.map_xor_0_l in * by (rewrite ?length_point; ZnWords.ZnWords). |
| 369 |
{ (* 0 + 0 *) |
| 370 |
eexists (exist _ (0,0,0)%F I); split. |
| 371 |
{ use_sep_assumption; cancel. reflexivity. } |
| 372 |
apply Decidable.dec_bool, Jacobian.iszero_iff in HP. |
| 373 |
apply Decidable.dec_bool, Jacobian.iszero_iff in HQ. |
| 374 |
rewrite Jacobian.eq_iff, Jacobian.to_affine_add, HP, HQ. |
| 375 |
exact I. } |
| 376 |
{ (* 0 + Q *) |
| 377 |
eexists; split. { ecancel_assumption. } |
| 378 |
apply Decidable.dec_bool, Jacobian.iszero_iff in HP. |
| 379 |
rewrite Jacobian.eq_iff, Jacobian.to_affine_add, HP. |
| 380 |
Import Curves.Weierstrass.AffineProofs. |
| 381 |
symmetry. |
| 382 |
unshelve eapply Hierarchy.left_identity. |
| 383 |
unshelve eapply Hierarchy.monoid_is_left_identity. |
| 384 |
unshelve eapply Hierarchy.group_monoid; shelve_unifiable. |
| 385 |
unshelve eapply Hierarchy.commutative_group_group. |
| 386 |
unshelve eapply W.commutative_group; try exact _. |
| 387 |
cbv [id]. abstract Decidable.vm_decide. } |
| 388 |
{ (* P + 0 *) |
| 389 |
eexists; split. { ecancel_assumption. } |
| 390 |
apply Decidable.dec_bool, Jacobian.iszero_iff in HQ. |
| 391 |
rewrite Jacobian.eq_iff, Jacobian.to_affine_add, HQ. |
| 392 |
symmetry. |
| 393 |
unshelve eapply Hierarchy.right_identity. |
| 394 |
unshelve eapply Hierarchy.monoid_is_right_identity. |
| 395 |
unshelve eapply Hierarchy.group_monoid; shelve_unifiable. |
| 396 |
unshelve eapply Hierarchy.commutative_group_group. |
| 397 |
unshelve eapply W.commutative_group; try exact _. |
| 398 |
cbv [id]. abstract Decidable.vm_decide. } |
| 399 |
{ (* nz + nz' *) |
| 400 |
rewrite <-Bool.not_true_iff_false in HP, HQ. |
| 401 |
(* Decidable.dec_iff? *) |
| 402 |
cbv [iszero] in HP, HQ; case Decidable.dec in HP; case Decidable.dec in HQ; try congruence. |
| 403 |
destruct (H19 ltac:(trivial) ltac:(trivial)) as [HE|]; [|intuition fail]. |
| 404 |
case HE as [_ (?&HE)]. |
| 405 |
repeat straightline_cleanup. |
| 406 |
eexists; split; [ecancel_assumption|]. |
| 407 |
rewrite Jacobian.eq_iff, Jacobian.to_affine_add, Jacobian.to_affine_add_inequal_nz_nz; trivial; reflexivity. } } |
| 408 |
{ (* if !ok *) |
| 409 |
rewrite <-word.unsigned_of_Z_0, !word.unsigned_inj_iff in H27 by exact _. |
| 410 |
rewrite !word.lor_0_iff, !word.broadcast_0_iff in H27. |
| 411 |
case H27 as ((HP&HQ)&->); rewrite ?HP, ?HQ in *; |
| 412 |
repeat match goal with |
| 413 |
| H : _ = _ -> _ |- _ => specialize (H eq_refl) |
| 414 |
| H : ?x = ?y -> _ |- _ => assert (x = y -> False) as _ by inversion 1; clear H |
| 415 |
end; |
| 416 |
subst x4; subst x5; subst x6; |
| 417 |
rewrite ?Byte.map_xor_0_l in * by (rewrite ?length_point; ZnWords.ZnWords). |
| 418 |
rewrite <-Bool.not_true_iff_false in HP, HQ. |
| 419 |
cbv [iszero] in HP, HQ; case Decidable.dec in HP; case Decidable.dec in HQ; try congruence. |
| 420 |
case (H19 ltac:(trivial) ltac:(trivial)) as [[HE ?]|[? HE]]; [case (HE eq_refl)|]. |
| 421 |
|
| 422 |
straightline_call; repeat straightline. |
| 423 |
{ split. { ecancel_assumption. } |
| 424 |
rewrite ?map_length, ?combine_length, ?repeat_length. |
| 425 |
rewrite H18, length_point. clear; ZnWords.ZnWords. } |
| 426 |
|
| 427 |
straightline_call; repeat straightline; ssplit (* memcpy *). |
| 428 |
{ ecancel_assumption. } |
| 429 |
{ rewrite H10, length_point; trivial. } |
| 430 |
{ trivial. } |
| 431 |
{ clear; ZnWords.ZnWords. } |
| 432 |
repeat straightline. |
| 433 |
(* stackdealloc *) |
| 434 |
progress repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) H31 ltac:(rewrite ?length_point in *; lia || ZnWords.ZnWords). |
| 435 |
progress repeat match type of H31 with context [Array.array ptsto _ _ (point.to_bytes ?x)] => |
| 436 |
unique pose proof (length_point x) end. |
| 437 |
repeat straightline. |
| 438 |
progress repeat seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H31 ltac:(rewrite ?length_point in *; lia || ZnWords.ZnWords). |
| 439 |
|
| 440 |
eexists; ssplit. { ecancel_assumption. } |
| 441 |
rewrite <-HE, <-Jacobian.double_minus_3_eq_double. |
| 442 |
rewrite Jacobian.eq_iff, Jacobian.to_affine_double, Jacobian.to_affine_add. |
| 443 |
reflexivity. } |
| 444 |
Qed. |
| 445 |
|
| 446 |
Definition p256_point_double := func!(out, in1) { |
| 447 |
stackalloc 32 as D; |
| 448 |
stackalloc 32 as A; |
| 449 |
stackalloc 32 as tmp; |
| 450 |
p256_coord_add(D, in1.+$32, in1.+$32); |
| 451 |
p256_coord_sqr(tmp, in1.+$32.+$32); |
| 452 |
p256_coord_sqr(D, D); |
| 453 |
p256_coord_mul(out.+$32.+$32, in1.+$32.+$32, in1.+$32); |
| 454 |
p256_coord_add(out.+$32.+$32, out.+$32.+$32, out.+$32.+$32); |
| 455 |
p256_coord_add(A, in1, tmp); |
| 456 |
p256_coord_sub(tmp, in1, tmp); |
| 457 |
{ stackalloc 32 as t2; p256_coord_add(t2, tmp, tmp); p256_coord_add(tmp, t2, tmp) }; |
| 458 |
p256_coord_sqr(out.+$32, D); |
| 459 |
p256_coord_mul(A, A, tmp); |
| 460 |
p256_coord_mul(D, D, in1); |
| 461 |
p256_coord_sqr(out, A); |
| 462 |
p256_coord_add(tmp, D, D); |
| 463 |
p256_coord_sub(out, out, tmp); |
| 464 |
p256_coord_sub(D, D, out); |
| 465 |
p256_coord_mul(D, D, A); |
| 466 |
p256_coord_halve(out.+$32, out.+$32); |
| 467 |
p256_coord_sub(out.+$32, D, out.+$32) |
| 468 |
}. |
| 469 |
|
| 470 |
Lemma p256_point_double_ok : program_logic_goal_for_function! p256_point_double. |
| 471 |
Proof. |
| 472 |
cbv [spec_of_p256_point_double]. |
| 473 |
straightline; repeat straightline_cleanup. |
| 474 |
destruct P as (((x1 & y1) & z1) & p1); |
| 475 |
cbv [proj1_sig proj2_sig fst snd point.to_bytes] in * |-. |
| 476 |
progress repeat seprewrite_in_by Array.sep_eq_of_list_word_at_app H18 |
| 477 |
ltac:(rewrite ?app_length, ?length_coord; trivial; try Lia.lia). |
| 478 |
|
| 479 |
repeat straightline. |
| 480 |
repeat seprewrite_in_by Array.array1_iff_eq_of_list_word_at H18 ltac:(Lia.lia). |
| 481 |
|
| 482 |
rewrite <-(firstn_skipn 32 out) in H18. |
| 483 |
rewrite <-(firstn_skipn 32 (skipn _ out)) in H18. |
| 484 |
rewrite !skipn_skipn in H18. |
| 485 |
rewrite ?app_length, ?length_coord in *. |
| 486 |
progress repeat seprewrite_in_by Array.sep_eq_of_list_word_at_app H18 |
| 487 |
ltac:(repeat rewrite ?app_length, ?firstn_length, ?skipn_length, ?Nat.min_l; try Lia.lia; trivial). |
| 488 |
|
| 489 |
progress change (Z.of_nat 32) with 32 in *. |
| 490 |
|
| 491 |
progress repeat (set (List.skipn _ _) in H18 || set (List.firstn _ _) in H18). |
| 492 |
|
| 493 |
progress repeat (straightline_call; ssplit; |
| 494 |
[ solve [repeat match goal with |
| 495 |
| |- True => exact I |
| 496 |
| |- exists _, _ => letexists |
| 497 |
| |- _ => |
| 498 |
repeat match goal with x := _ : list _ |- _ => subst x end; |
| 499 |
progress rewrite ?length_coord, ?firstn_length, ?skipn_length; lia |
| 500 |
| _ => ecancel_assumption |
| 501 |
end] .. |
| 502 |
| repeat straightline ]). |
| 503 |
|
| 504 |
(* stackalloc *) |
| 505 |
repeat seprewrite_in_by Array.array1_iff_eq_of_list_word_at H42 ltac:(Lia.lia). |
| 506 |
|
| 507 |
progress repeat (straightline_call; ssplit; |
| 508 |
[ solve [repeat match goal with |
| 509 |
| |- True => exact I |
| 510 |
| |- exists _, _ => letexists |
| 511 |
| |- _ => |
| 512 |
repeat match goal with x := _ : list _ |- _ => subst x end; |
| 513 |
progress rewrite ?length_coord, ?firstn_length, ?skipn_length; lia |
| 514 |
| _ => ecancel_assumption |
| 515 |
end] .. |
| 516 |
| repeat straightline ]). |
| 517 |
|
| 518 |
(* stackdealloc *) |
| 519 |
progress repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) H50 ltac:(rewrite ?length_coord; Lia.lia). |
| 520 |
progress repeat match type of H50 with context [Array.array ptsto _ _ (coord.to_bytes ?x)] => |
| 521 |
unique pose proof (length_coord x) end. |
| 522 |
repeat straightline. |
| 523 |
progress repeat seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H50 ltac:(rewrite ?length_coord; Lia.lia). |
| 524 |
|
| 525 |
progress repeat (straightline_call; ssplit; |
| 526 |
[ solve [repeat match goal with |
| 527 |
| |- True => exact I |
| 528 |
| |- exists _, _ => letexists |
| 529 |
| |- _ => |
| 530 |
repeat match goal with x := _ : list _ |- _ => subst x end; |
| 531 |
progress rewrite ?length_coord, ?firstn_length, ?skipn_length; lia |
| 532 |
| _ => ecancel_assumption |
| 533 |
end] .. |
| 534 |
| repeat straightline ]). |
| 535 |
|
| 536 |
(* stackdealloc *) |
| 537 |
progress repeat seprewrite_in_by (symmetry! @Array.array1_iff_eq_of_list_word_at) H69 ltac:(rewrite ?length_coord; Lia.lia). |
| 538 |
progress repeat match type of H69 with context [Array.array ptsto _ _ (coord.to_bytes ?x)] => |
| 539 |
unique pose proof (length_coord x) end. |
| 540 |
repeat straightline. |
| 541 |
progress repeat seprewrite_in_by (@Array.array1_iff_eq_of_list_word_at) H69 ltac:(rewrite ?length_coord; Lia.lia). |
| 542 |
|
| 543 |
(* postcondition *) |
| 544 |
|
| 545 |
cbv [proj1_sig proj2_sig fst snd point.to_bytes Jacobian.double_minus_3 Jacobian.double_minus3_impl Jacobian.Fsquare Jacobian.Ftriple Jacobian.Fhalve ]. |
| 546 |
progress repeat seprewrite_in_by Array.list_word_at_app_of_adjacent_eq H69 ltac:(rewrite ?length_coord; listZnWords). |
| 547 |
rewrite ?F.pow_3_r, ?F.pow_2_r in H69. |
| 548 |
ecancel_assumption. |
| 549 |
Qed. |
| 550 |
|
| 551 |
Import BinInt. Local Open Scope Z_scope. |
| 552 |
|
| 553 |
Definition fe_set_1 := func! (o) { |
| 554 |
o0 = $1; o1 = $0xffffffff00000000; o2 = -$1; o3 = $0xfffffffe; |
| 555 |
store(o, o0); store(o+$8, o1); store(o+$16, o2); store(o+$24, o3) |
| 556 |
}. |
| 557 |
|
| 558 |
Definition p256_point_add_affine_nz_nz_neq := func! (out, in1, in2) ~> ok { |
| 559 |
stackalloc 32 as z1z1; |
| 560 |
stackalloc 32 as u2; |
| 561 |
stackalloc 32 as h; |
| 562 |
stackalloc 32 as s2; |
| 563 |
stackalloc 32 as r; |
| 564 |
stackalloc 32 as Hsqr; |
| 565 |
stackalloc 32 as Hcub; |
| 566 |
|
| 567 |
p256_coord_sqr(z1z1, in1.+$32.+$32); |
| 568 |
p256_coord_mul(u2, in2, z1z1); |
| 569 |
p256_coord_sub(h, u2, in1); |
| 570 |
p256_coord_mul(s2, in1.+$32.+$32, z1z1); |
| 571 |
p256_coord_mul(out.+$32.+$32, h, in1.+$32.+$32); |
| 572 |
p256_coord_mul(s2, s2, in2.+$32); |
| 573 |
p256_coord_sub(r, s2, in1.+$32); |
| 574 |
p256_coord_sqr(Hsqr, h); |
| 575 |
p256_coord_sqr(out, r); |
| 576 |
p256_coord_mul(Hcub, Hsqr, h); |
| 577 |
p256_coord_mul(u2, in1, Hsqr); |
| 578 |
|
| 579 |
unpack! different_x = p256_coord_nonzero(Hcub); |
| 580 |
unpack! different_y = p256_coord_nonzero(out); |
| 581 |
unpack! ok = br_value_barrier(different_x | different_y); |
| 582 |
|
| 583 |
p256_coord_sub(out, out, Hcub); |
| 584 |
p256_coord_sub(out, out, u2); |
| 585 |
p256_coord_sub(out, out, u2); |
| 586 |
p256_coord_sub(h, u2, out); |
| 587 |
p256_coord_mul(s2, Hcub, in1.+$32); |
| 588 |
p256_coord_mul(h, h, r); |
| 589 |
p256_coord_sub(out.+$32, h, s2) |
| 590 |
}. |
| 591 |
|
| 592 |
Definition p256_point_add_affine_conditional := func! (out, in1, in2, c) { |
| 593 |
unpack! p1zero = p256_point_iszero(in1.+$32.+$32); |
| 594 |
unpack! p2zero = constant_time_is_zero_w(c); |
| 595 |
stackalloc (3*32) as p_out; |
| 596 |
unpack! ok = p256_point_add_affine_nz_nz_neq(p_out, in1, in2); |
| 597 |
unpack! ok = br_declassify(p1zero | p2zero | ok); |
| 598 |
stackalloc (3*32) as t; |
| 599 |
br_memset(t, $0, $3*$32); |
| 600 |
memcxor(t, p_out, $3*$32, ~p1zero & ~p2zero); |
| 601 |
memcxor(t, in1, $3*$32, ~p1zero & p2zero); |
| 602 |
memcxor(t, in2, $3*$32, p1zero & ~p2zero); |
| 603 |
if !ok { p256_point_double(t, in1) }; |
| 604 |
br_memcpy(out, t, $(3*32)) |
| 605 |
}. |
| 606 |
|
| 607 |
Definition sc_halve := func!(y, x) { |
| 608 |
unpack! m = br_value_barrier(-(load(x)&$1)); (* is x odd? *) |
| 609 |
mh0 = $0x79dce5617e3192a8; mh1 = $0xde737d56d38bcf42; mh2 = $0x7fffffffffffffff; mh3 = $0x7fffffff80000000; (* minus one half modulo l *) |
| 610 |
stackalloc 32 as mmh; (* maybe minus half *) |
| 611 |
store(mmh, m&mh0); store(mmh+$8, m&mh1); store(mmh+$16, m&mh2); store(mmh+$24, m&mh3); |
| 612 |
y0 = load(y); y1 = load(y+$8); y2 = load(y+$16); y3 = load(y+$24); |
| 613 |
unpack! y0 = shrd_64(y0, y1, $1); |
| 614 |
unpack! y1 = shrd_64(y1, y2, $1); |
| 615 |
unpack! y2 = shrd_64(y2, y3, $1); |
| 616 |
y3 = y3 >> $1; |
| 617 |
store(y, y0); store(y+$8, y1); store(y+$16, y2); store(y+$24, y3); |
| 618 |
sc_sub(y, y, mmh) |
| 619 |
}. |
| 620 |
|
| 621 |
Definition sc_sub := func!(out, x, y) { |
| 622 |
unpack! x1, x2 = sbb64($0, load(x), load(y)); |
| 623 |
unpack! x3, x4 = sbb64(x2, load(x+$8), load(y+$8)); |
| 624 |
unpack! x5, x6 = sbb64(x4, load(x+$16), load(y+$16)); |
| 625 |
unpack! x7, x8 = sbb64(x6, load(x+$24), load(y+$24)); |
| 626 |
x9 = -x8; |
| 627 |
unpack! x10, x11 = adc64($0, x1, x9 & $0xf3b9cac2fc632551); |
| 628 |
unpack! x12, x13 = adc64(x11, x3, x9 & $0xbce6faada7179e84); |
| 629 |
unpack! x14, x15 = adc64(x13, x5, x9); |
| 630 |
unpack! x16, x17 = adc64(x15, x7, x9 << $32); |
| 631 |
x17 = x17+$0; |
| 632 |
store(out, x10); |
| 633 |
store(out+$8, x12); |
| 634 |
store(out+$16, x14); |
| 635 |
store(out+$24, x16) |
| 636 |
}. |