Debian Codegraph @

src/Fancy/Compiler.v

raw
1 From Coq Require Import ZArith Lia.
2 From Coq Require Import List. Import ListNotations.
3 Require Import Rewriter.Language.Wf. Import Language.Wf.Compilers.
4 Require Import Rewriter.Language.Inversion. Import Language.Inversion.Compilers.
5 Require Import Crypto.Language.API. Import Language.API.Compilers.
6 Import Compilers.API.
7 Require Import Crypto.CastLemmas.
8 Require Import Crypto.Fancy.Spec.
9 Require Import Crypto.Util.Option.
10 Require Import Crypto.Util.Prod.
11 Require Import Crypto.Util.Tactics.BreakMatch.
12 Require Import Crypto.Util.Tactics.DestructHead.
13 Require Import Crypto.Util.Tuple.
14 Require Import Crypto.Util.ZRange.
15 Require Import Crypto.Util.ZUtil.Zselect.
16 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
17 Require Import Crypto.Util.ZUtil.AddModulo.
18 Require Import Crypto.Util.ZUtil.Definitions.
19 Require Import Crypto.Util.ZUtil.Div.
20 Require Import Crypto.Util.ZUtil.Notations.
21 Require Import Crypto.Util.Notations.
22 Local Open Scope Z_scope.
23
24 (* TODO: organize this file *)
25 Section of_prefancy.
26 (* We would like to write [Local Notation cexpr := (@API.expr).] but
27 we can't, in part because implicit status doesn't work through
28 nested notations, and in part because
29 COQBUG(https://github.com/coq/coq/issues/10803) *)
30 (* Set Printing All. Print API.expr. *)
31 Local Notation cexpr := (@expr.expr (Language.Compilers.base.type.type Compilers.base) IdentifiersBasicGENERATED.Compilers.ident).
32 Local Notation tZ := (base.type.type_base base.type.Z).
33 Local Notation tzrange := (base.type.type_base base.type.zrange).
34 Local Notation cstZ r
35 := (expr.App
36 (expr.Ident ident.Z_cast)
37 (expr.Ident (@ident.Literal base.type.zrange r%zrange))).
38 Local Notation cstZZ r1 r2
39 := (expr.App
40 (expr.Ident ident.Z_cast2)
41 (expr.App
42 (expr.App
43 (expr.Ident ident.pair)
44 (expr.Ident (@ident.Literal base.type.zrange r1%zrange)))
45 (expr.Ident (@ident.Literal base.type.zrange r2%zrange)))).
46 Local Notation LetInAppIdentZ S D r eidc x f
47 := (expr.LetIn
48 (A:=type.base tZ)
49 (B:=type.base D)
50 (expr.App
51 (s:=type.base tZ)
52 (d:=type.base tZ)
53 (cstZ r)
54 (expr.App
55 (s:=type.base S)
56 (d:=type.base tZ)
57 eidc
58 x))
59 f).
60 Local Notation LetInAppIdentZZ S D r1 r2 eidc x f
61 := (expr.LetIn
62 (A:=type.base (base.type.prod tZ tZ))
63 (B:=type.base D)
64 (expr.App
65 (s:=type.base (base.type.prod tZ tZ))
66 (d:=type.base (base.type.prod tZ tZ))
67 (cstZZ r1 r2)
68 (expr.App
69 (s:=type.base S)
70 (d:=type.base (base.type.prod tZ tZ))
71 eidc
72 x))
73 f).
74 Local Notation pair_LiteralZ B v e :=
75 (expr.App
76 (s:=type.base B)
77 (expr.App
78 (expr.Ident (@ident.pair _ _))
79 (expr.Ident (ident.Literal (t:=base.type.Z) v)))
80 e).
81 Local Notation pair_LiteralZZ B v1 v2 e :=
82 (expr.App
83 (s:=type.base B)
84 (expr.App
85 (expr.Ident (@ident.pair _ _))
86 (expr.App
87 (expr.App
88 (expr.Ident (@ident.pair _ _))
89 (expr.Ident (ident.Literal (t:=base.type.Z) v1)))
90 (expr.Ident (ident.Literal (t:=base.type.Z) v2))))
91 e).
92
93 Context (name : Type) (name_succ : name -> name) (error : name) (consts : Z -> option name).
94
95 Fixpoint base_var (t : base.type) : Type :=
96 match t with
97 | tZ => name
98 | base.type.prod a b => base_var a * base_var b
99 | _ => unit
100 end.
101 Fixpoint var (t : type.type base.type) : Type :=
102 match t with
103 | type.base t => base_var t
104 | type.arrow s d => var s -> var d
105 end.
106 Fixpoint base_error {t} : base_var t
107 := match t with
108 | tZ => error
109 | base.type.prod A B => (@base_error A, @base_error B)
110 | _ => tt
111 end.
112 Fixpoint make_error {t} : var t
113 := match t with
114 | type.base _ => base_error
115 | type.arrow s d => fun _ => @make_error d
116 end.
117
118 Fixpoint of_prefancy_scalar {t} (s : @cexpr var t) : var t
119 := match s in expr.expr t return var t with
120 | Compilers.expr.Var t v => v
121 | expr.App s d f x => @of_prefancy_scalar _ f (@of_prefancy_scalar _ x)
122 | expr.Ident t idc
123 => match idc in ident.ident t return var t with
124 | ident.Literal base.type.Z v => match consts v with
125 | Some n => n
126 | None => error
127 end
128 | ident.pair A B => fun a b => (a, b)%core
129 | ident.fst A B => fun v => fst v
130 | ident.snd A B => fun v => snd v
131 | ident.Z_cast => fun r v => v
132 | ident.Z_cast2 => fun r v => v
133 | ident.Z_land => fun x y => x
134 | _ => make_error
135 end
136 | expr.Abs s d f => make_error
137 | expr.LetIn A B x f => make_error
138 end%expr_pat%etype.
139
140 (* Note : some argument orders are reversed for MUL128LU, MUL128UL, SELC, SELM, and SELL *)
141 Definition of_prefancy_ident {s d : base.type} (idc : ident.ident (s -> d))
142 : @cexpr var s -> option {i : instruction & tuple name i.(num_source_regs) } :=
143 match idc in ident.ident t return match t return Type with
144 | type.arrow (type.base s) (type.base d)
145 => @cexpr var s
146 | _ => unit
147 end
148 -> option {i : instruction & tuple name i.(num_source_regs) }
149 with
150 | ident.fancy_selc
151 => fun args : @cexpr var (tZ * tZ * tZ) => Some (existT _ SELC (of_prefancy_scalar ((#ident.snd @ args), (#ident.snd @ (#ident.fst @ args)))))
152 | ident.fancy_sell
153 => fun args : @cexpr var (tZ * tZ * tZ) => Some (existT _ SELL (of_prefancy_scalar ((#ident.snd @ args), (#ident.snd @ (#ident.fst @ args)))))
154 | ident.fancy_addm
155 => fun args : @cexpr var (tZ * tZ * tZ) => Some (existT _ ADDM (of_prefancy_scalar args))
156 | ident.fancy_mulll
157 => fun args : @cexpr var (tZ * (tZ * tZ)) =>
158 match args with
159 | pair_LiteralZ (tZ * tZ) log2wordmax args =>
160 if Z.eqb log2wordmax 256
161 then Some (existT _ MUL128LL (of_prefancy_scalar args))
162 else None
163 | _ => None
164 end
165 | ident.fancy_mullh
166 => fun args : @cexpr var (tZ * (tZ * tZ)) =>
167 match args with
168 | pair_LiteralZ (tZ * tZ) log2wordmax args =>
169 if Z.eqb log2wordmax 256
170 then Some (existT _ MUL128LU (of_prefancy_scalar ((#ident.snd @ args), (#ident.fst @ args))))
171 else None
172 | _ => None
173 end
174 | ident.fancy_mulhl
175 => fun args : @cexpr var (tZ * (tZ * tZ)) =>
176 match args with
177 | pair_LiteralZ (tZ * tZ) log2wordmax args =>
178 if Z.eqb log2wordmax 256
179 then Some (existT _ MUL128UL (of_prefancy_scalar ((#ident.snd @ args), (#ident.fst @ args))))
180 else None
181 | _ => None
182 end
183 | ident.fancy_mulhh
184 => fun args : @cexpr var (tZ * (tZ * tZ)) =>
185 match args with
186 | pair_LiteralZ (tZ * tZ) log2wordmax args =>
187 if Z.eqb log2wordmax 256
188 then Some (existT _ MUL128UU (of_prefancy_scalar args))
189 else None
190 | _ => None
191 end
192 | ident.fancy_selm
193 => fun args : @cexpr var (tZ * (tZ * tZ * tZ)) =>
194 match args with
195 | pair_LiteralZ (tZ * tZ * tZ) log2wordmax args =>
196 if Z.eqb log2wordmax 256
197 then Some (existT _ SELM (of_prefancy_scalar ((#ident.snd @ args), (#ident.snd @ (#ident.fst @ args)))))
198 else None
199 | _ => None
200 end
201 | ident.fancy_add
202 => fun args : @cexpr var ((tZ * tZ) * (tZ * tZ)) =>
203 match args with
204 | pair_LiteralZZ (tZ * tZ) log2wordmax imm args =>
205 if Z.eqb log2wordmax 256
206 then Some (existT _ (ADD imm) (of_prefancy_scalar args))
207 else None
208 | _ => None
209 end
210 | ident.fancy_addc
211 => fun args : @cexpr var ((tZ * tZ) * (tZ * tZ * tZ)) =>
212 match args with
213 | pair_LiteralZZ (tZ * tZ * tZ) log2wordmax imm args =>
214 if Z.eqb log2wordmax 256
215 then Some (existT _ (ADDC imm) (of_prefancy_scalar ((#ident.snd @ (#ident.fst @ args)), (#ident.snd @ args))))
216 else None
217 | _ => None
218 end
219 | ident.fancy_sub
220 => fun args : @cexpr var ((tZ * tZ) * (tZ * tZ)) =>
221 match args with
222 | pair_LiteralZZ (tZ * tZ) log2wordmax imm args =>
223 if Z.eqb log2wordmax 256
224 then Some (existT _ (SUB imm) (of_prefancy_scalar args))
225 else None
226 | _ => None
227 end
228 | ident.fancy_subb
229 => fun args : @cexpr var ((tZ * tZ) * (tZ * tZ * tZ)) =>
230 match args with
231 | pair_LiteralZZ (tZ * tZ * tZ) log2wordmax imm args =>
232 if Z.eqb log2wordmax 256
233 then Some (existT _ (SUBC imm) (of_prefancy_scalar ((#ident.snd @ (#ident.fst @ args)), (#ident.snd @ args))))
234 else None
235 | _ => None
236 end
237 | ident.fancy_rshi
238 => fun args : @cexpr var ((tZ * tZ) * (tZ * tZ)) =>
239 match args with
240 | pair_LiteralZZ (tZ * tZ) log2wordmax imm args =>
241 if Z.eqb log2wordmax 256
242 then Some (existT _ (RSHI imm) (of_prefancy_scalar args))
243 else None
244 | _ => None
245 end
246 | _ => fun _ => None
247 end%option.
248
249 Local Notation "x <- y ; f" := (match y with Some x => f | None => Ret error end).
250 Definition of_prefancy_step
251 (of_prefancy : forall (next_name : name) {t} (e : @cexpr var t), @expr name)
252 (next_name : name) {t} (e : @cexpr var t) : @expr name
253 := let default _ := (e' <- type.try_transport (@cexpr var) t tZ e;
254 Ret (of_prefancy_scalar e')) in
255 match e with
256 | LetInAppIdentZ s d r eidc x f
257 => idc <- invert_expr.invert_Ident eidc;
258 instr_args <- @of_prefancy_ident s tZ idc x;
259 let i : instruction := projT1 instr_args in
260 let args : tuple name i.(num_source_regs) := projT2 instr_args in
261 Instr i next_name args (@of_prefancy (name_succ next_name) _ (f next_name))
262 | LetInAppIdentZZ s d r1 r2 eidc x f
263 => idc <- invert_expr.invert_Ident eidc;
264 instr_args <- @of_prefancy_ident s (tZ * tZ) idc x;
265 let i : instruction := projT1 instr_args in
266 let args : tuple name i.(num_source_regs) := projT2 instr_args in
267 Instr i next_name args (@of_prefancy (name_succ next_name) _ (f (next_name, next_name))) (* the second argument is for the carry, and it will not be read from directly. *)
268 | _ => default tt
269 end.
270 Fixpoint of_prefancy (next_name : name) {t} (e : @cexpr var t) : @expr name
271 := @of_prefancy_step of_prefancy next_name t e.
272
273 Section Proofs.
274 Context (name_eqb : name -> name -> bool).
275 Context (name_lt : name -> name -> Prop)
276 (name_lt_trans : forall n1 n2 n3,
277 name_lt n1 n2 -> name_lt n2 n3 -> name_lt n1 n3)
278 (name_lt_irr : forall n, ~ name_lt n n)
279 (name_lt_succ : forall n, name_lt n (name_succ n))
280 (name_eqb_eq : forall n1 n2, name_eqb n1 n2 = true -> n1 = n2)
281 (name_eqb_neq : forall n1 n2, name_eqb n1 n2 = false -> n1 <> n2).
282 Local Notation wordmax := (2^256).
283 Local Notation interp := (interp name_eqb wordmax cc_spec).
284 Local Notation uint256 := r[0~>wordmax-1]%zrange.
285 Local Notation uint128 := r[0~>(2 ^ (Z.log2 wordmax / 2) - 1)]%zrange.
286 Local Notation "'existZ' x" := (existT _ (type.base tZ) x) (at level 200).
287 Local Notation "'existZZ' x" := (existT _ (type.base (tZ * tZ)%etype) x) (at level 200).
288 Local Notation cinterp := API.interp.
289 Definition interp_if_Z {t} (e : cexpr t) : option Z :=
290 option_map (API.interp (t:=tZ))
291 (type.try_transport
292 _ _ tZ e).
293
294 Lemma interp_if_Z_Some {t} e r :
295 @interp_if_Z t e = Some r ->
296 exists e',
297 (type.try_transport
298 _ _ tZ e) = Some e' /\
299 API.interp (t:=tZ) e' = r.
300 Proof using Type.
301 clear. cbv [interp_if_Z option_map].
302 break_match; inversion 1; intros.
303 subst; eexists. tauto.
304 Qed.
305
306 Inductive valid_scalar
307 : @cexpr var tZ -> Prop :=
308 | valid_scalar_literal :
309 forall v n,
310 consts v = Some n ->
311 valid_scalar (expr.Ident (@ident.Literal base.type.Z v))
312 | valid_scalar_Var :
313 forall v,
314 valid_scalar (expr.App (cstZ uint256) (expr.Var v))
315 | valid_scalar_fst :
316 forall v r2,
317 valid_scalar
318 (expr.App (cstZ uint256)
319 (expr.App (expr.Ident (@ident.fst tZ
320 tZ))
321 (expr.App (cstZZ uint256 r2) (expr.Var v))))
322 .
323 Inductive valid_carry
324 : @cexpr var tZ -> Prop :=
325 | valid_carry_0 : consts 0 <> None -> valid_carry (expr.Ident (@ident.Literal base.type.Z 0))
326 | valid_carry_1 : consts 1 <> None -> valid_carry (expr.Ident (@ident.Literal base.type.Z 1))
327 | valid_carry_snd :
328 forall v r2,
329 valid_carry
330 (expr.App (cstZ r[0~>1])
331 (expr.App (expr.Ident (@ident.snd tZ
332 tZ))
333 (expr.App (cstZZ r2 r[0~>1]) (expr.Var v))))
334 .
335
336 Definition interp_base (ctx : name -> Z) (cctx : name -> bool) {t}
337 : base_var t -> base.interp t :=
338 match t as t0 return base_var t0 -> base.interp t0 with
339 | tZ => fun n => ctx n
340 | (tZ * tZ)%etype =>
341 fun v => (ctx (fst v), Z.b2z (cctx (snd v)))
342 | (_a * _b)%etype =>
343 fun _ => DefaultValue.type.base.default
344 | _ => fun _ : unit =>
345 DefaultValue.type.base.default
346 end.
347
348 Definition new_write {d} : var d -> name :=
349 match d with
350 | type.base tZ => fun r => r
351 | type.base (tZ * tZ)%etype => fst
352 | _ => fun _ => error
353 end.
354 Definition new_cc_to_name (old_cc_to_name : CC.code -> name) (i : instruction)
355 {d} (new_r : var d) (x : CC.code) : name :=
356 if (in_dec CC.code_dec x (writes_conditions i))
357 then new_write new_r
358 else old_cc_to_name x.
359
360 Inductive valid_ident
361 : forall {s d},
362 (CC.code -> name) -> (* last variables that wrote to each flag *)
363 (var d -> CC.code -> name) -> (* new last variables that wrote to each flag *)
364 ident.ident (s->d) -> @cexpr var s -> Prop :=
365 | valid_fancy_add :
366 forall r imm x y,
367 valid_scalar x ->
368 valid_scalar y ->
369 valid_ident r (new_cc_to_name r (ADD imm)) ident.fancy_add ((##256, ##imm), (x, y))%expr
370 | valid_fancy_addc :
371 forall r imm c x y,
372 (of_prefancy_scalar (t:= tZ) c = r CC.C) ->
373 valid_carry c ->
374 valid_scalar x ->
375 valid_scalar y ->
376 valid_ident r (new_cc_to_name r (ADDC imm)) ident.fancy_addc ((##256, ##imm), (c, x, y))%expr
377 | valid_fancy_sub :
378 forall r imm x y,
379 valid_scalar x ->
380 valid_scalar y ->
381 valid_ident r (new_cc_to_name r (SUB imm)) ident.fancy_sub ((##256, ##imm), (x, y))%expr
382 | valid_fancy_subb :
383 forall r imm c x y,
384 (of_prefancy_scalar (t:= tZ) c = r CC.C) ->
385 valid_carry c ->
386 valid_scalar x ->
387 valid_scalar y ->
388 valid_ident r (new_cc_to_name r (SUBC imm)) ident.fancy_subb ((##256, ##imm), (c, x, y))%expr
389 | valid_fancy_mulll :
390 forall r x y,
391 valid_scalar x ->
392 valid_scalar y ->
393 valid_ident r (new_cc_to_name r MUL128LL) ident.fancy_mulll ((##256), (x, y))%expr
394 | valid_fancy_mullh :
395 forall r x y,
396 valid_scalar x ->
397 valid_scalar y ->
398 valid_ident r (new_cc_to_name r MUL128LU) ident.fancy_mullh ((##256), (x, y))%expr
399 | valid_fancy_mulhl :
400 forall r x y,
401 valid_scalar x ->
402 valid_scalar y ->
403 valid_ident r (new_cc_to_name r MUL128UL) ident.fancy_mulhl ((##256), (x, y))%expr
404 | valid_fancy_mulhh :
405 forall r x y,
406 valid_scalar x ->
407 valid_scalar y ->
408 valid_ident r (new_cc_to_name r MUL128UU) ident.fancy_mulhh ((##256), (x, y))%expr
409 | valid_fancy_rshi :
410 forall r imm x y,
411 valid_scalar x ->
412 valid_scalar y ->
413 valid_ident r (new_cc_to_name r (RSHI imm)) ident.fancy_rshi ((##256, ##imm), (x, y))%expr
414 | valid_fancy_selc :
415 forall r c x y,
416 (of_prefancy_scalar (t:= tZ) c = r CC.C) ->
417 valid_carry c ->
418 valid_scalar x ->
419 valid_scalar y ->
420 valid_ident r (new_cc_to_name r SELC) ident.fancy_selc (c, x, y)%expr
421 | valid_fancy_selm :
422 forall r c x y,
423 (of_prefancy_scalar (t:= tZ) c = r CC.M) ->
424 valid_scalar c ->
425 valid_scalar x ->
426 valid_scalar y ->
427 valid_ident r (new_cc_to_name r SELM) ident.fancy_selm ((##256), (c, x, y))%expr
428 | valid_fancy_sell :
429 forall r c x y,
430 (of_prefancy_scalar (t:= tZ) c = r CC.L) ->
431 valid_scalar c ->
432 valid_scalar x ->
433 valid_scalar y ->
434 valid_ident r (new_cc_to_name r SELL) ident.fancy_sell (c, x, y)%expr
435 | valid_fancy_addm :
436 forall r x y m,
437 valid_scalar x ->
438 valid_scalar y ->
439 valid_scalar m ->
440 valid_ident r (new_cc_to_name r ADDM) ident.fancy_addm (x, y, m)%expr
441 .
442
443 Inductive valid_expr
444 : forall t,
445 (CC.code -> name) -> (* the last variables that wrote to each flag *)
446 @cexpr var t -> Prop :=
447 | valid_LetInZ_loosen :
448 forall s d idc r rf x f u ia,
449 valid_ident r rf idc x ->
450 0 < u < wordmax ->
451 (forall x, valid_expr _ (rf x) (f x)) ->
452 of_prefancy_ident idc x = Some ia ->
453 (forall cc ctx,
454 (forall n v, consts v = Some n -> ctx n = v) ->
455 (forall n, ctx n mod wordmax = ctx n) ->
456 let args := Tuple.map ctx (projT2 ia) in
457 spec (projT1 ia) args cc mod wordmax = spec (projT1 ia) args cc mod (u+1)) ->
458 valid_expr _ r (LetInAppIdentZ s d r[0~>u] (expr.Ident idc) x f)
459 | valid_LetInZ :
460 forall s d idc r rf x f,
461 valid_ident r rf idc x ->
462 (forall x, valid_expr _ (rf x) (f x)) ->
463 valid_expr _ r (LetInAppIdentZ s d uint256 (expr.Ident idc) x f)
464 | valid_LetInZZ :
465 forall s d idc r rf x f,
466 valid_ident r rf idc x ->
467 (forall x : var (type.base (tZ * tZ)%etype),
468 fst x = snd x ->
469 valid_expr _ (rf x) (f x)) ->
470 valid_expr _ r (LetInAppIdentZZ s d uint256 r[0~>1] (expr.Ident idc) x f)
471 | valid_Ret :
472 forall r x,
473 valid_scalar x ->
474 valid_expr _ r x
475 .
476
477 Lemma wordmax_nonneg : 0 <= wordmax.
478 Proof using Type. cbv; congruence. Qed.
479
480 Lemma of_prefancy_scalar_correct'
481 (e1 : @cexpr var (type.base tZ))
482 (e2 : cexpr (type.base tZ))
483 G (ctx : name -> Z) (cctx : name -> bool) :
484 valid_scalar e1 ->
485 API.wf G e1 e2 ->
486 (forall n v, consts v = Some n -> In (existZ (n, v)) G) ->
487 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cctx v1 = v2) ->
488 (forall v1 v2, In (existZ (v1, v2)) G -> ctx v1 = v2) -> (* implied by above *)
489 (forall n, ctx n mod wordmax = ctx n) ->
490 (forall v1 v2, In (existZZ (v1, v2)) G -> ctx (fst v1) = fst v2) ->
491 (forall v1 v2, In (existZZ (v1, v2)) G -> Z.b2z (cctx (snd v1)) = snd v2) ->
492 ctx (of_prefancy_scalar e1) = cinterp e2.
493 Proof using Type.
494 inversion 1; inversion 1;
495 cbv [interp_if_Z option_map];
496 cbn [of_prefancy_scalar interp_base]; intros.
497 all: repeat first [
498 progress subst
499 | exfalso; assumption
500 | progress inversion_sigma
501 | progress inversion_option
502 | progress inversion_prod
503 | progress destruct_head'_sig
504 | progress destruct_head'_and
505 | progress inversion_type
506 | progress cbn [fst snd upper lower fst snd eq_rect projT1 projT2 eq_rect type.decode f_equal f_equal2] in *
507 | progress cbn [expr.interp ident.interp type.interp base.interp base.base_interp] in *
508 | progress cbv [ident.cast2 ident.literal] in *
509 | progress destruct_head'_prod
510 | progress expr.inversion_expr
511 | progress expr.inversion_wf_one_constr
512 | progress expr.invert_subst
513 | break_innermost_match_hyps_step
514 | progress break_innermost_match
515 | match goal with H : context [_ = cinterp _] |- context [cinterp _] =>
516 rewrite <-H by eauto; try reflexivity end
517 | solve [eauto using (f_equal2 pair), cast_oor_id, wordmax_nonneg]
518 | rewrite ident.cast_out_of_bounds_simple_0_mod
519 | rewrite Z.mod_mod by lia
520 | rewrite cast_oor_mod by (cbv; congruence)
521 | lia
522 | match goal with
523 H : context[ ?x mod _ = ?x ] |- _ => rewrite H end
524 | match goal with
525 | H : context [In _ _ -> _ = _] |- _ => erewrite H by eauto end
526 | match goal with
527 | H : forall v1 v2, In _ _ -> ?ctx v1 = v2 |- ?x = ?x mod ?m =>
528 replace m with wordmax by ring; erewrite <-(H _ x) by eauto; solve [eauto]
529 end
530 | match goal with
531 | H : forall v1 v2, In _ _ -> ?ctx (fst v1) = fst v2,
532 H' : In (existZZ (_,(?x,?y))) _ |- ?x = ?x mod ?m =>
533 replace m with wordmax by ring;
534 specialize (H _ _ H'); cbn [fst] in H; rewrite <-H; solve [eauto] end
535 ].
536 Qed.
537
538 Lemma of_prefancy_scalar_correct
539 (e1 : @cexpr var (type.base tZ))
540 (e2 : cexpr (type.base tZ))
541 G (ctx : name -> Z) cc :
542 valid_scalar e1 ->
543 API.wf G e1 e2 ->
544 (forall n v, consts v = Some n -> In (existZ (n, v)) G) ->
545 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cc v1 = v2) ->
546 (forall n, ctx n mod wordmax = ctx n) ->
547 ctx (of_prefancy_scalar e1) = cinterp e2.
548 Proof using Type.
549 intros; match goal with H : context [interp_base _ _ _ = _] |- _ =>
550 pose proof (H tZ);
551 pose proof (H (tZ * tZ)%etype); cbn [interp_base] in *
552 end.
553 eapply of_prefancy_scalar_correct'; eauto;
554 match goal with
555 | H : forall _ _, In _ _ -> (_, _) = _ |- _ =>
556 let v1 := fresh "v" in
557 let v2 := fresh "v" in
558 intros v1 v2 ?; rewrite <-(H v1 v2) by auto
559 end; reflexivity.
560 Qed.
561
562 Lemma of_prefancy_ident_Some_gen {s d} idc r rf x:
563 @valid_ident s d r rf idc x ->
564 match s, d return ident (s -> d) -> cexpr s -> Prop with
565 | type.base s, type.base d
566 => fun idc x => of_prefancy_ident idc x <> None
567 | _, _ => fun idc x => False
568 end idc x.
569 Proof using Type.
570 destruct 1; intros;
571 repeat first [
572 progress HProp.eliminate_hprop_eq
573 | progress subst
574 | progress cbn [GallinaReify.base.reify ident.ident_Literal ident.buildIdent] in *
575 | progress cbn [projT1 projT2 eq_rect of_prefancy_ident option_map] in *
576 | progress cbv [ident.literal] in *
577 | progress inversion_sigma
578 | progress Z.ltb_to_lt
579 | progress break_innermost_match
580 | congruence
581 ].
582 Qed.
583
584 Lemma of_prefancy_ident_Some {s d} idc r rf x:
585 @valid_ident (type.base s) (type.base d) r rf idc x ->
586 of_prefancy_ident idc x <> None.
587 Proof using Type. exact (@of_prefancy_ident_Some_gen _ _ idc r rf x). Qed.
588
589 Ltac name_eqb_to_eq :=
590 repeat match goal with
591 | H : name_eqb _ _ = true |- _ => apply name_eqb_eq in H
592 | H : name_eqb _ _ = false |- _ => apply name_eqb_neq in H
593 end.
594 Ltac inversion_of_prefancy_ident :=
595 match goal with
596 | H : of_prefancy_ident _ _ = None |- _ =>
597 eapply of_prefancy_ident_Some in H;
598 [ contradiction | eassumption]
599 end.
600
601 Local Ltac hammer :=
602 repeat first [
603 progress subst
604 | progress inversion_sigma
605 | progress inversion_option
606 | progress inversion_of_prefancy_ident
607 | progress inversion_prod
608 | progress cbv [id]
609 | progress cbn [eq_rect projT1 projT2 expr.interp ident.interp interp_base Coercions.base Coercions.type_base interp interp_if_Z option_map] in *
610 | progress cbv [ident.cast2 ident.literal] in *
611 | progress ident.fancy.cbv_fancy_in_all
612 | progress cbn [invert_expr.invert_Ident] in * (* N.B. Must be above [break_innermost_match] for proofs below to work *)
613 | progress Language.Inversion.Compilers.type_beq_to_eq
614 | progress name_eqb_to_eq
615 | progress Language.Inversion.Compilers.rewrite_type_transport_correct
616 | progress HProp.eliminate_hprop_eq
617 | progress break_innermost_match_hyps
618 | progress break_innermost_match
619 | progress inversion_type
620 | progress expr.invert_subst
621 | progress Language.Inversion.Compilers.expr.inversion_expr
622 | solve [auto]
623 | contradiction
624 ].
625 Ltac prove_Ret :=
626 repeat match goal with
627 | H : valid_scalar (expr.LetIn _ _) |- _ =>
628 inversion H
629 | _ => progress cbn [id of_prefancy of_prefancy_step of_prefancy_scalar]
630 | _ => progress hammer
631 | H : valid_scalar (expr.Ident _) |- _ =>
632 inversion H; clear H
633 | |- _ = cinterp ?f (cinterp ?x) =>
634 transitivity
635 (cinterp (f @ x)%expr);
636 [ | reflexivity ];
637 erewrite <-of_prefancy_scalar_correct by (try reflexivity; eassumption)
638 end.
639
640 Lemma cast_mod u v :
641 0 <= u ->
642 ident.cast r[0~>u] v = v mod (u + 1).
643 Proof using Type. apply ident.cast_out_of_bounds_simple_0_mod. Qed.
644
645 Lemma cc_spec_c v :
646 Z.b2z (cc_spec CC.C v) = (v / wordmax) mod 2.
647 Proof using Type. cbv [cc_spec]; apply Z.testbit_spec'. lia. Qed.
648
649 Lemma cc_m_zselect x z nz :
650 x mod wordmax = x ->
651 (if (if cc_spec CC.M x then 1 else 0) =? 1 then nz else z) =
652 Z.zselect (x >> 255) z nz.
653 Proof using Type.
654 intro Hx_small.
655 transitivity (if (Z.b2z (cc_spec CC.M x) =? 1) then nz else z); [ reflexivity | ].
656 cbv [cc_spec Z.zselect].
657 rewrite Z.testbit_spec', Z.shiftr_div_pow2 by lia. rewrite <-Hx_small.
658 rewrite Z.div_between_0_if by (try replace (2 * (2 ^ 255)) with wordmax by reflexivity;
659 auto with zarith).
660 break_innermost_match; Z.ltb_to_lt; try rewrite Z.mod_small in * by lia; congruence.
661 Qed.
662
663 Lemma cc_l_zselect x z nz :
664 (if (if cc_spec CC.L x then 1 else 0) =? 1 then nz else z) = Z.zselect (x &' 1) z nz.
665 Proof using Type.
666 transitivity (if (Z.b2z (cc_spec CC.L x) =? 1) then nz else z); [ reflexivity | ].
667 transitivity (Z.zselect (x &' Z.ones 1) z nz); [ | reflexivity ].
668 cbv [cc_spec Z.zselect]. rewrite Z.testbit_spec', Z.land_ones by lia.
669 autorewrite with zsimplify_fast. rewrite Zmod_even.
670 break_innermost_match; Z.ltb_to_lt; congruence.
671 Qed.
672
673 Lemma b2z_range b : 0<= Z.b2z b < 2.
674 Proof using Type. cbv [Z.b2z]. break_match; lia. Qed.
675
676
677 Lemma of_prefancy_scalar_carry
678 (c : @cexpr var (type.base tZ))
679 (e : cexpr (type.base tZ))
680 G (ctx : name -> Z) cctx :
681 valid_carry c ->
682 API.wf G c e ->
683 (forall n0, consts 0 = Some n0 -> cctx n0 = false) ->
684 (forall n1, consts 1 = Some n1 -> cctx n1 = true) ->
685 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cctx v1 = v2) ->
686 Z.b2z (cctx (of_prefancy_scalar c)) = cinterp e.
687 Proof using Type.
688 destruct 1; intros; hammer; cbn;
689 repeat match goal with
690 | H : context [ _ = false] |- Z.b2z _ = 0 => rewrite H; reflexivity
691 | H : context [ _ = true] |- Z.b2z _ = 1 => rewrite H; reflexivity
692 | _ => progress Compilers.expr.inversion_wf_one_constr
693 | _ => progress cbn [fst snd]
694 | _ => progress destruct_head'_sig
695 | _ => progress destruct_head'_and
696 | _ => progress hammer
697 | _ => progress Language.Inversion.Compilers.expr.invert_subst
698 | _ => rewrite cast_mod by (cbv; congruence)
699 | _ => rewrite Z.mod_mod by lia
700 | _ => rewrite Z.mod_small by apply b2z_range
701 | H : (forall _ _ _, In _ _ -> interp_base _ _ _ = _),
702 H' : In (existZZ (?v, _)) _ |- context [cctx (snd ?v)] =>
703 specialize (H _ _ _ H'); cbn in H
704 end.
705 Qed.
706
707 Ltac hammer_wf :=
708 repeat first [ progress subst
709 | progress cbn [eq_rect fst snd projT1 projT2] in *
710 | progress destruct_head'_False
711 | progress expr.inversion_wf_one_constr
712 | progress expr.invert_subst
713 | progress destruct_head'_and
714 | progress destruct_head'_sig
715 | break_innermost_match_hyps_step ].
716
717 Ltac simplify_ident :=
718 repeat match goal with
719 | _ => progress inversion_option
720 | _ => progress hammer_wf
721 | _ => progress intros
722 | _ => progress cbn [fst snd of_prefancy_ident] in *
723 | H : upper _ = _ |- _ => rewrite H
724 | _ => rewrite cc_spec_c by auto
725 | _ => rewrite cast_mod by (cbv; congruence)
726 | H : ?P, H' : ?P |- _ => clear H'
727 | _ => progress hammer
728 end.
729
730 (* TODO: zero flag is a little tricky, since the value
731 depends both on the stored variable and the carry if there
732 is one. For now, since Barrett doesn't use it, we're just
733 pretending it doesn't exist. *)
734 Definition cc_good cc cctx ctx r :=
735 CC.cc_c cc = cctx (r CC.C) /\
736 CC.cc_m cc = cc_spec CC.M (ctx (r CC.M)) /\
737 CC.cc_l cc = cc_spec CC.L (ctx (r CC.L)) /\
738 (forall n0 : name, consts 0 = Some n0 -> cctx n0 = false) /\
739 (forall n1 : name, consts 1 = Some n1 -> cctx n1 = true).
740
741 Lemma of_prefancy_identZ_loosen_correct_gen {s d} idc:
742 forall (x : @cexpr var _) i ctx G cc cctx x2 r rf f u,
743 @valid_ident s d r rf idc x ->
744 match s, d return ident (s -> d) -> cexpr s -> (var d -> _ -> _) -> Prop with
745 | type.base s, type.base tZ
746 => fun idc x rf
747 => API.wf G (#idc @ x)%expr x2 ->
748 API.wf G (cstZ r[0~>u]) f ->
749 0 < u < wordmax ->
750 cc_good cc cctx ctx r ->
751 (forall n v, consts v = Some n -> In (existZ (n, v)) G) ->
752 (forall t (v1 : var (type.base t)) v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cctx v1 = v2) ->
753 (forall n, ctx n mod wordmax = ctx n) ->
754 of_prefancy_ident idc x = Some i ->
755 (spec (projT1 i) (Tuple.map ctx (projT2 i)) cc mod wordmax = spec (projT1 i) (Tuple.map ctx (projT2 i)) cc mod (u+1)) ->
756 spec (projT1 i) (Tuple.map ctx (projT2 i)) cc mod wordmax = (cinterp f (cinterp x2))
757 | _, _ => fun _ _ _ => True
758 end idc x rf.
759 Proof using Type.
760 destruct 1; try exact I; cbn [of_prefancy_ident]; cbn [GallinaReify.base.reify ident.ident_Literal ident.buildIdent] in *; hammer; intros; inversion_option; (simplify_ident; [ ]).
761 all:
762 rewrite cast_mod by lia;
763 match goal with
764 | H : context [spec _ _ _ mod _ = _] |- ?x mod wordmax = _ mod ?m =>
765 replace (x mod wordmax) with (x mod m) by auto
766 end.
767 all: cbn - [Z.shiftl wordmax]; cbv [cc_good] in *; destruct_head'_and;
768 repeat match goal with
769 | H : CC.cc_c _ = _ |- _ => rewrite H
770 | H : CC.cc_m _ = _ |- _ => rewrite H
771 | H : CC.cc_l _ = _ |- _ => rewrite H
772 | H : CC.cc_z _ = _ |- _ => rewrite H
773 | H: of_prefancy_scalar _ = ?r ?c |- _ => rewrite <-H
774 | _ => progress rewrite ?cc_m_zselect, ?cc_l_zselect by auto
775 | _ => progress rewrite ?Z.add_modulo_correct, ?Z.geb_leb by auto
776 | |- context [cinterp ?x] =>
777 erewrite of_prefancy_scalar_correct with (e2:=x) by eauto
778 | |- context [cinterp ?x] =>
779 erewrite <-of_prefancy_scalar_carry with (e:=x) by eauto
780 | |- context [if _ (of_prefancy_scalar _) then _ else _ ] =>
781 cbv [Z.zselect Z.b2z];
782 break_innermost_match; Z.ltb_to_lt; try reflexivity;
783 congruence
784 end; try reflexivity.
785
786 { (* RSHI case *)
787 cbv [Z.rshi].
788 rewrite Z.land_ones, Z.shiftl_mul_pow2 by (cbv; congruence).
789 change (2 ^ Z.log2 wordmax) with wordmax.
790 break_innermost_match; try congruence; [ ]. autorewrite with zsimplify_fast.
791 repeat (f_equal; try ring). }
792 Qed.
793 Lemma of_prefancy_identZ_loosen_correct {s} idc:
794 forall (x : @cexpr var _) i ctx G cc cctx x2 r rf f u,
795 @valid_ident (type.base s) tZ r rf idc x ->
796 API.wf G (#idc @ x)%expr x2 ->
797 API.wf G (cstZ r[0~>u]) f ->
798 0 < u < wordmax ->
799 cc_good cc cctx ctx r ->
800 (forall n v, consts v = Some n -> In (existZ (n, v)) G) ->
801 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cctx v1 = v2) ->
802 (forall n, ctx n mod wordmax = ctx n) ->
803 of_prefancy_ident idc x = Some i ->
804 (spec (projT1 i) (Tuple.map ctx (projT2 i)) cc mod wordmax = spec (projT1 i) (Tuple.map ctx (projT2 i)) cc mod (u+1)) ->
805 spec (projT1 i) (Tuple.map ctx (projT2 i)) cc mod wordmax = (cinterp f (cinterp x2)).
806 Proof using Type. apply (@of_prefancy_identZ_loosen_correct_gen (type.base s) (type.base tZ)). Qed.
807 Lemma of_prefancy_identZ_correct {s} idc:
808 forall (x : @cexpr var _) i ctx G cc cctx x2 r rf f,
809 @valid_ident (type.base s) tZ r rf idc x ->
810 API.wf G (#idc @ x)%expr x2 ->
811 API.wf G (cstZ uint256) f ->
812 cc_good cc cctx ctx r ->
813 (forall n v, consts v = Some n -> In (existZ (n, v)) G) ->
814 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cctx v1 = v2) ->
815 (forall n, ctx n mod wordmax = ctx n) ->
816 of_prefancy_ident idc x = Some i ->
817 spec (projT1 i) (Tuple.map ctx (projT2 i)) cc mod wordmax = (cinterp f (cinterp x2)).
818 Proof using Type.
819 intros; eapply of_prefancy_identZ_loosen_correct; try eassumption; [ | ].
820 { cbn; lia. } { intros; f_equal; ring. }
821 Qed.
822 Lemma of_prefancy_identZZ_correct' {s} idc:
823 forall (x : @cexpr var _) i ctx G cc cctx x2 r rf f,
824 @valid_ident (type.base s) (tZ * tZ) r rf idc x ->
825 API.wf G (#idc @ x)%expr x2 ->
826 API.wf G (cstZZ uint256 r[0~>1]) f ->
827 cc_good cc cctx ctx r ->
828 (forall n v, consts v = Some n -> In (existZ (n, v)) G) ->
829 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cctx v1 = v2) ->
830 (forall n, ctx n mod wordmax = ctx n) ->
831 of_prefancy_ident idc x = Some i ->
832 spec (projT1 i) (Tuple.map ctx (projT2 i)) cc mod wordmax = fst (cinterp f (cinterp x2)) /\
833 Z.b2z (cc_spec CC.C (spec (projT1 i) (Tuple.map ctx (projT2 i)) cc)) = snd (cinterp f (cinterp x2)).
834 Proof using Type.
835 inversion 1; inversion 1; cbn [of_prefancy_ident]; intros; hammer; (simplify_ident; [ ]);
836 cbn - [Z.div Z.modulo]; cbv [Z.sub_with_borrow Z.add_with_carry];
837 cbv [cc_good] in *; destruct_head'_and; autorewrite with zsimplify_fast.
838 all: repeat match goal with
839 | H : CC.cc_c _ = _ |- _ => rewrite H
840 | H: of_prefancy_scalar _ = ?r ?c |- _ => rewrite <-H
841 | H : API.wf _ ?x ?e |- context [cinterp ?e] =>
842 erewrite <-of_prefancy_scalar_correct with (e1:=x) (e2:=e) by eauto
843 | H : API.wf _ ?x ?e2 |- context [cinterp ?e2] =>
844 erewrite <-of_prefancy_scalar_carry with (c:=x) (e:=e2) by eauto
845 end.
846 all: match goal with |- context [(?x << ?n) mod ?m] =>
847 pose proof (Z.mod_pos_bound (x << n) m ltac:(lia)) end.
848 all:repeat match goal with
849 | |- context [if _ (of_prefancy_scalar _) then _ else _ ] =>
850 cbv [Z.zselect Z.b2z]; break_innermost_match; Z.ltb_to_lt; try congruence; [ | ]
851 | _ => rewrite Z.add_opp_r
852 | _ => rewrite Z.div_sub_small by auto with zarith
853 | H : forall n, ?ctx n mod wordmax = ?ctx n |- context [?ctx ?m - _] => rewrite <-(H m)
854 | |- ((?x - ?y - ?c) / _) mod _ = - ((- ?c + ?x - ?y) / _) mod _ =>
855 replace (-c + x - y) with (x - (y + c)) by ring; replace (x - y - c) with (x - (y + c)) by ring
856 | _ => split
857 | _ => try apply (f_equal2 Z.modulo); try apply (f_equal2 Z.div); ring
858 | _ => break_innermost_match; reflexivity
859 end.
860 Qed.
861 Lemma of_prefancy_identZZ_correct {s} idc:
862 forall (x : @cexpr var _) i ctx G cc cctx x2 r rf f,
863 @valid_ident (type.base s) (tZ * tZ) r rf idc x ->
864 API.wf G (#idc @ x)%expr x2 ->
865 API.wf G (cstZZ uint256 r[0~>1]) f ->
866 cc_good cc cctx ctx r ->
867 (forall n v, consts v = Some n -> In (existZ (n, v)) G) ->
868 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cctx v1 = v2) ->
869 (forall n, ctx n mod wordmax = ctx n) ->
870 of_prefancy_ident idc x = Some i ->
871 spec (projT1 i) (Tuple.map ctx (projT2 i)) cc mod wordmax = fst (cinterp f (cinterp x2)).
872 Proof using Type. apply of_prefancy_identZZ_correct'. Qed.
873 Lemma of_prefancy_identZZ_correct_carry {s} idc:
874 forall (x : @cexpr var _) i ctx G cc cctx x2 r rf f,
875 @valid_ident (type.base s) (tZ * tZ) r rf idc x ->
876 API.wf G (#idc @ x)%expr x2 ->
877 API.wf G (cstZZ uint256 r[0~>1]) f ->
878 cc_good cc cctx ctx r ->
879 (forall n v, consts v = Some n -> In (existZ (n, v)) G) ->
880 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cctx v1 = v2) ->
881 (forall n, ctx n mod wordmax = ctx n) ->
882 of_prefancy_ident idc x = Some i ->
883 Z.b2z (cc_spec CC.C (spec (projT1 i) (Tuple.map ctx (projT2 i)) cc)) = snd (cinterp f (cinterp x2)).
884 Proof using Type. apply of_prefancy_identZZ_correct'. Qed.
885
886 Lemma identZZ_writes {s} idc r rf x:
887 @valid_ident (type.base s) (tZ * tZ) r rf idc x ->
888 forall i, of_prefancy_ident idc x = Some i ->
889 In CC.C (writes_conditions (projT1 i)).
890 Proof using Type.
891 inversion 1;
892 repeat match goal with
893 | _ => progress intros
894 | _ => progress cbn [of_prefancy_ident writes_conditions ADD ADDC SUB SUBC In] in *
895 | _ => progress hammer; Z.ltb_to_lt
896 | _ => congruence
897 end.
898 Qed.
899
900 (* Common side conditions for cases in of_prefancy_correct *)
901 Local Ltac side_cond :=
902 repeat match goal with
903 | _ => progress intros
904 | _ => progress cbn [In fst snd] in *
905 | H : _ \/ _ |- _ => destruct H
906 | [H : forall _ _, In _ ?l -> _, H' : In _ ?l |- _] =>
907 let H'' := fresh in
908 pose proof H'; apply H in H''; clear H
909 | H : name_lt ?n ?n |- _ =>
910 specialize (name_lt_irr n); contradiction
911 | _ => progress hammer
912 | _ => solve [eauto]
913 end.
914
915 Lemma interp_base_helper G next_name ctx cctx :
916 (forall n v2, In (existZ (n, v2)) G -> name_lt n next_name) ->
917 (forall n v2, In (existZZ (n, v2)) G -> name_lt (fst n) next_name) ->
918 (forall n v2, In (existZZ (n, v2)) G -> fst n = snd n) ->
919 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cctx v1 = v2) ->
920 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G ->
921 t = tZ
922 \/ t = (tZ * tZ)%etype) ->
923 forall t v1 v2 x xc,
924 In (existT (fun t : type => (var t * type.interp base.interp t)%type) (type.base t) (v1, v2)%zrange)
925 ((existZ (next_name, x)%zrange) :: G) ->
926 interp_base (fun n : name => if name_eqb n next_name then x else ctx n)
927 (fun n : name => if name_eqb n next_name then xc else cctx n) v1 = v2.
928 Proof using name_eqb_eq name_eqb_neq name_lt_irr.
929 intros.
930 repeat match goal with
931 | H: In _ (_ :: _) |- _ => cbn [In] in H; destruct H; [ solve [side_cond] | ]
932 | H : (forall t _ _, In _ ?G -> (t = _ \/ t = _)), H' : In _ ?G |- _ =>
933 destruct (H _ _ _ H'); subst t
934 | H : forall _ _ _, In _ ?G -> interp_base _ _ _ = _, H' : In _ G |- _ => specialize (H _ _ _ H')
935 end; side_cond.
936 Qed.
937
938 Lemma name_eqb_refl n : name_eqb n n = true.
939 Proof using name_eqb_eq name_eqb_neq. case_eq (name_eqb n n); intros; name_eqb_to_eq; auto. Qed.
940
941 Lemma valid_ident_new_cc_to_name s d r rf idc x y n :
942 @valid_ident (type.base s) (type.base d) r rf idc x ->
943 of_prefancy_ident idc x = Some y ->
944 rf n = new_cc_to_name r (projT1 y) n.
945 Proof using Type. inversion 1; intros; hammer; simplify_ident. Qed.
946
947 Lemma new_cc_to_name_Z_cases r i n x :
948 new_cc_to_name (d:=tZ) r i n x
949 = if in_dec CC.code_dec x (writes_conditions i)
950 then n else r x.
951 Proof using Type. reflexivity. Qed.
952 Lemma new_cc_to_name_ZZ_cases r i n x :
953 new_cc_to_name (d:=tZ * tZ) r i n x
954 = if in_dec CC.code_dec x (writes_conditions i)
955 then fst n else r x.
956 Proof using Type. reflexivity. Qed.
957
958 Lemma cc_good_helper cc cctx ctx r i x next_name :
959 (forall c, name_lt (r c) next_name) ->
960 (forall n v, consts v = Some n -> name_lt n next_name) ->
961 cc_good cc cctx ctx r ->
962 cc_good (CC.update (writes_conditions i) x cc_spec cc)
963 (fun n : name =>
964 if name_eqb n next_name
965 then CC.cc_c (CC.update (writes_conditions i) x cc_spec cc)
966 else cctx n)
967 (fun n : name => if name_eqb n next_name then x mod wordmax else ctx n)
968 (new_cc_to_name (d:=tZ) r i next_name).
969 Proof using name_eqb_eq name_eqb_neq name_lt_irr.
970 cbv [cc_good]; intros; destruct_head'_and.
971 rewrite !new_cc_to_name_Z_cases.
972 cbv [CC.update CC.cc_c CC.cc_m CC.cc_l CC.cc_z].
973 repeat match goal with
974 | _ => split; intros
975 | _ => progress hammer
976 | H : forall c, name_lt (r c) (r ?c2) |- _ => specialize (H c2)
977 | H : (forall n v, consts v = Some n -> name_lt _ _),
978 H' : consts _ = Some _ |- _ => specialize (H _ _ H')
979 | H : name_lt ?n ?n |- _ => apply name_lt_irr in H; contradiction
980 | _ => cbv [cc_spec]; rewrite Z.mod_pow2_bits_low by lia
981 | _ => congruence
982 end.
983 Qed.
984
985 Lemma of_prefancy_correct
986 {t} (e1 : @cexpr var t) (e2 : @cexpr _ t) r :
987 valid_expr _ r e1 ->
988 forall G,
989 API.wf G e1 e2 ->
990 forall ctx cc cctx,
991 cc_good cc cctx ctx r ->
992 (forall n v, consts v = Some n -> In (existZ (n, v)) G) ->
993 (forall n v2, In (existZZ (n, v2)) G -> fst n = snd n) ->
994 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G -> interp_base ctx cctx v1 = v2) ->
995 (forall t v1 v2, In (existT _ (type.base t) (v1, v2)) G ->
996 t = tZ
997 \/ t = (tZ * tZ)%etype) ->
998 (forall n, ctx n mod wordmax = ctx n) ->
999 forall next_name result,
1000 (forall c : CC.code, name_lt (r c) next_name) ->
1001 (forall n v2, In (existZ (n, v2)) G -> name_lt n next_name) ->
1002 (forall n v2, In (existZZ (n, v2)) G -> name_lt (fst n) next_name) ->
1003 (interp_if_Z e2 = Some result) ->
1004 interp (@of_prefancy next_name t e1) cc ctx = result.
1005 Proof using name_eqb_eq name_eqb_neq name_lt_irr name_lt_succ name_lt_trans.
1006 induction 1; inversion 1; cbv [interp_if_Z];
1007 cbn [of_prefancy of_prefancy_step]; intros;
1008 match goal with H : context [interp_base _ _ _ = _] |- _ =>
1009 pose proof (H tZ) end;
1010 try solve [prove_Ret]; [ | | ]; hammer;
1011 match goal with
1012 | H : context [interp (of_prefancy _ _) _ _ = _]
1013 |- interp _ ?cc' ?ctx' = _ =>
1014 match goal with
1015 | _ : context [LetInAppIdentZ _ _ _ _ _ _] |- _=>
1016 erewrite H with
1017 (G := (existZ (next_name, ctx' next_name)) :: G)
1018 (e2 := _ (ctx' next_name))
1019 (cctx := (fun n => if name_eqb n next_name then CC.cc_c cc' else cctx n))
1020 | _ : context [LetInAppIdentZZ _ _ _ _ _ _ _] |- _=>
1021 erewrite H with
1022 (G := (existZZ ((next_name, next_name), (ctx' next_name, Z.b2z (CC.cc_c cc')))) :: G)
1023 (e2 := _ (ctx' next_name, Z.b2z (CC.cc_c cc')))
1024 (cctx := (fun n => if name_eqb n next_name then CC.cc_c cc' else cctx n))
1025 end
1026 end;
1027 repeat match goal with
1028 | _ => progress intros
1029 | _ => rewrite name_eqb_refl in *
1030 | _ => rewrite Z.testbit_spec' in *
1031 | _ => erewrite valid_ident_new_cc_to_name by eassumption
1032 | _ => rewrite new_cc_to_name_Z_cases
1033 | _ => rewrite new_cc_to_name_ZZ_cases
1034 | _ => solve [intros; eapply interp_base_helper; side_cond]
1035 | _ => solve [intros; apply cc_good_helper; eauto]
1036 | _ => reflexivity
1037 | _ => solve [eauto using Z.mod_small, b2z_range]
1038 | _ => progress autorewrite with zsimplify_fast
1039 | _ => progress side_cond
1040 end; [ | | ].
1041 { cbn - [cc_spec]; cbv [id]; cbn - [cc_spec].
1042 inversion wf_x; hammer.
1043 erewrite @of_prefancy_identZ_loosen_correct by eauto.
1044 reflexivity. }
1045 { cbn - [cc_spec]; cbv [id]; cbn - [cc_spec].
1046 inversion wf_x; hammer.
1047 erewrite of_prefancy_identZ_correct by eassumption.
1048 reflexivity. }
1049 { cbn - [cc_spec]; cbv [id]; cbn - [cc_spec].
1050 match goal with H : _ |- _ => pose proof H; eapply identZZ_writes in H; [ | eassumption] end.
1051 inversion wf_x; hammer.
1052 erewrite of_prefancy_identZZ_correct by eassumption.
1053 erewrite of_prefancy_identZZ_correct_carry by eassumption.
1054 rewrite <-surjective_pairing. reflexivity. }
1055 Qed.
1056 End Proofs.
1057 End of_prefancy.
1058
1059 Section allocate_registers.
1060 Context (reg name : Type) (name_eqb : name -> name -> bool) (error : reg).
1061 Fixpoint allocate (e : @expr name) (reg_list : list reg) (name_to_reg : name -> reg) : @expr reg :=
1062 match e with
1063 | Ret n => Ret (name_to_reg n)
1064 | Instr i rd args cont =>
1065 match reg_list with
1066 | r :: reg_list' => Instr i r (Tuple.map name_to_reg args) (allocate cont reg_list' (fun n => if name_eqb n rd then r else name_to_reg n))
1067 | nil => Ret error
1068 end
1069 end.
1070 End allocate_registers.
1071
1072 Definition test_prog : @expr positive :=
1073 Instr (ADD (128)) 3%positive (1, 2)%positive
1074 (Instr (ADDC 0) 4%positive (3,1)%positive
1075 (Ret 4%positive)).
1076
1077 Definition x1 := 2^256 - 1.
1078 Definition x2 := 2^128 - 1.
1079 Definition wordmax := 2^256.
1080 Definition expected :=
1081 let r3' := (x1 + (x2 << 128)) in
1082 let r3 := r3' mod wordmax in
1083 let c := r3' / wordmax in
1084 let r4' := (r3 + x1 + c) in
1085 r4' mod wordmax.
1086 Definition actual :=
1087 interp Pos.eqb
1088 (2^256) cc_spec test_prog {|CC.cc_c:=false; CC.cc_m:=false; CC.cc_l:=false; CC.cc_z:=false|}
1089 (fun n => if Pos.eqb n 1%positive
1090 then x1
1091 else if Pos.eqb n 2%positive
1092 then x2
1093 else 0).
1094 Lemma test_prog_ok : expected = actual.
1095 Proof. reflexivity. Qed.
1096
1097 Definition of_Expr {t} next_name (consts : Z -> option positive)
1098 (e : expr.Expr t)
1099 (x : type.for_each_lhs_of_arrow (var positive) t)
1100 : positive -> @expr positive :=
1101 fun error =>
1102 @of_prefancy positive Pos.succ error consts next_name _ (invert_expr.smart_App_curried (e _) x).
1103
1104 Section Proofs.
1105 Fixpoint var_pairs {t var1 var2}
1106 : type.for_each_lhs_of_arrow var1 t
1107 -> type.for_each_lhs_of_arrow var2 t
1108 -> list {t : Compilers.type base.type.type & (var1 t * var2 t)%type } :=
1109 match t as t0 return
1110 (type.for_each_lhs_of_arrow var1 t0
1111 -> type.for_each_lhs_of_arrow var2 t0 -> _) with
1112 | type.base _ => fun _ _ => nil
1113 | type.arrow s d =>
1114 fun x1 x2 =>
1115 existT _ _ (fst x1, fst x2) :: var_pairs (snd x1) (snd x2)
1116 end.
1117
1118 Local Notation tZ := (base.type.type_base base.type.Z).
1119 Local Notation existZ := (existT _ (type.base tZ)).
1120 Local Notation existZZ := (existT _ (type.base (tZ * tZ)%etype)).
1121
1122 Fixpoint make_ctx (var_list : list (positive * Z)) : positive -> Z :=
1123 match var_list with
1124 | [] => fun _ => 0
1125 | (n, v) :: l' => fun m => if (m =? n)%positive then v else make_ctx l' m
1126 end.
1127
1128 Definition make_pairs :
1129 list (positive * Z) -> list {t : Compilers.type base.type.type & (var positive t * @type.interp base.type base.interp t)%type } := List.map (fun x => existZ x).
1130
1131 Fixpoint make_consts (consts_list : list (positive * Z)) : Z -> option positive :=
1132 match consts_list with
1133 | [] => fun _ => None
1134 | (n, v) :: l' => fun x => if x =? v then Some n else make_consts l' x
1135 end.
1136
1137 Local Ltac ez :=
1138 repeat match goal with
1139 | _ => progress intros
1140 | _ => progress subst
1141 | H : _ \/ _ |- _ => destruct H
1142 | H : _ |- _ => rewrite Pos.eqb_eq in H
1143 | H : _ |- _ => rewrite Pos.eqb_neq in H
1144 | _ => progress break_innermost_match
1145 | _ => progress break_match_hyps
1146 | _ => progress inversion_sigma
1147 | _ => progress inversion_option
1148 | _ => progress inversion_prod
1149 | _ => progress HProp.eliminate_hprop_eq
1150 | _ => progress Z.ltb_to_lt
1151 | _ => reflexivity
1152 | _ => congruence
1153 | _ => solve [eauto]
1154 end.
1155
1156
1157 Lemma make_consts_ok consts_list n v :
1158 make_consts consts_list v = Some n ->
1159 In (existZ (n, v)%zrange) (make_pairs consts_list).
1160 Proof.
1161 cbv [make_pairs]; induction consts_list as [|[ ? ? ] ?]; cbn; ez.
1162 Qed.
1163
1164 Lemma make_pairs_ok consts_list:
1165 forall v1 v2,
1166 In (existZ (v1, v2)%zrange) (make_pairs consts_list) ->
1167 In (v1, v2) consts_list.
1168 Proof.
1169 cbv [make_pairs]. induction consts_list as [| [ n v ] ? ]; cbn; [ tauto | ]. ez.
1170 Qed.
1171 Lemma make_ctx_ok consts_list:
1172 (forall n v1 v2, In (n, v1) consts_list ->
1173 In (n, v2) consts_list -> v1 = v2) ->
1174 forall n v,
1175 In (n, v) consts_list ->
1176 make_ctx consts_list n = v.
1177 Proof.
1178 induction consts_list as [| [ n v ] ? ]; cbn; [ tauto | ].
1179 repeat match goal with
1180 | _ => progress cbn [eq_rect fst snd] in *
1181 | _ => progress ez
1182 end.
1183 Qed.
1184
1185 Lemma make_ctx_cases consts_list n :
1186 make_ctx consts_list n = 0 \/
1187 In (n, make_ctx consts_list n) consts_list.
1188 Proof. induction consts_list; cbn; ez. Qed.
1189
1190 Lemma only_integers consts_list t v1 v2 :
1191 In (existT (fun t : type => (var positive t * type.interp base.interp t)%type) (type.base t)
1192 (v1, v2)%zrange) (make_pairs consts_list) ->
1193 t = tZ.
1194 Proof.
1195 induction consts_list; cbn; [ tauto | ].
1196 destruct 1; congruence || tauto.
1197 Qed.
1198
1199 Lemma no_pairs consts_list v1 v2 :
1200 In (existZZ (v1, v2)%zrange) (make_pairs consts_list) -> False.
1201 Proof. intro H; apply only_integers in H. congruence. Qed.
1202
1203
1204 Definition make_cc last_wrote ctx carry_flag : CC.state :=
1205 {| CC.cc_c := carry_flag;
1206 CC.cc_m := cc_spec CC.M (ctx (last_wrote CC.M));
1207 CC.cc_l := cc_spec CC.L (ctx (last_wrote CC.L));
1208 CC.cc_z := cc_spec CC.Z (ctx (last_wrote CC.Z)
1209 + (if (last_wrote CC.C =? last_wrote CC.Z)%positive
1210 then wordmax * Z.b2z carry_flag else 0));
1211 |}.
1212
1213
1214 Hint Resolve Pos.lt_trans Pos.lt_irrefl Pos.lt_succ_diag_r Pos.eqb_refl : core.
1215 Hint Resolve in_or_app : core.
1216 Hint Resolve make_consts_ok make_pairs_ok make_ctx_ok no_pairs : core.
1217 (* TODO : probably not all of these preconditions are necessary -- prune them sometime *)
1218 Lemma of_Expr_correct next_name consts_list arg_list error
1219 (carry_flag : bool)
1220 (last_wrote : CC.code -> positive) (* variables which last wrote to each flag; put RegZero if flag empty *)
1221 t (e : Expr t)
1222 (x1 : type.for_each_lhs_of_arrow (var positive) t)
1223 (x2 : type.for_each_lhs_of_arrow _ t) result :
1224 let e1 := (invert_expr.smart_App_curried (e _) x1) in
1225 let e2 := (invert_expr.smart_App_curried (e _) x2) in
1226 let ctx := make_ctx (consts_list ++ arg_list) in
1227 let consts := make_consts consts_list in
1228 let cc := make_cc last_wrote ctx carry_flag in
1229 let G := make_pairs consts_list ++ make_pairs arg_list in
1230 (forall c, last_wrote c < next_name)%positive ->
1231 (forall n v, In (n, v) (consts_list ++ arg_list) -> (n < next_name)%positive) ->
1232 (In (last_wrote CC.C, Z.b2z carry_flag) consts_list) ->
1233 (forall n v1 v2, In (n, v1) (consts_list ++ arg_list) ->
1234 In (n, v2) (consts_list ++ arg_list) -> v1 = v2) (* no duplicate names *) ->
1235 (forall v1 v2, In (v1, v2) consts_list -> v2 mod 2 ^ 256 = v2) ->
1236 (forall v1 v2, In (v1, v2) arg_list -> v2 mod 2 ^ 256 = v2) ->
1237 (API.wf G e1 e2) ->
1238 valid_expr _ error consts _ last_wrote e1 ->
1239 interp_if_Z e2 = Some result ->
1240 interp Pos.eqb wordmax cc_spec (of_Expr next_name consts e x1 error) cc ctx = result.
1241 Proof.
1242 cbv [of_Expr]; intros.
1243 eapply of_prefancy_correct with (name_lt := Pos.lt)
1244 (cctx := fun n => if (n =? last_wrote CC.C)%positive
1245 then carry_flag
1246 else match make_consts consts_list 1 with
1247 | Some n1 => (n =? n1)%positive
1248 | _ => false
1249 end);
1250 cbv [id]; eauto;
1251 try apply Pos.eqb_neq; intros;
1252 try solve [apply make_ctx_ok; auto; apply make_pairs_ok;
1253 cbv [make_pairs]; rewrite map_app; auto ];
1254 repeat match goal with
1255 | H : _ |- _ => apply in_app_or in H; destruct H
1256 | H : In _ (make_pairs _) |- context [ _ = base.type.type_base _] => apply only_integers in H
1257 | H : In _ (make_pairs _) |- context [interp_base] =>
1258 pose proof (only_integers _ _ _ _ H); subst; cbn [interp_base]
1259 | _ => solve [apply Pos.eqb_eq; auto]
1260 | _ => solve [eauto]
1261 | _ => solve [exfalso; eauto]
1262 end; [ | ].
1263 (* TODO : clean this up *)
1264 { cbv [cc_good make_cc]; repeat split; intros;
1265 [ rewrite Pos.eqb_refl; reflexivity | | ];
1266 break_innermost_match; try rewrite Pos.eqb_eq in *;
1267 subst; try reflexivity;
1268 repeat match goal with
1269 | H : make_consts _ _ = Some _ |- _ =>
1270 try rewrite H;
1271 apply make_consts_ok, make_pairs_ok in H
1272 | _ => apply Pos.eqb_neq; intro; subst
1273 | _ => inversion_option; congruence
1274 end;
1275 match goal with
1276 | H : In (?n, ?x) consts_list,
1277 H': In (?n, ?y) consts_list,
1278 H'' : forall n x y, In (n,x) _ ->
1279 In (n,y) _ -> x = y |- _ =>
1280 assert (x = y) by (eapply H''; eauto)
1281 end; destruct carry_flag; cbn [Z.b2z] in *; congruence. }
1282 { match goal with |- context [make_ctx ?l ?n] =>
1283 let H := fresh in
1284 destruct (make_ctx_cases l n) as [H | H];
1285 [ rewrite H | apply in_app_or in H; destruct H ]
1286 end; eauto. }
1287 Qed.
1288 End Proofs.
1289
1290 (* Tactics and lemmas that help to prove two Fancy expressions are
1291 equivalent. *)
1292 Section Equivalence.
1293 Context {name} (name_eqb : name -> name -> bool) (wordmax : Z).
1294 Let interp := interp name_eqb wordmax cc_spec.
1295 Lemma interp_step i rd args cont cc ctx :
1296 interp (Instr i rd args cont) cc ctx =
1297 let result := spec i (Tuple.map ctx args) cc in
1298 let new_cc := CC.update (writes_conditions i) result cc_spec cc in
1299 let new_ctx := fun n => if name_eqb n rd then result mod wordmax else ctx n in interp cont new_cc new_ctx.
1300 Proof using Type. reflexivity. Qed.
1301
1302 Lemma interp_state_equiv e :
1303 forall cc ctx cc' ctx',
1304 cc = cc' -> (forall r, ctx r = ctx' r) ->
1305 interp e cc ctx = interp e cc' ctx'.
1306 Proof using name_eqb wordmax.
1307 induction e; intros; subst; cbn; [solve[auto]|].
1308 apply IHe; rewrite Tuple.map_ext with (g:=ctx') by auto;
1309 [reflexivity|].
1310 intros; break_match; auto.
1311 Qed.
1312
1313 Local Ltac prove_comm H :=
1314 rewrite !interp_step; cbn - [interp];
1315 intros; rewrite H; try reflexivity.
1316
1317 Lemma mulll_comm rd x y cont cc ctx :
1318 interp (Instr MUL128LL rd (x, y) cont) cc ctx =
1319 interp (Instr MUL128LL rd (y, x) cont) cc ctx.
1320 Proof using name_eqb wordmax. prove_comm Z.mul_comm. Qed.
1321
1322 Lemma mulhh_comm rd x y cont cc ctx :
1323 interp (Instr MUL128UU rd (x, y) cont) cc ctx =
1324 interp (Instr MUL128UU rd (y, x) cont) cc ctx.
1325 Proof using name_eqb wordmax. prove_comm Z.mul_comm. Qed.
1326
1327 Lemma mullh_mulhl rd x y cont cc ctx :
1328 interp (Instr MUL128LU rd (x, y) cont) cc ctx =
1329 interp (Instr MUL128UL rd (y, x) cont) cc ctx.
1330 Proof using name_eqb wordmax. prove_comm Z.mul_comm. Qed.
1331
1332 Lemma add_comm rd x y cont cc ctx :
1333 0 <= ctx x < 2^256 ->
1334 0 <= ctx y < 2^256 ->
1335 interp (Instr (ADD 0) rd (x, y) cont) cc ctx =
1336 interp (Instr (ADD 0) rd (y, x) cont) cc ctx.
1337 Proof using name_eqb wordmax.
1338 prove_comm Z.add_comm.
1339 rewrite !(Z.mod_small (ctx _)) by lia.
1340 reflexivity.
1341 Qed.
1342
1343 Lemma addc_comm rd x y cont cc ctx :
1344 0 <= ctx x < 2^256 ->
1345 0 <= ctx y < 2^256 ->
1346 interp (Instr (ADDC 0) rd (x, y) cont) cc ctx =
1347 interp (Instr (ADDC 0) rd (y, x) cont) cc ctx.
1348 Proof using name_eqb wordmax.
1349 prove_comm (Z.add_comm (ctx x)).
1350 rewrite !(Z.mod_small (ctx _)) by lia.
1351 reflexivity.
1352 Qed.
1353 End Equivalence.
1354
1355 (* Tactics for proving equivalence *)
1356
1357 (* pose the equivalence of a single instruction's result *)
1358 Ltac results_equiv :=
1359 match goal with
1360 |- ?lhs = ?rhs =>
1361 match lhs with
1362 context [spec ?li ?largs ?lcc] =>
1363 match rhs with
1364 context [spec ?ri ?rargs ?rcc] =>
1365 replace (spec li largs lcc) with (spec ri rargs rcc)
1366 end
1367 end
1368 end.
1369
1370 (* simplify the updating of condition codes to prevent exponential blowup in [step] *)
1371 Ltac simplify_cc :=
1372 match goal with
1373 |- context [CC.update ?to_write ?result ?cc_spec ?old_state] =>
1374 let e := eval cbv -[spec cc_spec CC.cc_l CC.cc_m CC.cc_z CC.cc_c] in
1375 (CC.update to_write result cc_spec old_state) in
1376 change (CC.update to_write result cc_spec old_state) with e
1377 end.
1378
1379 (* remember the result of an instruction (again, prevents exponential blowup in [step] *)
1380 Ltac remember_single_result :=
1381 match goal with |- context [(spec ?i ?args ?cc) mod ?w] =>
1382 let x := fresh "x" in
1383 let y := fresh "y" in
1384 remember (spec i args cc) as x;
1385 remember (x mod w) as y
1386 end.
1387
1388 (* rewrite with interp_step and remember the results *)
1389 Ltac step :=
1390 match goal with
1391 |- interp _ _ _ (Instr ?i ?rd1 ?args1 ?cont1) ?cc1 ?ctx1 =
1392 interp _ _ _ (Instr ?i ?rd2 ?args2 ?cont2) ?cc2 ?ctx2 =>
1393 rewrite (interp_step _ _ i rd1 args1 cont1);
1394 rewrite (interp_step _ _ i rd2 args2 cont2)
1395 end;
1396 cbn - [interp spec cc_spec];
1397 repeat progress
1398 rewrite ?Registers.reg_eqb_neq, ?Registers.reg_eqb_refl by congruence;
1399 results_equiv; [ remember_single_result; repeat simplify_cc | try reflexivity ].