-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Curves/Weierstrass #1953
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,72 @@ | ||
Require Import Crypto.Spec.WeierstrassCurve. | ||
Require Import Crypto.Algebra.Field. | ||
Require Import Crypto.Util.Decidable Crypto.Util.Tactics.DestructHead Crypto.Util.Tactics.BreakMatch. | ||
Require Import Crypto.Util.Tactics.SetoidSubst. | ||
Import RelationClasses Morphisms. | ||
|
||
Module W. | ||
Section W. | ||
Context {F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} {a b:F} | ||
{field:@Algebra.Hierarchy.field F Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv} | ||
{Feq_dec:DecidableRel Feq}. | ||
Local Infix "+" := Fadd. Local Infix "-" := Fsub. | ||
Local Infix "*" := Fmul. Local Infix "/" := Fdiv. | ||
Local Notation "x ^ 2" := (x*x) (at level 30). | ||
Local Notation point := (@W.point F Feq Fadd Fmul a b). | ||
|
||
Program Definition opp (P:@W.point F Feq Fadd Fmul a b) : @W.point F Feq Fadd Fmul a b | ||
:= match W.coordinates P return F*F+_ with | ||
| inl (x1, y1) => inl (x1, Fopp y1) | ||
| inr tt => inr tt | ||
end. | ||
Next Obligation. | ||
cbv [W.coordinates]; break_match; trivial; fsatz. | ||
Definition opp (P : point) : point. refine (exist _ ( | ||
match W.coordinates P with | ||
| inl (x1, y1) => inl (x1, Fopp y1) | ||
| inr tt => inr tt | ||
end) _). | ||
Proof. abstract (cbv [W.coordinates]; break_match; trivial; fsatz). Defined. | ||
|
||
Global Instance Equivalence_eq : Equivalence (@W.eq _ Feq Fadd Fmul a b). | ||
Proof. | ||
cbv [W.eq W.coordinates]; split; repeat intros [ [ []|[] ] ?]; intuition try solve | ||
[contradiction | apply reflexivity | apply symmetry; trivial | eapply transitivity; eauto 1]. | ||
Qed. | ||
|
||
Global Instance Proper_opp : Proper (W.eq ==> W.eq) opp. | ||
Proof. | ||
repeat (intros [ [[]|[] ]?] || intro); cbv [W.coordinates opp W.eq] in *; | ||
repeat (try destruct_head' @and; try case dec as []; try contradiction; try split); trivial. | ||
setoid_subst_rel Feq; reflexivity. | ||
Qed. | ||
|
||
(* Weierstraß Elliptic Curves and Side-Channel Attacks | ||
by Eric Brier and Marc Joye, 2002 *) | ||
Definition add' (P1 P2 : point) : point. refine (exist _ | ||
match W.coordinates P1, W.coordinates P2 with | ||
| inl (x1, y1), inl (x2, y2) => | ||
if dec (Feq y1 (Fopp y2)) then | ||
if dec (Feq x1 x2) then inr tt | ||
else let k := (y2-y1)/(x2-x1) in | ||
let x3 := k^2-x1-x2 in | ||
let y3 := k*(x1-x3)-y1 in | ||
inl (x3, y3) | ||
else let k := ((x1^2 + x1*x2 + x2^2 + a)/(y1+y2)) in | ||
let x3 := k^2-x1-x2 in | ||
let y3 := k*(x1-x3)-y1 in | ||
inl (x3, y3) | ||
| inr tt, inr tt => inr tt | ||
| inr tt, _ => W.coordinates P2 | ||
| _, inr tt => W.coordinates P1 | ||
end _). | ||
Proof. abstract (cbv [W.coordinates]; break_match; trivial; fsatz). Defined. | ||
|
||
Lemma add'_correct char_ge_3 : forall P Q : point, W.eq (W.add' P Q) (W.add(char_ge_3:=char_ge_3) P Q). | ||
Proof. intros [ [[]|]?] [ [[]|]?]; cbv [W.coordinates W.add W.add' W.eq]; break_match; try split; try fsatz. Qed. | ||
|
||
Global Instance Proper_add' : Proper (W.eq ==> W.eq ==> W.eq) add'. | ||
Proof. | ||
repeat (intros [ [[]|[] ]?] || intro); cbv [W.coordinates W.add' W.eq] in *; | ||
repeat (try destruct_head' @and; try case dec as []; try contradiction; try split); trivial. | ||
Time par : fsatz. (* setoid_subst_rel is slower *) | ||
Qed. | ||
|
||
Global Instance Proper_add {char_ge_3} : | ||
Proper (W.eq ==> W.eq ==> W.eq) (@W.add _ Feq Fzero Fone Fopp Fadd Fsub Fmul Finv Fdiv _ _ char_ge_3 a b). | ||
Proof. repeat intro. rewrite <-2add'_correct. apply Proper_add'; trivial. Qed. | ||
End W. | ||
End W. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JasonGross this is because dependent occurrences and destructable occurranes of the same expression. Do you know a more systematic fix from the top of your head?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be "at 1 in H" or similar?
A more systematic fix:
H: context C[@dec ?P ?pf] |- _ => destruct (@dec P pf) || (let p := fresh in pose (@dec P pf) as p; let C' := context C[p] in change C' in (type of H); destruct p)
Maybe too painful though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the idea that
change
works fine for changing some but not all occurrences?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
change
+context
will select only one occurrence at a time. This won't work if you need to simultaneously destruct in two or more occurrences though