(** * Push-Button Synthesis of Saturated Solinas *) From Coq Require Import String. From Coq Require Import Lia. From Coq Require Import ZArith. From Coq Require Import MSetPositive. From Coq Require Import List. From Coq Require Import QArith_base Qround. From Coq Require Import Derive. Require Import Crypto.Util.ErrorT. Require Import Crypto.Util.ListUtil. Require Import Crypto.Util.ListUtil.FoldBool. Require Import Crypto.Util.Strings.Decimal. Require Import Crypto.Util.Strings.Show. Require Import Crypto.Util.ZRange. Require Import Crypto.Util.ZUtil.Definitions. Require Import Crypto.Util.ZUtil.Zselect. Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. Require Import Crypto.Util.Tactics.HasBody. Require Import Crypto.Util.Tactics.Head. Require Import Crypto.Util.Tactics.SpecializeBy. Require Import Rewriter.Language.Wf. Require Import Rewriter.Language.Language. Require Import Crypto.Language.API. Require Import Crypto.AbstractInterpretation.AbstractInterpretation. Require Import Crypto.Stringification.Language. Require Import Crypto.Arithmetic.Core. Require Import Crypto.Arithmetic.ModOps. Require Import Crypto.Arithmetic.Saturated. Require Import Crypto.BoundsPipeline. Require Import Crypto.COperationSpecifications. Require Import Crypto.PushButtonSynthesis.ReificationCache. Require Import Crypto.PushButtonSynthesis.Primitives. Require Import Crypto.PushButtonSynthesis.SaturatedSolinasReificationCache. Require Import Crypto.Assembly.Equivalence. Import ListNotations. Local Open Scope string_scope. Local Open Scope Z_scope. Local Open Scope list_scope. Local Open Scope bool_scope. Import Language.Wf.Compilers Language.Compilers AbstractInterpretation.Compilers Stringification.Language.Compilers. Import Compilers.API. Import COperationSpecifications.Primitives. Import COperationSpecifications.Solinas. Import COperationSpecifications.SaturatedSolinas. Import Associational Positional. Local Coercion Z.of_nat : nat >-> Z. Local Coercion QArith_base.inject_Z : Z >-> Q. Local Coercion Z.pos : positive >-> Z. Local Set Keyed Unification. (* needed for making [autorewrite] fast, c.f. COQBUG(https://github.com/coq/coq/issues/9283) *) Local Opaque reified_mul_gen. (* needed for making [autorewrite] not take a very long time *) (* needed for making [autorewrite] with [Set Keyed Unification] fast *) Local Opaque expr.Interp. Section __. Context {output_language_api : ToString.OutputLanguageAPI} {pipeline_opts : PipelineOptions} {pipeline_to_string_opts : PipelineToStringOptions} {synthesis_opts : SynthesisOptions} (s : Z) (c : list (Z * Z)) (machine_wordsize : machine_wordsize_opt). Local Instance override_pipeline_opts : PipelineOptions := {| 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 *) |}. (* We include [0], so that even after bounds relaxation, we can notice where the constant 0s are, and remove them. *) Definition possible_values_of_machine_wordsize := prefix_with_carry [machine_wordsize]. Definition n : nat := Z.to_nat (Qceiling (Z.log2_up s / machine_wordsize)). Definition m := s - Associational.eval c. (* Number of reductions is calculated as follows : Let i be the highest limb index of c. Then, each reduction decreases the number of extra limbs by (n-i-1). (The -1 comes from possibly having an extra high partial product at the end of a reduction.) So, to go from the n extra limbs we have post-multiplication down to 0, we need ceil (n / (n - i - 1)) reductions. In some cases. however, [n - i <= 1], and in this case, we do [n] reductions (is this enough?). *) Definition nreductions : nat := let i := fold_right Z.max 0 (List.map (fun t => Z.log2 (fst t) / machine_wordsize) c) in if Z.of_nat n - i <=? 1 then n else Z.to_nat (Qceiling (Z.of_nat n / (Z.of_nat n - i - 1))). Local Notation possible_values := possible_values_of_machine_wordsize. Local Notation boundsn := (saturated_bounds n machine_wordsize). Local Existing Instance default_translate_to_fancy. Local Instance no_select_size : no_select_size_opt := no_select_size_of_no_select machine_wordsize. Local Instance split_mul_to : split_mul_to_opt := split_mul_to_of_should_split_mul machine_wordsize possible_values. Local Instance split_multiret_to : split_multiret_to_opt := split_multiret_to_of_should_split_multiret machine_wordsize possible_values. (** Note: If you change the name or type signature of this function, you will need to update the code in CLI.v *) Definition check_args {T} (requests : list string) (res : Pipeline.ErrorT T) : Pipeline.ErrorT T := check_args_of_list (List.map (fun v => (true, v)) [((0 0 /\ s <> 0 /\ 0 < machine_wordsize /\ n <> 0%nat. Proof using curve_good. prepare_use_curve_good (). { use_curve_good_t. } Qed. Local Notation weightf := (weight machine_wordsize 1). Local Notation evalf := (eval weightf n). Local Notation notations_for_docstring := (CorrectnessStringification.dyn_context.cons weightf "weight" (CorrectnessStringification.dyn_context.cons evalf "eval" CorrectnessStringification.dyn_context.nil))%string. Local Notation "'docstring_with_summary_from_lemma!' summary correctness" := (docstring_with_summary_from_lemma_with_ctx! notations_for_docstring summary correctness) (only parsing, at level 10, summary at next level, correctness at next level). Definition mul := Pipeline.BoundsPipeline false (* subst01 *) possible_values (reified_mul_gen @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify (machine_wordsize:Z) @ GallinaReify.Reify n @ GallinaReify.Reify nreductions) (Some boundsn, (Some boundsn, tt)) (Some boundsn, None (* Should be: Some r[0~>0]%zrange, but bounds analysis is not good enough *) ). Definition smul (prefix : string) : string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) := Eval cbv beta in FromPipelineToString! machine_wordsize prefix "mul" mul (docstring_with_summary_from_lemma! (fun fname : string => [text_before_function_name ++ fname ++ " multiplies two field elements."]%string) (mul_correct weightf n m boundsn)). Local Ltac solve_extra_bounds_side_conditions := cbn [lower upper fst snd] in *; Bool.split_andb; Z.ltb_to_lt; lia. Hint Rewrite (fun pf => @Rows.eval_mulmod (weight machine_wordsize 1) (@wprops _ _ pf)) using solve [ auto with zarith | congruence | solve_extra_bounds_side_conditions ] : push_eval. Hint Unfold mulmod : push_eval. Local Ltac prove_correctness _ := Primitives.prove_correctness use_curve_good. Lemma mul_correct res (Hres : mul = Success res) : mul_correct (weight machine_wordsize 1) n m boundsn (Interp res). Proof using curve_good. prove_correctness (). Qed. Lemma Wf_mul res (Hres : mul = Success res) : Wf res. Proof using Type. prove_pipeline_wf (). Qed. Section for_stringification. Local Open Scope string_scope. Local Open Scope list_scope. Definition known_functions := [("mul", wrap_s smul)]. Definition valid_names : string := Eval compute in String.concat ", " (List.map (@fst _ _) known_functions). (** Note: If you change the name or type signature of this function, you will need to update the code in CLI.v *) Definition Synthesize (comment_header : list string) (function_name_prefix : string) (requests : list string) : list (synthesis_output_kind * string * Pipeline.M (list string)) := Primitives.Synthesize machine_wordsize valid_names known_functions (fun _ => nil) all_typedefs! check_args ((ToString.comment_file_header_block (comment_header ++ [""; "Computed values:"; ""; " # reductions = " ++ show nreductions]%string))) function_name_prefix requests. End for_stringification. End __. Module Export Hints. #[global] Hint Opaque mul : wf_op_cache. #[global] Hint Immediate Wf_mul : wf_op_cache. End Hints.