Skip to content

Commit 5e8d843

Browse files
committed
Migrate PublicKeyEncryption.lean to Lean 4
1 parent b5470bc commit 5e8d843

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

Cryptolib/PublicKeyEncryption.lean

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,70 @@
11
/-
2-
-----------------------------------------------------------
3-
Security games as pmfs
4-
-----------------------------------------------------------
2+
Copyright (c) 2021 Joey Lupo. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Joey Lupo
55
-/
6+
import Cryptolib.ToMathlib
7+
import Cryptolib.Uniform
8+
import Mathlib.Data.ZMod.Basic
9+
import Mathlib.Probability.Distributions.Uniform
610

7-
import data.zmod.basic
8-
import probability.probability_mass_function.basic
9-
import to_mathlib
10-
import uniform
11+
/-!
12+
# Security games as PMFs
1113
12-
noncomputable theory
14+
This file provides formal definitions for correctness and semantic security of a public
15+
key encryption scheme.
16+
-/
17+
18+
noncomputable section
1319

1420
/-
15-
G1 = public key space, G2 = private key space,
16-
M = message space, C = ciphertext space
17-
A_state = type for state A1 passes to A2
21+
G1 = public key space, G2 = private key space,
22+
M = message space, C = ciphertext space
23+
A_state = type for state A1 passes to A2
1824
-/
19-
variables {G1 G2 M C A_state: Type} [decidable_eq M]
20-
(keygen : pmf (G1 × G2))
21-
(encrypt : G1 → M → pmf C)
22-
(decrypt : G2 → C → M)
23-
(A1 : G1 → pmf (M × M × A_state))
24-
(A2 : C → A_state → pmf (zmod 2)) -- Should have G1 else how can A2 do further encryptions? Any general result about encryptions before getting challenge ciphertext...?
25+
variable {G1 G2 M C A_state: Type} [DecidableEq M]
26+
(keygen : PMF (G1 × G2))
27+
(encrypt : G1 → M → PMF C)
28+
(decrypt : G2 → C → M)
29+
(A1 : G1 → PMF (M × M × A_state))
30+
(A2 : C → A_state → PMF (ZMod 2)) -- Should have G1 else how can A2 do further encryptions? Any general result about encryptions before getting challenge ciphertext...?
2531

2632
/-
27-
Executes the a public-key protocol defined by keygen,
28-
encrypt, and decrypt
33+
Executes a public-key protocol defined by keygen,
34+
encrypt, and decrypt
2935
-/
3036
-- Simulates running the program and returns 1 with prob 1 if:
3137
-- `Pr[D(sk, E(pk, m)) = m] = 1` holds
32-
def enc_dec (m : M) : pmf (zmod 2) := -- given a message m
38+
def enc_dec (m : M) : PMF (ZMod 2) := -- given a message m
3339
do
34-
k ← keygen, -- produces a public / secret key pair
35-
c ← encrypt k.1 m, -- encrypts m using pk
40+
let k ← keygen -- produces a public / secret key pair
41+
let c ← encrypt k.1 m -- encrypts m using pk
3642
pure (if decrypt k.2 c = m then 1 else 0) -- decrypts using sk and checks for equality with m
3743

3844
/-
39-
A public-key encryption protocol is correct if decryption undoes
40-
encryption with probability 1
45+
A public-key encryption protocol is correct if decryption undoes
46+
encryption with probability 1
4147
-/
4248

43-
def pke_correctness : Prop := ∀ (m : M), enc_dec keygen encrypt decrypt m = pure 1 -- This chain of encryption/decryption matches the monadic actions in the `enc_dec` function
49+
/-
50+
This chain of encryption/decryption matches the monadic actions in the `enc_dec` function
51+
-/
52+
def PkeCorrectness : Prop := ∀ (m : M), enc_dec keygen encrypt decrypt m = pure 1
4453

4554
/-
46-
The semantic security game.
47-
Returns 1 if the attacker A2 guesses the correct bit
55+
The semantic security game.
56+
Returns 1 if the attacker A2 guesses the correct bit
4857
-/
49-
def SSG : pmf (zmod 2):=
58+
def SSG : PMF (ZMod 2) :=
5059
do
51-
k ← keygen,
52-
m ← A1 k.1,
53-
b ← uniform_2,
54-
c ← encrypt k.1 (if b = 0 then m.1 else m.2.1),
55-
b' ← A2 c m.2.2,
60+
let k ← keygen
61+
let m ← A1 k.1
62+
let b ← uniform_2
63+
let c ← encrypt k.1 (if b = 0 then m.1 else m.2.1)
64+
let b' ← A2 c m.2.2
5665
pure (1 + b + b')
5766

5867
-- SSG(A) denotes the event that A wins the semantic security game
59-
local notation `Pr[SSG(A)]` := (SSG keygen encrypt A1 A2 1)
68+
local notation "Pr[SSG(A)]" => (SSG keygen encrypt A1 A2 1)
6069

61-
def pke_semantic_security (ε : nnreal) : Prop := abs (Pr[SSG(A)] - 1/2) ≤ ε
70+
def PkeSemanticSecurity (ε : NNReal) : Prop := abs (Pr[SSG(A)].toReal - 1/2) ≤ ε

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ This library is a work-in-progress merge, port, and refactor of several forks of
1818

1919
- `negligible.lean` - defines negligible functions and provides several useful lemmas regarding negligible functions
2020

21-
- [pke.lean](src/pke.lean) - provides formal definitions for correctness and semantic security of a public key encryption scheme
22-
2321
- [tactics.lean](src/tactics.lean) - provides the `bindskip` and `bindconst` tactics to help prove equivalences between pmfs
2422

2523
- [rsa.lean](src/rsa.lean) - contains proof of correctness of the RSA public key encryption protocol

0 commit comments

Comments
 (0)