| 1 |
From Coq Require Import Morphisms. |
| 2 |
|
| 3 |
Require Import Crypto.Spec.WeierstrassCurve. |
| 4 |
Require Import Curves.Weierstrass.Affine. |
| 5 |
Require Import Crypto.Util.Decidable Crypto.Algebra.Field. |
| 6 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 7 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 8 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 9 |
Require Import Crypto.Util.Tactics.SetoidSubst. |
| 10 |
Require Import Util.Tactics.Beta1. |
| 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 |
|
| 15 |
Module Jacobian. |
| 16 |
Section Jacobian. |
| 17 |
Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} {a b:F} |
| 18 |
{field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} |
| 19 |
{char_ge_3:@Ring.char_ge F Feq Fzero Fone Fopp Fadd Fsub Fmul (BinNat.N.succ_pos (BinNat.N.two))} |
| 20 |
{Feq_dec:DecidableRel Feq}. |
| 21 |
Local Infix "=" := Feq : type_scope. Local Notation "a <> b" := (not (a = b)) : type_scope. |
| 22 |
Local Notation "0" := Fzero. Local Notation "1" := Fone. |
| 23 |
Local Infix "+" := Fadd. Local Infix "-" := Fsub. |
| 24 |
Local Infix "*" := Fmul. Local Infix "/" := Fdiv. |
| 25 |
Local Notation "x ^ 2" := (x*x). Local Notation "x ^ 3" := (x^2*x). |
| 26 |
Local Notation Wpoint := (@W.point F Feq Fadd Fmul a b). |
| 27 |
|
| 28 |
Definition point : Type := { P : F*F*F | let '(X,Y,Z) := P in |
| 29 |
if dec (Z=0) then True else Y^2 = X^3 + a*X*(Z^2)^2 + b*(Z^3)^2 }. |
| 30 |
Definition eq (P Q : point) : Prop := |
| 31 |
match proj1_sig P, proj1_sig Q with |
| 32 |
| (X1, Y1, Z1), (X2, Y2, Z2) => |
| 33 |
if dec (Z1 = 0) then Z2 = 0 |
| 34 |
else Z2 <> 0 /\ X1*Z2^2 = X2*Z1^2 /\ Y1*Z2^3 = Y2*Z1^3 |
| 35 |
end. |
| 36 |
|
| 37 |
(* These cases are not needed to solve the goal, but handling them early speeds things up considerably *) |
| 38 |
Ltac prept_step_opt := |
| 39 |
match goal with |
| 40 |
| [ H : ?T |- ?T ] => exact H |
| 41 |
| [ |- ?x = ?x ] => reflexivity |
| 42 |
| [ H : ?T, H' : ~?T |- _ ] => solve [ exfalso; apply H', H ] |
| 43 |
end. |
| 44 |
|
| 45 |
Ltac lift_let := |
| 46 |
match goal with |
| 47 |
| H : context G [let x := ?v in @?c x] |- _ => |
| 48 |
first [is_var v | is_constructor v]; |
| 49 |
let cx := beta1 constr:(c v) in |
| 50 |
let G' := context G [cx] in |
| 51 |
change G' in (type of H) |
| 52 |
| H : context G [let x := ?v in @?c x] |- _ => |
| 53 |
let y := fresh x in |
| 54 |
pose v as y; |
| 55 |
let cx := beta1 constr:(c y) in |
| 56 |
let G' := context G [cx] in |
| 57 |
change G' in (type of H) |
| 58 |
| H := context G [let x := ?v in @?c x] |- _ => |
| 59 |
first [is_var v | is_constructor v]; |
| 60 |
let cx := beta1 constr:(c v) in |
| 61 |
let G' := context G [cx] in |
| 62 |
change G' in (value of H) |
| 63 |
| H := context G [let x := ?v in @?c x] |- _ => |
| 64 |
let y := fresh x in |
| 65 |
pose v as y; |
| 66 |
let cx := beta1 constr:(c y) in |
| 67 |
let G' := context G [cx] in |
| 68 |
change G' in (value of H) |
| 69 |
| |- context G [let x := ?v in @?c x] => |
| 70 |
first [is_var v | is_constructor v]; |
| 71 |
let cx := beta1 constr:(c v) in |
| 72 |
let G' := context G [cx] in |
| 73 |
change G' |
| 74 |
| |- context G [let x := ?v in @?c x] => |
| 75 |
let y := fresh x in |
| 76 |
pose v as y; |
| 77 |
let cx := beta1 constr:(c y) in |
| 78 |
let G' := context G [cx] in |
| 79 |
change G' |
| 80 |
end. |
| 81 |
|
| 82 |
Ltac subst_lets := repeat match goal with x := _ |- _ => subst x end. |
| 83 |
|
| 84 |
Ltac prept_step := |
| 85 |
match goal with |
| 86 |
| _ => progress prept_step_opt |
| 87 |
| _ => progress intros |
| 88 |
| _ => lift_let |
| 89 |
(*| _ => progress specialize_by_assumption*) |
| 90 |
(*| _ => progress specialize_by trivial*) |
| 91 |
| _ => progress cbv beta match delta [proj1_sig fst snd] in * |
| 92 |
| _ => progress autounfold with points_as_coordinates in * |
| 93 |
| _ => progress destruct_head'_True |
| 94 |
| _ => progress destruct_head'_unit |
| 95 |
| _ => progress destruct_head'_prod |
| 96 |
| _ => progress destruct_head'_sig |
| 97 |
| _ => progress destruct_head'_and |
| 98 |
| _ => progress destruct_head'_sum |
| 99 |
| _ => progress destruct_head'_bool |
| 100 |
| _ => progress destruct_head'_or |
| 101 |
| H: context[@dec ?P ?pf] |- _ => destruct (@dec P pf) |
| 102 |
| |- context[@dec ?P ?pf] => destruct (@dec P pf) |
| 103 |
| |- ?P => lazymatch type of P with Prop => split end |
| 104 |
end. |
| 105 |
Ltac prept := repeat prept_step. |
| 106 |
Ltac t := prept; trivial; try contradiction; subst_lets; fsatz. |
| 107 |
|
| 108 |
Create HintDb points_as_coordinates discriminated. |
| 109 |
Hint Unfold Proper respectful Reflexive Symmetric Transitive : points_as_coordinates. |
| 110 |
Hint Unfold point eq W.eq W.point W.coordinates W.eq W.add W.zero proj1_sig fst snd : points_as_coordinates. |
| 111 |
|
| 112 |
Global Instance Equivalence_eq : Equivalence eq. |
| 113 |
Proof. t. Qed. |
| 114 |
|
| 115 |
Definition of_affine_impl (P : F*F + unit) : F*F*F := |
| 116 |
match P with |
| 117 |
| inl (x, y) => (x, y, 1) |
| 118 |
| inr _ => (0, 0, 0) |
| 119 |
end. |
| 120 |
|
| 121 |
Definition of_affine (P : Wpoint) : point. refine ( |
| 122 |
exist _ (of_affine_impl (proj1_sig P)) _). |
| 123 |
Proof. abstract (cbv [of_affine_impl]; t). Defined. |
| 124 |
|
| 125 |
Definition to_affine_impl (P : F*F*F) : F*F+unit := |
| 126 |
match P return F*F+_ with |
| 127 |
| (X, Y, Z) => |
| 128 |
if dec (Z = 0) then inr tt |
| 129 |
else inl (X/Z^2, Y/Z^3) |
| 130 |
end. |
| 131 |
|
| 132 |
Definition to_affine (P:point) : Wpoint. refine (exist _ ( |
| 133 |
to_affine_impl (proj1_sig P)) _). |
| 134 |
Proof. abstract (cbv [to_affine_impl]; t). Defined. |
| 135 |
|
| 136 |
Hint Unfold to_affine_impl to_affine of_affine_impl of_affine : points_as_coordinates. |
| 137 |
|
| 138 |
Global Instance Proper_of_affine : Proper (W.eq ==> eq) of_affine. Proof. t. Qed. |
| 139 |
|
| 140 |
Lemma to_affine_of_affine P : W.eq (to_affine (of_affine P)) P. |
| 141 |
Proof. t. Qed. |
| 142 |
|
| 143 |
Lemma eq_iff P Q : eq P Q <-> W.eq (to_affine P) (to_affine Q). Proof. t. Qed. |
| 144 |
|
| 145 |
Global Instance Proper_to_affine : Proper (eq ==> W.eq) to_affine. |
| 146 |
Proof. cbv [respectful Proper]; intros. apply eq_iff; trivial. Qed. |
| 147 |
|
| 148 |
Lemma of_affine_to_affine P : eq (of_affine (to_affine P)) P. |
| 149 |
Proof. apply eq_iff, to_affine_of_affine. Qed. |
| 150 |
|
| 151 |
Definition iszero (P : point) := snd (proj1_sig P) = 0. |
| 152 |
|
| 153 |
Hint Unfold iszero : points_as_coordiantes. |
| 154 |
|
| 155 |
Lemma iszero_iff P : iszero P <-> W.eq (to_affine P) W.zero. |
| 156 |
Proof. cbv [iszero W.zero]; t. Qed. |
| 157 |
|
| 158 |
Definition opp (P:point) : point. refine (exist _ ( |
| 159 |
match proj1_sig P return F*F*F with |
| 160 |
| (X, Y, Z) => (X, Fopp Y, Z) |
| 161 |
end) _). |
| 162 |
Proof. abstract t. Defined. |
| 163 |
|
| 164 |
Hint Unfold opp W.opp : points_as_coordinates. |
| 165 |
|
| 166 |
Global Instance Proper_opp : Proper (eq ==> eq) opp. Proof. t. Qed. |
| 167 |
|
| 168 |
Lemma to_affine_opp P : W.eq (to_affine (opp P)) (W.opp (to_affine P)). |
| 169 |
Proof. t. Qed. |
| 170 |
|
| 171 |
(** From http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-2007-bl *) |
| 172 |
Definition double_impl (P : F*F*F) : F*F*F := |
| 173 |
let z := snd P in let x := fst (fst P) in let y := snd (fst P) in |
| 174 |
let xx := x^2 in |
| 175 |
let yy := y^2 in |
| 176 |
let yyyy := yy^2 in |
| 177 |
let zz := z^2 in |
| 178 |
(** [s = 2*((x + yy)^2 - xx - yyyy)] *) |
| 179 |
let s := x + yy in |
| 180 |
let s := s^2 in |
| 181 |
let s := s - xx in |
| 182 |
let s := s - yyyy in |
| 183 |
let s := s + s in |
| 184 |
(** [m = 3*xx + a*zz^2] *) |
| 185 |
let m := zz^2 in |
| 186 |
let m := a * m in |
| 187 |
let m := m + xx in |
| 188 |
let m := m + xx in |
| 189 |
let m := m + xx in |
| 190 |
(** [x_out = m^2 - 2*s] *) |
| 191 |
let x_out := m^2 in |
| 192 |
let x_out := x_out - s in |
| 193 |
let x_out := x_out - s in |
| 194 |
(** [z_out = (y + z)^2 - yy - zz] *) |
| 195 |
let z_out := y + z in |
| 196 |
let z_out := z_out^2 in |
| 197 |
let z_out := z_out - yy in |
| 198 |
let z_out := z_out - zz in |
| 199 |
(** [y_out = m*(s-x_out) - 8*yyyy] *) |
| 200 |
let yyyy := yyyy + yyyy in |
| 201 |
let yyyy := yyyy + yyyy in |
| 202 |
let yyyy := yyyy + yyyy in |
| 203 |
let y_out := s - x_out in |
| 204 |
let y_out := y_out * m in |
| 205 |
let y_out := y_out - yyyy in |
| 206 |
(x_out, y_out, z_out). |
| 207 |
|
| 208 |
Definition double (P : point) : point. refine (exist _ ( |
| 209 |
double_impl (proj1_sig P)) _). |
| 210 |
Proof. abstract (cbv [double_impl]; t). Defined. |
| 211 |
|
| 212 |
Local Ltac clear_neq := |
| 213 |
repeat match goal with |
| 214 |
| [ H : _ <> _ |- _ ] => clear H |
| 215 |
end. |
| 216 |
Local Ltac clear_eq := |
| 217 |
repeat match goal with |
| 218 |
| [ H : _ = _ |- _ ] => clear H |
| 219 |
end. |
| 220 |
Local Ltac clear_eq_and_neq := |
| 221 |
repeat (clear_eq || clear_neq). |
| 222 |
Local Ltac clean_up_speed_up_fsatz := |
| 223 |
repeat match goal with |
| 224 |
| [ H : forall a : F, _ = _ -> _ = _ |- _ ] => clear H |
| 225 |
| [ H : forall a : F, _ <> _ -> _ = _ |- _ ] => clear H |
| 226 |
| [ H : forall a : F, _ <> _ -> _ <> _ |- _ ] => clear H |
| 227 |
| [ H : forall a b : F, _ = _ |- _ ] => clear H |
| 228 |
| [ H : forall a b : F, _ = _ -> _ = _ |- _ ] => clear H |
| 229 |
| [ H : forall a b : F, _ <> _ -> _ = _ |- _ ] => clear H |
| 230 |
| [ H : forall a b : F, _ <> _ -> _ <> _ |- _ ] => clear H |
| 231 |
| [ H : forall a b : F, _ = _ -> _ <> _ -> _ = _ |- _ ] => clear H |
| 232 |
| [ H : forall a b : F, _ <> _ -> _ <> _ -> _ = _ |- _ ] => clear H |
| 233 |
| [ H : forall a b : F, _ <> _ -> _ <> _ -> _ <> _ |- _ ] => clear H |
| 234 |
| [ H : forall a b c : F, _ |- _ ] => clear H |
| 235 |
| [ H : ?T, H' : ?T |- _ ] => clear H' |
| 236 |
| [ H : ?x = ?x |- _ ] => clear H |
| 237 |
| [ H : ?x <> 0, H' : ?x = 1 |- _ ] => clear H |
| 238 |
| [ H : ?x = 0 |- _ ] => is_var x; clear H x |
| 239 |
| [ H : ?x = 1 |- _ ] => is_var x; clear H x |
| 240 |
| [ H : Finv (?x^3) <> 0, H' : ?x <> 0 |- _ ] => clear H |
| 241 |
end. |
| 242 |
Local Ltac find_and_do_or_prove_by ty by_tac tac := |
| 243 |
lazymatch goal with |
| 244 |
| [ H' : ty |- _ ] |
| 245 |
=> tac H' |
| 246 |
| _ |
| 247 |
=> assert ty by by_tac |
| 248 |
end. |
| 249 |
Local Ltac find_and_do_or_prove_by_fsatz ty tac := |
| 250 |
find_and_do_or_prove_by ty ltac:(clear_eq_and_neq; intros; fsatz) tac. |
| 251 |
Local Ltac find_and_do_or_prove_by_fsatz_with_neq ty tac := |
| 252 |
find_and_do_or_prove_by ty ltac:(clear_neq; intros; fsatz) tac. |
| 253 |
Local Ltac find_and_goal_apply_or_prove_by_fsatz ty := |
| 254 |
find_and_do_or_prove_by_fsatz ty ltac:(fun H' => apply H'). |
| 255 |
Local Ltac find_and_apply_or_prove_by_fsatz H ty := |
| 256 |
find_and_do_or_prove_by_fsatz ty ltac:(fun H' => apply H' in H). |
| 257 |
Local Ltac find_and_apply_or_prove_by_fsatz2 H0 H1 ty := |
| 258 |
find_and_do_or_prove_by_fsatz ty ltac:(fun H' => apply H' in H0; [ | exact H1 ]). |
| 259 |
Local Ltac find_and_apply_or_prove_by_fsatz' H ty preapp := |
| 260 |
find_and_do_or_prove_by_fsatz ty ltac:(fun H' => let H' := preapp H' in apply H' in H). |
| 261 |
Local Ltac speed_up_fsatz_step_on pkg := |
| 262 |
let goal_if_var_neq_zero (* we want to keep lazymatch below, so we hack backtracking on is_var *) |
| 263 |
:= match goal with |
| 264 |
| [ |- ?x <> 0 ] => let test := match goal with _ => is_var x end in |
| 265 |
constr:(x <> 0) |
| 266 |
| _ => I |
| 267 |
end in |
| 268 |
lazymatch goal with |
| 269 |
| [ H : False |- _ ] => solve [ exfalso; exact H ] |
| 270 |
| [ H : ?T |- ?T ] => exact H |
| 271 |
| [ H : ?x <> ?x |- _ ] => solve [ exfalso; apply H; reflexivity ] |
| 272 |
| [ H : ?x = ?y, H' : ?x <> ?y |- _ ] => solve [ exfalso; apply H', H ] |
| 273 |
| [ H : ?x = 0 |- ?x * ?y <> 0 ] => exfalso |
| 274 |
| [ H : ?y = 0 |- ?x * ?y <> 0 ] => exfalso |
| 275 |
| [ H : ~?T |- ?T ] => exfalso |
| 276 |
| [ H : ?T |- ~?T ] => exfalso |
| 277 |
| [ H : ?x - ?x = 0 |- _ ] => clear H |
| 278 |
| [ H : ?x * ?y = 0, H' : ?x = 0 |- _ ] => clear H |
| 279 |
| [ H : ?x * ?y = 0, H' : ?y = 0 |- _ ] => clear H |
| 280 |
| [ |- goal_if_var_neq_zero ] => intro |
| 281 |
| [ H : ?x = Fopp ?x |- _ ] |
| 282 |
=> F.apply_lemma_in_on pkg H (forall a, a = Fopp a -> a = 0) |
| 283 |
| [ H : ?x <> Fopp ?x |- _ ] |
| 284 |
=> F.apply_lemma_in_on pkg H (forall a, a <> Fopp a -> a <> 0) |
| 285 |
| [ H : ?x + ?x = 0 |- _ ] |
| 286 |
=> F.apply_lemma_in_on pkg H (forall y, y + y = 0 -> y = 0) |
| 287 |
| [ H : Finv (?x^3) = 0, H' : ?x <> 0 |- _ ] |
| 288 |
=> F.apply2_lemma_in_on pkg H H' (forall a, Finv (a^3) = 0 -> a <> 0 -> False) |
| 289 |
| [ H : ?x - ?y = 0, H' : ?y <> ?x |- _ ] |
| 290 |
=> F.apply_lemma_in_on pkg H (forall a b, a - b = 0 -> b = a) |
| 291 |
| [ H : ?x - ?y = 0, H' : ?x <> ?y |- _ ] |
| 292 |
=> F.apply_lemma_in_on pkg H (forall a b, a - b = 0 -> a = b) |
| 293 |
| [ H : ?x * ?y * ?z <> 0, H' : ?y <> 0 |- _ ] |
| 294 |
=> F.apply_lemma_in_on pkg H (forall a b c, a * b * c <> 0 -> a * c <> 0) |
| 295 |
| [ H : ?x * ?y^3 <> 0, H' : ?y <> 0 |- _ ] |
| 296 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> a <> 0) |
| 297 |
| [ H : ?x * ?y^2 <> 0, H' : ?y <> 0 |- _ ] |
| 298 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> a <> 0) |
| 299 |
| [ H : ?x^2 - ?y^2 = ?z |- _ ] |
| 300 |
=> F.apply_lemma_in_on pkg H (forall a b c, a^2 - b^2 = c -> (a - b) * (a + b) = c) |
| 301 |
| [ H : ?x + ?y * ?z = 0, H' : ?x <> Fopp (?z * ?y) |- _ ] |
| 302 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a + b * c = 0 -> a <> Fopp (c * b) -> False) |
| 303 |
| [ H : ?x + ?x <> 0 |- _ ] |
| 304 |
=> F.apply_lemma_in_on pkg H (forall a, a + a <> 0 -> a <> 0) |
| 305 |
| [ H : ?x - ?y <> 0, H' : ?y = ?x |- _ ] |
| 306 |
=> F.apply_lemma_in_on pkg H (forall a b, a - b <> 0 -> b <> a) |
| 307 |
| [ H : ?x - ?y <> 0, H' : ?x = ?y |- _ ] |
| 308 |
=> F.apply_lemma_in_on pkg H (forall a b, a - b <> 0 -> a <> b) |
| 309 |
| [ H : (?x + ?y)^2 - (?x^2 + ?y^2) = 0 |- _ ] |
| 310 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - (a^2 + b^2) = 0 -> a * b = 0) |
| 311 |
| [ H : (?x + ?y)^2 - (?x^2 + ?y^2) <> 0 |- _ ] |
| 312 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - (a^2 + b^2) <> 0 -> a * b <> 0) |
| 313 |
| [ H : ?x * ?y = 0, H' : ?x <> 0 |- _ ] |
| 314 |
=> F.apply2_lemma_in_on pkg H H' (forall a b, a * b = 0 -> a <> 0 -> b = 0) |
| 315 |
| [ H : ?x * ?y = 0, H' : ?y <> 0 |- _ ] |
| 316 |
=> F.apply2_lemma_in_on pkg H H' (forall a b, a * b = 0 -> b <> 0 -> a = 0) |
| 317 |
| [ H : ?x * ?y <> 0, H' : ?x <> 0 |- _ ] |
| 318 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> b <> 0) |
| 319 |
| [ H : ?x * ?y <> 0, H' : ?y <> 0 |- _ ] |
| 320 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> a <> 0) |
| 321 |
| [ H : ?x * ?y <> 0, H' : ?x = 0 |- _ ] |
| 322 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> a <> 0) |
| 323 |
| [ H : ?x * ?y <> 0, H' : ?y = 0 |- _ ] |
| 324 |
=> F.apply_lemma_in_on pkg H (forall a b, a * b <> 0 -> b <> 0) |
| 325 |
| [ |- (?x + ?y)^2 - (?x^2 + ?y^2) = 0 ] |
| 326 |
=> F.goal_apply_lemma_on pkg (forall a b, a * b = 0 -> (a + b)^2 - (a^2 + b^2) = 0) |
| 327 |
| [ |- (?x + ?y)^2 - (?x^2 + ?y^2) <> 0 ] |
| 328 |
=> F.goal_apply_lemma_on pkg (forall a b, a * b <> 0 -> (a + b)^2 - (a^2 + b^2) <> 0) |
| 329 |
| [ |- (?x + ?y)^2 - ?x^2 - ?y^2 = 0 ] |
| 330 |
=> F.goal_apply_lemma_on pkg (forall a b, a * b = 0 -> (a + b)^2 - a^2 - b^2 = 0) |
| 331 |
| [ |- (?x + ?y)^2 - ?x^2 - ?y^2 <> 0 ] |
| 332 |
=> F.goal_apply_lemma_on pkg (forall a b, a * b <> 0 -> (a + b)^2 - a^2 - b^2 <> 0) |
| 333 |
| [ H : (?x + ?y)^2 - (?x^2 + ?y^2) = 0 |- _ ] |
| 334 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - (a^2 + b^2) = 0 -> a * b = 0) |
| 335 |
| [ H : (?x + ?y)^2 - (?x^2 + ?y^2) <> 0 |- _ ] |
| 336 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - (a^2 + b^2) <> 0 -> a * b <> 0) |
| 337 |
| [ H : (?x + ?y)^2 - ?x^2 - ?y^2 = 0 |- _ ] |
| 338 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - a^2 - b^2 = 0 -> a * b = 0) |
| 339 |
| [ H : (?x + ?y)^2 - ?x^2 - ?y^2 <> 0 |- _ ] |
| 340 |
=> F.apply_lemma_in_on pkg H (forall a b, (a + b)^2 - a^2 - b^2 <> 0 -> a * b <> 0) |
| 341 |
| [ H : ?x = 0 |- ?x * ?y = 0 ] |
| 342 |
=> F.goal_apply_lemma_on pkg (forall a b, a = 0 -> a * b = 0) |
| 343 |
| [ H : ?y = 0 |- ?x * ?y = 0 ] |
| 344 |
=> F.goal_apply_lemma_on pkg (forall a b, b = 0 -> a * b = 0) |
| 345 |
| [ H : ?x <> 0 |- ?x * ?y <> 0 ] |
| 346 |
=> F.goal_apply_lemma_on pkg (forall a b, a <> 0 -> b <> 0 -> a * b <> 0) |
| 347 |
| [ H : ?y <> 0 |- ?x * ?y <> 0 ] |
| 348 |
=> F.goal_apply_lemma_on pkg (forall a b, a <> 0 -> b <> 0 -> a * b <> 0) |
| 349 |
| [ H : ?x <> 0 |- ?x * ?y = 0 ] |
| 350 |
=> F.goal_apply_lemma_on pkg (forall a b, b = 0 -> a * b = 0) |
| 351 |
| [ H : ?y <> 0 |- ?x * ?y = 0 ] |
| 352 |
=> F.goal_apply_lemma_on pkg (forall a b, a = 0 -> a * b = 0) |
| 353 |
|
| 354 |
| [ H : ?x - ?y * ?z <> ?w, H' : ?y = 1 |- _ ] |
| 355 |
=> 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')) |
| 356 |
| [ H : ?x - ?y * ?z = ?w, H' : ?y = 1 |- _ ] |
| 357 |
=> 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')) |
| 358 |
| [ H : ?x - ?y * ?z * ?w <> ?v, H' : ?y = 1 |- _ ] |
| 359 |
=> 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')) |
| 360 |
| [ H : ?x - ?y * ?z * ?w = ?v, H' : ?y = 1 |- _ ] |
| 361 |
=> 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')) |
| 362 |
| [ H : ?x - ?y * ?z^2 <> ?w, H' : ?z = 1 |- _ ] |
| 363 |
=> 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')) |
| 364 |
| [ H : ?x - ?y * ?z^2 = ?w, H' : ?z = 1 |- _ ] |
| 365 |
=> 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')) |
| 366 |
|
| 367 |
| [ H : ?x - ?y + ?z <> ?w, H' : ?y = 0 |- _ ] |
| 368 |
=> 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')) |
| 369 |
| [ H : ?x - ?y <> ?z, H' : ?y = 0 |- _ ] |
| 370 |
=> 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')) |
| 371 |
| [ H : ?x * ?y + ?z <> ?w, H' : ?x = 0 |- _ ] |
| 372 |
=> 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')) |
| 373 |
| [ H : ?x * ?y + ?z <> ?w, H' : ?y = 0 |- _ ] |
| 374 |
=> 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')) |
| 375 |
| [ H : ?x * ?y <> ?z, H' : ?x = 0 |- _ ] |
| 376 |
=> 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')) |
| 377 |
| [ H : ?x * ?y <> ?z, H' : ?y = 0 |- _ ] |
| 378 |
=> 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')) |
| 379 |
| [ H : Fopp (?x * ?y) <> ?z, H' : ?x = 0 |- _ ] |
| 380 |
=> 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')) |
| 381 |
| [ H : Fopp (?x * ?y) <> ?z, H' : ?y = 0 |- _ ] |
| 382 |
=> 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')) |
| 383 |
| [ H : ?x * ?y^3 = ?z, H' : ?y = 0 |- _ ] |
| 384 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a * b^3 = c -> b = 0 -> c = 0) |
| 385 |
| [ H : ?x * ?y = ?z, H' : ?x = 0 |- _ ] |
| 386 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a * b = c -> a = 0 -> c = 0) |
| 387 |
| [ H : ?x * ?y - ?z <> 0, H' : ?x = 0 |- _ ] |
| 388 |
=> 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')) |
| 389 |
| [ H : ?x * ?y - ?z <> 0, H' : ?y = 0 |- _ ] |
| 390 |
=> 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')) |
| 391 |
| [ H : ?x * ?y - ?z = 0, H' : ?x = 0 |- _ ] |
| 392 |
=> 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')) |
| 393 |
| [ H : ?x * ?y - ?z = 0, H' : ?y = 0 |- _ ] |
| 394 |
=> 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')) |
| 395 |
| [ H : ?x - ?y * ?z = 0, H' : ?z = 0 |- _ ] |
| 396 |
=> 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')) |
| 397 |
| [ H : ?x * (?y * ?y^2) - ?z <> ?w |- _ ] |
| 398 |
=> F.apply_lemma_in_on pkg H (forall a b c d, a * (b * b^2) - c <> d -> a * (b^3) - c <> d) |
| 399 |
| [ H : ?x * (?y * ?y^2) - ?z = ?w |- _ ] |
| 400 |
=> F.apply_lemma_in_on pkg H (forall a b c d, a * (b * b^2) - c = d -> a * (b^3) - c = d) |
| 401 |
| [ H : ?x - (?y * ?y^2) * ?z <> ?w |- _ ] |
| 402 |
=> F.apply_lemma_in_on pkg H (forall a b c d, a - (b * b^2) * c <> d -> a - (b^3) * c <> d) |
| 403 |
| [ H : ?x - (?y * ?y^2) * ?z = ?w |- _ ] |
| 404 |
=> F.apply_lemma_in_on pkg H (forall a b c d, a - (b * b^2) * c = d -> a - (b^3) * c = d) |
| 405 |
| [ H : ?x * (?y * ?y^2) = 0, H' : ?y <> 0 |- _ ] |
| 406 |
=> F.apply2_lemma_in_on pkg H H' (forall a b, a * (b * b^2) = 0 -> b <> 0 -> a = 0) |
| 407 |
| [ H : ?x * (?y * ?z) = 0, H' : ?z <> 0 |- _ ] |
| 408 |
=> 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')) |
| 409 |
| [ H : ?x = ?y * ?z, H' : ?y = 0 |- _ ] |
| 410 |
=> 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')) |
| 411 |
| [ H : ?x * ?y + ?z = 0, H' : ?x = 0 |- _ ] |
| 412 |
=> 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')) |
| 413 |
| [ H : ?x * (?y * ?y^2) - ?z * ?z^2 * ?w = 0, H' : ?x * ?y^3 + ?w * ?z^3 = 0 |- _ ] |
| 414 |
=> 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')) |
| 415 |
| [ H : ?x * Finv (?y^2) <> ?z * Finv (?w^2), Hy : ?y <> 0, Hw : ?w <> 0 |- _ ] |
| 416 |
=> 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)) |
| 417 |
| [ H : ?x * Finv (?y^2) = ?z * Finv (?w^2), Hy : ?y <> 0, Hw : ?w <> 0 |- _ ] |
| 418 |
=> 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)) |
| 419 |
| [ H : ?x * Finv (?y^3) = Fopp (?z * Finv (?w^3)), Hy : ?y <> 0, Hw : ?w <> 0 |- _ ] |
| 420 |
=> 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)) |
| 421 |
| [ H : ?x * Finv (?y^3) <> Fopp (?z * Finv (?w^3)), Hy : ?y <> 0, Hw : ?w <> 0 |- _ ] |
| 422 |
=> 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)) |
| 423 |
| [ H : ?x = Fopp (?y * ?z), H' : ?x - ?z * ?y = 0 |- _ ] |
| 424 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a = Fopp (b * c) -> a - c * b = 0 -> a = 0) |
| 425 |
| [ H : ?x = Fopp (?y * ?z), H' : ?x - ?z * ?y = 0 |- _ ] |
| 426 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a = Fopp (b * c) -> a - c * b = 0 -> a = 0) |
| 427 |
| [ H : ?x <> Fopp (?y * ?z), H' : ?x - ?z * ?y = 0 |- _ ] |
| 428 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a <> Fopp (b * c) -> a - c * b = 0 -> a * b * c <> 0) |
| 429 |
| [ H : ?x <> Fopp (?y * ?z), H' : ?x - ?z * ?y = 0 |- _ ] |
| 430 |
=> F.apply2_lemma_in_on pkg H H' (forall a b c, a <> Fopp (b * c) -> a - c * b = 0 -> a * b * c <> 0) |
| 431 |
| [ H : ?y^2 = ?c^3 + ?a * ?c * (?x^2)^2 + ?b * (?x^3)^2, |
| 432 |
H' : ?y'^2 = ?c'^3 + ?a * ?c' * (?x'^2)^2 + ?b * (?x'^3)^2, |
| 433 |
H0 : ?c' * ?x^2 - ?c * ?x'^2 = 0 |
| 434 |
|- _ ] |
| 435 |
=> 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)) |
| 436 |
| [ H : ?y^2 = ?c^3 + ?a * ?c * (?x^2)^2 + ?b * (?x^3)^2, |
| 437 |
H' : ?y'^2 = ?c'^3 + ?a * ?c' * (?x'^2)^2 + ?b * (?x'^3)^2, |
| 438 |
H0 : ?c' * ?x^2 = ?c * ?x'^2 |
| 439 |
|- _ ] |
| 440 |
=> 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)) |
| 441 |
|
| 442 |
| [ H0 : ?x * ?y^3 = ?A * ?B^3, |
| 443 |
H1 : ?x * ?z^3 - ?B^3 * ?D = 0, |
| 444 |
H2 : ?D * ?C^3 = ?w * ?z^3, |
| 445 |
H3 : ?A * ?C^3 - ?y^3 * ?w <> 0, |
| 446 |
Hz : ?z <> 0, |
| 447 |
HB : ?B <> 0 |
| 448 |
|- _ ] |
| 449 |
=> exfalso; revert H0 H1 H2 H3 Hz HB; |
| 450 |
generalize x, y, z, w, A, B, C, D; |
| 451 |
F.goal_exact_lemma_on pkg |
| 452 |
| [ H0 : ?x * ?y^3 = ?A * ?B^3, |
| 453 |
H1 : ?x * ?z^3 - ?B^3 * ?D <> 0, |
| 454 |
H2 : ?D * ?C^3 = ?w * ?z^3, |
| 455 |
H3 : ?A * ?C^3 - ?y^3 * ?w = 0, |
| 456 |
Hy : ?y <> 0, |
| 457 |
HC : ?C <> 0 |
| 458 |
|- _ ] |
| 459 |
=> exfalso; revert H0 H1 H2 H3 Hy HC; |
| 460 |
generalize x, y, z, w, A, B, C, D; |
| 461 |
F.goal_exact_lemma_on pkg |
| 462 |
| [ H0 : ?x * ?y^2 = ?A * ?B^2, |
| 463 |
H1 : ?x * ?z^2 - ?D * ?B^2 = 0, |
| 464 |
H2 : ?D * ?C^2 = ?w * ?z^2, |
| 465 |
H3 : ?A * ?C^2 - ?w * ?y^2 <> 0, |
| 466 |
Hz : ?z <> 0, |
| 467 |
HB : ?B <> 0 |
| 468 |
|- _ ] |
| 469 |
=> exfalso; revert H0 H1 H2 H3 Hz HB; |
| 470 |
generalize x, y, z, w, A, B, C, D; |
| 471 |
F.goal_exact_lemma_on pkg |
| 472 |
| [ H0 : ?A * ?B^2 = ?x * ?y^2, |
| 473 |
H1 : ?x * ?z^2 - ?D * ?B^2 = 0, |
| 474 |
H2 : ?w * ?z^2 = ?D * ?C^2, |
| 475 |
H3 : ?A * ?C^2 - ?w * ?y^2 <> 0, |
| 476 |
Hz : ?z <> 0, |
| 477 |
HB : ?B <> 0 |
| 478 |
|- _ ] |
| 479 |
=> exfalso; revert H0 H1 H2 H3 Hz HB; |
| 480 |
generalize x, y, z, w, A, B, C, D; |
| 481 |
F.goal_exact_lemma_on pkg |
| 482 |
| [ H : ?x <> 0 |- context[Finv (?x^2)] ] |
| 483 |
=> F.with_lemma_on pkg (forall a, a <> 0 -> Finv (a^2) = (Finv a)^2) ltac:(fun H' => rewrite !(H' x H)) |
| 484 |
| [ H : ?x <> 0 |- context[Finv (?x^3)] ] |
| 485 |
=> F.with_lemma_on pkg (forall a, a <> 0 -> Finv (a^3) = (Finv a)^3) ltac:(fun H' => rewrite !(H' x H)) |
| 486 |
| [ H : ?x <> 0 |- context[Finv (Finv ?x)] ] |
| 487 |
=> F.with_lemma_on pkg (forall a, a <> 0 -> Finv (Finv a) = a) ltac:(fun H' => rewrite !(H' x H)) |
| 488 |
| [ |- context[Finv ((?x + ?y)^2 - ?x^2 - ?y^2)] ] |
| 489 |
=> 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)) |
| 490 |
| [ |- context[Finv ((?x + ?y)^2 - (?x^2 + ?y^2))] ] |
| 491 |
=> 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)) |
| 492 |
| [ |- context[Finv (?x * ?y)] ] |
| 493 |
=> F.with_lemma_on |
| 494 |
pkg |
| 495 |
(forall a b, a * b <> 0 -> Finv (a * b) = Finv a * Finv b) |
| 496 |
ltac:(fun H' => rewrite !(H' x y) by (clear_eq; fsatz)) |
| 497 |
| _ => idtac |
| 498 |
end. |
| 499 |
Local Ltac speed_up_fsatz := |
| 500 |
let fld := guess_field in |
| 501 |
let pkg := F.get_package_on fld in |
| 502 |
divisions_to_inverses fld; |
| 503 |
repeat speed_up_fsatz_step_on pkg. |
| 504 |
(* sanity check to get error messages that would otherwise be gobbled by [repeat] *) |
| 505 |
Local Ltac speed_up_fsatz_check := |
| 506 |
let fld := guess_field in |
| 507 |
let pkg := F.get_package_on fld in |
| 508 |
speed_up_fsatz_step_on pkg. |
| 509 |
|
| 510 |
(* Everything this can solve, [t] can also solve, but this does some extra work to go faster *) |
| 511 |
Local Ltac pre_pre_faster_t := |
| 512 |
prept; try contradiction; speed_up_fsatz. |
| 513 |
Local Ltac pre_faster_t := |
| 514 |
pre_pre_faster_t; speed_up_fsatz_check; clean_up_speed_up_fsatz. |
| 515 |
Local Ltac faster_t_noclear := |
| 516 |
pre_faster_t; subst_lets; fsatz. |
| 517 |
Local Ltac faster_t := |
| 518 |
pre_faster_t; |
| 519 |
try solve [ lazymatch goal with |
| 520 |
| [ |- _ = _ ] => clear_neq |
| 521 |
| _ => idtac |
| 522 |
end; |
| 523 |
fsatz ]. |
| 524 |
|
| 525 |
Hint Unfold double double_impl to_affine : points_as_coordinates. |
| 526 |
|
| 527 |
Lemma to_affine_double P : W.eq (to_affine (double P)) (W.add (to_affine P) (to_affine P)). |
| 528 |
Proof. Time faster_t_noclear. Qed. |
| 529 |
|
| 530 |
Lemma Proper_double : Proper (eq ==> eq) double. |
| 531 |
Proof. intros ???H. apply eq_iff. rewrite 2 to_affine_double, H; reflexivity. Qed. |
| 532 |
|
| 533 |
|
| 534 |
Definition add_impl (P Q : F*F*F) (affine_Q : bool) : F*F*F := |
| 535 |
let z1 := snd P in let z2 := snd Q in let y1 := snd (fst P) in let y2 := snd (fst Q) in let x1 := fst (fst P) in let x2 := fst (fst Q) in |
| 536 |
let u1 := x1*z2^2 in |
| 537 |
let u2 := x2*z1^2 in |
| 538 |
let s1 := y1*z2^3 in |
| 539 |
let s2 := y2*z1^3 in |
| 540 |
let z := z1*z2 in |
| 541 |
let t := u1+u2 in |
| 542 |
let m := if dec (s1+s2 = 0) then u2-u1 else s1+s2 in |
| 543 |
let q := Fopp t*m^2 in |
| 544 |
let r := if dec (s1+s2 = 0) then s2-s1 else t^2-u1*u2 + a*(z^2)^2 in |
| 545 |
let x3 := r^2+q in |
| 546 |
let y3 := Fopp (r*((1+1)*x3+q)+m*(s1+s2)^3)/(1+1) in |
| 547 |
let z3 := m * z in |
| 548 |
if dec (s1+s2 = 0) |
| 549 |
then if dec (u1 = u2) then (0, 0, 0) else (x3, y3, z3) |
| 550 |
else (x3, y3, z3). |
| 551 |
Hint Unfold add_impl : points_as_coordinates. |
| 552 |
|
| 553 |
Definition add_nz_nz (P Q : point) : point. refine ( |
| 554 |
exist _ (add_impl (proj1_sig P) (proj1_sig Q) false) _). |
| 555 |
Proof. abstract faster_t_noclear. Defined. |
| 556 |
Hint Unfold add_nz_nz : points_as_coordinates. |
| 557 |
|
| 558 |
Hint Unfold W.add' : points_as_coordinates. |
| 559 |
|
| 560 |
Lemma to_affine_add_nz_nz (P Q : point) (HP : ~ iszero P) (HQ : ~ iszero Q) : |
| 561 |
W.eq (to_affine (add_nz_nz P Q)) (W.add (to_affine P) (to_affine Q)). |
| 562 |
Proof. |
| 563 |
rewrite <-W.add'_correct. |
| 564 |
case P as [((x1&y1)&z1)?], Q as [((x2&y2)&z2)?]. |
| 565 |
cbv [iszero] in *; prept. |
| 566 |
par: faster_t_noclear. |
| 567 |
Qed. |
| 568 |
|
| 569 |
Definition add (P Q : point) : point := |
| 570 |
if dec (snd (proj1_sig P) = 0) then Q |
| 571 |
else if dec (snd (proj1_sig Q) = 0) then P |
| 572 |
else add_nz_nz P Q. |
| 573 |
Hint Unfold add : points_as_coordinates. |
| 574 |
|
| 575 |
Lemma to_affine_add (P Q : point) : |
| 576 |
W.eq (to_affine (add P Q)) (W.add (to_affine P) (to_affine Q)). |
| 577 |
Proof. |
| 578 |
cbv [add]. |
| 579 |
case dec as [HP|HP]; try (setoid_rewrite iszero_iff in HP; rewrite HP; faster_t). |
| 580 |
case dec as [HQ|HQ]; try (setoid_rewrite iszero_iff in HQ; rewrite HQ; faster_t). |
| 581 |
rewrite to_affine_add_nz_nz by trivial; reflexivity. |
| 582 |
Qed. |
| 583 |
|
| 584 |
Global Instance Proper_add : Proper (eq ==> eq ==> eq) add. |
| 585 |
Proof. repeat intros ???H. apply eq_iff. rewrite ? to_affine_add; f_equiv; f_equiv; trivial. Qed. |
| 586 |
|
| 587 |
Definition add_inequal_impl (P Q : F*F*F) (affine_Q : bool) : (F*F*F)*bool := |
| 588 |
let z1 := snd P in let z2 := snd Q in let y1 := snd (fst P) in let y2 := snd (fst Q) in let x1 := fst (fst P) in let x2 := fst (fst Q) in |
| 589 |
let z1z1 := z1^2 in |
| 590 |
let u2 := x2*z1z1 in |
| 591 |
let z2z2 := if affine_Q then Fone else z2^2 in |
| 592 |
let u1 := if affine_Q then x1 else x1 * z2z2 in |
| 593 |
let h := u2 - u1 in |
| 594 |
let s2 := z1*z1z1 in |
| 595 |
let out_z := h * z1 in |
| 596 |
let out_z := if affine_Q then out_z else out_z * z2 in |
| 597 |
let s2 := s2*y2 in |
| 598 |
let s1 := if affine_Q then y1 else z2*z2z2*y1 in |
| 599 |
let r := s2 - s1 in |
| 600 |
let doubling := if dec (h = 0) then if dec(r = 0) then true else false else false in |
| 601 |
let Hsqr := h^2 in |
| 602 |
let out_x := r^2 in |
| 603 |
let Hcub := Hsqr*h in |
| 604 |
let u2 := u1 * Hsqr in |
| 605 |
let out_x := out_x - Hcub in |
| 606 |
let out_x := out_x - u2 in |
| 607 |
let out_x := out_x - u2 in |
| 608 |
let h := u2 - out_x in |
| 609 |
let s2 := Hcub * s1 in |
| 610 |
let h := h * r in |
| 611 |
let out_y := h - s2 in |
| 612 |
((out_x, out_y, out_z), doubling). |
| 613 |
|
| 614 |
Hint Unfold add_inequal_impl : points_as_coordinates. |
| 615 |
|
| 616 |
Definition add_inequal_nz_nz (P Q : point) (_ : ~ eq P Q) : point. refine (exist _ ( |
| 617 |
fst (add_inequal_impl (proj1_sig P) (proj1_sig Q) false)) _). |
| 618 |
Proof. abstract faster_t_noclear. Defined. |
| 619 |
|
| 620 |
Definition add_affine_inequal_nz_nz (P : point) (Q : Wpoint) (_ : ~ eq P (of_affine Q)) : point. refine (exist _ ( |
| 621 |
fst (add_inequal_impl (proj1_sig P) (proj1_sig (of_affine Q)) false)) _). |
| 622 |
Proof. abstract faster_t_noclear. Defined. |
| 623 |
|
| 624 |
Hint Unfold add_inequal_impl add_inequal_nz_nz add_affine_inequal_nz_nz : points_as_coordinates. |
| 625 |
|
| 626 |
Lemma snd_add_inequal_impl (P Q : point) (HP : ~ iszero P) (HQ : ~ iszero Q) : |
| 627 |
snd (add_inequal_impl (proj1_sig P) (proj1_sig Q) false) = true :> bool <-> eq P Q. |
| 628 |
Proof. |
| 629 |
case P as [((x1&y1)&z1)?], Q as [((x2&y2)&z2)?]. |
| 630 |
split; [|clear HP; clear HQ; t]; cbv [iszero] in *. |
| 631 |
prept; try congruence; try t. |
| 632 |
Qed. |
| 633 |
|
| 634 |
Lemma to_affine_add_inequal_nz_nz P Q (H : ~ eq P Q)(HP : ~ iszero P) (HQ : ~ iszero Q) : |
| 635 |
W.eq (to_affine (add_inequal_nz_nz P Q H)) (W.add (to_affine P) (to_affine Q)). |
| 636 |
Proof. |
| 637 |
case P as [((x1&y1)&z1)?], Q as [((x2&y2)&z2)?]. |
| 638 |
cbv [iszero] in *; prept; try apply H; prept. |
| 639 |
par : faster_t_noclear. |
| 640 |
Qed. |
| 641 |
|
| 642 |
Definition add_inequal (P Q : point) (H : ~ eq P Q) : point := |
| 643 |
if dec (snd (proj1_sig P) = 0) then Q |
| 644 |
else if dec (snd (proj1_sig Q) = 0) then P |
| 645 |
else add_inequal_nz_nz P Q H. |
| 646 |
Hint Unfold add_inequal : points_as_coordinates. |
| 647 |
|
| 648 |
Lemma to_affine_add_inequal (P Q : point) H : |
| 649 |
W.eq (to_affine (add_inequal P Q H)) (W.add (to_affine P) (to_affine Q)). |
| 650 |
Proof. |
| 651 |
cbv [add_inequal]. |
| 652 |
case dec as [HP|HP]; try (setoid_rewrite iszero_iff in HP; rewrite HP; t). |
| 653 |
case dec as [HQ|HQ]; try (setoid_rewrite iszero_iff in HQ; rewrite HQ; t). |
| 654 |
rewrite to_affine_add_inequal_nz_nz by trivial; reflexivity. |
| 655 |
Qed. |
| 656 |
|
| 657 |
Lemma add_affine_inequal_nz_nz_correct P Q H H': |
| 658 |
eq (add_affine_inequal_nz_nz P Q H) (add_inequal_nz_nz P (of_affine Q) H'). |
| 659 |
Proof. faster_t. Qed. |
| 660 |
|
| 661 |
Lemma to_affine_add_affine_inequal_nz_nz P Q H (_ : ~ iszero P) (_ : ~ W.eq Q W.zero) : |
| 662 |
W.eq (to_affine (add_affine_inequal_nz_nz P Q H)) (W.add (to_affine P) Q). |
| 663 |
Proof. |
| 664 |
unshelve rewrite @add_affine_inequal_nz_nz_correct, @to_affine_add_inequal_nz_nz, @to_affine_of_affine; |
| 665 |
trivial; try reflexivity. |
| 666 |
rewrite iszero_iff, to_affine_of_affine; trivial. |
| 667 |
Qed. |
| 668 |
|
| 669 |
Definition add_affine_inequal (P : point) (Q : Wpoint) (H : ~ eq P _) : point := |
| 670 |
if dec (snd (proj1_sig P) = 0) then of_affine Q |
| 671 |
else match W.coordinates Q with inr _ => P | _ => |
| 672 |
add_affine_inequal_nz_nz P Q H end. |
| 673 |
Hint Unfold add_affine_inequal : points_as_coordinates. |
| 674 |
|
| 675 |
Lemma to_affine_add_affine_inequal P Q H : |
| 676 |
W.eq (to_affine (add_affine_inequal P Q H)) (W.add (to_affine P) Q). |
| 677 |
Proof. |
| 678 |
cbv [add_affine_inequal]. |
| 679 |
case dec as [HP|HP]; try (setoid_rewrite iszero_iff in HP; rewrite HP; t). |
| 680 |
case W.coordinates as [|[] ] eqn:E. |
| 681 |
{ rewrite to_affine_add_affine_inequal_nz_nz; trivial; try reflexivity. |
| 682 |
cbv [W.eq]; rewrite E; cbv [W.coordinates W.zero]; case p; intuition idtac. } |
| 683 |
eassert (W.eq Q W.zero) as ->. { cbv [W.eq W.zero]. rewrite E. exact I. } |
| 684 |
faster_t. |
| 685 |
Qed. |
| 686 |
|
| 687 |
(** If [a] is -3, one can substitute a faster implementation of [double]. *) |
| 688 |
Section AEqMinus3. |
| 689 |
Context (a_eq_minus3 : a = Fopp (1+1+1)). |
| 690 |
Definition Fsquare x := x^2. |
| 691 |
Definition Ftriple x := x+x+x. |
| 692 |
Definition Fhalve x := x/(1+1). |
| 693 |
|
| 694 |
(* Based on "Guide to Elliptic Curve Cryptography" HMV'04 page 90 *) |
| 695 |
Definition double_minus3_impl P := |
| 696 |
let x := fst (fst P) in let y := snd (fst P) in let z := snd P in |
| 697 |
let D := Fadd y y in |
| 698 |
let tmp := Fsquare z in |
| 699 |
let D := Fsquare D in |
| 700 |
let out_z := Fmul z y in |
| 701 |
let out_z := Fadd out_z out_z in |
| 702 |
let A := Fadd x tmp in |
| 703 |
let tmp := Fsub x tmp in |
| 704 |
let tmp := Ftriple tmp in |
| 705 |
let out_y := Fsquare D in |
| 706 |
let A := Fmul A tmp in |
| 707 |
let D := Fmul D x in |
| 708 |
let out_x := Fsquare A in |
| 709 |
let tmp := Fadd D D in |
| 710 |
let out_x := Fsub out_x tmp in |
| 711 |
let D := Fsub D out_x in |
| 712 |
let D := Fmul D A in |
| 713 |
let out_y := Fhalve out_y in |
| 714 |
let out_y := Fsub D out_y in |
| 715 |
(out_x, out_y, out_z). |
| 716 |
|
| 717 |
Hint Unfold Fsquare Ftriple Fhalve double_minus3_impl : points_as_coordinates. |
| 718 |
|
| 719 |
Definition double_minus_3 (P : point) : point. refine (exist _ |
| 720 |
(double_minus3_impl (proj1_sig P)) |
| 721 |
_). |
| 722 |
Proof. abstract faster_t. Defined. |
| 723 |
|
| 724 |
Hint Unfold double_minus_3 : points_as_coordinates. |
| 725 |
|
| 726 |
Lemma double_minus_3_eq_double (P : point) : |
| 727 |
eq (double P) (double_minus_3 P). |
| 728 |
Proof. faster_t. Qed. |
| 729 |
|
| 730 |
Definition double_minus3_without_halving_impl P := |
| 731 |
let x := fst (fst P) in let y := snd (fst P) in let z := snd P in |
| 732 |
let delta := z^2 in |
| 733 |
let gamma := y^2 in |
| 734 |
let beta := x * gamma in |
| 735 |
let ftmp := x - delta in |
| 736 |
let ftmp2 := x + delta in |
| 737 |
let tmptmp := ftmp2 + ftmp2 in |
| 738 |
let ftmp2 := ftmp2 + tmptmp in |
| 739 |
let alpha := ftmp * ftmp2 in |
| 740 |
let x_out := alpha^2 in |
| 741 |
let fourbeta := beta + beta in |
| 742 |
let fourbeta := fourbeta + fourbeta in |
| 743 |
let tmptmp := fourbeta + fourbeta in |
| 744 |
let x_out := x_out - tmptmp in |
| 745 |
let delta := gamma + delta in |
| 746 |
let ftmp := y + z in |
| 747 |
let z_out := ftmp^2 in |
| 748 |
let z_out := z_out - delta in |
| 749 |
let y_out := fourbeta - x_out in |
| 750 |
let gamma := gamma + gamma in |
| 751 |
let gamma := gamma^2 in |
| 752 |
let y_out := alpha * y_out in |
| 753 |
let gamma := gamma + gamma in |
| 754 |
let y_out := y_out - gamma in |
| 755 |
(x_out, y_out, z_out). |
| 756 |
|
| 757 |
Hint Unfold double_minus3_without_halving_impl : points_as_coordinates. |
| 758 |
|
| 759 |
Definition double_minus3_without_halving (P : point) : point. refine (exist _ |
| 760 |
(double_minus3_without_halving_impl (proj1_sig P)) |
| 761 |
_). |
| 762 |
Proof. abstract faster_t. Defined. |
| 763 |
|
| 764 |
Hint Unfold double_minus3_without_halving : points_as_coordinates. |
| 765 |
|
| 766 |
Lemma double_minus3_without_halving_eq_double (P : point) : |
| 767 |
eq (double_minus3_without_halving P) (double_minus_3 P). |
| 768 |
Proof. abstract faster_t. Qed. |
| 769 |
End AEqMinus3. |
| 770 |
(** If [a] is 0, one can substitute a faster implementation of [double]. *) |
| 771 |
Section AEqZero. |
| 772 |
Context (a_eq_zero : a = Fzero). |
| 773 |
|
| 774 |
(** See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l *) |
| 775 |
Program Definition double_aeq0 (P : point) : point := |
| 776 |
match proj1_sig P return F*F*F with |
| 777 |
| (x_in, y_in, z_in) => |
| 778 |
let A := x_in^2 in |
| 779 |
let B := y_in^2 in |
| 780 |
let C := B^2 in |
| 781 |
let t0 := x_in + B in |
| 782 |
let t1 := t0^2 in |
| 783 |
let t2 := t1 - A in |
| 784 |
let t3 := t2 - C in |
| 785 |
let D := t3 + t3 in |
| 786 |
let E := A + A + A in |
| 787 |
let F := E^2 in |
| 788 |
let t4 := D + D in |
| 789 |
let x_out := F - t4 in |
| 790 |
let t5 := D - x_out in |
| 791 |
let four_C := C + C in |
| 792 |
let four_C := four_C + four_C in |
| 793 |
let t6 := four_C + four_C in |
| 794 |
let t7 := E * t5 in |
| 795 |
let y_out := t7 - t6 in |
| 796 |
let t8 := y_in * z_in in |
| 797 |
let z_out := t8 + t8 in |
| 798 |
(x_out, y_out, z_out) |
| 799 |
end. |
| 800 |
Next Obligation. Proof. t. Qed. |
| 801 |
|
| 802 |
Hint Unfold double_aeq0 : points_as_coordinates. |
| 803 |
|
| 804 |
Lemma double_aeq0_eq_double (P : point) : |
| 805 |
eq (double P) (double_aeq0 P). |
| 806 |
Proof. faster_t. Qed. |
| 807 |
End AEqZero. |
| 808 |
End Jacobian. |
| 809 |
End Jacobian. |