{-# OPTIONS --copatterns #-}
module Attack.Compression where
open import Type using (★)
open import Function
open import Data.Nat.NP
open import Data.Two renaming (_==_ to _==ᵇ_)
open import Data.Product
open import Data.Zero
open import Relation.Binary.PropositionalEquality.NP
import Game.IND-CPA
record Sized (A : ★) : ★ where
field
size : A → ℕ
open Sized {{...}}
module _ {A B} {{_ : Sized A}} {{_ : Sized B}} where
_==ˢ_ : A → B → 𝟚
x ==ˢ y = size x == size y
_≡ˢ_ : A → B → ★
x ≡ˢ y = size x ≡ size y
≡ˢ→==ˢ : ∀ {x y} → x ≡ˢ y → (x ==ˢ y) ≡ 1₂
≡ˢ→==ˢ {x} {y} x≡ˢy rewrite x≡ˢy = ✓→≡ (==.refl {size y})
==ˢ→≡ˢ : ∀ {x y} → (x ==ˢ y) ≡ 1₂ → x ≡ˢ y
==ˢ→≡ˢ p = ==.sound _ _ (≡→✓ p)
module _ {PubKey Message CipherText Rₑ : ★}
(Enc : PubKey → Message → Rₑ → CipherText)
{{_ : Sized Message}}
{{_ : Sized CipherText}}
where
EncSizeRndInd =
∀ {pk m r₀ r₁} → Enc pk m r₀ ≡ˢ Enc pk m r₁
EncLeakSize =
∀ {pk m₀ m₁ r₀ r₁} → Enc pk m₀ r₀ ≡ˢ Enc pk m₁ r₁ → m₀ ≡ˢ m₁
module M
{Message CompressedMessage : ★}
{{_ : Sized CompressedMessage}}
(compress : Message → CompressedMessage)
(m₀ m₁ : Message)
(different-compression
: size (compress m₀) ≢ size (compress m₁))
(PubKey : ★)
(SecKey : ★)
(CipherText : ★)
{{_ : Sized CipherText}}
(Rₑ Rₖ Rₓ : ★)
(KeyGen : Rₖ → PubKey × SecKey)
(Enc₀ : PubKey → CompressedMessage → Rₑ → CipherText)
(Enc₀SizeRndInd : EncSizeRndInd Enc₀)
(Enc₀LeakSize : EncLeakSize Enc₀)
where
Rₐ = Rₑ
Enc₁ : PubKey → Message → Rₑ → CipherText
Enc₁ pk m rₑ = Enc₀ pk (compress m) rₑ
module IND-CPA = Game.IND-CPA PubKey SecKey Message CipherText
Rₑ Rₖ Rₐ Rₓ KeyGen Enc₁
open IND-CPA.Adversary
A : IND-CPA.Adversary
m A = λ _ _ → [0: m₀ 1: m₁ ]
b′ A = λ rₑ pk c → c ==ˢ Enc₁ pk m₁ rₑ
A-always-wins : ∀ b r → IND-CPA.EXP b A r ≡ b
A-always-wins 0₂ _ = ≢1→≡0 (different-compression ∘ Enc₀LeakSize ∘ ==ˢ→≡ˢ)
A-always-wins 1₂ _ = ≡ˢ→==ˢ Enc₀SizeRndInd
lem : ∀ x y → (x ==ᵇ y) ≡ 0₂ → not (x ==ᵇ y) ≡ 1₂
lem 1₂ 1₂ = λ ()
lem 1₂ 0₂ = λ _ → refl
lem 0₂ 1₂ = λ _ → refl
lem 0₂ 0₂ = λ ()