Skip to content

Commit

Permalink
decide point equality (#1848)
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-erbsen authored Apr 4, 2024
1 parent eb5ba09 commit 841cc2f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/Curves/Edwards/AffineProofs.v
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Module E.
Program Definition opp (P:point) : point := (Fopp (fst P), (snd P)).
Next Obligation. match goal with P : point |- _ => destruct P as [ [??]?] end; cbv; fsatz. Qed.

Global Instance Decidable_eq : Decidable.DecidableRel (@E.eq _ Feq Fone Fadd Fmul a d) := _.

Ltac t_step :=
match goal with
| _ => solve [trivial | exact _ ]
Expand Down
34 changes: 21 additions & 13 deletions src/Curves/EdwardsMontgomery25519.v
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Require Import Coq.ZArith.ZArith. Local Open Scope Z_scope.
Require Import Crypto.Util.Decidable.
Require Import Crypto.Spec.ModularArithmetic. Local Open Scope F_scope.
Require Import Crypto.Curves.EdwardsMontgomery. Import M.
Require Import Crypto.Curves.Edwards.TwistIsomorphism.
Expand All @@ -11,19 +12,26 @@ Import MontgomeryCurve CompleteEdwardsCurve.

Local Definition a' := (M.a + (1 + 1)) / M.b.
Local Definition d' := (M.a - (1 + 1)) / M.b.
Definition r := sqrt (F.inv ((a' / M.b) / E.a)).
Local Definition r := sqrt (F.inv ((a' / M.b) / E.a)).

Local Lemma is_twist : E.a * d' = a' * E.d. Proof. Decidable.vm_decide. Qed.
Local Lemma nonzero_a' : a' <> 0. Proof. Decidable.vm_decide. Qed.
Local Lemma r_correct : E.a = r * r * a'. Proof. Decidable.vm_decide. Qed.
Local Lemma is_twist : E.a * d' = a' * E.d. Proof. vm_decide. Qed.
Local Lemma nonzero_a' : a' <> 0. Proof. vm_decide. Qed.
Local Lemma r_correct : E.a = r * r * a'. Proof. vm_decide. Qed.

Definition Montgomery_of_Edwards (P : Curve25519.E.point) : Curve25519.M.point :=
@of_Edwards _ _ _ _ _ _ _ _ _ _ field _ char_ge_3 M.a M.b M.b_nonzero a' d' eq_refl eq_refl nonzero_a'
(@E.point2_of_point1 _ _ _ _ _ _ _ _ _ _ field _ E.a E.d a' d' is_twist E.nonzero_a nonzero_a' r r_correct P).

Definition Edwards_of_Montgomery (P : Curve25519.M.point) : Curve25519.E.point :=
Module E.
Definition of_Montgomery (P : Curve25519.M.point) : Curve25519.E.point :=
@E.point1_of_point2 _ _ _ _ _ _ _ _ _ _ field _ E.a E.d a' d' is_twist E.nonzero_a nonzero_a' r r_correct
(@to_Edwards _ _ _ _ _ _ _ _ _ _ field _ M.a M.b M.b_nonzero a' d' eq_refl eq_refl nonzero_a' P).
Lemma of_Montgomery_B : E.eq E.B (of_Montgomery M.B). Proof. vm_decide. Qed.
End E.

Module M.
Definition of_Edwards (P : Curve25519.E.point) : Curve25519.M.point :=
@of_Edwards _ _ _ _ _ _ _ _ _ _ field _ char_ge_3 M.a M.b M.b_nonzero a' d' eq_refl eq_refl nonzero_a'
(@E.point2_of_point1 _ _ _ _ _ _ _ _ _ _ field _ E.a E.d a' d' is_twist E.nonzero_a nonzero_a' r r_correct P).
Lemma of_Edwards_B : M.eq M.B (of_Edwards E.B). Proof.
Proof. simple notypeclasses refine (@dec_bool _ _ _). apply Affine.M.Decidable_eq. vm_decide. Qed.
End M.

Local Notation Eopp := ((@AffineProofs.E.opp _ _ _ _ _ _ _ _ _ _ field _ E.a E.d E.nonzero_a)).

Expand All @@ -36,16 +44,16 @@ Local Arguments of_Edwards {_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _} _ _ { _ _ _ }.
Lemma EdwardsMontgomery25519 : @Group.isomorphic_commutative_groups
Curve25519.E.point E.eq Curve25519.E.add Curve25519.E.zero Eopp Curve25519.M.point
M.eq Curve25519.M.add M.zero Curve25519.M.opp
Montgomery_of_Edwards Edwards_of_Montgomery.
M.of_Edwards E.of_Montgomery.
Proof.
cbv [Montgomery_of_Edwards Edwards_of_Montgomery].
cbv [M.of_Edwards E.of_Montgomery].
epose proof E.twist_isomorphism(a1:=E.a)(a2:=a')(d1:=E.d)(d2:=d')(r:=r) as AB.
epose proof EdwardsMontgomeryIsomorphism(a:=Curve25519.M.a)(b:=Curve25519.M.b)as BC.
destruct AB as [A B ab ba], BC as [_ C bc cb].
pose proof Group.compose_homomorphism(homom:=ab)(homom2:=bc) as ac.
pose proof Group.compose_homomorphism(homom:=cb)(homom2:=ba)(groupH2:=ltac:(eapply A)) as ca.
split; try exact ac; try exact ca; try exact A; try exact C.
Unshelve.
all : try (pose (@PrimeFieldTheorems.F.Decidable_square p prime_p eq_refl); Decidable.vm_decide).
all : try (eapply Hierarchy.char_ge_weaken; try apply ModularArithmeticTheorems.F.char_gt; Decidable.vm_decide).
all : try (pose (@PrimeFieldTheorems.F.Decidable_square p prime_p eq_refl); vm_decide).
all : try (eapply Hierarchy.char_ge_weaken; try apply ModularArithmeticTheorems.F.char_gt; vm_decide).
Qed.
2 changes: 2 additions & 0 deletions src/Curves/Montgomery/Affine.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Module M.
Local Notation add := (M.add(b_nonzero:=b_nonzero)).
Local Notation point := (@M.point F Feq Fadd Fmul a b).

Global Instance Decidable_eq : Decidable.DecidableRel (@M.eq _ Feq Fadd Fmul a b) := _.

Section MontgomeryWeierstrass.
Local Notation "2" := (1+1).
Local Notation "3" := (1+2).
Expand Down
2 changes: 1 addition & 1 deletion src/Spec/Curve25519.v
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Module E.
refine (
exist _ (F.of_Z _ 15112221349535400772501151409588531511454012693041857206046113283949847762202, F.div (F.of_Z _ 4) (F.of_Z _ 5)) _).
Decidable.vm_decide.
Qed.
Defined.
End E.

Require Import Spec.MontgomeryCurve.
Expand Down
10 changes: 10 additions & 0 deletions src/Util/Decidable.v
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,22 @@ Global Instance dec_match_pair {A B} {P : A -> B -> Prop} {x : A * B}
: Decidable (let '(a, b) := x in P a b) | 1.
Proof. edestruct (_ : _ * _); assumption. Defined.

Global Instance dec_match_sum {A B} {P : A -> Prop} {Q : B -> Prop}
{HP : forall x, Decidable (P x)} {HQ : forall x, Decidable (Q x)}
{x : A + B}
: Decidable (match x with inl a => P a | inr b => Q b end) | 1.
Proof. edestruct (_ : _ + _); eauto. Defined.

Global Instance dec_if_bool {b : bool} {A B} {HA : Decidable A} {HB : Decidable B}
: Decidable (if b then A else B) | 10
:= if b return Decidable (if b then A else B)
then HA
else HB.

Global Instance dec_match_unit {P} {HP : Decidable P} {x : unit}
: Decidable (match x with tt => P end) | 1.
Proof. edestruct x. assumption. Defined.

Lemma not_not P {d:Decidable P} : not (not P) <-> P.
Proof. destruct (dec P); intuition. Qed.

Expand Down

0 comments on commit 841cc2f

Please sign in to comment.