| 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 Crypto.TAPSort. |
| 10 |
Require Import Crypto.Util.ErrorT. |
| 11 |
Require Import Crypto.Util.ListUtil. |
| 12 |
Require Import Crypto.Util.ListUtil.FoldBool. |
| 13 |
Require Import Crypto.Util.Strings.Decimal. |
| 14 |
Require Import Crypto.Util.ZRange. |
| 15 |
Require Crypto.Util.Option. |
| 16 |
Require Import Crypto.Util.Strings.Show. |
| 17 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 18 |
Require Import Crypto.Util.ZUtil.Zselect. |
| 19 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 20 |
Require Import Crypto.Util.Prod. |
| 21 |
Require Import Crypto.Util.Tactics.HasBody. |
| 22 |
Require Import Crypto.Util.Tactics.Head. |
| 23 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 24 |
Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy. |
| 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.Partition. |
| 33 |
Require Import Crypto.Arithmetic.Freeze. |
| 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.UnsaturatedSolinasReificationCache. |
| 40 |
Require Import Crypto.Assembly.Equivalence. |
| 41 |
Import Option.Notations. |
| 42 |
Import ListNotations. |
| 43 |
Local Open Scope string_scope. Local Open Scope bool_scope. Local Open Scope Z_scope. Local Open Scope list_scope. |
| 44 |
|
| 45 |
Import |
| 46 |
Language.Wf.Compilers |
| 47 |
Language.Compilers |
| 48 |
AbstractInterpretation.Compilers |
| 49 |
Stringification.Language.Compilers |
| 50 |
Rewriter.All.Compilers.RewriteRules. |
| 51 |
Import Compilers.API. |
| 52 |
|
| 53 |
Import COperationSpecifications.Primitives. |
| 54 |
Import COperationSpecifications.Solinas. |
| 55 |
|
| 56 |
Import Associational Positional. |
| 57 |
|
| 58 |
Local Coercion Z.of_nat : nat >-> Z. |
| 59 |
Local Coercion QArith_base.inject_Z : Z >-> Q. |
| 60 |
Local Coercion Z.pos : positive >-> Z. |
| 61 |
|
| 62 |
Local Set Keyed Unification. (* needed for making [autorewrite] fast, c.f. COQBUG(https://github.com/coq/coq/issues/9283) *) |
| 63 |
|
| 64 |
(* needed for making [autorewrite] not take a very long time *) |
| 65 |
Local Opaque |
| 66 |
reified_carry_mul_gen |
| 67 |
reified_carry_square_gen |
| 68 |
reified_carry_scmul_gen |
| 69 |
reified_carry_gen |
| 70 |
reified_add_gen |
| 71 |
reified_sub_gen |
| 72 |
reified_opp_gen |
| 73 |
reified_carry_add_gen |
| 74 |
reified_carry_sub_gen |
| 75 |
reified_carry_opp_gen |
| 76 |
reified_id_gen |
| 77 |
reified_to_bytes_gen |
| 78 |
reified_from_bytes_gen |
| 79 |
reified_encode_gen |
| 80 |
reified_encode_gen |
| 81 |
reified_zero_gen |
| 82 |
reified_one_gen |
| 83 |
reified_eval_gen |
| 84 |
reified_bytes_eval_gen |
| 85 |
reified_prime_gen |
| 86 |
expr.Interp. |
| 87 |
|
| 88 |
Section __. |
| 89 |
Context {output_language_api : ToString.OutputLanguageAPI} |
| 90 |
{pipeline_opts : PipelineOptions} |
| 91 |
{pipeline_to_string_opts : PipelineToStringOptions} |
| 92 |
{synthesis_opts : SynthesisOptions} |
| 93 |
{tight_upperbound_fraction : tight_upperbound_fraction_opt} |
| 94 |
(n : nat) |
| 95 |
(s : Z) |
| 96 |
(c : list (Z * Z)) |
| 97 |
(machine_wordsize : machine_wordsize_opt). |
| 98 |
|
| 99 |
Local Notation limbwidth := (limbwidth n s c). |
| 100 |
Definition idxs : list nat := carry_chains n s c. |
| 101 |
Definition n_bytes := bytes_n s. |
| 102 |
Local Notation prime_upperbound_list := (prime_upperbound_list n s c) (only parsing). |
| 103 |
Definition prime_bytes_upperbound_list : list Z |
| 104 |
:= Partition.partition (weight 8 1) n_bytes (s-1). |
| 105 |
Local Notation tight_upperbounds := (tight_upperbounds n s c) (only parsing). |
| 106 |
Local Notation loose_upperbounds := (loose_upperbounds n s c) (only parsing). |
| 107 |
Local Notation tight_bounds := (tight_bounds n s c) (only parsing). |
| 108 |
Local Notation loose_bounds := (loose_bounds n s c) (only parsing). |
| 109 |
Global Instance tight_bounds_typedef : typedef (t:=base.type.list base.type.Z) (Some tight_bounds) |
| 110 |
:= { name := "tight_field_element" |
| 111 |
; description name := (text_before_type_name ++ name ++ " is a field element with tight bounds.")%string }. |
| 112 |
Global Instance loose_bounds_typedef : typedef (t:=base.type.list base.type.Z) (Some loose_bounds) |
| 113 |
:= { name := "loose_field_element" |
| 114 |
; description name := (text_before_type_name ++ name ++ " is a field element with loose bounds.")%string }. |
| 115 |
Definition prime_bound : ZRange.type.interp base.type.Z |
| 116 |
:= r[0~>(s - Associational.eval c - 1)]%zrange. |
| 117 |
Definition prime_bounds : list (ZRange.type.option.interp (base.type.Z)) |
| 118 |
:= List.map (fun v => Some r[0 ~> v]%zrange) prime_upperbound_list. |
| 119 |
Definition prime_bytes_bounds : list (ZRange.type.option.interp base.type.Z) |
| 120 |
:= List.map (fun v => Some r[0 ~> v]%zrange) prime_bytes_upperbound_list. |
| 121 |
Local Notation word_bound := (word_bound machine_wordsize). |
| 122 |
Local Notation saturated_bounds := (saturated_bounds n machine_wordsize). |
| 123 |
Local Notation balance := (balance n s c). |
| 124 |
|
| 125 |
Definition m : Z := s - Associational.eval c. |
| 126 |
(* m_enc needs to be such that, if x is bounded by tight bounds: |
| 127 |
|
| 128 |
-2*fw[i] <= x[i] - m_enc[i] <= fw[i] |
| 129 |
|
| 130 |
...so as to obey the bounds for the sub-with-borrow in |
| 131 |
freeze. fw[i] here is shorthand for (weight (S i) / weight i). To |
| 132 |
obey the upper bound, we need to redistribute m_enc such that: |
| 133 |
|
| 134 |
tight_upperbounds[i] - fw[i] <= m_enc[i] |
| 135 |
|
| 136 |
Additionally, we cannot have the minimum be uniformly 0, or else |
| 137 |
we'll encode 0; if this happens, we bump the highest limb to be |
| 138 |
at least 1 *) |
| 139 |
Definition m_enc_min : list Z := |
| 140 |
let wt := weight (Qnum limbwidth) (Qden limbwidth) in |
| 141 |
let fw := List.map (fun i => wt (S i) / wt i) (seq 0 n) in |
| 142 |
let m_enc_min := List.map2 Z.sub tight_upperbounds fw in |
| 143 |
if List.forallb (Z.eqb 0) m_enc_min |
| 144 |
then set_nth (n-1) 1 m_enc_min |
| 145 |
else m_enc_min. |
| 146 |
|
| 147 |
Definition m_enc : list Z := |
| 148 |
let M := encode (weight (Qnum limbwidth) (Qden limbwidth)) n s c m in |
| 149 |
distribute_balance n s c m_enc_min M. |
| 150 |
|
| 151 |
Local Notation possible_values := (possible_values_of_machine_wordsize machine_wordsize). |
| 152 |
Local Notation possible_values_with_bytes := (possible_values_of_machine_wordsize_with_bytes machine_wordsize). |
| 153 |
|
| 154 |
Local Existing Instance default_translate_to_fancy. |
| 155 |
Local Instance no_select_size : no_select_size_opt := no_select_size_of_no_select machine_wordsize. |
| 156 |
Local Instance split_mul_to : split_mul_to_opt := split_mul_to_of_should_split_mul machine_wordsize possible_values. |
| 157 |
Local Instance split_multiret_to : split_multiret_to_opt := split_multiret_to_of_should_split_multiret machine_wordsize possible_values. |
| 158 |
|
| 159 |
Local Notation weightf := (weight (Qnum limbwidth) (QDen limbwidth)). |
| 160 |
Local Notation evalf := (eval weightf n). |
| 161 |
|
| 162 |
Lemma length_prime_bytes_upperbound_list : List.length prime_bytes_upperbound_list = n_bytes. |
| 163 |
Proof using Type. cbv [prime_bytes_upperbound_list]; now autorewrite with distr_length. Qed. |
| 164 |
Hint Rewrite length_prime_bytes_upperbound_list : distr_length. |
| 165 |
Lemma length_saturated_bounds : List.length saturated_bounds = n. |
| 166 |
Proof using Type. cbv [saturated_bounds]; now autorewrite with distr_length. Qed. |
| 167 |
Hint Rewrite length_saturated_bounds : distr_length. |
| 168 |
Lemma length_m_enc : List.length m_enc = n. |
| 169 |
Proof using Type. cbv [m_enc]; repeat distr_length. Qed. |
| 170 |
Hint Rewrite length_m_enc : distr_length. |
| 171 |
Lemma eval_list_Z_bounded_by_tight_bounds |
| 172 |
(Hwt : 0 < QDen limbwidth <= Qnum limbwidth) |
| 173 |
x |
| 174 |
(Hx : list_Z_bounded_by tight_bounds x) |
| 175 |
: 0 <= evalf x <= evalf tight_upperbounds. |
| 176 |
Proof using Type. |
| 177 |
Local Opaque UnsaturatedSolinasHeuristics.tight_upperbounds. |
| 178 |
cbv [tight_bounds] in *. |
| 179 |
intros. |
| 180 |
lazymatch goal with |
| 181 |
| [ H1 : list_Z_bounded_by (List.map (fun y => Some (@?f y)) ?b) ?x |
| 182 |
|- context[eval ?wt ?n ?x] ] |
| 183 |
=> 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 |
| 184 |
end. |
| 185 |
all: repeat first [ reflexivity |
| 186 |
| apply wprops |
| 187 |
| progress rewrite ?map_map in * |
| 188 |
| progress rewrite ?map_id in * |
| 189 |
| progress cbn [upper lower] in * |
| 190 |
| assumption |
| 191 |
| progress autorewrite with distr_length |
| 192 |
| lia |
| 193 |
| match goal with |
| 194 |
| [ H : context[List.map (fun _ => 0) _] |- _ ] => erewrite <- zeros_ext_map, ?eval_zeros in H by reflexivity; autorewrite with distr_length push_eval in H |
| 195 |
end ]. |
| 196 |
Local Transparent UnsaturatedSolinasHeuristics.tight_upperbounds. |
| 197 |
Qed. |
| 198 |
|
| 199 |
(** Note: If you change the name or type signature of this |
| 200 |
function, you will need to update the code in CLI.v *) |
| 201 |
Definition check_args {T} (requests : list string) (res : Pipeline.ErrorT T) |
| 202 |
: Pipeline.ErrorT T |
| 203 |
:= check_args_of_list |
| 204 |
((List.map |
| 205 |
(fun v => (true, v)) |
| 206 |
(* first, all the ones that should always hold *) |
| 207 |
[((Qle_bool 1 limbwidth)%Q, Pipeline.Value_not_leQ "limbwidth < 1" 1%Q limbwidth) |
| 208 |
; (n <=? Z.log2_up (s - Associational.eval c), Pipeline.Value_not_leZ "Z.log2_up (s - Associational.eval c) < n" n (Z.log2_up (s - Associational.eval c))) |
| 209 |
; (Associational.eval c <? s, Pipeline.Value_not_ltZ "s ≤ Associational.eval c" (Associational.eval c) s) |
| 210 |
; (0 <? s, Pipeline.Value_not_ltZ "s ≤ 0" 0 s) |
| 211 |
; (negb (n =? 0)%nat, Pipeline.Values_not_provably_distinctZ "n = 0" n 0%nat) |
| 212 |
; (0 <? machine_wordsize, Pipeline.Value_not_ltZ "machine_wordsize ≤ 0" 0 machine_wordsize) |
| 213 |
; (let v1 := s - Associational.eval c in |
| 214 |
let v2 := weight (Qnum limbwidth) (QDen limbwidth) n in |
| 215 |
(v1 <=? v2, Pipeline.Value_not_leZ "weight n < s - Associational.eval c" v1 v2)) |
| 216 |
|
| 217 |
(** For bedrock2 *) |
| 218 |
; (let v1 := List.fold_right Z.max 0 prime_bytes_upperbound_list in |
| 219 |
let v2 := 2^machine_wordsize-1 in |
| 220 |
(v1 <=? v2, |
| 221 |
Pipeline.Value_not_leZ "max(prime_bytes_upperbounds) > 2^machine_wordsize-1" v1 v2)) |
| 222 |
; (let v1 := List.fold_right Z.max 0 tight_upperbounds in |
| 223 |
let v2 := 2^machine_wordsize-1 in |
| 224 |
(v1 <=? v2, |
| 225 |
Pipeline.Value_not_leZ "max(tight_upperbounds) > 2^machine_wordsize-1" v1 v2)) |
| 226 |
; (let v1 := List.fold_right Z.max 0 loose_upperbounds in |
| 227 |
let v2 := 2^machine_wordsize-1 in |
| 228 |
(v1 <=? v2, |
| 229 |
Pipeline.Value_not_leZ "max(loose_upperbounds) > 2^machine_wordsize-1" v1 v2)) |
| 230 |
]) |
| 231 |
(* the littany of to_bytes ones *) |
| 232 |
++ (List.map |
| 233 |
(fun v => (request_present requests "to_bytes", v)) |
| 234 |
[(0 <? Associational.eval c, Pipeline.Value_not_ltZ "Associational.eval c ≤ 0 (needed for to_bytes)" 0 (Associational.eval c)) |
| 235 |
; (let v1 := s in |
| 236 |
let v2 := weight (Qnum limbwidth) (QDen limbwidth) n in |
| 237 |
(v1 =? v2, Pipeline.Values_not_provably_equalZ "s ≠ weight n (needed for to_bytes)" v1 v2)) |
| 238 |
; (let v1 := (List.map (Z.land (Z.ones machine_wordsize)) m_enc) in |
| 239 |
let v2 := m_enc in |
| 240 |
(list_beq _ Z.eqb v1 v2, Pipeline.Values_not_provably_equal_listZ "map mask m_enc ≠ m_enc (needed for to_bytes)" v1 v2)) |
| 241 |
; (let v1 := eval (weight (Qnum limbwidth) (QDen limbwidth)) n m_enc in |
| 242 |
let v2 := s - Associational.eval c in |
| 243 |
(v1 =? v2, Pipeline.Values_not_provably_equalZ "eval m_enc ≠ s - Associational.eval c (needed for to_bytes)" v1 v2)) |
| 244 |
; (let v1 := eval (weight (Qnum limbwidth) (QDen limbwidth)) n tight_upperbounds in |
| 245 |
let v2 := 2 * eval (weight (Qnum limbwidth) (QDen limbwidth)) n m_enc in |
| 246 |
(v1 <? v2, Pipeline.Value_not_ltZ "2 * eval m_enc ≤ eval tight_upperbounds (needed for to_bytes)" v1 v2)) |
| 247 |
]) |
| 248 |
++ [(request_present requests "from_bytes", |
| 249 |
(1 <? s, Pipeline.Value_not_ltZ "s ≤ 1 (need for from_bytes)" 1 s)) |
| 250 |
]) |
| 251 |
res. |
| 252 |
|
| 253 |
Local Ltac use_curve_good_t := |
| 254 |
repeat first [ use_requests_to_prove_curve_good_t_step |
| 255 |
| assumption |
| 256 |
| lia |
| 257 |
| progress autorewrite with distr_length |
| 258 |
| progress distr_length |
| 259 |
| progress autorewrite with zsimplify_fast in * ]. |
| 260 |
|
| 261 |
Context (requests : list string) |
| 262 |
(curve_good : check_args requests (Success tt) = Success tt). |
| 263 |
|
| 264 |
Lemma use_curve_good |
| 265 |
: let eval := eval (weight (Qnum limbwidth) (QDen limbwidth)) n in |
| 266 |
0 < Qden limbwidth <= Qnum limbwidth |
| 267 |
/\ n <= Z.log2_up (s - Associational.eval c) |
| 268 |
/\ 0 < s - Associational.eval c |
| 269 |
/\ 0 < s - Associational.eval c <= weight (Qnum limbwidth) (QDen limbwidth) n |
| 270 |
/\ s - Associational.eval c <> 0 |
| 271 |
/\ s <> 0 |
| 272 |
/\ 0 < machine_wordsize |
| 273 |
/\ n <> 0%nat |
| 274 |
/\ List.fold_right Z.max 0 prime_bytes_upperbound_list <= 2^machine_wordsize-1 |
| 275 |
/\ List.fold_right Z.max 0 tight_upperbounds <= 2^machine_wordsize-1 |
| 276 |
/\ List.fold_right Z.max 0 loose_upperbounds <= 2^machine_wordsize-1 |
| 277 |
/\ (request_present requests "from_bytes" = true -> 1 < s) |
| 278 |
/\ (request_present requests "to_bytes" = true -> 1 < s) |
| 279 |
/\ (request_present requests "to_bytes" = true -> 0 < Associational.eval c < s) |
| 280 |
/\ (request_present requests "to_bytes" = true -> s = weight (Qnum limbwidth) (QDen limbwidth) n) |
| 281 |
/\ (request_present requests "to_bytes" = true -> List.map (Z.land (Z.ones machine_wordsize)) m_enc = m_enc) |
| 282 |
/\ (request_present requests "to_bytes" = true -> eval m_enc = s - Associational.eval c) |
| 283 |
/\ (request_present requests "to_bytes" = true -> eval tight_upperbounds < 2 * eval m_enc) |
| 284 |
/\ List.length tight_bounds = n |
| 285 |
/\ List.length loose_bounds = n |
| 286 |
/\ List.length prime_bytes_upperbound_list = n_bytes |
| 287 |
/\ List.length saturated_bounds = n |
| 288 |
/\ Datatypes.length m_enc = n. |
| 289 |
Proof using curve_good. |
| 290 |
prepare_use_curve_good (). |
| 291 |
{ use_curve_good_t. } |
| 292 |
{ use_curve_good_t. } |
| 293 |
{ use_curve_good_t. } |
| 294 |
{ use_curve_good_t. } |
| 295 |
{ use_curve_good_t. } |
| 296 |
{ use_curve_good_t. } |
| 297 |
{ use_curve_good_t. } |
| 298 |
{ use_curve_good_t. } |
| 299 |
{ use_curve_good_t. } |
| 300 |
{ use_curve_good_t. } |
| 301 |
{ use_curve_good_t. } |
| 302 |
{ use_curve_good_t. } |
| 303 |
{ use_curve_good_t. } |
| 304 |
{ use_curve_good_t. } |
| 305 |
{ use_curve_good_t. } |
| 306 |
{ use_curve_good_t. } |
| 307 |
{ use_curve_good_t. } |
| 308 |
{ use_curve_good_t. } |
| 309 |
Qed. |
| 310 |
|
| 311 |
Lemma use_curve_good_extra |
| 312 |
: (request_present requests "to_bytes" = true -> forall x, list_Z_bounded_by tight_bounds x -> 0 <= evalf x < 2 * (s - Associational.eval c)). |
| 313 |
Proof using curve_good. |
| 314 |
pose proof use_curve_good; cbv beta zeta in *; destruct_head'_and. |
| 315 |
pose proof eval_list_Z_bounded_by_tight_bounds. |
| 316 |
repeat match goal with |- _ /\ _ => split end. |
| 317 |
{ intros. |
| 318 |
specialize_by auto. |
| 319 |
specialize_all_ways_under_binders_by eassumption. |
| 320 |
lia. } |
| 321 |
Qed. |
| 322 |
|
| 323 |
Local Notation notations_for_docstring |
| 324 |
:= (CorrectnessStringification.dyn_context.cons |
| 325 |
m "m" |
| 326 |
(CorrectnessStringification.dyn_context.cons |
| 327 |
weightf "weight" |
| 328 |
(CorrectnessStringification.dyn_context.cons |
| 329 |
evalf "eval" |
| 330 |
CorrectnessStringification.dyn_context.nil)))%string. |
| 331 |
Local Notation "'docstring_with_summary_from_lemma!' summary correctness" |
| 332 |
:= (docstring_with_summary_from_lemma_with_ctx! |
| 333 |
notations_for_docstring |
| 334 |
summary |
| 335 |
correctness) |
| 336 |
(only parsing, at level 10, summary at next level, correctness at next level). |
| 337 |
|
| 338 |
Definition carry_mul |
| 339 |
:= Pipeline.BoundsPipeline |
| 340 |
false (* subst01 *) |
| 341 |
possible_values |
| 342 |
(reified_carry_mul_gen |
| 343 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n @ GallinaReify.Reify idxs) |
| 344 |
(Some loose_bounds, (Some loose_bounds, tt)) |
| 345 |
(Some tight_bounds). |
| 346 |
|
| 347 |
Definition scarry_mul (prefix : string) |
| 348 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 349 |
:= Eval cbv beta in |
| 350 |
FromPipelineToString! |
| 351 |
machine_wordsize prefix "carry_mul" carry_mul |
| 352 |
(docstring_with_summary_from_lemma! |
| 353 |
(fun fname : string => [text_before_function_name ++ fname ++ " multiplies two field elements and reduces the result."]%string) |
| 354 |
(carry_mul_correct weightf n m tight_bounds loose_bounds)). |
| 355 |
|
| 356 |
Definition carry_square |
| 357 |
:= Pipeline.BoundsPipeline |
| 358 |
false (* subst01 *) |
| 359 |
possible_values |
| 360 |
(reified_carry_square_gen |
| 361 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n @ GallinaReify.Reify idxs) |
| 362 |
(Some loose_bounds, tt) |
| 363 |
(Some tight_bounds). |
| 364 |
|
| 365 |
Definition scarry_square (prefix : string) |
| 366 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 367 |
:= Eval cbv beta in |
| 368 |
FromPipelineToString! |
| 369 |
machine_wordsize prefix "carry_square" carry_square |
| 370 |
(docstring_with_summary_from_lemma! |
| 371 |
(fun fname : string => [text_before_function_name ++ fname ++ " squares a field element and reduces the result."]%string) |
| 372 |
(carry_square_correct weightf n m tight_bounds loose_bounds)). |
| 373 |
|
| 374 |
Definition carry_scmul_const (x : Z) |
| 375 |
:= Pipeline.BoundsPipeline |
| 376 |
false (* subst01 *) |
| 377 |
possible_values |
| 378 |
(reified_carry_scmul_gen |
| 379 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n @ GallinaReify.Reify idxs @ GallinaReify.Reify x) |
| 380 |
(Some loose_bounds, tt) |
| 381 |
(Some tight_bounds). |
| 382 |
|
| 383 |
Definition scarry_scmul_const (prefix : string) (x : Z) |
| 384 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 385 |
:= Eval cbv beta in |
| 386 |
FromPipelineToString! |
| 387 |
machine_wordsize prefix ("carry_scmul_" ++ Decimal.Z.to_string x)%string (carry_scmul_const x) |
| 388 |
(docstring_with_summary_from_lemma! |
| 389 |
(fun fname : string => [text_before_function_name ++ fname ++ " multiplies a field element by " ++ Decimal.Z.to_string x ++ " and reduces the result."]%string) |
| 390 |
(carry_scmul_const_correct weightf n m tight_bounds loose_bounds x)). |
| 391 |
|
| 392 |
Definition carry |
| 393 |
:= Pipeline.BoundsPipeline |
| 394 |
true (* subst01 *) |
| 395 |
possible_values |
| 396 |
(reified_carry_gen |
| 397 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n @ GallinaReify.Reify idxs) |
| 398 |
(Some loose_bounds, tt) |
| 399 |
(Some tight_bounds). |
| 400 |
|
| 401 |
Definition scarry (prefix : string) |
| 402 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 403 |
:= Eval cbv beta in |
| 404 |
FromPipelineToString! |
| 405 |
machine_wordsize prefix "carry" carry |
| 406 |
(docstring_with_summary_from_lemma! |
| 407 |
(fun fname : string => [text_before_function_name ++ fname ++ " reduces a field element."]%string) |
| 408 |
(carry_correct weightf n m tight_bounds loose_bounds)). |
| 409 |
|
| 410 |
Definition add |
| 411 |
:= Pipeline.BoundsPipeline |
| 412 |
true (* subst01 *) |
| 413 |
possible_values |
| 414 |
(reified_add_gen |
| 415 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify n) |
| 416 |
(Some tight_bounds, (Some tight_bounds, tt)) |
| 417 |
(Some loose_bounds). |
| 418 |
|
| 419 |
Definition sadd (prefix : string) |
| 420 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 421 |
:= Eval cbv beta in |
| 422 |
FromPipelineToString! |
| 423 |
machine_wordsize prefix "add" add |
| 424 |
(docstring_with_summary_from_lemma! |
| 425 |
(fun fname : string => [text_before_function_name ++ fname ++ " adds two field elements."]%string) |
| 426 |
(add_correct weightf n m tight_bounds loose_bounds)). |
| 427 |
|
| 428 |
Definition sub |
| 429 |
:= Pipeline.BoundsPipeline |
| 430 |
true (* subst01 *) |
| 431 |
possible_values |
| 432 |
(reified_sub_gen |
| 433 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify n @ GallinaReify.Reify balance) |
| 434 |
(Some tight_bounds, (Some tight_bounds, tt)) |
| 435 |
(Some loose_bounds). |
| 436 |
|
| 437 |
Definition ssub (prefix : string) |
| 438 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 439 |
:= Eval cbv beta in |
| 440 |
FromPipelineToString! |
| 441 |
machine_wordsize prefix "sub" sub |
| 442 |
(docstring_with_summary_from_lemma! |
| 443 |
(fun fname : string => [text_before_function_name ++ fname ++ " subtracts two field elements."]%string) |
| 444 |
(sub_correct weightf n m tight_bounds loose_bounds)). |
| 445 |
|
| 446 |
Definition opp |
| 447 |
:= Pipeline.BoundsPipeline |
| 448 |
true (* subst01 *) |
| 449 |
possible_values |
| 450 |
(reified_opp_gen |
| 451 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify n @ GallinaReify.Reify balance) |
| 452 |
(Some tight_bounds, tt) |
| 453 |
(Some loose_bounds). |
| 454 |
|
| 455 |
Definition sopp (prefix : string) |
| 456 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 457 |
:= Eval cbv beta in |
| 458 |
FromPipelineToString! |
| 459 |
machine_wordsize prefix "opp" opp |
| 460 |
(docstring_with_summary_from_lemma! |
| 461 |
(fun fname : string => [text_before_function_name ++ fname ++ " negates a field element."]%string) |
| 462 |
(opp_correct weightf n m tight_bounds loose_bounds)). |
| 463 |
|
| 464 |
Definition carry_add |
| 465 |
:= Pipeline.BoundsPipeline |
| 466 |
true (* subst01 *) |
| 467 |
possible_values |
| 468 |
(reified_carry_add_gen |
| 469 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n @ GallinaReify.Reify idxs) |
| 470 |
(Some tight_bounds, (Some tight_bounds, tt)) |
| 471 |
(Some tight_bounds). |
| 472 |
|
| 473 |
Definition scarry_add (prefix : string) |
| 474 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 475 |
:= Eval cbv beta in |
| 476 |
FromPipelineToString! |
| 477 |
machine_wordsize prefix "carry_add" carry_add |
| 478 |
(docstring_with_summary_from_lemma! |
| 479 |
(fun fname : string => [text_before_function_name ++ fname ++ " adds two field elements."]%string) |
| 480 |
(carry_add_correct weightf n m tight_bounds)). |
| 481 |
|
| 482 |
Definition carry_sub |
| 483 |
:= Pipeline.BoundsPipeline |
| 484 |
true (* subst01 *) |
| 485 |
possible_values |
| 486 |
(reified_carry_sub_gen |
| 487 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n @ GallinaReify.Reify idxs @ GallinaReify.Reify balance) |
| 488 |
(Some tight_bounds, (Some tight_bounds, tt)) |
| 489 |
(Some tight_bounds). |
| 490 |
|
| 491 |
Definition scarry_sub (prefix : string) |
| 492 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 493 |
:= Eval cbv beta in |
| 494 |
FromPipelineToString! |
| 495 |
machine_wordsize prefix "carry_sub" carry_sub |
| 496 |
(docstring_with_summary_from_lemma! |
| 497 |
(fun fname : string => [text_before_function_name ++ fname ++ " subtracts two field elements."]%string) |
| 498 |
(carry_sub_correct weightf n m tight_bounds)). |
| 499 |
|
| 500 |
Definition carry_opp |
| 501 |
:= Pipeline.BoundsPipeline |
| 502 |
true (* subst01 *) |
| 503 |
possible_values |
| 504 |
(reified_carry_opp_gen |
| 505 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n @ GallinaReify.Reify idxs @ GallinaReify.Reify balance) |
| 506 |
(Some tight_bounds, tt) |
| 507 |
(Some tight_bounds). |
| 508 |
|
| 509 |
Definition scarry_opp (prefix : string) |
| 510 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 511 |
:= Eval cbv beta in |
| 512 |
FromPipelineToString! |
| 513 |
machine_wordsize prefix "carry_opp" carry_opp |
| 514 |
(docstring_with_summary_from_lemma! |
| 515 |
(fun fname : string => [text_before_function_name ++ fname ++ " negates a field element."]%string) |
| 516 |
(carry_opp_correct weightf n m tight_bounds)). |
| 517 |
|
| 518 |
Definition relax |
| 519 |
:= Pipeline.BoundsPipeline |
| 520 |
true (* subst01 *) |
| 521 |
possible_values |
| 522 |
reified_id_gen |
| 523 |
(Some tight_bounds, tt) |
| 524 |
(Some loose_bounds). |
| 525 |
|
| 526 |
Definition srelax (prefix : string) |
| 527 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 528 |
:= Eval cbv beta in |
| 529 |
FromPipelineToString! |
| 530 |
machine_wordsize prefix "relax" relax |
| 531 |
(docstring_with_summary_from_lemma! |
| 532 |
(fun fname : string => [text_before_function_name ++ fname ++ " is the identity function converting from tight field elements to loose field elements."]%string) |
| 533 |
(relax_correct tight_bounds loose_bounds)). |
| 534 |
|
| 535 |
Definition to_bytes |
| 536 |
:= Pipeline.BoundsPipeline |
| 537 |
false (* subst01 *) |
| 538 |
possible_values_with_bytes |
| 539 |
(reified_to_bytes_gen |
| 540 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify n @ GallinaReify.Reify (machine_wordsize:Z) @ GallinaReify.Reify m_enc) |
| 541 |
(Some tight_bounds, tt) |
| 542 |
(Some prime_bytes_bounds). |
| 543 |
|
| 544 |
Definition sto_bytes (prefix : string) |
| 545 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 546 |
:= Eval cbv beta in |
| 547 |
FromPipelineToString! |
| 548 |
machine_wordsize prefix "to_bytes" to_bytes |
| 549 |
(docstring_with_summary_from_lemma! |
| 550 |
(fun fname : string => [text_before_function_name ++ fname ++ " serializes a field element to bytes in little-endian order."]%string) |
| 551 |
(to_bytes_correct weightf n n_bytes m tight_bounds)). |
| 552 |
|
| 553 |
Definition from_bytes |
| 554 |
:= Pipeline.BoundsPipeline |
| 555 |
false (* subst01 *) |
| 556 |
possible_values_with_bytes |
| 557 |
(reified_from_bytes_gen |
| 558 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify n) |
| 559 |
(Some prime_bytes_bounds, tt) |
| 560 |
(Some tight_bounds). |
| 561 |
|
| 562 |
Definition sfrom_bytes (prefix : string) |
| 563 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 564 |
:= Eval cbv beta in |
| 565 |
FromPipelineToString! |
| 566 |
machine_wordsize prefix "from_bytes" from_bytes |
| 567 |
(docstring_with_summary_from_lemma! |
| 568 |
(fun fname : string => [text_before_function_name ++ fname ++ " deserializes a field element from bytes in little-endian order."]%string) |
| 569 |
(from_bytes_correct weightf n n_bytes m s tight_bounds)). |
| 570 |
|
| 571 |
Definition encode |
| 572 |
:= Pipeline.BoundsPipeline |
| 573 |
true (* subst01 *) |
| 574 |
possible_values |
| 575 |
(reified_encode_gen |
| 576 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n) |
| 577 |
(Some prime_bound, tt) |
| 578 |
(Some tight_bounds). |
| 579 |
|
| 580 |
Definition sencode (prefix : string) |
| 581 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 582 |
:= Eval cbv beta in |
| 583 |
FromPipelineToString! |
| 584 |
machine_wordsize prefix "encode" encode |
| 585 |
(docstring_with_summary_from_lemma! |
| 586 |
(fun fname : string => [text_before_function_name ++ fname ++ " encodes an integer as a field element."]%string) |
| 587 |
(encode_correct weightf n m tight_bounds)). |
| 588 |
|
| 589 |
Definition encode_word |
| 590 |
:= Pipeline.BoundsPipeline |
| 591 |
true (* subst01 *) |
| 592 |
possible_values |
| 593 |
(reified_encode_gen |
| 594 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n) |
| 595 |
(Some word_bound, tt) |
| 596 |
(Some tight_bounds). |
| 597 |
|
| 598 |
Definition sencode_word (prefix : string) |
| 599 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 600 |
:= Eval cbv beta in |
| 601 |
FromPipelineToString! |
| 602 |
machine_wordsize prefix "encode_word" encode_word |
| 603 |
(docstring_with_summary_from_lemma! |
| 604 |
(fun fname : string => [text_before_function_name ++ fname ++ " encodes an integer as a field element."]%string) |
| 605 |
(encode_word_correct machine_wordsize weightf n m tight_bounds)). |
| 606 |
|
| 607 |
Definition zero |
| 608 |
:= Pipeline.BoundsPipeline |
| 609 |
true (* subst01 *) |
| 610 |
possible_values |
| 611 |
(reified_zero_gen |
| 612 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n) |
| 613 |
tt |
| 614 |
(Some tight_bounds). |
| 615 |
|
| 616 |
Definition szero (prefix : string) |
| 617 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 618 |
:= Eval cbv beta in |
| 619 |
FromPipelineToString! |
| 620 |
machine_wordsize prefix "zero" zero |
| 621 |
(docstring_with_summary_from_lemma! |
| 622 |
(fun fname => [text_before_function_name ++ fname ++ " returns the field element zero."]%string) |
| 623 |
(zero_correct weightf n m tight_bounds)). |
| 624 |
|
| 625 |
Definition one |
| 626 |
:= Pipeline.BoundsPipeline |
| 627 |
true (* subst01 *) |
| 628 |
possible_values |
| 629 |
(reified_one_gen |
| 630 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify s @ GallinaReify.Reify c @ GallinaReify.Reify n) |
| 631 |
tt |
| 632 |
(Some tight_bounds). |
| 633 |
|
| 634 |
Definition sone (prefix : string) |
| 635 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 636 |
:= Eval cbv beta in |
| 637 |
FromPipelineToString! |
| 638 |
machine_wordsize prefix "one" one |
| 639 |
(docstring_with_summary_from_lemma! |
| 640 |
(fun fname => [text_before_function_name ++ fname ++ " returns the field element one."]%string) |
| 641 |
(one_correct weightf n m tight_bounds)). |
| 642 |
|
| 643 |
Definition reval (* r for reified *) |
| 644 |
:= Pipeline.RepeatRewriteAddAssocLeftAndFlattenThunkedRects |
| 645 |
n |
| 646 |
(Pipeline.PreBoundsPipeline |
| 647 |
true (* subst01 *) |
| 648 |
false (* let_bind_return *) |
| 649 |
(reified_eval_gen |
| 650 |
@ GallinaReify.Reify (Qnum limbwidth) @ GallinaReify.Reify (Z.pos (Qden limbwidth)) @ GallinaReify.Reify n) |
| 651 |
(Some loose_bounds, tt)). |
| 652 |
|
| 653 |
Definition seval (arg_name : string) (* s for string *) |
| 654 |
:= show (invert_expr.smart_App_curried (reval _) (arg_name, tt)). |
| 655 |
|
| 656 |
Definition rbytes_eval (* r for reified *) |
| 657 |
:= Pipeline.RepeatRewriteAddAssocLeftAndFlattenThunkedRects |
| 658 |
n_bytes |
| 659 |
(Pipeline.PreBoundsPipeline |
| 660 |
true (* subst01 *) |
| 661 |
false (* let_bind_return *) |
| 662 |
(reified_bytes_eval_gen |
| 663 |
@ GallinaReify.Reify s) |
| 664 |
(Some prime_bytes_bounds, tt)). |
| 665 |
|
| 666 |
Definition sbytes_eval (arg_name : string) (* s for string *) |
| 667 |
:= show (invert_expr.smart_App_curried (rbytes_eval _) (arg_name, tt)). |
| 668 |
|
| 669 |
Definition selectznz : Pipeline.ErrorT _ := Primitives.selectznz n machine_wordsize. |
| 670 |
Definition sselectznz (prefix : string) |
| 671 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 672 |
:= Primitives.sselectznz n machine_wordsize prefix. |
| 673 |
|
| 674 |
Definition copy : Pipeline.ErrorT _ := Primitives.copy n machine_wordsize. |
| 675 |
Definition scopy (prefix : string) |
| 676 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 677 |
:= Primitives.scopy n machine_wordsize prefix. |
| 678 |
|
| 679 |
Local Ltac solve_extra_bounds_side_conditions := |
| 680 |
cbn [lower upper fst snd] in *; Bool.split_andb; Z.ltb_to_lt; lia. |
| 681 |
|
| 682 |
Hint Rewrite |
| 683 |
eval_carry_mulmod |
| 684 |
eval_carry_squaremod |
| 685 |
eval_carry_scmulmod |
| 686 |
eval_addmod |
| 687 |
eval_submod |
| 688 |
eval_oppmod |
| 689 |
eval_carry_addmod |
| 690 |
eval_carry_submod |
| 691 |
eval_carry_oppmod |
| 692 |
eval_carrymod |
| 693 |
freeze_to_bytesmod_partitions |
| 694 |
eval_to_bytesmod |
| 695 |
eval_from_bytesmod |
| 696 |
eval_encodemod |
| 697 |
using solve [ auto using eval_balance, length_balance | congruence | solve_extra_bounds_side_conditions ] : push_eval. |
| 698 |
Hint Unfold zeromod onemod : push_eval. |
| 699 |
|
| 700 |
Local Ltac solve_prove_correctness_side_conditions _ := |
| 701 |
solve [ auto | congruence | now autorewrite with distr_length | solve_extra_bounds_side_conditions ]. |
| 702 |
Local Ltac prove_correctness _ := |
| 703 |
Primitives.prove_correctness (conj use_curve_good use_curve_good_extra); |
| 704 |
try solve_prove_correctness_side_conditions (). |
| 705 |
|
| 706 |
(** Work around COQBUG(https://github.com/coq/coq/issues/9286) *) |
| 707 |
Local Opaque |
| 708 |
carry_mulmod |
| 709 |
carry_squaremod |
| 710 |
carry_scmulmod |
| 711 |
carrymod |
| 712 |
addmod |
| 713 |
submod |
| 714 |
oppmod |
| 715 |
carry_addmod |
| 716 |
carry_submod |
| 717 |
carry_oppmod |
| 718 |
from_bytesmod |
| 719 |
to_bytesmod |
| 720 |
(* Set Printing Width 100000. Print Rewrite HintDb push_eval. | sed s'/^.*->//g' | grep -o ' eval \(([^)]\+)\|[^ ]*\) \(([^)]\+)\|[^ ]*\) [^ )]*' | grep -o '[A-Za-z0-9_\.][A-Za-z0-9_\.]\+$' | sort | uniq *) |
| 721 |
addmod |
| 722 |
BaseConversion.convert_bases |
| 723 |
BaseConversion.convert_basesmod |
| 724 |
carrymod |
| 725 |
carry_mulmod |
| 726 |
carry_scmulmod |
| 727 |
carry_squaremod |
| 728 |
encodemod |
| 729 |
extend_to_length |
| 730 |
Freeze.from_bytes |
| 731 |
Freeze.to_bytes |
| 732 |
freeze_to_bytesmod |
| 733 |
from_bytesmod |
| 734 |
oppmod |
| 735 |
Partition.partition |
| 736 |
select |
| 737 |
submod |
| 738 |
to_bytesmod |
| 739 |
zeros |
| 740 |
zselect |
| 741 |
. |
| 742 |
|
| 743 |
Lemma carry_mul_correct res |
| 744 |
(Hres : carry_mul = Success res) |
| 745 |
: carry_mul_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds loose_bounds (Interp res). |
| 746 |
Proof using curve_good. prove_correctness (). Qed. |
| 747 |
|
| 748 |
Lemma Wf_carry_mul res (Hres : carry_mul = Success res) : Wf res. |
| 749 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 750 |
|
| 751 |
Lemma carry_square_correct res |
| 752 |
(Hres : carry_square = Success res) |
| 753 |
: carry_square_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds loose_bounds (Interp res). |
| 754 |
Proof using curve_good. prove_correctness (). Qed. |
| 755 |
|
| 756 |
Lemma Wf_carry_square res (Hres : carry_square = Success res) : Wf res. |
| 757 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 758 |
|
| 759 |
Lemma carry_scmul_const_correct a res |
| 760 |
(Hres : carry_scmul_const a = Success res) |
| 761 |
: carry_scmul_const_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds loose_bounds a (Interp res). |
| 762 |
Proof using curve_good. prove_correctness (). Qed. |
| 763 |
|
| 764 |
Lemma Wf_carry_scmul_const a res (Hres : carry_scmul_const a = Success res) : Wf res. |
| 765 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 766 |
|
| 767 |
Lemma carry_correct res |
| 768 |
(Hres : carry = Success res) |
| 769 |
: carry_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds loose_bounds (Interp res). |
| 770 |
Proof using curve_good. prove_correctness (). Qed. |
| 771 |
|
| 772 |
Lemma Wf_carry res (Hres : carry = Success res) : Wf res. |
| 773 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 774 |
|
| 775 |
Lemma add_correct res |
| 776 |
(Hres : add = Success res) |
| 777 |
: add_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds loose_bounds (Interp res). |
| 778 |
Proof using curve_good. prove_correctness (). Qed. |
| 779 |
|
| 780 |
Lemma Wf_add res (Hres : add = Success res) : Wf res. |
| 781 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 782 |
|
| 783 |
Lemma sub_correct res |
| 784 |
(Hres : sub = Success res) |
| 785 |
: sub_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds loose_bounds (Interp res). |
| 786 |
Proof using curve_good. prove_correctness (). Qed. |
| 787 |
|
| 788 |
Lemma Wf_sub res (Hres : sub = Success res) : Wf res. |
| 789 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 790 |
|
| 791 |
Lemma opp_correct res |
| 792 |
(Hres : opp = Success res) |
| 793 |
: opp_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds loose_bounds (Interp res). |
| 794 |
Proof using curve_good. prove_correctness (). Qed. |
| 795 |
|
| 796 |
Lemma Wf_opp res (Hres : opp = Success res) : Wf res. |
| 797 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 798 |
|
| 799 |
Lemma carry_add_correct res |
| 800 |
(Hres : carry_add = Success res) |
| 801 |
: carry_add_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds (Interp res). |
| 802 |
Proof using curve_good. prove_correctness (). Qed. |
| 803 |
|
| 804 |
Lemma Wf_carry_add res (Hres : carry_add = Success res) : Wf res. |
| 805 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 806 |
|
| 807 |
Lemma carry_sub_correct res |
| 808 |
(Hres : carry_sub = Success res) |
| 809 |
: carry_sub_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds (Interp res). |
| 810 |
Proof using curve_good. prove_correctness (). Qed. |
| 811 |
|
| 812 |
Lemma Wf_carry_sub res (Hres : carry_sub = Success res) : Wf res. |
| 813 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 814 |
|
| 815 |
Lemma carry_opp_correct res |
| 816 |
(Hres : carry_opp = Success res) |
| 817 |
: carry_opp_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds (Interp res). |
| 818 |
Proof using curve_good. prove_correctness (). Qed. |
| 819 |
|
| 820 |
Lemma Wf_carry_opp res (Hres : carry_opp = Success res) : Wf res. |
| 821 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 822 |
|
| 823 |
Lemma relax_correct res |
| 824 |
(Hres : relax = Success res) |
| 825 |
: relax_correct tight_bounds loose_bounds (Interp res). |
| 826 |
Proof using curve_good. prove_correctness (). Qed. |
| 827 |
|
| 828 |
Lemma Wf_relax res (Hres : relax = Success res) : Wf res. |
| 829 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 830 |
|
| 831 |
Lemma from_bytes_correct res |
| 832 |
(Hres : from_bytes = Success res) |
| 833 |
(Hrequests : request_present requests "from_bytes" = true) |
| 834 |
: from_bytes_correct (weight (Qnum limbwidth) (QDen limbwidth)) n n_bytes m s tight_bounds (Interp res). |
| 835 |
Proof using curve_good. prove_correctness (). Qed. |
| 836 |
|
| 837 |
Lemma Wf_from_bytes res (Hres : from_bytes = Success res) : Wf res. |
| 838 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 839 |
|
| 840 |
Lemma relax_valid |
| 841 |
: forall x, list_Z_bounded_by tight_bounds x -> list_Z_bounded_by loose_bounds x. |
| 842 |
Proof using Type. apply relax_list_Z_bounded_by, tight_bounds_tighter. Qed. |
| 843 |
|
| 844 |
Lemma to_bytes_correct res |
| 845 |
(Hres : to_bytes = Success res) |
| 846 |
(Hrequests : request_present requests "to_bytes" = true) |
| 847 |
: to_bytes_correct (weight (Qnum limbwidth) (QDen limbwidth)) n n_bytes m tight_bounds (Interp res). |
| 848 |
Proof using curve_good. |
| 849 |
prove_correctness (); []. |
| 850 |
erewrite freeze_to_bytesmod_partitions; [ reflexivity | .. ]. |
| 851 |
all: try solve_prove_correctness_side_conditions (). |
| 852 |
Qed. |
| 853 |
|
| 854 |
Lemma Wf_to_bytes res (Hres : to_bytes = Success res) : Wf res. |
| 855 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 856 |
|
| 857 |
Strategy -1000 [encode]. (* if we don't tell the kernel to unfold this early, then [Qed] seems to run off into the weeds *) |
| 858 |
Lemma encode_correct res |
| 859 |
(Hres : encode = Success res) |
| 860 |
: encode_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds (Interp res). |
| 861 |
Proof using curve_good. prove_correctness (). Qed. |
| 862 |
|
| 863 |
Lemma Wf_encode res (Hres : encode = Success res) : Wf res. |
| 864 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 865 |
|
| 866 |
Strategy -1000 [encode_word]. (* if we don't tell the kernel to unfold this early, then [Qed] seems to run off into the weeds *) |
| 867 |
Lemma encode_word_correct res |
| 868 |
(Hres : encode_word = Success res) |
| 869 |
: encode_word_correct machine_wordsize (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds (Interp res). |
| 870 |
Proof using curve_good. prove_correctness (). Qed. |
| 871 |
|
| 872 |
Lemma Wf_encode_word res (Hres : encode_word = Success res) : Wf res. |
| 873 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 874 |
|
| 875 |
Strategy -1000 [zero]. (* if we don't tell the kernel to unfold this early, then [Qed] seems to run off into the weeds *) |
| 876 |
Lemma zero_correct res |
| 877 |
(Hres : zero = Success res) |
| 878 |
: zero_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds (Interp res). |
| 879 |
Proof using curve_good. prove_correctness (). Qed. |
| 880 |
|
| 881 |
Lemma Wf_zero res (Hres : zero = Success res) : Wf res. |
| 882 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 883 |
|
| 884 |
Strategy -1000 [one]. (* if we don't tell the kernel to unfold this early, then [Qed] seems to run off into the weeds *) |
| 885 |
Lemma one_correct res |
| 886 |
(Hres : one = Success res) |
| 887 |
: one_correct (weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds (Interp res). |
| 888 |
Proof using curve_good. prove_correctness (). Qed. |
| 889 |
|
| 890 |
Lemma Wf_one res (Hres : one = Success res) : Wf res. |
| 891 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 892 |
|
| 893 |
Lemma copy_correct res |
| 894 |
(Hres : copy = Success res) |
| 895 |
: copy_correct saturated_bounds (Interp res). |
| 896 |
Proof using curve_good. apply Primitives.copy_correct; assumption. Qed. |
| 897 |
|
| 898 |
Lemma Wf_copy res (Hres : copy = Success res) : Wf res. |
| 899 |
Proof using Type. revert Hres; cbv [copy]; apply Wf_copy. Qed. |
| 900 |
|
| 901 |
Lemma selectznz_correct res |
| 902 |
(Hres : selectznz = Success res) |
| 903 |
: selectznz_correct saturated_bounds (Interp res). |
| 904 |
Proof using curve_good. apply Primitives.selectznz_correct, Hres. Qed. |
| 905 |
|
| 906 |
Lemma Wf_selectznz res (Hres : selectznz = Success res) : Wf res. |
| 907 |
Proof using Type. revert Hres; cbv [selectznz]; apply Wf_selectznz. Qed. |
| 908 |
|
| 909 |
Section ring. |
| 910 |
Context carry_mul_res (Hcarry_mul : carry_mul = Success carry_mul_res) |
| 911 |
add_res (Hadd : add = Success add_res) |
| 912 |
sub_res (Hsub : sub = Success sub_res) |
| 913 |
opp_res (Hopp : opp = Success opp_res) |
| 914 |
carry_res (Hcarry : carry = Success carry_res) |
| 915 |
encode_res (Hencode : encode = Success encode_res) |
| 916 |
zero_res (Hzero : zero = Success zero_res) |
| 917 |
one_res (Hone : one = Success one_res). |
| 918 |
|
| 919 |
Definition GoodT : Prop |
| 920 |
:= GoodT |
| 921 |
(weight (Qnum limbwidth) (QDen limbwidth)) n m tight_bounds |
| 922 |
(Interp carry_mul_res) |
| 923 |
(Interp add_res) |
| 924 |
(Interp sub_res) |
| 925 |
(Interp opp_res) |
| 926 |
(Interp carry_res) |
| 927 |
(Interp encode_res) |
| 928 |
(Interp zero_res) |
| 929 |
(Interp one_res). |
| 930 |
|
| 931 |
Theorem Good : GoodT. |
| 932 |
Proof using curve_good Hcarry_mul Hadd Hsub Hopp Hcarry Hencode Hzero Hone. |
| 933 |
pose proof use_curve_good; cbv zeta in *; destruct_head'_and. |
| 934 |
eapply Good. |
| 935 |
all: repeat first [ assumption |
| 936 |
| apply carry_mul_correct |
| 937 |
| apply add_correct |
| 938 |
| apply sub_correct |
| 939 |
| apply opp_correct |
| 940 |
| apply carry_correct |
| 941 |
| apply encode_correct |
| 942 |
| apply zero_correct |
| 943 |
| apply one_correct |
| 944 |
| apply relax_valid ]. |
| 945 |
Qed. |
| 946 |
End ring. |
| 947 |
|
| 948 |
Section for_stringification. |
| 949 |
Local Open Scope string_scope. |
| 950 |
Local Open Scope list_scope. |
| 951 |
|
| 952 |
Definition known_functions |
| 953 |
:= [("carry_mul", wrap_s scarry_mul); |
| 954 |
("carry_square", wrap_s scarry_square); |
| 955 |
("carry", wrap_s scarry); |
| 956 |
("add", wrap_s sadd); |
| 957 |
("sub", wrap_s ssub); |
| 958 |
("opp", wrap_s sopp); |
| 959 |
("carry_add", wrap_s scarry_add); |
| 960 |
("carry_sub", wrap_s scarry_sub); |
| 961 |
("carry_opp", wrap_s scarry_opp); |
| 962 |
("relax", wrap_s srelax); |
| 963 |
("selectznz", wrap_s sselectznz); |
| 964 |
("to_bytes", wrap_s sto_bytes); |
| 965 |
("from_bytes", wrap_s sfrom_bytes)]. |
| 966 |
|
| 967 |
Definition valid_names : string |
| 968 |
:= Eval compute in String.concat ", " (List.map (@fst _ _) known_functions) ++ ", or 'carry_scmul' followed by a decimal literal". |
| 969 |
|
| 970 |
Definition extra_special_synthesis (function_name_prefix : string) (name : string) |
| 971 |
: list (option { t : _ & string * Pipeline.M (Pipeline.ExtendedSynthesisResult t) }%type) |
| 972 |
:= [if prefix "carry_scmul" name |
| 973 |
then let sc := substring (String.length "carry_scmul") (String.length name) name in |
| 974 |
(scZ <- Decimal.Z.of_string sc; |
| 975 |
if (sc =? Decimal.Z.to_string scZ)%string |
| 976 |
then Some (wrap_s (fun _ => scarry_scmul_const function_name_prefix scZ) tt) |
| 977 |
else None)%option |
| 978 |
else None]. |
| 979 |
|
| 980 |
(** Note: If you change the name or type signature of this |
| 981 |
function, you will need to update the code in CLI.v *) |
| 982 |
Definition Synthesize (comment_header : list string) (function_name_prefix : string) (requests : list string) |
| 983 |
: list (synthesis_output_kind * string * Pipeline.M (list string)) |
| 984 |
:= Primitives.Synthesize |
| 985 |
machine_wordsize valid_names known_functions (extra_special_synthesis function_name_prefix) all_typedefs! |
| 986 |
check_args |
| 987 |
(ToString.comment_file_header_block |
| 988 |
(comment_header |
| 989 |
++ ["" |
| 990 |
; "Computed values:"] |
| 991 |
++ (List.map |
| 992 |
(fun s => " " ++ s)%string |
| 993 |
((ToString.prefix_and_indent "carry_chain = " [show idxs]) |
| 994 |
++ (ToString.prefix_and_indent "eval z = " [seval "z"]) |
| 995 |
++ (ToString.prefix_and_indent "bytes_eval z = " [sbytes_eval "z"]) |
| 996 |
++ (ToString.prefix_and_indent "balance = " [let show_lvl_Z := Hex.show_lvl_Z in show balance]))))) |
| 997 |
function_name_prefix requests. |
| 998 |
End for_stringification. |
| 999 |
End __. |
| 1000 |
|
| 1001 |
Module Export Hints. |
| 1002 |
#[global] |
| 1003 |
Hint Opaque |
| 1004 |
carry_mul |
| 1005 |
carry_square |
| 1006 |
carry_scmul_const |
| 1007 |
carry |
| 1008 |
add |
| 1009 |
sub |
| 1010 |
opp |
| 1011 |
carry_add |
| 1012 |
carry_sub |
| 1013 |
carry_opp |
| 1014 |
relax |
| 1015 |
from_bytes |
| 1016 |
to_bytes |
| 1017 |
encode |
| 1018 |
encode_word |
| 1019 |
zero |
| 1020 |
one |
| 1021 |
copy |
| 1022 |
selectznz |
| 1023 |
: wf_op_cache. |
| 1024 |
#[global] |
| 1025 |
Hint Immediate |
| 1026 |
Wf_carry_mul |
| 1027 |
Wf_carry_square |
| 1028 |
Wf_carry_scmul_const |
| 1029 |
Wf_carry |
| 1030 |
Wf_add |
| 1031 |
Wf_sub |
| 1032 |
Wf_opp |
| 1033 |
Wf_carry_add |
| 1034 |
Wf_carry_sub |
| 1035 |
Wf_carry_opp |
| 1036 |
Wf_relax |
| 1037 |
Wf_from_bytes |
| 1038 |
Wf_to_bytes |
| 1039 |
Wf_encode |
| 1040 |
Wf_encode_word |
| 1041 |
Wf_zero |
| 1042 |
Wf_one |
| 1043 |
Wf_copy |
| 1044 |
Wf_selectznz |
| 1045 |
: wf_op_cache. |
| 1046 |
End Hints. |