-
Notifications
You must be signed in to change notification settings - Fork 184
feat(Crypto/Primitives/ECC): Edwards Curves #809
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
Open
chris-anto-froeschl
wants to merge
8
commits into
leanprover:main
Choose a base branch
from
chris-anto-froeschl:curve-primitives
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b9bef92
adds new Crypto/Primitives/ECC directory with basic twisted Edwards c…
chris-anto-froeschl cd56149
adds def of edwardsCurve to TwistedEdwardsCurve.lean
chris-anto-froeschl fb3cc35
adds generic EdwardsCurve notions derived from TwistedEdwardsCurve
chris-anto-froeschl 2b9de9b
adds concrete references to ECC notions
chris-anto-froeschl 59ce03d
removes EdwardsCurve.lean while moving useful defs and dropping ofD
chris-anto-froeschl 11c33ca
removes simp annotation from lemmas
chris-anto-froeschl e5562c2
updates comments to mention ultimate mathlib destination, references …
chris-anto-froeschl d5215c6
relabels most of the results to lemmas instead of theorems
chris-anto-froeschl 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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /- | ||
| Copyright (c) 2026 Chris Anto Fröschl. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Chris Anto Fröschl | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Cslib.Init |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /- | ||
| Copyright (c) 2026 Chris Anto Fröschl. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Chris Anto Fröschl | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Cslib.Crypto.Primitives.ECC.Basic | ||
| public import Mathlib.Algebra.Ring.Commute | ||
| public import Mathlib.Data.Set.Defs | ||
|
|
||
| /-! | ||
| # Twisted Edwards curves | ||
|
|
||
| This file contains the curve-level definitions that are independent of any specific Elligator. | ||
| A twisted Edwards curve with coefficients `a` and `d` has affine equation | ||
| `a * x ^ 2 + y ^ 2 = 1 + d * x ^ 2 * y ^ 2`. | ||
|
|
||
| The definitions are made over a commutative ring. Finiteness and the hypotheses used by a | ||
| particular cryptographic construction belong in that construction, rather than in the definition | ||
| of a curve or its affine points. | ||
|
|
||
| Mathlib's elliptic-curve API is currently centred on Weierstrass models. A twisted Edwards model | ||
| is not itself a Weierstrass equation, so using `WeierstrassCurve.Affine.Equation` here would require | ||
| a birational coordinate conversion and extra invertibility hypotheses. The API below follows the | ||
| same useful separation as that API: coefficients, an affine equation, a set of affine points, and | ||
| a bundled point type. | ||
|
|
||
| ## Main definitions | ||
|
|
||
| * `TwistedEdwardsCurve`: the coefficients `a`, `d` of a twisted Edwards model, with its equation | ||
| `TwistedEdwardsCurve.Equation`, its affine points `TwistedEdwardsCurve.affinePoints` and the | ||
| nonsingularity condition `TwistedEdwardsCurve.IsValid`. | ||
| * `edwardsCurve d`: the untwisted Edwards curve `x ^ 2 + y ^ 2 = 1 + d * x ^ 2 * y ^ 2`. | ||
|
|
||
| ## TODO | ||
|
|
||
| - Move into mathlib next to Mathlib.AlgebraicGeometry.EllipticCurve.Weierstrass | ||
|
|
||
| ## References | ||
|
|
||
| * [Bernstein2007a], Section 2. | ||
| * [Bernstein2008a], Section 2, Definition 2.1. | ||
|
|
||
| -/ | ||
|
|
||
| @[expose] public section | ||
| namespace Cslib.Crypto.Primitives.ECC | ||
|
|
||
| /-- Coefficients of the twisted Edwards equation | ||
| `a * x ^ 2 + y ^ 2 = 1 + d * x ^ 2 * y ^ 2`. -/ | ||
| @[ext] | ||
| structure TwistedEdwardsCurve (R : Type*) where | ||
| /-- left hand side coefficient -/ | ||
| a : R | ||
| /-- right hand side coefficient -/ | ||
| d : R | ||
|
|
||
| variable {R : Type*} [CommRing R] | ||
|
|
||
| namespace TwistedEdwardsCurve | ||
|
|
||
| /-- The proposition that `(x, y)` is an affine point of a twisted Edwards curve. -/ | ||
| def Equation (E : TwistedEdwardsCurve R) (x y : R) : Prop := | ||
| E.a * x ^ 2 + y ^ 2 = 1 + E.d * x ^ 2 * y ^ 2 | ||
|
|
||
| /-- The set of affine coordinate pairs on a twisted Edwards curve. -/ | ||
| def affinePoints (E : TwistedEdwardsCurve R) : Set (R × R) := {p | E.Equation p.1 p.2} | ||
|
|
||
| /-- A bundled affine point on a twisted Edwards curve. -/ | ||
| abbrev Point (E : TwistedEdwardsCurve R) := {p : R × R // p ∈ E.affinePoints} | ||
|
|
||
| /-- The neutral affine coordinate pair `(0, 1)`. It lies on every twisted Edwards equation. -/ | ||
| def zero : R × R := (0, 1) | ||
|
|
||
| lemma zero_mem_affinePoints (E : TwistedEdwardsCurve R) : zero ∈ E.affinePoints := by | ||
| change E.a * 0 ^ 2 + 1 ^ 2 = 1 + E.d * 0 ^ 2 * 1 ^ 2 | ||
| simp | ||
|
|
||
| /-- The neutral point, bundled as an affine point of `E`. -/ | ||
| def zeroPoint (E : TwistedEdwardsCurve R) : E.Point := ⟨zero, E.zero_mem_affinePoints⟩ | ||
|
|
||
| /-- Negation of affine coordinates on a twisted Edwards curve. -/ | ||
| def neg (p : R × R) : R × R := (-p.1, p.2) | ||
|
|
||
| lemma neg_mem_affinePoints (E : TwistedEdwardsCurve R) (p : R × R) : | ||
| neg p ∈ E.affinePoints ↔ p ∈ E.affinePoints := by | ||
| change E.a * (-p.1) ^ 2 + p.2 ^ 2 = 1 + E.d * (-p.1) ^ 2 * p.2 ^ 2 ↔ | ||
| E.a * p.1 ^ 2 + p.2 ^ 2 = 1 + E.d * p.1 ^ 2 * p.2 ^ 2 | ||
| rw [neg_sq] | ||
|
|
||
| /-- The usual coefficient conditions for a nonsingular twisted Edwards model over a field. | ||
| Keeping this predicate separate from `TwistedEdwardsCurve` permits the equation and its points to | ||
| be used over more general rings and also permits partially specified curves during developments. | ||
| -/ | ||
| def IsValid (E : TwistedEdwardsCurve R) : Prop := E.a ≠ 0 ∧ E.d ≠ 0 ∧ E.a ≠ E.d | ||
|
|
||
| end TwistedEdwardsCurve | ||
|
|
||
| /-- The (untwisted) Edwards curve with coefficient `d`, obtained by setting `a = 1`. -/ | ||
| def edwardsCurve (d : R) : TwistedEdwardsCurve R where | ||
| a := 1 | ||
| d := d | ||
|
|
||
| /-- The equation of `edwardsCurve d`, written out. -/ | ||
| lemma edwardsCurve_equation_iff (d x y : R) : | ||
| (edwardsCurve d).Equation x y ↔ x ^ 2 + y ^ 2 = 1 + d * x ^ 2 * y ^ 2 := by | ||
| simp [TwistedEdwardsCurve.Equation, edwardsCurve] | ||
|
|
||
| /-- `edwardsCurve d` is a valid (nonsingular) model exactly when `d ≠ 0` and `d ≠ 1`. -/ | ||
| lemma edwardsCurve_isValid_iff [Nontrivial R] (d : R) : | ||
| (edwardsCurve d).IsValid ↔ d ≠ 0 ∧ d ≠ 1 := by | ||
| constructor | ||
| · rintro ⟨_, hd, had⟩ | ||
| exact ⟨hd, fun h ↦ had h.symm⟩ | ||
| · rintro ⟨hd, hd1⟩ | ||
| exact ⟨one_ne_zero, hd, fun h ↦ hd1 h.symm⟩ | ||
|
|
||
| end Cslib.Crypto.Primitives.ECC | ||
This file contains hidden or 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.
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.
This is probably the best @[simp] lemma here, but my preference is to remove the @[simp] lemmata and add them back when they have users and where it is obvious how they justify themselves.
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.
Fixed in 11c33ca.