| 1 |
(** * Push-Button Synthesis of Montgomery Reduction *) |
| 2 |
From Coq Require Import String. |
| 3 |
From Coq Require Import ZArith. |
| 4 |
From Coq Require Import Lia. |
| 5 |
From Coq Require Import List. |
| 6 |
From Coq Require Import Derive. |
| 7 |
Require Import Crypto.Util.ErrorT. |
| 8 |
Require Import Crypto.Util.ListUtil. |
| 9 |
Require Import Crypto.Util.ZRange. |
| 10 |
Require Import Crypto.Util.ZUtil.Div. |
| 11 |
Require Import Crypto.Util.ZUtil.ModInv. |
| 12 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 13 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 14 |
Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall. |
| 15 |
Require Import Rewriter.Language.Language. |
| 16 |
Require Import Crypto.Language.API. |
| 17 |
Require Import Crypto.Stringification.Language. |
| 18 |
Require Import Crypto.Arithmetic.Core. |
| 19 |
Require Import Crypto.Arithmetic.FancyMontgomeryReduction. |
| 20 |
Require Import Crypto.BoundsPipeline. |
| 21 |
Require Import Crypto.COperationSpecifications. |
| 22 |
Require Import Crypto.Fancy.Compiler. |
| 23 |
Require Import Crypto.PushButtonSynthesis.ReificationCache. |
| 24 |
Require Import Crypto.PushButtonSynthesis.Primitives. |
| 25 |
Require Import Crypto.PushButtonSynthesis.FancyMontgomeryReductionReificationCache. |
| 26 |
Require Import Crypto.PushButtonSynthesis.InvertHighLow. |
| 27 |
Import ListNotations. |
| 28 |
Local Open Scope string_scope. Local Open Scope Z_scope. Local Open Scope list_scope. Local Open Scope bool_scope. |
| 29 |
|
| 30 |
Import |
| 31 |
Language.Compilers |
| 32 |
Language.Wf.Compilers |
| 33 |
Stringification.Language.Compilers. |
| 34 |
Import Compilers.API. |
| 35 |
|
| 36 |
Import COperationSpecifications.Primitives. |
| 37 |
|
| 38 |
Import COperationSpecifications.MontgomeryReduction. |
| 39 |
|
| 40 |
Import Associational Positional FancyMontgomeryReduction.MontgomeryReduction. |
| 41 |
|
| 42 |
Local Set Keyed Unification. (* needed for making [autorewrite] fast, c.f. COQBUG(https://github.com/coq/coq/issues/9283) *) |
| 43 |
|
| 44 |
Local Opaque reified_montred_gen. (* needed for making [autorewrite] not take a very long time *) |
| 45 |
|
| 46 |
Section rmontred. |
| 47 |
Context {output_language_api : ToString.OutputLanguageAPI} |
| 48 |
{pipeline_opts : PipelineOptions} |
| 49 |
{pipeline_to_string_opts : PipelineToStringOptions} |
| 50 |
{synthesis_opts : SynthesisOptions} |
| 51 |
(N R N' : Z) (n : nat) |
| 52 |
(machine_wordsize : machine_wordsize_opt). |
| 53 |
|
| 54 |
Let value_range := r[0 ~> (2^machine_wordsize - 1)%Z]%zrange. |
| 55 |
Let flag_range := r[0 ~> 1]%zrange. |
| 56 |
Let bound := Some value_range. |
| 57 |
Let consts_list := [N; N']. |
| 58 |
Let R' := Z.modinv R N. |
| 59 |
|
| 60 |
Definition possible_values_of_machine_wordsize |
| 61 |
:= [1; machine_wordsize / 2; machine_wordsize; 2 * machine_wordsize]%Z. |
| 62 |
Local Arguments possible_values_of_machine_wordsize / . |
| 63 |
|
| 64 |
Local Notation possible_values := possible_values_of_machine_wordsize. |
| 65 |
|
| 66 |
Local Instance no_select_size : no_select_size_opt := no_select_size_of_no_select machine_wordsize. |
| 67 |
Local Instance split_mul_to : split_mul_to_opt := split_mul_to_of_should_split_mul machine_wordsize possible_values. |
| 68 |
Local Instance split_multiret_to : split_multiret_to_opt := split_multiret_to_of_should_split_multiret machine_wordsize possible_values. |
| 69 |
|
| 70 |
Local Instance fancy_args : translate_to_fancy_opt |
| 71 |
:= (Some {| BoundsPipeline.invert_low log2wordsize := invert_low log2wordsize consts_list; |
| 72 |
BoundsPipeline.invert_high log2wordsize := invert_high log2wordsize consts_list; |
| 73 |
BoundsPipeline.value_range := value_range; |
| 74 |
BoundsPipeline.flag_range := flag_range |}). |
| 75 |
|
| 76 |
Local Instance fancy_args_good : translate_to_fancy_opt_correct. |
| 77 |
Proof using consts_list value_range. |
| 78 |
cbv [translate_to_fancy_opt_correct translate_to_fancy fancy_args invert_low invert_high constant_to_scalar constant_to_scalar_single consts_list fold_right]; |
| 79 |
split; intros; break_innermost_match_hyps; Z.ltb_to_lt; subst; congruence. |
| 80 |
Qed. |
| 81 |
|
| 82 |
(** Note: If you change the name or type signature of this |
| 83 |
function, you will need to update the code in CLI.v *) |
| 84 |
Definition check_args {T} (requests : list string) (res : Pipeline.ErrorT T) |
| 85 |
: Pipeline.ErrorT T |
| 86 |
:= fold_right |
| 87 |
(fun '(b, e) k => if b:bool then Error e else k) |
| 88 |
res |
| 89 |
[ |
| 90 |
((negb (1 <? R))%Z, Pipeline.Value_not_ltZ "R ≤ 1" 1 R); |
| 91 |
((n =? 0)%nat, Pipeline.Values_not_provably_distinctZ "n = 0" (Z.of_nat n) 0); |
| 92 |
((R' =? 0)%Z, Pipeline.No_modular_inverse "R⁻¹ mod N" R N); |
| 93 |
(negb ((R * R') mod N =? 1 mod N)%Z, Pipeline.Values_not_provably_equalZ "(R * R') mod N ≠ 1 mod N" ((R * R') mod N) (1 mod N)); |
| 94 |
(negb ((N * N') mod R =? (-1) mod R)%Z, Pipeline.Values_not_provably_equalZ "(N * N') mod R ≠ (-1) mod R" ((N * N') mod R) ((-1) mod R)); |
| 95 |
(negb (2 ^ machine_wordsize =? R)%Z, Pipeline.Values_not_provably_equalZ "2^machine_wordsize ≠ R" (2^machine_wordsize) R); |
| 96 |
((negb (0 <? N))%Z, Pipeline.Value_not_ltZ "N ≤ 0" 0 N); |
| 97 |
((negb (N <? R))%Z, Pipeline.Value_not_ltZ "R ≤ N" R N); |
| 98 |
((negb (0 <=? N'))%Z, Pipeline.Value_not_leZ "N' < 0" 0 N'); |
| 99 |
((negb (N' <? R))%Z, Pipeline.Value_not_ltZ "R ≤ N'" R N'); |
| 100 |
((negb (2 <=? machine_wordsize))%Z, Pipeline.Value_not_leZ "machine_wordsize < 2" 2 machine_wordsize)]. |
| 101 |
|
| 102 |
Local Arguments Z.mul !_ !_. |
| 103 |
|
| 104 |
Local Ltac use_curve_good_t := |
| 105 |
repeat first [ assumption |
| 106 |
| progress cbv [EquivModulo.Z.equiv_modulo] |
| 107 |
| progress rewrite ?Z.mul_0_r, ?Pos.mul_1_r, ?Z.mul_1_r in * |
| 108 |
| reflexivity |
| 109 |
| lia |
| 110 |
| progress cbn in * |
| 111 |
| progress intros |
| 112 |
| solve [ auto with zarith ] |
| 113 |
| rewrite Z.log2_pow2 by use_curve_good_t ]. |
| 114 |
|
| 115 |
Context (requests : list string) |
| 116 |
(curve_good : check_args requests (Success tt) = Success tt). |
| 117 |
|
| 118 |
Lemma use_curve_good |
| 119 |
: 0 <= N < R |
| 120 |
/\ 0 <= N' < R |
| 121 |
/\ N <> 0 |
| 122 |
/\ R > 1 |
| 123 |
/\ EquivModulo.Z.equiv_modulo R (N * N') (-1) |
| 124 |
/\ EquivModulo.Z.equiv_modulo N (R * R') 1 |
| 125 |
/\ n <> 0%nat |
| 126 |
/\ 2 <= machine_wordsize |
| 127 |
/\ 2 ^ machine_wordsize = R. |
| 128 |
Proof using curve_good. |
| 129 |
prepare_use_curve_good (). |
| 130 |
{ use_curve_good_t. } |
| 131 |
{ use_curve_good_t. } |
| 132 |
{ use_curve_good_t. } |
| 133 |
Qed. |
| 134 |
|
| 135 |
Definition montred |
| 136 |
:= Pipeline.BoundsPipeline |
| 137 |
false (* subst01 *) |
| 138 |
possible_values |
| 139 |
(reified_montred_gen |
| 140 |
@ GallinaReify.Reify N @ GallinaReify.Reify R @ GallinaReify.Reify N' @ GallinaReify.Reify (machine_wordsize:Z)) |
| 141 |
(bound, (bound, tt)) |
| 142 |
bound. |
| 143 |
|
| 144 |
Definition smontred (prefix : string) |
| 145 |
: string * (Pipeline.M (Pipeline.ExtendedSynthesisResult _)) |
| 146 |
:= Eval cbv beta in |
| 147 |
FromPipelineToString! |
| 148 |
machine_wordsize prefix "montred" montred |
| 149 |
(fun _ _ _ => @nil string). |
| 150 |
|
| 151 |
Local Ltac solve_montred_preconditions := |
| 152 |
repeat first [ lia |
| 153 |
| apply use_curve_good |
| 154 |
| progress (push_Zmod; pull_Zmod) |
| 155 |
| progress autorewrite with zsimplify_fast |
| 156 |
| rewrite Z.div_add' by lia |
| 157 |
| rewrite Z.div_small by lia |
| 158 |
| progress Z.rewrite_mod_small ]. |
| 159 |
|
| 160 |
Local Strategy -100 [montred]. (* needed for making Qed not take forever *) |
| 161 |
Local Strategy -100 [montred']. (* needed for making prove_correctness not take forever *) |
| 162 |
Lemma montred_correct res (Hres : montred = Success res) |
| 163 |
: montred_correct N R R' (API.Interp res). |
| 164 |
Proof using n curve_good. |
| 165 |
cbv [montred_correct]; intros. |
| 166 |
rewrite <- MontgomeryReduction.montred'_correct with (R:=R) (N':=N') (Zlog2R:=machine_wordsize) (n:=n) (lo:=lo) (hi:=hi) by solve_montred_preconditions. |
| 167 |
prove_correctness' ltac:(fun _ => idtac) use_curve_good. |
| 168 |
{ cbv [ZRange.type.base.option.is_bounded_by ZRange.type.base.is_bounded_by bound is_bounded_by_bool value_range upper lower]. |
| 169 |
rewrite Bool.andb_true_iff, !Z.leb_le. lia. } |
| 170 |
{ cbv [ZRange.type.base.option.is_bounded_by ZRange.type.base.is_bounded_by bound is_bounded_by_bool value_range upper lower]. |
| 171 |
rewrite Bool.andb_true_iff, !Z.leb_le. lia. } |
| 172 |
Qed. |
| 173 |
|
| 174 |
Lemma Wf_montred res (Hres : montred = Success res) : Wf res. |
| 175 |
Proof using Type. prove_pipeline_wf (). Qed. |
| 176 |
End rmontred. |
| 177 |
|
| 178 |
Module Export Hints. |
| 179 |
#[global] |
| 180 |
Hint Opaque |
| 181 |
montred |
| 182 |
: wf_op_cache. |
| 183 |
#[global] |
| 184 |
Hint Immediate |
| 185 |
Wf_montred |
| 186 |
: wf_op_cache. |
| 187 |
End Hints. |