-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstream.lean
More file actions
57 lines (43 loc) · 1.12 KB
/
stream.lean
File metadata and controls
57 lines (43 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/-
-----------------------------------------------------------
Formalization and correctness of stream ciphers
-----------------------------------------------------------
-/
import data.bitvec.basic
import data.zmod.basic
import to_mathlib
-- a single bit encryption/descryption stream cipher
namespace stream_bit
-- a random single key bit
variable k : zmod 2
-- x is a plain text bit and y is a cipher text bit
variables x y : zmod 2
def encrypt : zmod 2 := x + k
def decrypt : zmod 2 := y + k
-- proof of correctness
lemma dec_undoes_enc :
x = decrypt k (encrypt k x) :=
begin
unfold encrypt,
unfold decrypt,
ring_nf,
finish,
end
end stream_bit
-- a vector of bits encryption/descryption stream cipher
namespace stream_bits
-- size of the text and the key
variable n : nat
-- xv = plaintext, yv = ciphertext, kv = keystream (all size n)
variables xv yv kv: bitvec n
def encrypt : bitvec n := xv + kv
def decrypt : bitvec n := yv + kv
-- proof of correctness
lemma dec_undoes_enc :
xv = decrypt n kv (encrypt n kv xv) :=
begin
unfold encrypt,
unfold decrypt,
rw ←bitvec.add_self_assoc,
end
end stream_bits