Debian Codegraph @

src/PushButtonSynthesis/BarrettReduction.v

raw
1 (** * Push-Button Synthesis of Barrett Reduction *)
2 From Coq Require Import String.
3 From Coq Require Import ZArith.
4 From Coq Require Import List.
5 From Coq Require Import Derive.
6 From Coq Require Import Lia.
7 Require Import Crypto.Util.ErrorT.
8 Require Import Crypto.Util.ListUtil.
9 Require Import Crypto.Util.ZRange.
10 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
11 Require Import Rewriter.Language.Language.
12 Require Import Crypto.Language.API.
13 Require Import Crypto.Stringification.Language.
14 Require Import Crypto.Arithmetic.BarrettReduction.
15 Require Import Crypto.Arithmetic.Core.
16 Require Import Crypto.Arithmetic.ModOps.
17 Require Import Crypto.Arithmetic.Partition.
18 Require Import Crypto.Arithmetic.UniformWeight.
19 Require Import Crypto.BoundsPipeline.
20 Require Import Crypto.Fancy.Compiler.
21 Require Import Crypto.COperationSpecifications.
22 Require Import Crypto.PushButtonSynthesis.ReificationCache.
23 Require Import Crypto.PushButtonSynthesis.Primitives.
24 Require Import Crypto.PushButtonSynthesis.BarrettReductionReificationCache.
25 Require Import Crypto.PushButtonSynthesis.InvertHighLow.
26 Import ListNotations.
27 Local Open Scope string_scope. Local Open Scope Z_scope. Local Open Scope list_scope. Local Open Scope bool_scope.
28
29 Import
30 Language.Compilers
31 Language.Wf.Compilers
32 Stringification.Language.Compilers.
33 Import Compilers.API.
34
35 Import COperationSpecifications.Primitives.
36 Import COperationSpecifications.BarrettReduction.
37
38 Import Associational Positional.
39
40 Local Set Keyed Unification. (* needed for making [autorewrite] fast, c.f. COQBUG(https://github.com/coq/coq/issues/9283) *)
41
42 Local Opaque reified_barrett_red_gen. (* needed for making [autorewrite] not take a very long time *)
43
44 Section rbarrett_red.
45 Context {output_language_api : ToString.OutputLanguageAPI}
46 {pipeline_opts : PipelineOptions}
47 {pipeline_to_string_opts : PipelineToStringOptions}
48 {synthesis_opts : SynthesisOptions}
49 (M : Z)
50 (machine_wordsize : machine_wordsize_opt).
51
52 Let value_range := r[0 ~> (2^machine_wordsize - 1)%Z]%zrange.
53 Let flag_range := r[0 ~> 1]%zrange.
54 Let bound := Some value_range.
55 Let mu := (2 ^ (2 * machine_wordsize)) / M.
56 Let muLow := mu mod (2 ^ machine_wordsize).
57 Let consts_list := [M; muLow].
58
59 Definition possible_values_of_machine_wordsize
60 := [1; machine_wordsize / 2; machine_wordsize; 2 * machine_wordsize]%Z.
61 Local Notation possible_values := possible_values_of_machine_wordsize.
62
63 Local Instance no_select_size : no_select_size_opt := no_select_size_of_no_select machine_wordsize.
64 Local Instance split_mul_to : split_mul_to_opt := split_mul_to_of_should_split_mul machine_wordsize possible_values.
65 Local Instance split_multiret_to : split_multiret_to_opt := split_multiret_to_of_should_split_multiret machine_wordsize possible_values.
66
67 Local Instance fancy_args : translate_to_fancy_opt
68 := (Some {| BoundsPipeline.invert_low log2wordsize := invert_low log2wordsize consts_list;
69 BoundsPipeline.invert_high log2wordsize := invert_high log2wordsize consts_list;
70 BoundsPipeline.value_range := value_range;
71 BoundsPipeline.flag_range := flag_range |}).
72
73 Local Instance fancy_args_good : translate_to_fancy_opt_correct.
74 Proof using Type.
75 cbv [translate_to_fancy_opt_correct translate_to_fancy fancy_args invert_low invert_high constant_to_scalar constant_to_scalar_single consts_list fold_right];
76 split; intros; break_innermost_match_hyps; Z.ltb_to_lt; subst; congruence.
77 Qed.
78
79 Lemma mut_correct :
80 0 < machine_wordsize ->
81 Partition.partition (uweight machine_wordsize) (1 + 1) (muLow + 2 ^ machine_wordsize) = [muLow; 1].
82 Proof using mu.
83 intros; cbn. subst muLow.
84 assert (0 < 2^machine_wordsize) by ZeroBounds.Z.zero_bounds.
85 pose proof (Z.mod_pos_bound mu (2^machine_wordsize) ltac:(lia)).
86 rewrite !uweight_S, weight_0; auto using uwprops with lia.
87 autorewrite with zsimplify.
88 Modulo.push_Zmod. autorewrite with zsimplify. Modulo.pull_Zmod.
89 rewrite <-Modulo.Z.mod_pull_div by lia.
90 autorewrite with zsimplify. RewriteModSmall.Z.rewrite_mod_small.
91 reflexivity.
92 Qed.
93 Lemma Mt_correct :
94 0 < machine_wordsize ->
95 2^(machine_wordsize - 1) < M < 2^machine_wordsize ->
96 Partition.partition (uweight machine_wordsize) 1 M = [M].
97 Proof using Type.
98 intros; cbn. assert (0 < 2^(machine_wordsize-1)) by ZeroBounds.Z.zero_bounds.
99 rewrite !uweight_S, weight_0; auto using uwprops with lia.
100 autorewrite with zsimplify. RewriteModSmall.Z.rewrite_mod_small.
101 reflexivity.
102 Qed.
103
104 (** Note: If you change the name or type signature of this
105 function, you will need to update the code in CLI.v *)
106 Definition check_args {T} (requests : list string) (res : Pipeline.ErrorT T)
107 : Pipeline.ErrorT T
108 := fold_right
109 (fun '(b, e) k => if b:bool then Error e else k)
110 res
111 [
112 ((negb (1 <? machine_wordsize))%Z, Pipeline.Value_not_ltZ "machine_wordsize ≤ 1" 1 machine_wordsize);
113 ((negb (2 ^ (machine_wordsize-1) <? M))%Z, Pipeline.Value_not_ltZ "M ≤ 2^(machine_wordsize-1)" (2^(machine_wordsize-1)) M);
114 ((negb (M <? 2 ^ machine_wordsize))%Z, Pipeline.Value_not_ltZ "2 ^ machine_wordsize ≤ M" M (2^machine_wordsize));
115 ((negb (muLow + 2 ^ machine_wordsize =? ((2 ^ 2) ^ machine_wordsize) / M))%Z, Pipeline.Values_not_provably_equalZ "muLow + 2^machine_wordsize ≠ (2 ^ 2) ^ machine_wordsize) / M" (muLow + 2^machine_wordsize) (((2 ^ 2) ^ machine_wordsize) / M));
116 (negb ((2 * (((2 ^ 2) ^ machine_wordsize) mod M) <=? 2 ^ (machine_wordsize + 1) - (muLow + 2 ^ machine_wordsize)))%Z, Pipeline.Value_not_leZ ("(2 * ((2 ^ 2) ^ machine_wordsize) mod M) 2 ^ (machine_wordsize + 1) - (muLow + 2 ^ machine_wordsize)") (2 * (((2 ^ 2) ^ machine_wordsize) mod M)) (2 ^ (machine_wordsize + 1) - (muLow + 2 ^ machine_wordsize))) ].
117
118 Local Arguments Z.mul !_ !_.
119
120 Local Ltac use_curve_good_t :=
121 repeat first [ assumption
122 | progress cbv [EquivModulo.Z.equiv_modulo]
123 | progress rewrite ?Z.mul_0_r, ?Pos.mul_1_r, ?Z.mul_1_r in *
124 | reflexivity
125 | lia
126 | progress cbn in *
127 | progress intros
128 | solve [ auto with zarith ]
129 | rewrite Z.log2_pow2 by use_curve_good_t ].
130
131 Context (requests : list string)
132 (curve_good : check_args requests (Success tt) = Success tt).
133
134 Lemma use_curve_good
135 : 1 < machine_wordsize
136 /\ 2 ^ (machine_wordsize - 1) <= M < 2 ^ machine_wordsize
137 /\ muLow + 2 ^ machine_wordsize = (2 ^ 2) ^ machine_wordsize / M
138 /\ 2 ^ (machine_wordsize - 1) < M < 2 ^ machine_wordsize
139 /\ 2 * ((2 ^ 2) ^ machine_wordsize mod M) <= 2 ^ (machine_wordsize + 1) - (muLow + 2 ^ machine_wordsize).
140 Proof using curve_good.
141 prepare_use_curve_good ().
142 { use_curve_good_t. }
143 Qed.
144
145 Definition barrett_red
146 := Pipeline.BoundsPipeline
147 false (* subst01 *)
148 possible_values
149 (reified_barrett_red_gen
150 @ GallinaReify.Reify M
151 @ GallinaReify.Reify (machine_wordsize:Z)
152 @ GallinaReify.Reify (machine_wordsize:Z)
153 @ GallinaReify.Reify 1%nat
154 @ GallinaReify.Reify [muLow;1]
155 @ GallinaReify.Reify [M])
156 (bound, (bound, tt))
157 bound.
158
159 Definition sbarrett_red (prefix : string)
160 : string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _))
161 := Eval cbv beta in
162 FromPipelineToString!
163 machine_wordsize prefix "barrett_red" barrett_red
164 (fun _ _ _ => @nil string).
165
166 Local Ltac solve_barrett_red_preconditions :=
167 repeat first [ lia
168 | assumption
169 | match goal with |- ?x = ?x => reflexivity end
170 | apply use_curve_good
171 | progress autorewrite with zsimplify
172 | progress intros
173 | progress cbv [weight]
174 | rewrite mut_correct
175 | rewrite Mt_correct
176 | rewrite uweight_eq_alt'
177 | rewrite Z.pow_mul_r by lia ].
178
179 Local Strategy -100 [barrett_red]. (* needed for making Qed not take forever *)
180 Local Opaque expr.Interp. (* to prevent [autorewrite with interp_gen_cache] from taking forever *)
181 Lemma barrett_red_correct res (Hres : barrett_red = Success res)
182 : barrett_red_correct machine_wordsize M (API.Interp res).
183 Proof using M curve_good.
184 cbv [barrett_red_correct]; intros.
185 assert (1 < machine_wordsize) by apply use_curve_good.
186 pose proof (Z.mod_pos_bound mu (2^machine_wordsize) ltac:(lia)).
187 rewrite <-Fancy.fancy_reduce_correct with (mu := muLow + 2^machine_wordsize) (width:=machine_wordsize) (sz:=1%nat) (mut:=[muLow;1]) (Mt:=[M]) by solve_barrett_red_preconditions.
188 remember Fancy.fancy_reduce eqn:?. (* to prevent [reflexivity] from taking forever *)
189 prove_correctness' ltac:(fun _ => idtac) use_curve_good.
190 { subst; reflexivity. }
191 { cbv [ZRange.type.base.option.is_bounded_by ZRange.type.base.is_bounded_by bound is_bounded_by_bool value_range upper lower]. rewrite Bool.andb_true_iff, !Z.leb_le. lia. }
192 { cbv [ZRange.type.base.option.is_bounded_by ZRange.type.base.is_bounded_by bound is_bounded_by_bool value_range upper lower]. rewrite Bool.andb_true_iff, !Z.leb_le. lia. }
193 Qed.
194
195 Lemma Wf_barrett_red res (Hres : barrett_red = Success res) : Wf res.
196 Proof using Type. prove_pipeline_wf (). Qed.
197 End rbarrett_red.
198
199 Module Export Hints.
200 #[global]
201 Hint Opaque
202 barrett_red
203 : wf_op_cache.
204 #[global]
205 Hint Immediate
206 Wf_barrett_red
207 : wf_op_cache.
208 End Hints.