Files
lean-tutorial-hashcloak/HashcloakTutorial.lean

74 lines
1.6 KiB
Lean4
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Mathlib.Data.ZMod.Basic
#eval 1 + 23
#eval (5: ZMod 7) + (6: ZMod 7)
abbrev BitString (L : ℕ) : Type := Vector (ZMod 2) L
namespace BitString
def xor {L : ℕ} (x y : BitString L) : BitString L :=
Vector.zipWith (fun a b => a + b) x y
#eval xor #[0, 1, 0, 1, 0].toVector #[1, 1, 0, 1, 1].toVector
lemma xor_comm_property {L : ℕ} (x y : BitString L) : xor x y = xor y x :=
by
apply Vector.ext
intro i ih
simp[xor]
simp[add_comm]
lemma xorg_assoc_property {L : ℕ} (x y z : BitString L) : xor x (xor y z) = xor (xor x y) z :=
by
apply Vector.ext
intro i iltL
simp[xor, add_assoc]
#eval (Vector.replicate 5 0: BitString 5)
def BitString_ID {L : ℕ} : BitString L := Vector.replicate L 0
lemma xor_show_identity {L : ℕ} (x: BitString L) : xor x BitString_ID = x :=
Vector.ext fun i i_lt_L => by
simp[xor]
simp[BitString_ID]
lemma xor_self_inverse {L : ℕ} (x : BitString L) : xor x x = BitString_ID := by
apply Vector.ext
intro i i_lt_L
simp[xor]
simp[BitString_ID]
exact CharTwo.add_eq_zero.mpr rfl
end BitString
structure ShannonCipher (K M C : Type) where
encode : K -> M -> C
decode : K -> C -> M
correctness : ∀ (k: K) (m: M), decode k (encode k m) = m
#check ShannonCipher Nat String Bool
def OneTimePad (L : ℕ) : ShannonCipher (BitString L) (BitString L) (BitString L) :=
{
encode := fun k m => BitString.xor k m,
decode := fun k c => BitString.xor k c,
correctness := open BitString in by
simp[xorg_assoc_property]
simp[xor_self_inverse]
simp[xor_comm_property]
simp[xor_show_identity]
}