| 1 |
From Coq Require Import Morphisms. |
| 2 |
|
| 3 |
Require Import Crypto.Spec.WeierstrassCurve. |
| 4 |
Require Import Crypto.Curves.Weierstrass.Affine Crypto.Curves.Weierstrass.AffineProofs. |
| 5 |
Require Import Crypto.Curves.Weierstrass.Jacobian.Jacobian. |
| 6 |
Require Import Crypto.Util.Decidable Crypto.Algebra.Field. |
| 7 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 8 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 9 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 10 |
Require Import Crypto.Util.Tactics.SetoidSubst. |
| 11 |
Require Import Crypto.Util.Notations Crypto.Util.LetIn. |
| 12 |
Require Import Crypto.Util.Sum Crypto.Util.Prod Crypto.Util.Sigma. |
| 13 |
Require Import Crypto.Util.FsatzAutoLemmas. |
| 14 |
From Coq Require Import BinPos. |
| 15 |
|
| 16 |
Module Jacobian. |
| 17 |
Section Co_Z. |
| 18 |
Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} {a b:F} |
| 19 |
{field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} |
| 20 |
{char_ge_3:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos (BinNat.N.two))} |
| 21 |
{Feq_dec:DecidableRel Feq}. |
| 22 |
Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 23 |
Local Notation "0" := Fzero. Local Notation "1" := Fone. |
| 24 |
Local Infix "+" := Fadd. Local Infix "-" := Fsub. |
| 25 |
Local Infix "*" := Fmul. Local Infix "/" := Fdiv. |
| 26 |
Local Notation "x ^ 2" := (x*x). Local Notation "x ^ 3" := (x^2*x). |
| 27 |
Local Notation "2" := (1+1). Local Notation "4" := (1+1+1+1). |
| 28 |
Local Notation "8" := (4+4). Local Notation "27" := (4*4 + 4+4 +1+1+1). |
| 29 |
Local Notation Wpoint := (@W.point F Feq Fadd Fmul a b). |
| 30 |
Context {char_ge_12:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul 12%positive} |
| 31 |
{discriminant_nonzero:id(4*a*a*a + 27*b*b <> 0)}. |
| 32 |
|
| 33 |
Local Notation Wopp := (@W.opp F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec). |
| 34 |
Local Notation Wadd := (@W.add F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv field Feq_dec char_ge_3 a b). |
| 35 |
Local Notation Wzero := (@W.zero F Feq Fadd Fmul a b). |
| 36 |
Local Notation Weq := (@W.eq F Feq Fadd Fmul a b). |
| 37 |
|
| 38 |
Local Notation point := (@Jacobian.point F Feq Fzero Fadd Fmul a b Feq_dec). |
| 39 |
Local Notation eq := (@Jacobian.eq F Feq Fzero Fadd Fmul a b Feq_dec). |
| 40 |
Local Notation opp := (@Jacobian.opp F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec). |
| 41 |
Local Notation add := (@Jacobian.add F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field char_ge_3). |
| 42 |
Local Notation double := (@Jacobian.double F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec). |
| 43 |
Local Notation of_affine := (@Jacobian.of_affine F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec). |
| 44 |
Local Notation to_affine := (@Jacobian.to_affine F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec). |
| 45 |
|
| 46 |
(* Stolen from Jacobian.v *) |
| 47 |
Ltac prept_step_opt := |
| 48 |
match goal with |
| 49 |
| [ H : ?T |- ?T ] => exact H |
| 50 |
| [ |- ?x = ?x ] => reflexivity |
| 51 |
| [ H : ?T, H' : ~?T |- _ ] => solve [ exfalso; apply H', H ] |
| 52 |
end. |
| 53 |
|
| 54 |
Ltac prept_step := |
| 55 |
match goal with |
| 56 |
| _ => progress prept_step_opt |
| 57 |
| _ => progress intros |
| 58 |
(*| _ => progress specialize_by_assumption*) |
| 59 |
(*| _ => progress specialize_by trivial*) |
| 60 |
| _ => progress cbv [proj1_sig fst snd] in * |
| 61 |
| _ => progress autounfold with points_as_coordinates in * |
| 62 |
| _ => progress destruct_head'_True |
| 63 |
| _ => progress destruct_head'_unit |
| 64 |
| _ => progress destruct_head'_prod |
| 65 |
| _ => progress destruct_head'_sig |
| 66 |
| _ => progress destruct_head'_and |
| 67 |
| _ => progress destruct_head'_sum |
| 68 |
| _ => progress destruct_head'_bool |
| 69 |
| _ => progress destruct_head'_or |
| 70 |
| H: context[@dec ?P ?pf] |- _ => destruct (@dec P pf) || destruct (@dec P pf) at 1 |
| 71 |
| |- context[@dec ?P ?pf] => destruct (@dec P pf) || destruct (@dec P pf) at 1 |
| 72 |
| |- ?P => lazymatch type of P with Prop => split end |
| 73 |
end. |
| 74 |
Ltac prept := repeat prept_step. |
| 75 |
Ltac t := prept; trivial; try contradiction; fsatz. |
| 76 |
|
| 77 |
Definition x_of (P : point) : F := |
| 78 |
match proj1_sig P with |
| 79 |
| (x, y, z) => x |
| 80 |
end. |
| 81 |
|
| 82 |
Definition y_of (P : point) : F := |
| 83 |
match proj1_sig P with |
| 84 |
| (x, y, z) => y |
| 85 |
end. |
| 86 |
|
| 87 |
Definition z_of (P : point) : F := |
| 88 |
match proj1_sig P with |
| 89 |
| (x, y, z) => z |
| 90 |
end. |
| 91 |
|
| 92 |
Create HintDb points_as_coordinates discriminated. |
| 93 |
Hint Unfold Proper respectful Reflexive Symmetric Transitive : points_as_coordinates. |
| 94 |
Hint Unfold Jacobian.point Jacobian.eq W.eq W.point W.coordinates proj1_sig fst snd : points_as_coordinates. |
| 95 |
Hint Unfold x_of y_of z_of : points_as_coordinates. |
| 96 |
|
| 97 |
Local Ltac clear_neq := |
| 98 |
repeat match goal with |
| 99 |
| [ H : _ <> _ |- _ ] => clear H |
| 100 |
end. |
| 101 |
Local Ltac clear_eq := |
| 102 |
repeat match goal with |
| 103 |
| [ H : _ = _ |- _ ] => clear H |
| 104 |
end. |
| 105 |
Local Ltac clear_eq_and_neq := |
| 106 |
repeat (clear_eq || clear_neq). |
| 107 |
Local Ltac clean_up_speed_up_fsatz := |
| 108 |
repeat match goal with |
| 109 |
| [ H : forall a : F, _ = _ -> _ = _ |- _ ] => clear H |
| 110 |
| [ H : forall a : F, _ <> _ -> _ = _ |- _ ] => clear H |
| 111 |
| [ H : forall a : F, _ <> _ -> _ <> _ |- _ ] => clear H |
| 112 |
| [ H : forall a b : F, _ = _ |- _ ] => clear H |
| 113 |
| [ H : forall a b : F, _ = _ -> _ = _ |- _ ] => clear H |
| 114 |
| [ H : forall a b : F, _ <> _ -> _ = _ |- _ ] => clear H |
| 115 |
| [ H : forall a b : F, _ <> _ -> _ <> _ |- _ ] => clear H |
| 116 |
| [ H : forall a b : F, _ = _ -> _ <> _ -> _ = _ |- _ ] => clear H |
| 117 |
| [ H : forall a b : F, _ <> _ -> _ <> _ -> _ = _ |- _ ] => clear H |
| 118 |
| [ H : forall a b : F, _ <> _ -> _ <> _ -> _ <> _ |- _ ] => clear H |
| 119 |
| [ H : forall a b c : F, _ |- _ ] => clear H |
| 120 |
| [ H : ?T, H' : ?T |- _ ] => clear H' |
| 121 |
| [ H : ?x = ?x |- _ ] => clear H |
| 122 |
| [ H : ?x <> 0, H' : ?x = 1 |- _ ] => clear H |
| 123 |
| [ H : ?x = 0 |- _ ] => is_var x; clear H x |
| 124 |
| [ H : ?x = 1 |- _ ] => is_var x; clear H x |
| 125 |
| [ H : Finv (?x^3) <> 0, H' : ?x <> 0 |- _ ] => clear H |
| 126 |
end. |
| 127 |
Local Ltac find_and_do_or_prove_by ty by_tac tac := |
| 128 |
lazymatch goal with |
| 129 |
| [ H' : ty |- _ ] |
| 130 |
=> tac H' |
| 131 |
| _ |
| 132 |
=> assert ty by by_tac |
| 133 |
end. |
| 134 |
Local Ltac find_and_do_or_prove_by_fsatz ty tac := |
| 135 |
find_and_do_or_prove_by ty ltac:(clear_eq_and_neq; intros; fsatz) tac. |
| 136 |
Local Ltac find_and_do_or_prove_by_fsatz_with_neq ty tac := |
| 137 |
find_and_do_or_prove_by ty ltac:(clear_neq; intros; fsatz) tac. |
| 138 |
Local Ltac find_and_goal_apply_or_prove_by_fsatz ty := |
| 139 |
find_and_do_or_prove_by_fsatz ty ltac:(fun H' => apply H'). |
| 140 |
Local Ltac find_and_apply_or_prove_by_fsatz H ty := |
| 141 |
find_and_do_or_prove_by_fsatz ty ltac:(fun H' => apply H' in H). |
| 142 |
Local Ltac find_and_apply_or_prove_by_fsatz2 H0 H1 ty := |
| 143 |
find_and_do_or_prove_by_fsatz ty ltac:(fun H' => apply H' in H0; [ | exact H1 ]). |
| 144 |
Local Ltac find_and_apply_or_prove_by_fsatz' H ty preapp := |
| 145 |
find_and_do_or_prove_by_fsatz ty ltac:(fun H' => let H' := preapp H' in apply H' in H). |
| 146 |
Local Ltac speed_up_fsatz_step_on pkg := |
| 147 |
let goal_if_var_neq_zero (* we want to keep lazymatch below, so we hack backtracking on is_var *) |
| 148 |
:= match goal with |
| 149 |
| [ |- ?x <> 0 ] => let test := match goal with _ => is_var x end in |
| 150 |
constr:(x <> 0) |
| 151 |
| _ => I |
| 152 |
end in |
| 153 |
lazymatch goal with |
| 154 |
| [ H : False |- _ ] => solve [ exfalso; exact H ] |
| 155 |
| [ H : ?T |- ?T ] => exact H |
| 156 |
| [ H : ?x <> ?x |- _ ] => solve [ exfalso; apply H; reflexivity ] |
| 157 |
| [ H : ?x = ?y, H' : ?x <> ?y |- _ ] => solve [ exfalso; apply H', H ] |
| 158 |
| [ H : ?x = 0 |- ?x * ?y <> 0 ] => exfalso |
| 159 |
| [ H : ?y = 0 |- ?x * ?y <> 0 ] => exfalso |
| 160 |
| [ H : ~?T |- ?T ] => exfalso |
| 161 |
| [ H : ?T |- ~?T ] => exfalso |
| 162 |
| [ H : ?x - ?x = 0 |- _ ] => clear H |
| 163 |
| [ H : ?x * ?y = 0, H' : ?x = 0 |- _ ] => clear H |
| 164 |
| [ H : ?x * ?y = 0, H' : ?y = 0 |- _ ] => clear H |
| 165 |
| [ |- goal_if_var_neq_zero ] => intro |
| 166 |
| [ H : ?x = Fopp ?x |- _ ] |
| 167 |
=> F.apply_lemma_in_on pkg H (forall a, a = Fopp a -> a = 0) |
| 168 |
| [ H : ?x <> Fopp ?x |- _ ] |
| 169 |
=> F.apply_lemma_in_on pkg H (forall a, a <> Fopp a -> a <> 0) |
| 170 |
| [ H : ?x + ?x = 0 |- _ ] |
| 171 |
=> F.apply_lemma_in_on pkg H (forall y, y + y = 0 -> y = 0) |
| 172 |
| [ H : Finv (?x^3) = 0, H' : ?x <> 0 |- _ ] |
| 173 |
=> F.apply2_lemma_in_on pkg H H' (forall a, Finv (a^3) = 0 -> a <> 0 -> False) |
| 174 |
| [ H : ?x - ?y = 0, H' : ?y <> ?x |- _ ] |
| 175 |
=> F.apply_lemma_in_on pkg H (forall a b, a - b = 0 -> b = a) |
| 176 |
| [ H : ?x - ?y = 0, H' : ?x <> ?y |- _ ] |
| 177 |
=> F.apply_lemma_in_on pkg H (forall a b, a - b = 0 -> a = b) |
| 178 |
| [ H : ?x * ?y * ?z <> 0, H' : ?y <> 0 |- _ ] |
| 179 |
=> F.apply_lemma_in_on pkg H (forall a b c, a * b * c <> 0 -> a * c <> 0) |
| 180 |
| [ H : ?x * ?y^3 <> 0, H' : ?y <> 0 |- _ ] |
| 181 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> a <> 0) |
| 182 |
| [ H : ?x * ?y^2 <> 0, H' : ?y <> 0 |- _ ] |
| 183 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> a <> 0) |
| 184 |
| [ H : ?x^2 - ?y^2 = ?z |- _ ] |
| 185 |
=> F.apply_lemma_in_on pkg H (forall a b c, a^2 - b^2 = c -> (a - b) * (a + b) = c) |
| 186 |
| [ H : ?x + ?y * ?z = 0, H' : ?x <> Fopp (?z * ?y) |- _ ] |
| 187 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a + b * c = 0 -> a <> Fopp (c * b) -> False) |
| 188 |
| [ H : ?x + ?x <> 0 |- _ ] |
| 189 |
=> F.apply_lemma_in_on pkg H (forall a, a + a <> 0 -> a <> 0) |
| 190 |
| [ H : ?x - ?y <> 0, H' : ?y = ?x |- _ ] |
| 191 |
=> F.apply_lemma_in_on pkg H (forall a b, a - b <> 0 -> b <> a) |
| 192 |
| [ H : ?x - ?y <> 0, H' : ?x = ?y |- _ ] |
| 193 |
=> F.apply_lemma_in_on pkg H (forall a b, a - b <> 0 -> a <> b) |
| 194 |
| [ H : (?x + ?y)^2 - (?x^2 + ?y^2) = 0 |- _ ] |
| 195 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - (a^2 + b^2) = 0 -> a * b = 0) |
| 196 |
| [ H : (?x + ?y)^2 - (?x^2 + ?y^2) <> 0 |- _ ] |
| 197 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - (a^2 + b^2) <> 0 -> a * b <> 0) |
| 198 |
| [ H : ?x * ?y = 0, H' : ?x <> 0 |- _ ] |
| 199 |
=> F.apply2_lemma_in_on pkg H H' (forall a b, a * b = 0 -> a <> 0 -> b = 0) |
| 200 |
| [ H : ?x * ?y = 0, H' : ?y <> 0 |- _ ] |
| 201 |
=> F.apply2_lemma_in_on pkg H H' (forall a b, a * b = 0 -> b <> 0 -> a = 0) |
| 202 |
| [ H : ?x * ?y <> 0, H' : ?x <> 0 |- _ ] |
| 203 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> b <> 0) |
| 204 |
| [ H : ?x * ?y <> 0, H' : ?y <> 0 |- _ ] |
| 205 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> a <> 0) |
| 206 |
| [ H : ?x * ?y <> 0, H' : ?x = 0 |- _ ] |
| 207 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> a <> 0) |
| 208 |
| [ H : ?x * ?y <> 0, H' : ?y = 0 |- _ ] |
| 209 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> b <> 0) |
| 210 |
| [ |- (?x + ?y)^2 - (?x^2 + ?y^2) = 0 ] |
| 211 |
=> F.goal_apply_lemma_on pkg (forall a b, a * b = 0 -> (a + b)^2 - (a^2 + b^2) = 0) |
| 212 |
| [ |- (?x + ?y)^2 - (?x^2 + ?y^2) <> 0 ] |
| 213 |
=> F.goal_apply_lemma_on pkg (forall a b, a * b <> 0 -> (a + b)^2 - (a^2 + b^2) <> 0) |
| 214 |
| [ |- (?x + ?y)^2 - ?x^2 - ?y^2 = 0 ] |
| 215 |
=> F.goal_apply_lemma_on pkg (forall a b, a * b = 0 -> (a + b)^2 - a^2 - b^2 = 0) |
| 216 |
| [ |- (?x + ?y)^2 - ?x^2 - ?y^2 <> 0 ] |
| 217 |
=> F.goal_apply_lemma_on pkg (forall a b, a * b <> 0 -> (a + b)^2 - a^2 - b^2 <> 0) |
| 218 |
| [ H : (?x + ?y)^2 - (?x^2 + ?y^2) = 0 |- _ ] |
| 219 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - (a^2 + b^2) = 0 -> a * b = 0) |
| 220 |
| [ H : (?x + ?y)^2 - (?x^2 + ?y^2) <> 0 |- _ ] |
| 221 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - (a^2 + b^2) <> 0 -> a * b <> 0) |
| 222 |
| [ H : (?x + ?y)^2 - ?x^2 - ?y^2 = 0 |- _ ] |
| 223 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - a^2 - b^2 = 0 -> a * b = 0) |
| 224 |
| [ H : (?x + ?y)^2 - ?x^2 - ?y^2 <> 0 |- _ ] |
| 225 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - a^2 - b^2 <> 0 -> a * b <> 0) |
| 226 |
| [ H : ?x = 0 |- ?x * ?y = 0 ] |
| 227 |
=> F.goal_apply_lemma_on pkg (forall a b, a = 0 -> a * b = 0) |
| 228 |
| [ H : ?y = 0 |- ?x * ?y = 0 ] |
| 229 |
=> F.goal_apply_lemma_on pkg (forall a b, b = 0 -> a * b = 0) |
| 230 |
| [ H : ?x <> 0 |- ?x * ?y <> 0 ] |
| 231 |
=> F.goal_apply_lemma_on pkg (forall a b, a <> 0 -> b <> 0 -> a * b <> 0) |
| 232 |
| [ H : ?y <> 0 |- ?x * ?y <> 0 ] |
| 233 |
=> F.goal_apply_lemma_on pkg (forall a b, a <> 0 -> b <> 0 -> a * b <> 0) |
| 234 |
| [ H : ?x <> 0 |- ?x * ?y = 0 ] |
| 235 |
=> F.goal_apply_lemma_on pkg (forall a b, b = 0 -> a * b = 0) |
| 236 |
| [ H : ?y <> 0 |- ?x * ?y = 0 ] |
| 237 |
=> F.goal_apply_lemma_on pkg (forall a b, a = 0 -> a * b = 0) |
| 238 |
|
| 239 |
| [ H : ?x - ?y * ?z <> ?w, H' : ?y = 1 |- _ ] |
| 240 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a - b * c <> d -> b = 1 -> a - c <> d) ltac:(fun H'' => constr:(fun Hv => H'' x y z w Hv H')) |
| 241 |
| [ H : ?x - ?y * ?z = ?w, H' : ?y = 1 |- _ ] |
| 242 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a - b * c = d -> b = 1 -> a - c = d) ltac:(fun H'' => constr:(fun Hv => H'' x y z w Hv H')) |
| 243 |
| [ H : ?x - ?y * ?z * ?w <> ?v, H' : ?y = 1 |- _ ] |
| 244 |
=> F.apply_lemma_in_on' pkg H (forall a b c d e, a - b * c * d <> e -> b = 1 -> a - c * d <> e) ltac:(fun H'' => constr:(fun Hv => H'' x y z w v Hv H')) |
| 245 |
| [ H : ?x - ?y * ?z * ?w = ?v, H' : ?y = 1 |- _ ] |
| 246 |
=> F.apply_lemma_in_on' pkg H (forall a b c d e, a - b * c * d = e -> b = 1 -> a - c * d = e) ltac:(fun H'' => constr:(fun Hv => H'' x y z w v Hv H')) |
| 247 |
| [ H : ?x - ?y * ?z^2 <> ?w, H' : ?z = 1 |- _ ] |
| 248 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a - b * c^2 <> d -> c = 1 -> a - b <> d) ltac:(fun H'' => constr:(fun Hv => H'' x y z w Hv H')) |
| 249 |
| [ H : ?x - ?y * ?z^2 = ?w, H' : ?z = 1 |- _ ] |
| 250 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a - b * c^2 = d -> c = 1 -> a - b = d) ltac:(fun H'' => constr:(fun Hv => H'' x y z w Hv H')) |
| 251 |
|
| 252 |
| [ H : ?x - ?y + ?z <> ?w, H' : ?y = 0 |- _ ] |
| 253 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a - b + c <> d -> b = 0 -> a + c <> d) ltac:(fun H'' => constr:(fun Hv => H'' x y z w Hv H')) |
| 254 |
| [ H : ?x - ?y <> ?z, H' : ?y = 0 |- _ ] |
| 255 |
=> F.apply_lemma_in_on' pkg H (forall a b c, a - b <> c -> b = 0 -> a <> c) ltac:(fun H'' => constr:(fun Hv => H'' x y z Hv H')) |
| 256 |
| [ H : ?x * ?y + ?z <> ?w, H' : ?x = 0 |- _ ] |
| 257 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a * b + c <> d -> a = 0 -> c <> d) ltac:(fun H'' => constr:(fun Hv => H'' x y z w Hv H')) |
| 258 |
| [ H : ?x * ?y + ?z <> ?w, H' : ?y = 0 |- _ ] |
| 259 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a * b + c <> d -> b = 0 -> c <> d) ltac:(fun H'' => constr:(fun Hv => H'' x y z w Hv H')) |
| 260 |
| [ H : ?x * ?y <> ?z, H' : ?x = 0 |- _ ] |
| 261 |
=> F.apply_lemma_in_on' pkg H (forall a b c, a * b <> c -> a = 0 -> c <> 0) ltac:(fun H'' => constr:(fun Hv => H'' x y z Hv H')) |
| 262 |
| [ H : ?x * ?y <> ?z, H' : ?y = 0 |- _ ] |
| 263 |
=> F.apply_lemma_in_on' pkg H (forall a b c, a * b <> c -> b = 0 -> c <> 0) ltac:(fun H'' => constr:(fun Hv => H'' x y z Hv H')) |
| 264 |
| [ H : Fopp (?x * ?y) <> ?z, H' : ?x = 0 |- _ ] |
| 265 |
=> F.apply_lemma_in_on' pkg H (forall a b c, Fopp (a * b) <> c -> a = 0 -> c <> 0) ltac:(fun H'' => constr:(fun Hv => H'' x y z Hv H')) |
| 266 |
| [ H : Fopp (?x * ?y) <> ?z, H' : ?y = 0 |- _ ] |
| 267 |
=> F.apply_lemma_in_on' pkg H (forall a b c, Fopp (a * b) <> c -> b = 0 -> c <> 0) ltac:(fun H'' => constr:(fun Hv => H'' x y z Hv H')) |
| 268 |
| [ H : ?x * ?y^3 = ?z, H' : ?y = 0 |- _ ] |
| 269 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a * b^3 = c -> b = 0 -> c = 0) |
| 270 |
| [ H : ?x * ?y = ?z, H' : ?x = 0 |- _ ] |
| 271 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a * b = c -> a = 0 -> c = 0) |
| 272 |
| [ H : ?x * ?y - ?z <> 0, H' : ?x = 0 |- _ ] |
| 273 |
=> F.apply_lemma_in_on' pkg H (forall a b c, a * b - c <> 0 -> a = 0 -> c <> 0) ltac:(fun H'' => constr:(fun Hv => H'' x y z Hv H')) |
| 274 |
| [ H : ?x * ?y - ?z <> 0, H' : ?y = 0 |- _ ] |
| 275 |
=> F.apply_lemma_in_on' pkg H (forall a b c, a * b - c <> 0 -> b = 0 -> c <> 0) ltac:(fun H'' => constr:(fun Hv => H'' x y z Hv H')) |
| 276 |
| [ H : ?x * ?y - ?z = 0, H' : ?x = 0 |- _ ] |
| 277 |
=> F.apply_lemma_in_on' pkg H (forall a b c, a * b - c = 0 -> a = 0 -> c = 0) ltac:(fun H'' => constr:(fun Hv => H'' x y z Hv H')) |
| 278 |
| [ H : ?x * ?y - ?z = 0, H' : ?y = 0 |- _ ] |
| 279 |
=> F.apply_lemma_in_on' pkg H (forall a b c, a * b - c = 0 -> b = 0 -> c = 0) ltac:(fun H'' => constr:(fun Hv => H'' x y z Hv H')) |
| 280 |
| [ H : ?x - ?y * ?z = 0, H' : ?z = 0 |- _ ] |
| 281 |
=> F.apply_lemma_in_on' pkg H (forall a b c, a - b * c = 0 -> c = 0 -> a = 0) ltac:(fun H'' => constr:(fun Hv => H'' x y z Hv H')) |
| 282 |
| [ H : ?x * (?y * ?y^2) - ?z <> ?w |- _ ] |
| 283 |
=> F.apply_lemma_in_on pkg H (forall a b c d, a * (b * b^2) - c <> d -> a * (b^3) - c <> d) |
| 284 |
| [ H : ?x * (?y * ?y^2) - ?z = ?w |- _ ] |
| 285 |
=> F.apply_lemma_in_on pkg H (forall a b c d, a * (b * b^2) - c = d -> a * (b^3) - c = d) |
| 286 |
| [ H : ?x - (?y * ?y^2) * ?z <> ?w |- _ ] |
| 287 |
=> F.apply_lemma_in_on pkg H (forall a b c d, a - (b * b^2) * c <> d -> a - (b^3) * c <> d) |
| 288 |
| [ H : ?x - (?y * ?y^2) * ?z = ?w |- _ ] |
| 289 |
=> F.apply_lemma_in_on pkg H (forall a b c d, a - (b * b^2) * c = d -> a - (b^3) * c = d) |
| 290 |
| [ H : ?x * (?y * ?y^2) = 0, H' : ?y <> 0 |- _ ] |
| 291 |
=> F.apply2_lemma_in_on pkg H H' (forall a b, a * (b * b^2) = 0 -> b <> 0 -> a = 0) |
| 292 |
| [ H : ?x * (?y * ?z) = 0, H' : ?z <> 0 |- _ ] |
| 293 |
=> F.apply_lemma_in_on' pkg H (forall a b c, c <> 0 -> a * (b * c) = 0 -> a * b = 0) ltac:(fun lem => constr:(lem x y z H')) |
| 294 |
| [ H : ?x = ?y * ?z, H' : ?y = 0 |- _ ] |
| 295 |
=> F.apply_lemma_in_on' pkg H (forall a b c, a = b * c -> b = 0 -> a = 0) ltac:(fun lem => constr:(fun H => lem x y z H H')) |
| 296 |
| [ H : ?x * ?y + ?z = 0, H' : ?x = 0 |- _ ] |
| 297 |
=> F.apply_lemma_in_on' pkg H (forall a b c, a = 0 -> a * b + c = 0 -> c = 0) ltac:(fun lem => constr:(lem x y z H')) |
| 298 |
| [ H : ?x * (?y * ?y^2) - ?z * ?z^2 * ?w = 0, H' : ?x * ?y^3 + ?w * ?z^3 = 0 |- _ ] |
| 299 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a * b^3 + d * c^3 = 0 -> a * (b * b^2) - c * c^2 * d = 0 -> a * b^3 = 0) ltac:(fun lem => constr:(lem x y z w H')) |
| 300 |
| [ H : ?x * Finv (?y^2) <> ?z * Finv (?w^2), Hy : ?y <> 0, Hw : ?w <> 0 |- _ ] |
| 301 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a * Finv (b^2) <> c * Finv (d^2) -> b <> 0 -> d <> 0 -> a * (d^2) <> c * (b^2)) ltac:(fun Hv => constr:(fun H => Hv x y z w H Hy Hw)) |
| 302 |
| [ H : ?x * Finv (?y^2) = ?z * Finv (?w^2), Hy : ?y <> 0, Hw : ?w <> 0 |- _ ] |
| 303 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a * Finv (b^2) = c * Finv (d^2) -> b <> 0 -> d <> 0 -> a * (d^2) = c * (b^2)) ltac:(fun Hv => constr:(fun H => Hv x y z w H Hy Hw)) |
| 304 |
| [ H : ?x * Finv (?y^3) = Fopp (?z * Finv (?w^3)), Hy : ?y <> 0, Hw : ?w <> 0 |- _ ] |
| 305 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a * Finv (b^3) = Fopp (c * Finv (d^3)) -> b <> 0 -> d <> 0 -> a * (d^3) = Fopp (c * (b^3))) ltac:(fun Hv => constr:(fun H => Hv x y z w H Hy Hw)) |
| 306 |
| [ H : ?x * Finv (?y^3) <> Fopp (?z * Finv (?w^3)), Hy : ?y <> 0, Hw : ?w <> 0 |- _ ] |
| 307 |
=> F.apply_lemma_in_on' pkg H (forall a b c d, a * Finv (b^3) <> Fopp (c * Finv (d^3)) -> b <> 0 -> d <> 0 -> a * (d^3) <> Fopp (c * (b^3))) ltac:(fun Hv => constr:(fun H => Hv x y z w H Hy Hw)) |
| 308 |
| [ H : ?x = Fopp (?y * ?z), H' : ?x - ?z * ?y = 0 |- _ ] |
| 309 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a = Fopp (b * c) -> a - c * b = 0 -> a = 0) |
| 310 |
| [ H : ?x = Fopp (?y * ?z), H' : ?x - ?z * ?y = 0 |- _ ] |
| 311 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a = Fopp (b * c) -> a - c * b = 0 -> a = 0) |
| 312 |
| [ H : ?x <> Fopp (?y * ?z), H' : ?x - ?z * ?y = 0 |- _ ] |
| 313 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a <> Fopp (b * c) -> a - c * b = 0 -> a * b * c <> 0) |
| 314 |
| [ H : ?x <> Fopp (?y * ?z), H' : ?x - ?z * ?y = 0 |- _ ] |
| 315 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a <> Fopp (b * c) -> a - c * b = 0 -> a * b * c <> 0) |
| 316 |
| [ H : ?y^2 = ?c^3 + ?a * ?c * (?x^2)^2 + ?b * (?x^3)^2, |
| 317 |
H' : ?y'^2 = ?c'^3 + ?a * ?c' * (?x'^2)^2 + ?b * (?x'^3)^2, |
| 318 |
H0 : ?c' * ?x^2 - ?c * ?x'^2 = 0 |
| 319 |
|- _ ] |
| 320 |
=> F.apply_lemma_in_on' pkg H (forall X Y X' Y' A B C C', Y^2 = C^3 + A * C * (X^2)^2 + B * (X^3)^2 -> Y'^2 = C'^3 + A * C' * (X'^2)^2 + B * (X'^3)^2 -> C' * X^2 - C * X'^2 = 0 -> (Y' * (X^3))^2 - ((X'^3) * Y)^2 = 0) ltac:(fun Hv => constr:(fun H => Hv x y x' y' a b c c' H H' H0)) |
| 321 |
| [ H : ?y^2 = ?c^3 + ?a * ?c * (?x^2)^2 + ?b * (?x^3)^2, |
| 322 |
H' : ?y'^2 = ?c'^3 + ?a * ?c' * (?x'^2)^2 + ?b * (?x'^3)^2, |
| 323 |
H0 : ?c' * ?x^2 = ?c * ?x'^2 |
| 324 |
|- _ ] |
| 325 |
=> F.apply_lemma_in_on' pkg H (forall X Y X' Y' A B C C', Y^2 = C^3 + A * C * (X^2)^2 + B * (X^3)^2 -> Y'^2 = C'^3 + A * C' * (X'^2)^2 + B * (X'^3)^2 -> C' * X^2 = C * X'^2 -> (Y' * (X^3))^2 - ((X'^3) * Y)^2 = 0) ltac:(fun Hv => constr:(fun H => Hv x y x' y' a b c c' H H' H0)) |
| 326 |
|
| 327 |
| [ H0 : ?x * ?y^3 = ?A * ?B^3, |
| 328 |
H1 : ?x * ?z^3 - ?B^3 * ?D = 0, |
| 329 |
H2 : ?D * ?C^3 = ?w * ?z^3, |
| 330 |
H3 : ?A * ?C^3 - ?y^3 * ?w <> 0, |
| 331 |
Hz : ?z <> 0, |
| 332 |
HB : ?B <> 0 |
| 333 |
|- _ ] |
| 334 |
=> exfalso; revert H0 H1 H2 H3 Hz HB; |
| 335 |
generalize x, y, z, w, A, B, C, D; |
| 336 |
F.goal_exact_lemma_on pkg |
| 337 |
| [ H0 : ?x * ?y^3 = ?A * ?B^3, |
| 338 |
H1 : ?x * ?z^3 - ?B^3 * ?D <> 0, |
| 339 |
H2 : ?D * ?C^3 = ?w * ?z^3, |
| 340 |
H3 : ?A * ?C^3 - ?y^3 * ?w = 0, |
| 341 |
Hy : ?y <> 0, |
| 342 |
HC : ?C <> 0 |
| 343 |
|- _ ] |
| 344 |
=> exfalso; revert H0 H1 H2 H3 Hy HC; |
| 345 |
generalize x, y, z, w, A, B, C, D; |
| 346 |
F.goal_exact_lemma_on pkg |
| 347 |
| [ H0 : ?x * ?y^2 = ?A * ?B^2, |
| 348 |
H1 : ?x * ?z^2 - ?D * ?B^2 = 0, |
| 349 |
H2 : ?D * ?C^2 = ?w * ?z^2, |
| 350 |
H3 : ?A * ?C^2 - ?w * ?y^2 <> 0, |
| 351 |
Hz : ?z <> 0, |
| 352 |
HB : ?B <> 0 |
| 353 |
|- _ ] |
| 354 |
=> exfalso; revert H0 H1 H2 H3 Hz HB; |
| 355 |
generalize x, y, z, w, A, B, C, D; |
| 356 |
F.goal_exact_lemma_on pkg |
| 357 |
| [ H0 : ?A * ?B^2 = ?x * ?y^2, |
| 358 |
H1 : ?x * ?z^2 - ?D * ?B^2 = 0, |
| 359 |
H2 : ?w * ?z^2 = ?D * ?C^2, |
| 360 |
H3 : ?A * ?C^2 - ?w * ?y^2 <> 0, |
| 361 |
Hz : ?z <> 0, |
| 362 |
HB : ?B <> 0 |
| 363 |
|- _ ] |
| 364 |
=> exfalso; revert H0 H1 H2 H3 Hz HB; |
| 365 |
generalize x, y, z, w, A, B, C, D; |
| 366 |
F.goal_exact_lemma_on pkg |
| 367 |
| [ H : ?x <> 0 |- context[Finv (?x^2)] ] |
| 368 |
=> F.with_lemma_on pkg (forall a, a <> 0 -> Finv (a^2) = (Finv a)^2) ltac:(fun H' => rewrite !(H' x H)) |
| 369 |
| [ H : ?x <> 0 |- context[Finv (?x^3)] ] |
| 370 |
=> F.with_lemma_on pkg (forall a, a <> 0 -> Finv (a^3) = (Finv a)^3) ltac:(fun H' => rewrite !(H' x H)) |
| 371 |
| [ H : ?x <> 0 |- context[Finv (Finv ?x)] ] |
| 372 |
=> F.with_lemma_on pkg (forall a, a <> 0 -> Finv (Finv a) = a) ltac:(fun H' => rewrite !(H' x H)) |
| 373 |
| [ |- context[Finv ((?x + ?y)^2 - ?x^2 - ?y^2)] ] |
| 374 |
=> F.with_lemma_on pkg (forall a b, (a + b)^2 - a^2 - b^2 = (1+1) * a * b) ltac:(fun H' => rewrite !(H' x y)) |
| 375 |
| [ |- context[Finv ((?x + ?y)^2 - (?x^2 + ?y^2))] ] |
| 376 |
=> F.with_lemma_on pkg (forall a b, (a + b)^2 - (a^2 + b^2) = (1+1) * a * b) ltac:(fun H' => rewrite !(H' x y)) |
| 377 |
| [ |- context[Finv (?x * ?y)] ] |
| 378 |
=> F.with_lemma_on |
| 379 |
pkg |
| 380 |
(forall a b, a * b <> 0 -> Finv (a * b) = Finv a * Finv b) |
| 381 |
ltac:(fun H' => rewrite !(H' x y) by (clear_eq; fsatz)) |
| 382 |
| _ => idtac |
| 383 |
end. |
| 384 |
Local Ltac speed_up_fsatz := |
| 385 |
let fld := guess_field in |
| 386 |
let pkg := F.get_package_on fld in |
| 387 |
divisions_to_inverses fld; |
| 388 |
repeat speed_up_fsatz_step_on pkg. |
| 389 |
Local Ltac speed_up_fsatz_check := |
| 390 |
let fld := guess_field in |
| 391 |
let pkg := F.get_package_on fld in |
| 392 |
speed_up_fsatz_step_on pkg. |
| 393 |
|
| 394 |
Local Ltac pre_pre_faster_t := |
| 395 |
prept; try contradiction; speed_up_fsatz. |
| 396 |
Local Ltac pre_faster_t := |
| 397 |
pre_pre_faster_t; speed_up_fsatz_check; clean_up_speed_up_fsatz. |
| 398 |
Local Ltac faster_t_noclear := |
| 399 |
pre_faster_t; fsatz. |
| 400 |
Local Ltac faster_t := |
| 401 |
pre_faster_t; |
| 402 |
try solve [ lazymatch goal with |
| 403 |
| [ |- _ = _ ] => clear_neq |
| 404 |
| _ => idtac |
| 405 |
end; |
| 406 |
fsatz ]. |
| 407 |
|
| 408 |
Local Hint Unfold W.add W.add' W.zero W.opp : points_as_coordinates. |
| 409 |
Local Hint Unfold Jacobian.double Jacobian.double_impl negb andb : points_as_coordinates. |
| 410 |
Local Hint Unfold Jacobian.to_affine Jacobian.to_affine_impl Jacobian.of_affine Jacobian.of_affine_impl Jacobian.add Jacobian.add_impl Jacobian.opp : points_as_coordinates. |
| 411 |
|
| 412 |
Lemma of_affine_add P Q |
| 413 |
: eq (of_affine (W.add P Q)) (add (of_affine P) (of_affine Q)). |
| 414 |
Proof. rewrite Jacobian.eq_iff, Jacobian.to_affine_add, 3Jacobian.to_affine_of_affine; reflexivity. Qed. |
| 415 |
|
| 416 |
Lemma add_comm (P Q : point) : |
| 417 |
eq (add P Q) (add Q P). |
| 418 |
Proof. |
| 419 |
pose proof W.commutative_group(discriminant_nonzero:=discriminant_nonzero) _. |
| 420 |
rewrite Jacobian.eq_iff, 2Jacobian.to_affine_add. |
| 421 |
rewrite Hierarchy.commutative. reflexivity. |
| 422 |
Qed. |
| 423 |
|
| 424 |
Lemma add_zero_l (P Q : point) (H : z_of P = 0) : |
| 425 |
eq (add P Q) Q. |
| 426 |
Proof. |
| 427 |
pose proof W.commutative_group(discriminant_nonzero:=discriminant_nonzero) _. |
| 428 |
rewrite Jacobian.eq_iff, Jacobian.to_affine_add. |
| 429 |
rewrite (proj1 (Jacobian.iszero_iff P)), Hierarchy.left_identity; [reflexivity|]. |
| 430 |
case P as [ [ []?] ?]; cbv [Jacobian.iszero z_of proj1_sig snd] in *; trivial. |
| 431 |
Qed. |
| 432 |
|
| 433 |
Lemma add_zero_r (P Q : point) (H : z_of Q = 0) : |
| 434 |
eq (add P Q) P. |
| 435 |
Proof. rewrite add_comm, add_zero_l; trivial; reflexivity. Qed. |
| 436 |
|
| 437 |
Lemma add_double (P Q : point) : |
| 438 |
eq P Q -> |
| 439 |
eq (add P Q) (double P). |
| 440 |
Proof. |
| 441 |
rewrite 2Jacobian.eq_iff, Jacobian.to_affine_add, Jacobian.to_affine_double. |
| 442 |
intros ->; reflexivity. |
| 443 |
Qed. |
| 444 |
|
| 445 |
Lemma add_opp_same_r (P : point) : |
| 446 |
eq (add P (opp P)) (of_affine W.zero). |
| 447 |
Proof. |
| 448 |
pose proof W.commutative_group(discriminant_nonzero:=discriminant_nonzero) _. |
| 449 |
rewrite Jacobian.eq_iff, Jacobian.to_affine_add, Jacobian.to_affine_of_affine, Jacobian.to_affine_opp. |
| 450 |
rewrite Hierarchy.right_inverse. reflexivity. |
| 451 |
Qed. |
| 452 |
|
| 453 |
Lemma z_of_eq_zero (P : point) : eq P (of_affine W.zero) -> z_of P = 0. |
| 454 |
Proof. prept. match goal with H : 0 <> 0 |- _ => case (H ltac:(reflexivity)) end. Qed. |
| 455 |
|
| 456 |
Lemma z_of_add_opp_same_r (P : point) : |
| 457 |
z_of (add P (opp P)) = 0. |
| 458 |
Proof. apply z_of_eq_zero, add_opp_same_r. Qed. |
| 459 |
|
| 460 |
(* This uses assumptions not present in Jacobian.v, |
| 461 |
namely char_ge_12 and discriminant_nonzero. *) |
| 462 |
Lemma add_assoc (P Q R : point) : |
| 463 |
eq (add (add P Q) R) (add P (add Q R)). |
| 464 |
Proof. |
| 465 |
rewrite <- (Jacobian.of_affine_to_affine (add (add _ _) _)). |
| 466 |
rewrite <- (Jacobian.of_affine_to_affine (add _ (add _ _))). |
| 467 |
rewrite (Jacobian.to_affine_add _ R). |
| 468 |
rewrite (Jacobian.to_affine_add P (add Q R)). |
| 469 |
do 2 rewrite of_affine_add. |
| 470 |
rewrite (Jacobian.to_affine_add P Q). |
| 471 |
rewrite (Jacobian.to_affine_add Q R). |
| 472 |
do 2 rewrite <- of_affine_add. |
| 473 |
destruct (@W.commutative_group F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv a b field Feq_dec char_ge_3 char_ge_12 ltac:(cbv [id] in *; fsatz)). |
| 474 |
destruct commutative_group_group. destruct group_monoid. |
| 475 |
destruct monoid_is_associative. |
| 476 |
rewrite associative. |
| 477 |
reflexivity. |
| 478 |
Qed. |
| 479 |
|
| 480 |
Definition is_point (P : F*F*F) : Prop := |
| 481 |
let '(X,Y,Z) := P in |
| 482 |
if dec (Z=0) then True else Y^2 = X^3 + a*X*(Z^2)^2 + b*(Z^3)^2. |
| 483 |
|
| 484 |
Definition co_z (P Q : point) : Prop := |
| 485 |
z_of P = z_of Q. |
| 486 |
|
| 487 |
Lemma opp_co_z (P : point) : |
| 488 |
co_z P (opp P). |
| 489 |
Proof. unfold co_z. prept. Qed. |
| 490 |
|
| 491 |
Program Definition make_co_z (P Q : point) (HQaff : z_of Q = 1) : point*point := |
| 492 |
match proj1_sig P, proj1_sig Q return (F*F*F)*(F*F*F) with |
| 493 |
| (x1, y1, z1), (x2, y2, z2) => |
| 494 |
let t1 := x2 in |
| 495 |
let t2 := y2 in |
| 496 |
let t3 := z1 in |
| 497 |
let t4 := t3^2 in |
| 498 |
let t1 := t4 * t1 in |
| 499 |
let t2 := t4 * t2 in |
| 500 |
let t2 := t3 * t2 in |
| 501 |
(P, (t1, t2, t3)) |
| 502 |
end. |
| 503 |
Next Obligation. Proof. prept. Qed. |
| 504 |
Next Obligation. Proof. prept. par: faster_t. Qed. |
| 505 |
|
| 506 |
Hint Unfold is_point co_z make_co_z : points_as_coordinates. |
| 507 |
|
| 508 |
Lemma make_co_z_correct (P Q : point) (HQaff : z_of Q = 1) |
| 509 |
(HPnz : z_of P <> 0) : |
| 510 |
let '(A, B) := make_co_z P Q HQaff in |
| 511 |
eq P A |
| 512 |
/\ eq Q B |
| 513 |
/\ co_z A B. |
| 514 |
Proof. faster_t. Qed. |
| 515 |
|
| 516 |
(* Scalar Multiplication on Weierstraß Elliptic Curves from Co-Z Arithmetic *) |
| 517 |
(* Goundar, Joye, Miyaji, Rivain, Vanelli *) |
| 518 |
(* Algorithm 19 Co-Z addition with update (register allocation) *) |
| 519 |
(* 6 field registers, 5M, 2S *) |
| 520 |
Program Definition zaddu (P Q : point) (H : co_z P Q) : point * point := |
| 521 |
match proj1_sig P, proj1_sig Q return (F*F*F)*(F*F*F) with |
| 522 |
| (x1, y1, z1), (x2, y2, z2) => |
| 523 |
let t1 := x1 in |
| 524 |
let t2 := y1 in |
| 525 |
let t3 := z1 in |
| 526 |
let t4 := x2 in |
| 527 |
let t5 := y2 in |
| 528 |
let t6 := t1 - t4 in |
| 529 |
let t3 := t3 * t6 in |
| 530 |
let t6 := t6^2 in |
| 531 |
let t1 := t1 * t6 in |
| 532 |
let t6 := t6 * t4 in |
| 533 |
let t5 := t2 - t5 in |
| 534 |
let t4 := t5^2 in |
| 535 |
let t4 := t4 - t1 in |
| 536 |
let t4 := t4 - t6 in |
| 537 |
let t6 := t1 - t6 in |
| 538 |
let t2 := t2 * t6 in |
| 539 |
let t6 := t1 - t4 in |
| 540 |
let t5 := t5 * t6 in |
| 541 |
let t5 := t5 - t2 in |
| 542 |
((t4, t5, t3), (t1, t2, t3)) |
| 543 |
end. |
| 544 |
Next Obligation. Proof. prept. all : faster_t_noclear. Qed. |
| 545 |
Next Obligation. Proof. prept. all : faster_t. Qed. |
| 546 |
|
| 547 |
Hint Unfold zaddu Jacobian.add_nz_nz : points_as_coordinates. |
| 548 |
|
| 549 |
(* ZADDU(P, Q) = (P + Q, P) if P <> Q, Q <> -P *) |
| 550 |
Lemma zaddu_correct (P Q : point) (H : co_z P Q) |
| 551 |
(Hneq : x_of P <> x_of Q): |
| 552 |
let '(R1, R2) := zaddu P Q H in |
| 553 |
eq (add P Q) R1 /\ eq P R2 /\ co_z R1 R2. |
| 554 |
Proof. prept. par : faster_t_noclear. Qed. |
| 555 |
|
| 556 |
Lemma zaddu_correct_alt (P Q : point) (H : co_z P Q) : |
| 557 |
let '(R1, R2) := zaddu P Q H in |
| 558 |
z_of R1 <> 0 -> |
| 559 |
eq (add P Q) R1 /\ eq P R2 /\ co_z R1 R2. |
| 560 |
Proof. |
| 561 |
generalize (zaddu_correct P Q H). |
| 562 |
rewrite (surjective_pairing (zaddu _ _ _)). |
| 563 |
intros A B. apply A. |
| 564 |
clear -B. t. |
| 565 |
Qed. |
| 566 |
|
| 567 |
Lemma zaddu_correct0 (P : point) : |
| 568 |
let '(R1, R2) := zaddu P (opp P) (opp_co_z P) in |
| 569 |
z_of R1 = 0 /\ co_z R1 R2. |
| 570 |
Proof. prept. all : faster_t_noclear. Qed. |
| 571 |
|
| 572 |
(* Scalar Multiplication on Weierstraß Elliptic Curves from Co-Z Arithmetic *) |
| 573 |
(* Goundar, Joye, Miyaji, Rivain, Vanelli *) |
| 574 |
(* Algorithm 20 Conjugate co-Z addition (register allocation) *) |
| 575 |
(* 7 field registers, 6M, 3S*) |
| 576 |
Program Definition zaddc (P Q : point) (H : co_z P Q) : point * point := |
| 577 |
match proj1_sig P, proj1_sig Q return (F*F*F)*(F*F*F) with |
| 578 |
| (x1, y1, z1), (x2, y2, z2) => |
| 579 |
let t1 := x1 in |
| 580 |
let t2 := y1 in |
| 581 |
let t3 := z1 in |
| 582 |
let t4 := x2 in |
| 583 |
let t5 := y2 in |
| 584 |
let t6 := t1 - t4 in |
| 585 |
let t3 := t3 * t6 in |
| 586 |
let t6 := t6^2 in |
| 587 |
let t7 := t1 * t6 in |
| 588 |
let t6 := t6 * t4 in |
| 589 |
let t1 := t2 + t5 in |
| 590 |
let t4 := t1^2 in |
| 591 |
let t4 := t4 - t7 in |
| 592 |
let t4 := t4 - t6 in |
| 593 |
let t1 := t2 - t5 in |
| 594 |
let t1 := t1^2 in |
| 595 |
let t1 := t1 - t7 in |
| 596 |
let t1 := t1 - t6 in |
| 597 |
let t6 := t6 - t7 in |
| 598 |
let t6 := t6 * t2 in |
| 599 |
let t2 := t2 - t5 in |
| 600 |
let t5 := t5 + t5 in |
| 601 |
let t5 := t2 + t5 in |
| 602 |
let t7 := t7 - t4 in |
| 603 |
let t5 := t5 * t7 in |
| 604 |
let t5 := t5 + t6 in |
| 605 |
let t7 := t4 + t7 in |
| 606 |
let t7 := t7 - t1 in |
| 607 |
let t2 := t2 * t7 in |
| 608 |
let t2 := t2 + t6 in |
| 609 |
((t1, t2, t3), (t4, t5, t3)) |
| 610 |
end. |
| 611 |
Next Obligation. Proof. prept. all : faster_t_noclear. Qed. |
| 612 |
Next Obligation. Proof. prept. all : faster_t_noclear. Qed. |
| 613 |
|
| 614 |
Hint Unfold zaddc : points_as_coordinates. |
| 615 |
(* ZADDC(P, Q) = (P + Q, P - Q) if P <> Q, Q <> -P *) |
| 616 |
Lemma zaddc_correct (P Q : point) (H : co_z P Q) |
| 617 |
(Hneq : x_of P <> x_of Q): |
| 618 |
let '(R1, R2) := zaddc P Q H in |
| 619 |
eq (add P Q) R1 /\ eq (add P (opp Q)) R2 /\ co_z R1 R2. |
| 620 |
Proof. prept. par : faster_t_noclear. Qed. |
| 621 |
|
| 622 |
Lemma zaddc_correct_alt (P Q : point) (H : co_z P Q) : |
| 623 |
let '(R1, R2) := zaddc P Q H in |
| 624 |
z_of R1 <> 0 -> |
| 625 |
eq (add P Q) R1 /\ eq (add P (opp Q)) R2 /\ co_z R1 R2. |
| 626 |
Proof. |
| 627 |
generalize (zaddc_correct P Q H). |
| 628 |
rewrite (surjective_pairing (zaddc _ _ _)). |
| 629 |
intros A B. apply A. |
| 630 |
clear -B. t. |
| 631 |
Qed. |
| 632 |
|
| 633 |
Lemma zaddc_correct0 (P : point) : |
| 634 |
let '(R1, R2) := zaddc P (opp P) (opp_co_z P) in |
| 635 |
z_of R1 = 0 /\ co_z R1 R2. |
| 636 |
Proof. faster_t_noclear. Qed. |
| 637 |
|
| 638 |
(* Scalar Multiplication on Weierstraß Elliptic Curves from Co-Z Arithmetic *) |
| 639 |
(* Goundar, Joye, Miyaji, Rivain, Vanelli *) |
| 640 |
(* Algorithm 21 Co-Z doubling with update (register allocation) *) |
| 641 |
(* 6 field registers, 1M, 5S *) |
| 642 |
Program Definition dblu (P : point) (H: z_of P = 1) : point * point := |
| 643 |
match proj1_sig P return (F*F*F)*(F*F*F) with |
| 644 |
| (x1, y1, _) => |
| 645 |
let t0 := a in |
| 646 |
let t1 := x1 in |
| 647 |
let t2 := y1 in |
| 648 |
let t3 := t2 + t2 in |
| 649 |
let t2 := t2^2 in |
| 650 |
let t4 := t1 + t2 in |
| 651 |
let t4 := t4^2 in |
| 652 |
let t5 := t1^2 in |
| 653 |
let t4 := t4 - t5 in |
| 654 |
let t2 := t2^2 in |
| 655 |
let t4 := t4 - t2 in |
| 656 |
let t1 := t4 + t4 in |
| 657 |
let t0 := t0 + t5 in |
| 658 |
let t5 := t5 + t5 in |
| 659 |
let t0 := t0 + t5 in |
| 660 |
let t4 := t0^2 in |
| 661 |
let t5 := t1 + t1 in |
| 662 |
let t4 := t4 - t5 in |
| 663 |
let t2 := 8 * t2 in |
| 664 |
let t5 := t1 - t4 in |
| 665 |
let t5 := t5 * t0 in |
| 666 |
let t5 := t5 - t2 in |
| 667 |
((t4, t5, t3), (t1, t2, t3)) |
| 668 |
end. |
| 669 |
Next Obligation. Proof. faster_t. Qed. |
| 670 |
Next Obligation. Proof. faster_t. Qed. |
| 671 |
|
| 672 |
Hint Unfold dblu : points_as_coordinates. |
| 673 |
|
| 674 |
(* DBLU(P) = (2P, P) when Z(P) = 1 *) |
| 675 |
Lemma dblu_correct (P : point) (H : z_of P = 1) |
| 676 |
(Hynz : y_of P <> 0) : |
| 677 |
let '(R1, R2) := dblu P H in |
| 678 |
eq (double P) R1 /\ eq P R2 /\ co_z R1 R2. |
| 679 |
Proof. faster_t. Qed. |
| 680 |
|
| 681 |
Lemma dblu_correct_alt (P : point) (H : z_of P = 1) : |
| 682 |
let '(R1, R2) := dblu P H in |
| 683 |
z_of R1 <> 0 -> |
| 684 |
eq (double P) R1 /\ eq P R2 /\ co_z R1 R2. |
| 685 |
Proof. |
| 686 |
generalize (dblu_correct P H). |
| 687 |
rewrite (surjective_pairing (dblu _ _)). |
| 688 |
intros A B. apply A. |
| 689 |
clear -B. t. |
| 690 |
Qed. |
| 691 |
|
| 692 |
Lemma dblu_correct0 (P : point) (H : z_of P = 1) |
| 693 |
(Hyz : y_of P = 0) : |
| 694 |
let '(R1, R2) := dblu P H in |
| 695 |
z_of R1 = 0 /\ co_z R1 R2. |
| 696 |
Proof. faster_t. Qed. |
| 697 |
|
| 698 |
(* Scalar Multiplication on Weierstraß Elliptic Curves from Co-Z Arithmetic *) |
| 699 |
(* Goundar, Joye, Miyaji, Rivain, Vanelli *) |
| 700 |
(* Algorithm 22 Co-Z tripling with update (register allocation) *) |
| 701 |
(* 6M, 7S *) |
| 702 |
Program Definition tplu (P : point) (H : z_of P = 1) : point * point := |
| 703 |
zaddu (snd (dblu P H)) (fst (dblu P H)) _. |
| 704 |
Next Obligation. faster_t. Qed. |
| 705 |
|
| 706 |
Hint Unfold tplu : points_as_coordinates. |
| 707 |
|
| 708 |
Lemma tplu_correct0 (P : point) (H : z_of P = 1) (Hyz : y_of P = 0) : |
| 709 |
let '(R1, R2) := tplu P H in |
| 710 |
z_of R1 = 0 /\ co_z R1 R2. |
| 711 |
Proof. faster_t. Qed. |
| 712 |
|
| 713 |
Lemma tplu_correct (P : point) (H : z_of P = 1) (Hynz : y_of P <> 0) : |
| 714 |
let '(R1, R2) := tplu P H in |
| 715 |
z_of R1 <> 0 -> |
| 716 |
eq (add (double P) P) R1 /\ eq P R2 /\ co_z R1 R2. |
| 717 |
Proof. |
| 718 |
rewrite (surjective_pairing (tplu P H)). |
| 719 |
unfold co_z, tplu. |
| 720 |
generalize (zaddu_correct_alt (snd (dblu P H)) (fst (dblu P H)) (tplu_obligation_1 P H)). |
| 721 |
destruct (zaddu (snd (dblu P H)) (fst (dblu P H)) (tplu_obligation_1 P H)) as [R1 R2] eqn:Hzaddu. |
| 722 |
intros A B. specialize (A B). destruct A as [A1 [A2 A3] ]. |
| 723 |
generalize (dblu_correct P H Hynz). |
| 724 |
rewrite (surjective_pairing (dblu P H)). intros [B1 [B2 B3] ]. |
| 725 |
simpl; repeat split; [| |assumption]. |
| 726 |
- rewrite B1. rewrite <- A1. |
| 727 |
rewrite B2 at 2. apply add_comm. |
| 728 |
- etransitivity; eauto. |
| 729 |
Qed. |
| 730 |
|
| 731 |
(* Scalar Multiplication on Weierstraß Elliptic Curves from Co-Z Arithmetic *) |
| 732 |
(* Goundar, Joye, Miyaji, Rivain, Vanelli *) |
| 733 |
(* Co-Z doubling-addition with update (naive version) *) |
| 734 |
(* 11M, 5S *) |
| 735 |
Program Definition zdau_naive (P Q : point) (H : co_z P Q) := |
| 736 |
zaddc (fst (zaddu P Q H)) (snd (zaddu P Q H)) _. |
| 737 |
Next Obligation. Proof. t. Qed. |
| 738 |
|
| 739 |
(* ZDAU(P, Q) = (2P + Q, Q) if P <> Q, Q <> -P *) |
| 740 |
Lemma zdau_naive_correct (P Q : point) (H : co_z P Q) |
| 741 |
(Hneq : x_of P <> x_of Q) : |
| 742 |
let '(R1, R2) := zdau_naive P Q H in |
| 743 |
z_of R1 <> 0 -> |
| 744 |
eq (add (double P) Q) R1 /\ eq Q R2 /\ co_z R1 R2. |
| 745 |
Proof. |
| 746 |
destruct (zdau_naive P Q H) as [R1 R2] eqn:HR. |
| 747 |
intros HR1. unfold zdau_naive in HR. |
| 748 |
generalize (zaddc_correct_alt (fst (zaddu P Q H)) (snd (zaddu P Q H)) (zdau_naive_obligation_1 P Q H)). rewrite HR. |
| 749 |
intros A. specialize (A HR1). |
| 750 |
destruct A as (A1 & A2 & A3). |
| 751 |
generalize (zaddu_correct P Q H Hneq). |
| 752 |
rewrite (surjective_pairing (zaddu P Q H)). |
| 753 |
intros (B1 & B2 & B3). |
| 754 |
repeat split; auto. |
| 755 |
- rewrite <- add_double, <- A1, <- B1, <- B2; [|reflexivity]. |
| 756 |
rewrite add_assoc, add_comm. reflexivity. |
| 757 |
- rewrite <- A2, <- B1, <- B2. |
| 758 |
rewrite (add_comm P Q). |
| 759 |
rewrite add_assoc. rewrite add_zero_r; [reflexivity|apply z_of_add_opp_same_r]. |
| 760 |
Qed. |
| 761 |
|
| 762 |
Lemma zdau_naive_correct_alt (P Q : point) (H : co_z P Q) |
| 763 |
(Hneq : x_of P <> x_of Q) |
| 764 |
(Hneq2: x_of (fst (zaddu P Q H)) <> x_of (snd (zaddu P Q H))) : |
| 765 |
let '(R1, R2) := zdau_naive P Q H in |
| 766 |
eq (add (double P) Q) R1 /\ eq Q R2 /\ co_z R1 R2. |
| 767 |
Proof. |
| 768 |
destruct (zdau_naive P Q H) as [R1 R2] eqn:HR. |
| 769 |
unfold zdau_naive in HR. |
| 770 |
generalize (zaddc_correct (fst (zaddu P Q H)) (snd (zaddu P Q H)) (zdau_naive_obligation_1 P Q H) Hneq2). rewrite HR. |
| 771 |
intros (A1 & A2 & A3). |
| 772 |
generalize (zaddu_correct P Q H Hneq). |
| 773 |
rewrite (surjective_pairing (zaddu P Q H)). |
| 774 |
intros (B1 & B2 & B3). |
| 775 |
repeat split; auto. |
| 776 |
- rewrite <- add_double, <- A1, <- B1, <- B2; [|reflexivity]. |
| 777 |
rewrite add_assoc, add_comm. reflexivity. |
| 778 |
- rewrite <- A2, <- B1, <- B2. |
| 779 |
rewrite (add_comm P Q). |
| 780 |
rewrite add_assoc. rewrite add_zero_r; [reflexivity|apply z_of_add_opp_same_r]. |
| 781 |
Qed. |
| 782 |
|
| 783 |
(* Scalar Multiplication on Weierstraß Elliptic Curves from Co-Z Arithmetic *) |
| 784 |
(* Goundar, Joye, Miyaji, Rivain, Vanelli *) |
| 785 |
(* Algorithm 23 Co-Z doubling-addition with update (register allocation) *) |
| 786 |
(* 8 field registers, 9M, 7S (instead of 11M, 5S) *) |
| 787 |
Program Definition zdau (P Q : point) (H : co_z P Q) : point * point := |
| 788 |
match proj1_sig P, proj1_sig Q return (F*F*F)*(F*F*F) with |
| 789 |
| (x1, y1, z1), (x2, y2, z2) => |
| 790 |
let t1 := x1 in |
| 791 |
let t2 := y1 in |
| 792 |
let t3 := z1 in |
| 793 |
let t4 := x2 in |
| 794 |
let t5 := y2 in |
| 795 |
let t6 := t1 - t4 in |
| 796 |
let t7 := t6^2 in |
| 797 |
let t1 := t1 * t7 in |
| 798 |
let t4 := t4 * t7 in |
| 799 |
let t5 := t2 - t5 in |
| 800 |
let t8 := t1 - t4 in |
| 801 |
let t2 := t2 * t8 in |
| 802 |
let t2 := t2 + t2 in |
| 803 |
let t8 := t5^2 in |
| 804 |
let t4 := t8 - t4 in |
| 805 |
let t4 := t4 - t1 in |
| 806 |
let t4 := t4 - t1 in |
| 807 |
let t6 := t4 + t6 in |
| 808 |
let t6 := t6^2 in |
| 809 |
let t6 := t6 - t7 in |
| 810 |
let t5 := t5 - t4 in |
| 811 |
let t5 := t5^2 in |
| 812 |
let t5 := t5 - t8 in |
| 813 |
let t5 := t5 - t2 in |
| 814 |
let t7 := t4^2 in |
| 815 |
let t5 := t5 - t7 in |
| 816 |
let t8 := t7 + t7 in |
| 817 |
let t8 := t8 + t8 in |
| 818 |
let t6 := t6 - t7 in |
| 819 |
let t3 := t3 * t6 in |
| 820 |
let t6 := t1 * t8 in |
| 821 |
let t1 := t1 + t4 in |
| 822 |
let t8 := t8 * t1 in |
| 823 |
let t7 := t2 + t5 in |
| 824 |
let t2 := t5 - t2 in |
| 825 |
let t1 := t8 - t6 in |
| 826 |
let t5 := t5 * t1 in |
| 827 |
let t6 := t6 + t8 in |
| 828 |
let t1 := t2^2 in |
| 829 |
let t1 := t1 - t6 in |
| 830 |
let t4 := t8 - t1 in |
| 831 |
let t2 := t2 * t4 in |
| 832 |
let t2 := t2 - t5 in |
| 833 |
let t4 := t7^2 in |
| 834 |
let t4 := t4 - t6 in |
| 835 |
let t8 := t8 - t4 in |
| 836 |
let t7 := t7 * t8 in |
| 837 |
let t5 := t7 - t5 in |
| 838 |
((t1, t2, t3), (t4, t5, t3)) |
| 839 |
end. |
| 840 |
Next Obligation. Proof. prept. par:setoid_subst_rel Feq; faster_t_noclear. Qed. |
| 841 |
Next Obligation. Proof. prept. par:faster_t_noclear. Qed. |
| 842 |
|
| 843 |
Hint Unfold zdau : points_as_coordinates. |
| 844 |
|
| 845 |
Lemma zdau_naive_eq_zdau (P Q : point) (H : co_z P Q) : |
| 846 |
let '(R1, R2) := zdau_naive P Q H in |
| 847 |
let '(S1, S2) := zdau P Q H in |
| 848 |
eq R1 S1 /\ eq R2 S2. |
| 849 |
Proof. prept. par : faster_t; try fsatz. Qed. |
| 850 |
|
| 851 |
(* Direct proof is intensive, which is why we need the naive implementation *) |
| 852 |
Lemma zdau_correct (P Q : point) (H : co_z P Q) |
| 853 |
(Hneq : x_of P <> x_of Q) : |
| 854 |
let '(R1, R2) := zdau P Q H in |
| 855 |
z_of R1 <> 0 -> |
| 856 |
eq (add (double P) Q) R1 /\ eq Q R2 /\ co_z R1 R2. |
| 857 |
Proof. |
| 858 |
generalize (zdau_naive_correct P Q H Hneq). |
| 859 |
generalize (zdau_naive_eq_zdau P Q H). |
| 860 |
rewrite (surjective_pairing (zdau_naive _ _ _)). |
| 861 |
rewrite (surjective_pairing (zdau _ _ _)). |
| 862 |
intros [A1 A2] X Y. |
| 863 |
assert (Z: forall A B, eq A B -> z_of A <> 0 -> z_of B <> 0) by (clear; intros; faster_t). |
| 864 |
repeat split. |
| 865 |
- rewrite <- A1. apply X. eapply Z; eauto. symmetry; assumption. |
| 866 |
- rewrite <- A2. apply X. eapply Z; eauto. symmetry; assumption. |
| 867 |
- clear -H. t. |
| 868 |
Qed. |
| 869 |
|
| 870 |
Lemma zdau_correct_alt (P Q : point) (H : co_z P Q) |
| 871 |
(Hneq : x_of P <> x_of Q) |
| 872 |
(Hneq2: x_of (fst (zaddu P Q H)) <> x_of (snd (zaddu P Q H))) : |
| 873 |
let '(R1, R2) := zdau P Q H in |
| 874 |
eq (add (double P) Q) R1 /\ eq Q R2 /\ co_z R1 R2. |
| 875 |
Proof. |
| 876 |
generalize (zdau_naive_correct_alt P Q H Hneq Hneq2). |
| 877 |
generalize (zdau_naive_eq_zdau P Q H). |
| 878 |
rewrite (surjective_pairing (zdau_naive _ _ _)). |
| 879 |
rewrite (surjective_pairing (zdau _ _ _)). |
| 880 |
intros [A1 A2] [A3 [A4 A5] ]. |
| 881 |
assert (Z: forall A B, eq A B -> z_of A <> 0 -> z_of B <> 0) by (clear; intros; faster_t). |
| 882 |
repeat split. |
| 883 |
- rewrite <- A1. auto. |
| 884 |
- rewrite <- A2. auto. |
| 885 |
- clear -H. t. |
| 886 |
Qed. |
| 887 |
|
| 888 |
Lemma zdau_correct0 (P : point) : |
| 889 |
let '(R1, R2) := zdau P (opp P) (opp_co_z P) in |
| 890 |
z_of R1 = 0 /\ co_z R1 R2. |
| 891 |
Proof. faster_t. Qed. |
| 892 |
End Co_Z. |
| 893 |
End Jacobian. |