Debian Codegraph @

src/Fancy/Prod.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Lia.
3 From Coq Require Import List. Import ListNotations.
4 Require Import Crypto.Algebra.Ring. (* for ring_simplify_subterms *)
5 Require Import Crypto.Fancy.Spec. Import Spec.Registers.
6 Require Import Crypto.Fancy.Compiler.
7 Require Import Crypto.Util.Tactics.BreakMatch.
8 Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo.
9 Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds.
10 Local Open Scope Z_scope.
11
12 Module Z.
13 (* TODO: move to Div *)
14 Lemma div_add_mod_cond_l' x y d :
15 d <> 0 ->
16 (x mod d + y) / d = (x + y) / d - x / d.
17 Proof.
18 intros.
19 rewrite (Div.Z.div_add_mod_cond_l x y d) by lia.
20 ring.
21 Qed.
22 Lemma div_add_mod_cond_r' x y d :
23 d <> 0 ->
24 (x + y mod d) / d = (x + y) / d - y / d.
25 Proof.
26 intros.
27 rewrite (Div.Z.div_add_mod_cond_r x y d) by lia.
28 ring.
29 Qed.
30 End Z.
31
32 Section Prod.
33 Definition Mul256 (out src1 src2 tmp : register) (cont : expr) : expr :=
34 Instr MUL128LL out (src1, src2)
35 (Instr MUL128UL tmp (src1, src2)
36 (Instr (ADD 128) out (out, tmp)
37 (Instr MUL128LU tmp (src1, src2)
38 (Instr (ADD 128) out (out, tmp) cont)))).
39 Definition Mul256x256 (out outHigh src1 src2 tmp : register) (cont : expr) : expr :=
40 Instr MUL128LL out (src1, src2)
41 (Instr MUL128UU outHigh (src1, src2)
42 (Instr MUL128UL tmp (src1, src2)
43 (Instr (ADD 128) out (out, tmp)
44 (Instr (ADDC (-128)) outHigh (outHigh, tmp)
45 (Instr MUL128LU tmp (src1, src2)
46 (Instr (ADD 128) out (out, tmp)
47 (Instr (ADDC (-128)) outHigh (outHigh, tmp) cont))))))).
48
49 Definition MontRed256 lo hi y t1 t2 scratch RegPInv : @expr register :=
50 Mul256 y lo RegPInv t1
51 (Mul256x256 t1 t2 y RegMod scratch
52 (Instr (ADD 0) lo (lo, t1)
53 (Instr (ADDC 0) hi (hi, t2)
54 (Instr SELC y (RegMod, RegZero)
55 (Instr (SUB 0) lo (hi, y)
56 (Instr ADDM lo (lo, RegZero, RegMod)
57 (Ret lo))))))).
58
59 (* Barrett reduction -- this is only the "reduce" part, excluding the initial multiplication. *)
60 Definition MulMod x xHigh RegMuLow scratchp1 scratchp2 scratchp3 scratchp4 scratchp5 : @expr register :=
61 let q1Bottom256 := scratchp1 in
62 let muSelect := scratchp2 in
63 let q2 := scratchp3 in
64 let q2High := scratchp4 in
65 let q2High2 := scratchp5 in
66 let q3 := scratchp1 in
67 let r2 := scratchp2 in
68 let r2High := scratchp3 in
69 let maybeM := scratchp1 in
70 Instr SELM muSelect (RegMuLow, RegZero)
71 (Instr (RSHI 255) q1Bottom256 (xHigh, x)
72 (Mul256x256 q2 q2High q1Bottom256 RegMuLow scratchp5
73 (Instr (RSHI 255) q2High2 (RegZero, xHigh)
74 (Instr (ADD 0) q2High (q2High, q1Bottom256)
75 (Instr (ADDC 0) q2High2 (q2High2, RegZero)
76 (Instr (ADD 0) q2High (q2High, muSelect)
77 (Instr (ADDC 0) q2High2 (q2High2, RegZero)
78 (Instr (RSHI 1) q3 (q2High2, q2High)
79 (Mul256x256 r2 r2High RegMod q3 scratchp4
80 (Instr (SUB 0) muSelect (x, r2)
81 (Instr (SUBC 0) xHigh (xHigh, r2High)
82 (Instr SELL maybeM (RegMod, RegZero)
83 (Instr (SUB 0) q3 (muSelect, maybeM)
84 (Instr ADDM x (q3, RegZero, RegMod)
85 (Ret x))))))))))))))).
86 End Prod.
87
88 (* Solves subgoals for commutativity proofs and simplifies register
89 lookup expressions *)
90 Ltac simplify_with_register_properties :=
91 repeat
92 match goal with
93 | _ => rewrite reg_eqb_refl
94 | _ => rewrite reg_eqb_neq by (assumption || congruence)
95 | H:?y = _ mod ?m |- 0 <= ?y < _ => rewrite H; apply Z.mod_pos_bound; lia
96 | _ => assumption
97 end.
98 Ltac cleanup :=
99 cbn - [interp spec cc_spec];
100 simplify_with_register_properties;
101 cbv [CC.update in_dec list_rec list_rect
102 CC.code_dec];
103 cbn [CC.cc_c CC.cc_m CC.cc_z CC.cc_l].
104 Ltac step_and_remember :=
105 rewrite interp_step; cleanup;
106 remember_single_result.
107 Ltac step_lhs :=
108 match goal with
109 | |- Spec.interp _ _ _ (Instr _ _ _ _) _ _ = _ =>
110 step_and_remember
111 end.
112 Ltac step_rhs :=
113 rewrite interp_step; cleanup;
114 match goal with
115 | H: ?x = spec ?i ?args _
116 |- context [spec ?i ?args ?cc] =>
117 replace (spec i args cc) with x by first [assumption | symmetry;assumption]
118 end;
119 match goal with
120 | H : ?y = (?x mod ?m)%Z |- context [(?x mod ?m)%Z] =>
121 rewrite <-H
122 end.
123 Ltac prove_programs_equivalent :=
124 repeat step_lhs; (* remember the results of each step on the LHS *)
125 repeat match goal with
126 | _ => step_rhs
127 | |- ?x = ?x => reflexivity
128 | _ => rewrite mulhh_comm by simplify_with_register_properties; step_rhs
129 | _ => rewrite mulll_comm by simplify_with_register_properties; step_rhs
130 | _ => rewrite add_comm by simplify_with_register_properties; step_rhs
131 | _ => rewrite addc_comm by simplify_with_register_properties; step_rhs
132 | _ => rewrite mullh_mulhl; step_rhs
133 | _ => rewrite <-mullh_mulhl; step_rhs
134 end.
135
136 Section ProdEquiv.
137 Context (wordmax : Z).
138
139 Let interp256 := interp reg_eqb wordmax cc_spec.
140 Lemma cc_overwrite_full x1 x2 l1 cc :
141 CC.update [CC.C; CC.M; CC.L; CC.Z] x2 cc_spec (CC.update l1 x1 cc_spec cc) = CC.update [CC.C; CC.M; CC.L; CC.Z] x2 cc_spec cc.
142 Proof.
143 cbv [CC.update]. cbn [CC.cc_c CC.cc_m CC.cc_l CC.cc_z].
144 break_match; try match goal with H : ~ In _ _ |- _ => cbv [In] in H; tauto end.
145 reflexivity.
146 Qed.
147
148 Definition value_unused r e : Prop :=
149 forall x cc ctx, interp256 e cc ctx = interp256 e cc (fun r' => if reg_eqb r' r then x else ctx r').
150
151 Lemma value_unused_skip r i rd args cont (Hcont: value_unused r cont) :
152 r <> rd ->
153 (~ In r (Tuple.to_list _ args)) ->
154 value_unused r (Instr i rd args cont).
155 Proof.
156 cbv [value_unused interp256] in *; intros.
157 rewrite !interp_step; cbv zeta.
158 rewrite Hcont with (x:=x).
159 match goal with |- ?lhs = ?rhs =>
160 match lhs with context [Tuple.map ?f ?t] =>
161 match rhs with context [Tuple.map ?g ?t] =>
162 rewrite (Tuple.map_ext_In f g) by (intros; cbv [reg_eqb]; break_match; congruence)
163 end end end.
164 apply interp_state_equiv; [ congruence | ].
165 { intros; cbv [reg_eqb] in *; break_match; congruence. }
166 Qed.
167
168 Lemma value_unused_overwrite r i args cont :
169 (~ In r (Tuple.to_list _ args)) ->
170 value_unused r (Instr i r args cont).
171 Proof.
172 cbv [value_unused interp256]; intros; rewrite !interp_step; cbv zeta.
173 match goal with |- ?lhs = ?rhs =>
174 match lhs with context [Tuple.map ?f ?t] =>
175 match rhs with context [Tuple.map ?g ?t] =>
176 rewrite (Tuple.map_ext_In f g) by (intros; cbv [reg_eqb]; break_match; congruence)
177 end end end.
178 apply interp_state_equiv; [ congruence | ].
179 { intros; cbv [reg_eqb] in *; break_match; congruence. }
180 Qed.
181
182 Lemma value_unused_ret r r' :
183 r <> r' ->
184 value_unused r (Ret r').
185 Proof.
186 cbv - [reg_dec]; intros.
187 break_match; congruence.
188 Qed.
189
190 Lemma interp_Mul256 out src1 src2 tmp tmp2 cont cc ctx:
191 out <> src1 ->
192 out <> src2 ->
193 out <> tmp ->
194 out <> tmp2 ->
195 src1 <> src2 ->
196 src1 <> tmp ->
197 src1 <> tmp2 ->
198 src2 <> tmp ->
199 src2 <> tmp2 ->
200 tmp <> tmp2 ->
201 value_unused tmp cont ->
202 value_unused tmp2 cont ->
203 interp256 (Mul256 out src1 src2 tmp cont) cc ctx =
204 interp256 (
205 Instr MUL128LU tmp (src1, src2)
206 (Instr MUL128UL tmp2 (src1, src2)
207 (Instr MUL128LL out (src1, src2)
208 (Instr (ADD 128) out (out, tmp2)
209 (Instr (ADD 128) out (out, tmp) cont))))) cc ctx.
210 Proof.
211 intros; cbv [Mul256 interp256].
212 repeat step_lhs.
213 repeat step_rhs.
214
215 match goal with H : value_unused tmp _ |- _ => erewrite H end.
216 match goal with H : value_unused tmp2 _ |- _ => erewrite H end.
217 apply interp_state_equiv.
218 { reflexivity. }
219 { intros; cbv [reg_eqb].
220 break_innermost_match; try congruence; reflexivity. }
221 Qed.
222
223 Lemma interp_Mul256x256 out outHigh src1 src2 tmp tmp2 cont cc ctx:
224 out <> src1 ->
225 out <> outHigh ->
226 out <> src2 ->
227 out <> tmp ->
228 out <> tmp2 ->
229 outHigh <> src1 ->
230 outHigh <> src2 ->
231 outHigh <> tmp ->
232 outHigh <> tmp2 ->
233 src1 <> src2 ->
234 src1 <> tmp ->
235 src1 <> tmp2 ->
236 src2 <> tmp ->
237 src2 <> tmp2 ->
238 tmp <> tmp2 ->
239 value_unused tmp cont ->
240 value_unused tmp2 cont ->
241 interp256 (Mul256x256 out outHigh src1 src2 tmp cont) cc ctx =
242 interp256 (
243 Instr MUL128UU outHigh (src1, src2)
244 (Instr MUL128LU tmp (src1, src2)
245 (Instr MUL128UL tmp2 (src1, src2)
246 (Instr MUL128LL out (src1, src2)
247 (Instr (ADD 128) out (out, tmp2)
248 (Instr (ADDC (-128)) outHigh (outHigh, tmp2)
249 (Instr (ADD 128) out (out, tmp)
250 (Instr (ADDC (-128)) outHigh (outHigh, tmp) cont)))))))) cc ctx.
251 Proof.
252 intros; cbv [Mul256x256 interp256].
253 repeat step_lhs.
254 repeat step_rhs.
255
256 match goal with H : value_unused tmp _ |- _ => erewrite H end.
257 match goal with H : value_unused tmp2 _ |- _ => erewrite H end.
258 apply interp_state_equiv; [ reflexivity | ].
259 intros; cbv [reg_eqb].
260 break_innermost_match; try congruence; reflexivity.
261 Qed.
262
263 Lemma interp_add_chain a b c cont cc ctx:
264 a <> b ->
265 a <> c ->
266 b <> c ->
267 (0 <= ctx a < wordmax)%Z ->
268 (0 <= ctx b < wordmax)%Z ->
269 (0 <= ctx c < wordmax)%Z ->
270 (wordmax = 2 ^ 256)%Z ->
271 let result := (ctx a + Z.shiftl (ctx c) 128 + ctx b * wordmax)%Z in
272 interp256 (
273 Instr (ADD 128) a (a, c)
274 (Instr (ADDC (-128)) b (b, c) cont))
275 cc ctx =
276 interp256 cont
277 (CC.update [CC.C; CC.M; CC.L; CC.Z]
278 (result / wordmax) cc_spec cc)
279 (fun r =>
280 if reg_eqb r a
281 then (result mod wordmax)%Z
282 else
283 if reg_eqb r b
284 then ((result / wordmax) mod wordmax)%Z
285 else ctx r).
286 Proof.
287 intros; cbv [interp256].
288 repeat step_and_remember.
289 cbn [spec ADD ADDC CC.cc_c] in *.
290 replace x0 with (result / wordmax)%Z in *.
291
292 { apply interp_state_equiv; [ reflexivity | ].
293 intros; cbv [reg_eqb].
294 break_innermost_match; try congruence; try reflexivity.
295 { subst. subst result.
296 autorewrite with zsimplify.
297 pull_Zmod. reflexivity. } }
298
299 (* TODO: this is a stupidly ugly arithmetic proof *)
300 { subst. subst result.
301 rewrite Z.shiftl_mul_pow2 by lia.
302 rewrite Z.shiftl_div_pow2 by lia.
303 rewrite Pos2Z.opp_neg. (* TODO : add to zsimplify? *)
304 autorewrite with zsimplify.
305
306 match goal with
307 |- context [if ?x then 1 else 0] =>
308 change (if x then 1 else 0) with (Z.b2z x)
309 end.
310 cbv [cc_spec].
311 rewrite Z.testbit_spec' by lia.
312
313 rewrite Z.mod_small with (b:=2) by
314 (split; [ Z.zero_bounds | ];
315 apply Z.div_lt_upper_bound; try lia;
316 match goal with
317 |- context [ ?x mod ?y ] =>
318 pose proof (Z.mod_pos_bound x y ltac:(lia))
319 end; lia).
320
321 autorewrite with zsimplify.
322
323 rewrite Z.div_add_mod_cond_r' by lia.
324 rewrite Z.mod_small with (a := ctx c / 2 ^ 128)
325 by (split; [ Z.zero_bounds | apply Z.div_lt_upper_bound; lia ]).
326 assert (0 < 2 ^ 128) by (cbn; lia).
327 change (2 ^ 256)%Z with (2 ^ 128 * 2 ^ 128)%Z.
328 rewrite Z.div_mul_cancel_r by lia.
329 ring. }
330 Qed.
331
332 Definition flags_unused e wordmax : Prop :=
333 forall cc x ctx,
334 interp reg_eqb wordmax cc_spec e cc ctx =
335 interp reg_eqb wordmax cc_spec e (CC.update [CC.C; CC.M; CC.L; CC.Z] x cc_spec cc) ctx.
336
337 Lemma swap_add_chain a b c d cont cc ctx:
338 a <> b ->
339 a <> c ->
340 a <> d ->
341 b <> c ->
342 b <> d ->
343 c <> d ->
344 0 <= ctx a < wordmax ->
345 0 <= ctx b < wordmax ->
346 0 <= ctx c < wordmax ->
347 0 <= ctx d < wordmax ->
348 wordmax = 2 ^ 256 ->
349 flags_unused cont wordmax ->
350 interp256 (
351 Instr (ADD 128) a (a, c)
352 (Instr (ADDC (-128)) b (b, c)
353 (Instr (ADD 128) a (a, d)
354 (Instr (ADDC (-128)) b (b, d) cont))))
355 cc ctx =
356 interp256 (
357 Instr (ADD 128) a (a, d)
358 (Instr (ADDC (-128)) b (b, d)
359 (Instr (ADD 128) a (a, c)
360 (Instr (ADDC (-128)) b (b, c) cont))))
361 cc ctx.
362 Proof.
363 intros.
364 assert (0 < wordmax) by lia.
365 repeat (rewrite interp_add_chain by (rewrite ?reg_eqb_refl, ?reg_eqb_neq by congruence; try assumption; auto using Z.mod_pos_bound with lia)).
366 rewrite ?reg_eqb_refl, ?reg_eqb_neq by congruence.
367 match goal with
368 | H : flags_unused ?e _
369 |- interp256 ?e ?ccl _ = interp256 ?e ?ccr _ =>
370 rewrite H with (cc:=ccl) (x:=0);
371 rewrite H with (cc:=ccr) (x:=0)
372 end.
373 rewrite !cc_overwrite_full.
374 apply interp_state_equiv; [ reflexivity | ].
375 intros; cbv [reg_eqb].
376 break_innermost_match; try congruence; try reflexivity.
377 { autorewrite with zsimplify. pull_Zmod.
378 f_equal; ring. }
379 { autorewrite with zsimplify. pull_Zmod.
380 rewrite !Z.div_add_mod_cond_l' by (subst; cbn; lia).
381 ring_simplify.
382 (* This is really annoying, can't a tactic do this? *)
383 match goal with |- context [?a + ?b + ?c] =>
384 match goal with |- context [?a + ?c + ?b] =>
385 replace (a + b + c) with (a + c + b) by ring
386 end end.
387 f_equal. ring. }
388 Qed.
389
390 End ProdEquiv.
391
392 Ltac push_value_unused :=
393 repeat match goal with
394 | _ => apply value_unused_skip; [ | assumption | ]
395 | _ => apply value_unused_overwrite
396 | _ => apply value_unused_ret; congruence
397 | _ => apply not_in_cons; split;
398 [ try assumption; symmetry; assumption | ]
399 | _ => apply in_nil
400 end.