Debian Codegraph @

src/Rewriter/PerfTesting/Core.v

raw
1 From Coq Require Export ZArith.
2 From Coq Require Import QArith_base Qround.
3 From Coq Require Export String.
4 From Coq Require Import List.
5 Require Crypto.ArithmeticCPS.Core.
6 Require Crypto.ArithmeticCPS.ModOps.
7 Require Crypto.ArithmeticCPS.Saturated.
8 Require Crypto.ArithmeticCPS.WordByWordMontgomery.
9 Require Import Crypto.UnsaturatedSolinasHeuristics.
10 Require Import Crypto.Util.Strings.ParseArithmetic.
11 Require Import Crypto.Util.Strings.ParseArithmeticToTaps.
12 Require Import Crypto.Util.Option.
13 Require Import Crypto.Util.ListUtil.
14 Require Crypto.PushButtonSynthesis.WordByWordMontgomery.
15 Require Crypto.PushButtonSynthesis.UnsaturatedSolinas.
16 Require Import Crypto.PushButtonSynthesis.WordByWordMontgomeryReificationCache.
17 Require Import Crypto.PushButtonSynthesis.UnsaturatedSolinasReificationCache.
18 Require Import Crypto.BoundsPipeline.
19 Require Import Rewriter.Language.Language.
20 Require Import Crypto.Rewriter.All.
21 Require Import Crypto.AbstractInterpretation.AbstractInterpretation.
22 Require Import Crypto.Stringification.C.
23 Require Import Crypto.Util.ZUtil.ModInv. (* Only needed for WBW Montgomery *)
24 Require Import Crypto.Util.Strings.Show.
25
26 Import ListNotations.
27 Global Open Scope string_scope.
28 Local Open Scope list_scope.
29 Local Open Scope option_scope.
30 Global Open Scope Z_scope.
31
32 (* replace this with vm_compute and the next fail with idtac to enable the precomputed versions *)
33 Declare Reduction precompute := cbv iota.
34 Ltac check_precomputed_enabled :=
35 let v := (eval precompute in (id true)) in
36 lazymatch v with
37 | true => idtac
38 | _ => fail 0 "Precomputed tests are disabled"
39 end.
40
41 Import
42 Language.Compilers
43 AbstractInterpretation.Compilers
44 Rewriter.All.Compilers.
45
46 Local Existing Instance Stringification.C.Compilers.ToString.C.OutputCAPI.
47 Local Existing Instance Primitives.Options.default_PipelineOptions.
48 Local Existing Instance Primitives.Options.default_PipelineToStringOptions.
49 Local Existing Instance Primitives.Options.default_SynthesisOptions.
50 Local Instance : tight_upperbound_fraction_opt := default_tight_upperbound_fraction.
51
52 Import API.
53
54 Local Set Primitive Projections.
55 Record Dyn := dyn { Ty : Type ; val :> Ty }.
56 Arguments dyn {_} _.
57
58 Module RT_ExtraAx := Core.RT_Extra Core.RuntimeAxioms.
59 Module ModOpsAx := ModOps.ModOps Core.RuntimeAxioms.
60 Module RT_ExtraDef := Core.RT_Extra Core.RuntimeDefinitions.
61 Module ModOpsDef := ModOps.ModOps Core.RuntimeDefinitions.
62 Module Import WordByWordMontgomeryAx := ArithmeticCPS.WordByWordMontgomery.WordByWordMontgomery Core.RuntimeAxioms.
63 Module Import WordByWordMontgomeryDef := ArithmeticCPS.WordByWordMontgomery.WordByWordMontgomery Core.RuntimeDefinitions.
64 Import Rewriter.Rewriter.ProofsCommon.Compilers.RewriteRules.GoalType.
65 Import Rewriter.Rewriter.ProofsCommon.Compilers.RewriteRules.GoalType.DefaultOptionType.
66
67 Ltac with_each_method tac :=
68 tac precomputed_decision_tree;
69 tac unreduced_decision_tree;
70 tac unreduced_naive.
71
72 Module Import UnsaturatedSolinas.
73 Class params :=
74 { n : nat;
75 s : Z;
76 c : list (Z * Z);
77 idxs : list nat;
78 limbwidth := limbwidth n s c;
79 machine_wordsize : Z }.
80
81 Global Instance show_lvl_params : ShowLevel params
82 := fun p => neg_wrap_parens ("{| n := " ++ show_lvl n term_lvl ++ "; s := " ++ show_lvl s term_lvl ++ "; c := " ++ show_lvl c term_lvl ++ "; idxs := " ++ show_lvl idxs term_lvl ++ "; machine_wordsize := " ++ show_lvl machine_wordsize term_lvl ++ "|}")%string.
83 Global Instance show_params : Show params := show_lvl_params.
84
85 Definition of_string (p : string) (bitwidth : Z) : list params
86 := match parseZ_arith_to_taps p with
87 | Some (sv, cv) => List.map
88 (fun nv => {| n := nv; s := sv; c := cv; machine_wordsize := bitwidth ; idxs := carry_chains nv sv cv |})
89 (firstn 2 (get_possible_limbs sv cv bitwidth))
90 | None => nil
91 end.
92
93 Definition GallinaAxOf (p : params) : Dyn
94 := dyn (fun f g : list Z => ModOpsAx.carry_mulmod (Qnum limbwidth) (Zpos (Qden limbwidth)) s c n idxs (RT_ExtraAx.expand_list 0 f n) (RT_ExtraAx.expand_list 0 g n)).
95 Time Definition GallinaAxComputedOf := Eval precompute in GallinaAxOf.
96 Definition GallinaDefOf (p : params) : Dyn
97 := dyn (fun f g : list Z => ModOpsDef.carry_mulmod (Qnum limbwidth) (Zpos (Qden limbwidth)) s c n idxs (RT_ExtraDef.expand_list 0 f n) (RT_ExtraDef.expand_list 0 g n)).
98
99 Definition PipelineFullOf : params * low_level_rewriter_method_opt -> Pipeline.ErrorT (Expr _)
100 := fun '(p, method) => PushButtonSynthesis.UnsaturatedSolinas.carry_mul n s c machine_wordsize.
101 Definition PipelineFullToStringsOf : params * low_level_rewriter_method_opt -> string * _
102 := fun '(p, method) => PushButtonSynthesis.UnsaturatedSolinas.scarry_mul n s c machine_wordsize "".
103 Section pipeline.
104 Context (p_opts : params * low_level_rewriter_method_opt).
105 Let p := fst p_opts.
106 Let opts := @Pipeline.opts_of_method (snd p_opts).
107 Local Existing Instance p.
108
109 Let E := (reified_carry_mul_gen
110 @ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n @ GallinaReify.Reify idxs)%Expr.
111
112 Let E2 := let E := PartialEvaluateWithListInfoFromBounds E (Some (List.repeat None n), (Some (List.repeat None n), tt)) in
113 let E := PartialEvaluate opts E in
114 E.
115
116 Definition PipelineNBEOf : Expr _
117 := E2.
118
119 Definition PipelineFlatNBEOf : GeneralizeVar.Flat.expr _
120 := GeneralizeVar.ToFlat PipelineNBEOf.
121
122 Definition PipelineArithOf : Expr _
123 := let E := E2 in
124 let E := DebugMonad.Debug.eval_result (Pipeline.RewriteAndEliminateDeadAndInline "RewriteArith_0" (RewriteRules.RewriteArith 0 opts) true (*with_dead_code_elimination*) false (*with_subst01*) true (* let_bind_return *) E) in
125 E.
126
127 Definition PipelineFlatArithOf : GeneralizeVar.Flat.expr _
128 := GeneralizeVar.ToFlat PipelineArithOf.
129 End pipeline.
130
131 Definition ForExtraction {R}
132 (extr_descr : string)
133 (seq : forall A B, (unit -> A) -> (unit -> B) -> R)
134 (time : forall A, string -> (unit -> A) -> R)
135 (prime : string) (bitwidth : string) (index : string)
136 (error : list string -> R)
137 : R
138 := Eval cbv beta iota delta [List.map List.fold_right] in
139 let str_bitwidth := bitwidth in
140 let str_index := index in
141 match parseZ_arith_strict bitwidth, parsenat_arith_strict index with
142 | Some bitwidth, Some index
143 => match List.nth_error (of_string prime bitwidth) index with
144 | Some p
145 => List.fold_right
146 (fun v1 v2 => seq _ _ (fun _ => v1) (fun _ => v2))
147 (seq _ _ id id)
148 (List.map
149 (fun method
150 => let make_descr := fun kind => ("Testing UnsaturatedSolinas " ++ prime ++ " (bitwidth = " ++ str_bitwidth ++ " ) (index = " ++ str_index ++ " ) (method = " ++ show method ++ " ) (params = " ++ show p ++ " ) " ++ kind ++ " with extraction " ++ extr_descr)%string in
151 (seq _ _)
152 (fun _ => time _ (make_descr "PipelineFullToStringsOf") (fun _ => PipelineFullToStringsOf (p, method)))
153 (fun _
154 => (seq _ _)
155 (fun _ => time _ (make_descr "PipelineFlatNBEOf") (fun _ => PipelineFlatNBEOf (p, method)))
156 (fun _ => time _ (make_descr "PipelineFlatArithOf") (fun _ => PipelineFlatArithOf (p, method)))))
157 [precomputed_decision_tree; unreduced_decision_tree; unreduced_naive])
158 | None
159 => error ["No such index"]
160 end
161 | None, None => error ["Could not parse bitwidth nor index"]
162 | None, _ => error ["Could not parse bitwidth"]
163 | _, None => error ["Could not parse index"]
164 end.
165
166 Tactic Notation "idtac_and_time" constr(prime) constr(bitwidth) constr(index) constr(p) constr(method) string(descr) tactic3(tac) :=
167 idtac "Testing UnsaturatedSolinas" prime "(bitwidth =" bitwidth ") (index =" index ") (method =" method ") (params =" p ")" descr ":";
168 time (idtac; tac ()).
169
170 Tactic Notation "idtac_and_time2" constr(prime) constr(bitwidth) constr(index) constr(p) constr(method) string(descr) tactic3(tac) :=
171 idtac "Testing UnsaturatedSolinas" prime "(bitwidth =" bitwidth ") (index =" index ") (method =" method ") (params =" p ")" descr "(1) :";
172 time (idtac; tac ());
173 idtac "Testing UnsaturatedSolinas" prime "(bitwidth =" bitwidth ") (index =" index ") (method =" method ") (params =" p ")" descr "(2) :";
174 time (idtac; tac ()).
175
176 Ltac compute_p prime bitwidth index :=
177 let p := (eval vm_compute in (List.nth_error (of_string prime bitwidth) index)) in
178 let __ := match p with
179 | Some _ => idtac
180 | None => idtac "No params" index "for prime" prime
181 end in
182 p.
183
184 Ltac perfGallinaAxOf' prime bitwidth index p :=
185 lazymatch p with
186 | Some ?p
187 => idtac_and_time prime bitwidth index p (@None unit) "GallinaAxOf with vm_compute" (fun _ => let __ := eval vm_compute in (GallinaAxOf p) in idtac);
188 idtac_and_time2 prime bitwidth index p (@None unit) "GallinaAxOf with native_compute" (fun _ => let __ := eval native_compute in (GallinaAxOf p) in idtac);
189 idtac_and_time prime bitwidth index p (@None unit) "GallinaAxOf with cbv" (fun _ => let __ := eval cbv in (GallinaAxOf p) in idtac);
190 idtac_and_time prime bitwidth index p (@None unit) "GallinaAxOf with lazy" (fun _ => let __ := eval lazy in (GallinaAxOf p) in idtac)
191 | None => idtac
192 end.
193 Ltac perfGallinaAxOf prime bitwidth index :=
194 let p := compute_p prime bitwidth index in perfGallinaAxOf' prime bitwidth index p.
195
196 Ltac perfGallinaAxComputedOf' prime bitwidth index p :=
197 check_precomputed_enabled;
198 lazymatch p with
199 | Some ?p
200 => idtac_and_time prime bitwidth index p (@None unit) "GallinaAxComputedOf with vm_compute" (fun _ => let __ := eval vm_compute in (GallinaAxComputedOf p) in idtac);
201 idtac_and_time prime bitwidth index p (@None unit) "GallinaAxComputedOf with cbv" (fun _ => let __ := eval cbv in (GallinaAxComputedOf p) in idtac);
202 idtac_and_time prime bitwidth index p (@None unit) "GallinaAxComputedOf with lazy" (fun _ => let __ := eval lazy in (GallinaAxComputedOf p) in idtac)
203 | None => idtac
204 end.
205 Ltac perfGallinaAxComputedOf prime bitwidth index :=
206 check_precomputed_enabled;
207 let p := compute_p prime bitwidth index in perfGallinaAxComputedOf' prime bitwidth index p.
208
209 Ltac perfPipelineOf' prime bitwidth index p :=
210 lazymatch p with
211 | Some ?p
212 => with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineFullOf (p, method)) in idtac));
213 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullToStringsOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineFullToStringsOf (p, method)) in idtac));
214 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineNBEOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineNBEOf (p, method)) in idtac));
215 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineArithOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineArithOf (p, method)) in idtac));
216 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth index p method "PipelineFullOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineFullOf (p, method)) in idtac));
217 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth index p method "PipelineFullToStringsOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineFullToStringsOf (p, method)) in idtac));
218 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth index p method "PipelineNBEOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineNBEOf (p, method)) in idtac));
219 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth index p method "PipelineArithOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineArithOf (p, method)) in idtac));
220 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullOf with cbv" (fun _ => let __ := eval cbv in (PipelineFullOf (p, method)) in idtac));
221 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullToStringsOf with cbv" (fun _ => let __ := eval cbv in (PipelineFullToStringsOf (p, method)) in idtac));
222 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineNBEOf with cbv" (fun _ => let __ := eval cbv in (PipelineNBEOf (p, method)) in idtac));
223 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineArithOf with cbv" (fun _ => let __ := eval cbv in (PipelineArithOf (p, method)) in idtac));
224 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullOf with lazy" (fun _ => let __ := eval lazy in (PipelineFullOf (p, method)) in idtac));
225 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullToStringsOf with lazy" (fun _ => let __ := eval lazy in (PipelineFullToStringsOf (p, method)) in idtac));
226 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineNBEOf with lazy" (fun _ => let __ := eval lazy in (PipelineNBEOf (p, method)) in idtac));
227 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineArithOf with lazy" (fun _ => let __ := eval lazy in (PipelineArithOf (p, method)) in idtac))
228 | None => idtac
229 end.
230 Ltac perfPipelineOf prime bitwidth index :=
231 let p := compute_p prime bitwidth index in perfPipelineOf' prime bitwidth index p.
232
233 Ltac perfGallinaDefOf' prime bitwidth index p :=
234 lazymatch p with
235 | Some ?p
236 => idtac_and_time prime bitwidth index p (@None unit) "GallinaDefOf with cbv_no_rt" (fun _ => let __ := eval cbv_no_rt in (GallinaDefOf p) in idtac);
237 idtac_and_time prime bitwidth index p (@None unit) "GallinaDefOf with lazy_no_rt" (fun _ => let __ := eval lazy_no_rt in (GallinaDefOf p) in idtac)
238 | None => idtac
239 end.
240 Ltac perfGallinaDefOf prime bitwidth index :=
241 let p := compute_p prime bitwidth index in perfGallinaDefOf' prime bitwidth index p.
242
243 Ltac perf prime bitwidth index :=
244 let p := (eval vm_compute in (List.nth_error (of_string prime bitwidth) index)) in
245 lazymatch p with
246 | Some ?p
247 => idtac_and_time prime bitwidth index p (@None unit) "GallinaAxOf with vm_compute" (fun _ => let __ := eval vm_compute in (GallinaAxOf p) in idtac);
248 idtac_and_time prime bitwidth index p (@None unit) "GallinaAxComputedOf with vm_compute" (fun _ => let __ := eval vm_compute in (GallinaAxComputedOf p) in idtac);
249 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineFullOf (p, method)) in idtac));
250 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullToStringsOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineFullToStringsOf (p, method)) in idtac));
251 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineNBEOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineNBEOf (p, method)) in idtac));
252 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineArithOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineArithOf (p, method)) in idtac));
253 idtac_and_time2 prime bitwidth index p (@None unit) "GallinaAxOf with native_compute" (fun _ => let __ := eval native_compute in (GallinaAxOf p) in idtac);
254 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth index p method "PipelineFullOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineFullOf (p, method)) in idtac));
255 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth index p method "PipelineFullToStringsOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineFullToStringsOf (p, method)) in idtac));
256 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth index p method "PipelineNBEOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineNBEOf (p, method)) in idtac));
257 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth index p method "PipelineArithOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineArithOf (p, method)) in idtac));
258 idtac_and_time prime bitwidth index p (@None unit) "GallinaAxOf with cbv" (fun _ => let __ := eval cbv in (GallinaAxOf p) in idtac);
259 idtac_and_time prime bitwidth index p (@None unit) "GallinaAxComputedOf with cbv" (fun _ => let __ := eval cbv in (GallinaAxComputedOf p) in idtac);
260 idtac_and_time prime bitwidth index p (@None unit) "GallinaAxOf with lazy" (fun _ => let __ := eval lazy in (GallinaAxOf p) in idtac);
261 idtac_and_time prime bitwidth index p (@None unit) "GallinaAxComputedOf with lazy" (fun _ => let __ := eval lazy in (GallinaAxComputedOf p) in idtac);
262 idtac_and_time prime bitwidth index p (@None unit) "GallinaDefOf with cbv_no_rt" (fun _ => let __ := eval cbv_no_rt in (GallinaDefOf p) in idtac);
263 idtac_and_time prime bitwidth index p (@None unit) "GallinaDefOf with lazy_no_rt" (fun _ => let __ := eval lazy_no_rt in (GallinaDefOf p) in idtac);
264 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullOf with cbv" (fun _ => let __ := eval cbv in (PipelineFullOf (p, method)) in idtac));
265 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullToStringsOf with cbv" (fun _ => let __ := eval cbv in (PipelineFullToStringsOf (p, method)) in idtac));
266 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineNBEOf with cbv" (fun _ => let __ := eval cbv in (PipelineNBEOf (p, method)) in idtac));
267 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineArithOf with cbv" (fun _ => let __ := eval cbv in (PipelineArithOf (p, method)) in idtac));
268 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullOf with lazy" (fun _ => let __ := eval lazy in (PipelineFullOf (p, method)) in idtac));
269 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineFullToStringsOf with lazy" (fun _ => let __ := eval lazy in (PipelineFullToStringsOf (p, method)) in idtac));
270 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineNBEOf with lazy" (fun _ => let __ := eval lazy in (PipelineNBEOf (p, method)) in idtac));
271 with_each_method ltac:(fun method => idtac_and_time prime bitwidth index p method "PipelineArithOf with lazy" (fun _ => let __ := eval lazy in (PipelineArithOf (p, method)) in idtac))
272 | None => idtac "No params" index "for prime" prime
273 end.
274
275 Global Set Printing Width 1000000.
276 End UnsaturatedSolinas.
277
278 Module Import WordByWordMontgomery.
279 Local Coercion Z.of_nat : nat >-> Z.
280 Local Coercion QArith_base.inject_Z : Z >-> Q.
281 Local Coercion Z.pos : positive >-> Z.
282 Class params :=
283 { m : Z;
284 machine_wordsize : Z;
285 s := 2^Z.log2_up m;
286 n : nat := Z.to_nat (Qceiling (Z.log2_up s / machine_wordsize));
287 r := 2^machine_wordsize;
288 m' := Z.modinv (-m) r }.
289
290 Global Instance show_lvl_params : ShowLevel params
291 := fun p => neg_wrap_parens ("{| m := " ++ show_lvl m term_lvl ++ "; machine_wordsize := " ++ show_lvl machine_wordsize term_lvl ++ "|}")%string.
292 Global Instance show_params : Show params := show_lvl_params.
293
294 Definition of_string (p : string) (bitwidth : Z) : option params
295 := match parseZ_arith_strict p with
296 | Some v => Some {| m := v ; machine_wordsize := bitwidth |}
297 | None => None
298 end.
299
300 Definition GallinaAxOf (p : params) : Dyn
301 := dyn (fun f g : list Z => WordByWordMontgomeryAx.mulmod machine_wordsize n m m' (RT_ExtraAx.expand_list 0 f n) (RT_ExtraAx.expand_list 0 g n)).
302 Time Definition GallinaAxComputedOf := Eval precompute in GallinaAxOf.
303 Definition GallinaDefOf (p : params) : Dyn
304 := dyn (fun f g : list Z => WordByWordMontgomeryDef.mulmod machine_wordsize n m m' (RT_ExtraDef.expand_list 0 f n) (RT_ExtraDef.expand_list 0 g n)).
305
306 Definition PipelineFullOf : params * low_level_rewriter_method_opt -> Pipeline.ErrorT (Expr _)
307 := fun '(p, method) => PushButtonSynthesis.WordByWordMontgomery.mul m machine_wordsize.
308 Definition PipelineFullToStringsOf : params * low_level_rewriter_method_opt -> string * _
309 := fun '(p, method) => PushButtonSynthesis.WordByWordMontgomery.smul m machine_wordsize "".
310 Section pipeline.
311 Context (p_opts : params * low_level_rewriter_method_opt).
312 Let p := fst p_opts.
313 Let opts := @Pipeline.opts_of_method (snd p_opts).
314 Local Existing Instance p.
315
316 Let E := (reified_mul_gen
317 @ GallinaReify.Reify machine_wordsize @ GallinaReify.Reify n @ GallinaReify.Reify m @ GallinaReify.Reify m')%Expr.
318
319 Let E2 := let E := PartialEvaluateWithListInfoFromBounds E (Some (List.repeat None n), (Some (List.repeat None n), tt)) in
320 let E := PartialEvaluate opts E in
321 E.
322
323 Definition PipelineNBEOf : Expr _
324 := E2.
325
326 Definition PipelineFlatNBEOf : GeneralizeVar.Flat.expr _
327 := GeneralizeVar.ToFlat PipelineNBEOf.
328
329 Definition PipelineArithOf : Expr _
330 := let E := E2 in
331 let E := DebugMonad.Debug.eval_result (Pipeline.RewriteAndEliminateDeadAndInline "RewriteArith_0" (RewriteRules.RewriteArith 0 opts) true (*with_dead_code_elimination*) false (*with_subst01*) true (* let_bind_return *) E) in
332 E.
333
334 Definition PipelineFlatArithOf : GeneralizeVar.Flat.expr _
335 := GeneralizeVar.ToFlat PipelineArithOf.
336 End pipeline.
337
338 Definition ForExtraction {R}
339 (extr_descr : string)
340 (seq : forall A B, (unit -> A) -> (unit -> B) -> R)
341 (time : forall A, string -> (unit -> A) -> R)
342 (prime : string) (bitwidth : string)
343 (error : list string -> R)
344 : R
345 := Eval cbv beta iota delta [List.map List.fold_right] in
346 let str_bitwidth := bitwidth in
347 match parseZ_arith_strict bitwidth with
348 | Some bitwidth
349 => match of_string prime bitwidth with
350 | Some p
351 => List.fold_right
352 (fun v1 v2 => seq _ _ (fun _ => v1) (fun _ => v2))
353 (seq _ _ id id)
354 (List.map
355 (fun method
356 => let make_descr := fun kind => ("Testing WordByWordMontgomery " ++ prime ++ " (bitwidth = " ++ str_bitwidth ++ " ) (method = " ++ show method ++ " ) (params = " ++ show p ++ " ) " ++ kind ++ " with extraction " ++ extr_descr)%string in
357 (seq _ _)
358 (fun _ => time _ (make_descr "PipelineFullToStringsOf") (fun _ => PipelineFullToStringsOf (p, method)))
359 (fun _
360 => (seq _ _)
361 (fun _ => time _ (make_descr "PipelineFlatNBEOf") (fun _ => PipelineFlatNBEOf (p, method)))
362 (fun _ => time _ (make_descr "PipelineFlatArithOf") (fun _ => PipelineFlatArithOf (p, method)))))
363 [precomputed_decision_tree; unreduced_decision_tree; unreduced_naive])
364 | None
365 => error ["Invalid prime"]
366 end
367 | None => error ["Could not parse bitwidth"]
368 end.
369
370 Tactic Notation "idtac_and_time" constr(prime) constr(bitwidth) constr(p) constr(method) string(descr) tactic3(tac) :=
371 idtac "Testing WordByWordMontgomery" prime "(bitwidth =" bitwidth ") (method =" method ") (params =" p ")" descr ":";
372 time (idtac; tac ()).
373
374 Tactic Notation "idtac_and_time2" constr(prime) constr(bitwidth) constr(p) constr(method) string(descr) tactic3(tac) :=
375 idtac "Testing WordByWordMontgomery" prime "(bitwidth =" bitwidth ") (method =" method ") (params =" p ")" descr "(1) :";
376 time (idtac; tac ());
377 idtac "Testing WordByWordMontgomery" prime "(bitwidth =" bitwidth ") (method =" method ") (params =" p ")" descr "(2) :";
378 time (idtac; tac ()).
379
380 Ltac compute_p prime bitwidth :=
381 let p := (eval vm_compute in (of_string prime bitwidth)) in
382 let __ := match p with
383 | Some _ => idtac
384 | None => idtac "No params for prime" prime
385 end in
386 p.
387
388 Ltac perfGallinaAxOf' prime bitwidth p :=
389 lazymatch p with
390 | Some ?p
391 => idtac_and_time prime bitwidth p (@None unit) "GallinaAxOf with vm_compute" (fun _ => let __ := eval vm_compute in (GallinaAxOf p) in idtac);
392 idtac_and_time2 prime bitwidth p (@None unit) "GallinaAxOf with native_compute" (fun _ => let __ := eval native_compute in (GallinaAxOf p) in idtac);
393 idtac_and_time prime bitwidth p (@None unit) "GallinaAxOf with cbv" (fun _ => let __ := eval cbv in (GallinaAxOf p) in idtac);
394 idtac_and_time prime bitwidth p (@None unit) "GallinaAxOf with lazy" (fun _ => let __ := eval lazy in (GallinaAxOf p) in idtac)
395 | None => idtac
396 end.
397 Ltac perfGallinaAxOf prime bitwidth :=
398 let p := compute_p prime bitwidth in perfGallinaAxOf' prime bitwidth p.
399
400 Ltac perfGallinaAxComputedOf' prime bitwidth p :=
401 check_precomputed_enabled;
402 lazymatch p with
403 | Some ?p
404 => idtac_and_time prime bitwidth p (@None unit) "GallinaAxComputedOf with vm_compute" (fun _ => let __ := eval vm_compute in (GallinaAxComputedOf p) in idtac);
405 idtac_and_time prime bitwidth p (@None unit) "GallinaAxComputedOf with cbv" (fun _ => let __ := eval cbv in (GallinaAxComputedOf p) in idtac);
406 idtac_and_time prime bitwidth p (@None unit) "GallinaAxComputedOf with lazy" (fun _ => let __ := eval lazy in (GallinaAxComputedOf p) in idtac)
407 | None => idtac
408 end.
409 Ltac perfGallinaAxComputedOf prime bitwidth :=
410 check_precomputed_enabled;
411 let p := compute_p prime bitwidth in perfGallinaAxComputedOf' prime bitwidth p.
412
413 Ltac perfPipelineOf' prime bitwidth p :=
414 lazymatch p with
415 | Some ?p
416 => with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineFullOf (p, method)) in idtac));
417 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullToStringsOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineFullToStringsOf (p, method)) in idtac));
418 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineNBEOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineNBEOf (p, method)) in idtac));
419 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineArithOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineArithOf (p, method)) in idtac));
420 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth p method "PipelineFullOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineFullOf (p, method)) in idtac));
421 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth p method "PipelineFullToStringsOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineFullToStringsOf (p, method)) in idtac));
422 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth p method "PipelineNBEOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineNBEOf (p, method)) in idtac));
423 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth p method "PipelineArithOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineArithOf (p, method)) in idtac));
424 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullOf with cbv" (fun _ => let __ := eval cbv in (PipelineFullOf (p, method)) in idtac));
425 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullToStringsOf with cbv" (fun _ => let __ := eval cbv in (PipelineFullToStringsOf (p, method)) in idtac));
426 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineNBEOf with cbv" (fun _ => let __ := eval cbv in (PipelineNBEOf (p, method)) in idtac));
427 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineArithOf with cbv" (fun _ => let __ := eval cbv in (PipelineArithOf (p, method)) in idtac));
428 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullOf with lazy" (fun _ => let __ := eval lazy in (PipelineFullOf (p, method)) in idtac));
429 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullToStringsOf with lazy" (fun _ => let __ := eval lazy in (PipelineFullToStringsOf (p, method)) in idtac));
430 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineNBEOf with lazy" (fun _ => let __ := eval lazy in (PipelineNBEOf (p, method)) in idtac));
431 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineArithOf with lazy" (fun _ => let __ := eval lazy in (PipelineArithOf (p, method)) in idtac))
432 | None => idtac
433 end.
434 Ltac perfPipelineOf prime bitwidth :=
435 let p := compute_p prime bitwidth in perfPipelineOf' prime bitwidth p.
436
437 Ltac perfGallinaDefOf' prime bitwidth p :=
438 lazymatch p with
439 | Some ?p
440 => idtac_and_time prime bitwidth p (@None unit) "GallinaDefOf with cbv_no_rt" (fun _ => let __ := eval cbv_no_rt in (GallinaDefOf p) in idtac);
441 idtac_and_time prime bitwidth p (@None unit) "GallinaDefOf with lazy_no_rt" (fun _ => let __ := eval lazy_no_rt in (GallinaDefOf p) in idtac)
442 | None => idtac
443 end.
444 Ltac perfGallinaDefOf prime bitwidth :=
445 let p := compute_p prime bitwidth in perfGallinaDefOf' prime bitwidth p.
446
447 Ltac perf prime bitwidth :=
448 let p := (eval vm_compute in (of_string prime bitwidth)) in
449 lazymatch p with
450 | Some ?p
451 => idtac_and_time prime bitwidth p (@None unit) "GallinaAxOf with vm_compute" (fun _ => let __ := eval vm_compute in (GallinaAxOf p) in idtac);
452 idtac_and_time prime bitwidth p (@None unit) "GallinaAxComputedOf with vm_compute" (fun _ => let __ := eval vm_compute in (GallinaAxComputedOf p) in idtac);
453 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineFullOf (p, method)) in idtac));
454 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullToStringsOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineFullToStringsOf (p, method)) in idtac));
455 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineNBEOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineNBEOf (p, method)) in idtac));
456 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineArithOf with vm_compute" (fun _ => let __ := eval vm_compute in (PipelineArithOf (p, method)) in idtac));
457 idtac_and_time2 prime bitwidth p (@None unit) "GallinaAxOf with native_compute" (fun _ => let __ := eval native_compute in (GallinaAxOf p) in idtac);
458 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth p method "PipelineFullOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineFullOf (p, method)) in idtac));
459 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth p method "PipelineFullToStringsOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineFullToStringsOf (p, method)) in idtac));
460 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth p method "PipelineNBEOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineNBEOf (p, method)) in idtac));
461 with_each_method ltac:(fun method => idtac_and_time2 prime bitwidth p method "PipelineArithOf with native_compute" (fun _ => let __ := eval native_compute in (PipelineArithOf (p, method)) in idtac));
462 idtac_and_time prime bitwidth p (@None unit) "GallinaAxOf with cbv" (fun _ => let __ := eval cbv in (GallinaAxOf p) in idtac);
463 idtac_and_time prime bitwidth p (@None unit) "GallinaAxComputedOf with cbv" (fun _ => let __ := eval cbv in (GallinaAxComputedOf p) in idtac);
464 idtac_and_time prime bitwidth p (@None unit) "GallinaAxOf with lazy" (fun _ => let __ := eval lazy in (GallinaAxOf p) in idtac);
465 idtac_and_time prime bitwidth p (@None unit) "GallinaAxComputedOf with lazy" (fun _ => let __ := eval lazy in (GallinaAxComputedOf p) in idtac);
466 idtac_and_time prime bitwidth p (@None unit) "GallinaDefOf with cbv_no_rt" (fun _ => let __ := eval cbv_no_rt in (GallinaDefOf p) in idtac);
467 idtac_and_time prime bitwidth p (@None unit) "GallinaDefOf with lazy_no_rt" (fun _ => let __ := eval lazy_no_rt in (GallinaDefOf p) in idtac);
468 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullOf with cbv" (fun _ => let __ := eval cbv in (PipelineFullOf (p, method)) in idtac));
469 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullToStringsOf with cbv" (fun _ => let __ := eval cbv in (PipelineFullToStringsOf (p, method)) in idtac));
470 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineNBEOf with cbv" (fun _ => let __ := eval cbv in (PipelineNBEOf (p, method)) in idtac));
471 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineArithOf with cbv" (fun _ => let __ := eval cbv in (PipelineArithOf (p, method)) in idtac));
472 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullOf with lazy" (fun _ => let __ := eval lazy in (PipelineFullOf (p, method)) in idtac));
473 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineFullToStringsOf with lazy" (fun _ => let __ := eval lazy in (PipelineFullToStringsOf (p, method)) in idtac));
474 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineNBEOf with lazy" (fun _ => let __ := eval lazy in (PipelineNBEOf (p, method)) in idtac));
475 with_each_method ltac:(fun method => idtac_and_time prime bitwidth p method "PipelineArithOf with lazy" (fun _ => let __ := eval lazy in (PipelineArithOf (p, method)) in idtac))
476 | None => idtac "No params for prime" prime
477 end.
478
479 Global Set Printing Width 1000000.
480 End WordByWordMontgomery.
481 (*
482 Global Set Printing Width 1000000.
483 Goal True.
484 UnsaturatedSolinas.perf "2^255-19" 64 0%nat.
485 WordByWordMontgomery.perf "2^384 - 2^128 - 2^96 + 2^32 - 1" 32.
486 Abort.
487 *)
488 (*
489 Definition primes : list string :=
490 [
491 (* single-tap: *)
492
493 "2^127 - 1"; (* "kummer strikes back" *)
494 "2^129 - 25";
495 "2^130 - 5"; (* poly1305 *)
496 "2^137 - 13";
497 "2^140 - 27";
498 "2^141 - 9";
499 "2^150 - 5";
500 "2^150 - 3";
501 "2^152 - 17";
502 "2^158 - 15";
503 "2^165 - 25";
504 "2^166 - 5";
505 "2^171 - 19";
506 "2^174 - 17";
507 "2^174 - 3";
508 "2^189 - 25";
509 "2^190 - 11";
510 "2^191 - 19";
511 "2^194 - 33";
512 "2^196 - 15";
513 "2^198 - 17";
514 "2^206 - 5";
515 "2^212 - 29";
516 "2^213 - 3";
517 "2^221 - 3";
518 "2^222 - 117";
519 "2^226 - 5";
520 "2^230 - 27";
521 "2^235 - 15";
522 "2^243 - 9";
523 "2^251 - 9";
524 "2^255 - 765";
525 "2^255 - 19"; (* curve25519 *)
526 "2^256 - 189";
527 "2^266 - 3";
528 "2^285 - 9";
529 "2^291 - 19";
530 "2^321 - 9";
531 "2^336 - 17";
532 "2^336 - 3";
533 "2^338 - 15";
534 "2^369 - 25";
535 "2^379 - 19";
536 "2^382 - 105";
537 "2^383 - 421";
538 "2^383 - 187";
539 "2^383 - 31";
540 "2^384 - 317";
541 "2^389 - 21";
542 "2^401 - 31";
543 "2^413 - 21";
544 "2^414 - 17";
545 "2^444 - 17";
546 "2^452 - 3";
547 "2^468 - 17";
548 "2^488 - 17";
549 "2^489 - 21";
550 "2^495 - 31";
551 "2^511 - 481";
552 "2^511 - 187";
553 "2^512 - 569";
554 "2^521 - 1"; (* p512 *)
555
556 (* two taps, golden ratio: *)
557
558 "2^192 - 2^64 - 1";
559 "2^216 - 2^108 - 1";
560 "2^322 - 2^161 - 1";
561 "2^416 - 2^208 - 1";
562 "2^448 - 2^224 - 1"; (* goldilocks *)
563 "2^450 - 2^225 - 1";
564 "2^480 - 2^240 - 1"; (* ridinghood *)
565
566 (* two or more taps *)
567
568 "2^205 - 45*2^198 - 1";
569 "2^224 - 2^96 + 1"; (* p224 *)
570 "2^256 - 2^224 + 2^192 + 2^96 - 1"; (* p256 *)
571 "2^256 - 2^32 - 977"; (* bitcoin *)
572 "2^256 - 4294968273"; (* bitcoin, for 64-bit impl *)
573 "2^384 - 2^128 - 2^96 + 2^32 - 1"; (* p384 *)
574
575 (* Montgomery-Friendly *)
576
577 "2^256 - 88*2^240 - 1";
578 "2^254 - 127*2^240 - 1";
579 "2^384 - 79*2^376 - 1";
580 "2^384 - 5*2^368 - 1";
581 "2^512 - 491*2^496 - 1";
582 "2^510 - 290*2^496 - 1"
583 ].
584 *)