Debian Codegraph @

src/Fancy/Montgomery256.v

raw
1 From Coq Require Import Bool.
2 From Coq Require Import Derive.
3 From Coq Require Import ZArith Lia.
4 From Coq Require Import List. Import ListNotations.
5 Require Import Rewriter.Language.Language. Import Language.Compilers.
6 Require Import Crypto.Language.API. Import Language.API.Compilers.
7 Require Import Rewriter.Language.Wf. Import Language.Wf.Compilers.
8 Require Import Crypto.Arithmetic.FancyMontgomeryReduction.
9 Require Import Crypto.PushButtonSynthesis.FancyMontgomeryReduction.
10 Require Import Crypto.Fancy.Compiler.
11 Require Import Crypto.Fancy.Prod.
12 Require Import Crypto.Fancy.Spec.
13 Require Import Crypto.Util.Tactics.BreakMatch.
14 Require Import Crypto.Util.ZUtil.EquivModulo.
15 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
16 Require Import Crypto.Util.Notations.
17 Local Open Scope Z_scope.
18
19 Module Montgomery256.
20
21 Definition N := Eval lazy in (2^256-2^224+2^192+2^96-1).
22 Definition N':= (115792089210356248768974548684794254293921932838497980611635986753331132366849).
23 Definition R := Eval lazy in (2^256).
24 Definition R' := 115792089183396302114378112356516095823261736990586219612555396166510339686400.
25 Definition machine_wordsize := 256.
26
27 Local Instance : Primitives.Options.PipelineOptions
28 := let _ := Primitives.Options.default_PipelineOptions in
29 {| Primitives.Options.widen_carry := false
30 ; Primitives.Options.widen_bytes := true
31 ; Primitives.Options.unfold_value_barrier := true |}.
32
33 Derive montred256
34 SuchThat (montred N R N' machine_wordsize = ErrorT.Success montred256)
35 As montred256_eq.
36 Proof. lazy; reflexivity. Qed.
37
38 Lemma Wf_montred256 : API.Wf montred256.
39 Proof using Type. eapply Wf_montred, montred256_eq. Qed.
40
41 Lemma montred256_correct :
42 COperationSpecifications.MontgomeryReduction.montred_correct N R R' (API.Interp montred256).
43 Proof.
44 apply montred_correct with (n:=2%nat) (machine_wordsize:=machine_wordsize) (N':=N') (requests:=[]).
45 { lazy. reflexivity. }
46 { apply montred256_eq. }
47 Qed.
48
49 Lemma montred'_correct_specialized R' (R'_correct : Z.equiv_modulo N (R * R') 1) :
50 forall (lo hi : Z),
51 0 <= lo < R -> 0 <= hi < R -> 0 <= lo + R * hi < R * N ->
52 MontgomeryReduction.montred' N R N' (Z.log2 R) lo hi = ((lo + R * hi) * R') mod N.
53 Proof.
54 intros.
55 apply MontgomeryReduction.montred'_correct with (T:=lo + R * hi) (R':=R') (n:=2%nat);
56 try match goal with
57 | |- context[R'] => assumption
58 | |- context [lo] =>
59 try assumption; progress autorewrite with zsimplify cancel_pair; reflexivity
60 end; lazy; try split; congruence.
61 Qed.
62
63 Definition montred256_fancy' (RegMod RegPInv RegZero lo hi error : positive) :=
64 of_Expr 6%positive
65 (make_consts [(RegMod, N); (RegZero, 0); (RegPInv, N')])
66 montred256
67 (lo, (hi, tt))
68 error.
69 Derive montred256_fancy
70 SuchThat (forall RegMod RegPInv RegZero,
71 montred256_fancy RegMod RegPInv RegZero = montred256_fancy' RegMod RegPInv RegZero)
72 As montred256_fancy_eq.
73 Proof.
74 intros.
75 cbv [montred256_fancy'].
76 lazy - [ADD ADDC SUB MUL128LL MUL128LU MUL128UL MUL128UU RSHI SELC SELM SELL ADDM].
77 reflexivity.
78 Qed.
79
80 (* TODO: these tactics are duplicated; move them elsewhere (probably translation *)
81 Local Ltac wf_subgoal :=
82 repeat first [ progress cbn [List.In type.and_for_each_lhs_of_arrow fst snd List.app List.map]
83 | apply expr.wf_smart_App_curried
84 | progress intros
85 | exfalso; assumption
86 | apply conj
87 | exact I
88 | solve [ eauto 50 using or_introl, or_intror, eq_refl with nocore ] ].
89 Local Ltac valid_expr_subgoal :=
90 repeat match goal with
91 | _ => progress intros
92 | |- context [valid_ident] => econstructor
93 | |- context[valid_scalar] => econstructor
94 | |- context [valid_carry] => econstructor
95 | _ => reflexivity
96 | |- _ <> None => cbn; congruence
97 | |- of_prefancy_scalar _ _ _ _ = _ => cbn; solve [eauto]
98 end.
99
100 Lemma montred256_fancy_correct :
101 forall lo hi error,
102 0 <= lo < R ->
103 0 <= hi < R ->
104 0 <= lo + R * hi < R * N ->
105 let RegZero := 1%positive in
106 let RegMod := 2%positive in
107 let RegPInv := 3%positive in
108 let RegHi := 4%positive in
109 let RegLo := 5%positive in
110 let consts_list := [(RegMod, N); (RegZero, 0); (RegPInv, N')] in
111 let arg_list := [(RegHi, hi); (RegLo, lo)] in
112 let ctx := make_ctx (consts_list ++ arg_list) in
113 let carry_flag := false in
114 let last_wrote := (fun x : CC.code => RegZero) in
115 let cc := make_cc last_wrote ctx carry_flag in
116 interp Pos.eqb wordmax cc_spec (montred256_fancy RegMod RegPInv RegZero RegLo RegHi error) cc ctx = ((lo + R * hi) * R') mod N.
117 Proof.
118 intros.
119 rewrite montred256_fancy_eq.
120 cbv [montred256_fancy'].
121 rewrite <-montred256_correct by (auto; reflexivity).
122 eapply of_Expr_correct with (x2 := (lo, (hi, tt))).
123 { cbn; intros; subst RegZero RegMod RegPInv RegHi RegLo.
124 intuition; Prod.inversion_prod; subst; cbv. break_innermost_match; congruence. }
125 { cbn; intros; subst RegZero RegMod RegPInv RegHi RegLo.
126 intuition; Prod.inversion_prod; subst; cbv; congruence. }
127 { cbn; intros; subst RegZero RegMod RegPInv RegHi RegLo. tauto. }
128 { cbn; intros; subst RegZero RegMod RegPInv RegHi RegLo.
129 intuition; Prod.inversion_prod; subst; cbv; congruence. }
130 { cbn; intros; subst RegZero RegMod RegPInv RegHi RegLo.
131 match goal with |- context [_ mod ?m] => change m with (2 ^ machine_wordsize) end.
132 intuition; Prod.inversion_prod; subst; apply Z.mod_small; cbv; try split; congruence. }
133 { cbn; intros; subst RegZero RegMod RegPInv RegHi RegLo.
134 match goal with |- context [_ mod ?m] => change m with (2 ^ machine_wordsize) end.
135 assert (R <= 2 ^ machine_wordsize) by (cbv; congruence).
136 intuition; Prod.inversion_prod; subst; apply Z.mod_small; lia. }
137 { repeat first [ eapply expr.wf_Proper_list, Wf_montred256
138 | progress cbv [make_pairs consts_list arg_list]
139 | wf_subgoal ]. }
140 { cbn. cbv [N' N].
141 repeat (econstructor; [ solve [valid_expr_subgoal] | intros ]).
142 econstructor. valid_expr_subgoal. }
143 { reflexivity. }
144 Qed.
145
146 Import Spec.Registers.
147
148 Definition montred256_alloc' lo hi RegPInv :=
149 fun errorP errorR =>
150 allocate register
151 positive Pos.eqb
152 errorR
153 (montred256_fancy 1000%positive 1001%positive 1002%positive 1003%positive 1004%positive errorP)
154 [r2;r3;r4;r5;r6;r7;r8;r9;r10;r11;r12;r13;r14;r15;r16;r17;r18;r19;r20]
155 (fun n => if n =? 1000 then RegMod
156 else if n =? 1001 then RegPInv
157 else if n =? 1002 then RegZero
158 else if n =? 1003 then lo
159 else if n =? 1004 then hi
160 else errorR)%positive.
161 Derive montred256_alloc
162 SuchThat (montred256_alloc = montred256_alloc')
163 As montred256_alloc_eq.
164 Proof.
165 intros.
166 cbv [montred256_alloc' montred256_fancy].
167 cbn. subst montred256_alloc.
168 reflexivity.
169 Qed.
170
171 Local Notation interp := (interp reg_eqb wordmax cc_spec).
172 Lemma montred256_alloc_equivalent errorP errorR cc_start_state start_context :
173 forall lo hi y t1 t2 scratch RegPInv extra_reg,
174 NoDup [lo; hi; y; t1; t2; scratch; RegPInv; extra_reg; RegMod; RegZero] ->
175 0 <= start_context lo < R ->
176 0 <= start_context hi < R ->
177 0 <= start_context RegPInv < R ->
178 interp
179 (montred256_alloc r0 r1 r30 errorP errorR) cc_start_state
180 (fun r => if reg_eqb r r0
181 then start_context lo
182 else if reg_eqb r r1
183 then start_context hi
184 else if reg_eqb r r30
185 then start_context RegPInv
186 else start_context r)
187 = interp (Prod.MontRed256 lo hi y t1 t2 scratch RegPInv) cc_start_state start_context.
188 Proof.
189 intros. cbv [R] in *.
190 cbv [Prod.MontRed256 montred256_alloc].
191
192 (* Extract proofs that no registers are equal to each other *)
193 repeat match goal with
194 | H : NoDup _ |- _ => inversion H; subst; clear H
195 | H : ~ In _ _ |- _ => cbv [In] in H
196 | H : ~ (_ \/ _) |- _ => apply Decidable.not_or in H; destruct H
197 | H : ~ False |- _ => clear H
198 end.
199
200 Time repeat match goal with
201 | _ => progress prove_programs_equivalent
202 | _ => rewrite interp_Mul256 with (tmp2 := extra_reg) by
203 (match goal with
204 | |- _ <> _ => assumption || congruence
205 | _ => push_value_unused
206 end)
207 | _ => rewrite interp_Mul256x256 with (tmp2 := extra_reg) by
208 (match goal with
209 | |- _ <> _ => assumption || congruence
210 | _ => push_value_unused
211 end)
212 end.
213
214 cbn [Spec.interp]; simplify_with_register_properties.
215 reflexivity.
216 Qed.
217
218
219 Lemma prod_montred256_correct :
220 forall (cc_start_state : CC.state) (* starting carry flags can be anything *)
221 (start_context : register -> Z) (* starting register values *)
222 (lo hi y t1 t2 scratch RegPInv extra_reg : register), (* registers to use in computation *)
223 NoDup [lo; hi; y; t1; t2; scratch; RegPInv; extra_reg; RegMod; RegZero] -> (* registers must be distinct *)
224 start_context RegPInv = N' -> (* RegPInv needs to hold the inverse of the modulus *)
225 start_context RegMod = N -> (* RegMod needs to hold the modulus *)
226 start_context RegZero = 0 -> (* RegZero needs to hold zero *)
227 (0 <= start_context lo < R) -> (* low half of the input is in bounds (R=2^256) *)
228 (0 <= start_context hi < R) -> (* high half of the input is in bounds (R=2^256) *)
229 let x := (start_context lo) + R * (start_context hi) in (* x is the input (split into two registers) *)
230 (0 <= x < R * N) -> (* input precondition *)
231 (interp (Prod.MontRed256 lo hi y t1 t2 scratch RegPInv) cc_start_state start_context = (x * R') mod N).
232 Proof.
233 intros. subst x.
234 erewrite <-montred256_fancy_correct with (error:=100000%positive) by eauto.
235 rewrite <-montred256_alloc_equivalent with (errorR := RegZero) (errorP := 1%positive) (extra_reg:=extra_reg)
236 by (cbv [R N N'] in *; auto with lia).
237
238 (* TODO: factor out this tactic *)
239 match goal with
240 |- context [make_cc ?last_wrote ?ctx ?carry] =>
241 let e := fresh in
242 let He := fresh in
243 remember (make_cc last_wrote ctx carry) as e eqn:He;
244 cbv [make_ctx app make_cc] in He;
245 cbn [Pos.eqb] in He; autorewrite with zsimplify in He;
246 subst e
247 end.
248 cbv [montred256_alloc montred256_fancy].
249 repeat match goal with
250 H : context [start_context] |- _ =>
251 rewrite <-H end.
252 repeat step.
253 reflexivity.
254 Qed.
255 End Montgomery256.
256
257 Section with_notations.
258 Import Crypto.Language.IdentifiersBasicGENERATED.Compilers.
259 Import Crypto.Util.ZRange.
260 Local Open Scope zrange_scope.
261 Local Open Scope Z_scope.
262 Local Open Scope expr_scope.
263 Local Notation uint256 := r[0 ~> 115792089237316195423570985008687907853269984665640564039457584007913129639935]%zrange.
264 Local Set Printing Width 500.
265 Redirect "Crypto.Fancy.Montgomery256.montred256" Print Montgomery256.montred256.
266 End with_notations.
267
268 Import Registers.
269
270 (* Notations to make code more readable *)
271 Local Notation "i rd x y ; cont" := (Instr i rd (x, y) cont) (at level 40, cont at level 200, format "i rd x y ; '//' cont").
272 Local Notation "i rd x y z ; cont" := (Instr i rd (x, y, z) cont) (at level 40, cont at level 200, format "i rd x y z ; '//' cont").
273
274 (* Montgomery reference code : *)
275 Local Set Printing Width 150.
276 Redirect "Crypto.Fancy.Montgomery256.Prod.MontRed256" Eval cbv beta iota delta [Prod.MontRed256 Prod.Mul256 Prod.Mul256x256] in Prod.MontRed256.
277 (*
278 = fun lo hi y t1 t2 scratch RegPInv : register =>
279 MUL128LL y lo RegPInv;
280 MUL128UL t1 lo RegPInv;
281 ADD 128 y y t1;
282 MUL128LU t1 lo RegPInv;
283 ADD 128 y y t1;
284 MUL128LL t1 y RegMod;
285 MUL128UU t2 y RegMod;
286 MUL128UL scratch y RegMod;
287 ADD 128 t1 t1 scratch;
288 ADDC (-128) t2 t2 scratch;
289 MUL128LU scratch y RegMod;
290 ADD 128 t1 t1 scratch;
291 ADDC (-128) t2 t2 scratch;
292 ADD 0 lo lo t1;
293 ADDC 0 hi hi t2;
294 SELC y RegMod RegZero;
295 SUB 0 lo hi y;
296 ADDM lo lo RegZero RegMod;
297 Ret lo
298 *)
299
300 (* Remove Redirect to see proof statement and remaining admitted statements,
301 or search for "prod_montred256_correct" to see comments on the proof
302 preconditions. *)
303 Redirect "Crypto.Fancy.Montgomery256.prod_montred256_correct" Check Montgomery256.prod_montred256_correct.
304 Redirect "Crypto.Fancy.Montgomery256.prod_montred256_correct.Assumptions" Print Assumptions Montgomery256.prod_montred256_correct.