| 1 |
(** * Push-Button Synthesis of Unsaturated 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.ZRange. |
| 14 |
Require Import Crypto.Util.Strings.Show. |
| 15 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 16 |
Require Import Crypto.Util.ZUtil.Zselect. |
| 17 |
Require Import Crypto.Util.ZUtil.Log2. |
| 18 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 19 |
Require Import Crypto.Util.ZUtil.Tactics.PeelLe. |
| 20 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 21 |
Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos. |
| 22 |
Require Import Crypto.Util.Tactics.HasBody. |
| 23 |
Require Import Crypto.Util.Tactics.Head. |
| 24 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 25 |
Require Import Rewriter.Language.Wf. |
| 26 |
Require Import Rewriter.Language.Language. |
| 27 |
Require Import Crypto.Language.API. |
| 28 |
Require Import Crypto.AbstractInterpretation.AbstractInterpretation. |
| 29 |
Require Import Crypto.Stringification.Language. |
| 30 |
Require Import Crypto.Arithmetic.Core. |
| 31 |
Require Import Crypto.Arithmetic.ModOps. |
| 32 |
Require Import Crypto.Arithmetic.BaseConversion. |
| 33 |
Require Import Crypto.Arithmetic.Partition. |
| 34 |
Require Import Crypto.BoundsPipeline. |
| 35 |
Require Import Crypto.COperationSpecifications. |
| 36 |
Require Import Crypto.UnsaturatedSolinasHeuristics. |
| 37 |
Require Import Crypto.PushButtonSynthesis.ReificationCache. |
| 38 |
Require Import Crypto.PushButtonSynthesis.Primitives. |
| 39 |
Require Import Crypto.PushButtonSynthesis.BaseConversionReificationCache. |
| 40 |
Require Import Crypto.Assembly.Equivalence. |
| 41 |
Import ListNotations. |
| 42 |
Local Open Scope string_scope. Local Open Scope Z_scope. Local Open Scope list_scope. Local Open Scope bool_scope. |
| 43 |
|
| 44 |
Import |
| 45 |
Language.Wf.Compilers |
| 46 |
Language.Compilers |
| 47 |
AbstractInterpretation.Compilers |
| 48 |
Stringification.Language.Compilers. |
| 49 |
Import Compilers.API. |
| 50 |
|
| 51 |
Import COperationSpecifications.Primitives. |
| 52 |
Import COperationSpecifications.BaseConversion. |
| 53 |
|
| 54 |
Import Associational Positional. |
| 55 |
|
| 56 |
Local Coercion Z.of_nat : nat >-> Z. |
| 57 |
Local Coercion QArith_base.inject_Z : Z >-> Q. |
| 58 |
Local Coercion Z.pos : positive >-> Z. |
| 59 |
|
| 60 |
Local Set Keyed Unification. (* needed for making [autorewrite] fast, c.f. COQBUG(https://github.com/coq/coq/issues/9283) *) |
| 61 |
|
| 62 |
(* needed for making [autorewrite] not take a very long time *) |
| 63 |
Local Opaque |
| 64 |
reified_convert_bases_gen |
| 65 |
expr.Interp. |
| 66 |
|
| 67 |
Inductive bounds := exactly (_ : list Z) | use_prime | use_bitwidth. |
| 68 |
|
| 69 |
Global Instance show_bounds : Show bounds |
| 70 |
:= fun v |
| 71 |
=> match v with |
| 72 |
| use_prime => "use_prime" |
| 73 |
| use_bitwidth => "use_bitwidth" |
| 74 |
| exactly l => @show_list _ PowersOfTwo.show_Z l |
| 75 |
end%string. |
| 76 |
|
| 77 |
Definition default_bounds : bounds := use_prime. |
| 78 |
|
| 79 |
Section __. |
| 80 |
Context {output_language_api : ToString.OutputLanguageAPI} |
| 81 |
{pipeline_opts : PipelineOptions} |
| 82 |
{pipeline_to_string_opts : PipelineToStringOptions} |
| 83 |
{synthesis_opts : SynthesisOptions} |
| 84 |
(s : Z) (c : list (Z * Z)) |
| 85 |
(src_n : nat) |
| 86 |
(src_limbwidth : Q) |
| 87 |
(dst_limbwidth : Q) |
| 88 |
(machine_wordsize : machine_wordsize_opt) |
| 89 |
(inbounds_multiplier : option Q) |
| 90 |
(outbounds_multiplier : option Q) |
| 91 |
(inbounds : bounds) |
| 92 |
(outbounds : bounds). |
| 93 |
|
| 94 |
Local Notation src_weight := (weight (Qnum src_limbwidth) (Qden src_limbwidth)). |
| 95 |
Local Notation dst_weight := (weight (Qnum dst_limbwidth) (Qden dst_limbwidth)). |
| 96 |
|
| 97 |
Local Notation force_power_of_two v |
| 98 |
:= (2^Z.log2_up v) (only parsing). |
| 99 |
|
| 100 |
Let in_upperbounds : list Z |
| 101 |
:= List.map |
| 102 |
(fun v : Z => Qceiling (Option.value inbounds_multiplier 1 * v)) |
| 103 |
(let encode := Partition.partition src_weight src_n in |
| 104 |
match inbounds with |
| 105 |
| exactly l => l |
| 106 |
| use_prime => encode (s - 1) |
| 107 |
| use_bitwidth => encode (src_weight src_n - 1) |
| 108 |
end). |
| 109 |
Let dst_n : nat |
| 110 |
:= let max := match outbounds with |
| 111 |
| exactly l => inl (List.length l) |
| 112 |
| use_prime => inr s |
| 113 |
| use_bitwidth => inr (src_weight src_n) |
| 114 |
end in |
| 115 |
match max with |
| 116 |
| inl len => len |
| 117 |
| inr max |
| 118 |
=> Z.to_nat (Qceiling (Z.log2_up max / dst_limbwidth)) |
| 119 |
end. |
| 120 |
Let out_upperbounds : list Z |
| 121 |
:= List.map |
| 122 |
(fun v : Z => Qceiling (Option.value outbounds_multiplier 1 * v)) |
| 123 |
(let encode := Partition.partition dst_weight dst_n in |
| 124 |
match outbounds with |
| 125 |
| exactly l => l |
| 126 |
| use_prime => encode (s - 1) |
| 127 |
| use_bitwidth => encode (dst_weight dst_n - 1) |
| 128 |
end). |
| 129 |
|
| 130 |
Local Notation possible_values_with_bytes := (possible_values_of_machine_wordsize_with_bytes machine_wordsize). |
| 131 |
|
| 132 |
Definition in_bounds : list (ZRange.type.option.interp base.type.Z) |
| 133 |
:= List.map (fun u => Some r[0~>u]%zrange) in_upperbounds. |
| 134 |
Definition out_bounds : list (ZRange.type.option.interp base.type.Z) |
| 135 |
:= List.map (fun u => Some r[0~>u]%zrange) out_upperbounds. |
| 136 |
|
| 137 |
Local Existing Instance default_translate_to_fancy. |
| 138 |
Local Instance no_select_size : no_select_size_opt := no_select_size_of_no_select machine_wordsize. |
| 139 |
Local Instance split_mul_to : split_mul_to_opt := split_mul_to_of_should_split_mul machine_wordsize possible_values_with_bytes. |
| 140 |
Local Instance split_multiret_to : split_multiret_to_opt := split_multiret_to_of_should_split_multiret machine_wordsize possible_values_with_bytes. |
| 141 |
|
| 142 |
(** Note: If you change the name or type signature of this |
| 143 |
function, you will need to update the code in CLI.v *) |
| 144 |
Definition check_args {T} (requests : list string) (res : Pipeline.ErrorT T) |
| 145 |
: Pipeline.ErrorT T |
| 146 |
:= check_args_of_list |
| 147 |
(List.map |
| 148 |
(fun v => (true, v)) |
| 149 |
[((Qle_bool 1 src_limbwidth)%Q, Pipeline.Value_not_leQ "src_limbwidth < 1" 1%Q src_limbwidth) |
| 150 |
; ((Qle_bool 1 dst_limbwidth)%Q, Pipeline.Value_not_leQ "dst_limbwidth < 1" 1%Q dst_limbwidth) |
| 151 |
; (negb (src_n =? 0)%nat, Pipeline.Values_not_provably_distinctZ "src_n = 0" src_n 0%nat) |
| 152 |
; (negb (dst_n =? 0)%nat, Pipeline.Values_not_provably_distinctZ "dst_n = 0" dst_n 0%nat) |
| 153 |
; ((List.length in_bounds =? src_n)%nat, Pipeline.Values_not_provably_equalZ "length in_bounds ≠ src_n" (List.length in_bounds) src_n) |
| 154 |
; ((List.length in_upperbounds =? src_n)%nat, Pipeline.Values_not_provably_equalZ "length in_upperbounds ≠ src_n" (List.length in_upperbounds) src_n) |
| 155 |
; ((List.length out_bounds =? dst_n)%nat, Pipeline.Values_not_provably_equalZ "length out_bounds ≠ dst_n" (List.length out_bounds) dst_n) |
| 156 |
; ((List.length out_upperbounds =? dst_n)%nat, Pipeline.Values_not_provably_equalZ "length out_upperbounds ≠ dst_n" (List.length out_upperbounds) dst_n) |
| 157 |
; ((Associational.eval c <? s)%Z, Pipeline.Value_not_ltZ "s ≤ Associational.eval c" (Associational.eval c) s) |
| 158 |
; (negb (s =? 0)%Z, Pipeline.Values_not_provably_distinctZ "s = 0" s 0) |
| 159 |
; (let eval_in_upperbounds := eval src_weight src_n in_upperbounds in |
| 160 |
let dst_weight_n := dst_weight dst_n in |
| 161 |
((eval_in_upperbounds <? dst_weight_n)%Z, |
| 162 |
Pipeline.Value_not_ltZ "dst_weight dst_n ≤ src_eval in_upperbounds" eval_in_upperbounds dst_weight_n)) |
| 163 |
; (0 <? machine_wordsize, Pipeline.Value_not_ltZ "machine_wordsize ≤ 0" 0 machine_wordsize) |
| 164 |
; (let v1 := List.fold_right Z.max 0 in_upperbounds in |
| 165 |
let v2 := 2^machine_wordsize-1 in |
| 166 |
((v1 <=? v2)%Z, |
| 167 |
Pipeline.Value_not_leZ "max(in_upperbounds) > 2^machine_wordsize-1" v1 v2)) |
| 168 |
; (let v1 := List.fold_right Z.max 0 out_upperbounds in |
| 169 |
let v2 := 2^machine_wordsize-1 in |
| 170 |
((v1 <=? v2)%Z, |
| 171 |
Pipeline.Value_not_leZ "max(out_upperbounds) > 2^machine_wordsize-1" v1 v2)) |
| 172 |
]) |
| 173 |
res. |
| 174 |
|
| 175 |
Local Ltac use_curve_good_t := |
| 176 |
repeat first [ use_requests_to_prove_curve_good_t_step |
| 177 |
| assumption |
| 178 |
| lia |
| 179 |
| progress autorewrite with distr_length |
| 180 |
| progress distr_length ]. |
| 181 |
|
| 182 |
Context (requests : list string) |
| 183 |
(curve_good : check_args requests (Success tt) = Success tt). |
| 184 |
|
| 185 |
Lemma use_curve_good |
| 186 |
: 0 < machine_wordsize |
| 187 |
/\ s - Associational.eval c <> 0 |
| 188 |
/\ s <> 0 |
| 189 |
/\ src_n <> 0%nat |
| 190 |
/\ dst_n <> 0%nat |
| 191 |
/\ List.length in_bounds = src_n |
| 192 |
/\ List.length out_bounds = dst_n |
| 193 |
/\ List.length in_upperbounds = src_n |
| 194 |
/\ List.length out_upperbounds = dst_n |
| 195 |
/\ 0 < Qden src_limbwidth <= Qnum src_limbwidth |
| 196 |
/\ 0 < Qden dst_limbwidth <= Qnum dst_limbwidth |
| 197 |
/\ eval src_weight src_n in_upperbounds < dst_weight dst_n. |
| 198 |
Proof using curve_good. |
| 199 |
prepare_use_curve_good (). |
| 200 |
{ use_curve_good_t. } |
| 201 |
{ use_curve_good_t. } |
| 202 |
{ use_curve_good_t. } |
| 203 |
{ use_curve_good_t. } |
| 204 |
{ use_curve_good_t. } |
| 205 |
Qed. |
| 206 |
|
| 207 |
Local Notation src_weightf := (weight (Qnum src_limbwidth) (QDen src_limbwidth)). |
| 208 |
Local Notation src_evalf := (eval src_weightf src_n). |
| 209 |
Local Notation dst_weightf := (weight (Qnum dst_limbwidth) (QDen dst_limbwidth)). |
| 210 |
Local Notation dst_evalf := (eval dst_weightf dst_n). |
| 211 |
Local Notation notations_for_docstring |
| 212 |
:= (CorrectnessStringification.dyn_context.cons |
| 213 |
src_weightf "src_weight" |
| 214 |
(CorrectnessStringification.dyn_context.cons |
| 215 |
src_evalf "src_eval" |
| 216 |
(CorrectnessStringification.dyn_context.cons |
| 217 |
dst_weightf "dst_weight" |
| 218 |
(CorrectnessStringification.dyn_context.cons |
| 219 |
dst_evalf "dst_eval" |
| 220 |
(CorrectnessStringification.dyn_context.cons |
| 221 |
(s - Associational.eval c) "(s - c)" |
| 222 |
CorrectnessStringification.dyn_context.nil)))))%string. |
| 223 |
Local Notation "'docstring_with_summary_from_lemma!' summary correctness" |
| 224 |
:= (docstring_with_summary_from_lemma_with_ctx! |
| 225 |
notations_for_docstring |
| 226 |
summary |
| 227 |
correctness) |
| 228 |
(only parsing, at level 10, summary at next level, correctness at next level). |
| 229 |
|
| 230 |
Definition convert_bases |
| 231 |
:= Pipeline.BoundsPipeline |
| 232 |
false (* subst01 *) |
| 233 |
possible_values_with_bytes |
| 234 |
(reified_convert_bases_gen |
| 235 |
@ GallinaReify.Reify (Qnum src_limbwidth) @ GallinaReify.Reify (Z.pos (Qden src_limbwidth)) |
| 236 |
@ GallinaReify.Reify (Qnum dst_limbwidth) @ GallinaReify.Reify (Z.pos (Qden dst_limbwidth)) |
| 237 |
@ GallinaReify.Reify src_n |
| 238 |
@ GallinaReify.Reify dst_n) |
| 239 |
(Some in_bounds, tt) |
| 240 |
(Some out_bounds). |
| 241 |
|
| 242 |
Definition sconvert_bases (prefix : string) |
| 243 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 244 |
:= Eval cbv beta in |
| 245 |
FromPipelineToString! |
| 246 |
machine_wordsize prefix "convert_bases" convert_bases |
| 247 |
(docstring_with_summary_from_lemma! |
| 248 |
(fun fname : string => [text_before_function_name ++ fname ++ " converts a field element from base " ++ Decimal.show_Q src_limbwidth ++ " to base " ++ Decimal.show_Q dst_limbwidth ++ " in little-endian order."]%string) |
| 249 |
(convert_bases_correct src_weight dst_weight src_n dst_n in_bounds)). |
| 250 |
|
| 251 |
Local Ltac solve_extra_bounds_side_conditions := |
| 252 |
cbn [lower upper fst snd] in *; Bool.split_andb; Z.ltb_to_lt; lia. |
| 253 |
|
| 254 |
Hint Rewrite |
| 255 |
eval_convert_basesmod |
| 256 |
using solve [ auto | congruence | solve_extra_bounds_side_conditions ] : push_eval. |
| 257 |
|
| 258 |
Local Ltac prove_correctness _ := |
| 259 |
Primitives.prove_correctness use_curve_good; |
| 260 |
try solve [ auto | congruence | solve_extra_bounds_side_conditions ]. |
| 261 |
|
| 262 |
Lemma convert_bases_correct res |
| 263 |
(Hres : convert_bases = Success res) |
| 264 |
: convert_bases_correct src_weight dst_weight src_n dst_n in_bounds (Interp res). |
| 265 |
Proof using curve_good. |
| 266 |
prove_correctness (); []. |
| 267 |
erewrite convert_basesmod_partitions; [ reflexivity | .. ]. |
| 268 |
all: repeat apply conj; (congruence || auto). |
| 269 |
all: cbv [in_bounds] in *. |
| 270 |
all: lazymatch goal with |
| 271 |
| [ H1 : list_Z_bounded_by (List.map (fun y => Some (@?f y)) ?b) ?x |
| 272 |
|- context[eval ?wt ?n ?x] ] |
| 273 |
=> unshelve epose proof (eval_list_Z_bounded_by wt n (List.map (fun x => Some (f x)) b) (List.map f b) x H1 _ _ (fun A B => Z.lt_le_incl _ _ (weight_positive _ _))); clear H1 |
| 274 |
end. |
| 275 |
all: congruence || auto. |
| 276 |
all: repeat first [ reflexivity |
| 277 |
| apply wprops |
| 278 |
| progress rewrite ?map_map in * |
| 279 |
| progress rewrite ?map_id in * |
| 280 |
| progress cbn [upper lower] in * |
| 281 |
| lia |
| 282 |
| match goal with |
| 283 |
| [ H : context[List.map (fun _ => 0) _] |- _ ] => erewrite <- zeros_ext_map, ?eval_zeros in H by reflexivity |
| 284 |
| [ H : List.length ?l = ?n, H' : context[eval ?w ?n (zeros (List.length ?l))] |- _ ] |
| 285 |
=> rewrite !H, eval_zeros in H' |
| 286 |
end |
| 287 |
| progress autorewrite with distr_length push_eval in * |
| 288 |
| destruct_head'_and; eapply Z.le_lt_trans; eassumption ]. |
| 289 |
Qed. |
| 290 |
|
| 291 |
Lemma Wf_convert_bases res (Hres : convert_bases = Success res) : Wf res. |
| 292 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 293 |
|
| 294 |
Section for_stringification. |
| 295 |
Local Open Scope string_scope. |
| 296 |
Local Open Scope list_scope. |
| 297 |
|
| 298 |
Definition known_functions |
| 299 |
:= [("convert_bases", wrap_s sconvert_bases)]. |
| 300 |
|
| 301 |
Definition valid_names : string |
| 302 |
:= Eval compute in String.concat ", " (List.map (@fst _ _) known_functions). |
| 303 |
|
| 304 |
(** Note: If you change the name or type signature of this |
| 305 |
function, you will need to update the code in CLI.v *) |
| 306 |
Definition Synthesize (comment_header : list string) (function_name_prefix : string) (requests : list string) |
| 307 |
: list (synthesis_output_kind * string * Pipeline.M (list string)) |
| 308 |
:= Primitives.Synthesize |
| 309 |
machine_wordsize valid_names known_functions (fun _ => nil) all_typedefs! |
| 310 |
check_args |
| 311 |
((ToString.comment_file_header_block |
| 312 |
(comment_header |
| 313 |
++ [""; |
| 314 |
"Computed values:"; |
| 315 |
" dst_n = " ++ show dst_n]%string))) |
| 316 |
function_name_prefix requests. |
| 317 |
End for_stringification. |
| 318 |
End __. |
| 319 |
|
| 320 |
Module Export Hints. |
| 321 |
#[global] |
| 322 |
Hint Opaque |
| 323 |
convert_bases |
| 324 |
: wf_op_cache. |
| 325 |
#[global] |
| 326 |
Hint Immediate |
| 327 |
Wf_convert_bases |
| 328 |
: wf_op_cache. |
| 329 |
End Hints. |