| 1 |
(** * Push-Button Synthesis of Saturated Solinas *) |
| 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.BoundsPipeline. |
| 30 |
Require Import Crypto.COperationSpecifications. |
| 31 |
Require Import Crypto.PushButtonSynthesis.ReificationCache. |
| 32 |
Require Import Crypto.PushButtonSynthesis.Primitives. |
| 33 |
Require Import Crypto.PushButtonSynthesis.SaturatedSolinasReificationCache. |
| 34 |
Require Import Crypto.Assembly.Equivalence. |
| 35 |
Import ListNotations. |
| 36 |
Local Open Scope string_scope. Local Open Scope Z_scope. Local Open Scope list_scope. Local Open Scope bool_scope. |
| 37 |
|
| 38 |
Import |
| 39 |
Language.Wf.Compilers |
| 40 |
Language.Compilers |
| 41 |
AbstractInterpretation.Compilers |
| 42 |
Stringification.Language.Compilers. |
| 43 |
Import Compilers.API. |
| 44 |
|
| 45 |
Import COperationSpecifications.Primitives. |
| 46 |
Import COperationSpecifications.Solinas. |
| 47 |
Import COperationSpecifications.SaturatedSolinas. |
| 48 |
|
| 49 |
Import Associational Positional. |
| 50 |
|
| 51 |
Local Coercion Z.of_nat : nat >-> Z. |
| 52 |
Local Coercion QArith_base.inject_Z : Z >-> Q. |
| 53 |
Local Coercion Z.pos : positive >-> Z. |
| 54 |
|
| 55 |
Local Set Keyed Unification. (* needed for making [autorewrite] fast, c.f. COQBUG(https://github.com/coq/coq/issues/9283) *) |
| 56 |
|
| 57 |
Local Opaque reified_mul_gen. (* needed for making [autorewrite] not take a very long time *) |
| 58 |
(* needed for making [autorewrite] with [Set Keyed Unification] fast *) |
| 59 |
Local Opaque expr.Interp. |
| 60 |
|
| 61 |
Section __. |
| 62 |
Context {output_language_api : ToString.OutputLanguageAPI} |
| 63 |
{pipeline_opts : PipelineOptions} |
| 64 |
{pipeline_to_string_opts : PipelineToStringOptions} |
| 65 |
{synthesis_opts : SynthesisOptions} |
| 66 |
(s : Z) |
| 67 |
(c : list (Z * Z)) |
| 68 |
(machine_wordsize : machine_wordsize_opt). |
| 69 |
|
| 70 |
Local Instance override_pipeline_opts : PipelineOptions |
| 71 |
:= {| 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 *) |
| 72 |
|}. |
| 73 |
|
| 74 |
(* We include [0], so that even after bounds relaxation, we can |
| 75 |
notice where the constant 0s are, and remove them. *) |
| 76 |
Definition possible_values_of_machine_wordsize |
| 77 |
:= prefix_with_carry [machine_wordsize]. |
| 78 |
|
| 79 |
Definition n : nat := Z.to_nat (Qceiling (Z.log2_up s / machine_wordsize)). |
| 80 |
Definition m := s - Associational.eval c. |
| 81 |
(* Number of reductions is calculated as follows : |
| 82 |
Let i be the highest limb index of c. Then, each reduction |
| 83 |
decreases the number of extra limbs by (n-i-1). (The -1 comes |
| 84 |
from possibly having an extra high partial product at the end |
| 85 |
of a reduction.) So, to go from the n extra limbs we have |
| 86 |
post-multiplication down to 0, we need ceil (n / (n - i - 1)) |
| 87 |
reductions. In some cases. however, [n - i <= 1], and in |
| 88 |
this case, we do [n] reductions (is this enough?). *) |
| 89 |
Definition nreductions : nat := |
| 90 |
let i := fold_right Z.max 0 (List.map (fun t => Z.log2 (fst t) / machine_wordsize) c) in |
| 91 |
if Z.of_nat n - i <=? 1 |
| 92 |
then n |
| 93 |
else Z.to_nat (Qceiling (Z.of_nat n / (Z.of_nat n - i - 1))). |
| 94 |
Local Notation possible_values := possible_values_of_machine_wordsize. |
| 95 |
Local Notation boundsn := (saturated_bounds n machine_wordsize). |
| 96 |
|
| 97 |
Local Existing Instance default_translate_to_fancy. |
| 98 |
Local Instance no_select_size : no_select_size_opt := no_select_size_of_no_select machine_wordsize. |
| 99 |
Local Instance split_mul_to : split_mul_to_opt := split_mul_to_of_should_split_mul machine_wordsize possible_values. |
| 100 |
Local Instance split_multiret_to : split_multiret_to_opt := split_multiret_to_of_should_split_multiret machine_wordsize possible_values. |
| 101 |
|
| 102 |
(** Note: If you change the name or type signature of this |
| 103 |
function, you will need to update the code in CLI.v *) |
| 104 |
Definition check_args {T} (requests : list string) (res : Pipeline.ErrorT T) |
| 105 |
: Pipeline.ErrorT T |
| 106 |
:= check_args_of_list |
| 107 |
(List.map |
| 108 |
(fun v => (true, v)) |
| 109 |
[((0 <? s - Associational.eval c)%Z, Pipeline.Value_not_ltZ "s - Associational.eval c ≤ 0" 0 (s - Associational.eval c)) |
| 110 |
; (negb (s =? 0)%Z, Pipeline.Values_not_provably_distinctZ "s ≠ 0" s 0) |
| 111 |
; (negb (n =? 0)%nat, Pipeline.Values_not_provably_distinctZ "n ≠ 0" n 0) |
| 112 |
; (0 <? machine_wordsize, Pipeline.Value_not_ltZ "0 < machine_wordsize" 0 machine_wordsize) |
| 113 |
]) |
| 114 |
res. |
| 115 |
|
| 116 |
|
| 117 |
Local Ltac use_curve_good_t := |
| 118 |
repeat first [ use_requests_to_prove_curve_good_t_step |
| 119 |
| assumption |
| 120 |
| lia |
| 121 |
| progress autorewrite with distr_length |
| 122 |
| progress distr_length ]. |
| 123 |
|
| 124 |
Context (requests : list string) |
| 125 |
(curve_good : check_args requests (Success tt) = Success tt). |
| 126 |
|
| 127 |
Lemma use_curve_good |
| 128 |
: 0 < s - Associational.eval c |
| 129 |
/\ s - Associational.eval c <> 0 |
| 130 |
/\ s <> 0 |
| 131 |
/\ 0 < machine_wordsize |
| 132 |
/\ n <> 0%nat. |
| 133 |
Proof using curve_good. |
| 134 |
prepare_use_curve_good (). |
| 135 |
{ use_curve_good_t. } |
| 136 |
Qed. |
| 137 |
|
| 138 |
Local Notation weightf := (weight machine_wordsize 1). |
| 139 |
Local Notation evalf := (eval weightf n). |
| 140 |
Local Notation notations_for_docstring |
| 141 |
:= (CorrectnessStringification.dyn_context.cons |
| 142 |
weightf "weight" |
| 143 |
(CorrectnessStringification.dyn_context.cons |
| 144 |
evalf "eval" |
| 145 |
CorrectnessStringification.dyn_context.nil))%string. |
| 146 |
Local Notation "'docstring_with_summary_from_lemma!' summary correctness" |
| 147 |
:= (docstring_with_summary_from_lemma_with_ctx! |
| 148 |
notations_for_docstring |
| 149 |
summary |
| 150 |
correctness) |
| 151 |
(only parsing, at level 10, summary at next level, correctness at next level). |
| 152 |
|
| 153 |
Definition mul |
| 154 |
:= Pipeline.BoundsPipeline |
| 155 |
false (* subst01 *) |
| 156 |
possible_values |
| 157 |
(reified_mul_gen |
| 158 |
@ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify (machine_wordsize:Z) @ GallinaReify.Reify n @ GallinaReify.Reify nreductions) |
| 159 |
(Some boundsn, (Some boundsn, tt)) |
| 160 |
(Some boundsn, None (* Should be: Some r[0~>0]%zrange, but bounds analysis is not good enough *) ). |
| 161 |
|
| 162 |
Definition smul (prefix : string) |
| 163 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 164 |
:= Eval cbv beta in |
| 165 |
FromPipelineToString! |
| 166 |
machine_wordsize prefix "mul" mul |
| 167 |
(docstring_with_summary_from_lemma! |
| 168 |
(fun fname : string => [text_before_function_name ++ fname ++ " multiplies two field elements."]%string) |
| 169 |
(mul_correct weightf n m boundsn)). |
| 170 |
|
| 171 |
Local Ltac solve_extra_bounds_side_conditions := |
| 172 |
cbn [lower upper fst snd] in *; Bool.split_andb; Z.ltb_to_lt; lia. |
| 173 |
|
| 174 |
Hint Rewrite |
| 175 |
(fun pf => @Rows.eval_mulmod (weight machine_wordsize 1) (@wprops _ _ pf)) |
| 176 |
using solve [ auto with zarith | congruence | solve_extra_bounds_side_conditions ] : push_eval. |
| 177 |
Hint Unfold mulmod : push_eval. |
| 178 |
|
| 179 |
Local Ltac prove_correctness _ := Primitives.prove_correctness use_curve_good. |
| 180 |
|
| 181 |
Lemma mul_correct res |
| 182 |
(Hres : mul = Success res) |
| 183 |
: mul_correct (weight machine_wordsize 1) n m boundsn (Interp res). |
| 184 |
Proof using curve_good. prove_correctness (). Qed. |
| 185 |
|
| 186 |
Lemma Wf_mul res (Hres : mul = Success res) : Wf res. |
| 187 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 188 |
|
| 189 |
Section for_stringification. |
| 190 |
Local Open Scope string_scope. |
| 191 |
Local Open Scope list_scope. |
| 192 |
|
| 193 |
Definition known_functions |
| 194 |
:= [("mul", wrap_s smul)]. |
| 195 |
|
| 196 |
Definition valid_names : string := Eval compute in String.concat ", " (List.map (@fst _ _) known_functions). |
| 197 |
|
| 198 |
(** Note: If you change the name or type signature of this |
| 199 |
function, you will need to update the code in CLI.v *) |
| 200 |
Definition Synthesize (comment_header : list string) (function_name_prefix : string) (requests : list string) |
| 201 |
: list (synthesis_output_kind * string * Pipeline.M (list string)) |
| 202 |
:= Primitives.Synthesize |
| 203 |
machine_wordsize valid_names known_functions (fun _ => nil) all_typedefs! |
| 204 |
check_args |
| 205 |
((ToString.comment_file_header_block |
| 206 |
(comment_header |
| 207 |
++ [""; |
| 208 |
"Computed values:"; |
| 209 |
""; |
| 210 |
" # reductions = " ++ show nreductions]%string))) |
| 211 |
function_name_prefix requests. |
| 212 |
End for_stringification. |
| 213 |
End __. |
| 214 |
|
| 215 |
Module Export Hints. |
| 216 |
#[global] |
| 217 |
Hint Opaque |
| 218 |
mul |
| 219 |
: wf_op_cache. |
| 220 |
#[global] |
| 221 |
Hint Immediate |
| 222 |
Wf_mul |
| 223 |
: wf_op_cache. |
| 224 |
End Hints. |