| 1 |
Require Import Crypto.Spec.MxDH. |
| 2 |
Require Import Crypto.Algebra.Monoid Crypto.Algebra.Group Crypto.Algebra.Ring Crypto.Algebra.Field. |
| 3 |
Require Import Crypto.Util.Tuple Crypto.Util.Prod. |
| 4 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 5 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 6 |
|
| 7 |
Section MxDHRepChange. |
| 8 |
Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} {field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} {Feq_dec:Decidable.DecidableRel Feq} {Fcswap:bool->F*F->F*F->(F*F)*(F*F)} {Fa24:F} {tb1:nat->bool}. |
| 9 |
Context {K Keq Kzero Kone Kopp Kadd Ksub Kmul Kinv Kdiv} {impl_field:@Algebra.Hierarchy.field K Keq Kzero Kone Kopp Kadd Ksub Kmul Kinv Kdiv} {Keq_dec:Decidable.DecidableRel Keq} {Kcswap:bool->K*K->K*K->(K*K)*(K*K)} {Ka24:K} {tb2:nat->bool}. |
| 10 |
|
| 11 |
Context {FtoK} {homom:@Ring.is_homomorphism F Feq Fone Fadd Fmul |
| 12 |
K Keq Kone Kadd Kmul FtoK}. |
| 13 |
Context {homomorphism_inv_zero:Keq (FtoK (Finv Fzero)) (Kinv Kzero)}. |
| 14 |
Context {homomorphism_a24:Keq (FtoK Fa24) Ka24}. |
| 15 |
Context {Fcswap_correct:forall b x y, Fcswap b x y = if b then (y,x) else (x,y)}. |
| 16 |
Context {Kcswap_correct:forall b x y, Kcswap b x y = if b then (y,x) else (x,y)}. |
| 17 |
Context {tb2_correct:forall i, tb2 i = tb1 i}. |
| 18 |
|
| 19 |
(* TODO: move to algebra *) |
| 20 |
Lemma homomorphism_multiplicative_inverse_complete' x : Keq (FtoK (Finv x)) (Kinv (FtoK x)). |
| 21 |
Proof using Feq_dec Keq_dec field homom homomorphism_inv_zero impl_field. |
| 22 |
eapply (homomorphism_multiplicative_inverse_complete). |
| 23 |
intro J; rewrite J. rewrite homomorphism_inv_zero, homomorphism_id. |
| 24 |
reflexivity. |
| 25 |
Qed. |
| 26 |
|
| 27 |
Ltac t := |
| 28 |
let hom := match goal with H : is_homomorphism |- _ => H end in |
| 29 |
let mhom := constr:(@homomorphism_is_homomorphism _ _ _ _ _ _ _ _ _ _ _ hom) in |
| 30 |
repeat ( |
| 31 |
rewrite (@homomorphism_id _ _ _ _ _ _ _ _ _ _ _ _ _ mhom) || |
| 32 |
rewrite (@homomorphism_one _ _ _ _ _ _ _ _ _ _ _ hom) || |
| 33 |
rewrite (@homomorphism_sub _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ hom) || |
| 34 |
rewrite (@homomorphism_add _ _ _ _ _ _ _ _ _ _ _ hom) || |
| 35 |
rewrite (@homomorphism_mul _ _ _ _ _ _ _ _ _ _ _ hom) || |
| 36 |
rewrite homomorphism_a24 || |
| 37 |
rewrite homomorphism_multiplicative_inverse_complete' || |
| 38 |
reflexivity |
| 39 |
). |
| 40 |
|
| 41 |
(*Import Crypto.Util.Tactics.*) |
| 42 |
Import List. |
| 43 |
Import Coq.Classes.Morphisms. |
| 44 |
|
| 45 |
Global Instance Proper_ladderstep : |
| 46 |
Proper (Keq ==> (fieldwise (n:=2) Keq) ==> fieldwise (n:=2) Keq ==> fieldwise (n:=2) (fieldwise (n:=2) Keq)) (@MxDH.ladderstep K Kadd Ksub Kmul Ka24). |
| 47 |
Proof using Keq_dec impl_field. |
| 48 |
cbv [MxDH.ladderstep tuple tuple' fieldwise fieldwise' fst snd]; |
| 49 |
repeat intro; destruct_head' prod; destruct_head' and; repeat split; |
| 50 |
repeat match goal with [H:Keq ?x ?y |- _ ] => rewrite !H; clear H x end; reflexivity. |
| 51 |
Qed. |
| 52 |
|
| 53 |
Lemma MxLadderstepRepChange u P Q Ku (Ku_correct:Keq (FtoK u) Ku): |
| 54 |
fieldwise (n:=2) (fieldwise (n:=2) Keq) |
| 55 |
((Tuple.map (n:=2) (Tuple.map (n:=2) FtoK)) (@MxDH.ladderstep F Fadd Fsub Fmul Fa24 u P Q)) |
| 56 |
(@MxDH.ladderstep K Kadd Ksub Kmul Ka24 Ku (Tuple.map (n:=2) FtoK P) (Tuple.map (n:=2) FtoK Q)). |
| 57 |
Proof using Feq_dec Keq_dec field homom homomorphism_a24 impl_field. |
| 58 |
destruct P as [? ?], Q as [? ?]; cbv; repeat split; rewrite <-?Ku_correct; t. |
| 59 |
Qed. |
| 60 |
|
| 61 |
Let loopiter_sig F Fzero Fone Fadd Fsub Fmul Finv Fa24 Fcswap b tb u : |
| 62 |
{ loopiter | @MxDH.montladder F Fzero Fone Fadd Fsub Fmul Finv Fa24 Fcswap b tb u = |
| 63 |
let '(_, _, _) := MxDH.downto _ _ loopiter in _ } := exist _ _ eq_refl. |
| 64 |
Let loopiter F Fzero Fone Fadd Fsub Fmul Finv Fa24 Fcswap b tb u := |
| 65 |
Eval cbv [proj1_sig loopiter_sig] in ( |
| 66 |
proj1_sig (loopiter_sig F Fzero Fone Fadd Fsub Fmul Finv Fa24 Fcswap b tb u)). |
| 67 |
|
| 68 |
Let loopiter_phi s : ((K * K) * (K * K)) * bool := |
| 69 |
(Tuple.map (n:=2) (Tuple.map (n:=2) FtoK) (fst s), snd s). |
| 70 |
|
| 71 |
Let loopiter_eq (a b: (((K * K) * (K * K)) * bool)) := |
| 72 |
fieldwise (n:=2) (fieldwise (n:=2) Keq) (fst a) (fst b) /\ snd a = snd b. |
| 73 |
|
| 74 |
Local Instance Equivalence_loopiter_eq : Equivalence loopiter_eq. |
| 75 |
Proof using Keq_dec impl_field loopiter_phi. |
| 76 |
unfold loopiter_eq; split; repeat intro; |
| 77 |
intuition (reflexivity||symmetry;eauto||etransitivity;symmetry;eauto). |
| 78 |
Qed. |
| 79 |
|
| 80 |
Lemma MxLoopIterRepChange b Fu s i Ku (HKu:Keq (FtoK Fu) Ku) : loopiter_eq |
| 81 |
(loopiter_phi (loopiter F Fzero Fone Fadd Fsub Fmul Finv Fa24 Fcswap b tb1 Fu s i)) |
| 82 |
(loopiter K Kzero Kone Kadd Ksub Kmul Kinv Ka24 Kcswap b tb2 Ku (loopiter_phi s) i). |
| 83 |
Proof using Fcswap_correct Feq_dec Kcswap_correct Keq_dec field homom homomorphism_a24 impl_field tb2_correct. |
| 84 |
destruct_head' prod; break_match. |
| 85 |
simpl. |
| 86 |
rewrite !Fcswap_correct, !Kcswap_correct, tb2_correct in *. |
| 87 |
break_match; cbv [loopiter_eq loopiter_phi fst snd]; split; try reflexivity; |
| 88 |
(etransitivity;[|etransitivity]; [ | eapply (MxLadderstepRepChange _ _ _ _ HKu) | ]; |
| 89 |
match goal with Heqp:_ |- _ => rewrite <-Heqp; reflexivity end). |
| 90 |
Qed. |
| 91 |
|
| 92 |
Global Instance Proper_fold_left {A RA B RB} : |
| 93 |
Proper ((RA==>RB==>RA) ==> SetoidList.eqlistA RB ==> RA ==> RA) (@fold_left A B). |
| 94 |
Proof using Type. |
| 95 |
intros ? ? ? ? ? Hl; induction Hl; repeat intro; [assumption|]. |
| 96 |
simpl; cbv [Proper respectful] in *; eauto. |
| 97 |
Qed. |
| 98 |
|
| 99 |
Lemma proj_fold_left {A B L} R {Equivalence_R:@Equivalence B R} (proj:A->B) step step' {Proper_step':(R ==> eq ==> R)%signature step' step'} (xs:list L) init init' (H0:R (proj init) init') (Hproj:forall x i, R (proj (step x i)) (step' (proj x) i)) : |
| 100 |
R (proj (fold_left step xs init)) (fold_left step' xs init'). |
| 101 |
Proof using Type. |
| 102 |
generalize dependent init; generalize dependent init'. |
| 103 |
induction xs as [|?? IHxs]; [solve [eauto]|]. |
| 104 |
repeat intro; simpl; rewrite IHxs by eauto. |
| 105 |
apply (_ : Proper ((R ==> eq ==> R) ==> SetoidList.eqlistA eq ==> R ==> R) (@fold_left _ _)); |
| 106 |
try reflexivity; |
| 107 |
eapply Proper_step'; eauto. |
| 108 |
Qed. |
| 109 |
|
| 110 |
Global Instance Proper_downto {T R} {Equivalence_R:@Equivalence T R} : |
| 111 |
Proper (R ==> Logic.eq ==> (R==>Logic.eq==>R) ==> R) MxDH.downto. |
| 112 |
Proof using Type. |
| 113 |
intros s0 s0' Hs0 n' n Hn'; subst n'; generalize dependent s0; generalize dependent s0'. |
| 114 |
induction n; repeat intro; [assumption|]. |
| 115 |
unfold MxDH.downto; simpl. |
| 116 |
eapply Proper_fold_left; try eassumption; try reflexivity. |
| 117 |
cbv [Proper respectful] in *; eauto. |
| 118 |
Qed. |
| 119 |
|
| 120 |
Global Instance Proper_loopiter a b c : |
| 121 |
Proper (loopiter_eq ==> eq ==> loopiter_eq) (loopiter K Kzero Kone Kadd Ksub Kmul Kinv Ka24 Kcswap a b c). |
| 122 |
Proof using Kcswap_correct Keq_dec impl_field. |
| 123 |
unfold loopiter; intros [? ?] [? ?] [[[] []] ?]; repeat intro ; cbv [fst snd] in * |-; subst. |
| 124 |
repeat (break_match; break_match_hyps). |
| 125 |
split; [|reflexivity]. |
| 126 |
etransitivity; [|etransitivity]; [ | eapply Proper_ladderstep | ]; |
| 127 |
[eapply eq_subrelation; [ exact _ | symmetry; eassumption ] |
| 128 |
| reflexivity | | |
| 129 |
| eapply eq_subrelation; [exact _ | eassumption ] ]; |
| 130 |
rewrite !Kcswap_correct in *; |
| 131 |
match goal with [H: (if ?b then _ else _) = _ |- _] => destruct b end; |
| 132 |
destruct_head prod; inversion_prod; subst; eauto. |
| 133 |
Qed. |
| 134 |
|
| 135 |
Lemma MxDHRepChange b (x:F) : |
| 136 |
Keq |
| 137 |
(FtoK (@MxDH.montladder F Fzero Fone Fadd Fsub Fmul Finv Fa24 Fcswap b tb1 x)) |
| 138 |
(@MxDH.montladder K Kzero Kone Kadd Ksub Kmul Kinv Ka24 Kcswap b tb2 (FtoK x)). |
| 139 |
Proof using Fcswap_correct Feq_dec Kcswap_correct Keq_dec field homom homomorphism_a24 homomorphism_inv_zero impl_field tb2_correct. |
| 140 |
cbv [MxDH.montladder]. |
| 141 |
repeat break_match. |
| 142 |
assert (Hrel:loopiter_eq (loopiter_phi (p, p0, b0)) (p1, p3, b1)). |
| 143 |
{ |
| 144 |
rewrite <-Heqp0; rewrite <-Heqp. |
| 145 |
unfold MxDH.downto. |
| 146 |
eapply (proj_fold_left (L:=nat) loopiter_eq loopiter_phi). |
| 147 |
{ eapply @Proper_loopiter. } |
| 148 |
{ cbv; repeat split; t. } |
| 149 |
{ intros; eapply MxLoopIterRepChange; reflexivity. } |
| 150 |
} |
| 151 |
{ destruct_head' prod; destruct Hrel as [[[] []] ?]; simpl in *; subst. |
| 152 |
rewrite !Fcswap_correct, !Kcswap_correct in *. |
| 153 |
match goal with [H: (if ?b then _ else _) = _ |- _] => destruct b end; |
| 154 |
destruct_head prod; inversion_prod; subst; |
| 155 |
repeat match goal with [H: Keq (FtoK ?x) (?kx)|- _ ] => rewrite <- H end; |
| 156 |
t. |
| 157 |
} |
| 158 |
Unshelve. |
| 159 |
exact 0. |
| 160 |
exact 0. |
| 161 |
Qed. |
| 162 |
End MxDHRepChange. |