| 1 |
(** * Push-Button Synthesis of Saturated Reduction *) |
| 2 |
From Coq Require Import String. |
| 3 |
From Coq Require Import Lia. |
| 4 |
From Coq Require Import ZArith. |
| 5 |
From Coq Require Import MSetPositive. |
| 6 |
From Coq Require Import List. |
| 7 |
From Coq Require Import QArith_base Qround. |
| 8 |
From Coq Require Import Derive. |
| 9 |
Require Import Crypto.Util.ErrorT. |
| 10 |
Require Import Crypto.Util.ListUtil. |
| 11 |
Require Import Crypto.Util.ListUtil.FoldBool. |
| 12 |
Require Import Crypto.Util.Strings.Decimal. |
| 13 |
Require Import Crypto.Util.Strings.Show. |
| 14 |
Require Import Crypto.Util.ZRange. |
| 15 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 16 |
Require Import Crypto.Util.ZUtil.Zselect. |
| 17 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 18 |
Require Import Crypto.Util.Tactics.HasBody. |
| 19 |
Require Import Crypto.Util.Tactics.Head. |
| 20 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 21 |
Require Import Rewriter.Language.Wf. |
| 22 |
Require Import Rewriter.Language.Language. |
| 23 |
Require Import Crypto.Language.API. |
| 24 |
Require Import Crypto.AbstractInterpretation.AbstractInterpretation. |
| 25 |
Require Import Crypto.Stringification.Language. |
| 26 |
Require Import Crypto.Arithmetic.Core. |
| 27 |
Require Import Crypto.Arithmetic.ModOps. |
| 28 |
Require Import Crypto.Arithmetic.Saturated. |
| 29 |
Require Import Crypto.Arithmetic.SolinasReduction. |
| 30 |
Require Import Crypto.BoundsPipeline. |
| 31 |
Require Import Crypto.COperationSpecifications. |
| 32 |
Require Import Crypto.PushButtonSynthesis.ReificationCache. |
| 33 |
Require Import Crypto.PushButtonSynthesis.Primitives. |
| 34 |
Require Import Crypto.PushButtonSynthesis.SaturatedSolinasReificationCache. |
| 35 |
Require Import Crypto.PushButtonSynthesis.SolinasReductionReificationCache. |
| 36 |
Require Import Crypto.Assembly.Equivalence. |
| 37 |
Import ListNotations. |
| 38 |
Local Open Scope string_scope. Local Open Scope Z_scope. Local Open Scope list_scope. Local Open Scope bool_scope. |
| 39 |
|
| 40 |
Import |
| 41 |
Language.Wf.Compilers |
| 42 |
Language.Compilers |
| 43 |
AbstractInterpretation.Compilers |
| 44 |
Stringification.Language.Compilers. |
| 45 |
Import Compilers.API. |
| 46 |
|
| 47 |
Import COperationSpecifications.Primitives. |
| 48 |
Import COperationSpecifications.Solinas. |
| 49 |
Import COperationSpecifications.SolinasReduction. |
| 50 |
|
| 51 |
Import Associational Positional. |
| 52 |
Import SolinasReduction. |
| 53 |
|
| 54 |
Local Coercion Z.of_nat : nat >-> Z. |
| 55 |
Local Coercion QArith_base.inject_Z : Z >-> Q. |
| 56 |
Local Coercion Z.pos : positive >-> Z. |
| 57 |
|
| 58 |
Local Set Keyed Unification. (* needed for making [autorewrite] fast, c.f. COQBUG(https://github.com/coq/coq/issues/9283) *) |
| 59 |
|
| 60 |
Local Opaque reified_mul_gen. (* needed for making [autorewrite] not take a very long time *) |
| 61 |
Local Opaque reified_square_gen. |
| 62 |
(* needed for making [autorewrite] with [Set Keyed Unification] fast *) |
| 63 |
Local Opaque expr.Interp. |
| 64 |
|
| 65 |
Section __. |
| 66 |
Context {output_language_api : ToString.OutputLanguageAPI} |
| 67 |
{pipeline_opts : PipelineOptions} |
| 68 |
{pipeline_to_string_opts : PipelineToStringOptions} |
| 69 |
{synthesis_opts : SynthesisOptions} |
| 70 |
(s : Z) |
| 71 |
(c : list (Z * Z)). |
| 72 |
Context (machine_wordsize : machine_wordsize_opt). |
| 73 |
|
| 74 |
Local Instance override_pipeline_opts : PipelineOptions |
| 75 |
:= {| widen_bytes := true (* true, because we don't allow byte-sized things anyway, so we should not expect carries to be widened to byte-size when emitting C code *) |
| 76 |
|}. |
| 77 |
|
| 78 |
(* We include [0], so that even after bounds relaxation, we can |
| 79 |
notice where the constant 0s are, and remove them. *) |
| 80 |
Definition possible_values_of_machine_wordsize |
| 81 |
:= prefix_with_carry [machine_wordsize]. |
| 82 |
|
| 83 |
Definition n : nat := Z.to_nat (Qceiling (Z.log2_up s / machine_wordsize)). |
| 84 |
Definition m := s - Associational.eval c. |
| 85 |
Definition weight := UniformWeight.uweight machine_wordsize. |
| 86 |
Definition up_bound := 2 ^ (machine_wordsize / 4). |
| 87 |
Definition base : Z := 2 ^ machine_wordsize. |
| 88 |
|
| 89 |
Local Notation possible_values := possible_values_of_machine_wordsize. |
| 90 |
Local Notation boundsn := (saturated_bounds n machine_wordsize). |
| 91 |
Local Notation bounds4 := (saturated_bounds 4 machine_wordsize). |
| 92 |
|
| 93 |
Local Existing Instance default_translate_to_fancy. |
| 94 |
Local Instance no_select_size : no_select_size_opt := no_select_size_of_no_select machine_wordsize. |
| 95 |
Local Instance split_mul_to : split_mul_to_opt := split_mul_to_of_should_split_mul machine_wordsize possible_values. |
| 96 |
Local Instance split_multiret_to : split_multiret_to_opt := split_multiret_to_of_should_split_multiret machine_wordsize possible_values. |
| 97 |
|
| 98 |
(** Note: If you change the name or type signature of this |
| 99 |
function, you will need to update the code in CLI.v *) |
| 100 |
Definition check_args {T} (requests : list string) (res : Pipeline.ErrorT T) |
| 101 |
: Pipeline.ErrorT T |
| 102 |
:= check_args_of_list |
| 103 |
(List.map |
| 104 |
(fun v => (true, v)) |
| 105 |
[((0 <? s)%Z, Pipeline.Value_not_ltZ "0 < s" 0 s) |
| 106 |
; ((0 <? Associational.eval c)%Z, Pipeline.Value_not_ltZ "0 < Associational.eval c" 0 (Associational.eval c)) |
| 107 |
; ((0 <? s - Associational.eval c)%Z, Pipeline.Value_not_ltZ "0 < s - Associational.eval c" 0 (s - Associational.eval c)) |
| 108 |
; (negb (s =? 0)%Z, Pipeline.Values_not_provably_distinctZ "s ≠ 0" s 0) |
| 109 |
; (negb (n =? 0)%nat, Pipeline.Values_not_provably_distinctZ "n ≠ 0" n 0) |
| 110 |
; ((n =? 4)%Z, Pipeline.Values_not_provably_equalZ "n = 4" n 4) |
| 111 |
; (0 <? machine_wordsize, Pipeline.Value_not_ltZ "0 < machine_wordsize" 0 machine_wordsize) |
| 112 |
; (machine_wordsize =? 64, Pipeline.Values_not_provably_equalZ "machine_wordsize = 64" machine_wordsize 64) |
| 113 |
; ((1 <? n)%nat, Pipeline.Value_not_ltZ "1 < n" 1 n) |
| 114 |
; (fst (Rows.adjust_s weight (S (S n)) s) =? weight n, Pipeline.Values_not_provably_equalZ "fst (Rows.adjust_s weight (S (S n)) s) = weight n" (fst (Rows.adjust_s weight (S (S n)) s)) (weight n)) |
| 115 |
; (snd (Rows.adjust_s weight (S (S n)) s), Pipeline.Invalid_argument "tmp") |
| 116 |
; (weight n / s * Associational.eval c <? up_bound, Pipeline.Value_not_ltZ "weight n / s * Associational.eval c < up_bound" (weight n / s * Associational.eval c) up_bound) |
| 117 |
]) |
| 118 |
res. |
| 119 |
|
| 120 |
Local Ltac use_curve_good_t := |
| 121 |
repeat first [ use_requests_to_prove_curve_good_t_step |
| 122 |
| assumption |
| 123 |
| lia |
| 124 |
| progress autorewrite with distr_length |
| 125 |
| progress distr_length ]. |
| 126 |
|
| 127 |
Context (requests : list string) |
| 128 |
(curve_good : check_args requests (Success tt) = Success tt). |
| 129 |
|
| 130 |
Lemma use_curve_good |
| 131 |
: (n > 1)%nat /\ |
| 132 |
s > 0 /\ |
| 133 |
Associational.eval c > 0 /\ |
| 134 |
s - Associational.eval c <> 0 /\ |
| 135 |
machine_wordsize = 64 /\ |
| 136 |
base <> 0 /\ |
| 137 |
Rows.adjust_s weight (S (S n)) s = (weight n, true) /\ |
| 138 |
weight n / s * Associational.eval c < up_bound. |
| 139 |
Proof using curve_good. |
| 140 |
prepare_use_curve_good (). |
| 141 |
{ use_curve_good_t. } |
| 142 |
{ use_curve_good_t. } |
| 143 |
{ use_curve_good_t. } |
| 144 |
{ unfold base. |
| 145 |
apply Z.pow_nonzero; use_curve_good_t. } |
| 146 |
{ lazymatch goal with |
| 147 |
| |- ?x = _ => rewrite surjective_pairing with (p:=x) |
| 148 |
end. |
| 149 |
congruence. } |
| 150 |
Qed. |
| 151 |
|
| 152 |
Local Notation evalf := (eval weight n). |
| 153 |
Local Notation weightf := weight. |
| 154 |
Local Notation notations_for_docstring |
| 155 |
:= (CorrectnessStringification.dyn_context.cons |
| 156 |
weightf "weight" |
| 157 |
(CorrectnessStringification.dyn_context.cons |
| 158 |
evalf "eval" |
| 159 |
CorrectnessStringification.dyn_context.nil))%string. |
| 160 |
Local Notation "'docstring_with_summary_from_lemma!' summary correctness" |
| 161 |
:= (docstring_with_summary_from_lemma_with_ctx! |
| 162 |
notations_for_docstring |
| 163 |
summary |
| 164 |
correctness) |
| 165 |
(only parsing, at level 10, summary at next level, correctness at next level). |
| 166 |
|
| 167 |
Definition mul |
| 168 |
:= Pipeline.BoundsPipeline |
| 169 |
false (* subst01 *) |
| 170 |
possible_values |
| 171 |
(reified_mul_gen |
| 172 |
@ GallinaReify.Reify base |
| 173 |
@ GallinaReify.Reify s |
| 174 |
@ GallinaReify.Reify c |
| 175 |
@ GallinaReify.Reify n) |
| 176 |
(Some boundsn, (Some boundsn, tt)) |
| 177 |
(Some boundsn). |
| 178 |
|
| 179 |
Definition square |
| 180 |
:= Pipeline.BoundsPipeline |
| 181 |
false (* subst01 *) |
| 182 |
possible_values |
| 183 |
(reified_square_gen |
| 184 |
@ GallinaReify.Reify base |
| 185 |
@ GallinaReify.Reify s |
| 186 |
@ GallinaReify.Reify c |
| 187 |
@ GallinaReify.Reify n) |
| 188 |
(Some boundsn, tt) |
| 189 |
(Some boundsn). |
| 190 |
|
| 191 |
Definition smul (prefix : string) |
| 192 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 193 |
:= Eval cbv beta in |
| 194 |
FromPipelineToString! |
| 195 |
machine_wordsize prefix "mul" mul |
| 196 |
(docstring_with_summary_from_lemma! |
| 197 |
(fun fname : string => [text_before_function_name ++ fname ++ " multiplies two field elements."]%string) |
| 198 |
(mul_correct weightf n m boundsn)). |
| 199 |
|
| 200 |
Definition ssquare (prefix : string) |
| 201 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 202 |
:= Eval cbv beta in |
| 203 |
FromPipelineToString! |
| 204 |
machine_wordsize prefix "square" square |
| 205 |
(docstring_with_summary_from_lemma! |
| 206 |
(fun fname : string => [text_before_function_name ++ fname ++ " squares a field element."]%string) |
| 207 |
(sqr_correct weightf n m boundsn)). |
| 208 |
|
| 209 |
Local Ltac solve_extra_bounds_side_conditions := |
| 210 |
cbn [lower upper fst snd] in *; Bool.split_andb; Z.ltb_to_lt; lia. |
| 211 |
|
| 212 |
Local Ltac prove_correctness _ := Primitives.prove_correctness use_curve_good. |
| 213 |
|
| 214 |
Lemma mul_correct res |
| 215 |
(Hres : mul = Success res) |
| 216 |
: mul_correct weight n m boundsn (Interp res). |
| 217 |
Proof using curve_good. |
| 218 |
prove_correctness (). |
| 219 |
cbv [evalf weightf weight up_bound] in *. |
| 220 |
match goal with |
| 221 |
| H : machine_wordsize = _ |- _ => rewrite H in * |
| 222 |
end. |
| 223 |
apply (fun pf => @SolinasReduction.SolinasReduction.mulmod_correct (@wprops _ _ pf)); auto; lia. |
| 224 |
Qed. |
| 225 |
|
| 226 |
Lemma Wf_mul res (Hres : mul = Success res) : Wf res. |
| 227 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 228 |
|
| 229 |
Lemma square_correct res |
| 230 |
(Hres : square = Success res) |
| 231 |
: sqr_correct weight n m boundsn (Interp res). |
| 232 |
Proof using curve_good. |
| 233 |
|
| 234 |
prove_correctness (). |
| 235 |
cbv [evalf weightf weight up_bound] in *. |
| 236 |
match goal with |
| 237 |
| H : machine_wordsize = _ |- _ => rewrite H in * |
| 238 |
end. |
| 239 |
apply (fun pf => @SolinasReduction.SolinasReduction.squaremod_correct (@wprops _ _ pf)); auto; lia. |
| 240 |
Qed. |
| 241 |
|
| 242 |
Lemma Wf_square res (Hres : square = Success res) : Wf res. |
| 243 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 244 |
|
| 245 |
Section for_stringification. |
| 246 |
Local Open Scope string_scope. |
| 247 |
Local Open Scope list_scope. |
| 248 |
|
| 249 |
Definition known_functions |
| 250 |
:= [("mul", wrap_s smul); ("square", wrap_s ssquare)]. |
| 251 |
|
| 252 |
Definition valid_names : string := Eval compute in String.concat ", " (List.map (@fst _ _) known_functions). |
| 253 |
|
| 254 |
(** Note: If you change the name or type signature of this |
| 255 |
function, you will need to update the code in CLI.v *) |
| 256 |
Definition Synthesize (comment_header : list string) (function_name_prefix : string) (requests : list string) |
| 257 |
: list (synthesis_output_kind * string * Pipeline.M (list string)) |
| 258 |
:= Primitives.Synthesize |
| 259 |
machine_wordsize valid_names known_functions (fun _ => nil) all_typedefs! |
| 260 |
check_args |
| 261 |
((ToString.comment_file_header_block |
| 262 |
(comment_header |
| 263 |
++ [""; |
| 264 |
"Computed values:"; |
| 265 |
""]%string))) |
| 266 |
function_name_prefix requests. |
| 267 |
End for_stringification. |
| 268 |
End __. |
| 269 |
|
| 270 |
Module Export Hints. |
| 271 |
#[global] |
| 272 |
Hint Opaque |
| 273 |
mul |
| 274 |
: wf_op_cache. |
| 275 |
#[global] |
| 276 |
Hint Immediate |
| 277 |
Wf_mul |
| 278 |
: wf_op_cache. |
| 279 |
|
| 280 |
#[global] |
| 281 |
Hint Opaque |
| 282 |
square |
| 283 |
: wf_op_cache. |
| 284 |
#[global] |
| 285 |
Hint Immediate |
| 286 |
Wf_square |
| 287 |
: wf_op_cache. |
| 288 |
End Hints. |