-
Notifications
You must be signed in to change notification settings - Fork 3
Port to Lean 4 #2
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
Draft
str4d
wants to merge
12
commits into
main
Choose a base branch
from
port-to-lean4
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.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5a74cd5
Migrate `ToMathlib.lean` to Lean 4
str4d 5564a71
Migrate `Uniform.lean` to Lean 4
str4d 225aa8b
Migrate `Negligible.lean` to Lean 4
str4d 5aa3d8d
Move Apache 2.0 Lean 3 files into Lean 4 codebase
str4d 6f8668e
Migrate `PublicKeyEncryption.lean` to Lean 4
str4d 4f6c3f7
Migrate `DecisionalDiffieHellman.lean` to Lean 4
str4d 0ed9639
Migrate `Tactic.lean` to Lean 4
str4d 435fca9
Migrate `ElGamal.lean` to Lean 4
str4d 28943ed
Migrate `ComputationalDiffieHellman.lean` to Lean 4
str4d 5934f46
Migrate `DiscreteLog.lean` to Lean 4
str4d ad8bc1d
Migrate `Commitments.lean` to Lean 4
str4d 9f810ec
Migrate `Pedersen.lean` to Lean 4
str4d 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,39 @@ | ||
| /- | ||
| Copyright (c) 2023 Ashley Blacquiere | ||
| Released under either MIT or Apache 2.0 as described in the file README.md. | ||
| Authors: Ashley Blacquiere | ||
| -/ | ||
| import Cryptolib.ToMathlib | ||
| import Cryptolib.Uniform | ||
| import Mathlib.Probability.Distributions.Uniform | ||
|
|
||
| /-! | ||
| # The computational Diffie-Hellman assumption as a security game | ||
| -/ | ||
|
|
||
| noncomputable section | ||
|
|
||
| section CDH | ||
|
|
||
| variable (G : Type) [Fintype G] [Group G] | ||
| (g : G) (g_gen_G : ∀ (x : G), x ∈ Subgroup.zpowers g) | ||
| (q : ℕ) [NeZero q] (G_card_q : Fintype.card G = q) | ||
| (A : G → G → PMF G) | ||
|
|
||
| include g_gen_G G_card_q -- assumptions used in the game reduction | ||
|
|
||
| def CDH : PMF (ZMod 2) := | ||
| do | ||
| let α ← uniform_zmod q | ||
| let β ← uniform_zmod q | ||
| let ω := g^(α.val * β.val) | ||
| let ω' ← A (g^α.val) (g^β.val) | ||
| pure (if ω = ω' then 1 else 0) -- ?? | ||
|
|
||
| -- CDHadv[A] is the probability that ω = ω' | ||
| -- Should CDH be a Prop? Not sure how to get both ω and ω' out to compare outside of the def | ||
| local notation "CDHadv[A]" => (CDH G g g_gen_G q G_card_q A) | ||
|
|
||
| #check CDH G g /- g_gen_G -/ q /- G_card_q -/ A | ||
|
|
||
| end CDH |
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,52 @@ | ||
| /- | ||
| Copyright (c) 2021 Joey Lupo | ||
| Released under Apache 2.0 license as described in the file LICENSE-APACHE. | ||
| Authors: Joey Lupo | ||
| -/ | ||
| import Cryptolib.ToMathlib | ||
| import Cryptolib.Uniform | ||
| import Mathlib.Probability.Distributions.Uniform | ||
|
|
||
| /-! | ||
| # The decisional Diffie-Hellman assumption as a security game | ||
|
|
||
| This file provides a formal specification of the decisional Diffie-Hellman assumption on a | ||
| finite cyclic group. | ||
| -/ | ||
|
|
||
| noncomputable section | ||
|
|
||
| section DDH | ||
|
|
||
| variable (G : Type) [Fintype G] [Group G] | ||
| (g : G) --(g_gen_G : ∀ (x : G), x ∈ Subgroup.zpowers g) | ||
| (q : ℕ) [NeZero q] --(G_card_q : Fintype.card G = q) | ||
| -- check Mario, 0 < q necessary for Fintype.card? | ||
| (D : G → G → G → PMF (ZMod 2)) | ||
|
|
||
| -- include g_gen_G G_card_q | ||
|
|
||
| def DDH0 : PMF (ZMod 2) := | ||
| do | ||
| let x ← uniform_zmod q | ||
| let y ← uniform_zmod q | ||
| let b ← D (g^x.val) (g^y.val) (g^(x.val * y.val)) | ||
| pure b | ||
|
|
||
| def DDH1 : PMF (ZMod 2) := | ||
| do | ||
| let x ← uniform_zmod q | ||
| let y ← uniform_zmod q | ||
| let z ← uniform_zmod q | ||
| let b ← D (g^x.val) (g^y.val) (g^z.val) | ||
| pure b | ||
|
|
||
| -- DDH0(D) is the event that D outputs 1 upon receiving (g^x, g^y, g^(xy)) | ||
| local notation "Pr[DDH0(D)]" => (DDH0 G g q D 1) | ||
|
|
||
| -- DDH1(D) is the event that D outputs 1 upon receiving (g^x, g^y, g^z) | ||
| local notation "Pr[DDH1(D)]" => (DDH1 G g q D 1) | ||
|
|
||
| def DDH (ε : NNReal) : Prop := abs (Pr[DDH0(D)].toReal - Pr[DDH1(D)].toReal) ≤ ε | ||
|
|
||
| end DDH | ||
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,47 @@ | ||
| /- | ||
| Copyright (c) 2023 Ashley Blacquiere | ||
| Released under either MIT or Apache 2.0 as described in the file README.md. | ||
| Authors: Ashley Blacquiere | ||
| -/ | ||
| import Cryptolib.ToMathlib | ||
| import Cryptolib.Uniform | ||
| import Mathlib.Data.ZMod.Basic | ||
| import Mathlib.Probability.Distributions.Uniform | ||
|
|
||
| noncomputable section | ||
|
|
||
| section DL | ||
|
|
||
| variable (G : Type) [Fintype G] [Group G] [DecidableEq G] | ||
| (g : G) (g_gen_G : ∀ (x : G), x ∈ Subgroup.zpowers g) | ||
| (q : ℕ) [NeZero q] (G_card_q : Fintype.card G = q) | ||
| (A : G → PMF (ZMod q)) | ||
|
|
||
| include g_gen_G G_card_q -- assumptions used in the game reduction | ||
|
|
||
| -- let α ← uniform_zmod q | ||
| -- let u := g^α.val | ||
| -- let α' ← A u | ||
|
|
||
| def DL /- (h : G) -/ : PMF (ZMod 2) := | ||
| do | ||
| let α ← uniform_zmod q | ||
| let u := g^α.val | ||
| let α' ← A u | ||
| pure (if α = α' then 1 else 0) | ||
|
|
||
| -- -- From DDH: | ||
| -- -- DDH0(D) is the event that D outputs 1 upon receiving (g^x, g^y, g^(xy)) | ||
| -- local notation "Pr[DDH0(D)]" => (DDH0 G g g_gen_G q G_card_q D 1) | ||
|
|
||
| -- -- DDH1(D) is the event that D outputs 1 upon receiving (g^x, g^y, g^z) | ||
| -- local notation "Pr[DDH1(D)]" => (DDH1 G g g_gen_G q G_card_q D 1) | ||
|
|
||
| local notation "Pr[DL(A)]" => (DL G g /- g_gen_G -/ q /- G_card_q -/ A 1) | ||
|
|
||
| def DLadv (ε: NNReal) : Prop := abs (Pr[DL(A)].toReal - 1/2) ≤ ε | ||
|
|
||
|
|
||
| #check DL G g /- g_gen_G -/ q /- G_card_q -/ A | ||
|
|
||
| end DL |
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.
Tentatively I think we usually want to be more careful about the arguments of reusable definitions than (as far as I know) is possible by use of
variable. In particular, we want to have precise control over whether a given argument is explicit or implicit. It's useful for an argument to be implicit when it can be reliably inferred, and explicit otherwise. Declaring it as a variable forces it to be always explicit or (I think this is also supported) always implicit.The example I've come across in my experimentation is with elliptic curves and elliptic curve points. To get a reasonably ergonomic abstraction for elliptic curve points, you really want the curve and its field of definition to be implicit in functions that take curve points: they can reliably be inferred from the type of the point(s), and it would be horribly verbose to have to specify them. But when constructing a curve, you want the field (and any constraints on it, e.g. on the characteristic) to be explicit I think.
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.
I may have been wrong here. The following approach ends up working well in practice:
variableto specify a background context (for example the field of definition in the case of curves).Addthey correctly constrain the arguments to be of the same type including context).You end up not having to use implicits much (but they're there if you need them). It's really well thought out.