| 1 |
Require Crypto.Spec.Curve25519. |
| 2 |
Require Import Rupicola.Lib.Api. |
| 3 |
Require Import Rupicola.Lib.Loops. |
| 4 |
Require Import Rupicola.Lib.ControlFlow.DownTo. |
| 5 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 6 |
Require Import Crypto.Arithmetic.FLia. |
| 7 |
Require Import Crypto.Bedrock.Specs.Field. |
| 8 |
Require Import Crypto.Bedrock.Field.Interface.Compilation2. |
| 9 |
Require Import Crypto.Algebra.Hierarchy. |
| 10 |
From Coq Require Import DecimalString. |
| 11 |
|
| 12 |
Local Open Scope Z_scope. |
| 13 |
|
| 14 |
Section Utils. |
| 15 |
|
| 16 |
Lemma Nat_iter_plus_one : |
| 17 |
forall A f k (x : A), Nat.iter k f (f x) = Nat.iter (S k) f x. |
| 18 |
Proof. |
| 19 |
intros. |
| 20 |
induction k. |
| 21 |
- reflexivity. |
| 22 |
- simpl. |
| 23 |
rewrite IHk. |
| 24 |
f_equal. |
| 25 |
Qed. |
| 26 |
|
| 27 |
End Utils. |
| 28 |
|
| 29 |
Section FElems. |
| 30 |
|
| 31 |
Context {width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte}. |
| 32 |
Context {locals: map.map String.string word}. |
| 33 |
Context {ext_spec: bedrock2.Semantics.ExtSpec}. |
| 34 |
Context {word_ok : word.ok word} {mem_ok : map.ok mem}. |
| 35 |
Context {locals_ok : map.ok locals}. |
| 36 |
Context {ext_spec_ok : Semantics.ext_spec.ok ext_spec}. |
| 37 |
|
| 38 |
Section Impl. |
| 39 |
Context (m : positive). |
| 40 |
Fixpoint exp_by_squaring (x : F m) (n : positive) : F m := |
| 41 |
match n with |
| 42 |
| 1 => x |
| 43 |
| n'~0 => let/n res := exp_by_squaring x n' in |
| 44 |
let/n res := F.pow res 2 in |
| 45 |
res |
| 46 |
| n'~1 => let/n res := exp_by_squaring x n' in |
| 47 |
let/n res := F.pow res 2 in |
| 48 |
let/n res := F.mul x res in |
| 49 |
res |
| 50 |
end%positive. |
| 51 |
|
| 52 |
Fixpoint run_length_encoding (n : positive) : list (bool * nat) := |
| 53 |
match n with |
| 54 |
| xH => [(true, 0)] |
| 55 |
| xO n' => |
| 56 |
match (run_length_encoding n') with |
| 57 |
| (false, k) :: t => (false, k + 1) :: t |
| 58 |
| t => (false, 0) :: t |
| 59 |
end |
| 60 |
| xI n' => |
| 61 |
match (run_length_encoding n') with |
| 62 |
| (true, k) :: t => (true, k + 1) :: t |
| 63 |
| t => (true, 0) :: t |
| 64 |
end |
| 65 |
end%nat. |
| 66 |
|
| 67 |
Definition exp_square_and_multiply (x : F m) (x' : F m) := |
| 68 |
let/n res := F.pow x' 2 in |
| 69 |
let/n res := F.mul res x in |
| 70 |
res. |
| 71 |
|
| 72 |
Definition exp_square (x' : F m) := |
| 73 |
let/n res := F.pow x' 2 in |
| 74 |
res. |
| 75 |
|
| 76 |
Fixpoint exp_from_encoding_simple (x : F m) (n : list (bool * nat)) : F m := |
| 77 |
match n with |
| 78 |
| [] => |
| 79 |
1 |
| 80 |
(* can add more cases for small k to be faster *) |
| 81 |
| (true, 0%nat) :: t => |
| 82 |
let/n res := exp_from_encoding_simple x t in |
| 83 |
let/n res := F.pow res 2 in |
| 84 |
let/n res := F.mul x res in |
| 85 |
res |
| 86 |
| (true, k) :: t => |
| 87 |
let/n res := exp_from_encoding_simple x t in |
| 88 |
let/n res := Nat.iter (S k) (exp_square_and_multiply x) res in |
| 89 |
res |
| 90 |
| (false, 0%nat) :: t => |
| 91 |
let/n res := exp_from_encoding_simple x t in |
| 92 |
let/n res := F.pow res 2 in |
| 93 |
res |
| 94 |
| (false, k) :: t => |
| 95 |
let/n res := exp_from_encoding_simple x t in |
| 96 |
let/n res := Nat.iter (S k) (exp_square) res in |
| 97 |
res |
| 98 |
end. |
| 99 |
|
| 100 |
Fixpoint exp_from_encoding (x : F m) (n : list (bool * nat)) : F m := |
| 101 |
match n with |
| 102 |
| [] => |
| 103 |
1 |
| 104 |
| [(true, 0%nat)] => |
| 105 |
x |
| 106 |
| [(true, 1%nat)] => |
| 107 |
let/n res := F.pow x 2 in |
| 108 |
let/n res := F.mul x res in |
| 109 |
res |
| 110 |
| [(true, (S k))] => |
| 111 |
let/n res := F.pow x 2 in |
| 112 |
let/n res := F.mul x res in |
| 113 |
let/n res := Nat.iter k (exp_square_and_multiply x) res in |
| 114 |
res |
| 115 |
| [(false, 0%nat); (true, 0%nat)] => |
| 116 |
let/n res := F.pow x 2 in |
| 117 |
res |
| 118 |
| [(false, k); (true, 0%nat)] => |
| 119 |
let/n res := F.pow x 2 in |
| 120 |
let/n res := Nat.iter k (exp_square) res in |
| 121 |
res |
| 122 |
| (true, 0%nat) :: t => |
| 123 |
let/n res := exp_from_encoding x t in |
| 124 |
let/n res := F.pow res 2 in |
| 125 |
let/n res := F.mul x res in |
| 126 |
res |
| 127 |
| (true, k) :: t => |
| 128 |
let/n res := exp_from_encoding x t in |
| 129 |
let/n res := Nat.iter (S k) (exp_square_and_multiply x) res in |
| 130 |
res |
| 131 |
| (false, 0%nat) :: t => |
| 132 |
let/n res := exp_from_encoding x t in |
| 133 |
let/n res := F.pow res 2 in |
| 134 |
res |
| 135 |
| (false, k) :: t => |
| 136 |
let/n res := exp_from_encoding x t in |
| 137 |
let/n res := Nat.iter (S k) (exp_square) res in |
| 138 |
res |
| 139 |
end. |
| 140 |
|
| 141 |
Definition exp_by_squaring_encoded_simple (x : F m) (n : positive) : F m := |
| 142 |
exp_from_encoding_simple x (run_length_encoding n). |
| 143 |
|
| 144 |
Definition exp_by_squaring_encoded (x : F m) (n : positive) : F m := |
| 145 |
exp_from_encoding x (run_length_encoding n). |
| 146 |
End Impl. |
| 147 |
|
| 148 |
Section Proofs. |
| 149 |
Context (m : positive). |
| 150 |
|
| 151 |
|
| 152 |
Lemma F_mul_1_r : forall x : F m, |
| 153 |
(x * 1)%F = x. |
| 154 |
Proof using Type. |
| 155 |
intros. |
| 156 |
F_lia. |
| 157 |
Qed. |
| 158 |
|
| 159 |
Lemma F_mul_1_l : forall x : F m, |
| 160 |
(1 * x)%F = x. |
| 161 |
Proof using Type. |
| 162 |
intros. |
| 163 |
F_lia. |
| 164 |
Qed. |
| 165 |
|
| 166 |
Create HintDb F_pow. |
| 167 |
Hint Rewrite @F.pow_2_r : F_pow. |
| 168 |
Hint Rewrite @F.pow_add_r : F_pow. |
| 169 |
Hint Rewrite @F.pow_mul_l : F_pow. |
| 170 |
Hint Rewrite <- @F.pow_pow_l : F_pow. |
| 171 |
Hint Rewrite @F.pow_1_r : F_pow. |
| 172 |
Hint Rewrite @F.pow_3_r : F_pow. |
| 173 |
|
| 174 |
Ltac simplify_F := |
| 175 |
unfold nlet; |
| 176 |
autorewrite with F_pow; |
| 177 |
repeat rewrite F_mul_1_r; |
| 178 |
repeat rewrite F_mul_1_l; |
| 179 |
try reflexivity; |
| 180 |
try F_lia. |
| 181 |
|
| 182 |
|
| 183 |
Definition Pos2N_pos_xI n : N.pos n~1 = (2 * N.pos n + 1)%N := eq_refl. |
| 184 |
Definition Pos2N_pos_xO n : N.pos n~0 = (2 * N.pos n)%N := eq_refl. |
| 185 |
|
| 186 |
Lemma exp_by_squaring_correct : |
| 187 |
forall n x, exp_by_squaring m x n = (x ^ N.pos n)%F. |
| 188 |
Proof using Type. |
| 189 |
induction n; intros; cbn [exp_by_squaring]; unfold nlet; |
| 190 |
rewrite (Pos2N_pos_xI n) || rewrite (Pos2N_pos_xO n) || idtac. |
| 191 |
all: try rewrite IHn. |
| 192 |
all: try destruct n eqn : H'; autorewrite with F_pow; try reflexivity. |
| 193 |
all: F_lia. |
| 194 |
Qed. |
| 195 |
|
| 196 |
Lemma run_length_encoding_nonempty: |
| 197 |
forall n, run_length_encoding n <> []. |
| 198 |
Proof using Type. |
| 199 |
destruct n eqn : H'; simpl. |
| 200 |
+ destruct (run_length_encoding p). |
| 201 |
{ inversion 1. } |
| 202 |
destruct p0; destruct b; inversion 1. |
| 203 |
+ destruct (run_length_encoding p). |
| 204 |
{ inversion 1. } |
| 205 |
destruct p0; destruct b; inversion 1. |
| 206 |
+ inversion 1. |
| 207 |
Qed. |
| 208 |
|
| 209 |
Lemma exp_by_squaring_encoded_simple_correct : |
| 210 |
forall n x, exp_by_squaring_encoded_simple m x n = (x ^ N.pos n)%F. |
| 211 |
Proof using Type. |
| 212 |
intros. rewrite <- exp_by_squaring_correct; eauto. |
| 213 |
unfold exp_by_squaring_encoded_simple; induction n; simpl. |
| 214 |
- destruct (run_length_encoding n) eqn : Heq. |
| 215 |
+ apply run_length_encoding_nonempty in Heq. |
| 216 |
inversion Heq. |
| 217 |
+ destruct p. destruct b. |
| 218 |
* rewrite <- IHn. |
| 219 |
cbn. replace (n0 + 1)%nat with (S n0) by lia. |
| 220 |
unfold nlet; destruct n0; cbn; destruct n eqn : H'; unfold exp_square_and_multiply; unfold nlet; set (exp_from_encoding_simple _ x l) as expxl; F_lia. |
| 221 |
* rewrite <- IHn. |
| 222 |
cbn. |
| 223 |
unfold nlet. |
| 224 |
destruct n0; destruct n eqn : H'; try reflexivity. |
| 225 |
- rewrite <- IHn. |
| 226 |
destruct n eqn : H'. |
| 227 |
* destruct (run_length_encoding p~1) as [|[[|] n0] t]; try reflexivity. |
| 228 |
simpl. |
| 229 |
replace (n0 + 1)%nat with (S n0) by lia. |
| 230 |
unfold nlet. |
| 231 |
cbn. |
| 232 |
unfold exp_square. |
| 233 |
destruct n0; cbv [nlet Nat.iter]; simpl; F_lia. |
| 234 |
* destruct (run_length_encoding p~0) as [|[[|] n0] t]; try reflexivity. |
| 235 |
simpl. |
| 236 |
replace (n0 + 1)% nat with (S n0) by lia. |
| 237 |
unfold nlet. |
| 238 |
simpl. |
| 239 |
unfold exp_square. |
| 240 |
destruct n0; cbv [nlet Nat.iter]; simpl; F_lia. |
| 241 |
* unfold run_length_encoding. |
| 242 |
unfold exp_from_encoding_simple. |
| 243 |
simplify_F. |
| 244 |
- simplify_F. |
| 245 |
Qed. |
| 246 |
|
| 247 |
Lemma exp_by_squaring_encoded_correct : |
| 248 |
forall n x, exp_by_squaring_encoded m x n = (x ^ N.pos n)%F. |
| 249 |
|
| 250 |
Proof using Type. |
| 251 |
intros. rewrite <- exp_by_squaring_encoded_simple_correct; eauto. |
| 252 |
unfold exp_by_squaring_encoded. unfold exp_by_squaring_encoded_simple. |
| 253 |
induction (run_length_encoding n). |
| 254 |
- cbn. |
| 255 |
reflexivity. |
| 256 |
- destruct a. |
| 257 |
destruct b; simpl exp_from_encoding; unfold exp_from_encoding_simple; fold exp_from_encoding_simple; destruct n0; try destruct n0; destruct l; try rewrite IHl; simpl exp_from_encoding_simple; try simplify_F. |
| 258 |
+ unfold exp_square_and_multiply. |
| 259 |
simpl Nat.iter. |
| 260 |
simplify_F. |
| 261 |
+ pose Nat_iter_plus_one. |
| 262 |
specialize e with (f := exp_square_and_multiply m x) (k := S (S n0)) (x := 1%F). |
| 263 |
rewrite <- e. |
| 264 |
replace (exp_square_and_multiply m x 1) with x. |
| 265 |
2: { unfold exp_square_and_multiply; simplify_F. } |
| 266 |
pose Nat_iter_plus_one. |
| 267 |
specialize e0 with (f := exp_square_and_multiply m x) (k := (S n0)) (x := x). |
| 268 |
rewrite <- e0. |
| 269 |
f_equal. |
| 270 |
unfold exp_square_and_multiply. |
| 271 |
simplify_F. |
| 272 |
+ destruct p; destruct b; destruct n0; eauto. |
| 273 |
destruct l; eauto. |
| 274 |
unfold exp_from_encoding_simple. |
| 275 |
simplify_F. |
| 276 |
+ destruct p; destruct b; destruct n0; eauto. |
| 277 |
destruct l; eauto. |
| 278 |
unfold exp_from_encoding_simple, exp_square. |
| 279 |
simpl Nat.iter. |
| 280 |
simplify_F. |
| 281 |
+ destruct p; destruct b; destruct n1; eauto. |
| 282 |
destruct l; eauto. |
| 283 |
unfold exp_from_encoding_simple. |
| 284 |
simplify_F. |
| 285 |
pose Nat_iter_plus_one. |
| 286 |
specialize e with (f := exp_square m) (k := (S (S n0))) (x := x). |
| 287 |
rewrite <- e. |
| 288 |
unfold exp_square. |
| 289 |
simplify_F. |
| 290 |
Qed. |
| 291 |
|
| 292 |
Lemma clean_width : |
| 293 |
forall x, x < 2 ^ 32 -> x < 2 ^ width. |
| 294 |
Proof using ext_spec_ok locals_ok mem_ok word_ok. |
| 295 |
intros; destruct width_cases as [ -> | -> ]; lia. |
| 296 |
Qed. |
| 297 |
|
| 298 |
End Proofs. |
| 299 |
|
| 300 |
Section Bedrock2. |
| 301 |
Section Lowering. |
| 302 |
Lemma letn_equal : |
| 303 |
forall A B (val : A) (body_l body_r : A -> B) (var : string), |
| 304 |
(let x := val in body_l x = body_r x) |
| 305 |
-> (let/n x as var := val in body_l x) = (let/n y as var := val in body_r y). |
| 306 |
Proof using Type. |
| 307 |
intros. assumption. |
| 308 |
Qed. |
| 309 |
|
| 310 |
Lemma letn_nested : |
| 311 |
forall A B C (a : C) (val1 : A) (body1 : A -> B) (body2 : B -> C) (var var' : string), |
| 312 |
a = (let/n x as var' := val1 in let/n y as var := body1 x in body2 y) |
| 313 |
-> a = (let/n y as var := let/n x as var' := val1 in body1 x in body2 y). |
| 314 |
Proof using Type. |
| 315 |
intros. assumption. |
| 316 |
Qed. |
| 317 |
|
| 318 |
Lemma letn_paren_equal : |
| 319 |
forall A B (a : A) (val : A) (body_r : A -> B) (var : string) left, |
| 320 |
left = (let/n y as var := val in (a, body_r y)) |
| 321 |
-> left = (a, let/n y as var := val in body_r y). |
| 322 |
Proof using Type. |
| 323 |
intros. |
| 324 |
rewrite H. reflexivity. |
| 325 |
Qed. |
| 326 |
End Lowering. |
| 327 |
|
| 328 |
Section Compilation. |
| 329 |
Hint Resolve relax_bounds : compiler. |
| 330 |
|
| 331 |
Create HintDb lowering. |
| 332 |
Hint Unfold exp_by_squaring : lowering. |
| 333 |
Hint Unfold exp_by_squaring_encoded : lowering. |
| 334 |
Hint Unfold exp_from_encoding : lowering. |
| 335 |
Hint Unfold exp_by_squaring_encoded_simple : lowering. |
| 336 |
Hint Unfold exp_from_encoding_simple : lowering. |
| 337 |
Hint Unfold run_length_encoding : lowering. |
| 338 |
Hint Unfold exp_square : lowering. |
| 339 |
Hint Unfold exp_square_and_multiply : lowering. |
| 340 |
|
| 341 |
Ltac lower_step := |
| 342 |
match goal with |
| 343 |
| [ |- forall _, _ ] => |
| 344 |
intros |
| 345 |
| [ |- let _ := _ in _ = _ ] => |
| 346 |
intros |
| 347 |
| [ |- _ = let/n _ as _ := ?x in _] => |
| 348 |
is_var x; change (nlet _ ?x ?k) with (k x) at 1; cbv beta |
| 349 |
| [ |- _ = let/n _ as _ := (let/n _ as _ := _ in _) in _] => |
| 350 |
eapply letn_nested |
| 351 |
| [ |- _ = let/n _ as _ := _ in _] => |
| 352 |
eapply letn_equal with (body_l := fun _ => _) |
| 353 |
| [ |- _ = (_, let/n _ as _ := _ in _)] => |
| 354 |
eapply letn_paren_equal |
| 355 |
end. |
| 356 |
|
| 357 |
Ltac lower_setup := |
| 358 |
autounfold with lowering; |
| 359 |
lazymatch goal with |
| 360 |
| [ H := _ |- _ ] => |
| 361 |
subst H |
| 362 |
end; |
| 363 |
repeat simpl Nat.add; |
| 364 |
cbn -[Nat.iter]. |
| 365 |
|
| 366 |
Ltac lower := |
| 367 |
lower_setup; |
| 368 |
repeat lower_step; |
| 369 |
reflexivity. |
| 370 |
|
| 371 |
(* TODO: Move to Rupicola's standard library and check if it breaks anything*) |
| 372 |
Ltac compile_try_copy_pointer := |
| 373 |
lazymatch goal with |
| 374 |
| [ |- WeakestPrecondition.cmd _ _ _ ?mem _ (_ (nlet_eq [ _ ] ?x _)) ] => |
| 375 |
lazymatch goal with |
| 376 |
| [ H : ?pred mem |- _ ] => |
| 377 |
lazymatch pred with |
| 378 |
| context [ ?data ?ptr x ] => |
| 379 |
simple eapply @compile_copy_pointer with (x_ptr := ptr) (Data := data) |
| 380 |
end |
| 381 |
end |
| 382 |
end. |
| 383 |
|
| 384 |
Local Ltac ecancel_assumption ::= ecancel_assumption_impl. |
| 385 |
|
| 386 |
Context {field_parameters : FieldParameters}. |
| 387 |
Context {field_representaton : FieldRepresentation}. |
| 388 |
Context {field_representation_ok : FieldRepresentation_ok}. |
| 389 |
|
| 390 |
Definition exp (e : positive) (x : F M_pos) := F.pow x (N.pos e). |
| 391 |
|
| 392 |
Instance spec_of_exp_6 |
| 393 |
: spec_of "exp_6" := |
| 394 |
fnspec! "exp_6" (sq_ptr x_ptr : word) / (sq x : F M_pos) R, |
| 395 |
{ requires tr mem := |
| 396 |
(FElem (Some tight_bounds) x_ptr x |
| 397 |
* FElem (Some tight_bounds) sq_ptr sq * R)%sep mem; |
| 398 |
ensures tr' mem' := |
| 399 |
tr = tr' |
| 400 |
/\ (FElem (Some tight_bounds) x_ptr x |
| 401 |
* FElem (Some tight_bounds) sq_ptr (exp 6 x) * R)%sep mem'}. |
| 402 |
|
| 403 |
Ltac rewrite_exponentiation lemma := |
| 404 |
lazymatch goal with |
| 405 |
| |- WeakestPrecondition.cmd _ _ _ ?mem _ (_ (?x ^ N.pos ?n)%F) => |
| 406 |
eassert (?[rewritten] = (x ^ N.pos n)%F) as <- |
| 407 |
by (rewrite <- lemma by assumption; |
| 408 |
lower; reflexivity) |
| 409 |
end. |
| 410 |
|
| 411 |
Section Exp_by_squaring. |
| 412 |
Hint Extern 1 => rewrite_exponentiation exp_by_squaring_correct; shelve : compiler_cleanup. |
| 413 |
|
| 414 |
Derive exp_6_body SuchThat |
| 415 |
(defn! "exp_6" ("res", "x") { exp_6_body }, |
| 416 |
implements (exp 6) using [square; mul]) |
| 417 |
As exp_6_body_correct. |
| 418 |
Proof. |
| 419 |
compile. |
| 420 |
Qed. |
| 421 |
End Exp_by_squaring. |
| 422 |
|
| 423 |
Instance spec_of_exp97 : spec_of "exp_97" := |
| 424 |
fnspec! "exp_97" (sq_ptr x_ptr : word) / (sq x : F M_pos) R, |
| 425 |
{ requires tr mem := |
| 426 |
(FElem (Some tight_bounds) x_ptr x |
| 427 |
* FElem (Some tight_bounds) sq_ptr sq * R)%sep mem; |
| 428 |
ensures tr' mem' := |
| 429 |
tr = tr' |
| 430 |
/\ (FElem (Some tight_bounds) x_ptr x |
| 431 |
* FElem (Some tight_bounds) sq_ptr (exp 97 x) * R)%sep mem'}. |
| 432 |
|
| 433 |
Local Instance spec_of_exp_large : spec_of "fe25519_inv" := |
| 434 |
fnspec! "fe25519_inv" (sq_ptr x_ptr : word) / (sq x : F M_pos) R, |
| 435 |
{ requires tr mem := |
| 436 |
(FElem (Some tight_bounds) x_ptr x |
| 437 |
* FElem None sq_ptr sq * R)%sep mem; |
| 438 |
ensures tr' mem' := |
| 439 |
tr = tr' |
| 440 |
/\ (FElem (Some tight_bounds) x_ptr x |
| 441 |
* FElem (Some tight_bounds) sq_ptr (exp (2^255-21) x) * R)%sep mem'}. |
| 442 |
|
| 443 |
Import LoopCompiler. |
| 444 |
Hint Resolve clean_width : compiler_side_conditions. |
| 445 |
Hint Extern 10 => lia : compiler_side_conditions. |
| 446 |
Hint Extern 1 => simple apply compile_square; shelve : compiler. |
| 447 |
Hint Extern 1 => simple apply compile_mul; shelve : compiler. |
| 448 |
Hint Extern 1 => compile_downto; shelve : compiler. |
| 449 |
Hint Extern 1 => compile_try_copy_pointer; shelve : compiler. |
| 450 |
|
| 451 |
Hint Extern 1 => rewrite_exponentiation exp_by_squaring_encoded_correct; shelve : compiler_cleanup. |
| 452 |
|
| 453 |
Derive exp_97_body SuchThat |
| 454 |
(defn! "exp_97" ("res", "x") { exp_97_body }, |
| 455 |
implements (exp 97) using [square; mul]) |
| 456 |
As exp_97_body_correct. |
| 457 |
Proof. |
| 458 |
compile. |
| 459 |
Qed. |
| 460 |
|
| 461 |
Derive fe25519_inv SuchThat |
| 462 |
(defn! "fe25519_inv" ("res", "x") { fe25519_inv }, |
| 463 |
implements (exp (2^255-21)) using [square; mul]) |
| 464 |
As fe25519_inv_correct_exp. |
| 465 |
Proof. |
| 466 |
compile. |
| 467 |
Qed. |
| 468 |
|
| 469 |
Context { F_M_pos : Z.pos M_pos = 2^255-19 }. |
| 470 |
|
| 471 |
Lemma compile_inv : forall m l tr functions x, |
| 472 |
let v := F.inv x in |
| 473 |
forall P (pred : P v -> predicate) (k : nlet_eq_k P v) k_impl |
| 474 |
(R : map.rep -> Prop) (out : F M_pos) |
| 475 |
(x_ptr : word.rep) (x_var : string) (out_ptr : word.rep) (out_var : string) |
| 476 |
(out_bounds : option bounds), |
| 477 |
|
| 478 |
spec_of_exp_large functions -> |
| 479 |
|
| 480 |
map.get l out_var = Some out_ptr -> |
| 481 |
map.get l x_var = Some x_ptr -> |
| 482 |
(FElem out_bounds out_ptr out ⋆ FElem (Some tight_bounds) x_ptr x ⋆ R) m -> |
| 483 |
|
| 484 |
(let v := v in |
| 485 |
forall m', |
| 486 |
(FElem (Some loose_bounds) out_ptr v ⋆ FElem (Some tight_bounds) x_ptr x ⋆ R) m' -> |
| 487 |
<{ Trace := tr; Memory := m'; Locals := l; Functions := functions }> |
| 488 |
k_impl |
| 489 |
<{ pred (k v eq_refl) }>) -> |
| 490 |
|
| 491 |
<{ Trace := tr; Memory := m; Locals := l; Functions := functions }> |
| 492 |
|
| 493 |
cmd.seq (cmd.call [] "fe25519_inv" [expr.var out_var; expr.var x_var]) k_impl |
| 494 |
|
| 495 |
<{ pred (let/n x as out_var eq:Heq := v in k x Heq) }>. |
| 496 |
Proof using F_M_pos ext_spec_ok field_representation_ok locals_ok mem_ok word_ok. |
| 497 |
repeat straightline. |
| 498 |
repeat (eexists; split; eauto). |
| 499 |
straightline_call. |
| 500 |
ecancel_assumption. |
| 501 |
repeat (eexists; split; eauto). |
| 502 |
repeat compile_step. |
| 503 |
unfold nlet_eq. |
| 504 |
intuition subst. |
| 505 |
eapply H3. |
| 506 |
repeat compile_step. |
| 507 |
intuition subst. |
| 508 |
subst v. |
| 509 |
replace (F.inv x) with (exp (2^255-21) x). |
| 510 |
2: { unshelve erewrite F.Fq_inv_fermat; rewrite F_M_pos; try vm_decide. |
| 511 |
exact Curve25519.prime_p. |
| 512 |
eauto. |
| 513 |
} |
| 514 |
ecancel_assumption. |
| 515 |
Qed. |
| 516 |
|
| 517 |
(* |
| 518 |
Context { F_M_pos : Z.pos M_pos = 2^255-19 }. |
| 519 |
Require Import Crypto.Spec.Curve25519. |
| 520 |
|
| 521 |
Derive fe25519_inv SuchThat |
| 522 |
(defn! inv ("res", "x") { fe25519_inv }, |
| 523 |
implements (exp (2^255-21)) using [square; mul]) |
| 524 |
As fe25519_inv_correct_exp. |
| 525 |
Proof. |
| 526 |
(* replace (exp (2^255-21)) with @F.inv.*) |
| 527 |
compile_setup_unfold_spec_of. |
| 528 |
unfold spec_of_UnOp, un_inv, un_model. |
| 529 |
cbv [ spec_of_UnOp unop_spec un_inv un_model ]. |
| 530 |
intros. |
| 531 |
replace (F.inv (feval x)) with (exp (2^255-21) (feval x)). |
| 532 |
2: { unshelve erewrite F.Fq_inv_fermat; rewrite F_M_pos; try vm_decide. |
| 533 |
exact Curve25519.prime_p. |
| 534 |
lia. |
| 535 |
unfold feval; eauto. |
| 536 |
} |
| 537 |
compile_setup. |
| 538 |
rewrite_exponentiation exp_by_squaring_encoded_correct. |
| 539 |
|
| 540 |
destruct H2. |
| 541 |
destruct H3. |
| 542 |
destruct H3. |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
eassert ((FElem _ px _ * _) mem0)%sep. |
| 547 |
unfold FElem. |
| 548 |
sepsimpl. |
| 549 |
eexists. |
| 550 |
sepsimpl. |
| 551 |
reflexivity. |
| 552 |
unfold un_xbounds in H2. |
| 553 |
|
| 554 |
unfold maybe_bounded. |
| 555 |
let x := open_constr:(Some _) in instantiate(2:=x). |
| 556 |
simpl. |
| 557 |
eapply H2. |
| 558 |
eapply H3. |
| 559 |
|
| 560 |
compile_step. |
| 561 |
compile_step. |
| 562 |
compile_step. |
| 563 |
|
| 564 |
unfold maybe_bounded. |
| 565 |
unfold un_xbounds in H2. |
| 566 |
unfold FElem. |
| 567 |
sepsimpl. |
| 568 |
eexists. |
| 569 |
sepsimpl. |
| 570 |
reflexivity. |
| 571 |
instantiate (2 := None). |
| 572 |
eauto. |
| 573 |
eapply H4. |
| 574 |
|
| 575 |
compile_step. |
| 576 |
compile_step. |
| 577 |
|
| 578 |
Set Typeclasses Debug. |
| 579 |
compile_step. |
| 580 |
Set Typeclasses Debug. |
| 581 |
compile_step. |
| 582 |
compile_step. |
| 583 |
compile_step. |
| 584 |
compile_step. |
| 585 |
|
| 586 |
instantiate (2 := pout). instantiate (1 := Rr). |
| 587 |
|
| 588 |
eapply H6. |
| 589 |
compile_step. |
| 590 |
|
| 591 |
let x := open_constr:(Some _) in instantiate(2:=pout). |
| 592 |
simpl. |
| 593 |
eapply H2. |
| 594 |
eapply H3. |
| 595 |
compile_step. |
| 596 |
|
| 597 |
ecancel_assumption. |
| 598 |
|
| 599 |
compile_step. |
| 600 |
compile_step. |
| 601 |
compile_step. |
| 602 |
compile_step. |
| 603 |
|
| 604 |
|
| 605 |
Set Typeclasses Debug. |
| 606 |
repeat compile_step. |
| 607 |
|
| 608 |
inversion H5. |
| 609 |
|
| 610 |
3: { instantiate (2 := "x"). repeat compile_step. } |
| 611 |
3: { instantiate (1 := "x"). repeat compile_step. } |
| 612 |
|
| 613 |
change (fun x => ?c x) with c. |
| 614 |
|
| 615 |
Global Instance spec_of_fe25519_inv : spec_of "fe25519_inv" := |
| 616 |
fnspec! "fe25519_inv" (sq_ptr x_ptr : word) / (sq x : F M_pos) R, |
| 617 |
{ requires tr mem := |
| 618 |
(FElem (Some tight_bounds) x_ptr x |
| 619 |
* FElem (Some tight_bounds) sq_ptr sq * R)%sep mem; |
| 620 |
ensures tr' mem' := |
| 621 |
tr = tr' |
| 622 |
/\ (FElem (Some tight_bounds) x_ptr x |
| 623 |
* FElem (Some tight_bounds) sq_ptr (F.inv x) * R)%sep mem'}. |
| 624 |
|
| 625 |
Require Import Crypto.Spec.Curve25519. *) |
| 626 |
Lemma fe_inv_correct : |
| 627 |
Z.pos M_pos = 2^255-19 -> |
| 628 |
(forall functions : map.rep, |
| 629 |
map.get functions "fe25519_inv" = Some fe25519_inv -> |
| 630 |
spec_of_UnOp un_square functions -> spec_of_BinOp bin_mul functions -> |
| 631 |
spec_of_exp_large functions). |
| 632 |
Proof using ext_spec_ok field_representation_ok locals_ok mem_ok word_ok. |
| 633 |
intros Hm HmPrime ? ** ? **. |
| 634 |
eapply Proper_call; [|eapply fe25519_inv_correct_exp; eauto 1; exact I]. |
| 635 |
intros ? ** ? ** ? ** ?; intuition idtac. |
| 636 |
Qed. |
| 637 |
|
| 638 |
End Compilation. |
| 639 |
End Bedrock2. |
| 640 |
End FElems. |
| 641 |
|
| 642 |
#[export] Hint Extern 8 (WeakestPrecondition.cmd _ _ _ _ _ (_ (nlet_eq _ (F.inv _) _))) => |
| 643 |
simple eapply compile_inv; shelve : compiler. |
| 644 |
|
| 645 |
(* |
| 646 |
(* LATER: Make a separate compilation lemma for (loop) *) |
| 647 |
|
| 648 |
Require Import bedrock2.BasicC64Semantics. |
| 649 |
|
| 650 |
Section Extraction. |
| 651 |
Definition _M_pos := (2 ^ 255 - 19)%positive. |
| 652 |
Context (_a24: F _M_pos). |
| 653 |
|
| 654 |
Instance fp : FieldParameters := |
| 655 |
{| M_pos := _M_pos; |
| 656 |
a24 := _a24; |
| 657 |
mul := "mul"; |
| 658 |
add := "add"; |
| 659 |
sub := "sub"; |
| 660 |
opp := "opp"; |
| 661 |
square := "square"; |
| 662 |
scmula24 := "scmula24"; |
| 663 |
inv := "inv"; |
| 664 |
from_bytes := "from_bytes"; |
| 665 |
to_bytes := "to_bytes"; |
| 666 |
select_znz := "select_znz"; |
| 667 |
felem_copy := "felem_copy"; |
| 668 |
from_word := "from_word" |}. |
| 669 |
End Extraction. |
| 670 |
*) |